Skip to content

Formatter moves a line comment in a switch/if/loop operand into the block #9060

Closed

Description

@cknitt

A line comment inside the operand of switch, if, while, for, for..of, for await..of or try moves when formatting: first after the {, then on the next run into the block. Formatting is not idempotent.

switch obj // c
.x {
| _ => ()
}

First run:

switch obj.x { // c
| _ => ()
}

Second run, the comment is now in the block:

switch obj.x {
// c
| _ => ()
}

The same happens with if, while, for..of, for await..of, try (where the comment moves past catch) and with these operands:

  • obj // c\n.x.y and obj // c\n.x // d\n.y
  • arr[// c\n0], obj[// c\n"member"], obj["member"] // c\n{
  • obj // c\n->f
  • a // c\n+ b
  • for i in 0 // c\nto n {: the comment moves past to n and into the loop body

switch f(// c\nx) { is fine, because the arguments group breaks after the comment.

Expected: the comment stays in the operand, for example

switch (
  obj.x // c
) {
| _ => ()
}

Cause: the printer queues a trailing line comment as a line suffix and prints it at the next line break of a breaking group. The field access, object get, pipe or binary expression is printed flat with no line break after the comment, so the first break is the one after the block's {.

Found while reviewing #8906, which is about a related case, but this bug is already on master.

Activity

  1. added theissue type on Oct 27, 2026
  2. added a commit that references this issue on Oct 27, 2026
    051894f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions