Skip to content

Commit

Permalink
Formatter: Allow trailing space in parenthesized unions (#10595)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil authored Apr 9, 2021
1 parent b0edf58 commit 0d0e0b2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 3 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,7 @@ describe Crystal::Formatter do

assert_format "foo : (A) | D"
assert_format "foo : (F(A)) | D"
assert_format "foo : ( A | B )", "foo : (A | B)"

assert_format "def foo(x : (A | B)) \n end", "def foo(x : (A | B))\nend"
assert_format "foo : (String -> String?) | (String)"
Expand All @@ -1074,6 +1075,7 @@ describe Crystal::Formatter do
assert_format "alias A = (B(C, (C | D)) | E)"
assert_format "alias A = ((B(C | D) | E) | F)"
assert_format "alias A = ({A, (B)})"
assert_format "alias A = ( A | B )", "alias A = (A | B)"

assert_format "foo : A(B)\nbar : C"
assert_format "foo : (A -> B)\nbar : C"
Expand All @@ -1083,6 +1085,7 @@ describe Crystal::Formatter do
assert_format "def foo : (A, B) ->\n nil\nend"
assert_format "def foo : (A | B(C))\n nil\nend"
assert_format "def foo : A | B(C)\n nil\nend"
assert_format "def foo(x : ( A | B )) : ( A | B )\nend", "def foo(x : (A | B)) : (A | B)\nend"

assert_format "foo &.bar.is_a?(Baz)"
assert_format "foo &.bar.responds_to?(:baz)"
Expand Down
8 changes: 5 additions & 3 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1259,9 +1259,12 @@ module Crystal
accept type

last = last?(i, node.types)
skip_space_or_newline unless last
if last
skip_space
else
skip_space_or_newline
end

must_break = false
while true
case @token.type
when :"|"
Expand All @@ -1284,7 +1287,6 @@ module Crystal
break
end
end
break if must_break
end

check_close_paren
Expand Down

0 comments on commit 0d0e0b2

Please sign in to comment.