Browse Source

mark side-effects that apply to included declarations

declarations-and-references
Rich-Harris 9 years ago
parent
commit
73c08381da
  1. 7
      src/Bundle.js
  2. 6
      src/Module.js
  3. 17
      src/Statement.js

7
src/Bundle.js

@ -55,9 +55,14 @@ export default class Bundle {
module.bindReferences(); module.bindReferences();
}); });
let settled = false;
while ( !settled ) {
settled = true;
this.modules.forEach( module => { this.modules.forEach( module => {
module.markAllSideEffects(); if ( module.markAllSideEffects() ) settled = false;
}); });
}
// mark all export statements // mark all export statements
entryModule.getExports().forEach( name => { entryModule.getExports().forEach( name => {

6
src/Module.js

@ -297,9 +297,13 @@ export default class Module {
} }
markAllSideEffects () { markAllSideEffects () {
let hasSideEffect = false;
this.statements.forEach( statement => { this.statements.forEach( statement => {
statement.markSideEffect(); if ( statement.markSideEffect() ) hasSideEffect = true;
}); });
return hasSideEffect;
} }
parse ( ast ) { parse ( ast ) {

17
src/Statement.js

@ -107,7 +107,7 @@ export default class Statement {
this.skip(); // don't descend from `foo.bar.baz` into `foo.bar` this.skip(); // don't descend from `foo.bar.baz` into `foo.bar`
} }
}, },
leave: ( node ) => { leave ( node ) {
if ( node._scope ) scope = scope.parent; if ( node._scope ) scope = scope.parent;
} }
}); });
@ -125,7 +125,10 @@ export default class Statement {
} }
markSideEffect () { markSideEffect () {
if ( this.isIncluded ) return;
const statement = this; const statement = this;
let hasSideEffect = false;
walk( this.node, { walk( this.node, {
enter ( node, parent ) { enter ( node, parent ) {
@ -134,7 +137,7 @@ export default class Statement {
// If this is a top-level call expression, or an assignment to a global, // If this is a top-level call expression, or an assignment to a global,
// this statement will need to be marked // this statement will need to be marked
if ( node.type === 'CallExpression' || node.type === 'NewExpression' ) { if ( node.type === 'CallExpression' || node.type === 'NewExpression' ) {
statement.mark(); hasSideEffect = true;
} }
else if ( node.type in modifierNodes ) { else if ( node.type in modifierNodes ) {
@ -143,11 +146,17 @@ export default class Statement {
const declaration = statement.module.trace( subject.name ); const declaration = statement.module.trace( subject.name );
// global if ( !declaration || declaration.statement.isIncluded ) {
if ( !declaration ) statement.mark(); hasSideEffect = true;
}
} }
if ( hasSideEffect ) this.skip();
} }
}); });
if ( hasSideEffect ) statement.mark();
return hasSideEffect;
} }
source () { source () {

Loading…
Cancel
Save