diff --git a/src/res_printer.ml b/src/res_printer.ml index a2780c79..2cb94696 100644 --- a/src/res_printer.ml +++ b/src/res_printer.ml @@ -99,6 +99,18 @@ let hasCommentBelow tbl loc = | [] -> false | exception Not_found -> false +let hasNestedJsxOrMoreThanOneChild expr = + let rec loop inRecursion expr = + match expr.Parsetree.pexp_desc with + | Pexp_construct + ({txt = Longident.Lident "::"}, Some {pexp_desc = Pexp_tuple [hd; tail]}) + -> + if inRecursion || ParsetreeViewer.isJsxExpression hd then true + else loop true tail + | _ -> false + in + loop false expr + let printMultilineCommentContent txt = (* Turns * |* first line @@ -3694,6 +3706,12 @@ and printJsxExpression lident args cmtTbl = true | _ -> false in + let lineSep = + match children with + | Some expr -> + if hasNestedJsxOrMoreThanOneChild expr then Doc.hardLine else Doc.line + | None -> Doc.line + in Doc.group (Doc.concat [ @@ -3728,10 +3746,10 @@ and printJsxExpression lident args cmtTbl = Doc.line; (match children with | Some childrenExpression -> - printJsxChildren childrenExpression cmtTbl + printJsxChildren childrenExpression ~sep:lineSep cmtTbl | None -> Doc.nil); ]); - Doc.line; + lineSep; Doc.text "" in let closing = Doc.text "" in - (* let (children, _) = ParsetreeViewer.collectListExpressions expr in *) + let lineSep = + if hasNestedJsxOrMoreThanOneChild expr then Doc.hardLine else Doc.line + in Doc.group (Doc.concat [ opening; (match expr.pexp_desc with | Pexp_construct ({txt = Longident.Lident "[]"}, None) -> Doc.nil - | _ -> Doc.indent (Doc.concat [Doc.line; printJsxChildren expr cmtTbl])); - Doc.line; + | _ -> + Doc.indent + (Doc.concat [Doc.line; printJsxChildren expr ~sep:lineSep cmtTbl])); + lineSep; closing; ]) -and printJsxChildren (childrenExpr : Parsetree.expression) cmtTbl = +and printJsxChildren (childrenExpr : Parsetree.expression) ~sep cmtTbl = match childrenExpr.pexp_desc with | Pexp_construct ({txt = Longident.Lident "::"}, _) -> let children, _ = ParsetreeViewer.collectListExpressions childrenExpr in Doc.group - (Doc.join ~sep:Doc.line + (Doc.join ~sep (List.map (fun (expr : Parsetree.expression) -> let leadingLineCommentPresent = diff --git a/tests/printer/expr/expected/callback.res.txt b/tests/printer/expr/expected/callback.res.txt index 02121c2f..50393c39 100644 --- a/tests/printer/expr/expected/callback.res.txt +++ b/tests/printer/expr/expected/callback.res.txt @@ -230,7 +230,9 @@ let decoratorTags = items ->Js.Array2.filter(items => {items.category === Decorators}) ->Belt.Array.map(item => { - + + + }) // Comments should still be printed here (callback in last position) diff --git a/tests/printer/expr/expected/exoticIdent.res.txt b/tests/printer/expr/expected/exoticIdent.res.txt index 6b4da918..4453cea5 100644 --- a/tests/printer/expr/expected/exoticIdent.res.txt +++ b/tests/printer/expr/expected/exoticIdent.res.txt @@ -61,4 +61,8 @@ lazy \"let" @let let x = 1 -let x =
\"module" \"let"
+let x = +
+ \"module" + \"let" +
diff --git a/tests/printer/expr/expected/jsx.res.txt b/tests/printer/expr/expected/jsx.res.txt index cc54a06a..e6b0c00d 100644 --- a/tests/printer/expr/expected/jsx.res.txt +++ b/tests/printer/expr/expected/jsx.res.txt @@ -5,6 +5,37 @@ let x = let x = let x = +// https://github.com/rescript-lang/syntax/issues/570 +let x = + + + + + + + + + + + + +let x = + + {children} + + +let x = + + + {children} + +let x = {a} +let x = + + {a} + {b} + + let x =
let nav = @@ -58,7 +89,9 @@ let nav3 = let avatarSection = <> -
+
+ +
{user.email !== viewer.email ?
test
} nav={} /> // https://github.com/rescript-lang/syntax/issues/113
{Js.log(a <= 10)}
-
{Js.log(a <= 10)}
-
Js.log(a <= 10)}>
{Js.log(a <= 10)}
+
+
{Js.log(a <= 10)}
+
+
+
Js.log(a <= 10)}> +
{Js.log(a <= 10)}
+
+
module App = { @react.component @@ -271,7 +310,9 @@ module App = { module App = { @react.component let make = () => { - <>
+ <> +
+ } } diff --git a/tests/printer/expr/jsx.res b/tests/printer/expr/jsx.res index 08f2af76..d3b86ab3 100644 --- a/tests/printer/expr/jsx.res +++ b/tests/printer/expr/jsx.res @@ -7,6 +7,12 @@ let x = +// https://github.com/rescript-lang/syntax/issues/570 +let x = +let x = {children} +let x = {children} +let x = {a} +let x = {a} {b} let x =
{ />
- {values[0]->Js.Int.toString->string} {meterSuffix->Belt.Option.getWithDefault(null)} + {values[0]->Js.Int.toString->string} + {meterSuffix->Belt.Option.getWithDefault(null)}
} diff --git a/tests/printer/pattern/expected/constant.res.txt b/tests/printer/pattern/expected/constant.res.txt index ed0700a8..895b2098 100644 --- a/tests/printer/pattern/expected/constant.res.txt +++ b/tests/printer/pattern/expected/constant.res.txt @@ -33,7 +33,9 @@ switch science { | None => React.null | Some(1.0) => React.null | Some(uploadProgress) => -
+
+ +
}}