Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): add extension rule
no-duplicate-imports
(#2609)
- Loading branch information
Showing
7 changed files
with
326 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Disallow duplicate imports (`no-duplicate-imports`) | ||
|
||
## Rule Details | ||
|
||
This rule extends the base [`eslint/no-duplicate-imports`](https://eslint.org/docs/rules/no-duplicate-imports) rule. | ||
This version adds support for type-only import and export. | ||
|
||
## How to use | ||
|
||
```jsonc | ||
{ | ||
// note you must disable the base rule as it can report incorrect errors | ||
"no-duplicate-imports": "off", | ||
"@typescript-eslint/no-duplicate-imports": ["error"] | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
See [`eslint/no-duplicate-imports` options](https://eslint.org/docs/rules/no-duplicate-imports#options). | ||
|
||
<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/no-duplicate-imports.md)</sup> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
packages/eslint-plugin/src/rules/no-duplicate-imports.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { | ||
AST_NODE_TYPES, | ||
TSESTree, | ||
} from '@typescript-eslint/experimental-utils'; | ||
import baseRule from 'eslint/lib/rules/no-duplicate-imports'; | ||
import * as util from '../util'; | ||
|
||
type Options = util.InferOptionsTypeFromRule<typeof baseRule>; | ||
type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>; | ||
|
||
export default util.createRule<Options, MessageIds>({ | ||
name: 'no-duplicate-imports', | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Disallow duplicate imports', | ||
category: 'Best Practices', | ||
recommended: false, | ||
extendsBaseRule: true, | ||
}, | ||
schema: baseRule.meta.schema, | ||
messages: { | ||
...baseRule.meta.messages, | ||
importType: '{{module}} type import is duplicated', | ||
importTypeAs: '{{module}} type import is duplicated as type export', | ||
exportType: '{{module}} type export is duplicated', | ||
exportTypeAs: '{{module}} type export is duplicated as type import', | ||
}, | ||
}, | ||
defaultOptions: [ | ||
{ | ||
includeExports: false, | ||
}, | ||
], | ||
create(context, [option]) { | ||
const rules = baseRule.create(context); | ||
const includeExports = option.includeExports; | ||
const typeImports = new Set(); | ||
const typeExports = new Set(); | ||
|
||
function report( | ||
messageId: MessageIds, | ||
node: TSESTree.Node, | ||
module: string, | ||
): void { | ||
context.report({ | ||
messageId, | ||
node, | ||
data: { | ||
module, | ||
}, | ||
}); | ||
} | ||
|
||
function isStringLiteral( | ||
node: TSESTree.Node | null, | ||
): node is TSESTree.StringLiteral { | ||
return ( | ||
!!node && | ||
node.type === AST_NODE_TYPES.Literal && | ||
typeof node.value === 'string' | ||
); | ||
} | ||
|
||
function checkTypeImport(node: TSESTree.ImportDeclaration): void { | ||
if (isStringLiteral(node.source)) { | ||
const value = node.source.value; | ||
if (typeImports.has(value)) { | ||
report('importType', node, value); | ||
} | ||
if (includeExports && typeExports.has(value)) { | ||
report('importTypeAs', node, value); | ||
} | ||
typeImports.add(value); | ||
} | ||
} | ||
|
||
function checkTypeExport( | ||
node: TSESTree.ExportNamedDeclaration | TSESTree.ExportAllDeclaration, | ||
): void { | ||
if (isStringLiteral(node.source)) { | ||
const value = node.source.value; | ||
if (typeExports.has(value)) { | ||
report('exportType', node, value); | ||
} | ||
if (typeImports.has(value)) { | ||
report('exportTypeAs', node, value); | ||
} | ||
typeExports.add(value); | ||
} | ||
} | ||
|
||
return { | ||
...rules, | ||
ImportDeclaration(node): void { | ||
if (node.importKind === 'type') { | ||
checkTypeImport(node); | ||
return; | ||
} | ||
rules.ImportDeclaration(node); | ||
}, | ||
ExportNamedDeclaration(node): void { | ||
if (includeExports && node.exportKind === 'type') { | ||
checkTypeExport(node); | ||
return; | ||
} | ||
rules.ExportNamedDeclaration?.(node); | ||
}, | ||
ExportAllDeclaration(node): void { | ||
if (includeExports && node.exportKind === 'type') { | ||
checkTypeExport(node); | ||
return; | ||
} | ||
rules.ExportAllDeclaration?.(node); | ||
}, | ||
}; | ||
}, | ||
}); |
153 changes: 153 additions & 0 deletions
153
packages/eslint-plugin/tests/rules/no-duplicate-imports.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import rule from '../../src/rules/no-duplicate-imports'; | ||
import { RuleTester } from '../RuleTester'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('no-dupe-class-members', rule, { | ||
valid: [ | ||
{ | ||
code: "import type foo from 'foo';", | ||
}, | ||
{ | ||
code: "import type { foo } from 'foo';", | ||
}, | ||
{ | ||
code: ` | ||
import foo from 'foo'; | ||
import type bar from 'foo'; | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
import { foo } from 'foo'; | ||
import type { bar } from 'foo'; | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
import type { foo } from 'foo'; | ||
export type foo = foo; | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
import type { foo } from 'foo'; | ||
export type { foo }; | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
export { foo } from 'foo'; | ||
export type { foo } from 'foo'; | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
export type * as foo from 'foo'; | ||
export type * as bar from 'foo'; | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
import type { bar } from 'foo'; | ||
export type { foo } from 'foo'; | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
import type { foo } from 'foo'; | ||
export type { bar } from 'bar'; | ||
`, | ||
options: [{ includeExports: true }], | ||
}, | ||
{ | ||
code: ` | ||
import type { foo } from 'foo'; | ||
export type { bar }; | ||
`, | ||
options: [{ includeExports: true }], | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
import type foo from 'foo'; | ||
import type bar from 'foo'; | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'importType', | ||
data: { | ||
module: 'foo', | ||
}, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
import type { foo } from 'foo'; | ||
import type { bar } from 'foo'; | ||
`, | ||
errors: [{ messageId: 'importType' }], | ||
}, | ||
{ | ||
code: ` | ||
export type { foo } from 'foo'; | ||
import type { bar } from 'foo'; | ||
`, | ||
options: [{ includeExports: true }], | ||
errors: [{ messageId: 'importTypeAs' }], | ||
}, | ||
{ | ||
code: ` | ||
import type foo from 'foo'; | ||
export type * from 'foo'; | ||
`, | ||
options: [{ includeExports: true }], | ||
errors: [{ messageId: 'exportTypeAs' }], | ||
}, | ||
{ | ||
code: ` | ||
import type { foo } from 'foo'; | ||
export type { foo } from 'foo'; | ||
`, | ||
options: [{ includeExports: true }], | ||
errors: [{ messageId: 'exportTypeAs' }], | ||
}, | ||
{ | ||
code: ` | ||
export type * as foo from 'foo'; | ||
export type * as bar from 'foo'; | ||
`, | ||
options: [{ includeExports: true }], | ||
errors: [{ messageId: 'exportType' }], | ||
}, | ||
|
||
// check base rule | ||
{ | ||
code: ` | ||
import foo from 'foo'; | ||
import bar from 'foo'; | ||
`, | ||
errors: [{ messageId: 'import' }], | ||
}, | ||
{ | ||
code: ` | ||
import foo from 'foo'; | ||
export * from 'foo'; | ||
`, | ||
options: [{ includeExports: true }], | ||
errors: [{ messageId: 'exportAs' }], | ||
}, | ||
{ | ||
code: ` | ||
import foo from 'foo'; | ||
export { foo } from 'foo'; | ||
`, | ||
options: [{ includeExports: true }], | ||
errors: [{ messageId: 'exportAs' }], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters