Skill站外收录 @skilllib/skill-2117939930272414773
understand-codebase
支持对代码库进行深度探索与结构分析,能够追踪函数调用链、定位符号定义与引用、解析类与模块依赖关系,适用于代码理解、重构评估及安全审计等场景。
效率工具 AI 技能
Skills.MD
# Understand Codebase
Use when navigating code, tracing how functions/classes connect, finding where symbols are defined or called, or answering "how does this work" questions about a codebase.
## Tool Selection
| Need | Tool |
| ------------------------------------------------------------ | --------------- |
| Structural patterns (functions, classes, syntax, call flows) | `sg` (ast-grep) |
| Text patterns (strings, comments, names) | `rg` (ripgrep) |
| File discovery by name/extension | `fd` |
### Choose ripgrep (`rg`) for:
- Text-based searches (strings, comments, variable names)
- Fast, simple pattern matching across many files
- When exact code structure doesn't matter
- Regex searches with context lines
- Searching binary files or non-code text
### Choose ast-grep (`sg`) for:
- Structural code searches (function signatures, class definitions)
- Syntax-aware matching that understands code semantics
- Finding patterns that span multiple lines naturally
- Refactoring analysis (matching specific AST node types)
- When you need to match code regardless of formatting/whitespace
### Choose fd for:
- Finding files by name, extension, or path pattern
- Filtering by modification time or file size
- Building file lists to pipe into `rg` or `sg`
- Batch operations on matched files
### Decision Flow
1. Need to find files first? → `fd`, then pipe to `rg` or `sg`
2. Need syntax-aware matching (functions, classes, imports)? → `sg`
3. Need fast text/regex search? → `rg`
4. Uncertain? Start with `rg` (faster), escalate to `sg` if structure matters
## Quick Start
### ast-grep (structural search)
```bash
# Find pattern with metavariables
sg -p 'console.log($MSG)' -l js
# Find function definitions
sg -p 'function $NAME($$$ARGS) { $$$ }' -l js
# Find async functions containing await
sg -p 'async function $NAME($$$) { $$$ }' --has 'await $EXPR' -l js
```
### ripgrep (text search)
```bash
# Basic search
rg 'TODO' --type js
# Case-insensitive with context
rg -i 'error' -C 2
# Fixed string (no regex)
rg -F 'user.email' src/
```
### fd (file finding)
```bash
# Find by extension
fd -e ts -e tsx src/
# Find files, then search content
fd -e py | xargs rg 'import numpy'
```
## Common Patterns
### Codebase Exploration
```bash
# Find entry points
sg -p 'export default $COMPONENT' -l tsx
rg 'if __name__.*main' --type py
# Find class definitions
sg -p 'class $NAME { $$$ }' -l ts
rg '^class \w+' --type py
# Find all imports of a module
sg -p 'import $$$IMPORTS from "react"' -l tsx
rg '^import.*from ["\x27]lodash' --type ts
```
### Pre-Refactoring Analysis
```bash
# Find all usages of a function
sg -p '$FUNC($$$)' -l js # where $FUNC matches your function name
rg 'myFunction\(' --type js
# Find method calls on objects
sg -p '$OBJ.methodName($$$)' -l js
# Find variable assignments
sg -p 'const $VAR = $VALUE' -l ts
```
### Security Audits
```bash
# Hardcoded secrets
rg '(password|secret|api_key)\s*[:=]\s*["\x27][^"\x27]+["\x27]' -i
# SQL injection risks
sg -p 'query($SQL)' -l js
rg 'execute\(.*\+.*\)' --type py
# Eval usage
sg -p 'eval($CODE)' -l js
rg '\beval\s*\(' --type py
# Console statements (for cleanup)
sg -p 'console.$METHOD($$$)' -l js
```
### Error Handling Analysis
```bash
# Find try-catch blocks
sg -p 'try { $$$ } catch ($E) { $$$ }' -l js
# Find empty catch blocks
sg -p 'try { $$$ } catch ($E) { }' -l js
# Find functions without error handling
sg -p 'async function $NAME($$$) { $$$ }' --not-has 'try' -l js
```
### Dependency Analysis
```bash
# Find all imports from a package
rg '^import.*from ["\x27]@company/' --type ts
# Find require statements
sg -p 'require($PATH)' -l js
# Find dynamic imports
sg -p 'import($PATH)' -l js
```
## Performance Tips
1. **Limit scope first**: Use `fd` to narrow files, then search content
```bash
fd -e py src/ | xargs rg 'class.*Test'
```
2. **Use file type filters**: Both `rg` and `sg` are faster with type hints
```bash
rg 'pattern' --type rust # vs searching all files
sg -p 'pattern' -l rs # language-specific parsing
```
3. **Exclude build artifacts**:
```bash
rg 'pattern' -g '!node_modules' -g '!dist' -g '!build'
fd -e js -E node_modules -E dist
```
## References
Read these for advanced usage:
- [references/ast-grep.md](references/ast-grep.md) - Metavariables, relational rules, composite patterns
- [references/ripgrep.md](references/ripgrep.md) - Regex syntax, filtering, replacements
- [references/fd.md](references/fd.md) - Type filters, execution, time-based search
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「understand-codebase」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次