Skip to content

Commit fe3a144

Browse files
committed
folding longer expressions with pipe operator
1 parent b2df869 commit fe3a144

4 files changed

Lines changed: 83 additions & 7 deletions

File tree

src/needs-parens.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ function needsParens(path, options) {
9090
return true;
9191
}
9292

93+
if (po === "|>" && no === "|>") {
94+
return false;
95+
}
96+
9397
if (pp === np && key === "right") {
9498
return true;
9599
}

src/printer.mjs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const {
5050
dedent,
5151
ifBreak,
5252
hardline,
53+
hardlineWithoutBreakParent,
5354
softline,
5455
literalline,
5556
align,
@@ -114,7 +115,15 @@ function genericPrint(path, options, print) {
114115
}
115116

116117
if (lineShouldEndWithSemicolon(path)) {
117-
parts.push(";");
118+
const isPipeChainStatement =
119+
node.kind === "expressionstatement" &&
120+
(node.expression.kind === "bin" && node.expression.type === "|>" ||
121+
(node.expression.kind === "assign" &&
122+
node.expression.right.kind === "bin" &&
123+
node.expression.right.type === "|>"));
124+
if (!isPipeChainStatement) {
125+
parts.push(";");
126+
}
118127
}
119128

120129
if (fileShouldEndWithHardline(path)) {
@@ -741,6 +750,28 @@ function shouldInlineLogicalExpression(node) {
741750
// precedence level and the AST is structured based on precedence
742751
// level, things are naturally broken up correctly, i.e. `&&` is
743752
// broken before `+`.
753+
function printPipeChain(path, print, trailingSemicolon = false) {
754+
const elements = [];
755+
756+
function collect() {
757+
const { node } = path;
758+
if (node.kind === "bin" && node.type === "|>") {
759+
elements.push(path.call(print, "left"));
760+
path.call(collect, "right");
761+
} else {
762+
elements.push(print());
763+
}
764+
}
765+
766+
collect();
767+
768+
const [first, ...rest] = elements;
769+
const semicolon = trailingSemicolon
770+
? ifBreak([hardlineWithoutBreakParent, ";"], ";")
771+
: "";
772+
return group([first, indent(rest.flatMap((el) => [line, "|> ", el])), semicolon]);
773+
}
774+
744775
function printBinaryExpression(
745776
path,
746777
print,
@@ -1524,6 +1555,7 @@ function printAssignmentRight(
15241555

15251556
const canBreak =
15261557
(pureRightNode.kind === "bin" &&
1558+
pureRightNode.type !== "|>" &&
15271559
!shouldInlineLogicalExpression(pureRightNode)) ||
15281560
(pureRightNode.kind === "retif" &&
15291561
((!pureRightNode.trueExpr &&
@@ -2487,6 +2519,16 @@ function printNode(path, options, print) {
24872519
);
24882520
}
24892521
case "bin": {
2522+
if (node.type === "|>") {
2523+
const { parent, grandparent } = path;
2524+
const isStatementLevel =
2525+
parent.kind === "expressionstatement" ||
2526+
(parent.kind === "assign" &&
2527+
grandparent &&
2528+
grandparent.kind === "expressionstatement");
2529+
return printPipeChain(path, print, isStatementLevel);
2530+
}
2531+
24902532
const { parent, grandparent: parentParent } = path;
24912533
const isInsideParenthesis =
24922534
node !== parent.body &&

tests/pipe/__snapshots__/jsfmt.spec.mjs.snap

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,22 @@ foo($value |> transform(...), $other);
2525
2626
$veryLongVariableName |> someVeryLongFunctionName(...) |> anotherVeryLongFunctionName(...);
2727
28+
$result = $a |> fn1(...) || $b |> fn2(...);
29+
30+
$result = $value |> fn1($inner |> transform(...)) |> fn2(...);
31+
32+
$result = $veryLongValue |> someFunction($innerValue |> innerTransform(...) |> anotherInnerTransform(...) |> yetAnotherInnerTransform(...)) |> finalTransform(...);
33+
2834
=====================================output=====================================
2935
<?php
3036
3137
$result = "Hello, World!" |> strtoupper(...);
32-
$result = "Hello, World!" |> (strtoupper(...) |> trim(...));
33-
$result =
34-
"Hello, World!" |>
35-
(strtoupper(...) |> (trim(...) |> htmlspecialchars(...)));
38+
$result = "Hello, World!" |> strtoupper(...) |> trim(...);
39+
$result = "Hello, World!"
40+
|> strtoupper(...)
41+
|> trim(...)
42+
|> htmlspecialchars(...)
43+
;
3644
3745
return $value |> transform(...);
3846
echo $value |> format(...);
@@ -45,8 +53,24 @@ $result = $value ?? ($fallback |> fn1(...));
4553
4654
foo($value |> transform(...), $other);
4755
48-
$veryLongVariableName |>
49-
(someVeryLongFunctionName(...) |> anotherVeryLongFunctionName(...));
56+
$veryLongVariableName
57+
|> someVeryLongFunctionName(...)
58+
|> anotherVeryLongFunctionName(...)
59+
;
60+
61+
$result = $a |> fn1(...) || ($b |> fn2(...));
62+
63+
$result = $value |> fn1($inner |> transform(...)) |> fn2(...);
64+
65+
$result = $veryLongValue
66+
|> someFunction(
67+
$innerValue
68+
|> innerTransform(...)
69+
|> anotherInnerTransform(...)
70+
|> yetAnotherInnerTransform(...),
71+
)
72+
|> finalTransform(...)
73+
;
5074
5175
================================================================================
5276
`;

tests/pipe/pipe.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@
1515
foo($value |> transform(...), $other);
1616

1717
$veryLongVariableName |> someVeryLongFunctionName(...) |> anotherVeryLongFunctionName(...);
18+
19+
$result = $a |> fn1(...) || $b |> fn2(...);
20+
21+
$result = $value |> fn1($inner |> transform(...)) |> fn2(...);
22+
23+
$result = $veryLongValue |> someFunction($innerValue |> innerTransform(...) |> anotherInnerTransform(...) |> yetAnotherInnerTransform(...)) |> finalTransform(...);

0 commit comments

Comments
 (0)