Skip to content

Commit

Permalink
Merge pull request microsoft#409 from phpactor/readonly-attr
Browse files Browse the repository at this point in the history
bugfix: Support attributes on readonly classes
  • Loading branch information
roblourens authored Jan 29, 2024
2 parents 8781df1 + c660589 commit 4fd2db9
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ private function parseAttributeStatement($parentNode) {
$statement = $this->parseInterfaceElementFn()($parentNode);
} else {
// Classlikes, anonymous functions, global functions, and arrow functions can have attributes. Global constants cannot.
if (in_array($this->token->kind, [TokenKind::ClassKeyword, TokenKind::TraitKeyword, TokenKind::InterfaceKeyword, TokenKind::AbstractKeyword, TokenKind::FinalKeyword, TokenKind::FunctionKeyword, TokenKind::FnKeyword, TokenKind::EnumKeyword], true) ||
if (in_array($this->token->kind, [TokenKind::ReadonlyKeyword, TokenKind::ClassKeyword, TokenKind::TraitKeyword, TokenKind::InterfaceKeyword, TokenKind::AbstractKeyword, TokenKind::FinalKeyword, TokenKind::FunctionKeyword, TokenKind::FnKeyword, TokenKind::EnumKeyword], true) ||
$this->token->kind === TokenKind::StaticKeyword && $this->lookahead([TokenKind::FunctionKeyword, TokenKind::FnKeyword])) {
$statement = $this->parseStatement($parentNode);
} else {
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/parser81/readonly_class_attribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
#[Foobar]readonly class A {}
1 change: 1 addition & 0 deletions tests/cases/parser81/readonly_class_attribute.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
91 changes: 91 additions & 0 deletions tests/cases/parser81/readonly_class_attribute.php.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
"SourceFileNode": {
"statementList": [
{
"InlineHtml": {
"scriptSectionEndTag": null,
"text": null,
"scriptSectionStartTag": {
"kind": "ScriptSectionStartTag",
"textLength": 6
}
}
},
{
"ClassDeclaration": {
"attributes": [
{
"AttributeGroup": {
"startToken": {
"kind": "AttributeToken",
"textLength": 2
},
"attributes": {
"AttributeElementList": {
"children": [
{
"Attribute": {
"name": {
"QualifiedName": {
"globalSpecifier": null,
"relativeSpecifier": null,
"nameParts": [
{
"kind": "Name",
"textLength": 6
}
]
}
},
"openParen": null,
"argumentExpressionList": null,
"closeParen": null
}
}
]
}
},
"endToken": {
"kind": "CloseBracketToken",
"textLength": 1
}
}
}
],
"abstractOrFinalModifier": {
"kind": "ReadonlyKeyword",
"textLength": 8
},
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
},
"name": {
"kind": "Name",
"textLength": 1
},
"classBaseClause": null,
"classInterfaceClause": null,
"classMembers": {
"ClassMembersNode": {
"openBrace": {
"kind": "OpenBraceToken",
"textLength": 1
},
"classMemberDeclarations": [],
"closeBrace": {
"kind": "CloseBraceToken",
"textLength": 1
}
}
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}

0 comments on commit 4fd2db9

Please sign in to comment.