Skip to content

Commit 4e9127c

Browse files
committed
Also handle new #[MyAttr] class {...}
I forgot that anonymous classes were parsed into an ObjectCreationExpression instead. Also support attributes there, cause a diagnostic to be emitted if the attribute group(s) aren't followed by the `class` keyword. For microsoft#320
1 parent 917e065 commit 4e9127c

33 files changed

+277
-1
lines changed

src/Node/Expression/ObjectCreationExpression.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Microsoft\PhpParser\Node\Expression;
88

9+
use Microsoft\PhpParser\Node\AttributeGroup;
910
use Microsoft\PhpParser\Node\ClassBaseClause;
1011
use Microsoft\PhpParser\Node\ClassInterfaceClause;
1112
use Microsoft\PhpParser\Node\ClassMembersNode;
@@ -19,6 +20,9 @@ class ObjectCreationExpression extends Expression {
1920
/** @var Token */
2021
public $newKeword;
2122

23+
/** @var AttributeGroup[]|null optional attributes of an anonymous class. */
24+
public $attributes;
25+
2226
/** @var QualifiedName|Variable|Token */
2327
public $classTypeDesignator;
2428

@@ -41,7 +45,8 @@ class ObjectCreationExpression extends Expression {
4145
public $classMembers;
4246

4347
const CHILD_NAMES = [
44-
'newKeword', // TODO
48+
'newKeword',
49+
'attributes',
4550
'classTypeDesignator',
4651
'openParen',
4752
'argumentExpressionList',

src/Parser.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3088,6 +3088,12 @@ private function parseObjectCreationExpression($parentNode) {
30883088
// TODO - add tests for this scenario
30893089
$oldIsParsingObjectCreationExpression = $this->isParsingObjectCreationExpression;
30903090
$this->isParsingObjectCreationExpression = true;
3091+
3092+
if ($this->getCurrentToken()->kind === TokenKind::AttributeToken) {
3093+
// Attributes such as `new #[MyAttr] class` can only be used with anonymous class declarations.
3094+
// But handle this like $objectCreationExpression->classMembers and leave it up to the applications to detect the invalid combination.
3095+
$objectCreationExpression->attributes = $this->parseAttributeGroups($objectCreationExpression);
3096+
}
30913097
$objectCreationExpression->classTypeDesignator =
30923098
$this->eatOptional1(TokenKind::ClassKeyword) ??
30933099
$this->parseExpression($objectCreationExpression);

tests/cases/parser/callExpression14.php.tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"kind": "NewKeyword",
2828
"textLength": 3
2929
},
30+
"attributes": null,
3031
"classTypeDesignator": {
3132
"QualifiedName": {
3233
"globalSpecifier": null,

tests/cases/parser/listExpression9.php.tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
"kind": "NewKeyword",
129129
"textLength": 3
130130
},
131+
"attributes": null,
131132
"classTypeDesignator": {
132133
"QualifiedName": {
133134
"globalSpecifier": null,

tests/cases/parser/objectCreationExpression1.php.tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"kind": "NewKeyword",
2020
"textLength": 3
2121
},
22+
"attributes": null,
2223
"classTypeDesignator": {
2324
"QualifiedName": {
2425
"globalSpecifier": null,

tests/cases/parser/objectCreationExpression10.php.tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"kind": "NewKeyword",
3636
"textLength": 3
3737
},
38+
"attributes": null,
3839
"classTypeDesignator": {
3940
"kind": "ClassKeyword",
4041
"textLength": 5

tests/cases/parser/objectCreationExpression11.php.tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"kind": "NewKeyword",
3636
"textLength": 3
3737
},
38+
"attributes": null,
3839
"classTypeDesignator": {
3940
"kind": "ClassKeyword",
4041
"textLength": 5

tests/cases/parser/objectCreationExpression12.php.tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"kind": "NewKeyword",
3636
"textLength": 3
3737
},
38+
"attributes": null,
3839
"classTypeDesignator": {
3940
"kind": "ClassKeyword",
4041
"textLength": 5

tests/cases/parser/objectCreationExpression13.php.tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"kind": "NewKeyword",
3636
"textLength": 3
3737
},
38+
"attributes": null,
3839
"classTypeDesignator": {
3940
"QualifiedName": {
4041
"globalSpecifier": null,

tests/cases/parser/objectCreationExpression14.php.tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"kind": "NewKeyword",
3636
"textLength": 3
3737
},
38+
"attributes": null,
3839
"classTypeDesignator": {
3940
"QualifiedName": {
4041
"globalSpecifier": null,

0 commit comments

Comments
 (0)