change the way modules handle default exports and function declaration exports
This commit is contained in:
@@ -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"),
|
||||
|
||||
29
lib/6to5/transformation/transformers/_modules-split.js
Normal file
29
lib/6to5/transformation/transformers/_modules-split.js
Normal 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];
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user