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

Test print #684

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 8 additions & 85 deletions src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,6 @@ let hasNestedJsxOrMoreThanOneChild expr =
in
loop false expr

let hasTailSingleLineComment tbl loc =
let rec getLastElement elements =
match elements with
| [] -> None
| [element] -> Some element
| _ :: rest -> getLastElement rest
in
match Hashtbl.find_opt tbl.CommentTable.trailing loc with
| None -> false
| Some comments -> (
let lastComment = getLastElement comments in
match lastComment with
| None -> false
| Some comment -> Comment.isSingleLineComment comment)

let hasCommentsInside tbl loc =
match Hashtbl.find_opt tbl.CommentTable.inside loc with
| None -> false
Expand Down Expand Up @@ -4056,20 +4041,8 @@ and printJsxExpression ~customLayout lident args cmtTbl =
Pexp_construct ({txt = Longident.Lident "[]"}, None);
}
when isSelfClosing ->
Doc.text "/>"
| _ ->
(* if last trailing comment of tag is single line comment then put > on the next line
<A
// single line comment
>
</A>
*)
if hasTailSingleLineComment cmtTbl lident.Asttypes.loc then
Doc.concat [Doc.softLine; Doc.greaterThan]
else
Doc.ifBreaks
(Doc.lineSuffix Doc.greaterThan)
Doc.greaterThan);
Doc.concat [Doc.line; Doc.text "/>"]
| _ -> Doc.concat [Doc.softLine; Doc.greaterThan]);
]);
(if isSelfClosing then Doc.nil
else
Expand Down Expand Up @@ -4167,27 +4140,6 @@ and printJsxChildren ~customLayout (childrenExpr : Parsetree.expression) ~sep

and printJsxProps ~customLayout args cmtTbl :
Doc.t * Parsetree.expression option =
(* This function was introduced because we have different formatting behavior for self-closing tags and other tags
we always put /> on a new line for self-closing tag when it breaks
<A
a=""
/>

<A
a="">
<B />
</A>
we should remove this function once the format is unified
*)
let isSelfClosing children =
match children with
| {
Parsetree.pexp_desc = Pexp_construct ({txt = Longident.Lident "[]"}, None);
pexp_loc = loc;
} ->
not (hasCommentsInside cmtTbl loc)
| _ -> false
in
let rec loop props args =
match args with
| [] -> (Doc.nil, None)
Expand All @@ -4199,42 +4151,13 @@ and printJsxProps ~customLayout args cmtTbl :
Pexp_construct ({txt = Longident.Lident "()"}, None);
} );
] ->
let doc = if isSelfClosing children then Doc.line else Doc.nil in
(doc, Some children)
| ((_, expr) as lastProp)
:: [
(Asttypes.Labelled "children", children);
( Asttypes.Nolabel,
{
Parsetree.pexp_desc =
Pexp_construct ({txt = Longident.Lident "()"}, None);
} );
] ->
let loc =
match expr.Parsetree.pexp_attributes with
| ({Location.txt = "ns.namedArgLoc"; loc}, _) :: _attrs ->
{loc with loc_end = expr.pexp_loc.loc_end}
| _ -> expr.pexp_loc
in
let tailSingleLineCommentPresent = hasTailSingleLineComment cmtTbl loc in
let propDoc = printJsxProp ~customLayout lastProp cmtTbl in
let formattedProps =
Doc.concat
[
Doc.indent
(Doc.concat
[
Doc.line;
Doc.group
(Doc.join ~sep:Doc.line (propDoc :: props |> List.rev));
]);
(* print > on new line if last comment is single line comment *)
(match (isSelfClosing children, tailSingleLineCommentPresent) with
(* we always put /> on a new line when a self-closing tag breaks *)
| true, _ -> Doc.line
| false, true -> Doc.softLine
| false, false -> Doc.nil);
]
Doc.indent
(match props with
| [] -> Doc.nil
| props ->
Doc.concat
[Doc.line; Doc.group (Doc.join ~sep:Doc.line (props |> List.rev))])
in
(formattedProps, Some children)
| arg :: args ->
Expand Down
3 changes: 2 additions & 1 deletion tests/conversion/reason/expected/bracedJsx.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ let make = () => {
<div
className=Styles.terminal
onClick={event => (event->ReactEvent.Mouse.target)["querySelector"]("input")["focus"]()}
ref={containerRef->ReactDOMRe.Ref.domRef}>
ref={containerRef->ReactDOMRe.Ref.domRef}
>
{state.history
->Array.mapWithIndex((index, item) =>
<div key={j`$index`} className=Styles.line>
Expand Down
27 changes: 6 additions & 21 deletions tests/printer/comments/expected/jsx.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,6 @@ module Cite = {
<B /* comment3 */ />
</A>

<A
value=""
/* comment */>
<B />
</A>

<A
// comment
>
<B />
</A>

<A
/* comment */>
<B />
</A>

<A /* comment */>
<B />
</A>

<div>
// Must not jump inside braces
{React.string("Hello, World!")}
Expand All @@ -70,3 +49,9 @@ let x =
// before b
{b} // after b
</>

<ChatComponents.Sidebar
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the example code

variant={own ? #Own : #Other} restricted spaceBetweenButtons={message.location->Option.isSome}
>
{children}
</ChatComponents.Sidebar>
30 changes: 7 additions & 23 deletions tests/printer/comments/jsx.res
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,6 @@ value=""
<B /* comment3 */ />
</A>

<A
value=""
/* comment */
>
<B/>
</A>

<A
// comment
>
<B />
</A>

<A
/* comment */
>
<B />
</A>

<A /* comment */>
<B />
</A>

<div>
// Must not jump inside braces
{React.string("Hello, World!")}
Expand All @@ -73,3 +50,10 @@ let x = <>
// before b
{b} // after b
</>

<ChatComponents.Sidebar
variant={own ? #Own : #Other}
restricted
spaceBetweenButtons={message.location->Option.isSome}>
{children}
</ChatComponents.Sidebar>
3 changes: 2 additions & 1 deletion tests/printer/expr/expected/jsx.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ let avatarSection =
onMouseLeave={_ => setHoveringAdmin(false)}
onClick={_e => {
stopImpersonating(csrfToken)
}}>
}}
>
<Avatar user={viewer} size={45} />
</div>
: React.nullElement}
Expand Down
6 changes: 4 additions & 2 deletions tests/printer/other/expected/signaturePicker.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ let make = () => {
<div className="pr-2 font-bold text-gray-400 text-lg"> {"Signature"->string} </div>
<select
id="country"
className="transition duration-150 ease-in-out sm:text-sm sm:leading-5 border-none font-bold text-2xl text-gray-600 bg-transparent">
className="transition duration-150 ease-in-out sm:text-sm sm:leading-5 border-none font-bold text-2xl text-gray-600 bg-transparent"
>
{options
->Belt.List.map(option =>
<option key={option->TimeSignature.toString}>
Expand All @@ -47,7 +48,8 @@ let make = () => {
fill="none"
strokeLinecap="round"
strokeLinejoin="round"
className="text-gray-400">
className="text-gray-400"
>
<polyline points="6 9 12 15 18 9" />
</svg>
</label>
Expand Down