/
util.ts
407 lines (361 loc) Β· 15.1 KB
/
util.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
/**
* @license
* Copyright Google LLC 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 {Expression, ExternalExpr, FactoryTarget, ParseLocation, ParseSourceFile, ParseSourceSpan, R3CompiledExpression, R3FactoryMetadata, R3Reference, ReadPropExpr, Statement, WrappedNodeExpr} from '@angular/compiler';
import ts from 'typescript';
import {assertSuccessfulReferenceEmit, ImportedFile, ImportFlags, ModuleResolver, Reference, ReferenceEmitter} from '../../../imports';
import {attachDefaultImportDeclaration} from '../../../imports/src/default';
import {DynamicValue, ForeignFunctionResolver, PartialEvaluator} from '../../../partial_evaluator';
import {ClassDeclaration, Decorator, Import, ImportedTypeValueReference, isNamedClassDeclaration, LocalTypeValueReference, ReflectionHost, TypeValueReference, TypeValueReferenceKind} from '../../../reflection';
import {CompileResult} from '../../../transform';
/**
* Convert a `TypeValueReference` to an `Expression` which refers to the type as a value.
*
* Local references are converted to a `WrappedNodeExpr` of the TypeScript expression, and non-local
* references are converted to an `ExternalExpr`. Note that this is only valid in the context of the
* file in which the `TypeValueReference` originated.
*/
export function valueReferenceToExpression(valueRef: LocalTypeValueReference|
ImportedTypeValueReference): Expression;
export function valueReferenceToExpression(valueRef: TypeValueReference): Expression|null;
export function valueReferenceToExpression(valueRef: TypeValueReference): Expression|null {
if (valueRef.kind === TypeValueReferenceKind.UNAVAILABLE) {
return null;
} else if (valueRef.kind === TypeValueReferenceKind.LOCAL) {
const expr = new WrappedNodeExpr(valueRef.expression);
if (valueRef.defaultImportStatement !== null) {
attachDefaultImportDeclaration(expr, valueRef.defaultImportStatement);
}
return expr;
} else {
let importExpr: Expression =
new ExternalExpr({moduleName: valueRef.moduleName, name: valueRef.importedName});
if (valueRef.nestedPath !== null) {
for (const property of valueRef.nestedPath) {
importExpr = new ReadPropExpr(importExpr, property);
}
}
return importExpr;
}
}
export function toR3Reference(
origin: ts.Node, valueRef: Reference, typeRef: Reference, valueContext: ts.SourceFile,
typeContext: ts.SourceFile, refEmitter: ReferenceEmitter): R3Reference {
const emittedValueRef = refEmitter.emit(valueRef, valueContext);
assertSuccessfulReferenceEmit(emittedValueRef, origin, 'class');
const emittedTypeRef = refEmitter.emit(
typeRef, typeContext, ImportFlags.ForceNewImport | ImportFlags.AllowTypeImports);
assertSuccessfulReferenceEmit(emittedTypeRef, origin, 'class');
return {
value: emittedValueRef.expression,
type: emittedTypeRef.expression,
};
}
export function isAngularCore(decorator: Decorator): decorator is Decorator&{import: Import} {
return decorator.import !== null && decorator.import.from === '@angular/core';
}
export function isAngularCoreReference(reference: Reference, symbolName: string): boolean {
return reference.ownedByModuleGuess === '@angular/core' && reference.debugName === symbolName;
}
export function findAngularDecorator(
decorators: Decorator[], name: string, isCore: boolean): Decorator|undefined {
return decorators.find(decorator => isAngularDecorator(decorator, name, isCore));
}
export function isAngularDecorator(decorator: Decorator, name: string, isCore: boolean): boolean {
if (isCore) {
return decorator.name === name;
} else if (isAngularCore(decorator)) {
return decorator.import.name === name;
}
return false;
}
/**
* Unwrap a `ts.Expression`, removing outer type-casts or parentheses until the expression is in its
* lowest level form.
*
* For example, the expression "(foo as Type)" unwraps to "foo".
*/
export function unwrapExpression(node: ts.Expression): ts.Expression {
while (ts.isAsExpression(node) || ts.isParenthesizedExpression(node)) {
node = node.expression;
}
return node;
}
function expandForwardRef(arg: ts.Expression): ts.Expression|null {
arg = unwrapExpression(arg);
if (!ts.isArrowFunction(arg) && !ts.isFunctionExpression(arg)) {
return null;
}
const body = arg.body;
// Either the body is a ts.Expression directly, or a block with a single return statement.
if (ts.isBlock(body)) {
// Block body - look for a single return statement.
if (body.statements.length !== 1) {
return null;
}
const stmt = body.statements[0];
if (!ts.isReturnStatement(stmt) || stmt.expression === undefined) {
return null;
}
return stmt.expression;
} else {
// Shorthand body - return as an expression.
return body;
}
}
/**
* If the given `node` is a forwardRef() expression then resolve its inner value, otherwise return
* `null`.
*
* @param node the forwardRef() expression to resolve
* @param reflector a ReflectionHost
* @returns the resolved expression, if the original expression was a forwardRef(), or `null`
* otherwise.
*/
export function tryUnwrapForwardRef(node: ts.Expression, reflector: ReflectionHost): ts.Expression|
null {
node = unwrapExpression(node);
if (!ts.isCallExpression(node) || node.arguments.length !== 1) {
return null;
}
const fn =
ts.isPropertyAccessExpression(node.expression) ? node.expression.name : node.expression;
if (!ts.isIdentifier(fn)) {
return null;
}
const expr = expandForwardRef(node.arguments[0]);
if (expr === null) {
return null;
}
const imp = reflector.getImportOfIdentifier(fn);
if (imp === null || imp.from !== '@angular/core' || imp.name !== 'forwardRef') {
return null;
}
return expr;
}
/**
* A foreign function resolver for `staticallyResolve` which unwraps forwardRef() expressions.
*
* @param ref a Reference to the declaration of the function being called (which might be
* forwardRef)
* @param args the arguments to the invocation of the forwardRef expression
* @returns an unwrapped argument if `ref` pointed to forwardRef, or null otherwise
*/
export const forwardRefResolver: ForeignFunctionResolver =
(fn, callExpr, resolve, unresolvable) => {
if (!isAngularCoreReference(fn, 'forwardRef') || callExpr.arguments.length !== 1) {
return unresolvable;
}
const expanded = expandForwardRef(callExpr.arguments[0]);
if (expanded !== null) {
return resolve(expanded);
} else {
return unresolvable;
}
};
/**
* Combines an array of resolver functions into a one.
* @param resolvers Resolvers to be combined.
*/
export function combineResolvers(resolvers: ForeignFunctionResolver[]): ForeignFunctionResolver {
return (fn, callExpr, resolve, unresolvable) => {
for (const resolver of resolvers) {
const resolved = resolver(fn, callExpr, resolve, unresolvable);
if (resolved !== unresolvable) {
return resolved;
}
}
return unresolvable;
};
}
export function isExpressionForwardReference(
expr: Expression, context: ts.Node, contextSource: ts.SourceFile): boolean {
if (isWrappedTsNodeExpr(expr)) {
const node = ts.getOriginalNode(expr.node);
return node.getSourceFile() === contextSource && context.pos < node.pos;
} else {
return false;
}
}
export function isWrappedTsNodeExpr(expr: Expression): expr is WrappedNodeExpr<ts.Node> {
return expr instanceof WrappedNodeExpr;
}
export function readBaseClass(
node: ClassDeclaration, reflector: ReflectionHost,
evaluator: PartialEvaluator): Reference<ClassDeclaration>|'dynamic'|null {
const baseExpression = reflector.getBaseClassExpression(node);
if (baseExpression !== null) {
const baseClass = evaluator.evaluate(baseExpression);
if (baseClass instanceof Reference && reflector.isClass(baseClass.node)) {
return baseClass as Reference<ClassDeclaration>;
} else {
return 'dynamic';
}
}
return null;
}
const parensWrapperTransformerFactory: ts.TransformerFactory<ts.Expression> =
(context: ts.TransformationContext) => {
const visitor: ts.Visitor = (node: ts.Node): ts.Node => {
const visited = ts.visitEachChild(node, visitor, context);
if (ts.isArrowFunction(visited) || ts.isFunctionExpression(visited)) {
return ts.factory.createParenthesizedExpression(visited);
}
return visited;
};
return (node: ts.Expression) => ts.visitEachChild(node, visitor, context);
};
/**
* Wraps all functions in a given expression in parentheses. This is needed to avoid problems
* where Tsickle annotations added between analyse and transform phases in Angular may trigger
* automatic semicolon insertion, e.g. if a function is the expression in a `return` statement.
* More
* info can be found in Tsickle source code here:
* https://github.com/angular/tsickle/blob/d7974262571c8a17d684e5ba07680e1b1993afdd/src/jsdoc_transformer.ts#L1021
*
* @param expression Expression where functions should be wrapped in parentheses
*/
export function wrapFunctionExpressionsInParens(expression: ts.Expression): ts.Expression {
return ts.transform(expression, [parensWrapperTransformerFactory]).transformed[0];
}
/**
* Resolves the given `rawProviders` into `ClassDeclarations` and returns
* a set containing those that are known to require a factory definition.
* @param rawProviders Expression that declared the providers array in the source.
*/
export function resolveProvidersRequiringFactory(
rawProviders: ts.Expression, reflector: ReflectionHost,
evaluator: PartialEvaluator): Set<Reference<ClassDeclaration>> {
const providers = new Set<Reference<ClassDeclaration>>();
const resolvedProviders = evaluator.evaluate(rawProviders);
if (!Array.isArray(resolvedProviders)) {
return providers;
}
resolvedProviders.forEach(function processProviders(provider) {
let tokenClass: Reference|null = null;
if (Array.isArray(provider)) {
// If we ran into an array, recurse into it until we've resolve all the classes.
provider.forEach(processProviders);
} else if (provider instanceof Reference) {
tokenClass = provider;
} else if (provider instanceof Map && provider.has('useClass') && !provider.has('deps')) {
const useExisting = provider.get('useClass')!;
if (useExisting instanceof Reference) {
tokenClass = useExisting;
}
}
// TODO(alxhub): there was a bug where `getConstructorParameters` would return `null` for a
// class in a .d.ts file, always, even if the class had a constructor. This was fixed for
// `getConstructorParameters`, but that fix causes more classes to be recognized here as needing
// provider checks, which is a breaking change in g3. Avoid this breakage for now by skipping
// classes from .d.ts files here directly, until g3 can be cleaned up.
if (tokenClass !== null && !tokenClass.node.getSourceFile().isDeclarationFile &&
reflector.isClass(tokenClass.node)) {
const constructorParameters = reflector.getConstructorParameters(tokenClass.node);
// Note that we only want to capture providers with a non-trivial constructor,
// because they're the ones that might be using DI and need to be decorated.
if (constructorParameters !== null && constructorParameters.length > 0) {
providers.add(tokenClass as Reference<ClassDeclaration>);
}
}
});
return providers;
}
/**
* Create an R3Reference for a class.
*
* The `value` is the exported declaration of the class from its source file.
* The `type` is an expression that would be used by ngcc in the typings (.d.ts) files.
*/
export function wrapTypeReference(reflector: ReflectionHost, clazz: ClassDeclaration): R3Reference {
const dtsClass = reflector.getDtsDeclaration(clazz);
const value = new WrappedNodeExpr(clazz.name);
const type = dtsClass !== null && isNamedClassDeclaration(dtsClass) ?
new WrappedNodeExpr(dtsClass.name) :
value;
return {value, type};
}
/** Creates a ParseSourceSpan for a TypeScript node. */
export function createSourceSpan(node: ts.Node): ParseSourceSpan {
const sf = node.getSourceFile();
const [startOffset, endOffset] = [node.getStart(), node.getEnd()];
const {line: startLine, character: startCol} = sf.getLineAndCharacterOfPosition(startOffset);
const {line: endLine, character: endCol} = sf.getLineAndCharacterOfPosition(endOffset);
const parseSf = new ParseSourceFile(sf.getFullText(), sf.fileName);
// +1 because values are zero-indexed.
return new ParseSourceSpan(
new ParseLocation(parseSf, startOffset, startLine + 1, startCol + 1),
new ParseLocation(parseSf, endOffset, endLine + 1, endCol + 1));
}
/**
* Collate the factory and definition compiled results into an array of CompileResult objects.
*/
export function compileResults(
fac: CompileResult, def: R3CompiledExpression, metadataStmt: Statement|null,
propName: string): CompileResult[] {
const statements = def.statements;
if (metadataStmt !== null) {
statements.push(metadataStmt);
}
return [
fac, {
name: propName,
initializer: def.expression,
statements: def.statements,
type: def.type,
}
];
}
export function toFactoryMetadata(
meta: Omit<R3FactoryMetadata, 'target'>, target: FactoryTarget): R3FactoryMetadata {
return {
name: meta.name,
type: meta.type,
internalType: meta.internalType,
typeArgumentCount: meta.typeArgumentCount,
deps: meta.deps,
target
};
}
export function resolveImportedFile(
moduleResolver: ModuleResolver, importedFile: ImportedFile, expr: Expression,
origin: ts.SourceFile): ts.SourceFile|null {
// If `importedFile` is not 'unknown' then it accurately reflects the source file that is
// being imported.
if (importedFile !== 'unknown') {
return importedFile;
}
// Otherwise `expr` has to be inspected to determine the file that is being imported. If `expr`
// is not an `ExternalExpr` then it does not correspond with an import, so return null in that
// case.
if (!(expr instanceof ExternalExpr)) {
return null;
}
// Figure out what file is being imported.
return moduleResolver.resolveModule(expr.value.moduleName!, origin.fileName);
}
/**
* Determines the most appropriate expression for diagnostic reporting purposes. If `expr` is
* contained within `container` then `expr` is used as origin node, otherwise `container` itself is
* used.
*/
export function getOriginNodeForDiagnostics(
expr: ts.Expression, container: ts.Expression): ts.Expression {
const nodeSf = expr.getSourceFile();
const exprSf = container.getSourceFile();
if (nodeSf === exprSf && expr.pos >= container.pos && expr.end <= container.end) {
// `expr` occurs within the same source file as `container` and is contained within it, so
// `expr` is appropriate to use as origin node for diagnostics.
return expr;
} else {
return container;
}
}
export function isAbstractClassDeclaration(clazz: ClassDeclaration): boolean {
return clazz.modifiers !== undefined &&
clazz.modifiers.some(mod => mod.kind === ts.SyntaxKind.AbstractKeyword);
}