/
r3_ast_spans_spec.ts
333 lines (288 loc) Β· 10.2 KB
/
r3_ast_spans_spec.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
/**
* @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 {ParseSourceSpan} from '../../src/parse_util';
import * as t from '../../src/render3/r3_ast';
import {parseR3 as parse} from './view/util';
class R3AstSourceSpans implements t.Visitor<void> {
result: any[] = [];
visitElement(element: t.Element) {
this.result.push([
'Element', humanizeSpan(element.sourceSpan), humanizeSpan(element.startSourceSpan),
humanizeSpan(element.endSourceSpan)
]);
this.visitAll([
element.attributes,
element.inputs,
element.outputs,
element.references,
element.children,
]);
}
visitTemplate(template: t.Template) {
this.result.push([
'Template', humanizeSpan(template.sourceSpan), humanizeSpan(template.startSourceSpan),
humanizeSpan(template.endSourceSpan)
]);
this.visitAll([
template.attributes,
template.inputs,
template.outputs,
template.templateAttrs,
template.references,
template.variables,
template.children,
]);
}
visitContent(content: t.Content) {
this.result.push(['Content', humanizeSpan(content.sourceSpan)]);
t.visitAll(this, content.attributes);
}
visitVariable(variable: t.Variable) {
this.result.push(
['Variable', humanizeSpan(variable.sourceSpan), humanizeSpan(variable.valueSpan)]);
}
visitReference(reference: t.Reference) {
this.result.push(
['Reference', humanizeSpan(reference.sourceSpan), humanizeSpan(reference.valueSpan)]);
}
visitTextAttribute(attribute: t.TextAttribute) {
this.result.push(
['TextAttribute', humanizeSpan(attribute.sourceSpan), humanizeSpan(attribute.valueSpan)]);
}
visitBoundAttribute(attribute: t.BoundAttribute) {
this.result.push(
['BoundAttribute', humanizeSpan(attribute.sourceSpan), humanizeSpan(attribute.valueSpan)]);
}
visitBoundEvent(event: t.BoundEvent) {
this.result.push(
['BoundEvent', humanizeSpan(event.sourceSpan), humanizeSpan(event.handlerSpan)]);
}
visitText(text: t.Text) {
this.result.push(['Text', humanizeSpan(text.sourceSpan)]);
}
visitBoundText(text: t.BoundText) {
this.result.push(['BoundText', humanizeSpan(text.sourceSpan)]);
}
visitIcu(icu: t.Icu) {
return null;
}
private visitAll(nodes: t.Node[][]) {
nodes.forEach(node => t.visitAll(this, node));
}
}
function humanizeSpan(span: ParseSourceSpan|null|undefined): string {
if (span === null || span === undefined) {
return `<empty>`;
}
return `${span.start.offset}:${span.end.offset}`;
}
function expectFromHtml(html: string) {
const res = parse(html);
return expectFromR3Nodes(res.nodes);
}
function expectFromR3Nodes(nodes: t.Node[]) {
const humanizer = new R3AstSourceSpans();
t.visitAll(humanizer, nodes);
return expect(humanizer.result);
}
describe('R3 AST source spans', () => {
describe('nodes without binding', () => {
it('is correct for text nodes', () => {
expectFromHtml('a').toEqual([
['Text', '0:1'],
]);
});
it('is correct for elements with attributes', () => {
expectFromHtml('<div a="b"></div>').toEqual([
['Element', '0:17', '0:11', '11:17'],
['TextAttribute', '5:10', '8:9'],
]);
});
it('is correct for elements with attributes without value', () => {
expectFromHtml('<div a></div>').toEqual([
['Element', '0:13', '0:7', '7:13'],
['TextAttribute', '5:6', '<empty>'],
]);
});
});
describe('bound text nodes', () => {
it('is correct for bound text nodes', () => {
expectFromHtml('{{a}}').toEqual([
['BoundText', '0:5'],
]);
});
});
describe('bound attributes', () => {
it('is correct for bound properties', () => {
expectFromHtml('<div [someProp]="v"></div>').toEqual([
['Element', '0:26', '0:20', '20:26'],
['BoundAttribute', '5:19', '17:18'],
]);
});
it('is correct for bound properties without value', () => {
expectFromHtml('<div [someProp]></div>').toEqual([
['Element', '0:22', '0:16', '16:22'],
['BoundAttribute', '5:15', '<empty>'],
]);
});
it('is correct for bound properties via bind- ', () => {
expectFromHtml('<div bind-prop="v"></div>').toEqual([
['Element', '0:25', '0:19', '19:25'],
['BoundAttribute', '5:18', '16:17'],
]);
});
it('is correct for bound properties via {{...}}', () => {
expectFromHtml('<div prop="{{v}}"></div>').toEqual([
['Element', '0:24', '0:18', '18:24'],
['BoundAttribute', '5:17', '11:16'],
]);
});
});
describe('templates', () => {
it('is correct for * directives', () => {
expectFromHtml('<div *ngIf></div>').toEqual([
['Template', '0:17', '0:11', '11:17'],
['TextAttribute', '5:10', '<empty>'],
['Element', '0:17', '0:11', '11:17'],
]);
});
it('is correct for <ng-template>', () => {
expectFromHtml('<ng-template></ng-template>').toEqual([
['Template', '0:27', '0:13', '13:27'],
]);
});
it('is correct for reference via #...', () => {
expectFromHtml('<ng-template #a></ng-template>').toEqual([
['Template', '0:30', '0:16', '16:30'],
['Reference', '13:15', '<empty>'],
]);
});
it('is correct for reference with name', () => {
expectFromHtml('<ng-template #a="b"></ng-template>').toEqual([
['Template', '0:34', '0:20', '20:34'],
['Reference', '13:19', '17:18'],
]);
});
it('is correct for reference via ref-...', () => {
expectFromHtml('<ng-template ref-a></ng-template>').toEqual([
['Template', '0:33', '0:19', '19:33'],
['Reference', '13:18', '<empty>'],
]);
});
it('is correct for variables via let-...', () => {
expectFromHtml('<ng-template let-a="b"></ng-template>').toEqual([
['Template', '0:37', '0:23', '23:37'],
['Variable', '13:22', '20:21'],
]);
});
it('is correct for attributes', () => {
expectFromHtml('<ng-template k1="v1"></ng-template>').toEqual([
['Template', '0:35', '0:21', '21:35'],
['TextAttribute', '13:20', '17:19'],
]);
});
it('is correct for bound attributes', () => {
expectFromHtml('<ng-template [k1]="v1"></ng-template>').toEqual([
['Template', '0:37', '0:23', '23:37'],
['BoundAttribute', '13:22', '19:21'],
]);
});
});
// TODO(joost): improve spans of nodes extracted from macrosyntax
describe('inline templates', () => {
it('is correct for attribute and bound attributes', () => {
// Desugared form is
// <ng-template ngFor [ngForOf]="items" let-item>
// <div></div>
// </ng-template>
expectFromHtml('<div *ngFor="let item of items"></div>').toEqual([
['Template', '0:38', '0:32', '32:38'],
['TextAttribute', '5:31', '<empty>'],
['BoundAttribute', '5:31', '25:30'], // *ngFor="let item of items" -> items
['Variable', '13:22', '<empty>'], // let item
['Element', '0:38', '0:32', '32:38'],
]);
// Note that this test exercises an *incorrect* usage of the ngFor
// directive. There is a missing 'let' in the beginning of the expression
// which causes the template to be desugared into
// <ng-template [ngFor]="item" [ngForOf]="items">
// <div></div>
// </ng-template>
expectFromHtml('<div *ngFor="item of items"></div>').toEqual([
['Template', '0:34', '0:28', '28:34'],
['BoundAttribute', '5:27', '13:17'], // ngFor="item of items" -> item
['BoundAttribute', '5:27', '21:26'], // ngFor="item of items" -> items
['Element', '0:34', '0:28', '28:34'],
]);
});
it('is correct for variables via let ...', () => {
expectFromHtml('<div *ngIf="let a=b"></div>').toEqual([
['Template', '0:27', '0:21', '21:27'],
['TextAttribute', '5:20', '<empty>'],
['Variable', '12:19', '18:19'], // let a=b -> b
['Element', '0:27', '0:21', '21:27'],
]);
});
it('is correct for variables via as ...', () => {
expectFromHtml('<div *ngIf="expr as local"></div>').toEqual([
['Template', '0:33', '0:27', '27:33'],
['BoundAttribute', '5:26', '12:16'], // ngIf="expr as local" -> expr
['Variable', '6:25', '6:10'], // ngIf="expr as local -> ngIf
['Element', '0:33', '0:27', '27:33'],
]);
});
});
describe('events', () => {
it('is correct for event names case sensitive', () => {
expectFromHtml('<div (someEvent)="v"></div>').toEqual([
['Element', '0:27', '0:21', '21:27'],
['BoundEvent', '5:20', '18:19'],
]);
});
it('is correct for bound events via on-', () => {
expectFromHtml('<div on-event="v"></div>').toEqual([
['Element', '0:24', '0:18', '18:24'],
['BoundEvent', '5:17', '15:16'],
]);
});
it('is correct for bound events and properties via [(...)]', () => {
expectFromHtml('<div [(prop)]="v"></div>').toEqual([
['Element', '0:24', '0:18', '18:24'],
['BoundAttribute', '5:17', '15:16'],
['BoundEvent', '5:17', '15:16'],
]);
});
it('is correct for bound events and properties via bindon-', () => {
expectFromHtml('<div bindon-prop="v"></div>').toEqual([
['Element', '0:27', '0:21', '21:27'],
['BoundAttribute', '5:20', '18:19'],
['BoundEvent', '5:20', '18:19'],
]);
});
});
describe('references', () => {
it('is correct for references via #...', () => {
expectFromHtml('<div #a></div>').toEqual([
['Element', '0:14', '0:8', '8:14'],
['Reference', '5:7', '<empty>'],
]);
});
it('is correct for references with name', () => {
expectFromHtml('<div #a="b"></div>').toEqual([
['Element', '0:18', '0:12', '12:18'],
['Reference', '5:11', '9:10'],
]);
});
it('is correct for references via ref-', () => {
expectFromHtml('<div ref-a></div>').toEqual([
['Element', '0:17', '0:11', '11:17'],
['Reference', '5:10', '<empty>'],
]);
});
});
});