From b962775b8cb7c90985a5ab63e56744bb2ba79644 Mon Sep 17 00:00:00 2001 From: Tibor Blenessy Date: Tue, 15 Oct 2019 23:55:02 +0200 Subject: [PATCH] Update: no-self-assign should detect member expression with this (#12279) * Update: no-self-assign should detect member expression with this * Remove redundant condition --- lib/rules/no-self-assign.js | 3 +++ tests/lib/rules/no-self-assign.js | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/rules/no-self-assign.js b/lib/rules/no-self-assign.js index 9a06ebba6bc..705d0f409c4 100644 --- a/lib/rules/no-self-assign.js +++ b/lib/rules/no-self-assign.js @@ -61,6 +61,9 @@ function isSameMember(left, right) { if (lobj.type === "MemberExpression") { return isSameMember(lobj, robj); } + if (lobj.type === "ThisExpression") { + return true; + } return lobj.type === "Identifier" && lobj.name === robj.name; } diff --git a/tests/lib/rules/no-self-assign.js b/tests/lib/rules/no-self-assign.js index 4e168a89789..eaa0d02d12d 100644 --- a/tests/lib/rules/no-self-assign.js +++ b/tests/lib/rules/no-self-assign.js @@ -70,6 +70,14 @@ ruleTester.run("no-self-assign", rule, { { code: "a[\n 'b'\n] = a[\n 'b'\n]", options: [{ props: false }] + }, + { + code: "this.x = this.y", + options: [{ props: true }] + }, + { + code: "this.x = this.x", + options: [{ props: false }] } ], invalid: [ @@ -120,6 +128,11 @@ ruleTester.run("no-self-assign", rule, { { code: "a.b.c = a.b.c", options: [{ props: true }], errors: ["'a.b.c' is assigned to itself."] }, { code: "a[b] = a[b]", options: [{ props: true }], errors: ["'a[b]' is assigned to itself."] }, { code: "a['b'] = a['b']", options: [{ props: true }], errors: ["'a['b']' is assigned to itself."] }, - { code: "a[\n 'b'\n] = a[\n 'b'\n]", options: [{ props: true }], errors: ["'a['b']' is assigned to itself."] } + { code: "a[\n 'b'\n] = a[\n 'b'\n]", options: [{ props: true }], errors: ["'a['b']' is assigned to itself."] }, + { + code: "this.x = this.x", + options: [{ props: true }], + errors: ["'this.x' is assigned to itself."] + } ] });