got comments within object and array literals working out
This commit is contained in:
parent
65809d08f6
commit
ec58d6fda2
|
@ -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.
|
||||
|
|
|
@ -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 = ['{', '(', '[']
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
]
|
|
@ -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
|
||||
];
|
||||
})();
|
|
@ -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]
|
||||
|
|
Loading…
Reference in New Issue