Skip to content

Commit d7eb937

Browse files
cknittclaude
andauthored
Keep attributes in front of a labeled parameter alias when formatting (#9111)
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 Claude-Session: https://claude.ai/code/session_01Kbf3gGkPma9LAZimuQjnrP Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4b1fcda commit d7eb937

7 files changed

Lines changed: 26 additions & 5 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fix the formatter moving an attribute or doc comment in front of a labeled parameter alias like `~a as x: int` onto the alias.

‎compiler/syntax/src/res_printer.ml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5656,13 +5656,14 @@ and print_exp_fun_parameter ~state parameter cmt_tbl =
56565656
])
56575657
cmt_tbl pattern.ppat_loc
56585658
| (Labelled {txt = lbl} | Optional {txt = lbl}), pattern ->
5659-
(* ~b as c *)
5659+
(* ~b as c: the parser puts attributes before [~b] on [c] *)
56605660
Doc.concat
56615661
[
5662+
print_attributes ~state pattern.ppat_attributes cmt_tbl;
56625663
Doc.text "~";
56635664
print_ident_like lbl;
56645665
Doc.text " as ";
5665-
print_pattern ~state pattern cmt_tbl;
5666+
print_pattern ~state {pattern with ppat_attributes = []} cmt_tbl;
56665667
]
56675668
in
56685669
let optional_label_suffix =

‎tests/syntax_tests/data/printer/comments/docCommentParameters.res‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ let f = (@res.doc("The runner") run: string) => run
22
let f = (/** The runner */ run: string) => run
33
let f = (/** Positional */ x, /** Labelled */ ~y, /** Typed */ ~z: int) => x + y + z
44
let f = (/** Optional */ ~a=?, /** Default */ ~b=1, /** Aliased */ ~c as d) => d
5+
let f = (/** Typed alias */ ~a as x: int=3) => x
56
let f = (/** Locally abstract */ type a, x: a) => x
67

78
// A doc comment on a single parameter that would be hugged

‎tests/syntax_tests/data/printer/comments/expected/docCommentParameters.res.txt‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ let f = (
1919
~a=?,
2020
/** Default */
2121
~b=1,
22-
~c as /** Aliased */
23-
d,
22+
/** Aliased */
23+
~c as d,
2424
) => d
25+
let f = (
26+
/** Typed alias */
27+
~a as x: int=3,
28+
) => x
2529
let f = (
2630
/** Locally abstract */
2731
type a,

‎tests/syntax_tests/data/printer/expr/expected/asyncAwait.res.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ let _ = await {
104104

105105
let f1 = async (~x, ~y) => x + y
106106
let f2 = async (@foo ~x, @bar ~y) => x + y
107-
let f3 = @foo async (~x as @bar @zz z, ~y) => x + y
107+
let f3 = @foo async (@bar @zz ~x as z, ~y) => x + y
108108
let f4 = async x => x
109109
let f5 = async x => async y => 3
110110
let f6 = async (~x1, ~x2) => async y => 3

‎tests/syntax_tests/data/printer/expr/expected/fun.res.txt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ let f = @attr (a, b) => @attr2 (c, d) => ()
7575
let f = (@attr ~a, @attr ~b) => ()
7676
let f = (@attr ~a, @attr ~b) => ()
7777

78+
// Attributes before a labeled alias stay in front of the label
79+
let f = (@attr ~a as x: int) => x
80+
let f = (@attr ~a as x: int=?, @attr ~b as y: int=1) => x + y
81+
let f = (@attr ~a as (x, y), @attr ~b as {z}, @attr ~c as _) => x + y + z
82+
let f = (@attr @inner ~a as x: int) => x
83+
let f = (~a as @attr x: int) => x
84+
7885
let f = (
7986
thisIsAVeryLongNaaaaaaaaaaaaaaaaaaameeeeeeeeeeeee,
8087
thisIsAVeryLongNaaaaaaaaaaaaaaaaaaameeeeeeeeeeeee,

‎tests/syntax_tests/data/printer/expr/fun.res‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ let f = @attr (a, b) => @attr2 (c, d) => ()
7575
let f = (@attr ~a, @attr ~b) => ()
7676
let f = (@attr ~a, @attr ~b) => ()
7777

78+
// Attributes before a labeled alias stay in front of the label
79+
let f = (@attr ~a as x: int) => x
80+
let f = (@attr ~a as x: int=?, @attr ~b as y: int=1) => x + y
81+
let f = (@attr ~a as (x, y), @attr ~b as {z}, @attr ~c as _) => x + y + z
82+
let f = (@attr ~a as @inner (x: int)) => x
83+
let f = (~a as @attr x: int) => x
84+
7885
let f = (
7986
thisIsAVeryLongNaaaaaaaaaaaaaaaaaaameeeeeeeeeeeee,
8087
thisIsAVeryLongNaaaaaaaaaaaaaaaaaaameeeeeeeeeeeee,

0 commit comments

Comments
 (0)