Skip to content

Keep line comments in operands followed by a block - #8906

Draft
cknitt wants to merge 1 commit into
parsetree-spreadfrom
operand-line-comments
Draft

cknitt wants to merge 1 commit into
parsetree-spreadfrom
operand-line-comments

Conversation

@cknitt

@cknitt cknitt commented Oct 18, 2026

Copy link
Copy Markdown
Member

Stacked on #8905.

A line comment inside the operand of switch, try, if, while or a loop moved behind the opening brace when the formatter kept the operand on one line, and the next format attached it to the block, so the output changed on every run:

// input
let x = switch [
  a, // c
  b,
] { | _ => 1 }

// before, pass 1           // before, pass 2
let x = switch [a, b] { // c   let x = switch [a, b] {
| _ => 1                       // c
}                              | _ => 1
                               }

The comment is printed as a line suffix in a flat group. Operands followed by a block now force the groups around such a comment to break (Doc.break_before_line_suffixes). A comment trailing the whole operand, which no group break can move, goes into parentheses with the operand, as if conditions already did:

let x = switch [
  a, // c
  b,
] {
| _ => 1
}

let x = switch (
  a // c
) {
| _ => 1
}

Making every same-line line comment break its group would also fix this, but it reformats stable code such as foo(v :> b) // comment (3 snapshot changes), so the change is limited to operands before a block.

Checked with a harness of 10 operand positions × 8 operand/comment shapes: all 80 now format idempotently (48 did not before). A 330-case differential of other contexts formats as before apart from the comment-in-operand cases.

New test: printer/comments/operandBeforeBlock.res.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EUpL6dKCiSwAeriDGRHqNr

@cknitt
cknitt added this pull request to stack #8907 October 18, 2026 18:28
@cknitt
cknitt force-pushed the operand-line-comments branch from dae0442 to cc7571e Compare October 18, 2026 18:31
@codecov

codecov Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.39216% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.20%. Comparing base (798978d) to head (cfa5e3e).

Files with missing lines Patch % Lines
compiler/syntax/src/res_printer.ml 81.81% 6 Missing ⚠️
compiler/syntax/src/res_doc.ml 77.77% 4 Missing ⚠️
Additional details and impacted files
@@                Coverage Diff                @@
##           parsetree-spread    #8906   +/-   ##
=================================================
  Coverage             81.20%   81.20%           
=================================================
  Files                   458      458           
  Lines                 63933    63957   +24     
=================================================
+ Hits                  51918    51938   +20     
- Misses                12015    12019    +4     
Files with missing lines Coverage Δ
compiler/syntax/src/res_doc.ml 69.04% <77.77%> (+0.81%) ⬆️
compiler/syntax/src/res_printer.ml 94.26% <81.81%> (+0.01%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@8906

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@8906

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@8906

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@8906

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@8906

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@8906

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@8906

commit: cfa5e3e

@cknitt

cknitt commented Oct 18, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-21T18:23:03.765759Z cfa5e3e Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cc7571ef6f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

| Indent doc -> has_ungrouped_line_suffix doc
| IfBreaks {yes; no} ->
has_ungrouped_line_suffix yes || has_ungrouped_line_suffix no
| Group _ | Nil | Text _ | LineBreak _ | BreakParent -> false

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Handle grouped suffixes without a later break

A Group does not guarantee that breaking it will encounter a LineBreak after its line suffix. For example, print_object_get_doc produces a group containing parent_doc; "[\"member\"]"; with an operand such as switch obj // c\n["member"] { ... }, the comment is attached to obj, but there is no break after that suffix inside the group. Returning false here prevents print_operand_before_brace from adding protective parentheses, and the inserted BreakParent only changes the mode—it does not flush the queued suffix—so the comment still prints after the following { and continues relocating on subsequent formats.

Useful? React with 👍 / 👎.

@cknitt
cknitt force-pushed the operand-line-comments branch from cc7571e to 1ebd43e Compare October 19, 2026 07:41
@cknitt
cknitt force-pushed the operand-line-comments branch from 1ebd43e to 621783f Compare October 21, 2026 17:42
Why:
    A line comment inside the operand of switch, try, if, while or a loop
    was printed as a line suffix in a flat group, so it moved behind the
    opening brace, and the next format attached it to the block.

The operand now breaks the groups around such a comment, and a comment
trailing the whole operand goes into parentheses with it, as if conditions
already did.

Format modified (line comment in a switch operand):
Example:
    let x = switch [
      a, // c
      b,
    ] {
    | _ => 1
    }
Before (rescript):
    let x = switch [a, b] { // c
    | _ => 1
    }
After (rescript):
    let x = switch [
      a, // c
      b,
    ] {
    | _ => 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.

Area: printer tests
Output: unchanged
Formatting: changed operands of switch, try, if, while and loops that
 contain a line comment; 80 operand/comment combinations format
 idempotently (48 did not before), and 330 spread contexts format as before
 apart from those
Formats: none
PPX: unchanged
Editor: unchanged
Tests: +tests/syntax_tests/data/printer/comments/operandBeforeBlock.res
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EUpL6dKCiSwAeriDGRHqNr
Claude-Session: https://claude.ai/code/session_01Kr4cjoUi2f5VpMvE6wT9bo
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the operand-line-comments branch from 621783f to cfa5e3e Compare October 21, 2026 18:03
@github-actions

Copy link
Copy Markdown

@cknitt

cknitt commented Oct 21, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: cfa5e3e866

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

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.

1 participant