Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ast-grep/ast-grep
⚡A CLI tool for code structural search, lint and rewriting. Written in Rust
https://github.com/ast-grep/ast-grep
ast babel codemod codereview command-line command-line-tool grep linter refactoring rust search static-analysis structural-search tree-sitter typescript
Last synced: 5 days ago
JSON representation
⚡A CLI tool for code structural search, lint and rewriting. Written in Rust
- Host: GitHub
- URL: https://github.com/ast-grep/ast-grep
- Owner: ast-grep
- License: mit
- Created: 2022-07-01T19:21:00.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-29T20:16:19.000Z (2 months ago)
- Last Synced: 2024-10-29T22:45:52.313Z (2 months ago)
- Topics: ast, babel, codemod, codereview, command-line, command-line-tool, grep, linter, refactoring, rust, search, static-analysis, structural-search, tree-sitter, typescript
- Language: Rust
- Homepage: https://ast-grep.github.io/
- Size: 5.7 MB
- Stars: 7,395
- Watchers: 24
- Forks: 169
- Open Issues: 56
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-rust - ast-grep - A CLI tool for code structural search, lint and rewriting. (Applications / Productivity)
- awesome-rust - ast-grep - A CLI tool for code structural search, lint and rewriting. (Applications / Productivity)
- StarryDivineSky - ast-grep/ast-grep - grep 是一个基于 AST 的工具,用于按模式代码搜索代码。将其视为您的老朋友, grep 但它匹配 AST 节点而不是文本。您可以像编写普通代码一样编写模式。它将匹配具有相同语法结构的所有代码。您可以使用 $ 符号 + 大写字母作为通配符,例如 $MATCH ,以匹配任何单个 AST 节点。将其视为 正则表达式点 . ,但它不是文本的。 (Rust 程序设计 / 网络服务_其他)
- my-awesome - ast-grep/ast-grep - line,command-line-tool,grep,linter,refactoring,rust,search,static-analysis,structural-search,tree-sitter,typescript pushed_at:2025-01 star:7.8k fork:0.2k ⚡A CLI tool for code structural search, lint and rewriting. Written in Rust (Rust)
- fucking-awesome-rust - ast-grep - A CLI tool for code structural search, lint and rewriting. (Applications / Productivity)
- fucking-awesome-rust - ast-grep - A CLI tool for code structural search, lint and rewriting. (Applications / Productivity)
README
## ast-grep(sg)
ast-grep(sg) is a CLI tool for code structural search, lint, and rewriting.
## Introduction
ast-grep is an AST-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 wildcard, e.g. `$MATCH`, to match any single AST node. Think of it as REGEX dot `.`, except it is not textual.Try the [online playground](https://ast-grep.github.io/playground.html) for a taste!
## Demo
![demo](https://ast-grep.github.io/image/search-replace.png)See more screenshots on the [website](https://ast-grep.github.io/).
## Installation
You can install it from [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), [homebrew](https://brew.sh/), [scoop](https://scoop.sh/) or [MacPorts](https://www.macports.org)!```bash
npm install --global @ast-grep/cli
pip install ast-grep-cli
cargo install ast-grep --locked# install via homebrew, thank @henryhchchc
brew install 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
```
Or you can build ast-grep from source. You need to install rustup, clone the repository and then
```bash
cargo install --path ./crates/cli --locked
```
[Packages](https://repology.org/project/ast-grep/versions) are available on other platforms too.## Command line usage example
ast-grep has following form.
```
sg --pattern 'var code = $PATTERN' --rewrite 'let code = new $PATTERN' --lang ts
```### Example
* [Rewrite code in null coalescing operator](https://twitter.com/Hchan_mgn/status/1547061516993699841?s=20&t=ldDoj4U2nq-FRKQkU5GWXA)
```bash
sg -p '$A && $A()' -l ts -r '$A?.()'
```* [Rewrite](https://twitter.com/Hchan_mgn/status/1561802312846278657) [Zodios](https://github.com/ecyrbe/zodios#migrate-to-v8)
```bash
sg -p 'new Zodios($URL, $CONF as const,)' -l ts -r 'new Zodios($URL, $CONF)' -i
```* [Implement eslint rule using YAML.](https://twitter.com/Hchan_mgn/status/1560108625460355073)
## Sponsor
![Sponsors](https://raw.githubusercontent.com/HerringtonDarkholme/sponsors/main/sponsorkit/sponsors.svg)If you find ast-grep interesting and useful for your work, please [buy me a coffee](https://github.com/sponsors/HerringtonDarkholme)
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.