Browse Source

more fixes

declarations-and-references
Rich-Harris 9 years ago
parent
commit
fbed0f2787
  1. 2
      src/Module.js
  2. 19
      src/Statement.js

2
src/Module.js

@ -348,7 +348,7 @@ export default class Module {
// TODO handle references inside IIFEs, and disregard
// function declarations
if ( !reference.scope.parent ) {
if ( reference.isImmediatelyUsed ) {
strongDependencies[ module.id ] = module;
}
}

19
src/Statement.js

@ -89,10 +89,12 @@ export default class Statement {
// find references
let { module, references, scope } = this;
let readDepth = 0;
walk( this.node, {
enter ( node, parent ) {
if ( node._scope ) scope = node._scope;
if ( /Function/.test( node.type ) && !isIife( node, parent ) ) readDepth += 1;
// special case – shorthand properties. because node.key === node.value,
// we can't differentiate once we've descended into the node
@ -104,7 +106,17 @@ export default class Statement {
}
if ( isReference( node, parent ) ) {
const reference = new Reference( node, scope );
// TODO this feels terribly kludgy. Function expression IDs and
// function parameters will show up in the parent scope – this
// hack fixes that
let realScope = scope;
if ( /Function/.test( parent.type ) ) {
if ( parent.type !== 'FunctionDeclaration' || node !== parent.id ) {
realScope = parent.body._scope;
}
}
const reference = new Reference( node, realScope );
references.push( reference );
if ( node.type === 'Identifier' ) {
@ -136,11 +148,14 @@ export default class Statement {
}
}
reference.isImmediatelyUsed = !readDepth;
this.skip(); // don't descend from `foo.bar.baz` into `foo.bar`
}
},
leave ( node ) {
leave ( node, parent ) {
if ( node._scope ) scope = scope.parent;
if ( /Function/.test( node.type ) && !isIife( node, parent ) ) readDepth -= 1;
}
});
}

Loading…
Cancel
Save