Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support hash specific format sush as Hash{String => Integer} #12

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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