Skip to content

Commit ed4575b

Browse files
committed
Remove underscore if placeholder is in first pos
1 parent ac0fc88 commit ed4575b

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

compiler/syntax/src/res_printer.ml

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3985,7 +3985,49 @@ 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 = print_operand ~is_lhs:false ~is_multiline:false rhs 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
4030+
in
39894031
Doc.group
39904032
(Doc.concat
39914033
[

tests/syntax_tests/data/printer/expr/expected/underscoreApply.res.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ let nested2 = (x, y, z) => List.length(_)
1212

1313
let l = [1, 2, 3]->List.map(i => i + 1, _)->List.filter(i => i > 0, _)
1414

15-
let l = (i => i + 1)->List.map(_, [1, 2, 3])
15+
let l = (i => i + 1)->List.map([1, 2, 3])
1616

1717
let x = List.length(_)
1818

@@ -52,8 +52,8 @@ f(a, b, _)[ix] = 2
5252

5353
getDirector(a, b, _).name = "Steve"
5454

55-
filterNone->Array.get(_, 0)
56-
filterNone->Array.get(_, 0)
55+
filterNone->Array.get(0)
56+
filterNone->Array.get(0)
5757
Array.get(_, 0)
5858
1 + Array.get(_, 0)
5959
Array.get(_, 1) + Array.get(_, 0)

0 commit comments

Comments
 (0)