diff --git a/compiler/syntax/src/res_printer.ml b/compiler/syntax/src/res_printer.ml index 7132e16dd9..67c32f8811 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -3985,7 +3985,49 @@ and print_binary_expression ~state (expr : Parsetree.expression) cmt_tbl = -> let lhs_has_comment_below = has_comment_below cmt_tbl lhs.pexp_loc in let lhs_doc = print_operand ~is_lhs:true ~is_multiline:false lhs op in - let rhs_doc = print_operand ~is_lhs:false ~is_multiline:false rhs op in + let rhs_doc = + (* For pipe operator, if RHS is (__x) => f(__x, ...), remove the first __x arg + so it prints as a->f(...) instead of a->f(_, ...) *) + let rhs_to_print = + match rhs.pexp_desc with + | Pexp_fun + { + arg_label = Nolabel; + default = None; + lhs = {ppat_desc = Ppat_var {txt = "__x"}} as pat; + rhs = + {pexp_desc = Pexp_apply {funct; args; partial; transformed_jsx}} + as body; + arity; + async; + } -> ( + match args with + | (Nolabel, {pexp_desc = Pexp_ident {txt = Longident.Lident "__x"}}) + :: rest_args -> + { + rhs with + pexp_desc = + Pexp_fun + { + arg_label = Nolabel; + default = None; + lhs = pat; + rhs = + { + body with + pexp_desc = + Pexp_apply + {funct; args = rest_args; partial; transformed_jsx}; + }; + arity; + async; + }; + } + | _ -> rhs) + | _ -> rhs + in + print_operand ~is_lhs:false ~is_multiline:false rhs_to_print op + in Doc.group (Doc.concat [ diff --git a/tests/syntax_tests/data/printer/expr/expected/underscoreApply.res.txt b/tests/syntax_tests/data/printer/expr/expected/underscoreApply.res.txt index 022a0f759a..488b19aa9e 100644 --- a/tests/syntax_tests/data/printer/expr/expected/underscoreApply.res.txt +++ b/tests/syntax_tests/data/printer/expr/expected/underscoreApply.res.txt @@ -12,7 +12,7 @@ let nested2 = (x, y, z) => List.length(_) let l = [1, 2, 3]->List.map(i => i + 1, _)->List.filter(i => i > 0, _) -let l = (i => i + 1)->List.map(_, [1, 2, 3]) +let l = (i => i + 1)->List.map([1, 2, 3]) let x = List.length(_) @@ -52,8 +52,8 @@ f(a, b, _)[ix] = 2 getDirector(a, b, _).name = "Steve" -filterNone->Array.get(_, 0) -filterNone->Array.get(_, 0) +filterNone->Array.get(0) +filterNone->Array.get(0) Array.get(_, 0) 1 + Array.get(_, 0) Array.get(_, 1) + Array.get(_, 0)