Browse Source

start moving statement logic into dedicated Statement class

contingency-plan
Rich Harris 10 years ago
parent
commit
81bd618665
  1. 4
      src/Bundle.js
  2. 13
      src/Module.js
  3. 10
      src/Statement.js
  4. 8
      src/ast/analyse.js

4
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 ) {

13
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;

10
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;
}
}

8
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;
}

Loading…
Cancel
Save