Skip to content

Commit 138f55f

Browse files
committed
Handle comment for array spread
1 parent 4321b31 commit 138f55f

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

compiler/syntax/src/res_printer.ml

Lines changed: 20 additions & 5 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

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,23 @@ let _ = [
3232
// comment 2
3333
b, c
3434
]
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+
]

0 commit comments

Comments
 (0)