Today, I’m introducing GritQL, which will allow you to dig into and manipulate source code without needing to be an expert in AST or parsing. The brilliant thing about this query language for searching, evaluating, and modifying code is that any piece of code is a valid query. You simply put a code snippet between single quotes, and presto, GritQL finds all the places where this pattern appears!
Okay, you might say, “Yeah, my grep can do that too…” but GritQL is on steroids thanks to its Rust engine that can analyze repositories with 10 million lines of code in the blink of an eye. Your old grep can pack it up! 😜
You can even use variables ($like_this) in your patterns to identify code snippets and then apply transformations in a “search/replace” mode to refactor on the fly.
To install it:
curl -fsSL https://docs.grit.io/install | bash
And if, for example, you want to replace all your console.log
statements with winston.log
, it’s easy:
grit ‘‘‘‘console.log($msg) => winston.log($msg)
And bam, your code is updated!
GritQL also has a standard library with over 200 reusable patterns, so you don’t need to reinvent the wheel for common tasks. And if you have specific patterns, you can even share them with the community.
Another strong point: once you master GritQL, you can use it with a bunch of languages. Whether you code in JavaScript, Python, Java, Go, Rust, or even SQL, no need to learn 15 different tools—one is enough!
And the cherry on top, it allows you to easily include “auto-fix” rules in your patterns. This way, you can correct and clean your code as you search it. Convenient for maintaining quality without hassle! 👌
In short, I really recommend checking out GritQL on GitHub.