From fc5a1087b0af27a8e05de7e8a2c9656af1a34b60 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Sun, 12 Jan 2025 19:47:06 +0800 Subject: [PATCH 1/7] 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 1f48309c24..7d30537089 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -4433,35 +4433,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 63093e4bcca0e1544898fb0dca0524481e6a9ca2 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Tue, 14 Jan 2025 09:09:45 +0800 Subject: [PATCH 2/7] 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 33aa565971535fe66bb551ed8938e72edbd50646 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Tue, 28 Jan 2025 18:15:09 +0800 Subject: [PATCH 3/7] 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 7d30537089..e1c3da60c3 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -4469,6 +4469,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 90cf7d874c4bc784f3071c70fd21400c4a6d0570 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Wed, 29 Jan 2025 13:28:34 +0800 Subject: [PATCH 4/7] 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 e0c57a042328819c3bdd25e7a1902a70c1972edb Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Wed, 29 Jan 2025 13:30:04 +0800 Subject: [PATCH 5/7] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3361e96640..cee5414e93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ - AST cleanup: represent concatenation (`++`) and (dis)equality operators (`==`, `===`, `!=`, `!==`) just like in the syntax. https://github.com/rescript-lang/rescript/pull/7248 - AST cleanup: use inline record for `Ptyp_arrow`. https://github.com/rescript-lang/rescript/pull/7250 - Playground: Bundle stdlib runtime so that the playground can execute functions from Core/Belt/Js. (#7255) +- Allow single newline in JSX. https://github.com/rescript-lang/rescript/pull/7246 # 12.0.0-alpha.7 From 58540ccaae0cce39d61c46281f2d0c6930dfc2ff Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Wed, 29 Jan 2025 15:31:05 +0800 Subject: [PATCH 6/7] 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 e1c3da60c3..674e75139a 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -4433,7 +4433,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 = @@ -4454,41 +4454,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 98d2ca9b52756eb5b9e69eda5b6dce651e9aa396 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Wed, 29 Jan 2025 16:23:16 +0800 Subject: [PATCH 7/7] 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)