Browse Source

move replaceIdentifiers into Statement class

contingency-plan
Rich Harris 10 years ago
parent
commit
0477169c99
  1. 7
      src/Bundle.js
  2. 56
      src/Statement.js
  3. 8
      src/ast/analyse.js
  4. 56
      src/utils/replaceIdentifiers.js

7
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 => {

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

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

56
src/utils/replaceIdentifiers.js

@ -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 ];
}
}
});
}
Loading…
Cancel
Save