Skip to content

Commit

Permalink
fix(linter): correct false positive in no-duplicates (#6748)
Browse files Browse the repository at this point in the history
closes #6736 

There may be a better solution. If there is a good way to fix it, please
feel free to close it. I will fix the clippy problem later because I
went back to the dormitory to sleep and my computer was in the studio.

---------

Co-authored-by: Don Isaac <[email protected]>
  • Loading branch information
shulaoda and DonIsaac authored Oct 22, 2024
1 parent 9648e98 commit 54a5032
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
13 changes: 10 additions & 3 deletions crates/oxc_linter/src/rules/import/no_duplicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Rule for NoDuplicates {
.filter(|requested_module| requested_module.is_import());
// When prefer_inline is false, 0 is value, 1 is type named, 2 is type namespace and 3 is type default
// When prefer_inline is true, 0 is value and type named, 2 is type // namespace and 3 is type default
let mut import_entries_maps: FxHashMap<i8, Vec<&RequestedModule>> =
let mut import_entries_maps: FxHashMap<u8, Vec<&RequestedModule>> =
FxHashMap::default();
for requested_module in requested_modules {
let imports = module_record
Expand All @@ -117,11 +117,13 @@ impl Rule for NoDuplicates {
.collect::<Vec<_>>();
if imports.is_empty() {
import_entries_maps.entry(0).or_default().push(requested_module);
continue;
}
let mut flags = [true; 4];
for imports in imports {
let key = if imports.is_type {
match imports.import_name {
ImportImportName::Name(_) => i8::from(!self.prefer_inline),
ImportImportName::Name(_) => u8::from(!self.prefer_inline),
ImportImportName::NamespaceObject => 2,
ImportImportName::Default(_) => 3,
}
Expand All @@ -131,7 +133,11 @@ impl Rule for NoDuplicates {
_ => 0,
}
};
import_entries_maps.entry(key).or_default().push(requested_module);

if flags[key as usize] {
flags[key as usize] = false;
import_entries_maps.entry(key).or_default().push(requested_module);
}
}
}

Expand Down Expand Up @@ -206,6 +212,7 @@ fn test() {
(r"import type * as something from './foo'; import { y } from './foo';", None),
(r"import y from './foo'; import type * as something from './foo';", None),
(r"import { y } from './foo'; import type * as something from './foo';", None),
(r"import { RouterModule, Routes } from '@angular/router';", None),
];

let fail = vec![
Expand Down
12 changes: 2 additions & 10 deletions crates/oxc_linter/src/snapshots/no_duplicates.snap
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ source: crates/oxc_linter/src/tester.rs
eslint-plugin-import(no-duplicates): Module './foo' is imported more than once in this file
╭─[index.ts:1:8]
1import './foo'; import def, {x} from './foo'
· ───┬─── ───────
· ───┬─── ───────
· ╰── It is first imported here
╰────
help: Merge these imports into a single import statement
Expand Down Expand Up @@ -249,7 +249,7 @@ source: crates/oxc_linter/src/tester.rs
eslint-plugin-import(no-duplicates): Module './foo' is imported more than once in this file
╭─[index.ts:1:17]
1import {x} from './foo'; import def, {y} from './foo'
· ───┬─── ───────
· ───┬─── ───────
· ╰── It is first imported here
╰────
help: Merge these imports into a single import statement
Expand Down Expand Up @@ -540,14 +540,6 @@ source: crates/oxc_linter/src/tester.rs
╰────
help: Merge these imports into a single import statement

eslint-plugin-import(no-duplicates): Module './foo' is imported more than once in this file
╭─[index.ts:1:38]
1import {AValue, type x, BValue} from './foo'; import {type y} from './foo'
· ───┬────
· ╰── It is first imported here
╰────
help: Merge these imports into a single import statement

eslint-plugin-import(no-duplicates): Module './foo' is imported more than once in this file
╭─[index.ts:1:38]
1import {AValue, type x, BValue} from './foo'; import {type y} from './foo'
Expand Down

0 comments on commit 54a5032

Please sign in to comment.