vize_atelier_sfc 0.34.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
//! Script compile context.
//!
//! Holds all state during script compilation.
//! Uses OXC for proper AST-based parsing instead of regex.

mod external_types;
mod helpers;
mod parse;
mod props;

use crate::types::{BindingMetadata, BindingType};
use vize_carton::{CompactString, String, ToCompactString};
use vize_croquis::analysis::Croquis;
use vize_croquis::macros::{EmitDefinition, ModelDefinition, PropDefinition};

use super::ScriptSetupMacros;

/// Script compile context - holds all state during compilation
#[derive(Debug)]
pub struct ScriptCompileContext {
    /// Source content
    pub source: String,

    /// Binding metadata
    pub bindings: BindingMetadata,

    /// Extracted macros
    pub macros: ScriptSetupMacros,

    /// Whether defineProps was called
    pub has_define_props_call: bool,

    /// Whether defineEmits was called
    pub has_define_emits_call: bool,

    /// Whether defineExpose was called
    pub has_define_expose_call: bool,

    /// Whether defineOptions was called
    pub has_define_options_call: bool,

    /// Whether defineSlots was called
    pub has_define_slots_call: bool,

    /// Whether defineModel was called
    pub has_define_model_call: bool,

    // --- Emits related fields ---
    /// Runtime declaration for emits (the argument passed to defineEmits)
    pub emits_runtime_decl: Option<String>,

    /// Type declaration for emits (the type parameter)
    pub emits_type_decl: Option<String>,

    /// The variable name emits is assigned to (e.g., "emit")
    pub emit_decl_id: Option<String>,

    /// TypeScript interface definitions (name -> body)
    /// Used to resolve type references in defineProps<InterfaceName>()
    pub interfaces: vize_carton::FxHashMap<String, String>,

    /// TypeScript type alias definitions (name -> body)
    /// Used to resolve type references in defineProps<TypeName>()
    pub type_aliases: vize_carton::FxHashMap<String, String>,
}

impl ScriptCompileContext {
    /// Create a new context
    pub fn new(source: &str) -> Self {
        Self {
            source: source.to_compact_string(),
            bindings: BindingMetadata::default(),
            macros: ScriptSetupMacros::default(),
            has_define_props_call: false,
            has_define_emits_call: false,
            has_define_expose_call: false,
            has_define_options_call: false,
            has_define_slots_call: false,
            has_define_model_call: false,
            emits_runtime_decl: None,
            emits_type_decl: None,
            emit_decl_id: None,
            interfaces: vize_carton::FxHashMap::default(),
            type_aliases: vize_carton::FxHashMap::default(),
        }
    }

    /// Analyze script setup and extract bindings
    pub fn analyze(&mut self) {
        // Temporarily take ownership of source to avoid borrow conflicts
        let source = std::mem::take(&mut self.source);
        self.parse_with_oxc(&source);
        self.source = source;
        // ScriptCompileContext is always used for <script setup>
        self.bindings.is_script_setup = true;
    }

    /// Convert to an Croquis for use in transforms and linting.
    ///
    /// This bridges the atelier script context to the shared croquis analysis format.
    pub fn to_analysis_summary(&self) -> Croquis {
        let mut summary = Croquis::new();

        // Convert bindings
        summary.bindings.is_script_setup = true;
        for (name, binding_type) in &self.bindings.bindings {
            summary.bindings.add(name.as_str(), *binding_type);
        }

        // Convert props aliases
        for (local, key) in &self.bindings.props_aliases {
            summary
                .bindings
                .props_aliases
                .insert(CompactString::new(local), CompactString::new(key));
        }

        // Convert props from macros
        if let Some(ref props_call) = self.macros.define_props {
            for (name, binding_type) in &self.bindings.bindings {
                if matches!(binding_type, BindingType::Props) {
                    summary.macros.add_prop(PropDefinition {
                        name: CompactString::new(name),
                        required: false, // We don't track this in the current implementation
                        prop_type: None,
                        default_value: props_call.binding_name.clone().map(CompactString::new),
                    });
                }
            }
        }

        // Convert emits
        if let Some(ref emits_call) = self.macros.define_emits {
            // Parse emits from the macro call args if available
            let trimmed = emits_call.args.trim();
            if trimmed.starts_with('[') && trimmed.ends_with(']') {
                // Array syntax: ['click', 'update']
                let inner = &trimmed[1..trimmed.len() - 1];
                for part in inner.split(',') {
                    let part = part.trim();
                    if (part.starts_with('\'') && part.ends_with('\''))
                        || (part.starts_with('"') && part.ends_with('"'))
                    {
                        let name = &part[1..part.len() - 1];
                        summary.macros.add_emit(EmitDefinition {
                            name: CompactString::new(name),
                            payload_type: None,
                        });
                    }
                }
            }
        }

        // Convert models
        for model_call in &self.macros.define_models {
            if let Some(ref binding_name) = model_call.binding_name {
                // Extract model name from args if present
                let args = model_call.args.trim();
                let name = if args.starts_with('\'') || args.starts_with('"') {
                    let quote = args.as_bytes()[0];
                    if let Some(end) = args[1..].find(|c: char| c as u8 == quote) {
                        CompactString::new(&args[1..=end])
                    } else {
                        CompactString::new("modelValue")
                    }
                } else {
                    CompactString::new("modelValue")
                };

                summary.macros.add_model(ModelDefinition {
                    name: name.clone(),
                    local_name: CompactString::new(binding_name),
                    model_type: None,
                    required: false,
                    default_value: None,
                });
            }
        }

        summary
    }

    /// Extract all macros from the source
    pub fn extract_all_macros(&mut self) {
        let source = std::mem::take(&mut self.source);
        self.parse_with_oxc(&source);
        self.source = source;
    }
}

#[cfg(test)]
mod tests {
    use super::ScriptCompileContext;
    use crate::types::BindingType;
    use vize_carton::ToCompactString;

    #[test]
    fn test_context_analyze() {
        let content = r#"
const msg = ref('hello')
const count = ref(0)
let name = 'world'
const double = computed(() => count.value * 2)
function increment() { count.value++ }
"#;
        let mut ctx = ScriptCompileContext::new(content);
        ctx.analyze();

        assert_eq!(
            ctx.bindings.bindings.get("msg"),
            Some(&BindingType::SetupRef)
        );
        assert_eq!(
            ctx.bindings.bindings.get("count"),
            Some(&BindingType::SetupRef)
        );
        assert_eq!(
            ctx.bindings.bindings.get("name"),
            Some(&BindingType::SetupLet)
        );
        assert_eq!(
            ctx.bindings.bindings.get("increment"),
            Some(&BindingType::SetupConst)
        );
    }

    #[test]
    fn test_extract_define_props_typed() {
        let content = r#"const props = defineProps<{ msg: string }>()"#;
        let mut ctx = ScriptCompileContext::new(content);
        ctx.analyze();

        assert!(ctx.has_define_props_call);
        assert!(ctx.macros.define_props.is_some());
        let props_call = ctx.macros.define_props.unwrap();
        assert_eq!(
            props_call.type_args,
            Some("{ msg: string }".to_compact_string())
        );
    }

    #[test]
    fn test_extract_define_emits_typed() {
        let content = r#"const emit = defineEmits<{ (e: 'click'): void }>()"#;
        let mut ctx = ScriptCompileContext::new(content);
        ctx.analyze();

        assert!(ctx.has_define_emits_call);
        assert!(ctx.macros.define_emits.is_some());
    }

    #[test]
    fn test_extract_with_defaults() {
        let content =
            r#"const props = withDefaults(defineProps<{ msg?: string }>(), { msg: 'hello' })"#;
        let mut ctx = ScriptCompileContext::new(content);
        ctx.analyze();

        assert!(ctx.has_define_props_call);
        assert!(ctx.macros.with_defaults.is_some());
    }

    #[test]
    fn test_props_destructure() {
        let content = r#"const { foo, bar } = defineProps<{ foo: string, bar: number }>()"#;
        let mut ctx = ScriptCompileContext::new(content);
        ctx.analyze();

        assert!(ctx.macros.props_destructure.is_some());
        let destructure = ctx.macros.props_destructure.as_ref().unwrap();
        assert_eq!(destructure.bindings.len(), 2);
        assert!(destructure.bindings.contains_key("foo"));
        assert!(destructure.bindings.contains_key("bar"));
    }

    #[test]
    fn test_props_destructure_with_alias() {
        let content =
            r#"const { foo: myFoo, bar = 123 } = defineProps<{ foo: string, bar?: number }>()"#;
        let mut ctx = ScriptCompileContext::new(content);
        ctx.analyze();

        assert!(ctx.macros.props_destructure.is_some());
        let destructure = ctx.macros.props_destructure.as_ref().unwrap();

        // Check that bindings use the key as the map key
        assert!(destructure.bindings.contains_key("foo"));
        assert!(destructure.bindings.contains_key("bar"));

        // Check local names
        assert_eq!(destructure.bindings.get("foo").unwrap().local, "myFoo");
        assert_eq!(destructure.bindings.get("bar").unwrap().local, "bar");

        // Check default value
        assert!(destructure.bindings.get("bar").unwrap().default.is_some());
    }

    #[test]
    fn test_define_props_with_interface_reference() {
        let content = r#"
interface Props {
    msg: string
    count?: number
}
const props = defineProps<Props>()
"#;
        let mut ctx = ScriptCompileContext::new(content);
        ctx.analyze();

        // Check interface was captured
        assert!(ctx.interfaces.contains_key("Props"));

        // Check props were extracted from interface
        assert!(ctx.has_define_props_call);
        assert_eq!(ctx.bindings.bindings.get("msg"), Some(&BindingType::Props));
        assert_eq!(
            ctx.bindings.bindings.get("count"),
            Some(&BindingType::Props)
        );
    }

    #[test]
    fn test_define_props_with_type_alias_reference() {
        let content = r#"
type Props = {
    foo: string
    bar: number
}
const props = defineProps<Props>()
"#;
        let mut ctx = ScriptCompileContext::new(content);
        ctx.analyze();

        // Check type alias was captured
        assert!(ctx.type_aliases.contains_key("Props"));

        // Check props were extracted from type alias
        assert!(ctx.has_define_props_call);
        assert_eq!(ctx.bindings.bindings.get("foo"), Some(&BindingType::Props));
        assert_eq!(ctx.bindings.bindings.get("bar"), Some(&BindingType::Props));
    }

    #[test]
    fn test_define_props_with_exported_type_alias() {
        let content = r#"
export type MenuItemProps = {
    id: string
    label: string
    routeName: string
    disabled?: boolean
}
const { label, disabled, routeName } = defineProps<MenuItemProps>()
"#;
        let mut ctx = ScriptCompileContext::new(content);
        ctx.analyze();

        // Check exported type alias was captured
        assert!(
            ctx.type_aliases.contains_key("MenuItemProps"),
            "export type alias should be collected"
        );

        // Check props were extracted from exported type alias
        assert!(ctx.has_define_props_call);
        assert_eq!(
            ctx.bindings.bindings.get("label"),
            Some(&BindingType::Props)
        );
        assert_eq!(
            ctx.bindings.bindings.get("disabled"),
            Some(&BindingType::Props)
        );
        assert_eq!(
            ctx.bindings.bindings.get("routeName"),
            Some(&BindingType::Props)
        );
    }

    #[test]
    fn test_define_props_with_exported_interface() {
        let content = r#"
export interface Props {
    msg: string
    count?: number
}
const props = defineProps<Props>()
"#;
        let mut ctx = ScriptCompileContext::new(content);
        ctx.analyze();

        // Check exported interface was captured
        assert!(
            ctx.interfaces.contains_key("Props"),
            "export interface should be collected"
        );

        // Check props were extracted from exported interface
        assert!(ctx.has_define_props_call);
        assert_eq!(ctx.bindings.bindings.get("msg"), Some(&BindingType::Props));
        assert_eq!(
            ctx.bindings.bindings.get("count"),
            Some(&BindingType::Props)
        );
    }

    #[test]
    fn test_with_defaults_with_interface() {
        let content = r#"
interface Props {
    msg?: string
    count?: number
}
const props = withDefaults(defineProps<Props>(), {
    msg: 'hello',
    count: 0
})
"#;
        let mut ctx = ScriptCompileContext::new(content);
        ctx.analyze();

        assert!(ctx.has_define_props_call);
        assert!(ctx.macros.with_defaults.is_some());
        assert_eq!(ctx.bindings.bindings.get("msg"), Some(&BindingType::Props));
        assert_eq!(
            ctx.bindings.bindings.get("count"),
            Some(&BindingType::Props)
        );
    }
}