This book represents the definitive technical reference for ast-grep, a structural code search, linting, and rewriting tool that operates on Abstract Syntax Trees rather than raw text. Where traditional text-based tools like grep and even regular expressions falter—unable to comprehend nested structures or immune to formatting variations—ast-grep succeeds by working directly with the syntactic representation of your code.
What ast-grep offers
Pattern-based code search that understands program structure, enabling you to find function calls regardless of argument formatting, match variable assignments while ignoring whitespace variations, and distinguish between structural code constructs and similar-looking text in strings or comments.
Beyond Documentation
This is not a repackaged version of ast-grep's documentation. While the official documentation serves as an essential reference, this book builds something more foundational: a progressive learning path that transforms you from user to expert. It organizes ast-grep's capabilities across four carefully structured levels—Introduction, Basic, Intermediate, and Advanced—each building logically upon the previous.
Building Mental Models, Not Memorizing Syntax
Following the tradition of Jeffrey Friedl's "Mastering Regular Expressions," this book prioritizes deep understanding over superficial coverage. Early chapters invest substantial depth in foundational concepts: what Abstract Syntax Trees actually represent, how tree-sitter parsers transform code into structured trees, and why pattern matching on ASTs fundamentally differs from regex matching on text. These foundations enable you to reason about ast-grep's behavior, predict edge cases, and apply the tool creatively to novel situations.
Understanding the "Why" Behind the "How"
This book doesn't simply catalog features—it reveals the reasoning behind ast-grep's design decisions. You'll understand why metavariables use the $VAR syntax, why rules compose atomic patterns through logical operators, and how the tool's architecture enables both simple searches and complex transformations. This understanding transforms you from someone who follows recipes into an expert who can solve unanticipated problems.