MCPcopy Index your code
hub / github.com/ast-grep/ast-grep

github.com/ast-grep/ast-grep @0.44.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release 0.44.1 ↗ · + Follow
2,907 symbols 10,060 edges 181 files 179 documented · 6% updated 1d ago0.44.1 · 2026-07-04★ 14,97335 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

ast-grep

coverage badge Discord Repology Badge Badge GitHub Sponsors Gurubase

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

demo

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

Click for more installation methods

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

Sponsors

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.

Extension points exported contracts — how you extend this code

Language (Interface)
Trait to abstract ts-language usage in ast-grep, which includes: which character is used for meta variable. if we need t [11 …
crates/core/src/language.rs
PrintProcessor (Interface)
A trait to process nodeMatches to diff/match output it must be Send + 'static to be shared in worker thread [5 implementers]
crates/cli/src/print/mod.rs
DependentRule (Interface)
(no doc) [3 implementers]
crates/config/src/rule/deserialize_env.rs
LangRegistration (Interface)
The JSON object to register dynamic languages
crates/napi/types/registerDynamicLang.d.ts
JsValueExt (Interface)
(no doc) [1 implementers]
crates/wasm/src/ts_types.rs
LSPLang (Interface)
(no doc) [1 implementers]
crates/lsp/src/lib.rs
Alias (Interface)
(no doc)
crates/language/src/lib.rs
Matcher (Interface)
`Matcher` defines whether a tree-sitter node matches certain pattern, and update the matched meta-variable values in `Me [22 …
crates/core/src/matcher.rs

Core symbols most depended-on inside this repo

unwrap
called by 320
crates/config/src/maybe.rs
collect
called by 221
crates/config/src/combined.rs
from_str
called by 192
crates/config/src/lib.rs
ast_grep
called by 191
crates/core/src/tree_sitter/mod.rs
root
called by 186
crates/core/src/node.rs
clone
called by 185
crates/config/src/rule/referent_rule.rs
iter
called by 163
crates/config/src/rule/parameterized_util.rs
push
called by 156
crates/cli/src/outline/output.rs

Shape

Function 1,495
Method 1,007
Class 267
Enum 95
Interface 43

Languages

Rust96%
Python2%
TypeScript1%

Modules by API surface

crates/core/src/node.rs71 symbols
crates/core/src/matcher/pattern.rs66 symbols
crates/core/src/tree_sitter/mod.rs58 symbols
crates/wasm/tests/web.rs53 symbols
crates/config/src/rule_config.rs51 symbols
crates/core/src/tree_sitter/traversal.rs47 symbols
crates/config/src/rule_core.rs46 symbols
crates/napi/src/sg_node.rs45 symbols
crates/lsp/src/lib.rs44 symbols
crates/cli/src/print/json_print.rs44 symbols
crates/wasm/src/sg_node.rs43 symbols
crates/config/src/rule/selector.rs42 symbols

For agents

$ claude mcp add ast-grep \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page