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

Support forwarding parameter #151

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 39 additions & 21 deletions lib/rbs/inline/ast/members.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def method_overloads(default_type)
required_keywords = {} #: Hash[Symbol, Types::Function::Param]
optional_keywords = {} #: Hash[Symbol, Types::Function::Param]
rest_keywords = nil #: Types::Function::Param?
forwarding_parameter = false

if node.parameters
node.parameters.requireds.each do |param|
Expand Down Expand Up @@ -231,17 +232,20 @@ def method_overloads(default_type)
end
end

if (kw_rest = node.parameters.keyword_rest).is_a?(Prism::KeywordRestParameterNode)
case node.parameters.keyword_rest
when Prism::KeywordRestParameterNode
double_splat_param_type = double_splat_param_type_annotation

if double_splat_param_type && double_splat_param_type.type
double_splat_type = double_splat_param_type.type
end

rest_keywords = Types::Function::Param.new(
name: kw_rest.name,
name: node.parameters.keyword_rest.name,
type: double_splat_type || default_type,
location: nil)
when Prism::ForwardingParameterNode
forwarding_parameter = true
end

if node.parameters.block
Expand All @@ -257,26 +261,40 @@ def method_overloads(default_type)
block = type
end

[
RBS::AST::Members::MethodDefinition::Overload.new(
method_type: RBS::MethodType.new(
type_params: [],
type: Types::Function.new(
required_positionals: required_positionals,
optional_positionals: optional_positionals,
rest_positionals: rest_positionals,
trailing_positionals: [],
required_keywords: required_keywords,
optional_keywords: optional_keywords,
rest_keywords: rest_keywords,
return_type: return_type || default_type
if forwarding_parameter
[
RBS::AST::Members::MethodDefinition::Overload.new(
method_type: RBS::MethodType.new(
type_params: [],
type: Types::UntypedFunction.new(return_type: return_type || default_type),
block: nil,
location: nil
),
block: block,
location: nil
),
annotations: []
)
]
annotations: []
)
]
else
[
RBS::AST::Members::MethodDefinition::Overload.new(
method_type: RBS::MethodType.new(
type_params: [],
type: Types::Function.new(
required_positionals: required_positionals,
optional_positionals: optional_positionals,
rest_positionals: rest_positionals,
trailing_positionals: [],
required_keywords: required_keywords,
optional_keywords: optional_keywords,
rest_keywords: rest_keywords,
return_type: return_type || default_type
),
block: block,
location: nil
),
annotations: []
)
]
end
end
end

Expand Down
32 changes: 32 additions & 0 deletions test/rbs/inline/writer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,38 @@ def i: (*String) -> untyped
RBS
end

def test_method_type__forwarding_parameter
output = translate(<<~RUBY)
class Foo
def f(...)
end

# @rbs return: Integer
def g(...)
end

def h(x, y, ...)
end

def i(kw1:, kw2:, ...)
end
end
RUBY

assert_equal <<~RBS, output
class Foo
def f: (?) -> untyped

# @rbs return: Integer
def g: (?) -> Integer

def h: (?) -> untyped

def i: (?) -> untyped
end
RBS
end

def test_method_type__double_splat
output = translate(<<~RUBY)
class Foo
Expand Down