Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/parser/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ module.exports = {
return [nullable, type];
},

peekSkipComments: function () {
peekSkipComments() {
const lexerState = this.lexer.getState();
let nextToken;

Expand Down
16 changes: 7 additions & 9 deletions src/parser/expr.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,6 @@ 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(
Expand Down Expand Up @@ -359,7 +351,13 @@ module.exports = {
return this.node("pre")("-", this.next().read_variable(false, false));

case this.tok.T_NEW:
return this.read_new_expr();
expr = this.read_new_expr();
if (this.token === this.tok.T_OBJECT_OPERATOR && this.version < 804) {
this.raiseError(
"New without parenthesis is not allowed before PHP 8.4",
);
}
return this.handleDereferencable(expr);

case this.tok.T_ISSET:
case this.tok.T_EMPTY:
Expand Down
54 changes: 40 additions & 14 deletions test/snapshot/__snapshots__/class.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,30 +1,56 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

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",
"expression": Call {
"arguments": [],
"kind": "call",
"what": PropertyLookup {
"kind": "propertylookup",
"offset": Identifier {
"kind": "identifier",
"name": "name",
},
"what": New {
"arguments": [],
"kind": "new",
"what": Name {
"kind": "name",
"name": "People",
"resolution": "uqn",
},
},
},
},
"kind": "expressionstatement",
},
],
"errors": [],
"kind": "program",
}
`;

exports[`Test classes 8.4 new without parenthesis with array lookup 1`] = `
Program {
"children": [
ExpressionStatement {
"expression": OffsetLookup {
"kind": "offsetlookup",
"offset": Number {
"kind": "number",
"value": "0",
},
"right": Call {
"what": New {
"arguments": [],
"kind": "call",
"kind": "new",
"what": Name {
"kind": "name",
"name": "name",
"name": "People",
"resolution": "uqn",
},
},
"type": "->",
},
"kind": "expressionstatement",
},
Expand Down
17 changes: 16 additions & 1 deletion test/snapshot/class.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,25 @@ describe("Test classes", function () {
expect(test_parser.parseEval(code)).toMatchSnapshot();
});

it("8.4 new without parenthesis with array lookup", () => {
const code = `new People()[0];`;
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();`;
const test_parser = parser.create({
parser: {
version: "8.3",
},
});
expect(() => {
parser.parseEval(code);
test_parser.parseEval(code);
}).toThrowErrorMatchingSnapshot();
});

Expand Down