From 5be634f20e39c110da6013b8a8d7ab00845c5e12 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sun, 28 Jun 2015 10:24:07 -0400 Subject: [PATCH] ensure unused default exports have legal name (fixes #33) --- src/Module.js | 23 +++++++++---------- .../function/rename-default-export/_config.js | 9 ++++++++ test/function/rename-default-export/bar.js | 3 +++ test/function/rename-default-export/baz.js | 5 ++++ test/function/rename-default-export/foo.js | 3 +++ test/function/rename-default-export/main.js | 7 ++++++ 6 files changed, 38 insertions(+), 12 deletions(-) create mode 100644 test/function/rename-default-export/_config.js create mode 100644 test/function/rename-default-export/bar.js create mode 100644 test/function/rename-default-export/baz.js create mode 100644 test/function/rename-default-export/foo.js create mode 100644 test/function/rename-default-export/main.js diff --git a/src/Module.js b/src/Module.js index c0900c4..83e87bc 100644 --- a/src/Module.js +++ b/src/Module.js @@ -12,6 +12,14 @@ import makeLegalIdentifier from './utils/makeLegalIdentifier'; const emptyArrayPromise = Promise.resolve([]); +function deconflict ( name, names ) { + while ( name in names ) { + name = `_${name}`; + } + + return name; +} + export default class Module { constructor ({ path, source, bundle }) { this.source = source; @@ -249,14 +257,9 @@ export default class Module { } getCanonicalName ( localName ) { - // Special case - if ( localName === 'default' && this.exports.default && this.exports.default.isModified ) { + if ( localName === 'default' && ( this.exports.default.isModified || !this.suggestedNames.default ) ) { let canonicalName = makeLegalIdentifier( this.path.replace( this.bundle.base + '/', '' ).replace( /\.js$/, '' ) ); - while ( this.definitions[ canonicalName ] ) { - canonicalName = `_${canonicalName}`; - } - - return canonicalName; + return deconflict( canonicalName, this.definitions ); } if ( this.suggestedNames[ localName ] ) { @@ -471,11 +474,7 @@ export default class Module { // deconflict anonymous default exports with this module's definitions const shouldDeconflict = this.exports.default && this.exports.default.isAnonymous; - if ( shouldDeconflict ) { - while ( suggestion in this.definitions ) { - suggestion = `_${suggestion}`; - } - } + if ( shouldDeconflict ) suggestion = deconflict( suggestion, this.definitions ); if ( !this.suggestedNames[ defaultOrBatch ] ) { this.suggestedNames[ defaultOrBatch ] = makeLegalIdentifier( suggestion ); diff --git a/test/function/rename-default-export/_config.js b/test/function/rename-default-export/_config.js new file mode 100644 index 0000000..24953e4 --- /dev/null +++ b/test/function/rename-default-export/_config.js @@ -0,0 +1,9 @@ +var assert = require( 'assert' ); + +module.exports = { + description: 'avoids SyntaxError with default token (#33)', + exports: function ( exports ) { + assert.equal( exports.foo, 42 ); + assert.equal( exports.bar, 42 ); + } +}; diff --git a/test/function/rename-default-export/bar.js b/test/function/rename-default-export/bar.js new file mode 100644 index 0000000..c435404 --- /dev/null +++ b/test/function/rename-default-export/bar.js @@ -0,0 +1,3 @@ +import { baz } from './baz'; + +export default baz; diff --git a/test/function/rename-default-export/baz.js b/test/function/rename-default-export/baz.js new file mode 100644 index 0000000..bc91852 --- /dev/null +++ b/test/function/rename-default-export/baz.js @@ -0,0 +1,5 @@ +function Baz () {} + +export var baz = 42; + +export default Baz( baz ); diff --git a/test/function/rename-default-export/foo.js b/test/function/rename-default-export/foo.js new file mode 100644 index 0000000..c435404 --- /dev/null +++ b/test/function/rename-default-export/foo.js @@ -0,0 +1,3 @@ +import { baz } from './baz'; + +export default baz; diff --git a/test/function/rename-default-export/main.js b/test/function/rename-default-export/main.js new file mode 100644 index 0000000..4725a8f --- /dev/null +++ b/test/function/rename-default-export/main.js @@ -0,0 +1,7 @@ +import foo from './foo'; +import bar from './bar'; + +export { + foo, + bar +};