Skip to content

Commit

Permalink
Merge pull request #12 from euglena1215/support-hash-specific-syntax
Browse files Browse the repository at this point in the history
Support hash specific format sush as Hash{String => Integer}
  • Loading branch information
euglena1215 authored Jan 7, 2024
2 parents 8b69d50 + f2bcdf7 commit 26b469e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [Unreleased]

- Some refactoring https://github.com/euglena1215/packwerk-yard/pull/11
- Support hash specific format sush as Hash{String => Integer} https://github.com/euglena1215/packwerk-yard/pull/12

## [0.2.0] - 2024-01-07

Expand Down
7 changes: 6 additions & 1 deletion lib/packwerk_yard/constantize_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ class ConstantizeType
private_constant :ARRAY_REGEXP

# Hash Syntax e.g. Hash<String, String>
HASH_REGEXP = T.let(/\AHash<([^,]*),\s?(.*)>/.freeze, Regexp)
HASH_REGEXP = T.let(/\AHash<([^,\s]+)\s*,\s*(.+)>/.freeze, Regexp)
private_constant :HASH_REGEXP

# Hash Syntax e.g. Hash{String => String}
HASH_SPECIFIC_REGEXP = T.let(/\AHash\{([^,\s]+)\s*=>\s*(.+)}/.freeze, Regexp)
private_constant :HASH_SPECIFIC_REGEXP

sig { params(yard_type: String).void }
def initialize(yard_type)
@yard_type = yard_type
Expand All @@ -37,6 +41,7 @@ def constantize?(name)
def split_type(type)
matched_types = Array(ARRAY_REGEXP.match(type).to_a[1])
matched_types = Array(HASH_REGEXP.match(type).to_a[1..2]) if matched_types.empty?
matched_types = Array(HASH_SPECIFIC_REGEXP.match(type).to_a[1..2]) if matched_types.empty?
matched_types.empty? ? [type] : matched_types.map { |t| split_type(t) }.flatten
end
end
Expand Down
8 changes: 8 additions & 0 deletions test/unit/packwerk_yard/constantize_type_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ def test_types_returns_constantize_types_with_nested_hash
assert_equal(["Integer", "String"], types)
end

def test_types_returns_constantize_types_with_hash_specific_format
types = PackwerkYard::ConstantizeType.new("Hash{Integer => String}").types
assert_equal(["Integer", "String"], types)

types = PackwerkYard::ConstantizeType.new("Hash{Integer=>String}").types
assert_equal(["Integer", "String"], types)
end

def test_types_returns_constantize_types_with_not_existing_class
types = PackwerkYard::ConstantizeType.new("NotExistsClass").types

Expand Down
17 changes: 17 additions & 0 deletions test/unit/packwerk_yard/yard_handler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,22 @@ def bar(foo)
assert_equal(["String", "Integer"], handler.param_types)
assert_equal(["String", "Integer"], handler.return_types)
end

def test_from_source_with_hash_specific_format
code = <<~RUBY
class Foo
# @param [Hash{Integer => String}] foo
# @return [Hash{Integer=>String}]
def bar(foo)
foo
end
end
RUBY

handler = PackwerkYard::YardHandler.from_source(code)

assert_equal(["Hash{Integer => String}"], handler.param_types)
assert_equal(["Hash{Integer=>String}"], handler.return_types)
end
end
end

0 comments on commit 26b469e

Please sign in to comment.