ast-grep-rule-crafter
Write AST-based code search and rewrite rules using ast-grep YAML. Create linting rules, code modernizations, and API migrations with auto-fix. Use when the user mentions ast-grep, tree-sitter patterns, code search rules, lint rules with YAML, AST matching, or code refactoring patterns.
下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o ast-grep-rule-crafter.zip https://jpskill.com/download/19225.zip && unzip -o ast-grep-rule-crafter.zip && rm ast-grep-rule-crafter.zip
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/19225.zip -OutFile "$d\ast-grep-rule-crafter.zip"; Expand-Archive "$d\ast-grep-rule-crafter.zip" -DestinationPath $d -Force; ri "$d\ast-grep-rule-crafter.zip"
完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。
💾 手動でダウンロードしたい(コマンドが難しい人向け)
- 1. 下の青いボタンを押して
ast-grep-rule-crafter.zipをダウンロード - 2. ZIPファイルをダブルクリックで解凍 →
ast-grep-rule-crafterフォルダができる - 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-18
- 取得日時
- 2026-05-18
- 同梱ファイル
- 3
📖 Skill本文(日本語訳)
※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。
ast-grep Rule Crafter
ast-grep は tree-sitter を使用してコードを AST にパースし、正確なパターンマッチングを可能にします。ルールは YAML で定義され、コードのリンティング、検索、および書き換えに使用されます。
クイックスタート
id: no-console-log
language: JavaScript
rule:
pattern: console.log($$$ARGS)
fix: logger.log($$$ARGS)
message: Replace console.log with logger
プロジェクト設定
プロジェクトレベルのスキャンには sgconfig.yml 設定ファイルが必要です。
# sgconfig.yml (プロジェクトのルートディレクトリ)
ruleDirs:
- rules # ルールディレクトリ、すべての .yml ファイルを再帰的にロードします
典型的なプロジェクト構造:
my-project/
├── sgconfig.yml
├── rules/
│ ├── no-console.yml
│ └── custom/
│ └── team-rules.yml
└── src/
プロジェクトスキャンを実行します。
ast-grep scan # sgconfig.yml を自動的に検索します
ast-grep scan --config path/to/sgconfig.yml # 設定を指定します
注意:
ast-grep scanコマンドにはsgconfig.ymlが必須ですが、ast-grep run -pは単独で使用できます。
ルールワークフロー
Lint Rule (一般的)
修正せずにチェックのみを行い、CI/エディタのヒントに使用します。
# rules/no-console-log.yml
id: no-console-log
language: JavaScript
severity: warning
message: Avoid console.log in production code
rule:
pattern: console.log($$$ARGS)
検証:
ast-grep scan -r rules/no-console-log.yml src/
Rewrite Rule (オプション)
自動修正が必要な場合は fix を追加します。
id: no-console-log
language: JavaScript
severity: warning
message: Replace console.log with logger
rule:
pattern: console.log($$$ARGS)
fix: logger.log($$$ARGS)
修正を適用します。
ast-grep scan -r rules/no-console-log.yml --update-all src/
開発フロー
- [ ] 1. CLI でパターンを探索: ast-grep -p 'pattern' src/
- [ ] 2. ルールファイル (.yml) を作成
- [ ] 3. 検証: ast-grep scan -r rule.yml src/
- [ ] 4. 誤検出がある場合 → constraints を追加 → 再検証
AST 構造のデバッグ:
ast-grep -p 'console.log($ARG)' --debug-query ast
必須構文
| 要素 | 構文 | 例 |
|---|---|---|
| 単一ノード | $VAR |
console.log($MSG) |
| 複数ノード | $$$ARGS |
fn($$$ARGS) |
| 同じ内容 | 同じ名前を使用 | $A == $A |
| 非キャプチャ | $_VAR |
$_FN($_FN) |
コアルールクイックリファレンス
| タイプ | 目的 | 例 |
|---|---|---|
pattern |
コード構造に一致 | pattern: if ($COND) {} |
kind |
AST ノードタイプに一致 | kind: function_declaration |
all |
すべての条件に一致 | all: [pattern: X, kind: Y] |
any |
いずれかの条件に一致 | any: [pattern: var $A, pattern: let $A] |
not |
一致を除外 | not: {pattern: safe_call()} |
has |
子を持つ必要がある | has: {kind: return_statement} |
inside |
祖先内にある必要がある | inside: {kind: class_body} |
詳細リファレンス
完全な構文ガイド: references/rule-syntax.md を参照してください。
- アトミックルール (pattern, kind, regex, nthChild, range)
- 複合ルール (all, any, not, matches)
- 関係ルール (has, inside, follows, precedes)
- Transform と fixConfig
言語固有のパターン: references/common-patterns.md を参照してください。
- JavaScript/TypeScript の例
- Python の例
- Go と Rust の例
サポートされている言語
Bash, C, Cpp, CSharp, Css, Elixir, Go, Haskell, Hcl, Html, Java, JavaScript, Json, Kotlin, Lua, Nix, Php, Python, Ruby, Rust, Scala, Solidity, Swift, Tsx, TypeScript, Yaml
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開
ast-grep Rule Crafter
ast-grep uses tree-sitter to parse code into AST, enabling precise pattern matching. Rules are defined in YAML for linting, searching, and rewriting code.
Quick Start
id: no-console-log
language: JavaScript
rule:
pattern: console.log($$$ARGS)
fix: logger.log($$$ARGS)
message: Replace console.log with logger
Project Configuration
项目级扫描需要 sgconfig.yml 配置文件:
# sgconfig.yml (项目根目录)
ruleDirs:
- rules # 规则目录,递归加载所有 .yml 文件
典型项目结构:
my-project/
├── sgconfig.yml
├── rules/
│ ├── no-console.yml
│ └── custom/
│ └── team-rules.yml
└── src/
运行项目扫描:
ast-grep scan # 自动查找 sgconfig.yml
ast-grep scan --config path/to/sgconfig.yml # 指定配置
注意:
ast-grep scan命令必须有sgconfig.yml,而ast-grep run -p可单独使用。
Rule Workflow
Lint Rule (常见)
只检查不修复,用于 CI/编辑器提示:
# rules/no-console-log.yml
id: no-console-log
language: JavaScript
severity: warning
message: Avoid console.log in production code
rule:
pattern: console.log($$$ARGS)
验证:
ast-grep scan -r rules/no-console-log.yml src/
Rewrite Rule (可选)
需要自动修复时添加 fix:
id: no-console-log
language: JavaScript
severity: warning
message: Replace console.log with logger
rule:
pattern: console.log($$$ARGS)
fix: logger.log($$$ARGS)
应用修复:
ast-grep scan -r rules/no-console-log.yml --update-all src/
开发流程
- [ ] 1. 用 CLI 探索 pattern: ast-grep -p 'pattern' src/
- [ ] 2. 创建规则文件 (.yml)
- [ ] 3. 验证: ast-grep scan -r rule.yml src/
- [ ] 4. 如有误报 → 添加 constraints → 重新验证
调试 AST 结构:
ast-grep -p 'console.log($ARG)' --debug-query ast
Essential Syntax
| Element | Syntax | Example |
|---|---|---|
| Single node | $VAR |
console.log($MSG) |
| Multiple nodes | $$$ARGS |
fn($$$ARGS) |
| Same content | Use same name | $A == $A |
| Non-capturing | $_VAR |
$_FN($_FN) |
Core Rules Quick Reference
| Type | Purpose | Example |
|---|---|---|
pattern |
Match code structure | pattern: if ($COND) {} |
kind |
Match AST node type | kind: function_declaration |
all |
Match ALL conditions | all: [pattern: X, kind: Y] |
any |
Match ANY condition | any: [pattern: var $A, pattern: let $A] |
not |
Exclude matches | not: {pattern: safe_call()} |
has |
Must have child | has: {kind: return_statement} |
inside |
Must be in ancestor | inside: {kind: class_body} |
Detailed References
Complete syntax guide: See references/rule-syntax.md
- Atomic rules (pattern, kind, regex, nthChild, range)
- Composite rules (all, any, not, matches)
- Relational rules (has, inside, follows, precedes)
- Transform and fixConfig
Language-specific patterns: See references/common-patterns.md
- JavaScript/TypeScript examples
- Python examples
- Go and Rust examples
Supported Languages
Bash, C, Cpp, CSharp, Css, Elixir, Go, Haskell, Hcl, Html, Java, JavaScript, Json, Kotlin, Lua, Nix, Php, Python, Ruby, Rust, Scala, Solidity, Swift, Tsx, TypeScript, Yaml
同梱ファイル
※ ZIPに含まれるファイル一覧。`SKILL.md` 本体に加え、参考資料・サンプル・スクリプトが入っている場合があります。
- 📄 SKILL.md (3,803 bytes)
- 📎 references/common-patterns.md (2,251 bytes)
- 📎 references/rule-syntax.md (4,013 bytes)