dk00 / livescript-next

Enable latest ES features for LiveScript
https://lsn.netlify.com/
The Unlicense
39 stars 3 forks source link

bug: certain types of dictionary comprehensions appear not to be supported #4

Closed gkovacs closed 7 years ago

gkovacs commented 7 years ago
foo = ->
  return {[key, val] for {key, val} in data}

doesn't appear to compile with livescript-next. error:

Error: t.ls: TypeError: Property id of VariableDeclarator expected node to be of a type ["LVal"] but instead got "ObjectExpression" (undefined:undefined)
  1 | foo = ->
  2 |   return {[key, val] for {key, val} in data}
    at /Users/geza/livescript-next/node_modules/livescript-next/lib/convert.js:136:43
    at t (/Users/geza/livescript-next/node_modules/livescript-next/lib/convert.js:138:4)
    at convertAll (/Users/geza/livescript-next/node_modules/livescript-next/lib/convert.js:192:42)
    at /Users/geza/livescript-next/node_modules/livescript-next/lib/convert.js:190:28
    at Array.reduce (native)
    at convertAll (/Users/geza/livescript-next/node_modules/livescript-next/lib/convert.js:187:53)
    at convertChildren (/Users/geza/livescript-next/node_modules/livescript-next/lib/convert.js:198:23)
    at /Users/geza/livescript-next/node_modules/livescript-next/lib/convert.js:134:66
    at t (/Users/geza/livescript-next/node_modules/livescript-next/lib/convert.js:138:4)
    at convertAll (/Users/geza/livescript-next/node_modules/livescript-next/lib/convert.js:192:42)

with standard livescript, this correctly generates:

// Generated by LiveScript 1.5.0
(function(){
  var foo;
  foo = function(){
    var key, val;
    return (function(){
      var ref$, resultObj$ = {};
      for (key in ref$ = data) {
        val = ref$[key];
        resultObj$[key] = val;
      }
      return resultObj$;
    }());
  };
}).call(this);
dk00 commented 7 years ago

Destructuring loop variables should be converted to ObjectPattern or ArrayPattern.

With new ES syntax, it should generate this:

foo = function () {
  let res$;
  return do {
    res$ = {};

    for (let {
      key,
      val
    } of data) res$[key] = val;

    res$;
  };
};