2 * @fileoverview Disallow renaming import, export, and destructured assignments to the same name.
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
17 description: "disallow renaming import, export, and destructured assignments to the same name",
18 category: "ECMAScript 6",
20 url: "https://eslint.org/docs/rules/no-useless-rename"
29 ignoreDestructuring: { type: "boolean", default: false },
30 ignoreImport: { type: "boolean", default: false },
31 ignoreExport: { type: "boolean", default: false }
33 additionalProperties: false
39 const sourceCode = context.getSourceCode(),
40 options = context.options[0] || {},
41 ignoreDestructuring = options.ignoreDestructuring === true,
42 ignoreImport = options.ignoreImport === true,
43 ignoreExport = options.ignoreExport === true;
45 //--------------------------------------------------------------------------
47 //--------------------------------------------------------------------------
50 * Reports error for unnecessarily renamed assignments
51 * @param {ASTNode} node node to report
52 * @param {ASTNode} initial node with initial name value
53 * @param {ASTNode} result node with new name value
54 * @param {string} type the type of the offending node
57 function reportError(node, initial, result, type) {
58 const name = initial.type === "Identifier" ? initial.name : initial.value;
60 return context.report({
62 message: "{{type}} {{name}} unnecessarily renamed.",
68 if (sourceCode.commentsExistBetween(initial, result)) {
72 const replacementText = result.type === "AssignmentPattern"
73 ? sourceCode.getText(result)
76 return fixer.replaceTextRange([
85 * Checks whether a destructured assignment is unnecessarily renamed
86 * @param {ASTNode} node node to check
89 function checkDestructured(node) {
90 if (ignoreDestructuring) {
94 for (const property of node.properties) {
97 * TODO: Remove after babel-eslint removes ExperimentalRestProperty
98 * https://github.com/eslint/eslint/issues/12335
100 if (property.type === "ExperimentalRestProperty") {
105 * Properties using shorthand syntax and rest elements can not be renamed.
106 * If the property is computed, we have no idea if a rename is useless or not.
108 if (property.shorthand || property.type === "RestElement" || property.computed) {
112 const key = (property.key.type === "Identifier" && property.key.name) || (property.key.type === "Literal" && property.key.value);
113 const renamedKey = property.value.type === "AssignmentPattern" ? property.value.left.name : property.value.name;
115 if (key === renamedKey) {
116 reportError(property, property.key, property.value, "Destructuring assignment");
122 * Checks whether an import is unnecessarily renamed
123 * @param {ASTNode} node node to check
126 function checkImport(node) {
131 if (node.imported.name === node.local.name &&
132 node.imported.range[0] !== node.local.range[0]) {
133 reportError(node, node.imported, node.local, "Import");
138 * Checks whether an export is unnecessarily renamed
139 * @param {ASTNode} node node to check
142 function checkExport(node) {
147 if (node.local.name === node.exported.name &&
148 node.local.range[0] !== node.exported.range[0]) {
149 reportError(node, node.local, node.exported, "Export");
154 //--------------------------------------------------------------------------
156 //--------------------------------------------------------------------------
159 ObjectPattern: checkDestructured,
160 ImportSpecifier: checkImport,
161 ExportSpecifier: checkExport