Skip to content

Commit

Permalink
Support type params for known signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
ksss committed Aug 11, 2023
1 parent 21009c9 commit c236a4e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lib/rbs/prototype/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def generate_class(mod)
unless decl
decl = AST::Declarations::Class.new(
name: to_type_name(only_name(mod)),
type_params: [],
type_params: type_params(mod),
super_class: generate_super_class(mod),
members: [],
annotations: [],
Expand Down Expand Up @@ -449,7 +449,7 @@ def generate_module(mod)
unless decl
decl = AST::Declarations::Module.new(
name: to_type_name(only_name(mod)),
type_params: [],
type_params: type_params(mod),
self_types: [],
members: [],
annotations: [],
Expand Down Expand Up @@ -504,7 +504,7 @@ def ensure_outer_module_declarations(mod)
if outer_module.is_a?(Class)
outer_decl = AST::Declarations::Class.new(
name: to_type_name(outer_module_name),
type_params: [],
type_params: type_params(outer_module),
super_class: generate_super_class(outer_module),
members: [],
annotations: [],
Expand All @@ -514,7 +514,7 @@ def ensure_outer_module_declarations(mod)
else
outer_decl = AST::Declarations::Module.new(
name: to_type_name(outer_module_name),
type_params: [],
type_params: type_params(outer_module),
self_types: [],
members: [],
annotations: [],
Expand Down Expand Up @@ -562,6 +562,15 @@ def type_args(type_name)
end
end

def type_params(mod)
type_name = to_type_name(const_name(mod), full_name: true)
if class_decl = env.class_decls[type_name.absolute!]
class_decl.type_params
else
[]
end
end

def block_from_ast_of(method)
return nil if RUBY_VERSION < '3.1'

Expand Down
44 changes: 44 additions & 0 deletions test/rbs/runtime_prototype_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -474,4 +474,48 @@ def test_nameerror_message
end
end
end

class TestTypeParams
class TestTypeParams
end
end

def test_type_params
SignatureManager.new do |manager|
manager.files[Pathname("foo.rbs")] = <<~RBS
module RBS
class RuntimePrototypeTest < ::Test::Unit::TestCase
class TestTypeParams[unchecked out Elem]
class TestTypeParams
end
end
end
end
RBS

manager.build do |env|
p = Runtime.new(patterns: ["RBS::RuntimePrototypeTest::TestTypeParams"], env: env, merge: true)
assert_write p.decls, <<~RBS
module RBS
class RuntimePrototypeTest < ::Test::Unit::TestCase
class TestTypeParams[unchecked out Elem]
end
end
end
RBS

p = Runtime.new(patterns: ["RBS::RuntimePrototypeTest::TestTypeParams::TestTypeParams"], env: env, merge: true)
assert_write p.decls, <<~RBS
module RBS
class RuntimePrototypeTest < ::Test::Unit::TestCase
class TestTypeParams[unchecked out Elem]
class TestTypeParams
end
end
end
end
RBS
end
end
end
end

0 comments on commit c236a4e

Please sign in to comment.