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.

sgrep idea followup: graspjs

In the last post I mentioned about a syntax-sensitive grep tool for code
searching. Lately, when digger around to find a nodejs refactoring tool, I found
graspjs, very similar tool out there.

Grasp is a command line utility that allows you to search and replace your JavaScript code - but unlike programs such as grep or sed, it searches the structure behind your code (the abstract syntax tree), rather than simply the text you’ve written.

Pretty much the exactly idea of sgrep, isn’t it? Graspjs is
aiming at code refactoring rather that searching. So it designs a little new
language of what you are searching and capturing. The backward of it is user
needs to learn something new. The good part is that with this little
domain-specific language, code structure is much esaier to address rather
regular expression. To me, the DSL is clear and easy to use if you got some
basic concept of Mozilla’s de facto JavaScript AST.

Though graspjs should be a great code searching tool. I don’t think it would be
sufficient as a refactoring tool. Handling code in grammar level (instead of
char sequence) is good. But it is not good enough for refactoring. Imagine you
want to rename a local variable, in grammar level you could say that you want to find
the declaration and usage of it and do the replace. The bad part is in the AST
you don’t have concept of scope. So if you declare another local variable in a
different scope with the same name, graspjs would replace both of them.

So, in short, give it a shot if you are searching javascript code. But don’t use
it as a refactor tool. You desire a better one.