I\'m trying to give a broad picture of my problem. I need to write a program with Node.js that should be able to detect all dependencies a function.
E.g.
No.
Sorry, this is impossible on a pretty theoretical level in a dynamic language with eval. Good IDEs detect basic stuff, but there are some things you simply can't detect very well:
Let's take your simple case:
function a() {
//do something
b();
};
Let's complicate it a bit:
function a() {
//do something
eval("b();")
};
Now we have to detect stuff in strings, let's go one step ahead:
function a() {
//do something
eval("b"+"();");
};
Now we have to detect the result of string concats. Let's do a couple more of those:
function a() {
//do something
var d = ["b"];
eval(d.join("")+"();");
};
Still not happy? Let's encode it:
function a() {
//do something
var d = "YigpOw==";
eval(atob(d));
};
Now, these are some very basic cases, I can complicate them as much as I want. There really is no way around running the code - you'd have to run it on every possible input and check and we all know that that's impractical.
Pass dependencies as parameters to the function and use inversion of control. Always be explicit about your more complicated dependencies and not implicit. That way you won't need tools to know what your dependencies are :)