structural-search
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseStructural Search
结构化搜索
Search code by its abstract syntax tree (AST) structure. Finds semantic patterns that regex cannot match reliably.
通过抽象语法树(AST)结构搜索代码。查找正则表达式无法可靠匹配的语义模式。
Tools
工具
| Tool | Command | Use For |
|---|---|---|
| ast-grep | | AST-aware code search |
| 工具 | 命令 | 适用场景 |
|---|---|---|
| ast-grep | | 支持AST的代码搜索 |
Pattern Syntax
模式语法
| Pattern | Matches | Example |
|---|---|---|
| Named identifier | |
| Any single node | |
| Zero or more nodes | |
| 模式 | 匹配内容 | 示例 |
|---|---|---|
| 命名标识符 | |
| 任意单个节点 | |
| 零或多个节点 | |
Top 10 Essential Patterns
10个核心实用模式
bash
undefinedbash
undefined1. Find console.log calls
1. 查找console.log调用
sg -p 'console.log($_)'
sg -p 'console.log($_)'
2. Find React hooks
2. 查找React hooks
sg -p 'const [$, $] = useState($)'
sg -p 'useEffect($, [$$$])'
sg -p 'const [$, $] = useState($)'
sg -p 'useEffect($, [$$$])'
3. Find function definitions
3. 查找函数定义
sg -p 'function $NAME($$$) { $$$ }'
sg -p 'def $NAME($$$): $$$' --lang python
sg -p 'function $NAME($$$) { $$$ }'
sg -p 'def $NAME($$$): $$$' --lang python
4. Find imports
4. 查找导入语句
sg -p 'import $_ from "$"'
sg -p 'from $ import $_' --lang python
sg -p 'import $_ from "$"'
sg -p 'from $ import $_' --lang python
5. Find async patterns
5. 查找异步模式
sg -p 'await $_'
sg -p 'async function $NAME($$$) { $$$ }'
sg -p 'await $_'
sg -p 'async function $NAME($$$) { $$$ }'
6. Find error handling
6. 查找错误处理代码
sg -p 'try { $$$ } catch ($_) { $$$ }'
sg -p 'if err != nil { $$$ }' --lang go
sg -p 'try { $$$ } catch ($_) { $$$ }'
sg -p 'if err != nil { $$$ }' --lang go
7. Find potential issues
7. 查找潜在问题
sg -p '$_ == $' # == instead of ===
sg -p 'eval($)' # Security risk
sg -p '$.innerHTML = $' # XSS vector
sg -p '$_ == $' # 使用==而非===
sg -p 'eval($)' # 安全风险
sg -p '$.innerHTML = $' # XSS风险点
8. Preview refactoring
8. 预览重构效果
sg -p 'console.log($)' -r 'logger.info($)'
sg -p 'console.log($)' -r 'logger.info($)'
9. Apply refactoring
9. 执行重构
sg -p 'var $NAME = $' -r 'const $NAME = $' --rewrite
sg -p 'var $NAME = $' -r 'const $NAME = $' --rewrite
10. Search specific language
10. 指定语言搜索
sg -p 'pattern' --lang typescript
undefinedsg -p 'pattern' --lang typescript
undefinedQuick Reference
快速参考
| Task | Command |
|---|---|
| Find pattern | |
| Specific language | |
| Replace (preview) | |
| Replace (apply) | |
| Show context | |
| JSON output | |
| File list only | |
| Count matches | |
| Run YAML rules | |
| 任务 | 命令 |
|---|---|
| 查找模式 | |
| 指定语言 | |
| 替换(预览) | |
| 替换(执行) | |
| 显示上下文 | |
| JSON格式输出 | |
| 仅显示文件列表 | |
| 统计匹配数量 | |
| 运行YAML规则 | |
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
- 查找某个函数/方法的所有调用位置
- 定位特定代码模式(hooks、API调用)
- 为大规模重构做准备
- 正则表达式会产生误匹配时
- 检测反模式和安全问题
- 创建自定义代码检查规则
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
如需完整模式,请查看:
- - JavaScript/TypeScript模式
./references/js-ts-patterns.md - - Python模式
./references/python-patterns.md - - Go和Rust模式
./references/go-rust-patterns.md - - 安全漏洞检测
./references/security-patterns.md - - YAML规则与工具集成
./references/advanced-usage.md - - 自定义规则入门模板
./assets/rule-template.yaml