Browse Source

more fixes

declarations-and-references
Rich-Harris 9 years ago
parent
commit
7159c791dc
  1. 4
      src/Bundle.js
  2. 6
      src/ExternalModule.js
  3. 21
      src/Module.js
  4. 4
      src/Statement.js
  5. 6
      src/ast/Scope.js

4
src/Bundle.js

@ -58,9 +58,7 @@ export default class Bundle {
const declaration = entryModule.traceExport( name ); const declaration = entryModule.traceExport( name );
declaration.isExported = true; declaration.isExported = true;
if ( declaration.statement ) { if ( declaration.statement ) declaration.use();
declaration.statement.mark();
}
}); });
let settled = false; let settled = false;

6
src/ExternalModule.js

@ -6,8 +6,6 @@ class ExternalDeclaration {
this.module = module; this.module = module;
this.name = name; this.name = name;
this.isExternal = true; this.isExternal = true;
this.references = [];
} }
addReference ( reference ) { addReference ( reference ) {
@ -31,6 +29,10 @@ class ExternalDeclaration {
return es6 ? this.name : `${this.module.name}.${this.name}`; return es6 ? this.name : `${this.module.name}.${this.name}`;
} }
use () {
// noop?
}
} }
export default class ExternalModule { export default class ExternalModule {

21
src/Module.js

@ -14,12 +14,11 @@ class SyntheticDefaultDeclaration {
this.statement = statement; this.statement = statement;
this.name = name; this.name = name;
this.references = []; this.original = null;
this.isExported = false;
} }
addReference ( reference ) { addReference ( reference ) {
this.references.push( reference );
reference.declaration = this; reference.declaration = this;
this.name = reference.name; this.name = reference.name;
} }
@ -33,6 +32,11 @@ class SyntheticDefaultDeclaration {
this.name : this.name :
this.original.render(); this.original.render();
} }
use () {
this.isUsed = true;
this.statement.mark();
}
} }
class SyntheticNamespaceDeclaration { class SyntheticNamespaceDeclaration {
@ -40,7 +44,6 @@ class SyntheticNamespaceDeclaration {
this.module = module; this.module = module;
this.name = null; this.name = null;
this.references = [];
this.needsNamespaceBlock = false; this.needsNamespaceBlock = false;
this.originals = blank(); this.originals = blank();
@ -72,12 +75,10 @@ class SyntheticNamespaceDeclaration {
keys( this.originals ).forEach( name => { keys( this.originals ).forEach( name => {
const original = this.originals[ name ]; const original = this.originals[ name ];
original.statement.mark(); original.use();
}); });
} }
this.references.push( reference );
reference.declaration = this; reference.declaration = this;
this.name = reference.name; this.name = reference.name;
} }
@ -99,6 +100,10 @@ class SyntheticNamespaceDeclaration {
render () { render () {
return this.name; return this.name;
} }
use () {
// noop
}
} }
export default class Module { export default class Module {
@ -581,7 +586,7 @@ export default class Module {
const defaultName = defaultDeclaration.render(); const defaultName = defaultDeclaration.render();
// prevent `var undefined = sideEffectyDefault(foo)` // prevent `var undefined = sideEffectyDefault(foo)`
if ( !defaultDeclaration.isExported && !defaultDeclaration.references.length ) { if ( !defaultDeclaration.isExported && !defaultDeclaration.isUsed ) {
magicString.remove( statement.start, statement.node.declaration.start ); magicString.remove( statement.start, statement.node.declaration.start );
return; return;
} }

4
src/Statement.js

@ -155,9 +155,7 @@ export default class Statement {
this.isIncluded = true; this.isIncluded = true;
this.references.forEach( reference => { this.references.forEach( reference => {
if ( reference.declaration && reference.declaration.statement ) { if ( reference.declaration ) reference.declaration.use();
reference.declaration.statement.mark();
}
}); });
} }

6
src/ast/Scope.js

@ -35,7 +35,6 @@ function extractNames ( param ) {
class Declaration { class Declaration {
constructor () { constructor () {
this.references = [];
this.statement = null; this.statement = null;
this.name = null; this.name = null;
@ -55,6 +54,11 @@ class Declaration {
return `exports.${this.name}`; return `exports.${this.name}`;
} }
use () {
this.isUsed = true;
if ( this.statement ) this.statement.mark();
}
} }
export default class Scope { export default class Scope {

Loading…
Cancel
Save