types.js
15 KB
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getContextualType = exports.isUnsafeAssignment = exports.isAnyOrAnyArrayTypeDiscriminated = exports.isTypeUnknownArrayType = exports.isTypeAnyArrayType = exports.isTypeAnyType = exports.isTypeUnknownType = exports.getTypeArguments = exports.getEqualsKind = exports.getTokenAtPosition = exports.getSourceFileOfNode = exports.typeIsOrHasBaseType = exports.isTypeFlagSet = exports.getTypeFlags = exports.getDeclaration = exports.isNullableType = exports.getConstrainedTypeAtLocation = exports.getTypeName = exports.containsAllTypesByName = exports.isTypeArrayTypeOrUnionOfArrayTypes = void 0;
const debug_1 = __importDefault(require("debug"));
const tsutils_1 = require("tsutils");
const ts = __importStar(require("typescript"));
const log = debug_1.default('typescript-eslint:eslint-plugin:utils:types');
/**
* Checks if the given type is either an array type,
* or a union made up solely of array types.
*/
function isTypeArrayTypeOrUnionOfArrayTypes(type, checker) {
for (const t of tsutils_1.unionTypeParts(type)) {
if (!checker.isArrayType(t)) {
return false;
}
}
return true;
}
exports.isTypeArrayTypeOrUnionOfArrayTypes = isTypeArrayTypeOrUnionOfArrayTypes;
/**
* @param type Type being checked by name.
* @param allowedNames Symbol names checking on the type.
* @returns Whether the type is, extends, or contains all of the allowed names.
*/
function containsAllTypesByName(type, allowAny, allowedNames) {
if (isTypeFlagSet(type, ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
return !allowAny;
}
if (tsutils_1.isTypeReference(type)) {
type = type.target;
}
const symbol = type.getSymbol();
if (symbol && allowedNames.has(symbol.name)) {
return true;
}
if (tsutils_1.isUnionOrIntersectionType(type)) {
return type.types.every(t => containsAllTypesByName(t, allowAny, allowedNames));
}
const bases = type.getBaseTypes();
return (typeof bases !== 'undefined' &&
bases.length > 0 &&
bases.every(t => containsAllTypesByName(t, allowAny, allowedNames)));
}
exports.containsAllTypesByName = containsAllTypesByName;
/**
* Get the type name of a given type.
* @param typeChecker The context sensitive TypeScript TypeChecker.
* @param type The type to get the name of.
*/
function getTypeName(typeChecker, type) {
// It handles `string` and string literal types as string.
if ((type.flags & ts.TypeFlags.StringLike) !== 0) {
return 'string';
}
// If the type is a type parameter which extends primitive string types,
// but it was not recognized as a string like. So check the constraint
// type of the type parameter.
if ((type.flags & ts.TypeFlags.TypeParameter) !== 0) {
// `type.getConstraint()` method doesn't return the constraint type of
// the type parameter for some reason. So this gets the constraint type
// via AST.
const symbol = type.getSymbol();
const decls = symbol === null || symbol === void 0 ? void 0 : symbol.getDeclarations();
const typeParamDecl = decls === null || decls === void 0 ? void 0 : decls[0];
if (ts.isTypeParameterDeclaration(typeParamDecl) &&
typeParamDecl.constraint != null) {
return getTypeName(typeChecker, typeChecker.getTypeFromTypeNode(typeParamDecl.constraint));
}
}
// If the type is a union and all types in the union are string like,
// return `string`. For example:
// - `"a" | "b"` is string.
// - `string | string[]` is not string.
if (type.isUnion() &&
type.types
.map(value => getTypeName(typeChecker, value))
.every(t => t === 'string')) {
return 'string';
}
// If the type is an intersection and a type in the intersection is string
// like, return `string`. For example: `string & {__htmlEscaped: void}`
if (type.isIntersection() &&
type.types
.map(value => getTypeName(typeChecker, value))
.some(t => t === 'string')) {
return 'string';
}
return typeChecker.typeToString(type);
}
exports.getTypeName = getTypeName;
/**
* Resolves the given node's type. Will resolve to the type's generic constraint, if it has one.
*/
function getConstrainedTypeAtLocation(checker, node) {
const nodeType = checker.getTypeAtLocation(node);
const constrained = checker.getBaseConstraintOfType(nodeType);
return constrained !== null && constrained !== void 0 ? constrained : nodeType;
}
exports.getConstrainedTypeAtLocation = getConstrainedTypeAtLocation;
/**
* Checks if the given type is (or accepts) nullable
* @param isReceiver true if the type is a receiving type (i.e. the type of a called function's parameter)
*/
function isNullableType(type, { isReceiver = false, allowUndefined = true, } = {}) {
const flags = getTypeFlags(type);
if (isReceiver && flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
return true;
}
if (allowUndefined) {
return (flags & (ts.TypeFlags.Null | ts.TypeFlags.Undefined)) !== 0;
}
else {
return (flags & ts.TypeFlags.Null) !== 0;
}
}
exports.isNullableType = isNullableType;
/**
* Gets the declaration for the given variable
*/
function getDeclaration(checker, node) {
var _a;
const symbol = checker.getSymbolAtLocation(node);
if (!symbol) {
return null;
}
const declarations = symbol.getDeclarations();
return (_a = declarations === null || declarations === void 0 ? void 0 : declarations[0]) !== null && _a !== void 0 ? _a : null;
}
exports.getDeclaration = getDeclaration;
/**
* Gets all of the type flags in a type, iterating through unions automatically
*/
function getTypeFlags(type) {
let flags = 0;
for (const t of tsutils_1.unionTypeParts(type)) {
flags |= t.flags;
}
return flags;
}
exports.getTypeFlags = getTypeFlags;
/**
* Checks if the given type is (or accepts) the given flags
* @param isReceiver true if the type is a receiving type (i.e. the type of a called function's parameter)
*/
function isTypeFlagSet(type, flagsToCheck, isReceiver) {
const flags = getTypeFlags(type);
if (isReceiver && flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
return true;
}
return (flags & flagsToCheck) !== 0;
}
exports.isTypeFlagSet = isTypeFlagSet;
/**
* @returns Whether a type is an instance of the parent type, including for the parent's base types.
*/
function typeIsOrHasBaseType(type, parentType) {
const parentSymbol = parentType.getSymbol();
if (!type.getSymbol() || !parentSymbol) {
return false;
}
const typeAndBaseTypes = [type];
const ancestorTypes = type.getBaseTypes();
if (ancestorTypes) {
typeAndBaseTypes.push(...ancestorTypes);
}
for (const baseType of typeAndBaseTypes) {
const baseSymbol = baseType.getSymbol();
if (baseSymbol && baseSymbol.name === parentSymbol.name) {
return true;
}
}
return false;
}
exports.typeIsOrHasBaseType = typeIsOrHasBaseType;
/**
* Gets the source file for a given node
*/
function getSourceFileOfNode(node) {
while (node && node.kind !== ts.SyntaxKind.SourceFile) {
node = node.parent;
}
return node;
}
exports.getSourceFileOfNode = getSourceFileOfNode;
function getTokenAtPosition(sourceFile, position) {
const queue = [sourceFile];
let current;
while (queue.length > 0) {
current = queue.shift();
// find the child that contains 'position'
for (const child of current.getChildren(sourceFile)) {
const start = child.getFullStart();
if (start > position) {
// If this child begins after position, then all subsequent children will as well.
return current;
}
const end = child.getEnd();
if (position < end ||
(position === end && child.kind === ts.SyntaxKind.EndOfFileToken)) {
queue.push(child);
break;
}
}
}
return current;
}
exports.getTokenAtPosition = getTokenAtPosition;
function getEqualsKind(operator) {
switch (operator) {
case '==':
return {
isPositive: true,
isStrict: false,
};
case '===':
return {
isPositive: true,
isStrict: true,
};
case '!=':
return {
isPositive: false,
isStrict: false,
};
case '!==':
return {
isPositive: false,
isStrict: true,
};
default:
return undefined;
}
}
exports.getEqualsKind = getEqualsKind;
function getTypeArguments(type, checker) {
var _a;
// getTypeArguments was only added in TS3.7
if (checker.getTypeArguments) {
return checker.getTypeArguments(type);
}
return (_a = type.typeArguments) !== null && _a !== void 0 ? _a : [];
}
exports.getTypeArguments = getTypeArguments;
/**
* @returns true if the type is `unknown`
*/
function isTypeUnknownType(type) {
return isTypeFlagSet(type, ts.TypeFlags.Unknown);
}
exports.isTypeUnknownType = isTypeUnknownType;
/**
* @returns true if the type is `any`
*/
function isTypeAnyType(type) {
if (isTypeFlagSet(type, ts.TypeFlags.Any)) {
if (type.intrinsicName === 'error') {
log('Found an "error" any type');
}
return true;
}
return false;
}
exports.isTypeAnyType = isTypeAnyType;
/**
* @returns true if the type is `any[]`
*/
function isTypeAnyArrayType(type, checker) {
return (checker.isArrayType(type) &&
isTypeAnyType(
// getTypeArguments was only added in TS3.7
getTypeArguments(type, checker)[0]));
}
exports.isTypeAnyArrayType = isTypeAnyArrayType;
/**
* @returns true if the type is `unknown[]`
*/
function isTypeUnknownArrayType(type, checker) {
return (checker.isArrayType(type) &&
isTypeUnknownType(
// getTypeArguments was only added in TS3.7
getTypeArguments(type, checker)[0]));
}
exports.isTypeUnknownArrayType = isTypeUnknownArrayType;
/**
* @returns `AnyType.Any` if the type is `any`, `AnyType.AnyArray` if the type is `any[]` or `readonly any[]`,
* otherwise it returns `AnyType.Safe`.
*/
function isAnyOrAnyArrayTypeDiscriminated(node, checker) {
const type = checker.getTypeAtLocation(node);
if (isTypeAnyType(type)) {
return 0 /* Any */;
}
if (isTypeAnyArrayType(type, checker)) {
return 1 /* AnyArray */;
}
return 2 /* Safe */;
}
exports.isAnyOrAnyArrayTypeDiscriminated = isAnyOrAnyArrayTypeDiscriminated;
/**
* Does a simple check to see if there is an any being assigned to a non-any type.
*
* This also checks generic positions to ensure there's no unsafe sub-assignments.
* Note: in the case of generic positions, it makes the assumption that the two types are the same.
*
* @example See tests for examples
*
* @returns false if it's safe, or an object with the two types if it's unsafe
*/
function isUnsafeAssignment(type, receiver, checker) {
var _a, _b;
if (isTypeAnyType(type)) {
// Allow assignment of any ==> unknown.
if (isTypeUnknownType(receiver)) {
return false;
}
if (!isTypeAnyType(receiver)) {
return { sender: type, receiver };
}
}
if (tsutils_1.isTypeReference(type) && tsutils_1.isTypeReference(receiver)) {
// TODO - figure out how to handle cases like this,
// where the types are assignable, but not the same type
/*
function foo(): ReadonlySet<number> { return new Set<any>(); }
// and
type Test<T> = { prop: T }
type Test2 = { prop: string }
declare const a: Test<any>;
const b: Test2 = a;
*/
if (type.target !== receiver.target) {
// if the type references are different, assume safe, as we won't know how to compare the two types
// the generic positions might not be equivalent for both types
return false;
}
const typeArguments = (_a = type.typeArguments) !== null && _a !== void 0 ? _a : [];
const receiverTypeArguments = (_b = receiver.typeArguments) !== null && _b !== void 0 ? _b : [];
for (let i = 0; i < typeArguments.length; i += 1) {
const arg = typeArguments[i];
const receiverArg = receiverTypeArguments[i];
const unsafe = isUnsafeAssignment(arg, receiverArg, checker);
if (unsafe) {
return { sender: type, receiver };
}
}
return false;
}
return false;
}
exports.isUnsafeAssignment = isUnsafeAssignment;
/**
* Returns the contextual type of a given node.
* Contextual type is the type of the target the node is going into.
* i.e. the type of a called function's parameter, or the defined type of a variable declaration
*/
function getContextualType(checker, node) {
const parent = node.parent;
if (!parent) {
return;
}
if (tsutils_1.isCallExpression(parent) || tsutils_1.isNewExpression(parent)) {
if (node === parent.expression) {
// is the callee, so has no contextual type
return;
}
}
else if (tsutils_1.isVariableDeclaration(parent) ||
tsutils_1.isPropertyDeclaration(parent) ||
tsutils_1.isParameterDeclaration(parent)) {
return parent.type ? checker.getTypeFromTypeNode(parent.type) : undefined;
}
else if (tsutils_1.isJsxExpression(parent)) {
return checker.getContextualType(parent);
}
else if (tsutils_1.isPropertyAssignment(parent) && tsutils_1.isIdentifier(node)) {
return checker.getContextualType(node);
}
else if (![ts.SyntaxKind.TemplateSpan, ts.SyntaxKind.JsxExpression].includes(parent.kind)) {
// parent is not something we know we can get the contextual type of
return;
}
// TODO - support return statement checking
return checker.getContextualType(node);
}
exports.getContextualType = getContextualType;
//# sourceMappingURL=types.js.map