Overview
AST-grep tools provide structural code search and transformation using abstract syntax tree (AST) pattern matching. Unlike regex-based search, AST-grep understands code structure and ignores formatting differences. Source:src/tools/ast-grep/
Features
- 25 supported languages (see full list below)
- Meta-variables for pattern capture (
$VAR,$$$) - Rewriting capabilities with pattern templates
- Context-aware matching (ignores whitespace/formatting)
- Fast execution with result limits and timeouts
Tools
ast_grep_search
Search code patterns using AST-aware matching.AST pattern with meta-variablesMeta-variables:
$VAR: Single AST node (identifier, expression, etc.)$$$: Multiple nodes (zero or more)
- ✅
"export async function $NAME($$$) { $$$ }" - ❌
"export async function $NAME"(incomplete)
Target language (25 options)See Supported Languages for full list.
Paths to search (default:
["."])Example: ["/workspace/src", "/workspace/tests"]Include/exclude glob patternsPrefix
! to exclude:["*.ts"]: Only.tsfiles["!*.test.ts"]: Exclude test files["*.tsx", "!**/node_modules/**"]:.tsxfiles, skipnode_modules
Context lines around match (default: 0)Example:
context: 3 shows 3 lines before/after matchMatch results with file, line, and matched codeExample:If more than 500 matches (configurable):
tools.ts:39
ast_grep_replace
Replace code patterns with AST-aware rewriting.AST pattern to match (same as search)
Replacement pattern (can use
$VAR from pattern)Meta-variables from pattern are available in rewrite.Target language
Paths to search (default:
["."])Include/exclude glob patterns
Preview changes without applying (default:
true)Critical: Set dryRun: false to apply changes.Rewrite results with diffDry-run output:Applied output:
tools.ts:82
Supported Languages
25 languages with full AST support:| Language | Extensions | Example Pattern |
|---|---|---|
bash | .bash, .sh, .zsh | "if [[ $CONDITION ]]; then" |
c | .c, .h | "int $NAME($$$) { $$$ }" |
cpp | .cpp, .hpp | "class $NAME { $$$ }" |
csharp | .cs | "public class $NAME { $$$ }" |
css | .css | ".$CLASS { $$$ }" |
elixir | .ex, .exs | "def $NAME($$$) do $$$ end" |
go | .go | "func $NAME($$$) { $$$ }" |
haskell | .hs | "$NAME :: $$$" |
html | .html, .htm | "<div class=\"$CLASS\">$$$</div>" |
java | .java | "public class $NAME { $$$ }" |
javascript | .js, .jsx, .mjs, .cjs | "function $NAME($$$) { $$$ }" |
json | .json | "{ \"$KEY\": $$$ }" |
kotlin | .kt, .kts | "fun $NAME($$$) { $$$ }" |
lua | .lua | "function $NAME($$$) $$$ end" |
nix | .nix | "{ $$$, $NAME, $$$ }:" |
php | .php | "function $NAME($$$) { $$$ }" |
python | .py, .pyi | "def $NAME($$$):" |
ruby | .rb, .rake | "def $NAME($$$) $$$ end" |
rust | .rs | "fn $NAME($$$) -> $$$ { $$$ }" |
scala | .scala, .sc | "def $NAME($$$): $$$ = { $$$ }" |
solidity | .sol | "function $NAME($$$) public { $$$ }" |
swift | .swift | "func $NAME($$$) -> $$$ { $$$ }" |
typescript | .ts, .cts, .mts | "function $NAME($$$): $$$ { $$$ }" |
tsx | .tsx | "export const $NAME: React.FC = () => { $$$ }" |
yaml | .yml, .yaml | "$KEY: $$$" |
language-support.ts:2
Pattern Syntax
Meta-Variables
$VAR: Matches single AST node (identifier, expression, statement)$$$: Matches multiple nodes (zero or more arguments, statements, etc.)
Naming Convention
- Uppercase recommended:
$NAME,$HANDLER,$ARG - Descriptive names improve readability
Complete AST Nodes
Patterns must be valid code that forms a complete AST node. ✅ Valid:Language-Specific Notes
Python: Remove trailing colons from patternsError Handling
Empty Results with Hint
For Python trailing colon:Pattern Parse Error
Timeout
paths or globs.
Configuration
Constants (constants.ts:33):
Use Cases
Refactoring
API Migration
Code Audit
Best Practices
✅ Do
- Use complete AST nodes in patterns
- Test patterns with search before applying replace
- Use dry-run (default) to preview changes
- Narrow scope with
pathsandglobsfor large codebases - Use descriptive meta-variable names (
$HANDLERnot$X)
❌ Don’t
- Don’t use incomplete patterns — include full syntax
- Don’t skip dry-run for destructive changes
- Don’t use AST-grep for simple text search — use
grepinstead - Don’t forget language-specific quirks (Python colons, etc.)
Related Tools
- grep: Regex-based search (simpler, faster for text patterns)
- lsp_find_references: Symbol-based search (more precise for refactoring)
- hashline_edit: Safe editing (alternative to
ast_grep_replace)
Source Files
| File | Purpose |
|---|---|
tools.ts:38 | Tool factories and execution |
cli.ts | CLI invocation and process management |
language-support.ts:2 | Language list and extensions |
result-formatter.ts | Format search/replace output |
types.ts | TypeScript types for matches/results |
constants.ts:33 | Timeout/limit configuration |