Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Surprising implicit shorthand property behavior #1038

Closed
pepkin88 opened this issue Mar 31, 2018 · 3 comments
Closed

Surprising implicit shorthand property behavior #1038

pepkin88 opened this issue Mar 31, 2018 · 3 comments
Assignees
Labels

Comments

@pepkin88
Copy link
Contributor

pepkin88 commented Mar 31, 2018

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
Copy link
Collaborator

rhendric commented Apr 2, 2018

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
Copy link

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

@rhendric
Copy link
Collaborator

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.

@rhendric rhendric added bug and removed discussion labels Sep 19, 2018
rhendric added a commit to rhendric/LiveScript that referenced this issue Oct 11, 2018
Expressions such as `[a: 1 2 3]` should be equivalent to
`[{a: 1}, 2, 3]` and not `[{a: 1, 2: 2, 3: 3}]`. Property shorthand,
including atomic identifiers/literals as shorthand for key-value pairs
with the atom as the key and value, should not work outside of braces.
@rhendric rhendric self-assigned this Oct 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants