From 49449a90c248521c4799fe7f318200ae9d3cfcbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Javier=20Cravero?= Date: Wed, 29 Jun 2016 11:01:37 +0100 Subject: [PATCH] feat: support object rest properties --- inject.js | 47 +++++ test/tests-object-spread.js | 395 ++++++++++++++++++++++++++++++++++++ 2 files changed, 442 insertions(+) diff --git a/inject.js b/inject.js index 424305c..20cc62c 100644 --- a/inject.js +++ b/inject.js @@ -4,6 +4,9 @@ module.exports = function(acorn) { var tt = acorn.tokTypes; var pp = acorn.Parser.prototype; + var originalCheckLVal = pp.checkLVal; + var originalToAssignable = pp.toAssignable; + // this is the same parseObj that acorn has with... function parseObj(isPattern, refDestructuringErrors) { let node = this.startNode(), first = true, propHash = {} @@ -42,8 +45,52 @@ module.exports = function(acorn) { return this.finishNode(node, isPattern ? "ObjectPattern" : "ObjectExpression") } + function toAssignable(node, isBinding) { + if (this.options.ecmaVersion >= 6 && node) { + switch (node.type) { + case "AssignmentPattern": + break; + + case "Property": + this.toAssignable(node.value, isBinding); + break; + + case "SpreadProperty": + node.type = "RestProperty"; + break; + + default: + originalToAssignable.call(this, node, isBinding); + break; + } + } + return node; + }; + + function checkLVal(expr, isBinding, checkClashes) { + switch (expr.type) { + case "ObjectPattern": + for (var i = 0; i < expr.properties.length; i++) { + let prop = expr.properties[i]; + if (prop.type === "Property") prop = prop.value; + this.checkLVal(prop, isBinding, checkClashes); + } + break; + + case "RestProperty": + this.checkLVal(expr.argument, isBinding, checkClashes); + break; + + default: + originalCheckLVal.call(this, expr, isBinding, checkClashes); + break; + } + }; + acorn.plugins.objectSpread = function objectSpreadPlugin(instance) { + pp.checkLVal = checkLVal; pp.parseObj = parseObj; + pp.toAssignable = toAssignable; }; return acorn; diff --git a/test/tests-object-spread.js b/test/tests-object-spread.js index 541cf13..311c257 100644 --- a/test/tests-object-spread.js +++ b/test/tests-object-spread.js @@ -489,6 +489,401 @@ var fbTestFixture = { ] } } + }, + 'ObjectRest': { + 'let {...x} = z': { + "type": "VariableDeclaration", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "id": { + "type": "ObjectPattern", + "start": 4, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "properties": [ + { + "type": "RestProperty", + "start": 5, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "argument": { + "type": "Identifier", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "name": "x" + } + } + ] + }, + "init": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "name": "z" + } + } + ], + "kind": "let" + }, + 'let {x, ...y} = z': { + "type": "VariableDeclaration", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "id": { + "type": "ObjectPattern", + "start": 4, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "properties": [ + { + "type": "Property", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + } + }, + "method": false, + "shorthand": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + } + }, + "name": "x" + }, + "value": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + } + }, + "name": "x" + } + }, + { + "type": "RestProperty", + "start": 8, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "argument": { + "type": "Identifier", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "name": "y" + } + } + ] + }, + "init": { + "type": "Identifier", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "name": "z" + } + } + ], + "kind": "let" + }, + '(function({x, ...y}) { })': { + "type": "ExpressionStatement", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "expression": { + "type": "FunctionExpression", + "start": 1, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "id": null, + "generator": false, + "expression": false, + "params": [ + { + "type": "ObjectPattern", + "start": 10, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "properties": [ + { + "type": "Property", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "method": false, + "shorthand": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "name": "x" + }, + "value": { + "type": "Identifier", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "name": "x" + } + }, + { + "type": "RestProperty", + "start": 14, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "argument": { + "type": "Identifier", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "name": "y" + } + } + ] + } + ], + "body": { + "type": "BlockStatement", + "start": 21, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "body": [] + } + } + } } };