Skip to content

Commit

Permalink
Merge pull request #1431 from nvasilevski/support-ruby-3-args-forward…
Browse files Browse the repository at this point in the history
…-shape-change

Support ruby 3.1 :args_forward shape change
  • Loading branch information
lsegal committed Jun 1, 2022
2 parents 92bf2dc + 89b0513 commit 75b2437
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/yard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def self.ruby2?; @ruby2 ||= (RUBY_VERSION >= '2.0.0') end

# @return [Boolean] whether YARD is being run in Ruby 3.0
def self.ruby3?; @ruby3 ||= (RUBY_VERSION >= '3.0.0') end

# @return [Boolean] whether YARD is being run in Ruby 3.1
def self.ruby31?; @ruby31 ||= (RUBY_VERSION >= '3.1.0') end
end

# Keep track of Ruby version for compatibility code
Expand Down
2 changes: 1 addition & 1 deletion lib/yard/handlers/ruby/method_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def format_args
params << ['**' + args.double_splat_param.source, nil]
end

params << ['&' + args.block_param.source, nil] if args.block_param
params << ['&' + args.block_param.source, nil] if args.block_param && !args.args_forward

params
end
Expand Down
7 changes: 7 additions & 0 deletions lib/yard/parser/ruby/ast_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,13 @@ def double_splat_param
def block_param
self[-1] ? self[-1][0] : nil
end

def args_forward
# shape is (required, optional, rest, more, keyword, keyword_rest, block)
# Ruby 3.1 moves :args_forward from rest to keyword_rest
args_index = YARD.ruby31? ? -2 : 2
self[args_index].type == :args_forward if self[args_index]
end
end

class MethodCallNode < AstNode
Expand Down
24 changes: 24 additions & 0 deletions spec/handlers/method_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,30 @@ def endless = true
expect(P('Bar#endless').signature).to eq "def endless"
end if YARD.ruby3?

it "handles method with arguments forwarding" do
YARD.parse_string <<-EOF
class Bar
def method_with_forwarding(...)
forward_to(...)
end
end
EOF

expect(P('Bar#method_with_forwarding').signature).to eq "def method_with_forwarding(...)"
end

it "handles method with anonymous block" do
YARD.parse_string <<-EOF
class Bar
def anon_block_method(&)
baz(a, &)
end
end
EOF

expect(P('Bar#anon_block_method').signature).to eq "def anon_block_method(&)"
end if YARD.ruby31?

it "handles endless method definitions with parameters" do
YARD.parse_string <<-EOF
class Bar
Expand Down

0 comments on commit 75b2437

Please sign in to comment.