且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

CoffeeScript示例代码中出现意外的“INDENT”

更新时间:2022-10-16 12:02:41

我很确定这是一个tabs-vs-spaces问题。告诉编辑不要将空格转换为制表符。此外,使用光标浏览您的代码,并确保它不会跳过空白区域。



问题是,虽然普通编辑器看到一个选项卡相当于两个或四个空格,coffeescript将其视为一个空格,因此缩进会混乱。



如果这一切都没有帮助,请确保您有一个最近的coffeescript版本,例如1.1.0或更新版本。


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?

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.