diff --git a/src/Module.js b/src/Module.js index 6e38164..ee71c8f 100644 --- a/src/Module.js +++ b/src/Module.js @@ -4,7 +4,6 @@ import { parse } from 'acorn'; import MagicString from 'magic-string'; import Statement from './Statement'; import walk from './ast/walk'; -import analyse from './ast/analyse'; import { blank, keys } from './utils/object'; import { first, sequence } from './utils/promise'; import { isImportDeclaration, isExportDeclaration } from './utils/map-helpers'; @@ -180,12 +179,10 @@ export default class Module { this.statements.forEach( statement => { if ( isImportDeclaration( statement ) ) this.addImport( statement ); else if ( isExportDeclaration( statement ) ) this.addExport( statement ); - }); - analyse( this.magicString, this ); + statement.analyse(); - // consolidate names that are defined/modified in this module - this.statements.forEach( statement => { + // consolidate names that are defined/modified in this module keys( statement.defines ).forEach( name => { this.definitions[ name ] = statement; }); diff --git a/src/Statement.js b/src/Statement.js index bad15b6..1ed36d7 100644 --- a/src/Statement.js +++ b/src/Statement.js @@ -24,10 +24,6 @@ export default class Statement { this.isIncluded = false; - this.leadingComments = []; - this.trailingComment = null; - this.margin = [ 0, 0 ]; - // some facts about this statement... this.isImportDeclaration = node.type === 'ImportDeclaration'; this.isExportDeclaration = /^Export/.test( node.type ); diff --git a/src/ast/analyse.js b/src/ast/analyse.js deleted file mode 100644 index 21b5f4c..0000000 --- a/src/ast/analyse.js +++ /dev/null @@ -1,66 +0,0 @@ -export default function analyse ( magicString, module ) { - // first we need to generate comprehensive scope info - let previousStatement = null; - let commentIndex = 0; - - module.statements.forEach( statement => { - const node = statement.node; - - let trailing = !!previousStatement; - let previousComment; - - // TODO surely this can be neater - // attach leading comment - do { - let comment = module.comments[ commentIndex ]; - - // prevent comments inside the previous statement being - // appended to it - if ( previousStatement ) { - while ( comment && comment.start < previousStatement.node.end ) { - commentIndex += 1; - comment = module.comments[ commentIndex ]; - } - } - - if ( !comment || ( comment.end > node.start ) ) break; - - // attach any trailing comment to the previous statement - if ( trailing && !/\n/.test( module.source.slice( previousStatement.node.end, comment.start ) ) ) { - previousStatement.trailingComment = comment; - } - - // then attach leading comments to this statement - else { - statement.leadingComments.push({ - separator: previousComment ? magicString.slice( previousComment.end, comment.start ) : '\n', - comment - }); - - previousComment = comment; - } - - commentIndex += 1; - trailing = false; - } while ( module.comments[ commentIndex ] ); - - // determine margin - const previousEnd = previousComment ? - previousComment.end : - previousStatement ? - ( previousStatement.trailingComment || previousStatement.node ).end : - 0; - - //const start = ( statement.leadingComments[0] || node ).start; - - const gap = magicString.original.slice( previousEnd, node.start ); - const margin = gap.split( '\n' ).length; - - if ( previousStatement ) previousStatement.margin[1] = margin; - statement.margin[0] = margin; - - statement.analyse(); - - previousStatement = statement; - }); -}