Skip to content

Commit

Permalink
fix(eslint-plugin): [no-duplicate-imports] distinguish member, default (
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjuan committed Oct 6, 2020
1 parent daac9da commit c71f423
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
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

0 comments on commit c71f423

Please sign in to comment.