Skip to content

Commit a42f864

Browse files
committed
Adds new spread argument
1 parent 89de838 commit a42f864

File tree

5 files changed

+76
-33
lines changed

5 files changed

+76
-33
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6566,7 +6566,7 @@ module ts {
65666566
result.splice(spliceIndex, 0, signature);
65676567
}
65686568
}
6569-
6569+
65706570
function getSpreadArgumentIndex(args: Expression[]): number {
65716571
for (let i = 0; i < args.length; i++) {
65726572
if (args[i].kind === SyntaxKind.SpreadElementExpression) {
@@ -7088,10 +7088,10 @@ module ts {
70887088
}
70897089

70907090
function resolveNewExpression(node: NewExpression, candidatesOutArray: Signature[]): Signature {
7091-
if (node.arguments && languageVersion < ScriptTarget.ES6) {
7091+
if (node.arguments && languageVersion < ScriptTarget.ES5) {
70927092
let spreadIndex = getSpreadArgumentIndex(node.arguments);
70937093
if (spreadIndex >= 0) {
7094-
error(node.arguments[spreadIndex], Diagnostics.Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_6_and_higher);
7094+
error(node.arguments[spreadIndex], Diagnostics.Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_5_and_higher);
70957095
}
70967096
}
70977097

@@ -7109,7 +7109,7 @@ module ts {
71097109
// If ConstructExpr's apparent type(section 3.8.1) is an object type with one or
71107110
// more construct signatures, the expression is processed in the same manner as a
71117111
// function call, but using the construct signatures as the initial set of candidate
7112-
// signatures for overload resolution.The result type of the function call becomes
7112+
// signatures for overload resolution. The result type of the function call becomes
71137113
// the result type of the operation.
71147114
expressionType = getApparentType(expressionType);
71157115
if (expressionType === unknownType) {

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ module ts {
334334
The_0_operator_cannot_be_applied_to_type_symbol: { code: 2469, category: DiagnosticCategory.Error, key: "The '{0}' operator cannot be applied to type 'symbol'." },
335335
Symbol_reference_does_not_refer_to_the_global_Symbol_constructor_object: { code: 2470, category: DiagnosticCategory.Error, key: "'Symbol' reference does not refer to the global Symbol constructor object." },
336336
A_computed_property_name_of_the_form_0_must_be_of_type_symbol: { code: 2471, category: DiagnosticCategory.Error, key: "A computed property name of the form '{0}' must be of type 'symbol'." },
337-
Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_6_and_higher: { code: 2472, category: DiagnosticCategory.Error, key: "Spread operator in 'new' expressions is only available when targeting ECMAScript 6 and higher." },
337+
Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_5_and_higher: { code: 2472, category: DiagnosticCategory.Error, key: "Spread operator in 'new' expressions is only available when targeting ECMAScript 5 and higher." },
338338
Enum_declarations_must_all_be_const_or_non_const: { code: 2473, category: DiagnosticCategory.Error, key: "Enum declarations must all be const or non-const." },
339339
In_const_enum_declarations_member_initializer_must_be_constant_expression: { code: 2474, category: DiagnosticCategory.Error, key: "In 'const' enum declarations member initializer must be constant expression." },
340340
const_enums_can_only_be_used_in_property_or_index_access_expressions_or_the_right_hand_side_of_an_import_declaration_or_export_assignment: { code: 2475, category: DiagnosticCategory.Error, key: "'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment." },

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,7 @@
13241324
"category": "Error",
13251325
"code": 2471
13261326
},
1327-
"Spread operator in 'new' expressions is only available when targeting ECMAScript 6 and higher.": {
1327+
"Spread operator in 'new' expressions is only available when targeting ECMAScript 5 and higher.": {
13281328
"category": "Error",
13291329
"code": 2472
13301330
},

src/compiler/emitter.ts

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,28 +1809,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
18091809
}
18101810

18111811
function emitCallWithSpread(node: CallExpression) {
1812-
let target: Expression;
1813-
let expr = skipParentheses(node.expression);
1814-
if (expr.kind === SyntaxKind.PropertyAccessExpression) {
1815-
// Target will be emitted as "this" argument
1816-
target = emitCallTarget((<PropertyAccessExpression>expr).expression);
1817-
write(".");
1818-
emit((<PropertyAccessExpression>expr).name);
1819-
}
1820-
else if (expr.kind === SyntaxKind.ElementAccessExpression) {
1821-
// Target will be emitted as "this" argument
1822-
target = emitCallTarget((<PropertyAccessExpression>expr).expression);
1823-
write("[");
1824-
emit((<ElementAccessExpression>expr).argumentExpression);
1825-
write("]");
1826-
}
1827-
else if (expr.kind === SyntaxKind.SuperKeyword) {
1828-
target = expr;
1829-
write("_super");
1830-
}
1831-
else {
1832-
emit(node.expression);
1833-
}
1812+
let { target } = emitCallOrNewExpressionTarget(node.expression);
18341813
write(".apply(");
18351814
if (target) {
18361815
if (target.kind === SyntaxKind.SuperKeyword) {
@@ -1880,14 +1859,73 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
18801859
write(")");
18811860
}
18821861
}
1862+
1863+
function emitCallOrNewExpressionTarget(node: Expression): CallTarget {
1864+
node = skipParentheses(node);
1865+
let target: Expression;
1866+
let accessor: Expression;
1867+
if (node.kind === SyntaxKind.PropertyAccessExpression) {
1868+
// Target will be emitted as "this" argument
1869+
target = emitCallTarget((<PropertyAccessExpression>node).expression);
1870+
accessor = (<PropertyAccessExpression>node).name;
1871+
write(".");
1872+
emit(accessor);
1873+
}
1874+
else if (node.kind === SyntaxKind.ElementAccessExpression) {
1875+
// Target will be emitted as "this" argument
1876+
target = emitCallTarget((<PropertyAccessExpression>node).expression);
1877+
accessor = (<ElementAccessExpression>node).argumentExpression;
1878+
write("[");
1879+
emit(accessor);
1880+
write("]");
1881+
}
1882+
else if (node.kind === SyntaxKind.SuperKeyword) {
1883+
target = node;
1884+
write("_super");
1885+
}
1886+
else {
1887+
emit(node);
1888+
}
1889+
return { target, accessor };
1890+
}
18831891

18841892
function emitNewExpression(node: NewExpression) {
18851893
write("new ");
1886-
emit(node.expression);
1887-
if (node.arguments) {
1888-
write("(");
1889-
emitCommaList(node.arguments);
1890-
write(")");
1894+
1895+
// Spread operator logic can be supported in new expressions in ES5 using a combination
1896+
// of Function.prototype.bind() and Function.prototype.apply().
1897+
//
1898+
// Example:
1899+
//
1900+
// var arguments = [1, 2, 3, 4, 5];
1901+
// new (Array.bind.apply(Array, [null].concat(arguments)));
1902+
//
1903+
if (languageVersion === ScriptTarget.ES5 &&
1904+
node.arguments &&
1905+
hasSpreadElement(node.arguments)) {
1906+
1907+
write('(');
1908+
let { target, accessor } = emitCallOrNewExpressionTarget(node.expression);
1909+
write('.bind.apply(');
1910+
if(target) {
1911+
emit(target);
1912+
write('.');
1913+
emit(accessor);
1914+
}
1915+
else {
1916+
emit(node.expression);
1917+
}
1918+
write(', [null].concat(');
1919+
emitListWithSpread(node.arguments, /*multiline*/false, /*trailingComma*/false);
1920+
write(')))');
1921+
}
1922+
else {
1923+
emit(node.expression);
1924+
if (node.arguments) {
1925+
write("(");
1926+
emitCommaList(node.arguments);
1927+
write(")");
1928+
}
18911929
}
18921930
}
18931931

src/compiler/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,11 @@ module ts {
393393
text: string; // Text of identifier (with escapes converted to characters)
394394
originalKeywordKind?: SyntaxKind; // Original syntaxKind which get set so that we can report an error later
395395
}
396+
397+
export interface CallTarget {
398+
target?: Expression;
399+
accessor?: Expression;
400+
}
396401

397402
export interface QualifiedName extends Node {
398403
// Must have same layout as PropertyAccess

0 commit comments

Comments
 (0)