jashkenas/coffee-script — Issue #1769: Issue with multiline object assignment as a static class property
Author: sernst
Created: 2011-10-13 16:39:57+00:00
State: open

I encountered an issue while trying to define a multiline object as a static method of a class. Here's an example:

```coffeescript
class Example

@STATIC_OBJECT = {
'a':1,
'b':2,
'c':3,
'd':4
}

constructor: () ->
alert('This is working!')
```

becomes:

```javascript
(function() {
var Example;
Example = (function() {
function Example() {}
Example.STATIC_OBJECT = {
'a': 1,
'b': 2,
'c': 3,
'd': 4
};
return Example;
})();
({
constructor: function() {
return alert('This is working!');
}
});
}).call(this);
```

Notice the premature return Example; and then the unassociated block that holds the constructor (and any other class members if they existed).

However, if I format the definition as follows:

```coffeescript
class Example

@STATIC_OBJECT = {
'a':1,
'b':2,
'c':3,
'd':4 }

constructor: () ->
alert('This is working!')
```
the resulting Javascript is correct:

```javascript
(function() {
var Example;
Example = (function() {
Example.STATIC_OBJECT = {
'a': 1,
'b': 2,
'c': 3,
'd': 4
};
function Example() {
alert('This is working!');
}
return Example;
})();
}).call(this);
```
The only next line indentation for the close bracket that seems to work is:

```coffeescript
class Example

@STATIC_OBJECT = {
'a':1,
'b':2,
'c':3,
'd':4
}

constructor: () ->
alert('This is working!')
```

which results in the correct:

```javascript
(function() {
var Example;
Example = (function() {
Example.STATIC_OBJECT = {
'a': 1,
'b': 2,
'c': 3,
'd': 4
};
function Example() {
alert('This is working!');
}
return Example;
})();
}).call(this);
```

This could just be a strictness of the whitespace interpreter in Coffeescript. I thought it worth mentioning, though, in case it wasn't intentional given that any of the above would work in Python.