Browse Source

ensure unused default exports have legal name (fixes #33)

contingency-plan
Rich-Harris 10 years ago
parent
commit
5be634f20e
  1. 23
      src/Module.js
  2. 9
      test/function/rename-default-export/_config.js
  3. 3
      test/function/rename-default-export/bar.js
  4. 5
      test/function/rename-default-export/baz.js
  5. 3
      test/function/rename-default-export/foo.js
  6. 7
      test/function/rename-default-export/main.js

23
src/Module.js

@ -12,6 +12,14 @@ import makeLegalIdentifier from './utils/makeLegalIdentifier';
const emptyArrayPromise = Promise.resolve([]); const emptyArrayPromise = Promise.resolve([]);
function deconflict ( name, names ) {
while ( name in names ) {
name = `_${name}`;
}
return name;
}
export default class Module { export default class Module {
constructor ({ path, source, bundle }) { constructor ({ path, source, bundle }) {
this.source = source; this.source = source;
@ -249,14 +257,9 @@ export default class Module {
} }
getCanonicalName ( localName ) { getCanonicalName ( localName ) {
// Special case if ( localName === 'default' && ( this.exports.default.isModified || !this.suggestedNames.default ) ) {
if ( localName === 'default' && this.exports.default && this.exports.default.isModified ) {
let canonicalName = makeLegalIdentifier( this.path.replace( this.bundle.base + '/', '' ).replace( /\.js$/, '' ) ); let canonicalName = makeLegalIdentifier( this.path.replace( this.bundle.base + '/', '' ).replace( /\.js$/, '' ) );
while ( this.definitions[ canonicalName ] ) { return deconflict( canonicalName, this.definitions );
canonicalName = `_${canonicalName}`;
}
return canonicalName;
} }
if ( this.suggestedNames[ localName ] ) { if ( this.suggestedNames[ localName ] ) {
@ -471,11 +474,7 @@ export default class Module {
// deconflict anonymous default exports with this module's definitions // deconflict anonymous default exports with this module's definitions
const shouldDeconflict = this.exports.default && this.exports.default.isAnonymous; const shouldDeconflict = this.exports.default && this.exports.default.isAnonymous;
if ( shouldDeconflict ) { if ( shouldDeconflict ) suggestion = deconflict( suggestion, this.definitions );
while ( suggestion in this.definitions ) {
suggestion = `_${suggestion}`;
}
}
if ( !this.suggestedNames[ defaultOrBatch ] ) { if ( !this.suggestedNames[ defaultOrBatch ] ) {
this.suggestedNames[ defaultOrBatch ] = makeLegalIdentifier( suggestion ); this.suggestedNames[ defaultOrBatch ] = makeLegalIdentifier( suggestion );

9
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 );
}
};

3
test/function/rename-default-export/bar.js

@ -0,0 +1,3 @@
import { baz } from './baz';
export default baz;

5
test/function/rename-default-export/baz.js

@ -0,0 +1,5 @@
function Baz () {}
export var baz = 42;
export default Baz( baz );

3
test/function/rename-default-export/foo.js

@ -0,0 +1,3 @@
import { baz } from './baz';
export default baz;

7
test/function/rename-default-export/main.js

@ -0,0 +1,7 @@
import foo from './foo';
import bar from './bar';
export {
foo,
bar
};
Loading…
Cancel
Save