Get a list of all functions called by another function

In JavaScript, can I get a list of all the functions that another function calls? I want to create a tree of function dependencies, analyze how the functions in the script are related to each other (and what functions are required to execute other functions).

For instance:

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"); } 
+6
source share
4 answers

Esprima can help you. This is a Javascript parser that can help you do static code analysis.

Here is a quick example ( 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); 

Obviously, this requires a lot of help in order to offer great value, but it may give you some idea of ​​what is possible and where to start.

+13
source

Interest Ask. I also doubt the motive for this ... I hope this is just for debugging or understanding the structure of the application better.

Here is the idea of ​​WILD: Just throwing it there ...

If you can bind to each function, you can get the called:

 arguments.callee.name 

And write this to a global variable (perhaps the object with each key is the name of the function, and the value is an array of function names).

+2
source

Basically, you cannot.

Objects / functions will not know what they will execute if you did not execute them, If you do not execute regular expressions for the java-script function of the code itself .. at best, unreliable.

If you want to do this backward by tracing the stack back, these questions have solutions: How can I get a Javascript stack trace when creating an exception?

To achieve what you are probably looking for, you can create a generic class from which your functions inherit using your own implemented method to assign them function calls.

+1
source

The obvious answer is as follows:

 var origCall = Function.prototype.call; Function.prototype.call = function (thisArg) { console.log("calling a function"); var args = Array.prototype.slice.call(arguments, 1); origCall.apply(thisArg, args); }; 

But it actually immediately falls into an infinite loop, because the call to the console.log call itself makes a function call that calls console.log, which makes a function call that calls console.log , which ...

OR

I assume you want to filter your own functions. In Firefox, Function.toString() returns the body of the function, which for the native functions will be in the form:

 function addEventListener() { [native code] } 

You can map the pattern /\[native code\]/ in your loop and omit the functions that match.

-3
source

All Articles