tern: a tool to analyze javascript source code

I use NodeJS and JavaScript a lot for web development. I love it because it is
a lightweight language comparing to Java, C++. It has many handy feature brings
from function programming, like clourse, anonymous function and function as
first-class object.
NodeJS’s “callback-driven” style is also quiet staightforward and simple model to handle
async operation in single thread.

And after you get familiar with a language. You may frequently feel like some
parts of coding should be done automatically. To get these jobs automatically
done, we need to teach computers to get some kinds of “understanding” of the
language and source code, and also need to teach them what the job is.

The first task is hard for all most every popular language. Languages have their
dark part. C has a very clear semantic. But the macro and preprocessor
instructions could be a mess. C++ has its template system. And for dynamic
language with eval, you could never statically know excatly what will happens in
the runtime. Besides of that, features like delay-binding also could obstruct
building a static analyzing tool to understand source code.

Luckily there is a theory for this. It is so called abstract
interpretation. i.e. you run the code line by line, constructing variables from
others. But all the variables does not contain any real data. Instead, they
contains infomation you are interesting like possible integer range.

tern is that kind of library for JavaScript. The author (not me, it is the same
author of acorn and CodeMirror) built up abstract interpretation to keep track
of type infomation for variables and functions. With those infomation, tern
could be able to determine type of variables and functions. In addition, it is
scope-sensitive, so the result could also be used for refactoring.