From 874037a240714287a9e98afd2717181d3aee2cd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Garc=C3=ADa?= Date: Tue, 22 Mar 2022 21:31:11 -0500 Subject: [PATCH 1/2] fix: add assignment expression logic --- packages/eslint-plugin/src/rules/no-this-alias.ts | 11 ++++++++--- .../eslint-plugin/tests/rules/no-this-alias.test.ts | 7 +++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-this-alias.ts b/packages/eslint-plugin/src/rules/no-this-alias.ts index 5c9c9f56881..0d79b3eca6e 100644 --- a/packages/eslint-plugin/src/rules/no-this-alias.ts +++ b/packages/eslint-plugin/src/rules/no-this-alias.ts @@ -48,11 +48,16 @@ export default util.createRule({ ], create(context, [{ allowDestructuring, allowedNames }]) { return { - "VariableDeclarator[init.type='ThisExpression']"( - node: TSESTree.VariableDeclarator, + "VariableDeclarator[init.type='ThisExpression'], AssignmentExpression[right.type='ThisExpression']"( + node: TSESTree.VariableDeclarator | TSESTree.AssignmentExpression, ): void { - const { id } = node; + let id; + if (node.type === AST_NODE_TYPES.VariableDeclarator) { + id = node.id; + } else { + id = node.left; + } if (allowDestructuring && id.type !== AST_NODE_TYPES.Identifier) { return; } diff --git a/packages/eslint-plugin/tests/rules/no-this-alias.test.ts b/packages/eslint-plugin/tests/rules/no-this-alias.test.ts index 0a8f7340bca..0d2832ec2ef 100644 --- a/packages/eslint-plugin/tests/rules/no-this-alias.test.ts +++ b/packages/eslint-plugin/tests/rules/no-this-alias.test.ts @@ -66,6 +66,13 @@ declare module 'foo' { code: 'const self = this;', errors: [idError], }, + { + code: ` +let that; +that = this; + `, + errors: [idError], + }, { code: 'const { props, state } = this;', options: [ From 84cef319b6faf784143d0f5eda247e3004b76238 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Garc=C3=ADa?= <82288753+juank1809@users.noreply.github.com> Date: Sat, 26 Mar 2022 18:55:11 -0500 Subject: [PATCH 2/2] Update packages/eslint-plugin/src/rules/no-this-alias.ts Co-authored-by: Josh Goldberg --- packages/eslint-plugin/src/rules/no-this-alias.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-this-alias.ts b/packages/eslint-plugin/src/rules/no-this-alias.ts index 0d79b3eca6e..5750b8209b1 100644 --- a/packages/eslint-plugin/src/rules/no-this-alias.ts +++ b/packages/eslint-plugin/src/rules/no-this-alias.ts @@ -51,13 +51,8 @@ export default util.createRule({ "VariableDeclarator[init.type='ThisExpression'], AssignmentExpression[right.type='ThisExpression']"( node: TSESTree.VariableDeclarator | TSESTree.AssignmentExpression, ): void { - let id; - - if (node.type === AST_NODE_TYPES.VariableDeclarator) { - id = node.id; - } else { - id = node.left; - } + const id = + node.type === AST_NODE_TYPES.VariableDeclarator ? node.id : node.left; if (allowDestructuring && id.type !== AST_NODE_TYPES.Identifier) { return; }