diff --git a/src/Module.js b/src/Module.js index 493fd01..876fec0 100644 --- a/src/Module.js +++ b/src/Module.js @@ -52,12 +52,12 @@ class SyntheticNamespaceDeclaration { addReference ( reference ) { // if we have e.g. `foo.bar`, we can optimise // the reference by pointing directly to `bar` - if ( reference.parts.length > 1 ) { - reference.parts.shift(); - //reference.name += `.${reference.parts[0]}`; - reference.name = reference.parts[0]; + if ( reference.parts.length ) { + reference.name = reference.parts.shift(); - const original = this.originals[ reference.parts[0]]; + reference.end += reference.name.length + 1; // TODO this is brittle + + const original = this.originals[ reference.name ]; original.addReference( reference ); return; @@ -530,14 +530,14 @@ export default class Module { const declaration = reference.declaration; if ( reference.declaration ) { - const { start, end } = reference.node; + const { start, end } = reference; const name = declaration.render( es6 ); if ( reference.name !== name ) { if ( reference.isShorthandProperty ) { magicString.insert( end, `: ${name}` ); } else { - magicString.overwrite( start, start + reference.name.length, name, true ); + magicString.overwrite( start, end, name, true ); } } } diff --git a/src/Statement.js b/src/Statement.js index d65414b..f422e17 100644 --- a/src/Statement.js +++ b/src/Statement.js @@ -19,7 +19,7 @@ function isReference ( node, parent ) { if ( node.type === 'Identifier' ) { // TODO is this right? - if ( parent.type === 'MemberExpression' ) return node === parent.object; + if ( parent.type === 'MemberExpression' ) return parent.computed || node === parent.object; // disregard the `bar` in { bar: foo } if ( parent.type === 'Property' && node !== parent.value ) return false; @@ -49,8 +49,10 @@ class Reference { root = root.object; } - this.parts.unshift( root.name ); this.name = root.name; + + this.start = node.start; + this.end = node.start + this.name.length; // can be overridden in the case of namespace members } }