@@ -1853,6 +1853,12 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
1853
1853
emitListWithSpread ( node . arguments , /*alwaysCopy*/ false , /*multiLine*/ false , /*trailingComma*/ false ) ;
1854
1854
write ( ")" ) ;
1855
1855
}
1856
+
1857
+ function emitBracedEnclosedCommaList ( nodes : Node [ ] ) {
1858
+ write ( "(" ) ;
1859
+ emitCommaList ( nodes ) ;
1860
+ write ( ")" ) ;
1861
+ }
1856
1862
1857
1863
function emitCallExpression ( node : CallExpression ) {
1858
1864
if ( languageVersion < ScriptTarget . ES6 && hasSpreadElement ( node . arguments ) ) {
@@ -1865,7 +1871,14 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
1865
1871
superCall = true ;
1866
1872
}
1867
1873
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
+ }
1869
1882
superCall = node . expression . kind === SyntaxKind . PropertyAccessExpression && ( < PropertyAccessExpression > node . expression ) . expression . kind === SyntaxKind . SuperKeyword ;
1870
1883
}
1871
1884
if ( superCall && languageVersion < ScriptTarget . ES6 ) {
@@ -1878,13 +1891,27 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
1878
1891
write ( ")" ) ;
1879
1892
}
1880
1893
else {
1881
- write ( "(" ) ;
1882
- emitCommaList ( node . arguments ) ;
1883
- write ( ")" ) ;
1894
+ emitBracedEnclosedCommaList ( node . arguments ) ;
1884
1895
}
1885
1896
}
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 ) {
1888
1915
write ( "new " ) ;
1889
1916
1890
1917
// 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) {
1898
1925
// Could be transpiled into ES5:
1899
1926
//
1900
1927
// 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)));
1902
1929
//
1903
1930
// `[void 0]` is the first argument which represents `thisArg` to the bind method above.
1904
1931
// 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) {
1909
1936
hasSpreadElement ( node . arguments ) ) {
1910
1937
1911
1938
write ( "(" ) ;
1912
- write ( "Function.bind.apply(" ) ;
1913
- emit ( node . expression ) ;
1939
+ let target = emitCallTarget ( node . expression ) ;
1940
+ write ( ".bind.apply(" ) ;
1941
+ emit ( target ) ;
1914
1942
write ( ", [void 0].concat(" ) ;
1915
1943
emitListWithSpread ( node . arguments , /*multiline*/ false , /*trailingComma*/ false ) ;
1916
1944
write ( ")))" ) ;
1945
+ if ( emitCallHelper ) {
1946
+ write ( "()" ) ;
1947
+ }
1917
1948
}
1918
1949
else {
1919
1950
emit ( node . expression ) ;
0 commit comments