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

for ,i in (array ? []) doesn't work #1121

Closed jonathanPoitz closed 1 year ago

jonathanPoitz commented 1 year ago

We stumbled upon a problem in livescript compiling (haven't found it in the issues, if it already exists, let me know!)

In our code, we sometimes use for in loops to only use the index since this produces quite elegant code. E.g:

array = ["a", "a", "a"]
for ,i in array
  console.log(i)

Independently, we sometimes use the ? operator to default to something in case a variable doesn't exist. E.g:

for el, i in (array ? [])
  console.log(el, i)

These things work fine. What doesn't work is the following:

array = ["a", "a", "a"]
for ,i in (array ? [])
  console.log(i)

It works when I write for el, i, but with for ,i this fails. This seems to be an issue with the compile:

  var array, i$, len$, i, results$ = [];
  array = ["a", "a", "a"];
  for (i$ = 0, len$ = array != null
    ? array
    : [].length; i$ < len$; ++i$) {
    i = i$;
    results$.push(console.log(i));
  }

In our opinion, since it doesn't use parentheses, it len$ always refers to [].length instead of array.length.

Can you fix this or is our way of writing for-in loops with the iterated element ommited (for ,i in array) generally not supported? (We've used it quite often and in other situations didn't come across problems)

Thanks in advance,

Jonathan

vendethiel commented 1 year ago

What version is that? I can't see that behavior on the website

jonathanPoitz commented 1 year ago

Ah sorry, forgot to mention. Version 1.6.

And I can reproduce it on the website working not_working