Get the FREE Ultimate OpenClaw Setup Guide →

structural-search

Scanned
npx machina-cli add skill aiskillstore/marketplace/structural-search --openclaw
Files (1)
SKILL.md
3.0 KB

Structural Search

Search code by its abstract syntax tree (AST) structure. Finds semantic patterns that regex cannot match reliably.

Tools

ToolCommandUse For
ast-grepsg -p 'pattern'AST-aware code search

Pattern Syntax

PatternMatchesExample
$NAMENamed identifierfunction $NAME() {}
$_Any single nodeconsole.log($_)
$$$Zero or more nodesfunction $_($$$) {}

Top 10 Essential Patterns

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

Quick Reference

TaskCommand
Find patternsg -p 'pattern'
Specific languagesg -p 'pattern' --lang python
Replace (preview)sg -p 'old' -r 'new'
Replace (apply)sg -p 'old' -r 'new' --rewrite
Show contextsg -p 'pattern' -A 3
JSON outputsg -p 'pattern' --json
File list onlysg -p 'pattern' -l
Count matchessg -p 'pattern' --count
Run YAML rulessg scan

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:

  • ./references/js-ts-patterns.md - JavaScript/TypeScript patterns
  • ./references/python-patterns.md - Python patterns
  • ./references/go-rust-patterns.md - Go and Rust patterns
  • ./references/security-patterns.md - Security vulnerability detection
  • ./references/advanced-usage.md - YAML rules and tool integration
  • ./assets/rule-template.yaml - Starter template for custom rules

Source

git clone https://github.com/aiskillstore/marketplace/blob/main/skills/0xdarkmatter/structural-search/SKILL.mdView on GitHub

Overview

Structural Search lets you query code by its AST structure using ast-grep. It finds semantic patterns like function calls, imports, and class definitions instead of brittle text patterns, enabling safer refactors and security checks.

How This Skill Works

You write AST-patterns using the sg syntax (e.g., $NAME, $_, $$$) and run sg -p 'pattern' to locate matches. Language-specific patterns can be used with --lang; you can preview changes with -r or apply them with --rewrite, and export JSON results for automation.

When to Use It

  • Finding all usages of a function or method
  • Locating specific code patterns like hooks or API calls
  • Preparing for large-scale refactoring
  • When regex would match false positives
  • Detecting anti-patterns and security issues

Quick Start

  1. Step 1: Write or choose an AST pattern to search (e.g., 'console.log($_)')
  2. Step 2: Run sg -p '<pattern>' to find matches and review results
  3. Step 3: Preview or apply changes with -r or --rewrite (sg -p '<old>' -r '<new>' --rewrite)

Best Practices

  • Start with narrow, well-scoped patterns and progressively generalize
  • Leverage language-specific patterns with --lang to reduce noise
  • Preview changes (-r) before applying (--rewrite) to avoid regressions
  • Use context (-A) or JSON output (--json) to understand matches
  • Integrate with linting/CI by exporting JSON results

Example Use Cases

  • Find console.log calls: sg -p 'console.log($_)'
  • Find React hooks: sg -p 'const [$_, $_] = useState($_)'; sg -p 'useEffect($_, [$$$])'
  • Find function definitions: sg -p 'function $NAME($$$) { $$$ }'
  • Find imports: sg -p 'import $_ from "$_"' --lang javascript
  • Preview/refactor: sg -p 'console.log($_)' -r 'logger.info($_)'

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers