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:
parent
9cc0981c51
commit
7877829fcb
@ -691,6 +691,8 @@ pp.parseObj = function (isPattern, refShorthandDefaultPos) {
|
|||||||
node.properties = [];
|
node.properties = [];
|
||||||
this.next();
|
this.next();
|
||||||
|
|
||||||
|
let firstRestLocation = null;
|
||||||
|
|
||||||
while (!this.eat(tt.braceR)) {
|
while (!this.eat(tt.braceR)) {
|
||||||
if (first) {
|
if (first) {
|
||||||
first = false;
|
first = false;
|
||||||
@ -713,7 +715,21 @@ pp.parseObj = function (isPattern, refShorthandDefaultPos) {
|
|||||||
prop = this.parseSpread();
|
prop = this.parseSpread();
|
||||||
prop.type = isPattern ? "RestProperty" : "SpreadProperty";
|
prop.type = isPattern ? "RestProperty" : "SpreadProperty";
|
||||||
node.properties.push(prop);
|
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;
|
prop.method = false;
|
||||||
@ -753,6 +769,10 @@ pp.parseObj = function (isPattern, refShorthandDefaultPos) {
|
|||||||
node.properties.push(prop);
|
node.properties.push(prop);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (firstRestLocation !== null) {
|
||||||
|
this.unexpected(firstRestLocation, "The rest element has to be the last element when destructuring");
|
||||||
|
}
|
||||||
|
|
||||||
if (decorators.length) {
|
if (decorators.length) {
|
||||||
this.raise(this.state.start, "You have trailing decorators with no property");
|
this.raise(this.state.start, "You have trailing decorators with no property");
|
||||||
}
|
}
|
||||||
|
|||||||
1
test/fixtures/experimental/object-rest-spread/10/actual.js
vendored
Normal file
1
test/fixtures/experimental/object-rest-spread/10/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
let { x, y, } = obj;
|
||||||
211
test/fixtures/experimental/object-rest-spread/10/expected.json
vendored
Normal file
211
test/fixtures/experimental/object-rest-spread/10/expected.json
vendored
Normal file
@ -0,0 +1,211 @@
|
|||||||
|
{
|
||||||
|
"type": "File",
|
||||||
|
"start": 0,
|
||||||
|
"end": 20,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 20
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"program": {
|
||||||
|
"type": "Program",
|
||||||
|
"start": 0,
|
||||||
|
"end": 20,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 20
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"sourceType": "script",
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "VariableDeclaration",
|
||||||
|
"start": 0,
|
||||||
|
"end": 20,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 20
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"declarations": [
|
||||||
|
{
|
||||||
|
"type": "VariableDeclarator",
|
||||||
|
"start": 4,
|
||||||
|
"end": 19,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 4
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 19
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"id": {
|
||||||
|
"type": "ObjectPattern",
|
||||||
|
"start": 4,
|
||||||
|
"end": 13,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 4
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 13
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"properties": [
|
||||||
|
{
|
||||||
|
"type": "ObjectProperty",
|
||||||
|
"start": 6,
|
||||||
|
"end": 7,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 6
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 7
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"method": false,
|
||||||
|
"shorthand": true,
|
||||||
|
"computed": false,
|
||||||
|
"key": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 6,
|
||||||
|
"end": 7,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 6
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 7
|
||||||
|
},
|
||||||
|
"identifierName": "x"
|
||||||
|
},
|
||||||
|
"name": "x"
|
||||||
|
},
|
||||||
|
"value": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 6,
|
||||||
|
"end": 7,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 6
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 7
|
||||||
|
},
|
||||||
|
"identifierName": "x"
|
||||||
|
},
|
||||||
|
"name": "x"
|
||||||
|
},
|
||||||
|
"extra": {
|
||||||
|
"shorthand": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "ObjectProperty",
|
||||||
|
"start": 9,
|
||||||
|
"end": 10,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 9
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 10
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"method": false,
|
||||||
|
"shorthand": true,
|
||||||
|
"computed": false,
|
||||||
|
"key": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 9,
|
||||||
|
"end": 10,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 9
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 10
|
||||||
|
},
|
||||||
|
"identifierName": "y"
|
||||||
|
},
|
||||||
|
"name": "y"
|
||||||
|
},
|
||||||
|
"value": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 9,
|
||||||
|
"end": 10,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 9
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 10
|
||||||
|
},
|
||||||
|
"identifierName": "y"
|
||||||
|
},
|
||||||
|
"name": "y"
|
||||||
|
},
|
||||||
|
"extra": {
|
||||||
|
"shorthand": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"init": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 16,
|
||||||
|
"end": 19,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 16
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 19
|
||||||
|
},
|
||||||
|
"identifierName": "obj"
|
||||||
|
},
|
||||||
|
"name": "obj"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"kind": "let"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
1
test/fixtures/experimental/object-rest-spread/6/actual.js
vendored
Normal file
1
test/fixtures/experimental/object-rest-spread/6/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
({x, ...y, a, ...b, c, })
|
||||||
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"type": "File",
|
"type": "File",
|
||||||
"start": 0,
|
"start": 0,
|
||||||
"end": 23,
|
"end": 25,
|
||||||
"loc": {
|
"loc": {
|
||||||
"start": {
|
"start": {
|
||||||
"line": 1,
|
"line": 1,
|
||||||
@ -9,13 +9,13 @@
|
|||||||
},
|
},
|
||||||
"end": {
|
"end": {
|
||||||
"line": 1,
|
"line": 1,
|
||||||
"column": 23
|
"column": 25
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"program": {
|
"program": {
|
||||||
"type": "Program",
|
"type": "Program",
|
||||||
"start": 0,
|
"start": 0,
|
||||||
"end": 23,
|
"end": 25,
|
||||||
"loc": {
|
"loc": {
|
||||||
"start": {
|
"start": {
|
||||||
"line": 1,
|
"line": 1,
|
||||||
@ -23,7 +23,7 @@
|
|||||||
},
|
},
|
||||||
"end": {
|
"end": {
|
||||||
"line": 1,
|
"line": 1,
|
||||||
"column": 23
|
"column": 25
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"sourceType": "script",
|
"sourceType": "script",
|
||||||
@ -31,7 +31,7 @@
|
|||||||
{
|
{
|
||||||
"type": "ExpressionStatement",
|
"type": "ExpressionStatement",
|
||||||
"start": 0,
|
"start": 0,
|
||||||
"end": 23,
|
"end": 25,
|
||||||
"loc": {
|
"loc": {
|
||||||
"start": {
|
"start": {
|
||||||
"line": 1,
|
"line": 1,
|
||||||
@ -39,13 +39,13 @@
|
|||||||
},
|
},
|
||||||
"end": {
|
"end": {
|
||||||
"line": 1,
|
"line": 1,
|
||||||
"column": 23
|
"column": 25
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"expression": {
|
"expression": {
|
||||||
"type": "ObjectExpression",
|
"type": "ObjectExpression",
|
||||||
"start": 1,
|
"start": 1,
|
||||||
"end": 22,
|
"end": 24,
|
||||||
"loc": {
|
"loc": {
|
||||||
"start": {
|
"start": {
|
||||||
"line": 1,
|
"line": 1,
|
||||||
@ -53,7 +53,7 @@
|
|||||||
},
|
},
|
||||||
"end": {
|
"end": {
|
||||||
"line": 1,
|
"line": 1,
|
||||||
"column": 22
|
"column": 24
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"properties": [
|
"properties": [
|
||||||
@ -86,7 +86,8 @@
|
|||||||
"end": {
|
"end": {
|
||||||
"line": 1,
|
"line": 1,
|
||||||
"column": 3
|
"column": 3
|
||||||
}
|
},
|
||||||
|
"identifierName": "x"
|
||||||
},
|
},
|
||||||
"name": "x"
|
"name": "x"
|
||||||
},
|
},
|
||||||
@ -102,9 +103,13 @@
|
|||||||
"end": {
|
"end": {
|
||||||
"line": 1,
|
"line": 1,
|
||||||
"column": 3
|
"column": 3
|
||||||
}
|
},
|
||||||
|
"identifierName": "x"
|
||||||
},
|
},
|
||||||
"name": "x"
|
"name": "x"
|
||||||
|
},
|
||||||
|
"extra": {
|
||||||
|
"shorthand": true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -133,7 +138,8 @@
|
|||||||
"end": {
|
"end": {
|
||||||
"line": 1,
|
"line": 1,
|
||||||
"column": 9
|
"column": 9
|
||||||
}
|
},
|
||||||
|
"identifierName": "y"
|
||||||
},
|
},
|
||||||
"name": "y"
|
"name": "y"
|
||||||
}
|
}
|
||||||
@ -167,7 +173,8 @@
|
|||||||
"end": {
|
"end": {
|
||||||
"line": 1,
|
"line": 1,
|
||||||
"column": 12
|
"column": 12
|
||||||
}
|
},
|
||||||
|
"identifierName": "a"
|
||||||
},
|
},
|
||||||
"name": "a"
|
"name": "a"
|
||||||
},
|
},
|
||||||
@ -183,9 +190,13 @@
|
|||||||
"end": {
|
"end": {
|
||||||
"line": 1,
|
"line": 1,
|
||||||
"column": 12
|
"column": 12
|
||||||
}
|
},
|
||||||
|
"identifierName": "a"
|
||||||
},
|
},
|
||||||
"name": "a"
|
"name": "a"
|
||||||
|
},
|
||||||
|
"extra": {
|
||||||
|
"shorthand": true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -214,7 +225,8 @@
|
|||||||
"end": {
|
"end": {
|
||||||
"line": 1,
|
"line": 1,
|
||||||
"column": 18
|
"column": 18
|
||||||
}
|
},
|
||||||
|
"identifierName": "b"
|
||||||
},
|
},
|
||||||
"name": "b"
|
"name": "b"
|
||||||
}
|
}
|
||||||
@ -248,7 +260,8 @@
|
|||||||
"end": {
|
"end": {
|
||||||
"line": 1,
|
"line": 1,
|
||||||
"column": 21
|
"column": 21
|
||||||
}
|
},
|
||||||
|
"identifierName": "c"
|
||||||
},
|
},
|
||||||
"name": "c"
|
"name": "c"
|
||||||
},
|
},
|
||||||
@ -264,14 +277,19 @@
|
|||||||
"end": {
|
"end": {
|
||||||
"line": 1,
|
"line": 1,
|
||||||
"column": 21
|
"column": 21
|
||||||
}
|
},
|
||||||
|
"identifierName": "c"
|
||||||
},
|
},
|
||||||
"name": "c"
|
"name": "c"
|
||||||
|
},
|
||||||
|
"extra": {
|
||||||
|
"shorthand": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"extra": {
|
"extra": {
|
||||||
"parenthesized": true
|
"parenthesized": true,
|
||||||
|
"parenStart": 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
1
test/fixtures/experimental/object-rest-spread/7/actual.js
vendored
Normal file
1
test/fixtures/experimental/object-rest-spread/7/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
let { ...x, y, z } = obj;
|
||||||
3
test/fixtures/experimental/object-rest-spread/7/options.json
vendored
Normal file
3
test/fixtures/experimental/object-rest-spread/7/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"throws": "The rest element has to be the last element when destructuring (1:10)"
|
||||||
|
}
|
||||||
1
test/fixtures/experimental/object-rest-spread/8/actual.js
vendored
Normal file
1
test/fixtures/experimental/object-rest-spread/8/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
let { x, y, ...z, } = obj;
|
||||||
3
test/fixtures/experimental/object-rest-spread/8/options.json
vendored
Normal file
3
test/fixtures/experimental/object-rest-spread/8/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"throws": "A trailing comma is not permitted after the rest element (1:16)"
|
||||||
|
}
|
||||||
1
test/fixtures/experimental/object-rest-spread/9/actual.js
vendored
Normal file
1
test/fixtures/experimental/object-rest-spread/9/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
let { x, ...y, ...z } = obj;
|
||||||
3
test/fixtures/experimental/object-rest-spread/9/options.json
vendored
Normal file
3
test/fixtures/experimental/object-rest-spread/9/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"throws": "Cannot have multiple rest elements when destructuring (1:13)"
|
||||||
|
}
|
||||||
@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"plugins": ["objectRestSpread"]
|
|
||||||
}
|
|
||||||
@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"plugins": ["objectRestSpread"]
|
|
||||||
}
|
|
||||||
@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"plugins": ["objectRestSpread"]
|
|
||||||
}
|
|
||||||
@ -1 +0,0 @@
|
|||||||
({x, ...y, a, ...b, c})
|
|
||||||
@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"plugins": ["objectRestSpread"]
|
|
||||||
}
|
|
||||||
@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"plugins": ["objectRestSpread"]
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user