coffeescript icon indicating copy to clipboard operation
coffeescript copied to clipboard

Bug: multiline object literal requires braces in post-loops

Open Inve1951 opened this issue 4 years ago • 1 comments

Choose one: is this a bug report or feature request?

A bit of both.

Input Code

#try

use k, v for k, v of 
  k: v

While this also applies to

use k, v for x in  # or `from`
  k: v

# and

use k, v while     # or `until`
  k: v

i don't see any point in supporting that.

Expected Behavior

# (shortened)
for (k in {k: v}) {
  use(k, v);
}

Current Behavior

[stdin]:2:1: error: unexpected indentation
  k: v
^^

Workaround

use k, v for k, v of \
  k: v \
  a: b \
  x: y

# or

use k, v for k, v of {
  k: v
  a: b
  x: y
}

Possible Solution

Add implicit line continuation support for objects in eof for-of loop headers. The logic used in function calls might be reusable.

Context

Ran into this a couple of times expecting the compiler to accept it.

Environment

  • CoffeeScript version: v2.5.1
  • Node.js version: any

    Btw, do you call this an implicit object? What's the term generally used for omitting object braces?

Inve1951 avatar Feb 07 '20 00:02 Inve1951

If you want to achieve this behavior in CoffeeScript, you can use explicit line continuation with a backslash \ or enclose the object in curly braces {} as you've mentioned in the workaround.

Here's your code with explicit line continuation using backslashes:

#try
use k, v for k, v of \
  k: v

And here's the same code with explicit curly braces:

#try
use k, v for k, v of {
  k: v
}

As for your question about terminology, what you're referring to as "implicit objects" or "omitting object braces" in CoffeeScript is essentially CoffeeScript's syntax for creating objects without using explicit braces. It's part of CoffeeScript's syntactic sugar for creating objects and can be considered a form of implicit object declaration. There's no widely standardized terminology for this specific syntax, but it's often referred to as "object literals" or "object shorthand" in the context of CoffeeScript and JavaScript.

ljluestc avatar Sep 09 '23 19:09 ljluestc