Skip to content

Commit ff2e10f

Browse files
committed
fix: attribute PSR compatibility
1 parent 885c1fe commit ff2e10f

File tree

4 files changed

+65
-62
lines changed

4 files changed

+65
-62
lines changed

src/printer.mjs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,30 +1170,25 @@ function printAttrs(path, options, print, { inline = false } = {}) {
11701170
return [];
11711171
}
11721172
path.each(() => {
1173-
const attrGroup = ["#["];
1173+
const attrGroup = [];
11741174
if (!inline && allAttrs.length > 0) {
11751175
allAttrs.push(hardline);
11761176
}
1177-
attrGroup.push(softline);
1178-
path.each(() => {
1177+
path.each((_, index) => {
11791178
const attrNode = path.node;
1180-
if (attrGroup.length > 2) {
1181-
attrGroup.push(",", line);
1182-
}
11831179
const attrStmt = [attrNode.name];
11841180
if (attrNode.args.length > 0) {
11851181
attrStmt.push(printArgumentsList(path, options, print, "args"));
11861182
}
1187-
attrGroup.push(group(attrStmt));
1183+
if (index === 0) {
1184+
attrGroup.push("#[");
1185+
} else {
1186+
attrGroup.push(ifBreak([line, "#["], ", "));
1187+
}
1188+
attrGroup.push(group(attrStmt), ifBreak("]"));
11881189
}, "attrs");
11891190
allAttrs.push(
1190-
group([
1191-
indent(attrGroup),
1192-
ifBreak(shouldPrintComma(options, "8.0") ? "," : ""),
1193-
softline,
1194-
"]",
1195-
inline ? ifBreak(softline, " ") : "",
1196-
])
1191+
group([attrGroup, ifBreak("", "]"), inline ? ifBreak(softline, " ") : ""])
11971192
);
11981193
}, "attrGroups");
11991194
if (allAttrs.length === 0) {

tests/attributes-trail-comma/__snapshots__/jsfmt.spec.mjs.snap

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,24 @@ final class ORM
2626
=====================================output=====================================
2727
<?php
2828
29-
#[
30-
W(
31-
"a",
32-
null,
33-
"looooong",
34-
"paraaaams",
35-
"list",
36-
"aaaaaaaaaaaaa",
37-
"vvvvvvvvvvvv",
38-
"cccccccccc",
39-
"eeeeeeeeeee",
40-
),
41-
X,
42-
]
29+
#[W(
30+
"a",
31+
null,
32+
"looooong",
33+
"paraaaams",
34+
"list",
35+
"aaaaaaaaaaaaa",
36+
"vvvvvvvvvvvv",
37+
"cccccccccc",
38+
"eeeeeeeeeee",
39+
)]
40+
#[X]
4341
final class ORM
4442
{
45-
#[
46-
ORM\\Column,
47-
ORM\\CustomIdGenerator(class: "bar"),
48-
ORM\\GeneratedValue(strategy: "CUSTOM"),
49-
ORM\\Id,
50-
]
43+
#[ORM\\Column]
44+
#[ORM\\CustomIdGenerator(class: "bar")]
45+
#[ORM\\GeneratedValue(strategy: "CUSTOM")]
46+
#[ORM\\Id]
5147
private string $id;
5248
}
5349

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

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ final class DomainEventMessage
6161
}
6262
}
6363
64+
// issue #2360
65+
#[Route('route/path', name: 'very_very_very_very_very_very_long_route_name', methods: ['GET'])]
66+
class Controller
67+
{
68+
}
69+
6470
#[
6571
Attr1(Attr1::FOO | Attr1::BAR),
6672
Attr2(-20 * 5 + 10)
@@ -148,36 +154,30 @@ class D
148154
}
149155
}
150156
151-
#[
152-
W(
153-
"a",
154-
null,
155-
"looooong",
156-
"paraaaams",
157-
"list",
158-
"aaaaaaaaaaaaa",
159-
"vvvvvvvvvvvv",
160-
"cccccccccc",
161-
"eeeeeeeeeee"
162-
),
163-
X
164-
]
157+
#[W(
158+
"a",
159+
null,
160+
"looooong",
161+
"paraaaams",
162+
"list",
163+
"aaaaaaaaaaaaa",
164+
"vvvvvvvvvvvv",
165+
"cccccccccc",
166+
"eeeeeeeeeee"
167+
)]
168+
#[X]
165169
function Y(
166-
#[
167-
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ(
168-
12345678,
169-
1234578
170-
)
171-
]
170+
#[ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ(
171+
12345678,
172+
1234578
173+
)]
172174
string $_
173175
): string {
174176
return new #[NON, Anon] class {};
175177
}
176178
177-
#[
178-
IA("interface"),
179-
\\Looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong\\Namespace\\WithStuff\\IB
180-
]
179+
#[IA("interface")]
180+
#[\\Looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong\\Namespace\\WithStuff\\IB]
181181
interface IC
182182
{
183183
#[ID]
@@ -202,15 +202,21 @@ final class DomainEventMessage
202202
}
203203
}
204204
205+
// issue #2360
206+
#[Route(
207+
"route/path",
208+
name: "very_very_very_very_very_very_long_route_name",
209+
methods: ["GET"]
210+
)]
211+
class Controller {}
212+
205213
#[Attr1(Attr1::FOO | Attr1::BAR), Attr2(-20 * 5 + 10)]
206214
class A {}
207215
208216
class ValueModel
209217
{
210-
#[
211-
Assert\\NotBlank(allowNull: false, groups: ["foo"]),
212-
Assert\\Length(max: 255, groups: ["foo"])
213-
]
218+
#[Assert\\NotBlank(allowNull: false, groups: ["foo"])]
219+
#[Assert\\Length(max: 255, groups: ["foo"])]
214220
public ?string $value = null;
215221
}
216222

tests/attributes/attributes.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ public function getPayload1(): string
5353
}
5454
}
5555

56+
// issue #2360
57+
#[Route('route/path', name: 'very_very_very_very_very_very_long_route_name', methods: ['GET'])]
58+
class Controller
59+
{
60+
}
61+
5662
#[
5763
Attr1(Attr1::FOO | Attr1::BAR),
5864
Attr2(-20 * 5 + 10)

0 commit comments

Comments
 (0)