fix fast forOf and add tests

This commit is contained in:
Sebastian McKenzie
2015-01-13 23:18:46 +11:00
parent 1c2bafe0e1
commit bd2fa77446
10 changed files with 113 additions and 8 deletions

View File

@@ -5,9 +5,10 @@ exports.ForOfStatement = function (node, parent, file, scope) {
var callback = spec;
if (file.isFast("forOf")) callback = fast;
var build = callback(node, parent, file, scope);
var loop = build.loop;
var block = loop.body;
var build = callback(node, parent, file, scope);
var declar = build.declar;
var loop = build.loop;
var block = loop.body;
// inherit comments from the original loop
t.inheritsComments(loop, node);
@@ -15,8 +16,14 @@ exports.ForOfStatement = function (node, parent, file, scope) {
// ensure that it's a block so we can take all it's statemetns
t.ensureBlock(node);
// push the value declaration to the new loop body
if (build.declar) block.body.push(build.declar);
// add the value declaration to the new loop body
if (declar){
if (build.shouldUnshift) {
block.body.unshift(declar);
} else {
block.body.push(declar);
}
}
// push the rest of the original loop body onto our new body
block.body = block.body.concat(node.body.body);
@@ -42,7 +49,7 @@ var fast = function (node, parent, file, scope) {
}
var loop = util.template("for-of-fast", {
LOOP_OBJECT: file.generateUidIdentifier("loopObject", scope),
LOOP_OBJECT: file.generateUidIdentifier("iterator", scope),
IS_ARRAY: file.generateUidIdentifier("isArray", scope),
OBJECT: node.right,
INDEX: file.generateUidIdentifier("i", scope),
@@ -50,8 +57,9 @@ var fast = function (node, parent, file, scope) {
});
return {
declar: declar,
loop: loop
shouldUnshift: true,
declar: declar,
loop: loop
};
};