{"by":"morgante","descendants":54,"id":39770908,"kids":[39772612,39773587,39772233,39772073,39771635,39771223,39773775,39773440,39772204,39775184,39771043,39776801,39774945,39776431,39771473,39775496,39771619,39774499,39778783,39788726,39773378,39775769,39776320],"score":280,"text":"Hi everyone!<p>I’m excited to open source GritQL, a Rust CLI for searching and transforming source code.<p>GritQL comes from my experiences with conducting large scale refactors and migrations.<p>Usually, I would start exploring a codebase with grep. This is easy to start with, but most migrations end up accumulating additional requirements like ensuring the right packages are imported and excluding cases which don’t have a viable migration path.<p>Eventually, to build a complex migration, I usually ended up having to write a full codemod program with a tool like jscodeshift. This comes with its own problems:<p>- Most of the exploratory work has to be abandoned as you figure out how to represent your original regex search as an AST.\n- Reading&#x2F;writing a codemod requires mentally translating from AST names back to what source code actually looks like.\n- Performance is often an afterthought, so iterating on a large codemod can be painfully slow.\n- Codemod frameworks are language-specific, so if you’re hopping between multiple languages—or trying to migrate a shared API—you have to learn different tools.<p>GritQL is an attempt to develop a powerful middle ground:\n- Exploratory analysis is easy: just put a code snippet in backticks and use $metavariables for placeholders.\n- Incrementally add complexity by introducing side conditions with where clauses.\n- Reuse named patterns to avoid rebuilding queries, and use shared patterns from our standard library for common tasks like ensuring modules are imported.\n- Iterate on large codebases quickly: we use Rust for maximum performance<p>GritQL has already been used on thousands of repositories for complex migrations[1] but we&#x27;re excited to collaborate more with the open source community.<p>[1] Ex. <a href=\"https:&#x2F;&#x2F;github.com&#x2F;openai&#x2F;openai-python&#x2F;discussions&#x2F;742\">https:&#x2F;&#x2F;github.com&#x2F;openai&#x2F;openai-python&#x2F;discussions&#x2F;742</a>","time":1710962591,"title":"Show HN: GritQL, a Rust CLI for rewriting source code","type":"story","url":"https://github.com/getgrit/gritql"}