Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/parser/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ module.exports = {
this.raiseError("Cannot use asymmetric visibility on constants");
}
const constants = this.read_constant_list(flags, attrs, locStart);
attrs = [];
if (this.expect(";")) {
this.next();
}
Expand Down
5 changes: 3 additions & 2 deletions src/parser/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,11 @@ module.exports = {
// optional flags
case this.tok.T_ABSTRACT:
case this.tok.T_FINAL:
case this.tok.T_READ_ONLY:
case this.tok.T_CLASS:
return this.read_class_declaration_statement();
return this.read_class_declaration_statement(attrs);
case this.tok.T_INTERFACE:
return this.read_interface_declaration_statement();
return this.read_interface_declaration_statement(attrs);
case this.tok.T_TRAIT:
return this.read_trait_declaration_statement(attrs);
case this.tok.T_ENUM:
Expand Down
39 changes: 1 addition & 38 deletions test/snapshot/__snapshots__/location.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4180,44 +4180,7 @@ Program {
},
"properties": [
Property {
"attrGroups": [
AttrGroup {
"attrs": [
Attribute {
"args": [],
"kind": "attribute",
"loc": Location {
"end": Position {
"column": 22,
"line": 1,
"offset": 22,
},
"source": "Deprecated",
"start": Position {
"column": 12,
"line": 1,
"offset": 12,
},
},
"name": "Deprecated",
},
],
"kind": "attrgroup",
"loc": Location {
"end": Position {
"column": 23,
"line": 1,
"offset": 23,
},
"source": "#[Deprecated]",
"start": Position {
"column": 10,
"line": 1,
"offset": 10,
},
},
},
],
"attrGroups": [],
"hooks": [],
"kind": "property",
"loc": Location {
Expand Down
43 changes: 43 additions & 0 deletions test/snapshot/attributes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,49 @@ describe("Parse Attributes", () => {
).toMatchSnapshot();
});

it("doesnt repeat attributes from a previous class constant", () => {
const ast = parser.parseEval(`
class A {
#[B]
const C = 1;
const D = 2;
public $e;
function f() {}
}
`);
const [c, d, e, f] = ast.children[0].body;
expect(c.attrGroups).toHaveLength(1);
expect(d.attrGroups).toEqual([]);
expect(e.properties[0].attrGroups).toEqual([]);
expect(f.attrGroups).toEqual([]);
});

it("can parse attributes on declarations in inner statements", () => {
const ast = parser.parseEval(`
if (true) {
#[A]
class B {}
#[C]
final class D {}
#[E]
interface F {}
#[G]
readonly class H {}
}
`);
expect(
ast.children[0].body.children.map((node) => [
node.kind,
node.attrGroups.map((group) => group.attrs[0].name),
]),
).toEqual([
["class", ["A"]],
["class", ["C"]],
["interface", ["E"]],
["class", ["G"]],
]);
});

it("parses attributes as comments for PHP < 8", () => {
expect(
parser.parseEval(
Expand Down