add improved for-of loose behaviour that supports destructuring - fixes #615

This commit is contained in:
Sebastian McKenzie
2015-01-29 00:50:22 +11:00
parent c25c33e3ee
commit 960a70287d
5 changed files with 31 additions and 25 deletions

View File

@@ -2,6 +2,7 @@ for (var LOOP_OBJECT = OBJECT,
IS_ARRAY = Array.isArray(LOOP_OBJECT),
INDEX = 0,
LOOP_OBJECT = IS_ARRAY ? LOOP_OBJECT : LOOP_OBJECT[Symbol.iterator]();;) {
var ID;
if (IS_ARRAY) {
if (INDEX >= LOOP_OBJECT.length) break;
ID = LOOP_OBJECT[INDEX++];

View File

@@ -20,11 +20,7 @@ exports.ForOfStatement = function (node, parent, scope, context, file) {
// add the value declaration to the new loop body
if (declar) {
if (build.shouldUnshift) {
block.body.unshift(declar);
} else {
block.body.push(declar);
}
block.body.push(declar);
}
// push the rest of the original loop body onto our new body
@@ -45,9 +41,9 @@ var loose = function (node, parent, scope, context, file) {
id = left;
} else if (t.isVariableDeclaration(left)) {
// for (var i of test)
id = left.declarations[0].id;
id = scope.generateUidIdentifier("ref");
declar = t.variableDeclaration(left.kind, [
t.variableDeclarator(id)
t.variableDeclarator(left.declarations[0].id, id)
]);
} else {
throw file.errorWithNode(left, "Unknown node type " + left.type + " in ForOfStatement");
@@ -61,10 +57,15 @@ var loose = function (node, parent, scope, context, file) {
ID: id
});
if (!declar) {
// no declaration so we need to remove the variable declaration at the top of
// the for-of-loose template
loop.body.body.shift();
}
return {
shouldUnshift: true,
declar: declar,
loop: loop
declar: declar,
loop: loop
};
};