AST Grep
Use this skill when text search would be noisy or unsafe.
Tools
code_search(pattern, lang, path?, limit?)— find code by AST patterncode_rewrite(pattern, rewrite, lang, path?, dry_run?)— structural find-and-replace
Examples
console.log($MSG)— find all console.log callsvar $NAME = $VAL→const $NAME = $VAL— modernize var declarationscatch ($ERR) { }— find empty catch blocksimport $X from "lodash"— find imports from a specific module
Workflow
code_search(pattern, lang, path, limit=20)— narrow path, sample first- Inspect matches — verify pattern catches what you want
code_rewrite(pattern, rewrite, lang, path, dry_run=true)— preview changes- Review the dry run output
code_rewrite(pattern, rewrite, lang, path, dry_run=false)— apply- Read changed files, run formatting/tests
Prefer other tools when
- You need plain text, comments, or strings →
grep - You need type/symbol-aware results → LSP/symbol tools if available
Safety
- Always run
dry_run=truebefore applying any rewrite - Scope with
pathto avoid unintended matches in vendored/generated code - Supported
langvalues: typescript, javascript, tsx, jsx, python, rust, go, java, c, cpp, csharp, ruby, swift, kotlin, lua, html, css, json, yaml - After applying rewrites, read changed files and run tests/formatting before moving on
Troubleshooting
- No matches: verify
langis correct (e.g.typescriptnotts), broaden pattern - Too many matches: narrow
path, add more structure to pattern, lowerlimit - Pattern error: simplify — patterns must be valid syntax in the target language
- Wrong language:
typescript≠tsx,javascript≠jsx— run separate passes