jashkenas/coffee-script — Issue #2071: Unexpected function compilation in conjunction with ?= assignment.
Author: jmalloc
Created: 2012-01-27 04:12:06+00:00
State: open

Hi there,

I'm having an issue when using ?= assignment with a function value when the ?= assignment is the last operation in a function.
I believe this to be a bug in CoffeeScript (I'm using 1.2.0).

```coffeescript
class TestClass

setup: (@func) ->
@func ?= ->
return 0 if true
return 1
```

Expected JavaScript (note the if(condition) line) ...

```javascript
var TestClass;

TestClass = (function() {

function TestClass() {}

TestClass.prototype.setup = function(func) {
var _ref;
this.func = func;
return (_ref = this.func) != null ? _ref : this.func = function(condition) {
if (condition) return 0;
return 1;
};
};

return TestClass;

})();
```

Actual JavaScript produced ...

```javascript
var TestClass;

TestClass = (function() {

function TestClass() {}

TestClass.prototype.setup = function(func) {
var _ref;
this.func = func;
return (_ref = this.func) != null ? _ref : this.func = function(condition) {
if (!condition) {}
return 1;
};
};

return TestClass;

})();
```

Placing a return after the ?= assignment seems to correct the issue, but also changes the behavior of setup() by not returning the result of the assignment.

Please let me know if I'm missing something :)