From 650f8a72356cf226b569c5b98415dd6c13652a15 Mon Sep 17 00:00:00 2001 From: ksss Date: Tue, 1 Aug 2023 11:33:54 +0900 Subject: [PATCH] Support type params for known signatures --- lib/rbs/prototype/runtime.rb | 17 +++++++++--- test/rbs/runtime_prototype_test.rb | 44 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/lib/rbs/prototype/runtime.rb b/lib/rbs/prototype/runtime.rb index ec398bce3a..34f5caca27 100644 --- a/lib/rbs/prototype/runtime.rb +++ b/lib/rbs/prototype/runtime.rb @@ -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: [], @@ -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: [], @@ -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: [], @@ -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: [], @@ -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' diff --git a/test/rbs/runtime_prototype_test.rb b/test/rbs/runtime_prototype_test.rb index 08f22a9bdc..1e55b33e07 100644 --- a/test/rbs/runtime_prototype_test.rb +++ b/test/rbs/runtime_prototype_test.rb @@ -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