vize_atelier_sfc 0.40.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
//! Tests for inline script compilation.

#[cfg(test)]
#[allow(clippy::module_inception)]
mod tests {
    use super::super::compiler::compile_script_setup_inline;
    use crate::compile_script::TemplateParts;
    use vize_carton::String;

    /// Helper to compile a minimal script setup and return the output code
    fn compile_setup(script_content: &str) -> String {
        let empty_template = TemplateParts {
            imports: "",
            hoisted: "",
            render_fn: "",
            render_fn_name: "",
            preamble: "",
            render_body: "null",
            render_is_block: false,
        };
        let result = compile_script_setup_inline(
            script_content,
            "TestComponent",
            false, // is_ts = false (JS output, strip TS)
            true,  // source_is_ts = true
            false, // is_vapor = false
            empty_template,
            None,
            &[], // no css_vars
            "",  // no scope_id
            None,
        )
        .expect("compilation should succeed");
        result.code
    }

    /// Helper to compile with is_ts=true (TypeScript output)
    fn compile_setup_ts(script_content: &str) -> String {
        let empty_template = TemplateParts {
            imports: "",
            hoisted: "",
            render_fn: "",
            render_fn_name: "",
            preamble: "",
            render_body: "null",
            render_is_block: false,
        };
        let result = compile_script_setup_inline(
            script_content,
            "TestComponent",
            true,  // is_ts = true (TS output)
            true,  // source_is_ts = true
            false, // is_vapor = false
            empty_template,
            None,
            &[], // no css_vars
            "",  // no scope_id
            None,
        )
        .expect("compilation should succeed");
        result.code
    }

    #[test]
    fn test_declare_global_not_in_setup_body_ts() {
        let content = r#"
import { ref } from 'vue'

const handleClick = () => {
  console.log('click')
}

declare global {
  interface Window {
    EyeDropper: any
  }
}

const x = ref(0)
"#;
        let output = compile_setup_ts(content);
        let setup_start = output.find("setup(").expect("should have setup function");
        let setup_body = &output[setup_start..];
        assert!(
            !setup_body.contains("declare global"),
            "declare global should NOT be inside setup function body. Got:\n{}",
            output
        );
    }

    #[test]
    fn test_export_type_reexport_stripped() {
        let content = r#"
import { ref } from 'vue'
import type { FilterType } from './types'

export type { FilterType }

const x = ref(0)
"#;
        let output = compile_setup(content);
        let setup_start = output.find("setup(").expect("should have setup");
        let setup_body = &output[setup_start..];
        assert!(
            !setup_body.contains("export type"),
            "export type re-export should not be inside setup body. Got:\n{}",
            output
        );
    }

    #[test]
    fn test_type_as_variable_at_line_start() {
        let content = r#"
import { ref } from 'vue'

const type = ref('material-symbols')
const identifier =
  type === 'material-symbols' ? 'name' : 'ligature'
"#;
        let output = compile_setup(content);
        assert!(
            output.contains("type ==="),
            "`type ===` continuation line should be preserved. Got:\n{}",
            output
        );
    }

    #[test]
    fn test_destructure_with_multiline_function_call() {
        let content = r#"
import { ref, toRef } from 'vue'
import { useSomething } from './useSomething'

const fileInputRef = ref()

const {
  handleSelect,
  handleChange,
} = useSomething(
  fileInputRef,
  {
    onError: (e) => console.log(e),
    onSuccess: () => console.log('ok'),
  },
  toRef(() => 'test'),
)

const other = ref(1)
"#;
        let output = compile_setup(content);
        // Function call args should be part of the destructure statement, not bare statements
        assert!(
            output.contains("useSomething("),
            "Function call should be present in destructure statement. Got:\n{}",
            output
        );
        assert!(
            output.contains("const other = ref(1)"),
            "Code after destructure should be present. Got:\n{}",
            output
        );
    }

    #[test]
    fn test_side_effect_import_without_semicolons() {
        let content = r#"
import { watch } from 'vue'
import '@/css/oldReset.scss'

const { dialogRef } = provideDialog()

watch(
  dialogRef,
  (val) => {
    console.log(val)
  },
  { immediate: true },
)
"#;
        let output = compile_setup_ts(content);
        assert!(
            output.contains("watch("),
            "watch() call should be in setup body. Got:\n{}",
            output
        );
    }

    #[test]
    fn test_multiline_standalone_await_preserves_object_literal() {
        let content = r#"
const client = useClient()

await client.reports.create({
  accountId: 'acc',
  message: 'hello',
})
"#;
        let output = compile_setup_ts(content);
        assert!(
            output.contains("_withAsyncContext(() => client.reports.create({"),
            "multiline await should keep the full call expression. Got:\n{}",
            output
        );
        assert!(
            output.contains("accountId: 'acc'") && output.contains("message: 'hello'"),
            "object literal fields should remain intact. Got:\n{}",
            output
        );
        assert!(
            !output.contains("create({))"),
            "await transform must not truncate the object literal. Got:\n{}",
            output
        );
    }

    #[test]
    fn test_multiline_await_assignment_preserves_initializer() {
        let content = r#"
const response = await fetch('/api/report', {
  method: 'POST',
  body: JSON.stringify({ ok: true }),
})
"#;
        let output = compile_setup_ts(content);
        assert!(
            output.contains("const response =")
                && output.contains("_withAsyncContext(() => fetch('/api/report', {"),
            "await assignment should wrap the whole initializer. Got:\n{}",
            output
        );
        assert!(
            output.contains("method: 'POST'")
                && output.contains("body: JSON.stringify({ ok: true })"),
            "initializer object literal should remain intact. Got:\n{}",
            output
        );
        assert!(
            !output.contains("fetch('/api/report', {))"),
            "await assignment must not truncate multiline initializers. Got:\n{}",
            output
        );
    }

    #[test]
    fn test_export_type_with_arrow_function_member() {
        let content = r#"
import { computed } from 'vue'
import { useRoute } from 'vue-router'

export type MenuSelectorOption = {
  label: string
  onClick: () => void
}

const route = useRoute()
const heading = computed(() => route.name)
"#;
        let output = compile_setup_ts(content);
        assert!(
            output.contains("export type MenuSelectorOption"),
            "export type should be at module level. Got:\n{}",
            output
        );
        let setup_start = output.find("setup(").expect("should have setup");
        let setup_body = &output[setup_start..];
        assert!(
            setup_body.contains("const route = useRoute()"),
            "const route should be inside setup body. Got:\n{}",
            output
        );
    }

    #[test]
    fn test_define_props_with_trailing_semicolon() {
        // Semicolons at end of defineProps() should not prevent macro detection
        let content = r#"
import { ref } from 'vue'

interface Props {
    msg: string
}

const { msg } = defineProps<Props>();
const count = ref(0)
"#;
        let output = compile_setup(content);
        assert!(
            output.contains("props: {"),
            "should generate props declaration even with trailing semicolon. Got:\n{}",
            output
        );
        assert!(
            output.contains("msg:"),
            "should include msg prop. Got:\n{}",
            output
        );
        assert!(
            output.contains("const count = ref(0)"),
            "code after defineProps should be present. Got:\n{}",
            output
        );
    }

    #[test]
    fn test_multiline_define_props_with_trailing_semicolon() {
        // Multi-line defineProps with trailing semicolon on closing line
        let content = r#"
import { ref } from 'vue'

const { label, disabled } = defineProps<{
    label: string
    disabled?: boolean
}>();
const x = ref(1)
"#;
        let output = compile_setup(content);
        assert!(
            output.contains("props: {"),
            "should generate props declaration for multiline defineProps with semicolon. Got:\n{}",
            output
        );
        assert!(
            output.contains("const x = ref(1)"),
            "code after defineProps should be present. Got:\n{}",
            output
        );
    }

    #[test]
    fn test_with_defaults_trailing_semicolon() {
        // withDefaults with trailing semicolon
        let content = r#"
import { ref } from 'vue'

interface Props {
    msg: string
    count?: number
}

const { msg, count } = withDefaults(defineProps<Props>(), {
    count: 0,
});
const x = ref(1)
"#;
        let output = compile_setup(content);
        assert!(
            output.contains("props: {"),
            "should generate props declaration for withDefaults with semicolon. Got:\n{}",
            output
        );
        assert!(
            output.contains("const x = ref(1)"),
            "code after withDefaults should be present. Got:\n{}",
            output
        );
    }

    /// Helper to compile with no template (empty render_body)
    fn compile_setup_no_template(script_content: &str) -> String {
        let empty_template = TemplateParts {
            imports: "",
            hoisted: "",
            render_fn: "",
            render_fn_name: "",
            preamble: "",
            render_body: "",
            render_is_block: false,
        };
        let result = compile_script_setup_inline(
            script_content,
            "TestComponent",
            false,
            true,
            false,
            empty_template,
            None,
            &[], // no css_vars
            "",  // no scope_id
            None,
        )
        .expect("compilation should succeed");
        result.code
    }

    #[test]
    fn test_no_template_returns_setup_bindings() {
        // When there's no template, setup bindings should be returned as an object
        let content = r#"
import { ref, computed } from 'vue'

const count = ref(0)
const doubled = computed(() => count.value * 2)
"#;
        let output = compile_setup_no_template(content);
        assert!(
            output.contains("return {"),
            "no-template case should return setup bindings. Got:\n{}",
            output
        );
        assert!(
            output.contains("count"),
            "should return count binding. Got:\n{}",
            output
        );
        assert!(
            output.contains("doubled"),
            "should return doubled binding. Got:\n{}",
            output
        );
    }

    #[test]
    fn test_no_template_returns_imported_bindings() {
        // Imported bindings should also be returned for runtime template compilation
        let content = r#"
import { onMounted } from 'vue'

onMounted(() => {
    console.log('mounted')
})
"#;
        let output = compile_setup_no_template(content);
        assert!(
            output.contains("return {") && output.contains("onMounted"),
            "no-template case should return imported bindings too (for runtime template access). Got:\n{}",
            output
        );
    }

    #[test]
    fn test_export_type_generates_props_declaration() {
        let content = r#"
export type MenuItemProps = {
    id: string
    label: string
    routeName: string
    disabled?: boolean
}
const { label, disabled, routeName } = defineProps<MenuItemProps>()
"#;
        let output = compile_setup(content);
        assert!(
            output.contains("props: {"),
            "should generate props declaration for export type. Got:\n{}",
            output
        );
        assert!(
            output.contains("label:") && output.contains("String"),
            "should include label prop. Got:\n{}",
            output
        );
        assert!(
            output.contains("routeName:") && output.contains("String"),
            "should include routeName prop. Got:\n{}",
            output
        );
        assert!(
            output.contains("disabled:"),
            "should include disabled prop. Got:\n{}",
            output
        );
    }

    #[test]
    fn test_define_props_destructure_value_on_next_line() {
        // Pattern: const { ... } =\n  defineProps<...>()
        // The destructure pattern is complete on line 1, but defineProps is on line 2.
        let content = r#"
import { computed } from 'vue'

interface TimetableCell {
    type: string
    title: string
    startTime: string
}

const { type, title, startTime } =
  defineProps<TimetableCell>();
const accentColor = computed(() => type === 'event' ? 'primary' : 'secondary')
"#;
        let output = compile_setup(content);
        assert!(
            output.contains("props: {"),
            "should generate props declaration for next-line defineProps. Got:\n{}",
            output
        );
        assert!(
            output.contains("type:") && output.contains("String"),
            "should include type prop. Got:\n{}",
            output
        );
        assert!(
            output.contains("title:") && output.contains("String"),
            "should include title prop. Got:\n{}",
            output
        );
        // Verify props destructure references are transformed correctly in setup body
        let setup_start = output.find("setup(").expect("should have setup");
        let setup_body = &output[setup_start..];
        assert!(
            setup_body.contains("__props.type"),
            "destructured prop 'type' should be rewritten to __props.type in setup body. Got:\n{}",
            output
        );
        assert!(
            !setup_body.contains("const { __props."),
            "destructure declaration should NOT appear in setup body. Got:\n{}",
            output
        );
    }

    #[test]
    fn test_define_props_destructure_value_on_next_line_with_semicolon() {
        // Same pattern with trailing semicolon
        let content = r#"
import { ref } from 'vue'

interface Props {
    msg: string
    count: number
}

const { msg, count } =
  defineProps<Props>();
const doubled = ref(count * 2)
"#;
        let output = compile_setup(content);
        assert!(
            output.contains("props: {"),
            "should generate props declaration. Got:\n{}",
            output
        );
        assert!(
            output.contains("msg:") && output.contains("String"),
            "should include msg prop. Got:\n{}",
            output
        );
    }

    #[test]
    fn test_multiline_conditional_type_ts() {
        // Multi-line conditional type with ? and : continuation markers
        let content = r#"
import { computed } from 'vue'

type KeyOfUnion<T> = T extends T ? keyof T : never;
type DistributiveOmit<T, K extends KeyOfUnion<T>> = T extends T
	? Omit<T, K>
	: never;

const x = computed(() => 1)
"#;
        let output = compile_setup_ts(content);
        // The full conditional type should be at module level, not in setup body
        assert!(
            output.contains("type DistributiveOmit"),
            "Conditional type should be in output. Got:\n{}",
            output
        );
        assert!(
            output.contains("? Omit<T, K>"),
            "Conditional type true branch should be in type declaration. Got:\n{}",
            output
        );
        assert!(
            output.contains(": never;"),
            "Conditional type false branch should be in type declaration. Got:\n{}",
            output
        );
        let setup_start = output.find("setup(").expect("should have setup");
        let setup_body = &output[setup_start..];
        assert!(
            !setup_body.contains("? Omit<T, K>"),
            "Conditional type branches should NOT be in setup body. Got:\n{}",
            output
        );
        assert!(
            setup_body.contains("const x = computed(() => 1)"),
            "Code after type should be in setup body. Got:\n{}",
            output
        );
    }

    #[test]
    fn test_non_props_destructure_value_on_next_line() {
        // Ensure regular (non-defineProps) destructures with value on next line
        // still work correctly
        let content = r#"
import { ref, toRefs } from 'vue'

const state = ref({ x: 1, y: 2 })
const { x, y } =
  toRefs(state.value)
const sum = ref(x.value + y.value)
"#;
        let output = compile_setup(content);
        assert!(
            output.contains("toRefs("),
            "non-props destructure should be preserved in setup body. Got:\n{}",
            output
        );
        assert!(
            output.contains("const sum = ref("),
            "code after destructure should be present. Got:\n{}",
            output
        );
    }
}