Skip to content

Parser trips on hard tabs with misleading error message: Unexpected '{' #2141

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

Closed
dkamins opened this issue Feb 26, 2012 · 9 comments
Closed
Labels

Comments

@dkamins
Copy link

dkamins commented Feb 26, 2012

Presence of hard tabs in certain places in .coffee source causes confusing and irrelevant parser error: Unexpected '{'

Tested with v1.2.0.

Specifically see example when object property line with tab is followed by regularly space-indented property line ("Test 2" below).

Test case with proper tabs preserved: http://pastebin.ca/2121616

Or test case inline below, but github converted the tab character to spaces, so you must convert it back yourself:

# Tabs vs. Spaces in CoffeeScript v1.2.0
#
# This file contains both TABS and SPACES - that's the whole point!
# Copy/Paste carefully.

# Test 1:
ok_in_this_order:
  spaces_on_this_line: 2
    tabs_on_this_line: 1

# Test 2:
confuses_parser_in_this_order:
    tabs_on_this_line: 1
  spaces_on_this_line: 2

# Test 2 yields the confusing message on compilation:
#  Parse error on line 7: Unexpected '{'

# Error: In test.coffee, Parse error on line 7: Unexpected '{'
#     at Object.parseError (/opt/local/lib/node_modules/coffee-script/lib/coffee-script/parser.js:470:11)
#     at Object.parse (/opt/local/lib/node_modules/coffee-script/lib/coffee-script/parser.js:546:22)
#     at Object.compile (/opt/local/lib/node_modules/coffee-script/lib/coffee-script/coffee-script.js:40:22)
#     at /opt/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:140:33
#     at /opt/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:110:18
#     at [object Object].<anonymous> (fs.js:115:5)
#     at [object Object].emit (events.js:64:17)
#     at afterRead (fs.js:1112:12)
#     at Object.wrapper [as oncomplete] (fs.js:254:17)
@michaelficarra
Copy link
Collaborator

Mixed spaces and tabs should be disallowed by #1275 anyway.

@andrewrk
Copy link

how about an immediate syntax error when any tabs are encountered at all in the entire file?

@michaelficarra
Copy link
Collaborator

Why? Tabs are valid indentation characters. It's the mixing of tabs and spaces that is the problem.

@andrewrk
Copy link

It's highly recommended to use 2-spaces for indentation in coffee-script, correct? Why not enforce it at the compiler level? It would simplify everybody's lives, including the compiler.

@michaelficarra
Copy link
Collaborator

Some people like tabs. We shouldn't disallow tabs if they're used consistently and unambiguously. Tabs are not evil. Just don't mix spaces/tabs. Or get a text editor smart enough to tell you when there's a problem or just handle the indentation for you.

@andrewrk
Copy link

Some people like mixing tabs and spaces too. I don't care either way, I just want it to be enforced so I don't have to deal with more than one style.

It's a pretty radical idea though, probably not a good idea for Coffee-Script's goals, so I'll drop it.

@jashkenas
Copy link
Owner

So -- spaces and tabs can be mixed ... if you use the same number of them for each level of indentation. Your example above does not do this, hence the indentation error.

CoffeeScript just counts tabs and counts spaces -- you can't possibly expect it to guess how wide your text editor is configured to visually show tab characters.

@andrewrk
Copy link

This issue is not that the examples should compile. This issue is that the error message is not helpful.

@dkamins
Copy link
Author

dkamins commented Feb 26, 2012

It turns out that the tabs vs. spaces is not even relevant, and the misleading parser error comes even with pure spaces. I agree with the closure of this (my) issue, because there is now too much noise in here. Based on jashkenas explanation of how tabs are treated, I've reproduced the behavior (bad error message) using only spaces, and filed a new issue. Please see now #2144.

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

4 participants