diff --git a/lib/rules/no-self-assign.js b/lib/rules/no-self-assign.js index dc8bf90147b..6ebd1925e49 100644 --- a/lib/rules/no-self-assign.js +++ b/lib/rules/no-self-assign.js @@ -152,13 +152,14 @@ function eachSelfAssignment(left, right, props, report) { } else if ( left.type === "Property" && right.type === "Property" && - !left.computed && - !right.computed && right.kind === "init" && - !right.method && - left.key.name === right.key.name + !right.method ) { - eachSelfAssignment(left.value, right.value, props, report); + const leftName = astUtils.getStaticPropertyName(left); + + if (leftName !== null && leftName === astUtils.getStaticPropertyName(right)) { + eachSelfAssignment(left.value, right.value, props, report); + } } else if ( props && left.type === "MemberExpression" && diff --git a/tests/lib/rules/no-self-assign.js b/tests/lib/rules/no-self-assign.js index fbf52dcba0e..4e168a89789 100644 --- a/tests/lib/rules/no-self-assign.js +++ b/tests/lib/rules/no-self-assign.js @@ -41,6 +41,8 @@ ruleTester.run("no-self-assign", rule, { { code: "({a} = {a: b})", parserOptions: { ecmaVersion: 6 } }, { code: "({a} = {a() {}})", parserOptions: { ecmaVersion: 6 } }, { code: "({a} = {[a]: a})", parserOptions: { ecmaVersion: 6 } }, + { code: "({[a]: b} = {[a]: b})", parserOptions: { ecmaVersion: 6 } }, + { code: "({'foo': a, 1: a} = {'bar': a, 2: a})", parserOptions: { ecmaVersion: 6 } }, { code: "({a, ...b} = {a, ...b})", parserOptions: { ecmaVersion: 2018 } }, { code: "a.b = a.c", options: [{ props: true }] }, { code: "a.b = c.b", options: [{ props: true }] }, @@ -80,6 +82,15 @@ ruleTester.run("no-self-assign", rule, { { code: "[[a], {b}] = [[a], {b}]", parserOptions: { ecmaVersion: 6 }, errors: ["'a' is assigned to itself.", "'b' is assigned to itself."] }, { code: "({a} = {a})", parserOptions: { ecmaVersion: 6 }, errors: ["'a' is assigned to itself."] }, { code: "({a: b} = {a: b})", parserOptions: { ecmaVersion: 6 }, errors: ["'b' is assigned to itself."] }, + { code: "({'a': b} = {'a': b})", parserOptions: { ecmaVersion: 6 }, errors: ["'b' is assigned to itself."] }, + { code: "({a: b} = {'a': b})", parserOptions: { ecmaVersion: 6 }, errors: ["'b' is assigned to itself."] }, + { code: "({'a': b} = {a: b})", parserOptions: { ecmaVersion: 6 }, errors: ["'b' is assigned to itself."] }, + { code: "({1: b} = {1: b})", parserOptions: { ecmaVersion: 6 }, errors: ["'b' is assigned to itself."] }, + { code: "({1: b} = {'1': b})", parserOptions: { ecmaVersion: 6 }, errors: ["'b' is assigned to itself."] }, + { code: "({'1': b} = {1: b})", parserOptions: { ecmaVersion: 6 }, errors: ["'b' is assigned to itself."] }, + { code: "({['a']: b} = {a: b})", parserOptions: { ecmaVersion: 6 }, errors: ["'b' is assigned to itself."] }, + { code: "({'a': b} = {[`a`]: b})", parserOptions: { ecmaVersion: 6 }, errors: ["'b' is assigned to itself."] }, + { code: "({1: b} = {[1]: b})", parserOptions: { ecmaVersion: 6 }, errors: ["'b' is assigned to itself."] }, { code: "({a, b} = {a, b})", parserOptions: { ecmaVersion: 6 }, errors: ["'a' is assigned to itself.", "'b' is assigned to itself."] }, { code: "({a, b} = {b, a})", parserOptions: { ecmaVersion: 6 }, errors: ["'b' is assigned to itself.", "'a' is assigned to itself."] }, { code: "({a, b} = {c, a})", parserOptions: { ecmaVersion: 6 }, errors: ["'a' is assigned to itself."] },