Skip to content

Commit

Permalink
8.4 Allow new without parenthesis
Browse files Browse the repository at this point in the history
  • Loading branch information
genintho committed Dec 2, 2024
1 parent d9cb05e commit 496b68d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/parser/expr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
36 changes: 36 additions & 0 deletions test/snapshot/__snapshots__/class.test.js.snap
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down Expand Up @@ -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": [
Expand Down
17 changes: 17 additions & 0 deletions test/snapshot/class.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 496b68d

Please sign in to comment.