From b1168db8797dbda0ba890b660d0f74fdf5e88ae1 Mon Sep 17 00:00:00 2001 From: Christian Zosel Date: Tue, 31 Dec 2024 08:29:50 +0100 Subject: [PATCH 1/3] 3.2.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 79f7df9b7..8325be13a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "php-parser", - "version": "3.2.1", + "version": "3.2.2", "description": "Parse PHP code from JS and returns its AST", "main": "src/index.js", "browser": "dist/php-parser.js", From 1f8042d4e9c2b2f6bb64304484aef5c29f707bc9 Mon Sep 17 00:00:00 2001 From: Thomas Genin Date: Thu, 17 Apr 2025 00:25:19 -0700 Subject: [PATCH 2/3] 8.4 Allow new without parenthesis (#1146) --- src/parser/expr.js | 9 +++++ .../snapshot/__snapshots__/class.test.js.snap | 36 +++++++++++++++++++ test/snapshot/class.test.js | 17 +++++++++ 3 files changed, 62 insertions(+) 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 d408ec7d5..068548dfe 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 { From c702357f7435af363051a947b64d1e064cca014b Mon Sep 17 00:00:00 2001 From: Christian Zosel Date: Thu, 17 Apr 2025 09:29:33 +0200 Subject: [PATCH 3/3] 3.2.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8325be13a..d2ac370e6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "php-parser", - "version": "3.2.2", + "version": "3.2.3", "description": "Parse PHP code from JS and returns its AST", "main": "src/index.js", "browser": "dist/php-parser.js",