-
Notifications
You must be signed in to change notification settings - Fork 190
/
nodes-v1.ts
365 lines (302 loc) · 8.06 KB
/
nodes-v1.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
import type { Nullable, PresentArray } from '@glimmer/interfaces';
import type * as src from '../source/api';
export interface BaseNode {
// Every leaf interface that extends BaseNode must specify a type property.
// The type property should be a string literal. For example, Identifier
// has: `type: "Identifier"`
type: NodeType;
loc: src.SourceSpan;
}
export interface CommonProgram extends BaseNode {
body: Statement[];
}
export interface Block extends CommonProgram {
type: 'Block';
params: VarHead[];
chained?: boolean;
/**
* string accessor for params.name
*/
blockParams: string[];
}
export type EntityEncodingState = 'transformed' | 'raw';
export interface Template extends CommonProgram {
type: 'Template';
locals: string[];
/**
* @deprecated use locals instead
*/
blockParams: string[];
}
/**
* @deprecated use Template or Block instead
*/
export type Program = Template | Block;
export type CallableExpression = SubExpression | PathExpression;
export interface CallParts {
path: CallableExpression;
params: Expression[];
hash: Hash;
}
export type CallNode =
| MustacheStatement
| BlockStatement
| ElementModifierStatement
| SubExpression;
export interface MustacheStatement extends BaseNode {
type: 'MustacheStatement';
path: Expression;
params: Expression[];
hash: Hash;
trusting: boolean;
strip: StripFlags;
/**
* @deprecated use trusting instead
*/
escaped: boolean;
}
export interface BlockStatement extends BaseNode {
type: 'BlockStatement';
path: CallableExpression;
params: Expression[];
hash: Hash;
program: Block;
inverse?: Nullable<Block>;
openStrip: StripFlags;
inverseStrip: StripFlags;
closeStrip: StripFlags;
// Printer extension
chained?: boolean;
}
export interface ElementModifierStatement extends BaseNode {
type: 'ElementModifierStatement';
path: CallableExpression;
params: Expression[];
hash: Hash;
}
export interface CommentStatement extends BaseNode {
type: 'CommentStatement';
value: string;
}
export interface MustacheCommentStatement extends BaseNode {
type: 'MustacheCommentStatement';
value: string;
}
export interface ElementNode extends BaseNode {
type: 'ElementNode';
path: PathExpression;
selfClosing: boolean;
attributes: AttrNode[];
params: VarHead[];
modifiers: ElementModifierStatement[];
comments: MustacheCommentStatement[];
children: Statement[];
/**
* span for the open tag
*/
openTag: src.SourceSpan;
/**
* span for the close tag, null for void or self-closing tags
*/
closeTag: Nullable<src.SourceSpan>;
/**
* string accessor for path.original
*/
tag: string;
/**
* string accessor for params.name
*/
blockParams: string[];
}
export type StatementName =
| 'MustacheStatement'
| 'CommentStatement'
| 'BlockStatement'
| 'MustacheCommentStatement'
| 'TextNode'
| 'ElementNode';
export interface AttrNode extends BaseNode {
type: 'AttrNode';
name: string;
value: AttrValue;
}
export type AttrValue = TextNode | MustacheStatement | ConcatStatement;
export type AttrPart = TextNode | MustacheStatement;
export interface TextNode extends BaseNode {
type: 'TextNode';
chars: string;
}
export interface ConcatStatement extends BaseNode {
type: 'ConcatStatement';
parts: PresentArray<TextNode | MustacheStatement>;
}
export type ExpressionName = 'SubExpression' | 'PathExpression' | LiteralName;
export interface SubExpression extends BaseNode {
type: 'SubExpression';
path: CallableExpression;
params: Expression[];
hash: Hash;
}
export interface ThisHead {
type: 'ThisHead';
original: 'this';
loc: src.SourceSpan;
}
export interface AtHead {
type: 'AtHead';
name: string;
loc: src.SourceSpan;
/**
* alias for name
*/
original: string;
}
export interface VarHead {
type: 'VarHead';
name: string;
loc: src.SourceSpan;
/**
* alias for name
*/
original: string;
}
export type PathHead = ThisHead | AtHead | VarHead;
export interface MinimalPathExpression extends BaseNode {
type: 'PathExpression';
head: PathHead;
tail: string[];
}
export interface PathExpression extends MinimalPathExpression {
type: 'PathExpression';
original: string;
head: PathHead;
tail: string[];
/**
* @deprecated use `head` and `tail` instead
*/
parts: readonly string[];
/**
* @deprecated use `head.type` instead
*/
readonly this: boolean;
/**
* @deprecated use `head.type' instead
*/
readonly data: boolean;
}
export type LiteralName =
| 'StringLiteral'
| 'BooleanLiteral'
| 'NumberLiteral'
| 'UndefinedLiteral'
| 'NullLiteral';
export interface StringLiteral extends BaseNode {
type: 'StringLiteral';
value: string;
/**
* @deprecated use value instead
*/
original: string;
}
export interface BooleanLiteral extends BaseNode {
type: 'BooleanLiteral';
value: boolean;
/**
* @deprecated use value instead
*/
original: boolean;
}
export interface NumberLiteral extends BaseNode {
type: 'NumberLiteral';
value: number;
/**
* @deprecated use value instead
*/
original: number;
}
export interface UndefinedLiteral extends BaseNode {
type: 'UndefinedLiteral';
value: undefined;
/**
* @deprecated use value instead
*/
original: undefined;
}
export interface NullLiteral extends BaseNode {
type: 'NullLiteral';
value: null;
/**
* @deprecated use value instead
*/
original: null;
}
export interface Hash extends BaseNode {
type: 'Hash';
pairs: HashPair[];
}
export interface HashPair extends BaseNode {
type: 'HashPair';
key: string;
value: Expression;
}
export interface StripFlags {
open: boolean;
close: boolean;
}
export type Nodes = {
Template: Template;
Block: Block;
MustacheStatement: MustacheStatement;
BlockStatement: BlockStatement;
ElementModifierStatement: ElementModifierStatement;
CommentStatement: CommentStatement;
MustacheCommentStatement: MustacheCommentStatement;
ElementNode: ElementNode;
AttrNode: AttrNode;
TextNode: TextNode;
ConcatStatement: ConcatStatement;
SubExpression: SubExpression;
PathExpression: PathExpression;
StringLiteral: StringLiteral;
BooleanLiteral: BooleanLiteral;
NumberLiteral: NumberLiteral;
NullLiteral: NullLiteral;
UndefinedLiteral: UndefinedLiteral;
Hash: Hash;
HashPair: HashPair;
};
export type NodeType = keyof Nodes;
export type Node = Nodes[NodeType];
// These "sub-node" cannot appear standalone, they are only used inside another
// "real" AST node to provide richer information. The distinction mostly exists
// for backwards compatibility reason. These nodes are not traversed and do not
// have visitor keys for them, so it won't break existing AST consumers (e.g.
// those that implemented an `All` visitor may not be expecting these new types
// of nodes).
//
// Conceptually, the idea of "sub-node" does make sense, and you can say source
// locations are another kind of these things. However, in these cases, they
// actually fully implement the `BaseNode` interface, and only not extending
// `BaseNode` because the `type` field is not `keyof Nodes` (which is circular
// reasoning). If these are not "real" nodes because they can only appear in
// very limited context, then the same reasoning probably applies for, say,
// HashPair.
//
// If we do eventually make some kind of breaking change here, perhaps with
// some kind of opt-in, then we can consider upgrading these into "real" nodes,
// but for now, this is where they go, and it isn't a huge problem in practice
// because there are little utility in traversing these kind of nodes anyway.
export type SubNodes = {
ThisHead: ThisHead;
AtHead: AtHead;
VarHead: VarHead;
};
export type SubNodeType = keyof SubNodes;
export type SubNode = SubNodes[SubNodeType];
export type Statement = Nodes[StatementName];
export type Statements = Pick<Nodes, StatementName>;
export type Literal = Nodes[LiteralName];
export type Expression = Nodes[ExpressionName];
export type Expressions = Pick<Nodes, ExpressionName>;
export type TopLevelStatement = Statement | Nodes['Block'];
export type ParentNode = Template | Block | ElementNode;