Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
A

ast-grep

> 开发工具
Open source

⚡A CLI tool for code structural search, lint and rewriting. Written in Rust

15.3K stars0 likes3 views
WebsiteGitHub

About

⚡A CLI tool for code structural search, lint and rewriting. Written in Rust

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!

bash
npm install --global @ast-grep/cli
# `pnpm approve-builds` may be needed
pip install ast-grep-cli
brew install ast-grep
Click for more installation methods
bash
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

bash
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

  • Rewrite code in null coalescing operator
bash
ast-grep -p '$A && $A()' -l ts -r '$A?.()'
  • Rewrite Zodios
bash
ast-grep -p 'new Zodios($URL,  $CONF as const,)' -l ts -r 'new Zodios($URL, $CONF)' -i
  • Implement eslint rule using YAML.

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.

Issues· 70 open

View all issuesOpen on GitHub
  • #11

    Dependency Dashboard

    Updated Oct 1, 2026
  • #2970

    Support custom outline rules for built-in languages

    enhancementUpdated Sep 27, 2026
  • #2963

    Unknown keys in rule YAML are silently ignored (e.g. `ignore` vs `ignores`)

    enhancementUpdated Sep 24, 2026
  • #2959

    --update-all leaves a file at 0 bytes when interrupted between open and write

    Updated Sep 22, 2026
  • #2957

    outline: a nested declaration produces no element and its members are attributed to the enclosing one

    Updated Sep 19, 2026
  • #2953

    outline: Java module declarations are missing from java.yml

    Updated Sep 19, 2026
  • #2952

    outline: Java annotation types and elements are missing from java.yml

    Updated Sep 19, 2026
  • #2951

    outline: Java enum constants are missing from java.yml

    Updated Sep 19, 2026
  • #2949

    outline: Java private/protected fields are missing from java.yml

    Updated Sep 19, 2026
  • #2944

    outline: Java `record_declaration` missing from `java.yml`

    Updated Sep 18, 2026

> Tags

Rustastbabelcodemodcodereview

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedOct 1, 2026
Category开发工具
PricingOpen source

> Related tools

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具