fixed up the comment/assignment interleaving in nodes.rb

This commit is contained in:
Jeremy Ashkenas
2009-12-30 20:24:24 -05:00
parent 5c7b77aa4d
commit c8711b419e
2 changed files with 31 additions and 24 deletions

View File

@@ -464,13 +464,20 @@ module CoffeeScript
@properties = properties
end
# All the mucking about with commas is to make sure that CommentNodes and
# AssignNodes get interleaved correctly, with no trailing commas or
# commas affixed to comments. TODO: Extract this and add it to ArrayNode.
def compile(o={})
o = super(o)
indent = o[:indent]
o[:indent] += TAB
joins = Hash.new("\n")
non_comments = @properties.select {|p| !p.is_a?(CommentNode) }
non_comments.each {|p| joins[p] = p == non_comments.last ? "\n" : ",\n" }
props = @properties.map { |prop|
joiner = prop == @properties.first ? '' : prop.is_a?(CommentNode) ? "\n" : ",\n"
joiner + o[:indent] + prop.compile(o)
join = joins[prop]
join = '' if prop == @properties.last
o[:indent] + prop.compile(o) + join
}.join('')
write("{\n#{props}\n#{indent}}")
end