/
prefer-return-this-type.ts
178 lines (151 loc) · 4.42 KB
/
prefer-return-this-type.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import {
TSESTree,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
import { createRule, forEachReturnStatement, getParserServices } from '../util';
import * as ts from 'typescript';
type ClassLikeDeclaration =
| TSESTree.ClassDeclaration
| TSESTree.ClassExpression;
type FunctionLike =
| TSESTree.MethodDefinition['value']
| TSESTree.ArrowFunctionExpression;
export default createRule({
name: 'prefer-return-this-type',
defaultOptions: [],
meta: {
type: 'suggestion',
docs: {
description:
'Enforce that `this` is used when only `this` type is returned',
category: 'Best Practices',
recommended: false,
requiresTypeChecking: true,
},
messages: {
useThisType: 'use `this` type instead.',
},
schema: [],
fixable: 'code',
},
create(context) {
const parserServices = getParserServices(context);
const checker = parserServices.program.getTypeChecker();
function tryGetNameInType(
name: string,
typeNode: TSESTree.TypeNode,
): TSESTree.Identifier | undefined {
if (
typeNode.type === AST_NODE_TYPES.TSTypeReference &&
typeNode.typeName.type === AST_NODE_TYPES.Identifier &&
typeNode.typeName.name === name
) {
return typeNode.typeName;
}
if (typeNode.type === AST_NODE_TYPES.TSUnionType) {
for (const type of typeNode.types) {
const found = tryGetNameInType(name, type);
if (found) {
return found;
}
}
}
return undefined;
}
function isThisSpecifiedInParameters(originalFunc: FunctionLike): boolean {
const firstArg = originalFunc.params[0];
return (
firstArg &&
firstArg.type === AST_NODE_TYPES.Identifier &&
firstArg.name === 'this'
);
}
function isFunctionReturningThis(
originalFunc: FunctionLike,
originalClass: ClassLikeDeclaration,
): boolean {
if (isThisSpecifiedInParameters(originalFunc)) {
return false;
}
const func = parserServices.esTreeNodeToTSNodeMap.get(originalFunc);
if (!func.body) {
return false;
}
const classType = checker.getTypeAtLocation(
parserServices.esTreeNodeToTSNodeMap.get(originalClass),
) as ts.InterfaceType;
if (func.body.kind !== ts.SyntaxKind.Block) {
const type = checker.getTypeAtLocation(func.body);
return classType.thisType === type;
}
let hasReturnThis = false;
let hasReturnClassType = false;
forEachReturnStatement(func.body as ts.Block, stmt => {
const expr = stmt.expression;
if (!expr) {
return;
}
// fast check
if (expr.kind === ts.SyntaxKind.ThisKeyword) {
hasReturnThis = true;
return;
}
const type = checker.getTypeAtLocation(expr);
if (classType === type) {
hasReturnClassType = true;
return true;
}
if (classType.thisType === type) {
hasReturnThis = true;
return;
}
return;
});
return !hasReturnClassType && hasReturnThis;
}
function checkFunction(
originalFunc: FunctionLike,
originalClass: ClassLikeDeclaration,
): void {
const className = originalClass.id?.name;
if (!className) {
return;
}
if (!originalFunc.returnType) {
return;
}
const classNameRef = tryGetNameInType(
className,
originalFunc.returnType.typeAnnotation,
);
if (!classNameRef) {
return;
}
if (isFunctionReturningThis(originalFunc, originalClass)) {
context.report({
node: classNameRef,
messageId: 'useThisType',
fix(fixer) {
return fixer.replaceText(classNameRef, 'this');
},
});
}
}
return {
'ClassBody > MethodDefinition'(node: TSESTree.MethodDefinition): void {
checkFunction(node.value, node.parent!.parent as ClassLikeDeclaration);
},
'ClassBody > ClassProperty'(node: TSESTree.ClassProperty): 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);
},
};
},
});