Skip to content

Commit 41347a1

Browse files
authored
feat(php8 hanging comma) Add support for haning comma for functions, closures, arrow functions and methods (#1769)
* added trailing commas for PHP 8 on function, closure, method, and arrow functions * update package as changes have been merged * fixed package path * removed placeholder comment
1 parent b08d1d2 commit 41347a1

6 files changed

Lines changed: 205 additions & 5 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"dependencies": {
1414
"linguist-languages": "^7.5.1",
1515
"mem": "^8.0.0",
16-
"php-parser": "https://github.com/glayzzle/php-parser#425c4dcc9f386596bdcd5b458503b3667a858141"
16+
"php-parser": "https://github.com/glayzzle/php-parser#cca83041d938b972c7ef74648f29de03777b0ea4"
1717
},
1818
"devDependencies": {
1919
"@babel/preset-env": "^7.7.5",

src/printer.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,10 @@ function printArgumentsList(path, options, print, argumentsKey = "arguments") {
632632
const lastArg = getLast(args);
633633

634634
const maybeTrailingComma =
635-
["call", "new", "unset", "isset"].includes(node.kind) &&
636-
shouldPrintComma(options, "7.3")
635+
(shouldPrintComma(options, "7.3") &&
636+
["call", "new", "unset", "isset"].includes(node.kind)) ||
637+
(shouldPrintComma(options, "8.0") &&
638+
["function", "closure", "method", "arrowfunc"].includes(node.kind))
637639
? indent(
638640
concat([
639641
lastArg && shouldPrintHardlineBeforeTrailingComma(lastArg)
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`function.php 1`] = `
4+
====================================options=====================================
5+
parsers: ["php"]
6+
phpVersion: "8.0"
7+
printWidth: 80
8+
trailingComma: "all"
9+
trailingCommaPHP: false
10+
| printWidth
11+
=====================================input======================================
12+
<?php
13+
14+
class f1 {
15+
function f2(
16+
int $classFunctionArgumentOne,
17+
?float $classFunctionArgumentTwo,
18+
string $classFunctionArgumentThree,
19+
) {
20+
return fn(
21+
$labdaFunctionArgumentOne,
22+
$labdaFunctionArgumentTwo,
23+
$labdaFunctionArgumentThree,
24+
$labdaFunctionArgumentFour,
25+
) => true;
26+
}
27+
}
28+
29+
function f3(
30+
$standaloneFunctionArgumentOne,
31+
$standaloneFunctionArgumentTwo,
32+
$standaloneFunctionArgumentThree,
33+
$standaloneFunctionArgumentFour,
34+
) {
35+
}
36+
37+
f3(
38+
"abcdefghijklmnopqrstuvwxyz",
39+
"abcdefghijklmnopqrstuvwxyz",
40+
"abcdefghijklmnopqrstuvwxyz",
41+
"abcdefghijklmnopqrstuvwxyz",
42+
);
43+
44+
=====================================output=====================================
45+
<?php
46+
47+
class f1
48+
{
49+
function f2(
50+
int $classFunctionArgumentOne,
51+
?float $classFunctionArgumentTwo,
52+
string $classFunctionArgumentThree
53+
) {
54+
return fn(
55+
$labdaFunctionArgumentOne,
56+
$labdaFunctionArgumentTwo,
57+
$labdaFunctionArgumentThree,
58+
$labdaFunctionArgumentFour
59+
) => true;
60+
}
61+
}
62+
63+
function f3(
64+
$standaloneFunctionArgumentOne,
65+
$standaloneFunctionArgumentTwo,
66+
$standaloneFunctionArgumentThree,
67+
$standaloneFunctionArgumentFour
68+
) {
69+
}
70+
71+
f3(
72+
"abcdefghijklmnopqrstuvwxyz",
73+
"abcdefghijklmnopqrstuvwxyz",
74+
"abcdefghijklmnopqrstuvwxyz",
75+
"abcdefghijklmnopqrstuvwxyz"
76+
);
77+
78+
================================================================================
79+
`;
80+
81+
exports[`function.php 2`] = `
82+
====================================options=====================================
83+
parsers: ["php"]
84+
phpVersion: "8.0"
85+
printWidth: 80
86+
trailingComma: "none"
87+
trailingCommaPHP: true
88+
| printWidth
89+
=====================================input======================================
90+
<?php
91+
92+
class f1 {
93+
function f2(
94+
int $classFunctionArgumentOne,
95+
?float $classFunctionArgumentTwo,
96+
string $classFunctionArgumentThree,
97+
) {
98+
return fn(
99+
$labdaFunctionArgumentOne,
100+
$labdaFunctionArgumentTwo,
101+
$labdaFunctionArgumentThree,
102+
$labdaFunctionArgumentFour,
103+
) => true;
104+
}
105+
}
106+
107+
function f3(
108+
$standaloneFunctionArgumentOne,
109+
$standaloneFunctionArgumentTwo,
110+
$standaloneFunctionArgumentThree,
111+
$standaloneFunctionArgumentFour,
112+
) {
113+
}
114+
115+
f3(
116+
"abcdefghijklmnopqrstuvwxyz",
117+
"abcdefghijklmnopqrstuvwxyz",
118+
"abcdefghijklmnopqrstuvwxyz",
119+
"abcdefghijklmnopqrstuvwxyz",
120+
);
121+
122+
=====================================output=====================================
123+
<?php
124+
125+
class f1
126+
{
127+
function f2(
128+
int $classFunctionArgumentOne,
129+
?float $classFunctionArgumentTwo,
130+
string $classFunctionArgumentThree,
131+
) {
132+
return fn(
133+
$labdaFunctionArgumentOne,
134+
$labdaFunctionArgumentTwo,
135+
$labdaFunctionArgumentThree,
136+
$labdaFunctionArgumentFour,
137+
) => true;
138+
}
139+
}
140+
141+
function f3(
142+
$standaloneFunctionArgumentOne,
143+
$standaloneFunctionArgumentTwo,
144+
$standaloneFunctionArgumentThree,
145+
$standaloneFunctionArgumentFour,
146+
) {
147+
}
148+
149+
f3(
150+
"abcdefghijklmnopqrstuvwxyz",
151+
"abcdefghijklmnopqrstuvwxyz",
152+
"abcdefghijklmnopqrstuvwxyz",
153+
"abcdefghijklmnopqrstuvwxyz",
154+
);
155+
156+
================================================================================
157+
`;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
class f1 {
4+
function f2(
5+
int $classFunctionArgumentOne,
6+
?float $classFunctionArgumentTwo,
7+
string $classFunctionArgumentThree,
8+
) {
9+
return fn(
10+
$labdaFunctionArgumentOne,
11+
$labdaFunctionArgumentTwo,
12+
$labdaFunctionArgumentThree,
13+
$labdaFunctionArgumentFour,
14+
) => true;
15+
}
16+
}
17+
18+
function f3(
19+
$standaloneFunctionArgumentOne,
20+
$standaloneFunctionArgumentTwo,
21+
$standaloneFunctionArgumentThree,
22+
$standaloneFunctionArgumentFour,
23+
) {
24+
}
25+
26+
f3(
27+
"abcdefghijklmnopqrstuvwxyz",
28+
"abcdefghijklmnopqrstuvwxyz",
29+
"abcdefghijklmnopqrstuvwxyz",
30+
"abcdefghijklmnopqrstuvwxyz",
31+
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
run_spec(__dirname, ["php"], {
2+
trailingComma: "all",
3+
trailingCommaPHP: false,
4+
phpVersion: "8.0",
5+
});
6+
run_spec(__dirname, ["php"], {
7+
trailingComma: "none",
8+
trailingCommaPHP: true,
9+
phpVersion: "8.0",
10+
});

yarn.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4603,9 +4603,9 @@ performance-now@^2.1.0:
46034603
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
46044604
integrity sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=
46054605

4606-
"php-parser@https://github.com/glayzzle/php-parser#425c4dcc9f386596bdcd5b458503b3667a858141":
4606+
"php-parser@https://github.com/glayzzle/php-parser#cca83041d938b972c7ef74648f29de03777b0ea4":
46074607
version "3.0.2"
4608-
resolved "https://github.com/glayzzle/php-parser#425c4dcc9f386596bdcd5b458503b3667a858141"
4608+
resolved "https://github.com/glayzzle/php-parser#cca83041d938b972c7ef74648f29de03777b0ea4"
46094609

46104610
picomatch@^2.0.4, picomatch@^2.0.5, picomatch@^2.2.1:
46114611
version "2.2.2"

0 commit comments

Comments
 (0)