Skip to content

Commit 136200e

Browse files
authored
Fix for expression in new statement (#1020)
1 parent dacca25 commit 136200e

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

src/parser/expr.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,16 @@ module.exports = {
745745
const result = this.node("new");
746746
this.expect(this.tok.T_NEW) && this.next();
747747
let args = [];
748+
if (this.token === "(") {
749+
this.next();
750+
const newExp = this.read_expr();
751+
this.expect(")");
752+
this.next();
753+
if (this.token === "(") {
754+
args = this.read_argument_list();
755+
}
756+
return result(newExp, args);
757+
}
748758
const attrs = this.read_attr_list();
749759
if (this.token === this.tok.T_CLASS) {
750760
const what = this.node("class");

test/snapshot/__snapshots__/new.test.js.snap

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,122 @@ Program {
338338
}
339339
`;
340340

341+
exports[`new result from function 1`] = `
342+
Program {
343+
"children": [
344+
ExpressionStatement {
345+
"expression": Assign {
346+
"kind": "assign",
347+
"left": Variable {
348+
"curly": false,
349+
"kind": "variable",
350+
"name": "a",
351+
},
352+
"operator": "=",
353+
"right": New {
354+
"arguments": [],
355+
"kind": "new",
356+
"what": Call {
357+
"arguments": [
358+
String {
359+
"isDoubleQuote": false,
360+
"kind": "string",
361+
"raw": "'d'",
362+
"unicode": false,
363+
"value": "d",
364+
},
365+
],
366+
"kind": "call",
367+
"what": Call {
368+
"arguments": [
369+
String {
370+
"isDoubleQuote": false,
371+
"kind": "string",
372+
"raw": "'c'",
373+
"unicode": false,
374+
"value": "c",
375+
},
376+
],
377+
"kind": "call",
378+
"what": Name {
379+
"kind": "name",
380+
"name": "b",
381+
"resolution": "uqn",
382+
},
383+
},
384+
},
385+
},
386+
},
387+
"kind": "expressionstatement",
388+
},
389+
],
390+
"errors": [],
391+
"kind": "program",
392+
}
393+
`;
394+
395+
exports[`new result from function with arguments 1`] = `
396+
Program {
397+
"children": [
398+
ExpressionStatement {
399+
"expression": Assign {
400+
"kind": "assign",
401+
"left": Variable {
402+
"curly": false,
403+
"kind": "variable",
404+
"name": "a",
405+
},
406+
"operator": "=",
407+
"right": New {
408+
"arguments": [
409+
String {
410+
"isDoubleQuote": false,
411+
"kind": "string",
412+
"raw": "'e'",
413+
"unicode": false,
414+
"value": "e",
415+
},
416+
],
417+
"kind": "new",
418+
"what": Call {
419+
"arguments": [
420+
String {
421+
"isDoubleQuote": false,
422+
"kind": "string",
423+
"raw": "'d'",
424+
"unicode": false,
425+
"value": "d",
426+
},
427+
],
428+
"kind": "call",
429+
"what": Call {
430+
"arguments": [
431+
String {
432+
"isDoubleQuote": false,
433+
"kind": "string",
434+
"raw": "'c'",
435+
"unicode": false,
436+
"value": "c",
437+
},
438+
],
439+
"kind": "call",
440+
"what": Name {
441+
"kind": "name",
442+
"name": "b",
443+
"resolution": "uqn",
444+
},
445+
},
446+
},
447+
},
448+
},
449+
"kind": "expressionstatement",
450+
},
451+
],
452+
"errors": [],
453+
"kind": "program",
454+
}
455+
`;
456+
341457
exports[`new self 1`] = `
342458
Program {
343459
"children": [

test/snapshot/new.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,10 @@ describe("new", function () {
7979
)
8080
).toMatchSnapshot();
8181
});
82+
it("result from function", function () {
83+
expect(parser.parseEval("$a = new (b('c')('d'));")).toMatchSnapshot();
84+
});
85+
it("result from function with arguments", function () {
86+
expect(parser.parseEval("$a = new (b('c')('d'))('e');")).toMatchSnapshot();
87+
});
8288
});

0 commit comments

Comments
 (0)