Add static errors for object rest (#149)

* Fix parsing object rest

This makes object-rest-spread behave according to spec and only
allow one rest operator and enforces it to be the last
param in the object.

Also move all object-rest-spread tests to a own folder.

* Show nicer error messages
This commit is contained in:
Daniel Tschinder
2016-09-30 17:37:55 +02:00
committed by Henry Zhu
parent 9cc0981c51
commit 7877829fcb
28 changed files with 281 additions and 34 deletions

View File

@@ -691,6 +691,8 @@ pp.parseObj = function (isPattern, refShorthandDefaultPos) {
node.properties = [];
this.next();
let firstRestLocation = null;
while (!this.eat(tt.braceR)) {
if (first) {
first = false;
@@ -713,7 +715,21 @@ pp.parseObj = function (isPattern, refShorthandDefaultPos) {
prop = this.parseSpread();
prop.type = isPattern ? "RestProperty" : "SpreadProperty";
node.properties.push(prop);
continue;
if (isPattern) {
const position = this.state.start;
if (firstRestLocation !== null) {
this.unexpected(firstRestLocation, "Cannot have multiple rest elements when destructuring");
} else if (this.eat(tt.braceR)) {
break;
} else if (this.match(tt.comma) && this.lookahead().type === tt.braceR) {
this.unexpected(position, "A trailing comma is not permitted after the rest element");
} else {
firstRestLocation = position;
continue;
}
} else {
continue;
}
}
prop.method = false;
@@ -753,6 +769,10 @@ pp.parseObj = function (isPattern, refShorthandDefaultPos) {
node.properties.push(prop);
}
if (firstRestLocation !== null) {
this.unexpected(firstRestLocation, "The rest element has to be the last element when destructuring");
}
if (decorators.length) {
this.raise(this.state.start, "You have trailing decorators with no property");
}