diff --git a/lib/yard/autoload.rb b/lib/yard/autoload.rb index 19ccac49d..d00371c64 100644 --- a/lib/yard/autoload.rb +++ b/lib/yard/autoload.rb @@ -64,6 +64,11 @@ module CodeObjects # parsing phase. This allows YARD as well as any custom extension to # analyze source and generate {CodeObjects} to be stored for later use. module Handlers + # Shared logic between C and Ruby handlers. + module Common + autoload :MethodHandler, __p('handlers/common/method_handler') + end + # CRuby Handlers # @since 0.8.0 module C diff --git a/lib/yard/handlers/c/handler_methods.rb b/lib/yard/handlers/c/handler_methods.rb index 47b9397a2..9f1769b78 100644 --- a/lib/yard/handlers/c/handler_methods.rb +++ b/lib/yard/handlers/c/handler_methods.rb @@ -5,6 +5,7 @@ module C module HandlerMethods include Parser::C include CodeObjects + include Common::MethodHandler def handle_class(var_name, class_name, parent, in_module = nil) parent = nil if parent == "0" @@ -67,7 +68,7 @@ def handle_method(scope, var_name, name, func_name, _source_file = nil) register_visibility(obj, visibility) find_method_body(obj, func_name) obj.explicit = true - obj.add_tag(Tags::Tag.new(:return, '', 'Boolean')) if name =~ /\?$/ + add_predicate_return_tag(obj) if name =~ /\?$/ end end diff --git a/lib/yard/handlers/common/method_handler.rb b/lib/yard/handlers/common/method_handler.rb new file mode 100644 index 000000000..3df668ea3 --- /dev/null +++ b/lib/yard/handlers/common/method_handler.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module YARD::Handlers + module Common + # Shared functionality between Ruby and C method handlers. + module MethodHandler + # @param [MethodObject] obj + def add_predicate_return_tag(obj) + if obj.tag(:return) && (obj.tag(:return).types || []).empty? + obj.tag(:return).types = ['Boolean'] + elsif obj.tag(:return).nil? + unless obj.tags(:overload).any? {|overload| overload.tag(:return) } + obj.add_tag(YARD::Tags::Tag.new(:return, "", "Boolean")) + end + end + end + end + end +end diff --git a/lib/yard/handlers/ruby/method_handler.rb b/lib/yard/handlers/ruby/method_handler.rb index 66a7a368d..2099a4ee6 100644 --- a/lib/yard/handlers/ruby/method_handler.rb +++ b/lib/yard/handlers/ruby/method_handler.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true # Handles a method definition class YARD::Handlers::Ruby::MethodHandler < YARD::Handlers::Ruby::Base + include YARD::Handlers::Common::MethodHandler + handles :def, :defs process do @@ -39,13 +41,7 @@ class YARD::Handlers::Ruby::MethodHandler < YARD::Handlers::Ruby::Base extended method_added method_removed method_undefined).include?(meth) obj.add_tag(YARD::Tags::Tag.new(:private, nil)) elsif meth.to_s =~ /\?$/ - if obj.tag(:return) && (obj.tag(:return).types || []).empty? - obj.tag(:return).types = ['Boolean'] - elsif obj.tag(:return).nil? - unless obj.tags(:overload).any? {|overload| overload.tag(:return) } - obj.add_tag(YARD::Tags::Tag.new(:return, "", "Boolean")) - end - end + add_predicate_return_tag(obj) end if obj.has_tag?(:option) diff --git a/spec/handlers/c/method_handler_spec.rb b/spec/handlers/c/method_handler_spec.rb index 4bb97a589..51fee3128 100644 --- a/spec/handlers/c/method_handler_spec.rb +++ b/spec/handlers/c/method_handler_spec.rb @@ -213,6 +213,7 @@ foo = Registry.at('Foo#foo?') expect(foo.docstring).to eq 'DOCSTRING' expect(foo.tag(:return).types).to eq ['Boolean'] + expect(foo.tags(:return).size).to eq 1 end it "does not add return tag if return tags exist" do @@ -226,6 +227,7 @@ eof foo = Registry.at('Foo#foo?') expect(foo.tag(:return).types).to eq ['String'] + expect(foo.tags(:return).size).to eq 1 end it "handles casted method names" do