Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow new formatter styles for trailing comma and whitespace around proc literal #14726

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Blacksmoke16 marked this conversation as resolved.
Show resolved Hide resolved
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,)
Blacksmoke16 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading