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.50.0

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.50.0
File Size Uploaded
ast_grep_cli-0.50.0.tar.gz 289.6 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for ast-grep-cli 0.50.0
File
ast_grep_cli-0.50.0-py3-none-win_arm64.whl Python 3 none Windows ARM64 Details
ast_grep_cli-0.50.0-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details
ast_grep_cli-0.50.0-py3-none-win32.whl Python 3 none Windows x86-32 Details
ast_grep_cli-0.50.0-py3-none-manylinux_2_28_x86_64.whl Python 3 none Linux glibc 2.28+ x86-64 Details
ast_grep_cli-0.50.0-py3-none-manylinux_2_28_aarch64.whl Python 3 none Linux glibc 2.28+ ARM64 Details
ast_grep_cli-0.50.0-py3-none-macosx_10_12_x86_64.whl Python 3 none macOS 10.12+ x86-64 Details
ast_grep_cli-0.50.0-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 11.0+ ARM64, macOS 10.12+ x86-64 Details

Total release size: 86.7 MB

Release files / ast_grep_cli-0.50.0.tar.gz

Download URL ast_grep_cli-0.50.0.tar.gz
Size 289.6 kB
Tags Source
SHA-256 checksum
How to use checksums
acdde02c80c99a26c9bdef6d89e824be799454f08fcbaadb94bea255bb9ba3de
BLAKE2b-256 checksum
How to use checksums
40723aebe818d0ea31ac724cc1735b06aaebb4e46dd5c2d560066e5503ac46aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.24 {"installer":{"name":"uv","version":"0.12.24","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.50.0-py3-none-win_arm64.whl

Download URL ast_grep_cli-0.50.0-py3-none-win_arm64.whl
Size 15.4 MB
Tags Python 3 Windows ARM64
SHA-256 checksum
How to use checksums
f7b91c7e12e3e9fc5483956f9ab2f4fdc7e2b5ae15bf52984724bc89799fab55
BLAKE2b-256 checksum
How to use checksums
e214dbe5b7cc80082413f96daf2f5aff04ee28ce4453ddd164a2557ef16f0f1a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.24 {"installer":{"name":"uv","version":"0.12.24","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.50.0-py3-none-win_amd64.whl

Download URL ast_grep_cli-0.50.0-py3-none-win_amd64.whl
Size 16.1 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
03af756f96c05f5e4de99a2a93f9d73937085ecd60edf09c4596ffeb1637c58d
BLAKE2b-256 checksum
How to use checksums
97445f5597524ef13e00017eae91dd42ae3bee7e43fc00930d60d0f33a3914a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.24 {"installer":{"name":"uv","version":"0.12.24","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.50.0-py3-none-win32.whl

Download URL ast_grep_cli-0.50.0-py3-none-win32.whl
Size 15.0 MB
Tags Python 3 Windows x86-32
SHA-256 checksum
How to use checksums
693a748cc7707a9080d789a6a9728722d421c6d870387b5c372133c3114c22b0
BLAKE2b-256 checksum
How to use checksums
69e202549e53f7ec7ba2718108c1a5414bd778ca00b3a47c4290ddbe8c8a5f8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.24 {"installer":{"name":"uv","version":"0.12.24","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.50.0-py3-none-manylinux_2_28_x86_64.whl

Download URL ast_grep_cli-0.50.0-py3-none-manylinux_2_28_x86_64.whl
Size 8.1 MB
Tags Linux glibc 2.28+ x86-64 Python 3
SHA-256 checksum
How to use checksums
d7f05aaadf39e8449419710b8b658af7bc0da1fcbcd9b30687013b89e36bd835
BLAKE2b-256 checksum
How to use checksums
c13fee72263d6ee1eb28d55b0a0ce07bf5a8ce5e2b20d377e182809b071fbe6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.24 {"installer":{"name":"uv","version":"0.12.24","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.50.0-py3-none-manylinux_2_28_aarch64.whl

Download URL ast_grep_cli-0.50.0-py3-none-manylinux_2_28_aarch64.whl
Size 7.8 MB
Tags Linux glibc 2.28+ ARM64 Python 3
SHA-256 checksum
How to use checksums
6f5bad696264c61a4c8e29f8227299928a53f69290e3fb427255b15ac0f2a34e
BLAKE2b-256 checksum
How to use checksums
f1c09f43df1f2002f0e372179352b31a717082410d1ebc66da9ed4a1cd11dfdb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.24 {"installer":{"name":"uv","version":"0.12.24","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.50.0-py3-none-macosx_10_12_x86_64.whl

Download URL ast_grep_cli-0.50.0-py3-none-macosx_10_12_x86_64.whl
Size 8.0 MB
Tags Python 3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
dc0cbc067373b46c5ec535c69cad0bed049c973a0229df054d95fd579a6f8cd0
BLAKE2b-256 checksum
How to use checksums
21055095de1abb430e12b4a047d42235823c05fefeb996fef109baf16df083eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.24 {"installer":{"name":"uv","version":"0.12.24","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.50.0-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl

Download URL ast_grep_cli-0.50.0-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Size 15.9 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
cb135961f377d8e7a892e51a51cf5754ef4e09f93e0975b17cfdcf4343155451
BLAKE2b-256 checksum
How to use checksums
cffa074d5724035c542afadf01fe8054d165004492888da1b298acff3970885a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.24 {"installer":{"name":"uv","version":"0.12.24","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.51.0

8 release files

This release

0.50.0 This release

8 release files

0.45.3

8 release files

0.45.2

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