Use graceful degradation for code search and structural analysis.
- ast-grep (
sg) - Structural pattern-based search - ripgrep (
rg) - Fast context-aware text search - grep - Standard text search (always available)
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
fiFinding 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]"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
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
- 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