Skip to content

Commit 1be34f1

Browse files
committed
Changes to emitCallTarget
1 parent a97ea4b commit 1be34f1

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

src/compiler/emitter.ts

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,6 +1853,12 @@ 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+
}
18561862

18571863
function emitCallExpression(node: CallExpression) {
18581864
if (languageVersion < ScriptTarget.ES6 && hasSpreadElement(node.arguments)) {
@@ -1865,7 +1871,14 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
18651871
superCall = true;
18661872
}
18671873
else {
1868-
emit(node.expression);
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+
}
18691882
superCall = node.expression.kind === SyntaxKind.PropertyAccessExpression && (<PropertyAccessExpression>node.expression).expression.kind === SyntaxKind.SuperKeyword;
18701883
}
18711884
if (superCall && languageVersion < ScriptTarget.ES6) {
@@ -1878,13 +1891,27 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
18781891
write(")");
18791892
}
18801893
else {
1881-
write("(");
1882-
emitCommaList(node.arguments);
1883-
write(")");
1894+
emitBracedEnclosedCommaList(node.arguments);
18841895
}
18851896
}
1886-
1887-
function emitNewExpression(node: NewExpression) {
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) {
18881915
write("new ");
18891916

18901917
// Spread operator logic can be supported in new expressions in ES5 using a combination
@@ -1898,7 +1925,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
18981925
// Could be transpiled into ES5:
18991926
//
19001927
// var arguments = [1, 2, 3, 4, 5];
1901-
// new (Function.bind.apply(Array, [void 0].concat(arguments)));
1928+
// new (Array.bind.apply(Array, [void 0].concat(arguments)));
19021929
//
19031930
// `[void 0]` is the first argument which represents `thisArg` to the bind method above.
19041931
// And `thisArg` will be set to the return value of the constructor when instantiated
@@ -1909,11 +1936,15 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
19091936
hasSpreadElement(node.arguments)) {
19101937

19111938
write("(");
1912-
write("Function.bind.apply(");
1913-
emit(node.expression);
1939+
let target = emitCallTarget(node.expression);
1940+
write(".bind.apply(");
1941+
emit(target);
19141942
write(", [void 0].concat(");
19151943
emitListWithSpread(node.arguments, /*multiline*/false, /*trailingComma*/false);
19161944
write(")))");
1945+
if (emitCallHelper) {
1946+
write("()");
1947+
}
19171948
}
19181949
else {
19191950
emit(node.expression);

0 commit comments

Comments
 (0)