Repository navigation
Expand file tree
/
Copy pathdefinition.ts
More file actions
625 lines (534 loc) Β· 20.4 KB
/
Copy pathdefinition.ts
File metadata and controls
625 lines (534 loc) Β· 20.4 KB
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/**
* @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.dev/license
*/
import {InputSignalNode} from '../../authoring/input/input_signal_node';
import {ModuleWithProviders, ProcessProvidersFunction} from '../../di/interface/provider';
import {EnvironmentInjector} from '../../di/r3_injector';
import {AbstractType, Type} from '../../interface/type';
import {SchemaMetadata} from '../../metadata/schema';
import {ViewEncapsulation} from '../../metadata/view';
import {FactoryFn} from '../definition_factory';
import {TAttributes, TConstantsOrFactory} from './node';
import {CssSelectorList} from './projection';
import type {TView} from './view';
import type {ControlDirectiveDef} from './control';
/**
* Definition of what a template rendering function should look like for a component.
*/
export type ComponentTemplate<T> = {
// Note: the ctx parameter is typed as T|U, as using only U would prevent a template with
// e.g. ctx: {} from being assigned to ComponentTemplate<any> as TypeScript won't infer U = any
// in that scenario. By including T this incompatibility is resolved.
<U extends T>(rf: RenderFlags, ctx: T | U): void;
};
/**
* Definition of what a view queries function should look like.
*/
export type ViewQueriesFunction<T> = <U extends T>(rf: RenderFlags, ctx: U) => void;
/** Function that resolves providers and publishes them to the DI system. */
export type ProvidersResolver = (
def: DirectiveDef<unknown>,
processProvidersFn?: ProcessProvidersFunction,
) => void;
/**
* Definition of what a content queries function should look like.
*/
export type ContentQueriesFunction<T> = <U extends T>(
rf: RenderFlags,
ctx: U,
directiveIndex: number,
) => void;
export interface ClassDebugInfo {
className: string;
filePath?: string;
lineNumber?: number;
forbidOrphanRendering?: boolean;
}
/**
* Flags passed into template functions to determine which blocks (i.e. creation, update)
* should be executed.
*
* Typically, a template runs both the creation block and the update block on initialization and
* subsequent runs only execute the update block. However, dynamically created views require that
* the creation block be executed separately from the update block (for backwards compat).
*/
export const enum RenderFlags {
/* Whether to run the creation block (e.g. create elements and directives) */
Create = 0b01,
/* Whether to run the update block (e.g. refresh bindings) */
Update = 0b10,
}
/**
* A subclass of `Type` which has a static `Ι΅cmp`:`ComponentDef` field making it
* consumable for rendering.
*/
export interface ComponentType<T> extends Type<T> {
Ι΅cmp: unknown;
}
/**
* A subclass of `Type` which has a static `Ι΅dir`:`DirectiveDef` field making it
* consumable for rendering.
*/
export interface DirectiveType<T> extends Type<T> {
Ι΅dir: unknown;
Ι΅fac: unknown;
}
/**
* A subclass of `Type` which has a static `Ι΅pipe`:`PipeDef` field making it
* consumable for rendering.
*/
export interface PipeType<T> extends Type<T> {
Ι΅pipe: unknown;
}
/**
* Runtime link information for Directives.
*
* This is an internal data structure used by the render to link
* directives into templates.
*
* NOTE: Always use `defineDirective` function to create this object,
* never create the object directly since the shape of this object
* can change between versions.
*
* @param Selector type metadata specifying the selector of the directive or component
*
* See: {@link defineDirective}
*/
export interface DirectiveDef<T> {
/**
* A dictionary mapping the inputs' public name to their minified property names
* (along with flags if there are any).
*/
readonly inputs: Record<
string,
[minifiedName: string, flags: number, transform: InputTransformFunction | null]
>;
/**
* Contains the raw input information produced by the compiler. Can be
* used to do further processing after the `inputs` have been inverted.
*/
readonly inputConfig: {
[P in keyof T]?: string | [number, string, string?, InputTransformFunction?];
};
/**
* @deprecated This is only here because `NgOnChanges` incorrectly uses declared name instead of
* public or minified name.
*/
readonly declaredInputs: Record<string, string>;
/**
* A dictionary mapping the outputs' minified property names to their public API names, which
* are their aliases if any, or their original unminified property names
* (as in `@Output('alias') propertyName: any;`).
*/
readonly outputs: Record<string, string>;
/**
* Function to create and refresh content queries associated with a given directive.
*/
contentQueries: ContentQueriesFunction<T> | null;
/**
* Query-related instructions for a directive. Note that while directives don't have a
* view and as such view queries won't necessarily do anything, there might be
* components that extend the directive.
*/
viewQuery: ViewQueriesFunction<T> | null;
/**
* Refreshes host bindings on the associated directive.
*/
readonly hostBindings: HostBindingsFunction<T> | null;
/**
* The number of bindings in this directive `hostBindings` (including pure fn bindings).
*
* Used to calculate the length of the component's LView array, so we
* can pre-fill the array and set the host binding start index.
*/
readonly hostVars: number;
/**
* Assign static attribute values to a host element.
*
* This property will assign static attribute values as well as class and style
* values to a host element. Since attribute values can consist of different types of values, the
* `hostAttrs` array must include the values in the following format:
*
* attrs = [
* // static attributes (like `title`, `name`, `id`...)
* attr1, value1, attr2, value,
*
* // a single namespace value (like `x:id`)
* NAMESPACE_MARKER, namespaceUri1, name1, value1,
*
* // another single namespace value (like `x:name`)
* NAMESPACE_MARKER, namespaceUri2, name2, value2,
*
* // a series of CSS classes that will be applied to the element (no spaces)
* CLASSES_MARKER, class1, class2, class3,
*
* // a series of CSS styles (property + value) that will be applied to the element
* STYLES_MARKER, prop1, value1, prop2, value2
* ]
*
* All non-class and non-style attributes must be defined at the start of the list
* first before all class and style values are set. When there is a change in value
* type (like when classes and styles are introduced) a marker must be used to separate
* the entries. The marker values themselves are set via entries found in the
* [AttributeMarker] enum.
*/
readonly hostAttrs: TAttributes | null;
/** Token representing the directive. Used by DI. */
readonly type: Type<T> | AbstractType<T>;
/** Function that resolves `providers` and publishes them into the DI system. */
providersResolver: ProvidersResolver | null;
/** Function that resolves `viewProviders` and publishes them into the DI system. */
viewProvidersResolver: ProvidersResolver | null;
/** The selectors that will be used to match nodes to this directive. */
readonly selectors: CssSelectorList;
/**
* Name under which the directive is exported (for use with local references in template)
*/
readonly exportAs: string[] | null;
/**
* Whether this directive (or component) is standalone.
*/
readonly standalone: boolean;
/**
* Whether this directive (or component) uses the signals authoring experience.
*/
readonly signals: boolean;
/**
* Factory function used to create a new directive instance. Will be null initially.
* Populated when the factory is first requested by directive instantiation logic.
*/
readonly factory: FactoryFn<T> | null;
/**
* The features applied to this directive
*/
readonly features: DirectiveDefFeature[] | null;
/**
* Info related to debugging/troubleshooting for this component. This info is only available in
* dev mode.
*/
debugInfo: ClassDebugInfo | null;
/**
* Function inteded to be called after template selector matching is done
* in order to resolve information about their host directives. Patched
* onto the definition by the `Ι΅Ι΅HostDirectivesFeature`.
*/
resolveHostDirectives: ((matches: DirectiveDef<unknown>[]) => HostDirectiveResolution) | null;
/**
* Additional directives to be applied whenever the directive has been matched.
*
* `HostDirectiveConfig` objects represent a host directive that can be resolved eagerly and were
* already pre-processed when the definition was created. A function needs to be resolved lazily
* during directive matching, because it's a forward reference.
*
* **Note:** we can't use `HostDirectiveConfig` in the array, because there's no way to
* distinguish if a function in the array is a `Type` or a `() => HostDirectiveConfig[]`.
*/
hostDirectives: (HostDirectiveDef | (() => HostDirectiveConfig[]))[] | null;
controlDef: ControlDirectiveDef | null;
/**
* Cache of inputs that this custom control directive covers,
* used by the signal forms system.
*/
signalFormsInputPresence: Record<string, boolean> | null;
setInput:
| (<U extends T>(
this: DirectiveDef<U>,
instance: U,
inputSignalNode: null | InputSignalNode<unknown, unknown>,
value: any,
publicName: string,
privateName: string,
) => void)
| null;
}
/**
* Runtime link information for Components.
*
* This is an internal data structure used by the render to link
* components into templates.
*
* NOTE: Always use `defineComponent` function to create this object,
* never create the object directly since the shape of this object
* can change between versions.
*
* See: {@link defineComponent}
*/
export interface ComponentDef<T> extends DirectiveDef<T> {
/**
* Unique ID for the component. Used in view encapsulation and
* to keep track of the injector in standalone components.
*/
readonly id: string;
/**
* The View template of the component.
*/
readonly template: ComponentTemplate<T>;
/** Constants associated with the component's view. */
readonly consts: TConstantsOrFactory | null;
/**
* An array of `ngContent[selector]` values that were found in the template.
*/
readonly ngContentSelectors?: string[];
/**
* A set of styles that the component needs to be present for component to render correctly.
*/
readonly styles: string[];
/**
* The number of nodes, local refs, and pipes in this component template.
*
* Used to calculate the length of the component's LView array, so we
* can pre-fill the array and set the binding start index.
*/
// TODO(kara): remove queries from this count
readonly decls: number;
/**
* The number of bindings in this component template (including pure fn bindings).
*
* Used to calculate the length of the component's LView array, so we
* can pre-fill the array and set the host binding start index.
*/
readonly vars: number;
/**
* Query-related instructions for a component.
*/
viewQuery: ViewQueriesFunction<T> | null;
/**
* The view encapsulation type, which determines how styles are applied to
* DOM elements. One of
* - `Emulated` (default): Emulate native scoping of styles.
* - `Native`: Use the native encapsulation mechanism of the renderer.
* - `ShadowDom`: Use modern [ShadowDOM](https://w3c.github.io/webcomponents/spec/shadow/) and
* create a ShadowRoot for component's host element.
* - `None`: Do not provide any template or style encapsulation.
*/
readonly encapsulation: ViewEncapsulation;
/**
* Defines arbitrary developer-defined data to be stored on a renderer instance.
* This is useful for renderers that delegate to other renderers.
*/
readonly data: {
[kind: string]: any;
animation?: any[];
};
/** Whether or not this component's ChangeDetectionStrategy is OnPush */
readonly onPush: boolean;
/** Whether or not this component is signal-based. */
readonly signals: boolean;
/**
* Registry of directives and components that may be found in this view.
*
* The property is either an array of `DirectiveDef`s or a function which returns the array of
* `DirectiveDef`s. The function is necessary to be able to support forward declarations.
*/
directiveDefs: DirectiveDefListOrFactory | null;
/**
* Registry of pipes that may be found in this view.
*
* The property is either an array of `PipeDefs`s or a function which returns the array of
* `PipeDefs`s. The function is necessary to be able to support forward declarations.
*/
pipeDefs: PipeDefListOrFactory | null;
/**
* Unfiltered list of all dependencies of a component, or `null` if none.
*/
dependencies: TypeOrFactory<DependencyTypeList> | null;
/**
* The set of schemas that declare elements to be allowed in the component's template.
*/
schemas: SchemaMetadata[] | null;
/**
* Ivy runtime uses this place to store the computed tView for the component. This gets filled on
* the first run of component.
*/
tView: TView | null;
/**
* A function used by the framework to create standalone injectors.
*/
getStandaloneInjector:
((parentInjector: EnvironmentInjector) => EnvironmentInjector | null) | null;
/**
* A function used by the framework to create the list of external runtime style URLs.
*/
getExternalStyles: ((encapsulationId?: string) => string[]) | null;
/**
* Used to store the result of `noSideEffects` function so that it is not removed by closure
* compiler. The property should never be read.
*/
readonly _?: unknown;
}
/**
* Runtime link information for Pipes.
*
* This is an internal data structure used by the renderer to link
* pipes into templates.
*
* NOTE: Always use `definePipe` function to create this object,
* never create the object directly since the shape of this object
* can change between versions.
*
* See: {@link definePipe}
*/
export interface PipeDef<T> {
/** Token representing the pipe. */
type: Type<T> | AbstractType<T>;
/**
* Pipe name.
*
* Used to resolve pipe in templates.
*/
readonly name: string;
/**
* Factory function used to create a new pipe instance. Will be null initially.
* Populated when the factory is first requested by pipe instantiation logic.
*/
factory: FactoryFn<T> | null;
/**
* Whether or not the pipe is pure.
*
* Pure pipes result only depends on the pipe input and not on internal
* state of the pipe.
*/
readonly pure: boolean;
/**
* Whether this pipe is standalone.
*/
readonly standalone: boolean;
/* The following are lifecycle hooks for this pipe */
onDestroy: (() => void) | null;
}
export interface DirectiveDefFeature {
<T>(directiveDef: DirectiveDef<T>): void;
/**
* Marks a feature as something that {@link InheritDefinitionFeature} will execute
* during inheritance.
*
* NOTE: DO NOT SET IN ROOT OF MODULE! Doing so will result in tree-shakers/bundlers
* identifying the change as a side effect, and the feature will be included in
* every bundle.
*/
ngInherit?: true;
}
/** Data produced after host directives are resolved for a node. */
export type HostDirectiveResolution = [
matches: DirectiveDef<unknown>[],
hostDirectiveDefs: HostDirectiveDefs | null,
hostDirectiveRanges: HostDirectiveRanges | null,
];
/**
* Map that tracks a selector-matched directive to the range within which its host directives
* are declared. Host directives for a specific directive are always contiguous within the runtime.
* Note that both the start and end are inclusive and they're both **after** `tNode.directiveStart`.
*/
export type HostDirectiveRanges = Map<DirectiveDef<unknown>, [start: number, end: number]>;
/** Runtime information used to configure a host directive. */
export interface HostDirectiveDef<T = unknown> {
/** Class representing the host directive. */
directive: Type<T>;
/** Directive inputs that have been exposed. */
inputs: HostDirectiveBindingMap;
/** Directive outputs that have been exposed. */
outputs: HostDirectiveBindingMap;
}
/**
* Mapping between the public aliases of directive bindings and the underlying inputs/outputs that
* they represent. Also serves as an allowlist of the inputs/outputs from the host directive that
* the author has decided to expose.
*/
export type HostDirectiveBindingMap = {
[publicName: string]: string;
};
/**
* Mapping between a directive that was used as a host directive
* and the configuration that was used to define it as such.
*/
export type HostDirectiveDefs = Map<DirectiveDef<unknown>, HostDirectiveDef>;
/** Value that can be used to configure a host directive. */
export type HostDirectiveConfig =
| Type<unknown>
| {
directive: Type<unknown>;
inputs?: string[];
outputs?: string[];
};
export interface ComponentDefFeature {
<T>(componentDef: ComponentDef<T>): void;
/**
* Marks a feature as something that {@link InheritDefinitionFeature} will execute
* during inheritance.
*
* NOTE: DO NOT SET IN ROOT OF MODULE! Doing so will result in tree-shakers/bundlers
* identifying the change as a side effect, and the feature will be included in
* every bundle.
*/
ngInherit?: true;
}
/** Function that can be used to transform incoming input values. */
export type InputTransformFunction = (value: any) => any;
/**
* Type used for directiveDefs on component definition.
*
* The function is necessary to be able to support forward declarations.
*/
export type DirectiveDefListOrFactory = (() => DirectiveDefList) | DirectiveDefList;
export type DirectiveDefList = (DirectiveDef<any> | ComponentDef<any>)[];
export type DependencyDef = DirectiveDef<unknown> | ComponentDef<unknown> | PipeDef<unknown>;
export type DirectiveTypesOrFactory = (() => DirectiveTypeList) | DirectiveTypeList;
export type DirectiveTypeList = (DirectiveType<any> | ComponentType<any> | Type<any>)[];
/* Type as workaround for: Microsoft/TypeScript/issues/4881 */
export type DependencyType = DirectiveType<any> | ComponentType<any> | PipeType<any> | Type<any>;
export type DependencyTypeList = Array<DependencyType>;
export type TypeOrFactory<T> = T | (() => T);
export type HostBindingsFunction<T> = <U extends T>(rf: RenderFlags, ctx: U) => void;
/**
* Type used for PipeDefs on component definition.
*
* The function is necessary to be able to support forward declarations.
*/
export type PipeDefListOrFactory = (() => PipeDefList) | PipeDefList;
export type PipeDefList = PipeDef<any>[];
export type PipeTypesOrFactory = (() => PipeTypeList) | PipeTypeList;
export type PipeTypeList = (PipeType<any> | Type<any>)[];
/* Type as workaround for: Microsoft/TypeScript/issues/4881 */
/**
* NgModule scope info as provided by AoT compiler
*
* In full compilation Ivy resolved all the "module with providers" and forward refs the whole array
* if at least one element is forward refed. So we end up with type `Type<any>[]|(() =>
* Type<any>[])`.
*
* In local mode the compiler passes the raw info as they are to the runtime functions as it is not
* possible to resolve them any further due to limited info at compile time. So we end up with type
* `RawScopeInfoFromDecorator[]`.
*/
export interface NgModuleScopeInfoFromDecorator {
/** List of components, directives, and pipes declared by this module. */
declarations?: Type<any>[] | (() => Type<any>[]) | RawScopeInfoFromDecorator[];
/** List of modules or `ModuleWithProviders` or standalone components imported by this module. */
imports?: Type<any>[] | (() => Type<any>[]) | RawScopeInfoFromDecorator[];
/**
* List of modules, `ModuleWithProviders`, components, directives, or pipes exported by this
* module.
*/
exports?: Type<any>[] | (() => Type<any>[]) | RawScopeInfoFromDecorator[];
/**
* The set of components that are bootstrapped when this module is bootstrapped. This field is
* only available in local compilation mode. In full compilation mode bootstrap info is passed
* directly to the module def runtime after statically analyzed and resolved.
*/
bootstrap?: Type<any>[] | (() => Type<any>[]) | RawScopeInfoFromDecorator[];
}
/**
* The array element type passed to:
* - NgModule's annotation imports/exports/declarations fields
* - standalone component annotation imports field
*/
export type RawScopeInfoFromDecorator =
| Type<any>
| ModuleWithProviders<any>
| (() => Type<any>)
| (() => ModuleWithProviders<any>)
| readonly any[];