From 38d0081a20adb877c77f8a873e7fd4194b86ecc1 Mon Sep 17 00:00:00 2001 From: Aaron Moat <2937187+AaronMoat@users.noreply.github.com> Date: Mon, 30 Sep 2024 02:05:16 +1000 Subject: [PATCH] Fix incorrect no-duplicates prefer-inline default type import handling (#162) --- .changeset/mighty-ladybugs-agree.md | 5 +++++ src/rules/no-duplicates.ts | 15 +++++++++++---- test/rules/no-duplicates.spec.ts | 15 +++++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 .changeset/mighty-ladybugs-agree.md diff --git a/.changeset/mighty-ladybugs-agree.md b/.changeset/mighty-ladybugs-agree.md new file mode 100644 index 000000000..552ec8214 --- /dev/null +++ b/.changeset/mighty-ladybugs-agree.md @@ -0,0 +1,5 @@ +--- +'eslint-plugin-import-x': patch +--- + +Fix issue where `no-duplicates` rule with `prefer-inline` incorrectly marks default type and named type imports as duplicates diff --git a/src/rules/no-duplicates.ts b/src/rules/no-duplicates.ts index b706541aa..334b2071b 100644 --- a/src/rules/no-duplicates.ts +++ b/src/rules/no-duplicates.ts @@ -463,12 +463,19 @@ export = createRule<[Options?], MessageId>({ moduleMaps.set(parent, map) } - if (!preferInline && n.importKind === 'type') { - return n.specifiers.length > 0 && + if (n.importKind === 'type') { + if ( + n.specifiers.length > 0 && n.specifiers[0].type === 'ImportDefaultSpecifier' - ? map.defaultTypesImported - : map.namedTypesImported + ) { + return map.defaultTypesImported + } + + if (!preferInline) { + return map.namedTypesImported + } } + if ( !preferInline && n.specifiers.some( diff --git a/test/rules/no-duplicates.spec.ts b/test/rules/no-duplicates.spec.ts index 3be9b76dd..1fb645d59 100644 --- a/test/rules/no-duplicates.spec.ts +++ b/test/rules/no-duplicates.spec.ts @@ -682,6 +682,21 @@ describe('TypeScript', () => { ...parserConfig, }), ]), + test({ + code: "import type { A } from 'foo';import type B from 'foo';", + ...parserConfig, + options: [{ 'prefer-inline': true }], + }), + test({ + code: "import { type A } from 'foo';import type B from 'foo';", + ...parserConfig, + options: [{ 'prefer-inline': true }], + }), + test({ + code: "import type A from 'foo';import { B } from 'foo';", + ...parserConfig, + options: [{ 'prefer-inline': true }], + }), ] const invalid = [