Browse Source

virtual scopes optionally inherit the parent scope

* locals should inherit references to global variables
* exports on the other hand should not
gh-109
Oskar Segersvärd 9 years ago
parent
commit
a05997b9c5
  1. 2
      src/ExternalModule.js
  2. 13
      src/Module.js
  3. 7
      src/Scope.js

2
src/ExternalModule.js

@ -22,7 +22,7 @@ export default class ExternalModule {
this.needsNamed = false; this.needsNamed = false;
this.needsAll = false; this.needsAll = false;
this.exports = bundle.scope.virtual(); this.exports = bundle.scope.virtual( false );
const { reference } = this.exports; const { reference } = this.exports;

13
src/Module.js

@ -51,14 +51,16 @@ export default class Module {
this.resolvedIds = blank(); this.resolvedIds = blank();
// Virtual scopes for the local and exported names. // Virtual scopes for the local and exported names.
this.locals = bundle.scope.virtual(); this.locals = bundle.scope.virtual( true );
this.exports = bundle.scope.virtual(); this.exports = bundle.scope.virtual( false );
const { reference, inScope } = this.exports; const { reference, inScope } = this.exports;
this.exports.reference = name => { this.exports.reference = name => {
// If we have it, grab it. // If we have it, grab it.
if ( inScope.call( this.exports, name ) ) return reference.call( this.exports, name ); if ( inScope.call( this.exports, name ) ) {
return reference.call( this.exports, name );
}
// ... otherwise search exportAlls. // ... otherwise search exportAlls.
for ( const module of this.exportAlls ) { for ( const module of this.exportAlls ) {
@ -67,7 +69,8 @@ export default class Module {
} }
} }
throw new Error( `The name ${name} is never exported!` ); // throw new Error( `The name "${name}" is never exported (from ${this.id})!` );
return reference.call( this.exports, name );
}; };
this.exports.inScope = name => { this.exports.inScope = name => {
@ -185,7 +188,7 @@ export default class Module {
name = declaration.id.name; name = declaration.id.name;
} }
this.locals.define({ this.locals.define( name, {
originalName: name, originalName: name,
name, name,

7
src/Scope.js

@ -143,11 +143,10 @@ export default class Scope {
} }
// Create and return a virtual `Scope` instance, bound to // Create and return a virtual `Scope` instance, bound to
// the actual scope of `this`. // the actual scope of `this`, optionally inherit the parent scope.
virtual () { virtual ( inheritParent ) {
const scope = new Scope(); const scope = new Scope( inheritParent ? this.parent : null );
scope.ids = this.ids; scope.ids = this.ids;
scope.parent = this.parent;
return scope; return scope;
} }
} }

Loading…
Cancel
Save