diff --git a/spec/compiler/formatter/formatter_spec.cr b/spec/compiler/formatter/formatter_spec.cr index dcd314c9e94a..45f91d00ea54 100644 --- a/spec/compiler/formatter/formatter_spec.cr +++ b/spec/compiler/formatter/formatter_spec.cr @@ -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)" @@ -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" @@ -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)" diff --git a/src/compiler/crystal/tools/formatter.cr b/src/compiler/crystal/tools/formatter.cr index 0aa62754b4ee..9b1729c383ad 100644 --- a/src/compiler/crystal/tools/formatter.cr +++ b/src/compiler/crystal/tools/formatter.cr @@ -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 :"|" @@ -1284,7 +1287,6 @@ module Crystal break end end - break if must_break end check_close_paren