refactor(resolver): remove the leading dot trim on extensions

This commit is contained in:
Boshen 2023-08-10 21:05:27 +08:00
parent 11954326b3
commit 17a6f63c31
No known key found for this signature in database
GPG key ID: 234DA6A7079C6801
2 changed files with 2 additions and 20 deletions

View file

@ -348,8 +348,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
}
for extension in extensions {
let mut path_with_extension = path.to_path_buf().into_os_string();
path_with_extension.reserve_exact(1 + extension.len());
path_with_extension.push(".");
path_with_extension.reserve_exact(extension.len());
path_with_extension.push(extension);
let path_with_extension = PathBuf::from(path_with_extension);
let cached_path = self.cache.value(&path_with_extension);
@ -679,7 +678,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
fn load_extension_alias(&self, cached_path: &CachedPath, ctx: &ResolveContext) -> ResolveState {
let Some(path_extension) = cached_path.path().extension() else { return Ok(None) };
let Some((_, extensions)) =
self.options.extension_alias.iter().find(|(ext, _)| OsStr::new(ext) == path_extension)
self.options.extension_alias.iter().find(|(ext, _)| OsStr::new(ext.trim_start_matches('.')) == path_extension)
else {
return Ok(None);
};

View file

@ -196,25 +196,8 @@ impl ResolveOptions {
self.extensions.retain(String::is_empty);
}
}
self.extensions = Self::remove_leading_dots(self.extensions);
self.extension_alias = self
.extension_alias
.into_iter()
.map(|(extension, extensions)| {
(Self::remove_leading_dot(&extension), Self::remove_leading_dots(extensions))
})
.collect();
self
}
// Remove the leading `.` because `Path::with_extension` does not accept the dot.
fn remove_leading_dot(s: &str) -> String {
s.trim_start_matches('.').to_string()
}
fn remove_leading_dots(v: Vec<String>) -> Vec<String> {
v.into_iter().map(|s| Self::remove_leading_dot(&s)).collect()
}
}
// For tracing