Skip to content

Commit dffc266

Browse files
authored
Fix comments attached to array element (#7672)
* Remove Example.res * WIP * Cleanup * Refactor * Add tests * Update CHANGELOG * Handle comment for array spread
1 parent 789591a commit dffc266

File tree

5 files changed

+169
-48
lines changed

5 files changed

+169
-48
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
- Fix I/O error message when trying to extract extra info from non-existing file. https://github.com/rescript-lang/rescript/pull/7656
5252
- Fix fatal error when JSX expression used without configuring JSX in `rescript.json`. https://github.com/rescript-lang/rescript/pull/7656
5353
- Rewatch: Only allow access to `"bs-dev-dependencies"` from `"type": "dev"` source files. https://github.com/rescript-lang/rescript/pull/7650
54+
- Fix comment attached to array element. https://github.com/rescript-lang/rescript/pull/7672
5455

5556
#### :nail_care: Polish
5657

compiler/syntax/src/res_comments_table.ml

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,38 @@ and walk_value_binding vb t comments =
10281028

10291029
and walk_expression expr t comments =
10301030
let open Location in
1031+
let walk_apply_expr call_expr arguments t comments =
1032+
let before, inside, after =
1033+
partition_by_loc comments call_expr.Parsetree.pexp_loc
1034+
in
1035+
let after =
1036+
if is_block_expr call_expr then (
1037+
let after_expr, rest =
1038+
partition_adjacent_trailing call_expr.Parsetree.pexp_loc after
1039+
in
1040+
walk_expression call_expr t (List.concat [before; inside; after_expr]);
1041+
rest)
1042+
else (
1043+
attach t.leading call_expr.Parsetree.pexp_loc before;
1044+
walk_expression call_expr t inside;
1045+
after)
1046+
in
1047+
let after_expr, rest =
1048+
partition_adjacent_trailing call_expr.Parsetree.pexp_loc after
1049+
in
1050+
attach t.trailing call_expr.Parsetree.pexp_loc after_expr;
1051+
walk_list
1052+
(arguments
1053+
|> List.map (fun (lbl, expr) ->
1054+
let loc =
1055+
match lbl with
1056+
| Asttypes.Labelled {loc} | Optional {loc} ->
1057+
{loc with loc_end = expr.Parsetree.pexp_loc.loc_end}
1058+
| _ -> expr.pexp_loc
1059+
in
1060+
ExprArgument {expr; loc}))
1061+
t rest
1062+
in
10311063
match expr.Parsetree.pexp_desc with
10321064
| _ when comments = [] -> ()
10331065
| Pexp_constant _ ->
@@ -1539,35 +1571,34 @@ and walk_expression expr t comments =
15391571
}
15401572
when Res_parsetree_viewer.is_tuple_array key_values ->
15411573
walk_list [Expression key_values] t comments
1542-
| Pexp_apply {funct = call_expr; args = arguments} ->
1543-
let before, inside, after = partition_by_loc comments call_expr.pexp_loc in
1544-
let after =
1545-
if is_block_expr call_expr then (
1546-
let after_expr, rest =
1547-
partition_adjacent_trailing call_expr.pexp_loc after
1574+
| Pexp_apply {funct = call_expr; args = arguments} -> (
1575+
(* Special handling for Belt.Array.concatMany - treat like an array *)
1576+
match call_expr.pexp_desc with
1577+
| Pexp_ident
1578+
{
1579+
txt =
1580+
Longident.Ldot
1581+
(Longident.Ldot (Longident.Lident "Belt", "Array"), "concatMany");
1582+
}
1583+
when List.length arguments = 1 -> (
1584+
match arguments with
1585+
| [(_, {pexp_desc = Pexp_array sub_arrays})] ->
1586+
(* Collect all individual expressions from sub-arrays *)
1587+
let all_exprs =
1588+
List.fold_left
1589+
(fun acc sub_array ->
1590+
match sub_array.Parsetree.pexp_desc with
1591+
| Pexp_array exprs -> acc @ exprs
1592+
| _ -> acc @ [sub_array])
1593+
[] sub_arrays
15481594
in
1549-
walk_expression call_expr t (List.concat [before; inside; after_expr]);
1550-
rest)
1551-
else (
1552-
attach t.leading call_expr.pexp_loc before;
1553-
walk_expression call_expr t inside;
1554-
after)
1555-
in
1556-
let after_expr, rest =
1557-
partition_adjacent_trailing call_expr.pexp_loc after
1558-
in
1559-
attach t.trailing call_expr.pexp_loc after_expr;
1560-
walk_list
1561-
(arguments
1562-
|> List.map (fun (lbl, expr) ->
1563-
let loc =
1564-
match lbl with
1565-
| Asttypes.Labelled {loc} | Optional {loc} ->
1566-
{loc with loc_end = expr.Parsetree.pexp_loc.loc_end}
1567-
| _ -> expr.pexp_loc
1568-
in
1569-
ExprArgument {expr; loc}))
1570-
t rest
1595+
walk_list (all_exprs |> List.map (fun e -> Expression e)) t comments
1596+
| _ ->
1597+
(* Fallback to regular apply handling *)
1598+
walk_apply_expr call_expr arguments t comments)
1599+
| _ ->
1600+
(* Regular apply handling *)
1601+
walk_apply_expr call_expr arguments t comments)
15711602
| Pexp_fun _ | Pexp_newtype _ -> (
15721603
let _, parameters, return_expr = fun_expr expr in
15731604
let comments =

compiler/syntax/src/res_printer.ml

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4035,15 +4035,30 @@ and print_binary_expression ~state (expr : Parsetree.expression) cmt_tbl =
40354035
and print_belt_array_concat_apply ~state sub_lists cmt_tbl =
40364036
let make_spread_doc comma_before_spread = function
40374037
| Some expr ->
4038+
(* Extract leading comments before dotdotdot *)
4039+
let leading_comments_doc =
4040+
print_leading_comments Doc.nil cmt_tbl.CommentTable.leading
4041+
expr.Parsetree.pexp_loc
4042+
in
4043+
(* Print expression without leading comments (they're already extracted) *)
4044+
let expr_doc =
4045+
let doc = print_expression ~state expr cmt_tbl in
4046+
match Parens.expr expr with
4047+
| Parens.Parenthesized -> add_parens doc
4048+
| Braced braces -> print_braces doc expr braces
4049+
| Nothing -> doc
4050+
in
4051+
(* Print trailing comments with the expression *)
4052+
let expr_with_trailing_comments =
4053+
print_trailing_comments expr_doc cmt_tbl.CommentTable.trailing
4054+
expr.Parsetree.pexp_loc
4055+
in
40384056
Doc.concat
40394057
[
40404058
comma_before_spread;
4059+
leading_comments_doc;
40414060
Doc.dotdotdot;
4042-
(let doc = print_expression_with_comments ~state expr cmt_tbl in
4043-
match Parens.expr expr with
4044-
| Parens.Parenthesized -> add_parens doc
4045-
| Braced braces -> print_braces doc expr braces
4046-
| Nothing -> doc);
4061+
expr_with_trailing_comments;
40474062
]
40484063
| None -> Doc.nil
40494064
in
@@ -4054,20 +4069,19 @@ and print_belt_array_concat_apply ~state sub_lists cmt_tbl =
40544069
| _ -> Doc.concat [Doc.text ","; Doc.line]
40554070
in
40564071
let spread_doc = make_spread_doc comma_before_spread spread in
4057-
Doc.concat
4058-
[
4059-
Doc.join
4060-
~sep:(Doc.concat [Doc.text ","; Doc.line])
4061-
(List.map
4062-
(fun expr ->
4063-
let doc = print_expression_with_comments ~state expr cmt_tbl in
4064-
match Parens.expr expr with
4065-
| Parens.Parenthesized -> add_parens doc
4066-
| Braced braces -> print_braces doc expr braces
4067-
| Nothing -> doc)
4068-
expressions);
4069-
spread_doc;
4070-
]
4072+
let expressions_doc =
4073+
Doc.join
4074+
~sep:(Doc.concat [Doc.text ","; Doc.line])
4075+
(List.map
4076+
(fun expr ->
4077+
let doc = print_expression_with_comments ~state expr cmt_tbl in
4078+
match Parens.expr expr with
4079+
| Parens.Parenthesized -> add_parens doc
4080+
| Braced braces -> print_braces doc expr braces
4081+
| Nothing -> doc)
4082+
expressions)
4083+
in
4084+
Doc.concat [expressions_doc; spread_doc]
40714085
in
40724086
Doc.group
40734087
(Doc.concat

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,40 @@ let _ = (
1515
->Belt.SortArray.stableSortBy((a, b) =>
1616
compare(a.createdTime, b.createdTime)
1717
)
18-
)[0]
18+
)[0]
19+
20+
let _ = [
21+
// comment 1
22+
a,
23+
// comment 2
24+
b,
25+
// comment 3
26+
c
27+
]
28+
29+
let _ = [
30+
// comment 1
31+
a,
32+
// comment 2
33+
b, c
34+
]
35+
36+
let _ = [
37+
// comment 0
38+
...xs,
39+
// comment 1
40+
a,
41+
// comment 2
42+
b, c
43+
]
44+
45+
let _ = [
46+
// comment 0
47+
...xs,
48+
// comment 1
49+
a,
50+
// comment 2
51+
...ys,
52+
// comment 3
53+
b, c
54+
]

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,42 @@ let _ = a[0] // zz
1010
// This comment will vanish
1111
->Belt.SortArray.stableSortBy((a, b) => compare(a.createdTime, b.createdTime))
1212
)[0]
13+
14+
let _ = [
15+
// comment 1
16+
a,
17+
// comment 2
18+
b,
19+
// comment 3
20+
c,
21+
]
22+
23+
let _ = [
24+
// comment 1
25+
a,
26+
// comment 2
27+
b,
28+
c,
29+
]
30+
31+
let _ = [
32+
// comment 0
33+
...xs,
34+
// comment 1
35+
a,
36+
// comment 2
37+
b,
38+
c,
39+
]
40+
41+
let _ = [
42+
// comment 0
43+
...xs,
44+
// comment 1
45+
a,
46+
// comment 2
47+
...ys,
48+
// comment 3
49+
b,
50+
c,
51+
]

0 commit comments

Comments
 (0)