Skip to content

Commit

Permalink
Fix: id-blacklist should ignore ObjectPatterns (fixes #12787) (#12792)
Browse files Browse the repository at this point in the history
* Fix: Ignore destructured object property reassignments. (fixes #12787)

* Updated to ignore blacklisted id's in ObjectPattern nodes.

* Remove console.log.

* Added tests for nested destructuring and destructuring function parameters.

* Fix typo.

* Undo accidentally auto-fixed comment indent.
  • Loading branch information
jpramassini authored and btmills committed Jan 17, 2020
1 parent 561b6d4 commit 756b95d
Show file tree
Hide file tree
Showing 2 changed files with 29 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 !== "ObjectPattern" &&
isInvalid(name)
);
}

/**
Expand Down Expand Up @@ -98,11 +101,11 @@ module.exports = {
report(node);
}

// Report AssignmentExpressions only if they are the left side of the assignment
// 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);
}
Expand All @@ -115,7 +118,7 @@ module.exports = {
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
20 changes: 20 additions & 0 deletions tests/lib/rules/id-blacklist.js
Expand Up @@ -65,6 +65,26 @@ ruleTester.run("id-blacklist", rule, {
code: "var obj = { key: foo.bar };",
options: ["f", "fo", "fooo", "b", "ba", "barr", "bazz", "bingg"]
},
{
code: "const {foo: bar} = baz",
options: ["foo", "bar"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "const {foo: {bar: baz}} = qux",
options: ["foo", "bar", "baz"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "function foo({ bar: baz }) {}",
options: ["bar", "baz"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "function foo({ bar: {baz: qux} }) {}",
options: ["bar", "baz", "qux"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "var arr = [foo.bar];",
options: ["f", "fo", "fooo", "b", "ba", "barr", "bazz", "bingg"]
Expand Down

0 comments on commit 756b95d

Please sign in to comment.