forked from angular/angular
/
t2_api.ts
230 lines (198 loc) · 7 KB
/
t2_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
/**
* @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 {AST} from '../../expression_parser/ast';
import {BoundAttribute, BoundEvent, DeferredBlock, DeferredBlockError, DeferredBlockLoading, DeferredBlockPlaceholder, DeferredTrigger, Element, ForLoopBlock, ForLoopBlockEmpty, IfBlockBranch, Node, Reference, SwitchBlockCase, Template, TextAttribute, Variable} from '../r3_ast';
/** Node that has a `Scope` associated with it. */
export type ScopedNode = Template|SwitchBlockCase|IfBlockBranch|ForLoopBlock|ForLoopBlockEmpty|
DeferredBlock|DeferredBlockError|DeferredBlockLoading|DeferredBlockPlaceholder;
/** Possible values that a reference can be resolved to. */
export type ReferenceTarget<DirectiveT> = {
directive: DirectiveT,
node: Element|Template
}|Element|Template;
/*
* t2 is the replacement for the `TemplateDefinitionBuilder`. It handles the operations of
* analyzing Angular templates, extracting semantic info, and ultimately producing a template
* definition function which renders the template using Ivy instructions.
*
* t2 data is also utilized by the template type-checking facilities to understand a template enough
* to generate type-checking code for it.
*/
/**
* A logical target for analysis, which could contain a template or other types of bindings.
*/
export interface Target {
template?: Node[];
}
/**
* A data structure which can indicate whether a given property name is present or not.
*
* This is used to represent the set of inputs or outputs present on a directive, and allows the
* binder to query for the presence of a mapping for property names.
*/
export interface InputOutputPropertySet {
hasBindingPropertyName(propertyName: string): boolean;
}
/**
* A data structure which captures the animation trigger names that are statically resolvable
* and whether some names could not be statically evaluated.
*/
export interface AnimationTriggerNames {
includesDynamicAnimations: boolean;
staticTriggerNames: string[];
}
/**
* Metadata regarding a directive that's needed to match it against template elements. This is
* provided by a consumer of the t2 APIs.
*/
export interface DirectiveMeta {
/**
* Name of the directive class (used for debugging).
*/
name: string;
/** The selector for the directive or `null` if there isn't one. */
selector: string|null;
/**
* Whether the directive is a component.
*/
isComponent: boolean;
/**
* Set of inputs which this directive claims.
*
* Goes from property names to field names.
*/
inputs: InputOutputPropertySet;
/**
* Set of outputs which this directive claims.
*
* Goes from property names to field names.
*/
outputs: InputOutputPropertySet;
/**
* Name under which the directive is exported, if any (exportAs in Angular).
*
* Null otherwise
*/
exportAs: string[]|null;
/**
* Whether the directive is a structural directive (e.g. `<div *ngIf></div>`).
*/
isStructural: boolean;
/**
* If the directive is a component, includes the selectors of its `ng-content` elements.
*/
ngContentSelectors: string[]|null;
/**
* Whether the template of the component preserves whitespaces.
*/
preserveWhitespaces: boolean;
/**
* The name of animations that the user defines in the component.
* Only includes the animation names.
*/
animationTriggerNames: AnimationTriggerNames|null;
}
/**
* Interface to the binding API, which processes a template and returns an object similar to the
* `ts.TypeChecker`.
*
* The returned `BoundTarget` has an API for extracting information about the processed target.
*/
export interface TargetBinder<D extends DirectiveMeta> {
bind(target: Target): BoundTarget<D>;
}
/**
* Result of performing the binding operation against a `Target`.
*
* The original `Target` is accessible, as well as a suite of methods for extracting binding
* information regarding the `Target`.
*
* @param DirectiveT directive metadata type
*/
export interface BoundTarget<DirectiveT extends DirectiveMeta> {
/**
* Get the original `Target` that was bound.
*/
readonly target: Target;
/**
* For a given template node (either an `Element` or a `Template`), get the set of directives
* which matched the node, if any.
*/
getDirectivesOfNode(node: Element|Template): DirectiveT[]|null;
/**
* For a given `Reference`, get the reference's target - either an `Element`, a `Template`, or
* a directive on a particular node.
*/
getReferenceTarget(ref: Reference): ReferenceTarget<DirectiveT>|null;
/**
* For a given binding, get the entity to which the binding is being made.
*
* This will either be a directive or the node itself.
*/
getConsumerOfBinding(binding: BoundAttribute|BoundEvent|TextAttribute): DirectiveT|Element
|Template|null;
/**
* If the given `AST` expression refers to a `Reference` or `Variable` within the `Target`, then
* return that.
*
* Otherwise, returns `null`.
*
* This is only defined for `AST` expressions that read or write to a property of an
* `ImplicitReceiver`.
*/
getExpressionTarget(expr: AST): Reference|Variable|null;
/**
* Given a particular `Reference` or `Variable`, get the `ScopedNode` which created it.
*
* All `Variable`s are defined on node, so this will always return a value for a `Variable`
* from the `Target`. Returns `null` otherwise.
*/
getDefinitionNodeOfSymbol(symbol: Reference|Variable): ScopedNode|null;
/**
* Get the nesting level of a particular `ScopedNode`.
*
* This starts at 1 for top-level nodes within the `Target` and increases for nodes
* nested at deeper levels.
*/
getNestingLevel(node: ScopedNode): number;
/**
* Get all `Reference`s and `Variables` visible within the given `ScopedNode` (or at the top
* level, if `null` is passed).
*/
getEntitiesInScope(node: ScopedNode|null): ReadonlySet<Reference|Variable>;
/**
* Get a list of all the directives used by the target,
* including directives from `@defer` blocks.
*/
getUsedDirectives(): DirectiveT[];
/**
* Get a list of eagerly used directives from the target.
* Note: this list *excludes* directives from `@defer` blocks.
*/
getEagerlyUsedDirectives(): DirectiveT[];
/**
* Get a list of all the pipes used by the target,
* including pipes from `@defer` blocks.
*/
getUsedPipes(): string[];
/**
* Get a list of eagerly used pipes from the target.
* Note: this list *excludes* pipes from `@defer` blocks.
*/
getEagerlyUsedPipes(): string[];
/**
* Get a list of all `@defer` blocks used by the target.
*/
getDeferBlocks(): DeferredBlock[];
/**
* Gets the element that a specific deferred block trigger is targeting.
* @param block Block that the trigger belongs to.
* @param trigger Trigger whose target is being looked up.
*/
getDeferredTriggerTarget(block: DeferredBlock, trigger: DeferredTrigger): Element|null;
}