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

Fix formatter to skip trailing comma for single-line parameters #14713

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
35 changes: 35 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,41 @@ describe Crystal::Formatter do
)
end
CRYSTAL

assert_format <<-CRYSTAL, flags: %w[def_trailing_comma]
def foo(a)
end
CRYSTAL

assert_format <<-CRYSTAL, flags: %w[def_trailing_comma]
def foo(a, b)
end
CRYSTAL

assert_format <<-CRYSTAL, flags: %w[def_trailing_comma]
def foo(a, *args)
end
CRYSTAL

assert_format <<-CRYSTAL, flags: %w[def_trailing_comma]
def foo(a, *args, &block)
end
CRYSTAL

assert_format <<-CRYSTAL, flags: %w[def_trailing_comma]
def foo(a, **kwargs)
end
CRYSTAL

assert_format <<-CRYSTAL, flags: %w[def_trailing_comma]
def foo(a, **kwargs, &block)
end
CRYSTAL

assert_format <<-CRYSTAL, flags: %w[def_trailing_comma]
def foo(a, &block)
end
CRYSTAL
end

assert_format "1 + 2", "1 + 2"
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1562,7 +1562,7 @@ module Crystal

args.each_with_index do |arg, i|
has_more = !last?(i, args) || double_splat || block_arg || yields || variadic
wrote_newline = format_def_arg(wrote_newline, has_more, true) do
wrote_newline = format_def_arg(wrote_newline, has_more, found_first_newline && !has_more) do
if i == splat_index
write_token :OP_STAR
skip_space_or_newline
Expand All @@ -1577,7 +1577,7 @@ module Crystal
end

if double_splat
wrote_newline = format_def_arg(wrote_newline, block_arg || yields, true) do
wrote_newline = format_def_arg(wrote_newline, block_arg || yields, found_first_newline) do
write_token :OP_STAR_STAR
skip_space_or_newline

Expand Down
Loading