Browse Source

deshadowing

declarations-and-references
Rich-Harris 9 years ago
parent
commit
db825931db
  1. 16
      src/Module.js

16
src/Module.js

@ -532,6 +532,8 @@ export default class Module {
magicString.overwrite( statement.end, statement.next, ';\n' ); // TODO account for trailing newlines
}
let toDeshadow = blank();
statement.references.forEach( reference => {
const declaration = reference.declaration;
@ -543,6 +545,12 @@ export default class Module {
// namespace optimisation – name of `foo.bar` could be `bar`
if ( reference.name === name && name.length === reference.end - reference.start ) return;
// prevent local variables from shadowing renamed references
const identifier = name.match( /[^\.]+/ )[0];
if ( reference.scope.contains( identifier ) ) {
toDeshadow[ identifier ] = `${identifier}$$`; // TODO more robust mechanism
}
if ( reference.isShorthandProperty ) {
magicString.insert( end, `: ${name}` );
} else {
@ -551,6 +559,14 @@ export default class Module {
}
});
if ( keys( toDeshadow ).length ) {
statement.references.forEach( reference => {
if ( reference.name in toDeshadow ) {
magicString.overwrite( reference.start, reference.end, toDeshadow[ reference.name ], true );
}
});
}
// modify exports as necessary
if ( statement.isExportDeclaration ) {
// remove `export` from `export var foo = 42`

Loading…
Cancel
Save