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.32k stars 155 forks source link

Surprising implicit shorthand property behavior #1038

Closed pepkin88 closed 5 years ago

pepkin88 commented 6 years ago

The code:

[a: 1 2 3]

compiles to this:

[{
  a: 1,
  2: 2,
  3: 3
}];

and not to this:

[
  {
    a: 1
  }, 2, 3
];

like [a: 1, 2, 3] would.

Are my expectations correct? Should [a: 1 2 3] be equivalent to [a: 1, 2, 3]?

Edit: Same with fn a: 1 2 3, v = a: 1 2 3, etc.

rhendric commented 6 years ago

In my opinion, the most straightforward reading of the docs implies that you're correct. However, every tagged version of LiveScript that I tested, as well as Coco, all do the same thing with that expression.

A fix would be relatively simple—swapping the order of add-implicit-braces and expand-literals in the lexer seems to do the trick—and testing that fix shows that no test and no code in the compiler relies on the current behavior. But it's possible someone out there does (a slightly less silly example would be something like [text, name: \node type]).

I'm on the fence about changing this. Anyone else have any opinions?

determin1st commented 6 years ago

i thought about [{a: [1 2 3]}].. it's unclear syntax imo

rhendric commented 6 years ago

The more I think about this, the more I agree with you, @pepkin88. This seems like a bug. Property shorthand should not work outside of braces. The cases in your postscript make that clear: v = a: 1, 2, 3 is a parse error, so it's very odd that v = a: 1 2 3 compiles to anything. Adding array brackets or a function being called shouldn't change how object expressions are parsed; braces should be the only thing that unlocks new property shorthands.