-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
no-duplicate-imports.ts
136 lines (126 loc) · 3.7 KB
/
no-duplicate-imports.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { getESLintCoreRule } from '../util/getESLintCoreRule';
import * as util from '../util';
const baseRule = getESLintCoreRule('no-duplicate-imports');
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',
recommended: false,
extendsBaseRule: true,
},
hasSuggestions: baseRule.meta.hasSuggestions,
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, [{ includeExports }]) {
const rules = baseRule.create(context);
const typeMemberImports = new Set();
const typeDefaultImports = 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 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;
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);
}
if (isMemberImport) {
typeMemberImports.add(value);
} else {
typeDefaultImports.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 (typeMemberImports.has(value) || typeDefaultImports.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);
},
};
},
});