Search Elm code (and other things) with tree-grepper

Hey folks! I’ve been working on some tools to build other tools lately. One of the things that came out of that was an AST search tool I’m calling tree-grepper. It sits on top of tree-sitter grammars and exposes the tree-sitter s-expression query API. I’m using it at NoRedInk on our giant production codebase and it’s quite fast, so I imagine it will be similarly fast on other codebases too!

Here’s a blog post I’ve written about it with some code samples:

And the repo:

Side note: I just know there’s an AST-based code transformation/refactoring tool out there that has Elm support. I wanted to include it in this post, but I just couldn’t find it again! Does anyone know what I’m talking about? If so, could you share the link here?

18 Likes

BTW: shouts out to @razze for elm-tree-sitter, which is the string I started pulling on to end up writing this tool. What a nice reusable thing you’ve made for the community! :heart:

7 Likes

Very neat!

Did you create an import graph out of the JSON output? Out of laziness to re-implement it myself, might I ask how you did that?

Yes, I end up storing something like RDF triples in a SQLite database. It’s a real duct tape and bubble gum kinda solution. Basically:

  • when we see an Elm module match, add ( module, "located-at", filename )
  • when we see an Elm import, add ( module, "imports", import )
  • when we see a JS import, add ( filename, "imports", require ), where require is the fully-qualified path if it’s a JS file, and a lookup in a table to find the module name if it’s an Elm file.
1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.