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
  • Loading branch information
kaicataldo committed Sep 29, 2019
1 parent 160b7c4 commit c6da1e4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
24 changes: 17 additions & 7 deletions lib/rules/no-useless-rename.js
Expand Up @@ -36,7 +36,8 @@ 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;
Expand Down Expand Up @@ -69,10 +70,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 @@ -90,7 +96,9 @@ module.exports = {
const properties = node.properties;

for (let i = 0; i < properties.length; i++) {
if (properties[i].shorthand) {
const property = properties[i];

if (property.shorthand) {
continue;
}

Expand All @@ -100,13 +108,15 @@ module.exports = {
* lacks a key, it is likely an ExperimentalRestProperty and
* so there is no "renaming" occurring here.
*/
if (properties[i].computed || !properties[i].key) {
if (property.computed || !property.key) {
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 c6da1e4

Please sign in to comment.