vize_atelier_sfc 0.69.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
//! Props and emit type extraction utilities.
//!
//! This module handles extracting prop types from TypeScript type definitions
//! and processing withDefaults defaults.

use oxc_allocator::Allocator;
use oxc_ast::ast::{Argument, Expression, ObjectPropertyKind, PropertyKey, Statement};
use oxc_parser::Parser;
use oxc_span::{GetSpan, SourceType};
use vize_carton::FxHashMap;
use vize_carton::{String, ToCompactString};

/// Prop type information
#[derive(Debug, Clone)]
pub struct PropTypeInfo {
    /// JavaScript type constructor name (String, Number, Boolean, Array, Object, Function)
    pub js_type: String,
    /// Original TypeScript type (for PropType<T> usage)
    pub ts_type: Option<String>,
    /// Whether the prop is optional
    pub optional: bool,
    /// Whether the prop accepts null at runtime
    pub nullable: bool,
}

/// Strip TypeScript comments from source while preserving string literals.
fn strip_ts_comments(input: &str) -> String {
    let mut result = String::with_capacity(input.len());
    let bytes = input.as_bytes();
    let mut i = 0;
    let mut in_string = false;
    let mut string_char = b'"';

    while i < bytes.len() {
        if in_string {
            if bytes[i] == string_char && (i == 0 || bytes[i - 1] != b'\\') {
                in_string = false;
            }
            result.push(bytes[i] as char);
            i += 1;
            continue;
        }

        match bytes[i] {
            b'\'' | b'"' | b'`' => {
                in_string = true;
                string_char = bytes[i];
                result.push(bytes[i] as char);
                i += 1;
            }
            b'/' if i + 1 < bytes.len() && bytes[i + 1] == b'/' => {
                // Line comment: skip until newline
                while i < bytes.len() && bytes[i] != b'\n' {
                    i += 1;
                }
            }
            b'/' if i + 1 < bytes.len() && bytes[i + 1] == b'*' => {
                // Block comment: skip until */
                i += 2;
                while i + 1 < bytes.len() && !(bytes[i] == b'*' && bytes[i + 1] == b'/') {
                    i += 1;
                }
                if i + 1 < bytes.len() {
                    i += 2; // skip */
                }
            }
            _ => {
                result.push(bytes[i] as char);
                i += 1;
            }
        }
    }
    result
}

/// Join multi-line type definitions where continuation lines start with `|` or `&`.
/// For example:
/// ```text
/// type?:
///     | 'input'
///     | 'text';
/// ```
/// becomes: `type?: | 'input' | 'text';`
fn join_union_continuation_lines(input: &str) -> String {
    let lines: Vec<&str> = input.lines().collect();
    let mut result = String::with_capacity(input.len());
    for (i, line) in lines.iter().enumerate() {
        let trimmed = line.trim();
        if trimmed.starts_with('|') || trimmed.starts_with('&') {
            // Join to previous line with a space
            result.push(' ');
            result.push_str(trimmed);
        } else {
            if i > 0 {
                result.push('\n');
            }
            result.push_str(line);
        }
    }
    result
}

/// Extract prop types from TypeScript type definition.
/// Returns a Vec to preserve definition order (important for matching Vue's output).
pub fn extract_prop_types_from_type(type_args: &str) -> Vec<(String, PropTypeInfo)> {
    let mut props = Vec::new();

    // Strip comments before parsing
    let stripped = strip_ts_comments(type_args);
    // Join multi-line union/intersection types (lines starting with | or &)
    let joined = join_union_continuation_lines(&stripped);
    let content = joined.trim();
    let content = if content.starts_with('{') && content.ends_with('}') {
        &content[1..content.len() - 1]
    } else {
        content
    };

    // Split by commas/semicolons/newlines (but not inside nested braces)
    let mut depth: i32 = 0;
    let mut current = String::default();
    let chars: Vec<char> = content.chars().collect();
    let mut i = 0;

    while i < chars.len() {
        let c = chars[i];
        match c {
            '{' | '<' | '(' | '[' => {
                depth += 1;
                current.push(c);
            }
            '}' | ')' | ']' => {
                if depth > 0 {
                    depth -= 1;
                }
                current.push(c);
            }
            '>' => {
                // Don't count `>` as closing angle bracket when preceded by `=` (arrow function `=>`)
                if i > 0 && chars[i - 1] == '=' {
                    current.push(c);
                } else {
                    if depth > 0 {
                        depth -= 1;
                    }
                    current.push(c);
                }
            }
            ',' | ';' if depth <= 0 => {
                extract_prop_type_info(&current, &mut props);
                current.clear();
                depth = 0;
            }
            '\n' if depth <= 0 => {
                // Don't split on newline if the current segment ends with ':' (type on next line)
                let trimmed_current = current.trim();
                if !trimmed_current.is_empty() && !trimmed_current.ends_with(':') {
                    extract_prop_type_info(&current, &mut props);
                    current.clear();
                    depth = 0;
                }
                // If ends with ':', keep accumulating (type continues on next line)
            }
            _ => current.push(c),
        }
        i += 1;
    }
    extract_prop_type_info(&current, &mut props);

    props
}

fn extract_prop_type_info(segment: &str, props: &mut Vec<(String, PropTypeInfo)>) {
    let trimmed = segment.trim();
    if trimmed.is_empty() {
        return;
    }

    // Parse "name?: Type" or "name: Type"
    if let Some(colon_pos) = trimmed.find(':') {
        let name_part = &trimmed[..colon_pos];
        let type_part = &trimmed[colon_pos + 1..];

        let optional = name_part.ends_with('?') || type_includes_top_level_undefined(type_part);
        let nullable = type_includes_top_level_null(type_part);
        let name = name_part.trim().trim_end_matches('?').trim();

        if !name.is_empty() && is_valid_identifier(name) {
            let ts_type_str = type_part.trim().to_compact_string();
            let js_type = ts_type_to_js_type(&ts_type_str);
            // Avoid duplicates (intersection types may have overlapping props)
            if !props.iter().any(|(n, _)| n == name) {
                props.push((
                    name.to_compact_string(),
                    PropTypeInfo {
                        js_type,
                        ts_type: Some(ts_type_str),
                        optional,
                        nullable,
                    },
                ));
            }
        }
    }
}

fn type_includes_top_level_undefined(ts_type: &str) -> bool {
    split_type_at_top_level(ts_type.trim(), '|')
        .into_iter()
        .any(|part| part.trim() == "undefined")
}

fn type_includes_top_level_null(ts_type: &str) -> bool {
    split_type_at_top_level(ts_type.trim(), '|')
        .into_iter()
        .any(|part| part.trim() == "null")
}

pub fn add_null_to_runtime_type(js_type: &str, nullable: bool) -> String {
    if !nullable || js_type == "null" {
        return js_type.to_compact_string();
    }

    if js_type.starts_with('[') && js_type.ends_with(']') {
        let inner = &js_type[1..js_type.len() - 1];
        if inner
            .split(',')
            .map(|part| part.trim())
            .any(|part| part == "null")
        {
            return js_type.to_compact_string();
        }

        let mut result = String::with_capacity(js_type.len() + 6);
        result.push('[');
        result.push_str(inner);
        if !inner.trim().is_empty() {
            result.push_str(", ");
        }
        result.push_str("null");
        result.push(']');
        return result;
    }

    let mut result = String::with_capacity(js_type.len() + 8);
    result.push('[');
    result.push_str(js_type);
    result.push_str(", null]");
    result
}

/// Split a type string at a delimiter only at the top level (depth 0),
/// respecting nested `<>`, `()`, `[]`, `{}` and `=>` arrows.
fn split_type_at_top_level(s: &str, delimiter: char) -> Vec<String> {
    let mut parts = Vec::new();
    let mut current = String::default();
    let mut depth: i32 = 0;
    let chars: Vec<char> = s.chars().collect();
    let mut i = 0;

    while i < chars.len() {
        let c = chars[i];
        match c {
            '(' | '[' | '{' | '<' => {
                depth += 1;
                current.push(c);
            }
            ')' | ']' | '}' => {
                if depth > 0 {
                    depth -= 1;
                }
                current.push(c);
            }
            '>' => {
                // Don't count > as closing angle bracket when preceded by = (arrow =>)
                if i > 0 && chars[i - 1] == '=' {
                    current.push(c);
                } else {
                    if depth > 0 {
                        depth -= 1;
                    }
                    current.push(c);
                }
            }
            c2 if c2 == delimiter && depth == 0 => {
                parts.push(std::mem::take(&mut current));
            }
            _ => current.push(c),
        }
        i += 1;
    }
    if !current.is_empty() || !parts.is_empty() {
        parts.push(current);
    }
    parts
}

/// Check if a type string contains a top-level `=>` (arrow function signature).
fn contains_top_level_arrow(s: &str) -> bool {
    let mut depth: i32 = 0;
    let chars: Vec<char> = s.chars().collect();
    for i in 0..chars.len() {
        match chars[i] {
            '(' | '[' | '{' | '<' => depth += 1,
            ')' | ']' | '}' => {
                if depth > 0 {
                    depth -= 1;
                }
            }
            '>' => {
                if i > 0 && chars[i - 1] == '=' {
                    // This is `=>`
                    if depth == 0 {
                        return true;
                    }
                    // Inside nested structure — don't change depth
                } else if depth > 0 {
                    depth -= 1;
                }
            }
            _ => {}
        }
    }
    false
}

/// Convert TypeScript type to JavaScript type constructor
fn ts_type_to_js_type(ts_type: &str) -> String {
    let ts_type = ts_type.trim();

    // Strip `readonly` prefix: `readonly T[]` → `T[]`
    let ts_type = if ts_type.starts_with("readonly ") {
        ts_type.strip_prefix("readonly ").unwrap().trim()
    } else {
        ts_type
    };

    // Handle string literal types: "foo" or 'bar' -> String
    if (ts_type.starts_with('"') && ts_type.ends_with('"'))
        || (ts_type.starts_with('\'') && ts_type.ends_with('\''))
    {
        return "String".to_compact_string();
    }

    // Handle numeric literal types: 123, 1.5 -> Number
    if ts_type.parse::<f64>().is_ok() {
        return "Number".to_compact_string();
    }

    // Handle boolean literal types: true, false -> Boolean
    if ts_type == "true" || ts_type == "false" {
        return "Boolean".to_compact_string();
    }

    // Arrow function types must be detected BEFORE union splitting,
    // because `(x: T) => A | B` is a single function type (return type is `A | B`),
    // not a union of `(x: T) => A` and `B`.
    // Also must come before array/object checks because `(items: T[]) => T[]`
    // ends with `[]` and contains `:`.
    if contains_top_level_arrow(ts_type) {
        return "Function".to_compact_string();
    }

    // Handle union types — split at top level only (respecting nesting).
    // For mixed types like `string | number`, produce `[String, Number]`.
    {
        let parts = split_type_at_top_level(ts_type, '|');
        if parts.len() > 1 {
            let meaningful: Vec<&str> = parts
                .iter()
                .map(|p| p.trim())
                .filter(|p| *p != "undefined" && *p != "null")
                .collect();

            if meaningful.is_empty() {
                return "null".to_compact_string();
            }

            // Collect unique JS types for each union member
            let mut js_types: Vec<String> = Vec::new();
            for part in &meaningful {
                let jt = ts_type_to_js_type(part);
                if !js_types.contains(&jt) {
                    js_types.push(jt);
                }
            }

            if js_types.len() == 1 {
                return js_types.into_iter().next().unwrap();
            }

            // Multiple distinct types → array form: [String, Number]
            let joined = js_types.join(", ");
            let mut result = String::with_capacity(joined.len() + 2);
            result.push('[');
            result.push_str(&joined);
            result.push(']');
            return result;
        }
    }

    // Map TypeScript types to JavaScript constructors
    match ts_type.to_lowercase().as_str() {
        "string" => "String".to_compact_string(),
        "number" => "Number".to_compact_string(),
        "boolean" => "Boolean".to_compact_string(),
        "object" => "Object".to_compact_string(),
        "function" => "Function".to_compact_string(),
        "symbol" => "Symbol".to_compact_string(),
        _ => {
            // Handle array types
            if ts_type.ends_with("[]") || ts_type.starts_with("Array<") {
                "Array".to_compact_string()
            } else if ts_type.starts_with('{') || contains_top_level_colon(ts_type) {
                // Object literal type
                "Object".to_compact_string()
            } else if ts_type.starts_with('(') && ts_type.contains("=>") {
                // Function type (fallback, already handled above)
                "Function".to_compact_string()
            } else {
                // Check if this is a built-in JavaScript constructor type
                let type_name = ts_type.split('<').next().unwrap_or(ts_type).trim();
                match type_name {
                    // Built-in JavaScript types that exist at runtime
                    "Date" | "RegExp" | "Error" | "Map" | "Set" | "WeakMap" | "WeakSet"
                    | "Promise" | "ArrayBuffer" | "DataView" | "Int8Array" | "Uint8Array"
                    | "Int16Array" | "Uint16Array" | "Int32Array" | "Uint32Array"
                    | "Float32Array" | "Float64Array" | "BigInt64Array" | "BigUint64Array"
                    | "URL" | "URLSearchParams" | "FormData" | "Blob" | "File" => {
                        type_name.to_compact_string()
                    }
                    // Vue reactive types that are objects at runtime
                    "Ref"
                    | "ShallowRef"
                    | "ComputedRef"
                    | "WritableComputedRef"
                    | "MaybeRef"
                    | "MaybeRefOrGetter"
                    | "Readonly"
                    | "UnwrapRef"
                    | "Reactive"
                    | "ShallowReactive"
                    | "ToRef"
                    | "ToRefs" => "Object".to_compact_string(),
                    // User-defined interface/type or generic type parameter
                    // - Single uppercase letter (T, U, K, V) = generic param → null
                    // - Otherwise = user-defined type → null (types don't exist at runtime)
                    _ => "null".to_compact_string(),
                }
            }
        }
    }
}

/// Check if a type string contains a `:` at the top level (not inside generics/parens).
/// Used to detect object literal types like `{ key: string }` vs types like `Record<K, V>`.
fn contains_top_level_colon(s: &str) -> bool {
    let mut depth: i32 = 0;
    let chars: Vec<char> = s.chars().collect();
    for i in 0..chars.len() {
        match chars[i] {
            '(' | '[' | '{' | '<' => depth += 1,
            ')' | ']' | '}' => {
                if depth > 0 {
                    depth -= 1;
                }
            }
            '>' => {
                if i > 0 && chars[i - 1] == '=' {
                    // Arrow =>, don't change depth
                } else if depth > 0 {
                    depth -= 1;
                }
            }
            ':' if depth == 0 => return true,
            _ => {}
        }
    }
    false
}

/// Resolve prop type references using type alias/interface maps.
/// For a prop type like `ButtonVariant`, resolves it using the type_aliases and interfaces
/// to determine the correct JS type constructor.
pub fn resolve_prop_js_type(
    ts_type: &str,
    interfaces: &FxHashMap<String, String>,
    type_aliases: &FxHashMap<String, String>,
) -> Option<String> {
    let trimmed = ts_type.trim();
    // Check if it's a simple type reference (identifier, no generics/brackets/arrows/pipes)
    // that would resolve to `null` by default
    if trimmed.is_empty() {
        return None;
    }

    // First try the normal resolution
    let js_type = ts_type_to_js_type(trimmed);
    if js_type != "null" {
        return None; // Normal resolution works fine
    }

    // It resolved to null - try to look up the type name and resolve based on the actual definition
    let base_name = if let Some(idx) = trimmed.find('<') {
        trimmed[..idx].trim()
    } else {
        trimmed
    };

    // Look up in type aliases first
    if let Some(body) = type_aliases.get(base_name) {
        let resolved_type = ts_type_to_js_type(body.trim());
        if resolved_type != "null" {
            return Some(resolved_type);
        }
        // If the alias body contains braces, it's an object type
        if body.contains('{') {
            return Some("Object".to_compact_string());
        }
    }

    // Look up in interfaces
    if let Some(body) = interfaces.get(base_name) {
        // Interfaces always resolve to Object
        let _ = body;
        return Some("Object".to_compact_string());
    }

    None
}

/// Strip the `readonly` keyword from a TypeScript type.
/// Handles patterns like `readonly { value: string }[]` → `{ value: string }[]`
pub fn strip_readonly_prefix(ts_type: &str) -> &str {
    let trimmed = ts_type.trim();
    if let Some(rest) = trimmed.strip_prefix("readonly ") {
        rest.trim()
    } else {
        trimmed
    }
}

/// Extract emit names from TypeScript type definition
pub fn extract_emit_names_from_type(type_args: &str) -> Vec<String> {
    let mut emits = Vec::new();

    // First, try Vue 3.3+ shorthand format:
    //   { change: [value: string]; submit: []; update: [id: number] }
    // Property names before `:` followed by `[` are event names
    let trimmed = type_args.trim();
    let is_shorthand = trimmed.starts_with('{')
        && trimmed.contains('[')
        && !trimmed.contains("(e:")
        && !trimmed.contains("(event:");

    if is_shorthand {
        // Extract property names from { name: [...], name: [...] } format
        let inner = if trimmed.starts_with('{') && trimmed.ends_with('}') {
            &trimmed[1..trimmed.len() - 1]
        } else {
            trimmed
        };

        // Split by lines or semicolons and extract property names
        for segment in inner.split([';', '\n']) {
            let seg = segment.trim();
            if seg.is_empty() {
                continue;
            }
            // Find the property name before the first ':'
            if let Some(colon_pos) = seg.find(':') {
                let name = seg[..colon_pos].trim();
                // Remove quotes if present
                let name = name.trim_matches(|c| c == '\'' || c == '"');
                if !name.is_empty()
                    && name
                        .chars()
                        .all(|c| c.is_alphanumeric() || c == '_' || c == '-')
                {
                    emits.push(name.to_compact_string());
                }
            }
        }

        if !emits.is_empty() {
            return emits;
        }
    }

    // Fall back to call signature format:
    //   (e: 'eventName'): void; (e: 'otherEvent', value: string): void
    // Match quoted string literals in (e: 'name') patterns
    let mut in_string = false;
    let mut quote_char = ' ';
    let mut current_string = String::default();

    for c in type_args.chars() {
        if !in_string && (c == '\'' || c == '"') {
            in_string = true;
            quote_char = c;
            current_string.clear();
        } else if in_string && c == quote_char {
            in_string = false;
            if !current_string.is_empty() {
                emits.push(current_string.clone());
            }
        } else if in_string {
            current_string.push(c);
        }
    }

    emits
}

/// Extract default values from withDefaults second argument
/// Input: "withDefaults(defineProps<{...}>(), { prop1: default1, prop2: default2 })"
/// Returns: HashMap of prop name to default value string
pub fn extract_with_defaults_defaults(with_defaults_args: &str) -> FxHashMap<String, String> {
    let mut defaults = FxHashMap::default();
    let trimmed = with_defaults_args.trim();
    if trimmed.is_empty() {
        return defaults;
    }

    const WRAP_PREFIX: &str = "const __vize_defaults__ = ";
    let mut wrapped = String::with_capacity(WRAP_PREFIX.len() + trimmed.len() + 1);
    wrapped.push_str(WRAP_PREFIX);
    wrapped.push_str(trimmed);
    wrapped.push(';');

    let allocator = Allocator::default();
    let parse_result = Parser::new(
        &allocator,
        &wrapped,
        SourceType::default().with_typescript(true),
    )
    .parse();
    if !parse_result.errors.is_empty() {
        return defaults;
    }

    let Some(Statement::VariableDeclaration(var_decl)) = parse_result.program.body.first() else {
        return defaults;
    };
    let Some(declarator) = var_decl.declarations.first() else {
        return defaults;
    };
    let Some(Expression::CallExpression(call)) = declarator.init.as_ref() else {
        return defaults;
    };
    let Expression::Identifier(callee) = &call.callee else {
        return defaults;
    };
    if callee.name.as_str() != "withDefaults" {
        return defaults;
    }

    let Some(Argument::ObjectExpression(obj)) = call.arguments.get(1) else {
        return defaults;
    };

    for property in obj.properties.iter() {
        let ObjectPropertyKind::ObjectProperty(prop) = property else {
            continue;
        };

        let key = match &prop.key {
            PropertyKey::StaticIdentifier(id) => id.name.to_compact_string(),
            PropertyKey::StringLiteral(lit) => lit.value.to_compact_string(),
            PropertyKey::NumericLiteral(lit) => lit.value.to_compact_string(),
            _ => continue,
        };

        let Some(value_start) = (prop.value.span().start as usize).checked_sub(WRAP_PREFIX.len())
        else {
            continue;
        };
        let Some(value_end) = (prop.value.span().end as usize).checked_sub(WRAP_PREFIX.len())
        else {
            continue;
        };
        if let Some(value_src) = trimmed.get(value_start..value_end) {
            defaults.insert(key, value_src.to_compact_string());
        }
    }

    defaults
}

/// Check if a string is a valid JS identifier
pub fn is_valid_identifier(s: &str) -> bool {
    if s.is_empty() {
        return false;
    }

    let mut chars = s.chars();
    match chars.next() {
        Some(c) if c.is_alphabetic() || c == '_' || c == '$' => {}
        _ => return false,
    }

    chars.all(|c| c.is_alphanumeric() || c == '_' || c == '$')
}