Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 2074447

Browse files
authored
Put each jsx tag on own line (#574)
* Put each JSX tag on its own line * Put each JSX tag nested in fragment on its own line * Refactor code to be more DRY * Avoid list allocations while looking for nested JSX exprs * Simplify “has nested jsx” check, do not recurse in non-list child exprs * Bring back recursion while looking for nested JSX, add more tests * Minor code styling * Put JSX children on own lines when more than two found * Simplify and more precise name for nested jsx search function
1 parent b1b9b5e commit 2074447

File tree

7 files changed

+93
-15
lines changed

7 files changed

+93
-15
lines changed

src/res_printer.ml

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ let hasCommentBelow tbl loc =
9999
| [] -> false
100100
| exception Not_found -> false
101101

102+
let hasNestedJsxOrMoreThanOneChild expr =
103+
let rec loop inRecursion expr =
104+
match expr.Parsetree.pexp_desc with
105+
| Pexp_construct
106+
({txt = Longident.Lident "::"}, Some {pexp_desc = Pexp_tuple [hd; tail]})
107+
->
108+
if inRecursion || ParsetreeViewer.isJsxExpression hd then true
109+
else loop true tail
110+
| _ -> false
111+
in
112+
loop false expr
113+
102114
let printMultilineCommentContent txt =
103115
(* Turns
104116
* |* first line
@@ -3694,6 +3706,12 @@ and printJsxExpression lident args cmtTbl =
36943706
true
36953707
| _ -> false
36963708
in
3709+
let lineSep =
3710+
match children with
3711+
| Some expr ->
3712+
if hasNestedJsxOrMoreThanOneChild expr then Doc.hardLine else Doc.line
3713+
| None -> Doc.line
3714+
in
36973715
Doc.group
36983716
(Doc.concat
36993717
[
@@ -3728,10 +3746,10 @@ and printJsxExpression lident args cmtTbl =
37283746
Doc.line;
37293747
(match children with
37303748
| Some childrenExpression ->
3731-
printJsxChildren childrenExpression cmtTbl
3749+
printJsxChildren childrenExpression ~sep:lineSep cmtTbl
37323750
| None -> Doc.nil);
37333751
]);
3734-
Doc.line;
3752+
lineSep;
37353753
Doc.text "</";
37363754
name;
37373755
Doc.greaterThan;
@@ -3741,24 +3759,28 @@ and printJsxExpression lident args cmtTbl =
37413759
and printJsxFragment expr cmtTbl =
37423760
let opening = Doc.text "<>" in
37433761
let closing = Doc.text "</>" in
3744-
(* let (children, _) = ParsetreeViewer.collectListExpressions expr in *)
3762+
let lineSep =
3763+
if hasNestedJsxOrMoreThanOneChild expr then Doc.hardLine else Doc.line
3764+
in
37453765
Doc.group
37463766
(Doc.concat
37473767
[
37483768
opening;
37493769
(match expr.pexp_desc with
37503770
| Pexp_construct ({txt = Longident.Lident "[]"}, None) -> Doc.nil
3751-
| _ -> Doc.indent (Doc.concat [Doc.line; printJsxChildren expr cmtTbl]));
3752-
Doc.line;
3771+
| _ ->
3772+
Doc.indent
3773+
(Doc.concat [Doc.line; printJsxChildren expr ~sep:lineSep cmtTbl]));
3774+
lineSep;
37533775
closing;
37543776
])
37553777

3756-
and printJsxChildren (childrenExpr : Parsetree.expression) cmtTbl =
3778+
and printJsxChildren (childrenExpr : Parsetree.expression) ~sep cmtTbl =
37573779
match childrenExpr.pexp_desc with
37583780
| Pexp_construct ({txt = Longident.Lident "::"}, _) ->
37593781
let children, _ = ParsetreeViewer.collectListExpressions childrenExpr in
37603782
Doc.group
3761-
(Doc.join ~sep:Doc.line
3783+
(Doc.join ~sep
37623784
(List.map
37633785
(fun (expr : Parsetree.expression) ->
37643786
let leadingLineCommentPresent =

tests/printer/expr/expected/callback.res.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,9 @@ let decoratorTags =
230230
items
231231
->Js.Array2.filter(items => {items.category === Decorators})
232232
->Belt.Array.map(item => {
233-
<span className="mr-2" key=item.name> <Tag text={item.name} /> </span>
233+
<span className="mr-2" key=item.name>
234+
<Tag text={item.name} />
235+
</span>
234236
})
235237

236238
// Comments should still be printed here (callback in last position)

tests/printer/expr/expected/exoticIdent.res.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,8 @@ lazy \"let"
6161
@let
6262
let x = 1
6363

64-
let x = <div \"aria-foo"=\"type"> \"module" \"let" </div>
64+
let x =
65+
<div \"aria-foo"=\"type">
66+
\"module"
67+
\"let"
68+
</div>

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

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,37 @@ let x = <Foo.Bar.Baz className="container" />
55
let x = <Foo.bar className="container" />
66
let x = <Foo.baz className="multiline" />
77

8+
// https://github.com/rescript-lang/syntax/issues/570
9+
let x =
10+
<A>
11+
<B>
12+
<C>
13+
<D />
14+
<E />
15+
</C>
16+
<F>
17+
<G />
18+
<H />
19+
</F>
20+
</B>
21+
</A>
22+
let x =
23+
<A>
24+
{children}
25+
<B />
26+
</A>
27+
let x =
28+
<A>
29+
<B />
30+
{children}
31+
</A>
32+
let x = <A> {a} </A>
33+
let x =
34+
<A>
35+
{a}
36+
{b}
37+
</A>
38+
839
let x = <div className="container" className2="container2" className3="container3" onClick />
940

1041
let nav =
@@ -58,7 +89,9 @@ let nav3 =
5889

5990
let avatarSection =
6091
<>
61-
<div style={{"zIndex": "1", "opacity": opacityUser}}> <Avatar user={user} size={45} /> </div>
92+
<div style={{"zIndex": "1", "opacity": opacityUser}}>
93+
<Avatar user={user} size={45} />
94+
</div>
6295
{user.email !== viewer.email
6396
? <div
6497
style={{
@@ -258,8 +291,14 @@ let x = <MyComponent sidebar={<div> test </div>} nav={<Navbar />} />
258291

259292
// https://github.com/rescript-lang/syntax/issues/113
260293
<div> {Js.log(a <= 10)} </div>
261-
<div> <div> {Js.log(a <= 10)} </div> </div>
262-
<div> <div onClick={_ => Js.log(a <= 10)}> <div> {Js.log(a <= 10)} </div> </div> </div>
294+
<div>
295+
<div> {Js.log(a <= 10)} </div>
296+
</div>
297+
<div>
298+
<div onClick={_ => Js.log(a <= 10)}>
299+
<div> {Js.log(a <= 10)} </div>
300+
</div>
301+
</div>
263302

264303
module App = {
265304
@react.component
@@ -271,7 +310,9 @@ module App = {
271310
module App = {
272311
@react.component
273312
let make = () => {
274-
<> <div /> </>
313+
<>
314+
<div />
315+
</>
275316
}
276317
}
277318

tests/printer/expr/jsx.res

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ let x = <Foo.baz
77
className="multiline"
88
/>
99

10+
// https://github.com/rescript-lang/syntax/issues/570
11+
let x = <A> <B> <C> <D /> <E /> </C> <F> <G /> <H /> </F> </B> </A>
12+
let x = <A> {children} <B/> </A>
13+
let x = <A> <B/> {children} </A>
14+
let x = <A> {a} </A>
15+
let x = <A> {a} {b} </A>
1016

1117
let x =
1218
<div

tests/printer/other/expected/fatSlider.res.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ let make = (~min=50, ~max=250, ~meterSuffix=?) => {
5050
/>
5151
</div>
5252
<div className="meter text-4xl font-bold text-gray-600 mt-4">
53-
{values[0]->Js.Int.toString->string} {meterSuffix->Belt.Option.getWithDefault(null)}
53+
{values[0]->Js.Int.toString->string}
54+
{meterSuffix->Belt.Option.getWithDefault(null)}
5455
</div>
5556
</div>
5657
}

tests/printer/pattern/expected/constant.res.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ switch science {
3333
| None => React.null
3434
| Some(1.0) => React.null
3535
| Some(uploadProgress) =>
36-
<div className=Styles.fill> <StudioUploadProgressIndicator progress=uploadProgress /> </div>
36+
<div className=Styles.fill>
37+
<StudioUploadProgressIndicator progress=uploadProgress />
38+
</div>
3739
}}
3840
</div>
3941

0 commit comments

Comments
 (0)