Semantic Diff

#8906 Keep line comments in operands followed by a block

3 commits

6562b42cdeca6b4e1c2eb425120f0e4505203f0d..pr-8906

603b12805 · cknitt + Claude · Oct 18, 2026, 18:28 UTC

Keep line comments in operands followed by a block

Why

#9061 keeps a line comment in the operand of switch, try, if, while or a loop by putting the operand into parentheses when Doc.has_pending_line_suffix finds the comment still queued at its end. A comment after an item or argument then moves to the end of the operand, away from the item it describes.

The operand now first breaks the groups around such a comment (Doc.break_before_line_suffixes), so the comment stays after its item and the parentheses are only needed when it is still pending. A binary expression is left flat: breaking it would flush the comment after the operator, where the next format attaches it to the right operand, so it keeps #9061's parentheses. In an operand with its own parentheses, such as a coercion, the comment also stays after its item inside them (operandLineComment.res).

modifiedFormat line comment in a switch operand

let x = switch [  a, // c  b,] {| _ => 1}
rescript
Before
After
let x = switch ( [a, b] // c) { let x = switch [ a, // c b,] {| _ => 1| _ => 1}}

Review

  • Why not make every same-line line comment break its group? That changes code that formats stably today, such as foo(v :> b) // comment, which would split over three lines; the drift only happens before a block.
  • Why are binary operands exempt? With their group broken, switch a // c\n+ b { printed a + // c\nb and then a +\n// c\nb on the next format; flat, it prints switch (\n a + b // c\n) { and stays.
a57123e5e · cknitt + Claude · Oct 27, 2026, 08:19 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, so the expression is passed to print_operand_before_brace in one place; if and while keep their own condition printing. The output is unchanged.

d32ce0b46 · cknitt + Claude · Oct 27, 2026, 09:25 UTC

Keep operands with a comment before a nested operator in parentheses

Why

Only a binary operand was left flat. A binary expression inside another operand, as in [a // c\n+ b], still had its group broken, so the comment was printed after the operator and the next format attached it to the right operand.

An operand is now also left flat when a binary expression or pipe in it has its operator at the start of a line, after the left operand, which is where such a comment sits (Parsetree_viewer.has_line_break_before_operator). The comment then goes into #9061's parentheses. A comment after an item that holds a binary expression keeps its place.

modifiedFormat binary expression in an array operand

let x = switch [a // c+ b, d] { | _ => 1 }
rescript
Before first run, then second run
let x = switch [  a + // c  b,  d,] {| _ => 1}
⇝
let x = switch [  a +  // c  b,  d,] {| _ => 1}
After stable
let x = switch (  [a + b, d] // c) {| _ => 1}

Review

  • Why look at the line of the operator rather than at the comments? A line comment between the left operand and the operator always puts the operator on a later line, and the check needs only the expression, before printing takes the comments out of the table. A multi-line binary expression without such a comment only costs another comment in the same operand its place next to its item.