Skip to content

fix(no-duplicate): fix no-duplicate false positive on side-effect + type imports #3194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/rules/no-duplicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,54 @@ function getFix(first, rest, sourceCode, context) {
};
}

function shouldSkipDuplicateCheckForInlineTypes(nodes, preferInline = false) {
const importTypes = {
hasType: false,
hasSideEffect: false,
hasDefault: false,
hasNamespaceTypeImport: false,
hasTypeImportSpecifier: false,
hasOther: false,
};

for (const node of nodes) {
if (node.importKind === 'type') {
importTypes.hasType = true;
if (node.specifiers.length === 1 && node.specifiers[0].type === 'ImportNamespaceSpecifier') {
importTypes.hasNamespaceTypeImport = true;
}
} else if (node.specifiers.length === 0) {
importTypes.hasSideEffect = true;
} else if (node.specifiers.length === 1 && node.specifiers[0].type === 'ImportDefaultSpecifier') {
importTypes.hasDefault = true;
} else if (node.specifiers.some((spec) => spec.importKind === 'type')) {
importTypes.hasTypeImportSpecifier = true;
} else {
importTypes.hasOther = true;
break;
}
}

if (!preferInline && importTypes.hasNamespaceTypeImport && importTypes.hasTypeImportSpecifier) {
return true;
}

return !importTypes.hasOther
&& importTypes.hasType
&& !importTypes.hasTypeImportSpecifier
&& (importTypes.hasSideEffect || importTypes.hasDefault);
}

/** @type {(imported: Map<string, import('estree').ImportDeclaration[]>, context: import('eslint').Rule.RuleContext) => void} */
function checkImports(imported, context) {
const preferInline = context.options[0] && context.options[0]['prefer-inline'];

for (const [module, nodes] of imported.entries()) {
if (nodes.length > 1) {
if (shouldSkipDuplicateCheckForInlineTypes(nodes, preferInline)) {
continue;
}

const message = `'${module}' imported multiple times.`;
const [first, ...rest] = nodes;
const sourceCode = getSourceCode(context);
Expand Down
57 changes: 42 additions & 15 deletions tests/src/rules/no-duplicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,21 +538,48 @@ context('TypeScript', function () {
`,
...parserConfig,
}),
].concat(!tsVersionSatisfies('>= 4.5') || !typescriptEslintParserSatisfies('>= 5.7.0') ? [] : [
]
// babel-eslint does not support `import type * as`
.concat(parser !== parsers.BABEL_OLD ? [
test({
code: `
import type * as A from 'a';
import { type B } from 'a';
`,
options: [{ 'prefer-inline': false }],
...parserConfig,
})] : [])
.concat(!tsVersionSatisfies('>= 4.5') || !typescriptEslintParserSatisfies('>= 5.7.0') ? [] : [
// #2470: ignore duplicate if is a typescript inline type import
test({
code: "import { type x } from './foo'; import y from './foo'",
...parserConfig,
}),
test({
code: "import { type x } from './foo'; import { y } from './foo'",
...parserConfig,
}),
test({
code: "import { type x } from './foo'; import type y from 'foo'",
...parserConfig,
}),
]);
test({
code: "import { type x } from './foo'; import y from './foo'",
...parserConfig,
}),
test({
code: "import { type x } from './foo'; import { y } from './foo'",
...parserConfig,
}),
test({
code: "import { type x } from './foo'; import type y from 'foo'",
...parserConfig,
}),
test({
code: `
import type { A } from 'a';
import 'a';
`,
options: [{ 'prefer-inline': true }],
...parserConfig,
}),
test({
code: `
import type { A } from 'a';
import B from 'a';
`,
options: [{ 'prefer-inline': true }],
...parserConfig,
}),
]);

const invalid = [
test(withoutAutofixOutput({
Expand Down Expand Up @@ -750,7 +777,7 @@ context('TypeScript', function () {
}),
]);

ruleTester.run('no-duplicates', rule, {
ruleTester.run(`no-duplicates${parser}`, rule, {
valid,
invalid,
});
Expand Down
Loading