Unexpected 'INDENT' in CoffeeScript Exampl

2019-01-23 12:01发布

As I was playing around for the first time with CoffeeScript, I ran in to a problem. In order to debug my problem, I tried replacing my whole file with one of the example bits of code from the coffee script site:

kids =
  brother:
    name: "Max"
    age:  11
  sister:
    name: "Ida"
    age:  9

However, when I try to compile that code, I get:

Error: In coffee/main.coffee, Parse error on line 3: Unexpected 'INDENT'
    at Object.parseError (/usr/lib/coffeescript/parser.js:501:11)
    at Object.parse (/usr/lib/coffeescript/parser.js:573:32)
    at Object.compile (/usr/lib/coffeescript/coffee-script.js:23:22)
    at /usr/lib/coffeescript/command.js:99:27
    at /usr/lib/coffeescript/command.js:72:28
    at fs:84:13
    at node.js:773:9
In coffee/main.coffee, Parse error on line 3: Unexpected 'INDENT'

Since this is code from the CoffeeScript site, I assume the code itself isn't the problem. However, the compiler also seems to be working properly; if I compile:

a = 2

it generates a file with:

(function(){
  var a;
  a = 2;
})();

as expected. So in other words, the code is good, the compiler is good, and yet somehow I'm getting this Unexpected 'IDENT' error ... can anyone help me understand what is going on?

6条回答
做个烂人
2楼-- · 2019-01-23 12:03

If you are using a JetBrains IDE (IntelliJ, PHPStorm, etc) the change of setting that worked for me is:

File > Settings > Project Settings > Code Style > CoffeeScript > Tabs and Indents

Tick "Use tab character" & "Smart tabs"

查看更多
贪生不怕死
3楼-- · 2019-01-23 12:05

Code is fine. Make sure you haven't messed up the whitespace (strange control chars showing as blanks, tabs or similar).

查看更多
Ridiculous、
4楼-- · 2019-01-23 12:12

I am pretty sure this is a tabs-vs-spaces issue. Tell your editor not to convert spaces to tabs if it does that. Also, go through your code with the cursor and make sure it doesn't jump over blank areas.

The issue is that while normal editors see a tab as equivalent to two or four spaces, coffeescript sees it as one space, so the indentation gets messed up.

If this all doesn't help, make sure you have a recent coffeescript version, e.g. 1.1.0 or newer.

查看更多
欢心
5楼-- · 2019-01-23 12:15

If you have the same problem, but your indentation is okay,
then you must be suffering from bug 2868.

Basically, the error is misleading. Check for indentation
errors in the required files.

查看更多
Ridiculous、
6楼-- · 2019-01-23 12:23

You can resolve this two ways 1. IF using webstorm File->Default Settings as said above 2. Other workaround, is to use a different editor like Sublime, there u can press enter on earlier line and it will auto tab it for you with spaces

查看更多
疯言疯语
7楼-- · 2019-01-23 12:27

When in Atom you can automatically convert tabs to spaces:

Packages > Whitespace > Convert Tabs to Spaces

查看更多
登录 后发表回答