Skip to content

Commit 9e98b8f

Browse files
authored
fix(eslint-plugin): [prefer-return-this-type] handle generics properly in fixer (typescript-eslint#3852)
1 parent ffdb5ff commit 9e98b8f

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

packages/eslint-plugin/src/rules/prefer-return-this-type.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {
2-
TSESTree,
32
AST_NODE_TYPES,
3+
TSESTree,
44
} from '@typescript-eslint/experimental-utils';
5-
import { createRule, forEachReturnStatement, getParserServices } from '../util';
65
import * as ts from 'typescript';
6+
import { createRule, forEachReturnStatement, getParserServices } from '../util';
77

88
type ClassLikeDeclaration =
99
| TSESTree.ClassDeclaration
@@ -40,13 +40,13 @@ export default createRule({
4040
function tryGetNameInType(
4141
name: string,
4242
typeNode: TSESTree.TypeNode,
43-
): TSESTree.Identifier | undefined {
43+
): TSESTree.TSTypeReference | undefined {
4444
if (
4545
typeNode.type === AST_NODE_TYPES.TSTypeReference &&
4646
typeNode.typeName.type === AST_NODE_TYPES.Identifier &&
4747
typeNode.typeName.name === name
4848
) {
49-
return typeNode.typeName;
49+
return typeNode;
5050
}
5151

5252
if (typeNode.type === AST_NODE_TYPES.TSUnionType) {
@@ -130,29 +130,23 @@ export default createRule({
130130
originalClass: ClassLikeDeclaration,
131131
): void {
132132
const className = originalClass.id?.name;
133-
if (!className) {
134-
return;
135-
}
136-
137-
if (!originalFunc.returnType) {
133+
if (!className || !originalFunc.returnType) {
138134
return;
139135
}
140136

141-
const classNameRef = tryGetNameInType(
137+
const node = tryGetNameInType(
142138
className,
143139
originalFunc.returnType.typeAnnotation,
144140
);
145-
if (!classNameRef) {
141+
if (!node) {
146142
return;
147143
}
148144

149145
if (isFunctionReturningThis(originalFunc, originalClass)) {
150146
context.report({
151-
node: classNameRef,
147+
node,
152148
messageId: 'useThisType',
153-
fix(fixer) {
154-
return fixer.replaceText(classNameRef, 'this');
155-
},
149+
fix: fixer => fixer.replaceText(node, 'this'),
156150
});
157151
}
158152
}

packages/eslint-plugin/tests/rules/prefer-return-this-type.test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import rule from '../../src/rules/prefer-return-this-type';
2-
import { RuleTester, getFixturesRootDir } from '../RuleTester';
2+
import { getFixturesRootDir, RuleTester } from '../RuleTester';
33

44
const rootPath = getFixturesRootDir();
55

@@ -281,6 +281,33 @@ class Foo {
281281
return this;
282282
}
283283
}
284+
}
285+
`,
286+
},
287+
{
288+
// https://github.com/typescript-eslint/typescript-eslint/issues/3842
289+
code: `
290+
class Animal<T> {
291+
eat(): Animal<T> {
292+
console.log("I'm moving!");
293+
return this;
294+
}
295+
}
296+
`,
297+
errors: [
298+
{
299+
messageId: 'useThisType',
300+
line: 3,
301+
column: 10,
302+
endColumn: 19,
303+
},
304+
],
305+
output: `
306+
class Animal<T> {
307+
eat(): this {
308+
console.log("I'm moving!");
309+
return this;
310+
}
284311
}
285312
`,
286313
},

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy