From 0477169c9915b297d54270dcac75a397e43f3caf Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 21 May 2015 17:17:35 -0400 Subject: [PATCH] move replaceIdentifiers into Statement class --- src/Bundle.js | 7 ++--- src/Statement.js | 56 ++++++++++++++++++++++++++++++++- src/ast/analyse.js | 8 ----- src/utils/replaceIdentifiers.js | 56 --------------------------------- 4 files changed, 57 insertions(+), 70 deletions(-) delete mode 100644 src/utils/replaceIdentifiers.js diff --git a/src/Bundle.js b/src/Bundle.js index edf60f2..ac8dd50 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -5,7 +5,6 @@ import { keys, has } from './utils/object'; import Module from './Module'; import ExternalModule from './ExternalModule'; import finalisers from './finalisers/index'; -import replaceIdentifiers from './utils/replaceIdentifiers'; import makeLegalIdentifier from './utils/makeLegalIdentifier'; import { defaultResolver } from './utils/resolvePath'; @@ -167,10 +166,10 @@ export default class Bundle { } }); - const source = statement.magicString.clone().trim(); + const source = statement.replaceIdentifiers( replacements ); // modify exports as necessary - if ( /^Export/.test( statement.node.type ) ) { + if ( statement.isExportDeclaration ) { // skip `export { foo, bar, baz }` if ( statement.node.type === 'ExportNamedDeclaration' && statement.node.specifiers.length ) { return; @@ -203,8 +202,6 @@ export default class Bundle { } } - replaceIdentifiers( statement.node, source, replacements ); - // add leading comments if ( statement.leadingComments.length ) { const commentBlock = statement.leadingComments.map( comment => { diff --git a/src/Statement.js b/src/Statement.js index 90bde0d..89932f2 100644 --- a/src/Statement.js +++ b/src/Statement.js @@ -17,7 +17,7 @@ export default class Statement { this.isIncluded = false; - this.leadingComments = [] + this.leadingComments = []; this.trailingComment = null; this.margin = [ 0, 0 ]; @@ -184,4 +184,58 @@ export default class Statement { node.arguments.forEach( arg => addNode( arg, false ) ); } } + + replaceIdentifiers ( names ) { + const magicString = this.magicString.clone().trim(); + const replacementStack = [ names ]; + const nameList = keys( names ); + + if ( nameList.length > 0 ) { + walk( this.node, { + enter ( node, parent ) { + const scope = node._scope; + + if ( scope ) { + let newNames = {}; + let hasReplacements; + + nameList.forEach( key => { + if ( !~scope.names.indexOf( key ) ) { + newNames[ key ] = names[ key ]; + hasReplacements = true; + } + }); + + if ( !hasReplacements ) { + return this.skip(); + } + + names = newNames; + replacementStack.push( newNames ); + } + + // We want to rewrite identifiers (that aren't property names) + if ( node.type !== 'Identifier' ) return; + if ( parent.type === 'MemberExpression' && node !== parent.object ) return; + if ( parent.type === 'Property' && node !== parent.value ) return; + // TODO others...? + + const name = has( names, node.name ) && names[ node.name ]; + + if ( name && name !== node.name ) { + magicString.overwrite( node.start, node.end, name ); + } + }, + + leave ( node ) { + if ( node._scope ) { + replacementStack.pop(); + names = replacementStack[ replacementStack.length - 1 ]; + } + } + }); + } + + return magicString; + } } diff --git a/src/ast/analyse.js b/src/ast/analyse.js index fe0c6dd..e8fe422 100644 --- a/src/ast/analyse.js +++ b/src/ast/analyse.js @@ -1,12 +1,4 @@ -import walk from './walk'; -import Scope from './Scope'; -import { getName } from '../utils/map-helpers'; -import { has } from '../utils/object'; -import getLocation from '../utils/getLocation'; - export default function analyse ( magicString, module ) { - let scope = new Scope(); - // first we need to generate comprehensive scope info let previousStatement = null; let commentIndex = 0; diff --git a/src/utils/replaceIdentifiers.js b/src/utils/replaceIdentifiers.js deleted file mode 100644 index da6f7db..0000000 --- a/src/utils/replaceIdentifiers.js +++ /dev/null @@ -1,56 +0,0 @@ -import walk from '../ast/walk'; -import { has } from './object'; - -export default function replaceIdentifiers ( statement, snippet, names ) { - const replacementStack = [ names ]; - - const keys = Object.keys( names ); - - if ( keys.length === 0 ) { - return; - } - - walk( statement, { - enter ( node, parent ) { - const scope = node._scope; - - if ( scope ) { - let newNames = {}; - let hasReplacements; - - keys.forEach( key => { - if ( !~scope.names.indexOf( key ) ) { - newNames[ key ] = names[ key ]; - hasReplacements = true; - } - }); - - if ( !hasReplacements ) { - return this.skip(); - } - - names = newNames; - replacementStack.push( newNames ); - } - - // We want to rewrite identifiers (that aren't property names) - if ( node.type !== 'Identifier' ) return; - if ( parent.type === 'MemberExpression' && node !== parent.object ) return; - if ( parent.type === 'Property' && node !== parent.value ) return; - // TODO others...? - - const name = has( names, node.name ) && names[ node.name ]; - - if ( name && name !== node.name ) { - snippet.overwrite( node.start, node.end, name ); - } - }, - - leave ( node ) { - if ( node._scope ) { - replacementStack.pop(); - names = replacementStack[ replacementStack.length - 1 ]; - } - } - }); -}