fix(linter): correct false positive in no-duplicates (#6748)

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 <donald.isaac@gmail.com>
This commit is contained in:
dalaoshu 2024-10-22 13:34:03 +08:00 committed by GitHub
parent 9648e98998
commit 54a50322db
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 13 deletions

View file

@ -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
@ -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,
}
@ -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);
}
}
}
@ -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![

View file

@ -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]
1 │ import './foo'; import def, {x} from './foo'
· ───┬─── ───────
· ───┬─── ───────
· ╰── It is first imported here
╰────
help: Merge these imports into a single import statement
@ -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]
1 │ import {x} from './foo'; import def, {y} from './foo'
· ───┬─── ───────
· ───┬─── ───────
· ╰── It is first imported here
╰────
help: Merge these imports into a single import statement
@ -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]
1 │ import {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]
1 │ import {AValue, type x, BValue} from './foo'; import {type y} from './foo'