Skip to content

Commit 8ac6df2

Browse files
committed
Pass in the traversed filepath to the filter function
1 parent 16132d3 commit 8ac6df2

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ var list = dependencyTree.toList({
4141
* `visited`: object used for avoiding redundant subtree generations via memoization.
4242
* `nonExistent`: array used for storing the list of partial paths that do not exist
4343
* `filter`: a function used to determine if a module (and its subtree) should be included in the dependency tree
44-
- The function should accept an absolute filepath and return a boolean
45-
- If the filter returns true, the module is included in the resulting tree
44+
- The first argument given to the filter is an absolute filepath to the dependency and the second is the filepath to the currently traversed file. Should return a `Boolean`. If it returns `true`, the module is included in the resulting tree.
4645
* `detective`: object with configuration specific to detectives used to find dependencies of a file
4746
- for example `detective.amd.skipLazyLoaded: true` tells the AMD detective to omit inner requires
4847
- See [precinct's usage docs](https://github.com/dependents/node-precinct#usage) for the list of module types you can pass options to.

index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ function traverse(config) {
152152
if (config.filter) {
153153
debug('using filter function to filter out dependencies');
154154
debug('unfiltered number of dependencies: ' + dependencies.length);
155-
dependencies = dependencies.filter(config.filter);
155+
dependencies = dependencies.filter(function(filePath) {
156+
return config.filter(filePath, config.filename);
157+
});
156158
debug('filtered number of dependencies: ' + dependencies.length);
157159
}
158160

test/test.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,11 @@ describe('dependencyTree', function() {
671671
filename,
672672
directory,
673673
// Skip all 3rd party deps
674-
filter: (path) => path.indexOf('node_modules') === -1
674+
filter: (filePath, moduleFile) => {
675+
assert.ok(filePath.match('node_modules/debug/node.js'));
676+
assert.ok(moduleFile.match('test/example/onlyRealDeps/a.js'));
677+
return filePath.indexOf('node_modules') === -1;
678+
}
675679
});
676680

677681
const subTree = tree[filename];

0 commit comments

Comments
 (0)