Skip to content

Latest commit

 

History

History
98 lines (69 loc) · 2.16 KB

File metadata and controls

98 lines (69 loc) · 2.16 KB

Code Analysis Tools

Use graceful degradation for code search and structural analysis.

Tool Priority

  1. ast-grep (sg) - Structural pattern-based search
  2. ripgrep (rg) - Fast context-aware text search
  3. grep - Standard text search (always available)

Detection

Check tool availability before use:

# Check for ast-grep
if command -v sg >/dev/null 2>&1; then
  # Use ast-grep for structural search
elif command -v rg >/dev/null 2>&1; then
  # Fall back to ripgrep
else
  # Use standard grep as final fallback
fi

Usage Examples

Finding function definitions:

# ast-grep (best - structural)
sg -p 'function $NAME($$$) { $$$ }'

# ripgrep (fallback - fast text)
rg '^function\s+\w+\(' --type-add 'source:*.[extension]' -t source

# grep (last resort - basic)
grep -r '^function ' --include="*.[extension]"

Finding imports/requires:

# ast-grep
sg -p 'import { $$$ } from "$MODULE"'

# ripgrep
rg '^import .* from' --type-add 'source:*.[extension]' -t source

# grep
grep -r '^import ' --include="*.[extension]"

Finding class/component definitions:

# ast-grep
sg -p 'class $NAME { $$$ }'

# ripgrep
rg '^(class|export class)\s+\w+' --type-add 'source:*.[extension]' -t source

# grep
grep -r '^class ' --include="*.[extension]"

Search Scope

Best practices:

  • Limit to source file extensions relevant to project
  • Exclude directories: node_modules, vendor, dist, build, .git
  • Focus on source directories: src, lib, app
  • Use file type filters when available

Performance tips:

  • Use specific patterns over broad searches
  • Limit directory depth with --max-depth (ripgrep/grep)
  • Cache results for repeated queries

Fallback Notice

If ast-grep unavailable, display once per session:

⚠️ ast-grep not detected. Install for more precise structural code analysis.
   https://ast-grep.github.io/guide/quick-start.html

When to Use

  • Finding usage patterns across codebase
  • Identifying code structure and organization
  • Locating function/class/component definitions
  • Analyzing import/dependency patterns
  • Refactoring impact analysis
  • Code navigation in unfamiliar codebases