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
//! AST traversal functions for template transformation.
use crate::ast::*;
use crate::transforms::v_slot::{get_slot_name, get_slot_prop_names, get_slot_props_string};
use vize_carton::profile;
use super::element::{transform_element, transform_interpolation};
use super::structural::{
StructuralDirectiveKind, take_structural_directive, transform_v_for, transform_v_if,
};
use super::{ExitFn, ParentNode, TransformContext};
fn enter_v_slot_scope_if_needed<'a>(ctx: &mut TransformContext<'a>, el: &ElementNode<'a>) -> bool {
if el.children.is_empty()
|| (el.tag_type != ElementType::Component && el.tag.as_str() != "template")
{
return false;
}
for prop in el.props.iter() {
if let PropNode::Directive(dir) = prop {
if dir.name != "slot" {
continue;
}
let prop_names = get_slot_prop_names(dir);
if prop_names.is_empty() {
return false;
}
let slot_name = get_slot_name(dir);
let props_pattern = get_slot_props_string(dir);
ctx.enter_v_slot_scope(
slot_name.as_str(),
props_pattern.as_ref().map(|pattern| pattern.as_str()),
&prop_names,
dir.loc.start.offset,
el.loc.end.offset,
);
return true;
}
}
false
}
/// Traverse children of a parent node
pub fn traverse_children<'a>(ctx: &mut TransformContext<'a>, parent: ParentNode<'a>) {
let children = parent.children_mut();
let mut i = 0;
while i < children.len() {
ctx.grandparent = ctx.parent;
ctx.parent = Some(parent);
ctx.child_index = i;
ctx.reset_node_removed();
traverse_node(ctx, &mut children[i]);
if ctx.was_node_removed() {
// Node was removed, don't increment i
} else {
i += 1;
}
}
}
/// Traverse a single node
pub fn traverse_node<'a>(ctx: &mut TransformContext<'a>, node: &mut TemplateChildNode<'a>) {
ctx.current_node = Some(node as *mut _);
// Collect exit functions from transforms
let mut exit_fns: std::vec::Vec<ExitFn<'a>> = std::vec::Vec::new();
// Apply node transforms based on node type
match node {
TemplateChildNode::Element(el) => {
// Check for structural directives first
let structural_result = profile!(
"atelier.transform.check_structural",
take_structural_directive(el)
);
if let Some((directive_kind, exp)) = structural_result {
// Handle the structural directive
match directive_kind {
StructuralDirectiveKind::If => {
if let Some(exits) = profile!(
"atelier.transform.v_if",
transform_v_if(ctx, exp.as_ref(), true)
) {
exit_fns.extend(exits);
}
}
StructuralDirectiveKind::ElseIf | StructuralDirectiveKind::Else => {
if let Some(exits) = profile!(
"atelier.transform.v_if",
transform_v_if(ctx, exp.as_ref(), false)
) {
exit_fns.extend(exits);
}
}
StructuralDirectiveKind::For => {
if let Some(exits) = profile!(
"atelier.transform.v_for",
transform_v_for(ctx, exp.as_ref())
) {
exit_fns.extend(exits);
}
}
}
// If node was replaced (e.g., by v-if transform), we need to traverse the new node
if let Some(current_ptr) = ctx.current_node {
// SAFETY: `ctx.current_node` is written immediately before
// transforms run and points at the child slot currently owned
// by the traversal loop. Structural transforms may replace
// that slot, but they do not free the bump-allocated storage
// or retain another mutable reference to it. We reborrow here
// only after the transform call returns so we can inspect the
// replacement without cloning the AST node.
let current = unsafe { &mut *current_ptr };
match current {
TemplateChildNode::If(if_node) => {
// Traverse if branches that were just created
for i in 0..if_node.branches.len() {
let branch_ptr = &mut if_node.branches[i] as *mut IfBranchNode<'a>;
profile!(
"atelier.transform.traverse_v_if_branch",
traverse_children(ctx, ParentNode::IfBranch(branch_ptr))
);
}
// Run exit functions and return early
for exit_fn in exit_fns.into_iter().rev() {
profile!("atelier.transform.exit_fn", exit_fn(ctx));
}
return;
}
TemplateChildNode::For(for_node) => {
// Enter v-for scope with aliases
let value = for_node.value_alias.as_ref().and_then(|e| {
if let ExpressionNode::Simple(exp) = e {
Some(exp.content.as_str())
} else {
None
}
});
let key = for_node.key_alias.as_ref().and_then(|e| {
if let ExpressionNode::Simple(exp) = e {
Some(exp.content.as_str())
} else {
None
}
});
let index = for_node.object_index_alias.as_ref().and_then(|e| {
if let ExpressionNode::Simple(exp) = e {
Some(exp.content.as_str())
} else {
None
}
});
let source = match &for_node.source {
ExpressionNode::Simple(exp) => exp.content.as_str(),
ExpressionNode::Compound(c) => c.loc.source.as_str(),
};
ctx.enter_v_for_scope(value, key, index, source);
// Traverse for children
let for_ptr = for_node.as_mut() as *mut ForNode<'a>;
profile!(
"atelier.transform.traverse_v_for_children",
traverse_children(ctx, ParentNode::For(for_ptr))
);
// Exit v-for scope
ctx.exit_scope();
// Add helpers
ctx.helper(RuntimeHelper::RenderList);
ctx.helper(RuntimeHelper::Fragment);
// Run exit functions and return early
for exit_fn in exit_fns.into_iter().rev() {
profile!("atelier.transform.exit_fn", exit_fn(ctx));
}
return;
}
TemplateChildNode::Element(el) => {
// Still an element, process it
if let Some(exits) =
profile!("atelier.transform.element", transform_element(ctx, el))
{
exit_fns.extend(exits);
}
}
_ => {}
}
} else {
// Node was removed, return early
return;
}
} else {
// No structural directive, process element normally
if let Some(exits) =
profile!("atelier.transform.element", transform_element(ctx, el))
{
exit_fns.extend(exits);
}
}
}
TemplateChildNode::Interpolation(interp) => {
profile!(
"atelier.transform.interpolation",
transform_interpolation(ctx, interp)
);
}
TemplateChildNode::Text(_) => {
ctx.helper(RuntimeHelper::CreateText);
}
TemplateChildNode::Comment(_) => {
ctx.helper(RuntimeHelper::CreateComment);
}
TemplateChildNode::If(if_node) => {
// Traverse if branches
for i in 0..if_node.branches.len() {
let branch_ptr = &mut if_node.branches[i] as *mut IfBranchNode<'a>;
profile!(
"atelier.transform.traverse_v_if_branch",
traverse_children(ctx, ParentNode::IfBranch(branch_ptr))
);
}
}
TemplateChildNode::For(for_node) => {
// Enter v-for scope with aliases
let value = for_node.value_alias.as_ref().and_then(|e| {
if let ExpressionNode::Simple(exp) = e {
Some(exp.content.as_str())
} else {
None
}
});
let key = for_node.key_alias.as_ref().and_then(|e| {
if let ExpressionNode::Simple(exp) = e {
Some(exp.content.as_str())
} else {
None
}
});
let index = for_node.object_index_alias.as_ref().and_then(|e| {
if let ExpressionNode::Simple(exp) = e {
Some(exp.content.as_str())
} else {
None
}
});
let source = match &for_node.source {
ExpressionNode::Simple(exp) => exp.content.as_str(),
ExpressionNode::Compound(c) => c.loc.source.as_str(),
};
ctx.enter_v_for_scope(value, key, index, source);
// Traverse for children
let for_ptr = for_node.as_mut() as *mut ForNode<'a>;
profile!(
"atelier.transform.traverse_v_for_children",
traverse_children(ctx, ParentNode::For(for_ptr))
);
// Exit v-for scope
ctx.exit_scope();
// Add helpers
ctx.helper(RuntimeHelper::RenderList);
ctx.helper(RuntimeHelper::Fragment);
}
_ => {}
}
// Traverse children for element nodes
if let TemplateChildNode::Element(el) = node
&& !el.children.is_empty()
{
let entered_slot_scope = enter_v_slot_scope_if_needed(ctx, el);
let el_ptr = el.as_mut() as *mut ElementNode<'a>;
profile!(
"atelier.transform.traverse_element_children",
traverse_children(ctx, ParentNode::Element(el_ptr))
);
if entered_slot_scope {
ctx.exit_scope();
}
}
// Call exit functions in reverse order
ctx.current_node = Some(node as *mut _);
for exit_fn in exit_fns.into_iter().rev() {
profile!("atelier.transform.exit_fn", exit_fn(ctx));
}
}