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 1, 2023
1 parent 4642668 commit 650f8a7
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 @@ -393,7 +393,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 @@ -447,7 +447,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 @@ -502,7 +502,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 @@ -512,7 +512,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 @@ -550,6 +550,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 @@ -460,4 +460,48 @@ def test_basic_object
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 650f8a7

Please sign in to comment.