Browse Source

various

value-tracking
Rich-Harris 8 years ago
parent
commit
2853787f32
  1. 26
      src/Bundle.js
  2. 5
      src/ast/nodes/AssignmentExpression.js
  3. 10
      src/ast/nodes/ClassDeclaration.js
  4. 7
      src/ast/nodes/ExportDefaultDeclaration.js
  5. 13
      src/ast/nodes/Identifier.js
  6. 4
      src/ast/nodes/NewExpression.js
  7. 3
      src/ast/scopes/ModuleScope.js
  8. 10
      src/ast/utils/isProgramLevel.js
  9. 1
      test/form/dedupes-external-imports/_config.js
  10. 1
      test/form/empty-if-statement/_config.js

26
src/Bundle.js

@ -138,12 +138,26 @@ export default class Bundle {
timeStart( 'phase 3' );
const exports = entryModule.getExports();
// mark statements that should appear in the bundle
if ( this.treeshake ) {
this.orderedModules.forEach( module => {
module.run();
});
// activate all export declarations
exports.forEach( name => {
const declaration = entryModule.traceExport( name );
declaration.exportName = name;
declaration.activate();
if ( declaration.isNamespace ) {
declaration.needsNamespaceBlock = true;
}
});
let settled = false;
while ( !settled ) {
settled = true;
@ -163,18 +177,6 @@ export default class Bundle {
}
}
// mark all export statements
entryModule.getExports().forEach( name => {
const declaration = entryModule.traceExport( name );
declaration.exportName = name;
declaration.activate();
if ( declaration.isNamespace ) {
declaration.needsNamespaceBlock = true;
}
});
timeEnd( 'phase 3' );
// Phase 4 – check for unused external imports, then deconflict

5
src/ast/nodes/AssignmentExpression.js

@ -1,7 +1,6 @@
import Node from '../Node.js';
import disallowIllegalReassignment from './shared/disallowIllegalReassignment.js';
import isUsedByBundle from './shared/isUsedByBundle.js';
import isProgramLevel from '../utils/isProgramLevel.js';
import { NUMBER, STRING } from '../values.js';
export default class AssignmentExpression extends Node {
@ -37,9 +36,7 @@ export default class AssignmentExpression extends Node {
initialise ( scope ) {
this.scope = scope;
if ( isProgramLevel( this ) ) {
this.module.bundle.potentialEffects.push( this );
}
this.module.bundle.potentialEffects.push( this );
super.initialise( scope );
}

10
src/ast/nodes/ClassDeclaration.js

@ -1,5 +1,11 @@
import Node from '../Node.js';
class Instance {
constructor ( _class ) {
this.class = _class;
}
}
// TODO is this basically identical to FunctionDeclaration?
export default class ClassDeclaration extends Node {
activate () {
@ -30,6 +36,10 @@ export default class ClassDeclaration extends Node {
values.add( this );
}
getInstance () {
return new Instance( this );
}
getName () {
return this.name;
}

7
src/ast/nodes/ExportDefaultDeclaration.js

@ -46,6 +46,13 @@ export default class ExportDefaultDeclaration extends Node {
return this.name;
}
mark () {
if ( this.isMarked ) return;
this.isMarked = true;
this.declaration.markChildren();
}
// TODO this is total chaos, tidy it up
render ( code, es ) {
const treeshake = this.module.bundle.treeshake;

13
src/ast/nodes/Identifier.js

@ -44,6 +44,10 @@ export default class Identifier extends Node {
}
}
getInstance () {
return this.scope.getValue( this.name ).getInstance();
}
initialise ( scope ) {
this.scope = scope;
}
@ -56,6 +60,11 @@ export default class Identifier extends Node {
markReturnStatements ( args ) {
const callee = this.scope.getValue( this.name );
if ( !callee ) {
throw new Error( `could not resolve callee ${this} ${this.locate()}` );
}
if ( !callee.markReturnStatements ) {
throw new Error( `${callee} does not have markReturnStatements method` );
}
@ -75,8 +84,4 @@ export default class Identifier extends Node {
}
}
}
run () {
if ( this.declaration ) this.declaration.activate();
}
}

4
src/ast/nodes/NewExpression.js

@ -2,6 +2,10 @@ import Node from '../Node.js';
import callHasEffects from './shared/callHasEffects.js';
export default class NewExpression extends Node {
getValue () {
return this.callee.getInstance();
}
hasEffects ( scope ) {
return callHasEffects( scope, this.callee, true );
}

3
src/ast/scopes/ModuleScope.js

@ -64,7 +64,8 @@ export default class ModuleScope extends Scope {
const imported = this.module.imports[ name ];
if ( imported ) {
const exported = imported.module.exports[ imported.name ];
return imported.module.scope.getValue( exported.localName );
const exportedName = exported.localName === 'default' && exported.identifier ? exported.identifier : exported.localName; // TODO this is a mess
return imported.module.scope.getValue( exportedName );
}
return this.parent.getValue( name );

10
src/ast/utils/isProgramLevel.js

@ -1,10 +0,0 @@
export default function isProgramLevel ( node ) {
do {
if ( node.type === 'Program' ) {
return true;
}
node = node.parent;
} while ( node && !/Function/.test( node.type ) );
return false;
}

1
test/form/dedupes-external-imports/_config.js

@ -1,5 +1,4 @@
module.exports = {
solo: true,
description: 'dedupes external imports',
options: {
external: [ 'external' ],

1
test/form/empty-if-statement/_config.js

@ -1,3 +1,4 @@
module.exports = {
solo: true,
description: 'removes an empty if statement'
};

Loading…
Cancel
Save