Sweet.js: Possible to expand a JS object into a JS object?

91 views Asked by At

Is it possible to use Sweet.Js to expand

{ "foo": "bar" }

to

{ "bar": "foo" }

for example?

My naïve attempt here doesn’t work and I don’t see an example in the documentation.

Thanks.

1

There are 1 answers

1
timdisney On BEST ANSWER

The trick is that : has special meaning in patterns so you need to escape it with $[:]

macro bar {
  rule { {$x $[:] $y} } =>  { {$y: $x} }
}

var o = bar { "bax": "quux" }