Question: I am currently maintaining a large number of JS files and the dependency issue is growing over my head. Right now I have each function in a separate file and I manually maintain a database to work out the dependencies between functions.

This I would like to automate. For instance if I have the function f

```
Array.prototype.f = function() {};

```

which is referenced in another function g

```
MyObject.g = function() {
    var a = new Array();
    a.f();
};

```

I want to be able to detect that g is referencing f.

How do I go about this? Where do I start? Do I need to actually write a compiler or can I tweak Spidermonkey for instance? Did anyone else already do this?

Any pointers to get me started is very much appreciated

Thanks
Dok

Answer: 
As @bobince already suggested, doing static analysis on a JavaScript program is a close to impossible problem to crack. [Google Closure compiler](http://code.google.com/closure/compiler/) does it to some extent but then it also relies on external help from [JSDoc](http://jsdoc.sourceforge.net/) comments.

I had a [similar problem](https://stackoverflow.com/questions/2394675/javascript-object-dependencies) of finding the order in which JS files should be concatenated in a previous project, and since there were loads of JS files, manually updating the inclusion order seemed too tedious. Instead, I stuck with certain conventions of what constitutes a dependency for my purposes, and based upon that and using simple [regexp](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) :) I was able to generated the correct inclusion order.

The solution used a [topological sort](http://en.wikipedia.org/wiki/Topological_sorting) algorithm to generate a [dependency graph](http://en.wikipedia.org/wiki/Dependency_graph) which then listed the files in the order in which they should be included to satisfy all dependencies. Since each file was basically a pseudo-class using [MooTools](http://mootools.net/) syntax, there were only 3 ways dependencies could be created for my situation.

1. When a class Extended some other class.
2. When a class Implemented some other class.
3. When a class instantiated an object of some other class using the `new` keyword.

It was a simple, and definitely a broken solution for general purpose usage but it served me well. If you're interested in the solution, you can see the [code here](http://pastebin.com/MUjaW1Dn) - it's in Ruby.

If your dependencies are more complex, then perhaps you could manually list the dependencies in each JS file itself using comments and some homegrown syntax such as:

```
// requires: Array
// requires: view/TabPanel
// requires: view/TabBar

```

Then read each JS file, parse out the requires comments, and construct a dependency graph which will give you the inclusion order you need.
I have written a tool to do something like this: <http://github.com/damonsmith/js-class-loader>

It's most useful if you have a java webapp and you structure your JS code in the java style. If you do that, it can detect all of your code dependencies and bundle them up, with support for both runtime and parse-time dependencies.