change the way modules handle default exports and function declaration exports
This commit is contained in:
parent
a821b2249b
commit
8243a34b69
@ -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) {
|
||||
|
||||
@ -2,9 +2,7 @@ if (true) {
|
||||
const x = 1;
|
||||
switch (x) {
|
||||
case 1: {
|
||||
function y() {
|
||||
assert(x, 1);
|
||||
}
|
||||
assert(x, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,7 +11,6 @@ define(["exports", "module"], function (exports, module) {
|
||||
module.exports = function () {};
|
||||
|
||||
function foo() {}
|
||||
var Foo = function Foo() {};
|
||||
|
||||
module.exports = Foo;
|
||||
var Foo = undefined;
|
||||
module.exports = Foo = function Foo() {};
|
||||
});
|
||||
|
||||
@ -11,8 +11,7 @@ define(["exports"], function (exports) {
|
||||
var foo5 = exports.foo5 = undefined;
|
||||
var foo6 = exports.foo6 = 3;
|
||||
function foo7() {}
|
||||
var foo8 = function foo8() {};
|
||||
var foo8 = exports.foo8 = function foo8() {};
|
||||
|
||||
exports.foo8 = foo8;
|
||||
exports.__esModule = true;
|
||||
});
|
||||
});
|
||||
|
||||
@ -10,6 +10,5 @@ module.exports = function () {};
|
||||
module.exports = function () {};
|
||||
|
||||
function foo() {}
|
||||
var Foo = function Foo() {};
|
||||
|
||||
module.exports = Foo;
|
||||
var Foo = undefined;
|
||||
module.exports = Foo = function Foo() {};
|
||||
|
||||
@ -10,7 +10,6 @@ var foo4 = exports.foo4 = 2;
|
||||
var foo5 = exports.foo5 = undefined;
|
||||
var foo6 = exports.foo6 = 3;
|
||||
function foo7() {}
|
||||
var foo8 = function foo8() {};
|
||||
var foo8 = exports.foo8 = function foo8() {};
|
||||
|
||||
exports.foo8 = foo8;
|
||||
exports.__esModule = true;
|
||||
exports.__esModule = true;
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
function foo() {}
|
||||
var Foo = function Foo() {};
|
||||
var Foo = undefined;
|
||||
Foo = function Foo() {};
|
||||
|
||||
@ -20,9 +20,8 @@ System.register([], function (_export) {
|
||||
|
||||
_export("default", function () {});
|
||||
|
||||
Foo = function Foo() {};
|
||||
|
||||
_export("default", Foo);
|
||||
Foo = undefined;
|
||||
_export("default", Foo = function Foo() {});
|
||||
}
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
@ -14,12 +14,9 @@ System.register([], function (_export) {
|
||||
foo4 = _export("foo4", 2);
|
||||
foo5 = _export("foo5", undefined);
|
||||
foo6 = _export("foo6", 3);
|
||||
foo8 = function foo8() {};
|
||||
|
||||
_export("foo8", foo8);
|
||||
|
||||
foo8 = _export("foo8", function foo8() {});
|
||||
_export("foo3", foo3 = 5);
|
||||
exports.__esModule = true;
|
||||
}
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
@ -17,7 +17,6 @@
|
||||
module.exports = function () {};
|
||||
|
||||
function foo() {}
|
||||
var Foo = function Foo() {};
|
||||
|
||||
module.exports = Foo;
|
||||
var Foo = undefined;
|
||||
module.exports = Foo = function Foo() {};
|
||||
});
|
||||
|
||||
@ -17,8 +17,7 @@
|
||||
var foo5 = exports.foo5 = undefined;
|
||||
var foo6 = exports.foo6 = 3;
|
||||
function foo7() {}
|
||||
var foo8 = function foo8() {};
|
||||
var foo8 = exports.foo8 = function foo8() {};
|
||||
|
||||
exports.foo8 = foo8;
|
||||
exports.__esModule = true;
|
||||
});
|
||||
});
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
"use strict";
|
||||
|
||||
var _to5Helpers = require("6to5-runtime/helpers");
|
||||
|
||||
var _regeneratorRuntime = require("6to5-runtime/regenerator");
|
||||
|
||||
var _core = require("6to5-runtime/core-js");
|
||||
|
||||
var _to5Helpers = require("6to5-runtime/helpers");
|
||||
|
||||
var giveWord = _regeneratorRuntime.mark(function giveWord() {
|
||||
return _regeneratorRuntime.wrap(function giveWord$(context$1$0) {
|
||||
while (1) switch (context$1$0.prev = context$1$0.next) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user