Disallow initializers in assignment rest elements

This commit is contained in:
Jason Freeman
2015-04-10 17:24:23 -07:00
parent e6ffb33adf
commit 2b21cd9825
2 changed files with 12 additions and 6 deletions

View File

@@ -7657,11 +7657,17 @@ module ts {
}
}
else {
if (i === elements.length - 1) {
checkDestructuringAssignment((<SpreadElementExpression>e).expression, createArrayType(elementType), contextualMapper);
if (i < elements.length - 1) {
error(e, Diagnostics.A_rest_element_must_be_last_in_an_array_destructuring_pattern);
}
else {
error(e, Diagnostics.A_rest_element_must_be_last_in_an_array_destructuring_pattern);
let restExpression = (<SpreadElementExpression>e).expression;
if (restExpression.kind === SyntaxKind.BinaryExpression && (<BinaryExpression>restExpression).operatorToken.kind === SyntaxKind.EqualsToken) {
error((<BinaryExpression>restExpression).operatorToken, Diagnostics.A_rest_element_cannot_have_an_initializer);
}
else {
checkDestructuringAssignment(restExpression, createArrayType(elementType), contextualMapper);
}
}
}
}

View File

@@ -1,10 +1,10 @@
tests/cases/conformance/es6/destructuring/restElementWithInitializer2.ts(3,5): error TS2364: Invalid left-hand side of assignment expression.
tests/cases/conformance/es6/destructuring/restElementWithInitializer2.ts(3,7): error TS1186: A rest element cannot have an initializer.
==== tests/cases/conformance/es6/destructuring/restElementWithInitializer2.ts (1 errors) ====
var a: number[];
var x: number[];
[...x = a] = a; // Error, rest element cannot have initializer
~~~~~
!!! error TS2364: Invalid left-hand side of assignment expression.
~
!!! error TS1186: A rest element cannot have an initializer.