From ffc7a3571c7b7443aa8c81f88f73c6bc8d923a2c Mon Sep 17 00:00:00 2001 From: Zheyu Zhang Date: Thu, 29 Sep 2022 18:37:16 +0800 Subject: [PATCH 01/10] avoid comments been eaten --- src/res_comments_table.ml | 41 +++++++++++++++++-- src/res_printer.ml | 13 ++---- .../reason/expected/bracedJsx.res.txt | 3 +- tests/printer/comments/expected/jsx.res.txt | 2 +- tests/printer/expr/expected/jsx.res.txt | 3 +- .../other/expected/signaturePicker.res.txt | 6 ++- 6 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/res_comments_table.ml b/src/res_comments_table.ml index 1e310fb7..01d95f1f 100644 --- a/src/res_comments_table.ml +++ b/src/res_comments_table.ml @@ -1,5 +1,6 @@ module Comment = Res_comment module Doc = Res_doc +module ParsetreeViewer = Res_parsetree_viewer type t = { leading: (Location.t, Comment.t list) Hashtbl.t; @@ -113,6 +114,19 @@ let partitionLeadingTrailing comments loc = in loop ([], []) comments +let partitionLeading comments loc = + let rec loop leading comments = + let open Location in + match comments with + | comment :: rest -> + let cmtLoc = Comment.loc comment in + if cmtLoc.loc_end.pos_cnum <= loc.loc_start.pos_cnum then + loop (comment :: leading) rest + else loop leading rest + | [] -> List.rev leading + in + loop [] comments + let partitionByOnSameLine loc comments = let rec loop (onSameLine, onOtherLine) comments = let open Location in @@ -1310,9 +1324,30 @@ and walkExpression expr t comments = walkExpression callExpr t inside; after) in - let afterExpr, rest = partitionAdjacentTrailing callExpr.pexp_loc after in - attach t.trailing callExpr.pexp_loc afterExpr; - walkList (arguments |> List.map (fun (_, e) -> ExprArgument e)) t rest + if ParsetreeViewer.isJsxExpression expr then ( + let props = + arguments + |> List.filter (fun (label, _) -> + match label with + | Asttypes.Labelled "children" -> false + | Asttypes.Nolabel -> false + | _ -> true) + in + let _, children = + arguments + |> List.find (fun (label, _) -> label = Asttypes.Labelled "children") + in + let leading, inside, _ = partitionByLoc after children.pexp_loc in + if props = [] then ( + let afterExpr, _ = partitionAdjacentTrailing callExpr.pexp_loc after in + attach t.trailing callExpr.pexp_loc afterExpr; + walkExpression children t inside) + else walkList (props |> List.map (fun (_, e) -> ExprArgument e)) t leading; + walkExpression children t inside) + else + let afterExpr, rest = partitionAdjacentTrailing callExpr.pexp_loc after in + attach t.trailing callExpr.pexp_loc afterExpr; + walkList (arguments |> List.map (fun (_, e) -> ExprArgument e)) t rest | Pexp_fun (_, _, _, _) | Pexp_newtype _ -> ( let _, parameters, returnExpr = funExpr expr in let comments = diff --git a/src/res_printer.ml b/src/res_printer.ml index ebd5bc76..60066d09 100644 --- a/src/res_printer.ml +++ b/src/res_printer.ml @@ -4039,20 +4039,15 @@ and printJsxExpression ~customLayout lident args cmtTbl = { Parsetree.pexp_desc = Pexp_construct ({txt = Longident.Lident "[]"}, None); - pexp_loc = loc; - } -> - if isSelfClosing then - Doc.concat - [Doc.line; printComments (Doc.text "/>") cmtTbl loc] - else - Doc.concat [Doc.softLine; printComments Doc.nil cmtTbl loc] - | _ -> Doc.nil); + } + when isSelfClosing -> + Doc.concat [Doc.line; Doc.text "/>"] + | _ -> Doc.concat [Doc.softLine; Doc.greaterThan]); ]); (if isSelfClosing then Doc.nil else Doc.concat [ - Doc.greaterThan; (if hasChildren then printChildren children else match children with diff --git a/tests/conversion/reason/expected/bracedJsx.res.txt b/tests/conversion/reason/expected/bracedJsx.res.txt index b49e82fe..17d75e3d 100644 --- a/tests/conversion/reason/expected/bracedJsx.res.txt +++ b/tests/conversion/reason/expected/bracedJsx.res.txt @@ -111,7 +111,8 @@ 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/comments/expected/jsx.res.txt b/tests/printer/comments/expected/jsx.res.txt index bfca6703..0cb35bff 100644 --- a/tests/printer/comments/expected/jsx.res.txt +++ b/tests/printer/comments/expected/jsx.res.txt @@ -9,7 +9,7 @@ module Cite = { diff --git a/tests/printer/expr/expected/jsx.res.txt b/tests/printer/expr/expected/jsx.res.txt index bdc104ff..326cbe3c 100644 --- a/tests/printer/expr/expected/jsx.res.txt +++ b/tests/printer/expr/expected/jsx.res.txt @@ -104,7 +104,8 @@ 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 f73d98b8..fe32b18e 100644 --- a/tests/printer/other/expected/signaturePicker.res.txt +++ b/tests/printer/other/expected/signaturePicker.res.txt @@ -28,7 +28,8 @@ let make = () => {
{"Signature"->string}