Skip to content

Commit 30d3c85

Browse files
authored
Update php-parser, fix unary snapshot tests, and make multiple fixes for attribute formatting. (#2502)
* Update php-parser, fix unary snapshot tests, and make multiple fixes for attribute formatting. * Update to php-parser 3.7.0 * Remove package manager declaration * Address PR feedback
1 parent 7ea62f3 commit 30d3c85

9 files changed

Lines changed: 154 additions & 36 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
],
2828
"dependencies": {
2929
"linguist-languages": "^8.0.0",
30-
"php-parser": "^3.4.0"
30+
"php-parser": "^3.7.0"
3131
},
3232
"devDependencies": {
3333
"@babel/preset-env": "^7.27.2",

‎src/comments.mjs‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ function handleOwnLineComment(comment, text, options) {
6262
options
6363
) ||
6464
handleTryComments(enclosingNode, followingNode, comment) ||
65+
handleClassMemberStatementComments(enclosingNode, followingNode, comment) ||
6566
handleClassComments(enclosingNode, followingNode, comment) ||
6667
handleFunctionParameter(
6768
text,
@@ -201,6 +202,7 @@ function handleRemainingComment(comment, text, options) {
201202
handleGoto(enclosingNode, comment) ||
202203
handleHalt(precedingNode, enclosingNode, followingNode, comment) ||
203204
handleBreakAndContinueStatementComments(enclosingNode, comment) ||
205+
handleClassMemberStatementComments(enclosingNode, followingNode, comment) ||
204206
handleInlineComments(
205207
enclosingNode,
206208
precedingNode,
@@ -433,6 +435,32 @@ function handleTraitUseComments(enclosingNode, followingNode, comment) {
433435
return false;
434436
}
435437

438+
function handleClassMemberStatementComments(
439+
enclosingNode,
440+
followingNode,
441+
comment
442+
) {
443+
if (
444+
enclosingNode &&
445+
enclosingNode.kind === "propertystatement" &&
446+
enclosingNode.properties?.includes(followingNode)
447+
) {
448+
addLeadingComment(enclosingNode, comment);
449+
return true;
450+
}
451+
452+
if (
453+
enclosingNode &&
454+
enclosingNode.kind === "classconstant" &&
455+
enclosingNode.constants?.includes(followingNode)
456+
) {
457+
addLeadingComment(enclosingNode, comment);
458+
return true;
459+
}
460+
461+
return false;
462+
}
463+
436464
function handleClassComments(enclosingNode, followingNode, comment) {
437465
if (
438466
enclosingNode &&
@@ -907,6 +935,24 @@ function getCommentChildNodes(node) {
907935
node.what.__parent_new_arguments = [...node.arguments];
908936
return [node.what];
909937
}
938+
939+
if (node.attrGroups && node.attrGroups.length > 0) {
940+
if (node.kind === "method" || node.kind === "function") {
941+
return [
942+
...node.attrGroups,
943+
...node.arguments,
944+
...(node.type ? [node.type] : []),
945+
];
946+
}
947+
948+
if (node.kind === "classconstant") {
949+
return [...node.attrGroups, ...node.constants];
950+
}
951+
952+
if (node.kind === "enumcase") {
953+
return node.attrGroups;
954+
}
955+
}
910956
}
911957

912958
function canAttachComment(node) {

‎src/printer.mjs‎

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,31 +1199,16 @@ function printAttrs(path, options, print, { inline = false } = {}) {
11991199
if (!path.node.attrGroups) {
12001200
return [];
12011201
}
1202-
path.each(() => {
1203-
const attrGroup = ["#["];
1202+
path.each((attrGroupPath) => {
12041203
if (!inline && allAttrs.length > 0) {
12051204
allAttrs.push(hardline);
12061205
}
1207-
attrGroup.push(softline);
1208-
path.each(() => {
1209-
const attrNode = path.node;
1210-
if (attrGroup.length > 2) {
1211-
attrGroup.push(",", line);
1212-
}
1213-
const attrStmt = [attrNode.name];
1214-
if (attrNode.args.length > 0) {
1215-
attrStmt.push(printArgumentsList(path, options, print, "args"));
1216-
}
1217-
attrGroup.push(group(attrStmt));
1218-
}, "attrs");
12191206
allAttrs.push(
1220-
group([
1221-
indent(attrGroup),
1222-
ifBreak(shouldPrintComma(options, 8.0) ? "," : ""),
1223-
softline,
1224-
"]",
1225-
inline ? ifBreak(softline, " ") : "",
1226-
])
1207+
printAllComments(
1208+
attrGroupPath,
1209+
() => printAttrGroup(attrGroupPath, options, print, { inline }),
1210+
options
1211+
)
12271212
);
12281213
}, "attrGroups");
12291214
if (allAttrs.length === 0) {
@@ -1232,6 +1217,29 @@ function printAttrs(path, options, print, { inline = false } = {}) {
12321217
return [...allAttrs, inline ? "" : hardline];
12331218
}
12341219

1220+
function printAttrGroup(path, options, print, { inline = false } = {}) {
1221+
const attrGroup = ["#["];
1222+
attrGroup.push(softline);
1223+
path.each(() => {
1224+
const attrNode = path.node;
1225+
if (attrGroup.length > 2) {
1226+
attrGroup.push(",", line);
1227+
}
1228+
const attrStmt = [attrNode.name];
1229+
if (attrNode.args.length > 0) {
1230+
attrStmt.push(printArgumentsList(path, options, print, "args"));
1231+
}
1232+
attrGroup.push(group(attrStmt));
1233+
}, "attrs");
1234+
return group([
1235+
indent(attrGroup),
1236+
ifBreak(shouldPrintComma(options, 8.0) ? "," : ""),
1237+
softline,
1238+
"]",
1239+
inline ? ifBreak(softline, " ") : "",
1240+
]);
1241+
}
1242+
12351243
function printClass(path, options, print) {
12361244
const { node } = path;
12371245
const isAnonymousClass = node.kind === "class" && node.isAnonymous;
@@ -2909,6 +2917,7 @@ function printNode(path, options, print) {
29092917

29102918
case "enumcase":
29112919
return group([
2920+
...printAttrs(path, options, print),
29122921
"case ",
29132922
print("name"),
29142923
node.value

‎tests/attributes/__snapshots__/jsfmt.spec.mjs.snap‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,12 @@ class D
134134
function k(#[L] int $m): callable
135135
{
136136
return #[N, O] #[P] fn(#[Q] int $r) => $r * 2;
137-
} //Testing T
137+
}
138138
139139
// Testing S
140-
//Testing S-T
141140
#[S]
142-
#[T]
141+
//Testing S-T
142+
#[T] //Testing T
143143
private function u()
144144
{
145145
return #[V] function () {

‎tests/comments/__snapshots__/jsfmt.spec.mjs.snap‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4922,9 +4922,9 @@ class Foo
49224922
49234923
public function emptyMethod(/* comments */) {}
49244924
4925-
abstract public function sortByName(/* bool $useNaturalSort = false */); /* comment */
4925+
abstract public function sortByName(/* bool $useNaturalSort = false */);
49264926
4927-
/* comment */ /* comment */ protected static $foo; /* comment */
4927+
/* comment */ /* comment */ /* comment */ protected static $foo; /* comment */
49284928
49294929
public function foo() {} // Comment
49304930

‎tests/enum/__snapshots__/jsfmt.spec.mjs.snap‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`enum.php 1`] = `
44
====================================options=====================================
@@ -143,3 +143,49 @@ class Enum extends Foo {}
143143
144144
================================================================================
145145
`;
146+
147+
exports[`enum-case-attributes.php 1`] = `
148+
====================================options=====================================
149+
parsers: ["php"]
150+
printWidth: 80
151+
| printWidth
152+
=====================================input======================================
153+
<?php
154+
155+
namespace App\\Enums;
156+
157+
use App\\Support\\Attributes\\Description;
158+
159+
enum SalutationEnum: string
160+
{
161+
#[Description('Mr.')]
162+
case MR = 'mr';
163+
164+
#[Description('Mrs.')]
165+
case MRS = 'mrs';
166+
167+
#[Description('Miss')]
168+
case MISS = 'miss';
169+
}
170+
171+
=====================================output=====================================
172+
<?php
173+
174+
namespace App\\Enums;
175+
176+
use App\\Support\\Attributes\\Description;
177+
178+
enum SalutationEnum: string
179+
{
180+
#[Description("Mr.")]
181+
case MR = "mr";
182+
183+
#[Description("Mrs.")]
184+
case MRS = "mrs";
185+
186+
#[Description("Miss")]
187+
case MISS = "miss";
188+
}
189+
190+
================================================================================
191+
`;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Enums;
4+
5+
use App\Support\Attributes\Description;
6+
7+
enum SalutationEnum: string
8+
{
9+
#[Description('Mr.')]
10+
case MR = 'mr';
11+
12+
#[Description('Mrs.')]
13+
case MRS = 'mrs';
14+
15+
#[Description('Miss')]
16+
case MISS = 'miss';
17+
}

‎tests/parens/__snapshots__/jsfmt.spec.mjs.snap‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ $var = $var + $var ** 2;
13001300
$var = ($var + $var) ** 2;
13011301
$var = $var + $var ** 2;
13021302
$var = (+$var) ** 2;
1303-
$var = (+$var) ** 2;
1303+
$var = +($var ** 2);
13041304
13051305
$var = $foo instanceof Foo;
13061306
$var = $foo instanceof Foo || $foo instanceof Foo;
@@ -2067,7 +2067,7 @@ $var = $var + $var ** 2;
20672067
$var = ($var + $var) ** 2;
20682068
$var = $var + $var ** 2;
20692069
$var = (+$var) ** 2;
2070-
$var = (+$var) ** 2;
2070+
$var = +($var ** 2);
20712071
20722072
$var = $foo instanceof Foo;
20732073
$var = $foo instanceof Foo || $foo instanceof Foo;
@@ -6574,7 +6574,7 @@ $var = ~~$var;
65746574
$var = !$var;
65756575
$var = !!$var;
65766576
6577-
$a = (+$a) ** 1;
6577+
$a = +($a ** 1);
65786578
$a = (+$a) ** 1;
65796579
$a = 1 ** +$a;
65806580
@@ -6718,7 +6718,7 @@ $var = ~~$var;
67186718
$var = !$var;
67196719
$var = !!$var;
67206720
6721-
$a = (+$a) ** 1;
6721+
$a = +($a ** 1);
67226722
$a = (+$a) ** 1;
67236723
$a = 1 ** +$a;
67246724

‎yarn.lock‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5737,10 +5737,10 @@ path-type@^4.0.0:
57375737
resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b"
57385738
integrity sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==
57395739

5740-
php-parser@^3.4.0:
5741-
version "3.4.0"
5742-
resolved "https://registry.yarnpkg.com/php-parser/-/php-parser-3.4.0.tgz#dcd7743bf01caaaa7281e8b253f4cdbbea6e1265"
5743-
integrity sha512-JoDPazv8OESrVtcoIuO8HC587zVNJYSYUIl9zYn+JEpwlHSOrxmyMHB+sDS0O1ID5z1aFxPnr+vAUGoSKphlHA==
5740+
php-parser@^3.7.0:
5741+
version "3.7.0"
5742+
resolved "https://registry.yarnpkg.com/php-parser/-/php-parser-3.7.0.tgz#b40b82dc9ba2ad3068b418130f50a84b663d29ac"
5743+
integrity sha512-JRc1t78GZAEa+MuzVC5A5RJS1NDFTS4UnprUEu/NnsN9cyHbGZLUqghO9IQZUSCay62HYQiWd3PxyWAEF45zmA==
57445744

57455745
picocolors@^1.1.1:
57465746
version "1.1.1"

0 commit comments

Comments
 (0)