Skip to content

Commit cc63b7e

Browse files
authored
Merge branch 'prettier:main' into ci/drop-node-18
2 parents 6b13f6a + f0e4ea4 commit cc63b7e

File tree

11 files changed

+1383
-712
lines changed

11 files changed

+1383
-712
lines changed

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,28 @@
2727
],
2828
"dependencies": {
2929
"linguist-languages": "^8.0.0",
30-
"php-parser": "^3.2.5"
30+
"php-parser": "^3.4.0"
3131
},
3232
"devDependencies": {
3333
"@babel/preset-env": "^7.27.2",
3434
"@rollup/plugin-alias": "^5.1.0",
35-
"@rollup/plugin-babel": "^6.0.3",
36-
"@rollup/plugin-commonjs": "^25.0.7",
35+
"@rollup/plugin-babel": "^7.0.0",
36+
"@rollup/plugin-commonjs": "^29.0.2",
3737
"@rollup/plugin-inject": "^5.0.5",
3838
"@rollup/plugin-json": "^6.1.0",
39-
"@rollup/plugin-node-resolve": "^15.2.1",
40-
"@rollup/plugin-replace": "^5.0.5",
39+
"@rollup/plugin-node-resolve": "^16.0.3",
40+
"@rollup/plugin-replace": "^6.0.3",
4141
"@rollup/plugin-terser": "^0.4.3",
4242
"c8": "^10.1.3",
4343
"cross-env": "^7.0.2",
4444
"eslint": "8.57.0",
4545
"eslint-config-prettier": "9.1.0",
4646
"eslint-plugin-import": "2.29.1",
4747
"eslint-plugin-jest": "27.6.3",
48-
"eslint-plugin-prettier": "5.2.1",
48+
"eslint-plugin-prettier": "5.5.5",
4949
"eslint-plugin-prettier-doc": "^1.1.0",
50-
"jest": "30.0.3",
51-
"jest-environment-jsdom": "30.0.2",
50+
"jest": "30.3.0",
51+
"jest-environment-jsdom": "30.3.0",
5252
"jest-light-runner": "^0.7.9",
5353
"jest-snapshot-serializer-raw": "^2.0.0",
5454
"prettier": "^3.6.1",

src/options.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const CATEGORY_PHP = "PHP";
77
const SUPPORTED_PHP_VERSIONS = [
88
5.0, 5.1, 5.2, 5.3, 5.4, 5.5, 5.6,
99
7.0, 7.1, 7.2, 7.3, 7.4,
10-
8.0, 8.1, 8.2, 8.3, 8.4,
10+
8.0, 8.1, 8.2, 8.3, 8.4, 8.5,
1111
];
1212

1313
export const LATEST_SUPPORTED_PHP_VERSION = Math.max(...SUPPORTED_PHP_VERSIONS);

src/printer.mjs

Lines changed: 40 additions & 2 deletions
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,
@@ -741,6 +742,32 @@ function shouldInlineLogicalExpression(node) {
741742
// precedence level and the AST is structured based on precedence
742743
// level, things are naturally broken up correctly, i.e. `&&` is
743744
// broken before `+`.
745+
function printPipeChain(path, print, trailingSemicolon = false) {
746+
const elements = [];
747+
748+
function collect() {
749+
const { node } = path;
750+
if (node.kind === "bin" && node.type === "|>") {
751+
elements.push(path.call(print, "left"));
752+
path.call(collect, "right");
753+
} else {
754+
elements.push(print());
755+
}
756+
}
757+
758+
collect();
759+
760+
const [first, ...rest] = elements;
761+
const semicolon = trailingSemicolon
762+
? ifBreak([hardlineWithoutBreakParent, ";"], ";")
763+
: "";
764+
return group([
765+
first,
766+
indent(rest.flatMap((el) => [line, "|> ", el])),
767+
semicolon,
768+
]);
769+
}
770+
744771
function printBinaryExpression(
745772
path,
746773
print,
@@ -1219,7 +1246,7 @@ function printClass(path, options, print) {
12191246
declaration.push("abstract ");
12201247
}
12211248

1222-
if (node.isReadonly) {
1249+
if (node.isReadonly && !isAnonymousClass) {
12231250
declaration.push("readonly ");
12241251
}
12251252

@@ -1524,6 +1551,7 @@ function printAssignmentRight(
15241551

15251552
const canBreak =
15261553
(pureRightNode.kind === "bin" &&
1554+
pureRightNode.type !== "|>" &&
15271555
!shouldInlineLogicalExpression(pureRightNode)) ||
15281556
(pureRightNode.kind === "retif" &&
15291557
((!pureRightNode.trueExpr &&
@@ -2094,7 +2122,7 @@ function printNode(path, options, print) {
20942122
() => printAttrs(path, options, print, { inline: true }),
20952123
"what"
20962124
),
2097-
"class",
2125+
node.what.isReadonly ? "readonly class" : "class",
20982126
node.arguments.length > 0
20992127
? [" ", printArgumentsList(path, options, print)]
21002128
: "",
@@ -2487,6 +2515,16 @@ function printNode(path, options, print) {
24872515
);
24882516
}
24892517
case "bin": {
2518+
if (node.type === "|>") {
2519+
const { parent, grandparent } = path;
2520+
const isStatementLevel =
2521+
parent.kind === "expressionstatement" ||
2522+
(parent.kind === "assign" &&
2523+
grandparent &&
2524+
grandparent.kind === "expressionstatement");
2525+
return printPipeChain(path, print, isStatementLevel);
2526+
}
2527+
24902528
const { parent, grandparent: parentParent } = path;
24912529
const isInsideParenthesis =
24922530
node !== parent.body &&

src/util.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const PRECEDENCE = new Map(
4141
"<<=",
4242
">>=",
4343
],
44+
["|>"],
4445
["??"],
4546
["||"],
4647
["&&"],
@@ -359,6 +360,17 @@ function lineShouldEndWithSemicolon(path) {
359360
return true;
360361
}
361362
}
363+
if (node.kind === "expressionstatement") {
364+
const expr = node.expression;
365+
const isPipeChain =
366+
(expr.kind === "bin" && expr.type === "|>") ||
367+
(expr.kind === "assign" &&
368+
expr.right.kind === "bin" &&
369+
expr.right.type === "|>");
370+
if (isPipeChain) {
371+
return false;
372+
}
373+
}
362374
return [
363375
"expressionstatement",
364376
"do",
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`pipe.php 1`] = `
4+
====================================options=====================================
5+
parsers: ["php"]
6+
phpVersion: "8.5"
7+
printWidth: 80
8+
| printWidth
9+
=====================================input======================================
10+
<?php
11+
12+
$result = "Hello, World!" |> strtoupper(...);
13+
$result = "Hello, World!" |> strtoupper(...) |> trim(...);
14+
$result = "Hello, World!" |> strtoupper(...) |> trim(...) |> htmlspecialchars(...);
15+
16+
return $value |> transform(...);
17+
echo $value |> format(...);
18+
19+
if ($x |> validate(...)) {}
20+
21+
$result = $value |> fn1(...) ?? $fallback;
22+
$result = $value ?? $fallback |> fn1(...);
23+
24+
foo($value |> transform(...), $other);
25+
26+
$veryLongVariableName |> someVeryLongFunctionName(...) |> anotherVeryLongFunctionName(...);
27+
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+
34+
=====================================output=====================================
35+
<?php
36+
37+
$result = "Hello, World!" |> strtoupper(...);
38+
$result = "Hello, World!" |> strtoupper(...) |> trim(...);
39+
$result = "Hello, World!"
40+
|> strtoupper(...)
41+
|> trim(...)
42+
|> htmlspecialchars(...)
43+
;
44+
45+
return $value |> transform(...);
46+
echo $value |> format(...);
47+
48+
if ($x |> validate(...)) {
49+
}
50+
51+
$result = $value |> fn1(...) ?? $fallback;
52+
$result = $value ?? ($fallback |> fn1(...));
53+
54+
foo($value |> transform(...), $other);
55+
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+
;
74+
75+
================================================================================
76+
`;

tests/pipe/jsfmt.spec.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
run_spec(import.meta, ["php"], { phpVersion: "8.5" });

tests/pipe/pipe.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
$result = "Hello, World!" |> strtoupper(...);
4+
$result = "Hello, World!" |> strtoupper(...) |> trim(...);
5+
$result = "Hello, World!" |> strtoupper(...) |> trim(...) |> htmlspecialchars(...);
6+
7+
return $value |> transform(...);
8+
echo $value |> format(...);
9+
10+
if ($x |> validate(...)) {}
11+
12+
$result = $value |> fn1(...) ?? $fallback;
13+
$result = $value ?? $fallback |> fn1(...);
14+
15+
foo($value |> transform(...), $other);
16+
17+
$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(...);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`anonymous.php 1`] = `
4+
====================================options=====================================
5+
parsers: ["php"]
6+
phpVersion: "8.4"
7+
printWidth: 80
8+
| printWidth
9+
=====================================input======================================
10+
<?php
11+
12+
new readonly class() {};
13+
new readonly class {};
14+
new readonly class($one, $two, $three) {};
15+
16+
$class = new readonly class {};
17+
$class = new readonly class() {};
18+
$class = new readonly class { public int $x = 0; };
19+
$class = new readonly class implements MyInterface {};
20+
$class = new readonly class implements MyInterface, MyOtherInterface {};
21+
$class = new readonly class extends MyParent {};
22+
$class = new readonly class extends MyParent implements MyInterface {};
23+
$class = new readonly class extends MyParent implements MyInterface, MyOtherInterface {};
24+
25+
$class = new readonly class implements VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyOtherClass {};
26+
$class = new readonly class extends VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyClass {};
27+
$class = new readonly class extends VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyClass implements MyI, MyII, MyIII {};
28+
29+
$class = new readonly class($one, $two) implements MyInterface {};
30+
$class = new readonly class($one, $two) extends MyParent implements MyInterface {};
31+
32+
=====================================output=====================================
33+
<?php
34+
35+
new readonly class {};
36+
new readonly class {};
37+
new readonly class ($one, $two, $three) {};
38+
39+
$class = new readonly class {};
40+
$class = new readonly class {};
41+
$class = new readonly class {
42+
public int $x = 0;
43+
};
44+
$class = new readonly class implements MyInterface {};
45+
$class = new readonly class implements MyInterface, MyOtherInterface {};
46+
$class = new readonly class extends MyParent {};
47+
$class = new readonly class extends MyParent implements MyInterface {};
48+
$class = new readonly class extends MyParent implements
49+
MyInterface,
50+
MyOtherInterface {};
51+
52+
$class = new readonly class implements
53+
VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyOtherClass {};
54+
$class = new readonly class extends
55+
VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyClass {};
56+
$class = new readonly class
57+
extends VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyClass
58+
implements
59+
MyI,
60+
MyII,
61+
MyIII {};
62+
63+
$class = new readonly class ($one, $two) implements MyInterface {};
64+
$class = new readonly class ($one, $two) extends MyParent implements
65+
MyInterface {};
66+
67+
================================================================================
68+
`;

tests/readonly-class/anonymous.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
new readonly class() {};
4+
new readonly class {};
5+
new readonly class($one, $two, $three) {};
6+
7+
$class = new readonly class {};
8+
$class = new readonly class() {};
9+
$class = new readonly class { public int $x = 0; };
10+
$class = new readonly class implements MyInterface {};
11+
$class = new readonly class implements MyInterface, MyOtherInterface {};
12+
$class = new readonly class extends MyParent {};
13+
$class = new readonly class extends MyParent implements MyInterface {};
14+
$class = new readonly class extends MyParent implements MyInterface, MyOtherInterface {};
15+
16+
$class = new readonly class implements VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyOtherClass {};
17+
$class = new readonly class extends VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyClass {};
18+
$class = new readonly class extends VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyClass implements MyI, MyII, MyIII {};
19+
20+
$class = new readonly class($one, $two) implements MyInterface {};
21+
$class = new readonly class($one, $two) extends MyParent implements MyInterface {};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
run_spec(import.meta, ["php"], { phpVersion: "8.4" });

0 commit comments

Comments
 (0)