Hacker News new | ask | show | jobs
by NikhilVerma 1719 days ago
I've been tinkering with an AST based grep tool. (It only works for JS/TS right now). It allows you to search for code in a more free-form way and I hope better code-searching tools like this can come out. Because searching by string is really primitive. (pun intended)

Here is an example:

npx @nikhilverma/ast-grep '{ meta: { title: "___" }}' src/

4 comments

Nice!

Have been working on something similar, although my use case is more about learning how code has changed across git commits: https://github.com/bugout-dev/locust

For Javascript/Typescript/React support, like you, I hooked into the Babel toolchain. Can't recommend it highly enough.

There's also a newish project called quick-lint-js which seems to have written their own from-scratch AST parser for JS, but I haven't tried it yet: https://github.com/quick-lint/quick-lint-js

Finally, another project that I know in this space is comby (I believe it is owned/maintained by the folks at Sourcegraph): https://comby.dev/

Don't know why I dumped all those links there. Just figured there may be something useful in them for you. Am also just super passionate about building knowledge about code bases by analyzing their ASTs. Nice to meet a fellow enthusiast. :)

Thank you! It's amazing to see so much work going into tools like this :) I am a huge fan of AST and AST based tools. I think there is so much to explore in this space.
I've been toying with similar idea, also for JS/TS, but I'm struggling to come up with a search term syntax that'd feel even somewhat intuitive. Maybe "function foo" would match all functions named foo, including those defined with

  const foo = () =>
and "var foo" would just match all values named foo, including functions? I don't know, I feel like there's lots of edge cases that might make this approach fall apart
I have a working prototype for a similar thing for PHP. But I abandoned it for now, as I have no time to improve on it and the query syntax is far from perfect.
You could check out Semmle. It is trying to solve a similar problem, but with code analysis.