Permalink
Browse files

Properly insert comment nodes and check for attached comments in prop…

…er order
  • Loading branch information...
1 parent 810505b commit a379c8af9c94aa47e02f5f3967ab1830a9eca97b @lsegal committed Aug 7, 2012
Showing with 29 additions and 2 deletions.
  1. +3 −2 lib/yard/parser/ruby/ruby_parser.rb
  2. +26 −0 spec/parser/ruby/ruby_parser_spec.rb
View
5 lib/yard/parser/ruby/ruby_parser.rb
@@ -539,8 +539,9 @@ def on_parse_error(msg)
def insert_comments
root.traverse do |node|
- next if node.type == :list || node.parent.type != :list
- (node.line - 2).upto(node.line) do |line|
+ next if node.type == :comment || node.type == :list || node.parent.type != :list
+ # 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]
if comment && !comment.empty?
add_comment(line, node)
View
26 spec/parser/ruby/ruby_parser_spec.rb
@@ -87,6 +87,32 @@ def method2; end
ss[2].comments.should == 'hello'
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
src = "->(a,b,c=1,*args,&block) { hello_world }"
stmt(src).source.should == src

0 comments on commit a379c8a

Please sign in to comment.