Skip to content

Commit 84d0c8c

Browse files
Fix formatting of trailing comments before equals
1 parent de28ae6 commit 84d0c8c

4 files changed

Lines changed: 33 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
- Rewatch: treat transitive workspace dependencies as local packages in monorepo roots. https://github.com/rescript-lang/rescript/pull/8411
3131
- Rewatch: use a single timestamp per compile pass. https://github.com/rescript-lang/rescript/pull/8428
3232
- Fix rewatch warning replay after early compile errors. https://github.com/rescript-lang/rescript/pull/8408
33+
- Fix formatting of trailing comments before `=` in let bindings. https://github.com/rescript-lang/rescript/pull/8444
3334

3435
#### :memo: Documentation
3536

compiler/syntax/src/res_printer.ml

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ let has_trailing_single_line_comment tbl loc =
7070
| Some (comment :: _) -> Comment.is_single_line_comment comment
7171
| _ -> false
7272

73+
let has_any_trailing_line_comment tbl loc =
74+
match Hashtbl.find_opt tbl.CommentTable.trailing loc with
75+
| Some comments -> List.exists Comment.is_single_line_comment comments
76+
| None -> false
77+
7378
let has_comment_below tbl loc =
7479
match Hashtbl.find tbl.CommentTable.trailing loc with
7580
| comment :: _ ->
@@ -2441,7 +2446,15 @@ and print_value_binding ~state ~rec_flag (vb : Parsetree.value_binding) cmt_tbl
24412446
| Braced braces -> print_braces doc expr braces
24422447
| Nothing -> doc
24432448
in
2449+
let pattern_has_trailing_line_comment =
2450+
has_any_trailing_line_comment cmt_tbl vb.pvb_pat.ppat_loc
2451+
in
24442452
let pattern_doc = print_pattern ~state vb.pvb_pat cmt_tbl in
2453+
let equal_doc =
2454+
if pattern_has_trailing_line_comment then
2455+
Doc.concat [Doc.hard_line; Doc.equal]
2456+
else Doc.text " ="
2457+
in
24452458
(*
24462459
* we want to optimize the layout of one pipe:
24472460
* let tbl = data->Js.Array2.reduce((map, curr) => {
@@ -2459,21 +2472,14 @@ and print_value_binding ~state ~rec_flag (vb : Parsetree.value_binding) cmt_tbl
24592472
[
24602473
Doc.group
24612474
(Doc.concat
2462-
[
2463-
attrs;
2464-
header;
2465-
pattern_doc;
2466-
Doc.text " =";
2467-
Doc.space;
2468-
printed_expr;
2469-
]);
2475+
[attrs; header; pattern_doc; equal_doc; Doc.space; printed_expr]);
24702476
Doc.group
24712477
(Doc.concat
24722478
[
24732479
attrs;
24742480
header;
24752481
pattern_doc;
2476-
Doc.text " =";
2482+
equal_doc;
24772483
Doc.indent (Doc.concat [Doc.line; printed_expr]);
24782484
]);
24792485
]
@@ -2505,7 +2511,7 @@ and print_value_binding ~state ~rec_flag (vb : Parsetree.value_binding) cmt_tbl
25052511
attrs;
25062512
header;
25072513
pattern_doc;
2508-
Doc.text " =";
2514+
equal_doc;
25092515
(if should_indent then
25102516
Doc.indent (Doc.concat [Doc.line; printed_expr])
25112517
else Doc.concat [Doc.space; printed_expr]);

tests/syntax_tests/data/printer/comments/expected/valueBindings.res.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ let walkList: 'node. unit = comments => {
1010
let x /* comment */ = 0
1111
}
1212

13+
let x // comment before equals
14+
= 1
15+
16+
let multilineString // comment before equals
17+
= "
18+
multiline
19+
"
20+
1321
let walkList: 'node. (
1422
~prevLoc: Location.t=?,
1523
~getLoc: 'node => Location.t,

tests/syntax_tests/data/printer/comments/valueBindings.res

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ let walkList: 'node. unit = comments => {
1010
let x /* comment */ = 0
1111
}
1212

13+
let x // comment before equals
14+
= 1
15+
16+
let multilineString // comment before equals
17+
= "
18+
multiline
19+
"
20+
1321
let walkList: 'node. (
1422
~prevLoc: Location.t=?,
1523
~getLoc: 'node => Location.t,

0 commit comments

Comments
 (0)