asawyer
asawyer

Reputation: 17818

Working loop is compiling to javascript incorrectly now

I've had the following coffeescript in production for a few months:

yearSource = (yearsBack) ->
    endYear = new Date().getFullYear()+1
    label: year, value: year for year in [endYear - yearsBack .. endYear]  

That compiled to this script:

yearSource = function(yearsBack) {
    var endYear, year, _i, _ref, _results;
    endYear = new Date().getFullYear() + 1;
    _results = [];
    for (year = _i = _ref = endYear - yearsBack; _ref <= endYear ? _i <= endYear : _i >= endYear; year = _ref <= endYear ? ++_i : --_i) {
      _results.push({
        label: year,
        value: year
      });
    }
    return _results;
  };

This returns a list of years from the given year forward.

After deploying an update where the scripts were recompiled and suddenly the script looks like this:

yearSource = function(yearsBack) {
    var endYear, year;

    endYear = new Date().getFullYear() + 1;
    return {
      label: year,
      value: (function() {
        var _i, _ref, _results;

        _results = [];
        for (year = _i = _ref = endYear - yearsBack; _ref <= endYear ? _i <= endYear : _i >= endYear; year = _ref <= endYear ? ++_i : --_i) {
          _results.push(year);
        }
        return _results;
      })()
    };
  };

Since the list is supposed to be the source for a jQuery UI Autocomplete widget this broke things pretty thoroughly.

I can fix it by moving the loop work to a new indented line:

yearSource = (yearsBack) ->
    endYear = new Date().getFullYear()+1
    for year in [endYear - yearsBack .. endYear]
        label: year, value: year

What was I doing wrong that caused things to break between coffeescript versions?

Upvotes: 0

Views: 61

Answers (1)

Adam Roben
Adam Roben

Reputation: 784

Do you know what version of CoffeeScript you were using previously and what version you're using now? That can help you track down exactly what changed in CoffeeScript to cause this.

But even without knowing the exact change, it's fairly easy to get a feel for what changed. Your code used to be parsed like this:

(label: year, value: year) for year in [endYear - yearsBack .. endYear]

But now it's getting parsed like this:

label: year, value: (year for year in [endYear - yearsBack .. endYear])

I.e., the precedence of the comprehension changed so that now only year is considered to be part of the comprehension.

Upvotes: 2

Related Questions