structural-search
Original:🇺🇸 English
Translated
Search code by AST structure using ast-grep. Find semantic patterns like function calls, imports, class definitions instead of text patterns. Triggers on: find all calls to X, search for pattern, refactor usages, find where function is used, structural search, ast-grep, sg.
1installs
Source0xdarkmatter/claude-mods
Added on
NPX Install
npx skill4agent add 0xdarkmatter/claude-mods structural-searchTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Structural Search
Search code by its abstract syntax tree (AST) structure. Finds semantic patterns that regex cannot match reliably.
Tools
| Tool | Command | Use For |
|---|---|---|
| ast-grep | | AST-aware code search |
Pattern Syntax
| Pattern | Matches | Example |
|---|---|---|
| Named identifier | |
| Any single node | |
| Zero or more nodes | |
Top 10 Essential Patterns
bash
# 1. Find console.log calls
sg -p 'console.log($_)'
# 2. Find React hooks
sg -p 'const [$_, $_] = useState($_)'
sg -p 'useEffect($_, [$$$])'
# 3. Find function definitions
sg -p 'function $NAME($$$) { $$$ }'
sg -p 'def $NAME($$$): $$$' --lang python
# 4. Find imports
sg -p 'import $_ from "$_"'
sg -p 'from $_ import $_' --lang python
# 5. Find async patterns
sg -p 'await $_'
sg -p 'async function $NAME($$$) { $$$ }'
# 6. Find error handling
sg -p 'try { $$$ } catch ($_) { $$$ }'
sg -p 'if err != nil { $$$ }' --lang go
# 7. Find potential issues
sg -p '$_ == $_' # == instead of ===
sg -p 'eval($_)' # Security risk
sg -p '$_.innerHTML = $_' # XSS vector
# 8. Preview refactoring
sg -p 'console.log($_)' -r 'logger.info($_)'
# 9. Apply refactoring
sg -p 'var $NAME = $_' -r 'const $NAME = $_' --rewrite
# 10. Search specific language
sg -p 'pattern' --lang typescriptQuick Reference
| Task | Command |
|---|---|
| Find pattern | |
| Specific language | |
| Replace (preview) | |
| Replace (apply) | |
| Show context | |
| JSON output | |
| File list only | |
| Count matches | |
| Run YAML rules | |
When to Use
- Finding all usages of a function/method
- Locating specific code patterns (hooks, API calls)
- Preparing for large-scale refactoring
- When regex would match false positives
- Detecting anti-patterns and security issues
- Creating custom linting rules
Additional Resources
For complete patterns, load:
- - JavaScript/TypeScript patterns
./references/js-ts-patterns.md - - Python patterns
./references/python-patterns.md - - Go and Rust patterns
./references/go-rust-patterns.md - - Security vulnerability detection
./references/security-patterns.md - - YAML rules and tool integration
./references/advanced-usage.md - - Starter template for custom rules
./assets/rule-template.yaml