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

Commit 88e5962

Browse files
author
Iwan
committed
Fix printing of Ptyp_arrow on type constraint arrow expressions
1 parent 667a787 commit 88e5962

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

src/res_parens.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,7 @@ type kind = Parenthesized | Braced of Location.t | Nothing
402402
let includeModExpr modExpr = match modExpr.Parsetree.pmod_desc with
403403
| Parsetree.Pmod_constraint _ -> true
404404
| _ -> false
405+
406+
let arrowReturnTypExpr typExpr = match typExpr.Parsetree.ptyp_desc with
407+
| Parsetree.Ptyp_arrow _ -> true
408+
| _ -> false

src/res_parens.mli

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ val modExprFunctorConstraint: Parsetree.module_type -> bool
2929
val bracedExpr: Parsetree.expression -> bool
3030
val callExpr: Parsetree.expression -> kind
3131

32-
val includeModExpr : Parsetree.module_expr -> bool
32+
val includeModExpr : Parsetree.module_expr -> bool
33+
34+
val arrowReturnTypExpr: Parsetree.core_type -> bool

src/res_printer.ml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2967,7 +2967,6 @@ and printExpression (e : Parsetree.expression) cmtTbl =
29672967
| Pexp_let _ ->
29682968
printExpressionBlock ~braces:true e cmtTbl
29692969
| Pexp_fun (Nolabel, None, {ppat_desc = Ppat_var {txt="__x"}}, ({pexp_desc = Pexp_apply _})) ->
2970-
29712970
(* (__x) => f(a, __x, c) -----> f(a, _, c) *)
29722971
printExpressionWithComments (ParsetreeViewer.rewriteUnderscoreApply e) cmtTbl
29732972
| Pexp_fun _ | Pexp_newtype _ ->
@@ -3038,7 +3037,15 @@ and printExpression (e : Parsetree.expression) cmtTbl =
30383037
)
30393038
in
30403039
let typConstraintDoc = match typConstraint with
3041-
| Some(typ) -> Doc.concat [Doc.text ": "; printTypExpr typ cmtTbl]
3040+
| Some(typ) ->
3041+
let typDoc =
3042+
let doc = printTypExpr typ cmtTbl in
3043+
if Parens.arrowReturnTypExpr typ then
3044+
addParens doc
3045+
else
3046+
doc
3047+
in
3048+
Doc.concat [Doc.text ": "; typDoc]
30423049
| _ -> Doc.nil
30433050
in
30443051
let attrs = printAttributes attrs cmtTbl in

tests/printer/expr/__snapshots__/render.spec.js.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2140,6 +2140,18 @@ let f = (~\\\\\\"aria-b\\": string, ~\\\\\\"aria-d\\" as \\\\\\"aria-xyz\\") =>
21402140
}
21412141
21422142
let f = (type \\\\\\"😎\\", x) => Js.log(x)
2143+
2144+
let query = (~url, ()): (unit => unit) => {
2145+
() => Js.log(\\"Queried \\" ++ url)
2146+
}
2147+
2148+
let query = (~url, ()): ((unit, unit) => unit) => {
2149+
((), ()) => Js.log(\\"Queried \\" ++ url)
2150+
}
2151+
2152+
let query = (~url, ()): ((unit, unit, unit) => unit) => {
2153+
((), (), ()) => Js.log(\\"Queried \\" ++ url)
2154+
}
21432155
"
21442156
`;
21452157

tests/printer/expr/fun.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,15 @@ let f = (~\"aria-b": string, ~\"aria-d" as \"aria-xyz" ) => {
281281
}
282282

283283
let f = (type \"😎", x) => Js.log(x)
284+
285+
let query = (~url, ()): (unit => unit) => {
286+
() => Js.log("Queried " ++ url)
287+
}
288+
289+
let query = (~url, ()): (unit => unit => unit) => {
290+
() => () => Js.log("Queried " ++ url)
291+
}
292+
293+
let query = (~url, ()): (unit => unit => unit => unit) => {
294+
() => () => () => Js.log("Queried " ++ url)
295+
}

0 commit comments

Comments
 (0)