From b71a9fe3dee363200e10e4586a8156d5a7706424 Mon Sep 17 00:00:00 2001 From: Ronen Amiel Date: Tue, 4 Feb 2025 21:02:59 +0200 Subject: [PATCH 1/2] handle accessor property with prefer-return-this-type --- .../src/rules/prefer-return-this-type.ts | 31 +++++++------- .../rules/prefer-return-this-type.test.ts | 42 +++++++++++++++++++ 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/packages/eslint-plugin/src/rules/prefer-return-this-type.ts b/packages/eslint-plugin/src/rules/prefer-return-this-type.ts index 304f63dd1027..7834cebb6547 100644 --- a/packages/eslint-plugin/src/rules/prefer-return-this-type.ts +++ b/packages/eslint-plugin/src/rules/prefer-return-this-type.ts @@ -148,24 +148,27 @@ export default createRule({ } } + function checkProperty( + node: TSESTree.AccessorProperty | TSESTree.PropertyDefinition, + ): void { + if ( + !( + node.value?.type === AST_NODE_TYPES.FunctionExpression || + node.value?.type === AST_NODE_TYPES.ArrowFunctionExpression + ) + ) { + return; + } + + checkFunction(node.value, node.parent.parent as ClassLikeDeclaration); + } + return { + 'ClassBody > AccessorProperty': checkProperty, 'ClassBody > MethodDefinition'(node: TSESTree.MethodDefinition): void { checkFunction(node.value, node.parent.parent as ClassLikeDeclaration); }, - 'ClassBody > PropertyDefinition'( - node: TSESTree.PropertyDefinition, - ): void { - if ( - !( - node.value?.type === AST_NODE_TYPES.FunctionExpression || - node.value?.type === AST_NODE_TYPES.ArrowFunctionExpression - ) - ) { - return; - } - - checkFunction(node.value, node.parent.parent as ClassLikeDeclaration); - }, + 'ClassBody > PropertyDefinition': checkProperty, }; }, }); diff --git a/packages/eslint-plugin/tests/rules/prefer-return-this-type.test.ts b/packages/eslint-plugin/tests/rules/prefer-return-this-type.test.ts index 4a9262e92ce9..796909a7406b 100644 --- a/packages/eslint-plugin/tests/rules/prefer-return-this-type.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-return-this-type.test.ts @@ -200,6 +200,48 @@ class Foo { }, { code: ` +class Foo { + accessor f = (): Foo => { + return this; + }; +} + `, + errors: [ + { + column: 20, + line: 3, + messageId: 'useThisType', + }, + ], + output: ` +class Foo { + accessor f = (): this => { + return this; + }; +} + `, + }, + { + code: ` +class Foo { + accessor f = (): Foo => this; +} + `, + errors: [ + { + column: 20, + line: 3, + messageId: 'useThisType', + }, + ], + output: ` +class Foo { + accessor f = (): this => this; +} + `, + }, + { + code: ` class Foo { f1(): Foo | undefined { return this; From b463fe5e76ee371065905b71062671964e7dbb81 Mon Sep 17 00:00:00 2001 From: Ronen Amiel Date: Thu, 6 Feb 2025 15:04:18 +0200 Subject: [PATCH 2/2] refactor and add missing tests --- .../src/rules/prefer-return-this-type.ts | 4 +- .../rules/prefer-return-this-type.test.ts | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/prefer-return-this-type.ts b/packages/eslint-plugin/src/rules/prefer-return-this-type.ts index 7834cebb6547..fff92141fe12 100644 --- a/packages/eslint-plugin/src/rules/prefer-return-this-type.ts +++ b/packages/eslint-plugin/src/rules/prefer-return-this-type.ts @@ -160,13 +160,13 @@ export default createRule({ return; } - checkFunction(node.value, node.parent.parent as ClassLikeDeclaration); + checkFunction(node.value, node.parent.parent); } return { 'ClassBody > AccessorProperty': checkProperty, 'ClassBody > MethodDefinition'(node: TSESTree.MethodDefinition): void { - checkFunction(node.value, node.parent.parent as ClassLikeDeclaration); + checkFunction(node.value, node.parent.parent); }, 'ClassBody > PropertyDefinition': checkProperty, }; diff --git a/packages/eslint-plugin/tests/rules/prefer-return-this-type.test.ts b/packages/eslint-plugin/tests/rules/prefer-return-this-type.test.ts index 796909a7406b..5b372350059f 100644 --- a/packages/eslint-plugin/tests/rules/prefer-return-this-type.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-return-this-type.test.ts @@ -79,6 +79,25 @@ class Derived extends Base { f(): Base { return this; } +} + `, + ` +class Foo { + accessor f = () => { + return this; + }; +} + `, + ` +class Foo { + accessor f = (): this => { + return this; + }; +} + `, + ` +class Foo { + f?: string; } `, ], @@ -108,6 +127,29 @@ class Foo { }, { code: ` +class Foo { + f = function (): Foo { + return this; + }; +} + `, + errors: [ + { + column: 20, + line: 3, + messageId: 'useThisType', + }, + ], + output: ` +class Foo { + f = function (): this { + return this; + }; +} + `, + }, + { + code: ` class Foo { f(): Foo { const self = this;