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.