ast-grep
できること
ast-grepは、多くの言語のコードを検索し、変更するためのツールです。単なるテキストとしてではなく、コードの構造を理解して、特定のパターンを見つけたり、変更を加えたりします。
使う場面
- ✓特定のパラメータを受け取るすべての関数を見つけるとき。
- ✓複数のファイルにまたがって、あるコードを別のコードに置き換えるとき。
- ✓コードが特定の構造上のルールに従っているかを確認するとき。
スキルのリンク
https://github.com/code-yeongyu/oh-my-openagent/blob/dev/packages/shared-skills/skills/ast-grep/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“TypeScriptのプロジェクトにあるすべての`console.log`の呼び出しを見つけて、`logger.info`に置き換えてください。”
毎週5つの新しいスキル。1分で読めます。
仕組み
コードの構造を理解できる書き方で、検索または変更したい内容を記述します。ツールはコードを実際のプログラムとして解析し、必要な変更を加えます。
技術的な内容(元のSKILL.md)
作者による元のテキスト(英語)を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:
…
よくある質問
ast-grepとgrep(または正規表現)の違いは何ですか?
grepや正規表現は、テキストを文字の並びとして扱います。ast-grepはコードの構造(関数や変数など)を理解するため、コードの形に基づいて、より正確な検索や変更ができます。
`$VAR`や`$$$`とは何ですか?
`$VAR`は、コードの構造の一部(変数名や関数名など)を取り込むために使います。`$$$`は、0個以上のコードの構造の部分をまとめて取り込むために使います。
見つけた変更を適用するにはどうすればよいですか?
まず変更内容のプレビュー(ドライラン)を確認し、その後、適用オプションを付けて同じコマンドを実行すると、実際にファイルが変更されます。
詳細
作者による元の説明
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.
人気
- GitHubのスター
- 70k
- ランキング
- #3128
- ライセンス
- NOASSERTION
- 更新日
- 2026/10/15
コメント
- …
このリポジトリの他のスキル
comment-checker
70kエージェントが自動コメントチェッカーからのフィードバックを理解し、それに対応するのを支援します。自動化されたコードやコンテンツのレビュー結果の処理に役立ちます。
lcx-contribute-bug-fix
70kエージェントがLazyCodex関連プロジェクトのバグ修正に貢献するのを支援します。Issueやプルリクエストを作成し、コードの品質を保ちます。
lcx-doctor
70kエージェントがLazyCodexとCodex CLIのインストール状態を確認できるようにします。更新や変更の後に動作がおかしいときに役立ちます。
lcx-report-bug
70kエージェントがLazyCodexプロジェクトのバグを詳しく報告するのを支援します。開発者が問題を修正するのに役立つ情報を含むIssueを作成します。
似ているスキル
diffs
394k編集内容の実際の共有可能な差分を、手書きの要約ではなく、ビューアーのリンクや画像ファイルとして作成するようエージェントを支援します。
coding-agent
394kCodex、Claude Code、OpenCodeに大きめのコーディング作業をバックグラウンドのワーカーとして渡し、その進捗を確認するようエージェントを支援します。
gh-issues
394kGitHubのイシューを探し、取り組むものを選び、修正を含むプルリクエストを開くようエージェントを支援します。レビューコメントへの対応も含みます。