Announcing the Book: Mastering ast-grep

Since the release of ast-grep, I have watched the community grow and the tool evolve. The documentation website has expanded alongside the feature set, serving as a reliable reference for command-line flags, YAML schema definitions, and rule syntax. However, as the tool has matured, I realized there was a gap between looking up how a feature works and understanding how to architect a complex code transformation system.
I frequently see users asking questions about specific API design choices, misunderstanding the nuances of the interface, or having a hard time wiring complex rules together effectively. These common hurdles highlighted the need for a place to articulate the tool's design principles and provide a thorough, end-to-end explanation of how the system functions as a whole.
Today, I am proud to announce the release of Mastering ast-grep, a book designed to bridge that gap.
From Reference to Narrative
The documentation is non-linear; you jump to the section you need. Mastering ast-grep, conversely, is built on a storyline. It is a technical tutorial that progresses systematically from foundational concepts to advanced applications.
While the official documentation answers the "what," it often lacks the space to fully explore the "how" and the "why." The book guides you through three distinct phases of mastery:
- Foundational Mental Models: We start by dismantling the assumption that text search is sufficient for code. We explore why Abstract Syntax Trees (ASTs) are the correct abstraction for code manipulation and how the underlying parsers actually see your source files.
- Practical Application: We move quickly into pattern-based searching, covering the core syntax (
$VAR,$$$), rule composition, and the "smart" matching algorithm that makes ast-grep feel intuitive. - Architectural Integration: Finally, we tackle the complex reality of production environments. This includes project configuration with
sgconfig.yml, testing frameworks, and integration with editors via the Language Server Protocol (LSP).
The "Why" Behind the Design
One of the main motivations for writing this book was the freedom to explore topics that feel out of place in terse documentation.
In the book, I have the room to dive deep into the engineering decisions that power the tool. The book explores the specific trade-offs of the regex engine used (automata-based vs. backtracking) and why that matters for preventing ReDoS attacks in CI pipelines. We discuss the nuances of UTF-8 encoding in source parsing. We dissect the strictness levels—cst, smart, ast, and signature—not just as configuration options, but as distinct strategies for balancing precision and flexibility.
This context transforms the tool from a "black box" into a transparent instrument that you can reason about and predict.
How the Book is Organized
The book is structured to guide you through four distinct phases of mastery, moving from simple concepts to sophisticated engineering. We start with the Introduction, establishing the conceptual foundation by dismantling the assumption that text search is sufficient for code and exploring why Abstract Syntax Trees are the correct abstraction for analysis. The Basic section then introduces the core mechanics of pattern-based searching, teaching you to write patterns using metavariables, compose YAML rules, apply logical operators, and perform basic code rewrites. From there, we transition to the Intermediate section, where the focus shifts from writing isolated rules to building production-ready systems. This covers refining ambiguous patterns with context, configuring projects via sgconfig.yml, establishing robust testing workflows, and integrating ast-grep into your editor via the Language Server Protocol. The journey concludes with the Advanced section, which explores the full depth of the tool's capabilities. We dive into strictness levels (cst, smart, ast), recursive utility rules, complex transformations using rewriters, and leveraging the programmatic API to embed structural search into your own applications.
Supporting the Project
Open source is a labor of love, but it is also labor. Mastering ast-grep is available for purchase today. Buying a copy is the most direct way to support the ongoing development and maintenance of the ast-grep project financially.
By purchasing the book, you are not only investing in your own capability to manipulate code at scale, but you are also ensuring the sustainability of the tool itself.
Whether you are looking to automate a massive refactor, enforce strict code quality standards, or simply stop wrestling with fragile regular expressions, I believe this book provides the foundation you need.