ast-grep
Fast CLI for structural code search, linting, and rewriting by AST pattern.
Repository Health
Technical Analysis
ast-grep (command sg) is a command-line tool for searching, linting, and rewriting code by its abstract syntax tree instead of by text. You write patterns that look like ordinary code, using $ wildcards such as $MATCH to match any AST node, and ast-grep finds every fragment with the same syntactic structure across your codebase.
Powered by tree-sitter, it supports many programming languages and can run one-off searches and rewrites from the terminal or codify reusable rules in YAML for project-wide linting and codemods. The ast-grep-cli PyPI package distributes the prebuilt binary so you can install it with pip.
What You Get
- The
sg/ast-grepCLI for structural search across a codebase - Pattern syntax using real code plus
$VARmetavariable wildcards - Automated code rewriting and codemods from the command line
- YAML rule files for reusable, project-wide linting and refactors
- tree-sitter-backed multi-language support and an interactive scan mode
Common Use Cases
- Searching a codebase for a syntactic pattern instead of a text regex
- Running large-scale automated refactors and codemods
- Enforcing custom lint rules defined as YAML patterns in CI
- Auditing code for structural anti-patterns during review
Under The Hood
Architecture The project is a Rust workspace: a core matching engine (ast-grep-core) turns patterns into tree-sitter queries and matches nodes with metavariable binding, language crates register grammars, a config crate parses YAML rule files, and the sg binary orchestrates parallel file walking, scanning, and rewriting with an interactive TUI mode. Tech Stack Rust throughout, built on tree-sitter for parsing, with additional npm and pip distribution wrappers that ship the compiled binary; the repo also exposes N-API and napi bindings for JavaScript. Code Quality It is a mature, very actively maintained project with Codecov-tracked coverage, CI workflows, and a large contributor base, reflecting high engineering standards. API Design / DX The CLI is grep-like and approachable for simple searches, while the pattern and YAML-rule DSL introduces a learning curve for advanced rewrites; excellent documentation, an online playground, and clear error output substantially smooth adoption.