I am afraid that you are using the wrong tool for the job. jscodeshift, as the name suggests, is a tool that modifies your codebase by analyzing and transforming ASTs, and it works on the file level. In other words, it is meant for modifying file one by one, not for statically analyzing a codebase.
What you showed in the link is really using the parser and the AST tree walker (node visitor) that jscodeshift depends on to aggregate the statistics. For your goal, you need to write a nodeJS script that uses the glob
tool to find all the js files, a parser that analyzes each of the file (e.g. acorn
or @babel/parser
used as default in jscodeshift), and a matching walker (acorn-walk
or @babel/traverse
) to find the node and add it to your statistics. Here is an example using glob
, @babel/parser
and @babel/traverse
(just as an example, I haven't run the code)
import * as glob from 'glob';
import * as fs from 'node:fs';
import * as parser from '@babel/parser';
import traverse from '@babel/traverse';
glob('**/*.js', {}, (err, files) => {
if (err) {
// handle error
}
files.forEach(file => {
const code = fs.readFileSync(file, 'utf-8');
const ast = parser.parse(code, {
sourceType: 'module'
});
traverse(ast, {
ImportDeclaration (path) {
// handle your statistics
}
});
});
});