Skip to content
This repository has been archived by the owner on Jun 12, 2020. It is now read-only.

feat: support object rest properties #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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;
Expand Down
Loading