Skip to content

Commit 99cb3dd

Browse files
authored
refactor: add test cases for resolve alias value with fragment (#170)
1 parent 3ba7638 commit 99cb3dd

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

src/lib.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -880,22 +880,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
880880
AliasValue::Path(alias_value) => {
881881
let new_specifier =
882882
Specifier::parse(alias_value).map_err(ResolveError::Specifier)?;
883-
884-
// `#` can be a fragment or a path, try fragment as path first
885-
if new_specifier.query.is_none() && new_specifier.fragment.is_some() {
886-
if let Some(path) = self.load_alias_value(
887-
cached_path,
888-
alias_key,
889-
alias_value, // pass in original alias value, not parsed
890-
specifier,
891-
ctx,
892-
&mut should_stop,
893-
)? {
894-
return Ok(Some(path));
895-
}
896-
}
897-
898-
// Then try path without query and fragment
883+
// Resolve path without query and fragment
899884
let old_query = ctx.query.clone();
900885
let old_fragment = ctx.fragment.clone();
901886
ctx.with_query_fragment(new_specifier.query, new_specifier.fragment);

src/tests/alias.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,34 @@ fn all_alias_values_are_not_found() {
233233
let resolution = resolver.resolve(&f, "m1/a.js");
234234
assert_eq!(resolution, Err(ResolveError::NotFound("m1/a.js".to_string())));
235235
}
236+
237+
#[test]
238+
fn alias_fragment() {
239+
let f = super::fixture();
240+
241+
let data = [
242+
// enhanced-resolve has `#` prepended with a `\0`, they are removed from the
243+
// following 3 expected test results.
244+
// See https://github.com/webpack/enhanced-resolve#escaping
245+
(
246+
"handle fragment edge case (no fragment)",
247+
"./no#fragment/#/#",
248+
f.join("no#fragment/#/#.js"),
249+
),
250+
("handle fragment edge case (fragment)", "./no#fragment/#/", f.join("no.js#fragment/#/")),
251+
(
252+
"handle fragment escaping",
253+
"./no\0#fragment/\0#/\0##fragment",
254+
f.join("no#fragment/#/#.js#fragment"),
255+
),
256+
];
257+
258+
for (comment, request, expected) in data {
259+
let resolver = Resolver::new(ResolveOptions {
260+
alias: vec![("foo".to_string(), vec![AliasValue::Path(request.to_string())])],
261+
..ResolveOptions::default()
262+
});
263+
let resolved_path = resolver.resolve(&f, "foo").map(|r| r.full_path());
264+
assert_eq!(resolved_path, Ok(expected), "{comment} {request}");
265+
}
266+
}

0 commit comments

Comments
 (0)