Adds an always emit of pair of parens in spread syntax in new expression in ES5

This commit is contained in:
Tingan Ho
2015-05-15 08:16:01 +08:00
parent 1be34f191e
commit b88d5422d2
2 changed files with 29 additions and 58 deletions

View File

@@ -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(<NewExpression>node.expression, /*emitCallHelper*/true);
emitBracedEnclosedCommaList(node.arguments);
return;
}
else {
emit(node.expression);
}
emit(node.expression);
superCall = node.expression.kind === SyntaxKind.PropertyAccessExpression && (<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);