allowing comments in the middle of switch statements

This commit is contained in:
Jeremy Ashkenas 2010-01-09 13:25:44 -05:00
parent 2319affa61
commit 8e3922b6c6
3 changed files with 15 additions and 4 deletions

View File

@ -385,7 +385,7 @@ rule
LEADING_WHEN Expression Block { result = IfNode.new(val[1], val[2], nil, {:statement => true}) }
| LEADING_WHEN Expression Block
Terminator { result = IfNode.new(val[1], val[2], nil, {:statement => true}) }
| Comment
| Comment Terminator When { result = val[2].add_comment(val[0]) }
;
# The most basic form of "if".

View File

@ -769,6 +769,11 @@ module CoffeeScript
self
end
def add_comment(comment)
@comment = comment
self
end
def force_statement
@tags[:statement] = true
self
@ -795,7 +800,7 @@ module CoffeeScript
# The IfNode only compiles into a statement if either of the bodies needs
# to be a statement.
def statement?
@is_statement ||= !!(@tags[:statement] || @body.statement? || (@else_body && @else_body.statement?))
@is_statement ||= !!(@comment || @tags[:statement] || @body.statement? || (@else_body && @else_body.statement?))
end
def compile_node(o)
@ -811,7 +816,9 @@ module CoffeeScript
o[:indent] = idt(1)
o[:top] = true
if_dent = child ? '' : idt
if_part = "#{if_dent}if (#{@condition.compile(cond_o)}) {\n#{Expressions.wrap(@body).compile(o)}\n#{idt}}"
com_dent = child ? idt : ''
prefix = @comment ? @comment.compile(cond_o) + "\n#{com_dent}" : ''
if_part = "#{prefix}#{if_dent}if (#{@condition.compile(cond_o)}) {\n#{Expressions.wrap(@body).compile(o)}\n#{idt}}"
return if_part unless @else_body
else_part = chain? ?
" else #{@else_body.compile(o.merge(:indent => idt, :chain_child => true))}" :

View File

@ -7,7 +7,11 @@ result: switch num
true
false
when 10 then true
# Mid-switch comment with whitespace
# and multi line
when 11 then false
else false
print(result)