Browse Source

fix namespace stuff

declarations-and-references
Rich-Harris 9 years ago
parent
commit
3bedee7930
  1. 14
      src/Module.js
  2. 6
      src/Statement.js

14
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 );
}
}
}

6
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
}
}

Loading…
Cancel
Save