Skip to content

Commit

Permalink
Make ASTNode#line 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 e96ae99 commit c545e24
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/yard/parser/ruby/ast_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def has_line?

# @return [Fixnum] the starting line number of the node
def line
line_range && line_range.first
line_range && (line_range.begin || line_range.end)
end

# @return [String] the first line of source represented by the node.
Expand Down
14 changes: 14 additions & 0 deletions spec/parser/ruby/ast_node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,18 @@
" source: 4..6))\n"
end
end unless YARD.ruby31?

describe "#line" 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\nbye", "x").ast
# obj.range returns (2..2) in this case, but sometimes, this range is set
# to a beginless or endless one.
obj.line_range = Range.new(nil, 2)
expect(obj.line).to eq 2
obj.line_range = Range.new(2, nil)
expect(obj.line).to eq 2
end
end
end if HAVE_RIPPER

0 comments on commit c545e24

Please sign in to comment.