Safely transform all usages of rest arguments

This commit is contained in:
Victor Felder
2015-12-16 15:41:43 +01:00
parent 2dea8b2352
commit 6cc0538d2f
3 changed files with 7 additions and 17 deletions

View File

@@ -121,23 +121,11 @@ export let visitor = {
argsId._shadowedFunctionLiteral = path;
function optimiseCandidate(parent, parentPath, offset) {
if (t.isReturnStatement(parentPath.parent) || t.isIdentifier(parentPath.parent.id) || t.isIdentifier(parentPath.parent.left)) {
if (parent.property) {
parentPath.replaceWith(loadRest({
ARGUMENTS: argsId,
INDEX: t.numericLiteral(parent.property.value + offset)
}));
} else {
if (offset === 0) return;
let newExpr;
let prop = parent.property;
if (t.isLiteral(prop)) {
prop.value += offset;
prop.raw = String(prop.value);
} else { // UnaryExpression, BinaryExpression
newExpr = t.binaryExpression("+", prop, t.numericLiteral(offset));
parent.property = newExpr;
}
}
}

View File

@@ -13,6 +13,7 @@ function t(f, ...items) {
function u(f, g, ...items) {
var x = f;
var y = g;
x = items[0];
y = items[1];
x[12] = items[0];
y.prop = items[1];
var z = items[2] | 0 || 12;
}

View File

@@ -13,6 +13,7 @@ function t(f) {
function u(f, g) {
var x = f;
var y = g;
x = arguments.length <= 2 ? undefined : arguments[2];
y = arguments.length <= 3 ? undefined : arguments[3];
x[12] = arguments.length <= 2 ? undefined : arguments[2];
y.prop = arguments.length <= 3 ? undefined : arguments[3];
var z = (arguments.length <= 4 ? undefined : arguments[4]) | 0 || 12;
}