From 5bbbd9e1e1919528ff361a6052241cb863b8b77c Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Thu, 6 May 2021 17:13:21 +0200 Subject: [PATCH] Add support for Ruby 3.0 endless method definitions (fixes #1376). --- CHANGELOG.md | 2 ++ lib/yard.rb | 3 +++ lib/yard/handlers/ruby/method_handler.rb | 2 +- lib/yard/parser/ruby/ast_node.rb | 4 ++-- spec/handlers/method_handler_spec.rb | 20 ++++++++++++++++++++ 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 77589f568..0f683ff83 100644 --- a/CHANGELOG.md +++ b/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 diff --git a/lib/yard.rb b/lib/yard.rb index dd02af11a..6afd7a0a9 100644 --- a/lib/yard.rb +++ b/lib/yard.rb @@ -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 diff --git a/lib/yard/handlers/ruby/method_handler.rb b/lib/yard/handlers/ruby/method_handler.rb index f9de11a00..5030a9658 100644 --- a/lib/yard/handlers/ruby/method_handler.rb +++ b/lib/yard/handlers/ruby/method_handler.rb @@ -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 = [] diff --git a/lib/yard/parser/ruby/ast_node.rb b/lib/yard/parser/ruby/ast_node.rb index 84db76e14..0d19e115d 100644 --- a/lib/yard/parser/ruby/ast_node.rb +++ b/lib/yard/parser/ruby/ast_node.rb @@ -480,7 +480,7 @@ 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 @@ -488,7 +488,7 @@ def parameters(include_block_param = true) 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 diff --git a/spec/handlers/method_handler_spec.rb b/spec/handlers/method_handler_spec.rb index 6e06c7a87..6cfb1cc6c 100644 --- a/spec/handlers/method_handler_spec.rb +++ b/spec/handlers/method_handler_spec.rb @@ -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'