ast-grep · pproenca — скилл для Claude Code: правила поиска и замены кода
Скилл с набором правил по написанию и отладке ast-grep-запросов: поиск по AST, линтинг и переписывание кода.
npx skills add pproenca/dot-skills --skill ast-grep -a claude-codeЧто делает ast-grep
Скилл описывает, как писать YAML-правила для ast-grep и как их отлаживать. Внутри 46 правил, разбитых на 8 категорий по приоритету: корректность паттернов, метапеременные, композиция правил, ограничения, переписывание, организация проекта, производительность и тестирование. Каждое правило лежит отдельным файлом в references/.
Работает по шагам: сначала уточняете, что ищете, потом пишете пример кода, затем само правило. Для простых структур хватает pattern, для сложных — kind вместе с has или inside, плюс связки all, any, not. Отладка идёт через ast-grep run --pattern '[code]' --lang [language] --debug-query=ast, а проверить правило можно на stdin через ast-grep scan --inline-rules.
Один момент ломает поиск чаще всего: в правилах с inside и has нужно ставить stopBy: end, иначе обход поддерева обрывается и совпадения теряются. В inline-правилах для шелла экранируйте \$VAR или берите правило в одинарные кавычки. Скилл ставится как файл в каталог скиллов агента, отдельного CLI он не приносит — сам ast-grep нужен в системе.
Какие задачи решает
- написать YAML-правило для поиска конструкции по AST
- починить паттерн, который не находит ожидаемый код
- настроить линтинг с автоматической заменой
- разложить правила ast-grep по файлам проекта
- ускорить поиск по большой кодовой базе
Примеры запросов агенту
- Напиши ast-grep-правило, которое найдёт все вызовы console.log в src
- Почему это правило не матчит мой код? Проверь AST через --debug-query=ast
- Сделай правило с inside и has для поиска методов внутри класса
Как установить ast-grep
1Через Skills CLI (Vercel)
npx skills add pproenca/dot-skills --skill ast-grep -a claude-code2Вручную: положить файлы
~/.claude/skills/ast-grep/SKILL.mdПапка скилла целиком; для одного проекта — .claude/skills/ast-grep/
1Через Skills CLI (Vercel)
npx skills add pproenca/dot-skills --skill ast-grep -a codex2Вручную: положить файлы
.agents/skills/ast-grep/SKILL.mdДля всех проектов — ~/.agents/skills/
1Через Skills CLI (Vercel)
npx skills add pproenca/dot-skills --skill ast-grep -a cursor2Вручную: положить файлы
.cursor/skills/ast-grep/SKILL.mdCursor читает и .agents/skills/, и .claude/skills/
1GitHub CLI
gh skill install pproenca/dot-skills ast-grep2Вручную: положить файлы
.github/skills/ast-grep/SKILL.md1Через Skills CLI (Vercel)
npx skills add pproenca/dot-skills --skill ast-grep -a gemini-cli2Командой платформы
gemini skills install https://github.com/pproenca/dot-skills --path skills/.experimental/ast-grepВручную: положить файлы
.devin/skills/ast-grep/SKILL.mdСтарый путь .windsurf/skills/ тоже читается
Через Skills CLI (Vercel)
npx skills add pproenca/dot-skills --skill ast-grep -a opencodeClaude.ai
Упакуйте папку в zip и загрузите в Settings → Capabilities → Skills
По шагам, с командами для каждой платформы: Как установить скилл в Claude Code: пять способов
Вопросы о ast-grep
Как установить ast-grep в Claude?
Выполните npx skills add pproenca/dot-skills --skill ast-grep -a claude-code. Скилл окажется в ~/.claude/skills/ast-grep/SKILL.md.
Что делает ast-grep?
Это скилл с практиками написания правил ast-grep: поиск по синтаксическому дереву, линтинг и переписывание кода. Он помогает агенту писать и отлаживать YAML-правила.
Чем ast-grep отличается от обычного поиска по тексту?
Правила матчат узлы AST, а не строки, поэтому находят конструкции с учётом синтаксиса языка. Для этого и нужны kind, has, inside и метапеременные.
Исходный текст
SKILL.md · MIT
--- name: ast-grep description: 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 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 `pattern` for simple structures - Use `kind` with `has`/`inside` for complex structures - Combine with `all`, `any`, or `not` for compound queries - **Always use `stopBy: end` for relational rules** (`inside`, `has`) to ensure complete search ### Step 4: Test the Rule ```bash # 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: ```bash # 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 1. **Always use `stopBy: end`** - Ensures complete subtree traversal for relational rules 2. **Start simple, add complexity** - Begin with patterns, progress to kinds, then relational rules 3. **Debug with AST inspection** - Use `--debug-query=ast` to verify structure matching 4. **Escape in inline rules** - Use `\$VAR` or single quotes for shell commands 5. **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`](references/pattern-valid-syntax.md) - Use valid parseable code as patterns - [`pattern-language-aware`](references/pattern-language-aware.md) - Account for language-specific syntax differences - [`pattern-context-selector`](references/pattern-context-selector.md) - Use context and selector for code fragments - [`pattern-avoid-comments-strings`](references/pattern-avoid-comments-strings.md) - Avoid matching inside comments and strings - [`pattern-strictness-levels`](references/pattern-strictness-levels.md) - Configure pattern strictness appropriately - [`pattern-kind-vs-pattern`](references/pattern-kind-vs-pattern.md) - Choose kind or pattern based on specificity needs - [`pattern-debug-ast`](references/pattern-debug-ast.md) - Use debug query to inspect AST structure - [`pattern-nthchild-matching`](references/pattern-nthchild-matching.md) - Use nthChild for index-based positional matching - [`pattern-range-matching`](references/pattern-range-matching.md) - Use range for character position matching ### 2. Meta Variable Usage (CRITICAL) - [`meta-naming-convention`](references/meta-naming-convention.md) - Follow meta variable naming conventions - [`meta-single-node`](references/meta-single-node.md) - Match single AST nodes with meta variables - [`meta-reuse-binding`](references/meta-reuse-binding.md) - Reuse meta variables to enforce equality - [`meta-underscore-noncapture`](references/meta-underscore-noncapture.md) - Use underscore prefix for non-capturing matches - [`meta-named-vs-unnamed`](references/meta-named-vs-unnamed.md) - Use double dollar for unnamed node matching - [`meta-multi-match-lazy`](references/meta-multi-match-lazy.md) - Understand multi-match variables are lazy ### 3. Rule Composition (HIGH) - [`compose-all-for-and-logic`](references/compose-all-for-and-logic.md) - Use all for AND logic between rules - [`compose-any-for-or-logic`](references/compose-any-for-or-logic.md) - Use any for OR logic between rules - [`compose-not-for-exclusion`](references/compose-not-for-exclusion.md) - Use not for exclusion patterns - [`compose-inside-for-context`](references/compose-inside-for-context.md) - Use inside for contextual matching - [`compose-has-for-children`](references/compose-has-for-children.md) - Use has for child node requirements - [`compose-matches-for-reuse`](references/compose-matches-for-reuse.md) - Use matches for rule reusability - [`compose-precedes-follows`](references/compose-precedes-follows.md) - Use precedes and follows for sequential positioning - [`compose-field-targeting`](references/compose-field-targeting.md) - Use field to target specific sub-nodes ### 4. Constraint Design (HIGH) - [`const-kind-filter`](references/const-kind-filter.md) - Use kind constraints to filter meta variables - [`const-regex-filter`](references/const-regex-filter.md) - Use regex constraints for text patterns - [`const-not-inside-not`](references/const-not-inside-not.md) - Avoid constraints inside not rules - [`const-pattern-constraint`](references/const-pattern-constraint.md) - Use pattern constraints for structural filtering - [`const-post-match-timing`](references/const-post-match-timing.md) - Understand constraints apply after matching ### 5. Rewrite Correctness (MEDIUM-HIGH) - [`rewrite-preserve-semantics`](references/rewrite-preserve-semantics.md) - Preserve program semantics in rewrites - [`rewrite-meta-variable-reference`](references/rewrite-meta-variable-reference.md) - Reference all necessary meta variables in fix - [`rewrite-transform-operations`](references/rewrite-transform-operations.md) - Use transform for complex rewrites - [`rewrite-test-before-deploy`](references/rewrite-test-before-deploy.md) - Test rewrites on representative code - [`rewrite-syntax-validity`](references/rewrite-syntax-validity.md) - Ensure fix templates produce valid syntax ### 6. Project Organization (MEDIUM) - [`org-project-structure`](references/org-project-structure.md) - Use standard project directory structure - [`org-unique-rule-ids`](references/org-unique-rule-ids.md) - Use unique descriptive rule IDs - [`org-severity-levels`](references/org-severity-levels.md) - Assign appropriate severity levels - [`org-file-filtering`](references/org-file-filtering.md) - Use file filtering for targeted rules - [`org-message-clarity`](references/org-message-clarity.md) - Write clear actionable messages ### 7. Performance Optimization (MEDIUM) - [`perf-specific-patterns`](references/perf-specific-patterns.md) - Use specific patterns over generic ones - [`perf-stopby-boundaries`](references/perf-stopby-boundaries.md) - Use stopBy to limit search depth - [`perf-thread-parallelism`](references/perf-thread-parallelism.md) - Leverage parallel scanning with threads - [`perf-avoid-regex-heavy`](references/perf-avoid-regex-heavy.md) - Avoid heavy regex in hot paths ### 8. Testing & Debugging (LOW-MEDIUM) - [`test-valid-invalid-cases`](references/test-valid-invalid-cases.md) - Write both valid and invalid test cases - [`test-snapshot-updates`](references/test-snapshot-updates.md) - Use snapshot testing for fix verification - [`test-playground-first`](references/test-playground-first.md) - Test patterns in playground first - [`test-edge-cases`](references/test-edge-cases.md) - Test edge cases and boundary conditions ## How to Use Read individual reference files for detailed explanations and code examples: - [Section definitions](references/_sections.md) - Category structure and impact levels - [Rule template](assets/templates/_template.md) - Template for adding new rules ## Full Compiled Document - [AGENTS.md](AGENTS.md) - Complete compiled guide with all rules ## Reference Files | File | Description | |------|-------------| | [AGENTS.md](AGENTS.md) | Complete compiled guide with all rules | | [references/_sections.md](references/_sections.md) | Category definitions and ordering | | [assets/templates/_template.md](assets/templates/_template.md) | Template for new rules | | [metadata.json](metadata.json) | Version and reference information |
© pproenca · MIT · pproenca/dot-skills
Похожие скиллы
Все скиллы по теме «Настройка агента: память, контекст, субагенты» →Сначала — ast-grep от других авторов.