Skip to content

Commit aea2fed

Browse files
authored
Allow empty line between JSX expressions (#7269)
* Keep empty line in JSX * Changes to the tests * Handle braces expression * Fix test * Update CHANGELOG * WIP * Fix test * Handle case with comments * Fix test * Fix formatting * Update CHANGELOG
1 parent bb0b6a7 commit aea2fed

File tree

4 files changed

+180
-27
lines changed

4 files changed

+180
-27
lines changed

Diff for: CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
1313
# 12.0.0-alpha.9 (Unreleased)
1414

15+
#### :nail_care: Polish
16+
17+
- Allow single newline in JSX. https://github.com/rescript-lang/rescript/pull/7269
18+
1519
# 12.0.0-alpha.8
1620

1721
#### :bug: Bug fix

Diff for: compiler/syntax/src/res_printer.ml

+45-27
Original file line numberDiff line numberDiff line change
@@ -4429,33 +4429,51 @@ and print_jsx_children ~state (children_expr : Parsetree.expression) ~sep
44294429
match children_expr.pexp_desc with
44304430
| Pexp_construct ({txt = Longident.Lident "::"}, _) ->
44314431
let children, _ = ParsetreeViewer.collect_list_expressions children_expr in
4432-
Doc.group
4433-
(Doc.join ~sep
4434-
(List.map
4435-
(fun (expr : Parsetree.expression) ->
4436-
let leading_line_comment_present =
4437-
has_leading_line_comment cmt_tbl expr.pexp_loc
4438-
in
4439-
let expr_doc =
4440-
print_expression_with_comments ~state expr cmt_tbl
4441-
in
4442-
let add_parens_or_braces expr_doc =
4443-
(* {(20: int)} make sure that we also protect the expression inside *)
4444-
let inner_doc =
4445-
if Parens.braced_expr expr then add_parens expr_doc
4446-
else expr_doc
4447-
in
4448-
if leading_line_comment_present then add_braces inner_doc
4449-
else Doc.concat [Doc.lbrace; inner_doc; Doc.rbrace]
4450-
in
4451-
match Parens.jsx_child_expr expr with
4452-
| Nothing -> expr_doc
4453-
| Parenthesized -> add_parens_or_braces expr_doc
4454-
| Braced braces_loc ->
4455-
print_comments
4456-
(add_parens_or_braces expr_doc)
4457-
cmt_tbl braces_loc)
4458-
children))
4432+
let print_expr (expr : Parsetree.expression) =
4433+
let leading_line_comment_present =
4434+
has_leading_line_comment cmt_tbl expr.pexp_loc
4435+
in
4436+
let expr_doc = print_expression_with_comments ~state expr cmt_tbl in
4437+
let add_parens_or_braces expr_doc =
4438+
(* {(20: int)} make sure that we also protect the expression inside *)
4439+
let inner_doc =
4440+
if Parens.braced_expr expr then add_parens expr_doc else expr_doc
4441+
in
4442+
if leading_line_comment_present then add_braces inner_doc
4443+
else Doc.concat [Doc.lbrace; inner_doc; Doc.rbrace]
4444+
in
4445+
match Parens.jsx_child_expr expr with
4446+
| Nothing -> expr_doc
4447+
| Parenthesized -> add_parens_or_braces expr_doc
4448+
| Braced braces_loc ->
4449+
print_comments (add_parens_or_braces expr_doc) cmt_tbl braces_loc
4450+
in
4451+
let get_first_leading_comment loc =
4452+
match get_first_leading_comment cmt_tbl loc with
4453+
| None -> loc
4454+
| Some comment -> Comment.loc comment
4455+
in
4456+
let get_loc expr =
4457+
match ParsetreeViewer.process_braces_attr expr with
4458+
| None, _ -> get_first_leading_comment expr.pexp_loc
4459+
| Some ({loc}, _), _ -> get_first_leading_comment loc
4460+
in
4461+
let rec loop prev acc exprs =
4462+
match exprs with
4463+
| [] -> List.rev acc
4464+
| expr :: tails ->
4465+
let start_loc = (get_loc expr).loc_start.pos_lnum in
4466+
let end_loc = (get_loc prev).loc_end.pos_lnum in
4467+
let expr_doc = print_expr expr in
4468+
let docs =
4469+
if start_loc - end_loc > 1 then
4470+
Doc.concat [Doc.hard_line; expr_doc] :: acc
4471+
else expr_doc :: acc
4472+
in
4473+
loop expr docs tails
4474+
in
4475+
let docs = loop children_expr [] children in
4476+
Doc.group (Doc.join ~sep docs)
44594477
| _ ->
44604478
let leading_line_comment_present =
44614479
has_leading_line_comment cmt_tbl children_expr.pexp_loc

Diff for: tests/syntax_tests/data/printer/expr/expected/jsx.res.txt

+65
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ module App = {
351351
// comment
352352
content
353353
}
354+
354355
{
355356
// comment
356357
if condition() {
@@ -460,3 +461,67 @@ let x = {
460461
let _ = <C> {children} </C>
461462
msg->React.string
462463
}
464+
465+
let x =
466+
<div>
467+
{
468+
hello()
469+
hello()
470+
}
471+
{
472+
world()
473+
world()
474+
}
475+
476+
{
477+
hello()
478+
hello()
479+
}
480+
{
481+
world()
482+
world()
483+
}
484+
// another test
485+
<span
486+
id="1"
487+
className="sdf sdfdsf sdfs sdf asdf dsf"
488+
onClick={() => {
489+
()
490+
}}
491+
/>
492+
493+
// Comment 2
494+
495+
<span
496+
id="2"
497+
className="sdf sdfdsf sdfs sdf asdf dsf"
498+
onClick={() => {
499+
()
500+
}}>
501+
<span
502+
id="2-1"
503+
className="sdf sdfdsf sdfs sdf asdf dsf"
504+
onClick={() => {
505+
()
506+
}}
507+
/>
508+
509+
// Comment
510+
<span
511+
id="2-2"
512+
className="sdf sdfdsf sdfs sdf asdf dsf"
513+
onClick={() => {
514+
()
515+
}}
516+
/>
517+
518+
<span id="2-3" />
519+
</span>
520+
<span
521+
id="3"
522+
className="sdf sdfdsf sdfs sdf asdf dsf"
523+
onClick={() => {
524+
()
525+
}}
526+
/>
527+
</div>

Diff for: tests/syntax_tests/data/printer/expr/jsx.res

+66
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,69 @@ let x = {
443443
let _ = <C> {children} </C>
444444
msg->React.string
445445
}
446+
447+
let x =
448+
<div>
449+
{
450+
hello()
451+
hello()
452+
}
453+
{
454+
world()
455+
world()
456+
}
457+
458+
{
459+
hello()
460+
hello()
461+
} {
462+
world()
463+
world()
464+
}
465+
// another test
466+
<span
467+
id="1"
468+
className="sdf sdfdsf sdfs sdf asdf dsf"
469+
onClick={() => {
470+
()
471+
}}
472+
/>
473+
474+
// Comment 2
475+
476+
<span
477+
id="2"
478+
className="sdf sdfdsf sdfs sdf asdf dsf"
479+
onClick={() => {
480+
()
481+
}}
482+
>
483+
<span
484+
id="2-1"
485+
className="sdf sdfdsf sdfs sdf asdf dsf"
486+
onClick={() => {
487+
()
488+
}}
489+
/>
490+
491+
492+
// Comment
493+
<span
494+
id="2-2"
495+
className="sdf sdfdsf sdfs sdf asdf dsf"
496+
onClick={() => {
497+
()
498+
}}
499+
/>
500+
501+
<span id="2-3"
502+
/>
503+
</span>
504+
<span
505+
id="3"
506+
className="sdf sdfdsf sdfs sdf asdf dsf"
507+
onClick={() => {
508+
()
509+
}}
510+
/>
511+
</div>

0 commit comments

Comments
 (0)