vize_atelier_core 0.237.0

Atelier Core - The core workshop for Vize Vue template parsing, transform lanes, and code generation
Documentation
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
//! Main props generation logic.

use crate::{ExpressionNode, PropNode, RuntimeHelper};
use vize_relief::options::BindingType;

use super::{
    super::expression::generate_expression,
    super::{
        context::CodegenContext,
        helpers::{escape_js_string, is_valid_js_identifier},
    },
    directives::{generate_directive_prop_with_static, is_supported_directive},
    events::{generate_merged_event_handlers, get_von_event_key},
    generate_vbind_object_exp, generate_von_object_exp,
    scan::PropsScan,
};
use vize_carton::{FxHashSet, String};

/// Generate props object
pub fn generate_props(ctx: &mut CodegenContext, props: &[PropNode<'_>]) {
    // Clone scope_id to avoid borrow checker issues.
    // skip_scope_id suppresses duplicate scope attrs for synthetic prop objects.
    let scope_id = if ctx.skip_scope_id {
        None
    } else {
        ctx.options.scope_id.clone()
    };

    // If no props but we have scope_id, generate object with just scope_id
    if props.is_empty() {
        if let Some(ref sid) = scope_id {
            ctx.push("{ \"");
            ctx.push(sid);
            ctx.push("\": \"\" }");
        } else {
            ctx.push("null");
        }
        return;
    }

    if try_generate_static_attrs(ctx, props, scope_id.as_deref()) {
        return;
    }

    let scan = PropsScan::new(ctx, props, ctx.skip_is_prop);

    // Handle cases with object spreads (v-bind="obj" or v-on="obj")
    if scan.has_vbind_obj || scan.has_von_obj {
        if scan.has_other || (scan.has_vbind_obj && scan.has_von_obj) {
            // Multiple spreads or spread with other props: _mergeProps(...).
            // Vue walks props in source order, accumulating non-spread props into
            // object literals and flushing them around each spread, preserving
            // the original ordering (transforms/transformElement.ts buildProps).
            ctx.use_helper(RuntimeHelper::MergeProps);
            ctx.push(ctx.helper(RuntimeHelper::MergeProps));
            ctx.push("(");

            let mut first_merge_arg = true;
            let mut seg_start = 0usize;

            // scope_id is emitted once as a trailing object, never per segment.
            let prev_skip_scope_id = ctx.skip_scope_id;
            ctx.skip_scope_id = true;

            let flush_object =
                |ctx: &mut CodegenContext, start: usize, end: usize, first: &mut bool| {
                    // Does this range hold any renderable non-spread prop?
                    let segment = &props[start..end];
                    let has_renderable = segment.iter().any(|p| match p {
                        PropNode::Attribute(attr) => !(ctx.skip_is_prop && attr.name == "is"),
                        PropNode::Directive(dir) => {
                            // A `:is`/`v-bind:is` directive on a dynamic component is consumed
                            // as the component tag and skipped during generation (mirrors the
                            // skip_is_prop branch in generate_props_object_inner). It must not
                            // count as renderable, or an empty `{}` is flushed into mergeProps.
                            let is_skipped_is = ctx.skip_is_prop
                                && dir.name == "bind"
                                && matches!(
                                    &dir.arg,
                                    Some(ExpressionNode::Simple(exp)) if exp.content == "is"
                                );
                            !is_skipped_is
                                && !(dir.arg.is_none() && (dir.name == "bind" || dir.name == "on"))
                                && is_supported_directive(dir)
                                && dir.name != "slot"
                        }
                    });
                    if !has_renderable {
                        return;
                    }
                    if !*first {
                        ctx.push(", ");
                    }
                    *first = false;
                    let seg_scan = PropsScan::new(ctx, segment, ctx.skip_is_prop);
                    generate_props_object_inner(ctx, segment, true, true, &seg_scan);
                };

            for (index, prop) in props.iter().enumerate() {
                let PropNode::Directive(dir) = prop else {
                    continue;
                };
                let is_vbind_spread = dir.name == "bind" && dir.arg.is_none();
                let is_von_spread = dir.name == "on" && dir.arg.is_none();
                if !is_vbind_spread && !is_von_spread {
                    continue;
                }

                // Flush accumulated non-spread props before this spread.
                flush_object(ctx, seg_start, index, &mut first_merge_arg);
                seg_start = index + 1;

                if !first_merge_arg {
                    ctx.push(", ");
                }
                first_merge_arg = false;
                if is_vbind_spread {
                    if let Some(exp) = &dir.exp {
                        generate_expression(ctx, exp);
                    }
                } else {
                    // v-on spread wrapped with _toHandlers(..., true)
                    ctx.use_helper(RuntimeHelper::ToHandlers);
                    ctx.push(ctx.helper(RuntimeHelper::ToHandlers));
                    ctx.push("(");
                    if let Some(exp) = &dir.exp {
                        generate_expression(ctx, exp);
                    }
                    ctx.push(", true)");
                }
            }

            // Flush any trailing non-spread props.
            flush_object(ctx, seg_start, props.len(), &mut first_merge_arg);

            ctx.skip_scope_id = prev_skip_scope_id;

            // scope_id (if present) is appended as a trailing object.
            if let Some(ref sid) = scope_id {
                if !first_merge_arg {
                    ctx.push(", ");
                }
                ctx.push("{ \"");
                ctx.push(sid);
                ctx.push("\": \"\" }");
            }

            ctx.push(")");
        } else if scan.has_vbind_obj {
            // v-bind="attrs" alone
            // If we have scope_id, we need to merge it with the bound object
            if let Some(ref sid) = scope_id {
                // _mergeProps(_normalizeProps(_guardReactiveProps(obj)), { "data-v-xxx": "" })
                ctx.use_helper(RuntimeHelper::MergeProps);
                ctx.use_helper(RuntimeHelper::NormalizeProps);
                ctx.use_helper(RuntimeHelper::GuardReactiveProps);
                ctx.push(ctx.helper(RuntimeHelper::MergeProps));
                ctx.push("(");
                ctx.push(ctx.helper(RuntimeHelper::NormalizeProps));
                ctx.push("(");
                ctx.push(ctx.helper(RuntimeHelper::GuardReactiveProps));
                ctx.push("(");
                generate_vbind_object_exp(ctx, props);
                ctx.push(")), { \"");
                ctx.push(sid);
                ctx.push("\": \"\" })");
            } else {
                // _normalizeProps(_guardReactiveProps(_ctx.attrs))
                ctx.use_helper(RuntimeHelper::NormalizeProps);
                ctx.use_helper(RuntimeHelper::GuardReactiveProps);
                ctx.push(ctx.helper(RuntimeHelper::NormalizeProps));
                ctx.push("(");
                ctx.push(ctx.helper(RuntimeHelper::GuardReactiveProps));
                ctx.push("(");
                generate_vbind_object_exp(ctx, props);
                ctx.push("))");
            }
        } else {
            // v-on="handlers" alone
            // If we have scope_id, we need to merge it with the handlers
            if let Some(ref sid) = scope_id {
                // _mergeProps(_toHandlers(handlers, true), { "data-v-xxx": "" })
                ctx.use_helper(RuntimeHelper::MergeProps);
                ctx.push(ctx.helper(RuntimeHelper::MergeProps));
                ctx.push("(");
                generate_von_object_exp(ctx, props);
                ctx.push(", { \"");
                ctx.push(sid);
                ctx.push("\": \"\" })");
            } else {
                // _toHandlers(_ctx.handlers)
                generate_von_object_exp(ctx, props);
            }
        }
        return;
    }

    // Check if we need normalizeProps wrapper
    // - dynamic v-model argument
    // - dynamic v-bind key (:[attr])
    // - dynamic v-on key (@[event])
    let needs_normalize = scan.needs_normalize();
    if needs_normalize {
        ctx.use_helper(RuntimeHelper::NormalizeProps);
        ctx.push(ctx.helper(RuntimeHelper::NormalizeProps));
        ctx.push("(");
    }

    generate_props_object(ctx, props, false, &scan);

    // Close normalizeProps wrapper if needed
    if needs_normalize {
        ctx.push(")");
    }
}

fn try_generate_static_attrs(
    ctx: &mut CodegenContext,
    props: &[PropNode<'_>],
    scope_id: Option<&str>,
) -> bool {
    if ctx.skip_is_prop || ctx.options.inline {
        return false;
    }
    if ctx.in_v_for
        && props
            .iter()
            .any(|prop| matches!(prop, PropNode::Attribute(attr) if attr.name == "ref"))
    {
        return false;
    }
    if !props
        .iter()
        .all(|prop| matches!(prop, PropNode::Attribute(_)))
    {
        return false;
    }

    // Vue's behavior on duplicate attributes is to keep the first
    // occurrence and ignore later repeats — the parser already records
    // a recoverable diagnostic for the duplicate but leaves both nodes
    // in the AST so linters can flag them. Dedupe here so the rendered
    // props object doesn't emit `{ id: "a", id: "b" }`. (#958)
    let mut seen: FxHashSet<String> = FxHashSet::default();
    let mut unique_props: Vec<&PropNode<'_>> = Vec::with_capacity(props.len());
    for prop in props {
        if let PropNode::Attribute(attr) = prop {
            if seen.contains(attr.name.as_str()) {
                continue;
            }
            seen.insert(attr.name.clone());
        }
        unique_props.push(prop);
    }

    let multiline = unique_props.len() + usize::from(scope_id.is_some()) > 1;
    if multiline {
        ctx.push("{");
        ctx.indent();
    } else {
        ctx.push("{ ");
    }

    let mut first = true;
    for prop in unique_props {
        let PropNode::Attribute(attr) = prop else {
            // Panic path by invariant: the preflight `all(Attribute)` check above
            // has already rejected directive props. Reaching this arm would mean
            // `props` was mutated while iterating, which is impossible through the
            // shared slice used by codegen.
            unreachable!("checked above");
        };

        if !first {
            ctx.push(",");
        }
        if multiline {
            ctx.newline();
        } else if !first {
            ctx.push(" ");
        }
        first = false;

        let needs_quotes = !is_valid_js_identifier(&attr.name);
        if needs_quotes {
            ctx.push("\"");
        }
        // Anchor the generated prop key back to the attribute name in source,
        // recording the symbol so it lands in the v3 `names` array. No-op
        // without `source_map`.
        ctx.record_mapping_named(&attr.name_loc.start, &attr.name);
        ctx.push(&attr.name);
        if needs_quotes {
            ctx.push("\"");
        }
        ctx.push(": ");
        if let Some(value) = &attr.value {
            ctx.push("\"");
            // Anchor the generated value literal back to the attribute value in
            // source, just inside the opening quote. No-op without `source_map`.
            ctx.record_mapping(&value.loc.start);
            ctx.push(&escape_js_string(&value.content));
            ctx.push("\"");
        } else {
            ctx.push("\"\"");
        }
    }

    if let Some(sid) = scope_id {
        if !first {
            ctx.push(",");
        }
        if multiline {
            ctx.newline();
        } else if !first {
            ctx.push(" ");
        }
        ctx.push("\"");
        ctx.push(sid);
        ctx.push("\": \"\"");
    }

    if multiline {
        ctx.deindent();
        ctx.newline();
        ctx.push("}");
    } else {
        ctx.push(" }");
    }

    true
}

/// Generate props as a regular object { key: value, ... }
fn generate_props_object(
    ctx: &mut CodegenContext,
    props: &[PropNode<'_>],
    skip_object_spreads: bool,
    scan: &PropsScan<'_>,
) {
    generate_props_object_inner(ctx, props, skip_object_spreads, false, scan);
}

/// Generate the props object with optional class/style normalization skipping.
/// `inside_merge_props`: when true, skip normalizeClass/normalizeStyle wrappers
/// because mergeProps handles normalization internally.
fn generate_props_object_inner(
    ctx: &mut CodegenContext,
    props: &[PropNode<'_>],
    skip_object_spreads: bool,
    inside_merge_props: bool,
    scan: &PropsScan<'_>,
) {
    // When inside mergeProps, skip normalizeClass/normalizeStyle wrappers
    let prev_skip = ctx.skip_normalize;
    if inside_merge_props {
        ctx.skip_normalize = true;
    }

    // Clone scope_id to avoid borrow checker issues.
    // skip_scope_id suppresses duplicate scope attrs for synthetic prop objects.
    let scope_id = if ctx.skip_scope_id {
        None
    } else {
        ctx.options.scope_id.clone()
    };

    // Skip static class/style if we have dynamic version (will merge them)
    let skip_static_class = scan.skip_static_class();
    let skip_static_style = scan.skip_static_style();
    let multiline = scan.multiline(scope_id.is_some());

    if multiline {
        ctx.push("{");
        ctx.indent();
    } else {
        ctx.push("{ ");
    }

    let mut first = true;
    // Track which event names have already been output (for array merging)
    let mut emitted_events: Option<FxHashSet<String>> = None;

    for prop in props {
        // Skip v-slot directive (handled separately in slots codegen)
        if let PropNode::Directive(dir) = prop
            && dir.name == "slot"
        {
            continue;
        }

        // Skip `is` prop when generating for dynamic components
        if ctx.skip_is_prop {
            match prop {
                PropNode::Attribute(attr) if attr.name == "is" => continue,
                PropNode::Directive(dir)
                    if dir.name == "bind"
                        && matches!(&dir.arg, Some(ExpressionNode::Simple(exp)) if exp.content == "is") =>
                {
                    continue;
                }
                _ => {}
            }
        }

        match prop {
            PropNode::Attribute(attr) => {
                // Skip static class/style if merging with dynamic
                if skip_static_class && attr.name == "class" {
                    continue;
                }
                if skip_static_style && attr.name == "style" {
                    continue;
                }
                if !first {
                    ctx.push(",");
                }
                if multiline {
                    ctx.newline();
                } else if !first {
                    ctx.push(" ");
                }
                first = false;

                // Check if this is a ref attribute that needs ref_key generation
                let ref_value = if attr.name == "ref" && ctx.options.inline {
                    attr.value.as_ref()
                } else {
                    None
                };
                let ref_binding_type = if let Some(value) = ref_value {
                    ctx.options
                        .binding_metadata
                        .as_ref()
                        .and_then(|m| m.bindings.get(value.content.as_str()).copied())
                } else {
                    None
                };
                let should_ref_runtime_binding = matches!(
                    ref_binding_type,
                    Some(
                        BindingType::SetupLet | BindingType::SetupRef | BindingType::SetupMaybeRef
                    )
                );
                let needs_ref_for = attr.name == "ref" && ctx.in_v_for;

                if let (true, Some(ref_value)) = (should_ref_runtime_binding, ref_value) {
                    // Emit ref_key + ref pair for setup-let/ref/maybe-ref bindings.
                    // Vue's runtime setRef() needs ref_key to write to instance.refs,
                    // which is essential for useTemplateRef to receive the element.
                    let ref_name = &ref_value.content;
                    if needs_ref_for {
                        ctx.push("ref_for: true, ");
                    }
                    ctx.push("ref_key: \"");
                    ctx.push(ref_name);
                    ctx.push("\", ref: ");
                    ctx.push(ref_name);
                } else {
                    if needs_ref_for {
                        ctx.push("ref_for: true, ");
                    }
                    // Normal attribute output
                    let needs_quotes = !is_valid_js_identifier(&attr.name);
                    if needs_quotes {
                        ctx.push("\"");
                    }
                    // Anchor the generated prop key back to the attribute name in
                    // source, recording the symbol so it lands in the v3 `names`
                    // array. No-op without `source_map`.
                    ctx.record_mapping_named(&attr.name_loc.start, &attr.name);
                    ctx.push(&attr.name);
                    if needs_quotes {
                        ctx.push("\"");
                    }
                    ctx.push(": ");
                    if let Some(value) = &attr.value {
                        // In inline mode, ref="refName" should reference a mutable/setup-ref
                        // binding. Other bindings (notably props) are still string refs.
                        if should_ref_runtime_binding {
                            ctx.push(&value.content);
                        } else {
                            ctx.push("\"");
                            // Anchor the generated value literal back to the
                            // attribute value, just inside the opening quote.
                            // No-op without `source_map`.
                            ctx.record_mapping(&value.loc.start);
                            ctx.push(&escape_js_string(&value.content));
                            ctx.push("\"");
                        }
                    } else {
                        ctx.push("\"\"");
                    }
                }
            }
            PropNode::Directive(dir) => {
                // Skip v-bind/v-on object spreads (handled separately by generate_props)
                if skip_object_spreads
                    && dir.arg.is_none()
                    && (dir.name == "bind" || dir.name == "on")
                {
                    continue;
                }
                // Only add comma if directive produces valid output
                if is_supported_directive(dir) {
                    // Check for duplicate v-on events that should be merged into arrays
                    if dir.name == "on"
                        && let Some(event_key) = get_von_event_key(dir, ctx.props_is_plain_element)
                    {
                        let count = scan.event_counts.count(&event_key);
                        if count > 1 {
                            let emitted_events =
                                emitted_events.get_or_insert_with(FxHashSet::default);
                            if emitted_events.contains(&event_key) {
                                // Skip: already emitted as part of array
                                continue;
                            }
                            // First occurrence: emit as array with all handlers for this event
                            emitted_events.insert(event_key.clone());
                            if !first {
                                ctx.push(",");
                            }
                            if multiline {
                                ctx.newline();
                            } else if !first {
                                ctx.push(" ");
                            }
                            first = false;
                            generate_merged_event_handlers(
                                ctx,
                                props,
                                &event_key,
                                scan.static_class,
                                scan.static_style,
                            );
                            continue;
                        }
                    }

                    if !first {
                        ctx.push(",");
                    }
                    if multiline {
                        ctx.newline();
                    } else if !first {
                        ctx.push(" ");
                    }
                    first = false;
                    generate_directive_prop_with_static(
                        ctx,
                        dir,
                        super::directives::StaticMerge {
                            class: scan.static_class,
                            class_before: scan.static_class_before_dynamic,
                            style: scan.static_style,
                            style_before: scan.static_style_before_dynamic,
                        },
                    );
                }
            }
        }
    }

    // Add scope_id attribute for scoped CSS
    if let Some(ref sid) = scope_id {
        if !first {
            ctx.push(",");
        }
        if multiline {
            ctx.newline();
        } else if !first {
            ctx.push(" ");
        }
        ctx.push("\"");
        ctx.push(sid);
        ctx.push("\": \"\"");
    }

    if multiline {
        ctx.deindent();
        ctx.newline();
        ctx.push("}");
    } else {
        ctx.push(" }");
    }

    // Restore skip_normalize flag
    ctx.skip_normalize = prev_skip;
}