install
source · Clone the upstream repo
git clone https://github.com/ComeOnOliver/skillshub
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ComeOnOliver/skillshub "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/pproenca/dot-skills/ast-grep" ~/.claude/skills/comeonoliver-skillshub-ast-grep-45d55e && rm -rf "$T"
manifest:
skills/pproenca/dot-skills/ast-grep/SKILL.mdsource content
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
for simple structurespattern - Use
withkind
/has
for complex structuresinside - Combine with
,all
, orany
for compound queriesnot - Always use
for relational rules (stopBy: end
,inside
) to ensure complete searchhas
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
- Ensures complete subtree traversal for relational rulesstopBy: end - Start simple, add complexity - Begin with patterns, progress to kinds, then relational rules
- Debug with AST inspection - Use
to verify structure matching--debug-query=ast - Escape in inline rules - Use
or single quotes for shell commands\$VAR - 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 | |
| 2 | Meta Variable Usage | CRITICAL | |
| 3 | Rule Composition | HIGH | |
| 4 | Constraint Design | HIGH | |
| 5 | Rewrite Correctness | MEDIUM-HIGH | |
| 6 | Project Organization | MEDIUM | |
| 7 | Performance Optimization | MEDIUM | |
| 8 | Testing & Debugging | LOW-MEDIUM | |
Quick Reference
1. Pattern Correctness (CRITICAL)
- Use valid parseable code as patternspattern-valid-syntax
- Account for language-specific syntax differencespattern-language-aware
- Use context and selector for code fragmentspattern-context-selector
- Avoid matching inside comments and stringspattern-avoid-comments-strings
- Configure pattern strictness appropriatelypattern-strictness-levels
- Choose kind or pattern based on specificity needspattern-kind-vs-pattern
- Use debug query to inspect AST structurepattern-debug-ast
- Use nthChild for index-based positional matchingpattern-nthchild-matching
- Use range for character position matchingpattern-range-matching
2. Meta Variable Usage (CRITICAL)
- Follow meta variable naming conventionsmeta-naming-convention
- Match single AST nodes with meta variablesmeta-single-node
- Reuse meta variables to enforce equalitymeta-reuse-binding
- Use underscore prefix for non-capturing matchesmeta-underscore-noncapture
- Use double dollar for unnamed node matchingmeta-named-vs-unnamed
- Understand multi-match variables are lazymeta-multi-match-lazy
3. Rule Composition (HIGH)
- Use all for AND logic between rulescompose-all-for-and-logic
- Use any for OR logic between rulescompose-any-for-or-logic
- Use not for exclusion patternscompose-not-for-exclusion
- Use inside for contextual matchingcompose-inside-for-context
- Use has for child node requirementscompose-has-for-children
- Use matches for rule reusabilitycompose-matches-for-reuse
- Use precedes and follows for sequential positioningcompose-precedes-follows
- Use field to target specific sub-nodescompose-field-targeting
4. Constraint Design (HIGH)
- Use kind constraints to filter meta variablesconst-kind-filter
- Use regex constraints for text patternsconst-regex-filter
- Avoid constraints inside not rulesconst-not-inside-not
- Use pattern constraints for structural filteringconst-pattern-constraint
- Understand constraints apply after matchingconst-post-match-timing
5. Rewrite Correctness (MEDIUM-HIGH)
- Preserve program semantics in rewritesrewrite-preserve-semantics
- Reference all necessary meta variables in fixrewrite-meta-variable-reference
- Use transform for complex rewritesrewrite-transform-operations
- Test rewrites on representative coderewrite-test-before-deploy
- Ensure fix templates produce valid syntaxrewrite-syntax-validity
6. Project Organization (MEDIUM)
- Use standard project directory structureorg-project-structure
- Use unique descriptive rule IDsorg-unique-rule-ids
- Assign appropriate severity levelsorg-severity-levels
- Use file filtering for targeted rulesorg-file-filtering
- Write clear actionable messagesorg-message-clarity
7. Performance Optimization (MEDIUM)
- Use specific patterns over generic onesperf-specific-patterns
- Use stopBy to limit search depthperf-stopby-boundaries
- Leverage parallel scanning with threadsperf-thread-parallelism
- Avoid heavy regex in hot pathsperf-avoid-regex-heavy
8. Testing & Debugging (LOW-MEDIUM)
- Write both valid and invalid test casestest-valid-invalid-cases
- Use snapshot testing for fix verificationtest-snapshot-updates
- Test patterns in playground firsttest-playground-first
- Test edge cases and boundary conditionstest-edge-cases
How to Use
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
Full Compiled Document
- AGENTS.md - Complete compiled guide with all rules
Reference Files
| File | Description |
|---|---|
| AGENTS.md | Complete compiled guide with all rules |
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |