Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8.4 Allow new without parenthesis #1146

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading