From fbed0f27871b54bf84d963c00f0b7432c90a6918 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sun, 4 Oct 2015 13:11:23 -0400 Subject: [PATCH] more fixes --- src/Module.js | 2 +- src/Statement.js | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Module.js b/src/Module.js index e5c38a0..dd67cf0 100644 --- a/src/Module.js +++ b/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; } } diff --git a/src/Statement.js b/src/Statement.js index f422e17..e9eecb8 100644 --- a/src/Statement.js +++ b/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; } }); }