Skip to content

Keep the comments around a list pattern's only spread when formatting - #9127

Merged
cknitt merged 1 commit into
masterfrom
fix-9123-list-pattern-spread-comment
Oct 29, 2026
Merged

cknitt merged 1 commit into
masterfrom
fix-9123-list-pattern-spread-comment

Conversation

@cknitt

@cknitt cknitt commented Oct 29, 2026

Copy link
Copy Markdown
Member

list{...r} parses to the pattern r itself, but the parser gave that pattern the location of the whole list{...}. A comment around the spread therefore lay inside the location of the leaf r, where comment attachment leaves it unattached, and formatting dropped it:

let f = x =>
  switch x {
  | list{/* d */ ...r} => r
  }

was formatted as | r => r.

The parser now keeps the location of r for a list pattern that is only a spread, as it already does for list{...e} in expressions, so the comments become leading or trailing comments of r. The output is /* d */ | r => r, which is where the formatter already puts a leading comment of a case pattern (| /* d */ r).

Tests: new cases in tests/syntax_tests/data/printer/comments/pattern.res for a comment before and after the spread, several comments, a nested list{...list{...r}}, and the spread in let, function parameter and switch patterns.

Fixes #9123

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gHnoR8Goys4AMxET4q

Why:
    `list{...r}` parses to the pattern `r` itself, but the parser gave that
    pattern the location of the whole `list{...}`. A comment around the
    spread was then inside the location of the leaf `r`, where comment
    attachment leaves it unattached, so formatting dropped it. The pattern
    now keeps the location of `r`, like `list{...e}` in expressions, and the
    comments become leading or trailing comments of `r`.

Format modified (comment around a list pattern's only spread):
Example:
    let f = x =>
      switch x {
      | list{/* d */ ...r} => r
      }
    let g = (list{...r /* t */}) => r
Before (rescript):
    let f = x =>
      switch x {
      | r => r
      }
    let g = r => r
After (rescript):
    let f = x =>
      switch x {
      /* d */ | r => r
      }
    let g = r /* t */ => r

Review: Why is the comment printed before `|`? That is where the formatter
already puts a leading comment of a case pattern, as for `| /* d */ r`.

Fixes: #9123

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHnoR8Goys4AMxET4q
Signed-off-by: Christoph Knittel <ck@cca.io>
@github-actions

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Keep the comments around the spread of a list pattern without other items when formatting, as in list{/* c */ ...r}.

Semantic Diff

1 commit · Full page

Keep the comments around a list pattern's only spread when formatting

939f26b · cknitt + Claude · Oct 29, 2026, 17:59 UTC

Why

list{...r} parses to the pattern r itself, but the parser gave that pattern the location of the whole list{...}. A comment around the spread was then inside the location of the leaf r, where comment attachment leaves it unattached, so formatting dropped it. The pattern now keeps the location of r, like list{...e} in expressions, and the comments become leading or trailing comments of r.

Format modified (comment around a list pattern's only spread)

let f = x =>
  switch x {
  | list{/* d */ ...r} => r
  }
let g = (list{...r /* t */}) => r

rescript

 let f = x =>
   switch x {
-  | r => r
+  /* d */ | r => r
   }
-let g = r => r
+let g = r /* t */ => r

Fixes: #9123

Review

  • Why is the comment printed before |? That is where the formatter already puts a leading comment of a case pattern, as for | /* d */ r.

@codecov

codecov Bot commented Oct 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.71%. Comparing base (6ea63e5) to head (939f26b).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9127   +/-   ##
=======================================
  Coverage   81.71%   81.71%           
=======================================
  Files         460      460           
  Lines       64761    64762    +1     
=======================================
+ Hits        52918    52919    +1     
  Misses      11843    11843           
Files with missing lines Coverage Δ
compiler/syntax/src/res_core.ml 92.02% <100.00%> (+<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 29, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 939f26b

@github-actions

Copy link
Copy Markdown

@cknitt
cknitt marked this pull request as ready for review October 29, 2026 19:48
@cknitt

cknitt commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 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-29T19:50:12.577709Z 939f26b 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. Breezy!

Reviewed commit: 939f26b139

ℹ️ 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 enabled auto-merge (squash) October 29, 2026 19:56
@cknitt
cknitt requested a review from cristianoc October 29, 2026 19:56

@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 bfe3834 into master Oct 29, 2026
26 checks passed
@cknitt
cknitt deleted the fix-9123-list-pattern-spread-comment branch October 29, 2026 22:09
cknitt added a commit that referenced this pull request Oct 30, 2026
…ment pattern snapshot (#9137)

Add the blank line before a multi-line let to the comment pattern snapshot

#9127 added list-pattern cases to printer/comments/pattern.res, and its
snapshot was made before #8961, which puts a blank line around multi-line
module items, was merged. Together they print a blank line before
`let f = x => switch x {…}`, and the syntax test of master has failed on it
since #9127.


Claude-Session: https://claude.ai/code/session_01Kbf3gLpf7RN1MiRcubPgRn

Signed-off-by: Christoph Knittel <ck@cca.io>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
aspeddro pushed a commit to aspeddro/rescript-compiler that referenced this pull request Oct 31, 2026
…rescript-lang#9127)

Why:
    `list{...r}` parses to the pattern `r` itself, but the parser gave that
    pattern the location of the whole `list{...}`. A comment around the
    spread was then inside the location of the leaf `r`, where comment
    attachment leaves it unattached, so formatting dropped it. The pattern
    now keeps the location of `r`, like `list{...e}` in expressions, and the
    comments become leading or trailing comments of `r`.

Format modified (comment around a list pattern's only spread):
Example:
    let f = x =>
      switch x {
      | list{/* d */ ...r} => r
      }
    let g = (list{...r /* t */}) => r
Before (rescript):
    let f = x =>
      switch x {
      | r => r
      }
    let g = r => r
After (rescript):
    let f = x =>
      switch x {
      /* d */ | r => r
      }
    let g = r /* t */ => r

Review: Why is the comment printed before `|`? That is where the formatter
already puts a leading comment of a case pattern, as for `| /* d */ r`.

Fixes: rescript-lang#9123

Claude-Session: https://claude.ai/code/session_01Kbf3gHnoR8Goys4AMxET4q

Signed-off-by: Christoph Knittel <ck@cca.io>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Pedro Castro <aspeddro@gmail.com>
aspeddro pushed a commit to aspeddro/rescript-compiler that referenced this pull request Oct 31, 2026
…ts to the comment pattern snapshot (rescript-lang#9137)

Add the blank line before a multi-line let to the comment pattern snapshot

rescript-lang#9127 added list-pattern cases to printer/comments/pattern.res, and its
snapshot was made before rescript-lang#8961, which puts a blank line around multi-line
module items, was merged. Together they print a blank line before
`let f = x => switch x {…}`, and the syntax test of master has failed on it
since rescript-lang#9127.

Claude-Session: https://claude.ai/code/session_01Kbf3gLpf7RN1MiRcubPgRn

Signed-off-by: Christoph Knittel <ck@cca.io>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Pedro Castro <aspeddro@gmail.com>
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 drops a comment before the spread of a list pattern like list{/* c */ ...r}

2 participants