Skip to content
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

fix(eslint-plugin): [no-duplicate-imports] distinguish member, default #2637

Merged
merged 1 commit into from Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 21 additions & 4 deletions packages/eslint-plugin/src/rules/no-duplicate-imports.ts
Expand Up @@ -35,7 +35,8 @@ export default util.createRule<Options, MessageIds>({
create(context, [option]) {
const rules = baseRule.create(context);
const includeExports = option.includeExports;
const typeImports = new Set();
const typeMemberImports = new Set();
const typeDefaultImports = new Set();
const typeExports = new Set();

function report(
Expand All @@ -62,16 +63,32 @@ export default util.createRule<Options, MessageIds>({
);
}

function isAllMemberImport(node: TSESTree.ImportDeclaration): boolean {
return node.specifiers.every(
specifier => specifier.type === AST_NODE_TYPES.ImportSpecifier,
);
}

function checkTypeImport(node: TSESTree.ImportDeclaration): void {
if (isStringLiteral(node.source)) {
const value = node.source.value;
if (typeImports.has(value)) {
const isMemberImport = isAllMemberImport(node);
if (
isMemberImport
? typeMemberImports.has(value)
: typeDefaultImports.has(value)
) {
report('importType', node, value);
}

if (includeExports && typeExports.has(value)) {
report('importTypeAs', node, value);
}
typeImports.add(value);
if (isMemberImport) {
typeMemberImports.add(value);
} else {
typeDefaultImports.add(value);
}
}
}

Expand All @@ -83,7 +100,7 @@ export default util.createRule<Options, MessageIds>({
if (typeExports.has(value)) {
report('exportType', node, value);
}
if (typeImports.has(value)) {
if (typeMemberImports.has(value) || typeDefaultImports.has(value)) {
report('exportTypeAs', node, value);
}
typeExports.add(value);
Expand Down
37 changes: 36 additions & 1 deletion packages/eslint-plugin/tests/rules/no-duplicate-imports.test.ts
Expand Up @@ -5,14 +5,32 @@ const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
});

ruleTester.run('no-dupe-class-members', rule, {
ruleTester.run('no-duplicate-imports', rule, {
valid: [
{
code: "import type foo from 'foo';",
},
{
code: "import type { foo } from 'foo';",
},
{
code: `
import type { foo } from 'foo';
import type Bar from 'foo';
`,
},
{
code: `
import type Foo from 'foo';
import type { bar } from 'foo';
`,
},
{
code: `
import type Foo from 'foo';
import type { bar as Bar } from 'foo';
`,
},
{
code: `
import foo from 'foo';
Expand Down Expand Up @@ -69,6 +87,14 @@ ruleTester.run('no-dupe-class-members', rule, {
`,
options: [{ includeExports: true }],
},
{
code: `
import type Foo from 'foo';
import type { bar } from 'foo';
export type { bar };
`,
options: [{ includeExports: true }],
},
],
invalid: [
{
Expand Down Expand Up @@ -116,6 +142,15 @@ ruleTester.run('no-dupe-class-members', rule, {
options: [{ includeExports: true }],
errors: [{ messageId: 'exportTypeAs' }],
},
{
code: `
import type Foo from 'foo';
import type { bar } from 'foo';
export type { bar } from 'foo';
`,
options: [{ includeExports: true }],
errors: [{ messageId: 'exportTypeAs' }],
},
{
code: `
export type * as foo from 'foo';
Expand Down