Skip to content

Commit

Permalink
Fix: no-param-reassign false positive on destructuring (fixes #8279) (#…
Browse files Browse the repository at this point in the history
…8281)

* Fix: no-param-reassign false positive on destructuring (fixes #8279)

This updates no-param-reassign to stop traversing up the AST if it encounters a Property node where the variable reference is part of the key.

* Add test for object shorthand destructuring
  • Loading branch information
not-an-aardvark committed Mar 20, 2017
1 parent f8176b3 commit ddc6350
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/rules/no-param-reassign.js
Expand Up @@ -96,8 +96,15 @@ module.exports = {
}
break;

default:
// EXCLUDES: e.g. ({ [foo]: a }) = bar;
case "Property":
if (parent.key === node) {
return false;
}

break;

// no default
}

node = parent;
Expand Down
19 changes: 18 additions & 1 deletion tests/lib/rules/no-param-reassign.js
Expand Up @@ -35,7 +35,12 @@ ruleTester.run("no-param-reassign", rule, {
{ code: "function foo(a) { ++a.b; }", options: [{ props: true, ignorePropertyModificationsFor: ["a"] }] },
{ code: "function foo(a) { delete a.b; }", options: [{ props: true, ignorePropertyModificationsFor: ["a"] }] },
{ code: "function foo(a, z) { a.b = 0; x.y = 0; }", options: [{ props: true, ignorePropertyModificationsFor: ["a", "x"] }] },
{ code: "function foo(a) { a.b.c = 0;}", options: [{ props: true, ignorePropertyModificationsFor: ["a"] }] }
{ code: "function foo(a) { a.b.c = 0;}", options: [{ props: true, ignorePropertyModificationsFor: ["a"] }] },
{
code: "function foo(a) { ({ [a]: variable }) = value }",
options: [{ props: true }],
parserOptions: { ecmaVersion: 6 }
}
],

invalid: [
Expand Down Expand Up @@ -82,6 +87,18 @@ ruleTester.run("no-param-reassign", rule, {
parserOptions: { ecmaVersion: 6 },
options: [{ props: true, ignorePropertyModificationsFor: ["a"] }],
errors: [{ message: "Assignment to property of function parameter 'bar'." }]
},
{
code: "function foo(bar) { ({foo: bar.a} = {}); }",
parserOptions: { ecmaVersion: 6 },
options: [{ props: true }],
errors: [{ message: "Assignment to property of function parameter 'bar'." }]
},
{
code: "function foo(a) { ({a} = obj); }",
parserOptions: { ecmaVersion: 6 },
options: [{ props: true }],
errors: [{ message: "Assignment to function parameter 'a'." }]
}
]
});

0 comments on commit ddc6350

Please sign in to comment.