Skip to content

Commit 3ad99ae

Browse files
feat: callback on file (#48)
Co-authored-by: XhmikosR <xhmikosr@gmail.com>
1 parent f960074 commit 3ad99ae

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const dependencies = detective(mySourceCode);
2828
- `mixedImports`: (default: `false`) Include CJS imports in dependency list
2929
- `skipAsyncImports`: (default: `false`) Whether or not to omit async imports (import('foo'))
3030
- `jsx`: (default: `false`) Enable parsing of JSX
31+
- `onFile`: A callback that will be called before the file is processed. Intended for use with [`dependency-tree`](https://github.com/dependents/node-dependency-tree). Passed an object argument with properties `options` (echoing any options passed in, e.g., by [`precinct`](https://github.com/dependents/node-precinct)), `src` (source code for file as string), `ast` (parsed AST object for the file source), and `walker` (a `Walker` instance from [`source-walk`](https://github.com/dependents/node-source-walk) configured for TypeScript to which you can pass the `ast` or `src`).
32+
- `onAfterFile`: Similar to `onFile`, but the callback is also passed an object property `dependencies`, a string array with the extracted dependencies.
3133

3234
## License
3335

index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ module.exports = (src, options = {}) => {
2929
const walker = new Walker(walkerOptions);
3030
const dependencies = [];
3131

32+
// Pre-parse the source to get the AST to pass to `onFile`,
33+
// then reuse that AST below in our walker walk.
34+
const ast = typeof src === 'string' ? walker.parse(src) : src;
35+
36+
if (options.onFile) {
37+
options.onFile({ options, src, ast, walker });
38+
}
39+
3240
walker.walk(src, node => {
3341
switch (node.type) {
3442
case 'ImportExpression': {
@@ -100,6 +108,10 @@ module.exports = (src, options = {}) => {
100108
}
101109
});
102110

111+
if (options.onAfterFile) {
112+
options.onAfterFile({ options, src, ast, walker, dependencies });
113+
}
114+
103115
return dependencies;
104116
};
105117

test/test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,47 @@ describe('detective-typescript', () => {
3737
assert.equal(deps[0], 'mylib');
3838
});
3939

40+
it('calls onFile callback', () => {
41+
let onFileCalledArgs;
42+
const onFile = (...args) => {
43+
onFileCalledArgs = args;
44+
};
45+
46+
const src = 'import {foo, bar} from "mylib";';
47+
48+
detective(src, { onFile });
49+
50+
assert.ok(onFileCalledArgs);
51+
assert.ok(onFileCalledArgs[0]);
52+
assert.equal(onFileCalledArgs[0].src, src);
53+
assert.ok(typeof onFileCalledArgs[0].ast === 'object');
54+
assert.ok(typeof onFileCalledArgs[0].walker === 'object');
55+
56+
assert.ok(typeof onFileCalledArgs[0].options === 'object');
57+
assert.equal(onFileCalledArgs[0].options.onFile, onFile);
58+
});
59+
60+
it('calls onAfterFile callback', () => {
61+
let onAfterFileCalledArgs;
62+
const onAfterFile = (...args) => {
63+
onAfterFileCalledArgs = args;
64+
};
65+
66+
const src = 'import {foo, bar} from "mylib";';
67+
68+
detective(src, { onAfterFile });
69+
70+
assert.ok(onAfterFileCalledArgs);
71+
assert.ok(onAfterFileCalledArgs[0]);
72+
assert.equal(onAfterFileCalledArgs[0].src, src);
73+
assert.ok(typeof onAfterFileCalledArgs[0].ast === 'object');
74+
assert.ok(typeof onAfterFileCalledArgs[0].walker === 'object');
75+
assert.ok(Array.isArray(onAfterFileCalledArgs[0].dependencies));
76+
77+
assert.ok(typeof onAfterFileCalledArgs[0].options === 'object');
78+
assert.equal(onAfterFileCalledArgs[0].options.onAfterFile, onAfterFile);
79+
});
80+
4081
it('retrieves the re-export dependencies of modules', () => {
4182
const deps = detective('export {foo, bar} from "mylib";');
4283
assert.equal(deps.length, 1);

0 commit comments

Comments
 (0)