2 * @fileoverview Restrict usage of duplicate imports.
3 * @author Simen Bekkhus
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
12 * Returns the name of the module imported or re-exported.
13 * @param {ASTNode} node A node to get.
14 * @returns {string} the name of the module, or empty string if no name.
16 function getValue(node) {
17 if (node && node.source && node.source.value) {
18 return node.source.value.trim();
25 * Checks if the name of the import or export exists in the given array, and reports if so.
26 * @param {RuleContext} context The ESLint rule context object.
27 * @param {ASTNode} node A node to get.
28 * @param {string} value The name of the imported or exported module.
29 * @param {string[]} array The array containing other imports or exports in the file.
30 * @param {string} messageId A messageId to be reported after the name of the module
32 * @returns {void} No return value
34 function checkAndReport(context, node, value, array, messageId) {
35 if (array.indexOf(value) !== -1) {
47 * @callback nodeCallback
48 * @param {ASTNode} node A node to handle.
52 * Returns a function handling the imports of a given file
53 * @param {RuleContext} context The ESLint rule context object.
54 * @param {boolean} includeExports Whether or not to check for exports in addition to imports.
55 * @param {string[]} importsInFile The array containing other imports in the file.
56 * @param {string[]} exportsInFile The array containing other exports in the file.
58 * @returns {nodeCallback} A function passed to ESLint to handle the statement.
60 function handleImports(context, includeExports, importsInFile, exportsInFile) {
61 return function(node) {
62 const value = getValue(node);
65 checkAndReport(context, node, value, importsInFile, "import");
68 checkAndReport(context, node, value, exportsInFile, "importAs");
71 importsInFile.push(value);
77 * Returns a function handling the exports of a given file
78 * @param {RuleContext} context The ESLint rule context object.
79 * @param {string[]} importsInFile The array containing other imports in the file.
80 * @param {string[]} exportsInFile The array containing other exports in the file.
82 * @returns {nodeCallback} A function passed to ESLint to handle the statement.
84 function handleExports(context, importsInFile, exportsInFile) {
85 return function(node) {
86 const value = getValue(node);
89 checkAndReport(context, node, value, exportsInFile, "export");
90 checkAndReport(context, node, value, importsInFile, "exportAs");
92 exportsInFile.push(value);
102 description: "disallow duplicate module imports",
103 category: "ECMAScript 6",
105 url: "https://eslint.org/docs/rules/no-duplicate-imports"
116 additionalProperties: false
119 import: "'{{module}}' import is duplicated.",
120 importAs: "'{{module}}' import is duplicated as export.",
121 export: "'{{module}}' export is duplicated.",
122 exportAs: "'{{module}}' export is duplicated as import."
127 const includeExports = (context.options[0] || {}).includeExports,
132 ImportDeclaration: handleImports(context, includeExports, importsInFile, exportsInFile)
135 if (includeExports) {
136 handlers.ExportNamedDeclaration = handleExports(context, importsInFile, exportsInFile);
137 handlers.ExportAllDeclaration = handleExports(context, importsInFile, exportsInFile);