Browse Source

prevent conflicts with globals (assumed or otherwise) - fixes #20

contingency-plan
Rich-Harris 10 years ago
parent
commit
f0e78f6d1c
  1. 15
      src/Bundle.js
  2. 8
      src/Module.js
  3. 3
      test/function/deconflicts-globals/_config.js
  4. 6
      test/function/deconflicts-globals/bar.js
  5. 5
      test/function/deconflicts-globals/foo.js
  6. 5
      test/function/deconflicts-globals/main.js

15
src/Bundle.js

@ -43,6 +43,7 @@ export default class Bundle {
this.statements = [];
this.externalModules = [];
this.internalNamespaceModules = [];
this.assumedGlobals = blank();
}
fetchModule ( importee, importer ) {
@ -169,11 +170,23 @@ export default class Bundle {
module.name = name;
});
// Ensure we don't conflict with globals
keys( this.assumedGlobals ).forEach( name => {
if ( definers[ name ] ) {
conflicts[ name ] = true;
}
});
// Rename conflicting identifiers so they can live in the same scope
keys( conflicts ).forEach( name => {
const modules = definers[ name ];
modules.pop(); // the module closest to the entryModule gets away with keeping things as they are
if ( !this.assumedGlobals[ name ] ) {
// the module closest to the entryModule gets away with
// keeping things as they are, unless we have a conflict
// with a global name
modules.pop();
}
modules.forEach( module => {
const replacement = getSafeName( name );

8
src/Module.js

@ -181,6 +181,14 @@ export default class Module {
this.modifications[ name ].push( statement );
});
});
this.statements.forEach( statement => {
keys( statement.dependsOn ).forEach( name => {
if ( !this.definitions[ name ] && !this.imports[ name ] ) {
this.bundle.assumedGlobals[ name ] = true;
}
});
});
}
findDeclaration ( localName ) {

3
test/function/deconflicts-globals/_config.js

@ -0,0 +1,3 @@
module.exports = {
description: 'deconflicts names that are assumed to be global'
};

6
test/function/deconflicts-globals/bar.js

@ -0,0 +1,6 @@
import foo from './foo';
export default function() {
assert.equal( foo(), 'foo' );
return Number;
};

5
test/function/deconflicts-globals/foo.js

@ -0,0 +1,5 @@
export var Number = 42;
export default function() {
return 'foo';
};

5
test/function/deconflicts-globals/main.js

@ -0,0 +1,5 @@
import { Number } from './foo';
import bar from './bar';
assert.equal( Number, 42 );
assert.notEqual( bar(), Number );
Loading…
Cancel
Save