| name | description |
|---|---|
ast-grep-patterns |
Skill teaching Claude when and how to use ast-grep for structural code searches |
Skill: Teach Claude when and how to use ast-grep for structural code searches
# Install ast-grep skill
npx skills add ast-grep/agent-skill
# Or via plugin marketplace
/plugin marketplace addSuggest ast-grep to the user when:
| Scenario | Signal Words | Why ast-grep? |
|---|---|---|
| Large refactoring | "rename all calls to...", "change API signature", ">50k lines" | Structural precision needed |
| Framework migration | "React 17→18", "Vue 2→3", "upgrade dependencies" | AST-aware transformations |
| Pattern detection | "find functions without...", "locate unused...", "identify anti-patterns" | Structural rules |
| Codebase analysis | "which components depend on...", "find tightly coupled..." | Dependency graphs |
Don't suggest for:
- Simple string searches (function names, imports) → Use Grep
- Small projects (<10k lines) → Grep is sufficient
- One-off searches → Grep is faster
- Semantic searches → Use Serena MCP or grepai
User request analysis:
├─ "Find string/text" → Grep (native)
├─ "Find by meaning" → Serena MCP or grepai
├─ "Find by structure" → ast-grep (plugin)
└─ Mixed requirements → Start with Grep, escalate if needed
Use case: Find async functions missing try/catch blocks
# ast-grep rule
rule:
pattern: |
async function $FUNC($$$PARAMS) {
$$$BODY
}
not:
has:
pattern: try { $$$TRY } catchWhen to use: Security audits, production readiness checks
Use case: Find all components using useEffect without cleanup
rule:
pattern: |
useEffect(() => {
$$$BODY
})
not:
has:
pattern: return () => { $$$CLEANUP }When to use: Memory leak detection, React best practices audit
Use case: Find functions with >5 parameters (complexity smell)
rule:
pattern: function $NAME($P1, $P2, $P3, $P4, $P5, $P6, $$$REST) { $$$BODY }When to use: Code quality improvement, refactoring candidates
Use case: Remove debug logging from production files
rule:
pattern: console.log($$$ARGS)
inside:
pattern: |
class $CLASS {
$$$METHODS
}When to use: Production cleanup, pre-release audits
Use case: Detect props passed but never used
rule:
pattern: |
function $COMP({ $PROP, $$$OTHER }) {
$$$BODY
}
not:
has:
pattern: $PROP
inside: $$$BODYWhen to use: Dead code elimination, performance optimization
Use case: Find usage of old API methods
rule:
any:
- pattern: React.Component
- pattern: componentWillMount
- pattern: componentWillReceivePropsWhen to use: Framework migrations, deprecation cleanup
Use case: Find potential SQL injection vulnerabilities
rule:
pattern: |
db.query($TEMPLATE_LITERAL)
where:
$TEMPLATE_LITERAL:
kind: template_stringWhen to use: Security audits, vulnerability scanning
Use case: Enforce explicit return types
rule:
pattern: |
function $NAME($$$PARAMS) {
$$$BODY
}
not:
has:
pattern: ': $TYPE'When to use: TypeScript best practices, type safety improvements
Use case: Find switch statements with >10 cases
rule:
pattern: |
switch ($EXPR) {
$C1: $$$B1
$C2: $$$B2
$C3: $$$B3
$C4: $$$B4
$C5: $$$B5
$C6: $$$B6
$C7: $$$B7
$C8: $$$B8
$C9: $$$B9
$C10: $$$B10
$C11: $$$B11
}When to use: Complexity reduction, polymorphism refactoring
Use case: Find error handling that silently fails
rule:
pattern: |
try {
$$$TRY
} catch ($ERR) {
// empty or only comment
}When to use: Debugging mysterious failures, error handling audit
| Codebase Size | Setup Worth It? | Alternative |
|---|---|---|
| <10k lines | ❌ No | Use Grep |
| 10k-50k lines | Start with Grep, escalate if needed | |
| 50k-200k lines | ✅ Yes | ast-grep for structural, Grep for text |
| >200k lines | ✅ Definitely | ast-grep + Serena MCP combo |
# Verify installation
npx ast-grep --version
# Reinstall skill
npx skills add ast-grep/agent-skill --forceProblem: Claude uses Grep instead of ast-grep
Solution: Be explicit in your request
- ❌ "Find async functions"
- ✅ "Use ast-grep to find async functions"
Problem: ast-grep slow on large codebase
Solutions:
- Narrow search scope:
ast-grep --path src/components/ - Use file filters:
ast-grep --lang tsx - Cache results for iterative refinement
Problem: ast-grep pattern doesn't match expected code
Debug steps:
- Test pattern in isolation:
ast-grep -p 'your-pattern' file.js - Check AST structure:
ast-grep --debug-query - Simplify pattern incrementally
- Verify language syntax (JS vs TS vs JSX)
#!/bin/bash
# .git/hooks/pre-commit
# Check for console.log in staged files
if ast-grep -p 'console.log($$$)' $(git diff --cached --name-only); then
echo "❌ Found console.log statements"
exit 1
fi#!/bin/bash
# Migrate React class components to hooks
# Find all class components
ast-grep -p 'class $C extends React.Component' --json > components.json
# Process each component
jq -r '.[] | .file' components.json | while read file; do
echo "Migrating: $file"
# ... transformation logic
done#!/bin/bash
# security-audit.sh
echo "=== Security Audit ==="
# SQL injection risks
ast-grep -p 'db.query(`${$VAR}`)' --lang ts
# XSS risks
ast-grep -p 'innerHTML = $VAR' --lang js
# Hardcoded secrets
ast-grep -p 'password: "$PASSWORD"' --lang tsI need to refactor [FEATURE] across our codebase (~[SIZE] lines).
Use ast-grep to:
1. Find all instances of [OLD_PATTERN]
2. Identify which files will be affected
3. Suggest transformation strategy
4. Create a phased migration plan
Start with analysis only, wait for my approval before making changes.
We're migrating from [OLD_FRAMEWORK v1] to [NEW_FRAMEWORK v2].
Use ast-grep to:
1. Find all deprecated API usage
2. Map to new API equivalents
3. Estimate migration effort (files affected)
4. Identify high-risk changes
Provide a dependency graph showing migration order.
Run a code quality audit on [DIRECTORY] using ast-grep.
Focus on:
- Functions with >5 parameters
- Async functions without error handling
- Empty catch blocks
- Unused function parameters
Rank issues by severity and provide refactoring suggestions.
# 1. ast-grep finds structural patterns
ast-grep -p 'async function $F' --json > async-funcs.json
# 2. Serena finds symbols and dependencies
claude mcp call serena find_symbol --name "authenticate"
# 3. Combine insights for full context
# ast-grep: "This is an async function"
# Serena: "Called by 12 other functions"# 1. grepai for semantic search
# "Find authentication-related code"
# 2. ast-grep for structural refinement
# "Among those results, which are async without error handling?"- Start Simple: Begin with Grep, escalate to ast-grep when needed
- Test Patterns: Verify on small files before running on entire codebase
- Document Patterns: Save successful patterns as reusable rules
- Explicit Requests: Always tell Claude explicitly when to use ast-grep
- Combine Tools: Use ast-grep for structure, Grep for text, Serena for symbols
Last updated: January 2026 Compatible with: Claude Code 2.1.7+