Skip to content

Commit

Permalink
chore: improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
minht11 committed May 11, 2024
1 parent af2590a commit 4bd8041
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions crates/biome_js_analyze/src/lint/nursery/use_import_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,19 @@ fn get_extensionless_import(
let module_name_token = node.module_name_token()?;
let module_path = inner_string_text(&module_name_token);
let path = Path::new(module_path.text());
let mut path_components = path.components();
let first_component = path_components.next()?;

if !matches!(
path.components().next(),
Some(Component::CurDir | Component::ParentDir)
) || path.extension().is_some()
if !matches!(first_component, Component::CurDir | Component::ParentDir)
|| path.extension().is_some()
{
return None;
}

let has_query_or_hash = path
.components()
.last()
.and_then(|c| c.as_os_str().to_str())
let last_component = path_components.last().unwrap_or(first_component);
let has_query_or_hash = last_component
.as_os_str()
.to_str()
.map_or(false, |last| last.contains('?') || last.contains('#'));

if has_query_or_hash {
Expand All @@ -159,14 +159,12 @@ fn get_extensionless_import(
let import_ext = resolve_import_extension(file_ext, path);
let mut path_buf = path.to_path_buf();

let is_index_file = match path.components().last() {
Some(Component::ParentDir) => true,
let is_index_file = match last_component {
Component::ParentDir => true,
// `import ".././"` is the same as `import "../"`
// Rust Path does not expose `./` path segment at very end, likely because it does not do anything.
// To provide proper fix, we need to remove it as well.
Some(Component::Normal(os_str))
if module_path.ends_with("./") || module_path.ends_with('.') =>
{
Component::Normal(os_str) if module_path.ends_with("./") || module_path.ends_with('.') => {
if let Some(base_name) = os_str.to_str() {
path_buf.set_file_name(base_name);

Expand Down

0 comments on commit 4bd8041

Please sign in to comment.