Skip to content

Commit dd8994a

Browse files
committed
fix: print attributes on anonymous classes one per line
Attributes on `new` anonymous classes were collapsed onto a single line (`new #[Lazy] #[Computed] #[Title("Staff")] class ...`). Print each attribute group on its own line, like named classes, with `new` on its own line. Fixes #2437
1 parent 269374b commit dd8994a

3 files changed

Lines changed: 57 additions & 9 deletions

File tree

‎src/printer.mjs‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,25 +2118,30 @@ function printNode(path, options, print) {
21182118

21192119
const parts = [];
21202120

2121-
parts.push("new ");
2122-
21232121
if (isAnonymousClassNode) {
2122+
const hasAttrs =
2123+
node.what.attrGroups && node.what.attrGroups.length > 0;
2124+
// Attributes on anonymous classes print one per line, like named
2125+
// classes, with `new` on its own line.
2126+
const breakAfterNew = hasAttrs ? hardline : " ";
21242127
parts.push(
2128+
"new",
2129+
breakAfterNew,
21252130
node.what.leadingComments &&
21262131
node.what.leadingComments[0].kind === "commentblock"
2127-
? [printComments(node.what.leadingComments, options), " "]
2132+
? [printComments(node.what.leadingComments, options), breakAfterNew]
21282133
: "",
2129-
...path.call(
2130-
() => printAttrs(path, options, print, { inline: true }),
2131-
"what"
2132-
),
2134+
...(hasAttrs
2135+
? path.call(() => printAttrs(path, options, print), "what")
2136+
: []),
21332137
node.what.isReadonly ? "readonly class" : "class",
21342138
node.arguments.length > 0
21352139
? [" ", printArgumentsList(path, options, print)]
21362140
: "",
21372141
group(print("what"))
21382142
);
21392143
} else {
2144+
parts.push("new ");
21402145
const isExpression = ["call", "offsetlookup"].includes(node.what.kind);
21412146
const printed = [
21422147
isExpression ? "(" : "",

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

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

33
exports[`attributes.php 1`] = `
44
====================================options=====================================
@@ -112,6 +112,19 @@ class ParamCommentFunctionAnnotation {
112112
113113
}
114114
115+
// issue #2437: attributes on anonymous classes print one per line
116+
$anonymous = new
117+
#[Lazy]
118+
#[Computed]
119+
#[Title('Staff')]
120+
class extends Component {
121+
public $editing = [];
122+
123+
public $branches;
124+
};
125+
126+
$singleAttributeAnonymous = new #[Single] class {};
127+
115128
=====================================output=====================================
116129
<?php
117130
@@ -171,7 +184,9 @@ function Y(
171184
]
172185
string $_,
173186
): string {
174-
return new #[NON, Anon] class {};
187+
return new
188+
#[NON, Anon]
189+
class {};
175190
}
176191
177192
#[
@@ -248,5 +263,20 @@ class ParamCommentFunctionAnnotation
248263
}
249264
}
250265
266+
// issue #2437: attributes on anonymous classes print one per line
267+
$anonymous = new
268+
#[Lazy]
269+
#[Computed]
270+
#[Title("Staff")]
271+
class extends Component {
272+
public $editing = [];
273+
274+
public $branches;
275+
};
276+
277+
$singleAttributeAnonymous = new
278+
#[Single]
279+
class {};
280+
251281
================================================================================
252282
`;

‎tests/attributes/attributes.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,16 @@ function bar2(
103103
}
104104

105105
}
106+
107+
// issue #2437: attributes on anonymous classes print one per line
108+
$anonymous = new
109+
#[Lazy]
110+
#[Computed]
111+
#[Title('Staff')]
112+
class extends Component {
113+
public $editing = [];
114+
115+
public $branches;
116+
};
117+
118+
$singleAttributeAnonymous = new #[Single] class {};

0 commit comments

Comments
 (0)