Skip to content

Keep attributes in front of a labeled parameter alias when formatting - #9111

Draft
cknitt wants to merge 1 commit into
masterfrom
fix-9101-labeled-alias-attrs
Draft

cknitt wants to merge 1 commit into
masterfrom
fix-9101-labeled-alias-attrs

Conversation

@cknitt

@cknitt cknitt commented Oct 28, 2026

Copy link
Copy Markdown
Member

Formatting let g = (@attr ~a as x: int) => x produced let g = (~a as @attr x: int) => x, which is a different AST: the parser puts attributes written before ~a as on the whole alias pattern (here the Ppat_constraint), while after as they attach to the inner x. A doc comment in the same position (accepted since #9055) was printed between as and the pattern, on its own line.

The ~b as c case of print_exp_fun_parameter now prints the pattern's own attributes before ~, and the pattern without them after as, like the punned ~a and ~a: t cases already do. Attributes that belong to an inner pattern (~a as @attr x: int) are untouched.

Tests: printer/expr/fun.res gains labeled aliases with attributes on constrained, optional, defaulted, tuple, record, wildcard and parenthesized patterns, plus one with the attribute on the inner pattern that must stay put; printer/comments/docCommentParameters.res gains a doc comment before a typed alias with a default. The existing asyncAwait.res and docCommentParameters.res snapshots now print the attribute and doc comment before the label.

Fixes #9101

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gGkPma9LAZimuQjnrP

Why:
    The parser puts attributes written before `~a as x` on the alias
    pattern, and the formatter printed them after `as`, where they parse
    onto the inner pattern, so formatting changed the AST (#9101). Doc
    comments in that position were printed between `as` and the pattern.

Format modified (labeled parameter alias):
Example:
    let g = (@attr ~a as x: int) => x
    let h = (/** a */ ~a as x: int=3) => x
Before (rescript):
    let g = (~a as @attr x: int) => x
    let h = (
      ~a as /** a */
      x: int=3,
    ) => x
After (rescript):
    let g = (@attr ~a as x: int) => x
    let h = (
      /** a */
      ~a as x: int=3,
    ) => x

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

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix the formatter moving an attribute or doc comment in front of a labeled parameter alias like ~a as x: int onto the alias.

Semantic Diff

1 commit · Full page

Keep attributes in front of a labeled parameter alias when formatting

1b247dc · cknitt + Claude · Oct 28, 2026, 15:25 UTC

Why

The parser puts attributes written before ~a as x on the alias pattern, and the formatter printed them after as, where they parse onto the inner pattern, so formatting changed the AST (#9101). Doc comments in that position were printed between as and the pattern.

Format modified (labeled parameter alias)

let g = (@attr ~a as x: int) => x
let h = (/** a */ ~a as x: int=3) => x

rescript

-let g = (~a as @attr x: int) => x
+let g = (@attr ~a as x: int) => x
 let h = (
-  ~a as /** a */
-  x: int=3,
+  /** a */
+  ~a as x: int=3,
 ) => x

@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.46%. Comparing base (85f31fb) to head (1b247dc).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9111   +/-   ##
=======================================
  Coverage   81.46%   81.46%           
=======================================
  Files         460      460           
  Lines       64675    64676    +1     
=======================================
+ Hits        52689    52690    +1     
  Misses      11986    11986           
Files with missing lines Coverage Δ
compiler/syntax/src/res_printer.ml 94.52% <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 28, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 1b247dc

@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-28T16:42:22.535651Z 1b247dc 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. Already looking forward to the next diff.

Reviewed commit: 1b247dcbf9

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

Formatter moves an attribute in front of ~a as x: int onto x

1 participant