And*_*een 6 javascript dependency-graph
在JavaScript中,是否可以获取由另一个函数调用的所有函数的列表?我想创建一个函数依赖树,分析脚本中的函数如何相互关联(以及哪些函数需要哪些函数).
例如:
getAllCalledFunctions(funcA); //this should return [funcB, funcC, funcD], since these are the functions that are required by funcA.
function getAllCalledFunctions(functionName){
//how should I implement this?
}
function funcA(){
funcB();
funcC();
}
function funcB(){
funcD();
}
function funcC(){
funcD();
}
function funcD(){
console.log("This function is called by funcC and funcD");
}
Run Code Online (Sandbox Code Playgroud)
Tre*_*xon 14
Esprima可以帮到你.它是一个Javascript解析器,可以帮助您进行静态代码分析.
这是一个简单的例子(http://jsfiddle.net/fyBvT/):
var code = 'function funcA() { funcB(); funcC(); } function funcB(){ funcD(); } function funcC() { funcD(); } function funcD(){ console.log("This function is called by funcC and funcD"); }';
var syntax = esprima.parse(code);
var funcs = [];
_.each(syntax.body, function(i) {
if (i.type == 'FunctionDeclaration') {
var func = {name: i.id.name};
_.each(i.body.body, function(j) {
if (j.type == 'ExpressionStatement' && j.expression.type == 'CallExpression') {
func.calls = func.calls || [];
func.calls.push(j.expression.callee.name);
}
});
funcs.push(func);
}
});
console.log(funcs);
Run Code Online (Sandbox Code Playgroud)
显然,这需要很多帮助来提供很多价值,但它可能会让你知道什么是可能的,从哪里开始.
| 归档时间: |
|
| 查看次数: |
2729 次 |
| 最近记录: |