From 28715cf15c245bb0b15412ccde9e287fe7f0085d Mon Sep 17 00:00:00 2001 From: Zzzen Date: Mon, 1 Mar 2021 21:57:50 +0800 Subject: [PATCH 1/2] Fix: handle logical assignment in no-self-assign --- lib/rules/no-self-assign.js | 2 +- tests/lib/rules/no-self-assign.js | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/rules/no-self-assign.js b/lib/rules/no-self-assign.js index 705be324cf0..6ee37b75825 100644 --- a/lib/rules/no-self-assign.js +++ b/lib/rules/no-self-assign.js @@ -174,7 +174,7 @@ module.exports = { return { AssignmentExpression(node) { - if (node.operator === "=") { + if (["=", "&=", "|=", "&&=", "||=", "??="].includes(node.operator)) { eachSelfAssignment(node.left, node.right, props, report); } } diff --git a/tests/lib/rules/no-self-assign.js b/tests/lib/rules/no-self-assign.js index 5a9bb6fcfed..89ee20b9278 100644 --- a/tests/lib/rules/no-self-assign.js +++ b/tests/lib/rules/no-self-assign.js @@ -147,6 +147,31 @@ ruleTester.run("no-self-assign", rule, { code: "a.b = a?.b", parserOptions: { ecmaVersion: 2020 }, errors: [{ messageId: "selfAssignment", data: { name: "a?.b" } }] + }, + + // logical assignment + { + code: "a &= a", + errors: [{ messageId: "selfAssignment", data: { name: "a" } }] + }, + { + code: "a |= a", + errors: [{ messageId: "selfAssignment", data: { name: "a" } }] + }, + { + code: "a &&= a", + parserOptions: { ecmaVersion: 2021 }, + errors: [{ messageId: "selfAssignment", data: { name: "a" } }] + }, + { + code: "a ||= a", + parserOptions: { ecmaVersion: 2021 }, + errors: [{ messageId: "selfAssignment", data: { name: "a" } }] + }, + { + code: "a ??= a", + parserOptions: { ecmaVersion: 2021 }, + errors: [{ messageId: "selfAssignment", data: { name: "a" } }] } ] }); From 2dc996f51f3cc56143500446b268c685f50cad9c Mon Sep 17 00:00:00 2001 From: Zzzen Date: Thu, 30 Dec 2021 11:20:17 +0800 Subject: [PATCH 2/2] ignore `&=` and `|=` --- docs/rules/no-self-assign.md | 8 ++++++++ lib/rules/no-self-assign.js | 2 +- tests/lib/rules/no-self-assign.js | 10 ++-------- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/docs/rules/no-self-assign.md b/docs/rules/no-self-assign.md index 2991504e838..c8f944d0446 100644 --- a/docs/rules/no-self-assign.md +++ b/docs/rules/no-self-assign.md @@ -24,6 +24,10 @@ foo = foo; [a, ...b] = [x, ...b]; ({a, b} = {a, x}); + +foo &&= foo; +foo ||= foo; +foo ??= foo; ``` Examples of **correct** code for this rule: @@ -50,6 +54,10 @@ obj[a] = obj["a"]; obj.a().b = obj.a().b; a().b = a().b; +// `&=` and `|=` have an effect on non-integers. +foo &= foo; +foo |= foo; + // Known limitation: this does not support computed properties except single literal or single identifier. obj[a + b] = obj[a + b]; obj["a" + "b"] = obj["a" + "b"]; diff --git a/lib/rules/no-self-assign.js b/lib/rules/no-self-assign.js index 6ee37b75825..2eaba74fc38 100644 --- a/lib/rules/no-self-assign.js +++ b/lib/rules/no-self-assign.js @@ -174,7 +174,7 @@ module.exports = { return { AssignmentExpression(node) { - if (["=", "&=", "|=", "&&=", "||=", "??="].includes(node.operator)) { + if (["=", "&&=", "||=", "??="].includes(node.operator)) { eachSelfAssignment(node.left, node.right, props, report); } } diff --git a/tests/lib/rules/no-self-assign.js b/tests/lib/rules/no-self-assign.js index 89ee20b9278..adbc7a2b553 100644 --- a/tests/lib/rules/no-self-assign.js +++ b/tests/lib/rules/no-self-assign.js @@ -25,6 +25,8 @@ ruleTester.run("no-self-assign", rule, { "a += a", "a = +a", "a = [a]", + "a &= a", + "a |= a", { code: "let a = a", parserOptions: { ecmaVersion: 6 } }, { code: "const a = a", parserOptions: { ecmaVersion: 6 } }, { code: "[a] = a", parserOptions: { ecmaVersion: 6 } }, @@ -150,14 +152,6 @@ ruleTester.run("no-self-assign", rule, { }, // logical assignment - { - code: "a &= a", - errors: [{ messageId: "selfAssignment", data: { name: "a" } }] - }, - { - code: "a |= a", - errors: [{ messageId: "selfAssignment", data: { name: "a" } }] - }, { code: "a &&= a", parserOptions: { ecmaVersion: 2021 },