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

[CS2] Enhancement: Support object spread syntax in multiline objects without braces or commas #4600

Closed
laurentpayot opened this issue Jul 3, 2017 · 8 comments
Milestone

Comments

@laurentpayot
Copy link

laurentpayot commented Jul 3, 2017

foo =
  a: 1
  b: 2

bar =
  foo...
  c: 3

results in

error: unexpected ...
	foo...
	   ^^^

For now we have to use:

bar = {
  foo...,
  c: 3
}

It's a bit ugly and it adds an exception to the nice CS way of object notation.

@connec
Copy link
Collaborator

connec commented Jul 3, 2017

Copying #4493 (comment)


That's probably related to looksObjectish in the rewriter, which seems to only accept implicit object literals when there's a : involved (which is consistent with being unable to use shorthand properties in implicit objects).

Tbh we probably want to leave it like this, or we'll need to face down ambiguities in expressions like

f k: v, rest...

Which, currently, will compile to two arguments (an object literal and a splat param) but would be ambiguous if we allowed splats to exist in implicit objects.

@connec
Copy link
Collaborator

connec commented Jul 3, 2017

As an aside, commas are still optional, even with explicit braces:

{
  foo...
  bar
  baz: 'qux'
}

@GeoffreyBooth
Copy link
Collaborator

GeoffreyBooth commented Jul 4, 2017

I ran into this just writing the documentation. I think it’s a thing that will be very common for people to want to do. Could we find a way to allow spreads in multiline objects, while leaving function arguments unaffected (i.e. to avoid ambiguity problems)?

@helixbass this looks like a rewriter issue, if you feel like taking a crack at it.

@GeoffreyBooth GeoffreyBooth added this to the 2.0.0 milestone Jul 4, 2017
@GeoffreyBooth GeoffreyBooth changed the title [CS2] Object spread without brackets and commas [CS2] Enhancement: Support object spread syntax in multiline objects without brackets or commas Jul 4, 2017
@GeoffreyBooth GeoffreyBooth changed the title [CS2] Enhancement: Support object spread syntax in multiline objects without brackets or commas [CS2] Enhancement: Support object spread syntax in multiline objects without braces or commas Jul 4, 2017
@zdenko
Copy link
Collaborator

zdenko commented Jul 6, 2017

@GeoffreyBooth, @connec, @helixbass
I've started to play with this and made some progress.

foo =
  bar...
  c: 3

compiles to

foo = Object.assign({}, bar, {
    c: 3
  });

f a:x, rest... into f({a:x}, ...rest)

f {a:x, rest...} into

f(Object.assign({
    a: x
  }, rest))

And if I'm correct, there is also this case

foo = 
  bar
  c: 3

which should compile to

foo = {
   bar,
   c: 3
}

@vendethiel
Copy link
Collaborator

And if I'm correct, there is also this case

foo = 
  bar
  c: 3

which should compile to

foo = {
   bar,
   c: 3
}

I'm not sure we want this. I read it as foo = bar c: 3 at first, and we don't do that for function calls.

@zdenko
Copy link
Collaborator

zdenko commented Jul 6, 2017

Current status:

# foo = 
#   bar 
#   c: 3
foo = {
  bar,
  c: 3
}

# foo = 
#   bar c: 3
foo = bar({c:3))

# foo = 
#   bar 
#     c: 3
foo = bar({c:3))

@GeoffreyBooth
Copy link
Collaborator

See also #4551 (comment)

@GeoffreyBooth
Copy link
Collaborator

Closing per discussion in #4618.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants