Add grammar error for const { ...a: b } = {}; (#21267)

* Add grammar error for `const { ...a: b } = {};`

* Fix bug: bring in forEachChild change from #21268
This commit is contained in:
Andy
2018-01-18 10:57:35 -08:00
committed by GitHub
parent 9436b1cc0b
commit b5ae9de7ba
7 changed files with 43 additions and 1 deletions

View File

@@ -26194,7 +26194,7 @@ namespace ts {
function checkGrammarBindingElement(node: BindingElement) {
if (node.dotDotDotToken) {
const elements = (<BindingPattern>node.parent).elements;
if (node !== lastOrUndefined(elements)) {
if (node !== last(elements)) {
return grammarErrorOnNode(node, Diagnostics.A_rest_element_must_be_last_in_a_destructuring_pattern);
}
@@ -26202,6 +26202,10 @@ namespace ts {
return grammarErrorOnNode(node.name, Diagnostics.A_rest_element_cannot_contain_a_binding_pattern);
}
if (node.propertyName) {
return grammarErrorOnNode(node.name, Diagnostics.A_rest_element_cannot_have_a_property_name);
}
if (node.initializer) {
// Error on equals token which immediately precedes the initializer
return grammarErrorAtPos(node, node.initializer.pos - 1, 1, Diagnostics.A_rest_element_cannot_have_an_initializer);

View File

@@ -1976,6 +1976,10 @@
"category": "Error",
"code": 2565
},
"A rest element cannot have a property name.": {
"category": "Error",
"code": 2566
},
"JSX element attributes type '{0}' may not be a union type.": {
"category": "Error",
"code": 2600

View File

@@ -0,0 +1,8 @@
tests/cases/compiler/objectBindingPattern_restElementWithPropertyName.ts(1,15): error TS2566: A rest element cannot have a property name.
==== tests/cases/compiler/objectBindingPattern_restElementWithPropertyName.ts (1 errors) ====
const { ...a: b } = {};
~
!!! error TS2566: A rest element cannot have a property name.

View File

@@ -0,0 +1,15 @@
//// [objectBindingPattern_restElementWithPropertyName.ts]
const { ...a: b } = {};
//// [objectBindingPattern_restElementWithPropertyName.js]
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
t[p[i]] = s[p[i]];
return t;
};
var b = __rest({}, []);

View File

@@ -0,0 +1,4 @@
=== tests/cases/compiler/objectBindingPattern_restElementWithPropertyName.ts ===
const { ...a: b } = {};
>b : Symbol(b, Decl(objectBindingPattern_restElementWithPropertyName.ts, 0, 7))

View File

@@ -0,0 +1,6 @@
=== tests/cases/compiler/objectBindingPattern_restElementWithPropertyName.ts ===
const { ...a: b } = {};
>a : any
>b : {}
>{} : {}

View File

@@ -0,0 +1 @@
const { ...a: b } = {};