Skip to content

Commit ddc6350

Browse files
Fix: no-param-reassign false positive on destructuring (fixes #8279) (#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
1 parent f8176b3 commit ddc6350

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

lib/rules/no-param-reassign.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,15 @@ module.exports = {
9696
}
9797
break;
9898

99-
default:
99+
// EXCLUDES: e.g. ({ [foo]: a }) = bar;
100+
case "Property":
101+
if (parent.key === node) {
102+
return false;
103+
}
104+
100105
break;
106+
107+
// no default
101108
}
102109

103110
node = parent;

tests/lib/rules/no-param-reassign.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ ruleTester.run("no-param-reassign", rule, {
3535
{ code: "function foo(a) { ++a.b; }", options: [{ props: true, ignorePropertyModificationsFor: ["a"] }] },
3636
{ code: "function foo(a) { delete a.b; }", options: [{ props: true, ignorePropertyModificationsFor: ["a"] }] },
3737
{ code: "function foo(a, z) { a.b = 0; x.y = 0; }", options: [{ props: true, ignorePropertyModificationsFor: ["a", "x"] }] },
38-
{ code: "function foo(a) { a.b.c = 0;}", options: [{ props: true, ignorePropertyModificationsFor: ["a"] }] }
38+
{ code: "function foo(a) { a.b.c = 0;}", options: [{ props: true, ignorePropertyModificationsFor: ["a"] }] },
39+
{
40+
code: "function foo(a) { ({ [a]: variable }) = value }",
41+
options: [{ props: true }],
42+
parserOptions: { ecmaVersion: 6 }
43+
}
3944
],
4045

4146
invalid: [
@@ -82,6 +87,18 @@ ruleTester.run("no-param-reassign", rule, {
8287
parserOptions: { ecmaVersion: 6 },
8388
options: [{ props: true, ignorePropertyModificationsFor: ["a"] }],
8489
errors: [{ message: "Assignment to property of function parameter 'bar'." }]
90+
},
91+
{
92+
code: "function foo(bar) { ({foo: bar.a} = {}); }",
93+
parserOptions: { ecmaVersion: 6 },
94+
options: [{ props: true }],
95+
errors: [{ message: "Assignment to property of function parameter 'bar'." }]
96+
},
97+
{
98+
code: "function foo(a) { ({a} = obj); }",
99+
parserOptions: { ecmaVersion: 6 },
100+
options: [{ props: true }],
101+
errors: [{ message: "Assignment to function parameter 'a'." }]
85102
}
86103
]
87104
});

0 commit comments

Comments
 (0)