From b88d5422d2b554c44685331b033cfa5f852471b2 Mon Sep 17 00:00:00 2001 From: Tingan Ho Date: Fri, 15 May 2015 08:16:01 +0800 Subject: [PATCH] Adds an always emit of pair of parens in spread syntax in new expression in ES5 --- src/compiler/emitter.ts | 43 +++--------------- tests/baselines/reference/newWithSpreadES5.js | 44 +++++++++---------- 2 files changed, 29 insertions(+), 58 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 876698609eb..8027ffaa0d4 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1853,12 +1853,6 @@ var __param = (this && this.__param) || function (paramIndex, decorator) { emitListWithSpread(node.arguments, /*alwaysCopy*/ false, /*multiLine*/ false, /*trailingComma*/ false); write(")"); } - - function emitBracedEnclosedCommaList(nodes: Node[]) { - write("("); - emitCommaList(nodes); - write(")"); - } function emitCallExpression(node: CallExpression) { if (languageVersion < ScriptTarget.ES6 && hasSpreadElement(node.arguments)) { @@ -1871,14 +1865,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) { superCall = true; } else { - if (node.expression.kind === SyntaxKind.NewExpression) { - emitNewExpression(node.expression, /*emitCallHelper*/true); - emitBracedEnclosedCommaList(node.arguments); - return; - } - else { - emit(node.expression); - } + emit(node.expression); superCall = node.expression.kind === SyntaxKind.PropertyAccessExpression && (node.expression).expression.kind === SyntaxKind.SuperKeyword; } if (superCall && languageVersion < ScriptTarget.ES6) { @@ -1891,27 +1878,13 @@ var __param = (this && this.__param) || function (paramIndex, decorator) { write(")"); } else { - emitBracedEnclosedCommaList(node.arguments); + write("("); + emitCommaList(node.arguments); + write(")"); } } - - /** Emit a new expression. - * - * Emit call helper: - * When we are emitting a call expression from a new expressions with spread syntax in ES5 - * we need an argument to help this function decide whether it should emit a pair of extra - * braces "()" at the end. These extra braces are there to help the expression to not be - * consumed by the new expression directly. - * - * Example: - * - * new Array(...arguments)(); - * - * Should be transpiled into ES5: - * - * new (Array.bind.apply(Array, [void 0].concat(arguments)))()() - */ - function emitNewExpression(node: NewExpression, emitCallHelper = false) { + + function emitNewExpression(node: NewExpression) { write("new "); // 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) { write(", [void 0].concat("); emitListWithSpread(node.arguments, /*multiline*/false, /*trailingComma*/false); write(")))"); - if (emitCallHelper) { - write("()"); - } + write("()"); } else { emit(node.expression); diff --git a/tests/baselines/reference/newWithSpreadES5.js b/tests/baselines/reference/newWithSpreadES5.js index 329861d6742..22667d80cab 100644 --- a/tests/baselines/reference/newWithSpreadES5.js +++ b/tests/baselines/reference/newWithSpreadES5.js @@ -116,50 +116,50 @@ var h; var i; // Basic expression new f(1, 2, "string"); -new (f.bind.apply(f, [void 0].concat([1, 2].concat(a)))); -new (f.bind.apply(f, [void 0].concat([1, 2].concat(a, ["string"])))); +new (f.bind.apply(f, [void 0].concat([1, 2].concat(a))))(); +new (f.bind.apply(f, [void 0].concat([1, 2].concat(a, ["string"]))))(); // Call expression new f(1, 2, "string")(); new (f.bind.apply(f, [void 0].concat([1, 2].concat(a))))()(); new (f.bind.apply(f, [void 0].concat([1, 2].concat(a, ["string"]))))()(); // Property access expression new b.f(1, 2, "string"); -new ((_a = b.f).bind.apply(_a, [void 0].concat([1, 2].concat(a)))); -new ((_b = b.f).bind.apply(_b, [void 0].concat([1, 2].concat(a, ["string"])))); +new ((_a = b.f).bind.apply(_a, [void 0].concat([1, 2].concat(a))))(); +new ((_b = b.f).bind.apply(_b, [void 0].concat([1, 2].concat(a, ["string"]))))(); // Parenthesised expression new (b.f)(1, 2, "string"); -new ((_c = (b.f)).bind.apply(_c, [void 0].concat([1, 2].concat(a)))); -new ((_d = (b.f)).bind.apply(_d, [void 0].concat([1, 2].concat(a, ["string"])))); +new ((_c = (b.f)).bind.apply(_c, [void 0].concat([1, 2].concat(a))))(); +new ((_d = (b.f)).bind.apply(_d, [void 0].concat([1, 2].concat(a, ["string"]))))(); // Element access expression new d[1].f(1, 2, "string"); -new ((_e = d[1].f).bind.apply(_e, [void 0].concat([1, 2].concat(a)))); -new ((_f = d[1].f).bind.apply(_f, [void 0].concat([1, 2].concat(a, ["string"])))); +new ((_e = d[1].f).bind.apply(_e, [void 0].concat([1, 2].concat(a))))(); +new ((_f = d[1].f).bind.apply(_f, [void 0].concat([1, 2].concat(a, ["string"]))))(); // Element access expression with a punctuated key new e["a-b"].f(1, 2, "string"); -new ((_g = e["a-b"].f).bind.apply(_g, [void 0].concat([1, 2].concat(a)))); -new ((_h = e["a-b"].f).bind.apply(_h, [void 0].concat([1, 2].concat(a, ["string"])))); +new ((_g = e["a-b"].f).bind.apply(_g, [void 0].concat([1, 2].concat(a))))(); +new ((_h = e["a-b"].f).bind.apply(_h, [void 0].concat([1, 2].concat(a, ["string"]))))(); // Basic expression new B(1, 2, "string"); -new (B.bind.apply(B, [void 0].concat([1, 2].concat(a)))); -new (B.bind.apply(B, [void 0].concat([1, 2].concat(a, ["string"])))); +new (B.bind.apply(B, [void 0].concat([1, 2].concat(a))))(); +new (B.bind.apply(B, [void 0].concat([1, 2].concat(a, ["string"]))))(); // Property access expression new c["a-b"](1, 2, "string"); -new ((_j = c["a-b"]).bind.apply(_j, [void 0].concat([1, 2].concat(a)))); -new ((_k = c["a-b"]).bind.apply(_k, [void 0].concat([1, 2].concat(a, ["string"])))); +new ((_j = c["a-b"]).bind.apply(_j, [void 0].concat([1, 2].concat(a))))(); +new ((_k = c["a-b"]).bind.apply(_k, [void 0].concat([1, 2].concat(a, ["string"]))))(); // Parenthesised expression new (c["a-b"])(1, 2, "string"); -new ((_l = (c["a-b"])).bind.apply(_l, [void 0].concat([1, 2].concat(a)))); -new ((_m = (c["a-b"])).bind.apply(_m, [void 0].concat([1, 2].concat(a, ["string"])))); +new ((_l = (c["a-b"])).bind.apply(_l, [void 0].concat([1, 2].concat(a))))(); +new ((_m = (c["a-b"])).bind.apply(_m, [void 0].concat([1, 2].concat(a, ["string"]))))(); // Element access expression new g[1]["a-b"](1, 2, "string"); -new ((_o = g[1]["a-b"]).bind.apply(_o, [void 0].concat([1, 2].concat(a)))); -new ((_p = g[1]["a-b"]).bind.apply(_p, [void 0].concat([1, 2].concat(a, ["string"])))); +new ((_o = g[1]["a-b"]).bind.apply(_o, [void 0].concat([1, 2].concat(a))))(); +new ((_p = g[1]["a-b"]).bind.apply(_p, [void 0].concat([1, 2].concat(a, ["string"]))))(); // Element access expression with a punctuated key new h["a-b"]["a-b"](1, 2, "string"); -new ((_q = h["a-b"]["a-b"]).bind.apply(_q, [void 0].concat([1, 2].concat(a)))); -new ((_r = h["a-b"]["a-b"]).bind.apply(_r, [void 0].concat([1, 2].concat(a, ["string"])))); +new ((_q = h["a-b"]["a-b"]).bind.apply(_q, [void 0].concat([1, 2].concat(a))))(); +new ((_r = h["a-b"]["a-b"]).bind.apply(_r, [void 0].concat([1, 2].concat(a, ["string"]))))(); // Element access expression with a number new i["a-b"][1](1, 2, "string"); -new ((_s = i["a-b"][1]).bind.apply(_s, [void 0].concat([1, 2].concat(a)))); -new ((_t = i["a-b"][1]).bind.apply(_t, [void 0].concat([1, 2].concat(a, ["string"])))); +new ((_s = i["a-b"][1]).bind.apply(_s, [void 0].concat([1, 2].concat(a))))(); +new ((_t = i["a-b"][1]).bind.apply(_t, [void 0].concat([1, 2].concat(a, ["string"]))))(); var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;