diff --git a/lib/coffee_script/grammar.y b/lib/coffee_script/grammar.y index 4b88bbc7..7a81a84a 100644 --- a/lib/coffee_script/grammar.y +++ b/lib/coffee_script/grammar.y @@ -118,6 +118,7 @@ rule # Assignment within an object literal. AssignObj: IDENTIFIER ":" Expression { result = AssignNode.new(val[0], val[2], :object) } + | Comment { result = val[0] } ; # A return statement. diff --git a/lib/coffee_script/lexer.rb b/lib/coffee_script/lexer.rb index 8a832a59..89a6eabf 100644 --- a/lib/coffee_script/lexer.rb +++ b/lib/coffee_script/lexer.rb @@ -32,7 +32,7 @@ module CoffeeScript # Token cleaning regexes. JS_CLEANER = /(\A`|`\Z)/ MULTILINER = /\n/ - COMMENT_CLEANER = /^\s*#/ + COMMENT_CLEANER = /(^\s*#|\n\s*$)/ # Tokens that always constitute the start of an expression. EXP_START = ['{', '(', '['] diff --git a/lib/coffee_script/nodes.rb b/lib/coffee_script/nodes.rb index 6f25b44f..ef86d4bc 100644 --- a/lib/coffee_script/nodes.rb +++ b/lib/coffee_script/nodes.rb @@ -423,7 +423,10 @@ module CoffeeScript o = super(o) indent = o[:indent] o[:indent] += TAB - props = @properties.map {|p| o[:indent] + p.compile(o) }.join(",\n") + props = @properties.map { |prop| + joiner = prop == @properties.last ? '' : prop.is_a?(CommentNode) ? "\n" : ",\n" + o[:indent] + prop.compile(o) + joiner + }.join('') write("{\n#{props}\n#{indent}}") end end @@ -438,8 +441,12 @@ module CoffeeScript def compile(o={}) o = super(o) - objects = @objects.map {|obj| obj.compile(o) }.join(', ') - write("[#{objects}]") + objects = @objects.map { |obj| + joiner = obj.is_a?(CommentNode) ? "\n#{o[:indent] + TAB}" : obj == @objects.last ? '' : ', ' + obj.compile(o.merge(:indent => o[:indent] + TAB)) + joiner + }.join('') + ending = objects.include?("\n") ? "\n#{o[:indent]}]" : ']' + write("[#{objects}#{ending}") end end diff --git a/test/fixtures/inner_comments.cs b/test/fixtures/inner_comments.cs new file mode 100644 index 00000000..48121ffd --- /dev/null +++ b/test/fixtures/inner_comments.cs @@ -0,0 +1,15 @@ +object: { + a: 1 + # Comments between the elements. + b: 2 + # Like this. + c: 3 +} + +array: [ + 1 + # Comments between the elements. + 2 + # Like this. + 3 +] \ No newline at end of file diff --git a/test/fixtures/inner_comments.js b/test/fixtures/inner_comments.js new file mode 100644 index 00000000..1b4386d2 --- /dev/null +++ b/test/fixtures/inner_comments.js @@ -0,0 +1,15 @@ +(function(){ + var object = { + a: 1, + // Comments between the elements. + b: 2, + // Like this. + c: 3 + }; + var array = [1, + // Comments between the elements. + 2, + // Like this. + 3 + ]; +})(); \ No newline at end of file diff --git a/test/unit/test_parser.rb b/test/unit/test_parser.rb index 4a906edc..45d6ff09 100644 --- a/test/unit/test_parser.rb +++ b/test/unit/test_parser.rb @@ -57,6 +57,11 @@ class ParserTest < Test::Unit::TestCase assert nodes[1].is_a? CommentNode end + def test_parsing_inner_comments + nodes = @par.parse(File.read('test/fixtures/inner_comments.cs')) + assert nodes.compile == File.read('test/fixtures/inner_comments.js') + end + def test_parsing nodes = @par.parse(File.read('test/fixtures/each.cs')) assign = nodes.expressions[1]