Skip to content

Fix comments removed in JSX #7424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

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

#### :house: Internal

Expand Down
27 changes: 13 additions & 14 deletions compiler/syntax/src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4584,6 +4584,18 @@ and get_line_sep_for_jsx_children (children : Parsetree.jsx_children) =

and print_jsx_children ~state (children : Parsetree.jsx_children) cmt_tbl =
let open Parsetree in
let get_loc (expr : Parsetree.expression) =
let braces =
expr.pexp_attributes
|> List.find_map (fun (attr, _) ->
match attr with
| {Location.txt = "res.braces"; loc} -> Some loc
| _ -> None)
in
match braces with
| None -> expr.pexp_loc
| Some loc -> loc
in
let sep = get_line_sep_for_jsx_children children in
let print_expr (expr : Parsetree.expression) =
let leading_line_comment_present =
Expand All @@ -4599,7 +4611,7 @@ and print_jsx_children ~state (children : Parsetree.jsx_children) cmt_tbl =
else Doc.concat [Doc.lbrace; inner_doc; Doc.rbrace]
in
match Parens.jsx_child_expr expr with
| Nothing -> expr_doc
| Nothing -> print_comments expr_doc cmt_tbl (get_loc expr)
| Parenthesized -> add_parens_or_braces expr_doc
| Braced braces_loc ->
print_comments (add_parens_or_braces expr_doc) cmt_tbl braces_loc
Expand All @@ -4608,19 +4620,6 @@ and print_jsx_children ~state (children : Parsetree.jsx_children) cmt_tbl =
| JSXChildrenItems [] -> Doc.nil
| JSXChildrenSpreading child -> Doc.concat [Doc.dotdotdot; print_expr child]
| JSXChildrenItems children ->
let get_loc (expr : Parsetree.expression) =
let braces =
expr.pexp_attributes
|> List.find_map (fun (attr, _) ->
match attr with
| {Location.txt = "res.braces"; loc} -> Some loc
| _ -> None)
in
match braces with
| None -> expr.pexp_loc
| Some loc -> loc
in

let rec visit acc children =
match children with
| [] -> acc
Expand Down
9 changes: 9 additions & 0 deletions tests/syntax_tests/data/printer/comments/expected/jsx.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ module Cite = {
React.string("Hello, World!")}
</div>

<div>
// Outside comment
{
// But this one is inside
let x = 1
let y = 2
}
</div>

let x =
<>
// before a
Expand Down
8 changes: 8 additions & 0 deletions tests/syntax_tests/data/printer/comments/jsx.res
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ value=""
React.string("Hello, World!")}
</div>

<div>
// Outside comment
{// But this one is inside
let x = 1
let y = 2
}
</div>

let x = <>
// before a
{a} // after a
Expand Down