Skip to content
Merged
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
11 changes: 6 additions & 5 deletions src/parser/expr.js
Original file line number Diff line number Diff line change
Expand Up @@ -730,28 +730,29 @@ module.exports = {
},

read_attribute() {
const node = this.node("attribute");
const name = this.text();
let args = [];
this.next();
if (this.token === "(") {
args = this.read_argument_list();
}
return this.node("attribute")(name, args);
return node(name, args);
},
read_attr_list() {
const list = [];
if (this.token === this.tok.T_ATTRIBUTE) {
do {
const attrGr = this.node("attrgroup")([]);
const node = this.node("attrgroup");
this.next();
attrGr.attrs.push(this.read_attribute());
const attrs = [this.read_attribute()];
while (this.token === ",") {
this.next();
if (this.token !== "]") attrGr.attrs.push(this.read_attribute());
if (this.token !== "]") attrs.push(this.read_attribute());
}
list.push(attrGr);
this.expect("]");
this.next();
list.push(node(attrs));
} while (this.token === this.tok.T_ATTRIBUTE);
}
return list;
Expand Down
4 changes: 2 additions & 2 deletions test/snapshot/__snapshots__/attributes.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1056,14 +1056,14 @@ Program {
Attribute {
"args": [],
"kind": "attribute",
"leadingComments": [
"name": "Pure",
"trailingComments": [
CommentBlock {
"kind": "commentblock",
"offset": 26,
"value": "/* Pure */",
},
],
"name": "Pure",
},
],
"kind": "attrgroup",
Expand Down
302 changes: 302 additions & 0 deletions test/snapshot/__snapshots__/location.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2487,6 +2487,308 @@ Program {
}
`;

exports[`Test locations test attrgroup 1`] = `
Program {
"children": [
_Function {
"arguments": [],
"attrGroups": [
AttrGroup {
"attrs": [
Attribute {
"args": [],
"kind": "attribute",
"loc": Location {
"end": Position {
"column": 6,
"line": 1,
"offset": 6,
},
"source": "Pure",
"start": Position {
"column": 2,
"line": 1,
"offset": 2,
},
},
"name": "Pure",
},
],
"kind": "attrgroup",
"loc": Location {
"end": Position {
"column": 7,
"line": 1,
"offset": 7,
},
"source": "#[Pure]",
"start": Position {
"column": 0,
"line": 1,
"offset": 0,
},
},
},
AttrGroup {
"attrs": [
Attribute {
"args": [],
"kind": "attribute",
"loc": Location {
"end": Position {
"column": 20,
"line": 1,
"offset": 20,
},
"source": "Deprecated",
"start": Position {
"column": 10,
"line": 1,
"offset": 10,
},
},
"name": "Deprecated",
},
],
"kind": "attrgroup",
"loc": Location {
"end": Position {
"column": 21,
"line": 1,
"offset": 21,
},
"source": "#[Deprecated]",
"start": Position {
"column": 8,
"line": 1,
"offset": 8,
},
},
},
],
"body": Block {
"children": [],
"kind": "block",
"loc": Location {
"end": Position {
"column": 39,
"line": 1,
"offset": 39,
},
"source": "{}",
"start": Position {
"column": 37,
"line": 1,
"offset": 37,
},
},
},
"byref": false,
"kind": "function",
"loc": Location {
"end": Position {
"column": 39,
"line": 1,
"offset": 39,
},
"source": "function foo()",
"start": Position {
"column": 22,
"line": 1,
"offset": 22,
},
},
"name": Identifier {
"kind": "identifier",
"loc": Location {
"end": Position {
"column": 34,
"line": 1,
"offset": 34,
},
"source": "foo",
"start": Position {
"column": 31,
"line": 1,
"offset": 31,
},
},
"name": "foo",
},
"nullable": false,
"type": null,
},
],
"errors": [],
"kind": "program",
"loc": Location {
"end": Position {
"column": 39,
"line": 1,
"offset": 39,
},
"source": "#[Pure] #[Deprecated] function foo() {}",
"start": Position {
"column": 0,
"line": 1,
"offset": 0,
},
},
}
`;

exports[`Test locations test attribute 1`] = `
Program {
"children": [
_Function {
"arguments": [],
"attrGroups": [
AttrGroup {
"attrs": [
Attribute {
"args": [
namedargument {
"kind": "namedargument",
"loc": Location {
"end": Position {
"column": 32,
"line": 1,
"offset": 32,
},
"source": "reason: 'since 5.2'",
"start": Position {
"column": 13,
"line": 1,
"offset": 13,
},
},
"name": "reason",
"value": String {
"isDoubleQuote": false,
"kind": "string",
"loc": Location {
"end": Position {
"column": 32,
"line": 1,
"offset": 32,
},
"source": "'since 5.2'",
"start": Position {
"column": 21,
"line": 1,
"offset": 21,
},
},
"raw": "'since 5.2'",
"unicode": false,
"value": "since 5.2",
},
},
],
"kind": "attribute",
"loc": Location {
"end": Position {
"column": 33,
"line": 1,
"offset": 33,
},
"source": "Deprecated(reason: 'since 5.2')",
"start": Position {
"column": 2,
"line": 1,
"offset": 2,
},
},
"name": "Deprecated",
},
],
"kind": "attrgroup",
"loc": Location {
"end": Position {
"column": 34,
"line": 1,
"offset": 34,
},
"source": "#[Deprecated(reason: 'since 5.2')]",
"start": Position {
"column": 0,
"line": 1,
"offset": 0,
},
},
},
],
"body": Block {
"children": [],
"kind": "block",
"loc": Location {
"end": Position {
"column": 52,
"line": 1,
"offset": 52,
},
"source": "{}",
"start": Position {
"column": 50,
"line": 1,
"offset": 50,
},
},
},
"byref": false,
"kind": "function",
"loc": Location {
"end": Position {
"column": 52,
"line": 1,
"offset": 52,
},
"source": "function foo()",
"start": Position {
"column": 35,
"line": 1,
"offset": 35,
},
},
"name": Identifier {
"kind": "identifier",
"loc": Location {
"end": Position {
"column": 47,
"line": 1,
"offset": 47,
},
"source": "foo",
"start": Position {
"column": 44,
"line": 1,
"offset": 44,
},
},
"name": "foo",
},
"nullable": false,
"type": null,
},
],
"errors": [],
"kind": "program",
"loc": Location {
"end": Position {
"column": 52,
"line": 1,
"offset": 52,
},
"source": "#[Deprecated(reason: 'since 5.2')] function foo() {}",
"start": Position {
"column": 0,
"line": 1,
"offset": 0,
},
},
}
`;

exports[`Test locations test bin 1`] = `
Program {
"children": [
Expand Down
2 changes: 2 additions & 0 deletions test/snapshot/location.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ string";`,
["intersection type return", "function foo(): A&B { return 1; }"],
["union type parameter", "function foo(string|int $bar) {}"],
["dnf type parameter", "function foo((A&B)|null $bar) {}"],
["attribute", "#[Deprecated(reason: 'since 5.2')] function foo() {}"],
["attrgroup", "#[Pure] #[Deprecated] function foo() {}"],
])("test %s", (_, code) => {
expect(
parser.parseEval(code, {
Expand Down