Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: no-self-assign false positive with rest and spread in array #12099

Merged
merged 1 commit into from Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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