From 860432cdfdf3cb5b8373b1bc0837ca34b40f9ca2 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 1 Jan 2015 22:31:59 +1100 Subject: [PATCH] hoist export default to very top --- lib/6to5/transformation/modules/_default.js | 15 +++++++------- lib/6to5/transformation/modules/common.js | 2 +- .../transformers/_block-hoist.js | 20 ++++++++++--------- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/lib/6to5/transformation/modules/_default.js b/lib/6to5/transformation/modules/_default.js index 1f1747f44d..2c5ffaebe1 100644 --- a/lib/6to5/transformation/modules/_default.js +++ b/lib/6to5/transformation/modules/_default.js @@ -124,9 +124,9 @@ DefaultFormatter.prototype._pushStatement = function (ref, nodes) { return ref; }; -DefaultFormatter.prototype._hoistExport = function (declar, assign) { +DefaultFormatter.prototype._hoistExport = function (declar, assign, priority) { if (t.isFunctionDeclaration(declar)) { - assign._blockHoist = true; + assign._blockHoist = priority || 1; } return assign; @@ -139,17 +139,18 @@ DefaultFormatter.prototype._exportSpecifier = function (getRef, specifier, node, if (node.source) { if (t.isExportBatchSpecifier(specifier)) { // export * from "foo"; - nodes.push(this._exportsWildcard(getRef())); + nodes.push(this._exportsWildcard(getRef(), node)); } else { // export { foo } from "test"; nodes.push(this._exportsAssign( t.getSpecifierName(specifier), - t.memberExpression(getRef(), specifier.id) + t.memberExpression(getRef(), specifier.id), + node )); } } else { // export { foo }; - nodes.push(this._exportsAssign(t.getSpecifierName(specifier), specifier.id)); + nodes.push(this._exportsAssign(t.getSpecifierName(specifier), specifier.id, node)); } }; @@ -181,7 +182,7 @@ DefaultFormatter.prototype.exportDeclaration = function (node, nodes) { for (var i in declar.declarations) { var decl = declar.declarations[i]; - decl.init = this._exportsAssign(decl.id, decl.init).expression; + decl.init = this._exportsAssign(decl.id, decl.init, node).expression; var newDeclar = t.variableDeclaration(declar.kind, [decl]); if (i === "0") t.inherits(newDeclar, declar); @@ -195,7 +196,7 @@ DefaultFormatter.prototype.exportDeclaration = function (node, nodes) { nodes.push(declar); } - assign = this._exportsAssign(id, ref); + assign = this._exportsAssign(id, ref, node); nodes.push(assign); diff --git a/lib/6to5/transformation/modules/common.js b/lib/6to5/transformation/modules/common.js index 341846f4a0..6f5025f5ab 100644 --- a/lib/6to5/transformation/modules/common.js +++ b/lib/6to5/transformation/modules/common.js @@ -69,7 +69,7 @@ CommonJSFormatter.prototype.exportDeclaration = function (node, nodes) { }, true); // hoist to the top if this default is a function - nodes.push(this._hoistExport(declar, assign)); + nodes.push(this._hoistExport(declar, assign, 2)); } else { DefaultFormatter.prototype.exportDeclaration.apply(this, arguments); } diff --git a/lib/6to5/transformation/transformers/_block-hoist.js b/lib/6to5/transformation/transformers/_block-hoist.js index 8efe4108fd..6d7eb6a469 100644 --- a/lib/6to5/transformation/transformers/_block-hoist.js +++ b/lib/6to5/transformation/transformers/_block-hoist.js @@ -1,17 +1,19 @@ +var _ = require("lodash"); + exports.BlockStatement = exports.Program = { exit: function (node) { - var unshift = []; + var hasChange = false; + for (var i in node.body) { + var bodyNode = node.body[i]; + if (bodyNode && bodyNode._blockHoist) hasChange = true; + } + if (!hasChange) return; - node.body = node.body.filter(function (bodyNode) { - if (bodyNode._blockHoist) { - unshift.push(bodyNode); - return false; - } else { - return true; - } + var nodePriorities = _.groupBy(node.body, function (bodyNode) { + return bodyNode._blockHoist || 0; }); - node.body = unshift.concat(node.body); + node.body = _.flatten(_.values(nodePriorities).reverse()); } };