Skip to content

Fix formatter: commas and indentation after a heredoc or comment - #17761

Open
Sija wants to merge 5 commits into
crystal-lang:masterfrom
Sija:formatter-fix-heredoc-comma-placement
Open

Sija wants to merge 5 commits into
crystal-lang:masterfrom
Sija:formatter-fix-heredoc-comma-placement

Conversation

@Sija

@Sija Sija commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

The formatter wrote a heredoc's body as soon as the newline following it was read, so code that reads ahead before deciding on a comma would put the comma after the heredoc's terminator, producing invalid code.

Write the body when that newline is written or moved past instead, which lets the comma go on the line that starts the heredoc. This also makes the has_heredoc_in_line workaround for literals unnecessary.

# Before (invalid)
foo <<-H
  foo
  H
, &.bar

# After
foo <<-H,
  foo
  H
  &.bar

Fixes the following:

  • call and index arguments, including when followed by a block
  • foo <<-H, followed by &.bar on a later line
  • foo[1, # c followed by &.bar], which got the comma after the comment
  • [ or { literals with a comment after the comma, which got a double comma (<<-H,, # c)

Also fix the indentation of arguments, &.bar, &block, closing delimiters and comments before them, when a heredoc or comment has already ended the line. This changes the output for existing code without heredocs, too:

# Before
if 1
  foo(1, # c
    2  )
end

# After
if 1
  foo(1, # c
    2)
end

) before a block now follows the indentation of a multiline call chain:

# Before
a
  .bar(
    1
) { }

# After
a
  .bar(
    1
  ) { }

Resolves #10278

@Sija
Sija force-pushed the formatter-fix-heredoc-comma-placement branch from f0354a9 to aa08114 Compare November 3, 2026 19:38
@Sija Sija changed the title Formatter fix heredoc comma placement Fix formatter: trailing comma placement after a pending heredoc Nov 3, 2026
The formatter wrote a heredoc's body as soon as the newline following it
was read, so code that reads ahead before deciding on a comma would put
the comma after the heredoc's terminator, producing invalid code.

Write the body when that newline is written or moved past instead, which
lets the comma go on the line that starts the heredoc. This also makes
the `has_heredoc_in_line` workaround for literals unnecessary.

Fixes the following:

- call and index arguments, including when followed by a block
- `foo <<-H,` followed by `&.bar` on a later line
- `[` or `{` literals with a comment after the comma, which got a double
  comma (`<<-H,, # c`)

Also indent `&.bar` and `&block` when they start a new line after a
comment or a comma, instead of writing a single space or none. This
changes the output for existing code such as `foo 1, # c` followed by
`&.bar` or `&block`.

Assisted-by: Claude:claude-opus-5-5
@Sija
Sija force-pushed the formatter-fix-heredoc-comma-placement branch from aa08114 to ea4a904 Compare November 4, 2026 13:59
Sija added 4 commits November 4, 2026 15:26
Fixes the following:

- a blank line written before `)` when the last parameter of a `def` or
  `macro` has a heredoc as its default value
- a blank line added above a comment that follows a heredoc's terminator
  inside call parentheses
- `)` written at column 0 in nested code after a trailing comma, when a
  heredoc or comment had already ended the line, which was only corrected
  by formatting again
- a comment before `)` losing its indentation in `yield(...)` and other
  calls going through `format_parenthesized_args`

This makes all of these idempotent. The `)` fix also changes the output
for nested code without heredocs, such as `foo(1, # c` followed by `)`.

Assisted-by: Claude:claude-opus-5-5
Keep both behaviours in `Formatter#format_block`: the lookahead for a comment before `&.foo` skips any spaces and newlines, but stops at a newline with a pending heredoc.

Assisted-by: Claude:claude-opus-5-5
Fixes the following:

- a named argument written at column 0 after a heredoc followed by a comma and a comment
- `}` of a block written with a single space instead of the indentation when a heredoc or comment had already ended the line
- `&block` and `&.bar` written at column 0 after a comment following `(`, when there are no other arguments
- the first argument of `yield(...)` and annotations written at column 0 after a comment following `(`
- additional comment lines before `)` or `}` losing their indentation

Assisted-by: Claude:claude-opus-5-5
…redoc

`finish_args` and the code writing `)` before a block relied on whether a comment was found anywhere in the arguments, rather than on whether the line had already ended. Use `@wrote_newline` instead, which fixes the following:

- `)` and `]` written at column 0 in nested code after a heredoc or comment
- spaces written before `)` when an earlier argument was followed by a comment, such as `foo(1, # c` followed by `2)`
- `)` before a block not following the indentation of a multiline call chain

Also let `format_block` write the comma before `&.foo` inside an index call. It was written after a preceding heredoc or comment, producing invalid code.

Assisted-by: Claude:claude-opus-5-5
@Sija Sija changed the title Fix formatter: trailing comma placement after a pending heredoc Fix formatter: commas and indentation after a heredoc or comment Dec 3, 2026

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invalid formatting when using HEREDOC as the last parameter in a call with trailing comma

1 participant