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

Lexer: Indenting within object literal produces unexpected behavior, unhelpful error message #2667

Closed
FiloSottile opened this issue Jan 15, 2013 · 17 comments
Labels

Comments

@FiloSottile
Copy link

This code compiles

{ a: 1
b: 2 }

while this other Syntax Errors

{ a: 1
  b: 2 }

(And generally every time you indent inside a {} literal.)

Not sure it is a bug, but I can't find why in the docs.
Test it

@vendethiel
Copy link
Collaborator

You have no "base" indentation so that's expected.

@FiloSottile
Copy link
Author

I don't think I understand.

Shouldn't this work?

o = { a: 1,
      b: 2 }

@vendethiel
Copy link
Collaborator

The first member's indentation is taken as base indent. You can use

a =
 b: c
 D: e

@epidemian
Copy link
Contributor

Shouldn't this work?

o = { a: 1,
      b: 2 }

I personally would expect that to work, especially given that:

a = [ foo
      bar
      baz ]

And, more importantly

o = { foo
      bar
      baz }

Both work as expected (with or without separator commas).

@FiloSottile
Copy link
Author

Indeed, I understand what works and what not, but I'm questioning this behavior.

@satyr
Copy link
Collaborator

satyr commented Jan 15, 2013

Shouldn't this work?

Would work if there weren't implicit objects. The code in question is rewritten as:

{ a: 1
 {b: 2} }

and becomes invalid.

@vendethiel
Copy link
Collaborator

To be clearer this is lexed as

a = {b: c
  {d: e}
}

Whereas {a b} isn't triggering implicit objects rules. (That may be counted as a bug actually, but that's how INDENT obj works)

@FiloSottile
Copy link
Author

Ok, makes sense, sorry for the trouble.
(But it's not clear to the newcomer...)

@epidemian
Copy link
Contributor

Why closing this? @satyr and @Nami-Doc gave an explanation of why this happens, but i don't think they suggested that it's not a bug, or that it's a nice behavior of the compiler. I think it's an inconsistent behavior and should be considered a bug. At least the confusing error message should be considered a bug IMO:

o = { a: 1,
      b: 2, 
      c: 3 }

Error: Parse error on line 2: Unexpected '{'

WAT? There's no '{' at line 2! That error message is as useful as "Parse error: please move your code around a bit until it parses correctly", which is basically what i'd do in that situation =P

@FiloSottile
Copy link
Author

Fair enough.

I fear the parser issue in unfixable, as it would require ignoring indentation generated objects inside others objects. And there's no reason for such a behavior.

However, a better error message is needed.

@FiloSottile FiloSottile reopened this Jan 15, 2013
@vendethiel
Copy link
Collaborator

Something more future-proof would be to display rewritten coffee, maybe

@satyr
Copy link
Collaborator

satyr commented Jan 15, 2013

#2160

@mhart
Copy link

mhart commented May 15, 2013

Just chiming in - I've been bitten by this a number of times and there's nothing that I can see that's ambiguous about the syntax.

It also makes wrapping objects quite messy unless you're going to list each property on a separate line - which in a number of cases is overkill.

@xixixao
Copy link
Contributor

xixixao commented Dec 2, 2013

Is it unreasonable to require

{ a: b
    c: d
} # for ({a: b({c: d})});

? See from @epidemian's examples that the whitespace is already special cased inside the brackets, because

[ a
  b ] # works
a()
  b() # doesn't

I couldn't find an open issue for:

{ a: b
  b: c } # ({a: b({c: d})});

but I argue it should be fixed alongside this. (following #3269; this issue was closed as a duplicate of the error message issue, not the original syntax issue)

@xixixao
Copy link
Contributor

xixixao commented Jan 22, 2014

So I meant to write this:

I'd like to open this and fix the original issue. Even with better error message (#3319) this still seems like a surprising behavior; and to have same rules to #3305.

here, and I see I have already asked to get this reopened.

Also mentioned here: #1769 (comment)

@jashkenas jashkenas reopened this Jan 22, 2014
@xixixao
Copy link
Contributor

xixixao commented Jan 28, 2014

Also

{ a: b,
  b: c }

should work as well.

@GeoffreyBooth GeoffreyBooth changed the title Possible bug in object literals identation Lexer: Indenting within object literal produces unexpected behavior, unhelpful error message May 6, 2017
@GeoffreyBooth
Copy link
Collaborator

Currently

{ a: 1
  b: 2 }

throws

[stdin]:2:6: error: unexpected indentation
  b: 2 }
     ^

which I believe is correct. The b line is indented when it shouldn’t be. It would be better if the caret pointed at the b, but that’s a quibble. The error message is specific enough.

As a general policy I don’t think we should be creating more exceptions where CoffeeScript ignores indentation. We’re a whitespace-significant language, and people just need to get used to that.

If you want the keys to start on the same column, just write it like:

{
  a: 1
  b: 2
}

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

8 participants