ast-grep
БесплатноБез исполняемых скриптовНе проверенast-grep rule writing and usage best practices. This skill should be used when writing, reviewing, or debugging ast-grep rules for code search, linting, and…
от pproenca★ 231 звезда на GitHubОб этом скилле
ast-grep Community Best Practices
Comprehensive best practices guide for ast-grep rule writing and usage, maintained by the ast-grep community. Contains 46 rules across 8 categories, prioritized by impact to guide automated rule generation and code transformation.
When to Apply
Reference these guidelines when:
- Writing new ast-grep rules for linting or search
- Debugging patterns that don't match expected code
- Optimizing rule performance for large codebases
- Setting up ast-grep projects with proper organization
- Reviewing ast-grep rules for correctness and maintainability
General Workflow
Follow this workflow when creating ast-grep rules for code search:
Step 1: Understand the Query
Clarify what you want to find:
- Target programming language
- Edge cases to handle
- What to include vs exclude
Step 2: Create Example Code
Write a sample code snippet representing the desired match pattern.
Step 3: Write the ast-grep Rule
Choose the right approach:
- Use
patternfor simple structures - Use
kindwithhas/insidefor complex structures - Combine with
all,any, ornotfor compound queries - Always use
stopBy: endfor relational rules (inside,has) to ensure complete search
Step 4: Test the Rule
# Inspect AST structure
ast-grep run --pattern '[code]' --lang [language] --debug-query=ast
# Test inline rule
echo "[code]" | ast-grep scan --inline-rules "[rule]" --stdin
# Test from file
ast-grep scan --rule [file.yml] [path]
Step 5: Search the Codebase
Deploy the validated rule:
# Search with pattern (simple matches)
ast-grep run --pattern '[pattern]' --lang [language] [path]
# Search with rule file (complex queries)
ast-grep scan --rule [file.yml] [path]
# Apply fixes interactively
ast-grep scan --rule [file.yml] --interactive [path]
Quick Tips
- Always use
stopBy: end- Ensures complete subtree traversal for relational rules - Start simple, add complexity - Begin with patterns, progress to kinds, then relational rules
- Debug with AST inspection - Use
--debug-query=astto verify structure matching - Escape in inline rules - Use
\$VARor single quotes for shell commands - Test in playground first - Use https://ast-grep.github.io/playground.html for rapid iteration
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Pattern Correctness | CRITICAL | pattern- |
| 2 | Meta Variable Usage | CRITICAL | meta- |
| 3 | Rule Composition | HIGH | compose- |
| 4 | Constraint Design | HIGH | const- |
| 5 | Rewrite Correctness | MEDIUM-HIGH | rewrite- |
| 6 | Project Organization | MEDIUM | org- |
| 7 | Performance Optimization | MEDIUM | perf- |
| 8 | Testing & Debugging | LOW-MEDIUM | test- |
Quick Reference
1. Pattern Correctness (CRITICAL)
pattern-valid-syntax- Use valid parseable code as patternspattern-language-aware- Account for language-specific syntax differencespattern-context-selector- Use context and selector for code fragmentspattern-avoid-comments-strings- Avoid matching inside comments and stringspattern-strictness-levels- Configure pattern strictness appropriatelypattern-kind-vs-pattern- Choose kind or pattern based on specificity needspattern-debug-ast- Use debug query to inspect AST structurepattern-nthchild-matching- Use nthChild for index-based positional matchingpattern-range-matching- Use range for character position matching
2. Meta Variable Usage (CRITICAL)
meta-naming-convention- Follow meta variable naming conventionsmeta-single-node- Match single AST nodes with meta variablesmeta-reuse-binding- Reuse meta variables to enforce equalitymeta-underscore-noncapture- Use underscore prefix for non-capturing matchesmeta-named-vs-unnamed- Use double dollar for unnamed node matchingmeta-multi-match-lazy- Understand multi-match variables are lazy
3. Rule Composition (HIGH)
compose-all-for-and-logic- Use all for AND logic between rulescompose-any-for-or-logic- Use any for OR logic between rulescompose-not-for-exclusion- Use not for exclusion patterns- [`com
Установить ast-grep в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
Вложенные файлы
FAQ
Что делает скилл ast-grep?
ast-grep rule writing and usage best practices. This skill should be used when writing, reviewing, or debugging ast-grep rules for code search, linting, and transformation. Triggers on tasks involving YAML rules, pattern syntax, meta variables, constraints, or code rewriting.
Как установить скилл ast-grep?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл ast-grep запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
doc-coauthoring
Guide users through a structured workflow for co-authoring documentation. Use when user wants to write documentation, proposals, technical specs, decision…
от Anthropicacademy-guide
Stop and check this skill before finishing any reply to a question about how to use Claude or a Claude product — it recommends matching courses, tutorials…
от Anthropicinternal-comms
A set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill…
от Anthropicskill-development
This skill should be used when the user wants to "create a skill", "add a skill to plugin", "write a new skill", "improve skill description", "organize skill…
от Anthropic