Skip to content

Commit e8d70af

Browse files
committed
Only apply in pipe context
1 parent b00af70 commit e8d70af

File tree

3 files changed

+51
-42
lines changed

3 files changed

+51
-42
lines changed

compiler/syntax/src/res_parsetree_viewer.ml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,50 @@ let rewrite_underscore_apply expr =
141141
}
142142
| _ -> expr
143143

144+
(* For pipe RHS: (__x) => f(__x, a, b) -----> f(a, b)
145+
Omits the first __x argument since the piped value fills that position *)
146+
let rewrite_underscore_apply_in_pipe expr =
147+
match expr.pexp_desc with
148+
| Pexp_fun
149+
{
150+
arg_label = Nolabel;
151+
default = None;
152+
lhs = {ppat_desc = Ppat_var {txt = "__x"}};
153+
rhs = {pexp_desc = Pexp_apply {funct = call_expr; args}} as e;
154+
} -> (
155+
match args with
156+
| (Nolabel, {pexp_desc = Pexp_ident {txt = Longident.Lident "__x"}})
157+
:: rest_args ->
158+
(* First arg is __x, skip it and convert remaining __x to _ *)
159+
let new_args =
160+
List.map
161+
(fun arg ->
162+
match arg with
163+
| ( lbl,
164+
({pexp_desc = Pexp_ident ({txt = Longident.Lident "__x"} as lid)}
165+
as arg_expr) ) ->
166+
( lbl,
167+
{
168+
arg_expr with
169+
pexp_desc = Pexp_ident {lid with txt = Longident.Lident "_"};
170+
} )
171+
| arg -> arg)
172+
rest_args
173+
in
174+
{
175+
e with
176+
pexp_desc =
177+
Pexp_apply
178+
{
179+
funct = call_expr;
180+
args = new_args;
181+
partial = false;
182+
transformed_jsx = false;
183+
};
184+
}
185+
| _ -> rewrite_underscore_apply expr)
186+
| _ -> expr
187+
144188
type fun_param_kind =
145189
| Parameter of {
146190
attrs: Parsetree.attributes;

compiler/syntax/src/res_parsetree_viewer.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ val is_single_pipe_expr : Parsetree.expression -> bool
146146
(* (__x) => f(a, __x, c) -----> f(a, _, c) *)
147147
val rewrite_underscore_apply : Parsetree.expression -> Parsetree.expression
148148

149+
val rewrite_underscore_apply_in_pipe : Parsetree.expression -> Parsetree.expression
150+
149151
(* (__x) => f(a, __x, c) -----> f(a, _, c) *)
150152
val is_underscore_apply_sugar : Parsetree.expression -> bool
151153

compiler/syntax/src/res_printer.ml

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3985,49 +3985,12 @@ and print_binary_expression ~state (expr : Parsetree.expression) cmt_tbl =
39853985
->
39863986
let lhs_has_comment_below = has_comment_below cmt_tbl lhs.pexp_loc in
39873987
let lhs_doc = print_operand ~is_lhs:true ~is_multiline:false lhs op in
3988-
let rhs_doc =
3989-
(* For pipe operator, if RHS is (__x) => f(__x, ...), remove the first __x arg
3990-
so it prints as a->f(...) instead of a->f(_, ...) *)
3991-
let rhs_to_print =
3992-
match rhs.pexp_desc with
3993-
| Pexp_fun
3994-
{
3995-
arg_label = Nolabel;
3996-
default = None;
3997-
lhs = {ppat_desc = Ppat_var {txt = "__x"}} as pat;
3998-
rhs =
3999-
{pexp_desc = Pexp_apply {funct; args; partial; transformed_jsx}}
4000-
as body;
4001-
arity;
4002-
async;
4003-
} -> (
4004-
match args with
4005-
| (Nolabel, {pexp_desc = Pexp_ident {txt = Longident.Lident "__x"}})
4006-
:: rest_args ->
4007-
{
4008-
rhs with
4009-
pexp_desc =
4010-
Pexp_fun
4011-
{
4012-
arg_label = Nolabel;
4013-
default = None;
4014-
lhs = pat;
4015-
rhs =
4016-
{
4017-
body with
4018-
pexp_desc =
4019-
Pexp_apply
4020-
{funct; args = rest_args; partial; transformed_jsx};
4021-
};
4022-
arity;
4023-
async;
4024-
};
4025-
}
4026-
| _ -> rhs)
4027-
| _ -> rhs
4028-
in
4029-
print_operand ~is_lhs:false ~is_multiline:false rhs_to_print op
3988+
(* For pipe RHS, use pipe-specific rewrite to omit redundant first underscore *)
3989+
let rhs =
3990+
if op = "->" then ParsetreeViewer.rewrite_underscore_apply_in_pipe rhs
3991+
else rhs
40303992
in
3993+
let rhs_doc = print_operand ~is_lhs:false ~is_multiline:false rhs op in
40313994
Doc.group
40323995
(Doc.concat
40333996
[

0 commit comments

Comments
 (0)