Skip to content

Commit e86e655

Browse files
authored
Fix for readonly keyword ordering (glayzzle#997)
1 parent 7ae945f commit e86e655

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

src/parser/function.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ module.exports = {
257257
let readonly = false;
258258
let attrs = [];
259259
if (this.token === this.tok.T_ATTRIBUTE) attrs = this.read_attr_list();
260-
const flags = this.read_promoted();
261260

262261
if (this.version >= 801 && this.token === this.tok.T_READ_ONLY) {
263262
if (is_class_constructor) {
@@ -270,6 +269,23 @@ module.exports = {
270269
}
271270
}
272271

272+
const flags = this.read_promoted();
273+
274+
if (
275+
!readonly &&
276+
this.version >= 801 &&
277+
this.token === this.tok.T_READ_ONLY
278+
) {
279+
if (is_class_constructor) {
280+
this.next();
281+
readonly = true;
282+
} else {
283+
this.raiseError(
284+
"readonly properties can be used only on class constructor"
285+
);
286+
}
287+
}
288+
273289
if (this.token === "?") {
274290
this.next();
275291
nullable = true;

test/snapshot/__snapshots__/class.test.js.snap

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,61 @@ Program {
10701070
"name": "Bob",
10711071
},
10721072
},
1073+
Class {
1074+
"attrGroups": Array [],
1075+
"body": Array [
1076+
Method {
1077+
"arguments": Array [
1078+
Parameter {
1079+
"attrGroups": Array [],
1080+
"byref": false,
1081+
"flags": 1,
1082+
"kind": "parameter",
1083+
"name": Identifier {
1084+
"kind": "identifier",
1085+
"name": "id",
1086+
},
1087+
"nullable": false,
1088+
"readonly": true,
1089+
"type": TypeReference {
1090+
"kind": "typereference",
1091+
"name": "int",
1092+
"raw": "int",
1093+
},
1094+
"value": null,
1095+
"variadic": false,
1096+
},
1097+
],
1098+
"attrGroups": Array [],
1099+
"body": Block {
1100+
"children": Array [],
1101+
"kind": "block",
1102+
},
1103+
"byref": false,
1104+
"isAbstract": false,
1105+
"isFinal": false,
1106+
"isStatic": false,
1107+
"kind": "method",
1108+
"name": Identifier {
1109+
"kind": "identifier",
1110+
"name": "__construct",
1111+
},
1112+
"nullable": false,
1113+
"type": null,
1114+
"visibility": "public",
1115+
},
1116+
],
1117+
"extends": null,
1118+
"implements": null,
1119+
"isAbstract": false,
1120+
"isAnonymous": false,
1121+
"isFinal": false,
1122+
"kind": "class",
1123+
"name": Identifier {
1124+
"kind": "identifier",
1125+
"name": "Bob2",
1126+
},
1127+
},
10731128
],
10741129
"errors": Array [],
10751130
"kind": "program",

test/snapshot/class.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,11 @@ describe("Test classes", function () {
167167
`
168168
class Bob {
169169
public function __construct(public readonly int $id) {}
170-
}`,
170+
}
171+
class Bob2 {
172+
public function __construct(readonly public int $id) {}
173+
}
174+
`,
171175
{
172176
parser: {
173177
version: "8.1",

0 commit comments

Comments
 (0)