Skip to content

Commit 884f382

Browse files
Allow new formatter styles for trailing comma and whitespace around proc literal (#14726)
In preparation of changing the formatter style, this change accepts the new style without enforcing it. Existing code is not affected (this will happen in a follow-up). Co-authored-by: Johannes Müller <[email protected]>
1 parent 2c62b19 commit 884f382

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

spec/compiler/formatter/formatter_spec.cr

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
require "spec"
22
require "../../../src/compiler/crystal/formatter"
33

4-
private def assert_format(input, output = input, strict = false, flags = nil, file = __FILE__, line = __LINE__)
5-
it "formats #{input.inspect}", file, line do
4+
private def assert_format(input, output = input, strict = false, flags = nil, file = __FILE__, line = __LINE__, focus = false)
5+
it "formats #{input.inspect}", file, line, focus: focus do
66
output = "#{output}\n" unless strict
77
result = Crystal.format(input, flags: flags)
88
unless result == output
@@ -812,7 +812,7 @@ describe Crystal::Formatter do
812812
end
813813
CRYSTAL
814814
def foo(x,
815-
y)
815+
y,)
816816
yield
817817
end
818818
CRYSTAL
@@ -888,7 +888,7 @@ describe Crystal::Formatter do
888888
end
889889
CRYSTAL
890890
def foo(
891-
x
891+
x,
892892
)
893893
yield
894894
end
@@ -901,6 +901,39 @@ describe Crystal::Formatter do
901901
CRYSTAL
902902
end
903903

904+
# Allows trailing commas, but doesn't enforce them
905+
assert_format <<-CRYSTAL
906+
def foo(
907+
a,
908+
b
909+
)
910+
end
911+
CRYSTAL
912+
913+
assert_format <<-CRYSTAL
914+
def foo(
915+
a,
916+
b,
917+
)
918+
end
919+
CRYSTAL
920+
921+
assert_format <<-CRYSTAL
922+
macro foo(
923+
a,
924+
*b,
925+
)
926+
end
927+
CRYSTAL
928+
929+
assert_format <<-CRYSTAL
930+
macro foo(
931+
a,
932+
**b,
933+
)
934+
end
935+
CRYSTAL
936+
904937
context "adds trailing comma to def multi-line normal, splat, and double splat parameters" do
905938
assert_format <<-CRYSTAL, <<-CRYSTAL, flags: %w[def_trailing_comma]
906939
macro foo(
@@ -1693,6 +1726,13 @@ describe Crystal::Formatter do
16931726
assert_format "-> : Int32 {}", "-> : Int32 { }", flags: %w[proc_literal_whitespace]
16941727
assert_format "->do\nend", "-> do\nend", flags: %w[proc_literal_whitespace]
16951728

1729+
# Allows whitespace around proc literal, but doesn't enforce them
1730+
assert_format "-> { }"
1731+
assert_format "-> { 1 }"
1732+
assert_format "->(x : Int32) { }"
1733+
assert_format "-> : Int32 { }"
1734+
assert_format "-> do\nend"
1735+
16961736
assert_format "-> : Int32 {}"
16971737
assert_format "-> : Int32 | String { 1 }"
16981738
assert_format "-> : Array(Int32) {}"
@@ -1703,7 +1743,7 @@ describe Crystal::Formatter do
17031743
assert_format "-> : {Int32} { String }"
17041744
assert_format "-> : {x: Int32, y: String} {}"
17051745
assert_format "->\n:\nInt32\n{\n}", "-> : Int32 {\n}"
1706-
assert_format "->( x )\n:\nInt32 { }", "->(x) : Int32 {}"
1746+
assert_format "->( x )\n:\nInt32 { }", "->(x) : Int32 { }"
17071747
assert_format "->: Int32 do\nx\nend", "-> : Int32 do\n x\nend"
17081748

17091749
{:+, :-, :*, :/, :^, :>>, :<<, :|, :&, :&+, :&-, :&*, :&**}.each do |sym|

src/compiler/crystal/tools/formatter.cr

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,7 +1651,7 @@ module Crystal
16511651
yield
16521652

16531653
# Write "," before skipping spaces to prevent inserting comment between argument and comma.
1654-
write "," if has_more || (write_trailing_comma && flag?("def_trailing_comma"))
1654+
write "," if has_more || (wrote_newline && @token.type.op_comma?) || (write_trailing_comma && flag?("def_trailing_comma"))
16551655

16561656
just_wrote_newline = skip_space
16571657
if @token.type.newline?
@@ -4242,6 +4242,7 @@ module Crystal
42424242

42434243
def visit(node : ProcLiteral)
42444244
write_token :OP_MINUS_GT
4245+
whitespace_after_op_minus_gt = @token.type.space?
42454246
skip_space_or_newline
42464247

42474248
a_def = node.def
@@ -4272,15 +4273,15 @@ module Crystal
42724273
skip_space_or_newline
42734274
end
42744275

4275-
write " " if a_def.args.present? || return_type || flag?("proc_literal_whitespace")
4276+
write " " if a_def.args.present? || return_type || flag?("proc_literal_whitespace") || whitespace_after_op_minus_gt
42764277

42774278
is_do = false
42784279
if @token.keyword?(:do)
42794280
write_keyword :do
42804281
is_do = true
42814282
else
42824283
write_token :OP_LCURLY
4283-
write " " if a_def.body.is_a?(Nop) && flag?("proc_literal_whitespace")
4284+
write " " if a_def.body.is_a?(Nop) && (flag?("proc_literal_whitespace") || @token.type.space?)
42844285
end
42854286
skip_space
42864287

0 commit comments

Comments
 (0)