Semantic Diff

#9061 Keep a line comment in a block construct's operand in the operand

4 commits

1a49d918eb73d0b058655608cfb2c2f4e6af4e1f..pr-9061

e5db72d2a · cknitt + Claude · Oct 27, 2026, 07:03 UTC

Keep a line comment in a block construct's operand in the operand

Why

A trailing line comment is printed at the next line break of a breaking group. In a flat operand such as obj.x, obj["m"] or obj->f, that is the break after the block's {, so formatting moved the comment after the { and on the next run into the block. When the comment is still pending at the end of the operand of switch, if, while, for, for..of, for await..of or try, the operand now goes into parentheses with the comment before the ).

modifiedFormat switch operand

switch obj // c.x {| _ => ()}
rescript
Before first run, then second run
switch obj.x { // c| _ => ()}
⇝
switch obj.x {// c| _ => ()}
After stable
switch (  obj.x // c) {| _ => ()}

Review

  • Does code that formatted stably before change? No: an operand only gets parentheses when a line comment in it is still queued at its end, which always moved the comment. No existing snapshot changes.
8bf8c32c2 · cknitt + Claude · Oct 27, 2026, 08:14 UTC

Keep a line comment in an operand that has its own parentheses

Why

A coercion or type constraint operand is printed in its own parentheses. A comment still pending at its end got a second pair around them and was printed after the inner ). That position is not adjacent to the operand, so the next format attached the comment to the first case and moved it into the block (#9060).

Such an operand now breaks its own parentheses when a comment is pending, which keeps the comment before the ). A while condition with its own parentheses no longer gets the extra pair it adds when a condition breaks.

modifiedFormat parenthesized switch operand

switch (f(  a, // c  b,) :> int) {| _ => ()}
rescript
Before first run, then second run
switch (  (f(a, b) :> int) // c) {| _ => ()}
⇝
switch (f(a, b) :> int) {// c
| _ => ()}
After stable
switch (  f(a, b) :> int // c) {| _ => ()}
631dbaf5c · cknitt + Claude · Oct 27, 2026, 10:35 UTC

Print the operands of switch, try and loops with one helper

The operands of switch, try, for, for..of and for await..of were each printed by the same block: the expression, its parentheses or braces, and print_operand_before_brace. print_block_operand does that once; if and while keep their own condition printing. The output is unchanged.

364e15225 · cknitt + Claude · Oct 27, 2026, 10:36 UTC

Test line comments after the items of a block construct's operand

These cases from the dropped #8906 guard the parentheses against a comment after an item of an array, list, tuple, record or call, after a pipe's receiver, and before the operator of a binary expression or pipe inside an item, under switch, if, while, for..of and try. Each formats the same again.