From 12888d760061c6734cfca77850e973fdd3223823 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Sun, 12 Jan 2025 19:47:06 +0800 Subject: [PATCH 01/11] Keep empty line in JSX --- compiler/syntax/src/res_printer.ml | 67 +++++++++++++++++------------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/compiler/syntax/src/res_printer.ml b/compiler/syntax/src/res_printer.ml index 0b887d74fd..510304649d 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -4427,35 +4427,46 @@ and print_jsx_fragment ~state expr cmt_tbl = and print_jsx_children ~state (children_expr : Parsetree.expression) ~sep cmt_tbl = match children_expr.pexp_desc with - | Pexp_construct ({txt = Longident.Lident "::"}, _) -> + | Pexp_construct ({loc; txt = Longident.Lident "::"}, _) -> let children, _ = ParsetreeViewer.collect_list_expressions children_expr in - Doc.group - (Doc.join ~sep - (List.map - (fun (expr : Parsetree.expression) -> - let leading_line_comment_present = - has_leading_line_comment cmt_tbl expr.pexp_loc - in - let expr_doc = - print_expression_with_comments ~state expr cmt_tbl - in - let add_parens_or_braces expr_doc = - (* {(20: int)} make sure that we also protect the expression inside *) - let inner_doc = - if Parens.braced_expr expr then add_parens expr_doc - else expr_doc - in - if leading_line_comment_present then add_braces inner_doc - else Doc.concat [Doc.lbrace; inner_doc; Doc.rbrace] - in - match Parens.jsx_child_expr expr with - | Nothing -> expr_doc - | Parenthesized -> add_parens_or_braces expr_doc - | Braced braces_loc -> - print_comments - (add_parens_or_braces expr_doc) - cmt_tbl braces_loc) - children)) + let print_expr (expr : Parsetree.expression) = + let leading_line_comment_present = + has_leading_line_comment cmt_tbl expr.pexp_loc + in + let expr_doc = print_expression_with_comments ~state expr cmt_tbl in + let add_parens_or_braces expr_doc = + (* {(20: int)} make sure that we also protect the expression inside *) + let inner_doc = + if Parens.braced_expr expr then add_parens expr_doc else expr_doc + in + if leading_line_comment_present then add_braces inner_doc + else Doc.concat [Doc.lbrace; inner_doc; Doc.rbrace] + in + match Parens.jsx_child_expr expr with + | Nothing -> expr_doc + | Parenthesized -> add_parens_or_braces expr_doc + | Braced braces_loc -> + print_comments (add_parens_or_braces expr_doc) cmt_tbl braces_loc + in + let rec loop prev acc exprs = + match exprs with + | [] -> List.rev acc + | ({Parsetree.pexp_loc = current_loc} as expr) :: tails -> + let docs = + if current_loc.loc_start.pos_lnum - prev.Warnings.loc_end.pos_lnum > 1 + then + let expr = print_expr expr in + let acc = Doc.concat [Doc.hard_line; expr] :: acc in + acc + else + let expr = print_expr expr in + let acc = expr :: acc in + acc + in + loop current_loc docs tails + in + let docs = loop loc [] children in + Doc.group (Doc.join ~sep docs) | _ -> let leading_line_comment_present = has_leading_line_comment cmt_tbl children_expr.pexp_loc From f100b6339434b3cd2fb42fc66485f3dee1aa9a0c Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Tue, 14 Jan 2025 09:09:45 +0800 Subject: [PATCH 02/11] Changes to the tests --- .../printer/comments/expected/jsx.res.txt | 1 + .../data/printer/expr/expected/jsx.res.txt | 52 +++++++++++++++++++ tests/syntax_tests/data/printer/expr/jsx.res | 49 +++++++++++++++++ 3 files changed, 102 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..a6b84d48d1 100644 --- a/tests/syntax_tests/data/printer/comments/expected/jsx.res.txt +++ b/tests/syntax_tests/data/printer/comments/expected/jsx.res.txt @@ -69,6 +69,7 @@ let x = <> // before a {a} // after a + // before b {b} // after b diff --git a/tests/syntax_tests/data/printer/expr/expected/jsx.res.txt b/tests/syntax_tests/data/printer/expr/expected/jsx.res.txt index 2384f5ecb6..1ee1525c64 100644 --- a/tests/syntax_tests/data/printer/expr/expected/jsx.res.txt +++ b/tests/syntax_tests/data/printer/expr/expected/jsx.res.txt @@ -210,11 +210,13 @@ let x =
ident "constant" + { let a = 1 let b = 2 a + b } + {event => handleClick(event)} {(event1, event2, event3) => { Js.log("debug click") @@ -263,14 +265,17 @@ let x = do(i) }} {(20: int)} + { module L = Log L.log() } + { exception Exit raise(Exit) } + {assert(true)} {module(Foo)} module(Foo) @@ -351,6 +356,7 @@ module App = { // comment content } + { // comment if condition() { @@ -460,3 +466,49 @@ let x = { let _ = {children} msg->React.string } + +let x = +
+ { + () + }} + /> + + // Comment 2 + + { + () + }}> + { + () + }} + /> + + // Comment + { + () + }} + /> + + + + { + () + }} + /> +
diff --git a/tests/syntax_tests/data/printer/expr/jsx.res b/tests/syntax_tests/data/printer/expr/jsx.res index 2745f5974f..b3d76d081c 100644 --- a/tests/syntax_tests/data/printer/expr/jsx.res +++ b/tests/syntax_tests/data/printer/expr/jsx.res @@ -443,3 +443,52 @@ let x = { let _ = {children} msg->React.string } + +let x = +
+ { + () + }} + /> + + // Comment 2 + + { + () + }} + > + { + () + }} + /> + + + // Comment + { + () + }} + /> + + + + { + () + }} + /> +
From 9823662ba5c0041464517d0e08aab5f96ea8766b Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Tue, 28 Jan 2025 18:15:09 +0800 Subject: [PATCH 03/11] Handle braces expression --- compiler/syntax/src/res_printer.ml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/compiler/syntax/src/res_printer.ml b/compiler/syntax/src/res_printer.ml index 510304649d..299807e212 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -4463,6 +4463,23 @@ and print_jsx_children ~state (children_expr : Parsetree.expression) ~sep let acc = expr :: acc in acc in + (* adjust for braces when we forward the current_loc to the recursion *) + let current_loc = + match expr with + | {Parsetree.pexp_loc = loc} + when loc.loc_start.pos_lnum == loc.loc_end.pos_lnum -> + current_loc + | _ when ParsetreeViewer.is_braced_expr expr -> + { + current_loc with + loc_end = + { + current_loc.loc_end with + pos_lnum = current_loc.loc_end.pos_lnum + 1; + }; + } + | _ -> current_loc + in loop current_loc docs tails in let docs = loop loc [] children in From 6b118f9b87220f8813ac4c97dd939b90fc6a5cdb Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Wed, 29 Jan 2025 13:28:34 +0800 Subject: [PATCH 04/11] Fix test --- tests/syntax_tests/data/printer/expr/expected/jsx.res.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/syntax_tests/data/printer/expr/expected/jsx.res.txt b/tests/syntax_tests/data/printer/expr/expected/jsx.res.txt index 1ee1525c64..3485590ff5 100644 --- a/tests/syntax_tests/data/printer/expr/expected/jsx.res.txt +++ b/tests/syntax_tests/data/printer/expr/expected/jsx.res.txt @@ -216,7 +216,6 @@ let x = let b = 2 a + b } - {event => handleClick(event)} {(event1, event2, event3) => { Js.log("debug click") @@ -275,7 +274,6 @@ let x = exception Exit raise(Exit) } - {assert(true)} {module(Foo)} module(Foo) From 969fed2a909a7fc07fa6f06dbdc1953ff989f5d2 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Wed, 29 Jan 2025 13:30:04 +0800 Subject: [PATCH 05/11] Update CHANGELOG --- CHANGELOG.md | 3 +++ Example.res | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 Example.res diff --git a/CHANGELOG.md b/CHANGELOG.md index facc5f0d36..d5faca8e9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ # 12.0.0-alpha.9 (Unreleased) +#### :bug: Bug fix +- Allow single newline in JSX. https://github.com/rescript-lang/rescript/pull/7246 + # 12.0.0-alpha.8 #### :bug: Bug fix diff --git a/Example.res b/Example.res new file mode 100644 index 0000000000..752ea08d2a --- /dev/null +++ b/Example.res @@ -0,0 +1,58 @@ +
+ // before a + {a} // after a + + {b} // after b + + { + hello() + hello() + } + { + world() + world() + } + + { + hello() + hello() + } { + world() + world() + } + // another test + { + () + }} + /> + + // comment + { + () + }} + /> + { + () + }} + /> + { hello() } + + { + hello() + } + + + { + world() + world() + } +
From fbf8b11b9b1539a9f48f2d69e6ea5f7fc6ad3e43 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Wed, 29 Jan 2025 15:31:05 +0800 Subject: [PATCH 06/11] WIP --- compiler/syntax/src/res_printer.ml | 45 ++++++++++-------------------- 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/compiler/syntax/src/res_printer.ml b/compiler/syntax/src/res_printer.ml index 299807e212..d24b6f7877 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -4427,7 +4427,7 @@ and print_jsx_fragment ~state expr cmt_tbl = and print_jsx_children ~state (children_expr : Parsetree.expression) ~sep cmt_tbl = match children_expr.pexp_desc with - | Pexp_construct ({loc; txt = Longident.Lident "::"}, _) -> + | Pexp_construct ({txt = Longident.Lident "::"}, _) -> let children, _ = ParsetreeViewer.collect_list_expressions children_expr in let print_expr (expr : Parsetree.expression) = let leading_line_comment_present = @@ -4448,41 +4448,26 @@ and print_jsx_children ~state (children_expr : Parsetree.expression) ~sep | Braced braces_loc -> print_comments (add_parens_or_braces expr_doc) cmt_tbl braces_loc in + let get_loc expr = + match ParsetreeViewer.process_braces_attr expr with + | None, _ -> expr.pexp_loc + | Some ({loc}, _), _ -> loc + in let rec loop prev acc exprs = match exprs with | [] -> List.rev acc - | ({Parsetree.pexp_loc = current_loc} as expr) :: tails -> + | expr :: tails -> + let start_loc = (get_loc expr).loc_start.pos_lnum in + let end_loc = (get_loc prev).loc_end.pos_lnum in + let expr_doc = print_expr expr in let docs = - if current_loc.loc_start.pos_lnum - prev.Warnings.loc_end.pos_lnum > 1 - then - let expr = print_expr expr in - let acc = Doc.concat [Doc.hard_line; expr] :: acc in - acc - else - let expr = print_expr expr in - let acc = expr :: acc in - acc - in - (* adjust for braces when we forward the current_loc to the recursion *) - let current_loc = - match expr with - | {Parsetree.pexp_loc = loc} - when loc.loc_start.pos_lnum == loc.loc_end.pos_lnum -> - current_loc - | _ when ParsetreeViewer.is_braced_expr expr -> - { - current_loc with - loc_end = - { - current_loc.loc_end with - pos_lnum = current_loc.loc_end.pos_lnum + 1; - }; - } - | _ -> current_loc + if start_loc - end_loc > 1 then + Doc.concat [Doc.hard_line; expr_doc] :: acc + else expr_doc :: acc in - loop current_loc docs tails + loop expr docs tails in - let docs = loop loc [] children in + let docs = loop children_expr [] children in Doc.group (Doc.join ~sep docs) | _ -> let leading_line_comment_present = From 1ca442a250280b8aba819fa3720d74e52986bc09 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Wed, 29 Jan 2025 16:23:16 +0800 Subject: [PATCH 07/11] Fix test --- tests/syntax_tests/data/printer/expr/expected/jsx.res.txt | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/syntax_tests/data/printer/expr/expected/jsx.res.txt b/tests/syntax_tests/data/printer/expr/expected/jsx.res.txt index 3485590ff5..4c59e0f619 100644 --- a/tests/syntax_tests/data/printer/expr/expected/jsx.res.txt +++ b/tests/syntax_tests/data/printer/expr/expected/jsx.res.txt @@ -210,7 +210,6 @@ let x =
ident "constant" - { let a = 1 let b = 2 @@ -264,12 +263,10 @@ let x = do(i) }} {(20: int)} - { module L = Log L.log() } - { exception Exit raise(Exit) From a7a48e55ab4fc6edb5e4480de76c9ff12fb1a9cd Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Sat, 1 Feb 2025 12:47:11 +0800 Subject: [PATCH 08/11] Handle case with comments --- compiler/syntax/src/res_printer.ml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/compiler/syntax/src/res_printer.ml b/compiler/syntax/src/res_printer.ml index d24b6f7877..e31e351a7e 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -4448,10 +4448,15 @@ and print_jsx_children ~state (children_expr : Parsetree.expression) ~sep | Braced braces_loc -> print_comments (add_parens_or_braces expr_doc) cmt_tbl braces_loc in + let get_first_leading_comment loc = + match get_first_leading_comment cmt_tbl loc with + | None -> loc + | Some comment -> Comment.loc comment + in let get_loc expr = match ParsetreeViewer.process_braces_attr expr with - | None, _ -> expr.pexp_loc - | Some ({loc}, _), _ -> loc + | None, _ -> get_first_leading_comment expr.pexp_loc + | Some ({loc}, _), _ -> get_first_leading_comment loc in let rec loop prev acc exprs = match exprs with From 5cbeca6b14e720057d47fb4160002a1d0fb30970 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Sat, 1 Feb 2025 12:49:43 +0800 Subject: [PATCH 09/11] Fix test --- Example.res | 58 ------------------- .../printer/comments/expected/jsx.res.txt | 1 - .../data/printer/expr/expected/jsx.res.txt | 18 ++++++ tests/syntax_tests/data/printer/expr/jsx.res | 17 ++++++ 4 files changed, 35 insertions(+), 59 deletions(-) delete mode 100644 Example.res diff --git a/Example.res b/Example.res deleted file mode 100644 index 752ea08d2a..0000000000 --- a/Example.res +++ /dev/null @@ -1,58 +0,0 @@ -
- // before a - {a} // after a - - {b} // after b - - { - hello() - hello() - } - { - world() - world() - } - - { - hello() - hello() - } { - world() - world() - } - // another test - { - () - }} - /> - - // comment - { - () - }} - /> - { - () - }} - /> - { hello() } - - { - hello() - } - - - { - world() - world() - } -
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 a6b84d48d1..98f1bf3ee3 100644 --- a/tests/syntax_tests/data/printer/comments/expected/jsx.res.txt +++ b/tests/syntax_tests/data/printer/comments/expected/jsx.res.txt @@ -69,7 +69,6 @@ let x = <> // before a {a} // after a - // before b {b} // after b diff --git a/tests/syntax_tests/data/printer/expr/expected/jsx.res.txt b/tests/syntax_tests/data/printer/expr/expected/jsx.res.txt index 4c59e0f619..5df8ca4e5a 100644 --- a/tests/syntax_tests/data/printer/expr/expected/jsx.res.txt +++ b/tests/syntax_tests/data/printer/expr/expected/jsx.res.txt @@ -464,6 +464,24 @@ let x = { let x =
+ { + hello() + hello() + } + { + world() + world() + } + + { + hello() + hello() + } + { + world() + world() + } + // another test + { + hello() + hello() + } + { + world() + world() + } + + { + hello() + hello() + } { + world() + world() + } + // another test Date: Sat, 1 Feb 2025 13:09:40 +0800 Subject: [PATCH 10/11] Fix formatting --- compiler/syntax/src/res_printer.ml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/syntax/src/res_printer.ml b/compiler/syntax/src/res_printer.ml index e31e351a7e..9b313a00ea 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -4448,10 +4448,10 @@ and print_jsx_children ~state (children_expr : Parsetree.expression) ~sep | Braced braces_loc -> print_comments (add_parens_or_braces expr_doc) cmt_tbl braces_loc in - let get_first_leading_comment loc = - match get_first_leading_comment cmt_tbl loc with - | None -> loc - | Some comment -> Comment.loc comment + let get_first_leading_comment loc = + match get_first_leading_comment cmt_tbl loc with + | None -> loc + | Some comment -> Comment.loc comment in let get_loc expr = match ParsetreeViewer.process_braces_attr expr with From 8ec1952693d4d591d0bccd062909782ffd6d713d Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Sun, 2 Feb 2025 07:58:49 +0800 Subject: [PATCH 11/11] Update CHANGELOG --- CHANGELOG.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5faca8e9a..ec807a97d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,8 +12,9 @@ # 12.0.0-alpha.9 (Unreleased) -#### :bug: Bug fix -- Allow single newline in JSX. https://github.com/rescript-lang/rescript/pull/7246 +#### :nail_care: Polish + +- Allow single newline in JSX. https://github.com/rescript-lang/rescript/pull/7269 # 12.0.0-alpha.8