0

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?

5
  • "After deploying an update ..." - seems like that would be something to look into. What sort of update was it? Commented Sep 16, 2013 at 20:16
  • An overall project deployment. During the dev time this script was recompiled. Well actually pretty much all the scripts were recompiled, but this is the only one that suddenly changed and broke. (That I am aware of. Cross your fingers) Commented Sep 16, 2013 at 20:19
  • 3
    You can also restore your original functionality with { label: year, value: year } for year in [endYear - yearsBack .. endYear]. Sounds like a "fix" to CoffeeScript broke your code. Pitfall of using a brand new language. Commented Sep 16, 2013 at 20:20
  • @meagar Thanks! That does do the trick nicely. Commented Sep 16, 2013 at 20:34
  • Well this appears to be by design. github.com/jashkenas/coffee-script/issues/… Commented Sep 16, 2013 at 21:46

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

1.6.3 now, not sure exactly when it was first deployed but it would have been about a year ago. 1.3.1 most likely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.