Skip to content

Commit

Permalink
Fix double return tag.
Browse files Browse the repository at this point in the history
  • Loading branch information
thomthom committed Mar 1, 2019
1 parent 565343f commit 4bb63b3
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
5 changes: 5 additions & 0 deletions lib/yard/autoload.rb
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/yard/handlers/c/handler_methods.rb
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions lib/yard/handlers/common/method_handler.rb
@@ -0,0 +1,18 @@
# frozen_string_literal: true
# Shared functionality between Ruby and C method handlers.
module YARD::Handlers
module Common
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
10 changes: 3 additions & 7 deletions 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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions spec/handlers/c/method_handler_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 4bb63b3

Please sign in to comment.