Skip to content

Commit

Permalink
Allow new formatter styles for trailing comma and whitespace around p…
Browse files Browse the repository at this point in the history
…roc 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 <straightshoota@gmail.com>
  • Loading branch information
Blacksmoke16 and straight-shoota committed Jun 22, 2024
1 parent 2c62b19 commit 884f382
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
50 changes: 45 additions & 5 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require "spec"
require "../../../src/compiler/crystal/formatter"

private def assert_format(input, output = input, strict = false, flags = nil, file = __FILE__, line = __LINE__)
it "formats #{input.inspect}", file, line do
private def assert_format(input, output = input, strict = false, flags = nil, file = __FILE__, line = __LINE__, focus = false)
it "formats #{input.inspect}", file, line, focus: focus do
output = "#{output}\n" unless strict
result = Crystal.format(input, flags: flags)
unless result == output
Expand Down Expand Up @@ -812,7 +812,7 @@ describe Crystal::Formatter do
end
CRYSTAL
def foo(x,
y)
y,)
yield
end
CRYSTAL
Expand Down Expand Up @@ -888,7 +888,7 @@ describe Crystal::Formatter do
end
CRYSTAL
def foo(
x
x,
)
yield
end
Expand All @@ -901,6 +901,39 @@ describe Crystal::Formatter do
CRYSTAL
end

# Allows trailing commas, but doesn't enforce them
assert_format <<-CRYSTAL
def foo(
a,
b
)
end
CRYSTAL

assert_format <<-CRYSTAL
def foo(
a,
b,
)
end
CRYSTAL

assert_format <<-CRYSTAL
macro foo(
a,
*b,
)
end
CRYSTAL

assert_format <<-CRYSTAL
macro foo(
a,
**b,
)
end
CRYSTAL

context "adds trailing comma to def multi-line normal, splat, and double splat parameters" do
assert_format <<-CRYSTAL, <<-CRYSTAL, flags: %w[def_trailing_comma]
macro foo(
Expand Down Expand Up @@ -1693,6 +1726,13 @@ describe Crystal::Formatter do
assert_format "-> : Int32 {}", "-> : Int32 { }", flags: %w[proc_literal_whitespace]
assert_format "->do\nend", "-> do\nend", flags: %w[proc_literal_whitespace]

# Allows whitespace around proc literal, but doesn't enforce them
assert_format "-> { }"
assert_format "-> { 1 }"
assert_format "->(x : Int32) { }"
assert_format "-> : Int32 { }"
assert_format "-> do\nend"

assert_format "-> : Int32 {}"
assert_format "-> : Int32 | String { 1 }"
assert_format "-> : Array(Int32) {}"
Expand All @@ -1703,7 +1743,7 @@ describe Crystal::Formatter do
assert_format "-> : {Int32} { String }"
assert_format "-> : {x: Int32, y: String} {}"
assert_format "->\n:\nInt32\n{\n}", "-> : Int32 {\n}"
assert_format "->( x )\n:\nInt32 { }", "->(x) : Int32 {}"
assert_format "->( x )\n:\nInt32 { }", "->(x) : Int32 { }"
assert_format "->: Int32 do\nx\nend", "-> : Int32 do\n x\nend"

{:+, :-, :*, :/, :^, :>>, :<<, :|, :&, :&+, :&-, :&*, :&**}.each do |sym|
Expand Down
7 changes: 4 additions & 3 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1651,7 +1651,7 @@ module Crystal
yield

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

just_wrote_newline = skip_space
if @token.type.newline?
Expand Down Expand Up @@ -4242,6 +4242,7 @@ module Crystal

def visit(node : ProcLiteral)
write_token :OP_MINUS_GT
whitespace_after_op_minus_gt = @token.type.space?
skip_space_or_newline

a_def = node.def
Expand Down Expand Up @@ -4272,15 +4273,15 @@ module Crystal
skip_space_or_newline
end

write " " if a_def.args.present? || return_type || flag?("proc_literal_whitespace")
write " " if a_def.args.present? || return_type || flag?("proc_literal_whitespace") || whitespace_after_op_minus_gt

is_do = false
if @token.keyword?(:do)
write_keyword :do
is_do = true
else
write_token :OP_LCURLY
write " " if a_def.body.is_a?(Nop) && flag?("proc_literal_whitespace")
write " " if a_def.body.is_a?(Nop) && (flag?("proc_literal_whitespace") || @token.type.space?)
end
skip_space

Expand Down

0 comments on commit 884f382

Please sign in to comment.