From d2efef7bff85da7f27d7eb2c3931cb23f0eda26a Mon Sep 17 00:00:00 2001 From: George Dietrich Date: Fri, 14 Jun 2024 13:18:53 -0400 Subject: [PATCH] Ensure trailing commas are not written when parameters are not multi-line --- spec/compiler/formatter/formatter_spec.cr | 35 +++++++++++++++++++++++ src/compiler/crystal/tools/formatter.cr | 4 +-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/spec/compiler/formatter/formatter_spec.cr b/spec/compiler/formatter/formatter_spec.cr index e4e76279f4d5..7bb7a1034e72 100644 --- a/spec/compiler/formatter/formatter_spec.cr +++ b/spec/compiler/formatter/formatter_spec.cr @@ -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" diff --git a/src/compiler/crystal/tools/formatter.cr b/src/compiler/crystal/tools/formatter.cr index 614ecc836637..dc14c70a90ad 100644 --- a/src/compiler/crystal/tools/formatter.cr +++ b/src/compiler/crystal/tools/formatter.cr @@ -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 @@ -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