1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

Fixes Issue #542. Ensure that top-level objects are parenthesized. It's not valid code, but we might as well not SyntaxError out.

This commit is contained in:
Jeremy Ashkenas 2010-08-10 22:16:51 -04:00
parent 87fd05afb0
commit c2ec40e6ce
4 changed files with 14 additions and 7 deletions

View file

@ -703,8 +703,12 @@
__extends(ObjectNode, BaseNode);
ObjectNode.prototype["class"] = 'ObjectNode';
ObjectNode.prototype.children = ['properties'];
ObjectNode.prototype.topSensitive = function() {
return true;
};
ObjectNode.prototype.compileNode = function(o) {
var _b, _c, _d, _e, _f, _g, _h, i, indent, inner, join, lastNoncom, nonComments, prop, props;
var _b, _c, _d, _e, _f, _g, _h, i, indent, join, lastNoncom, nonComments, obj, prop, props, top;
top = del(o, 'top');
o.indent = this.idt(1);
nonComments = (function() {
_b = []; _d = this.properties;
@ -737,8 +741,8 @@
return _f;
}).call(this);
props = props.join('');
inner = props ? '\n' + props + '\n' + this.idt() : '';
return '{' + inner + '}';
obj = '{' + (props ? '\n' + props + '\n' + this.idt() : '') + '}';
return top ? ("(" + (obj) + ")") : obj;
};
return ObjectNode;
})();

View file

@ -145,7 +145,7 @@
before = this.tokens[i - 2];
if (token[0] === ':' && (!last || last[0] !== '{')) {
stack.push('{');
idx = before[0] === '@' ? i - 2 : i - 1;
idx = before && before[0] === '@' ? i - 2 : i - 1;
this.tokens.splice(idx, 0, ['{', '{', token[2]]);
condition = function(token, i) {
var _c, _d, _e, one, three, two;

View file

@ -623,10 +623,13 @@ exports.ObjectNode = class ObjectNode extends BaseNode
class: 'ObjectNode'
children: ['properties']
topSensitive: -> true
constructor: (props) ->
@objects = @properties = props or []
compileNode: (o) ->
top = del o, 'top'
o.indent = @idt 1
nonComments = prop for prop in @properties when not (prop instanceof CommentNode)
lastNoncom = nonComments[nonComments.length - 1]
@ -638,8 +641,8 @@ exports.ObjectNode = class ObjectNode extends BaseNode
prop = new AssignNode prop, prop, 'object' unless prop instanceof AssignNode or prop instanceof CommentNode
indent + prop.compile(o) + join
props = props.join('')
inner = if props then '\n' + props + '\n' + @idt() else ''
'{' + inner + '}'
obj = '{' + (if props then '\n' + props + '\n' + @idt() else '') + '}'
if top then "(#{obj})" else obj
#### ArrayNode

View file

@ -133,7 +133,7 @@ exports.Rewriter = class Rewriter
before = @tokens[i - 2]
if token[0] is ':' and (not last or last[0] isnt '{')
stack.push '{'
idx = if before[0] is '@' then i - 2 else i - 1
idx = if before and before[0] is '@' then i - 2 else i - 1
@tokens.splice idx, 0, ['{', '{', token[2]]
condition = (token, i) ->
[one, two, three] = @tokens.slice(i + 1, i + 4)