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.

Release files for ast-grep-cli 0.45.3

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.3
File Size Uploaded
ast_grep_cli-0.45.3.tar.gz 283.7 kB Details

Built distributions (wheels)

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

Release files / ast_grep_cli-0.45.3.tar.gz

Download URL ast_grep_cli-0.45.3.tar.gz
Size 283.7 kB
Tags Source
SHA-256 checksum
How to use checksums
d8b527b71655eede16637311bc3ca49624b8198c48bffeb5dcf06a78332bf917
BLAKE2b-256 checksum
How to use checksums
804635e89cbf27fe8a866796f9cb6ae711b88e0ff67e14e2580ced649b3d6a19
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","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.3-py3-none-win_arm64.whl

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

Download URL ast_grep_cli-0.45.3-py3-none-win_amd64.whl
Size 15.9 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
13dfb43b2028bb585f41b2f399e8b30bf0310e9bbeff2369d60a2e36accbb2e6
BLAKE2b-256 checksum
How to use checksums
b2cca8d5a4039470b58a4b0a5ce9826975796ff091539849984f04a0ac98582f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","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.3-py3-none-win32.whl

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

Download URL ast_grep_cli-0.45.3-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
2d151b89c6d45c2640338fe5d24b2ee0810224902d603cb271803bf7418054ea
BLAKE2b-256 checksum
How to use checksums
c2201445bff6593d492c31904fe309c157cb16bcb73cfd30b4a55718acef5cb1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","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.3-py3-none-manylinux_2_28_aarch64.whl

Download URL ast_grep_cli-0.45.3-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
ced023ae9fbc7c779570cd8bcc0fa7626d9561afd60369d3c335bfce2ca565b3
BLAKE2b-256 checksum
How to use checksums
a9d5584cc4cf46a751f35fc086e9dca9962c05de2b37f95056f3653dc4736e19
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","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.3-py3-none-macosx_10_12_x86_64.whl

Download URL ast_grep_cli-0.45.3-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
7afbe49870d8a34c0b224ac940da1fbf736617fcc81aa846884e377eddf8c1a0
BLAKE2b-256 checksum
How to use checksums
30f8d9b41ae197bf9a41034a03d742d9bb2ca05bf4f7da741c00242019b44d3e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","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.3-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl

Download URL ast_grep_cli-0.45.3-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
ebc1510a6775f65a8681345dd422590fb257fe48036a7c599a5ca9f54dea8801
BLAKE2b-256 checksum
How to use checksums
588dfded0d3cf2138b56454101e061ba8525bd47c9d022d924fd92320fad4d98
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","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

This release

0.45.3 This release

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