diff --git a/crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs b/crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs index 4ccee5868bf74..3e4c873d0e2da 100644 --- a/crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs +++ b/crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs @@ -1018,6 +1018,33 @@ impl NoRestrictedImports { } } } + + for (source, spans) in &side_effect_import_map { + let mut whitelist_found = false; + let mut err = None; + for pattern in &self.patterns { + match pattern.get_group_glob_result(source) { + GlobResult::Whitelist => { + whitelist_found = true; + break; + } + GlobResult::Found => { + err = Some(get_diagnostic_from_import_name_result_pattern( + spans[0], + source, + &ImportNameResult::GeneralDisallowed, + pattern, + )); + } + GlobResult::None => {} + } + } + if !whitelist_found { + if let Some(err) = err { + ctx.diagnostic(err); + } + } + } } fn report_import_name_allowed(&self, ctx: &LintContext<'_>, entry: &ImportEntry) { @@ -3038,6 +3065,13 @@ fn test() { serde_json::json!([{ "paths": [{ "name": "foo", "message": "foo is forbidden, use bar instead" }] }]), ), ), + // https://github.com/oxc-project/oxc/issues/10984 + ( + r"import 'foo'", + Some( + serde_json::json!([{ "patterns": [{ "group": ["foo"], "message": "foo is forbidden, use bar instead" }] }]), + ), + ), ]; let fail_typescript = vec![ diff --git a/crates/oxc_linter/src/snapshots/eslint_no_restricted_imports.snap b/crates/oxc_linter/src/snapshots/eslint_no_restricted_imports.snap index 31c454aace38a..4cae91da72e03 100644 --- a/crates/oxc_linter/src/snapshots/eslint_no_restricted_imports.snap +++ b/crates/oxc_linter/src/snapshots/eslint_no_restricted_imports.snap @@ -815,6 +815,13 @@ source: crates/oxc_linter/src/tester.rs ╰──── help: foo is forbidden, use bar instead + ⚠ eslint(no-restricted-imports): 'foo' import is restricted from being used by a pattern. + ╭─[no_restricted_imports.tsx:1:1] + 1 │ import 'foo' + · ──────────── + ╰──── + help: foo is forbidden, use bar instead + ⚠ eslint(no-restricted-imports): 'import1' import is restricted from being used. ╭─[no_restricted_imports.tsx:1:1] 1 │ import foo from 'import1';