-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): add extension rule no-duplicate-imports
#2609
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c4e3a23
feat(eslint-plugin): add extension rule `no-duplicate-imports`
yeonjuan 9b65f59
to camelcase
yeonjuan 086337d
Merge branch 'master' into issue-2315
yeonjuan 5b3592f
add test case
yeonjuan 85dc819
fix lint
yeonjuan d8aa340
add test cases
yeonjuan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi :) |
||
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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait... is this really how the base rule works?
What is wrong with the code
This seems like it's much better than
Because the latter code defines a local variable for no reason.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bradzacher right! This rule seems just focus on duplicate module sources.🤣