-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathindex.ts
338 lines (319 loc) · 9.9 KB
/
index.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
import type { RuleContext, ASTNode } from "../../types"
import type * as TS from "typescript"
import { loadModule } from "../load-module"
export type TypeScript = typeof TS
export type { TS }
export type TSTools = {
service: {
esTreeNodeToTSNodeMap: ReadonlyMap<unknown, TS.Node>
tsNodeToESTreeNodeMap: ReadonlyMap<TS.Node, ASTNode>
program: TS.Program
hasFullTypeInformation: boolean
}
ts: TypeScript
}
/**
* Get TypeScript tools
*/
export function getTypeScriptTools(context: RuleContext): TSTools | null {
const ts = getTypeScript(context)
if (!ts) {
return null
}
const { program, esTreeNodeToTSNodeMap, tsNodeToESTreeNodeMap } =
context.parserServices
if (!program || !esTreeNodeToTSNodeMap || !tsNodeToESTreeNodeMap) {
return null
}
const hasFullTypeInformation =
context.parserServices.hasFullTypeInformation ?? true
if (!hasFullTypeInformation) {
// Full type information is required. User must specify parserOptions.project.
return null
}
return {
service: {
esTreeNodeToTSNodeMap,
tsNodeToESTreeNodeMap,
hasFullTypeInformation,
program,
},
ts,
}
}
let cacheTypeScript: TypeScript | null = null
/**
* Get TypeScript tools
*/
export function getTypeScript(context: RuleContext): TypeScript | null {
if (cacheTypeScript) {
return cacheTypeScript
}
cacheTypeScript = loadModule(context, "typescript")
if (cacheTypeScript) {
return cacheTypeScript
}
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports -- ignore
cacheTypeScript ??= require("typescript")
} catch {
// ignore
}
return cacheTypeScript
}
/**
* Check whether the given type is a truthy literal type or not.
*/
export function isTruthyLiteral(type: TS.Type, tsTools: TSTools): boolean {
if (type.isUnion()) {
return type.types.every((t) => isTruthyLiteral(t, tsTools))
}
return (
(isBooleanLiteralType(type, tsTools.ts) &&
tsTools.service.program.getTypeChecker().typeToString(type) === "true") ||
(type.isLiteral() && Boolean(type.value))
)
}
/**
* Check whether the given type is a falsy type or not.
*/
export function isFalsyType(type: TS.Type, tsTools: TSTools): boolean {
if (type.isUnion()) {
return type.types.every((t) => isFalsyType(t, tsTools))
}
if (
isUndefinedType(type, tsTools.ts) ||
isNullType(type, tsTools.ts) ||
isVoidType(type, tsTools.ts)
)
return true
if (type.isLiteral()) return !type.value
return (
isBooleanLiteralType(type, tsTools.ts) &&
tsTools.service.program.getTypeChecker().typeToString(type) === "false"
)
}
/**
* Check whether the given type is a nullish type or not.
*/
export function isNullishType(type: TS.Type, ts: TypeScript): boolean {
if (type.isUnion()) {
return type.types.every((t) => isNullishType(t, ts))
}
return isNullType(type, ts) || isUndefinedType(type, ts)
}
/**
* Checks whether the given type is nullable or not.
*/
export function isNullableType(type: TS.Type, ts: TypeScript): boolean {
if (type.isUnion()) {
return type.types.some((t) => isNullableType(t, ts))
}
return isNullType(type, ts) || isUndefinedType(type, ts)
}
/**
* Check whether the given type is a boolean literal type or not.
*/
export function isBooleanLiteralType(type: TS.Type, ts: TypeScript): boolean {
return (type.flags & ts.TypeFlags.BooleanLiteral) !== 0
}
/**
* Check whether the given type is an object type or not.
*/
export function isObjectType(
type: TS.Type,
ts: TypeScript,
): type is TS.ObjectType {
return (type.flags & ts.TypeFlags.Object) !== 0
}
/**
* Check whether the given type is a reference type or not.
*/
export function isReferenceObjectType(
type: TS.Type,
ts: TypeScript,
): type is TS.TypeReference {
return (
isObjectType(type, ts) &&
(type.objectFlags & ts.ObjectFlags.Reference) !== 0
)
}
/**
* Check whether the given type is a tuple type or not.
*/
export function isTupleObjectType(
type: TS.Type,
ts: TypeScript,
): type is TS.TupleType {
return (
isObjectType(type, ts) && (type.objectFlags & ts.ObjectFlags.Tuple) !== 0
)
}
/**
* Check whether the given type is a tuple type or not.
* Unlike isTupleObjectType, it also refers to reference types.
*/
export function isTupleType(type: TS.Type, ts: TypeScript): boolean {
return (
isTupleObjectType(type, ts) ||
(isReferenceObjectType(type, ts) && isTupleObjectType(type.target, ts))
)
}
/**
* Check whether the given type is an any type or not.
*/
export function isAnyType(type: TS.Type, ts: TypeScript): boolean {
return (type.flags & ts.TypeFlags.Any) !== 0
}
/**
* Check whether the given type is an unknown type or not.
*/
export function isUnknownType(type: TS.Type, ts: TypeScript): boolean {
return (type.flags & ts.TypeFlags.Unknown) !== 0
}
/**
* Check whether the given type is a never type or not.
*/
export function isNeverType(type: TS.Type, ts: TypeScript): boolean {
return (type.flags & ts.TypeFlags.Never) !== 0
}
/**
* Check whether the given type is an undefined type or not.
*/
export function isUndefinedType(type: TS.Type, ts: TypeScript): boolean {
return (type.flags & ts.TypeFlags.Undefined) !== 0
}
/**
* Check whether the given type is a void type or not.
*/
export function isVoidType(type: TS.Type, ts: TypeScript): boolean {
return (type.flags & ts.TypeFlags.Void) !== 0
}
/**
* Check whether the given type is a null type or not.
*/
export function isNullType(type: TS.Type, ts: TypeScript): boolean {
return (type.flags & ts.TypeFlags.Null) !== 0
}
/**
* Check whether the given type is a possibly falsy type or not.
*/
export function isPossiblyFalsyType(type: TS.Type, ts: TypeScript): boolean {
if (type.isUnion()) {
return type.types.some((t) => isPossiblyFalsyType(t, ts))
}
return (type.flags & ts.TypeFlags.PossiblyFalsy) !== 0
}
/**
* Get the call signatures from the given type.
* This method is heavily inspired by tsutils. https://github.com/ajafff/tsutils
* The MIT License (MIT) Copyright (c) 2017 Klaus Meinhardt
* https://github.com/ajafff/tsutils/blob/master/LICENSE
*/
export function getCallSignaturesOfType(
type: TS.Type,
): readonly TS.Signature[] {
if (type.isUnion()) {
return type.types.flatMap((t) => getCallSignaturesOfType(t))
}
if (type.isIntersection()) {
let signatures: readonly TS.Signature[] = []
for (const t of type.types) {
const sig = getCallSignaturesOfType(t)
if (sig.length !== 0) {
if (signatures.length) {
// if more than one type of the intersection has call signatures, none of them is useful for inference
return []
}
signatures = sig
}
}
return signatures
}
return type.getCallSignatures()
}
/**
* Resolves the given node's type. Will resolve to the type's generic constraint, if it has one.
* Copied this method from @typescript-eslint/type-utils. https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/type-utils
* The MIT License (MIT) Copyright (c) 2021 TypeScript ESLint and other contributors
* https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/type-utils/LICENSE
*/
export function getConstrainedTypeAtLocation(
checker: TS.TypeChecker,
node: TS.Node,
): TS.Type {
const nodeType = checker.getTypeAtLocation(node)
const constrained = checker.getBaseConstraintOfType(nodeType)
return constrained ?? nodeType
}
/**
* Get the type name of a given type.
*
* Copied this method from @typescript-eslint/type-utils. https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/type-utils
* The MIT License (MIT) Copyright (c) 2021 TypeScript ESLint and other contributors
* https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/type-utils/LICENSE
*
* @param type The type to get the name of.
*/
export function getTypeName(type: TS.Type, tsTools: TSTools): string {
const { ts } = tsTools
// It handles `string` and string literal types as string.
if ((type.flags & ts.TypeFlags.StringLike) !== 0) {
return "string"
}
const typeChecker = tsTools.service.program.getTypeChecker()
// 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?.getDeclarations()
const typeParamDecl = decls?.[0] as TS.TypeParameterDeclaration
if (
ts.isTypeParameterDeclaration(typeParamDecl) &&
typeParamDecl.constraint != null
) {
return getTypeName(
typeChecker.getTypeFromTypeNode(typeParamDecl.constraint),
tsTools,
)
}
}
// 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(value, tsTools))
.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(value, tsTools))
.some((t) => t === "string")
) {
return "string"
}
return typeChecker.typeToString(type)
}
/**
* Return the type of the given property in the given type, or undefined if no such property exists
*/
export function getTypeOfPropertyOfType(
type: TS.Type,
name: string,
checker: TS.TypeChecker,
): TS.Type | undefined {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- getTypeOfPropertyOfType is an internal API of TS.
return (checker as any).getTypeOfPropertyOfType(type, name)
}