+
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions .changeset/petite-waves-love.md

This file was deleted.

11 changes: 1 addition & 10 deletions crates/biome_module_graph/src/js_module_info/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ use crate::{

use super::{ResolvedPath, collector::JsModuleInfoCollector};

/// Extensions to try to resolve based on the extension in the import specifier.
/// ref: https://www.typescriptlang.org/docs/handbook/modules/reference.html#the-moduleresolution-compiler-option
const EXTENSION_ALIAS: &[(&str, &[&str])] = &[
("js", &["ts", "tsx", "d.ts", "js", "jsx"]),
("mjs", &["mts", "d.mts", "mjs"]),
("cjs", &["cts", "d.cts", "cjs"]),
];

pub(crate) struct JsModuleVisitor<'a> {
root: AnyJsRoot,
directory: &'a Utf8Path,
Expand Down Expand Up @@ -401,7 +393,7 @@ impl<'a> JsModuleVisitor<'a> {
if let Ok(binding) = node.pattern() {
self.visit_binding_pattern(
binding,
collector,
collector,
);
}
}
Expand Down Expand Up @@ -450,7 +442,6 @@ impl<'a> JsModuleVisitor<'a> {
condition_names: &["types", "import", "default"],
default_files: &["index"],
extensions: SUPPORTED_EXTENSIONS,
extension_alias: EXTENSION_ALIAS,
resolve_node_builtins: true,
resolve_types: true,
..Default::default()
Expand Down
60 changes: 3 additions & 57 deletions crates/biome_resolver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ pub fn resolve(
}

if specifier.starts_with('/') {
return resolve_absolute_path_with_extension_alias(
Utf8PathBuf::from(specifier),
fs,
options,
);
return resolve_absolute_path(Utf8PathBuf::from(specifier), fs, options);
}

if is_relative_specifier(specifier) {
Expand All @@ -62,37 +58,6 @@ pub fn resolve(
resolve_module(specifier, base_dir, fs, options)
}

fn resolve_absolute_path_with_extension_alias(
path: Utf8PathBuf,
fs: &dyn ResolverFsProxy,
options: &ResolveOptions,
) -> Result<Utf8PathBuf, ResolveError> {
// Skip if no extension is in the path.
let Some(extension) = path.extension() else {
return resolve_absolute_path(path, fs, options);
};

// Skip if no extension alias is configured.
let Some(&(_, aliases)) = options
.extension_alias
.iter()
.find(|(ext, _)| *ext == extension)
else {
return resolve_absolute_path(path, fs, options);
};

// Try to resolve the path for each extension alias.
for alias in aliases {
match resolve_absolute_path(path.with_extension(alias), fs, options) {
Ok(path) => return Ok(path),
Err(ResolveError::NotFound) => { /* continue */ }
Err(error) => return Err(error),
}
}

Err(ResolveError::NotFound)
}

/// Resolves the given absolute `path`.
///
/// Absolute paths are those starting with `/` or a Windows prefix/root.
Expand Down Expand Up @@ -146,7 +111,7 @@ fn resolve_relative_path(
fs: &dyn ResolverFsProxy,
options: &ResolveOptions,
) -> Result<Utf8PathBuf, ResolveError> {
resolve_absolute_path_with_extension_alias(base_dir.join(path), fs, options)
resolve_absolute_path(base_dir.join(path), fs, options)
}

/// Resolve the directory `dir_path`.
Expand Down Expand Up @@ -755,16 +720,9 @@ pub struct ResolveOptions<'a> {
/// Extensions are checked in the order given, meaning the first extension
/// in the list has the highest priority.
///
/// Extensions should be provided without a leading dot.
/// Extensions should be provided without leading dot.
pub extensions: &'a [&'a str],

/// List of extension aliases to search for in absolute or relative paths.
/// Typically used to resolve `.ts` files by `.js` extension.
/// Same behavior as the `extensionAlias` option in [enhanced-resolve](https://github.com/webpack/enhanced-resolve?tab=readme-ov-file#resolver-options).
///
/// Extensions should be provided without a leading dot.
pub extension_alias: &'a [(&'a str, &'a [&'a str])],

/// Defines which `package.json` file should be used.
///
/// See [`DiscoverableManifest`] for more details.
Expand Down Expand Up @@ -834,7 +792,6 @@ impl<'a> ResolveOptions<'a> {
condition_names: &[],
default_files: &[],
extensions: &[],
extension_alias: &[],
package_json: DiscoverableManifest::Auto,
resolve_node_builtins: false,
resolve_types: false,
Expand Down Expand Up @@ -873,15 +830,6 @@ impl<'a> ResolveOptions<'a> {
self
}

/// Sets [`Self::extension_alias`] and returns this instance.
pub const fn with_extension_alias(
mut self,
extension_alias: &'a [(&'a str, &'a [&'a str])],
) -> Self {
self.extension_alias = extension_alias;
self
}

/// Sets [`Self::package_json`] and returns this instance.
pub fn with_package_json(
mut self,
Expand Down Expand Up @@ -921,7 +869,6 @@ impl<'a> ResolveOptions<'a> {
condition_names: self.condition_names,
default_files: self.default_files,
extensions: self.extensions,
extension_alias: self.extension_alias,
package_json: DiscoverableManifest::Off,
resolve_node_builtins: self.resolve_node_builtins,
resolve_types: self.resolve_types,
Expand All @@ -936,7 +883,6 @@ impl<'a> ResolveOptions<'a> {
condition_names: self.condition_names,
default_files: self.default_files,
extensions: &[],
extension_alias: &[],
package_json: DiscoverableManifest::Off,
resolve_node_builtins: self.resolve_node_builtins,
resolve_types: self.resolve_types,
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
62 changes: 0 additions & 62 deletions crates/biome_resolver/tests/spec_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,65 +533,3 @@ fn test_resolve_type_definitions_without_type_specification() {
)))
);
}

#[test]
fn test_resolve_extension_alias() {
let base_dir = get_fixtures_path("resolver_cases_7");
let fs = OsFileSystem::new(base_dir.clone());

let options = ResolveOptions {
default_files: &["index"],
extensions: &["js"],
extension_alias: &[("js", &["ts", "js"]), ("mjs", &["mts"])],
..Default::default()
};

assert_eq!(
resolve("./index.js", &base_dir, &fs, &options),
Ok(Utf8PathBuf::from(format!("{base_dir}/index.ts"))),
"should alias fully specified file",
);

assert_eq!(
resolve("./dir/index.js", &base_dir, &fs, &options),
Ok(Utf8PathBuf::from(format!("{base_dir}/dir/index.ts"))),
"should alias fully specified file when there are two alternatives",
);

assert_eq!(
resolve("./dir2/index.js", &base_dir, &fs, &options),
Ok(Utf8PathBuf::from(format!("{base_dir}/dir2/index.js"))),
"should also allow the second alternative",
);

assert_eq!(
resolve("./dir2/index.mjs", &base_dir, &fs, &options),
Ok(Utf8PathBuf::from(format!("{base_dir}/dir2/index.mts"))),
"should support alias option without an array",
);
}

#[test]
fn test_resolve_extension_alias_not_apply_to_extension_nor_main_files() {
let base_dir = get_fixtures_path("resolver_cases_7");
let fs = OsFileSystem::new(base_dir.clone());

let options = ResolveOptions {
default_files: &["index"],
extensions: &["js"],
extension_alias: &[("js", &[])],
..Default::default()
};

assert_eq!(
resolve("./dir2", &base_dir, &fs, &options),
Ok(Utf8PathBuf::from(format!("{base_dir}/dir2/index.js"))),
"directory",
);

assert_eq!(
resolve("./dir2/index", &base_dir, &fs, &options),
Ok(Utf8PathBuf::from(format!("{base_dir}/dir2/index.js"))),
"file",
);
}
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载