Open kddnewton opened 8 months ago
I checked this with Ruby 3.3. & 3.2, and they also have the problem.
Refresher on ranges, 'endless' ranges were added in Ruby 2.6, 'beginless' ranges were added in Ruby 2.7. Range#first
and Range#last
will throw errors if the range is 'beginless' or 'endless', respectively. Both begin
and end
return nil
instead of raising an error. I think the original purpose was to make it easier to slice arrays.
The following patch seems to fix the errors, but I haven't had time to check further, nor write any tests...
diff --git a/lib/yard/parser/ruby/ast_node.rb b/lib/yard/parser/ruby/ast_node.rb
index c9deb3ed..ccfa9b6c 100644
--- a/lib/yard/parser/ruby/ast_node.rb
+++ b/lib/yard/parser/ruby/ast_node.rb
@@ -271,7 +271,7 @@ module YARD
# @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.
@@ -345,8 +345,8 @@ module YARD
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
diff --git a/lib/yard/parser/ruby/ruby_parser.rb b/lib/yard/parser/ruby/ruby_parser.rb
index a7ba5fab..b8eaa7c2 100644
--- a/lib/yard/parser/ruby/ruby_parser.rb
+++ b/lib/yard/parser/ruby/ruby_parser.rb
@@ -627,11 +627,13 @@ module YARD
end
# 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)
- break
+ if (n_l = node.line)
+ ((n_l - 1).downto(n_l - 2).to_a + [n_l]).each do |line|
+ comment = @comments[line]
+ if comment && !comment.empty?
+ add_comment(line, node)
+ break
+ end
end
end
Steps to reproduce
Actual Output
Expected Output
Parses successfully.
Environment details:
ruby -v
): ruby 3.4.0dev (2024-02-07T11:34:48Z master 9ebaf7a8ef) [arm64-darwin23]yard -v
): yard 0.9.35