Skip to content

Commit 1f8042d

Browse files
authored
8.4 Allow new without parenthesis (glayzzle#1146)
1 parent b1168db commit 1f8042d

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

src/parser/expr.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ module.exports = {
9797
if (this.token === this.tok.T_SPACESHIP) {
9898
return result("bin", "<=>", expr, this.next().read_expr());
9999
}
100+
if (this.token === this.tok.T_OBJECT_OPERATOR) {
101+
if (this.version < 804) {
102+
this.raiseError(
103+
"New without parenthesis is not allowed before PHP 8.4",
104+
);
105+
}
106+
return result("bin", "->", expr, this.next().read_expr());
107+
}
108+
100109
if (this.token === this.tok.T_INSTANCEOF) {
101110
expr = result(
102111
"bin",

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`Test classes 8.4 allow new without parenthesis 1`] = `
4+
Program {
5+
"children": [
6+
ExpressionStatement {
7+
"expression": Bin {
8+
"kind": "bin",
9+
"left": New {
10+
"arguments": [],
11+
"kind": "new",
12+
"what": Name {
13+
"kind": "name",
14+
"name": "People",
15+
"resolution": "uqn",
16+
},
17+
},
18+
"right": Call {
19+
"arguments": [],
20+
"kind": "call",
21+
"what": Name {
22+
"kind": "name",
23+
"name": "name",
24+
"resolution": "uqn",
25+
},
26+
},
27+
"type": "->",
28+
},
29+
"kind": "expressionstatement",
30+
},
31+
],
32+
"errors": [],
33+
"kind": "program",
34+
}
35+
`;
36+
337
exports[`Test classes Advanced tests 1`] = `
438
Program {
539
"children": [
@@ -1958,6 +1992,8 @@ Program {
19581992
}
19591993
`;
19601994

1995+
exports[`Test classes new without parenthesis throw errors in PHP < 8.4 1`] = `"New without parenthesis is not allowed before PHP 8.4 on line 1"`;
1996+
19611997
exports[`Test classes readonly class in PHP8.2 should support abstract readonly 1`] = `
19621998
Program {
19631999
"children": [

test/snapshot/class.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,23 @@ describe("Test classes", function () {
256256
).toMatchSnapshot();
257257
});
258258

259+
it("8.4 allow new without parenthesis", () => {
260+
const code = `new People()->name();`;
261+
const test_parser = parser.create({
262+
parser: {
263+
version: "8.4",
264+
},
265+
});
266+
expect(test_parser.parseEval(code)).toMatchSnapshot();
267+
});
268+
269+
it("new without parenthesis throw errors in PHP < 8.4", () => {
270+
const code = `new People()->name();`;
271+
expect(() => {
272+
parser.parseEval(code);
273+
}).toThrowErrorMatchingSnapshot();
274+
});
275+
259276
it("knows where a function definiton starts", function () {
260277
const phpCode = `
261278
class b {

0 commit comments

Comments
 (0)