Skip to content

Commit

Permalink
Update: Fix handling of property names in no-self-assign (#12105)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic authored and ilyavolodin committed Sep 14, 2019
1 parent 1ee61b0 commit b2498d2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/rules/no-self-assign.js
Expand Up @@ -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" &&
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/rules/no-self-assign.js
Expand Up @@ -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 }] },
Expand Down Expand Up @@ -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."] },
Expand Down

0 comments on commit b2498d2

Please sign in to comment.