Skip to content

Commit 8c2cfbc

Browse files
committed
fix(linter): false negative in no-restricted-imports (#11026)
in scenrio such as ```ts import 'foo' import { a } from 'b' ``` if `foo` was banned module, it wouldn't be reported as `module_record.import_entries.is_empty()` would not be true. we now check if `import 'foo'` is included in `module_record.import_entries` to decide whether to add to side effects map
1 parent db6afb9 commit 8c2cfbc

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,13 @@ impl NoRestrictedImports {
993993

994994
for (source, requests) in &module_record.requested_modules {
995995
for request in requests {
996-
if request.is_import && module_record.import_entries.is_empty() {
996+
if request.is_import
997+
&& (module_record.import_entries.is_empty()
998+
|| module_record
999+
.import_entries
1000+
.iter()
1001+
.all(|entry| entry.statement_span != request.statement_span))
1002+
{
9971003
side_effect_import_map.entry(source).or_default().push(request.statement_span);
9981004
}
9991005
}
@@ -3026,6 +3032,12 @@ fn test() {
30263032
// }]
30273033
// }])),
30283034
// ),
3035+
(
3036+
r"import 'foo'; import {a} from 'b'",
3037+
Some(
3038+
serde_json::json!([{ "paths": [{ "name": "foo", "message": "foo is forbidden, use bar instead" }] }]),
3039+
),
3040+
),
30293041
];
30303042

30313043
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
@@ -808,6 +808,13 @@ source: crates/oxc_linter/src/tester.rs
808808
╰────
809809
help: foo is forbidden, use bar instead
810810

811+
eslint(no-restricted-imports): 'foo' import is restricted from being used.
812+
╭─[no_restricted_imports.tsx:1:1]
813+
1import 'foo'; import {a} from 'b'
814+
· ─────────────
815+
╰────
816+
help: foo is forbidden, use bar instead
817+
811818
eslint(no-restricted-imports): 'import1' import is restricted from being used.
812819
╭─[no_restricted_imports.tsx:1:1]
813820
1import foo from 'import1';

0 commit comments

Comments
 (0)