vize_atelier_sfc 0.48.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
//! 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);
        insta::assert_snapshot!(output.as_str());
    }

    #[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);
        insta::assert_snapshot!(output.as_str());
    }

    #[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);
        insta::assert_snapshot!(output.as_str());
    }

    #[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);
        insta::assert_snapshot!(output.as_str());
    }

    #[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);
        insta::assert_snapshot!(output.as_str());
    }

    #[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);
        insta::assert_snapshot!(output.as_str());
    }

    #[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);
        insta::assert_snapshot!(output.as_str());
    }

    #[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);
        insta::assert_snapshot!(output.as_str());
    }

    #[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);
        insta::assert_snapshot!(output.as_str());
    }

    #[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);
        insta::assert_snapshot!(output.as_str());
    }

    #[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);
        insta::assert_snapshot!(output.as_str());
    }

    /// 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);
        insta::assert_snapshot!(output.as_str());
    }

    #[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);
        insta::assert_snapshot!(output.as_str());
    }

    #[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);
        insta::assert_snapshot!(output.as_str());
    }

    #[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);
        insta::assert_snapshot!(output.as_str());
    }

    #[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);
        insta::assert_snapshot!(output.as_str());
    }

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

interface Props {
    msg: string
    count: number
}

const props =
  defineProps<Props>();
const doubled = computed(() => props.count * 2)
"#;
        let output = compile_setup(content);
        insta::assert_snapshot!(output.as_str());
    }

    #[test]
    fn test_define_slots_assignment_value_on_next_line() {
        let content = r#"
const slots =
  defineSlots<{
    default?: () => string
  }>();

const hasDefault = !!slots.default
"#;
        let output = compile_setup(content);
        insta::assert_snapshot!(output.as_str());
    }

    #[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);
        insta::assert_snapshot!(output.as_str());
    }

    #[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);
        insta::assert_snapshot!(output.as_str());
    }
}