From e907da611f2f4db7dcd4c1491dfb82f96f80d2e8 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Tue, 31 Jan 2017 23:46:15 +0100 Subject: [PATCH] Fix #304 and #309 --- src/parser/expression.js | 3 +- .../object-rest-spread/16/actual.js | 1 + .../object-rest-spread/16/expected.json | 243 +++++++++++++++ .../object-rest-spread/17/actual.js | 1 + .../object-rest-spread/17/expected.json | 278 ++++++++++++++++++ 5 files changed, 525 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/experimental/object-rest-spread/16/actual.js create mode 100644 test/fixtures/experimental/object-rest-spread/16/expected.json create mode 100644 test/fixtures/experimental/object-rest-spread/17/actual.js create mode 100644 test/fixtures/experimental/object-rest-spread/17/expected.json diff --git a/src/parser/expression.js b/src/parser/expression.js index df90d47f0a..b0f18a9010 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -735,8 +735,9 @@ pp.parseObj = function (isPattern, refShorthandDefaultPos) { } if (this.hasPlugin("objectRestSpread") && this.match(tt.ellipsis)) { - prop = this.parseSpread(); + prop = this.parseSpread(isPattern ? { start: 0 } : undefined); prop.type = isPattern ? "RestProperty" : "SpreadProperty"; + if (isPattern) this.toAssignable(prop.argument, true, "object pattern"); node.properties.push(prop); if (isPattern) { const position = this.state.start; diff --git a/test/fixtures/experimental/object-rest-spread/16/actual.js b/test/fixtures/experimental/object-rest-spread/16/actual.js new file mode 100644 index 0000000000..a267ff84eb --- /dev/null +++ b/test/fixtures/experimental/object-rest-spread/16/actual.js @@ -0,0 +1 @@ +var {...{z}} = { z: 1}; diff --git a/test/fixtures/experimental/object-rest-spread/16/expected.json b/test/fixtures/experimental/object-rest-spread/16/expected.json new file mode 100644 index 0000000000..6f6be6d836 --- /dev/null +++ b/test/fixtures/experimental/object-rest-spread/16/expected.json @@ -0,0 +1,243 @@ +{ + "type": "File", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "sourceType": "script", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "id": { + "type": "ObjectPattern", + "start": 4, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "properties": [ + { + "type": "RestProperty", + "start": 5, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "argument": { + "type": "ObjectPattern", + "start": 8, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "properties": [ + { + "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": "z" + }, + "name": "z" + }, + "value": { + "type": "Identifier", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "z" + }, + "name": "z" + }, + "extra": { + "shorthand": true + } + } + ] + } + } + ] + }, + "init": { + "type": "ObjectExpression", + "start": 15, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "properties": [ + { + "type": "ObjectProperty", + "start": 17, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 18 + }, + "identifierName": "z" + }, + "name": "z" + }, + "value": { + "type": "NumericLiteral", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + ] + } + } + ], + "kind": "var" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/experimental/object-rest-spread/17/actual.js b/test/fixtures/experimental/object-rest-spread/17/actual.js new file mode 100644 index 0000000000..c27b7e33c4 --- /dev/null +++ b/test/fixtures/experimental/object-rest-spread/17/actual.js @@ -0,0 +1 @@ +var { ...{ x = 5 } } = {x : 1}; diff --git a/test/fixtures/experimental/object-rest-spread/17/expected.json b/test/fixtures/experimental/object-rest-spread/17/expected.json new file mode 100644 index 0000000000..0c15cfbe9c --- /dev/null +++ b/test/fixtures/experimental/object-rest-spread/17/expected.json @@ -0,0 +1,278 @@ +{ + "type": "File", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "sourceType": "script", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "id": { + "type": "ObjectPattern", + "start": 4, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "properties": [ + { + "type": "RestProperty", + "start": 6, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "argument": { + "type": "ObjectPattern", + "start": 9, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "properties": [ + { + "type": "ObjectProperty", + "start": 11, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "method": false, + "shorthand": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "x" + }, + "name": "x" + }, + "value": { + "type": "AssignmentPattern", + "start": 11, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "left": { + "type": "Identifier", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "x" + }, + "name": "x" + }, + "right": { + "type": "NumericLiteral", + "start": 15, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "extra": { + "rawValue": 5, + "raw": "5" + }, + "value": 5 + } + }, + "extra": { + "shorthand": true + } + } + ] + } + } + ] + }, + "init": { + "type": "ObjectExpression", + "start": 23, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "properties": [ + { + "type": "ObjectProperty", + "start": 24, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 24, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 25 + }, + "identifierName": "x" + }, + "name": "x" + }, + "value": { + "type": "NumericLiteral", + "start": 28, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + ] + } + } + ], + "kind": "var" + } + ], + "directives": [] + } +} \ No newline at end of file