Skip to content

Commit e947779

Browse files
authored
Merge pull request #2 from shulhi/fix-jsx-ast
Fix comments
2 parents ff113ce + e6f2e8c commit e947779

File tree

5 files changed

+64
-56
lines changed

5 files changed

+64
-56
lines changed

compiler/syntax/src/res_comments_table.ml

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,24 @@ let copy tbl =
2424

2525
let empty = make ()
2626

27+
let print_loc (k : Warnings.loc) =
28+
Doc.concat
29+
[
30+
Doc.lbracket;
31+
Doc.text (string_of_int k.loc_start.pos_lnum);
32+
Doc.text ":";
33+
Doc.text (string_of_int (k.loc_start.pos_cnum - k.loc_start.pos_bol));
34+
Doc.text "-";
35+
Doc.text (string_of_int k.loc_end.pos_lnum);
36+
Doc.text ":";
37+
Doc.text (string_of_int (k.loc_end.pos_cnum - k.loc_end.pos_bol));
38+
Doc.rbracket;
39+
]
40+
2741
let print_entries tbl =
28-
let open Location in
2942
Hashtbl.fold
3043
(fun (k : Location.t) (v : Comment.t list) acc ->
31-
let loc =
32-
Doc.concat
33-
[
34-
Doc.lbracket;
35-
Doc.text (string_of_int k.loc_start.pos_lnum);
36-
Doc.text ":";
37-
Doc.text
38-
(string_of_int (k.loc_start.pos_cnum - k.loc_start.pos_bol));
39-
Doc.text "-";
40-
Doc.text (string_of_int k.loc_end.pos_lnum);
41-
Doc.text ":";
42-
Doc.text (string_of_int (k.loc_end.pos_cnum - k.loc_end.pos_bol));
43-
Doc.rbracket;
44-
]
45-
in
44+
let loc = print_loc k in
4645
let doc =
4746
Doc.breakable_group ~force_break:true
4847
(Doc.concat
@@ -1512,7 +1511,8 @@ and walk_expression expr t comments =
15121511
let opening_token = {expr.pexp_loc with loc_end = opening_greater_than} in
15131512
let on_same_line, rest = partition_by_on_same_line opening_token comments in
15141513
attach t.trailing opening_token on_same_line;
1515-
exprs |> List.iter (fun e -> walk_expression e t rest)
1514+
let xs = exprs |> List.map (fun e -> Expression e) in
1515+
walk_list xs t rest
15161516
| Pexp_send _ -> ()
15171517

15181518
and walk_expr_parameter (_attrs, _argLbl, expr_opt, pattern) t comments =

compiler/syntax/src/res_parens.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ let jsx_child_expr expr =
384384
( Pexp_ident _ | Pexp_constant _ | Pexp_field _ | Pexp_construct _
385385
| Pexp_variant _ | Pexp_array _ | Pexp_pack _ | Pexp_record _
386386
| Pexp_extension _ | Pexp_letmodule _ | Pexp_letexception _
387-
| Pexp_open _ | Pexp_sequence _ | Pexp_let _ );
387+
| Pexp_open _ | Pexp_sequence _ | Pexp_let _ | Pexp_jsx_fragment _ );
388388
pexp_attributes = [];
389389
} ->
390390
Nothing

compiler/syntax/src/res_parsetree_viewer.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ let is_jsx_expression expr =
492492
| _ :: attrs -> loop attrs
493493
in
494494
match expr.pexp_desc with
495+
| Pexp_jsx_fragment _ -> true
495496
| Pexp_apply _ -> loop expr.Parsetree.pexp_attributes
496497
| _ -> false
497498

compiler/syntax/src/res_printer.ml

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3413,6 +3413,7 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
34133413
| Pexp_ifthenelse _ ->
34143414
true
34153415
| Pexp_match _ when ParsetreeViewer.is_if_let_expr e -> true
3416+
| Pexp_jsx_fragment _ -> true
34163417
| Pexp_construct _ when ParsetreeViewer.has_jsx_attribute e.pexp_attributes
34173418
->
34183419
true
@@ -4437,17 +4438,32 @@ and print_jsx_fragment ~state (opening_greater_than : Lexing.position)
44374438
Doc.line;
44384439
Doc.join ~sep:line_sep
44394440
(List.map
4440-
(fun e ->
4441-
let doc =
4442-
print_jsx_child ~spread:false ~state e ~cmt_tbl
4443-
in
4444-
print_comments doc cmt_tbl e.pexp_loc)
4441+
(fun e -> print_jsx_child ~state e cmt_tbl)
44454442
children);
44464443
]));
44474444
line_sep;
44484445
closing;
44494446
])
44504447

4448+
and print_jsx_child ~state (expr : Parsetree.expression) cmt_tbl =
4449+
let leading_line_comment_present =
4450+
has_leading_line_comment cmt_tbl expr.pexp_loc
4451+
in
4452+
let expr_doc = print_expression_with_comments ~state expr cmt_tbl in
4453+
let add_parens_or_braces expr_doc =
4454+
(* {(20: int)} make sure that we also protect the expression inside *)
4455+
let inner_doc =
4456+
if Parens.braced_expr expr then add_parens expr_doc else expr_doc
4457+
in
4458+
if leading_line_comment_present then add_braces inner_doc
4459+
else Doc.concat [Doc.lbrace; inner_doc; Doc.rbrace]
4460+
in
4461+
match Parens.jsx_child_expr expr with
4462+
| Nothing -> expr_doc
4463+
| Parenthesized -> add_parens_or_braces expr_doc
4464+
| Braced braces_loc ->
4465+
print_comments (add_parens_or_braces expr_doc) cmt_tbl braces_loc
4466+
44514467
and print_jsx_children ~state (children_expr : Parsetree.expression) ~sep
44524468
cmt_tbl =
44534469
match children_expr.pexp_desc with
@@ -4498,27 +4514,26 @@ and print_jsx_children ~state (children_expr : Parsetree.expression) ~sep
44984514
in
44994515
let docs = loop children_expr [] children in
45004516
Doc.group (Doc.join ~sep docs)
4501-
| _ -> print_jsx_child ~state children_expr ~cmt_tbl
4502-
4503-
and print_jsx_child ?(spread = true) ~state
4504-
(children_expr : Parsetree.expression) ~cmt_tbl =
4505-
let leading_line_comment_present =
4506-
has_leading_line_comment cmt_tbl children_expr.pexp_loc
4507-
in
4508-
let expr_doc = print_expression_with_comments ~state children_expr cmt_tbl in
4509-
Doc.concat
4510-
[
4511-
(if spread then Doc.dotdotdot else Doc.nil);
4512-
(match Parens.jsx_child_expr children_expr with
4513-
| Parenthesized | Braced _ ->
4514-
let inner_doc =
4515-
if Parens.braced_expr children_expr then add_parens expr_doc
4516-
else expr_doc
4517-
in
4518-
if leading_line_comment_present then add_braces inner_doc
4519-
else Doc.concat [Doc.lbrace; inner_doc; Doc.rbrace]
4520-
| Nothing -> expr_doc);
4521-
]
4517+
| _ ->
4518+
let leading_line_comment_present =
4519+
has_leading_line_comment cmt_tbl children_expr.pexp_loc
4520+
in
4521+
let expr_doc =
4522+
print_expression_with_comments ~state children_expr cmt_tbl
4523+
in
4524+
Doc.concat
4525+
[
4526+
Doc.dotdotdot;
4527+
(match Parens.jsx_child_expr children_expr with
4528+
| Parenthesized | Braced _ ->
4529+
let inner_doc =
4530+
if Parens.braced_expr children_expr then add_parens expr_doc
4531+
else expr_doc
4532+
in
4533+
if leading_line_comment_present then add_braces inner_doc
4534+
else Doc.concat [Doc.lbrace; inner_doc; Doc.rbrace]
4535+
| Nothing -> expr_doc);
4536+
]
45224537

45234538
and print_jsx_props ~state args cmt_tbl : Doc.t * Parsetree.expression option =
45244539
(* This function was introduced because we have different formatting behavior for self-closing tags and other tags

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,8 @@ module Cite = {
6767

6868
let x =
6969
<>
70-
{
71-
// before a
72-
a // after a
73-
// before b
74-
// after b
75-
}
76-
{
77-
// before a
78-
// after a
79-
// before b
80-
b // after b
81-
}
70+
// before a
71+
{a} // after a
72+
// before b
73+
{b} // after b
8274
</>

0 commit comments

Comments
 (0)