2 * @fileoverview Disallow renaming import, export, and destructured assignments to the same name.
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils = require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
23 description: "disallow renaming import, export, and destructured assignments to the same name",
24 category: "ECMAScript 6",
26 url: "https://eslint.org/docs/rules/no-useless-rename"
35 ignoreDestructuring: { type: "boolean", default: false },
36 ignoreImport: { type: "boolean", default: false },
37 ignoreExport: { type: "boolean", default: false }
39 additionalProperties: false
44 unnecessarilyRenamed: "{{type}} {{name}} unnecessarily renamed."
49 const sourceCode = context.getSourceCode(),
50 options = context.options[0] || {},
51 ignoreDestructuring = options.ignoreDestructuring === true,
52 ignoreImport = options.ignoreImport === true,
53 ignoreExport = options.ignoreExport === true;
55 //--------------------------------------------------------------------------
57 //--------------------------------------------------------------------------
60 * Reports error for unnecessarily renamed assignments
61 * @param {ASTNode} node node to report
62 * @param {ASTNode} initial node with initial name value
63 * @param {string} type the type of the offending node
66 function reportError(node, initial, type) {
67 const name = initial.type === "Identifier" ? initial.name : initial.value;
69 return context.report({
71 messageId: "unnecessarilyRenamed",
77 const replacementNode = node.type === "Property" ? node.value : node.local;
79 if (sourceCode.getCommentsInside(node).length > sourceCode.getCommentsInside(replacementNode).length) {
83 // Don't autofix code such as `({foo: (foo) = a} = obj);`, parens are not allowed in shorthand properties.
85 replacementNode.type === "AssignmentPattern" &&
86 astUtils.isParenthesised(sourceCode, replacementNode.left)
91 return fixer.replaceText(node, sourceCode.getText(replacementNode));
97 * Checks whether a destructured assignment is unnecessarily renamed
98 * @param {ASTNode} node node to check
101 function checkDestructured(node) {
102 if (ignoreDestructuring) {
106 for (const property of node.properties) {
109 * Properties using shorthand syntax and rest elements can not be renamed.
110 * If the property is computed, we have no idea if a rename is useless or not.
112 if (property.type !== "Property" || property.shorthand || property.computed) {
116 const key = (property.key.type === "Identifier" && property.key.name) || (property.key.type === "Literal" && property.key.value);
117 const renamedKey = property.value.type === "AssignmentPattern" ? property.value.left.name : property.value.name;
119 if (key === renamedKey) {
120 reportError(property, property.key, "Destructuring assignment");
126 * Checks whether an import is unnecessarily renamed
127 * @param {ASTNode} node node to check
130 function checkImport(node) {
135 if (node.imported.name === node.local.name &&
136 node.imported.range[0] !== node.local.range[0]) {
137 reportError(node, node.imported, "Import");
142 * Checks whether an export is unnecessarily renamed
143 * @param {ASTNode} node node to check
146 function checkExport(node) {
151 if (node.local.name === node.exported.name &&
152 node.local.range[0] !== node.exported.range[0]) {
153 reportError(node, node.local, "Export");
158 //--------------------------------------------------------------------------
160 //--------------------------------------------------------------------------
163 ObjectPattern: checkDestructured,
164 ImportSpecifier: checkImport,
165 ExportSpecifier: checkExport