fervid_codegen 0.2.0

EcmaScript code generation for the fervid crate
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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
use fervid_core::{
    fervid_atom, AttributeOrBinding, FervidAtom, StrOrExpr, VBindDirective, VOnDirective,
    VueImports,
};
use regex::Regex;
use swc_core::{
    common::{Span, Spanned, DUMMY_SP},
    ecma::ast::{
        ArrayLit, ArrowExpr, BinExpr, BinaryOp, BlockStmt, BlockStmtOrExpr, CallExpr, Callee,
        ComputedPropName, Expr, ExprOrSpread, Ident, KeyValueProp, Lit, ObjectLit, Prop, PropName,
        PropOrSpread, Str,
    },
};

use crate::{context::CodegenContext, utils::str_to_propname};

lazy_static! {
    static ref CSS_RE: Regex =
        Regex::new(r"(?U)([a-zA-Z_-][a-zA-Z_0-9-]*):\s*(.+)(?:;|$)").unwrap();
}

/// Type alias for all the directives not handled as attributes.
/// Only `v-on` and `v-bind` as well as `v-model` for components generate attribute code.
/// Other directives have their own specifics of code generation, which are handled separately.
// pub type DirectivesToProcess<'i> = SmallVec<[&'i VDirective<'i>; 2]>;

#[derive(Debug, Default)]
pub struct GenerateAttributesResultHints<'i> {
    // _normalizeProps({
    //     foo: "bar",
    //     [_ctx.dynamic || ""]: _ctx.hi
    // })
    pub needs_normalize_props: bool,

    /// When `v-bind="smth"` was found
    pub v_bind_no_arg: Option<&'i VBindDirective>,

    /// When `v-on="smth"` was found
    pub v_on_no_event: Option<&'i VOnDirective>,

    /// When a js binding in :class was found
    pub class_patch_flag: bool,

    /// When a js binding in :style was found
    pub style_patch_flag: bool,

    /// When a prop other than `class` or `style` has a js binding
    pub props_patch_flag: bool,
}

impl CodegenContext {
    pub fn generate_attributes<'attr>(
        &mut self,
        attributes: &'attr [AttributeOrBinding],
        out: &mut Vec<PropOrSpread>,
    ) -> GenerateAttributesResultHints<'attr> {
        // Special generation for `class` and `style` attributes,
        // as they can have both Regular and VDirective variants
        let mut class_regular_attr: Option<(&FervidAtom, Span)> = None;
        let mut class_bound: Option<(Box<Expr>, Span)> = None;
        let mut style_regular_attr: Option<(&FervidAtom, Span)> = None;
        let mut style_bound: Option<(Box<Expr>, Span)> = None;

        // Hints on what was processed and what to do next
        let mut result_hints = GenerateAttributesResultHints::default();

        for attribute in attributes {
            match attribute {
                // First, we check the special case: `class` and `style` attributes
                // class
                AttributeOrBinding::RegularAttribute { name, value, span } if name == "class" => {
                    class_regular_attr = Some((value, *span));
                }

                // style
                AttributeOrBinding::RegularAttribute { name, value, span } if name == "style" => {
                    style_regular_attr = Some((value, *span));
                }

                // Any regular attribute will be added as an object entry,
                // where key is attribute name and value is attribute value as string literal
                AttributeOrBinding::RegularAttribute { name, value, span } => {
                    // let raw = Some(Atom::from(value.as_ref()));

                    out.push(PropOrSpread::Prop(Box::from(Prop::KeyValue(
                        KeyValueProp {
                            key: str_to_propname(&name, *span),
                            value: Box::from(Expr::Lit(Lit::Str(Str {
                                span: span.to_owned(),
                                value: value.to_owned(),
                                raw: None,
                            }))),
                        },
                    ))));
                }

                // Directive.
                // `v-on` and `v-bind` are processed here, other directives
                // will be added to the vector of unprocessed directives

                // :class
                AttributeOrBinding::VBind(VBindDirective {
                    argument: Some(StrOrExpr::Str(argument)),
                    value,
                    span,
                    ..
                }) if argument == "class" => {
                    class_bound = Some((value.to_owned(), *span));
                }

                // :style
                AttributeOrBinding::VBind(VBindDirective {
                    argument: Some(StrOrExpr::Str(argument)),
                    value,
                    span,
                    ..
                }) if argument == "style" => {
                    style_bound = Some((value.to_owned(), *span));
                }

                // `v-bind` directive without argument needs its own processing
                AttributeOrBinding::VBind(v_bind) if v_bind.argument.is_none() => {
                    // IN:
                    // v-on="ons" v-bind="bounds" @click=""
                    //
                    // OUT:
                    // _mergeProps(_toHandlers(_ctx.ons), _ctx.bounds, {
                    //   onClick: _cache[1] || (_cache[1] = () => {})
                    // })
                    result_hints.v_bind_no_arg = Some(v_bind);
                }

                // `v-on` directive without event name also needs its own processing
                AttributeOrBinding::VOn(v_on) if v_on.event.is_none() => {
                    result_hints.v_on_no_event = Some(v_on);
                }

                // `v-bind` directive, shortcut `:`, e.g. `:custom-prop="value"`
                AttributeOrBinding::VBind(VBindDirective {
                    argument: Some(argument),
                    value,
                    span,
                    ..
                }) => {
                    // Transform the raw expression
                    // let was_transformed =
                    //     transform_scoped(&mut value, &self.scope_helper, template_scope_id);
                    let was_transformed = true; // todo
                    let span = *span;

                    // Add the PROPS patch flag
                    result_hints.props_patch_flag =
                        result_hints.props_patch_flag || was_transformed;

                    let key = match argument {
                        StrOrExpr::Str(s) => str_to_propname(s, span),
                        StrOrExpr::Expr(expr) => {
                            // Dynamic prop needs a `_normalizeProps` call
                            // TODO Take from patch flags?
                            result_hints.needs_normalize_props = true;

                            // `[key_transformed || ""]`
                            PropName::Computed(ComputedPropName {
                                span,
                                expr: Box::from(Expr::Bin(BinExpr {
                                    span,
                                    op: BinaryOp::LogicalOr,
                                    left: expr.to_owned(), // ?
                                    right: Box::from(Expr::Lit(Lit::Str(Str {
                                        span,
                                        value: FervidAtom::from(""),
                                        raw: None,
                                    }))),
                                })),
                            })
                        }
                    };

                    out.push(PropOrSpread::Prop(Box::from(Prop::KeyValue(
                        KeyValueProp {
                            key,
                            value: value.to_owned(), // ?
                        },
                    ))));
                }

                // v-on directive, shortcut `@`, e.g. `@custom-event.modifier="value"`
                AttributeOrBinding::VOn(VOnDirective {
                    event: Some(event),
                    handler,
                    modifiers,
                    span,
                }) => {
                    // TODO Use _cache
                    let span = *span;

                    // Transform or default to () => {}
                    // The patch flag does not apply to v-on
                    // TODO Empty `v-on` should be handled using `mergeProps` and `toHandlers`
                    let handler = handler
                        .to_owned()
                        .unwrap_or_else(|| Box::new(empty_arrow_expr(span)));

                    // To generate as an array of `["modifier1", "modifier2"]`
                    let modifiers: Vec<Option<ExprOrSpread>> = modifiers
                        .iter()
                        .map(|modifier| {
                            Some(ExprOrSpread {
                                spread: None,
                                expr: Box::from(Expr::Lit(Lit::Str(Str {
                                    span,
                                    value: modifier.to_owned(),
                                    raw: None,
                                }))),
                            })
                        })
                        .collect();

                    let handler_expr = if modifiers.len() != 0 {
                        let with_modifiers_import =
                            self.get_and_add_import_ident(VueImports::WithModifiers);

                        // `_withModifiers(transformed, ["modifier"]))`
                        Box::new(Expr::Call(CallExpr {
                            span,
                            callee: Callee::Expr(Box::from(Expr::Ident(Ident {
                                span,
                                sym: with_modifiers_import,
                                optional: false,
                            }))),
                            args: vec![
                                ExprOrSpread {
                                    expr: handler,
                                    spread: None,
                                },
                                ExprOrSpread {
                                    expr: Box::from(Expr::Array(ArrayLit {
                                        span,
                                        elems: modifiers,
                                    })),
                                    spread: None,
                                },
                            ],
                            type_args: None,
                        }))
                    } else {
                        // No modifiers, leave expression the same
                        handler
                    };

                    // TODO Cache

                    // TODO Dynamic events are hard, but similar to `v-on`
                    // IN:
                    // foo="bar" :[dynamic]="hi" @[dynamic]="" @[dynamic2]="" v-on="whatever"
                    //
                    // OUT:
                    // _mergeProps({
                    //     foo: "bar",
                    //     [_ctx.dynamic || ""]: _ctx.hi
                    // }, {
                    //     [_toHandlerKey(_ctx.dynamic)]: _cache[4] || (_cache[4] = () => {})
                    // }, {
                    //     [_toHandlerKey(_ctx.dynamic2)]: _cache[5] || (_cache[5] = () => {})
                    // }, _toHandlers(whatever, true))

                    // IDEA: Do the transformation beforehand, and put resulting `Expr`s in the return struct

                    match event {
                        StrOrExpr::Str(event_name_str) => {
                            // e.g. `onClick: _ctx.handleClick` or `onClick: _withModifiers(() => {}, ["stop"])
                            out.push(PropOrSpread::Prop(Box::from(Prop::KeyValue(
                                KeyValueProp {
                                    key: str_to_propname(&event_name_str, span),
                                    value: handler_expr,
                                },
                            ))));
                        }

                        // TODO Instead of pushing to `out`, signify that `mergeProps` and `toHandlerKey` are needed
                        StrOrExpr::Expr(event_name_expr) => {
                            out.push(PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
                                key: PropName::Computed(ComputedPropName {
                                    span: DUMMY_SP,
                                    expr: event_name_expr.to_owned(),
                                }),
                                value: handler_expr,
                            }))));
                        }
                    }
                }

                _ => unreachable!(),
            }
        }

        result_hints.class_patch_flag =
            self.generate_class_bindings(class_regular_attr, class_bound, out);
        result_hints.style_patch_flag =
            self.generate_style_bindings(style_regular_attr, style_bound, out);

        result_hints
    }

    /// Process `class` attribute. We may have a regular one, a bound one, both or neither.
    /// Returns `true` when there were JavaScript bindings
    fn generate_class_bindings(
        &mut self,
        class_regular_attr: Option<(&FervidAtom, Span)>,
        class_bound: Option<(Box<Expr>, Span)>,
        out: &mut Vec<PropOrSpread>,
    ) -> bool {
        let mut expr: Option<Expr> = None;
        let mut has_js_bindings = false;

        match (class_regular_attr, class_bound) {
            // Both regular `class` and bound `:class`
            (Some((regular_value, regular_span)), Some((bound_value, bound_span))) => {
                // 1. []
                // Normalize class with both `class` and `:class` needs an array
                let mut normalize_array = ArrayLit {
                    span: bound_span, // Idk which span should be used here
                    elems: Vec::with_capacity(2),
                };

                // 2. ["regular classes"]
                // Include the content of a regular class
                normalize_array.elems.push(Some(ExprOrSpread {
                    spread: None,
                    expr: Box::from(Expr::Lit(Lit::Str(Str {
                        span: regular_span,
                        value: regular_value.to_owned(),
                        raw: None, //Some(Atom::from(regular_value.as_ref())),
                    }))),
                }));

                // 3. Transform the bound value
                // let was_transformed =
                //     transform_scoped(&mut bound_value, &self.scope_helper, scope_to_use);
                let was_transformed = true; // TODO

                // 4. ["regular classes", boundClasses]
                normalize_array.elems.push(Some(ExprOrSpread {
                    spread: None,
                    expr: bound_value,
                }));

                // `normalizeClass(["regular classes", boundClasses])`
                expr = Some(Expr::Call(CallExpr {
                    span: bound_span,
                    callee: Callee::Expr(Box::from(Expr::Ident(Ident {
                        span: bound_span,
                        sym: self.get_and_add_import_ident(VueImports::NormalizeClass),
                        optional: false,
                    }))),
                    args: vec![ExprOrSpread {
                        spread: None,
                        expr: Box::from(Expr::Array(normalize_array)),
                    }],
                    type_args: None,
                }));

                has_js_bindings = was_transformed;
            }

            // Just regular `class`
            (Some((regular_value, span)), None) => {
                expr = Some(Expr::Lit(Lit::Str(Str {
                    raw: None, // Some(Atom::from(regular_value.as_ref())),
                    value: regular_value.to_owned(),
                    span,
                })));
            }

            // Just bound `:class`
            (None, Some((bound_value, span))) => {
                // let was_transformed =
                //     transform_scoped(&mut bound_value, &self.scope_helper, scope_to_use);
                let was_transformed = true; // TODO

                // `normalizeClass(boundClasses)`
                expr = Some(Expr::Call(CallExpr {
                    span,
                    callee: Callee::Expr(Box::from(Expr::Ident(Ident {
                        span,
                        sym: self.get_and_add_import_ident(VueImports::NormalizeClass),
                        optional: false,
                    }))),
                    args: vec![ExprOrSpread {
                        spread: None,
                        expr: bound_value,
                    }],
                    type_args: None,
                }));

                has_js_bindings = was_transformed;
            }

            // Neither
            (None, None) => {}
        }

        // Add `class` to attributes
        if let Some(expr) = expr {
            out.push(PropOrSpread::Prop(Box::from(Prop::KeyValue(
                KeyValueProp {
                    key: PropName::Ident(Ident::new(fervid_atom!("class"), expr.span())),
                    value: Box::from(expr),
                },
            ))));
        }

        has_js_bindings
    }

    /// Process `style` attribute. We may have a regular one, a bound one, both or neither.
    /// Returns `true` when there were JavaScript bindings
    fn generate_style_bindings(
        &mut self,
        style_regular_attr: Option<(&FervidAtom, Span)>,
        style_bound: Option<(Box<Expr>, Span)>,
        out: &mut Vec<PropOrSpread>,
    ) -> bool {
        let mut expr = None;
        let mut has_js_bindings = false;

        match (style_regular_attr, style_bound) {
            // Both `style` and `:style`
            (Some((regular_value, regular_span)), Some((bound_value, bound_span))) => {
                // 1. []
                // normalizeStyle with both `style` and `:style` needs an array
                let mut normalize_array = ArrayLit {
                    span: bound_span, // Idk which span should be used here
                    elems: Vec::with_capacity(2),
                };

                // 2. { regular: "styles as an object" }
                // Generate the regular styles into an object
                let regular_styles_obj = generate_regular_style(regular_value, regular_span);

                // 3. [{ regular: "styles as an object" }]
                // Include the content of a regular style
                normalize_array.elems.push(Some(ExprOrSpread {
                    spread: None,
                    expr: Box::from(Expr::Object(regular_styles_obj)),
                }));

                // 4. Transform the bound value
                // let was_transformed =
                //     transform_scoped(&mut bound_value, &self.scope_helper, scope_to_use);
                let was_transformed = true; // TODO

                // 5. [{ regular: "styles as an object" }, boundStyles]
                normalize_array.elems.push(Some(ExprOrSpread {
                    spread: None,
                    expr: bound_value, // ?
                }));

                // `normalizeClass([{ regular: "styles as an object" }, boundStyles])`
                expr = Some(Expr::Call(CallExpr {
                    span: bound_span,
                    callee: Callee::Expr(Box::from(Expr::Ident(Ident {
                        span: bound_span,
                        sym: self.get_and_add_import_ident(VueImports::NormalizeStyle),
                        optional: false,
                    }))),
                    args: vec![ExprOrSpread {
                        spread: None,
                        expr: Box::from(Expr::Array(normalize_array)),
                    }],
                    type_args: None,
                }));

                has_js_bindings = was_transformed;
            }

            // `style`
            (Some((regular_value, span)), None) => {
                expr = Some(Expr::Object(generate_regular_style(regular_value, span)));
            }

            // `:style`
            (None, Some((bound_value, span))) => {
                // let was_transformed =
                //     transform_scoped(&mut bound_value, &self.scope_helper, scope_to_use);
                let was_transformed = true; // TODO

                // `normalizeStyle(boundStyles)`
                expr = Some(Expr::Call(CallExpr {
                    span,
                    callee: Callee::Expr(Box::from(Expr::Ident(Ident {
                        span,
                        sym: self.get_and_add_import_ident(VueImports::NormalizeStyle),
                        optional: false,
                    }))),
                    args: vec![ExprOrSpread {
                        spread: None,
                        expr: bound_value,
                    }],
                    type_args: None,
                }));

                has_js_bindings = was_transformed;
            }

            (None, None) => {}
        }

        // Add `style` to attributes
        if let Some(expr) = expr {
            out.push(PropOrSpread::Prop(Box::from(Prop::KeyValue(
                KeyValueProp {
                    key: PropName::Ident(Ident::new(fervid_atom!("style"), expr.span())),
                    value: Box::from(expr),
                },
            ))));
        }

        has_js_bindings
    }
}

fn generate_regular_style(style: &str, span: Span) -> ObjectLit {
    let mut result = ObjectLit {
        span,
        props: Vec::with_capacity(4), // pre-allocate more just in case
    };

    for mat in CSS_RE.captures_iter(style) {
        let Some(style_name) = mat.get(1).map(|v| v.as_str().trim()) else {
            continue;
        };
        let Some(style_value) = mat.get(2).map(|v| v.as_str().trim()) else {
            continue;
        };

        if style_name.len() == 0 || style_value.len() == 0 {
            continue;
        }

        result
            .props
            .push(PropOrSpread::Prop(Box::from(Prop::KeyValue(
                KeyValueProp {
                    key: str_to_propname(style_name, span),
                    value: Box::from(Expr::Lit(Lit::Str(Str {
                        span,
                        value: style_value.into(),
                        raw: None, // Some(style_value.into()),
                    }))),
                },
            ))));
    }

    result
}

/// Generates () => {}
fn empty_arrow_expr(span: Span) -> Expr {
    Expr::Arrow(ArrowExpr {
        span,
        params: vec![],
        body: Box::from(BlockStmtOrExpr::BlockStmt(BlockStmt {
            span,
            stmts: vec![],
        })),
        is_async: false,
        is_generator: false,
        type_params: None,
        return_type: None,
    })
}

#[cfg(test)]
mod tests {
    use fervid_core::{AttributeOrBinding, VOnDirective};
    use swc_core::{common::DUMMY_SP, ecma::ast::ObjectLit};

    use crate::{
        context::CodegenContext,
        test_utils::{js, regular_attribute, v_bind_attribute, v_on_attribute},
    };

    #[test]
    fn it_generates_class_regular() {
        test_out(
            vec![regular_attribute("class", "both regular and bound")],
            r#"{class:"both regular and bound"}"#,
        );
    }

    #[test]
    fn it_generates_class_bound() {
        test_out(
            vec![v_bind_attribute("class", "[item2, index]")],
            r#"{class:_normalizeClass([item2,index])}"#,
        );
    }

    #[test]
    fn it_generates_both_classes() {
        test_out(
            vec![
                regular_attribute("class", "both regular and bound"),
                v_bind_attribute("class", "[item2, index]"),
            ],
            r#"{class:_normalizeClass(["both regular and bound",[item2,index]])}"#,
        );
    }

    #[test]
    fn it_generates_style_regular() {
        test_out(
            vec![regular_attribute(
                "style",
                "margin: 0px; background-color: magenta",
            )],
            r#"{style:{margin:"0px","background-color":"magenta"}}"#,
        );
    }

    #[test]
    fn it_generates_style_bound() {
        // `:style="{ backgroundColor: v ? 'yellow' : undefined }"`
        test_out(
            vec![v_bind_attribute(
                "style",
                "{ backgroundColor: v ? 'yellow' : undefined }",
            )],
            r#"{style:_normalizeStyle({backgroundColor:v?"yellow":undefined})}"#,
        );
    }

    #[test]
    fn it_generates_both_styles() {
        test_out(
            vec![
                regular_attribute("style", "margin: 0px; background-color: magenta"),
                v_bind_attribute("style", "{ backgroundColor: v ? 'yellow' : undefined }"),
            ],
            r#"{style:_normalizeStyle([{margin:"0px","background-color":"magenta"},{backgroundColor:v?"yellow":undefined}])}"#,
        );
    }

    #[test]
    fn it_generates_v_bind() {
        // :disabled="true"
        test_out(
            vec![v_bind_attribute("disabled", "true")],
            "{disabled:true}",
        );

        // :multi-word-binding="true"
        test_out(
            vec![v_bind_attribute("multi-word-binding", "true")],
            r#"{"multi-word-binding":true}"#,
        );

        // :disabled="some && expression || maybe !== not"
        test_out(
            vec![v_bind_attribute(
                "disabled",
                "some && expression || maybe !== not",
            )],
            "{disabled:some&&expression||maybe!==not}",
        );
    }

    #[test]
    fn it_generates_v_on() {
        // @click
        test_out(
            vec![AttributeOrBinding::VOn(VOnDirective {
                event: Some("onClick".into()),
                handler: None,
                modifiers: vec![],
                span: DUMMY_SP,
            })],
            r"{onClick:()=>{}}",
        );

        // @multi-word-event (gets transformed to `onMultiWordEvent`)
        test_out(
            vec![AttributeOrBinding::VOn(VOnDirective {
                event: Some("onMultiWordEvent".into()),
                handler: None,
                modifiers: vec![],
                span: DUMMY_SP,
            })],
            r"{onMultiWordEvent:()=>{}}",
        );

        // @click="handleClick"
        test_out(
            vec![v_on_attribute("onClick", "handleClick")],
            r"{onClick:handleClick}",
        );

        // TODO
        // This should have been transformed previously
        // @click="console.log('hello')"
        // test_out(
        //     vec![AttributeOrBinding::VOn(VOnDirective {
        //         event: Some("click".into()),
        //         handler: Some(js("() => console.log('hello')")),
        //         modifiers: vec![],
        //         span: DUMMY_SP
        //     })],
        //     r"{onClick:()=>console.log('hello')}"
        // );

        // @click="() => console.log('hello')"
        test_out(
            vec![v_on_attribute("onClick", "() => console.log('hello')")],
            r#"{onClick:()=>console.log("hello")}"#,
        );

        // @click="$event => handleClick($event, foo, bar)"
        test_out(
            vec![v_on_attribute(
                "onClick",
                "$event => handleClick($event, foo, bar)",
            )],
            r"{onClick:$event=>handleClick($event,foo,bar)}",
        );

        // @click.stop.prevent.self
        test_out(
            vec![AttributeOrBinding::VOn(VOnDirective {
                event: Some("onClick".into()),
                handler: None,
                modifiers: vec!["stop".into(), "prevent".into(), "self".into()],
                span: DUMMY_SP,
            })],
            r#"{onClick:_withModifiers(()=>{},["stop","prevent","self"])}"#,
        );

        // @click.stop="$event => handleClick($event, foo, bar)"
        test_out(
            vec![AttributeOrBinding::VOn(VOnDirective {
                event: Some("onClick".into()),
                handler: Some(js("$event => handleClick($event, foo, bar)")),
                modifiers: vec!["stop".into()],
                span: DUMMY_SP,
            })],
            r#"{onClick:_withModifiers($event=>handleClick($event,foo,bar),["stop"])}"#,
        );
    }

    fn test_out(input: Vec<AttributeOrBinding>, expected: &str) {
        let mut ctx = CodegenContext::default();
        let mut out = ObjectLit {
            span: DUMMY_SP,
            props: vec![],
        };
        ctx.generate_attributes(&input, &mut out.props);
        assert_eq!(crate::test_utils::to_str(out), expected)
    }
}