vize_atelier_sfc 0.10.0

Atelier SFC - The Single File Component workshop for Vize
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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
//! Template compilation for Vue SFCs.
//!
//! This module handles compilation of `<template>` blocks,
//! supporting both DOM mode and Vapor mode.

use vize_atelier_vapor::{compile_vapor, VaporCompilerOptions};
use vize_carton::Bump;

use crate::types::*;

/// Compile template block
pub(crate) fn compile_template_block(
    template: &SfcTemplateBlock,
    options: &TemplateCompileOptions,
    scope_id: &str,
    has_scoped: bool,
    is_ts: bool,
    bindings: Option<&BindingMetadata>,
    croquis: Option<vize_croquis::analysis::Croquis>,
) -> Result<String, SfcError> {
    let allocator = Bump::new();

    // Build DOM compiler options
    let mut dom_opts = options.compiler_options.clone().unwrap_or_default();
    dom_opts.mode = vize_atelier_core::options::CodegenMode::Module;
    dom_opts.prefix_identifiers = true;
    dom_opts.scope_id = if has_scoped {
        let mut attr = String::with_capacity(scope_id.len() + 7);
        attr.push_str("data-v-");
        attr.push_str(scope_id);
        Some(attr.into())
    } else {
        None
    };
    dom_opts.ssr = options.ssr;
    dom_opts.is_ts = is_ts;

    // For script setup, use inline mode to match Vue's actual compiler behavior
    // Inline mode generates direct closure references (e.g., msg instead of $setup.msg)
    // which are captured in the setup() function scope
    if bindings.is_some() {
        dom_opts.inline = true;
        dom_opts.hoist_static = true;
        dom_opts.cache_handlers = true;
    }

    // Pass binding metadata directly (no string conversion needed)
    dom_opts.binding_metadata = bindings.cloned();

    // Pass Croquis to DOM compiler for enhanced transforms
    if let Some(c) = croquis {
        dom_opts.croquis = Some(Box::new(c));
    }

    // Compile template
    let (_, errors, result) =
        vize_atelier_dom::compile_template_with_options(&allocator, &template.content, dom_opts);

    if !errors.is_empty() {
        let mut message = String::from("Template compilation errors: ");
        use std::fmt::Write as _;
        let _ = write!(&mut message, "{:?}", errors);
        return Err(SfcError {
            message,
            code: Some("TEMPLATE_ERROR".to_string()),
            loc: Some(template.loc.clone()),
        });
    }

    // Generate render function with proper imports
    let mut output = String::new();

    // Add Vue imports
    output.push_str(&result.preamble);
    output.push('\n');

    // The codegen already generates a complete function with closing brace,
    // so we just need to use it directly
    output.push_str(&result.code);
    output.push('\n');

    Ok(output)
}

/// Compile template block using Vapor mode
pub(crate) fn compile_template_block_vapor(
    template: &SfcTemplateBlock,
    scope_id: &str,
    has_scoped: bool,
) -> Result<String, SfcError> {
    let allocator = Bump::new();

    // Build Vapor compiler options
    let vapor_opts = VaporCompilerOptions {
        prefix_identifiers: false,
        ssr: false,
        ..Default::default()
    };

    // Compile template with Vapor
    let result = compile_vapor(&allocator, &template.content, vapor_opts);

    if !result.error_messages.is_empty() {
        let mut message = String::from("Vapor template compilation errors: ");
        use std::fmt::Write as _;
        let _ = write!(&mut message, "{:?}", result.error_messages);
        return Err(SfcError {
            message,
            code: Some("VAPOR_TEMPLATE_ERROR".to_string()),
            loc: Some(template.loc.clone()),
        });
    }

    // Process the Vapor output to extract imports and render function
    let mut output = String::new();
    let scope_attr = if has_scoped {
        let mut attr = String::with_capacity(scope_id.len() + 7);
        attr.push_str("data-v-");
        attr.push_str(scope_id);
        attr
    } else {
        String::new()
    };

    // Parse the Vapor output to separate imports and function body
    let code = &result.code;

    // Extract import line
    if let Some(import_end) = code.find('\n') {
        let import_line = &code[..import_end];
        // Rewrite import to use 'vue' instead of 'vue/vapor' for compatibility
        output.push_str(import_line);
        output.push('\n');

        // Extract template declarations and function body
        let rest = &code[import_end + 1..];

        // Find template declarations (const tN = ...)
        let mut template_decls = Vec::new();
        let mut func_start = 0;
        for (i, line) in rest.lines().enumerate() {
            if line.starts_with("const t") && line.contains("_template(") {
                // Add scope ID to template if scoped
                if has_scoped && !scope_attr.is_empty() {
                    let modified = add_scope_id_to_template(line, &scope_attr);
                    template_decls.push(modified);
                } else {
                    template_decls.push(line.to_string());
                }
            } else if line.starts_with("export default") {
                func_start = i;
                break;
            }
        }

        // Output template declarations
        for decl in template_decls {
            output.push_str(&decl);
            output.push('\n');
        }

        // Extract and convert the function body
        let lines: Vec<&str> = rest.lines().collect();
        if func_start < lines.len() {
            // Convert "export default () => {" to "function render(_ctx, $props, $emit, $attrs, $slots) {"
            output.push_str("function render(_ctx, $props, $emit, $attrs, $slots) {\n");

            // Copy function body (skip "export default () => {" and final "}")
            for line in lines.iter().skip(func_start + 1) {
                if *line == "}" {
                    break;
                }
                output.push_str(line);
                output.push('\n');
            }

            output.push_str("}\n");
        }
    }

    Ok(output)
}

/// Add scope ID to template string
fn add_scope_id_to_template(template_line: &str, scope_id: &str) -> String {
    // Find the template string content and add scope_id to the first element
    if let Some(start) = template_line.find("\"<") {
        if let Some(end) = template_line.rfind(">\"") {
            let prefix = &template_line[..start + 2]; // up to and including "<"
            let content = &template_line[start + 2..end + 1]; // element content
            let suffix = &template_line[end + 1..]; // closing quote and paren

            // Find end of first tag name
            if let Some(tag_end) = content.find(|c: char| c.is_whitespace() || c == '>') {
                let tag_name = &content[..tag_end];
                let rest = &content[tag_end..];

                // Insert scope_id attribute after tag name
                let mut result = String::with_capacity(
                    prefix.len() + tag_name.len() + scope_id.len() + rest.len() + suffix.len() + 1,
                );
                result.push_str(prefix);
                result.push_str(tag_name);
                result.push(' ');
                result.push_str(scope_id);
                result.push_str(rest);
                result.push_str(suffix);
                return result;
            }
        }
    }
    template_line.to_string()
}

/// State for tracking string/template literal/comment context across multiple lines.
/// Required because template literals (backtick strings) and block comments can span
/// multiple lines, and `${...}` expressions within template literals contain code-mode
/// braces that must be distinguished from surrounding code braces.
#[derive(Clone)]
struct StringTrackState {
    /// Whether we're inside a string literal (regular or template literal text portion)
    in_string: bool,
    /// The quote character of the current string ('\'' | '"' | '`')
    string_char: char,
    /// Whether the last character was a backslash escape
    escape: bool,
    /// Stack for nested template literal `${...}` expressions.
    /// Each entry tracks the brace depth within that expression.
    /// When a `}` is encountered and the top depth is 0, the expression ends
    /// and we return to the enclosing template literal text.
    template_expr_brace_stack: Vec<i32>,
    /// Whether we're inside a `/* ... */` block comment.
    /// Block comments can span multiple lines and may contain quote characters
    /// (e.g., `doesn't`) that must not be treated as string delimiters.
    in_block_comment: bool,
}

impl Default for StringTrackState {
    fn default() -> Self {
        Self {
            in_string: false,
            string_char: '\0',
            escape: false,
            template_expr_brace_stack: Vec::new(),
            in_block_comment: false,
        }
    }
}

/// Count net brace depth change ({ minus }) in a line, properly tracking
/// string literals, block comments, and template literal `${...}` expressions.
/// State is carried across lines to handle multiline template literals and comments.
fn count_braces_with_state(line: &str, state: &mut StringTrackState) -> i32 {
    let mut count: i32 = 0;
    let chars: Vec<char> = line.chars().collect();
    let len = chars.len();
    let mut i = 0;

    while i < len {
        let ch = chars[i];

        if state.escape {
            state.escape = false;
            i += 1;
            continue;
        }

        // Inside a block comment: skip everything until */
        if state.in_block_comment {
            if ch == '*' && i + 1 < len && chars[i + 1] == '/' {
                state.in_block_comment = false;
                i += 2; // Skip both * and /
                continue;
            }
            i += 1;
            continue;
        }

        if state.in_string {
            if ch == '\\' {
                state.escape = true;
                i += 1;
                continue;
            }

            if state.string_char == '`' {
                if ch == '`' {
                    // Close template literal
                    state.in_string = false;
                } else if ch == '$' && i + 1 < len && chars[i + 1] == '{' {
                    // Enter template expression ${...}
                    // The ${ is template syntax, not a code brace
                    state.in_string = false;
                    state.template_expr_brace_stack.push(0);
                    i += 2; // Skip both $ and {
                    continue;
                }
            } else if ch == state.string_char {
                // Close regular string (' or ")
                state.in_string = false;
            }
        } else {
            // Not in string - we're in code mode
            match ch {
                '/' if i + 1 < len && chars[i + 1] == '*' => {
                    // Enter block comment /*
                    state.in_block_comment = true;
                    i += 2; // Skip both / and *
                    continue;
                }
                '/' if i + 1 < len && chars[i + 1] == '/' => {
                    // Line comment // — skip rest of line
                    break;
                }
                '\'' | '"' => {
                    state.in_string = true;
                    state.string_char = ch;
                }
                '`' => {
                    state.in_string = true;
                    state.string_char = '`';
                }
                '{' => {
                    if let Some(depth) = state.template_expr_brace_stack.last_mut() {
                        *depth += 1;
                    }
                    count += 1;
                }
                '}' => {
                    if let Some(&depth) = state.template_expr_brace_stack.last() {
                        if depth == 0 {
                            // This } closes the ${...} expression, not a code brace
                            state.template_expr_brace_stack.pop();
                            state.in_string = true;
                            state.string_char = '`';
                            i += 1;
                            continue;
                        } else {
                            // Nested brace inside ${...} expression
                            *state.template_expr_brace_stack.last_mut().unwrap() -= 1;
                            count -= 1;
                        }
                    } else {
                        count -= 1;
                    }
                }
                _ => {}
            }
        }

        i += 1;
    }

    count
}

/// Count net brace depth change ({  minus }) in a line, ignoring braces inside string literals.
/// NOTE: This function does NOT track state across lines. For multiline template literals,
/// use `count_braces_with_state` instead.
#[cfg(test)]
fn count_braces_outside_strings(line: &str) -> i32 {
    let mut state = StringTrackState::default();
    count_braces_with_state(line, &mut state)
}

/// Count net paren depth change (( minus )) in a line, properly tracking
/// string literals, block comments, and template literal `${...}` expressions.
/// State is carried across lines to handle multiline template literals and comments.
fn count_parens_with_state(line: &str, state: &mut StringTrackState) -> i32 {
    let mut count: i32 = 0;
    let chars: Vec<char> = line.chars().collect();
    let len = chars.len();
    let mut i = 0;

    while i < len {
        let ch = chars[i];

        if state.escape {
            state.escape = false;
            i += 1;
            continue;
        }

        // Inside a block comment: skip everything until */
        if state.in_block_comment {
            if ch == '*' && i + 1 < len && chars[i + 1] == '/' {
                state.in_block_comment = false;
                i += 2; // Skip both * and /
                continue;
            }
            i += 1;
            continue;
        }

        if state.in_string {
            if ch == '\\' {
                state.escape = true;
                i += 1;
                continue;
            }

            if state.string_char == '`' {
                if ch == '`' {
                    state.in_string = false;
                } else if ch == '$' && i + 1 < len && chars[i + 1] == '{' {
                    state.in_string = false;
                    state.template_expr_brace_stack.push(0);
                    i += 2;
                    continue;
                }
            } else if ch == state.string_char {
                state.in_string = false;
            }
        } else {
            // Not in string - we're in code mode
            match ch {
                '/' if i + 1 < len && chars[i + 1] == '*' => {
                    // Enter block comment /*
                    state.in_block_comment = true;
                    i += 2; // Skip both / and *
                    continue;
                }
                '/' if i + 1 < len && chars[i + 1] == '/' => {
                    // Line comment // — skip rest of line
                    break;
                }
                '\'' | '"' => {
                    state.in_string = true;
                    state.string_char = ch;
                }
                '`' => {
                    state.in_string = true;
                    state.string_char = '`';
                }
                '{' => {
                    // Track braces for template expression depth even though we're counting parens
                    if let Some(depth) = state.template_expr_brace_stack.last_mut() {
                        *depth += 1;
                    }
                }
                '}' => {
                    if let Some(&depth) = state.template_expr_brace_stack.last() {
                        if depth == 0 {
                            state.template_expr_brace_stack.pop();
                            state.in_string = true;
                            state.string_char = '`';
                            i += 1;
                            continue;
                        } else {
                            *state.template_expr_brace_stack.last_mut().unwrap() -= 1;
                        }
                    }
                }
                '(' => {
                    count += 1;
                }
                ')' => {
                    count -= 1;
                }
                _ => {}
            }
        }

        i += 1;
    }

    count
}

/// Compact render body by removing unnecessary line breaks inside function calls and arrays
#[allow(dead_code)]
fn compact_render_body(render_body: &str) -> String {
    let mut result = String::new();
    let mut chars = render_body.chars().peekable();
    let mut paren_depth: i32 = 0;
    let mut bracket_depth: i32 = 0;
    let mut brace_depth: i32 = 0;
    let mut in_string = false;
    let mut string_char = '\0';
    let mut in_template = false;

    while let Some(ch) = chars.next() {
        match ch {
            '"' | '\'' if !in_template => {
                if !in_string {
                    in_string = true;
                    string_char = ch;
                } else if string_char == ch {
                    in_string = false;
                }
                result.push(ch);
            }
            '`' => {
                in_template = !in_template;
                result.push(ch);
            }
            '(' if !in_string && !in_template => {
                paren_depth += 1;
                result.push(ch);
            }
            ')' if !in_string && !in_template => {
                paren_depth = paren_depth.saturating_sub(1);
                result.push(ch);
            }
            '[' if !in_string && !in_template => {
                bracket_depth += 1;
                result.push(ch);
            }
            ']' if !in_string && !in_template => {
                bracket_depth = bracket_depth.saturating_sub(1);
                result.push(ch);
            }
            '{' if !in_string && !in_template => {
                brace_depth += 1;
                result.push(ch);
            }
            '}' if !in_string && !in_template => {
                brace_depth = brace_depth.saturating_sub(1);
                result.push(ch);
            }
            '\n' => {
                // If inside braces (block bodies), keep newlines to preserve statement separation
                if brace_depth > 0 && !in_string && !in_template {
                    result.push('\n');
                } else if (paren_depth > 0 || bracket_depth > 0) && !in_string && !in_template {
                    result.push(' ');
                    // Skip following whitespace
                    while let Some(&next_ch) = chars.peek() {
                        if next_ch.is_whitespace() && next_ch != '\n' {
                            chars.next();
                        } else {
                            break;
                        }
                    }
                } else {
                    // Keep newline outside of function calls/arrays or inside strings
                    result.push(ch);
                }
            }
            _ => result.push(ch),
        }
    }

    result
}

/// Extract imports, hoisted consts, and render function from compiled template code
/// Returns (imports, hoisted, render_function) where render_function is the full function definition
pub(crate) fn extract_template_parts_full(template_code: &str) -> (String, String, String) {
    let mut imports = String::new();
    let mut hoisted = String::new();
    let mut render_fn = String::new();
    let mut in_render = false;
    let mut brace_depth = 0;
    let mut brace_state = StringTrackState::default();

    for line in template_code.lines() {
        let trimmed = line.trim();

        if trimmed.starts_with("import ") {
            imports.push_str(line);
            imports.push('\n');
        } else if trimmed.starts_with("const _hoisted_") {
            hoisted.push_str(line);
            hoisted.push('\n');
        } else if trimmed.starts_with("export function render(")
            || trimmed.starts_with("function render(")
        {
            in_render = true;
            brace_depth = 0;
            brace_state = StringTrackState::default();
            brace_depth += count_braces_with_state(line, &mut brace_state);
            render_fn.push_str(line);
            render_fn.push('\n');
        } else if in_render {
            brace_depth += count_braces_with_state(line, &mut brace_state);
            render_fn.push_str(line);
            render_fn.push('\n');

            if brace_depth == 0 {
                in_render = false;
            }
        }
    }

    (imports, hoisted, render_fn)
}

/// Extract imports, hoisted consts, preamble (component/directive resolution), and render body
/// from compiled template code.
/// Returns (imports, hoisted, preamble, render_body)
#[allow(dead_code)]
pub(crate) fn extract_template_parts(template_code: &str) -> (String, String, String, String) {
    let mut imports = String::new();
    let mut hoisted = String::new();
    let mut preamble = String::new(); // Component/directive resolution statements
    let mut render_body = String::new();
    let mut in_render = false;
    let mut in_return = false;
    let mut brace_depth = 0;
    let mut brace_state = StringTrackState::default();
    let mut paren_state = StringTrackState::default();
    let mut return_paren_depth = 0;

    // Collect all lines for look-ahead
    let lines: Vec<&str> = template_code.lines().collect();

    for (i, line) in lines.iter().enumerate() {
        let trimmed = line.trim();

        if trimmed.starts_with("import ") {
            imports.push_str(line);
            imports.push('\n');
        } else if trimmed.starts_with("const _hoisted_") {
            // Hoisted template variables
            hoisted.push_str(line);
            hoisted.push('\n');
        } else if trimmed.starts_with("export function render(")
            || trimmed.starts_with("function render(")
        {
            in_render = true;
            brace_depth = 0;
            brace_state = StringTrackState::default();
            paren_state = StringTrackState::default();
            brace_depth += count_braces_with_state(line, &mut brace_state);
        } else if in_render {
            brace_depth += count_braces_with_state(line, &mut brace_state);

            // Extract the return statement inside the render function (may span multiple lines)
            if in_return {
                // Continue collecting return body
                render_body.push('\n');
                render_body.push_str(line);
                return_paren_depth += count_parens_with_state(line, &mut paren_state);

                // Check if return statement is complete:
                // - Parentheses must be balanced (return_paren_depth <= 0)
                // - Next non-empty line must NOT be a ternary continuation (? or :)
                if return_paren_depth <= 0 {
                    // Look ahead to check for ternary continuation
                    let next_continues_ternary = lines
                        .iter()
                        .skip(i + 1)
                        .map(|l| l.trim())
                        .find(|l| !l.is_empty())
                        .map(|l| l.starts_with('?') || l.starts_with(':'))
                        .unwrap_or(false);

                    if !next_continues_ternary {
                        in_return = false;
                        // Remove trailing semicolon if present
                        let trimmed_body = render_body.trim_end();
                        if let Some(stripped) = trimmed_body.strip_suffix(';') {
                            render_body = stripped.to_string();
                        }
                    }
                }
            } else if let Some(stripped) = trimmed.strip_prefix("return ") {
                render_body = stripped.to_string();
                // Count parentheses to handle multi-line return (string-aware)
                paren_state = StringTrackState::default();
                return_paren_depth = count_parens_with_state(stripped, &mut paren_state);
                if return_paren_depth > 0 {
                    in_return = true;
                } else {
                    // Check if next non-empty line is a ternary continuation
                    let next_continues_ternary = lines
                        .iter()
                        .skip(i + 1)
                        .map(|l| l.trim())
                        .find(|l| !l.is_empty())
                        .map(|l| l.starts_with('?') || l.starts_with(':'))
                        .unwrap_or(false);

                    if next_continues_ternary {
                        in_return = true;
                    } else {
                        // Single line return - remove trailing semicolon if present
                        if render_body.ends_with(';') {
                            render_body.pop();
                        }
                    }
                }
            } else if trimmed.starts_with("const _component_")
                || trimmed.starts_with("const _directive_")
            {
                // Component/directive resolution statements go in preamble
                preamble.push_str(trimmed);
                preamble.push('\n');
            }

            if brace_depth == 0 {
                in_render = false;
            }
        }
    }

    // Compact the render body to remove unnecessary line breaks inside function calls
    let compacted = compact_render_body(&render_body);

    (imports, hoisted, preamble, compacted)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_add_scope_id_to_template() {
        let input = r#"const t0 = _template("<div class='container'>Hello</div>")"#;
        let result = add_scope_id_to_template(input, "data-v-abc123");
        assert!(result.contains("data-v-abc123"));
    }

    // --- count_braces_outside_strings tests ---

    #[test]
    fn test_count_braces_normal() {
        assert_eq!(count_braces_outside_strings("{ a: 1 }"), 0);
        assert_eq!(count_braces_outside_strings("{"), 1);
        assert_eq!(count_braces_outside_strings("}"), -1);
        assert_eq!(count_braces_outside_strings("{ { }"), 1);
    }

    #[test]
    fn test_count_braces_ignores_string_braces() {
        assert_eq!(
            count_braces_outside_strings("_toDisplayString(isArray.value ? ']' : '}')"),
            0
        );
        assert_eq!(count_braces_outside_strings(r#"var x = "{";"#), 0);
        assert_eq!(count_braces_outside_strings("var x = `{`;"), 0);
    }

    #[test]
    fn test_count_braces_mixed_string_and_code() {
        assert_eq!(count_braces_outside_strings("if (x) { var s = '}'"), 1);
    }

    #[test]
    fn test_count_braces_escaped_quotes() {
        assert_eq!(count_braces_outside_strings(r"var x = '\'' + '}'"), 0);
    }

    #[test]
    fn test_extract_template_parts_full_brace_in_string() {
        let template_code = r#"import { toDisplayString as _toDisplayString } from 'vue'

export function render(_ctx, _cache) {
  return _toDisplayString(isArray.value ? ']' : '}')
}"#;

        let (imports, _hoisted, render_fn) = extract_template_parts_full(template_code);

        assert!(imports.contains("import"));
        assert!(
            render_fn.contains("_toDisplayString"),
            "Render function was truncated. Got:\n{}",
            render_fn
        );
        let trimmed = render_fn.trim();
        assert!(
            trimmed.ends_with('}'),
            "Render function should end with closing brace. Got:\n{}",
            render_fn
        );
    }

    #[test]
    fn test_extract_template_parts_basic() {
        let template_code = r#"import { createVNode as _createVNode } from 'vue'

const _hoisted_1 = { class: "test" }

export function render(_ctx, _cache) {
  return _createVNode("div", _hoisted_1, "Hello")
}"#;

        let (imports, hoisted, _preamble, render_body) = extract_template_parts(template_code);

        assert!(imports.contains("import"));
        assert!(hoisted.contains("_hoisted_1"));
        assert!(render_body.contains("_createVNode"));
    }

    // --- Multiline template literal tests ---

    #[test]
    fn test_count_braces_multiline_template_literal() {
        // Simulate processing lines of a multiline template literal
        // from actual codegen output
        let mut state = StringTrackState::default();

        // Line with } closing object, then opening a template literal with ${
        // Content: }, _toDisplayString(`${t("key")}: v${ver.major}.${
        // Using concat! to avoid raw string delimiter issues
        let line1 = concat!(r#"}, _toDisplayString(`${t("key")}: v${ver.major}.${"#);
        let count1 = count_braces_with_state(line1, &mut state);
        // The } before _toDisplayString is a code brace (-1)
        // Template literal opens with `, then ${...} are template expression syntax
        assert_eq!(count1, -1, "Line 1 brace count");
        // After ${t("key")} closes and ${ver.major} closes, we enter ${ again
        // The line ends inside ${ expression inside template literal
        assert!(
            !state.template_expr_brace_stack.is_empty(),
            "Should be inside template expression after line 1"
        );

        // Line inside template expression (just expression content)
        let line2 = "            ver.minor";
        let count2 = count_braces_with_state(line2, &mut state);
        assert_eq!(count2, 0, "Line 2 brace count");

        // Line with } closing ${...} expression, ` closing template literal
        let line3 = r##"          }`) + "\n      ", 1 /* TEXT */)))"##;
        let count3 = count_braces_with_state(line3, &mut state);
        // The } closes ${...} (not a code brace), ` closes template literal
        assert_eq!(count3, 0, "Line 3 brace count");
        assert!(!state.in_string, "Should be outside string after line 3");
        assert!(
            state.template_expr_brace_stack.is_empty(),
            "Template expression stack should be empty"
        );

        // Total brace change should be -1 (only the } before _toDisplayString)
        assert_eq!(count1 + count2 + count3, -1);
    }

    #[test]
    fn test_extract_template_parts_multiline_template_literal() {
        // This is the actual pattern that was causing the bug:
        // a render function with a multiline template literal inside ${}
        let template_code = r#"import { openBlock as _openBlock, createElementBlock as _createElementBlock, toDisplayString as _toDisplayString, createCommentVNode as _createCommentVNode } from "vue"

export function render(_ctx, _cache, $props, $setup, $data, $options) {
  return (show.value)
    ? (_openBlock(), _createElementBlock("div", {
      key: 0,
      class: "outer"
    }, [
      _createElementVNode("div", { class: "inner" }, [
        (ver.value)
          ? (_openBlock(), _createElementBlock("span", { key: 0 }, "\n        " + _toDisplayString(`${t("key")}: v${ver.value.major}.${
            ver.value.minor
          }`) + "\n      ", 1 /* TEXT */))
          : (_openBlock(), _createElementBlock("span", { key: 1 }, "no"))
      ])
    ]))
    : _createCommentVNode("v-if", true)
}"#;

        let (_imports, _hoisted, _preamble, render_body) = extract_template_parts(template_code);

        // The render body must contain both v-if branches and the comment node
        assert!(
            render_body.contains("_toDisplayString"),
            "Should contain the template literal expression. Got:\n{}",
            render_body
        );
        assert!(
            render_body.contains("_createCommentVNode"),
            "Should contain the v-if comment node (else branch). Got:\n{}",
            render_body
        );
        assert!(
            render_body.contains("\"no\""),
            "Should contain the v-else branch content. Got:\n{}",
            render_body
        );
    }

    #[test]
    fn test_extract_template_parts_full_multiline_template_literal() {
        let template_code = r#"import { toDisplayString as _toDisplayString } from 'vue'

export function render(_ctx, _cache) {
  return _toDisplayString(`${t("key")}: v${ver.major}.${
    ver.minor
  }`)
}"#;

        let (_imports, _hoisted, render_fn) = extract_template_parts_full(template_code);

        assert!(
            render_fn.contains("_toDisplayString"),
            "Render function should contain the expression. Got:\n{}",
            render_fn
        );
        let trimmed = render_fn.trim();
        assert!(
            trimmed.ends_with('}'),
            "Render function should end with closing brace. Got:\n{}",
            render_fn
        );
    }

    // --- Generalized template literal / string tracking tests ---

    #[test]
    fn test_count_braces_template_literal_with_nested_object() {
        // Template literal containing ${...} with nested object literal: `${fn({a: 1})}`
        let mut state = StringTrackState::default();
        let line = r#"x = `result: ${fn({a: 1, b: {c: 2}})}`"#;
        let count = count_braces_with_state(line, &mut state);
        // No code-level braces; all { and } are inside template literal expressions
        assert_eq!(
            count, 0,
            "Braces inside template expression should be balanced"
        );
        assert!(!state.in_string, "Template literal should be closed");
    }

    #[test]
    fn test_count_braces_nested_template_literals() {
        // Nested template literals: `outer ${`inner ${x}`} end`
        let mut state = StringTrackState::default();
        let line = r#"x = `outer ${`inner ${x}`} end`"#;
        let count = count_braces_with_state(line, &mut state);
        assert_eq!(
            count, 0,
            "Nested template literals should not affect brace count"
        );
        assert!(!state.in_string, "All template literals should be closed");
    }

    #[test]
    fn test_count_braces_multiline_template_expr_with_object() {
        // Multiline: template expression contains an object literal spanning lines
        let mut state = StringTrackState::default();

        // `value: ${fn({`  — the { after fn( is a real code brace inside the expr
        let line1 = r#"x = `value: ${fn({"#;
        let c1 = count_braces_with_state(line1, &mut state);
        assert_eq!(
            c1, 1,
            "Line 1: object literal brace inside template expression"
        );

        let line2 = r#"  key: val"#;
        let c2 = count_braces_with_state(line2, &mut state);
        assert_eq!(c2, 0, "Line 2: no braces");

        // })}` — the first } closes the object (-1), the second } closes ${...} (not counted)
        let line3 = r#"})}`"#;
        let c3 = count_braces_with_state(line3, &mut state);
        assert_eq!(c3, -1, "Line 3: closing object brace");
        assert!(!state.in_string, "Template literal should be closed");
        assert_eq!(c1 + c2 + c3, 0, "Total should be balanced");
    }

    #[test]
    fn test_count_braces_template_literal_with_arrow_function() {
        // Template expression with arrow function: `${items.map(x => ({ name: x })).join()}`
        let mut state = StringTrackState::default();
        let line = r#"x = `${items.map(x => ({ name: x })).join()}`"#;
        let count = count_braces_with_state(line, &mut state);
        assert_eq!(count, 0);
        assert!(!state.in_string);
    }

    #[test]
    fn test_count_braces_state_across_many_lines() {
        // Simulate a real render function with multiline template literal
        let mut state = StringTrackState::default();

        // function render() {
        let c1 = count_braces_with_state("function render() {", &mut state);
        assert_eq!(c1, 1);

        //   return _toDisplayString(`${fn({
        // The { after fn( is a code brace inside the template expression (+1)
        let c2 = count_braces_with_state(r#"  return _toDisplayString(`${fn({"#, &mut state);
        assert_eq!(c2, 1, "Object literal brace inside template expression");

        //     key: val,
        let c3 = count_braces_with_state("    key: val,", &mut state);
        assert_eq!(c3, 0);

        //     nested: {
        // This { is a nested code brace inside the template expression
        let c4 = count_braces_with_state("    nested: {", &mut state);
        assert_eq!(c4, 1, "Nested brace inside template expression");

        //       deep: true
        let c5 = count_braces_with_state("      deep: true", &mut state);
        assert_eq!(c5, 0);

        //     }
        // This } closes the nested object brace
        let c6 = count_braces_with_state("    }", &mut state);
        assert_eq!(c6, -1, "Closing nested brace inside template expression");

        //   })}`)
        // The first } closes the outer object (-1), the second } closes ${...} (not counted)
        let c7 = count_braces_with_state(r#"  })}`)"#, &mut state);
        assert_eq!(c7, -1, "Closing outer object brace");

        // }
        let c8 = count_braces_with_state("}", &mut state);
        assert_eq!(c8, -1);

        assert_eq!(
            c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8,
            0,
            "Total: function opens and closes"
        );
        assert!(!state.in_string);
        assert!(state.template_expr_brace_stack.is_empty());
    }

    #[test]
    fn test_count_braces_regular_strings_with_braces() {
        // Single and double quoted strings with braces should still work
        let mut state = StringTrackState::default();

        let line = r#"if (x) { var s = "}" + '{' }"#;
        let count = count_braces_with_state(line, &mut state);
        assert_eq!(count, 0, "Braces inside regular strings should be ignored");
    }

    #[test]
    fn test_extract_template_parts_deeply_nested_multiline() {
        // Multiple levels of nesting with multiline template literals
        let template_code = r#"import { toDisplayString as _toDisplayString, createElementBlock as _createElementBlock, openBlock as _openBlock, createCommentVNode as _createCommentVNode, createElementVNode as _createElementVNode } from "vue"

export function render(_ctx, _cache, $props, $setup, $data, $options) {
  return (cond.value)
    ? (_openBlock(), _createElementBlock("div", { key: 0 }, [
        _createElementVNode("p", null, _toDisplayString(`${items.value.map(x => ({
          name: x.name,
          label: `${x.prefix}-${
            x.suffix
          }`
        })).length} items`)),
        _createElementVNode("span", null, "after")
      ]))
    : _createCommentVNode("v-if", true)
}"#;

        let (_imports, _hoisted, _preamble, render_body) = extract_template_parts(template_code);

        assert!(
            render_body.contains("_createCommentVNode"),
            "Should contain comment node (else branch). Got:\n{}",
            render_body
        );
        assert!(
            render_body.contains("\"after\""),
            "Should contain content after template literal. Got:\n{}",
            render_body
        );
    }

    #[test]
    fn test_extract_template_parts_full_deeply_nested_multiline() {
        // Same deep nesting test for extract_template_parts_full
        let template_code = r#"import { toDisplayString as _toDisplayString } from "vue"

export function render(_ctx, _cache) {
  return _toDisplayString(`${items.map(x => ({
    name: x.name,
    value: `nested-${
      x.value
    }`
  })).length} items`)
}"#;

        let (_imports, _hoisted, render_fn) = extract_template_parts_full(template_code);

        let trimmed = render_fn.trim();
        assert!(
            trimmed.ends_with('}'),
            "Render function should end with closing brace. Got:\n{}",
            render_fn
        );
        assert!(
            render_fn.contains("items"),
            "Render function should contain the full expression. Got:\n{}",
            render_fn
        );
    }
}