diff --git a/src/parser/expr.js b/src/parser/expr.js index 1e280d5b9..538fffdbd 100644 --- a/src/parser/expr.js +++ b/src/parser/expr.js @@ -97,6 +97,15 @@ module.exports = { if (this.token === this.tok.T_SPACESHIP) { return result("bin", "<=>", expr, this.next().read_expr()); } + if (this.token === this.tok.T_OBJECT_OPERATOR) { + if (this.version < 804) { + this.raiseError( + "New without parenthesis is not allowed before PHP 8.4", + ); + } + return result("bin", "->", expr, this.next().read_expr()); + } + if (this.token === this.tok.T_INSTANCEOF) { expr = result( "bin", diff --git a/test/snapshot/__snapshots__/class.test.js.snap b/test/snapshot/__snapshots__/class.test.js.snap index 6fd21a3aa..d5c328eb3 100644 --- a/test/snapshot/__snapshots__/class.test.js.snap +++ b/test/snapshot/__snapshots__/class.test.js.snap @@ -1,5 +1,39 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Test classes 8.4 allow new without parenthesis 1`] = ` +Program { + "children": [ + ExpressionStatement { + "expression": Bin { + "kind": "bin", + "left": New { + "arguments": [], + "kind": "new", + "what": Name { + "kind": "name", + "name": "People", + "resolution": "uqn", + }, + }, + "right": Call { + "arguments": [], + "kind": "call", + "what": Name { + "kind": "name", + "name": "name", + "resolution": "uqn", + }, + }, + "type": "->", + }, + "kind": "expressionstatement", + }, + ], + "errors": [], + "kind": "program", +} +`; + exports[`Test classes Advanced tests 1`] = ` Program { "children": [ @@ -1958,6 +1992,8 @@ Program { } `; +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"`; + exports[`Test classes readonly class in PHP8.2 should support abstract readonly 1`] = ` Program { "children": [ diff --git a/test/snapshot/class.test.js b/test/snapshot/class.test.js index b93d642cb..ac0d31a0d 100644 --- a/test/snapshot/class.test.js +++ b/test/snapshot/class.test.js @@ -256,6 +256,23 @@ describe("Test classes", function () { ).toMatchSnapshot(); }); + it("8.4 allow new without parenthesis", () => { + const code = `new People()->name();`; + const test_parser = parser.create({ + parser: { + version: "8.4", + }, + }); + expect(test_parser.parseEval(code)).toMatchSnapshot(); + }); + + it("new without parenthesis throw errors in PHP < 8.4", () => { + const code = `new People()->name();`; + expect(() => { + parser.parseEval(code); + }).toThrowErrorMatchingSnapshot(); + }); + it("knows where a function definiton starts", function () { const phpCode = ` class b {