Coffeescript 1.7.1意外的换行怪异

时间:2014-03-12 15:44:02

标签: coffeescript

我有以下代码(大约一年),我用它来处理应用程序中的zipcodes。今天我不得不改变一个值,现在突然间我不能再编译它了。我在" h" in" hilleroed"。

class Application
    postal_codes:
        regions:
            sjaelland:[0..4999]
            fyn: [5000..5999]
            soenderjylland: [6000..6200]
                .concat [6261..6280]
                .concat [6600..6899]
                .concat [6041..6064]
                .concat [6960]
                .concat [7000..7079]
                .concat [7200]
                .concat [7250]
            nordmidtjylland: [6900..6999]
                .concat [7080..7199]
                .concat [7182..7184]
                .concat [7190..7190]
                .concat [7260..9999]

        office_postal:
            ringsted: 4100
            hilleroed: 3400
            kgslyngby: 2800
            odense: 5220
            haderslev: 6100
            esbjerg: 6715
            herning: 7400
            aalborg: 9200
            aarhus: 8210
            horsens: 8700

        offices:
            ringsted: [0..2690]
                .concat [2770,2791]
                .concat [4000..4030]
                .concat [4050..4990]

            hilleroed: [2700..2765]
                .concat [2980..3670]
                .concat [4040]

            kgslyngby: [2800..2970]

            odense: [5000..5985]

            haderslev: [6000..6240]
                .concat [6300..6622]
                .concat [6630..6640]
                .concat [7000..7080]
                .concat [7182..7184]

            esbjerg: [6261..6280]
                .concat [6623]
                .concat [6650..6879] # 6880 is herning.. meh
                .concat [6881..6893]
                .concat [6960]
                .concat [7200..7260]

            herning: [6880]
                .concat [6900..6950]
                .concat [6971..6990]
                .concat [7270..7280]
                .concat [7330..7680]
                .concat [7800..7884]
                .concat [8800]
                .concat [8831]

            aalborg: [7700..7790]
                .concat [7900..7990]
                .concat [8832]
                .concat [8970]
                .concat [9000..9999]

            aarhus: [8000..8270]
                .concat [8305..8340]
                .concat [8355..8643]
                .concat [8660..8670]
                .concat [8830]
                .concat [8840..8963]
                .concat [8981..8990]

            horsens: [7100..7173]
                .concat [7190]
                .concat [7300..7323]
                .concat [8300,8350]
                .concat [8653..8659]
                .concat [8680..8783]

我不能为我的生活弄清楚什么是错的。这是一个错误还是我错过了什么?如果我将代码粘贴到coffeescript.org上的在线编译器中,我会得到同样的错误。怎么解决?

编辑:

我正在使用咖啡脚本1.7.1。它适用于1.6.3。

更新:

这是一个意想不到的错误,请参阅https://github.com/jashkenas/coffee-script/issues/3408

1 个答案:

答案 0 :(得分:2)

问题出在office对象中。此代码失败:

offices:                           
  ringsted: [0..2690]              
    .concat [2770, 2791]           
    .concat [4000..4030]           
    .concat [4050..4990]           
  hilleroed: [2700..2765]          
    .concat [2980..3670]           
    .concat [4040]                 
  kgslyngby: [2800..2970]         

虽然这通过了:

offices:                                                                           
  ringsted: [0..2690].concat [2770, 2791].concat [4000..4030].concat [4050..4990]  
  hilleroed: [2700..2765].concat [2980..3670].concat [4040]                        
  kgslyngby: [2800..2970] 

如果您真的想保留换行符,请添加一些内容:

offices:                          
  ringsted: ([0..2690]            
    .concat [2770..2791]          
    .concat [4000..4030]          
    .concat [4050..4990])         
  hilleroed: ([2700..2765]        
    .concat [2980..3670]          
    .concat [4040])               
  kgslyngby: [2800..2970]       

看起来编译器对于对象定义的结束感到困惑。如果你遗漏了parens,你会得到这个js:

// without parens

var _i, _j, _k, _l, _results, _results1, _results2, _results3;

({
  offices: {
    ringsted: (function() {
      _results3 = [];
      for (_l = 0; _l <= 233; _l++){ _results3.push(_l); }
      return _results3;
    }).apply(this)
  }.concat((function() {
    _results2 = [];
    for (_k = 2770; _k <= 2791; _k++){ _results2.push(_k); }
    return _results2;
  }).apply(this)).concat((function() {
    _results1 = [];
    for (_j = 4000; _j <= 4030; _j++){ _results1.push(_j); }
    return _results1;
  }).apply(this)).concat((function() {
    _results = [];
    for (_i = 4050; _i <= 4990; _i++){ _results.push(_i); }
    return _results;
  }).apply(this))
});

当在节点repl中运行时,返回:

TypeError: Object #<Object> has no method 'concat'

这是有道理的,基于js,但根据咖啡没有意义。这可能值submitting as an issue

TLDR :当您希望将函数返回值分配给对象的字段时,请使用parens。