Skip to content

Commit 78c311c

Browse files
committed
refactor(linter/no-unassigned-import): use fast-glob instead of globset (#12867)
Related to #12825
1 parent fcdb91d commit 78c311c

File tree

1 file changed

+11
-19
lines changed

1 file changed

+11
-19
lines changed

crates/oxc_linter/src/rules/import/no_unassigned_import.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use globset::{Glob, GlobSet, GlobSetBuilder};
21
use oxc_ast::{
32
AstKind,
43
ast::{Argument, Expression},
@@ -21,7 +20,7 @@ pub struct NoUnassignedImport(Box<NoUnassignedImportConfig>);
2120

2221
#[derive(Debug, Default, Clone)]
2322
pub struct NoUnassignedImportConfig {
24-
globs: GlobSet,
23+
globs: Vec<CompactStr>,
2524
}
2625

2726
impl std::ops::Deref for NoUnassignedImport {
@@ -71,36 +70,23 @@ declare_oxc_lint!(
7170
suspicious,
7271
);
7372

74-
fn build_globset(patterns: Vec<CompactStr>) -> Result<GlobSet, globset::Error> {
75-
if patterns.is_empty() {
76-
return Ok(GlobSet::empty());
77-
}
78-
let mut builder = GlobSetBuilder::new();
79-
for pattern in patterns {
80-
let pattern_str = pattern.as_str();
81-
builder.add(Glob::new(pattern_str)?);
82-
}
83-
builder.build()
84-
}
85-
8673
impl Rule for NoUnassignedImport {
8774
fn from_configuration(value: Value) -> Self {
8875
let obj = value.get(0);
89-
let allow = obj
76+
let globs = obj
9077
.and_then(|v| v.get("allow"))
9178
.and_then(Value::as_array)
9279
.map(|v| v.iter().filter_map(Value::as_str).map(CompactStr::from).collect())
9380
.unwrap_or_default();
94-
Self(Box::new(NoUnassignedImportConfig { globs: build_globset(allow).unwrap_or_default() }))
81+
Self(Box::new(NoUnassignedImportConfig { globs }))
9582
}
9683
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
9784
match node.kind() {
9885
AstKind::ImportDeclaration(import_decl) => {
9986
if import_decl.specifiers.is_some() {
10087
return;
10188
}
102-
let source_str = import_decl.source.value.as_str();
103-
if !self.globs.is_match(source_str) {
89+
if !self.is_match_allow_globs(import_decl.source.value.as_str()) {
10490
ctx.diagnostic(no_unassigned_import_diagnostic(
10591
import_decl.span,
10692
"Imported module should be assigned",
@@ -118,7 +104,7 @@ impl Rule for NoUnassignedImport {
118104
let Argument::StringLiteral(source_str) = first_arg else {
119105
return;
120106
};
121-
if !self.globs.is_match(source_str.value.as_str()) {
107+
if !self.is_match_allow_globs(source_str.value.as_str()) {
122108
ctx.diagnostic(no_unassigned_import_diagnostic(
123109
call_expr.span,
124110
"A `require()` style import is forbidden.",
@@ -130,6 +116,12 @@ impl Rule for NoUnassignedImport {
130116
}
131117
}
132118

119+
impl NoUnassignedImportConfig {
120+
fn is_match_allow_globs(&self, source: &str) -> bool {
121+
self.globs.iter().any(|glob| fast_glob::glob_match(glob.as_str(), source))
122+
}
123+
}
124+
133125
#[test]
134126
fn test() {
135127
use crate::tester::Tester;

0 commit comments

Comments
 (0)