Ast grep

From pproenca/dot-skills · View interactive version

Ast grep is an AI agent skill from the GitHub repository pproenca/dot-skills. 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.

How to use this skill

Skill file

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:

General Workflow

Follow this workflow when creating ast-grep rules for code search:

Step 1: Understand the Query

Clarify what you want to find:

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:

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

  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)

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)

How to Use

Read individual reference files for detailed explanations and code examples:

Full Compiled Document

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 |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.