Skip to main content

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.

Metadata

Release files for ast-grep-cli 0.45.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ast-grep-cli 0.45.2
File Size Uploaded
ast_grep_cli-0.45.2.tar.gz 281.1 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for ast-grep-cli 0.45.2
File
ast_grep_cli-0.45.2-py3-none-win_arm64.whl Python 3 none Windows ARM64 Details
ast_grep_cli-0.45.2-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details
ast_grep_cli-0.45.2-py3-none-win32.whl Python 3 none Windows x86-32 Details
ast_grep_cli-0.45.2-py3-none-manylinux_2_28_x86_64.whl Python 3 none Linux glibc 2.28+ x86-64 Details
ast_grep_cli-0.45.2-py3-none-manylinux_2_28_aarch64.whl Python 3 none Linux glibc 2.28+ ARM64 Details
ast_grep_cli-0.45.2-py3-none-macosx_10_12_x86_64.whl Python 3 none macOS 10.12+ x86-64 Details
ast_grep_cli-0.45.2-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl Python 3 none macOS 10.12+ universal2 (ARM64, x86-64), macOS 10.12+ x86-64, macOS 11.0+ ARM64 Details

Total release size: 85.7 MB

Release files / ast_grep_cli-0.45.2.tar.gz

Download URL ast_grep_cli-0.45.2.tar.gz
Size 281.1 kB
Tags Source
SHA-256 checksum
How to use checksums
300bd5161bd7e6db532ea330647137381f461e0e995b72e56e5e3abcd611ebc3
BLAKE2b-256 checksum
How to use checksums
82ad2f00726d90bcdbf821e1f8893a219d531d3a4a3a525c0ea7554c36b44328
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / ast_grep_cli-0.45.2-py3-none-win_arm64.whl

Download URL ast_grep_cli-0.45.2-py3-none-win_arm64.whl
Size 15.3 MB
Tags Python 3 Windows ARM64
SHA-256 checksum
How to use checksums
1d5ab9db412f3f95dce9eadc3c3bd4d37d0ae7d1182b148c03fff81a8c6c3f40
BLAKE2b-256 checksum
How to use checksums
76a0882638f2c1e8ec63500f9a7ae256292e7cdb8177abf6b2e7d5f09266a3f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / ast_grep_cli-0.45.2-py3-none-win_amd64.whl

Download URL ast_grep_cli-0.45.2-py3-none-win_amd64.whl
Size 15.8 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
19aab16ad9bc361d1a750c8e5e36b2d746e9c256c9bd83be08925cb1469c6e6b
BLAKE2b-256 checksum
How to use checksums
9f7f8c573f3e3102debc034adfe3fb9806a89076ada4c767eb1930700d044e4d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / ast_grep_cli-0.45.2-py3-none-win32.whl

Download URL ast_grep_cli-0.45.2-py3-none-win32.whl
Size 14.9 MB
Tags Python 3 Windows x86-32
SHA-256 checksum
How to use checksums
f613a82dcb51527ef0d63b26261f10ea24990ffb0c8b9dda2abbacfa45d1d982
BLAKE2b-256 checksum
How to use checksums
0548f956aca058464cb43eb9669f06bfa19d2c5e7613a80277f41e13cd4a854d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / ast_grep_cli-0.45.2-py3-none-manylinux_2_28_x86_64.whl

Download URL ast_grep_cli-0.45.2-py3-none-manylinux_2_28_x86_64.whl
Size 8.0 MB
Tags Linux glibc 2.28+ x86-64 Python 3
SHA-256 checksum
How to use checksums
bd41060e56fd4c7611050d0dbb3f0741336725b0741ba3eec2ff0d0059042d45
BLAKE2b-256 checksum
How to use checksums
4c6d9e9651cad69aa2804621d149067210668fbc7debb3dce93b568b39e1e3ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / ast_grep_cli-0.45.2-py3-none-manylinux_2_28_aarch64.whl

Download URL ast_grep_cli-0.45.2-py3-none-manylinux_2_28_aarch64.whl
Size 7.7 MB
Tags Linux glibc 2.28+ ARM64 Python 3
SHA-256 checksum
How to use checksums
cbaf74b65ea2c3918b23e9578e1790aec6951d5aa8070b7d912e5fb79a7afedb
BLAKE2b-256 checksum
How to use checksums
1810b5f21447fe303d6409520282ec3a0e516e114d0730ec8f7274c879e8f4a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / ast_grep_cli-0.45.2-py3-none-macosx_10_12_x86_64.whl

Download URL ast_grep_cli-0.45.2-py3-none-macosx_10_12_x86_64.whl
Size 7.9 MB
Tags Python 3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
c4afe7aa52171d8d482b95e5db6289cc97e2678adee15ad15af25a41da036d02
BLAKE2b-256 checksum
How to use checksums
e48220718fa8c4b5ea9f471a906022343b25053216249ee508a40dd806cfefbb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / ast_grep_cli-0.45.2-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl

Download URL ast_grep_cli-0.45.2-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Size 15.7 MB
Tags Python 3 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
ecc6edefd6e87a53ed41997071802b8b81e9417a6009089887d930968755aa18
BLAKE2b-256 checksum
How to use checksums
97c3ef578c61ef91b4cf7429442f3067915ea96e027603591ffcaa5f7fc45929
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

0.45.3

8 release files

This release

0.45.2 This release

8 release files

0.45.0

8 release files

0.44.0

8 release files

0.42.3

8 release files

0.42.2

8 release files

0.42.0

8 release files

0.41.1

8 release files

0.41.0

8 release files

0.40.5

8 release files

0.40.3

8 release files

0.40.1

8 release files

0.40.0

8 release files

0.39.7

8 release files

0.39.4

8 release files

0.39.2

8 release files

0.39.1

8 release files

0.39.0

8 release files

0.38.7

8 release files

0.38.3

8 release files

0.38.2

8 release files

0.38.1

8 release files

0.38.0

8 release files

0.37.0

8 release files

0.36.3

8 release files

0.36.2

8 release files

0.36.1

8 release files

0.35.0

8 release files

0.34.3

8 release files

0.34.2

8 release files

0.34.1

8 release files

0.34.0

8 release files

0.33.1

8 release files

0.32.3

8 release files

0.32.2

8 release files

0.32.1

8 release files

0.32.0

8 release files

0.30.1

8 release files

0.30.0

8 release files

0.29.0

8 release files

0.28.1

8 release files

0.27.3

8 release files

0.27.2

8 release files

0.26.3

8 release files

0.26.2

8 release files

0.25.4

8 release files

0.25.3

8 release files

0.25.2

8 release files

0.25.1

8 release files

0.25.0

8 release files

0.24.1

8 release files

0.24.0

8 release files

0.23.1

8 release files

0.23.0

8 release files

0.22.4

8 release files

0.22.3

8 release files

0.22.2

8 release files

0.22.1

8 release files

0.21.1

8 release files

0.20.5

8 release files

0.20.2

8 release files

0.20.1

8 release files

0.20.0

8 release files

0.19.4

8 release files

0.19.3

8 release files

0.19.2

8 release files

0.19.1

8 release files

0.19.0

8 release files

0.18.0

8 release files

0.17.1

8 release files

0.17.0

8 release files

0.16.0

8 release files

0.15.1

8 release files

0.15.0

8 release files

0.14.4

8 release files

0.14.3

8 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page