allow let scope access within FunctionDeclaration, add _block-hoist helper transformer - fixes #77

This commit is contained in:
Sebastian McKenzie
2014-10-17 11:09:49 +11:00
parent 112932f9ee
commit 41ab47dde8
4 changed files with 36 additions and 22 deletions

View File

@@ -94,7 +94,8 @@ transform.transformers = {
unicodeRegex: require("./transformers/unicode-regex"),
generators: require("./transformers/generators"),
_aliasFunctions: require("./transformers/_alias-functions")
_aliasFunctions: require("./transformers/_alias-functions"),
_blockHoist: require("./transformers/_block-hoist")
};
_.each(transform.transformers, function (transformer, key) {

View File

@@ -0,0 +1,17 @@
exports.BlockStatement =
exports.Program = {
exit: function (node) {
var unshift = [];
node.body = node.body.filter(function (bodyNode) {
if (bodyNode._blockHoist) {
unshift.push(bodyNode);
return false;
} else {
return true;
}
});
node.body = unshift.concat(node.body);
}
};

View File

@@ -37,9 +37,22 @@ exports.VariableDeclaration = function (node, parent, file) {
});
if (letReferences.length) {
return b.callExpression(b.functionExpression(null, letReferences, b.blockStatement([
b.returnStatement(node)
])), letReferences);
var callNode = function () {
return b.callExpression(b.functionExpression(null, letReferences, b.blockStatement([
b.returnStatement(node)
])), letReferences);
};
if (node.type === "FunctionDeclaration") {
node.type = "FunctionExpression";
var declar = b.variableDeclaration("var", [
b.variableDeclarator(node.id, callNode())
]);
declar._blockHoist = true;
return declar;
} else {
return callNode();
}
} else {
return false;
}

View File

@@ -95,30 +95,13 @@ var pushExportDeclaration = function (node, parent, nodes) {
nodes.push(declar);
if (declar.type === "FunctionDeclaration") {
assign._modulesHoist = true;
assign._blockHoist = true;
}
nodes.push(assign);
}
};
exports.Program = {
exit: function (node) {
var unshift = [];
node.body = node.body.filter(function (bodyNode) {
if (bodyNode._modulesHoist) {
unshift.push(bodyNode);
return false;
} else {
return true;
}
});
node.body = unshift.concat(node.body);
}
};
exports.ExportDeclaration = function (node, parent) {
var nodes = [];