Skip to content

[Formatter] Add trailing comma after last element in multiline call argument list#17018

Draft
Sija wants to merge 4 commits into
crystal-lang:masterfrom
Sija:formatter-trailing-comma-in-call-argument-list
Draft

[Formatter] Add trailing comma after last element in multiline call argument list#17018
Sija wants to merge 4 commits into
crystal-lang:masterfrom
Sija:formatter-trailing-comma-in-call-argument-list

Conversation

@Sija

@Sija Sija commented May 27, 2026

Copy link
Copy Markdown
Contributor

Before

foo(
  1,
  2
)

After

foo(
  1,
  2,
)

Refs #13169
Refs crystal-ameba/ameba#833

@Sija Sija force-pushed the formatter-trailing-comma-in-call-argument-list branch from edfd61f to a32b907 Compare May 27, 2026 21:43
Comment thread src/compiler/crystal/tools/formatter.cr Outdated
@straight-shoota

Copy link
Copy Markdown
Member

This looks like the kind of formatter change that we should guard behind a flag and enable in bulk (ref. #13002).

@Sija Sija force-pushed the formatter-trailing-comma-in-call-argument-list branch 3 times, most recently from 2f9aa48 to 98034f0 Compare May 28, 2026 15:45
@Sija Sija force-pushed the formatter-trailing-comma-in-call-argument-list branch from 98034f0 to 032b3ca Compare May 28, 2026 16:52
@Sija

Sija commented May 28, 2026

Copy link
Copy Markdown
Contributor Author

I believe I've fixed all edge cases; This PR is ready for another round of review.

The matter of guarding this change behind the flag is left out for now, until we find a suitable way of moving forward with it.

@Sija Sija requested a review from straight-shoota May 28, 2026 16:57
assert_format "foo(\n 1,\n &block\n)"
assert_format "foo \\\n 1,\n bar: 2"
assert_format "foo \\\n 1"
assert_format "foo(\n 1,\n bar: <<-HEREDOC\n foo\n HEREDOC\n)"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Shouldn't we add a comma here?

Suggested change
assert_format "foo(\n 1,\n bar: <<-HEREDOC\n foo\n HEREDOC\n)"
assert_format "foo(\n 1,\n bar: <<-HEREDOC\n foo\n HEREDOC\n)", "foo(\n 1,\n bar: <<-HEREDOC,\n foo\n HEREDOC\n)"

Btw. we might want to use heredocs for the strings to makes the examples easier to read.

@Sija

Sija commented May 28, 2026

Copy link
Copy Markdown
Contributor Author

It seems I've found a bug in the current formatter (Crystal v1.20.2):

foo(
  <<-HEREDOC,
    foo
    HEREDOC
)

gets formatted to:

foo(
  <<-HEREDOC
    foo
    HEREDOC
,
)

The same happens for a named argument as well.

@Sija Sija marked this pull request as draft May 28, 2026 23:52
@Sija

Sija commented May 29, 2026

Copy link
Copy Markdown
Contributor Author

@straight-shoota Should I create a new issue re: the above formatter bug?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants