better block scoped functions

This commit is contained in:
Sebastian McKenzie 2015-01-20 02:30:41 +11:00
parent 5436e95c9a
commit faba02afd4

View File

@ -2,23 +2,28 @@
var t = require("../../types");
exports.FunctionDeclaration = function (node, parent) {
if (t.isProgram(parent) || t.isExportDeclaration(parent)) {
exports.BlockStatement = function (node, parent) {
if (t.isFunction(parent) || t.isExportDeclaration(parent)) {
return;
}
// this is to avoid triggering the TDZ detection
node.id.loc = null;
for (var i = 0; i < node.body.length; i++) {
var func = node.body[i];
if (!t.isFunctionDeclaration(i)) continue;
var declar = t.variableDeclaration("let", [
t.variableDeclarator(node.id, t.toExpression(node))
]);
// this is to avoid triggering the TDZ detection
func.id.loc = null;
// hoist it up above everything else
declar._blockHoist = 2;
var declar = t.variableDeclaration("let", [
t.variableDeclarator(func.id, t.toExpression(func))
]);
// todo: name this
node.id = null;
// hoist it up above everything else
declar._blockHoist = 2;
return declar;
// todo: name this
func.id = null;
func.body[i] = declar;
}
};