ast-grep
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseast-grep (sg)
ast-grep (sg)
ast-grep is a fast and polyglot tool for code searching, linting, and rewriting based on Abstract Syntax Trees (AST). It excels at structural search and replace where regex fails.
ast-grep是一款基于抽象语法树(AST)的快速多语言工具,用于代码搜索、代码检查与代码重写。在正则表达式无法胜任的结构化搜索和替换场景中,它表现出色。
When to use
适用场景
- Structural Search: Finding code based on structure (e.g., "all function calls to with 2 arguments") regardless of whitespace.
foo - Refactoring: Renaming variables, changing function signatures, or transforming code patterns safely.
- Linting: Creating custom rules to enforce code style or best practices.
- Code Analysis: Extracting information from codebases.
- 结构化搜索:基于代码结构查找内容(例如“所有传入2个参数的函数调用”),不受空格格式影响。
foo - 代码重构:安全地重命名变量、修改函数签名或转换代码模式。
- 代码检查:创建自定义规则以强制执行代码风格或最佳实践。
- 代码分析:从代码库中提取信息。
Quick Start
快速开始
CLI Basics
CLI基础操作
bash
undefinedbash
undefinedSearch (pattern must be in single quotes)
搜索(模式必须用单引号包裹)
ast-grep -p '$A + $B' --lang ts
ast-grep -p '$A + $B' --lang ts
Rewrite (dry run)
重写(试运行)
ast-grep -p '$A != null' --rewrite '$A' --lang ts
ast-grep -p '$A != null' --rewrite '$A' --lang ts
Interactive Rewrite
交互式重写
ast-grep -p 'var $A = $B' --rewrite 'const $A = $B' --interactive
undefinedast-grep -p 'var $A = $B' --rewrite 'const $A = $B' --interactive
undefinedPattern Syntax
模式语法
- Meta-variables: matches any single node.
$VAR - Multi-meta-variables: matches zero or more nodes (list of items).
$$$ARGS - Wildcard: matches any node (non-capturing).
$_ - Anonymous: matches any list of nodes (non-capturing).
$$
See Pattern Syntax for details.
- 元变量:匹配任意单个节点。
$VAR - 多元变量:匹配零个或多个节点(节点列表)。
$$$ARGS - 通配符:匹配任意节点(非捕获型)。
$_ - 匿名变量:匹配任意节点列表(非捕获型)。
$$
详情请查看模式语法。
Core Concepts
核心概念
Understanding Named vs Unnamed nodes and Matching Strictness is crucial for precise patterns.
- Named Nodes: ,
identifier(matched byfunction_definition).$VAR - Unnamed Nodes: ,
(,)(skipped by default in;mode).smart - Strictness: Control matching precision (,
smart,cst,ast,relaxed).signature
See Core Concepts for details.
理解命名节点与未命名节点以及匹配严格度是编写精准模式的关键。
- 命名节点:、
identifier(可通过function_definition匹配)。$VAR - 未命名节点:、
(、)(默认在;模式下会被忽略)。smart - 严格度:控制匹配精度(可选值:、
smart、cst、ast、relaxed)。signature
详情请查看核心概念。
Rule Configuration (YAML)
规则配置(YAML)
For complex tasks, use YAML configuration files.
yaml
id: no-console-log
language: TypeScript
rule:
pattern: console.log($$$ARGS)
inside:
kind: function_declaration
stopBy: end
fix: '' # Remove the logSee Rule Configuration for details.
对于复杂任务,可使用YAML配置文件。
yaml
id: no-console-log
language: TypeScript
rule:
pattern: console.log($$$ARGS)
inside:
kind: function_declaration
stopBy: end
fix: '' # 移除日志语句详情请查看规则配置。
Advanced Rewriting
高级重写
ast-grep supports complex transformations (regex replace, case conversion) and rewriters for sub-node transformation.
See Rewriting & Transformations for details.
ast-grep支持复杂的转换操作(正则替换、大小写转换)以及用于子节点转换的重写器。
详情请查看重写与转换。
Project Setup & Testing
项目设置与测试
For larger projects, organize rules and tests using .
sgconfig.yml- Scaffold:
ast-grep new project - Config: defines rule and test directories.
sgconfig.yml - Testing: Define and
validcases to ensure rule accuracy.invalid
See Project Setup & Testing for details.
针对大型项目,可使用组织规则与测试用例。
sgconfig.yml- 初始化项目:
ast-grep new project - 配置文件:定义规则与测试目录。
sgconfig.yml - 测试:定义和
valid用例以确保规则的准确性。invalid
详情请查看项目设置与测试。
Utility Rules
工具规则
Reuse logic with local or global utility rules. Enables recursive matching.
yaml
utils:
is-literal:
any: [{kind: string}, {kind: number}]
rule:
matches: is-literalSee Utility Rules for details.
可复用本地或全局的工具规则,支持递归匹配。
yaml
utils:
is-literal:
any: [{kind: string}, {kind: number}]
rule:
matches: is-literal详情请查看工具规则。
Configuration Reference
配置参考
Full reference for YAML fields (, , , ) and supported languages.
idseverityfilesignoresSee Configuration Reference for details.
包含YAML字段(、、、)及支持语言的完整参考。
idseverityfilesignores详情请查看配置参考。
CLI Reference
CLI参考
Common commands: , , , , .
scanrunnewtestlspSee CLI Reference for details.
常用命令:、、、、。
scanrunnewtestlsp详情请查看CLI参考。