diff --git a/src/Module.js b/src/Module.js index 8ea8222..f7509d2 100644 --- a/src/Module.js +++ b/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`