Skip to content

Commit

Permalink
Fix for expression in new statement (glayzzle#1020)
Browse files Browse the repository at this point in the history
  • Loading branch information
cseufert authored Oct 20, 2022
1 parent dacca25 commit 136200e
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/parser/expr.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,16 @@ module.exports = {
const result = this.node("new");
this.expect(this.tok.T_NEW) && this.next();
let args = [];
if (this.token === "(") {
this.next();
const newExp = this.read_expr();
this.expect(")");
this.next();
if (this.token === "(") {
args = this.read_argument_list();
}
return result(newExp, args);
}
const attrs = this.read_attr_list();
if (this.token === this.tok.T_CLASS) {
const what = this.node("class");
Expand Down
116 changes: 116 additions & 0 deletions test/snapshot/__snapshots__/new.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,122 @@ Program {
}
`;

exports[`new result from function 1`] = `
Program {
"children": [
ExpressionStatement {
"expression": Assign {
"kind": "assign",
"left": Variable {
"curly": false,
"kind": "variable",
"name": "a",
},
"operator": "=",
"right": New {
"arguments": [],
"kind": "new",
"what": Call {
"arguments": [
String {
"isDoubleQuote": false,
"kind": "string",
"raw": "'d'",
"unicode": false,
"value": "d",
},
],
"kind": "call",
"what": Call {
"arguments": [
String {
"isDoubleQuote": false,
"kind": "string",
"raw": "'c'",
"unicode": false,
"value": "c",
},
],
"kind": "call",
"what": Name {
"kind": "name",
"name": "b",
"resolution": "uqn",
},
},
},
},
},
"kind": "expressionstatement",
},
],
"errors": [],
"kind": "program",
}
`;

exports[`new result from function with arguments 1`] = `
Program {
"children": [
ExpressionStatement {
"expression": Assign {
"kind": "assign",
"left": Variable {
"curly": false,
"kind": "variable",
"name": "a",
},
"operator": "=",
"right": New {
"arguments": [
String {
"isDoubleQuote": false,
"kind": "string",
"raw": "'e'",
"unicode": false,
"value": "e",
},
],
"kind": "new",
"what": Call {
"arguments": [
String {
"isDoubleQuote": false,
"kind": "string",
"raw": "'d'",
"unicode": false,
"value": "d",
},
],
"kind": "call",
"what": Call {
"arguments": [
String {
"isDoubleQuote": false,
"kind": "string",
"raw": "'c'",
"unicode": false,
"value": "c",
},
],
"kind": "call",
"what": Name {
"kind": "name",
"name": "b",
"resolution": "uqn",
},
},
},
},
},
"kind": "expressionstatement",
},
],
"errors": [],
"kind": "program",
}
`;

exports[`new self 1`] = `
Program {
"children": [
Expand Down
6 changes: 6 additions & 0 deletions test/snapshot/new.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,10 @@ describe("new", function () {
)
).toMatchSnapshot();
});
it("result from function", function () {
expect(parser.parseEval("$a = new (b('c')('d'));")).toMatchSnapshot();
});
it("result from function with arguments", function () {
expect(parser.parseEval("$a = new (b('c')('d'))('e');")).toMatchSnapshot();
});
});

0 comments on commit 136200e

Please sign in to comment.