Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
This sweet.js macro grants you the awesome spread operator. You can read more about it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator. It's still in its infancy, and doesn't allow you to do all functionality, but you can flatten array literals and use the spread operator within a function call b…
// Used internally by the ..> macro
macro (..el) {
rule {
[$x:invoke(..el) (,) ...]
} => {
$x (,) ...
}
rule {
$x
} => {
$x
}
}
/*
Main spread operator macro. You can't use '...' as this is used internally by sweet.js and causes issues, so for now it uses '..>'.
NOTE: This will work for flattening array literals and for spreading an array into a function's parameters through the '~' operator.
At this time it will not spread array variables or allow you to flatten and mix array literals and variables.
What you can do:
var arr = ..>[1,2,3,[1,[2, [3, [4]]]]] yields: var arr = [1,2,3,1,2,3,4];
or
var num = ~some(..>app) yeilds var num = some.apply(null, app);
*/
macro (..>) {
rule {
[$x:invoke(..el) (,) ...]
} => {
[$x (,) ...]
}
}
/*
A macro to allow you to use the spread operator within a function call.
NOTE: At this time, it will only work if you pass a single array preceded by '..>' as the fuction's parameter.
*/
macro (~) {
rule {
$x(..>$y)
} => {
$x.apply(null, $y)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment