fix(resolver): fix a case with multi-dot file extensions (#704)

This commit is contained in:
Boshen 2023-08-09 18:31:17 +08:00 committed by GitHub
parent 09761b4f8b
commit 341c678b2f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 0 deletions

View file

@ -350,6 +350,13 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
if let Some(path) = self.load_alias_or_file(&cached_path, ctx)? {
return Ok(Some(path));
}
// `std::path::PathBuf::set_extension` only removes 1 dot.
// This removes multi-dot extensions such as `.d.ts`.
let num_dots = extension.chars().filter(|c| *c == '.').count();
for _ in 0..num_dots {
path_with_extension.set_extension("");
}
}
Ok(None)
}

View file

@ -73,3 +73,25 @@ fn respect_enforce_extension() {
assert_eq!(resolved.map(Resolution::into_path_buf), Ok(f.join("foo.ts")));
// TODO: need to match missingDependencies returned from the resolve function
}
// Test for `.d.ts`, not part of enhanced-resolve.
#[test]
fn multi_dot_extension() {
let f = fixture();
let resolver = Resolver::new(ResolveOptions {
extensions: vec![".a.b.c".into(), ".d.ts".into(), ".ts".into(), ".js".into()],
..ResolveOptions::default()
});
#[rustfmt::skip]
let pass = [
("should resolve according to order of provided extensions", "./foo", "foo.ts"),
];
for (comment, request, expected_path) in pass {
let resolved_path = resolver.resolve(&f, request).map(|r| r.full_path());
let expected = f.join(expected_path);
assert_eq!(resolved_path, Ok(expected), "{comment} {request} {expected_path}");
}
}