From b21744256e61fc59a8fd097ed5c0cccc36a8cd93 Mon Sep 17 00:00:00 2001 From: shulaoda Date: Mon, 21 Oct 2024 23:23:52 +0800 Subject: [PATCH 1/2] fix(linter): correct false positive in no-duplicates --- crates/oxc_linter/src/rules/import/no_duplicates.rs | 9 ++++++++- crates/oxc_linter/src/snapshots/no_duplicates.snap | 12 ++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/crates/oxc_linter/src/rules/import/no_duplicates.rs b/crates/oxc_linter/src/rules/import/no_duplicates.rs index 7d4ce9c1c5454..da82dfd2b320a 100644 --- a/crates/oxc_linter/src/rules/import/no_duplicates.rs +++ b/crates/oxc_linter/src/rules/import/no_duplicates.rs @@ -117,7 +117,9 @@ impl Rule for NoDuplicates { .collect::>(); 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 { @@ -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![ diff --git a/crates/oxc_linter/src/snapshots/no_duplicates.snap b/crates/oxc_linter/src/snapshots/no_duplicates.snap index df546929e05fe..ab90d2dd8b211 100644 --- a/crates/oxc_linter/src/snapshots/no_duplicates.snap +++ b/crates/oxc_linter/src/snapshots/no_duplicates.snap @@ -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' From 29124bfaddf1992d9aff62f95db942bdc2535421 Mon Sep 17 00:00:00 2001 From: shulaoda Date: Tue, 22 Oct 2024 13:06:57 +0800 Subject: [PATCH 2/2] clippy --- crates/oxc_linter/src/rules/import/no_duplicates.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/oxc_linter/src/rules/import/no_duplicates.rs b/crates/oxc_linter/src/rules/import/no_duplicates.rs index da82dfd2b320a..d35ee702b9d1e 100644 --- a/crates/oxc_linter/src/rules/import/no_duplicates.rs +++ b/crates/oxc_linter/src/rules/import/no_duplicates.rs @@ -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> = + let mut import_entries_maps: FxHashMap> = FxHashMap::default(); for requested_module in requested_modules { let imports = module_record @@ -123,7 +123,7 @@ impl Rule for NoDuplicates { 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, }