lsegal / yard

YARD is a Ruby Documentation tool. The Y stands for "Yay!"
http://yardoc.org
MIT License
1.95k stars 398 forks source link

Fix right hand assignment inside case/when/end causes cannot get the first element of beginless range (RangeError) #1447

Closed bradfeehan closed 1 year ago

bradfeehan commented 2 years ago

Description

This implements the fix suggested by @akimd here:

I believe the proper fix is in lib/yard/parser/ruby/ast_node.rb to replace:

def line
  line_range && line_range.first
end

by

def line
  line_range && line_range.begin
end

since begin always returns the "first" element, as nil if it does not exist (while first dies if begin is nil).

However in that case yardoc dies farther:

/opt/local/lib/ruby3.1/gems/3.1.0/gems/yard-0.9.27/lib/yard/parser/ruby/ruby_parser.rb:624:in `block in insert_comments': undefined method `-' for nil:NilClass (NoMethodError)

            ((node.line - 1).downto(node.line - 2).to_a + [node.line]).each do |line|
                        ^
  from /opt/local/lib/ruby3.1/gems/3.1.0/gems/yard-0.9.27/lib/yard/parser/ruby/ast_node.rb:212:in `traverse'
  from /opt/local/lib/ruby3.1/gems/3.1.0/gems/yard-0.9.27/lib/yard/parser/ruby/ruby_parser.rb:615:in `insert_comments'

so it looks like after all it does not like line to return nil, contrary to what the && seems to imply. So I made it

def line
   line_range && line_range.begin || 1
end

and this time yardoc finishes properly.

Fixes lsegal/yard#1434.

Describe your pull request and problem statement here.

Completed Tasks