Skip to content

Commit 498f397

Browse files
authored
feat(eslint-plugin): add extension rule no-duplicate-imports (typescript-eslint#2609)
1 parent d72951a commit 498f397

File tree

7 files changed

+326
-2
lines changed

7 files changed

+326
-2
lines changed

packages/eslint-plugin/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ In these cases, we create what we call an extension rule; a rule within our plug
200200
| [`@typescript-eslint/lines-between-class-members`](./docs/rules/lines-between-class-members.md) | Require or disallow an empty line between class members | | :wrench: | |
201201
| [`@typescript-eslint/no-array-constructor`](./docs/rules/no-array-constructor.md) | Disallow generic `Array` constructors | :heavy_check_mark: | :wrench: | |
202202
| [`@typescript-eslint/no-dupe-class-members`](./docs/rules/no-dupe-class-members.md) | Disallow duplicate class members | | | |
203+
| [`@typescript-eslint/no-duplicate-imports`](./docs/rules/no-duplicate-imports.md) | Disallow duplicate imports | | | |
203204
| [`@typescript-eslint/no-empty-function`](./docs/rules/no-empty-function.md) | Disallow empty functions | :heavy_check_mark: | | |
204205
| [`@typescript-eslint/no-extra-parens`](./docs/rules/no-extra-parens.md) | Disallow unnecessary parentheses | | :wrench: | |
205206
| [`@typescript-eslint/no-extra-semi`](./docs/rules/no-extra-semi.md) | Disallow unnecessary semicolons | :heavy_check_mark: | :wrench: | |
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Disallow duplicate imports (`no-duplicate-imports`)
2+
3+
## Rule Details
4+
5+
This rule extends the base [`eslint/no-duplicate-imports`](https://eslint.org/docs/rules/no-duplicate-imports) rule.
6+
This version adds support for type-only import and export.
7+
8+
## How to use
9+
10+
```jsonc
11+
{
12+
// note you must disable the base rule as it can report incorrect errors
13+
"no-duplicate-imports": "off",
14+
"@typescript-eslint/no-duplicate-imports": ["error"]
15+
}
16+
```
17+
18+
## Options
19+
20+
See [`eslint/no-duplicate-imports` options](https://eslint.org/docs/rules/no-duplicate-imports#options).
21+
22+
<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/no-duplicate-imports.md)</sup>

packages/eslint-plugin/src/configs/all.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export = {
1414
'brace-style': 'off',
1515
'@typescript-eslint/brace-style': 'error',
1616
'@typescript-eslint/class-literal-property-style': 'error',
17+
'comma-dangle': 'off',
18+
'@typescript-eslint/comma-dangle': 'error',
1719
'comma-spacing': 'off',
1820
'@typescript-eslint/comma-spacing': 'error',
1921
'@typescript-eslint/consistent-indexed-object-style': 'error',
@@ -47,6 +49,8 @@ export = {
4749
'@typescript-eslint/no-confusing-non-null-assertion': 'error',
4850
'no-dupe-class-members': 'off',
4951
'@typescript-eslint/no-dupe-class-members': 'error',
52+
'no-duplicate-imports': 'off',
53+
'@typescript-eslint/no-duplicate-imports': 'error',
5054
'@typescript-eslint/no-dynamic-delete': 'error',
5155
'no-empty-function': 'off',
5256
'@typescript-eslint/no-empty-function': 'error',
@@ -140,7 +144,5 @@ export = {
140144
'@typescript-eslint/typedef': 'error',
141145
'@typescript-eslint/unbound-method': 'error',
142146
'@typescript-eslint/unified-signatures': 'error',
143-
'comma-dangle': 'off',
144-
'@typescript-eslint/comma-dangle': 'error',
145147
},
146148
};

packages/eslint-plugin/src/rules/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ import typeAnnotationSpacing from './type-annotation-spacing';
106106
import typedef from './typedef';
107107
import unboundMethod from './unbound-method';
108108
import unifiedSignatures from './unified-signatures';
109+
import noDuplicateImports from './no-duplicate-imports';
109110

110111
export default {
111112
'adjacent-overload-signatures': adjacentOverloadSignatures,
@@ -212,6 +213,7 @@ export default {
212213
'type-annotation-spacing': typeAnnotationSpacing,
213214
'unbound-method': unboundMethod,
214215
'unified-signatures': unifiedSignatures,
216+
'no-duplicate-imports': noDuplicateImports,
215217
indent: indent,
216218
quotes: quotes,
217219
semi: semi,
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import {
2+
AST_NODE_TYPES,
3+
TSESTree,
4+
} from '@typescript-eslint/experimental-utils';
5+
import baseRule from 'eslint/lib/rules/no-duplicate-imports';
6+
import * as util from '../util';
7+
8+
type Options = util.InferOptionsTypeFromRule<typeof baseRule>;
9+
type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>;
10+
11+
export default util.createRule<Options, MessageIds>({
12+
name: 'no-duplicate-imports',
13+
meta: {
14+
type: 'problem',
15+
docs: {
16+
description: 'Disallow duplicate imports',
17+
category: 'Best Practices',
18+
recommended: false,
19+
extendsBaseRule: true,
20+
},
21+
schema: baseRule.meta.schema,
22+
messages: {
23+
...baseRule.meta.messages,
24+
importType: '{{module}} type import is duplicated',
25+
importTypeAs: '{{module}} type import is duplicated as type export',
26+
exportType: '{{module}} type export is duplicated',
27+
exportTypeAs: '{{module}} type export is duplicated as type import',
28+
},
29+
},
30+
defaultOptions: [
31+
{
32+
includeExports: false,
33+
},
34+
],
35+
create(context, [option]) {
36+
const rules = baseRule.create(context);
37+
const includeExports = option.includeExports;
38+
const typeImports = new Set();
39+
const typeExports = new Set();
40+
41+
function report(
42+
messageId: MessageIds,
43+
node: TSESTree.Node,
44+
module: string,
45+
): void {
46+
context.report({
47+
messageId,
48+
node,
49+
data: {
50+
module,
51+
},
52+
});
53+
}
54+
55+
function isStringLiteral(
56+
node: TSESTree.Node | null,
57+
): node is TSESTree.StringLiteral {
58+
return (
59+
!!node &&
60+
node.type === AST_NODE_TYPES.Literal &&
61+
typeof node.value === 'string'
62+
);
63+
}
64+
65+
function checkTypeImport(node: TSESTree.ImportDeclaration): void {
66+
if (isStringLiteral(node.source)) {
67+
const value = node.source.value;
68+
if (typeImports.has(value)) {
69+
report('importType', node, value);
70+
}
71+
if (includeExports && typeExports.has(value)) {
72+
report('importTypeAs', node, value);
73+
}
74+
typeImports.add(value);
75+
}
76+
}
77+
78+
function checkTypeExport(
79+
node: TSESTree.ExportNamedDeclaration | TSESTree.ExportAllDeclaration,
80+
): void {
81+
if (isStringLiteral(node.source)) {
82+
const value = node.source.value;
83+
if (typeExports.has(value)) {
84+
report('exportType', node, value);
85+
}
86+
if (typeImports.has(value)) {
87+
report('exportTypeAs', node, value);
88+
}
89+
typeExports.add(value);
90+
}
91+
}
92+
93+
return {
94+
...rules,
95+
ImportDeclaration(node): void {
96+
if (node.importKind === 'type') {
97+
checkTypeImport(node);
98+
return;
99+
}
100+
rules.ImportDeclaration(node);
101+
},
102+
ExportNamedDeclaration(node): void {
103+
if (includeExports && node.exportKind === 'type') {
104+
checkTypeExport(node);
105+
return;
106+
}
107+
rules.ExportNamedDeclaration?.(node);
108+
},
109+
ExportAllDeclaration(node): void {
110+
if (includeExports && node.exportKind === 'type') {
111+
checkTypeExport(node);
112+
return;
113+
}
114+
rules.ExportAllDeclaration?.(node);
115+
},
116+
};
117+
},
118+
});
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import rule from '../../src/rules/no-duplicate-imports';
2+
import { RuleTester } from '../RuleTester';
3+
4+
const ruleTester = new RuleTester({
5+
parser: '@typescript-eslint/parser',
6+
});
7+
8+
ruleTester.run('no-dupe-class-members', rule, {
9+
valid: [
10+
{
11+
code: "import type foo from 'foo';",
12+
},
13+
{
14+
code: "import type { foo } from 'foo';",
15+
},
16+
{
17+
code: `
18+
import foo from 'foo';
19+
import type bar from 'foo';
20+
`,
21+
},
22+
{
23+
code: `
24+
import { foo } from 'foo';
25+
import type { bar } from 'foo';
26+
`,
27+
},
28+
{
29+
code: `
30+
import type { foo } from 'foo';
31+
export type foo = foo;
32+
`,
33+
},
34+
{
35+
code: `
36+
import type { foo } from 'foo';
37+
export type { foo };
38+
`,
39+
},
40+
{
41+
code: `
42+
export { foo } from 'foo';
43+
export type { foo } from 'foo';
44+
`,
45+
},
46+
{
47+
code: `
48+
export type * as foo from 'foo';
49+
export type * as bar from 'foo';
50+
`,
51+
},
52+
{
53+
code: `
54+
import type { bar } from 'foo';
55+
export type { foo } from 'foo';
56+
`,
57+
},
58+
{
59+
code: `
60+
import type { foo } from 'foo';
61+
export type { bar } from 'bar';
62+
`,
63+
options: [{ includeExports: true }],
64+
},
65+
{
66+
code: `
67+
import type { foo } from 'foo';
68+
export type { bar };
69+
`,
70+
options: [{ includeExports: true }],
71+
},
72+
],
73+
invalid: [
74+
{
75+
code: `
76+
import type foo from 'foo';
77+
import type bar from 'foo';
78+
`,
79+
errors: [
80+
{
81+
messageId: 'importType',
82+
data: {
83+
module: 'foo',
84+
},
85+
},
86+
],
87+
},
88+
{
89+
code: `
90+
import type { foo } from 'foo';
91+
import type { bar } from 'foo';
92+
`,
93+
errors: [{ messageId: 'importType' }],
94+
},
95+
{
96+
code: `
97+
export type { foo } from 'foo';
98+
import type { bar } from 'foo';
99+
`,
100+
options: [{ includeExports: true }],
101+
errors: [{ messageId: 'importTypeAs' }],
102+
},
103+
{
104+
code: `
105+
import type foo from 'foo';
106+
export type * from 'foo';
107+
`,
108+
options: [{ includeExports: true }],
109+
errors: [{ messageId: 'exportTypeAs' }],
110+
},
111+
{
112+
code: `
113+
import type { foo } from 'foo';
114+
export type { foo } from 'foo';
115+
`,
116+
options: [{ includeExports: true }],
117+
errors: [{ messageId: 'exportTypeAs' }],
118+
},
119+
{
120+
code: `
121+
export type * as foo from 'foo';
122+
export type * as bar from 'foo';
123+
`,
124+
options: [{ includeExports: true }],
125+
errors: [{ messageId: 'exportType' }],
126+
},
127+
128+
// check base rule
129+
{
130+
code: `
131+
import foo from 'foo';
132+
import bar from 'foo';
133+
`,
134+
errors: [{ messageId: 'import' }],
135+
},
136+
{
137+
code: `
138+
import foo from 'foo';
139+
export * from 'foo';
140+
`,
141+
options: [{ includeExports: true }],
142+
errors: [{ messageId: 'exportAs' }],
143+
},
144+
{
145+
code: `
146+
import foo from 'foo';
147+
export { foo } from 'foo';
148+
`,
149+
options: [{ includeExports: true }],
150+
errors: [{ messageId: 'exportAs' }],
151+
},
152+
],
153+
});

packages/eslint-plugin/typings/eslint-rules.d.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,3 +762,29 @@ declare module 'eslint/lib/rules/comma-dangle' {
762762
>;
763763
export = rule;
764764
}
765+
766+
declare module 'eslint/lib/rules/no-duplicate-imports' {
767+
import { TSESLint, TSESTree } from '@typescript-eslint/experimental-utils';
768+
769+
const rule: TSESLint.RuleModule<
770+
| 'import'
771+
| 'importAs'
772+
| 'export'
773+
| 'exportAs'
774+
| 'importType'
775+
| 'importTypeAs'
776+
| 'exportType'
777+
| 'exportTypeAs',
778+
[
779+
{
780+
includeExports?: boolean;
781+
},
782+
],
783+
{
784+
ImportDeclaration(node: TSESTree.ImportDeclaration): void;
785+
ExportNamedDeclaration?(node: TSESTree.ExportNamedDeclaration): void;
786+
ExportAllDeclaration?(node: TSESTree.ExportAllDeclaration): void;
787+
}
788+
>;
789+
export = rule;
790+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy