This week's tip of the week is ast-grep.
Have you ever needed to update code across a large codebase and immediately regretted opening the search box? Regex can get you surprisingly far, but eventually you hit the wall. Code is structured. Text search is not.
That's where ast-grep comes in. It lets you search and rewrite code based on its abstract syntax tree (AST) instead of matching raw text. It understands the structure of your code, which means you can find things that would be painful, fragile, or downright impossible with traditional search.
For example, find every console.log call:
ast-grep --pattern 'console.log($$$)'Or find every slog.New call in a Go progam:
ast-grep --lang go --pattern 'slog.New($$$)'Or find every React useEffect:
ast-grep --pattern 'useEffect($$$)'You can even perform safe code transformations across an entire project. Let's say your team deprecated a function oldApi(user) and replaced it with newApi(user). Instead of manually hunting through hundreds of files, ast-grep can find every usage and even rewrite them for you.
The real magic happens when you start creating reusable rules. Teams can use ast-grep for code migrations, enforcing conventions, detecting anti-patterns, and cleaning up large refactors.
If you've ever written a regex that looked like Klingon just to find a function call, ast-grep is worth a look.
That's it! Short and sweet. Until the next one!

