ast-grep/ast-grep

GitHub: ast-grep/ast-grep

一个基于抽象语法树的代码结构搜索、lint检查和重写工具,让开发者能用类似写普通代码的方式直观定义匹配模式。

Stars: 12824 | Forks: 323

ast-grep

coverage badge Discord Repology Badge Badge GitHub Sponsors Gurubase

## ast-grep(sg) ast-grep(sg) 是一个用于代码结构搜索、lint 和重写的 CLI 工具。 ## 介绍 ast-grep 是一个基于[抽象语法树](https://dev.to/balapriya/abstract-syntax-tree-ast-explained-in-plain-english-1h38)的工具,通过模式代码来搜索代码。把它想象成你的老朋友 [`grep`](https://en.wikipedia.org/wiki/Grep#:~:text=grep%20is%20a%20command%2Dline,which%20has%20the%20same%20effect.),不过它匹配的是 AST 节点而不是文本。 你可以像编写普通代码一样编写模式。它将匹配所有具有相同语法结构的代码。 你可以使用 `$` 符号 + 大写字母作为[通配符](https://en.wikipedia.org/wiki/Wildcard_character),例如 `$MATCH`,来匹配任何单个 AST 节点。把它想象成[正则表达式点](https://regexone.com/lesson/wildcards_dot) `.`,只不过它不是基于文本的。 试试 [在线 Playground](https://ast-grep.github.io/playground.html) 体验一下吧! ## 截图 ![demo](https://ast-grep.github.io/image/search-replace.png) 在[网站](https://ast-grep.github.io/)上查看更多截图。 ## 安装 你可以通过 [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm)、[pip](https://pypi.org/)、[cargo](https://doc.rust-lang.org/cargo/getting-started/installation.html)、[cargo-binstall](https://github.com/cargo-bins/cargo-binstall)、[homebrew](https://brew.sh/)、[scoop](https://scoop.sh/)、[mise](https://github.com/jdx/mise) 或 [MacPorts](https://www.macports.org) 进行安装! ``` npm install --global @ast-grep/cli pip install ast-grep-cli brew install ast-grep ```
点击查看更多安装方法 ``` cargo install ast-grep --locked cargo binstall ast-grep # 通过 scoop 安装,感谢 @brian6932 scoop install main/ast-grep # 通过 MacPorts 安装 sudo port install ast-grep # 在 nix-shell 中试用 ast-grep nix-shell -p ast-grep # 使用 mise 试用 ast-grep mise use -g ast-grep ```
或者你可以从源码构建 ast-grep。你需要安装 rustup,clone 仓库然后执行 ``` cargo install --path ./crates/cli --locked ``` 其他平台也有可用的[包](https://repology.org/project/ast-grep/versions)。 ## 命令行使用示例 ast-grep 具有以下形式。 ``` ast-grep --pattern 'var code = $PATTERN' --rewrite 'let code = new $PATTERN' --lang ts ``` ### 示例 * [使用空值合并运算符重写代码](https://twitter.com/Hchan_mgn/status/1547061516993699841?s=20&t=ldDoj4U2nq-FRKQkU5GWXA) ``` ast-grep -p '$A && $A()' -l ts -r '$A?.()' ``` * [重写](https://twitter.com/Hchan_mgn/status/1561802312846278657) [Zodios](https://github.com/ecyrbe/zodios#migrate-to-v8) ``` ast-grep -p 'new Zodios($URL, $CONF as const,)' -l ts -r 'new Zodios($URL, $CONF)' -i ``` * [使用 YAML 实现 eslint 规则。](https://twitter.com/Hchan_mgn/status/1560108625460355073) ## 功能亮点 ast-grep 的核心是一个基于 tree-sitter 生成的抽象语法树来搜索和替换代码的算法。 它可以帮助你以直观的方式进行轻量级静态分析和大规模代码操作。 主要亮点: * 直观的 AST 查找和替换模式。 ast-grep 的模式看起来就像你每天编写的普通代码(你可以说模式与代码是同构的)。 * 类 jQuery 的 API 用于 AST 遍历和操作。 * YAML 配置用于编写新的 lint 规则或代码修改。 * 使用编译型语言编写,基于 tree-sitter 解析并利用多核。 * 漂亮的命令行界面 :) ast-grep 的愿景是实现抽象语法树魔法的民主化,并将人们从繁琐的 AST 编程中解放出来! * 如果你是开源库作者,ast-grep 可以帮助你的库用户更轻松地适应破坏性变更。 * 如果你是团队的技术负责人,ast-grep 可以帮助你执行针对业务需求定制的代码最佳实践。 * 如果你是安全研究员,ast-grep 可以帮助你更快地编写规则。
标签:CLI, DevSecOps, DNS解析, IPv6支持, Lint工具, odt, Rust, WiFi技术, 上游代理, 云安全监控, 代码安全, 代码搜索, 代码规范, 代码重构, 可视化界面, 威胁情报, 开发者工具, 开源项目, 抽象语法树, 文本处理, 文档结构分析, 模式匹配, 漏洞枚举, 网络流量审计, 自动化资产收集, 通知系统, 静态分析, 默认DNS解析器