Skip to content

Commit 3750ca3

Browse files
authored
Allow single empty line between JSX expressions (#7246)
* Keep empty line in JSX * Changes to the tests * Handle braces expression * Fix test * Update CHANGELOG * WIP * Fix test
1 parent be5d0ec commit 3750ca3

File tree

5 files changed

+138
-27
lines changed

5 files changed

+138
-27
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
- AST cleanup: represent concatenation (`++`) and (dis)equality operators (`==`, `===`, `!=`, `!==`) just like in the syntax. https://github.com/rescript-lang/rescript/pull/7248
3232
- AST cleanup: use inline record for `Ptyp_arrow`. https://github.com/rescript-lang/rescript/pull/7250
3333
- Playground: Bundle stdlib runtime so that the playground can execute functions from Core/Belt/Js. (#7255)
34+
- Allow single newline in JSX. https://github.com/rescript-lang/rescript/pull/7246
3435

3536
#### :nail_care: Polish
3637

compiler/syntax/src/res_printer.ml

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

tests/syntax_tests/data/printer/comments/expected/jsx.res.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ let x =
6969
<>
7070
// before a
7171
{a} // after a
72+
7273
// before b
7374
{b} // after b
7475
</>

tests/syntax_tests/data/printer/expr/expected/jsx.res.txt

Lines changed: 47 additions & 0 deletions
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,49 @@ let x = {
460461
let _ = <C> {children} </C>
461462
msg->React.string
462463
}
464+
465+
let x =
466+
<div>
467+
<span
468+
id="1"
469+
className="sdf sdfdsf sdfs sdf asdf dsf"
470+
onClick={() => {
471+
()
472+
}}
473+
/>
474+
475+
// Comment 2
476+
477+
<span
478+
id="2"
479+
className="sdf sdfdsf sdfs sdf asdf dsf"
480+
onClick={() => {
481+
()
482+
}}>
483+
<span
484+
id="2-1"
485+
className="sdf sdfdsf sdfs sdf asdf dsf"
486+
onClick={() => {
487+
()
488+
}}
489+
/>
490+
491+
// Comment
492+
<span
493+
id="2-2"
494+
className="sdf sdfdsf sdfs sdf asdf dsf"
495+
onClick={() => {
496+
()
497+
}}
498+
/>
499+
500+
<span id="2-3" />
501+
</span>
502+
<span
503+
id="3"
504+
className="sdf sdfdsf sdfs sdf asdf dsf"
505+
onClick={() => {
506+
()
507+
}}
508+
/>
509+
</div>

tests/syntax_tests/data/printer/expr/jsx.res

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,52 @@ let x = {
443443
let _ = <C> {children} </C>
444444
msg->React.string
445445
}
446+
447+
let x =
448+
<div>
449+
<span
450+
id="1"
451+
className="sdf sdfdsf sdfs sdf asdf dsf"
452+
onClick={() => {
453+
()
454+
}}
455+
/>
456+
457+
// Comment 2
458+
459+
<span
460+
id="2"
461+
className="sdf sdfdsf sdfs sdf asdf dsf"
462+
onClick={() => {
463+
()
464+
}}
465+
>
466+
<span
467+
id="2-1"
468+
className="sdf sdfdsf sdfs sdf asdf dsf"
469+
onClick={() => {
470+
()
471+
}}
472+
/>
473+
474+
475+
// Comment
476+
<span
477+
id="2-2"
478+
className="sdf sdfdsf sdfs sdf asdf dsf"
479+
onClick={() => {
480+
()
481+
}}
482+
/>
483+
484+
<span id="2-3"
485+
/>
486+
</span>
487+
<span
488+
id="3"
489+
className="sdf sdfdsf sdfs sdf asdf dsf"
490+
onClick={() => {
491+
()
492+
}}
493+
/>
494+
</div>

0 commit comments

Comments
 (0)