Skip to content

Commit

Permalink
Make ASTNode#reset_line_info support beginless and endless ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
stackmystack committed Sep 12, 2024
1 parent c545e24 commit f520f8d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/yard/parser/ruby/ast_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ def reset_line_info
elsif !children.empty?
f = children.first
l = children.last
self.line_range = Range.new(f.line_range.first, l.line_range.last)
self.source_range = Range.new(f.source_range.first, l.source_range.last)
self.line_range = Range.new(f.line_range.begin, l.line_range.end)
self.source_range = Range.new(f.source_range.begin, l.source_range.end)
elsif @fallback_line || @fallback_source
self.line_range = @fallback_line
self.source_range = @fallback_source
Expand Down
19 changes: 19 additions & 0 deletions spec/parser/ruby/ast_node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,23 @@
expect(obj.line).to eq 2
end
end

describe "#reset_line_info" do
it "does not break on beginless or endless ranges" do
skip "Unsupported ruby version" if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.7')

obj = YARD::Parser::Ruby::RubyParser.parse("# x\ndef fn(arg); 42; end", "x").ast
obj = obj.first
expect(obj.children.size).to eq 3
fst = obj.children.first
lst = obj.children.last
fst.line_range = Range.new(nil, 10)
fst.source_range = Range.new(nil, 10)
lst.line_range = Range.new(2, nil)
lst.source_range = Range.new(2, nil)
obj.send(:reset_line_info)
expect(obj.line_range).to eq Range.new(nil, nil)
expect(obj.source_range).to eq Range.new(nil, nil)
end
end
end if HAVE_RIPPER

0 comments on commit f520f8d

Please sign in to comment.