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
//! Inline (non-block) element generation.
//!
//! Generates elements that are not block roots, using `createElementVNode()`
//! and `createVNode()` instead of their block counterparts.
use crate::{
ast::*,
transforms::v_memo::{get_memo_exp, has_v_memo},
};
use super::{
super::{
children::generate_children,
context::CodegenContext,
expression::generate_expression,
helpers::is_builtin_component,
node::generate_node,
patch_flag::{calculate_element_patch_info, patch_flag_name},
props::generate_props,
slots::{generate_slots, has_slot_children},
},
directives::{generate_vmodel_closing, generate_vshow_closing},
helpers::{has_vmodel_directive, has_vshow_directive, is_is_prop, is_renderable_prop},
};
use vize_carton::ToCompactString;
/// Generate element code (non-block)
pub fn generate_element(ctx: &mut CodegenContext, el: &ElementNode<'_>) {
// Check for v-memo directive - wrap with memoization
let memo_cache_index = if has_v_memo(el) {
if let Some(memo_exp) = get_memo_exp(el) {
let cache_index = ctx.next_cache_index();
ctx.use_helper(RuntimeHelper::WithMemo);
ctx.push(ctx.helper(RuntimeHelper::WithMemo));
ctx.push("(");
// Generate the memo deps expression with proper _ctx. prefixing
generate_expression(ctx, memo_exp);
ctx.push(", () => ");
Some(cache_index)
} else {
None
}
} else {
None
};
match el.tag_type {
ElementType::Element => {
// Check for v-model directive on native elements (only if no v-show)
let has_vmodel = has_vmodel_directive(el);
if has_vmodel {
ctx.use_helper(RuntimeHelper::WithDirectives);
ctx.push(ctx.helper(RuntimeHelper::WithDirectives));
ctx.push("(");
}
// Check for v-show directive (only if no v-model)
let has_vshow = has_vshow_directive(el) && !has_vmodel;
if has_vshow {
ctx.use_helper(RuntimeHelper::WithDirectives);
ctx.use_helper(RuntimeHelper::VShow);
ctx.push(ctx.helper(RuntimeHelper::WithDirectives));
ctx.push("(");
}
ctx.push_pure();
let helper = ctx.helper(RuntimeHelper::CreateElementVNode);
ctx.use_helper(RuntimeHelper::CreateElementVNode);
ctx.push(helper);
ctx.push("(\"");
ctx.push(&el.tag);
ctx.push("\"");
// Calculate patch flag and dynamic props
let (patch_flag, dynamic_props) = calculate_element_patch_info(
el,
ctx.options.binding_metadata.as_ref(),
ctx.options.cache_handlers,
);
let has_patch_info = patch_flag.is_some() || dynamic_props.is_some();
// Generate props (only if there are renderable props, not just v-show)
// If props are hoisted, use the hoisted reference
if let Some(hoisted_index) = el.hoisted_props_index {
ctx.push(", _hoisted_");
ctx.push(&hoisted_index.to_compact_string());
} else if super::helpers::has_renderable_props(el) {
ctx.push(", ");
generate_props(ctx, &el.props);
} else if !el.children.is_empty() || has_patch_info {
ctx.push(", null");
}
// Generate children
if !el.children.is_empty() {
ctx.push(", ");
generate_children(ctx, &el.children);
} else if has_patch_info {
ctx.push(", null");
}
// Generate patch flag
if let Some(flag) = patch_flag {
ctx.push(", ");
ctx.push(&flag.to_compact_string());
ctx.push(" /* ");
let flag_name = patch_flag_name(flag);
ctx.push(&flag_name);
ctx.push(" */");
}
// Generate dynamic props
if let Some(props) = dynamic_props {
ctx.push(", [");
for (i, prop) in props.iter().enumerate() {
if i > 0 {
ctx.push(", ");
}
ctx.push("\"");
ctx.push(prop);
ctx.push("\"");
}
ctx.push("]");
}
ctx.push(")");
// Close withDirectives for v-model
if has_vmodel {
generate_vmodel_closing(ctx, el);
}
// Close withDirectives for v-show
if has_vshow {
generate_vshow_closing(ctx, el);
}
}
ElementType::Component => {
// Support v-show on non-block components:
// _withDirectives(_createVNode(...), [[_vShow, expr]])
let has_vshow = has_vshow_directive(el);
if has_vshow {
ctx.use_helper(RuntimeHelper::WithDirectives);
ctx.use_helper(RuntimeHelper::VShow);
ctx.push(ctx.helper(RuntimeHelper::WithDirectives));
ctx.push("(");
}
ctx.push_pure();
let helper = ctx.helper(RuntimeHelper::CreateVNode);
ctx.use_helper(RuntimeHelper::CreateVNode);
ctx.push(helper);
ctx.push("(");
// Check for dynamic component (<component :is="..."> or <Component is="...">)
let is_dynamic_component = el.tag == "component" || el.tag == "Component";
let (dynamic_is, static_is) = if is_dynamic_component {
let dynamic = el.props.iter().find_map(|p| {
if let PropNode::Directive(dir) = p {
if dir.name == "bind" {
if let Some(ExpressionNode::Simple(arg)) = &dir.arg {
if arg.content == "is" {
return dir.exp.as_ref();
}
}
}
}
None
});
let static_val = el.props.iter().find_map(|p| {
if let PropNode::Attribute(attr) = p {
if attr.name == "is" {
return attr.value.as_ref().map(|v| v.content.as_str());
}
}
None
});
(dynamic, static_val)
} else {
(None, None)
};
if let Some(is_exp) = dynamic_is {
ctx.use_helper(RuntimeHelper::ResolveDynamicComponent);
ctx.push(ctx.helper(RuntimeHelper::ResolveDynamicComponent));
ctx.push("(");
generate_expression(ctx, is_exp);
ctx.push(")");
} else if let Some(component_name) = static_is {
ctx.use_helper(RuntimeHelper::ResolveDynamicComponent);
ctx.push(ctx.helper(RuntimeHelper::ResolveDynamicComponent));
ctx.push("(\"");
ctx.push(component_name);
ctx.push("\")");
} else if let Some(builtin) = is_builtin_component(&el.tag) {
ctx.use_helper(builtin);
ctx.push(ctx.helper(builtin));
} else if ctx.is_component_in_bindings(&el.tag) {
if !ctx.options.inline {
ctx.push("$setup.");
}
ctx.push(&el.tag);
} else {
ctx.push("_component_");
ctx.push(&el.tag.replace('-', "_"));
}
// Generate props -- for dynamic components, filter out the `is` prop
let effective_has_props = if is_dynamic_component {
el.props
.iter()
.any(|p| !is_is_prop(p) && is_renderable_prop(p))
} else {
!el.props.is_empty()
};
if effective_has_props {
ctx.push(", ");
if is_dynamic_component {
ctx.skip_is_prop = true;
}
// Components: skip scope_id in props -- Vue runtime applies it via __scopeId
let prev_skip_scope_id = ctx.skip_scope_id;
ctx.skip_scope_id = true;
generate_props(ctx, &el.props);
ctx.skip_scope_id = prev_skip_scope_id;
ctx.skip_is_prop = false;
} else if !el.children.is_empty() {
ctx.push(", null");
}
// Generate children/slots - use slot generation for component children
if has_slot_children(el) {
ctx.push(", ");
generate_slots(ctx, el);
}
ctx.push(")");
// Close withDirectives for v-show on component
if has_vshow {
generate_vshow_closing(ctx, el);
}
}
ElementType::Slot => {
let helper = ctx.helper(RuntimeHelper::RenderSlot);
ctx.use_helper(RuntimeHelper::RenderSlot);
ctx.push(helper);
ctx.push("(_ctx.$slots, ");
// Get slot name from props
let slot_name = el
.props
.iter()
.find_map(|p| match p {
PropNode::Attribute(attr) if attr.name == "name" => {
attr.value.as_ref().map(|v| v.content.as_str())
}
_ => None,
})
.unwrap_or("default");
ctx.push("\"");
ctx.push(slot_name);
ctx.push("\"");
// Generate slot props (excluding 'name' attribute)
let slot_props: Vec<_> = el
.props
.iter()
.filter(|p| match p {
PropNode::Attribute(attr) => attr.name != "name",
PropNode::Directive(_) => true,
})
.collect();
// Generate fallback content if present
// Slots: skip scope_id in props -- not a real rendered element
let prev_skip_scope_id = ctx.skip_scope_id;
ctx.skip_scope_id = true;
if !el.children.is_empty() {
// If we have children but no props, pass empty object
if slot_props.is_empty() {
ctx.push(", {}");
} else {
ctx.push(", ");
generate_props(ctx, &el.props);
}
ctx.push(", () => [");
ctx.skip_scope_id = prev_skip_scope_id;
ctx.indent();
for (i, child) in el.children.iter().enumerate() {
if i > 0 {
ctx.push(",");
}
ctx.newline();
generate_node(ctx, child);
}
ctx.deindent();
ctx.newline();
ctx.push("])");
} else if !slot_props.is_empty() {
ctx.push(", ");
generate_props(ctx, &el.props);
ctx.skip_scope_id = prev_skip_scope_id;
ctx.push(")");
} else {
ctx.skip_scope_id = prev_skip_scope_id;
ctx.push(")");
}
}
ElementType::Template => {
// Template elements render their children directly
if el.children.len() == 1 {
generate_node(ctx, &el.children[0]);
} else {
ctx.push("[");
for (i, child) in el.children.iter().enumerate() {
if i > 0 {
ctx.push(", ");
}
generate_node(ctx, child);
}
ctx.push("]");
}
}
}
// Close withMemo wrapper if v-memo was present
if let Some(cache_index) = memo_cache_index {
ctx.push(", _cache, ");
ctx.push(&cache_index.to_compact_string());
ctx.push(")");
}
}