-
Notifications
You must be signed in to change notification settings - Fork 189
/
TypeChecker.ts
279 lines (245 loc) · 10.1 KB
/
TypeChecker.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
import { errors, SymbolFlags, SyntaxKind, ts, TypeFormatFlags } from "@ts-morph/common";
import { ProjectContext } from "../../ProjectContext";
import { CallLikeExpression } from "../ast/aliases";
import { Node } from "../ast/common";
import { EnumMember } from "../ast/enum";
import { Expression } from "../ast/expression";
import { ExportSpecifier } from "../ast/module";
import { Signature, Symbol } from "../symbols";
import { Type } from "../types";
/**
* Wrapper around the TypeChecker.
*/
export class TypeChecker {
/** @internal */
readonly #context: ProjectContext;
/** @internal */
#getCompilerObject!: () => ts.TypeChecker;
/** @private */
constructor(context: ProjectContext) {
this.#context = context;
}
/**
* Gets the compiler's TypeChecker.
*/
get compilerObject() {
return this.#getCompilerObject();
}
/**
* Resets the type checker.
* @internal
*/
_reset(getTypeChecker: () => ts.TypeChecker) {
this.#getCompilerObject = getTypeChecker;
}
/**
* Gets the ambient module symbols (ex. modules in the @types folder or node_modules).
*/
getAmbientModules() {
return this.compilerObject.getAmbientModules().map(s => this.#context.compilerFactory.getSymbol(s));
}
/**
* Gets the apparent type of a type.
* @param type - Type to get the apparent type of.
*/
getApparentType(type: Type) {
return this.#context.compilerFactory.getType(this.compilerObject.getApparentType(type.compilerType));
}
/**
* Gets the constant value of a declaration.
* @param node - Node to get the constant value from.
*/
getConstantValue(node: EnumMember) {
return this.compilerObject.getConstantValue(node.compilerNode);
}
/**
* Gets the fully qualified name of a symbol.
* @param symbol - Symbol to get the fully qualified name of.
*/
getFullyQualifiedName(symbol: Symbol) {
return this.compilerObject.getFullyQualifiedName(symbol.compilerSymbol);
}
/**
* Gets the type at the specified location.
* @param node - Node to get the type for.
*/
getTypeAtLocation(node: Node): Type {
return this.#context.compilerFactory.getType(this.compilerObject.getTypeAtLocation(node.compilerNode));
}
/**
* Gets the contextual type of an expression.
* @param expression - Expression.
*/
getContextualType(expression: Expression): Type | undefined {
const contextualType = this.compilerObject.getContextualType(expression.compilerNode);
return contextualType == null ? undefined : this.#context.compilerFactory.getType(contextualType);
}
/**
* Gets the type of a symbol at the specified location.
* @param symbol - Symbol to get the type for.
* @param node - Location to get the type for.
*/
getTypeOfSymbolAtLocation(symbol: Symbol, node: Node): Type {
return this.#context.compilerFactory.getType(this.compilerObject.getTypeOfSymbolAtLocation(symbol.compilerSymbol, node.compilerNode));
}
/**
* Gets the declared type of a symbol.
* @param symbol - Symbol to get the type for.
*/
getDeclaredTypeOfSymbol(symbol: Symbol): Type {
return this.#context.compilerFactory.getType(this.compilerObject.getDeclaredTypeOfSymbol(symbol.compilerSymbol));
}
/**
* Gets the symbol at the specified location or undefined if none exists.
* @param node - Node to get the symbol for.
*/
getSymbolAtLocation(node: Node): Symbol | undefined {
const compilerSymbol = this.compilerObject.getSymbolAtLocation(node.compilerNode);
return compilerSymbol == null ? undefined : this.#context.compilerFactory.getSymbol(compilerSymbol);
}
/**
* Gets the aliased symbol of a symbol.
* @param symbol - Symbol to get the alias symbol of.
*/
getAliasedSymbol(symbol: Symbol): Symbol | undefined {
if (!symbol.hasFlags(SymbolFlags.Alias))
return undefined;
const tsAliasSymbol = this.compilerObject.getAliasedSymbol(symbol.compilerSymbol);
return tsAliasSymbol == null ? undefined : this.#context.compilerFactory.getSymbol(tsAliasSymbol);
}
/**
* Follow a single alias to get the immediately aliased symbol.
* @param symbol - Symbol to get the immediate alias symbol of.
*/
getImmediatelyAliasedSymbol(symbol: Symbol): Symbol | undefined {
const tsAliasSymbol = this.compilerObject.getImmediateAliasedSymbol(symbol.compilerSymbol);
return tsAliasSymbol == null ? undefined : this.#context.compilerFactory.getSymbol(tsAliasSymbol);
}
/**
* Gets the export symbol of a local symbol with a corresponding export symbol. Otherwise returns the input symbol.
*
* The following is from the compiler API documentation:
*
* For example, at `export type T = number;`:
* - `getSymbolAtLocation` at the location `T` will return the exported symbol for `T`.
* - But the result of `getSymbolsInScope` will contain the *local* symbol for `T`, not the exported symbol.
* - Calling `getExportSymbolOfSymbol` on that local symbol will return the exported symbol.
*/
getExportSymbolOfSymbol(symbol: Symbol) {
return this.#context.compilerFactory.getSymbol(this.compilerObject.getExportSymbolOfSymbol(symbol.compilerSymbol));
}
/**
* Gets the properties of a type.
* @param type - Type.
*/
getPropertiesOfType(type: Type) {
return this.compilerObject.getPropertiesOfType(type.compilerType).map(p => this.#context.compilerFactory.getSymbol(p));
}
/**
* Gets the type text
* @param type - Type to get the text of.
* @param enclosingNode - Enclosing node.
* @param typeFormatFlags - Type format flags.
*/
getTypeText(type: Type, enclosingNode?: Node, typeFormatFlags?: TypeFormatFlags) {
if (typeFormatFlags == null)
typeFormatFlags = this.#getDefaultTypeFormatFlags(enclosingNode);
return this.compilerObject.typeToString(type.compilerType, enclosingNode?.compilerNode, typeFormatFlags);
}
/**
* Gets the return type of a signature.
* @param signature - Signature to get the return type of.
*/
getReturnTypeOfSignature(signature: Signature): Type {
return this.#context.compilerFactory.getType(this.compilerObject.getReturnTypeOfSignature(signature.compilerSignature));
}
/**
* Gets a signature from a node.
* @param node - Node to get the signature from.
*/
getSignatureFromNode(node: Node<ts.SignatureDeclaration>): Signature | undefined {
const signature = this.compilerObject.getSignatureFromDeclaration(node.compilerNode);
return signature == null ? undefined : this.#context.compilerFactory.getSignature(signature);
}
/**
* Gets the exports of a module.
* @param moduleSymbol - Module symbol.
*/
getExportsOfModule(moduleSymbol: Symbol) {
const symbols = this.compilerObject.getExportsOfModule(moduleSymbol.compilerSymbol);
return (symbols || []).map(s => this.#context.compilerFactory.getSymbol(s));
}
/**
* Gets the local target symbol of the provided export specifier.
* @param exportSpecifier - Export specifier.
*/
getExportSpecifierLocalTargetSymbol(exportSpecifier: ExportSpecifier) {
const symbol = this.compilerObject.getExportSpecifierLocalTargetSymbol(exportSpecifier.compilerNode);
return symbol == null ? undefined : this.#context.compilerFactory.getSymbol(symbol);
}
/**
* Gets the resolved signature from a node or returns undefined if the signature can't be resolved.
* @param node - Node to get the signature from.
*/
getResolvedSignature(node: CallLikeExpression) {
const resolvedSignature = this.compilerObject.getResolvedSignature(node.compilerNode);
if (!resolvedSignature || !resolvedSignature.declaration)
return undefined;
return this.#context.compilerFactory.getSignature(resolvedSignature);
}
/**
* Gets the resolved signature from a node or throws if the signature cannot be resolved.
* @param node - Node to get the signature from.
*/
getResolvedSignatureOrThrow(node: CallLikeExpression, message?: string | (() => string)) {
return errors.throwIfNullOrUndefined(this.getResolvedSignature(node), message ?? "Signature could not be resolved.", node);
}
/**
* Gets the base type of a literal type.
*
* For example, for a number literal type it will return the number type.
* @param type - Literal type to get the base type of.
*/
getBaseTypeOfLiteralType(type: Type) {
return this.#context.compilerFactory.getType(this.compilerObject.getBaseTypeOfLiteralType(type.compilerType));
}
/**
* Gets the symbols in the scope of the provided node.
*
* Note: This will always return the local symbols. If you want the export symbol from a local symbol, then
* use the `#getExportSymbolOfSymbol(symbol)` method.
* @param node - Node to check the scope for.
* @param meaning - Meaning of symbol to filter by.
*/
getSymbolsInScope(node: Node, meaning: SymbolFlags) {
return this.compilerObject.getSymbolsInScope(node.compilerNode, meaning)
.map(s => this.#context.compilerFactory.getSymbol(s));
}
/**
* Gets the type arguments from a type reference.
* @param typeReference - Type reference.
*/
getTypeArguments(typeReference: Type) {
return this.compilerObject.getTypeArguments(typeReference.compilerType as ts.TypeReference)
.map(arg => this.#context.compilerFactory.getType(arg));
}
/** Checks if a type is assignable to another type. */
isTypeAssignableTo(sourceType: Type, targetType: Type) {
return this.compilerObject.isTypeAssignableTo(sourceType.compilerType, targetType.compilerType);
}
/** @internal */
#getDefaultTypeFormatFlags(enclosingNode?: Node) {
let formatFlags = (TypeFormatFlags.UseTypeOfFunction | TypeFormatFlags.NoTruncation | TypeFormatFlags.UseFullyQualifiedType
| TypeFormatFlags.WriteTypeArgumentsOfSignature) as TypeFormatFlags;
if (enclosingNode != null && enclosingNode.getKind() === SyntaxKind.TypeAliasDeclaration)
formatFlags |= TypeFormatFlags.InTypeAlias;
return formatFlags;
}
/**
* Gets the shorthand assignment value symbol of the provided node.
*/
getShorthandAssignmentValueSymbol(node: Node) {
const symbol = this.compilerObject.getShorthandAssignmentValueSymbol(node.compilerNode);
return symbol ? this.#context.compilerFactory.getSymbol(symbol) : undefined;
}
}