-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathnoUnnecessaryTypeAnnotationRule.ts
366 lines (327 loc) · 15.5 KB
/
noUnnecessaryTypeAnnotationRule.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import * as ts from 'typescript';
import * as Lint from 'tslint';
import {
isTypeParameter,
getVariableDeclarationKind,
VariableDeclarationKind,
getPropertyName,
isTypeFlagSet,
isExpressionValueUsed,
isUnionType,
isThisParameter,
isTypePredicateNode,
isValidNumericLiteral,
isIntersectionType,
getIIFE,
} from 'tsutils';
type FunctionExpressionLike = ts.ArrowFunction | ts.FunctionExpression;
const CHECK_RETURN_TYPE_OPTION = 'check-return-type';
const FAIL_MESSAGE = `type annotation is redundant`;
interface IOptions {
checkReturnType: boolean;
}
export class Rule extends Lint.Rules.TypedRule {
public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
return this.applyWithFunction(
sourceFile,
walk, {
checkReturnType: this.ruleArguments.indexOf(CHECK_RETURN_TYPE_OPTION) !== -1,
},
program.getTypeChecker(),
);
}
}
const formatFlags = ts.TypeFormatFlags.UseStructuralFallback
| ts.TypeFormatFlags.UseFullyQualifiedType
| ts.TypeFormatFlags.UseAliasDefinedOutsideCurrentScope
| ts.TypeFormatFlags.NoTruncation
| ts.TypeFormatFlags.WriteClassExpressionAsTypeLiteral
| ts.TypeFormatFlags.WriteArrowStyleSignature;
function walk(ctx: Lint.WalkContext<IOptions>, checker: ts.TypeChecker) {
return ts.forEachChild(ctx.sourceFile, function cb(node): void {
switch (node.kind) {
case ts.SyntaxKind.ArrowFunction:
case ts.SyntaxKind.FunctionExpression:
checkFunction(<FunctionExpressionLike>node);
break;
case ts.SyntaxKind.MethodDeclaration:
if (node.parent!.kind === ts.SyntaxKind.ObjectLiteralExpression)
checkObjectLiteralMethod(<ts.MethodDeclaration>node);
break;
case ts.SyntaxKind.VariableDeclarationList:
// TODO add an option where to put the type annotation for functions (variable or function)
checkVariables(<ts.VariableDeclarationList>node);
}
return ts.forEachChild(node, cb);
});
function checkFunction(node: FunctionExpressionLike) {
// TODO use getContextuallyTypedParameterType
if (!functionHasTypeDeclarations(node))
return;
const iife = getIIFE(node);
if (iife !== undefined)
return checkIife(node, iife);
const type = getContextualTypeOfFunction(node);
if (type === undefined)
return;
checkContextSensitiveFunctionOrMethod(node, type);
}
function checkObjectLiteralMethod(node: ts.MethodDeclaration) {
if (!functionHasTypeDeclarations(node))
return;
const type = getContextualTypeOfObjectLiteralMethod(node);
if (type === undefined)
return;
checkContextSensitiveFunctionOrMethod(node, type);
}
function checkContextSensitiveFunctionOrMethod(node: ts.FunctionLikeDeclaration, contextualType: ts.Type) {
const parameters = parametersExceptThis(node.parameters);
const sig = getMatchingSignature(contextualType, parameters);
if (sig === undefined)
return;
const [signature, checkReturn] = sig;
if (ctx.options.checkReturnType && checkReturn && node.type !== undefined && !signatureHasGenericOrTypePredicateReturn(signature) &&
typesAreEqual(checker.getTypeFromTypeNode(node.type), signature.getReturnType()))
fail(node.type);
let restParameterContext = false;
let contextualParameterType: ts.Type;
for (let i = 0; i < parameters.length; ++i) {
if (!restParameterContext) {
const context = signature.parameters[i];
// wotan-disable-next-line no-useless-predicate
if (context === undefined || context.valueDeclaration === undefined)
break;
if (isTypeParameter(checker.getTypeAtLocation(context.valueDeclaration)))
continue;
contextualParameterType = checker.getTypeOfSymbolAtLocation(context, node);
if ((<ts.ParameterDeclaration>context.valueDeclaration).dotDotDotToken !== undefined) {
const indexType = contextualParameterType.getNumberIndexType();
if (indexType === undefined)
break;
contextualParameterType = indexType;
restParameterContext = true;
}
}
const parameter = parameters[i];
if (parameter.type === undefined)
continue;
let declaredType: ts.Type;
if (parameter.dotDotDotToken !== undefined) {
if (!restParameterContext)
break;
declaredType = checker.getTypeFromTypeNode(parameter.type);
const indexType = declaredType.getNumberIndexType();
if (indexType === undefined)
break;
declaredType = indexType;
} else {
declaredType = checker.getTypeFromTypeNode(parameter.type);
}
if (compareParameterTypes(
contextualParameterType!,
declaredType,
parameter.questionToken !== undefined || parameter.initializer !== undefined,
))
fail(parameter.type);
}
}
function checkIife(func: FunctionExpressionLike, iife: ts.CallExpression) {
if (ctx.options.checkReturnType && func.type !== undefined && func.name === undefined &&
(
!isExpressionValueUsed(iife) ||
!containsTypeWithFlag(checker.getTypeFromTypeNode(func.type), ts.TypeFlags.Literal) &&
checker.getContextualType(iife) !== undefined
))
fail(func.type);
const parameters = parametersExceptThis(func.parameters);
const args = iife.arguments;
const len = Math.min(parameters.length, args.length);
outer: for (let i = 0; i < len; ++i) {
const parameter = parameters[i];
if (parameter.type === undefined)
continue;
const declaredType = checker.getTypeFromTypeNode(parameter.type);
const contextualType = checker.getBaseTypeOfLiteralType(checker.getTypeAtLocation(args[i]));
if (parameter.dotDotDotToken !== undefined) {
const indexType = declaredType.getNumberIndexType();
if (indexType === undefined || !typesAreEqual(indexType, contextualType))
break;
for (let j = i + 1; j < args.length; ++j)
if (!typesAreEqual(contextualType, checker.getBaseTypeOfLiteralType(checker.getTypeAtLocation(args[j]))))
break outer; // TODO build UnionType
fail(parameter.type);
} else if (compareParameterTypes(
contextualType,
declaredType,
parameter.questionToken !== undefined || parameter.initializer !== undefined,
)) {
fail(parameter.type);
}
}
}
function checkVariables(list: ts.VariableDeclarationList) {
const isConst = getVariableDeclarationKind(list) === VariableDeclarationKind.Const;
for (const variable of list.declarations) {
if (variable.type === undefined || variable.initializer === undefined)
continue;
let inferred = checker.getTypeAtLocation(variable.initializer);
if (!isConst)
inferred = checker.getBaseTypeOfLiteralType(inferred);
const declared = checker.getTypeFromTypeNode(variable.type);
if (typesAreEqual(declared, inferred) || isConst && typesAreEqual(declared, checker.getBaseTypeOfLiteralType(inferred)))
fail(variable.type);
}
}
function fail(type: ts.TypeNode) {
ctx.addFailure(type.pos - 1, type.end, FAIL_MESSAGE, Lint.Replacement.deleteFromTo(type.pos - 1, type.end));
}
// TODO this could use a little more effort
function typesAreEqual(a: ts.Type, b: ts.Type): boolean {
return a === b || checker.typeToString(a, undefined, formatFlags) === checker.typeToString(b, undefined, formatFlags);
}
function getContextualTypeOfFunction(func: FunctionExpressionLike): ts.Type | undefined {
const type = checker.getContextualType(func);
return type && checker.getApparentType(type);
}
function getContextualTypeOfObjectLiteralMethod(method: ts.MethodDeclaration): ts.Type | undefined {
let type = checker.getContextualType(<ts.ObjectLiteralExpression>method.parent);
if (type === undefined)
return;
type = checker.getApparentType(type);
if (!isTypeFlagSet(type, ts.TypeFlags.StructuredType))
return;
const t = checker.getTypeAtLocation(method);
const symbol = t.symbol && type.getProperties().find((s) => s.escapedName === t.symbol!.escapedName);
return symbol !== undefined
? checker.getTypeOfSymbolAtLocation(symbol, method.name)
: isNumericPropertyName(method.name) && type.getNumberIndexType() || type.getStringIndexType();
}
function signatureHasGenericOrTypePredicateReturn(signature: ts.Signature): boolean {
if (signature.declaration === undefined)
return false;
if (signature.declaration.type !== undefined && isTypePredicateNode(signature.declaration.type))
return true;
const original = checker.getSignatureFromDeclaration(<ts.SignatureDeclaration>signature.declaration);
return original !== undefined && isTypeParameter(original.getReturnType());
}
function removeOptionalityFromType(type: ts.Type): ts.Type {
if (!containsTypeWithFlag(type, ts.TypeFlags.Undefined))
return type;
const allowsNull = containsTypeWithFlag(type, ts.TypeFlags.Null);
type = checker.getNonNullableType(type);
return allowsNull ? checker.getNullableType(type, ts.TypeFlags.Null) : type;
}
function compareParameterTypes(context: ts.Type, declared: ts.Type, optional: boolean): boolean {
if (optional)
declared = removeOptionalityFromType(declared);
return typesAreEqual(declared, context) ||
optional && typesAreEqual(checker.getNullableType(declared, ts.TypeFlags.Undefined), context);
}
function isNumericPropertyName(name: ts.PropertyName) {
const str = getPropertyName(name);
if (str !== undefined)
return isValidNumericLiteral(str) && String(+str) === str;
return isAssignableToNumber(checker.getTypeAtLocation((<ts.ComputedPropertyName>name).expression)); // TODO use isTypeAssignableTo
}
function isAssignableToNumber(type: ts.Type) {
let typeParametersSeen: Set<ts.Type> | undefined;
return (function check(t): boolean {
if (isTypeParameter(t) && t.symbol !== undefined && t.symbol.declarations !== undefined) {
if (typeParametersSeen === undefined) {
typeParametersSeen = new Set([t]);
} else if (!typeParametersSeen.has(t)) {
typeParametersSeen.add(t);
} else {
return false;
}
const declaration = <ts.TypeParameterDeclaration>t.symbol.declarations[0];
if (declaration.constraint === undefined)
return true;
return check(checker.getTypeFromTypeNode(declaration.constraint));
}
if (isUnionType(t))
return t.types.every(check);
if (isIntersectionType(t))
return t.types.some(check);
return isTypeFlagSet(t, ts.TypeFlags.NumberLike | ts.TypeFlags.Any);
})(type);
}
function getMatchingSignature(type: ts.Type, parameters: ReadonlyArray<ts.ParameterDeclaration>): [ts.Signature, boolean] | undefined {
const minArguments = getMinArguments(parameters);
const signatures = getSignaturesOfType(type).filter(
(s) => s.declaration !== undefined &&
getNumParameters(<ReadonlyArray<ts.ParameterDeclaration>>s.declaration.parameters) >= minArguments,
);
switch (signatures.length) {
case 0:
return;
case 1:
return [signatures[0], true];
default: {
const str = checker.signatureToString(signatures[0], undefined, formatFlags);
const withoutReturn = removeSignatureReturn(str);
let returnUsable = true;
for (let i = 1; i < signatures.length; ++i) { // check if all signatures are the same
const sig = checker.signatureToString(signatures[i], undefined, formatFlags);
if (str !== sig) {
if (withoutReturn !== removeSignatureReturn(sig))
return;
returnUsable = false;
}
}
return [signatures[0], returnUsable];
}
}
}
}
function removeSignatureReturn(str: string): string {
const sourceFile = ts.createSourceFile('tmp.ts', `type T=${str}`, ts.ScriptTarget.ESNext);
const signature = <ts.FunctionOrConstructorTypeNode>(<ts.TypeAliasDeclaration>sourceFile.statements[0]).type;
return sourceFile.text.substring(7, signature.parameters.end + 1);
}
function getSignaturesOfType(type: ts.Type): ts.Signature[] {
if (isUnionType(type)) {
const signatures = [];
for (const t of type.types)
signatures.push(...getSignaturesOfType(t));
return signatures;
}
if (isIntersectionType(type)) {
let signatures: ts.Signature[] | undefined;
for (const t of type.types) {
const sig = getSignaturesOfType(t);
if (sig.length !== 0) {
if (signatures !== undefined)
return []; // if more than one type of the intersection has call signatures, none of them is useful for inference
signatures = sig;
}
}
return signatures === undefined ? [] : signatures;
}
return type.getCallSignatures();
}
function getNumParameters(parameters: ReadonlyArray<ts.ParameterDeclaration>): number {
if (parameters.length === 0)
return 0;
if (parameters[parameters.length - 1].dotDotDotToken !== undefined)
return Infinity;
return parametersExceptThis(parameters).length;
}
function getMinArguments(parameters: ReadonlyArray<ts.ParameterDeclaration>): number {
let minArguments = parameters.length;
for (; minArguments > 0; --minArguments) {
const parameter = parameters[minArguments - 1];
if (parameter.questionToken === undefined && parameter.initializer === undefined && parameter.dotDotDotToken === undefined)
break;
}
return minArguments;
}
function containsTypeWithFlag(type: ts.Type, flag: ts.TypeFlags): boolean {
return isUnionType(type) ? type.types.some((t) => isTypeFlagSet(t, flag)) : isTypeFlagSet(type, flag);
}
function parametersExceptThis(parameters: ReadonlyArray<ts.ParameterDeclaration>) {
return parameters.length !== 0 && isThisParameter(parameters[0]) ? parameters.slice(1) : parameters;
}
function functionHasTypeDeclarations(func: ts.FunctionLikeDeclaration): boolean {
return func.type !== undefined || parametersExceptThis(func.parameters).some((p) => p.type !== undefined);
}