ast-grep
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseast-grep
ast-grep
Structural code search and rewriting using AST matching.
基于AST匹配的结构化代码搜索与重写工具。
Pattern Syntax
模式语法
| Pattern | Description |
|---|---|
| Match a single AST node and capture it as |
| Match zero or more AST nodes (spread) and capture as |
| Anonymous placeholder (matches any single node, no capture) |
| Anonymous spread placeholder (matches any number of nodes) |
Shell quoting: Escape as or wrap in single quotes.
$\$VAR| 模式 | 描述 |
|---|---|
| 匹配单个AST节点并将其捕获为 |
| 匹配零个或多个AST节点(展开)并捕获为 |
| 匿名占位符(匹配任意单个节点,不捕获) |
| 匿名展开占位符(匹配任意数量的节点) |
Shell 转义: 将转义为或用单引号包裹。
$\$VARSupported Languages
支持的语言
javascript, typescript, tsx, html, css, python, go, rust, java, c, cpp, csharp, ruby, php, yaml
javascript, typescript, tsx, html, css, python, go, rust, java, c, cpp, csharp, ruby, php, yaml
Commands
命令
| Command | Description |
|---|---|
| One-time search or rewrite (default) |
| Scan and rewrite by configuration |
| Test ast-grep rules |
| Create new project or rules |
| 命令 | 描述 |
|---|---|
| 一次性搜索或重写(默认命令) |
| 按配置执行扫描与重写 |
| 测试ast-grep规则 |
| 创建新项目或规则 |
Search Examples
搜索示例
bash
undefinedbash
undefinedFind console.log calls
查找console.log调用
ast-grep run --pattern 'console.log($$$ARGS)' --lang javascript .
ast-grep run --pattern 'console.log($$$ARGS)' --lang javascript .
Find React useState hooks
查找React useState钩子
ast-grep run --pattern 'const [$STATE, $SETTER] = useState($INIT)' --lang tsx .
ast-grep run --pattern 'const [$STATE, $SETTER] = useState($INIT)' --lang tsx .
Find async functions
查找异步函数
ast-grep run --pattern 'async function $NAME($$$ARGS) { $$$BODY }' --lang typescript .
ast-grep run --pattern 'async function $NAME($$$ARGS) { $$$BODY }' --lang typescript .
Find Express route handlers
查找Express路由处理器
ast-grep run --pattern 'app.$METHOD($PATH, ($$$ARGS) => { $$$BODY })' --lang javascript .
ast-grep run --pattern 'app.$METHOD($PATH, ($$$ARGS) => { $$$BODY })' --lang javascript .
Find Python function definitions
查找Python函数定义
ast-grep run --pattern 'def $NAME($$$ARGS): $$$BODY' --lang python .
ast-grep run --pattern 'def $NAME($$$ARGS): $$$BODY' --lang python .
Find Go error handling
查找Go错误处理
ast-grep run --pattern 'if $ERR != nil { $$$BODY }' --lang go .
ast-grep run --pattern 'if $ERR != nil { $$$BODY }' --lang go .
Find all function calls
查找所有函数调用
ast-grep run --pattern '$FUNCTION($$$_)' --lang typescript .
ast-grep run --pattern '$FUNCTION($$$_)' --lang typescript .
Find specific function calls
查找特定函数调用
ast-grep run --pattern 'fetch($$$_)' --lang typescript .
ast-grep run --pattern 'fetch($$$_)' --lang typescript .
Find try-catch blocks
查找try-catch块
ast-grep run --pattern 'try { $$$BODY } catch ($$$_)' --lang typescript .
ast-grep run --pattern 'try { $$$BODY } catch ($$$_)' --lang typescript .
Find Promise error handling
查找Promise错误处理
ast-grep run --pattern '.catch($$$_)' --lang typescript .
undefinedast-grep run --pattern '.catch($$$_)' --lang typescript .
undefinedSearch and Replace (Dry Run)
搜索与替换(试运行)
bash
undefinedbash
undefinedReplace == with ===
将 == 替换为 ===
ast-grep run --pattern '$A == $B' --rewrite '$A === $B' --lang javascript .
ast-grep run --pattern '$A == $B' --rewrite '$A === $B' --lang javascript .
Replace != with !==
将 != 替换为 !==
ast-grep run --pattern '$A != $B' --rewrite '$A !== $B' --lang typescript .
ast-grep run --pattern '$A != $B' --rewrite '$A !== $B' --lang typescript .
Convert function to arrow function
将普通函数转换为箭头函数
ast-grep run --pattern 'function $NAME($$$ARGS) { $$$BODY }'
--rewrite 'const $NAME = ($$$ARGS) => { $$$BODY }' --lang javascript .
--rewrite 'const $NAME = ($$$ARGS) => { $$$BODY }' --lang javascript .
ast-grep run --pattern 'function $NAME($$$ARGS) { $$$BODY }'
--rewrite 'const $NAME = ($$$ARGS) => { $$$BODY }' --lang javascript .
--rewrite 'const $NAME = ($$$ARGS) => { $$$BODY }' --lang javascript .
Replace var with let
将var替换为let
ast-grep run --pattern 'var $NAME = $VALUE' --rewrite 'let $NAME = $VALUE' --lang javascript .
ast-grep run --pattern 'var $NAME = $VALUE' --rewrite 'let $NAME = $VALUE' --lang javascript .
Add optional chaining
添加可选链操作符
ast-grep run --pattern '$OBJ && $OBJ.$PROP' --rewrite '$OBJ?.$PROP' --lang javascript .
ast-grep run --pattern '$OBJ && $OBJ.$PROP' --rewrite '$OBJ?.$PROP' --lang javascript .
Replace console.log with logger
将console.log替换为logger
ast-grep run --pattern 'console.log($$$)' --rewrite 'logger.info($$$)' --lang typescript .
undefinedast-grep run --pattern 'console.log($$$)' --rewrite 'logger.info($$$)' --lang typescript .
undefinedApply Changes
应用修改
bash
undefinedbash
undefinedApply changes with --update-all
使用--update-all应用修改
ast-grep run --pattern '$A == $B' --rewrite '$A === $B' --lang javascript --update-all .
undefinedast-grep run --pattern '$A == $B' --rewrite '$A === $B' --lang javascript --update-all .
undefinedConfiguration-Based Rules
基于配置的规则
yaml
undefinedyaml
undefinedrules/my-rules.yaml
rules/my-rules.yaml
patterns:
- pattern: "console.log($$$)" rewrite: "logger.info($$$)" languages: [javascript, typescript] message: "Use logger instead of console.log"
```bashpatterns:
- pattern: "console.log($$$)" rewrite: "logger.info($$$)" languages: [javascript, typescript] message: "Use logger instead of console.log"
```bashRun custom rules
运行自定义规则
ast-grep scan --project my-rules
ast-grep scan --project my-rules
Dry-run preview
试运行预览
ast-grep scan --project my-rules --dry-run
undefinedast-grep scan --project my-rules --dry-run
undefinedTips
小贴士
- Use to preview changes before applying
--dry-run - Use to apply to all matching files
--update-all - Use single quotes for patterns with variables
$ - Combine with for detailed matching info
--verbose
- 使用在应用修改前预览变更
--dry-run - 使用将修改应用到所有匹配的文件
--update-all - 对于包含变量的模式,使用单引号包裹
$ - 结合获取详细的匹配信息
--verbose