change the way modules handle default exports and function declaration exports

This commit is contained in:
Sebastian McKenzie
2015-01-26 15:53:17 +11:00
parent a821b2249b
commit 8243a34b69
15 changed files with 66 additions and 37 deletions

View File

@@ -109,10 +109,9 @@ _.each({
"playground.memoizationOperator": require("./transformers/playground/memoization-operator"),
"playground.objectGetterMemoization": require("./transformers/playground/object-getter-memoization"),
react: require("./transformers/other/react"),
react: require("./transformers/other/react"),
// needs to be before `_blockHoist` due to function hoisting etc
"es6.modules": require("./transformers/es6/modules"),
_modulesSplit: require("./transformers/_modules-split"),
// needs to be before `regenerator` due to generator comprehensions
// needs to be before `_aliasFunction`
@@ -164,6 +163,9 @@ _.each({
// needs to be before `es6.modules` due to dynamic imports
selfContained: require("./transformers/other/self-contained"),
// needs to be before `_blockHoist` due to function hoisting etc
"es6.modules": require("./transformers/es6/modules"),
_blockHoist: require("./transformers/_block-hoist"),
"spec.protoToAssign": require("./transformers/spec/proto-to-assign"),

View File

@@ -0,0 +1,29 @@
"use strict";
var t = require("../../types");
exports.ExportDeclaration = function (node, parent, scope, context, file) {
var declar = node.declaration;
if (node.default) {
if (t.isClassDeclaration(declar)) {
// we need to replace default class declarations with an assignment
// because VariableDeclaration nodes aren't allowed in `export default`
node.declaration = t.assignmentExpression("=", declar.id, t.toExpression(declar));
return [
t.variableDeclaration("let", [
t.variableDeclarator(declar.id)
]),
node
];
}
} else {
if (t.isFunctionDeclaration(declar)) {
node.declaration = null;
node.specifiers = [t.importSpecifier(declar.id, declar.id)];
node._blockHoist = 2;
return [declar, node];
}
}
};

View File

@@ -23,6 +23,7 @@ exports.ImportDeclaration = function (node, parent, scope, context, file) {
exports.ExportDeclaration = function (node, parent, scope, context, file) {
var nodes = [];
var i;
if (node.declaration) {
// make sure variable exports have an initialiser
@@ -34,10 +35,16 @@ exports.ExportDeclaration = function (node, parent, scope, context, file) {
file.moduleFormatter.exportDeclaration(node, nodes, parent);
} else if (node.specifiers) {
for (var i = 0; i < node.specifiers.length; i++) {
for (i = 0; i < node.specifiers.length; i++) {
file.moduleFormatter.exportSpecifier(node.specifiers[i], node, nodes, parent);
}
}
if (node._blockHoist) {
for (i = 0; i < nodes.length; i++) {
nodes[i]._blockHoist = node._blockHoist;
}
}
return nodes;
};

View File

@@ -364,6 +364,8 @@ t.toStatement = function (node, ignore) {
} else if (t.isFunction(node)) {
mustHaveId = true;
newType = "FunctionDeclaration";
} else if (t.isAssignmentExpression(node)) {
return t.expressionStatement(node);
}
if (mustHaveId && !node.id) {