Skip to content

Commit

Permalink
Revert "Allow single empty line between JSX expressions (#7246)" (#7263)
Browse files Browse the repository at this point in the history
This reverts commit 3750ca3.
  • Loading branch information
cknitt authored Jan 30, 2025
1 parent bb663bd commit 7105216
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 138 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
- 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

#### :nail_care: Polish

Expand Down
67 changes: 27 additions & 40 deletions compiler/syntax/src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4435,46 +4435,33 @@ and print_jsx_children ~state (children_expr : Parsetree.expression) ~sep
match children_expr.pexp_desc with
| 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 =
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 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
| 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 start_loc - end_loc > 1 then
Doc.concat [Doc.hard_line; expr_doc] :: acc
else expr_doc :: acc
in
loop expr docs tails
in
let docs = loop children_expr [] children in
Doc.group (Doc.join ~sep docs)
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 leading_line_comment_present =
has_leading_line_comment cmt_tbl children_expr.pexp_loc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ let x =
<>
// before a
{a} // after a

// before b
{b} // after b
</>
47 changes: 0 additions & 47 deletions tests/syntax_tests/data/printer/expr/expected/jsx.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ module App = {
// comment
content
}

{
// comment
if condition() {
Expand Down Expand Up @@ -461,49 +460,3 @@ let x = {
let _ = <C> {children} </C>
msg->React.string
}

let x =
<div>
<span
id="1"
className="sdf sdfdsf sdfs sdf asdf dsf"
onClick={() => {
()
}}
/>

// Comment 2

<span
id="2"
className="sdf sdfdsf sdfs sdf asdf dsf"
onClick={() => {
()
}}>
<span
id="2-1"
className="sdf sdfdsf sdfs sdf asdf dsf"
onClick={() => {
()
}}
/>

// Comment
<span
id="2-2"
className="sdf sdfdsf sdfs sdf asdf dsf"
onClick={() => {
()
}}
/>

<span id="2-3" />
</span>
<span
id="3"
className="sdf sdfdsf sdfs sdf asdf dsf"
onClick={() => {
()
}}
/>
</div>
49 changes: 0 additions & 49 deletions tests/syntax_tests/data/printer/expr/jsx.res
Original file line number Diff line number Diff line change
Expand Up @@ -443,52 +443,3 @@ let x = {
let _ = <C> {children} </C>
msg->React.string
}

let x =
<div>
<span
id="1"
className="sdf sdfdsf sdfs sdf asdf dsf"
onClick={() => {
()
}}
/>

// Comment 2

<span
id="2"
className="sdf sdfdsf sdfs sdf asdf dsf"
onClick={() => {
()
}}
>
<span
id="2-1"
className="sdf sdfdsf sdfs sdf asdf dsf"
onClick={() => {
()
}}
/>


// Comment
<span
id="2-2"
className="sdf sdfdsf sdfs sdf asdf dsf"
onClick={() => {
()
}}
/>

<span id="2-3"
/>
</span>
<span
id="3"
className="sdf sdfdsf sdfs sdf asdf dsf"
onClick={() => {
()
}}
/>
</div>

1 comment on commit 7105216

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Syntax Benchmarks'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.05.

Benchmark suite Current: 7105216 Previous: e1b7fb7 Ratio
Parse RedBlackTree.res - time/run 1.3621886066666666 ms 1.2123143266666667 ms 1.12
Parse Napkinscript.res - time/run 42.66800703333333 ms 39.28006235333333 ms 1.09
Parse HeroGraphic.res - time/run 6.431421599999999 ms 5.13472718 ms 1.25

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.