From faba02afd49e75657b14835b953e0cca617e96f5 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 20 Jan 2015 02:30:41 +1100 Subject: [PATCH] better block scoped functions --- .../spec-block-scoped-functions.js | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/6to5/transformation/transformers/spec-block-scoped-functions.js b/lib/6to5/transformation/transformers/spec-block-scoped-functions.js index 087b881d9f..2359246b91 100644 --- a/lib/6to5/transformation/transformers/spec-block-scoped-functions.js +++ b/lib/6to5/transformation/transformers/spec-block-scoped-functions.js @@ -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; + } };