From e2752f136f13888d8759a05b0ccadf65717073e7 Mon Sep 17 00:00:00 2001 From: Zheyu Zhang Date: Fri, 14 Oct 2022 16:01:18 +0800 Subject: [PATCH 1/6] dont print > on new line --- src/res_printer.ml | 41 ++++++++++++++++++- .../reason/expected/bracedJsx.res.txt | 3 +- tests/printer/expr/expected/jsx.res.txt | 3 +- .../other/expected/signaturePicker.res.txt | 6 +-- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/res_printer.ml b/src/res_printer.ml index 60066d09..57af1aa8 100644 --- a/src/res_printer.ml +++ b/src/res_printer.ml @@ -3978,6 +3978,38 @@ and printPexpApply ~customLayout expr cmtTbl = and printJsxExpression ~customLayout lident args cmtTbl = let name = printJsxName lident in + let hasTailSingleLineComment = + let props = + args + |> List.filter (fun (label, _) -> + match label with + | Asttypes.Labelled "children" -> false + | Asttypes.Nolabel -> false + | _ -> true) + in + let getLast elements = + match List.rev elements with + | [] -> None + | last :: _ -> Some last + in + let tailComment = + match getLast props with + | None -> None + | Some (_, expr) -> ( + 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 + match Hashtbl.find_opt cmtTbl.CommentTable.trailing loc with + | None -> None + | Some comments -> getLast comments) + in + match tailComment with + | None -> false + | Some comment -> Comment.isSingleLineComment comment + in let formattedProps, children = printJsxProps ~customLayout args cmtTbl in (*
*) let hasChildren = @@ -4042,7 +4074,14 @@ and printJsxExpression ~customLayout lident args cmtTbl = } when isSelfClosing -> Doc.concat [Doc.line; Doc.text "/>"] - | _ -> Doc.concat [Doc.softLine; Doc.greaterThan]); + | _ -> + (* print_endline (string_of_bool hasTailSingleLineComment); *) + Doc.concat + [ + (if hasTailSingleLineComment then Doc.softLine + else Doc.nil); + Doc.greaterThan; + ]); ]); (if isSelfClosing then Doc.nil else diff --git a/tests/conversion/reason/expected/bracedJsx.res.txt b/tests/conversion/reason/expected/bracedJsx.res.txt index 17d75e3d..b49e82fe 100644 --- a/tests/conversion/reason/expected/bracedJsx.res.txt +++ b/tests/conversion/reason/expected/bracedJsx.res.txt @@ -111,8 +111,7 @@ let make = () => {
(event->ReactEvent.Mouse.target)["querySelector"]("input")["focus"]()} - ref={containerRef->ReactDOMRe.Ref.domRef} - > + ref={containerRef->ReactDOMRe.Ref.domRef}> {state.history ->Array.mapWithIndex((index, item) =>
diff --git a/tests/printer/expr/expected/jsx.res.txt b/tests/printer/expr/expected/jsx.res.txt index 326cbe3c..bdc104ff 100644 --- a/tests/printer/expr/expected/jsx.res.txt +++ b/tests/printer/expr/expected/jsx.res.txt @@ -104,8 +104,7 @@ let avatarSection = onMouseLeave={_ => setHoveringAdmin(false)} onClick={_e => { stopImpersonating(csrfToken) - }} - > + }}>
: React.nullElement} diff --git a/tests/printer/other/expected/signaturePicker.res.txt b/tests/printer/other/expected/signaturePicker.res.txt index fe32b18e..f73d98b8 100644 --- a/tests/printer/other/expected/signaturePicker.res.txt +++ b/tests/printer/other/expected/signaturePicker.res.txt @@ -28,8 +28,7 @@ let make = () => {
{"Signature"->string}