Browse Source

prevent duplicate exports from single module (#679)

gh-669
Rich-Harris 8 years ago
parent
commit
e45547b58b
  1. 17
      src/Module.js
  2. 8
      test/function/double-default-export/_config.js
  3. 2
      test/function/double-default-export/foo.js
  4. 2
      test/function/double-default-export/main.js
  5. 8
      test/function/double-named-export/_config.js
  6. 3
      test/function/double-named-export/foo.js
  7. 2
      test/function/double-named-export/main.js
  8. 8
      test/function/double-named-reexport/_config.js
  9. 1
      test/function/double-named-reexport/bar.js
  10. 3
      test/function/double-named-reexport/foo.js
  11. 2
      test/function/double-named-reexport/main.js

17
src/Module.js

@ -77,7 +77,13 @@ export default class Module {
else {
node.specifiers.forEach( specifier => {
this.reexports[ specifier.exported.name ] = {
const name = specifier.exported.name;
if ( this.exports[ name ] || this.reexports[ name ] ) {
throw new Error( `A module cannot have multiple exports with the same name ('${name}')` );
}
this.reexports[ name ] = {
start: specifier.start,
source,
localName: specifier.local.name,
@ -93,6 +99,11 @@ export default class Module {
else if ( node.type === 'ExportDefaultDeclaration' ) {
const identifier = ( node.declaration.id && node.declaration.id.name ) || node.declaration.name;
if ( this.exports.default ) {
// TODO indicate location
throw new Error( 'A module can only have one default export' );
}
this.exports.default = {
localName: 'default',
identifier
@ -129,6 +140,10 @@ export default class Module {
const localName = specifier.local.name;
const exportedName = specifier.exported.name;
if ( this.exports[ exportedName ] || this.reexports[ exportedName ] ) {
throw new Error( `A module cannot have multiple exports with the same name ('${exportedName}')` );
}
this.exports[ exportedName ] = { localName };
});
} else {

8
test/function/double-default-export/_config.js

@ -0,0 +1,8 @@
const assert = require( 'assert' );
module.exports = {
description: 'throws on double default exports',
error: err => {
assert.equal( err.message, 'A module can only have one default export' );
}
};

2
test/function/double-default-export/foo.js

@ -0,0 +1,2 @@
export default 1;
export default 2;

2
test/function/double-default-export/main.js

@ -0,0 +1,2 @@
import foo from './foo.js';
console.log( foo );

8
test/function/double-named-export/_config.js

@ -0,0 +1,8 @@
const assert = require( 'assert' );
module.exports = {
description: 'throws on duplicate named exports',
error: err => {
assert.equal( err.message, `A module cannot have multiple exports with the same name ('foo')` );
}
};

3
test/function/double-named-export/foo.js

@ -0,0 +1,3 @@
var foo = 1;
export { foo };
export { foo };

2
test/function/double-named-export/main.js

@ -0,0 +1,2 @@
import { foo } from './foo.js';
console.log( foo );

8
test/function/double-named-reexport/_config.js

@ -0,0 +1,8 @@
const assert = require( 'assert' );
module.exports = {
description: 'throws on duplicate named exports',
error: err => {
assert.equal( err.message, `A module cannot have multiple exports with the same name ('foo')` );
}
};

1
test/function/double-named-reexport/bar.js

@ -0,0 +1 @@
export var foo = 2;

3
test/function/double-named-reexport/foo.js

@ -0,0 +1,3 @@
var foo = 1;
export { foo };
export { foo } from './bar.js';

2
test/function/double-named-reexport/main.js

@ -0,0 +1,2 @@
import { foo } from './foo.js';
console.log( foo );
Loading…
Cancel
Save