Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix double return tag. #1226

Merged
merged 1 commit into from Mar 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
19 changes: 19 additions & 0 deletions 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
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the fix to handler_methods.rb this test still passed. And I don't quite understand why. I would have expected this to yield 2 and fail when YARD added an extra @return tag.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no extra tags on the method defined in this specific test, which would explain why the result hasn't changed.

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the test that catches the bug. I can dig deeper into why the other test didn't raise any errors before the fix.

end

it "handles casted method names" do
Expand Down