AST 構造検索
AST構造を用いてコードを検索し、関数呼び出しやクラス定義といった意味的なパターンを、テキストパターンではなく見つけ出すためのSkill。
📜 元の英語説明(参考)
Search code by AST structure using ast-grep. Find semantic patterns like function calls, imports, class definitions instead of text patterns. Triggers on: find all calls to X, search for pattern, refactor usages, find where function is used, structural search, ast-grep, sg.
🇯🇵 日本人クリエイター向け解説
AST構造を用いてコードを検索し、関数呼び出しやクラス定義といった意味的なパターンを、テキストパターンではなく見つけ出すためのSkill。
※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。
下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o structural-search.zip https://jpskill.com/download/5942.zip && unzip -o structural-search.zip && rm structural-search.zip
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/5942.zip -OutFile "$d\structural-search.zip"; Expand-Archive "$d\structural-search.zip" -DestinationPath $d -Force; ri "$d\structural-search.zip"
完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。
💾 手動でダウンロードしたい(コマンドが難しい人向け)
- 1. 下の青いボタンを押して
structural-search.zipをダウンロード - 2. ZIPファイルをダブルクリックで解凍 →
structural-searchフォルダができる - 3. そのフォルダを
C:\Users\あなたの名前\.claude\skills\(Win)または~/.claude/skills/(Mac)へ移動 - 4. Claude Code を再起動
⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。
🎯 このSkillでできること
下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。
📦 インストール方法 (3ステップ)
- 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
- 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
- 3. 展開してできたフォルダを、ホームフォルダの
.claude/skills/に置く- · macOS / Linux:
~/.claude/skills/ - · Windows:
%USERPROFILE%\.claude\skills\
- · macOS / Linux:
Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。
詳しい使い方ガイドを見る →- 最終更新
- 2026-05-17
- 取得日時
- 2026-05-18
- 同梱ファイル
- 1
📖 Skill本文(日本語訳)
※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。
構造検索
コードを抽象構文木(AST)構造で検索します。正規表現では確実に一致させることができない意味的パターンを見つけます。
ツール
| ツール | コマンド | 用途 |
|---|---|---|
| ast-grep | sg -p 'pattern' |
ASTを意識したコード検索 |
パターン構文
| パターン | 一致するもの | 例 |
|---|---|---|
$NAME |
名前付き識別子 | function $NAME() {} |
$_ |
任意の単一ノード | console.log($_) |
$$$ |
ゼロ個以上のノード | function $_($$$) {} |
必須パターン トップ10
# 1. console.log の呼び出しを検索
sg -p 'console.log($_)'
# 2. React hooks を検索
sg -p 'const [$_, $_] = useState($_)'
sg -p 'useEffect($_, [$$$])'
# 3. 関数定義を検索
sg -p 'function $NAME($$$) { $$$ }'
sg -p 'def $NAME($$$): $$$' --lang python
# 4. インポートを検索
sg -p 'import $_ from "$_"'
sg -p 'from $_ import $_' --lang python
# 5. 非同期パターンを検索
sg -p 'await $_'
sg -p 'async function $NAME($$$) { $$$ }'
# 6. エラー処理を検索
sg -p 'try { $$$ } catch ($_) { $$$ }'
sg -p 'if err != nil { $$$ }' --lang go
# 7. 潜在的な問題を検索
sg -p '$_ == $_' # === の代わりに ==
sg -p 'eval($_)' # セキュリティリスク
sg -p '$_.innerHTML = $_' # XSS ベクター
# 8. リファクタリングをプレビュー
sg -p 'console.log($_)' -r 'logger.info($_)'
# 9. リファクタリングを適用
sg -p 'var $NAME = $_' -r 'const $NAME = $_' --rewrite
# 10. 特定の言語を検索
sg -p 'pattern' --lang typescript
クイックリファレンス
| タスク | コマンド |
|---|---|
| パターンを検索 | sg -p 'pattern' |
| 特定の言語 | sg -p 'pattern' --lang python |
| 置換(プレビュー) | sg -p 'old' -r 'new' |
| 置換(適用) | sg -p 'old' -r 'new' --rewrite |
| コンテキストを表示 | sg -p 'pattern' -A 3 |
| JSON 出力 | sg -p 'pattern' --json |
| ファイルリストのみ | sg -p 'pattern' -l |
| 一致数をカウント | sg -p 'pattern' --count |
| YAML ルールを実行 | sg scan |
使用する場面
- 関数/メソッドのすべての使用箇所を見つける場合
- 特定のコードパターン(フック、API呼び出し)を特定する場合
- 大規模なリファクタリングの準備をする場合
- 正規表現が誤検知を生成する場合
- アンチパターンやセキュリティ上の問題を検出する場合
- カスタムリンティングルールを作成する場合
追加リソース
完全なパターンについては、以下をロードしてください。
./references/js-ts-patterns.md- JavaScript/TypeScript パターン./references/python-patterns.md- Python パターン./references/go-rust-patterns.md- Go および Rust パターン./references/security-patterns.md- セキュリティ脆弱性検出./references/advanced-usage.md- YAML ルールとツール統合./assets/rule-template.yaml- カスタムルール用のスターターテンプレート
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開
Structural Search
Search code by its abstract syntax tree (AST) structure. Finds semantic patterns that regex cannot match reliably.
Tools
| Tool | Command | Use For |
|---|---|---|
| ast-grep | sg -p 'pattern' |
AST-aware code search |
Pattern Syntax
| Pattern | Matches | Example |
|---|---|---|
$NAME |
Named identifier | function $NAME() {} |
$_ |
Any single node | console.log($_) |
$$$ |
Zero or more nodes | function $_($$$) {} |
Top 10 Essential Patterns
# 1. Find console.log calls
sg -p 'console.log($_)'
# 2. Find React hooks
sg -p 'const [$_, $_] = useState($_)'
sg -p 'useEffect($_, [$$$])'
# 3. Find function definitions
sg -p 'function $NAME($$$) { $$$ }'
sg -p 'def $NAME($$$): $$$' --lang python
# 4. Find imports
sg -p 'import $_ from "$_"'
sg -p 'from $_ import $_' --lang python
# 5. Find async patterns
sg -p 'await $_'
sg -p 'async function $NAME($$$) { $$$ }'
# 6. Find error handling
sg -p 'try { $$$ } catch ($_) { $$$ }'
sg -p 'if err != nil { $$$ }' --lang go
# 7. Find potential issues
sg -p '$_ == $_' # == instead of ===
sg -p 'eval($_)' # Security risk
sg -p '$_.innerHTML = $_' # XSS vector
# 8. Preview refactoring
sg -p 'console.log($_)' -r 'logger.info($_)'
# 9. Apply refactoring
sg -p 'var $NAME = $_' -r 'const $NAME = $_' --rewrite
# 10. Search specific language
sg -p 'pattern' --lang typescript
Quick Reference
| Task | Command |
|---|---|
| Find pattern | sg -p 'pattern' |
| Specific language | sg -p 'pattern' --lang python |
| Replace (preview) | sg -p 'old' -r 'new' |
| Replace (apply) | sg -p 'old' -r 'new' --rewrite |
| Show context | sg -p 'pattern' -A 3 |
| JSON output | sg -p 'pattern' --json |
| File list only | sg -p 'pattern' -l |
| Count matches | sg -p 'pattern' --count |
| Run YAML rules | sg scan |
When to Use
- Finding all usages of a function/method
- Locating specific code patterns (hooks, API calls)
- Preparing for large-scale refactoring
- When regex would match false positives
- Detecting anti-patterns and security issues
- Creating custom linting rules
Additional Resources
For complete patterns, load:
./references/js-ts-patterns.md- JavaScript/TypeScript patterns./references/python-patterns.md- Python patterns./references/go-rust-patterns.md- Go and Rust patterns./references/security-patterns.md- Security vulnerability detection./references/advanced-usage.md- YAML rules and tool integration./assets/rule-template.yaml- Starter template for custom rules