Skip to content

Commit

Permalink
Merge pull request #2231 from tk0miya/Union.to_s
Browse files Browse the repository at this point in the history
Union#to_s should follow RBS/Lint/AmbiguousOperatorPrecedence
  • Loading branch information
soutaro authored Jan 15, 2025
2 parents 17b74e8 + f5e8af0 commit 5c48490
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
13 changes: 11 additions & 2 deletions lib/rbs/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -751,10 +751,19 @@ def sub(s)
end

def to_s(level = 0)
strs = types.map do |ty|
case ty
when Intersection
ty.to_s([1, level].max)
else
ty.to_s
end
end

if level > 0
"(#{types.join(" | ")})"
"(#{strs.join(" | ")})"
else
types.join(" | ")
strs.join(" | ")
end
end

Expand Down
4 changes: 2 additions & 2 deletions test/rbs/types_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ def test_to_s
assert_equal "(String | bool)?", parse_type("(String | bool)?").to_s
assert_equal "String & bool?", parse_type("String & bool?").to_s
assert_equal "(String & bool)?", parse_type("(String & bool)?").to_s
assert_equal "Integer | String & bool", parse_type("Integer | String & bool").to_s
assert_equal "Integer | (String & bool)", parse_type("Integer | String & bool").to_s
assert_equal "(Integer | String) & bool", parse_type("(Integer | String) & bool").to_s
assert_equal "(Integer | String & bool)?", parse_type("(Integer | String & bool)?").to_s
assert_equal "(Integer | (String & bool))?", parse_type("(Integer | String & bool)?").to_s
assert_equal "((Integer | String) & bool)?", parse_type("((Integer | String) & bool)?").to_s
assert_equal "^() -> void", parse_type("^() -> void").to_s
assert_equal "(^() -> void)?", parse_type("(^() -> void)?").to_s
Expand Down

0 comments on commit 5c48490

Please sign in to comment.