Skip to content

Commit

Permalink
Merge pull request #168 from ohai/support-single-overload
Browse files Browse the repository at this point in the history
Support single overload
  • Loading branch information
AaronC81 committed May 22, 2023
2 parents 6e174ea + b5ec946 commit e60b829
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/sord/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,15 @@ def add_methods(item)
next
end

# If the method has YARD's "@overload" tags, the information of the
# first one is merged into meth object
# NOTE: This code does not handle the second and subsequent @overload tags
if meth.tag("overload")
meth.parameters = meth.tag("overload").parameters
meth.tag("overload").tags.each { |tag| meth.add_tag(tag) }
meth.docstring += meth.tag("overload").docstring
end

# Sort parameters
meth.parameters.reverse.sort! { |pair1, pair2| sort_params(pair1, pair2) }

Expand Down
53 changes: 53 additions & 0 deletions spec/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2017,4 +2017,57 @@ def z(a: -> {}); end
end
RUBY
end

it 'works with YARD\'s overload tag' do
YARD.parse_string(<<-RUBY)
class A
# @overload x(a, b)
# @param a [String]
# @param b [Integer]
# @return [void]
def x(*args); end
end
RUBY

expect(rbi_gen.generate.strip).to eq fix_heredoc(<<-RUBY)
# typed: strong
class A
# _@param_ `a`
#
# _@param_ `b`
sig { params(a: String, b: Integer).void }
def x(a, b); end
end
RUBY
end

it 'works with YARD\'s overload tag with toplevel return tag' do
YARD.parse_string(<<-RUBY)
class A
# Comment for method x
# @overload x(a, b)
# Overload comment
# @param a [String]
# @param b [Integer]
# @return [Integer] example integer
def x(*args); end
end
RUBY

expect(rbi_gen.generate.strip).to eq fix_heredoc(<<-RUBY)
# typed: strong
class A
# Comment for method x
# Overload comment
#
# _@param_ `a`
#
# _@param_ `b`
#
# _@return_ — example integer
sig { params(a: String, b: Integer).returns(Integer) }
def x(a, b); end
end
RUBY
end
end

0 comments on commit e60b829

Please sign in to comment.