-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathr3.rs
More file actions
1570 lines (1454 loc) · 49.2 KB
/
r3.rs
File metadata and controls
1570 lines (1454 loc) · 49.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
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! R3 AST nodes for the Angular template compiler.
//!
//! This module contains the R3 (Render3) AST, which is an intermediate
//! representation between the raw HTML AST and the IR operations.
//!
//! Ported from Angular's `render3/r3_ast.ts`.
use oxc_allocator::{Allocator, Box, HashMap, Vec};
use oxc_span::{Atom, Span};
use crate::ast::expression::{ASTWithSource, AngularExpression, BindingType, ParsedEventType};
use crate::i18n::serializer::format_i18n_placeholder_name;
// ============================================================================
// i18n Metadata
// ============================================================================
/// i18n metadata attached to R3 nodes.
#[derive(Debug)]
pub enum I18nMeta<'a> {
/// Root of an i18n message.
Message(I18nMessage<'a>),
/// Part of a containing message.
Node(I18nNode<'a>),
/// Control flow block placeholder (for @if/@else/@for etc. inside i18n blocks).
/// Ported from Angular's `i18n.BlockPlaceholder`.
BlockPlaceholder(I18nBlockPlaceholder<'a>),
}
/// An i18n message containing translatable content.
#[derive(Debug)]
pub struct I18nMessage<'a> {
/// Unique instance ID for this message.
///
/// This ID is used to track message identity across moves/copies. It ensures that
/// when an i18n attribute is copied (e.g., from element to conditional via
/// `ingestControlFlowInsertionPoint`), both references can be linked to the same
/// i18n context. Without this, Rust's move semantics would cause pointer-based
/// identity checks to fail.
///
/// Assigned during parsing and must be unique per compilation unit.
pub instance_id: u32,
/// Message AST nodes.
pub nodes: Vec<'a, I18nNode<'a>>,
/// The meaning of the message (for disambiguation).
pub meaning: Atom<'a>,
/// Description of the message for translators.
pub description: Atom<'a>,
/// Custom ID specified by the developer.
pub custom_id: Atom<'a>,
/// The computed message ID.
pub id: Atom<'a>,
/// Legacy IDs for backwards compatibility.
pub legacy_ids: Vec<'a, Atom<'a>>,
/// The serialized message string for goog.getMsg and $localize.
/// Contains the message text with placeholder markers like "{$interpolation}".
pub message_string: Atom<'a>,
}
/// i18n AST node.
#[derive(Debug)]
pub enum I18nNode<'a> {
/// Plain text content.
Text(I18nText<'a>),
/// Container for child nodes.
Container(I18nContainer<'a>),
/// ICU expression (plural, select, selectordinal).
Icu(I18nIcu<'a>),
/// HTML tag placeholder.
TagPlaceholder(I18nTagPlaceholder<'a>),
/// Expression placeholder.
Placeholder(I18nPlaceholder<'a>),
/// ICU placeholder (nested ICU reference).
IcuPlaceholder(I18nIcuPlaceholder<'a>),
/// Control flow block placeholder.
BlockPlaceholder(I18nBlockPlaceholder<'a>),
}
/// Plain text content.
#[derive(Debug)]
pub struct I18nText<'a> {
/// The text value.
pub value: Atom<'a>,
/// Source span.
pub source_span: Span,
}
/// Container for child nodes.
#[derive(Debug)]
pub struct I18nContainer<'a> {
/// Child nodes.
pub children: Vec<'a, I18nNode<'a>>,
/// Source span.
pub source_span: Span,
}
/// ICU expression (plural, select, selectordinal).
#[derive(Debug)]
pub struct I18nIcu<'a> {
/// The expression being evaluated.
pub expression: Atom<'a>,
/// ICU type string (plural, select, selectordinal, or custom).
pub icu_type: Atom<'a>,
/// Case branches.
pub cases: HashMap<'a, Atom<'a>, I18nNode<'a>>,
/// Source span.
pub source_span: Span,
/// Expression placeholder name (for message serialization).
pub expression_placeholder: Option<Atom<'a>>,
}
/// HTML tag placeholder.
#[derive(Debug)]
pub struct I18nTagPlaceholder<'a> {
/// Tag name.
pub tag: Atom<'a>,
/// Tag attributes.
pub attrs: HashMap<'a, Atom<'a>, Atom<'a>>,
/// Start tag placeholder name.
pub start_name: Atom<'a>,
/// Close tag placeholder name.
pub close_name: Atom<'a>,
/// Child nodes.
pub children: Vec<'a, I18nNode<'a>>,
/// Whether this is a void element.
pub is_void: bool,
/// Source span (overall).
pub source_span: Span,
/// Start tag source span.
pub start_source_span: Option<Span>,
/// End tag source span.
pub end_source_span: Option<Span>,
}
/// Expression placeholder.
#[derive(Debug)]
pub struct I18nPlaceholder<'a> {
/// The expression value.
pub value: Atom<'a>,
/// Placeholder name.
pub name: Atom<'a>,
/// Source span.
pub source_span: Span,
}
/// ICU placeholder (reference to a nested ICU).
#[derive(Debug)]
pub struct I18nIcuPlaceholder<'a> {
/// The ICU expression.
pub value: Box<'a, I18nIcu<'a>>,
/// Placeholder name.
pub name: Atom<'a>,
/// Source span.
pub source_span: Span,
}
/// Control flow block placeholder.
#[derive(Debug)]
pub struct I18nBlockPlaceholder<'a> {
/// Block name.
pub name: Atom<'a>,
/// Block parameters.
pub parameters: Vec<'a, Atom<'a>>,
/// Start block placeholder name.
pub start_name: Atom<'a>,
/// End block placeholder name.
pub close_name: Atom<'a>,
/// Child nodes.
pub children: Vec<'a, I18nNode<'a>>,
/// Source span (overall).
pub source_span: Span,
/// Start block source span.
pub start_source_span: Option<Span>,
/// End block source span.
pub end_source_span: Option<Span>,
}
// ============================================================================
// i18n Clone implementations
// ============================================================================
impl<'a> I18nMeta<'a> {
/// Creates a deep clone of this i18n metadata using the provided allocator.
pub fn clone_in(&self, allocator: &'a Allocator) -> Self {
match self {
I18nMeta::Message(msg) => I18nMeta::Message(msg.clone_in(allocator)),
I18nMeta::Node(node) => I18nMeta::Node(node.clone_in(allocator)),
I18nMeta::BlockPlaceholder(bp) => I18nMeta::BlockPlaceholder(bp.clone_in(allocator)),
}
}
}
impl<'a> I18nMessage<'a> {
/// Creates a deep clone of this i18n message using the provided allocator.
///
/// Note: This preserves the `instance_id` so that cloned messages maintain
/// their identity for i18n context sharing.
pub fn clone_in(&self, allocator: &'a Allocator) -> Self {
let mut nodes = Vec::new_in(allocator);
for node in self.nodes.iter() {
nodes.push(node.clone_in(allocator));
}
let mut legacy_ids = Vec::new_in(allocator);
for id in self.legacy_ids.iter() {
legacy_ids.push(id.clone());
}
I18nMessage {
instance_id: self.instance_id,
nodes,
meaning: self.meaning.clone(),
description: self.description.clone(),
custom_id: self.custom_id.clone(),
id: self.id.clone(),
legacy_ids,
message_string: self.message_string.clone(),
}
}
}
impl<'a> I18nNode<'a> {
/// Creates a deep clone of this i18n node using the provided allocator.
pub fn clone_in(&self, allocator: &'a Allocator) -> Self {
match self {
I18nNode::Text(t) => I18nNode::Text(t.clone_in()),
I18nNode::Container(c) => I18nNode::Container(c.clone_in(allocator)),
I18nNode::Icu(i) => I18nNode::Icu(i.clone_in(allocator)),
I18nNode::TagPlaceholder(tp) => I18nNode::TagPlaceholder(tp.clone_in(allocator)),
I18nNode::Placeholder(p) => I18nNode::Placeholder(p.clone_in()),
I18nNode::IcuPlaceholder(ip) => I18nNode::IcuPlaceholder(ip.clone_in(allocator)),
I18nNode::BlockPlaceholder(bp) => I18nNode::BlockPlaceholder(bp.clone_in(allocator)),
}
}
}
impl<'a> I18nText<'a> {
/// Creates a clone of this i18n text (no allocator needed - only atoms and spans).
pub fn clone_in(&self) -> Self {
I18nText { value: self.value.clone(), source_span: self.source_span }
}
}
impl<'a> I18nContainer<'a> {
/// Creates a deep clone of this i18n container using the provided allocator.
pub fn clone_in(&self, allocator: &'a Allocator) -> Self {
let mut children = Vec::new_in(allocator);
for child in self.children.iter() {
children.push(child.clone_in(allocator));
}
I18nContainer { children, source_span: self.source_span }
}
}
impl<'a> I18nIcu<'a> {
/// Creates a deep clone of this ICU expression using the provided allocator.
pub fn clone_in(&self, allocator: &'a Allocator) -> Self {
let mut cases = HashMap::new_in(allocator);
for (key, value) in self.cases.iter() {
cases.insert(key.clone(), value.clone_in(allocator));
}
I18nIcu {
expression: self.expression.clone(),
icu_type: self.icu_type.clone(),
cases,
source_span: self.source_span,
expression_placeholder: self.expression_placeholder.clone(),
}
}
}
impl<'a> I18nTagPlaceholder<'a> {
/// Creates a deep clone of this tag placeholder using the provided allocator.
pub fn clone_in(&self, allocator: &'a Allocator) -> Self {
let mut attrs = HashMap::new_in(allocator);
for (key, value) in self.attrs.iter() {
attrs.insert(key.clone(), value.clone());
}
let mut children = Vec::new_in(allocator);
for child in self.children.iter() {
children.push(child.clone_in(allocator));
}
I18nTagPlaceholder {
tag: self.tag.clone(),
attrs,
start_name: self.start_name.clone(),
close_name: self.close_name.clone(),
children,
is_void: self.is_void,
source_span: self.source_span,
start_source_span: self.start_source_span,
end_source_span: self.end_source_span,
}
}
}
impl<'a> I18nPlaceholder<'a> {
/// Creates a clone of this placeholder (no allocator needed - only atoms and spans).
pub fn clone_in(&self) -> Self {
I18nPlaceholder {
value: self.value.clone(),
name: self.name.clone(),
source_span: self.source_span,
}
}
}
impl<'a> I18nIcuPlaceholder<'a> {
/// Creates a deep clone of this ICU placeholder using the provided allocator.
pub fn clone_in(&self, allocator: &'a Allocator) -> Self {
I18nIcuPlaceholder {
value: Box::new_in(self.value.clone_in(allocator), allocator),
name: self.name.clone(),
source_span: self.source_span,
}
}
}
impl<'a> I18nBlockPlaceholder<'a> {
/// Creates a deep clone of this block placeholder using the provided allocator.
pub fn clone_in(&self, allocator: &'a Allocator) -> Self {
let mut parameters = Vec::new_in(allocator);
for param in self.parameters.iter() {
parameters.push(param.clone());
}
let mut children = Vec::new_in(allocator);
for child in self.children.iter() {
children.push(child.clone_in(allocator));
}
I18nBlockPlaceholder {
name: self.name.clone(),
parameters,
start_name: self.start_name.clone(),
close_name: self.close_name.clone(),
children,
source_span: self.source_span,
start_source_span: self.start_source_span,
end_source_span: self.end_source_span,
}
}
}
// ============================================================================
// i18n Message Serialization
// ============================================================================
/// Serialize i18n nodes to the $localize / goog.getMsg message format.
///
/// This produces a message string with placeholder markers like "{$interpolation}"
/// for expression placeholders and "{$startTag}/{$closeTag}" for element boundaries.
///
/// Ported from Angular's `serialize_message` in `i18n/i18n_ast.ts`.
pub fn serialize_i18n_nodes(nodes: &[I18nNode<'_>]) -> String {
let mut result = String::new();
for node in nodes {
serialize_i18n_node(node, &mut result);
}
result
}
/// Serialize a single i18n node to the message string format.
///
/// Placeholder names are formatted to camelCase for goog.getMsg compatibility.
/// For example: `INTERPOLATION` -> `{$interpolation}`, `START_TAG_DIV` -> `{$startTagDiv}`
fn serialize_i18n_node(node: &I18nNode<'_>, result: &mut String) {
match node {
I18nNode::Text(text) => {
result.push_str(text.value.as_str());
}
I18nNode::Container(container) => {
for child in container.children.iter() {
serialize_i18n_node(child, result);
}
}
I18nNode::Icu(icu) => {
serialize_i18n_icu(icu, result);
}
I18nNode::TagPlaceholder(ph) => {
let start_name = format_i18n_placeholder_name(ph.start_name.as_str(), true);
let close_name = format_i18n_placeholder_name(ph.close_name.as_str(), true);
result.push_str(&format!("{{${start_name}}}"));
for child in ph.children.iter() {
serialize_i18n_node(child, result);
}
result.push_str(&format!("{{${close_name}}}"));
}
I18nNode::Placeholder(ph) => {
let name = format_i18n_placeholder_name(ph.name.as_str(), true);
result.push_str(&format!("{{${name}}}"));
}
I18nNode::IcuPlaceholder(ph) => {
let name = format_i18n_placeholder_name(ph.name.as_str(), true);
result.push_str(&format!("{{${name}}}"));
}
I18nNode::BlockPlaceholder(ph) => {
let start_name = format_i18n_placeholder_name(ph.start_name.as_str(), true);
let close_name = format_i18n_placeholder_name(ph.close_name.as_str(), true);
result.push_str(&format!("{{${start_name}}}"));
for child in ph.children.iter() {
serialize_i18n_node(child, result);
}
result.push_str(&format!("{{${close_name}}}"));
}
}
}
/// Serialize an ICU expression to the message string format.
fn serialize_i18n_icu(icu: &I18nIcu<'_>, result: &mut String) {
// Use expression_placeholder if available, otherwise use expression directly
let expr =
icu.expression_placeholder.as_ref().map_or_else(|| icu.expression.as_str(), |p| p.as_str());
result.push('{');
result.push_str(expr);
result.push_str(", ");
result.push_str(icu.icu_type.as_str());
result.push_str(", ");
// Serialize cases - must be sorted for deterministic output
let mut cases: std::vec::Vec<_> = icu.cases.iter().collect();
cases.sort_by(|a, b| a.0.as_str().cmp(b.0.as_str()));
for (i, (key, value)) in cases.iter().enumerate() {
if i > 0 {
result.push(' ');
}
result.push_str(key.as_str());
result.push_str(" {");
serialize_i18n_node(value, result);
result.push('}');
}
result.push('}');
}
// ============================================================================
// Core Node Enum
// ============================================================================
/// The main R3 node enum containing all R3 AST node types.
#[derive(Debug)]
pub enum R3Node<'a> {
/// A comment node.
Comment(Box<'a, R3Comment<'a>>),
/// A static text node.
Text(Box<'a, R3Text<'a>>),
/// A bound text node with interpolation.
BoundText(Box<'a, R3BoundText<'a>>),
/// An HTML element.
Element(Box<'a, R3Element<'a>>),
/// A template element (`<ng-template>` or structural directive).
Template(Box<'a, R3Template<'a>>),
/// A content projection slot (`<ng-content>`).
Content(Box<'a, R3Content<'a>>),
/// A template variable (`let x`).
Variable(Box<'a, R3Variable<'a>>),
/// A template reference (`#ref`).
Reference(Box<'a, R3Reference<'a>>),
/// An ICU message.
Icu(Box<'a, R3Icu<'a>>),
/// A deferred block (`@defer`).
DeferredBlock(Box<'a, R3DeferredBlock<'a>>),
/// A deferred block placeholder (`@placeholder`).
DeferredBlockPlaceholder(Box<'a, R3DeferredBlockPlaceholder<'a>>),
/// A deferred block loading state (`@loading`).
DeferredBlockLoading(Box<'a, R3DeferredBlockLoading<'a>>),
/// A deferred block error state (`@error`).
DeferredBlockError(Box<'a, R3DeferredBlockError<'a>>),
/// A switch block (`@switch`).
SwitchBlock(Box<'a, R3SwitchBlock<'a>>),
/// A switch block case group (containing cases and children).
SwitchBlockCaseGroup(Box<'a, R3SwitchBlockCaseGroup<'a>>),
/// A for loop block (`@for`).
ForLoopBlock(Box<'a, R3ForLoopBlock<'a>>),
/// A for loop empty block (`@empty`).
ForLoopBlockEmpty(Box<'a, R3ForLoopBlockEmpty<'a>>),
/// An if block (`@if`).
IfBlock(Box<'a, R3IfBlock<'a>>),
/// An if block branch (`@if`, `@else if`, `@else`).
IfBlockBranch(Box<'a, R3IfBlockBranch<'a>>),
/// An unknown block (for error recovery).
UnknownBlock(Box<'a, R3UnknownBlock<'a>>),
/// A let declaration (`@let`).
LetDeclaration(Box<'a, R3LetDeclaration<'a>>),
/// A component reference.
Component(Box<'a, R3Component<'a>>),
/// A directive reference.
Directive(Box<'a, R3Directive<'a>>),
/// A host element (for type checking only, cannot be visited).
HostElement(Box<'a, R3HostElement<'a>>),
}
impl<'a> R3Node<'a> {
/// Returns the source span of this node.
pub fn source_span(&self) -> Span {
match self {
Self::Comment(n) => n.source_span,
Self::Text(n) => n.source_span,
Self::BoundText(n) => n.source_span,
Self::Element(n) => n.source_span,
Self::Template(n) => n.source_span,
Self::Content(n) => n.source_span,
Self::Variable(n) => n.source_span,
Self::Reference(n) => n.source_span,
Self::Icu(n) => n.source_span,
Self::DeferredBlock(n) => n.source_span,
Self::DeferredBlockPlaceholder(n) => n.source_span,
Self::DeferredBlockLoading(n) => n.source_span,
Self::DeferredBlockError(n) => n.source_span,
Self::SwitchBlock(n) => n.source_span,
Self::SwitchBlockCaseGroup(n) => n.source_span,
Self::ForLoopBlock(n) => n.source_span,
Self::ForLoopBlockEmpty(n) => n.source_span,
Self::IfBlock(n) => n.source_span,
Self::IfBlockBranch(n) => n.source_span,
Self::UnknownBlock(n) => n.source_span,
Self::LetDeclaration(n) => n.source_span,
Self::Component(n) => n.source_span,
Self::Directive(n) => n.source_span,
Self::HostElement(n) => n.source_span,
}
}
/// Visit this node with the given visitor.
pub fn visit<V: R3Visitor<'a> + ?Sized>(&self, visitor: &mut V) {
match self {
Self::Comment(n) => visitor.visit_comment(n),
Self::Text(n) => visitor.visit_text(n),
Self::BoundText(n) => visitor.visit_bound_text(n),
Self::Element(n) => visitor.visit_element(n),
Self::Template(n) => visitor.visit_template(n),
Self::Content(n) => visitor.visit_content(n),
Self::Variable(n) => visitor.visit_variable(n),
Self::Reference(n) => visitor.visit_reference(n),
Self::Icu(n) => visitor.visit_icu(n),
Self::DeferredBlock(n) => visitor.visit_deferred_block(n),
Self::DeferredBlockPlaceholder(n) => visitor.visit_deferred_block_placeholder(n),
Self::DeferredBlockLoading(n) => visitor.visit_deferred_block_loading(n),
Self::DeferredBlockError(n) => visitor.visit_deferred_block_error(n),
Self::SwitchBlock(n) => visitor.visit_switch_block(n),
Self::SwitchBlockCaseGroup(n) => visitor.visit_switch_block_case_group(n),
Self::ForLoopBlock(n) => visitor.visit_for_loop_block(n),
Self::ForLoopBlockEmpty(n) => visitor.visit_for_loop_block_empty(n),
Self::IfBlock(n) => visitor.visit_if_block(n),
Self::IfBlockBranch(n) => visitor.visit_if_block_branch(n),
Self::UnknownBlock(n) => visitor.visit_unknown_block(n),
Self::LetDeclaration(n) => visitor.visit_let_declaration(n),
Self::Component(n) => visitor.visit_component(n),
Self::Directive(n) => visitor.visit_directive(n),
// HostElement cannot be visited (used only for type checking)
Self::HostElement(_) => {}
}
}
}
// ============================================================================
// Basic Nodes
// ============================================================================
/// A comment node.
#[derive(Debug, Clone)]
pub struct R3Comment<'a> {
/// The comment text.
pub value: Atom<'a>,
/// Source span.
pub source_span: Span,
}
/// A static text node.
#[derive(Debug)]
pub struct R3Text<'a> {
/// The text content.
pub value: Atom<'a>,
/// Source span.
pub source_span: Span,
}
/// A bound text node containing interpolation.
#[derive(Debug)]
pub struct R3BoundText<'a> {
/// The interpolation expression.
pub value: AngularExpression<'a>,
/// Source span.
pub source_span: Span,
/// i18n metadata.
pub i18n: Option<I18nMeta<'a>>,
}
// ============================================================================
// Attributes
// ============================================================================
/// A static text attribute.
#[derive(Debug)]
pub struct R3TextAttribute<'a> {
/// Attribute name.
pub name: Atom<'a>,
/// Attribute value.
pub value: Atom<'a>,
/// Source span.
pub source_span: Span,
/// Key span (the attribute name).
pub key_span: Option<Span>,
/// Value span.
pub value_span: Option<Span>,
/// i18n metadata.
pub i18n: Option<I18nMeta<'a>>,
}
/// Security context for DOM sanitization.
///
/// In Angular, this can be a single context or an array of contexts
/// (e.g., when an attribute could match multiple element types).
/// The special case `[URL, RESOURCE_URL]` is represented here as
/// `UrlOrResourceUrl` for simplicity.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SecurityContext {
/// No security context needed.
None,
/// HTML content.
Html,
/// Style content.
Style,
/// Script content.
Script,
/// URL content.
Url,
/// Resource URL content.
ResourceUrl,
/// Attribute that should not be bound (security-sensitive).
AttributeNoBinding,
/// Ambiguous URL/ResourceURL context - resolved at runtime.
///
/// This is used when the host element isn't known and the attribute
/// (like `src` or `href`) could require either URL or ResourceURL
/// sanitization depending on the element type. The runtime function
/// `ɵɵsanitizeUrlOrResourceUrl` selects the appropriate sanitizer
/// based on the element tag name.
UrlOrResourceUrl,
}
impl Default for SecurityContext {
fn default() -> Self {
Self::None
}
}
/// A bound attribute with a dynamic expression.
#[derive(Debug)]
pub struct R3BoundAttribute<'a> {
/// Attribute name.
pub name: Atom<'a>,
/// Binding type (Property, Attribute, Class, Style, etc.).
pub binding_type: BindingType,
/// Security context for sanitization.
pub security_context: SecurityContext,
/// The binding expression.
pub value: AngularExpression<'a>,
/// Unit for style bindings (e.g., "px").
pub unit: Option<Atom<'a>>,
/// Source span.
pub source_span: Span,
/// Key span.
pub key_span: Span,
/// Value span.
pub value_span: Option<Span>,
/// i18n metadata.
pub i18n: Option<I18nMeta<'a>>,
}
/// A bound event.
#[derive(Debug)]
pub struct R3BoundEvent<'a> {
/// Event name.
pub name: Atom<'a>,
/// Event type.
pub event_type: ParsedEventType,
/// Handler expression.
pub handler: AngularExpression<'a>,
/// Target element (for `window:` or `document:` events).
pub target: Option<Atom<'a>>,
/// Animation phase.
pub phase: Option<Atom<'a>>,
/// Source span.
pub source_span: Span,
/// Handler span.
pub handler_span: Span,
/// Key span.
pub key_span: Span,
}
// ============================================================================
// Elements
// ============================================================================
/// An HTML element.
#[derive(Debug)]
pub struct R3Element<'a> {
/// Element tag name.
pub name: Atom<'a>,
/// Static attributes.
pub attributes: Vec<'a, R3TextAttribute<'a>>,
/// Bound input properties.
pub inputs: Vec<'a, R3BoundAttribute<'a>>,
/// Bound output events.
pub outputs: Vec<'a, R3BoundEvent<'a>>,
/// Directives applied to this element.
pub directives: Vec<'a, R3Directive<'a>>,
/// Child nodes.
pub children: Vec<'a, R3Node<'a>>,
/// Template references.
pub references: Vec<'a, R3Reference<'a>>,
/// Whether the element is self-closing.
pub is_self_closing: bool,
/// Source span.
pub source_span: Span,
/// Start tag span.
pub start_source_span: Span,
/// End tag span (None for self-closing).
pub end_source_span: Option<Span>,
/// Whether the element is void (e.g., `<br>`).
pub is_void: bool,
/// i18n metadata.
pub i18n: Option<I18nMeta<'a>>,
}
/// A template element (`<ng-template>` or structural directive).
#[derive(Debug)]
pub struct R3Template<'a> {
/// Tag name (None for structural directives on `ng-template`).
pub tag_name: Option<Atom<'a>>,
/// Static attributes.
pub attributes: Vec<'a, R3TextAttribute<'a>>,
/// Bound inputs.
pub inputs: Vec<'a, R3BoundAttribute<'a>>,
/// Bound outputs.
pub outputs: Vec<'a, R3BoundEvent<'a>>,
/// Directives.
pub directives: Vec<'a, R3Directive<'a>>,
/// Template attributes (from structural directive microsyntax).
pub template_attrs: Vec<'a, R3TemplateAttr<'a>>,
/// Child nodes.
pub children: Vec<'a, R3Node<'a>>,
/// Template references.
pub references: Vec<'a, R3Reference<'a>>,
/// Template variables.
pub variables: Vec<'a, R3Variable<'a>>,
/// Whether self-closing.
pub is_self_closing: bool,
/// Source span.
pub source_span: Span,
/// Start span.
pub start_source_span: Span,
/// End span.
pub end_source_span: Option<Span>,
/// i18n metadata.
pub i18n: Option<I18nMeta<'a>>,
}
/// A template attribute (either bound or static).
#[derive(Debug)]
pub enum R3TemplateAttr<'a> {
/// A bound attribute.
Bound(R3BoundAttribute<'a>),
/// A text attribute.
Text(R3TextAttribute<'a>),
}
/// A content projection slot (`<ng-content>`).
#[derive(Debug)]
pub struct R3Content<'a> {
/// The selector for content projection.
pub selector: Atom<'a>,
/// Static attributes.
pub attributes: Vec<'a, R3TextAttribute<'a>>,
/// Child nodes (usually empty).
pub children: Vec<'a, R3Node<'a>>,
/// Whether self-closing.
pub is_self_closing: bool,
/// Source span.
pub source_span: Span,
/// Start span.
pub start_source_span: Span,
/// End span.
pub end_source_span: Option<Span>,
/// i18n metadata.
pub i18n: Option<I18nMeta<'a>>,
}
/// A template variable.
#[derive(Debug)]
pub struct R3Variable<'a> {
/// Variable name.
pub name: Atom<'a>,
/// Variable value (for `let x = value`).
pub value: Atom<'a>,
/// Source span.
pub source_span: Span,
/// Key span.
pub key_span: Span,
/// Value span.
pub value_span: Option<Span>,
}
/// A template reference.
#[derive(Debug)]
pub struct R3Reference<'a> {
/// Reference name.
pub name: Atom<'a>,
/// Reference value (directive name or empty).
pub value: Atom<'a>,
/// Source span.
pub source_span: Span,
/// Key span.
pub key_span: Span,
/// Value span.
pub value_span: Option<Span>,
}
// ============================================================================
// Control Flow Blocks
// ============================================================================
/// An if block (`@if`).
#[derive(Debug)]
pub struct R3IfBlock<'a> {
/// The branches of the if block.
pub branches: Vec<'a, R3IfBlockBranch<'a>>,
/// Source span.
pub source_span: Span,
/// Start span.
pub start_source_span: Span,
/// End span.
pub end_source_span: Option<Span>,
/// Name span.
pub name_span: Span,
}
/// An if block branch.
#[derive(Debug)]
pub struct R3IfBlockBranch<'a> {
/// Condition expression (None for `@else`).
pub expression: Option<AngularExpression<'a>>,
/// Child nodes.
pub children: Vec<'a, R3Node<'a>>,
/// Expression alias (`as` variable).
pub expression_alias: Option<R3Variable<'a>>,
/// Source span.
pub source_span: Span,
/// Start span.
pub start_source_span: Span,
/// End span.
pub end_source_span: Option<Span>,
/// Name span.
pub name_span: Span,
/// i18n metadata.
pub i18n: Option<I18nMeta<'a>>,
}
/// A for loop block (`@for`).
#[derive(Debug)]
pub struct R3ForLoopBlock<'a> {
/// The loop item variable.
pub item: R3Variable<'a>,
/// The iterable expression.
pub expression: ASTWithSource<'a>,
/// The track expression.
pub track_by: ASTWithSource<'a>,
/// Track keyword span.
pub track_keyword_span: Span,
/// Context variables ($index, $first, etc.).
pub context_variables: Vec<'a, R3Variable<'a>>,
/// Child nodes.
pub children: Vec<'a, R3Node<'a>>,
/// Empty block.
pub empty: Option<R3ForLoopBlockEmpty<'a>>,
/// Source span.
pub source_span: Span,
/// Main block span.
pub main_block_span: Span,
/// Start span.
pub start_source_span: Span,
/// End span.
pub end_source_span: Option<Span>,
/// Name span.
pub name_span: Span,
/// i18n metadata.
pub i18n: Option<I18nMeta<'a>>,
}
/// A for loop empty block (`@empty`).
#[derive(Debug)]
pub struct R3ForLoopBlockEmpty<'a> {
/// Child nodes.
pub children: Vec<'a, R3Node<'a>>,
/// Source span.
pub source_span: Span,
/// Start span.
pub start_source_span: Span,
/// End span.
pub end_source_span: Option<Span>,
/// Name span.
pub name_span: Span,
/// i18n metadata.
pub i18n: Option<I18nMeta<'a>>,
}
/// A switch block (`@switch`).
#[derive(Debug)]
pub struct R3SwitchBlock<'a> {
/// The switch expression.
pub expression: AngularExpression<'a>,
/// The switch case groups.
pub groups: Vec<'a, R3SwitchBlockCaseGroup<'a>>,
/// Unknown blocks for error recovery.
pub unknown_blocks: Vec<'a, R3UnknownBlock<'a>>,
/// Source span.
pub source_span: Span,
/// Start span.
pub start_source_span: Span,
/// End span.
pub end_source_span: Option<Span>,
/// Name span.
pub name_span: Span,
}
/// A switch block case (`@case` or `@default`).
///
/// Note: In the R3 AST, `SwitchBlockCase` does NOT have children.
/// Children are stored in `SwitchBlockCaseGroup`.
#[derive(Debug)]
pub struct R3SwitchBlockCase<'a> {
/// Case expression (None for `@default`).
pub expression: Option<AngularExpression<'a>>,
/// Source span.
pub source_span: Span,
/// Start span.
pub start_source_span: Span,
/// End span.
pub end_source_span: Option<Span>,
/// Name span.
pub name_span: Span,
}
/// A switch block case group.
///
/// Groups consecutive case blocks that fall through to the same body.
/// For example: `@case (1) @case (2) { body }` creates one group with
/// two cases and the body as children.
#[derive(Debug)]
pub struct R3SwitchBlockCaseGroup<'a> {
/// The switch cases in this group.
pub cases: Vec<'a, R3SwitchBlockCase<'a>>,
/// Child nodes (the body of the group).
pub children: Vec<'a, R3Node<'a>>,
/// Source span.
pub source_span: Span,
/// Start span.
pub start_source_span: Span,
/// End span.
pub end_source_span: Option<Span>,
/// Name span.
pub name_span: Span,
/// i18n metadata.
pub i18n: Option<I18nMeta<'a>>,
}
// ============================================================================
// Deferred Blocks
// ============================================================================
/// A deferred trigger.
#[derive(Debug)]
pub enum R3DeferredTrigger<'a> {
/// A bound trigger (`when condition`).
Bound(R3BoundDeferredTrigger<'a>),
/// Never trigger.
Never(R3NeverDeferredTrigger),
/// Idle trigger.
Idle(R3IdleDeferredTrigger),
/// Immediate trigger.
Immediate(R3ImmediateDeferredTrigger),
/// Hover trigger.
Hover(R3HoverDeferredTrigger<'a>),
/// Timer trigger.
Timer(R3TimerDeferredTrigger),
/// Interaction trigger.
Interaction(R3InteractionDeferredTrigger<'a>),
/// Viewport trigger.
Viewport(R3ViewportDeferredTrigger<'a>),
}
impl<'a> R3DeferredTrigger<'a> {
/// Returns the source span of the trigger.
pub fn source_span(&self) -> Span {
match self {
Self::Bound(t) => t.source_span,
Self::Never(t) => t.source_span,
Self::Idle(t) => t.source_span,
Self::Immediate(t) => t.source_span,
Self::Hover(t) => t.source_span,
Self::Timer(t) => t.source_span,