Skip to content

Commit fb3a6bd

Browse files
authored
Fix comments removed in JSX (#7424)
* WIP * Add test * Update CHANGELOG
1 parent 64a29eb commit fb3a6bd

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
- Fix broken `bstracing` CLI location. https://github.com/rescript-lang/rescript/pull/7398
3131
- Fix field flattening optimization to avoid creating unnecessary copies of allocating constants. https://github.com/rescript-lang/rescript-compiler/pull/7421
32+
- Fix leading comments removed when braces inside JSX contains `let` assignment. https://github.com/rescript-lang/rescript/pull/7424
3233

3334
#### :house: Internal
3435

compiler/syntax/src/res_printer.ml

+13-14
Original file line numberDiff line numberDiff line change
@@ -4584,6 +4584,18 @@ and get_line_sep_for_jsx_children (children : Parsetree.jsx_children) =
45844584

45854585
and print_jsx_children ~state (children : Parsetree.jsx_children) cmt_tbl =
45864586
let open Parsetree in
4587+
let get_loc (expr : Parsetree.expression) =
4588+
let braces =
4589+
expr.pexp_attributes
4590+
|> List.find_map (fun (attr, _) ->
4591+
match attr with
4592+
| {Location.txt = "res.braces"; loc} -> Some loc
4593+
| _ -> None)
4594+
in
4595+
match braces with
4596+
| None -> expr.pexp_loc
4597+
| Some loc -> loc
4598+
in
45874599
let sep = get_line_sep_for_jsx_children children in
45884600
let print_expr (expr : Parsetree.expression) =
45894601
let leading_line_comment_present =
@@ -4599,7 +4611,7 @@ and print_jsx_children ~state (children : Parsetree.jsx_children) cmt_tbl =
45994611
else Doc.concat [Doc.lbrace; inner_doc; Doc.rbrace]
46004612
in
46014613
match Parens.jsx_child_expr expr with
4602-
| Nothing -> expr_doc
4614+
| Nothing -> print_comments expr_doc cmt_tbl (get_loc expr)
46034615
| Parenthesized -> add_parens_or_braces expr_doc
46044616
| Braced braces_loc ->
46054617
print_comments (add_parens_or_braces expr_doc) cmt_tbl braces_loc
@@ -4608,19 +4620,6 @@ and print_jsx_children ~state (children : Parsetree.jsx_children) cmt_tbl =
46084620
| JSXChildrenItems [] -> Doc.nil
46094621
| JSXChildrenSpreading child -> Doc.concat [Doc.dotdotdot; print_expr child]
46104622
| JSXChildrenItems children ->
4611-
let get_loc (expr : Parsetree.expression) =
4612-
let braces =
4613-
expr.pexp_attributes
4614-
|> List.find_map (fun (attr, _) ->
4615-
match attr with
4616-
| {Location.txt = "res.braces"; loc} -> Some loc
4617-
| _ -> None)
4618-
in
4619-
match braces with
4620-
| None -> expr.pexp_loc
4621-
| Some loc -> loc
4622-
in
4623-
46244623
let rec visit acc children =
46254624
match children with
46264625
| [] -> acc

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

+9
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ module Cite = {
6565
React.string("Hello, World!")}
6666
</div>
6767

68+
<div>
69+
// Outside comment
70+
{
71+
// But this one is inside
72+
let x = 1
73+
let y = 2
74+
}
75+
</div>
76+
6877
let x =
6978
<>
7079
// before a

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

+8
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ value=""
6767
React.string("Hello, World!")}
6868
</div>
6969

70+
<div>
71+
// Outside comment
72+
{// But this one is inside
73+
let x = 1
74+
let y = 2
75+
}
76+
</div>
77+
7078
let x = <>
7179
// before a
7280
{a} // after a

0 commit comments

Comments
 (0)