add support for export extensions https://github.com/leebyron/ecmascript-more-export-from - closes #1091
This commit is contained in:
@@ -559,10 +559,10 @@ pp.parseExport = function(node) {
|
||||
this.next()
|
||||
// export * from '...'
|
||||
if (this.eat(tt.star)) {
|
||||
this.expectContextual("from")
|
||||
node.source = this.type === tt.string ? this.parseExprAtom() : this.unexpected()
|
||||
this.semicolon()
|
||||
this.checkExport(node)
|
||||
if (this.options.features["es7.exportExtensions"] && this.eatContextual("as")) {
|
||||
node.exported = this.parseIdent()
|
||||
}
|
||||
this.parseExportFrom(node)
|
||||
return this.finishNode(node, "ExportAllDeclaration")
|
||||
}
|
||||
if (this.eat(tt._default)) { // export default ...
|
||||
@@ -587,6 +587,10 @@ pp.parseExport = function(node) {
|
||||
node.declaration = this.parseStatement(true)
|
||||
node.specifiers = []
|
||||
node.source = null
|
||||
} else if (this.type === tt.name) {
|
||||
node.exported = this.parseIdent()
|
||||
this.parseExportFrom(node)
|
||||
return this.finishNode(node, "ExportNamespaceDeclaration")
|
||||
} else { // export { x, y as z } [from '...']
|
||||
node.declaration = null
|
||||
node.specifiers = this.parseExportSpecifiers()
|
||||
@@ -601,6 +605,13 @@ pp.parseExport = function(node) {
|
||||
return this.finishNode(node, "ExportNamedDeclaration")
|
||||
}
|
||||
|
||||
pp.parseExportFrom = function(node) {
|
||||
this.expectContextual("from")
|
||||
node.source = this.type === tt.string ? this.parseExprAtom() : this.unexpected()
|
||||
this.semicolon()
|
||||
this.checkExport(node)
|
||||
}
|
||||
|
||||
pp.shouldParseExportDeclaration = function() {
|
||||
return this.options.features["es7.asyncFunctions"] && this.isContextual("async")
|
||||
}
|
||||
|
||||
@@ -21,8 +21,21 @@ export function ExportSpecifier(node, print) {
|
||||
}
|
||||
}
|
||||
|
||||
export function ExportNamespaceDeclaration(node, print) {
|
||||
this.push("export ");
|
||||
print(node.exported);
|
||||
this.push(" from ");
|
||||
print(node.source);
|
||||
this.semicolon();
|
||||
}
|
||||
|
||||
export function ExportAllDeclaration(node, print) {
|
||||
this.push("export * from ");
|
||||
this.push("export *");
|
||||
if (node.exported) {
|
||||
this.push(" as ");
|
||||
print(node.exported);
|
||||
}
|
||||
this.push(" from ");
|
||||
print(node.source);
|
||||
this.semicolon();
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ var exportsVisitor = traverse.explode({
|
||||
extend(formatter.localExports, declar.getBindingIdentifiers());
|
||||
}
|
||||
|
||||
if (!t.isExportDefaultDeclaration(node)) {
|
||||
if (!t.isExportDefaultDeclaration(node) && !t.isExportNamespaceDeclaration(node)) {
|
||||
formatter.hasNonDefaultExports = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
// https://github.com/leebyron/ecmascript-more-export-from
|
||||
|
||||
import * as t from "../../../types";
|
||||
|
||||
export function check(node) {
|
||||
return t.isExportNamespaceDeclaration(node) || (t.isExportAllDeclaration(node) && node.exported);
|
||||
}
|
||||
|
||||
export function ExportNamespaceDeclaration(node, parent, scope) {
|
||||
var uid = scope.generateUidIdentifier("default");
|
||||
return [
|
||||
t.importDeclaration([t.importDefaultSpecifier(uid)], node.source),
|
||||
t.exportDefaultDeclaration(uid)
|
||||
];
|
||||
}
|
||||
|
||||
export function ExportAllDeclaration(node, parent, scope) {
|
||||
if (node.exported) {
|
||||
var uid = scope.generateUidIdentifier(node.exported.name);
|
||||
return [
|
||||
t.importDeclaration([t.importNamespaceSpecifier(uid)], node.source),
|
||||
t.exportNamedDeclaration(null, [t.exportSpecifier(uid, node.exported)])
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -84,6 +84,7 @@ export default {
|
||||
runtime: require("./other/runtime"),
|
||||
|
||||
// needs to be before `_blockHoist` due to function hoisting etc
|
||||
"es7.exportExtensions": require("./es7/export-extensions"),
|
||||
"es6.modules": require("./es6/modules"),
|
||||
|
||||
_blockHoist: require("./internal/block-hoist"),
|
||||
|
||||
@@ -14,10 +14,12 @@
|
||||
"EmptyStatement": ["Statement"],
|
||||
"LabeledStatement": ["Statement"],
|
||||
"VariableDeclaration": ["Statement", "Declaration"],
|
||||
"ExportAllDeclaration": ["Statement", "Declaration", "ModuleDeclaration", "ExportDeclaration"],
|
||||
"ExportDefaultDeclaration": ["Statement", "Declaration", "ModuleDeclaration", "ExportDeclaration"],
|
||||
"ExportNamedDeclaration": ["Statement", "Declaration", "ModuleDeclaration", "ExportDeclaration"],
|
||||
"ImportDeclaration": ["Statement", "Declaration", "ModuleDeclaration"],
|
||||
|
||||
"ExportNamespaceDeclaration": ["Statement", "Declaration", "ModuleDeclaration", "ExportDeclaration"],
|
||||
"ExportAllDeclaration": ["Statement", "Declaration", "ModuleDeclaration", "ExportDeclaration"],
|
||||
"ExportDefaultDeclaration": ["Statement", "Declaration", "ModuleDeclaration", "ExportDeclaration"],
|
||||
"ExportNamedDeclaration": ["Statement", "Declaration", "ModuleDeclaration", "ExportDeclaration"],
|
||||
"ImportDeclaration": ["Statement", "Declaration", "ModuleDeclaration"],
|
||||
|
||||
"ArrowFunctionExpression": ["Scopable", "Function", "Expression"],
|
||||
"FunctionDeclaration": ["Scopable", "Function", "Statement", "Declaration"],
|
||||
|
||||
@@ -22,10 +22,6 @@
|
||||
"DoWhileStatement": ["body", "test"],
|
||||
"DoExpression": ["body"],
|
||||
"EmptyStatement": [],
|
||||
"ExportAllDeclaration": ["source"],
|
||||
"ExportDefaultDeclaration": ["declaration"],
|
||||
"ExportNamedDeclaration": ["declaration", "specifiers", "source"],
|
||||
"ExportSpecifier": ["local", "exported"],
|
||||
"ExpressionStatement": ["expression"],
|
||||
"File": ["program"],
|
||||
"ForInStatement": ["left", "right", "body"],
|
||||
@@ -72,6 +68,12 @@
|
||||
"WithStatement": ["object", "body"],
|
||||
"YieldExpression": ["argument"],
|
||||
|
||||
"ExportAllDeclaration": ["source", "exported"],
|
||||
"ExportNamespaceDeclaration": ["exported"],
|
||||
"ExportDefaultDeclaration": ["declaration"],
|
||||
"ExportNamedDeclaration": ["declaration", "specifiers", "source"],
|
||||
"ExportSpecifier": ["local", "exported"],
|
||||
|
||||
"AnyTypeAnnotation": [],
|
||||
"ArrayTypeAnnotation": ["elementType"],
|
||||
"BooleanTypeAnnotation": [],
|
||||
|
||||
@@ -2058,3 +2058,55 @@ test('export async function foo(){}', {
|
||||
});
|
||||
|
||||
// ES7 decorators
|
||||
|
||||
// ES7 export extensions - https://github.com/leebyron/ecmascript-more-export-from
|
||||
|
||||
test('export foo from "bar";', {
|
||||
type: "Program",
|
||||
body: [{
|
||||
type: "ExportNamespaceDeclaration",
|
||||
start: 0,
|
||||
end: 22,
|
||||
exported: {
|
||||
type: "Identifier",
|
||||
name: "foo",
|
||||
start: 7,
|
||||
end: 10,
|
||||
},
|
||||
source: {
|
||||
type: "Literal",
|
||||
value: "bar",
|
||||
start: 16,
|
||||
end: 21
|
||||
}
|
||||
}]
|
||||
}, {
|
||||
ecmaVersion: 7,
|
||||
sourceType: "module",
|
||||
features: { "es7.exportExtensions": true }
|
||||
});
|
||||
|
||||
test('export * as foo from "bar";', {
|
||||
type: "Program",
|
||||
body: [{
|
||||
type: "ExportAllDeclaration",
|
||||
start: 0,
|
||||
end: 27,
|
||||
exported: {
|
||||
type: "Identifier",
|
||||
name: "foo",
|
||||
start: 12,
|
||||
end: 15,
|
||||
},
|
||||
source: {
|
||||
type: "Literal",
|
||||
value: "bar",
|
||||
start: 21,
|
||||
end: 26
|
||||
}
|
||||
}]
|
||||
}, {
|
||||
ecmaVersion: 7,
|
||||
sourceType: "module",
|
||||
features: { "es7.exportExtensions": true }
|
||||
});
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
export foo from "bar";
|
||||
@@ -0,0 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
var _default = babelHelpers.interopRequire(require("bar"));
|
||||
|
||||
module.exports = _default;
|
||||
@@ -0,0 +1 @@
|
||||
export foo from "bar";
|
||||
@@ -0,0 +1,4 @@
|
||||
"use strict";
|
||||
|
||||
import _default from "bar";
|
||||
export default _default;
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"blacklist": ["es6.modules"]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export * as foo from "bar";
|
||||
@@ -0,0 +1,9 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
|
||||
var _foo = babelHelpers.interopRequireWildcard(require("bar"));
|
||||
|
||||
exports.foo = _foo;
|
||||
@@ -0,0 +1 @@
|
||||
export * as foo from "bar";
|
||||
@@ -0,0 +1,4 @@
|
||||
"use strict";
|
||||
|
||||
import * as _foo from "bar";
|
||||
export { _foo as foo };
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"blacklist": ["es6.modules"]
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"noCheckAst": true,
|
||||
"externalHelpers": true,
|
||||
"optional": "es7.exportExtensions"
|
||||
}
|
||||
Reference in New Issue
Block a user