Skip to content

Commit

Permalink
Properly insert comment nodes and check for attached comments in prop…
Browse files Browse the repository at this point in the history
…er order
  • Loading branch information
lsegal committed Aug 7, 2012
1 parent 810505b commit a379c8a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/yard/parser/ruby/ruby_parser.rb
Expand Up @@ -539,8 +539,9 @@ def on_parse_error(msg)


def insert_comments def insert_comments
root.traverse do |node| root.traverse do |node|
next if node.type == :list || node.parent.type != :list next if node.type == :comment || node.type == :list || node.parent.type != :list
(node.line - 2).upto(node.line) do |line| # check upwards from line before node; check node's line at the end
((node.line-1).downto(node.line-2).to_a + [node.line]).each do |line|
comment = @comments[line] comment = @comments[line]
if comment && !comment.empty? if comment && !comment.empty?
add_comment(line, node) add_comment(line, node)
Expand Down
26 changes: 26 additions & 0 deletions spec/parser/ruby/ruby_parser_spec.rb
Expand Up @@ -87,6 +87,32 @@ def method2; end
ss[2].comments.should == 'hello' ss[2].comments.should == 'hello'
end end


it "should handle block comment followed by line comment" do
ss = stmts <<-eof
# comments1
=begin
comments2
=end
# comments3
def hello; end
eof
ss.last.comments.should == "comments3"
end

it "should handle block comment followed by block comment" do
ss = stmts <<-eof
=begin
comments1
=end
=begin
comments2
=end
def hello; end
eof
ss.last.comments.strip.should == "comments2"
end

it "should handle 1.9 lambda syntax with args" do it "should handle 1.9 lambda syntax with args" do
src = "->(a,b,c=1,*args,&block) { hello_world }" src = "->(a,b,c=1,*args,&block) { hello_world }"
stmt(src).source.should == src stmt(src).source.should == src
Expand Down

0 comments on commit a379c8a

Please sign in to comment.