diff --git a/CHANGELOG.md b/CHANGELOG.md index d1557a4e5e..0f667bae70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/compiler/syntax/src/res_printer.ml b/compiler/syntax/src/res_printer.ml index 8d23dabcf8..a6f039d031 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -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 = @@ -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 @@ -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 diff --git a/tests/syntax_tests/data/printer/comments/expected/jsx.res.txt b/tests/syntax_tests/data/printer/comments/expected/jsx.res.txt index 98f1bf3ee3..664018ce3b 100644 --- a/tests/syntax_tests/data/printer/comments/expected/jsx.res.txt +++ b/tests/syntax_tests/data/printer/comments/expected/jsx.res.txt @@ -65,6 +65,15 @@ module Cite = { React.string("Hello, World!")} +