sputnikus/opencodedots/ast-grep
- community
Checking that the SKILL.md file is still in the repository.
Guide for writing ast-grep rules to perform structural code search and analysis. Use when users need to search codebases using Abstract Syntax Tree (AST) patterns, find specific code structures, or perform complex code queries that go beyond simple text search. This skill should be used when users ask to search for code patterns, find specific language constructs, or locate code with particular structural characteristics.
Install
npx vskill@latest install sputnikus/opencodedots/ast-grepSKILL.md
ast-grep Code Search
Overview
This skill helps translate natural language queries into ast-grep rules for structural code search. ast-grep uses Abstract Syntax Tree (AST) patterns to match code based on its structure rather than just text, enabling powerful and precise code search across large codebases.
When to Use This Skill
Use this skill when users:
- Need to search for code patterns using structural matching (e.g., "find all async functions that don't have error handling")
- Want to locate specific language constructs (e.g., "find all function calls with specific parameters")
- Request searches that require understanding code structure rather than just text
- Ask to search for code with particular AST characteristics
- Need to perform complex code queries that traditional text search cannot handle
General Workflow
Follow this process to help users write effective ast-grep rules:
Step 1: Understand the Query
Clearly understand what the user wants to find. Ask clarifying questions if needed:
- What specific code pattern or structure are they looking for?
- Which programming language?
- Are there specific edge cases or variations to consider?
- What should be included or excluded from matches?
Step 2: Create Example Code
Write a simple code snippet that represents what the user wants to match. Save this to a temporary file for testing.
Example: If searching for "async functions that use await", create a test file:
Step 3: Write the ast-grep Rule
Translate the pattern into an ast-grep rule. Start simple and add complexity as needed.
Key principles:
- Always use
stopBy: endfor relational rules (inside,has) to ensure search goes to the end of the direction - Use
patternfor simple structures - Use
kindwithhas/insidefor complex structures - Break complex queries into smaller sub-rules using
all,any, ornot
Example rule file (test_rule.yml):
See references/rule_reference.md for comprehensive rule documentation.
Step 4: Test the Rule
Use ast-grep CLI to verify the rule matches the example code. There are two main approaches:
Option A: Test with inline rules (for quick iterations)
Option B: Test with rule files (recommended for complex rules)
Debugging if no matches:
- Simplify the rule (remove sub-rules)
- Add
stopBy: endto relational rules if not present - Use
--debug-queryto understand the AST structure (see below) - Check if
kindvalues are correct for the language
Step 5: Search the Codebase
Once the rule matches the example code correctly, search the actual codebase:
For simple pattern searches:
For complex rule-based searches:
For inline rules (without creating files):
ast-grep CLI Commands
Inspect Code Structure (--debug-query)
Dump the AST structure to understand how code is parsed:
Available formats:
cst: Concrete Syntax Tree (shows all nodes including punctuation)ast: Abstract Syntax Tree (shows only named nodes)pattern: Shows how ast-grep interprets your pattern
Use this to:
- Find the correct
kindvalues for nodes - Understand the structure of code you want to match
- Debug why patterns aren't matching
Example:
Test Rules (scan with --stdin)
Test a rule against code snippet without creating files:
Add --json for structured output:
Search with Patterns (run)
Simple pattern-based search for single AST node matches:
When to use:
- Simple, single-node matches
- Quick searches without complex logic
- When you don't need relational rules (inside/has)
Search with Rules (scan)
YAML rule-based search for complex structural queries:
When to use:
- Complex structural searches
- Relational rules (inside, has, precedes, follows)
- Composite logic (all, any, not)
- When you need the power of full YAML rules
Tip: For relational rules (inside/has), always add stopBy: end to ensure complete traversal.
Tips for Writing Effective Rules
Always Use stopBy: end
For relational rules, always use stopBy: end unless there's a specific reason not to:
This ensures the search traverses the entire subtree rather than stopping at the first non-matching node.
Start Simple, Then Add Complexity
Begin with the simplest rule that could work:
- Try a
patternfirst - If that doesn't work, try
kindto match the node type - Add relational rules (
has,inside) as needed - Combine with composite rules (
all,any,not) for complex logic
Use the Right Rule Type
- Pattern: For simple, direct code matching (e.g.,
console.log($ARG)) - Kind + Relational: For complex structures (e.g., "function containing await")
- Composite: For logical combinations (e.g., "function with await but not in try-catch")
Debug with AST Inspection
When rules don't match:
- Use
--debug-query=cstto see the actual AST structure - Check if metavariables are being detected correctly
- Verify the node
kindmatches what you expect - Ensure relational rules are searching in the right direction
Escaping in Inline Rules
When using --inline-rules, escape metavariables in shell commands:
- Use
\$VARinstead of$VAR(shell interprets$as variable) - Or use single quotes:
'$VAR'works in most shells
Example:
Common Use Cases
Find Functions with Specific Content
Find async functions that use await:
Find Code Inside Specific Contexts
Find console.log inside class methods:
Find Code Missing Expected Patterns
Find async functions without try-catch:
Resources
references/
Contains detailed documentation for ast-grep rule syntax:
rule_reference.md: Comprehensive ast-grep rule documentation covering atomic rules, relational rules, composite rules, and metavariables
Load these references when detailed rule syntax information is needed.
Security
- Security scanPassed, 90 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/ast-grep/SKILL.md
Popularity
- 7-day trend score
- 5
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/sputnikus/opencodedots/ast-grep)