forked from eslint/eslint
/
no-duplicate-imports.js
307 lines (285 loc) · 9.28 KB
/
no-duplicate-imports.js
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/**
* @fileoverview Restrict usage of duplicate imports.
* @author Simen Bekkhus
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
const EXPORT_ALL_DECLARATION = "ExportAllDeclaration";
const IMPORT_NAME_SPACE_SPECIFIER = "ImportNamespaceSpecifier";
const IMPORT_SPECIFIER = "ImportSpecifier";
const CONTRADICTORY_IMPORT_TYPES = [
IMPORT_NAME_SPACE_SPECIFIER,
IMPORT_SPECIFIER
];
/**
* Return the type of import.
* @param {ASTNode} node A node to get.
* @returns {string} the type of the import.
*/
function getImportType(node) {
if (
node &&
node.specifiers &&
node.specifiers[0] &&
node.specifiers[0].type
) {
const index = node.specifiers.findIndex(specifier =>
CONTRADICTORY_IMPORT_TYPES.includes(
specifier.type
));
if (index > -1) {
return node.specifiers[index].type;
}
return node.specifiers[0].type;
}
if (node && node.type) {
return node.type;
}
return "";
}
/**
* Returns the name of the module imported or re-exported.
* @param {ASTNode} node A node to get.
* @returns {string} the name of the module, or empty string if no name.
*/
function getValue(node) {
if (node && node.source && node.source.value) {
return node.source.value.trim();
}
return "";
}
/**
* Returns a boolean if we should report contradictory import type.
* @param {string[]} importTypes An array contain import types of a module.
* @param {string} importType An contradictory import type to check if we should report it or not.
* @returns {boolean} true if the contradictory import type should be reported.
*/
function shouldReportContradictoryImportType(importTypes, importType) {
if (importTypes.indexOf(importType) > -1) {
return true;
}
return (
importTypes.findIndex(importTypeItem => CONTRADICTORY_IMPORT_TYPES.includes(importTypeItem)) === -1
);
}
/**
* Checks if the name of the import or export exists in the given array, and reports if so.
* @param {RuleContext} context The ESLint rule context object.
* @param {ASTNode} node A node to get.
* @param {string} value The name of the imported or exported module.
* @param {string[]} array The array containing other imports or exports in the file.
* @param {string} messageId A messageId to be reported after the name of the module
* @param {{}} modulesWithImportTypes The object containing the name of unique modules with their first import type [specificImport, nameSpaceImport].
* @param {string} type the name of import type.
* @param {string[]} ExportAllDeclarationsInFile The array containing ExportAllDeclarations in the file.
*
* @returns {void} No return value
*/
function checkAndReport(
context,
node,
value,
array,
messageId,
modulesWithImportTypes,
type,
ExportAllDeclarationsInFile
) {
let isDuplicate = false;
if (type === EXPORT_ALL_DECLARATION) {
if (ExportAllDeclarationsInFile.indexOf(value) !== -1) {
isDuplicate = true;
}
} else if (array.indexOf(value) !== -1) {
isDuplicate = true;
if (CONTRADICTORY_IMPORT_TYPES.includes(type)) {
isDuplicate = shouldReportContradictoryImportType(
modulesWithImportTypes[value],
type
);
}
}
if (isDuplicate) {
context.report({
node,
messageId,
data: {
module: value
}
});
}
}
/**
* @callback nodeCallback
* @param {ASTNode} node A node to handle.
*/
/**
* Returns a function handling the imports of a given file
* @param {RuleContext} context The ESLint rule context object.
* @param {boolean} includeExports Whether or not to check for exports in addition to imports.
* @param {string[]} importsInFile The array containing other imports in the file.
* @param {string[]} exportsInFile The array containing other exports in the file.
* @param {{}} modulesWithImportTypes The object containing the name of unique modules with their first import type [specificImport, nameSpaceImport].
* @param {string[]} ExportAllDeclarationsInFile The array containing ExportAllDeclarations in the file.
*
* @returns {nodeCallback} A function passed to ESLint to handle the statement.
*/
function handleImports(
context,
includeExports,
importsInFile,
exportsInFile,
modulesWithImportTypes,
ExportAllDeclarationsInFile
) {
return function(node) {
const value = getValue(node);
const type = getImportType(node);
if (value) {
checkAndReport(
context,
node,
value,
importsInFile,
"import",
modulesWithImportTypes,
type,
ExportAllDeclarationsInFile
);
if (includeExports) {
checkAndReport(
context,
node,
value,
exportsInFile,
"importAs",
modulesWithImportTypes,
type,
ExportAllDeclarationsInFile
);
}
importsInFile.push(value);
if (modulesWithImportTypes[value]) {
modulesWithImportTypes[value] = modulesWithImportTypes[
value
].concat(type);
} else {
modulesWithImportTypes[value] = [type];
}
}
};
}
/**
* Returns a function handling the exports of a given file
* @param {RuleContext} context The ESLint rule context object.
* @param {string[]} importsInFile The array containing other imports in the file.
* @param {string[]} exportsInFile The array containing other exports in the file.
* @param {{}} modulesWithImportTypes The object containing the name of unique modules with their first import type [specificImport, nameSpaceImport].
* @param {string[]} ExportAllDeclarationsInFile The array containing ExportAllDeclarations in the file.
*
* @returns {nodeCallback} A function passed to ESLint to handle the statement.
*/
function handleExports(
context,
importsInFile,
exportsInFile,
modulesWithImportTypes,
ExportAllDeclarationsInFile
) {
return function(node) {
const value = getValue(node);
const type = getImportType(node);
if (value) {
checkAndReport(
context,
node,
value,
exportsInFile,
"export",
modulesWithImportTypes,
type,
ExportAllDeclarationsInFile
);
if (type === EXPORT_ALL_DECLARATION) {
ExportAllDeclarationsInFile.push(value);
} else {
checkAndReport(
context,
node,
value,
importsInFile,
"exportAs",
modulesWithImportTypes,
type,
ExportAllDeclarationsInFile
);
exportsInFile.push(value);
}
}
};
}
module.exports = {
meta: {
type: "problem",
docs: {
description: "disallow duplicate module imports",
category: "ECMAScript 6",
recommended: false,
url: "https://eslint.org/docs/rules/no-duplicate-imports"
},
schema: [
{
type: "object",
properties: {
includeExports: {
type: "boolean",
default: false
}
},
additionalProperties: false
}
],
messages: {
import: "'{{module}}' import is duplicated.",
importAs: "'{{module}}' import is duplicated as export.",
export: "'{{module}}' export is duplicated.",
exportAs: "'{{module}}' export is duplicated as import."
}
},
create(context) {
const includeExports = (context.options[0] || {}).includeExports,
importsInFile = [],
modulesWithImportTypes = {},
exportsInFile = [],
ExportAllDeclarationsInFile = [];
const handlers = {
ImportDeclaration: handleImports(
context,
includeExports,
importsInFile,
exportsInFile,
modulesWithImportTypes,
ExportAllDeclarationsInFile
)
};
if (includeExports) {
handlers.ExportNamedDeclaration = handleExports(
context,
importsInFile,
exportsInFile,
modulesWithImportTypes,
ExportAllDeclarationsInFile
);
handlers.ExportAllDeclaration = handleExports(
context,
importsInFile,
exportsInFile,
modulesWithImportTypes,
ExportAllDeclarationsInFile
);
}
return handlers;
}
};