Skip to content

Commit

Permalink
early return
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau committed Sep 6, 2019
1 parent 1508d88 commit 088d220
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions packages/babel-plugin-proposal-object-rest-spread/src/index.js
Expand Up @@ -391,45 +391,47 @@ export default declare((api, opts) => {
const leftPath = path.get("left");
const left = node.left;

if (hasObjectPatternRestElement(leftPath)) {
if (!t.isVariableDeclaration(left)) {
// for ({a, ...b} of []) {}
const temp = scope.generateUidIdentifier("ref");
if (!hasObjectPatternRestElement(leftPath)) {
return;
}

node.left = t.variableDeclaration("var", [
t.variableDeclarator(temp),
]);
if (!t.isVariableDeclaration(left)) {
// for ({a, ...b} of []) {}
const temp = scope.generateUidIdentifier("ref");

path.ensureBlock();
node.left = t.variableDeclaration("var", [
t.variableDeclarator(temp),
]);

if (node.body.body.length === 0 && path.isCompletionRecord()) {
node.body.body.unshift(
t.expressionStatement(scope.buildUndefinedNode()),
);
}
path.ensureBlock();

if (node.body.body.length === 0 && path.isCompletionRecord()) {
node.body.body.unshift(
t.expressionStatement(
t.assignmentExpression("=", left, t.cloneNode(temp)),
),
t.expressionStatement(scope.buildUndefinedNode()),
);
} else {
// for (var {a, ...b} of []) {}
const pattern = left.declarations[0].id;
}

node.body.body.unshift(
t.expressionStatement(
t.assignmentExpression("=", left, t.cloneNode(temp)),
),
);
} else {
// for (var {a, ...b} of []) {}
const pattern = left.declarations[0].id;

const key = scope.generateUidIdentifier("ref");
node.left = t.variableDeclaration(left.kind, [
t.variableDeclarator(key, null),
]);
const key = scope.generateUidIdentifier("ref");
node.left = t.variableDeclaration(left.kind, [
t.variableDeclarator(key, null),
]);

path.ensureBlock();
path.ensureBlock();

node.body.body.unshift(
t.variableDeclaration(node.left.kind, [
t.variableDeclarator(pattern, t.cloneNode(key)),
]),
);
}
node.body.body.unshift(
t.variableDeclaration(node.left.kind, [
t.variableDeclarator(pattern, t.cloneNode(key)),
]),
);
}
},
// [{a, ...b}] = c;
Expand Down

0 comments on commit 088d220

Please sign in to comment.