Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coffeescript 1.7.1 unexpected newline weirdness

Tags:

coffeescript

I have the following code (about a year old), which i use for working with zipcodes in an application. Today i had to change a value, and now all of a sudden i can't compile it anymore. I get an unexpected newline at the "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]

I can't for the life of me figure out what's wrong. Is this a bug or am i missing something? If i paste the code into the online compiler on coffeescript.org i get the same error. How to fix?

EDIT:

I am using coffee-script 1.7.1. It works in 1.6.3.

UPDATE:

This is an unintended bug, see https://github.com/jashkenas/coffee-script/issues/3408

like image 277
Mikkel Schmidt Avatar asked Mar 12 '14 15:03

Mikkel Schmidt


1 Answers

The issue is in the offices object. This code fails:

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

While this passes:

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

If you really want to keep the line breaks, add some parens:

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

It appears that the compiler is confused about where your object definitions end. If you leave out the parens you'll get this 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))
});

Which, when run in the node repl, returns:

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

Which makes sense, based on the js, but doesn't make sense based on the coffee. This might be worth submitting as an issue.

TLDR: use parens when you want function return values to be assigned to a field of an object.

like image 65
jcollum Avatar answered Oct 02 '22 12:10

jcollum