Skip to content

Commit b88d542

Browse files
committed
Adds an always emit of pair of parens in spread syntax in new expression in ES5
1 parent 1be34f1 commit b88d542

File tree

2 files changed

+29
-58
lines changed

2 files changed

+29
-58
lines changed

src/compiler/emitter.ts

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,12 +1853,6 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
18531853
emitListWithSpread(node.arguments, /*alwaysCopy*/ false, /*multiLine*/ false, /*trailingComma*/ false);
18541854
write(")");
18551855
}
1856-
1857-
function emitBracedEnclosedCommaList(nodes: Node[]) {
1858-
write("(");
1859-
emitCommaList(nodes);
1860-
write(")");
1861-
}
18621856

18631857
function emitCallExpression(node: CallExpression) {
18641858
if (languageVersion < ScriptTarget.ES6 && hasSpreadElement(node.arguments)) {
@@ -1871,14 +1865,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
18711865
superCall = true;
18721866
}
18731867
else {
1874-
if (node.expression.kind === SyntaxKind.NewExpression) {
1875-
emitNewExpression(<NewExpression>node.expression, /*emitCallHelper*/true);
1876-
emitBracedEnclosedCommaList(node.arguments);
1877-
return;
1878-
}
1879-
else {
1880-
emit(node.expression);
1881-
}
1868+
emit(node.expression);
18821869
superCall = node.expression.kind === SyntaxKind.PropertyAccessExpression && (<PropertyAccessExpression>node.expression).expression.kind === SyntaxKind.SuperKeyword;
18831870
}
18841871
if (superCall && languageVersion < ScriptTarget.ES6) {
@@ -1891,27 +1878,13 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
18911878
write(")");
18921879
}
18931880
else {
1894-
emitBracedEnclosedCommaList(node.arguments);
1881+
write("(");
1882+
emitCommaList(node.arguments);
1883+
write(")");
18951884
}
18961885
}
1897-
1898-
/** Emit a new expression.
1899-
*
1900-
* Emit call helper:
1901-
* When we are emitting a call expression from a new expressions with spread syntax in ES5
1902-
* we need an argument to help this function decide whether it should emit a pair of extra
1903-
* braces "()" at the end. These extra braces are there to help the expression to not be
1904-
* consumed by the new expression directly.
1905-
*
1906-
* Example:
1907-
*
1908-
* new Array(...arguments)();
1909-
*
1910-
* Should be transpiled into ES5:
1911-
*
1912-
* new (Array.bind.apply(Array, [void 0].concat(arguments)))()()
1913-
*/
1914-
function emitNewExpression(node: NewExpression, emitCallHelper = false) {
1886+
1887+
function emitNewExpression(node: NewExpression) {
19151888
write("new ");
19161889

19171890
// Spread operator logic can be supported in new expressions in ES5 using a combination
@@ -1942,9 +1915,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
19421915
write(", [void 0].concat(");
19431916
emitListWithSpread(node.arguments, /*multiline*/false, /*trailingComma*/false);
19441917
write(")))");
1945-
if (emitCallHelper) {
1946-
write("()");
1947-
}
1918+
write("()");
19481919
}
19491920
else {
19501921
emit(node.expression);

tests/baselines/reference/newWithSpreadES5.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -116,50 +116,50 @@ var h;
116116
var i;
117117
// Basic expression
118118
new f(1, 2, "string");
119-
new (f.bind.apply(f, [void 0].concat([1, 2].concat(a))));
120-
new (f.bind.apply(f, [void 0].concat([1, 2].concat(a, ["string"]))));
119+
new (f.bind.apply(f, [void 0].concat([1, 2].concat(a))))();
120+
new (f.bind.apply(f, [void 0].concat([1, 2].concat(a, ["string"]))))();
121121
// Call expression
122122
new f(1, 2, "string")();
123123
new (f.bind.apply(f, [void 0].concat([1, 2].concat(a))))()();
124124
new (f.bind.apply(f, [void 0].concat([1, 2].concat(a, ["string"]))))()();
125125
// Property access expression
126126
new b.f(1, 2, "string");
127-
new ((_a = b.f).bind.apply(_a, [void 0].concat([1, 2].concat(a))));
128-
new ((_b = b.f).bind.apply(_b, [void 0].concat([1, 2].concat(a, ["string"]))));
127+
new ((_a = b.f).bind.apply(_a, [void 0].concat([1, 2].concat(a))))();
128+
new ((_b = b.f).bind.apply(_b, [void 0].concat([1, 2].concat(a, ["string"]))))();
129129
// Parenthesised expression
130130
new (b.f)(1, 2, "string");
131-
new ((_c = (b.f)).bind.apply(_c, [void 0].concat([1, 2].concat(a))));
132-
new ((_d = (b.f)).bind.apply(_d, [void 0].concat([1, 2].concat(a, ["string"]))));
131+
new ((_c = (b.f)).bind.apply(_c, [void 0].concat([1, 2].concat(a))))();
132+
new ((_d = (b.f)).bind.apply(_d, [void 0].concat([1, 2].concat(a, ["string"]))))();
133133
// Element access expression
134134
new d[1].f(1, 2, "string");
135-
new ((_e = d[1].f).bind.apply(_e, [void 0].concat([1, 2].concat(a))));
136-
new ((_f = d[1].f).bind.apply(_f, [void 0].concat([1, 2].concat(a, ["string"]))));
135+
new ((_e = d[1].f).bind.apply(_e, [void 0].concat([1, 2].concat(a))))();
136+
new ((_f = d[1].f).bind.apply(_f, [void 0].concat([1, 2].concat(a, ["string"]))))();
137137
// Element access expression with a punctuated key
138138
new e["a-b"].f(1, 2, "string");
139-
new ((_g = e["a-b"].f).bind.apply(_g, [void 0].concat([1, 2].concat(a))));
140-
new ((_h = e["a-b"].f).bind.apply(_h, [void 0].concat([1, 2].concat(a, ["string"]))));
139+
new ((_g = e["a-b"].f).bind.apply(_g, [void 0].concat([1, 2].concat(a))))();
140+
new ((_h = e["a-b"].f).bind.apply(_h, [void 0].concat([1, 2].concat(a, ["string"]))))();
141141
// Basic expression
142142
new B(1, 2, "string");
143-
new (B.bind.apply(B, [void 0].concat([1, 2].concat(a))));
144-
new (B.bind.apply(B, [void 0].concat([1, 2].concat(a, ["string"]))));
143+
new (B.bind.apply(B, [void 0].concat([1, 2].concat(a))))();
144+
new (B.bind.apply(B, [void 0].concat([1, 2].concat(a, ["string"]))))();
145145
// Property access expression
146146
new c["a-b"](1, 2, "string");
147-
new ((_j = c["a-b"]).bind.apply(_j, [void 0].concat([1, 2].concat(a))));
148-
new ((_k = c["a-b"]).bind.apply(_k, [void 0].concat([1, 2].concat(a, ["string"]))));
147+
new ((_j = c["a-b"]).bind.apply(_j, [void 0].concat([1, 2].concat(a))))();
148+
new ((_k = c["a-b"]).bind.apply(_k, [void 0].concat([1, 2].concat(a, ["string"]))))();
149149
// Parenthesised expression
150150
new (c["a-b"])(1, 2, "string");
151-
new ((_l = (c["a-b"])).bind.apply(_l, [void 0].concat([1, 2].concat(a))));
152-
new ((_m = (c["a-b"])).bind.apply(_m, [void 0].concat([1, 2].concat(a, ["string"]))));
151+
new ((_l = (c["a-b"])).bind.apply(_l, [void 0].concat([1, 2].concat(a))))();
152+
new ((_m = (c["a-b"])).bind.apply(_m, [void 0].concat([1, 2].concat(a, ["string"]))))();
153153
// Element access expression
154154
new g[1]["a-b"](1, 2, "string");
155-
new ((_o = g[1]["a-b"]).bind.apply(_o, [void 0].concat([1, 2].concat(a))));
156-
new ((_p = g[1]["a-b"]).bind.apply(_p, [void 0].concat([1, 2].concat(a, ["string"]))));
155+
new ((_o = g[1]["a-b"]).bind.apply(_o, [void 0].concat([1, 2].concat(a))))();
156+
new ((_p = g[1]["a-b"]).bind.apply(_p, [void 0].concat([1, 2].concat(a, ["string"]))))();
157157
// Element access expression with a punctuated key
158158
new h["a-b"]["a-b"](1, 2, "string");
159-
new ((_q = h["a-b"]["a-b"]).bind.apply(_q, [void 0].concat([1, 2].concat(a))));
160-
new ((_r = h["a-b"]["a-b"]).bind.apply(_r, [void 0].concat([1, 2].concat(a, ["string"]))));
159+
new ((_q = h["a-b"]["a-b"]).bind.apply(_q, [void 0].concat([1, 2].concat(a))))();
160+
new ((_r = h["a-b"]["a-b"]).bind.apply(_r, [void 0].concat([1, 2].concat(a, ["string"]))))();
161161
// Element access expression with a number
162162
new i["a-b"][1](1, 2, "string");
163-
new ((_s = i["a-b"][1]).bind.apply(_s, [void 0].concat([1, 2].concat(a))));
164-
new ((_t = i["a-b"][1]).bind.apply(_t, [void 0].concat([1, 2].concat(a, ["string"]))));
163+
new ((_s = i["a-b"][1]).bind.apply(_s, [void 0].concat([1, 2].concat(a))))();
164+
new ((_t = i["a-b"][1]).bind.apply(_t, [void 0].concat([1, 2].concat(a, ["string"]))))();
165165
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;

0 commit comments

Comments
 (0)