From 7c0660c30c0a6d34efa3d95dce936fc20c24ade1 Mon Sep 17 00:00:00 2001 From: ksss Date: Sun, 15 May 2022 23:12:51 +0900 Subject: [PATCH] Fix broken args after parsed decls --- lib/rbs/prototype/rb.rb | 8 ++++---- test/rbs/rb_prototype_test.rb | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/rbs/prototype/rb.rb b/lib/rbs/prototype/rb.rb index 0e1bd878e..d54e61745 100644 --- a/lib/rbs/prototype/rb.rb +++ b/lib/rbs/prototype/rb.rb @@ -518,15 +518,15 @@ def literal_to_type(node) Types::ClassInstance.new(name: type_name, args: [], location: nil) end when :ZLIST, :ZARRAY - BuiltinNames::Array.instance_type([untyped]) + BuiltinNames::Array.instance_type(untyped) when :LIST, :ARRAY elem_types = node.children.compact.map { |e| literal_to_type(e) } t = types_to_union_type(elem_types) - BuiltinNames::Array.instance_type([t]) + BuiltinNames::Array.instance_type(t) when :DOT2, :DOT3 types = node.children.map { |c| literal_to_type(c) } type = range_element_type(types) - BuiltinNames::Range.instance_type([type]) + BuiltinNames::Range.instance_type(type) when :HASH list = node.children[0] if list @@ -554,7 +554,7 @@ def literal_to_type(node) else key_type = types_to_union_type(key_types) value_type = types_to_union_type(value_types) - BuiltinNames::Hash.instance_type([key_type, value_type]) + BuiltinNames::Hash.instance_type(key_type, value_type) end when :CALL receiver, method_name, * = node.children diff --git a/test/rbs/rb_prototype_test.rb b/test/rbs/rb_prototype_test.rb index 9891338d9..c74d52ded 100644 --- a/test/rbs/rb_prototype_test.rb +++ b/test/rbs/rb_prototype_test.rb @@ -859,6 +859,23 @@ def hello: () -> untyped RBS end + def test_literal_to_type + parser = RBS::Prototype::RB.new + [ + [%{"abc"}, %{"abc"}], + [%{:abc}, %{:abc}], + [%{[]}, %{::Array[untyped]}], + [%{[true]}, %{::Array[true]}], + [%{1..2}, %{::Range[::Integer]}], + [%{{}}, %{::Hash[untyped, untyped]}], + [%{{a: nil}}, %{ { a: nil } }], + [%{{"a" => /b/}}, %{ ::Hash[::String, ::Regexp] }], + ].each do |rb, rbs| + node = RubyVM::AbstractSyntaxTree.parse(rb).children[2] + assert_equal RBS::Parser.parse_type(rbs), parser.literal_to_type(node) + end + end + if RUBY_VERSION >= '2.7' def test_argument_forwarding parser = RB.new