Skip to content

Commit 353ae29

Browse files
committed
Revert "fix: do not print > on new line (rescript-lang#678)"
This reverts commit b9f2f7b.
1 parent b9f2f7b commit 353ae29

File tree

6 files changed

+16
-133
lines changed

6 files changed

+16
-133
lines changed

src/res_printer.ml

Lines changed: 8 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -113,21 +113,6 @@ let hasNestedJsxOrMoreThanOneChild expr =
113113
in
114114
loop false expr
115115

116-
let hasTailSingleLineComment tbl loc =
117-
let rec getLastElement elements =
118-
match elements with
119-
| [] -> None
120-
| [element] -> Some element
121-
| _ :: rest -> getLastElement rest
122-
in
123-
match Hashtbl.find_opt tbl.CommentTable.trailing loc with
124-
| None -> false
125-
| Some comments -> (
126-
let lastComment = getLastElement comments in
127-
match lastComment with
128-
| None -> false
129-
| Some comment -> Comment.isSingleLineComment comment)
130-
131116
let hasCommentsInside tbl loc =
132117
match Hashtbl.find_opt tbl.CommentTable.inside loc with
133118
| None -> false
@@ -4056,20 +4041,8 @@ and printJsxExpression ~customLayout lident args cmtTbl =
40564041
Pexp_construct ({txt = Longident.Lident "[]"}, None);
40574042
}
40584043
when isSelfClosing ->
4059-
Doc.text "/>"
4060-
| _ ->
4061-
(* if last trailing comment of tag is single line comment then put > on the next line
4062-
<A
4063-
// single line comment
4064-
>
4065-
</A>
4066-
*)
4067-
if hasTailSingleLineComment cmtTbl lident.Asttypes.loc then
4068-
Doc.concat [Doc.softLine; Doc.greaterThan]
4069-
else
4070-
Doc.ifBreaks
4071-
(Doc.lineSuffix Doc.greaterThan)
4072-
Doc.greaterThan);
4044+
Doc.concat [Doc.line; Doc.text "/>"]
4045+
| _ -> Doc.concat [Doc.softLine; Doc.greaterThan]);
40734046
]);
40744047
(if isSelfClosing then Doc.nil
40754048
else
@@ -4167,27 +4140,6 @@ and printJsxChildren ~customLayout (childrenExpr : Parsetree.expression) ~sep
41674140

41684141
and printJsxProps ~customLayout args cmtTbl :
41694142
Doc.t * Parsetree.expression option =
4170-
(* This function was introduced because we have different formatting behavior for self-closing tags and other tags
4171-
we always put /> on a new line for self-closing tag when it breaks
4172-
<A
4173-
a=""
4174-
/>
4175-
4176-
<A
4177-
a="">
4178-
<B />
4179-
</A>
4180-
we should remove this function once the format is unified
4181-
*)
4182-
let isSelfClosing children =
4183-
match children with
4184-
| {
4185-
Parsetree.pexp_desc = Pexp_construct ({txt = Longident.Lident "[]"}, None);
4186-
pexp_loc = loc;
4187-
} ->
4188-
not (hasCommentsInside cmtTbl loc)
4189-
| _ -> false
4190-
in
41914143
let rec loop props args =
41924144
match args with
41934145
| [] -> (Doc.nil, None)
@@ -4199,42 +4151,13 @@ and printJsxProps ~customLayout args cmtTbl :
41994151
Pexp_construct ({txt = Longident.Lident "()"}, None);
42004152
} );
42014153
] ->
4202-
let doc = if isSelfClosing children then Doc.line else Doc.nil in
4203-
(doc, Some children)
4204-
| ((_, expr) as lastProp)
4205-
:: [
4206-
(Asttypes.Labelled "children", children);
4207-
( Asttypes.Nolabel,
4208-
{
4209-
Parsetree.pexp_desc =
4210-
Pexp_construct ({txt = Longident.Lident "()"}, None);
4211-
} );
4212-
] ->
4213-
let loc =
4214-
match expr.Parsetree.pexp_attributes with
4215-
| ({Location.txt = "ns.namedArgLoc"; loc}, _) :: _attrs ->
4216-
{loc with loc_end = expr.pexp_loc.loc_end}
4217-
| _ -> expr.pexp_loc
4218-
in
4219-
let tailSingleLineCommentPresent = hasTailSingleLineComment cmtTbl loc in
4220-
let propDoc = printJsxProp ~customLayout lastProp cmtTbl in
42214154
let formattedProps =
4222-
Doc.concat
4223-
[
4224-
Doc.indent
4225-
(Doc.concat
4226-
[
4227-
Doc.line;
4228-
Doc.group
4229-
(Doc.join ~sep:Doc.line (propDoc :: props |> List.rev));
4230-
]);
4231-
(* print > on new line if last comment is single line comment *)
4232-
(match (isSelfClosing children, tailSingleLineCommentPresent) with
4233-
(* we always put /> on a new line when a self-closing tag breaks *)
4234-
| true, _ -> Doc.line
4235-
| false, true -> Doc.softLine
4236-
| false, false -> Doc.nil);
4237-
]
4155+
Doc.indent
4156+
(match props with
4157+
| [] -> Doc.nil
4158+
| props ->
4159+
Doc.concat
4160+
[Doc.line; Doc.group (Doc.join ~sep:Doc.line (props |> List.rev))])
42384161
in
42394162
(formattedProps, Some children)
42404163
| arg :: args ->

tests/conversion/reason/expected/bracedJsx.res.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ let make = () => {
111111
<div
112112
className=Styles.terminal
113113
onClick={event => (event->ReactEvent.Mouse.target)["querySelector"]("input")["focus"]()}
114-
ref={containerRef->ReactDOMRe.Ref.domRef}>
114+
ref={containerRef->ReactDOMRe.Ref.domRef}
115+
>
115116
{state.history
116117
->Array.mapWithIndex((index, item) =>
117118
<div key={j`$index`} className=Styles.line>

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,6 @@ module Cite = {
3131
<B /* comment3 */ />
3232
</A>
3333

34-
<A
35-
value=""
36-
/* comment */>
37-
<B />
38-
</A>
39-
40-
<A
41-
// comment
42-
>
43-
<B />
44-
</A>
45-
46-
<A
47-
/* comment */>
48-
<B />
49-
</A>
50-
51-
<A /* comment */>
52-
<B />
53-
</A>
54-
5534
<div>
5635
// Must not jump inside braces
5736
{React.string("Hello, World!")}

tests/printer/comments/jsx.res

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,6 @@ value=""
3333
<B /* comment3 */ />
3434
</A>
3535

36-
<A
37-
value=""
38-
/* comment */
39-
>
40-
<B/>
41-
</A>
42-
43-
<A
44-
// comment
45-
>
46-
<B />
47-
</A>
48-
49-
<A
50-
/* comment */
51-
>
52-
<B />
53-
</A>
54-
55-
<A /* comment */>
56-
<B />
57-
</A>
58-
5936
<div>
6037
// Must not jump inside braces
6138
{React.string("Hello, World!")}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ let avatarSection =
104104
onMouseLeave={_ => setHoveringAdmin(false)}
105105
onClick={_e => {
106106
stopImpersonating(csrfToken)
107-
}}>
107+
}}
108+
>
108109
<Avatar user={viewer} size={45} />
109110
</div>
110111
: React.nullElement}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ let make = () => {
2828
<div className="pr-2 font-bold text-gray-400 text-lg"> {"Signature"->string} </div>
2929
<select
3030
id="country"
31-
className="transition duration-150 ease-in-out sm:text-sm sm:leading-5 border-none font-bold text-2xl text-gray-600 bg-transparent">
31+
className="transition duration-150 ease-in-out sm:text-sm sm:leading-5 border-none font-bold text-2xl text-gray-600 bg-transparent"
32+
>
3233
{options
3334
->Belt.List.map(option =>
3435
<option key={option->TimeSignature.toString}>
@@ -47,7 +48,8 @@ let make = () => {
4748
fill="none"
4849
strokeLinecap="round"
4950
strokeLinejoin="round"
50-
className="text-gray-400">
51+
className="text-gray-400"
52+
>
5153
<polyline points="6 9 12 15 18 9" />
5254
</svg>
5355
</label>

0 commit comments

Comments
 (0)