This skill should be used automatically when searching for code patterns, function usages, class definitions, or syntax structures. Prefer ast-grep over grep/find/ripgrep for code-aware searching that understands language syntax. Use for finding function calls, understanding API usage, locating pattern implementations, and code exploration.
/plugin marketplace add bcowdery/claude-skills/plugin install patsy@bcowdery-claude-skillsThis skill is limited to using the following tools:
references/advanced-patterns.mdUse ast-grep for syntax-aware code searching that understands language structure. Unlike text-based tools (grep, ripgrep, find), ast-grep matches actual code patterns by parsing the abstract syntax tree.
Prefer ast-grep over grep/find when:
ast-grep -p 'fetchData($$$)'Use grep/ripgrep instead when:
# Check installation
which ast-grep || echo "Install with: brew install ast-grep"
# Basic pattern search
ast-grep --pattern 'PATTERN' --lang LANGUAGE [PATH]
# Search with context
ast-grep -p 'PATTERN' -l LANG -A 3 -B 2
# JSON output for parsing
ast-grep -p 'PATTERN' -l LANG --json
| Pattern | Matches | Example |
|---|---|---|
$VAR | Single AST node | console.log($MSG) matches console.log("hello") |
$$$ARGS | Multiple nodes (named) | function($$$PARAMS) matches any function params |
$$$ | Any sequence of nodes | { $$$ } matches any block contents |
javascript, typescript, tsx, python, go, rust, java, c, cpp, csharp, kotlin, swift, ruby, php, lua, html, css
# Find all useState hooks
ast-grep -p 'useState($$$)' -l typescript
# Find specific function calls
ast-grep -p 'fetchUser($$$)' -l typescript
# Find async functions
ast-grep -p 'async function $NAME($$$) { $$$ }' -l typescript
# Find arrow function assignments
ast-grep -p 'const $NAME = ($$$) => $$$' -l typescript
# Find React component definitions
ast-grep -p 'function $COMPONENT($$$): JSX.Element { $$$ }' -l tsx
# Find useEffect with dependencies
ast-grep -p 'useEffect($$$)' -l typescript
# Find imports from a module
ast-grep -p "import { $$$ } from '$MODULE'" -l typescript
# Find class methods
ast-grep -p 'class $CLASS { $$$ $METHOD($$$) { $$$ } $$$ }' -l typescript
# Find function definitions
ast-grep -p 'def $FUNC($$$): $$$' -l python
# Find class definitions
ast-grep -p 'class $CLASS: $$$' -l python
# Find decorator usage
ast-grep -p '@$DECORATOR
def $FUNC($$$): $$$' -l python
# Find specific method calls
ast-grep -p '$OBJ.$METHOD($$$)' -l python
# Find async function definitions
ast-grep -p 'async def $FUNC($$$): $$$' -l python
# Find list comprehensions
ast-grep -p '[$EXPR for $VAR in $ITER]' -l python
# Find function definitions
ast-grep -p 'func $NAME($$$) $$$ { $$$ }' -l go
# Find method definitions
ast-grep -p 'func ($RECV $TYPE) $NAME($$$) $$$ { $$$ }' -l go
# Find interface definitions
ast-grep -p 'type $NAME interface { $$$ }' -l go
# Find struct definitions
ast-grep -p 'type $NAME struct { $$$ }' -l go
# Find error handling patterns
ast-grep -p 'if err != nil { $$$ }' -l go
# Find goroutine spawns
ast-grep -p 'go $FUNC($$$)' -l go
# Find class definitions
ast-grep -p 'class $NAME { $$$ }' -l java
# Find method definitions
ast-grep -p '$RET $METHOD($$$) { $$$ }' -l java
# Find annotations
ast-grep -p '@$ANNOTATION
$$$' -l java
# Find try-catch blocks
ast-grep -p 'try { $$$ } catch ($E) { $$$ }' -l java
which ast-grep || brew install ast-grep
Determine the language flag needed:
.ts, .tsx → -l typescript or -l tsx.js, .jsx → -l javascript.py → -l python.go → -l go.rs → -l rust.java → -l javaStart simple and refine:
# Start broad
ast-grep -p 'fetchData($$$)' -l typescript
# Add specificity if too many results
ast-grep -p 'await fetchData($$$)' -l typescript
# Narrow to specific directory
ast-grep -p 'fetchData($$$)' -l typescript src/api/
# Count matches
ast-grep -p 'PATTERN' -l LANG --json | jq length
# Get file list only
ast-grep -p 'PATTERN' -l LANG --json | jq -r '.[].file' | sort -u
# Get matches with context
ast-grep -p 'PATTERN' -l LANG -A 5 -B 2
| Task | ast-grep | grep/ripgrep |
|---|---|---|
Find log("...") calls | ast-grep -p 'log($$$)' - matches only function calls | rg 'log\(' - matches comments, strings too |
| Find variable assignments | ast-grep -p 'const $X = $Y' - matches declarations | rg 'const' - matches any occurrence |
| Find function definitions | ast-grep -p 'function $F($$$)' - accurate | rg 'function' - includes comments, strings |
| Find class methods | Pattern matches syntax tree | Regex struggles with nesting |
| Search comments | Not designed for this | Better choice |
| Search config files | Won't parse non-code | Better choice |
Pattern doesn't match expected code:
--json output to debug what's matchingToo many results:
$NAME instead of $$$ to match single nodesNo results when expecting matches:
Performance on large codebases:
.astgrepignore file to exclude directories (like node_modules)# macOS
brew install ast-grep
# Cargo (Rust)
cargo install ast-grep --locked
# npm (less performant)
npm install -g @ast-grep/cli
This skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
This skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or agent development best practices for Claude Code plugins.
This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.