allow let scope access within FunctionDeclaration, add _block-hoist helper transformer - fixes #77
This commit is contained in:
@@ -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) {
|
||||
|
||||
17
lib/6to5/transformers/_block-hoist.js
Normal file
17
lib/6to5/transformers/_block-hoist.js
Normal 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);
|
||||
}
|
||||
};
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 = [];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user