Skip to content
Permalink
Browse files

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

  • Loading branch information
yeonjuan committed Oct 6, 2020
1 parent daac9da commit c71f423b89bf034caf2a4f1bb3ed0389b72f3aa9
@@ -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(
@@ -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);
}
}
}

@@ -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);
@@ -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';
@@ -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: [
{
@@ -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';

0 comments on commit c71f423

Please sign in to comment.
You can’t perform that action at this time.