From 41ab47dde8928215e68647e276e18228048f6265 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 17 Oct 2014 11:09:49 +1100 Subject: [PATCH] allow let scope access within FunctionDeclaration, add _block-hoist helper transformer - fixes #77 --- lib/6to5/transform.js | 3 ++- lib/6to5/transformers/_block-hoist.js | 17 +++++++++++++++++ lib/6to5/transformers/let-scoping.js | 19 ++++++++++++++++--- lib/6to5/transformers/modules.js | 19 +------------------ 4 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 lib/6to5/transformers/_block-hoist.js diff --git a/lib/6to5/transform.js b/lib/6to5/transform.js index 6c8867fc52..8649b476ce 100644 --- a/lib/6to5/transform.js +++ b/lib/6to5/transform.js @@ -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) { diff --git a/lib/6to5/transformers/_block-hoist.js b/lib/6to5/transformers/_block-hoist.js new file mode 100644 index 0000000000..8efe4108fd --- /dev/null +++ b/lib/6to5/transformers/_block-hoist.js @@ -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); + } +}; diff --git a/lib/6to5/transformers/let-scoping.js b/lib/6to5/transformers/let-scoping.js index fcf465cb1a..238946cde5 100644 --- a/lib/6to5/transformers/let-scoping.js +++ b/lib/6to5/transformers/let-scoping.js @@ -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; } diff --git a/lib/6to5/transformers/modules.js b/lib/6to5/transformers/modules.js index 62928f0978..4e445d50d6 100644 --- a/lib/6to5/transformers/modules.js +++ b/lib/6to5/transformers/modules.js @@ -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 = [];