Browse Source

prevent deshadowing logic causing double rewrites

better-aggressive
Rich Harris 9 years ago
parent
commit
567bd38ace
  1. 4
      src/Module.js
  2. 1
      src/Statement.js
  3. 1
      test/function/deshadow-top-level-declaration/_config.js

4
src/Module.js

@ -534,6 +534,8 @@ export default class Module {
// namespace optimisation – name of `foo.bar` could be `bar` // namespace optimisation – name of `foo.bar` could be `bar`
if ( reference.name === name && name.length === reference.end - reference.start ) return; if ( reference.name === name && name.length === reference.end - reference.start ) return;
reference.rewritten = true;
// prevent local variables from shadowing renamed references // prevent local variables from shadowing renamed references
const identifier = name.match( /[^\.]+/ )[0]; const identifier = name.match( /[^\.]+/ )[0];
if ( reference.scope.contains( identifier ) ) { if ( reference.scope.contains( identifier ) ) {
@ -550,7 +552,7 @@ export default class Module {
if ( keys( toDeshadow ).length ) { if ( keys( toDeshadow ).length ) {
statement.references.forEach( reference => { statement.references.forEach( reference => {
if ( reference.name in toDeshadow ) { if ( !reference.rewritten && reference.name in toDeshadow ) {
magicString.overwrite( reference.start, reference.end, toDeshadow[ reference.name ], true ); magicString.overwrite( reference.start, reference.end, toDeshadow[ reference.name ], true );
} }
}); });

1
src/Statement.js

@ -53,6 +53,7 @@ class Reference {
this.start = node.start; this.start = node.start;
this.end = node.start + this.name.length; // can be overridden in the case of namespace members this.end = node.start + this.name.length; // can be overridden in the case of namespace members
this.rewritten = false;
} }
} }

1
test/function/deshadow-top-level-declaration/_config.js

@ -1,4 +1,3 @@
module.exports = { module.exports = {
solo: true,
description: 'deshadows top-level declarations' description: 'deshadows top-level declarations'
}; };

Loading…
Cancel
Save