From 53fdc77081ac1ce215f8758747b79c2df0fc477f Mon Sep 17 00:00:00 2001 From: Teppei Shintani Date: Tue, 11 Jun 2024 09:49:23 +0900 Subject: [PATCH] Support Hash type --- CHANGELOG.md | 2 ++ lib/rbs_inline_data/parser.rb | 2 +- test/rbs_inline_data/parser_test.rb | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00600be..cb3ce7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## [Unreleased] +- Support Hash type + ## [0.1.0] - 2024-06-09 - Initial release diff --git a/lib/rbs_inline_data/parser.rb b/lib/rbs_inline_data/parser.rb index 5d31696..775cbb2 100644 --- a/lib/rbs_inline_data/parser.rb +++ b/lib/rbs_inline_data/parser.rb @@ -86,7 +86,7 @@ def extract_definition(node) fields = field_text.split("\n").map(&:strip).reject(&:empty?).map do |str| case str - when /:(\w+),? #:: ([\w\:\[\]]+)/ + when /:(\w+),? #:: ([\w\:\[\], ]+)/ [::Regexp.last_match(1), ::Regexp.last_match(2)] when /:(\w+),?/ [::Regexp.last_match(1), "untyped"] diff --git a/test/rbs_inline_data/parser_test.rb b/test/rbs_inline_data/parser_test.rb index 2b70a46..ff4a355 100644 --- a/test/rbs_inline_data/parser_test.rb +++ b/test/rbs_inline_data/parser_test.rb @@ -105,5 +105,21 @@ class C; end Parser::TypedField.new(field_name: "y", type: "Array[A::B::C]"), ]) end + + def test_hash_type + definitions = Parser.parse(parse_ruby(<<~RUBY)) + class A + B = Data.define( + :x, #:: Hash[Symbol, String] + ) + end + RUBY + + assert_equal definitions[0], Parser::TypedDefinition.new( + class_name: "A::B", + fields: [ + Parser::TypedField.new(field_name: "x", type: "Hash[Symbol, String]"), + ]) + end end end