jlongster / es6-macros

A collection of sweet.js macros that implement ES6 features for ES5
BSD 2-Clause "Simplified" License
238 stars 18 forks source link

One more important feature of destructuring. #21

Open nmn opened 10 years ago

nmn commented 10 years ago

The Destructuring macros works great for variable decralations:

var [a,b] = someArray
var {x,y} = someObject

It does this by doing the obvious thing, which is to store the array in a temporary variable first.

var _ref = someArray
var a = _ref[0];
var b = _ref[1];

However, another great feature of destructuring is reassigning values that isn't supported by this macro as of now:

[a, b] = [b, a]

Now this should work basically the same way as everything else:

var _ref = [b, a]
a = _ref[0];
b = _ref[1];

There may be a problem without a keyword, so it is possible to perhaps create an empty keyword that doesn't put var or let?

nmn commented 10 years ago

my best take so far is this:

let d = macro {
  rule { $id:ident } => {
    $id
  }

  rule { $pattern:destructor = $rhs:expr ;... } => {
    var __ref = $rhs;
    destruct $pattern ; __ref
  }
}
export d;