Skip to content

Commit

Permalink
chore: use Path::new to check for extension
Browse files Browse the repository at this point in the history
  • Loading branch information
minht11 committed May 8, 2024
1 parent aa9dd38 commit a456967
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::Path;

use biome_analyze::{
context::RuleContext, declare_rule, ActionCategory, Ast, Rule, RuleDiagnostic,
};
Expand All @@ -9,7 +11,7 @@ use biome_rowan::{AstNode, BatchMutationExt, TokenText};

use crate::JsRuleAction;

use super::use_import_restrictions::{get_basename, get_extension};
use super::use_import_restrictions::get_basename;

declare_rule! {
/// Require import extensions for relative imports.
Expand Down Expand Up @@ -132,12 +134,12 @@ fn get_extensionless_import(
return None;
}

let mut path_parts: Vec<_> = module_path.text().split('/').collect();

if get_extension(&path_parts).is_some() {
if Path::new(module_path.text()).extension().is_some() {
return None;
}

let mut path_parts: Vec<_> = module_path.text().split('/').collect();

if module_path.ends_with('/') {
path_parts.pop();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::Path;

use biome_analyze::{
context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic, RuleSource, RuleSourceKind,
};
Expand Down Expand Up @@ -131,8 +133,8 @@ fn get_restricted_import(module_path: &TokenText) -> Option<ImportRestrictionsSt
let mut path_parts: Vec<_> = module_path.text().split('/').collect();
let mut index_filename = None;

if let Some(extension) = get_extension(&path_parts) {
if !SOURCE_EXTENSIONS.contains(&extension) {
if let Some(extension) = Path::new(module_path.text()).extension() {
if !SOURCE_EXTENSIONS.contains(&extension.to_str()?) {
return None; // Resource files are exempt.
}

Expand Down Expand Up @@ -180,12 +182,3 @@ pub(crate) fn get_basename<'a>(path_parts: &'_ [&'a str]) -> Option<&'a str> {
_ => part,
})
}

pub(crate) fn get_extension<'a>(path_parts: &'_ [&'a str]) -> Option<&'a str> {
path_parts.last().and_then(|part| match part.find('.') {
Some(dot_index) if dot_index > 0 && dot_index < part.len() - 1 => {
Some(&part[dot_index + 1..])
}
_ => None,
})
}

0 comments on commit a456967

Please sign in to comment.