Skip to content

Instantly share code, notes, and snippets.

@grncdr
Forked from jlongster/gist:3881008
Last active December 11, 2015 05:48
Show Gist options
  • Save grncdr/4554165 to your computer and use it in GitHub Desktop.
Save grncdr/4554165 to your computer and use it in GitHub Desktop.
hygienic destructuring var assignments with sweet.js
macro $do {
case ($($x = $y) (,) ...) $body => {
(function ($x (,) ...) $body)($y (,) ...)
}
case $name ($($x = $y) (,) ...) $body => {
(function $name ($x (,) ...) $body)($y (,) ...)
}
}
macro $var {
case [$name (,) ...] = $expr => {
var $name (,) ...
$do (i = 0, array = $expr) {
$($name = array[i++];) ...
}
}
case {$name (,) ...} = $expr => {
var $name (,) ...
$do (obj = $expr) {
$($name = obj.$name;) ...
}
}
case $name:ident = $expr => {
var $name = $expr
}
}
$var [x, y, z] = [0, 1, 2]
$var {a, b} = {a: 1, b: 45}
$var [x, y, z] = [0, 1, 2];
console.log(x, y, z); // 0 1 2
$var {x, y, z} = {x: 5, y: 6, z: 7};
console.log(x, y, z); // 5 6 7
$var w = 10;
console.log(w); // 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment