gkz / LiveScript

LiveScript is a language which compiles to JavaScript. It has a straightforward mapping to JavaScript and allows you to write expressive code devoid of repetitive boilerplate. While LiveScript adds many features to assist in functional style programming, it also has many improvements for object oriented and imperative programming.
http://livescript.net
MIT License
2.31k stars 156 forks source link

Destructuring optional object in array produces invalid javascript #1096

Closed LeXofLeviafan closed 4 years ago

LeXofLeviafan commented 4 years ago

The code ([{foo}?]) -> produces following output:

(function(arg$){
  var ref$, foo;
  (ref$ = arg$[0]) != null && foo = ref$.foo;
});

Which fails to evaluate because of && before assignment.

At the same time, ([{foo, bar}?]) -> doesn't fail, because resulting output is taken in parens:

(function(arg$){
  var ref$, foo, bar;
  (ref$ = arg$[0]) != null && (foo = ref$.foo, bar = ref$.bar);
});

More to the point, optional object with alternative (([{foo}={}]) ->) doesn't fail either, because there's no right-side assignment:

(function(arg$){
  var ref$, foo;
  foo = ((ref$ = arg$[0]) != null
    ? ref$
    : {}).foo;
});

And adding more fields (([{foo, bar}={}]) ->) results in a comma chain, as usual:

(function(arg$){
  var ref$, foo, bar;
  ref$ = (ref$ = arg$[0]) != null
    ? ref$
    : {}, foo = ref$.foo, bar = ref$.bar;
});
rhendric commented 4 years ago

Looks like a valid bug to me, although there's a small possibility I'll change my mind after a closer read of the docs.