Skip to content

Commit 98708eb

Browse files
fix(linter): Fix inconsistent behavior in no-duplicate-imports rule (#12192)
Currently, the `no-duplicate-imports` rule in Oxlint does not report an error for the following code: ```js /*eslint no-duplicate-imports: ["error", { "includeExports": true }]*/ export { a } from 'foo'; import { f } from 'foo'; ```
1 parent 47ff2bb commit 98708eb

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,11 @@ impl Rule for NoDuplicateImports {
272272
continue;
273273
}
274274

275-
if existing.iter().any(|(t, _, module_type)| {
275+
if existing.iter().any(|(t, import_span, module_type)| {
276+
// import { a } from 'foo'; export { a as t };
277+
if matches!(t, ImportType::Named) && module_request.span != *import_span {
278+
return true;
279+
}
276280
(matches!(
277281
t,
278282
ImportType::Named | ImportType::SideEffect | ImportType::Default
@@ -429,9 +433,23 @@ fn test() {
429433
export * from "os";"#,
430434
Some(serde_json::json!([{ "includeExports": true }])),
431435
),
436+
(
437+
"
438+
import { a } from 'f';
439+
export { b as r };
440+
",
441+
Some(serde_json::json!([{ "includeExports": true }])),
442+
),
432443
];
433444

434445
let fail = vec![
446+
(
447+
"
448+
export { a } from 'foo';
449+
import { f } from 'foo';
450+
",
451+
Some(serde_json::json!([{ "includeExports": true }])),
452+
),
435453
(
436454
r#"import "fs";
437455
import "fs""#,

crates/oxc_linter/src/snapshots/eslint_no_duplicate_imports.snap

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
---
22
source: crates/oxc_linter/src/tester.rs
33
---
4+
eslint(no-duplicate-imports): 'foo' export is duplicated
5+
╭─[no_duplicate_imports.tsx:2:26]
6+
1
7+
2export { a } from 'foo';
8+
· ┬
9+
· ╰── This export is duplicated
10+
3import { f } from 'foo';
11+
· ──┬──
12+
· ╰── Can be merged with this
13+
4
14+
╰────
15+
help: Merge the duplicated exports into a single export statement
16+
417
eslint(no-duplicate-imports): 'fs' import is duplicated
518
╭─[no_duplicate_imports.tsx:1:8]
619
1import "fs";

0 commit comments

Comments
 (0)