From c7ef21ca947f5684356ebaf4b0f6aee1a9c918d6 Mon Sep 17 00:00:00 2001 From: "Anders D. Johnson" Date: Fri, 15 Jul 2022 23:21:53 -0500 Subject: [PATCH 1/5] feat: callback on file --- index.js | 15 ++++++++++++++- test/test.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 25702f0..8530550 100644 --- a/index.js +++ b/index.js @@ -30,7 +30,15 @@ module.exports = (src, options = {}) => { const walker = new Walker(walkerOptions); - walker.walk(src, (node) => { + // Pre-parse the source to get the AST to pass to `onFile`, + // then reuse that AST below in our walker walk. + const ast = typeof src === 'string' ? walker.parse(src) : src; + + if (options.onFile) { + options.onFile({ options, src, ast, walker }); + } + + walker.walk(ast, (node) => { switch (node.type) { case 'ImportExpression': if (!options.skipAsyncImports && node.source && node.source.value) { @@ -83,6 +91,11 @@ module.exports = (src, options = {}) => { } }); + + if (options.onAfterFile) { + options.onAfterFile({ options, src, ast, walker, dependencies }); + } + return dependencies; }; diff --git a/test/test.js b/test/test.js index 85e9140..2e62889 100644 --- a/test/test.js +++ b/test/test.js @@ -37,6 +37,48 @@ describe('detective-typescript', () => { assert.equal(deps[0], 'mylib'); }); + it('calls onFile callback', () => { + let onFileCalledArgs; + const onFile = (...args) => { + onFileCalledArgs = args; + } + + const src = 'import {foo, bar} from "mylib";' + + detective(src, { onFile }); + + assert.ok(onFileCalledArgs); + assert.ok(onFileCalledArgs[0]); + assert.equal(onFileCalledArgs[0].src, src); + assert.ok(typeof onFileCalledArgs[0].ast === 'object'); + assert.ok(typeof onFileCalledArgs[0].walker === 'object'); + + assert.ok(typeof onFileCalledArgs[0].options === 'object'); + assert.equal(onFileCalledArgs[0].options.onFile, onFile); + }); + + it('calls onAfterFile callback', () => { + let onAfterFileCalledArgs; + const onAfterFile = (...args) => { + onAfterFileCalledArgs = args; + } + + const src = 'import {foo, bar} from "mylib";' + + detective(src, { onAfterFile }); + + assert.ok(onAfterFileCalledArgs); + assert.ok(onAfterFileCalledArgs[0]); + assert.equal(onAfterFileCalledArgs[0].src, src); + assert.ok(typeof onAfterFileCalledArgs[0].ast === 'object'); + assert.ok(typeof onAfterFileCalledArgs[0].walker === 'object'); + assert.ok(Array.isArray(onAfterFileCalledArgs[0].dependencies)); + + assert.ok(typeof onAfterFileCalledArgs[0].options === 'object'); + assert.equal(onAfterFileCalledArgs[0].options.onAfterFile, onAfterFile); + }); + + it('retrieves the re-export dependencies of modules', () => { const deps = detective('export {foo, bar} from "mylib";'); assert.equal(deps.length, 1); From 890d412967085773bc61bacee99f0179c1c91ba8 Mon Sep 17 00:00:00 2001 From: "Anders D. Johnson" Date: Tue, 19 Jul 2022 21:17:02 -0500 Subject: [PATCH 2/5] docs: add options to readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 911040b..263b247 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,8 @@ const dependencies = detective(mySourceCode); - `mixedImports`: (default: `false`) Include CJS imports in dependency list - `skipAsyncImports`: (default: `false`) Whether or not to omit async imports (import('foo')) - `jsx`: (default: `false`) Enable parsing of JSX +- `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`). +- `onAfterFile`: Similar to `onFile`, but the callback is also passed an object property `dependencies`, a string array with the extracted dependencies. #### License From 9156f44a4b5aabf5cd4ad19361c69553aea6277a Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Sun, 16 Apr 2023 22:02:15 +0300 Subject: [PATCH 3/5] Update test.js --- test/test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/test.js b/test/test.js index 2e62889..aeda71e 100644 --- a/test/test.js +++ b/test/test.js @@ -78,7 +78,6 @@ describe('detective-typescript', () => { assert.equal(onAfterFileCalledArgs[0].options.onAfterFile, onAfterFile); }); - it('retrieves the re-export dependencies of modules', () => { const deps = detective('export {foo, bar} from "mylib";'); assert.equal(deps.length, 1); From 6f59c32e8e3b91a5a77d0d762e348b0421577f7f Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Sun, 16 Apr 2023 22:03:14 +0300 Subject: [PATCH 4/5] Update index.js --- index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/index.js b/index.js index 8530550..779a5fa 100644 --- a/index.js +++ b/index.js @@ -91,7 +91,6 @@ module.exports = (src, options = {}) => { } }); - if (options.onAfterFile) { options.onAfterFile({ options, src, ast, walker, dependencies }); } From 050705512e50081c14ca3e4aea28d42d5c4caec1 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Fri, 12 May 2023 21:34:37 +0300 Subject: [PATCH 5/5] Update test.js --- test/test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test.js b/test/test.js index a8eb46e..501648e 100644 --- a/test/test.js +++ b/test/test.js @@ -41,9 +41,9 @@ describe('detective-typescript', () => { let onFileCalledArgs; const onFile = (...args) => { onFileCalledArgs = args; - } + }; - const src = 'import {foo, bar} from "mylib";' + const src = 'import {foo, bar} from "mylib";'; detective(src, { onFile }); @@ -61,9 +61,9 @@ describe('detective-typescript', () => { let onAfterFileCalledArgs; const onAfterFile = (...args) => { onAfterFileCalledArgs = args; - } + }; - const src = 'import {foo, bar} from "mylib";' + const src = 'import {foo, bar} from "mylib";'; detective(src, { onAfterFile });