common-shake
common-shake copied to clipboard
Dependent uses
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.)