/
no-unsafe-argument.ts
278 lines (250 loc) · 8.35 KB
/
no-unsafe-argument.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
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import * as ts from 'typescript';
import * as util from '../util';
type MessageIds =
| 'unsafeArgument'
| 'unsafeTupleSpread'
| 'unsafeArraySpread'
| 'unsafeSpread';
const enum RestTypeKind {
Array,
Tuple,
Other,
}
type RestType =
| {
type: ts.Type;
kind: RestTypeKind.Array;
index: number;
}
| {
typeArguments: readonly ts.Type[];
kind: RestTypeKind.Tuple;
index: number;
}
| {
type: ts.Type;
kind: RestTypeKind.Other;
index: number;
};
class FunctionSignature {
private parameterTypeIndex = 0;
public static create(
checker: ts.TypeChecker,
tsNode: ts.CallLikeExpression,
): FunctionSignature | null {
const signature = checker.getResolvedSignature(tsNode);
if (!signature) {
return null;
}
const paramTypes: ts.Type[] = [];
let restType: RestType | null = null;
const parameters = signature.getParameters();
for (let i = 0; i < parameters.length; i += 1) {
const param = parameters[i];
const type = checker.getTypeOfSymbolAtLocation(param, tsNode);
const decl = param.getDeclarations()?.[0];
if (decl && ts.isParameter(decl) && decl.dotDotDotToken) {
// is a rest param
if (checker.isArrayType(type)) {
restType = {
type: checker.getTypeArguments(type)[0],
kind: RestTypeKind.Array,
index: i,
};
} else if (checker.isTupleType(type)) {
restType = {
typeArguments: checker.getTypeArguments(type),
kind: RestTypeKind.Tuple,
index: i,
};
} else {
restType = {
type,
kind: RestTypeKind.Other,
index: i,
};
}
break;
}
paramTypes.push(type);
}
return new this(paramTypes, restType);
}
private hasConsumedArguments = false;
private constructor(
private paramTypes: ts.Type[],
private restType: RestType | null,
) {}
public getNextParameterType(): ts.Type | null {
const index = this.parameterTypeIndex;
this.parameterTypeIndex += 1;
if (index >= this.paramTypes.length || this.hasConsumedArguments) {
if (this.restType == null) {
return null;
}
switch (this.restType.kind) {
case RestTypeKind.Tuple: {
const typeArguments = this.restType.typeArguments;
if (this.hasConsumedArguments) {
// all types consumed by a rest - just assume it's the last type
// there is one edge case where this is wrong, but we ignore it because
// it's rare and really complicated to handle
// eg: function foo(...a: [number, ...string[], number])
return typeArguments[typeArguments.length - 1];
}
const typeIndex = index - this.restType.index;
if (typeIndex >= typeArguments.length) {
return typeArguments[typeArguments.length - 1];
}
return typeArguments[typeIndex];
}
case RestTypeKind.Array:
case RestTypeKind.Other:
return this.restType.type;
}
}
return this.paramTypes[index];
}
public consumeRemainingArguments(): void {
this.hasConsumedArguments = true;
}
}
export default util.createRule<[], MessageIds>({
name: 'no-unsafe-argument',
meta: {
type: 'problem',
docs: {
description: 'Disallows calling an function with an any type value',
category: 'Possible Errors',
// TODO - enable this with next breaking
recommended: false,
requiresTypeChecking: true,
},
messages: {
unsafeArgument:
'Unsafe argument of type `{{sender}}` assigned to a parameter of type `{{receiver}}`.',
unsafeTupleSpread:
'Unsafe spread of a tuple type. The {{index}} element is of type `{{sender}}` and is assigned to a parameter of type `{{reciever}}`.',
unsafeArraySpread: 'Unsafe spread of an `any` array type.',
unsafeSpread: 'Unsafe spread of an `any` type.',
},
schema: [],
},
defaultOptions: [],
create(context) {
const { program, esTreeNodeToTSNodeMap } = util.getParserServices(context);
const checker = program.getTypeChecker();
return {
'CallExpression, NewExpression'(
node: TSESTree.CallExpression | TSESTree.NewExpression,
): void {
if (node.arguments.length === 0) {
return;
}
// ignore any-typed calls as these are caught by no-unsafe-call
if (
util.isTypeAnyType(
checker.getTypeAtLocation(esTreeNodeToTSNodeMap.get(node.callee)),
)
) {
return;
}
const tsNode = esTreeNodeToTSNodeMap.get(node);
const signature = FunctionSignature.create(checker, tsNode);
if (!signature) {
return;
}
for (let i = 0; i < node.arguments.length; i += 1) {
const argument = node.arguments[i];
switch (argument.type) {
// spreads consume
case AST_NODE_TYPES.SpreadElement: {
const spreadArgType = checker.getTypeAtLocation(
esTreeNodeToTSNodeMap.get(argument.argument),
);
if (util.isTypeAnyType(spreadArgType)) {
// foo(...any)
context.report({
node: argument,
messageId: 'unsafeSpread',
});
} else if (util.isTypeAnyArrayType(spreadArgType, checker)) {
// foo(...any[])
// TODO - we could break down the spread and compare the array type against each argument
context.report({
node: argument,
messageId: 'unsafeArraySpread',
});
} else if (checker.isTupleType(spreadArgType)) {
// foo(...[tuple1, tuple2])
const spreadTypeArguments = checker.getTypeArguments(
spreadArgType,
);
for (let j = 0; j < spreadTypeArguments.length; j += 1) {
const tupleType = spreadTypeArguments[j];
const parameterType = signature.getNextParameterType();
if (parameterType == null) {
continue;
}
const result = util.isUnsafeAssignment(
tupleType,
parameterType,
checker,
);
if (result) {
context.report({
node: argument,
messageId: 'unsafeTupleSpread',
data: {
sender: checker.typeToString(tupleType),
receiver: checker.typeToString(parameterType),
},
});
}
}
if (spreadArgType.target.hasRestElement) {
// the last element was a rest - so all remaining defined arguments can be considered "consumed"
// all remaining arguments should be compared against the rest type (if one exists)
signature.consumeRemainingArguments();
}
} else {
// something that's iterable
// handling this will be pretty complex - so we ignore it for now
// TODO - handle generic iterable case
}
break;
}
default: {
const parameterType = signature.getNextParameterType();
if (parameterType == null) {
continue;
}
const argumentType = checker.getTypeAtLocation(
esTreeNodeToTSNodeMap.get(argument),
);
const result = util.isUnsafeAssignment(
argumentType,
parameterType,
checker,
);
if (result) {
context.report({
node: argument,
messageId: 'unsafeArgument',
data: {
sender: checker.typeToString(argumentType),
receiver: checker.typeToString(parameterType),
},
});
}
}
}
}
},
};
},
});