Skip to content

Keep doc comments and comments in place in four formatter cases - #9119

Merged
cknitt merged 1 commit into
masterfrom
fix-9107-comment-placement
Oct 29, 2026
Merged

cknitt merged 1 commit into
masterfrom
fix-9107-comment-placement

Conversation

@cknitt

@cknitt cknitt commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

The formatter put doc comments and comments in the wrong place in the cases reported in #9107. In one of them it also changed the order of the attributes in the AST, so formatting changed the program that PPXs see.

  • Doc comments after an attribute. partition_doc_comment_attributes moved every doc comment in front of all attributes. For example, type u = /** d1 */ @a /** d2 */ {x: int} came out as /** d1 */ /** d2 */ @a type u. It now takes only the doc comments that come before every other attribute. print_attributes prints later ones inline, in their place: /** d1 */ on its own line, then @a /** d2 */ type u = {x: int}. This keeps the attribute order in declarations, constructors, record fields, polymorphic variant tags, type expressions, patterns and module arguments. It also fixes type q = @a /** d */ int, which used to print as type q = /** d */ @a int and then no longer parsed.
  • A doc comment on a single hugged parameter. parameters_should_hug checked only the parameter's own attributes, but the doc comment is stored on the pattern. let f = (/** r */ {x, y}) => x therefore printed {x, y}) => x at column 0. A doc comment on the pattern now prevents hugging, and the parameter gets the normal indented layout.
  • A comment before a list's tail spread. list{1, /* c */ ...xs} printed as list{1, .../* c */ xs}. The list-with-tail printer now uses the same code as the array and dict spreads, now shared as print_spread_with_comments, so leading comments go before the dots. The list pattern tail takes its leading comments before printing the dots as well.
  • A file with only a line comment got an extra empty line, because the last comment kept the line break that follows a line comment. It is now printed without it.

Two related layouts stay as they are. They format stably and keep the AST the same, so they are accepted:

  • A comment after a parenthesized switch/if/loop operand (switch (x) // c) still moves into the block. Fixing it would need a token flag on comments or operand locations that include the parentheses.
  • A doc comment on a callback's parameters still starts the parameter at column 0.

Tests

  • printer/comments/docCommentAttributes.res and printer/typeDef/attributes.res: doc comments after attributes on declarations, constructors, fields, tags and type expressions, including the issue's example.
  • printer/comments/docCommentParameters.res: hugged record, array, tuple and unit parameters with a doc comment, and one with an attribute before the doc comment.
  • printer/comments/array.res: comments around a list's tail spread in expressions and patterns.
  • These existing snapshots change accordingly:
    • the comment-only files lose the extra empty line;
    • pattern.res puts the comment before the dots;
    • modExpr/attributes.res and typeDef/attributes.res keep the attribute order.

Fixes #9107

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gGkPma9LAZimuQjnrP

@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix the formatter's placement of a doc comment on a single hugged parameter, of doc comments after an attribute, and of a comment before a list's tail spread, and the extra newline after a file with only a line comment.

Semantic Diff

1 commit · Full page

✨ Keep doc comments and comments in place in four formatter cases

6d2afac · cknitt + Claude · Oct 28, 2026, 20:24 UTC · new

Why

The formatter put doc comments and comments in the wrong place in four of the cases reported in #9107, and in one of them it reordered the attributes in the AST. Doc comments after an attribute were moved in front of all attributes. A single parameter with a doc comment was hugged, so its pattern started at column 0. A comment before a list's tail spread was printed after the dots, in expressions and in patterns. A file with only a line comment got an extra newline.

Only the doc comments in front of every other attribute are now printed first; later ones stay in their place, inline. The fifth case, a comment after a parenthesized switch or if operand that moves into the block, keeps its stable output.

AST modified (doc comment after an attribute, after formatting)

type u = /** d1 */ @a /** d2 */ {x: int}

parse

 type_declaration "u"
   attribute  "res.doc"
-  attribute  "res.doc"
   attribute  "a"
+  attribute  "res.doc"

Format modified (doc comment after an attribute)

type u = /** d1 */ @a /** d2 */ {x: int}

rescript

 /** d1 */
-/** d2 */
-@a type u = {x: int}
+@a /** d2 */ type u = {x: int}

Format modified (doc comment on a hugged parameter)

let f = (/** r */ {x, y}) => x

rescript

-let f = (/** r */
-{x, y}) => x
+let f = (
+  /** r */
+  {x, y},
+) => x

Format modified (comment before a list's tail spread)

let l = list{1, /* c */ ...xs}
let f = (list{a, /* c */ ...rest}) => a

rescript

-let l = list{1, .../* c */ xs}
-let f = (list{a, .../* c */ rest}) => a
+let l = list{1, /* c */ ...xs}
+let f = (list{a, /* c */ ...rest}) => a

Format modified (file with only a line comment)

// c

text

-"// c\n\n"
+"// c\n"

Review

  • Why not fix the comment after a parenthesized operand? It formats stably with the same AST; fixing it needs a token flag on comments or operand locations that include the parentheses.

@codecov

codecov Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.47%. Comparing base (e63fc6f) to head (6d2afac).
⚠️ Report is 5 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9119   +/-   ##
=======================================
  Coverage   81.47%   81.47%           
=======================================
  Files         460      460           
  Lines       64686    64688    +2     
=======================================
+ Hits        52700    52705    +5     
+ Misses      11986    11983    -3     
Files with missing lines Coverage Δ
compiler/syntax/src/res_parsetree_viewer.ml 94.61% <100.00%> (+0.08%) ⬆️
compiler/syntax/src/res_printer.ml 94.58% <100.00%> (+0.06%) ⬆️

... and 1 file with indirect coverage changes

🚀 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 28, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 6d2afac

@github-actions

Copy link
Copy Markdown

@cknitt

cknitt commented Oct 28, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 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-28T20:27:28.666468Z 6d2afac 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

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 2cf5866b1f

ℹ️ 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".

Why:
    The formatter put doc comments and comments in the wrong place in four
    of the cases reported in #9107, and in one of them it reordered the
    attributes in the AST. Doc comments after an attribute were moved in
    front of all attributes. A single parameter with a doc comment was
    hugged, so its pattern started at column 0. A comment before a list's
    tail spread was printed after the dots, in expressions and in
    patterns. A file with only a line comment got an extra newline.

    Only the doc comments in front of every other attribute are now
    printed first; later ones stay in their place, inline. The fifth case,
    a comment after a parenthesized `switch` or `if` operand that moves
    into the block, keeps its stable output.

AST modified (doc comment after an attribute, after formatting):
Example:
    type u = /** d1 */ @A /** d2 */ {x: int}
Before (parse):
    type_declaration "u"
      attribute  "res.doc"
      attribute  "res.doc"
      attribute  "a"
After (parse):
    type_declaration "u"
      attribute  "res.doc"
      attribute  "a"
      attribute  "res.doc"

Format modified (doc comment after an attribute):
Example:
    type u = /** d1 */ @A /** d2 */ {x: int}
Before (rescript):
    /** d1 */
    /** d2 */
    @A type u = {x: int}
After (rescript):
    /** d1 */
    @A /** d2 */ type u = {x: int}

Format modified (doc comment on a hugged parameter):
Example:
    let f = (/** r */ {x, y}) => x
Before (rescript):
    let f = (/** r */
    {x, y}) => x
After (rescript):
    let f = (
      /** r */
      {x, y},
    ) => x

Format modified (comment before a list's tail spread):
Example:
    let l = list{1, /* c */ ...xs}
    let f = (list{a, /* c */ ...rest}) => a
Before (rescript):
    let l = list{1, .../* c */ xs}
    let f = (list{a, .../* c */ rest}) => a
After (rescript):
    let l = list{1, /* c */ ...xs}
    let f = (list{a, /* c */ ...rest}) => a

Format modified (file with only a line comment):
Example:
    // c
Before (text): "// c\n\n"
After (text): "// c\n"

Review: Why not fix the comment after a parenthesized operand? It formats
stably with the same AST; fixing it needs a token flag on comments or
operand locations that include the parentheses.

Fixes: #9107
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Kbf3gGkPma9LAZimuQjnrP
Signed-off-by: Christoph Knittel <[email protected]>
@cknitt
cknitt force-pushed the fix-9107-comment-placement branch from 2cf5866 to 6d2afac Compare October 28, 2026 20:25
@cknitt cknitt changed the title Keep comments and doc comments where they were written when formatting Keep doc comments and comments in place in four formatter cases Oct 28, 2026
@cknitt

cknitt commented Oct 28, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 6d2afacd98

ℹ️ 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".

@cknitt
cknitt marked this pull request as ready for review October 29, 2026 06:37
@cknitt
cknitt enabled auto-merge (squash) October 29, 2026 06:37
@cknitt
cknitt requested a review from cristianoc October 29, 2026 06:37

@cristianoc cristianoc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good to me. Thanks for the fix.

@cknitt
cknitt merged commit 4b1fcda into master Oct 29, 2026
26 checks passed
@cknitt
cknitt deleted the fix-9107-comment-placement branch October 29, 2026 11:42
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.

Formatter: five cases where comments and doc comments are placed wrongly

2 participants