Skip to content

Commit

Permalink
Handle rest/kwrest modifiers on overload arguments (#601)
Browse files Browse the repository at this point in the history
* Handle rest/kwrest modifiers on overload arguments

* Do not use endless Range (support older Rubies)
  • Loading branch information
grncdr authored Dec 8, 2022
1 parent b3ed7c0 commit 956b82a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/solargraph/pin/method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,14 @@ def overloads
closure: self,
# args: tag.parameters.map(&:first),
parameters: tag.parameters.map do |src|
name, decl = parse_overload_param(src.first)
Pin::Parameter.new(
location: location,
closure: self,
comments: tag.docstring.all.to_s,
name: src.first,
name: name,
presence: location ? location.range : nil,
decl: :arg
decl: decl
)
end,
comments: tag.docstring.all.to_s
Expand Down Expand Up @@ -240,6 +241,21 @@ def infer_from_iv api_map
return ComplexType::UNDEFINED if types.empty?
ComplexType.try_parse(*types.map(&:tag).uniq)
end

# When YARD parses an overload tag, it includes rest modifiers in the parameters names.
#
# @param arg [String]
# @return [Array(String, Symbol)]
def parse_overload_param(name)
if name.start_with?('**')
[name[2..-1], :kwrestarg]
elsif name.start_with?('*')
[name[1..-1], :restarg]
else
[name, :arg]
end
end

end
end
end
12 changes: 12 additions & 0 deletions spec/pin/method_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,18 @@ def bar
expect(overload.docstring.tag(:param).types).to eq(['Integer'])
end

it 'processes overload tags with restargs' do
pin = Solargraph::Pin::Method.new(name: 'foo', comments: %<
@overload foo(*bar)
@overload foo(**bar)
>)
expect(pin.overloads.length).to eq(2)
restarg_overload = pin.overloads.first
kwrestarg_overload = pin.overloads.last
expect(restarg_overload.parameters.first.decl).to eq(:restarg)
expect(kwrestarg_overload.parameters.first.decl).to eq(:kwrestarg)
end

it 'infers from nil return nodes' do
source = Solargraph::Source.load_string(%(
class Foo
Expand Down

0 comments on commit 956b82a

Please sign in to comment.