common-shake icon indicating copy to clipboard operation
common-shake copied to clipboard

Dependent uses

Open goto-bus-stop opened this issue 7 years ago • 0 comments

This patch adds dependency tracking to individual exports. If an export A is only used inside another exported function B that is not used, the A export will also be marked as unused.

To detect whether an export is used inside another export, I copied the ancestor() walker from Acorn, so the siftMember method can check if any of the ancestors of the current node also assign an export.

I added a parameter to Module#isUsed() when checking dependent uses, which tracks which dependencies have already been visited, to address recursive dependencies like:

// a.js
exports.a = function () { return require('./b').b }
// b.js
exports.b = function () { return require('./a').a }

(Initially I used a this.computingDependents array for this purpose but that didn't work when a recursive dependency crossed module boundaries.)

goto-bus-stop avatar Mar 16 '18 12:03 goto-bus-stop