diff --git a/src/Bundle.js b/src/Bundle.js index ad6410b..1f82965 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -67,7 +67,7 @@ export default class Bundle { if ( entryModule.exports.default ) { let defaultExportName = makeLegalIdentifier( basename( this.entryPath ).slice( 0, -extname( this.entryPath ).length ) ); - while ( entryModule.ast._scope.contains( defaultExportName ) ) { + while ( entryModule.scope.contains( defaultExportName ) ) { defaultExportName = `_${defaultExportName}`; } @@ -197,7 +197,7 @@ export default class Bundle { } } - replaceIdentifiers( statement, source, replacements ); + replaceIdentifiers( statement.node, source, replacements ); // add leading comments if ( statement._leadingComments.length ) { diff --git a/src/Module.js b/src/Module.js index f64260b..01ab1fe 100644 --- a/src/Module.js +++ b/src/Module.js @@ -2,6 +2,7 @@ import { relative } from 'path'; import { Promise } from 'sander'; import { parse } from 'acorn'; import MagicString from 'magic-string'; +import Statement from './Statement'; import analyse from './ast/analyse'; import { has, keys } from './utils/object'; import { sequence } from './utils/promise'; @@ -34,6 +35,10 @@ export default class Module { throw err; } + this.statements = this.ast.body.map( node => { + return new Statement( node ); + }); + this.analyse(); } @@ -42,7 +47,7 @@ export default class Module { this.imports = {}; this.exports = {}; - this.ast.body.forEach( node => { + this.statements.forEach( node => { let source; // import foo from './foo'; @@ -142,7 +147,7 @@ export default class Module { analyse( this.ast, this.code, this ); - this.definedNames = this.ast._scope.names.slice(); + this.definedNames = this.scope.names.slice(); // TODO is this used? this.canonicalNames = {}; @@ -150,7 +155,7 @@ export default class Module { this.definitionPromises = {}; this.modifications = {}; - this.ast.body.forEach( statement => { + this.statements.forEach( statement => { Object.keys( statement._defines ).forEach( name => { this.definitions[ name ] = statement; }); @@ -339,7 +344,7 @@ export default class Module { expandAllStatements ( isEntryModule ) { let allStatements = []; - return sequence( this.ast.body, statement => { + return sequence( this.statements, statement => { // skip already-included statements if ( statement._included ) return; diff --git a/src/Statement.js b/src/Statement.js new file mode 100644 index 0000000..de1f72b --- /dev/null +++ b/src/Statement.js @@ -0,0 +1,10 @@ +import { keys } from './utils/object'; + +export default class Statement { + constructor ( node ) { + // TODO remove this in favour of this.node + keys( node ).forEach( key => this[ key ] = node[ key ] ); + + this.node = node; + } +} diff --git a/src/ast/analyse.js b/src/ast/analyse.js index a0a582e..0d394ae 100644 --- a/src/ast/analyse.js +++ b/src/ast/analyse.js @@ -30,7 +30,7 @@ export default function analyse ( ast, magicString, module ) { let previousStatement = null; let commentIndex = 0; - ast.body.forEach( statement => { + module.statements.forEach( statement => { currentTopLevelStatement = statement; // so we can attach scoping info Object.defineProperties( statement, { @@ -87,7 +87,7 @@ export default function analyse ( ast, magicString, module ) { if ( previousStatement ) previousStatement._margin[1] = margin; statement._margin[0] = margin; - walk( statement, { + walk( statement.node, { enter ( node ) { let newScope; @@ -160,7 +160,7 @@ export default function analyse ( ast, magicString, module ) { // then, we need to find which top-level dependencies this statement has, // and which it potentially modifies - ast.body.forEach( statement => { + module.statements.forEach( statement => { function checkForReads ( node, parent ) { if ( node.type === 'Identifier' ) { // disregard the `bar` in `foo.bar` - these appear as Identifier nodes @@ -237,5 +237,5 @@ export default function analyse ( ast, magicString, module ) { }); }); - ast._scope = scope; + module.scope = scope; }