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
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 {printeda + // c\nband thena +\n// c\nbon the next format; flat, it printsswitch (\n a + b // c\n) {and stays.