From 7f35404cce6e38196c00ab6820fb47da5da674c7 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Mon, 5 May 2025 19:17:31 +0800 Subject: [PATCH 1/3] WIP --- compiler/syntax/src/res_printer.ml | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) 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 From 4782d78545eb1c73d231464b8d2d7857bbfae54d Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Mon, 5 May 2025 20:13:14 +0800 Subject: [PATCH 2/3] Add test --- .../data/printer/comments/expected/jsx.res.txt | 9 +++++++++ tests/syntax_tests/data/printer/comments/jsx.res | 8 ++++++++ 2 files changed, 17 insertions(+) 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!")} +
+ // Outside comment + { + // But this one is inside + let x = 1 + let y = 2 + } +
+ let x = <> // before a diff --git a/tests/syntax_tests/data/printer/comments/jsx.res b/tests/syntax_tests/data/printer/comments/jsx.res index d554e249af..40dd824745 100644 --- a/tests/syntax_tests/data/printer/comments/jsx.res +++ b/tests/syntax_tests/data/printer/comments/jsx.res @@ -67,6 +67,14 @@ value="" React.string("Hello, World!")} +
+ // Outside comment + {// But this one is inside + let x = 1 + let y = 2 + } +
+ let x = <> // before a {a} // after a From 18a44418a27b4267f55d378eeaad935de53ade51 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Mon, 5 May 2025 20:29:50 +0800 Subject: [PATCH 3/3] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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