Keep comments on labelled parameters in the formatter - #8690
Conversation
walk_expr_parameter split the trailing comments of a labeled parameter's pattern into those adjacent to it and the rest, discarded the first part (_afterPat) and attached the whole list to the pattern while also walking the rest into the default expression, so (~x as y /* a */ = /* b */ 1) printed /* b */ twice. Attach only the adjacent comments, as walk_expr_argument does. Signed-off-by: Cristiano Calcagno <cristianoc@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The printer prints a punned labelled parameter (`~x`, `~x: t`) from its label and type, without the pattern, so comments the comment table attaches to the pattern or to the variable inside its type constraint are printed only when that location coincides with the whole parameter's. With a default value it does not, and the comments are dropped: (~x /* a */ = /* b */ 1, ()) printed as (~x=/* b */ 1, ()) (~x: int /* a */ = 1, ()) printed as (~x: int=1, ()) (~x /* a */ : int, ()) printed as (~x: int, ()) For `~x /* a */ = ?` the comment was printed after `=?`, where reparsing attaches it to the next parameter, so formatting was not idempotent. The punned cases now print the pattern's and the variable's comments, and `=?` follows the parameter's trailing comments. The namedArgs comment fixture covers default, typed, optional and aliased parameters; before the change its snapshot lost `/* a */` in the first four new cases and placed it after `=?` in the last three. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Cristiano Calcagno <cristianoc@users.noreply.github.com>
The printer printed an optional labelled argument of an arrow type with `=?` inside the span whose trailing comments it prints, so a comment before `=?` was printed after it: type f = (~x: int /* a */ =?, unit) => int printed as (~x: int=? /* a */, unit) => int Reparsing attaches that comment to the next argument, so a second format gave (~x: int=?, /* a */ unit), and for a last argument moved it onto the return type. The argument's location ends at its type, before `=?`, so its trailing comments now print before `=?`. The typexpr comment fixture covers a type alias, a last argument and an external; before the change its snapshot placed `/* a */` after `=?` in all three. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Cristiano Calcagno <cristianoc@users.noreply.github.com>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Cristiano Calcagno <cristianoc@users.noreply.github.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b733f3949a
ℹ️ 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".
| print_comments doc cmt_tbl loc | ||
| (* [loc] ends before [=?], so the comments attached to it precede [=?] in | ||
| the source. *) | ||
| Doc.concat [print_comments doc cmt_tbl loc; optional_indicator] |
There was a problem hiding this comment.
Preserve comments that follow
=?
When an optional parameter is written as ~x: int=? /* after */, the comment walker attaches /* after */ to the same pre-marker location as a comment before =?; appending optional_indicator after print_comments therefore reformats it as ~x: int /* after */=?. The analogous expression path at line 5536 also changes ~x=? /* after */ into ~x /* after */=?, so the trailing comments must be partitioned by which side of the marker they occur on rather than placing all of them before it.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Confirmed: a comment written after =? (e.g. ~x: int=? /* after */, in a multi-line parameter list) was attached to the same location as one before =?, and both printed before the marker. 4cf8752 splits those trailing comments by their preceding token: a comment whose preceding token ends past the parameter location follows the ? of =? and prints after it, the others before. This applies to both the arrow-type path and the function-parameter path. New cases in printer/comments/namedArgs.res and typexpr.res cover after-only and before-and-after comments; they fail on the previous head and format stably.
There was a problem hiding this comment.
Follow-up in 0a29474: on a single line, the comments walker gave a comment after =? to the next parameter (or to the body or return type), so (~x=? /* a */, ~y) changed on a second format. Comments now record whether their preceding token is ?, and the parameter-list walker keeps such comments with the preceding parameter. The printer also keeps a run like ~x /* a */ /* a2 */ =? before the marker. Short single-line cases in namedArgs.res and typexpr.res cover this, and formatting their output again gives identical text.
| // comment 2 | ||
| int, | ||
| ) => unit = "test" | ||
| type optionalArg = (~x: int /* a */=?, unit) => int |
There was a problem hiding this comment.
Add this formatter fix to the changelog
This is a user-facing formatter bug fix, but the commit does not update CHANGELOG.md; add an entry under the current unreleased bug-fix section and end it with the PR link as required.
AGENTS.md reference: AGENTS.md:L93-L95
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Added in bfcc089, under the Unreleased section with the PR link.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #8690 +/- ##
==========================================
+ Coverage 79.19% 79.20% +0.01%
==========================================
Files 473 473
Lines 64031 64057 +26
==========================================
+ Hits 50712 50739 +27
+ Misses 13319 13318 -1
🚀 New features to boost your workflow:
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/darwin-x64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
Developer playground preview: https://rescript-lang.github.io/rescript/dev-playground/?version=pr-8690 |
A trailing comment of an optional parameter without a default either precedes =? or follows it in the source. The comment's preceding token tells them apart: past the parameter's location it is the ? of =?. Both the arrow-type and the function-parameter printers now place each comment on its own side of =?. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Cristiano Calcagno <cristianoc@users.noreply.github.com>
On a single line, a comment after the =? of an optional parameter became a leading comment of the next parameter, or of the body or return type for the last one, so `(~x=? /* a */, ~y)` reformatted to `(~x=?, /* a */ ~y)`. Comments record whether their preceding token is `?`, and the list walker keeps such comments, together with the comments adjacent to them, as trailing comments of the preceding parameter. The printer places before =? only the comments adjacent to the parameter, so a run such as `~x /* a */ /* a2 */ =?` stays before the marker. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Cristiano Calcagno <cristianoc@users.noreply.github.com>
|
@codex review |
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
Part of #8712.
Summary
~x,~x: t) from its label and type, without the pattern, so comments the comment table attaches to the pattern or to the variable inside its type constraint are printed only when that location coincides with the whole parameter's. With a default value it does not, and the comments are dropped: (~x /* a / = / b / 1, ()) printed as (~x=/ b / 1, ()) (~x: int / a / = 1, ()) printed as (~x: int=1, ()) (~x / a */ : int, ()) printed as (~x: int, ())=?inside the span whose trailing comments it prints, so a comment before=?was printed after it: type f = (~x: int /* a / =?, unit) => int printed as (~x: int=? / a */, unit) => intTest plan
dune builddune build @fmtdune build --profile browser @checkmakemake liblib/outputsmake test-syntaxmake test-syntax-roundtripmake testmake checkformatnpm run check🤖 Generated with Claude Code