TL;DR — Quick Summary
ast-grep searches and rewrites code using AST patterns. Match structural code patterns — not text. Find deprecated APIs, enforce coding standards, refactor at scale.
ast-grep searches code by structure, not text. Find patterns, rewrite at scale — AST-powered refactoring.
Installation
# macOS
brew install ast-grep
# Cargo
cargo install ast-grep
# npm
npm install -g @ast-grep/cli
Usage
# Find console.log calls
sg -p 'console.log($ARG)' --lang js
# Replace console.log with logger
sg -p 'console.log($ARG)' -r 'logger.info($ARG)' --lang js
# Find async functions
sg -p 'async function $NAME($$$ARGS) { $$$ }' --lang js
# Interactive rewrite
sg -p 'var $X = $Y' -r 'const $X = $Y' --lang js --interactive
Summary
- ast-grep searches code using AST patterns
- Language-aware — understands syntax structure
- Rewrite patterns for large-scale refactoring
- YAML rules for CI/CD linting
- Written in Rust — fast across large codebases