How do I go about collecting all the 3rd party libs that are used in my project and creating a list that will contain the name of the lib, and the number of times each of its methods has been called in a file eg
{
lodash: {
isArray: 5, // used 5 times in my project
isEmpty: 3
etc...
}
}
Would using an AST the only way to go about this or is there any simpler approaches?
It can be solved with help of a tool I'm working on Putout
export const find = (ast, {traverse}) => {
const result = {};
traverse(ast, {
'__a.__b()': (path) => {
const {__a, __b} = getTemplateValues(path, '__a.__b()');
const {name} = __a;
const method = __b.name;
result[name] = result[name] || {};
result[name][method] = result[name][method] || 0;
++result[name][method]
}
});
ast.program.body.push(ExpressionStatement(template.ast('(' + stringify(result) + ')')));
return [];
};
Check it out in Putout Editor
This is Finder , the most low level type of plugin.
What it does is search for all method calls, saves result
object, and then adds it to AST. Also it use getTemplateValues to get nodes according to provided template.
So for data:
lodash.isArray();
lodash.isEmpty();
lodash.isEmpty();
We will see result:
lodash.isArray();
lodash.isEmpty();
lodash.isEmpty();
({
'lodash': {
'isArray': 1,
'isEmpty': 2
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.