Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix: no-self-assign false positive with rest and spread in array (#12099
)
  • Loading branch information
mdjermanovic authored and kaicataldo committed Aug 26, 2019
1 parent a81d263 commit 644ce33
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/rules/no-self-assign.js
Expand Up @@ -94,9 +94,19 @@ function eachSelfAssignment(left, right, props, report) {
const end = Math.min(left.elements.length, right.elements.length);

for (let i = 0; i < end; ++i) {
const leftElement = left.elements[i];
const rightElement = right.elements[i];

eachSelfAssignment(left.elements[i], rightElement, props, report);
// Avoid cases such as [...a] = [...a, 1]
if (
leftElement &&
leftElement.type === "RestElement" &&
i < right.elements.length - 1
) {
break;
}

eachSelfAssignment(leftElement, rightElement, props, report);

// After a spread element, those indices are unknown.
if (rightElement && rightElement.type === "SpreadElement") {
Expand Down
2 changes: 2 additions & 0 deletions tests/lib/rules/no-self-assign.js
Expand Up @@ -32,6 +32,8 @@ ruleTester.run("no-self-assign", rule, {
{ code: "[a, b] = [b, a]", parserOptions: { ecmaVersion: 6 } },
{ code: "[a,, b] = [, b, a]", parserOptions: { ecmaVersion: 6 } },
{ code: "[x, a] = [...x, a]", parserOptions: { ecmaVersion: 6 } },
{ code: "[...a] = [...a, 1]", parserOptions: { ecmaVersion: 6 } },
{ code: "[a, ...b] = [0, ...b, 1]", parserOptions: { ecmaVersion: 6 } },
{ code: "[a, b] = {a, b}", parserOptions: { ecmaVersion: 6 } },
{ code: "({a} = a)", parserOptions: { ecmaVersion: 6 } },
{ code: "({a = 1} = {a})", parserOptions: { ecmaVersion: 6 } },
Expand Down

0 comments on commit 644ce33

Please sign in to comment.