ast-grep is a Rust-based command-line utility designed for structural code searching, static analysis, linting, and automated source code rewriting. It uses Tree-sitter to parse source files into abstract syntax trees instead of relying exclusively on textual pattern matching. Developers can construct search patterns using familiar programming syntax and metavariables representing code structures. Declarative YAML rules support complex matching conditions, customized diagnostics, and automated fixes. Integrated rewriting functionality facilitates large-scale refactoring and library migration workflows. Multithreaded processing enables efficient analysis across extensive software repositories. Programmable interfaces are available for JavaScript, Python, and WebAssembly. Supported languages include JavaScript, TypeScript, Python, Rust, Go, Java, and C++.
Features
- Abstract syntax tree-based structural code searching through Tree-sitter parsing and language-aware pattern matching
- Intuitive code patterns with reusable metavariables for matching and capturing complex syntactic structures
- Automated source code rewriting for large-scale refactoring, API migrations, and repetitive code transformations
- Declarative YAML linting rules with relational matching conditions, customized diagnostics, and automated fixes
- Multithreaded repository scanning with extensive programming language compatibility and custom parser integration
- Programmable JavaScript, Python, and WebAssembly APIs with structured JSON reporting and rule testing tools