sweet.js: transforming occurrences of a repeated token -
i want define sweet macro transforms
{ a, b } # o into
{ o.a, o.b } my current attempt is
macro (#) { case infix { { $prop:ident (,) ... } | _ $o } => { return #{ { $prop: $o.$prop (,) ... } } } } however, give me
syntaxerror: [patterns] ellipses level not match in template i suspect don't understand how ... works, , may need somehow loop on values of $prop , build syntax objects each , somehow concatenate them, i'm @ loss how that.
the problem syntax expander thinks you're trying expand $o.$prop instead of $prop: $o.$prop. here's solution:
macro (#) { rule infix { { $prop:ident (,) ... } | $o:ident } => { { $($prop: $o.$prop) (,) ... } } } notice placed unit of code in $() block of own disambiguate ellipse expansion.
example: var x = { a, b } # o; becomes var x = { a: o.a, b: o.b };.
Comments
Post a Comment