Skip to content

Commit

Permalink
Disallow binding patterns in binding rest elements (fixes #2519)
Browse files Browse the repository at this point in the history
  • Loading branch information
JsonFreeman committed Apr 14, 2015
1 parent 6637f49 commit 2d3b22c
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12729,6 +12729,11 @@ module ts {
if (node !== elements[elements.length - 1]) {
return grammarErrorOnNode(node, Diagnostics.A_rest_element_must_be_last_in_an_array_destructuring_pattern);
}

if (node.name.kind === SyntaxKind.ArrayBindingPattern || node.name.kind === SyntaxKind.ObjectBindingPattern) {
return grammarErrorOnNode(node.name, Diagnostics.A_rest_element_cannot_contain_a_binding_pattern);
}

if (node.initializer) {
// Error on equals token which immediate precedes the initializer
return grammarErrorAtPos(getSourceFileOfNode(node), node.initializer.pos - 1, 1, Diagnostics.A_rest_element_cannot_have_an_initializer);
Expand Down
1 change: 1 addition & 0 deletions src/compiler/diagnosticInformationMap.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ module ts {
External_module_0_uses_export_and_cannot_be_used_with_export_Asterisk: { code: 2498, category: DiagnosticCategory.Error, key: "External module '{0}' uses 'export =' and cannot be used with 'export *'." },
An_interface_can_only_extend_an_identifier_Slashqualified_name_with_optional_type_arguments: { code: 2499, category: DiagnosticCategory.Error, key: "An interface can only extend an identifier/qualified-name with optional type arguments." },
A_class_can_only_implement_an_identifier_Slashqualified_name_with_optional_type_arguments: { code: 2500, category: DiagnosticCategory.Error, key: "A class can only implement an identifier/qualified-name with optional type arguments." },
A_rest_element_cannot_contain_a_binding_pattern: { code: 2501, category: DiagnosticCategory.Error, key: "A rest element cannot contain a binding pattern." },
Import_declaration_0_is_using_private_name_1: { code: 4000, category: DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." },
Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: { code: 4002, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." },
Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1: { code: 4004, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported interface has or is using private name '{1}'." },
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,10 @@
"category": "Error",
"code": 2500
},
"A rest element cannot contain a binding pattern.": {
"category": "Error",
"code": 2501
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tests/cases/conformance/es6/destructuring/restElementWithBindingPattern.ts(1,9): error TS2501: A rest element cannot contain a binding pattern.


==== tests/cases/conformance/es6/destructuring/restElementWithBindingPattern.ts (1 errors) ====
var [...[a, b]] = [0, 1];
~~~~~~
!!! error TS2501: A rest element cannot contain a binding pattern.
5 changes: 5 additions & 0 deletions tests/baselines/reference/restElementWithBindingPattern.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//// [restElementWithBindingPattern.ts]
var [...[a, b]] = [0, 1];

//// [restElementWithBindingPattern.js]
var _a = [0, 1], [a, b] = _a.slice(0);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
tests/cases/conformance/es6/destructuring/restElementWithBindingPattern2.ts(1,9): error TS2501: A rest element cannot contain a binding pattern.
tests/cases/conformance/es6/destructuring/restElementWithBindingPattern2.ts(1,16): error TS2459: Type 'number[]' has no property 'b' and no string index signature.


==== tests/cases/conformance/es6/destructuring/restElementWithBindingPattern2.ts (2 errors) ====
var [...{0: a, b }] = [0, 1];
~~~~~~~~~~
!!! error TS2501: A rest element cannot contain a binding pattern.
~
!!! error TS2459: Type 'number[]' has no property 'b' and no string index signature.
5 changes: 5 additions & 0 deletions tests/baselines/reference/restElementWithBindingPattern2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//// [restElementWithBindingPattern2.ts]
var [...{0: a, b }] = [0, 1];

//// [restElementWithBindingPattern2.js]
var _a = [0, 1], { 0: a, b } = _a.slice(0);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var [...[a, b]] = [0, 1];
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var [...{0: a, b }] = [0, 1];

0 comments on commit 2d3b22c

Please sign in to comment.