UNPKG

4.53 kBJavaScriptView Raw
1/**
2 * @fileoverview Restrict usage of duplicate imports.
3 * @author Simen Bekkhus
4 */
5"use strict";
6
7//------------------------------------------------------------------------------
8// Rule Definition
9//------------------------------------------------------------------------------
10
11/**
12 * Returns the name of the module imported or re-exported.
13 *
14 * @param {ASTNode} node - A node to get.
15 * @returns {string} the name of the module, or empty string if no name.
16 */
17function getValue(node) {
18 if (node && node.source && node.source.value) {
19 return node.source.value.trim();
20 }
21
22 return "";
23}
24
25/**
26 * Checks if the name of the import or export exists in the given array, and reports if so.
27 *
28 * @param {RuleContext} context - The ESLint rule context object.
29 * @param {ASTNode} node - A node to get.
30 * @param {string} value - The name of the imported or exported module.
31 * @param {string[]} array - The array containing other imports or exports in the file.
32 * @param {string} messageId - A messageId to be reported after the name of the module
33 *
34 * @returns {void} No return value
35 */
36function checkAndReport(context, node, value, array, messageId) {
37 if (array.indexOf(value) !== -1) {
38 context.report({
39 node,
40 messageId,
41 data: {
42 module: value
43 }
44 });
45 }
46}
47
48/**
49 * @callback nodeCallback
50 * @param {ASTNode} node - A node to handle.
51 */
52
53/**
54 * Returns a function handling the imports of a given file
55 *
56 * @param {RuleContext} context - The ESLint rule context object.
57 * @param {boolean} includeExports - Whether or not to check for exports in addition to imports.
58 * @param {string[]} importsInFile - The array containing other imports in the file.
59 * @param {string[]} exportsInFile - The array containing other exports in the file.
60 *
61 * @returns {nodeCallback} A function passed to ESLint to handle the statement.
62 */
63function handleImports(context, includeExports, importsInFile, exportsInFile) {
64 return function(node) {
65 const value = getValue(node);
66
67 if (value) {
68 checkAndReport(context, node, value, importsInFile, "import");
69
70 if (includeExports) {
71 checkAndReport(context, node, value, exportsInFile, "importAs");
72 }
73
74 importsInFile.push(value);
75 }
76 };
77}
78
79/**
80 * Returns a function handling the exports of a given file
81 *
82 * @param {RuleContext} context - The ESLint rule context object.
83 * @param {string[]} importsInFile - The array containing other imports in the file.
84 * @param {string[]} exportsInFile - The array containing other exports in the file.
85 *
86 * @returns {nodeCallback} A function passed to ESLint to handle the statement.
87 */
88function handleExports(context, importsInFile, exportsInFile) {
89 return function(node) {
90 const value = getValue(node);
91
92 if (value) {
93 checkAndReport(context, node, value, exportsInFile, "export");
94 checkAndReport(context, node, value, importsInFile, "exportAs");
95
96 exportsInFile.push(value);
97 }
98 };
99}
100
101module.exports = {
102 meta: {
103 type: "problem",
104
105 docs: {
106 description: "disallow duplicate module imports",
107 category: "ECMAScript 6",
108 recommended: false,
109 url: "https://eslint.org/docs/rules/no-duplicate-imports"
110 },
111
112 schema: [{
113 type: "object",
114 properties: {
115 includeExports: {
116 type: "boolean",
117 default: false
118 }
119 },
120 additionalProperties: false
121 }],
122 messages: {
123 import: "'{{module}}' import is duplicated.",
124 importAs: "'{{module}}' import is duplicated as export.",
125 export: "'{{module}}' export is duplicated.",
126 exportAs: "'{{module}}' export is duplicated as import."
127 }
128 },
129
130 create(context) {
131 const includeExports = (context.options[0] || {}).includeExports,
132 importsInFile = [],
133 exportsInFile = [];
134
135 const handlers = {
136 ImportDeclaration: handleImports(context, includeExports, importsInFile, exportsInFile)
137 };
138
139 if (includeExports) {
140 handlers.ExportNamedDeclaration = handleExports(context, importsInFile, exportsInFile);
141 handlers.ExportAllDeclaration = handleExports(context, importsInFile, exportsInFile);
142 }
143
144 return handlers;
145 }
146};