Loading...
Loading...
Compare original and translation side by side
ast-grepast-grep| Use ast-grep when... | Use grep/rg when... |
|---|---|
| Pattern depends on code structure | Simple text or regex match |
| Need to match any number of arguments | Searching logs, docs, config |
| Refactoring across many files | One-off literal string search |
| Finding anti-patterns (empty catch, etc.) | Language doesn't matter |
| Replacing while preserving variables | Quick filename/line check |
| 适合使用 ast-grep 的场景 | 适合使用 grep/rg 的场景 |
|---|---|
| 匹配模式依赖代码结构 | 简单的文本或正则匹配 |
| 需要匹配任意数量的参数 | 搜索日志、文档、配置文件 |
| 跨多文件执行重构 | 一次性字面量字符串搜索 |
| 查找代码反模式(如空 catch 块等) | 不限制搜索内容的语言类型 |
| 替换内容同时保留变量 | 快速检查文件名/行号 |
| Pattern | Matches | Example |
|---|---|---|
| Single AST node | |
| Zero or more nodes | |
| Single node (no capture) | |
| Same node repeated | Finds |
| 模式 | 匹配规则 | 示例 |
|---|---|---|
| 单个 AST 节点 | |
| 零个或多个节点 | |
| 单个节点(不捕获) | |
| 重复出现的相同节点 | 可匹配 |
undefinedundefinedundefinedundefinedundefinedundefinedundefinedundefined| Code | Language | Code | Language |
|---|---|---|---|
| JavaScript | | Python |
| TypeScript | | Rust |
| JSX | | Go |
| TSX | | Java |
| C++ | | Ruby |
| C | | PHP |
| 代码 | 编程语言 | 代码 | 编程语言 |
|---|---|---|---|
| JavaScript | | Python |
| TypeScript | | Rust |
| JSX | | Go |
| TSX | | Java |
| C++ | | Ruby |
| C | | PHP |
undefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefined| Flag | Purpose |
|---|---|
| Search pattern |
| Replacement pattern |
| Target language |
| Review changes one by one |
| Apply all changes |
| JSON output ( |
| Lines after match |
| Lines before match |
| Lines around match |
| Debug pattern parsing |
| 参数 | 用途 |
|---|---|
| 搜索模式 |
| 替换模式 |
| 目标语言 |
| 逐次审核变更 |
| 应用所有变更 |
| JSON 格式输出(可选 |
| 展示匹配结果后N行 |
| 展示匹配结果前N行 |
| 展示匹配结果前后N行 |
| 调试模式解析查询 |
ast-grep scanundefinedast-grep scanundefined
Minimal rule file:
```yaml
id: no-empty-catch
language: JavaScript
severity: warning
message: Empty catch block hides errors
rule:
pattern: try { $$$ } catch ($E) { }
fix: |
try { $$$ } catch ($E) { console.error($E) }
最小规则文件示例:
```yaml
id: no-empty-catch
language: JavaScript
severity: warning
message: Empty catch block hides errors
rule:
pattern: try { $$$ } catch ($E) { }
fix: |
try { $$$ } catch ($E) { console.error($E) }| Context | Command |
|---|---|
| Quick structural search | |
| Count matches | |
| File list only | |
| Batch refactor | |
| Scan with rules | |
| Debug pattern | |
| 场景 | 命令 |
|---|---|
| 快速结构化搜索 | |
| 统计匹配数量 | |
| 仅输出匹配的文件列表 | |
| 批量重构 | |
| 使用规则扫描 | |
| 调试模式解析规则 | |
undefinedundefinedundefinedundefined