/
typescript.ts
564 lines (495 loc) Β· 19.6 KB
/
typescript.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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as ts from 'typescript';
import {ClassDeclaration, ClassMember, ClassMemberKind, CtorParameter, Declaration, Decorator, FunctionDefinition, Import, ReflectionHost, isDecoratorIdentifier} from './host';
import {typeToValue} from './type_to_value';
/**
* reflector.ts implements static reflection of declarations using the TypeScript `ts.TypeChecker`.
*/
export class TypeScriptReflectionHost implements ReflectionHost {
constructor(protected checker: ts.TypeChecker) {}
getDecoratorsOfDeclaration(declaration: ts.Declaration): Decorator[]|null {
if (declaration.decorators === undefined || declaration.decorators.length === 0) {
return null;
}
return declaration.decorators.map(decorator => this._reflectDecorator(decorator))
.filter((dec): dec is Decorator => dec !== null);
}
getMembersOfClass(clazz: ClassDeclaration): ClassMember[] {
const tsClazz = castDeclarationToClassOrDie(clazz);
return tsClazz.members.map(member => this._reflectMember(member))
.filter((member): member is ClassMember => member !== null);
}
getConstructorParameters(clazz: ClassDeclaration): CtorParameter[]|null {
const tsClazz = castDeclarationToClassOrDie(clazz);
// First, find the constructor.
const ctor = tsClazz.members.find(ts.isConstructorDeclaration);
if (ctor === undefined) {
return null;
}
return ctor.parameters.map(node => {
// The name of the parameter is easy.
const name = parameterName(node.name);
const decorators = this.getDecoratorsOfDeclaration(node);
// It may or may not be possible to write an expression that refers to the value side of the
// type named for the parameter.
let originalTypeNode = node.type || null;
let typeNode = originalTypeNode;
// Check if we are dealing with a simple nullable union type e.g. `foo: Foo|null`
// and extract the type. More complex union types e.g. `foo: Foo|Bar` are not supported.
// We also don't need to support `foo: Foo|undefined` because Angular's DI injects `null` for
// optional tokes that don't have providers.
if (typeNode && ts.isUnionTypeNode(typeNode)) {
let childTypeNodes = typeNode.types.filter(
childTypeNode => childTypeNode.kind !== ts.SyntaxKind.NullKeyword);
if (childTypeNodes.length === 1) {
typeNode = childTypeNodes[0];
} else {
typeNode = null;
}
}
const typeValueReference = typeToValue(typeNode, this.checker);
return {
name,
nameNode: node.name, typeValueReference,
typeNode: originalTypeNode, decorators,
};
});
}
getImportOfIdentifier(id: ts.Identifier): Import|null {
const directImport = this.getDirectImportOfIdentifier(id);
if (directImport !== null) {
return directImport;
} else if (ts.isQualifiedName(id.parent) && id.parent.right === id) {
return this.getImportOfNamespacedIdentifier(id, getQualifiedNameRoot(id.parent));
} else if (ts.isPropertyAccessExpression(id.parent) && id.parent.name === id) {
return this.getImportOfNamespacedIdentifier(id, getFarLeftIdentifier(id.parent));
} else {
return null;
}
}
getExportsOfModule(node: ts.Node): Map<string, Declaration>|null {
// In TypeScript code, modules are only ts.SourceFiles. Throw if the node isn't a module.
if (!ts.isSourceFile(node)) {
throw new Error(`getDeclarationsOfModule() called on non-SourceFile in TS code`);
}
const map = new Map<string, Declaration>();
// Reflect the module to a Symbol, and use getExportsOfModule() to get a list of exported
// Symbols.
const symbol = this.checker.getSymbolAtLocation(node);
if (symbol === undefined) {
return null;
}
this.checker.getExportsOfModule(symbol).forEach(exportSymbol => {
// Map each exported Symbol to a Declaration and add it to the map.
const decl = this.getDeclarationOfSymbol(exportSymbol, null);
if (decl !== null) {
map.set(exportSymbol.name, decl);
}
});
return map;
}
isClass(node: ts.Node): node is ClassDeclaration {
// In TypeScript code, classes are ts.ClassDeclarations.
// (`name` can be undefined in unnamed default exports: `default export class { ... }`)
return ts.isClassDeclaration(node) && (node.name !== undefined) && ts.isIdentifier(node.name);
}
hasBaseClass(clazz: ClassDeclaration): boolean {
return (ts.isClassDeclaration(clazz) || ts.isClassExpression(clazz)) &&
clazz.heritageClauses !== undefined &&
clazz.heritageClauses.some(clause => clause.token === ts.SyntaxKind.ExtendsKeyword);
}
getBaseClassExpression(clazz: ClassDeclaration): ts.Expression|null {
if (!(ts.isClassDeclaration(clazz) || ts.isClassExpression(clazz)) ||
clazz.heritageClauses === undefined) {
return null;
}
const extendsClause =
clazz.heritageClauses.find(clause => clause.token === ts.SyntaxKind.ExtendsKeyword);
if (extendsClause === undefined) {
return null;
}
const extendsType = extendsClause.types[0];
if (extendsType === undefined) {
return null;
}
return extendsType.expression;
}
getDeclarationOfIdentifier(id: ts.Identifier): Declaration|null {
// Resolve the identifier to a Symbol, and return the declaration of that.
let symbol: ts.Symbol|undefined = this.checker.getSymbolAtLocation(id);
if (symbol === undefined) {
return null;
}
return this.getDeclarationOfSymbol(symbol, id);
}
getDefinitionOfFunction(node: ts.Node): FunctionDefinition|null {
if (!ts.isFunctionDeclaration(node) && !ts.isMethodDeclaration(node) &&
!ts.isFunctionExpression(node)) {
return null;
}
return {
node,
body: node.body !== undefined ? Array.from(node.body.statements) : null,
helper: null,
parameters: node.parameters.map(param => {
const name = parameterName(param.name);
const initializer = param.initializer || null;
return {name, node: param, initializer};
}),
};
}
getGenericArityOfClass(clazz: ClassDeclaration): number|null {
if (!ts.isClassDeclaration(clazz)) {
return null;
}
return clazz.typeParameters !== undefined ? clazz.typeParameters.length : 0;
}
getVariableValue(declaration: ts.VariableDeclaration): ts.Expression|null {
return declaration.initializer || null;
}
getDtsDeclaration(_: ts.Declaration): ts.Declaration|null { return null; }
protected getDirectImportOfIdentifier(id: ts.Identifier): Import|null {
const symbol = this.checker.getSymbolAtLocation(id);
if (symbol === undefined || symbol.declarations === undefined ||
symbol.declarations.length !== 1) {
return null;
}
const decl: ts.Declaration = symbol.declarations[0];
const importDecl = getContainingImportDeclaration(decl);
// Ignore declarations that are defined locally (not imported).
if (importDecl === null) {
return null;
}
// The module specifier is guaranteed to be a string literal, so this should always pass.
if (!ts.isStringLiteral(importDecl.moduleSpecifier)) {
// Not allowed to happen in TypeScript ASTs.
return null;
}
return {from: importDecl.moduleSpecifier.text, name: getExportedName(decl, id)};
}
/**
* Try to get the import info for this identifier as though it is a namespaced import.
*
* For example, if the identifier is the `Directive` part of a qualified type chain like:
*
* ```
* core.Directive
* ```
*
* then it might be that `core` is a namespace import such as:
*
* ```
* import * as core from 'tslib';
* ```
*
* @param id the TypeScript identifier to find the import info for.
* @returns The import info if this is a namespaced import or `null`.
*/
protected getImportOfNamespacedIdentifier(
id: ts.Identifier, namespaceIdentifier: ts.Identifier|null): Import|null {
if (namespaceIdentifier === null) {
return null;
}
const namespaceSymbol = this.checker.getSymbolAtLocation(namespaceIdentifier);
if (!namespaceSymbol) {
return null;
}
const declaration =
namespaceSymbol.declarations.length === 1 ? namespaceSymbol.declarations[0] : null;
if (!declaration) {
return null;
}
const namespaceDeclaration = ts.isNamespaceImport(declaration) ? declaration : null;
if (!namespaceDeclaration) {
return null;
}
const importDeclaration = namespaceDeclaration.parent.parent;
if (!ts.isStringLiteral(importDeclaration.moduleSpecifier)) {
// Should not happen as this would be invalid TypesScript
return null;
}
return {
from: importDeclaration.moduleSpecifier.text,
name: id.text,
};
}
/**
* Resolve a `ts.Symbol` to its declaration, keeping track of the `viaModule` along the way.
*
* @internal
*/
private getDeclarationOfSymbol(symbol: ts.Symbol, originalId: ts.Identifier|null): Declaration
|null {
// If the symbol points to a ShorthandPropertyAssignment, resolve it.
let valueDeclaration: ts.Declaration|undefined = undefined;
if (symbol.valueDeclaration !== undefined) {
valueDeclaration = symbol.valueDeclaration;
} else if (symbol.declarations.length > 0) {
valueDeclaration = symbol.declarations[0];
}
if (valueDeclaration !== undefined && ts.isShorthandPropertyAssignment(valueDeclaration)) {
const shorthandSymbol = this.checker.getShorthandAssignmentValueSymbol(valueDeclaration);
if (shorthandSymbol === undefined) {
return null;
}
return this.getDeclarationOfSymbol(shorthandSymbol, originalId);
} else if (valueDeclaration !== undefined && ts.isExportSpecifier(valueDeclaration)) {
const targetSymbol = this.checker.getExportSpecifierLocalTargetSymbol(valueDeclaration);
if (targetSymbol === undefined) {
return null;
}
return this.getDeclarationOfSymbol(targetSymbol, originalId);
}
const importInfo = originalId && this.getImportOfIdentifier(originalId);
const viaModule =
importInfo !== null && importInfo.from !== null && !importInfo.from.startsWith('.') ?
importInfo.from :
null;
// Now, resolve the Symbol to its declaration by following any and all aliases.
while (symbol.flags & ts.SymbolFlags.Alias) {
symbol = this.checker.getAliasedSymbol(symbol);
}
// Look at the resolved Symbol's declarations and pick one of them to return. Value declarations
// are given precedence over type declarations.
if (symbol.valueDeclaration !== undefined) {
return {
node: symbol.valueDeclaration,
viaModule,
};
} else if (symbol.declarations !== undefined && symbol.declarations.length > 0) {
return {
node: symbol.declarations[0],
viaModule,
};
} else {
return null;
}
}
private _reflectDecorator(node: ts.Decorator): Decorator|null {
// Attempt to resolve the decorator expression into a reference to a concrete Identifier. The
// expression may contain a call to a function which returns the decorator function, in which
// case we want to return the arguments.
let decoratorExpr: ts.Expression = node.expression;
let args: ts.Expression[]|null = null;
// Check for call expressions.
if (ts.isCallExpression(decoratorExpr)) {
args = Array.from(decoratorExpr.arguments);
decoratorExpr = decoratorExpr.expression;
}
// The final resolved decorator should be a `ts.Identifier` - if it's not, then something is
// wrong and the decorator can't be resolved statically.
if (!isDecoratorIdentifier(decoratorExpr)) {
return null;
}
const decoratorIdentifier = ts.isIdentifier(decoratorExpr) ? decoratorExpr : decoratorExpr.name;
const importDecl = this.getImportOfIdentifier(decoratorIdentifier);
return {
name: decoratorIdentifier.text,
identifier: decoratorExpr,
import: importDecl, node, args,
};
}
private _reflectMember(node: ts.ClassElement): ClassMember|null {
let kind: ClassMemberKind|null = null;
let value: ts.Expression|null = null;
let name: string|null = null;
let nameNode: ts.Identifier|null = null;
if (ts.isPropertyDeclaration(node)) {
kind = ClassMemberKind.Property;
value = node.initializer || null;
} else if (ts.isGetAccessorDeclaration(node)) {
kind = ClassMemberKind.Getter;
} else if (ts.isSetAccessorDeclaration(node)) {
kind = ClassMemberKind.Setter;
} else if (ts.isMethodDeclaration(node)) {
kind = ClassMemberKind.Method;
} else if (ts.isConstructorDeclaration(node)) {
kind = ClassMemberKind.Constructor;
} else {
return null;
}
if (ts.isConstructorDeclaration(node)) {
name = 'constructor';
} else if (ts.isIdentifier(node.name)) {
name = node.name.text;
nameNode = node.name;
} else {
return null;
}
const decorators = this.getDecoratorsOfDeclaration(node);
const isStatic = node.modifiers !== undefined &&
node.modifiers.some(mod => mod.kind === ts.SyntaxKind.StaticKeyword);
return {
node,
implementation: node, kind,
type: node.type || null, name, nameNode, decorators, value, isStatic,
};
}
}
export function reflectNameOfDeclaration(decl: ts.Declaration): string|null {
const id = reflectIdentifierOfDeclaration(decl);
return id && id.text || null;
}
export function reflectIdentifierOfDeclaration(decl: ts.Declaration): ts.Identifier|null {
if (ts.isClassDeclaration(decl) || ts.isFunctionDeclaration(decl)) {
return decl.name || null;
} else if (ts.isVariableDeclaration(decl)) {
if (ts.isIdentifier(decl.name)) {
return decl.name;
}
}
return null;
}
export function reflectTypeEntityToDeclaration(
type: ts.EntityName, checker: ts.TypeChecker): {node: ts.Declaration, from: string | null} {
let realSymbol = checker.getSymbolAtLocation(type);
if (realSymbol === undefined) {
throw new Error(`Cannot resolve type entity ${type.getText()} to symbol`);
}
while (realSymbol.flags & ts.SymbolFlags.Alias) {
realSymbol = checker.getAliasedSymbol(realSymbol);
}
let node: ts.Declaration|null = null;
if (realSymbol.valueDeclaration !== undefined) {
node = realSymbol.valueDeclaration;
} else if (realSymbol.declarations !== undefined && realSymbol.declarations.length === 1) {
node = realSymbol.declarations[0];
} else {
throw new Error(`Cannot resolve type entity symbol to declaration`);
}
if (ts.isQualifiedName(type)) {
if (!ts.isIdentifier(type.left)) {
throw new Error(`Cannot handle qualified name with non-identifier lhs`);
}
const symbol = checker.getSymbolAtLocation(type.left);
if (symbol === undefined || symbol.declarations === undefined ||
symbol.declarations.length !== 1) {
throw new Error(`Cannot resolve qualified type entity lhs to symbol`);
}
const decl = symbol.declarations[0];
if (ts.isNamespaceImport(decl)) {
const clause = decl.parent !;
const importDecl = clause.parent !;
if (!ts.isStringLiteral(importDecl.moduleSpecifier)) {
throw new Error(`Module specifier is not a string`);
}
return {node, from: importDecl.moduleSpecifier.text};
} else {
throw new Error(`Unknown import type?`);
}
} else {
return {node, from: null};
}
}
export function filterToMembersWithDecorator(members: ClassMember[], name: string, module?: string):
{member: ClassMember, decorators: Decorator[]}[] {
return members.filter(member => !member.isStatic)
.map(member => {
if (member.decorators === null) {
return null;
}
const decorators = member.decorators.filter(dec => {
if (dec.import !== null) {
return dec.import.name === name && (module === undefined || dec.import.from === module);
} else {
return dec.name === name && module === undefined;
}
});
if (decorators.length === 0) {
return null;
}
return {member, decorators};
})
.filter((value): value is {member: ClassMember, decorators: Decorator[]} => value !== null);
}
export function findMember(
members: ClassMember[], name: string, isStatic: boolean = false): ClassMember|null {
return members.find(member => member.isStatic === isStatic && member.name === name) || null;
}
export function reflectObjectLiteral(node: ts.ObjectLiteralExpression): Map<string, ts.Expression> {
const map = new Map<string, ts.Expression>();
node.properties.forEach(prop => {
if (ts.isPropertyAssignment(prop)) {
const name = propertyNameToString(prop.name);
if (name === null) {
return;
}
map.set(name, prop.initializer);
} else if (ts.isShorthandPropertyAssignment(prop)) {
map.set(prop.name.text, prop.name);
} else {
return;
}
});
return map;
}
function castDeclarationToClassOrDie(declaration: ClassDeclaration):
ClassDeclaration<ts.ClassDeclaration> {
if (!ts.isClassDeclaration(declaration)) {
throw new Error(
`Reflecting on a ${ts.SyntaxKind[declaration.kind]} instead of a ClassDeclaration.`);
}
return declaration;
}
function parameterName(name: ts.BindingName): string|null {
if (ts.isIdentifier(name)) {
return name.text;
} else {
return null;
}
}
function propertyNameToString(node: ts.PropertyName): string|null {
if (ts.isIdentifier(node) || ts.isStringLiteral(node) || ts.isNumericLiteral(node)) {
return node.text;
} else {
return null;
}
}
/**
* Compute the left most identifier in a qualified type chain. E.g. the `a` of `a.b.c.SomeType`.
* @param qualifiedName The starting property access expression from which we want to compute
* the left most identifier.
* @returns the left most identifier in the chain or `null` if it is not an identifier.
*/
function getQualifiedNameRoot(qualifiedName: ts.QualifiedName): ts.Identifier|null {
while (ts.isQualifiedName(qualifiedName.left)) {
qualifiedName = qualifiedName.left;
}
return ts.isIdentifier(qualifiedName.left) ? qualifiedName.left : null;
}
/**
* Compute the left most identifier in a property access chain. E.g. the `a` of `a.b.c.d`.
* @param propertyAccess The starting property access expression from which we want to compute
* the left most identifier.
* @returns the left most identifier in the chain or `null` if it is not an identifier.
*/
function getFarLeftIdentifier(propertyAccess: ts.PropertyAccessExpression): ts.Identifier|null {
while (ts.isPropertyAccessExpression(propertyAccess.expression)) {
propertyAccess = propertyAccess.expression;
}
return ts.isIdentifier(propertyAccess.expression) ? propertyAccess.expression : null;
}
/**
* Return the ImportDeclaration for the given `node` if it is either an `ImportSpecifier` or a
* `NamespaceImport`. If not return `null`.
*/
function getContainingImportDeclaration(node: ts.Node): ts.ImportDeclaration|null {
return ts.isImportSpecifier(node) ? node.parent !.parent !.parent ! :
ts.isNamespaceImport(node) ? node.parent.parent : null;
}
/**
* Compute the name by which the `decl` was exported, not imported.
* If no such declaration can be found (e.g. it is a namespace import)
* then fallback to the `originalId`.
*/
function getExportedName(decl: ts.Declaration, originalId: ts.Identifier): string {
return ts.isImportSpecifier(decl) ?
(decl.propertyName !== undefined ? decl.propertyName : decl.name).text :
originalId.text;
}