Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "cannot get the first element of beginless range" error #1

Merged
merged 1 commit into from
Jun 21, 2022

Conversation

bradfeehan
Copy link
Owner

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#1434.

@bradfeehan bradfeehan merged commit 099aa60 into main Jun 21, 2022
@bradfeehan bradfeehan deleted the fix-ast-node-open-range-error branch June 21, 2022 07:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Right hand assignment inside case/when/end causes cannot get the first element of beginless range (RangeError)
1 participant