Skip to content
This repository was archived by the owner on Mar 29, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions lib/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ ExportDeclarationList.prototype.declarationForNode = function(node) {
} else if (n.FunctionDeclaration.check(node.declaration)) {
return new FunctionExportDeclaration(this.module, node);
} else if (n.ExportBatchSpecifier.check(node.specifiers[0])) {
throw new Error(
'`export *` found at ' + sourcePosition(this.module, node) +
' is not supported, please use `export { … }` instead'
);
return new BatchExportDeclaration(this.module, node);
} else {
return new NamedExportDeclaration(this.module, node);
}
Expand Down Expand Up @@ -225,6 +222,25 @@ memo(FunctionExportDeclaration.prototype, 'specifiers', /** @this FunctionExport
return [new ExportSpecifier(this, this.node.declaration)];
});

/**
* Represents an batch export declaration.
*
* @constructor
* @extends ExportDeclaration
* @param {Module} mod
* @param {AST.ExportDeclaration} node
*/
function BatchExportDeclaration(mod, node) {
ExportDeclaration.call(this, mod, node);
}
extend(BatchExportDeclaration, ExportDeclaration);

memo(BatchExportDeclaration.prototype, 'specifiers', /** @this BatchExportDeclaration */function() {
return this.source.exports.declarations.reduce(function(specifiers, declaration) {
return specifiers.concat(declaration.specifiers);
}, []);
});

/**
* Represents an export specifier in an export declaration.
*
Expand Down
19 changes: 7 additions & 12 deletions lib/formatters/commonjs_formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,13 @@ CommonJSFormatter.prototype.buildLateExports = function(mod) {
return;
}

var from =
specifier.importSpecifier ?
self.reference(
specifier.importSpecifier.declaration.source,
specifier.importSpecifier.from
) :
specifier.declaration.source ?
self.reference(
specifier.declaration.source,
specifier.name
) :
b.identifier(specifier.from);
var terminalExportSpecifier = specifier.terminalExportSpecifier;
var from = terminalExportSpecifier.declaration.module === mod ?
b.identifier(terminalExportSpecifier.name) :
self.reference(
terminalExportSpecifier.declaration.module,
terminalExportSpecifier.name
);

assignments.push(b.assignmentExpression(
'=',
Expand Down
7 changes: 7 additions & 0 deletions test/examples/batch-export-bindings/first.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* jshint esnext:true */

export var count = 0;

export function incr() {
count++;
}
3 changes: 3 additions & 0 deletions test/examples/batch-export-bindings/second.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* jshint esnext:true */

export * from './first';
7 changes: 7 additions & 0 deletions test/examples/batch-export-bindings/third.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* jshint esnext:true */

import { count, incr } from './second';

assert.strictEqual(count, 0);
incr();
assert.strictEqual(count, 1);
5 changes: 5 additions & 0 deletions test/examples/batch-export/first.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* jshint esnext:true */

export var a = 1;
export var b = 2;
export var c = 3;
4 changes: 4 additions & 0 deletions test/examples/batch-export/second.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* jshint esnext:true */

export var c = 99;
export * from './first';
7 changes: 7 additions & 0 deletions test/examples/batch-export/third.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* jshint esnext:true */

import { a, b, c } from './second';

assert.strictEqual(a, 1);
assert.strictEqual(b, 2);
assert.strictEqual(c, 99);
5 changes: 5 additions & 0 deletions test/support/test_resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ TestResolver.prototype.resolveModule = function(path, mod, container) {
path = Path.normalize(Path.join(mod.relativePath, '..', path));
}

var cachedModule = container.getCachedModule(path);
if (cachedModule) {
return cachedModule;
}

var resolved = new Module(path, path, container);
resolved.src = this.sources[path] || '';
return resolved;
Expand Down
89 changes: 89 additions & 0 deletions test/unit/module_binding_specifier_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
var assert = require('assert');
var Container = require('../../lib/container');
var Module = require('../../lib/module');
var TestFormatter = require('../support/test_formatter').TestFormatter;
var TestResolver = require('../support/test_resolver').TestResolver;

describe('ModuleBindingSpecifier', function() {
describe('#terminalExportSpecifier', function() {
var sources;
var container;
var specifier;

beforeEach(function() {
container = new Container({
formatter: new TestFormatter(),
resolvers: [new TestResolver(sources)]
});

specifier = getExportSpecifier('root.js', 'a');
});

function getExportSpecifier(modulePath, exportedName) {
var mod = container.getModule(modulePath);
var specifier = mod.exports.findSpecifierByName(exportedName);
if (!specifier) {
throw new Error('unable to find export `' + exportedName + '` in module: ' + modulePath);
}
return specifier;
}

context('when the export is a variable declaration', function() {
before(function() {
sources = { 'root.js': 'export var a = 1;' };
});

it('is the export itself', function() {
assert.strictEqual(specifier.terminalExportSpecifier, specifier);
});
});

context('when the export is a function declaration', function() {
before(function() {
sources = { 'root.js': 'export function a() {}' };
});

it('is the export itself', function() {
assert.strictEqual(specifier.terminalExportSpecifier, specifier);
});
});

context('when the export binds an import by name from another module', function() {
before(function() {
sources = {
'root.js': 'import { a } from "middle.js";\nexport { a };',
'middle.js': 'import { a } from "leaf.js";\nexport { a };',
'leaf.js': 'export var a = 1;'
};
});

it('follows the trail of imports until it finds the original', function() {
var leafA = getExportSpecifier('leaf.js', 'a');
assert.strictEqual(
specifier.terminalExportSpecifier,
leafA,
'expected ' + specifier.terminalExportSpecifier + ' to equal ' + leafA
);
});
});

context('when the export binds an import through a batch export', function() {
before(function() {
sources = {
'root.js': 'import { a } from "middle.js";\nexport { a };',
'middle.js': 'export * from "leaf.js";',
'leaf.js': 'export var a = 1;'
};
});

it('follows the trail of imports until it finds the original', function() {
var leafA = getExportSpecifier('leaf.js', 'a');
assert.strictEqual(
specifier.terminalExportSpecifier,
leafA,
'expected ' + specifier.terminalExportSpecifier + ' to equal ' + leafA
);
});
});
});
});