-
Notifications
You must be signed in to change notification settings - Fork 24.8k
/
api.ts
548 lines (464 loc) Β· 15.3 KB
/
api.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
/**
* @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 {ChangeDetectionStrategy, ViewEncapsulation} from '../../core';
import {InterpolationConfig} from '../../ml_parser/defaults';
import * as o from '../../output/output_ast';
import {ParseSourceSpan} from '../../parse_util';
import * as t from '../r3_ast';
import {R3DependencyMetadata} from '../r3_factory';
import {MaybeForwardRefExpression, R3Reference} from '../util';
/**
* Information needed to compile a directive for the render3 runtime.
*/
export interface R3DirectiveMetadata {
/**
* Name of the directive type.
*/
name: string;
/**
* An expression representing a reference to the directive itself.
*/
type: R3Reference;
/**
* Number of generic type parameters of the type itself.
*/
typeArgumentCount: number;
/**
* A source span for the directive type.
*/
typeSourceSpan: ParseSourceSpan;
/**
* Dependencies of the directive's constructor.
*/
deps: R3DependencyMetadata[]|'invalid'|null;
/**
* Unparsed selector of the directive, or `null` if there was no selector.
*/
selector: string|null;
/**
* Information about the content queries made by the directive.
*/
queries: R3QueryMetadata[];
/**
* Information about the view queries made by the directive.
*/
viewQueries: R3QueryMetadata[];
/**
* Mappings indicating how the directive interacts with its host element (host bindings,
* listeners, etc).
*/
host: R3HostMetadata;
/**
* Information about usage of specific lifecycle events which require special treatment in the
* code generator.
*/
lifecycle: {
/**
* Whether the directive uses NgOnChanges.
*/
usesOnChanges: boolean;
};
/**
* A mapping of inputs from class property names to binding property names, or to a tuple of
* binding property name and class property name if the names are different.
*/
inputs: {[field: string]: R3InputMetadata};
/**
* A mapping of outputs from class property names to binding property names, or to a tuple of
* binding property name and class property name if the names are different.
*/
outputs: {[field: string]: string};
/**
* Whether or not the component or directive inherits from another class
*/
usesInheritance: boolean;
/**
* Whether or not the component or directive inherits its entire decorator from its base class.
*/
fullInheritance: boolean;
/**
* Reference name under which to export the directive's type in a template,
* if any.
*/
exportAs: string[]|null;
/**
* The list of providers defined in the directive.
*/
providers: o.Expression|null;
/**
* Whether or not the component or directive is standalone.
*/
isStandalone: boolean;
/**
* Whether or not the component or directive is signal-based.
*/
isSignal: boolean;
/**
* Additional directives applied to the directive host.
*/
hostDirectives: R3HostDirectiveMetadata[]|null;
}
/**
* Defines how dynamic imports for deferred dependencies should be emitted in the
* generated output:
* - either in a function on per-component basis (in case of local compilation)
* - or in a function on per-block basis (in full compilation mode)
*/
export const enum DeferBlockDepsEmitMode {
/**
* Dynamic imports are grouped on per-block basis.
*
* This is used in full compilation mode, when compiler has more information
* about particular dependencies that belong to this block.
*/
PerBlock,
/**
* Dynamic imports are grouped on per-component basis.
*
* In local compilation, compiler doesn't have enough information to determine
* which deferred dependencies belong to which block. In this case we group all
* dynamic imports into a single file on per-component basis.
*/
PerComponent,
}
/**
* Specifies how a list of declaration type references should be emitted into the generated code.
*/
export const enum DeclarationListEmitMode {
/**
* The list of declarations is emitted into the generated code as is.
*
* ```
* directives: [MyDir],
* ```
*/
Direct,
/**
* The list of declarations is emitted into the generated code wrapped inside a closure, which
* is needed when at least one declaration is a forward reference.
*
* ```
* directives: function () { return [MyDir, ForwardDir]; },
* ```
*/
Closure,
/**
* Similar to `Closure`, with the addition that the list of declarations can contain individual
* items that are themselves forward references. This is relevant for JIT compilations, as
* unwrapping the forwardRef cannot be done statically so must be deferred. This mode emits
* the declaration list using a mapping transform through `resolveForwardRef` to ensure that
* any forward references within the list are resolved when the outer closure is invoked.
*
* Consider the case where the runtime has captured two declarations in two distinct values:
* ```
* const dirA = MyDir;
* const dirB = forwardRef(function() { return ForwardRef; });
* ```
*
* This mode would emit the declarations captured in `dirA` and `dirB` as follows:
* ```
* directives: function () { return [dirA, dirB].map(ng.resolveForwardRef); },
* ```
*/
ClosureResolved,
RuntimeResolved,
}
/**
* Describes a dependency used within a `@defer` block.
*/
export interface R3DeferBlockTemplateDependency {
/**
* Reference to a dependency.
*/
type: o.WrappedNodeExpr<unknown>;
/**
* Dependency class name.
*/
symbolName: string;
/**
* Whether this dependency can be defer-loaded.
*/
isDeferrable: boolean;
/**
* Import path where this dependency is located.
*/
importPath: string|null;
/**
* Whether the symbol is the default export.
*/
isDefaultImport: boolean;
}
/**
* Information necessary to compile a `defer` block.
*/
export interface R3DeferBlockMetadata {
/** Dependencies used within the block. */
deps: R3DeferBlockTemplateDependency[];
/** Mapping between triggers and the DOM nodes they refer to. */
triggerElements: Map<t.DeferredTrigger, t.Element|null>;
}
/**
* Information needed to compile a component for the render3 runtime.
*/
export interface R3ComponentMetadata<DeclarationT extends R3TemplateDependency> extends
R3DirectiveMetadata {
/**
* Information about the component's template.
*/
template: {
/**
* Parsed nodes of the template.
*/
nodes: t.Node[];
/**
* Any ng-content selectors extracted from the template. Contains `*` when an ng-content
* element without selector is present.
*/
ngContentSelectors: string[];
/**
* Whether the template preserves whitespaces from the user's code.
*/
preserveWhitespaces?: boolean;
};
declarations: DeclarationT[];
/**
* Map of all types that can be defer loaded (ts.ClassDeclaration) ->
* corresponding import declaration (ts.ImportDeclaration) within
* the current source file.
*/
deferrableDeclToImportDecl: Map<o.Expression, o.Expression>;
/**
* Map of `@defer` blocks -> their corresponding metadata.
*/
deferBlocks: Map<t.DeferredBlock, R3DeferBlockMetadata>;
/**
* Defines how dynamic imports for deferred dependencies should be grouped:
* - either in a function on per-component basis (in case of local compilation)
* - or in a function on per-block basis (in full compilation mode)
*/
deferBlockDepsEmitMode: DeferBlockDepsEmitMode;
/**
* Map of deferrable symbol names -> corresponding import paths.
*/
deferrableTypes: Map<string, {importPath: string, isDefaultImport: boolean}>;
/**
* Specifies how the 'directives' and/or `pipes` array, if generated, need to be emitted.
*/
declarationListEmitMode: DeclarationListEmitMode;
/**
* A collection of styling data that will be applied and scoped to the component.
*/
styles: string[];
/**
* An encapsulation policy for the component's styling.
* Possible values:
* - `ViewEncapsulation.Emulated`: Apply modified component styles in order to emulate
* a native Shadow DOM CSS encapsulation behavior.
* - `ViewEncapsulation.None`: Apply component styles globally without any sort of encapsulation.
* - `ViewEncapsulation.ShadowDom`: Use the browser's native Shadow DOM API to encapsulate styles.
*/
encapsulation: ViewEncapsulation;
/**
* A collection of animation triggers that will be used in the component template.
*/
animations: o.Expression|null;
/**
* The list of view providers defined in the component.
*/
viewProviders: o.Expression|null;
/**
* Path to the .ts file in which this template's generated code will be included, relative to
* the compilation root. This will be used to generate identifiers that need to be globally
* unique in certain contexts (such as g3).
*/
relativeContextFilePath: string;
/**
* Whether translation variable name should contain external message id
* (used by Closure Compiler's output of `goog.getMsg` for transition period).
*/
i18nUseExternalIds: boolean;
/**
* Overrides the default interpolation start and end delimiters ({{ and }}).
*/
interpolation: InterpolationConfig;
/**
* Strategy used for detecting changes in the component.
*
* In global compilation mode the value is ChangeDetectionStrategy if available as it is
* statically resolved during analysis phase. Whereas in local compilation mode the value is the
* expression as appears in the decorator.
*/
changeDetection: ChangeDetectionStrategy|o.Expression|null;
/**
* The imports expression as appears on the component decorate for standalone component. This
* field is currently needed only for local compilation, and so in other compilation modes it may
* not be set. If component has empty array imports then this field is not set.
*/
rawImports?: o.Expression;
useTemplatePipeline: boolean;
}
/**
* Metadata for an individual input on a directive.
*/
export interface R3InputMetadata {
classPropertyName: string;
bindingPropertyName: string;
required: boolean;
isSignal: boolean;
/**
* Transform function for the input.
*
* Null if there is no transform, or if this is a signal input.
* Signal inputs capture their transform as part of the `InputSignal`.
*/
transformFunction: o.Expression|null;
}
export enum R3TemplateDependencyKind {
Directive = 0,
Pipe = 1,
NgModule = 2,
}
/**
* A dependency that's used within a component template.
*/
export interface R3TemplateDependency {
kind: R3TemplateDependencyKind;
/**
* The type of the dependency as an expression.
*/
type: o.Expression;
}
/**
* A dependency that's used within a component template
*/
export type R3TemplateDependencyMetadata =
R3DirectiveDependencyMetadata|R3PipeDependencyMetadata|R3NgModuleDependencyMetadata;
/**
* Information about a directive that is used in a component template. Only the stable, public
* facing information of the directive is stored here.
*/
export interface R3DirectiveDependencyMetadata extends R3TemplateDependency {
kind: R3TemplateDependencyKind.Directive;
/**
* The selector of the directive.
*/
selector: string;
/**
* The binding property names of the inputs of the directive.
*/
inputs: string[];
/**
* The binding property names of the outputs of the directive.
*/
outputs: string[];
/**
* Name under which the directive is exported, if any (exportAs in Angular). Null otherwise.
*/
exportAs: string[]|null;
/**
* If true then this directive is actually a component; otherwise it is not.
*/
isComponent: boolean;
}
export interface R3PipeDependencyMetadata extends R3TemplateDependency {
kind: R3TemplateDependencyKind.Pipe;
name: string;
}
export interface R3NgModuleDependencyMetadata extends R3TemplateDependency {
kind: R3TemplateDependencyKind.NgModule;
}
/**
* Information needed to compile a query (view or content).
*/
export interface R3QueryMetadata {
/**
* Name of the property on the class to update with query results.
*/
propertyName: string;
/**
* Whether to read only the first matching result, or an array of results.
*/
first: boolean;
/**
* Either an expression representing a type or `InjectionToken` for the query
* predicate, or a set of string selectors.
*
* Note: At compile time we split selectors as an optimization that avoids this
* extra work at runtime creation phase.
*
* Notably, if the selector is not statically analyzable due to an expression,
* the selectors may need to be split up at runtime.
*/
predicate: MaybeForwardRefExpression|string[];
/**
* Whether to include only direct children or all descendants.
*/
descendants: boolean;
/**
* If the `QueryList` should fire change event only if actual change to query was computed (vs old
* behavior where the change was fired whenever the query was recomputed, even if the recomputed
* query resulted in the same list.)
*/
emitDistinctChangesOnly: boolean;
/**
* An expression representing a type to read from each matched node, or null if the default value
* for a given node is to be returned.
*/
read: o.Expression|null;
/**
* Whether or not this query should collect only static results.
*
* If static is true, the query's results will be set on the component after nodes are created,
* but before change detection runs. This means that any results that relied upon change detection
* to run (e.g. results inside *ngIf or *ngFor views) will not be collected. Query results are
* available in the ngOnInit hook.
*
* If static is false, the query's results will be set on the component after change detection
* runs. This means that the query results can contain nodes inside *ngIf or *ngFor views, but
* the results will not be available in the ngOnInit hook (only in the ngAfterContentInit for
* content hooks and ngAfterViewInit for view hooks).
*
* Note: For signal-based queries, this option does not have any effect.
*/
static: boolean;
/** Whether the query is signal-based. */
isSignal: boolean;
}
/**
* Mappings indicating how the class interacts with its
* host element (host bindings, listeners, etc).
*/
export interface R3HostMetadata {
/**
* A mapping of attribute binding keys to `o.Expression`s.
*/
attributes: {[key: string]: o.Expression};
/**
* A mapping of event binding keys to unparsed expressions.
*/
listeners: {[key: string]: string};
/**
* A mapping of property binding keys to unparsed expressions.
*/
properties: {[key: string]: string};
specialAttributes: {styleAttr?: string; classAttr?: string;};
useTemplatePipeline: boolean;
}
/**
* Information needed to compile a host directive for the render3 runtime.
*/
export interface R3HostDirectiveMetadata {
/** An expression representing the host directive class itself. */
directive: R3Reference;
/** Whether the expression referring to the host directive is a forward reference. */
isForwardReference: boolean;
/** Inputs from the host directive that will be exposed on the host. */
inputs: {[publicName: string]: string}|null;
/** Outputs from the host directive that will be exposed on the host. */
outputs: {[publicName: string]: string}|null;
}