mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Adds spread syntax in new expression in ES5
This commit is contained in:
@@ -6599,7 +6599,7 @@ module ts {
|
||||
result.splice(spliceIndex, 0, signature);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function getSpreadArgumentIndex(args: Expression[]): number {
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
if (args[i].kind === SyntaxKind.SpreadElementExpression) {
|
||||
@@ -7121,10 +7121,10 @@ module ts {
|
||||
}
|
||||
|
||||
function resolveNewExpression(node: NewExpression, candidatesOutArray: Signature[]): Signature {
|
||||
if (node.arguments && languageVersion < ScriptTarget.ES6) {
|
||||
if (node.arguments && languageVersion < ScriptTarget.ES5) {
|
||||
let spreadIndex = getSpreadArgumentIndex(node.arguments);
|
||||
if (spreadIndex >= 0) {
|
||||
error(node.arguments[spreadIndex], Diagnostics.Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_6_and_higher);
|
||||
error(node.arguments[spreadIndex], Diagnostics.Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_5_and_higher);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7142,7 +7142,7 @@ module ts {
|
||||
// If ConstructExpr's apparent type(section 3.8.1) is an object type with one or
|
||||
// more construct signatures, the expression is processed in the same manner as a
|
||||
// function call, but using the construct signatures as the initial set of candidate
|
||||
// signatures for overload resolution.The result type of the function call becomes
|
||||
// signatures for overload resolution. The result type of the function call becomes
|
||||
// the result type of the operation.
|
||||
expressionType = getApparentType(expressionType);
|
||||
if (expressionType === unknownType) {
|
||||
|
||||
@@ -334,7 +334,7 @@ module ts {
|
||||
The_0_operator_cannot_be_applied_to_type_symbol: { code: 2469, category: DiagnosticCategory.Error, key: "The '{0}' operator cannot be applied to type 'symbol'." },
|
||||
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." },
|
||||
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'." },
|
||||
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." },
|
||||
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." },
|
||||
Enum_declarations_must_all_be_const_or_non_const: { code: 2473, category: DiagnosticCategory.Error, key: "Enum declarations must all be const or non-const." },
|
||||
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." },
|
||||
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." },
|
||||
|
||||
@@ -1324,7 +1324,7 @@
|
||||
"category": "Error",
|
||||
"code": 2471
|
||||
},
|
||||
"Spread operator in 'new' expressions is only available when targeting ECMAScript 6 and higher.": {
|
||||
"Spread operator in 'new' expressions is only available when targeting ECMAScript 5 and higher.": {
|
||||
"category": "Error",
|
||||
"code": 2472
|
||||
},
|
||||
|
||||
+35
-4
@@ -1886,11 +1886,42 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
||||
|
||||
function emitNewExpression(node: NewExpression) {
|
||||
write("new ");
|
||||
emit(node.expression);
|
||||
if (node.arguments) {
|
||||
|
||||
// Spread operator logic can be supported in new expressions in ES5 using a combination
|
||||
// of Function.prototype.bind() and Function.prototype.apply().
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// var arguments = [1, 2, 3, 4, 5];
|
||||
// new Array(...arguments);
|
||||
//
|
||||
// Could be transpiled into ES5:
|
||||
//
|
||||
// var arguments = [1, 2, 3, 4, 5];
|
||||
// new (Function.bind.apply(Array, [void 0].concat(arguments)));
|
||||
//
|
||||
// `[void 0]` is the first argument which represents `thisArg` to the bind method above.
|
||||
// And `thisArg` will be set to the return value of the constructor when instantiated
|
||||
// with the new operator — regardless of any value we set `thisArg` to. Thus, we set it
|
||||
// to an undefined, `void 0`.
|
||||
if (languageVersion === ScriptTarget.ES5 &&
|
||||
node.arguments &&
|
||||
hasSpreadElement(node.arguments)) {
|
||||
|
||||
write("(");
|
||||
emitCommaList(node.arguments);
|
||||
write(")");
|
||||
write("Function.bind.apply(");
|
||||
emit(node.expression);
|
||||
write(", [void 0].concat(");
|
||||
emitListWithSpread(node.arguments, /*multiline*/false, /*trailingComma*/false);
|
||||
write(")))");
|
||||
}
|
||||
else {
|
||||
emit(node.expression);
|
||||
if (node.arguments) {
|
||||
write("(");
|
||||
emitCommaList(node.arguments);
|
||||
write(")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user