Skip to content

Commit 6b05527

Browse files
committed
Print parentheses around arrow function in pipe operator
Related to #1124.
1 parent f01ac40 commit 6b05527

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/PhpParser/PrettyPrinter/Standard.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,10 @@ protected function pExpr_BinaryOp_Coalesce(BinaryOp\Coalesce $node, int $precede
426426
}
427427

428428
protected function pExpr_BinaryOp_Pipe(BinaryOp\Pipe $node, int $precedence, int $lhsPrecedence): string {
429+
if ($node->right instanceof Expr\ArrowFunction) {
430+
// Force parentheses around arrow functions.
431+
$lhsPrecedence = $this->precedenceMap[Expr\ArrowFunction::class][0];
432+
}
429433
return $this->pInfixOp(BinaryOp\Pipe::class, $node->left, ' |> ', $node->right, $precedence, $lhsPrecedence);
430434
}
431435

test/code/prettyPrinter/expr/pipe.test

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ $a |> $b == $c;
77
$c == $a |> $b;
88
($a == $b) |> ($c == $d);
99
$a . ($b |> $c) . $d;
10+
$a |> (fn($x) => $x) |> (fn($y) => $y) |> $b;
11+
(fn($x) => $x) |> $y;
12+
fn($x) => $x |> $y;
1013
-----
1114
$a |> $b |> $c;
1215
$a . $b |> $c . $d;
1316
$a |> $b == $c;
1417
$c == $a |> $b;
1518
($a == $b) |> ($c == $d);
16-
$a . ($b |> $c) . $d;
19+
$a . ($b |> $c) . $d;
20+
$a |> (fn($x) => $x) |> (fn($y) => $y) |> $b;
21+
(fn($x) => $x) |> $y;
22+
fn($x) => $x |> $y;

0 commit comments

Comments
 (0)