diff --git a/lib/coffee_script/grammar.js b/lib/coffee_script/grammar.js index 1f21d5a5..42fcdaac 100644 --- a/lib/coffee_script/grammar.js +++ b/lib/coffee_script/grammar.js @@ -234,7 +234,7 @@ ], // A Parameter (or ParamSplat) in a function definition. Param: [o("PARAM", function() { - return yytext; + return new LiteralNode(yytext); }), o("Param . . .", function() { return new SplatNode($1); }) diff --git a/lib/coffee_script/nodes.js b/lib/coffee_script/nodes.js index 49b7af85..0d74df3d 100644 --- a/lib/coffee_script/nodes.js +++ b/lib/coffee_script/nodes.js @@ -239,10 +239,10 @@ }, // Is the node last in this block of expressions? is_last: function is_last(node) { - var l; + var l, last_index; l = this.expressions.length; - this.last_index = this.last_index || this.expressions[l - 1] instanceof CommentNode ? 2 : 1; - return node === this.expressions[l - this.last_index]; + last_index = this.expressions[l - 1] instanceof CommentNode ? 2 : 1; + return node === this.expressions[l - last_index]; }, compile: function compile(o) { o = o || { @@ -437,7 +437,7 @@ }, compile_node: function compile_node(o) { var delimiter; - delimiter = "\n" + this.idt() + '//'; + delimiter = this.idt() + '//'; return delimiter + this.lines.join(delimiter); } })); @@ -642,7 +642,7 @@ // AssignNodes get interleaved correctly, with no trailing commas or // commas affixed to comments. TODO: Extract this and add it to ArrayNode. compile_node: function compile_node(o) { - var __a, __b, __c, __d, __e, i, indent, join, last_noncom, non_comments, prop, props; + var __a, __b, __c, __d, __e, i, indent, inner, join, last_noncom, non_comments, prop, props; o.indent = this.idt(1); non_comments = (function() { __a = []; __b = this.properties; @@ -673,7 +673,9 @@ } return __d; }).call(this); - return '{\n' + props.join('') + '\n' + this.idt() + '}'; + props = props.join(''); + inner = props ? '\n' + props + '\n' + this.idt() : ''; + return '{' + inner + '}'; } })); // An array literal. @@ -885,11 +887,14 @@ SplatNode = (exports.SplatNode = inherit(Node, { type: 'Splat', constructor: function constructor(name) { + if (!(name.compile)) { + name = new LiteralNode(name); + } this.children = [(this.name = name)]; return this; }, compile_node: function compile_node(o) { - return this.index ? this.compile_param(o) : this.name.compile(o); + return (typeof this.index !== "undefined" && this.index !== null) ? this.compile_param(o) : this.name.compile(o); }, compile_param: function compile_param(o) { var name; @@ -1261,7 +1266,7 @@ compile_condition: function compile_condition(o) { var __a, __b, __c, cond; return ((function() { - __a = []; __b = flatten(this.condition); + __a = []; __b = flatten([this.condition]); for (__c = 0; __c < __b.length; __c++) { cond = __b[__c]; __a.push(cond.compile(o)); @@ -1284,7 +1289,7 @@ if_dent = child ? '' : this.idt(); com_dent = child ? this.idt() : ''; prefix = this.comment ? this.comment.compile(cond_o) + '\n' + com_dent : ''; - body = Expressions.wrap([body]).compile(o); + body = Expressions.wrap([this.body]).compile(o); if_part = prefix + if_dent + 'if (' + this.compile_condition(cond_o) + ') {\n' + body + '\n' + this.idt() + '}'; if (!(this.else_body)) { return if_part; diff --git a/src/grammar.coffee b/src/grammar.coffee index ec78723e..7d4bff8d 100644 --- a/src/grammar.coffee +++ b/src/grammar.coffee @@ -217,7 +217,7 @@ grammar: { # A Parameter (or ParamSplat) in a function definition. Param: [ - o "PARAM", -> yytext + o "PARAM", -> new LiteralNode(yytext) o "Param . . .", -> new SplatNode($1) ] diff --git a/src/nodes.coffee b/src/nodes.coffee index 8509be16..3b435e80 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -144,8 +144,8 @@ Expressions: exports.Expressions: inherit Node, { # Is the node last in this block of expressions? is_last: (node) -> l: @expressions.length - @last_index ||= if @expressions[l - 1] instanceof CommentNode then 2 else 1 - node is @expressions[l - @last_index] + last_index: if @expressions[l - 1] instanceof CommentNode then 2 else 1 + node is @expressions[l - last_index] compile: (o) -> o ||= {} @@ -310,7 +310,7 @@ CommentNode: exports.CommentNode: inherit Node, { this compile_node: (o) -> - delimiter: "\n" + @idt() + '//' + delimiter: @idt() + '//' delimiter + @lines.join(delimiter) } @@ -515,7 +515,9 @@ ObjectNode: exports.ObjectNode: inherit Node, { join: '' if i is non_comments.length - 1 indent: if prop instanceof CommentNode then '' else @idt(1) indent + prop.compile(o) + join - '{\n' + props.join('') + '\n' + @idt() + '}' + props: props.join('') + inner: if props then '\n' + props + '\n' + @idt() else '' + '{' + inner + '}' } @@ -682,11 +684,12 @@ SplatNode: exports.SplatNode: inherit Node, { type: 'Splat' constructor: (name) -> + name: new LiteralNode(name) unless name.compile @children: [@name: name] this compile_node: (o) -> - if @index then @compile_param(o) else @name.compile(o) + if @index? then @compile_param(o) else @name.compile(o) compile_param: (o) -> name: @name.compile(o) @@ -988,7 +991,7 @@ IfNode: exports.IfNode: inherit Node, { @statement ||= !!(@comment or @tags.statement or @body.is_statement() or (@else_body and @else_body.is_statement())) compile_condition: (o) -> - (cond.compile(o) for cond in flatten(@condition)).join(' || ') + (cond.compile(o) for cond in flatten([@condition])).join(' || ') compile_node: (o) -> if @is_statement() then @compile_statement(o) else @compile_ternary(o) @@ -1004,7 +1007,7 @@ IfNode: exports.IfNode: inherit Node, { if_dent: if child then '' else @idt() com_dent: if child then @idt() else '' prefix: if @comment then @comment.compile(cond_o) + '\n' + com_dent else '' - body: Expressions.wrap([body]).compile(o) + body: Expressions.wrap([@body]).compile(o) if_part: prefix + if_dent + 'if (' + @compile_condition(cond_o) + ') {\n' + body + '\n' + @idt() + '}' return if_part unless @else_body else_part: if @is_chain()