Skip to content

Commit

Permalink
Fix: Ignore destructured object property reassignments. (fixes eslint…
Browse files Browse the repository at this point in the history
  • Loading branch information
jpramassini committed Jan 15, 2020
1 parent a1d999c commit ec3983c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/rules/id-blacklist.js
Expand Up @@ -61,9 +61,12 @@ module.exports = {
* @returns {boolean} whether an error should be reported or not
*/
function shouldReport(effectiveParent, name) {
return effectiveParent.type !== "CallExpression" &&
return (
effectiveParent.type !== "CallExpression" &&
effectiveParent.type !== "NewExpression" &&
isInvalid(name);
effectiveParent.parent.type !== "BlockStatement" &&
isInvalid(name)
);
}

/**
Expand Down Expand Up @@ -101,21 +104,21 @@ module.exports = {
// Report AssignmentExpressions only if they are the left side of the assignment
} else if (effectiveParent.type === "AssignmentExpression" &&
(effectiveParent.right.type !== "MemberExpression" ||
effectiveParent.left.type === "MemberExpression" &&
effectiveParent.left.property.name === node.name)) {
effectiveParent.left.type === "MemberExpression" &&
effectiveParent.left.property.name === node.name)) {
if (isInvalid(name)) {
report(node);
}
}

// Properties have their own rules
// Properties have their own rules
} else if (node.parent.type === "Property") {

if (shouldReport(effectiveParent, name)) {
report(node);
}

// Report anything that is a match and not a CallExpression
// Report anything that is a match and not a CallExpression
} else if (shouldReport(effectiveParent, name)) {
report(node);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/rules/id-blacklist.js
Expand Up @@ -65,6 +65,10 @@ ruleTester.run("id-blacklist", rule, {
code: "var obj = { key: foo.bar };",
options: ["f", "fo", "fooo", "b", "ba", "barr", "bazz", "bingg"]
},
{
code: "{foo: bar}",
options: ["foo"]
},
{
code: "var arr = [foo.bar];",
options: ["f", "fo", "fooo", "b", "ba", "barr", "bazz", "bingg"]
Expand Down Expand Up @@ -176,6 +180,13 @@ ruleTester.run("id-blacklist", rule, {
error
]
},
{
code: "{foo: bar}",
options: ["foo", "bar"],
errors: [
error
]
},
{
code: "var arr = [foo.bar];",
options: ["arr"],
Expand Down

0 comments on commit ec3983c

Please sign in to comment.