Skip to content

Commit

Permalink
Fix formatter to skip trailing comma for single-line parameters (#14713)
Browse files Browse the repository at this point in the history
  • Loading branch information
Blacksmoke16 authored Jun 16, 2024
1 parent ff01499 commit 6a2097d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
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

0 comments on commit 6a2097d

Please sign in to comment.