diff --git a/lib/rules/no-useless-rename.js b/lib/rules/no-useless-rename.js index 4ff65d4b885..67af97c8bc0 100644 --- a/lib/rules/no-useless-rename.js +++ b/lib/rules/no-useless-rename.js @@ -36,13 +36,12 @@ module.exports = { }, create(context) { - const options = context.options[0] || {}, + const sourceCode = context.getSourceCode(), + options = context.options[0] || {}, ignoreDestructuring = options.ignoreDestructuring === true, ignoreImport = options.ignoreImport === true, ignoreExport = options.ignoreExport === true; - const sourceCode = context.getSourceCode(); - //-------------------------------------------------------------------------- // Helpers //-------------------------------------------------------------------------- @@ -69,10 +68,15 @@ module.exports = { if (sourceCode.commentsExistBetween(initial, result)) { return null; } + + const replacementText = result.type === "AssignmentPattern" + ? sourceCode.getText(result) + : name; + return fixer.replaceTextRange([ initial.range[0], result.range[1] - ], name); + ], replacementText); } }); } @@ -87,26 +91,21 @@ module.exports = { return; } - const properties = node.properties; - - for (let i = 0; i < properties.length; i++) { - if (properties[i].shorthand) { - continue; - } + for (const property of node.properties) { /** - * If an ObjectPattern property is computed, we have no idea - * if a rename is useless or not. If an ObjectPattern property - * lacks a key, it is likely an ExperimentalRestProperty and - * so there is no "renaming" occurring here. + * Properties using shorthand syntax and rest elements can not be renamed. + * If the property is computed, we have no idea if a rename is useless or not. */ - if (properties[i].computed || !properties[i].key) { + if (property.shorthand || property.type === "RestElement" || property.computed) { continue; } - if (properties[i].key.type === "Identifier" && properties[i].key.name === properties[i].value.name || - properties[i].key.type === "Literal" && properties[i].key.value === properties[i].value.name) { - reportError(properties[i], properties[i].key, properties[i].value, "Destructuring assignment"); + const key = (property.key.type === "Identifier" && property.key.name) || (property.key.type === "Literal" && property.key.value); + const renamedKey = property.value.type === "AssignmentPattern" ? property.value.left.name : property.value.name; + + if (key === renamedKey) { + reportError(property, property.key, property.value, "Destructuring assignment"); } } } diff --git a/tests/lib/rules/no-useless-rename.js b/tests/lib/rules/no-useless-rename.js index 58796c625aa..c9be46d695a 100644 --- a/tests/lib/rules/no-useless-rename.js +++ b/tests/lib/rules/no-useless-rename.js @@ -182,6 +182,21 @@ ruleTester.run("no-useless-rename", rule, { output: "let {'foo': {bar}, baz} = obj;", errors: ["Destructuring assignment bar unnecessarily renamed.", "Destructuring assignment baz unnecessarily renamed."] }, + { + code: "let {foo: foo = 1, 'bar': bar = 1, baz: baz} = obj;", + output: "let {foo = 1, bar = 1, baz} = obj;", + errors: ["Destructuring assignment foo unnecessarily renamed.", "Destructuring assignment bar unnecessarily renamed.", "Destructuring assignment baz unnecessarily renamed."] + }, + { + code: "let {foo: {bar: bar = 1, 'baz': baz = 1}} = obj;", + output: "let {foo: {bar = 1, baz = 1}} = obj;", + errors: ["Destructuring assignment bar unnecessarily renamed.", "Destructuring assignment baz unnecessarily renamed."] + }, + { + code: "let {foo: {bar: bar = {}} = {}} = obj;", + output: "let {foo: {bar = {}} = {}} = obj;", + errors: ["Destructuring assignment bar unnecessarily renamed."] + }, { code: "function func({foo: foo}) {}", output: "function func({foo}) {}", @@ -202,6 +217,21 @@ ruleTester.run("no-useless-rename", rule, { output: "function func({foo, bar}) {}", errors: ["Destructuring assignment foo unnecessarily renamed.", "Destructuring assignment bar unnecessarily renamed."] }, + { + code: "function func({foo: foo = 1, 'bar': bar = 1, baz: baz}) {}", + output: "function func({foo = 1, bar = 1, baz}) {}", + errors: ["Destructuring assignment foo unnecessarily renamed.", "Destructuring assignment bar unnecessarily renamed.", "Destructuring assignment baz unnecessarily renamed."] + }, + { + code: "function func({foo: {bar: bar = 1, 'baz': baz = 1}}) {}", + output: "function func({foo: {bar = 1, baz = 1}}) {}", + errors: ["Destructuring assignment bar unnecessarily renamed.", "Destructuring assignment baz unnecessarily renamed."] + }, + { + code: "function func({foo: {bar: bar = {}} = {}}) {}", + output: "function func({foo: {bar = {}} = {}}) {}", + errors: ["Destructuring assignment bar unnecessarily renamed."] + }, { code: "({foo: foo}) => {}", output: "({foo}) => {}", @@ -222,6 +252,21 @@ ruleTester.run("no-useless-rename", rule, { output: "({foo, bar}) => {}", errors: ["Destructuring assignment foo unnecessarily renamed.", "Destructuring assignment bar unnecessarily renamed."] }, + { + code: "({foo: foo = 1, 'bar': bar = 1, baz: baz}) => {}", + output: "({foo = 1, bar = 1, baz}) => {}", + errors: ["Destructuring assignment foo unnecessarily renamed.", "Destructuring assignment bar unnecessarily renamed.", "Destructuring assignment baz unnecessarily renamed."] + }, + { + code: "({foo: {bar: bar = 1, 'baz': baz = 1}}) => {}", + output: "({foo: {bar = 1, baz = 1}}) => {}", + errors: ["Destructuring assignment bar unnecessarily renamed.", "Destructuring assignment baz unnecessarily renamed."] + }, + { + code: "({foo: {bar: bar = {}} = {}}) => {}", + output: "({foo: {bar = {}} = {}}) => {}", + errors: ["Destructuring assignment bar unnecessarily renamed."] + }, { code: "const {foo: foo, ...stuff} = myObject;", output: "const {foo, ...stuff} = myObject;",