Skip to content

Commit

Permalink
Update: no-useless-rename also reports default values (fixes #12301) (#…
Browse files Browse the repository at this point in the history
…12322)

* Update: no-useless-rename also reports default values (fixes #12301)

* Refactor
  • Loading branch information
kaicataldo authored and platinumazure committed Sep 29, 2019
1 parent 41bfe91 commit 334ca7c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 18 deletions.
35 changes: 17 additions & 18 deletions lib/rules/no-useless-rename.js
Expand Up @@ -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
//--------------------------------------------------------------------------
Expand All @@ -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);
}
});
}
Expand All @@ -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");
}
}
}
Expand Down
45 changes: 45 additions & 0 deletions tests/lib/rules/no-useless-rename.js
Expand Up @@ -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}) {}",
Expand All @@ -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}) => {}",
Expand All @@ -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;",
Expand Down

0 comments on commit 334ca7c

Please sign in to comment.