diff --git a/CHANGELOG.md b/CHANGELOG.md index f1d9706af2..f4269b52cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ # 11.0.0-alpha.4 (Unreleased) +#### :bug: Bug Fix + +- Fix broken formatting in uncurried mode for functions with _ placeholder args. https://github.com/rescript-lang/rescript-compiler/pull/6148 + # 11.0.0-alpha.3 #### :rocket: Main New Feature diff --git a/res_syntax/src/res_parsetree_viewer.ml b/res_syntax/src/res_parsetree_viewer.ml index 1d2b438041..c02026421b 100644 --- a/res_syntax/src/res_parsetree_viewer.ml +++ b/res_syntax/src/res_parsetree_viewer.ml @@ -154,13 +154,27 @@ let funExpr expr = let rec collect ~uncurried ~nFun attrsBefore acc expr = match expr with | { - pexp_desc = - Pexp_fun - ( Nolabel, - None, - {ppat_desc = Ppat_var {txt = "__x"}}, - {pexp_desc = Pexp_apply _} ); - } -> + pexp_desc = + Pexp_fun + ( Nolabel, + None, + {ppat_desc = Ppat_var {txt = "__x"}}, + {pexp_desc = Pexp_apply _} ); + } + | { + pexp_desc = + Pexp_construct + ( {txt = Lident "Function$"}, + Some + { + pexp_desc = + Pexp_fun + ( Nolabel, + None, + {ppat_desc = Ppat_var {txt = "__x"}}, + {pexp_desc = Pexp_apply _} ); + } ); + } -> (uncurried, attrsBefore, List.rev acc, rewriteUnderscoreApply expr) | {pexp_desc = Pexp_newtype (stringLoc, rest); pexp_attributes = attrs} -> let stringLocs, returnExpr = collectNewTypes [stringLoc] rest in diff --git a/res_syntax/src/res_printer.ml b/res_syntax/src/res_printer.ml index 4d413cbf6f..2f6ef766d3 100644 --- a/res_syntax/src/res_printer.ml +++ b/res_syntax/src/res_printer.ml @@ -2671,18 +2671,32 @@ and printExpression ~state (e : Parsetree.expression) cmtTbl = (Doc.concat [attrs; parametersDoc; typConstraintDoc; Doc.text " =>"; returnExprDoc]) in + let uncurried = Ast_uncurried.exprIsUncurriedFun e in + let e_fun = + if uncurried then Ast_uncurried.exprExtractUncurriedFun e else e + in let printedExpression = - match e.pexp_desc with + match e_fun.pexp_desc with | Pexp_fun ( Nolabel, None, {ppat_desc = Ppat_var {txt = "__x"}}, - {pexp_desc = Pexp_apply _} ) -> + {pexp_desc = Pexp_apply _} ) + | Pexp_construct + ( {txt = Lident "Function$"}, + Some + { + pexp_desc = + Pexp_fun + ( Nolabel, + None, + {ppat_desc = Ppat_var {txt = "__x"}}, + {pexp_desc = Pexp_apply _} ); + } ) -> (* (__x) => f(a, __x, c) -----> f(a, _, c) *) printExpressionWithComments ~state - (ParsetreeViewer.rewriteUnderscoreApply e) + (ParsetreeViewer.rewriteUnderscoreApply e_fun) cmtTbl - | _ when Ast_uncurried.exprIsUncurriedFun e -> printArrow e | Pexp_fun _ | Pexp_newtype _ -> printArrow e | Parsetree.Pexp_constant c -> printConstant ~templateLiteral:(ParsetreeViewer.isTemplateLiteral e) c