diff --git a/lib/nodes.js b/lib/nodes.js index d3a6d041..bd0de8a8 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -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; })(); diff --git a/lib/rewriter.js b/lib/rewriter.js index d6e377be..480a76cc 100644 --- a/lib/rewriter.js +++ b/lib/rewriter.js @@ -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; diff --git a/src/nodes.coffee b/src/nodes.coffee index c0ca7359..2c2ba3aa 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -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 diff --git a/src/rewriter.coffee b/src/rewriter.coffee index 20991ef6..0cc497c3 100644 --- a/src/rewriter.coffee +++ b/src/rewriter.coffee @@ -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)