ast-grep
Fungsinya
ast-grep adalah alat yang mencari dan mengubah kode di banyak bahasa pemrograman. Alat ini memahami struktur kode, bukan hanya teks, untuk menemukan pola tertentu atau membuat perubahan.
Kapan dipakai
- βMenemukan semua fungsi yang menerima parameter tertentu.
- βMengganti satu potongan kode dengan potongan lain di banyak file.
- βMemeriksa apakah kode mengikuti aturan struktur tertentu.
Link skill
https://github.com/code-yeongyu/oh-my-openagent/blob/dev/packages/shared-skills/skills/ast-grep/SKILL.mdSalin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.
Cara pakai
Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.
Pesan siap tempel
Untuk Claude Code, Codex, atau Cursor: jalankan di folder proyek, lalu skill langsung terpasang.
Unduh SKILL.md lalu taruh di folder skill agen Anda.
Contoh permintaan ke AI
βCari semua pemanggilan `console.log` dan ganti dengan `logger.info` di proyek TypeScript saya.β
5 skill baru setiap minggu. 1 menit baca.
Cara kerjanya
Anda menjelaskan apa yang ingin dicari atau diubah dengan bahasa yang memahami struktur kode. Alat ini menganalisis kode Anda seolah-olah kode sungguhan, lalu melakukan modifikasi yang diperlukan.
Konten teknis (SKILL.md asli)
Teks asli dari pembuat, dalam bahasa Inggris, langsung dari GitHub.
ast-grep
sg (also installed as ast-grep) is an AST-aware search and rewrite tool across 25 languages. It treats your pattern as code, parses it the same way it parses your project, and matches structurally. It is the right tool whenever your question depends on code shape rather than text bytes.
This skill ships a Python wrapper at scripts/ast_grep_helper.py and platform install scripts at install.sh (POSIX) and install.ps1 (Windows). The helper adds offline pattern validation, the two-pass write trick, and binary auto-resolution. Use it as your default entry point.
OmO native: the ast-grep MCP tools (fastest path, no CLI needed)
OmO native also registers a bundled ast-grep MCP server. Its three tools are deferred: they are absent from the resident tool list and activate automatically the first time you call them by name β no tool_search round trip needed.
mcp__ast_grep_search({ pattern, language, paths })β AST-shape search, the MCP equivalent ofsg run -p. Prefer this over shelling out tosgfor one-off lookups: no install, no PATH issues.mcp__ast_grep_rewrite({ pattern, rewrite, language, paths, apply })β preview matches (apply: false, the default) or apply the rewrite (apply: true). Same two-pass discipline assg run -r: preview first, then apply.mcp__ast_grep_scan({ paths })β run project YAML rules (sgconfig.yml,rules/), the MCP equivalent ofsg scan.
Same pattern language as the CLI: $VAR matches one AST node, $$$VAR matches zero or more, and the pattern is code, not regex. paths accepts a file or directory list; language is required for patterns that are ambiguous across languages.
When to use this skill
Use it whenever the user's question is about code structure, not bytes:
- "Find every function that takes a
Requestparameter." - "Rewrite every
console.log(x)tologger.info(x)." - "Strip every
as anycast." - "Replace
require(...)withimportacross the repo." - "Find empty catch blocks."
- "Migrate
Optional[X]toX | None." - "Apply this codemod across these 200 files."
- "Run our YAML lint rules and surface violations."
Switch to plain grep / rg when the question is text-shaped (string literal contents, comments, license headers, file names, cross-language regex). When in doubt, ask: "does the answer depend on the language's syntax tree, or just on the file's bytes?" If the former, ast-grep. If the latter, grep.
Three things the agent must internalize
1. ast-grep is NOT regex
The wildcards are $VAR (one AST node) and $$$ (zero or more nodes). Regex syntax fails silently:
| You wrote | What ast-grep saw | What you wanted |
|---|---|---|
foo|bar |
bitwise-or of foo and bar |
run two separate searches |
.*foo |
not parseable | $$$ foo (if $$$ is a list of nodes) or use rg |
\w+ |
not parseable | $VAR to capture any identifier |
[a-z] |
character class, not parseable | switch to rg |
The full anti-pattern table is in references/pitfalls.md Β§1. The helper's validate subcommand catches these mechanically β call it before debugging "no matches" by hand.
2. Patterns must be valid code
The pattern itself must parse. def $FN($$$): fails because the trailing : makes it incomplete; use def $FN($$$). function $NAME without params/body fails; use function $NAME($$$) { $$$ }. Full table per language in references/pitfalls.md Β§2.
3. --update-all and --json are mutually exclusive (silently)
This is the single biggest gotcha when scripting. sg run -p P -r R --json --update-all returns the JSON but does not mutate files. To both preview AND apply, run two passes:
sg run -p P -r R --json=compact . # pass 1: see what would change
sg run -p P -r R --update-all . # pass 2: actually apply
The helper does this automatically when you call replace --apply. Read references/pitfalls.md Β§9.
The helper script β scripts/ast_grep_helper.py
A single-file Python 3 stdlib wrapper. Same on every OS. The agent's default entry point.
search β find all matches of a pattern
python3 scripts/ast_grep_helper.py search 'console.log($MSG)' --lang ts src/
Validates the pattern offline first. If the pattern looks like regex (\w, .*, |, etc.) the helper exits with a hint and never calls sg β saves a round-trip. Pass --force to skip validation.
Flags:
β¦
Pertanyaan umum
Apa bedanya ast-grep dengan grep (atau regex)?
grep dan regex memandang teks sebagai urutan karakter. ast-grep memahami struktur kode (seperti fungsi, variabel, dan lainnya), sehingga pencarian dan modifikasi bisa lebih presisi berdasarkan bentuk kode.
Apa fungsi karakter `$VAR` dan `$$$`?
`$VAR` digunakan untuk menangkap satu bagian struktur kode (seperti nama variabel atau fungsi). `$$$` digunakan untuk menangkap nol atau lebih bagian struktur kode.
Bagaimana cara menerapkan perubahan yang ditemukan?
Anda bisa melihat pratinjau perubahan terlebih dahulu (dry-run), lalu menjalankan perintah lagi dengan opsi apply untuk benar-benar mengubah file.
Detail
Deskripsi asli dari pembuat
Searches and rewrites code by AST shape across 25 languages. Use when the target is a syntax pattern (every call/class/import shaped like X, a codemod, a YAML rule) rather than literal text; for plain strings, comments, or filenames, use rg.
Popularitas
- Bintang GitHub
- 70k
- Peringkat
- #3128
- Lisensi
- NOASSERTION
- Diperbarui
- 15/10/2026
Komentar
- β¦
Skill lain dari repositori ini
comment-checker
70kMembantu agent Anda memahami dan menanggapi feedback dari pengecek komentar otomatis. Berguna untuk memproses hasil review kode atau konten otomatis.
lcx-contribute-bug-fix
70kMembantu agent Anda berkontribusi dalam perbaikan bug untuk proyek terkait LazyCodex. Agent membuat issue atau pull request untuk menjaga kualitas kode.
lcx-doctor
70kMemungkinkan agent Anda mengecek kesehatan instalasi LazyCodex dan Codex CLI. Berguna saat sistem berperilaku aneh setelah update atau perubahan.
lcx-report-bug
70kMembantu agent Anda melaporkan bug secara detail untuk proyek LazyCodex. Agent membuat issue berisi informasi yang berguna agar developer bisa memperbaiki masalahnya.
Skill serupa
diffs
394kMemungkinkan agent Anda menghasilkan diff perubahan yang nyata dan dapat dibagikan sebagai tautan viewer atau file gambar, bukan ringkasan yang ditulis manual.
coding-agent
394kMembantu agent Anda menyerahkan pekerjaan pemrograman besar ke Codex, Claude Code, atau OpenCode di latar belakang, lalu memeriksa kemajuannya.
gh-issues
394kMembantu agent Anda menemukan issue GitHub, memilih yang dikerjakan, dan membuka pull request perbaikan, termasuk tindak lanjut komentar tinjauan.