Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Typed Class Constants parsing #1140

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
38 changes: 23 additions & 15 deletions src/parser/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,6 @@ module.exports = {
this.next();
}

const [nullable, type] =
this.version >= 830 ? this.read_optional_type() : [false, null];

const result = this.node("classconstant");
const items = this.read_list(
/*
Expand All @@ -249,28 +246,39 @@ module.exports = {
*/
function read_constant_declaration() {
const result = this.node("constant");
const nullable = false;

let type = this.read_types();
let constName = null;
let name = null;
let value = null;
if (
this.token === this.tok.T_STRING ||
(this.version >= 700 && this.is("IDENTIFIER"))
) {

if (type && type.kind === "name") {
constName = this.node("identifier");
const name = this.text();
this.next();
constName = constName(name);
constName = constName(type.name);
type = null;
value = this.next().read_expr();
} else {
this.expect("IDENTIFIER");
}
if (this.expect("=")) {
constName = this.node("identifier");
name = this.text();
constName = constName(name);
this.next();
this.expect("=");
value = this.next().read_expr();
}
return result(constName, value);

if (this.version < 803 && type !== null) {
this.raiseError(
"Parse Error: Typed Class Constants requires PHP 8.3+",
);
}

return result(constName, value, nullable, type);
},
",",
);

return result(null, items, flags, nullable, type, attrs || []);
return result(null, items, flags, attrs || []);
},
/*
* Read member flags
Expand Down
48 changes: 17 additions & 31 deletions test/snapshot/__snapshots__/acid.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -755,23 +755,22 @@ Program {
],
},
ClassConstant {
"attrGroups": [],
"attrGroups": Location {
"end": Position {
"column": 27,
"line": 31,
"offset": 561,
},
"source": "FOOBAR = 'BARFOO'",
"start": Position {
"column": 10,
"line": 31,
"offset": 544,
},
},
"constants": [
Constant {
"kind": "constant",
"loc": Location {
"end": Position {
"column": 27,
"line": 31,
"offset": 561,
},
"source": "FOOBAR = 'BARFOO'",
"start": Position {
"column": 10,
"line": 31,
"offset": 544,
},
},
"name": Identifier {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks as though the source code location is lost during parsing as well

"kind": "identifier",
"loc": Location {
Expand All @@ -780,11 +779,11 @@ Program {
"line": 31,
"offset": 550,
},
"source": "FOOBAR",
"source": " ",
"start": Position {
"column": 10,
"column": 17,
"line": 31,
"offset": 544,
"offset": 551,
},
},
"name": "FOOBAR",
Expand Down Expand Up @@ -813,20 +812,7 @@ Program {
],
"final": false,
"kind": "classconstant",
"loc": Location {
"end": Position {
"column": 27,
"line": 31,
"offset": 561,
},
"source": "FOOBAR = 'BARFOO'",
"start": Position {
"column": 10,
"line": 31,
"offset": 544,
},
},
"nullable": false,
"nullable": [],
"type": null,
"visibility": "",
},
Expand Down
52 changes: 26 additions & 26 deletions test/snapshot/__snapshots__/attributes.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -308,18 +308,7 @@ Program {
"attrGroups": [],
"body": [
ClassConstant {
"attrGroups": [
AttrGroup {
"attrs": [
Attribute {
"args": [],
"kind": "attribute",
"name": "B",
},
],
"kind": "attrgroup",
},
],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attribute groups are lost too

"attrGroups": undefined,
"constants": [
Constant {
"kind": "constant",
Expand All @@ -335,7 +324,18 @@ Program {
],
"final": false,
"kind": "classconstant",
"nullable": false,
"nullable": [
AttrGroup {
"attrs": [
Attribute {
"args": [],
"kind": "attribute",
"name": "B",
},
],
"kind": "attrgroup",
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not make sense either, nullable should be a bool value? looks like attrGroups and nullable are switched around somewhere

],
"type": null,
"visibility": "",
},
Expand Down Expand Up @@ -499,18 +499,7 @@ Program {
],
"body": [
ClassConstant {
"attrGroups": [
AttrGroup {
"attrs": [
Attribute {
"args": [],
"kind": "attribute",
"name": "C",
},
],
"kind": "attrgroup",
},
],
"attrGroups": undefined,
"constants": [
Constant {
"kind": "constant",
Expand All @@ -526,7 +515,18 @@ Program {
],
"final": false,
"kind": "classconstant",
"nullable": false,
"nullable": [
AttrGroup {
"attrs": [
Attribute {
"args": [],
"kind": "attribute",
"name": "C",
},
],
"kind": "attrgroup",
},
],
"type": null,
"visibility": "",
},
Expand Down
22 changes: 10 additions & 12 deletions test/snapshot/__snapshots__/class.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Program {
"attrGroups": [],
"body": [
ClassConstant {
"attrGroups": [],
"attrGroups": undefined,
"constants": [
Constant {
"kind": "constant",
Expand All @@ -164,16 +164,15 @@ Program {
],
"final": false,
"kind": "classconstant",
"leadingComments": [
"nullable": [],
"type": [
CommentLine {
"kind": "commentline",
"offset": 332,
"value": "// some doc
",
},
],
"nullable": false,
"type": null,
"visibility": "",
},
Method {
Expand Down Expand Up @@ -218,7 +217,7 @@ Program {
Trait {
"body": [
ClassConstant {
"attrGroups": [],
"attrGroups": undefined,
"constants": [
Constant {
"kind": "constant",
Expand All @@ -234,16 +233,15 @@ Program {
],
"final": false,
"kind": "classconstant",
"leadingComments": [
"nullable": [],
"type": [
CommentLine {
"kind": "commentline",
"offset": 455,
"value": "// some doc
",
},
],
"nullable": false,
"type": null,
"visibility": "",
},
Method {
Expand Down Expand Up @@ -1323,7 +1321,7 @@ Program {
"attrGroups": [],
"body": [
ClassConstant {
"attrGroups": [],
"attrGroups": undefined,
"constants": [
Constant {
"kind": "constant",
Expand All @@ -1342,7 +1340,7 @@ Program {
],
"final": false,
"kind": "classconstant",
"nullable": false,
"nullable": [],
"type": null,
"visibility": "",
},
Expand Down Expand Up @@ -1435,7 +1433,7 @@ Program {
"visibility": "public",
},
ClassConstant {
"attrGroups": [],
"attrGroups": undefined,
"constants": [
Constant {
"kind": "constant",
Expand All @@ -1454,7 +1452,7 @@ Program {
],
"final": false,
"kind": "classconstant",
"nullable": false,
"nullable": [],
"type": null,
"visibility": "",
},
Expand Down
Loading
Loading