mcpField
A
ast-grep

ast-grep

github.com/pproencaDevelopment220335 installsUpdated 11w ago
Repository

About ast-grep

ast-grep is a community-maintained best-practices skill for writing and using ast-grep rules. It guides developers through pattern design, composition, rewrites, performance, and validation of rules.

Key features

  1. 0146 rules across 8 categories
  2. 02Pattern and meta-variable guidance
  3. 03Rule composition and constraints
  4. 04Rewrite correctness practices
  5. 05Performance and debugging advice

Use cases

  1. 01Writing rules for code search or linting
  2. 02Debugging patterns that fail to match
  3. 03Optimizing scans of large codebases
  4. 04Organizing and testing rule projects

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/pproenca/dot-skills --skill ast-grep

After installing, type/ast-grepin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

AGENTS.md
metadata.json
README.md
assets/templates/
_template.md
references/
_sections.md
compose-all-for-and-logic.md
compose-any-for-or-logic.md
compose-field-targeting.md
compose-has-for-children.md
compose-inside-for-context.md
compose-matches-for-reuse.md
compose-not-for-exclusion.md
compose-precedes-follows.md
const-kind-filter.md
const-not-inside-not.md
const-pattern-constraint.md
const-post-match-timing.md
const-regex-filter.md
meta-multi-match-lazy.md
meta-named-vs-unnamed.md
meta-naming-convention.md
meta-reuse-binding.md
meta-single-node.md
meta-underscore-noncapture.md
org-file-filtering.md
org-message-clarity.md
org-project-structure.md
org-severity-levels.md
org-unique-rule-ids.md
pattern-avoid-comments-strings.md

Details

From the project README

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
# 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

PriorityCategoryImpactPrefix
1Pattern CorrectnessCRITICALpattern-
2Meta Variable UsageCRITICALmeta-
3Rule CompositionHIGHcompose-
4Constraint DesignHIGHconst-
5Rewrite CorrectnessMEDIUM-HIGHrewrite-
6Project OrganizationMEDIUMorg-
7Performance OptimizationMEDIUMperf-
8Testing & DebuggingLOW-MEDIUMtest-

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

  • AGENTS.md - Complete compiled guide with all rules

Reference Files

FileDescription
AGENTS.mdComplete compiled guide with all rules
references/_sections.mdCategory definitions and ordering
assets/templates/_template.mdTemplate for new rules
metadata.jsonVersion and reference information

Comments

Markdown supported