Skip to content

Commit 7fb5e37

Browse files
committed
list + better error message
1 parent 00bfd14 commit 7fb5e37

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

src/parser/class.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -253,23 +253,26 @@ module.exports = {
253253
let name = null;
254254
let value = null;
255255

256-
if (
257-
this.version >= 803 &&
258-
type &&
259-
(type.kind === "typereference" || type.kind === "uniontype")
260-
) {
256+
if (type && type.kind === "name") {
257+
constName = this.node("identifier");
258+
constName = constName(type.name);
259+
type = null;
260+
value = this.next().read_expr();
261+
} else {
261262
constName = this.node("identifier");
262263
name = this.text();
263264
constName = constName(name);
264265
this.next();
265266
this.expect("=");
266267
value = this.next().read_expr();
267-
} else if (type && type.kind === "name") {
268-
constName = this.node("identifier");
269-
constName = constName(type.name);
270-
type = null;
271-
value = this.next().read_expr();
272268
}
269+
270+
if (this.version < 803 && type !== null) {
271+
this.raiseError(
272+
"Parse Error: Typed Class Constants requires PHP 8.3+",
273+
);
274+
}
275+
273276
return result(constName, value, nullable, type);
274277
},
275278
",",

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@ Program {
13211321
"attrGroups": [],
13221322
"body": [
13231323
ClassConstant {
1324-
"attrGroups": [],
1324+
"attrGroups": undefined,
13251325
"constants": [
13261326
Constant {
13271327
"kind": "constant",
@@ -1340,7 +1340,7 @@ Program {
13401340
],
13411341
"final": false,
13421342
"kind": "classconstant",
1343-
"nullable": false,
1343+
"nullable": [],
13441344
"type": null,
13451345
"visibility": "",
13461346
},
@@ -1433,7 +1433,7 @@ Program {
14331433
"visibility": "public",
14341434
},
14351435
ClassConstant {
1436-
"attrGroups": [],
1436+
"attrGroups": undefined,
14371437
"constants": [
14381438
Constant {
14391439
"kind": "constant",
@@ -1452,7 +1452,7 @@ Program {
14521452
],
14531453
"final": false,
14541454
"kind": "classconstant",
1455-
"nullable": false,
1455+
"nullable": [],
14561456
"type": null,
14571457
"visibility": "",
14581458
},

test/snapshot/classconstant.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,15 @@ describe("classconstant", () => {
6363
),
6464
).toThrowErrorMatchingSnapshot();
6565
});
66+
67+
it("accept the constant name 'list'", () => {
68+
expect(
69+
parser.parseEval(
70+
`class Foo {
71+
const list = "Hello world!";
72+
}`,
73+
{ parser: { version: 803 } },
74+
),
75+
).toMatchSnapshot();
76+
});
6677
});

0 commit comments

Comments
 (0)