use-astgrep
ตั้งค่าและใช้งาน ast-grep สำหรับ code search, lint และ refactoring ด้วย AST-based patterns — dispatcher เข้า workflows/{scan,rewrite,transform,migration}
SKILL.mduse-astgrep/SKILL.md
GitHubGoal
Set up and use ast-grep for code search, lint, and refactoring with AST-based patterns that are more precise than regex — setup lives here, execution lives in the workflows
Scope
Covers configuring sgconfig.yml, project structure, the scan script, and dispatching into the workflows — writing permanent rules lives in /update-astgrep-rules, ad-hoc search lives in /search-with-astgrep, programmatic/scripting lives in /use-astgrep-programmatic
Execute
1. Install Or Use ast-grep
Goal: install or invoke ast-grep correctly
- If the project has
@ast-grep/cliindevDependencies, usebunx ast-grep(theast-greporsgbin from the local install) - If there is no local install, use
bunx -p @ast-grep/cli ast-grep— never run a barebunx ast-grep, because it pulls theast-greppackage (a different, abandoned package) - For regular use, add
@ast-grep/clitodevDependencies— latest0.51.0 (verified 2026-10-24)
2. Configure sgconfig.yml
Goal: create
sgconfig.ymlwith the required fields
- Create the
sgconfig.ymlfile at the root directory withruleDirspointing torules/{dependencies,architecture,glob}per the canonical layout in/update-astgrep-rules - If the project has multiple workspaces, add each workspace's rule dir to
ruleDirs(there is nodevPathsfield — the actual fields are:ruleDirs,testConfigs,utilDirs,languageGlobs,customLanguages,languageInjections) - If the project has custom file extensions, add
languageGlobs - If you want test rules, add
testConfigs - If you want reusable patterns, add
utilDirs - Verify the config with
ast-grep scan --inspect summary
3. Setup Project Structure
Goal: create the directory structure for rules and tests
- Create ast-grep rules in the
rules/directory at the project root per the canonical layout in/update-astgrep-rules(leave.agents/rules/*.mdalone — those are natural-language guidance rules owned by/update-dot-agents-project, a separate concern) - If there is a
testConfigs, create therule-tests/directory at the project root - If there is a
utilDirs, create theutils/directory at the project root - You can use
ast-grep newto scaffold a new project (orbunx -p @ast-grep/cli ast-grep newif not yet installed)
4. Add Scan Script
Goal: add a scan script to
package.json
- Add a script to
package.json:
{
"scripts": {
"scan": "ast-grep scan"
}
}
- Use
ast-grep scanorsg scanonce@ast-grep/cliis installed as a devDependency — if not yet installed, usebunx -p @ast-grep/cli ast-grep scan
5. Choose Workflow
Goal: route to the workflow that matches the task
| Task | Workflow |
|---|---|
| scan the project with rules, filter/format findings, CI | /use-astgrep scan |
batch rewrite many files with a pattern (-p/-r, dry-run → confirm → apply) |
/use-astgrep rewrite |
rule-YAML transforms (transform/rewriters/constraints) |
/use-astgrep transform |
| whole-codebase migration in staged batches | /use-astgrep migration |
| full migration plan, cutover, and rollback orchestration | /migrate — this skill supplies the rewrites only |
| ad-hoc search only (no rewrite) | /search-with-astgrep |
| batch/programmatic via Bun scripts, napi | /use-astgrep-programmatic |
write/promote permanent rules in rules/ |
/update-astgrep-rules |
- If the argument matches a workflow → read
workflows/<name>/SKILL.mdand follow the flow there - Always test a pattern on 1-2 sample files before running it across the whole project
Rules
1. CLI Reference
ast-grep newto scaffold a new project,ast-grep new ruleto create a new ruleast-grep scanto scan the whole project with rules (detailed options →workflows/scan/SKILL.md)ast-grep run --pattern 'PATTERN'for ad-hoc search (runis the default subcommand)ast-grep run -p 'PATTERN' -r 'REWRITE'for rewriting (detailed options →workflows/rewrite/SKILL.md)ast-grep testto test rules,ast-grep outlineto view structure,ast-grep lspfor the language server,ast-grep completions <shell>for completions- There is no
ast-grep rewritesubcommand — rewriting is done viarun -r/scanwith--interactiveor-U
2. Separation From Devin Rules
rules/stores.ymlast-grep rules at the project root (rules/{dependencies,architecture,glob}).agents/rules/*.mdare natural-language agent-guidance rules (architecture, dependencies, business) owned by/update-dot-agents-project— leave them, never delete them here- ast-grep
rules/*.yml+sgconfig.ymlprovide machine enforcement; the two coexist as separate concerns - Rule files use
kebab-casefilenames - Non-rule files in
rules/are ignored
3. Integration With Biome
- Use
ast-grepfor structural patterns thatBiomecannot express — it complements rather than replaces - Use
Biomefor standard lint and format
4. Rule Writing
- Writing permanent rules lives in
/update-astgrep-rules, not this workflow
5. Pattern Discipline
Use AST-based patterns for structural matching before regex
Verify syntax with
ast-grep run --pattern 'PATTERN' --debugTest a pattern on sample files before running it across the whole project
Let ast-grep auto-detect the language, or specify
--langif the extension is non-standardEvery rewrite must be dry-run + preview + confirmed before applying — the workflows already enforce this, never skip it
Use /check-code-structure if needed
Use
/deep-reviewif needed
Expected Outcome
sgconfig.ymlfully configured with all required fields- Project structure created (
rules/,rule-tests/,utils/) - Scan script added to
package.jsonand working - Tasks routed into the correct workflow (
scan/rewrite/transform/migration) - Works alongside
Biomewithout conflict