Skip to content

Commit 9a368be

Browse files
committed
fix(linter): false negative in no-restriced-imports with patterns and side effects (#11027)
previously, when using `patterns` with side effects imports, the diagnostic would not be reported, this PR fixes it by checking the module name against patterns. fixes #10984
1 parent 8c2cfbc commit 9a368be

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,33 @@ impl NoRestrictedImports {
10181018
}
10191019
}
10201020
}
1021+
1022+
for (source, spans) in &side_effect_import_map {
1023+
let mut whitelist_found = false;
1024+
let mut err = None;
1025+
for pattern in &self.patterns {
1026+
match pattern.get_group_glob_result(source) {
1027+
GlobResult::Whitelist => {
1028+
whitelist_found = true;
1029+
break;
1030+
}
1031+
GlobResult::Found => {
1032+
err = Some(get_diagnostic_from_import_name_result_pattern(
1033+
spans[0],
1034+
source,
1035+
&ImportNameResult::GeneralDisallowed,
1036+
pattern,
1037+
));
1038+
}
1039+
GlobResult::None => {}
1040+
}
1041+
}
1042+
if !whitelist_found {
1043+
if let Some(err) = err {
1044+
ctx.diagnostic(err);
1045+
}
1046+
}
1047+
}
10211048
}
10221049

10231050
fn report_import_name_allowed(&self, ctx: &LintContext<'_>, entry: &ImportEntry) {
@@ -3038,6 +3065,13 @@ fn test() {
30383065
serde_json::json!([{ "paths": [{ "name": "foo", "message": "foo is forbidden, use bar instead" }] }]),
30393066
),
30403067
),
3068+
// https://github.com/oxc-project/oxc/issues/10984
3069+
(
3070+
r"import 'foo'",
3071+
Some(
3072+
serde_json::json!([{ "patterns": [{ "group": ["foo"], "message": "foo is forbidden, use bar instead" }] }]),
3073+
),
3074+
),
30413075
];
30423076

30433077
let fail_typescript = vec![

crates/oxc_linter/src/snapshots/eslint_no_restricted_imports.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,13 @@ source: crates/oxc_linter/src/tester.rs
815815
╰────
816816
help: foo is forbidden, use bar instead
817817

818+
eslint(no-restricted-imports): 'foo' import is restricted from being used by a pattern.
819+
╭─[no_restricted_imports.tsx:1:1]
820+
1import 'foo'
821+
· ────────────
822+
╰────
823+
help: foo is forbidden, use bar instead
824+
818825
eslint(no-restricted-imports): 'import1' import is restricted from being used.
819826
╭─[no_restricted_imports.tsx:1:1]
820827
1import foo from 'import1';

0 commit comments

Comments
 (0)