Skip to content

Commit

Permalink
Add support for Ruby 3.0 endless method definitions (fixes #1376).
Browse files Browse the repository at this point in the history
  • Loading branch information
rkh committed May 6, 2021
1 parent 3590066 commit 5bbbd9e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,7 @@
# main

- Add support for Ruby 3.0 endless method definitions. (#1376)

# 0.9.26 - December 26th, 2020

[0.9.26]: https://github.com/lsegal/yard/compare/v0.9.25...v0.9.26
Expand Down
3 changes: 3 additions & 0 deletions lib/yard.rb
Expand Up @@ -48,6 +48,9 @@ def self.ruby19?; @ruby19 ||= (RUBY_VERSION >= "1.9.1") end

# @return [Boolean] whether YARD is being run in Ruby 2.0
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
end

# Keep track of Ruby version for compatibility code
Expand Down
2 changes: 1 addition & 1 deletion lib/yard/handlers/ruby/method_handler.rb
Expand Up @@ -67,7 +67,7 @@ class YARD::Handlers::Ruby::MethodHandler < YARD::Handlers::Ruby::Base
end

def format_args
args = statement.parameters
return [] unless args = statement.parameters

params = []

Expand Down
4 changes: 2 additions & 2 deletions lib/yard/parser/ruby/ast_node.rb
Expand Up @@ -480,15 +480,15 @@ def method_name(name_only = false)
end

def parameters(include_block_param = true)
params = self[1 + index_adjust]
return unless params = self[1 + index_adjust]
params = params[0] if params.type == :paren
include_block_param ? params : params[0...-1]
end

def signature
params_src = ''
params = self[1 + index_adjust]
if params.first
if params and params.first
params_src = params.type == :paren ? '' : ' '
params_src += params.source.gsub(/\s+(\s|\))/m, '\1')
end
Expand Down
20 changes: 20 additions & 0 deletions spec/handlers/method_handler_spec.rb
Expand Up @@ -62,6 +62,26 @@ def multiline_params(x,
expect(P('Bar#multiline_params').signature).to eq sig
end if YARD.ruby2?

it "handles endless method definitions without parameters" do
YARD.parse_string <<-EOF
class Bar
def endless = true
end
EOF

expect(P('Bar#endless').signature).to eq "def endless"
end if YARD.ruby3?

it "handles endless method definitions with parameters" do
YARD.parse_string <<-EOF
class Bar
def endless_with_arg(arg = true) = true
end
EOF

expect(P('Bar#endless_with_arg').signature).to eq "def endless_with_arg(arg = true)"
end if YARD.ruby3?

it "handles method signature with no parameters" do
YARD.parse_string "class Bar; def foo; end end"
expect(P('Bar#foo').signature).to eq 'def foo'
Expand Down

0 comments on commit 5bbbd9e

Please sign in to comment.