vize_atelier_sfc 0.37.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
//! Tests for props destructure handling.

#[cfg(test)]
#[allow(clippy::module_inception)]
mod tests {
    use super::super::helpers::gen_props_access_exp;
    use super::super::transform::transform_destructured_props;
    use super::super::{PropsDestructureBinding, PropsDestructuredBindings};
    use vize_carton::ToCompactString;

    fn make_bindings(names: &[&str]) -> PropsDestructuredBindings {
        let mut bindings = PropsDestructuredBindings::default();
        for name in names {
            bindings.bindings.insert(
                name.to_compact_string(),
                PropsDestructureBinding {
                    local: name.to_compact_string(),
                    default: None,
                },
            );
        }
        bindings
    }

    #[test]
    fn test_gen_props_access_exp() {
        assert_eq!(gen_props_access_exp("msg"), "__props.msg");
        assert_eq!(gen_props_access_exp("my-prop"), "__props[\"my-prop\"]");
    }

    #[test]
    fn test_transform_simple() {
        let bindings = make_bindings(&["msg"]);
        let source = "console.log(msg)";
        let result = transform_destructured_props(source, &bindings);
        assert!(result.contains("__props.msg"), "Got: {}", result);
    }

    #[test]
    fn test_transform_with_shadowing() {
        let bindings = make_bindings(&["msg"]);

        // msg is shadowed by the arrow function parameter
        let source = "const fn = (msg) => console.log(msg)";
        let result = transform_destructured_props(source, &bindings);
        // The msg inside the arrow function should NOT be rewritten
        assert!(!result.contains("__props"), "Got: {}", result);
    }

    #[test]
    fn test_transform_in_computed() {
        let bindings = make_bindings(&["count"]);

        // count inside computed arrow function should be rewritten
        let source = "const double = computed(() => count * 2)";
        let result = transform_destructured_props(source, &bindings);
        assert!(
            result.contains("__props.count"),
            "Expected __props.count, got: {}",
            result
        );
        assert_eq!(result, "const double = computed(() => __props.count * 2)");
    }

    #[test]
    fn test_transform_multiple_refs() {
        let bindings = make_bindings(&["foo", "bar"]);

        let source = "const result = foo + bar";
        let result = transform_destructured_props(source, &bindings);
        assert!(result.contains("__props.foo"), "Got: {}", result);
        assert!(result.contains("__props.bar"), "Got: {}", result);
    }

    // ==================== New test cases ====================

    #[test]
    fn test_transform_in_function_declaration() {
        let bindings = make_bindings(&["count"]);

        // count inside function declaration should be rewritten
        let source = r#"function double() {
    return count * 2
}"#;
        let result = transform_destructured_props(source, &bindings);
        assert!(
            result.contains("__props.count"),
            "Expected __props.count in function body, got: {}",
            result
        );
    }

    #[test]
    fn test_transform_in_nested_arrow_function() {
        let bindings = make_bindings(&["msg"]);

        // msg inside nested arrow function should be rewritten
        let source = r#"const outer = () => {
    const inner = () => msg
    return inner()
}"#;
        let result = transform_destructured_props(source, &bindings);
        assert!(
            result.contains("__props.msg"),
            "Expected __props.msg in nested arrow, got: {}",
            result
        );
    }

    #[test]
    fn test_transform_in_method() {
        let bindings = make_bindings(&["count"]);

        // count inside regular function expression should be rewritten
        let source = r#"const obj = {
    getCount: function() { return count }
}"#;
        let result = transform_destructured_props(source, &bindings);
        assert!(
            result.contains("__props.count"),
            "Expected __props.count in method, got: {}",
            result
        );
    }

    #[test]
    fn test_transform_in_watch_callback() {
        let bindings = make_bindings(&["count"]);

        let source = r#"watch(() => count, (newVal) => {
    console.log(newVal)
})"#;
        let result = transform_destructured_props(source, &bindings);
        assert!(
            result.contains("__props.count"),
            "Expected __props.count in watch callback, got: {}",
            result
        );
    }

    #[test]
    fn test_transform_shadowed_in_for_of() {
        let bindings = make_bindings(&["item"]);

        // item is shadowed in for...of
        let source = r#"for (const item of items) {
    console.log(item)
}"#;
        let result = transform_destructured_props(source, &bindings);
        // The item inside the loop should NOT be rewritten
        assert!(
            !result.contains("__props.item"),
            "item should be shadowed in for...of, got: {}",
            result
        );
    }

    #[test]
    fn test_transform_shadowed_in_for_in() {
        let bindings = make_bindings(&["key"]);

        // key is shadowed in for...in
        let source = r#"for (const key in obj) {
    console.log(key)
}"#;
        let result = transform_destructured_props(source, &bindings);
        // The key inside the loop should NOT be rewritten
        assert!(
            !result.contains("__props.key"),
            "key should be shadowed in for...in, got: {}",
            result
        );
    }

    #[test]
    fn test_transform_shadowed_in_catch() {
        let bindings = make_bindings(&["error"]);

        // error is shadowed in catch
        let source = r#"try {
    doSomething()
} catch (error) {
    console.log(error)
}"#;
        let result = transform_destructured_props(source, &bindings);
        // The error inside catch should NOT be rewritten
        assert!(
            !result.contains("__props.error"),
            "error should be shadowed in catch, got: {}",
            result
        );
    }

    #[test]
    fn test_transform_in_block_scope() {
        let bindings = make_bindings(&["count"]);

        // count inside a block but not shadowed should be rewritten
        let source = r#"{
    const doubled = count * 2
    console.log(doubled)
}"#;
        let result = transform_destructured_props(source, &bindings);
        assert!(
            result.contains("__props.count"),
            "Expected __props.count in block scope, got: {}",
            result
        );
    }

    #[test]
    fn test_transform_shadowed_in_block() {
        let bindings = make_bindings(&["count"]);

        // count is shadowed in block
        let source = r#"{
    const count = 10
    console.log(count)
}"#;
        let result = transform_destructured_props(source, &bindings);
        // The count inside the block should NOT be rewritten because it's shadowed
        assert!(
            !result.contains("__props.count"),
            "count should be shadowed in block, got: {}",
            result
        );
    }

    #[test]
    fn test_transform_template_literal() {
        let bindings = make_bindings(&["name"]);

        let source = "const greeting = `Hello, ${name}!`";
        let result = transform_destructured_props(source, &bindings);
        assert!(
            result.contains("__props.name"),
            "Expected __props.name in template literal, got: {}",
            result
        );
    }

    #[test]
    fn test_transform_ternary() {
        let bindings = make_bindings(&["show"]);

        let source = "const display = show ? 'visible' : 'hidden'";
        let result = transform_destructured_props(source, &bindings);
        assert!(
            result.contains("__props.show"),
            "Expected __props.show in ternary, got: {}",
            result
        );
    }

    #[test]
    fn test_transform_logical_expression() {
        let bindings = make_bindings(&["enabled", "active"]);

        let source = "const isOn = enabled && active";
        let result = transform_destructured_props(source, &bindings);
        assert!(
            result.contains("__props.enabled"),
            "Expected __props.enabled, got: {}",
            result
        );
        assert!(
            result.contains("__props.active"),
            "Expected __props.active, got: {}",
            result
        );
    }

    #[test]
    fn test_transform_object_shorthand() {
        let bindings = make_bindings(&["foo"]);

        let source = "const obj = { foo }";
        let result = transform_destructured_props(source, &bindings);
        // Should transform { foo } to { foo: __props.foo }
        assert!(
            result.contains("__props.foo"),
            "Expected __props.foo in object shorthand, got: {}",
            result
        );
    }

    #[test]
    fn test_transform_array_access() {
        let bindings = make_bindings(&["items"]);

        let source = "const first = items[0]";
        let result = transform_destructured_props(source, &bindings);
        assert!(
            result.contains("__props.items"),
            "Expected __props.items in array access, got: {}",
            result
        );
    }

    #[test]
    fn test_transform_member_expression() {
        let bindings = make_bindings(&["user"]);

        let source = "const name = user.name";
        let result = transform_destructured_props(source, &bindings);
        assert!(
            result.contains("__props.user"),
            "Expected __props.user in member expression, got: {}",
            result
        );
    }

    #[test]
    fn test_transform_chained_member() {
        let bindings = make_bindings(&["data"]);

        let source = "const value = data.nested.value";
        let result = transform_destructured_props(source, &bindings);
        assert!(
            result.contains("__props.data"),
            "Expected __props.data in chained member, got: {}",
            result
        );
    }

    #[test]
    fn test_transform_call_argument() {
        let bindings = make_bindings(&["count"]);

        let source = "doSomething(count, 'test')";
        let result = transform_destructured_props(source, &bindings);
        assert!(
            result.contains("__props.count"),
            "Expected __props.count as call argument, got: {}",
            result
        );
    }

    #[test]
    fn test_transform_multiple_statements() {
        let bindings = make_bindings(&["msg", "count"]);

        let source = r#"console.log(msg)
const double = count * 2
return { msg, count }"#;
        let result = transform_destructured_props(source, &bindings);
        // Count occurrences of __props
        let props_count = result.matches("__props").count();
        assert!(
            props_count >= 4,
            "Expected at least 4 __props occurrences, got {}: {}",
            props_count,
            result
        );
    }

    #[test]
    fn test_no_transform_property_key() {
        let bindings = make_bindings(&["msg"]);

        // msg as property key should NOT be rewritten
        let source = "const obj = { msg: 'hello' }";
        let result = transform_destructured_props(source, &bindings);
        // The msg as key should stay as is
        assert!(
            !result.contains("__props.msg"),
            "Property key should not be transformed, got: {}",
            result
        );
    }

    #[test]
    fn test_no_transform_property_access() {
        let bindings = make_bindings(&["name"]);

        // .name should NOT be rewritten (it's property access, not reference)
        let source = "const userName = user.name";
        let result = transform_destructured_props(source, &bindings);
        // Only "name" after the dot should stay as is
        assert!(
            !result.contains("__props.name"),
            "Property access should not be transformed, got: {}",
            result
        );
    }

    #[test]
    fn test_transform_aliased_prop() {
        let mut bindings = PropsDestructuredBindings::default();
        // prop key is "message", local name is "msg"
        bindings.bindings.insert(
            "message".to_compact_string(),
            PropsDestructureBinding {
                local: "msg".to_compact_string(),
                default: None,
            },
        );

        let source = "console.log(msg)";
        let result = transform_destructured_props(source, &bindings);
        // Should rewrite msg to __props.message (the original key)
        assert!(
            result.contains("__props.message"),
            "Expected __props.message for aliased prop, got: {}",
            result
        );
    }

    // ==================== Snapshot tests ====================

    #[allow(clippy::disallowed_macros)]
    mod snapshots {
        use super::{
            make_bindings, transform_destructured_props, PropsDestructureBinding,
            PropsDestructuredBindings,
        };
        use vize_carton::ToCompactString;

        #[test]
        fn test_basic_usage() {
            let bindings = make_bindings(&["foo"]);
            let source = "console.log(foo)";
            let result = transform_destructured_props(source, &bindings);
            insta::assert_snapshot!(result);
        }

        #[test]
        fn test_nested_scope() {
            let bindings = make_bindings(&["foo", "bar"]);
            let source = r#"function test(foo) {
    console.log(foo)
    console.log(bar)
}"#;
            let result = transform_destructured_props(source, &bindings);
            insta::assert_snapshot!(result);
        }

        #[test]
        fn test_multiple_variable_declarations() {
            let bindings = make_bindings(&["foo"]);
            let source = r#"const bar = 'fish', hello = 'world'
console.log(foo)"#;
            let result = transform_destructured_props(source, &bindings);
            insta::assert_snapshot!(result);
        }

        #[test]
        fn test_function_param_same_name() {
            let bindings = make_bindings(&["value"]);
            let source = r#"function test(value) {
    try {
    } catch {
    }
}
console.log(value)"#;
            let result = transform_destructured_props(source, &bindings);
            insta::assert_snapshot!(result);
        }

        #[test]
        fn test_aliasing() {
            let mut bindings = PropsDestructuredBindings::default();
            bindings.bindings.insert(
                "foo".to_compact_string(),
                PropsDestructureBinding {
                    local: "x".to_compact_string(),
                    default: None,
                },
            );
            bindings.bindings.insert(
                "foo".to_compact_string(),
                PropsDestructureBinding {
                    local: "y".to_compact_string(),
                    default: None,
                },
            );
            let source = r#"let a = x
let b = y"#;
            let result = transform_destructured_props(source, &bindings);
            insta::assert_snapshot!(result);
        }

        #[test]
        fn test_computed_property() {
            let bindings = make_bindings(&["count"]);
            let source = r#"const double = computed(() => count * 2)
const triple = computed(function() { return count * 3 })"#;
            let result = transform_destructured_props(source, &bindings);
            insta::assert_snapshot!(result);
        }

        #[test]
        fn test_watch_callback() {
            let bindings = make_bindings(&["count"]);
            let source = r#"watch(() => count, (newVal, oldVal) => {
    console.log('changed from', oldVal, 'to', newVal)
})"#;
            let result = transform_destructured_props(source, &bindings);
            insta::assert_snapshot!(result);
        }

        #[test]
        fn test_template_literal() {
            let bindings = make_bindings(&["name", "age"]);
            let source = r#"const greeting = `Hello ${name}, you are ${age} years old`"#;
            let result = transform_destructured_props(source, &bindings);
            insta::assert_snapshot!(result);
        }

        #[test]
        fn test_object_shorthand_props() {
            let bindings = make_bindings(&["foo", "bar"]);
            let source = r#"const obj = { foo, bar, baz: 123 }"#;
            let result = transform_destructured_props(source, &bindings);
            insta::assert_snapshot!(result);
        }

        #[test]
        fn test_complex_nested_function() {
            let bindings = make_bindings(&["data", "config"]);
            let source = r#"function processData() {
    const result = data.map(item => {
        const inner = config.map(c => c.value)
        return { item, inner }
    })
    return result
}"#;
            let result = transform_destructured_props(source, &bindings);
            insta::assert_snapshot!(result);
        }

        #[test]
        fn test_for_loops() {
            let bindings = make_bindings(&["items", "index"]);
            let source = r#"for (let i = 0; i < items.length; i++) {
    console.log(items[i])
}
for (const item of items) {
    console.log(item)
}
for (const index in items) {
    console.log(index)
}"#;
            let result = transform_destructured_props(source, &bindings);
            insta::assert_snapshot!(result);
        }

        #[test]
        fn test_try_catch_finally() {
            let bindings = make_bindings(&["error", "data"]);
            let source = r#"try {
    console.log(data)
} catch (error) {
    console.log(error)
} finally {
    console.log(error)
}"#;
            let result = transform_destructured_props(source, &bindings);
            insta::assert_snapshot!(result);
        }

        #[test]
        fn test_conditional_expression() {
            let bindings = make_bindings(&["show", "msg"]);
            let source = r#"const display = show ? msg : 'hidden'
const result = show && msg || 'default'"#;
            let result = transform_destructured_props(source, &bindings);
            insta::assert_snapshot!(result);
        }

        #[test]
        fn test_member_expression_chain() {
            let bindings = make_bindings(&["user"]);
            let source = r#"const name = user.profile.name
const email = user?.contact?.email
const id = user['id']"#;
            let result = transform_destructured_props(source, &bindings);
            insta::assert_snapshot!(result);
        }

        #[test]
        fn test_array_methods() {
            let bindings = make_bindings(&["items"]);
            let source = r#"const filtered = items.filter(x => x > 0)
const mapped = items.map(x => x * 2)
const reduced = items.reduce((acc, x) => acc + x, 0)"#;
            let result = transform_destructured_props(source, &bindings);
            insta::assert_snapshot!(result);
        }

        #[test]
        fn test_destructuring_in_params() {
            let bindings = make_bindings(&["data"]);
            let source = r#"const fn = ({ x, y }) => x + y
console.log(data)"#;
            let result = transform_destructured_props(source, &bindings);
            insta::assert_snapshot!(result);
        }

        #[test]
        fn test_switch_statement() {
            let bindings = make_bindings(&["value", "msg"]);
            let source = r#"switch (value) {
    case 1:
        console.log(msg)
        break
    case 2:
        console.log('two')
        break
    default:
        console.log(value)
}"#;
            let result = transform_destructured_props(source, &bindings);
            insta::assert_snapshot!(result);
        }

        #[test]
        fn test_while_and_do_while() {
            let bindings = make_bindings(&["count"]);
            let source = r#"while (count > 0) {
    console.log(count)
}
do {
    console.log(count)
} while (count > 0)"#;
            let result = transform_destructured_props(source, &bindings);
            insta::assert_snapshot!(result);
        }

        #[test]
        fn test_rest_spread_member_access() {
            // const { actions, ...props } = defineProps<{...}>()
            // props.status should become __props.status
            let mut bindings = PropsDestructuredBindings::default();
            bindings.bindings.insert(
                "actions".to_compact_string(),
                PropsDestructureBinding {
                    local: "actions".to_compact_string(),
                    default: None,
                },
            );
            bindings.rest_id = Some("props".to_compact_string());

            let source = r#"const status = computed(() => {
    if (props.status.reblog) return props.status.reblog
    return props.status
})
const rebloggedBy = computed(() => props.status.reblog ? props.status.account : null)
console.log(actions)"#;
            let result = transform_destructured_props(source, &bindings);
            insta::assert_snapshot!(result);
        }

        #[test]
        fn test_rest_spread_bare_identifier() {
            // const { a, ...rest } = defineProps<{...}>()
            // bare `rest` should become `__props`
            let mut bindings = PropsDestructuredBindings::default();
            bindings.bindings.insert(
                "a".to_compact_string(),
                PropsDestructureBinding {
                    local: "a".to_compact_string(),
                    default: None,
                },
            );
            bindings.rest_id = Some("rest".to_compact_string());

            let source = r#"console.log(rest)
const b = rest"#;
            let result = transform_destructured_props(source, &bindings);
            insta::assert_snapshot!(result);
        }
    }
}