forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.ts
More file actions
952 lines (878 loc) · 26.2 KB
/
Copy pathast.ts
File metadata and controls
952 lines (878 loc) · 26.2 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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
/**
* @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 {SecurityContext} from '../core';
import {ParseError, ParseSourceSpan} from '../parse_util';
export class ParseSpan {
constructor(
public start: number,
public end: number,
) {}
toAbsolute(absoluteOffset: number): AbsoluteSourceSpan {
return new AbsoluteSourceSpan(absoluteOffset + this.start, absoluteOffset + this.end);
}
}
export abstract class AST {
constructor(
public span: ParseSpan,
/**
* Absolute location of the expression AST in a source code file.
*/
public sourceSpan: AbsoluteSourceSpan,
) {}
abstract visit(visitor: AstVisitor, context?: any): any;
toString(): string {
return 'AST';
}
}
export abstract class ASTWithName extends AST {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
public nameSpan: AbsoluteSourceSpan,
) {
super(span, sourceSpan);
}
}
export class EmptyExpr extends AST {
override visit(visitor: AstVisitor, context: any = null) {
return visitor.visitEmptyExpr?.(this, context);
}
}
export class ImplicitReceiver extends AST {
override visit(visitor: AstVisitor, context: any = null): any {
return visitor.visitImplicitReceiver(this, context);
}
}
export class ThisReceiver extends AST {
override visit(visitor: AstVisitor, context: any = null): any {
return visitor.visitThisReceiver?.(this, context);
}
}
/**
* Multiple expressions separated by a semicolon.
*/
export class Chain extends AST {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
public expressions: AST[],
) {
super(span, sourceSpan);
}
override visit(visitor: AstVisitor, context: any = null): any {
return visitor.visitChain(this, context);
}
}
export class Conditional extends AST {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
public condition: AST,
public trueExp: AST,
public falseExp: AST,
) {
super(span, sourceSpan);
}
override visit(visitor: AstVisitor, context: any = null): any {
return visitor.visitConditional(this, context);
}
}
export class PropertyRead extends ASTWithName {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
nameSpan: AbsoluteSourceSpan,
public receiver: AST,
public name: string,
) {
super(span, sourceSpan, nameSpan);
}
override visit(visitor: AstVisitor, context: any = null): any {
return visitor.visitPropertyRead(this, context);
}
}
export class SafePropertyRead extends ASTWithName {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
nameSpan: AbsoluteSourceSpan,
public receiver: AST,
public name: string,
) {
super(span, sourceSpan, nameSpan);
}
override visit(visitor: AstVisitor, context: any = null): any {
return visitor.visitSafePropertyRead(this, context);
}
}
export class KeyedRead extends AST {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
public receiver: AST,
public key: AST,
) {
super(span, sourceSpan);
}
override visit(visitor: AstVisitor, context: any = null): any {
return visitor.visitKeyedRead(this, context);
}
}
export class SafeKeyedRead extends AST {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
public receiver: AST,
public key: AST,
) {
super(span, sourceSpan);
}
override visit(visitor: AstVisitor, context: any = null): any {
return visitor.visitSafeKeyedRead(this, context);
}
}
/** Possible types for a pipe. */
export enum BindingPipeType {
/**
* Pipe is being referenced by its name, for example:
* `@Pipe({name: 'foo'}) class FooPipe` and `{{123 | foo}}`.
*/
ReferencedByName,
/**
* Pipe is being referenced by its class name, for example:
* `@Pipe() class FooPipe` and `{{123 | FooPipe}}`.
*/
ReferencedDirectly,
}
export class BindingPipe extends ASTWithName {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
public exp: AST,
public name: string,
public args: AST[],
readonly type: BindingPipeType,
nameSpan: AbsoluteSourceSpan,
) {
super(span, sourceSpan, nameSpan);
}
override visit(visitor: AstVisitor, context: any = null): any {
return visitor.visitPipe(this, context);
}
}
export class LiteralPrimitive extends AST {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
public value: string | number | boolean | null | undefined,
) {
super(span, sourceSpan);
}
override visit(visitor: AstVisitor, context: any = null): any {
return visitor.visitLiteralPrimitive(this, context);
}
}
export class LiteralArray extends AST {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
public expressions: AST[],
) {
super(span, sourceSpan);
}
override visit(visitor: AstVisitor, context: any = null): any {
return visitor.visitLiteralArray(this, context);
}
}
export class SpreadElement extends AST {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
readonly expression: AST,
) {
super(span, sourceSpan);
}
override visit(visitor: AstVisitor, context: any = null): any {
return visitor.visitSpreadElement(this, context);
}
}
export interface LiteralMapPropertyKey {
kind: 'property';
key: string;
quoted: boolean;
span: ParseSpan;
sourceSpan: AbsoluteSourceSpan;
isShorthandInitialized?: boolean;
}
export interface LiteralMapSpreadKey {
kind: 'spread';
span: ParseSpan;
sourceSpan: AbsoluteSourceSpan;
}
export type LiteralMapKey = LiteralMapPropertyKey | LiteralMapSpreadKey;
export class LiteralMap extends AST {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
public keys: LiteralMapKey[],
public values: AST[],
) {
super(span, sourceSpan);
}
override visit(visitor: AstVisitor, context: any = null): any {
return visitor.visitLiteralMap(this, context);
}
}
export class Interpolation extends AST {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
public strings: string[],
public expressions: AST[],
) {
super(span, sourceSpan);
}
override visit(visitor: AstVisitor, context: any = null): any {
return visitor.visitInterpolation(this, context);
}
}
export type AssignmentOperation =
| '='
| '+='
| '-='
| '*='
| '/='
| '%='
| '**='
| '&&='
| '||='
| '??=';
type BinaryOperation =
| AssignmentOperation
// Logical
| '&&'
| '||'
| '??'
// Equality
| '=='
| '!='
| '==='
| '!=='
// Relational
| '<'
| '>'
| '<='
| '>='
| 'in'
| 'instanceof'
// Additive
| '+'
| '-'
// Multiplicative
| '*'
| '%'
| '/'
// Exponentiation
| '**';
export class Binary extends AST {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
public operation: BinaryOperation,
public left: AST,
public right: AST,
) {
super(span, sourceSpan);
}
override visit(visitor: AstVisitor, context: any = null): any {
return visitor.visitBinary(this, context);
}
static isAssignmentOperation(op: string): op is AssignmentOperation {
return (
op === '=' ||
op === '+=' ||
op === '-=' ||
op === '*=' ||
op === '/=' ||
op === '%=' ||
op === '**=' ||
op === '&&=' ||
op === '||=' ||
op === '??='
);
}
}
/**
* For backwards compatibility reasons, `Unary` inherits from `Binary` and mimics the binary AST
* node that was originally used. This inheritance relation can be deleted in some future major,
* after consumers have been given a chance to fully support Unary.
*/
export class Unary extends Binary {
// Redeclare the properties that are inherited from `Binary` as `never`, as consumers should not
// depend on these fields when operating on `Unary`.
override left: never = null as never;
override right: never = null as never;
override operation: never = null as never;
/**
* Creates a unary minus expression "-x", represented as `Binary` using "0 - x".
*/
static createMinus(span: ParseSpan, sourceSpan: AbsoluteSourceSpan, expr: AST): Unary {
return new Unary(
span,
sourceSpan,
'-',
expr,
'-',
new LiteralPrimitive(span, sourceSpan, 0),
expr,
);
}
/**
* Creates a unary plus expression "+x", represented as `Binary` using "x - 0".
*/
static createPlus(span: ParseSpan, sourceSpan: AbsoluteSourceSpan, expr: AST): Unary {
return new Unary(
span,
sourceSpan,
'+',
expr,
'-',
expr,
new LiteralPrimitive(span, sourceSpan, 0),
);
}
/**
* During the deprecation period this constructor is private, to avoid consumers from creating
* a `Unary` with the fallback properties for `Binary`.
*/
private constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
public operator: '+' | '-',
public expr: AST,
binaryOp: BinaryOperation,
binaryLeft: AST,
binaryRight: AST,
) {
super(span, sourceSpan, binaryOp, binaryLeft, binaryRight);
}
override visit(visitor: AstVisitor, context: any = null): any {
if (visitor.visitUnary !== undefined) {
return visitor.visitUnary(this, context);
}
return visitor.visitBinary(this, context);
}
}
export class PrefixNot extends AST {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
public expression: AST,
) {
super(span, sourceSpan);
}
override visit(visitor: AstVisitor, context: any = null): any {
return visitor.visitPrefixNot(this, context);
}
}
export class TypeofExpression extends AST {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
public expression: AST,
) {
super(span, sourceSpan);
}
override visit(visitor: AstVisitor, context: any = null): any {
return visitor.visitTypeofExpression(this, context);
}
}
export class VoidExpression extends AST {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
public expression: AST,
) {
super(span, sourceSpan);
}
override visit(visitor: AstVisitor, context: any = null): any {
return visitor.visitVoidExpression(this, context);
}
}
export class NonNullAssert extends AST {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
public expression: AST,
) {
super(span, sourceSpan);
}
override visit(visitor: AstVisitor, context: any = null): any {
return visitor.visitNonNullAssert(this, context);
}
}
export class Call extends AST {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
public receiver: AST,
public args: AST[],
public argumentSpan: AbsoluteSourceSpan,
) {
super(span, sourceSpan);
}
override visit(visitor: AstVisitor, context: any = null): any {
return visitor.visitCall(this, context);
}
}
export class SafeCall extends AST {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
public receiver: AST,
public args: AST[],
public argumentSpan: AbsoluteSourceSpan,
) {
super(span, sourceSpan);
}
override visit(visitor: AstVisitor, context: any = null): any {
return visitor.visitSafeCall(this, context);
}
}
export class TaggedTemplateLiteral extends AST {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
public tag: AST,
public template: TemplateLiteral,
) {
super(span, sourceSpan);
}
override visit(visitor: AstVisitor, context?: any) {
return visitor.visitTaggedTemplateLiteral(this, context);
}
}
export class TemplateLiteral extends AST {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
public elements: TemplateLiteralElement[],
public expressions: AST[],
) {
super(span, sourceSpan);
}
override visit(visitor: AstVisitor, context?: any) {
return visitor.visitTemplateLiteral(this, context);
}
}
export class TemplateLiteralElement extends AST {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
public text: string,
) {
super(span, sourceSpan);
}
override visit(visitor: AstVisitor, context?: any) {
return visitor.visitTemplateLiteralElement(this, context);
}
}
export class ParenthesizedExpression extends AST {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
public expression: AST,
) {
super(span, sourceSpan);
}
override visit(visitor: AstVisitor, context?: any) {
return visitor.visitParenthesizedExpression(this, context);
}
}
export class ArrowFunctionIdentifierParameter {
constructor(
public name: string,
public span: ParseSpan,
public sourceSpan: AbsoluteSourceSpan,
) {}
}
export type ArrowFunctionParameter = ArrowFunctionIdentifierParameter; // TODO(crisbeto): also rest parameters?
export class ArrowFunction extends AST {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
public parameters: ArrowFunctionParameter[],
public body: AST,
) {
super(span, sourceSpan);
}
override visit(visitor: AstVisitor, context?: any) {
return visitor.visitArrowFunction(this, context);
}
}
export class RegularExpressionLiteral extends AST {
constructor(
span: ParseSpan,
sourceSpan: AbsoluteSourceSpan,
readonly body: string,
readonly flags: string | null,
) {
super(span, sourceSpan);
}
override visit(visitor: AstVisitor, context?: any) {
return visitor.visitRegularExpressionLiteral(this, context);
}
}
/**
* Records the absolute position of a text span in a source file, where `start` and `end` are the
* starting and ending byte offsets, respectively, of the text span in a source file.
*/
export class AbsoluteSourceSpan {
constructor(
public readonly start: number,
public readonly end: number,
) {}
}
export class ASTWithSource<T extends AST = AST> extends AST {
constructor(
public ast: T,
public source: string | null,
public location: string,
absoluteOffset: number,
public errors: ParseError[],
) {
super(
new ParseSpan(0, source === null ? 0 : source.length),
new AbsoluteSourceSpan(
absoluteOffset,
source === null ? absoluteOffset : absoluteOffset + source.length,
),
);
}
override visit(visitor: AstVisitor, context: any = null): any {
if (visitor.visitASTWithSource) {
return visitor.visitASTWithSource(this, context);
}
return this.ast.visit(visitor, context);
}
override toString(): string {
return `${this.source} in ${this.location}`;
}
}
/**
* TemplateBinding refers to a particular key-value pair in a microsyntax
* expression. A few examples are:
*
* |---------------------|--------------|---------|--------------|
* | expression | key | value | binding type |
* |---------------------|--------------|---------|--------------|
* | 1. let item | item | null | variable |
* | 2. of items | ngForOf | items | expression |
* | 3. let x = y | x | y | variable |
* | 4. index as i | i | index | variable |
* | 5. trackBy: func | ngForTrackBy | func | expression |
* | 6. *ngIf="cond" | ngIf | cond | expression |
* |---------------------|--------------|---------|--------------|
*
* (6) is a notable exception because it is a binding from the template key in
* the LHS of a HTML attribute to the expression in the RHS. All other bindings
* in the example above are derived solely from the RHS.
*/
export type TemplateBinding = VariableBinding | ExpressionBinding;
export class VariableBinding {
/**
* @param sourceSpan entire span of the binding.
* @param key name of the LHS along with its span.
* @param value optional value for the RHS along with its span.
*/
constructor(
public readonly sourceSpan: AbsoluteSourceSpan,
public readonly key: TemplateBindingIdentifier,
public readonly value: TemplateBindingIdentifier | null,
) {}
}
export class ExpressionBinding {
/**
* @param sourceSpan entire span of the binding.
* @param key binding name, like ngForOf, ngForTrackBy, ngIf, along with its
* span. Note that the length of the span may not be the same as
* `key.source.length`. For example,
* 1. key.source = ngFor, key.span is for "ngFor"
* 2. key.source = ngForOf, key.span is for "of"
* 3. key.source = ngForTrackBy, key.span is for "trackBy"
* @param value optional expression for the RHS.
*/
constructor(
public readonly sourceSpan: AbsoluteSourceSpan,
public readonly key: TemplateBindingIdentifier,
public readonly value: ASTWithSource | null,
) {}
}
export interface TemplateBindingIdentifier {
source: string;
span: AbsoluteSourceSpan;
}
export interface AstVisitor {
/**
* The `visitUnary` method is declared as optional for backwards compatibility. In an upcoming
* major release, this method will be made required.
*/
visitUnary?(ast: Unary, context: any): any;
visitBinary(ast: Binary, context: any): any;
visitChain(ast: Chain, context: any): any;
visitConditional(ast: Conditional, context: any): any;
/**
* The `visitThisReceiver` method is declared as optional for backwards compatibility.
* In an upcoming major release, this method will be made required.
*/
visitThisReceiver?(ast: ThisReceiver, context: any): any;
visitImplicitReceiver(ast: ImplicitReceiver, context: any): any;
visitInterpolation(ast: Interpolation, context: any): any;
visitKeyedRead(ast: KeyedRead, context: any): any;
visitLiteralArray(ast: LiteralArray, context: any): any;
visitLiteralMap(ast: LiteralMap, context: any): any;
visitLiteralPrimitive(ast: LiteralPrimitive, context: any): any;
visitPipe(ast: BindingPipe, context: any): any;
visitPrefixNot(ast: PrefixNot, context: any): any;
visitTypeofExpression(ast: TypeofExpression, context: any): any;
visitVoidExpression(ast: TypeofExpression, context: any): any;
visitNonNullAssert(ast: NonNullAssert, context: any): any;
visitPropertyRead(ast: PropertyRead, context: any): any;
visitSafePropertyRead(ast: SafePropertyRead, context: any): any;
visitSafeKeyedRead(ast: SafeKeyedRead, context: any): any;
visitCall(ast: Call, context: any): any;
visitSafeCall(ast: SafeCall, context: any): any;
visitTemplateLiteral(ast: TemplateLiteral, context: any): any;
visitTemplateLiteralElement(ast: TemplateLiteralElement, context: any): any;
visitTaggedTemplateLiteral(ast: TaggedTemplateLiteral, context: any): any;
visitParenthesizedExpression(ast: ParenthesizedExpression, context: any): any;
visitArrowFunction(ast: ArrowFunction, context: any): any;
visitRegularExpressionLiteral(ast: RegularExpressionLiteral, context: any): any;
visitSpreadElement(ast: SpreadElement, context: any): any;
visitASTWithSource?(ast: ASTWithSource, context: any): any;
visitEmptyExpr?(ast: EmptyExpr, context: any): any;
/**
* This function is optionally defined to allow classes that implement this
* interface to selectively decide if the specified `ast` should be visited.
* @param ast node to visit
* @param context context that gets passed to the node and all its children
*/
visit?(ast: AST, context?: any): any;
}
export class RecursiveAstVisitor implements AstVisitor {
visit(ast: AST, context?: any): any {
// The default implementation just visits every node.
// Classes that extend RecursiveAstVisitor should override this function
// to selectively visit the specified node.
ast.visit(this, context);
}
visitUnary(ast: Unary, context: any): any {
this.visit(ast.expr, context);
}
visitBinary(ast: Binary, context: any): any {
this.visit(ast.left, context);
this.visit(ast.right, context);
}
visitChain(ast: Chain, context: any): any {
this.visitAll(ast.expressions, context);
}
visitConditional(ast: Conditional, context: any): any {
this.visit(ast.condition, context);
this.visit(ast.trueExp, context);
this.visit(ast.falseExp, context);
}
visitPipe(ast: BindingPipe, context: any): any {
this.visit(ast.exp, context);
this.visitAll(ast.args, context);
}
visitImplicitReceiver(ast: ThisReceiver, context: any): any {}
visitThisReceiver(ast: ThisReceiver, context: any): any {}
visitInterpolation(ast: Interpolation, context: any): any {
this.visitAll(ast.expressions, context);
}
visitKeyedRead(ast: KeyedRead, context: any): any {
this.visit(ast.receiver, context);
this.visit(ast.key, context);
}
visitLiteralArray(ast: LiteralArray, context: any): any {
this.visitAll(ast.expressions, context);
}
visitLiteralMap(ast: LiteralMap, context: any): any {
this.visitAll(ast.values, context);
}
visitLiteralPrimitive(ast: LiteralPrimitive, context: any): any {}
visitPrefixNot(ast: PrefixNot, context: any): any {
this.visit(ast.expression, context);
}
visitTypeofExpression(ast: TypeofExpression, context: any) {
this.visit(ast.expression, context);
}
visitVoidExpression(ast: VoidExpression, context: any) {
this.visit(ast.expression, context);
}
visitNonNullAssert(ast: NonNullAssert, context: any): any {
this.visit(ast.expression, context);
}
visitPropertyRead(ast: PropertyRead, context: any): any {
this.visit(ast.receiver, context);
}
visitSafePropertyRead(ast: SafePropertyRead, context: any): any {
this.visit(ast.receiver, context);
}
visitSafeKeyedRead(ast: SafeKeyedRead, context: any): any {
this.visit(ast.receiver, context);
this.visit(ast.key, context);
}
visitCall(ast: Call, context: any): any {
this.visit(ast.receiver, context);
this.visitAll(ast.args, context);
}
visitSafeCall(ast: SafeCall, context: any): any {
this.visit(ast.receiver, context);
this.visitAll(ast.args, context);
}
visitTemplateLiteral(ast: TemplateLiteral, context: any) {
// Iterate in the declaration order. Note that there will
// always be one expression less than the number of elements.
for (let i = 0; i < ast.elements.length; i++) {
this.visit(ast.elements[i], context);
const expression = i < ast.expressions.length ? ast.expressions[i] : null;
if (expression !== null) {
this.visit(expression, context);
}
}
}
visitTemplateLiteralElement(ast: TemplateLiteralElement, context: any) {}
visitTaggedTemplateLiteral(ast: TaggedTemplateLiteral, context: any) {
this.visit(ast.tag, context);
this.visit(ast.template, context);
}
visitParenthesizedExpression(ast: ParenthesizedExpression, context: any) {
this.visit(ast.expression, context);
}
visitArrowFunction(ast: ArrowFunction, context: any): any {
this.visit(ast.body, context);
}
visitRegularExpressionLiteral(ast: RegularExpressionLiteral, context: any) {}
visitSpreadElement(ast: SpreadElement, context: any) {
this.visit(ast.expression, context);
}
visitEmptyExpr(ast: EmptyExpr, context: any): any {}
// This is not part of the AstVisitor interface, just a helper method
visitAll(asts: AST[], context: any): any {
for (const ast of asts) {
this.visit(ast, context);
}
}
}
// Bindings
export class ParsedProperty {
public readonly isLiteral: boolean;
public readonly isLegacyAnimation: boolean;
public readonly isAnimation: boolean;
constructor(
public name: string,
public expression: ASTWithSource,
public type: ParsedPropertyType,
public sourceSpan: ParseSourceSpan,
readonly keySpan: ParseSourceSpan,
public valueSpan: ParseSourceSpan | undefined,
) {
this.isLiteral = this.type === ParsedPropertyType.LITERAL_ATTR;
this.isLegacyAnimation = this.type === ParsedPropertyType.LEGACY_ANIMATION;
this.isAnimation = this.type === ParsedPropertyType.ANIMATION;
}
}
export enum ParsedPropertyType {
DEFAULT,
LITERAL_ATTR,
LEGACY_ANIMATION,
TWO_WAY,
ANIMATION,
}
export enum ParsedEventType {
// DOM or Directive event
Regular,
// Legacy animation specific event
LegacyAnimation,
// Event side of a two-way binding (e.g. `[(property)]="expression"`).
TwoWay,
// Animation specific event
Animation,
}
export class ParsedEvent {
// Regular events have a target
// Legacy Animation events have a phase
constructor(
name: string,
targetOrPhase: string | null,
type: ParsedEventType.TwoWay,
handler: ASTWithSource<NonNullAssert | PropertyRead | KeyedRead>,
sourceSpan: ParseSourceSpan,
handlerSpan: ParseSourceSpan,
keySpan: ParseSourceSpan,
);
constructor(
name: string,
targetOrPhase: string | null,
type: ParsedEventType,
handler: ASTWithSource,
sourceSpan: ParseSourceSpan,
handlerSpan: ParseSourceSpan,
keySpan: ParseSourceSpan,
);
constructor(
public name: string,
public targetOrPhase: string | null,
public type: ParsedEventType,
public handler: ASTWithSource,
public sourceSpan: ParseSourceSpan,
public handlerSpan: ParseSourceSpan,
readonly keySpan: ParseSourceSpan,
) {}
}
/**
* ParsedVariable represents a variable declaration in a microsyntax expression.
*/
export class ParsedVariable {
constructor(
public readonly name: string,
public readonly value: string,
public readonly sourceSpan: ParseSourceSpan,
public readonly keySpan: ParseSourceSpan,
public readonly valueSpan?: ParseSourceSpan,
) {}
}
export enum BindingType {
// A regular binding to a property (e.g. `[property]="expression"`).
Property,
// A binding to an element attribute (e.g. `[attr.name]="expression"`).
Attribute,
// A binding to a CSS class (e.g. `[class.name]="condition"`).
Class,
// A binding to a style rule (e.g. `[style.rule]="expression"`).
Style,
// A binding to a legacy animation reference (e.g. `[animate.key]="expression"`).
LegacyAnimation,
// Property side of a two-way binding (e.g. `[(property)]="expression"`).
TwoWay,
// A binding to an animation CSS class or function (e.g. `[animate.leave]="expression"`).
Animation,
}
export class BoundElementProperty {
constructor(
public name: string,
public type: BindingType,
public securityContext: SecurityContext,
public value: ASTWithSource,
public unit: string | null,
public sourceSpan: ParseSourceSpan,
readonly keySpan: ParseSourceSpan | undefined,
public valueSpan: ParseSourceSpan | undefined,
) {}
}