npm 安装后可能需执行 pnpm approve-builds;从源码构建需先装 rustup 并指定 crates/cli 路径;示例命令需用 --lang 指定语言
ast-grep
ast-grepMIT活跃维护基于语法树做代码结构化搜索、lint 和批量重写的命令行工具,用 Rust 编写,比正则匹配更精准,适合大规模重构和代码审查。
⚡A CLI tool for code structural search, lint and rewriting. Written in Rust
npm install --global @ast-grep/cliREADME 未说明,仅说明用 Rust 编写、基于 tree-sitter 解析并利用多核
基于 AST 做代码结构搜索、lint 和重写的 Rust CLI 工具,比文本 grep 更懂语法
基于语法树做代码结构化搜索、lint 和批量重写的命令行工具,用 Rust 编写,比正则匹配更精准,适合大规模重构和代码审查。
核心能力
- 用普通代码写模式匹配 AST 节点,$大写字母作通配符
- 支持 --rewrite 按模式批量改写代码
- 用 YAML 配置编写自定义 lint 规则和代码修改
- 基于 tree-sitter 解析并利用多核并行
- 提供类 jQuery 的 AST 遍历与操作 API
容易踩的坑
- 0.51.1最新正式版2026-11-02 · 2026-11-02 05:33 UTC
- fix(cli): report skipped files and reason in --inspect entity
#3044 - chore(deps): update rust crate tree-sitter-scala to v0.26.4
#3054 - fix(dynamic): only replace metavariable sigils with the expando char
#3052 - fix(core): keep $ followed by a digit literal in fix templates
#3045 - fix(deps): update rust-wasm-bindgen monorepo
#3046 - Fix macOS universal2 wheel matching
#3050 - chore(deps): update rust crate napi-derive to v3.7.0
#3049 - chore(deps): update rust crate napi to v3.16.1
#3047 - fix(scan): bind utilDirs utils for --inline-rules and --rules
#2943 - fix(cli): percent-encode file paths in sarif uri
#3042 - chore(deps): update dependency node to v26
#3043 - chore(deps): update dependency oxlint to v1.90.0
#3036 - chore(deps): update astral-sh/setup-uv action to v10.3.3
#3040 - chore(deps): update rust crate globset to v0.4.21
#3037 - chore(deps): update rust crate ignore to v0.4.34
#3038 - chore(deps): update dependency node to v26
#3039 - fix(sarif): set the log version to 2.1.0
#3035 - fix(cli): escape special characters in github format output
#3034 - chore(deps): update dependency web-tree-sitter to ^0.26.0 || ^0.27.0
#3019 - chore(deps): update dependency dprint to v0.64.3
#3029 - chore(deps): update rust crate smallvec to v1.16.4
#3030 - chore(deps): update rust crate similar to v3.2.1
#3031 - chore(deps): update rust crate tree-sitter-md to v0.5.4
#3032 - fix(deps): update rust crate tree-sitter-dart to 0.3.0
#3033 - chore(deps): update dependency oxlint to v1.89.0
#3018 - chore(deps): update dependency @napi-rs/cli to v3.10.9
#3025 - chore(deps): update rust crate napi-derive to v3.6.13
#3026 - chore(deps): update rust crate napi to v3.15.0
#3028 - chore(deps): update astral-sh/setup-uv action to v10.3.2
#3024 - chore(deps): update dependency dprint to v0.64.1
#3017 - chore(deps): update rust crate tokio to v1.54.1
#3020 - refactor(cli): apply project defaults through InputArgs
#3027 - feat(cli): support no-ignore in project config
#3003 - fix(deps): update rust crate tree-sitter-php to 0.25.0
#3022 - chore(deps): update rust crate tree-sitter to v0.27.1
#3014 - chore(deps): update dependency @ast-grep/napi to v0.51.0 [`#
- fix(cli): report skipped files and reason in --inspect entity
- 0.51.02026-10-15 · 2026-10-15 15:10 UTC
查看更新说明
- fix(cli): use
ast-grepname in project setup hint#3001 - fix: replace deprecated serde_yaml with serde-saphyr
#2999 - fix(deps): update rust crate tree-sitter-haskell to 0.24.0
#2988 - fix(cli): preserve files when update-all writes fail
#2995 - Revise security policy for clarity and updates
a7d3f69 - fix: update wasm linting
ea55463
- fix(cli): use
- 0.50.02026-10-09 · 2026-10-09 14:19 UTC
查看更新说明
- feat(outline): add Java annotation type and element rules
#2955 - fix(outline): report Java private/protected fields in the outline
#2950 - feat(outline): add Java enum member rule
#2954 - chore(deps): update dependency @types/node to v24.19.1
#2983 - chore(deps): update dependency dprint to v0.60.1
#2984 - feat(outline): add Java module declaration rule
#2956 - chore(deps): update rust crate termimad to v0.35.5
#2925 - chore(deps): update rust crate napi-derive to v3.6.10
#2936 - chore(deps): update dependency @napi-rs/cli to v3.10.6
#2922 - chore(deps): update dependency chalk to v6.0.1
#2979 - chore(deps): update rust crate tokio to v1.53.2
#2981 - chore(deps): update rust crate thiserror to v2.0.21
#2966 - chore(deps): update dependency oxlint to v1.86.0
#2967 - chore(deps): update rust crate smallvec to v1.16.2
#2968 - feat(outline): add Java wildcard-import outline rules
#2948 - fix(outline): capture C/C++ declarator identifier names
#2946 - feat(cli): add trust and revoke custom language policies
#2972 - refactor(cli): extract custom language policy
#2977 - feat(cli): add custom language loading policy
#2971 - fix(schema): reject null relation fields
#2969 - chore(deps): update rust crate clap_complete to v4.6.11
#2965 - chore(deps): update rust crate clap to v4.6.7
#2964 - chore(deps): update rust crate napi to v3.13.0
#2932 - chore(deps): update dependency @types/node to v24.13.6
#2941 - feat(language): add Zig support
#2942 - fix: require opt-in for custom language libraries
#2960 - refactor: parse default run through app
#2962 - feat(outline): add Java record_declaration rule
#2945 - chore(deps): update rust crate toml_edit to v0.25.15
#2937 - chore(deps): update rust crate smallvec to v1.16.1
#2920 - chore(deps): update dependency @types/node to v24.13.4
#2931 - chore(deps): update dependency dprint to v0.57.4
#2926 - chore(deps): update dependency oxlint to v1.82.0
#2928 - chore(deps): update dependency oxlint to v1.81.0
#2919 - chore(deps): update rust crate termimad to v0.35.3
#2921 - chore(deps): update dependency dprint to v0.57.1
#2923 - chore(deps): update dependency @as
- feat(outline): add Java annotation type and element rules
- 0.45.32026-08-31 · 2026-08-31 01:42 UTC
查看更新说明
- chore(deps): update dependency dprint to v0.57.0
#2916 - feat: add min-severity cli
#2917 - fix(outline): anchor fallback signatures to names
#2913 - feat(outline): add Markdown heading support
#2912 - chore(deps): update dependency oxlint to v1.80.0
#2908 - chore(deps): update dependency @ast-grep/napi to v0.45.2
#2906 - feat: add min-severity cli (#2917)
#2720 - fix: ast-grep-ignore must be at first be the frist alphabet to take effect
#2909 - chore: update tree-sitter 0.27
bb74529 - fix: fix clippy
c6b32d7
- chore(deps): update dependency dprint to v0.57.0
- 0.45.22026-08-23 · 2026-08-23 19:48 UTC
查看更新说明
- chore(deps): update astral-sh/setup-uv action to v10
#2892 - chore(deps): update rust crate terminal-light to v1.9.1
#2905 - chore(deps): update dependency web-tree-sitter to v0.26.13
#2903 - chore(deps): update rust crate tree-sitter to v0.26.13
#2904 - chore(deps): update dependency dprint to v0.56.1
#2901 - chore(deps): update dependency @napi-rs/cli to v3.8.6
#2882 - fix(deps): update rust-wasm-bindgen monorepo
#2878 - chore(deps): update dependency dprint to v0.56.0
#2896 - chore(deps): update dependency oxlint to v1.79.0
#2897 - chore(deps): update dependency @ast-grep/napi to v0.45.1
#2876 - chore(deps): update dependency web-tree-sitter to v0.26.12
#2880 - fix: exit quietly on broken pipe instead of erroring
#2887 - feat(outline): extract embedded language symbols
#2883 - chore(deps): update dependency oxlint to v1.78.0
#2888 - feat: add namedChildren support to wasm
#2893 - feat: add namedChildren to napi
#2893 - feat: add named_children to pyo3
4dc0d36 - fix: bump down napi
61f1b98 - chore: bump deps
912da32
- chore(deps): update astral-sh/setup-uv action to v10
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
ast-grep(sg)
ast-grep(sg) is a CLI tool for code structural search, lint, and rewriting.
Introduction
ast-grep is an abstract syntax tree based tool to search code by pattern code. Think of it as your old-friend grep, but matching AST nodes instead of text.
You can write patterns as if you are writing ordinary code. It will match all code that has the same syntactical structure.
You can use $ sign + upper case letters as a wildcard, e.g. $MATCH, to match any single AST node. Think of it as regular expression dot ., except it is not textual.
Try the online playground for a taste!
Screenshot

See more screenshots on the website.
Installation
You can install it from npm, pip, cargo, cargo-binstall, homebrew, scoop, mise or MacPorts!
npm install --global @ast-grep/cli
# `pnpm approve-builds` may be needed
pip install ast-grep-cli
brew install ast-grep
cargo install ast-grep --locked
cargo binstall ast-grep
# install via scoop, thank @brian6932
scoop install main/ast-grep
# install via MacPorts
sudo port install ast-grep
# try ast-grep in nix-shell
nix-shell -p ast-grep
# try ast-grep with mise
mise use -g ast-grep
Or you can build ast-grep from source. You need to install rustup, clone the repository and then
cargo install --path ./crates/cli --locked
Packages are available on other platforms too.
Command line usage example
ast-grep has following form.
ast-grep --pattern 'var code = $PATTERN' --rewrite 'let code = new $PATTERN' --lang ts
Example
ast-grep -p '$A && $A()' -l ts -r '$A?.()'
ast-grep -p 'new Zodios($URL, $CONF as const,)' -l ts -r 'new Zodios($URL, $CONF)' -i
Sponsor
If you find ast-grep interesting and useful for your work, please buy me a coffee so I can spend more time on the project!
Feature Highlight
ast-grep's core is an algorithm to search and replace code based on abstract syntax tree produced by tree-sitter. It can help you to do lightweight static analysis and massive scale code manipulation in an intuitive way.
Key highlights:
-
An intuitive pattern to find and replace AST. ast-grep's pattern looks like ordinary code you would write every day (you could say the pattern is isomorphic to code).
-
jQuery like API for AST traversal and manipulation.
-
YAML configuration to write new linting rules or code modification.
-
Written in compiled language, with tree-sitter based parsing and utilizing multiple cores.
-
Beautiful command line interface :)
ast-grep's vision is to democratize abstract syntax tree magic and to liberate one from cumbersome AST programming!
- If you are an open-source library author, ast-grep can help your library users adopt breaking changes more easily.
- if you are a tech lead in your team, ast-grep can help you enforce code best practice tailored to your business need.
- If you are a security researcher, ast-grep can help you write rules much faster.