regenerator
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -23,2 +23,3 @@ /** | ||
var vars = {}; | ||
var funDeclsToRaise = []; | ||
@@ -56,5 +57,11 @@ function varDeclToExpr(vdec, includeIdentifiers) { | ||
} else { | ||
// We don't need to traverse this expression any further because | ||
// there can't be any new declarations inside an expression. | ||
this.replace(b.expressionStatement(expr)); | ||
} | ||
// Since the original node has been either removed or replaced, | ||
// avoid traversing it any further. | ||
return false; | ||
} else if (n.ForStatement.check(node)) { | ||
@@ -75,3 +82,4 @@ if (n.VariableDeclaration.check(node.init)) { | ||
this.replace(b.expressionStatement( | ||
var parentNode = this.parent.node; | ||
var assignment = b.expressionStatement( | ||
b.assignmentExpression( | ||
@@ -88,4 +96,18 @@ "=", | ||
) | ||
)); | ||
); | ||
if (n.BlockStatement.check(this.parent.node)) { | ||
funDeclsToRaise.push({ | ||
block: this.parent.node, | ||
assignment: assignment | ||
}); | ||
// Remove the function declaration for now, but reinsert the assignment | ||
// form later, at the top of the enclosing BlockStatement. | ||
this.replace(); | ||
} else { | ||
this.replace(assignment); | ||
} | ||
// Don't hoist variables out of inner functions. | ||
@@ -100,2 +122,6 @@ return false; | ||
funDeclsToRaise.forEach(function(entry) { | ||
entry.block.body.unshift(entry.assignment); | ||
}); | ||
var paramNames = {}; | ||
@@ -102,0 +128,0 @@ fun.params.forEach(function(param) { |
@@ -23,2 +23,4 @@ /** | ||
if (!n.Function.check(node) || !node.generator) { | ||
// Note that because we are not returning false here the traversal | ||
// will continue into the subtree rooted at this node, as desired. | ||
return; | ||
@@ -45,9 +47,2 @@ } | ||
// TODO Need to do something about FunctionDeclarations. | ||
// They don't need to be exploded, and it's probably safer to move | ||
// them out of the inner function like the hoisted variables. | ||
// How about this: | ||
// 1. hoist the function names as variables, and then | ||
// 2. turn each declaration site into an assignment to the name. | ||
var outerBody = []; | ||
@@ -54,0 +49,0 @@ |
@@ -19,3 +19,3 @@ { | ||
], | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"homepage": "http://github.com/facebook/regenerator", | ||
@@ -35,3 +35,3 @@ "repository": { | ||
"esprima": "git://github.com/ariya/esprima.git#harmony", | ||
"ast-types": "~0.3.1", | ||
"ast-types": "~0.3.3", | ||
"recast": "~0.4.23", | ||
@@ -38,0 +38,0 @@ "private": "~0.0.5" |
@@ -559,1 +559,34 @@ /** | ||
}); | ||
describe("function declaration hoisting", function() { | ||
it("should work even if the declarations are out of order", function() { | ||
function *gen(n) { | ||
yield increment(n); | ||
function increment(x) { | ||
return x + 1; | ||
} | ||
if (n % 2) { | ||
yield halve(decrement(n)); | ||
function halve(x) { | ||
return x >> 1; | ||
} | ||
function decrement(x) { | ||
return x - 1; | ||
} | ||
} else function increment(x) { | ||
return x + 2; | ||
} | ||
yield typeof halve; | ||
yield increment(increment(n)); | ||
} | ||
check(gen(3), [4, 1, "function", 5]); | ||
check(gen(4), [5, "undefined", 8]); | ||
}); | ||
}); |
69518
2072
Updatedast-types@~0.3.3