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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ast_grep_cli-0.45.0.tar.gz (279.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

ast_grep_cli-0.45.0-py3-none-win_arm64.whl (15.6 MB view details)

Uploaded Python 3Windows ARM64

ast_grep_cli-0.45.0-py3-none-win_amd64.whl (16.2 MB view details)

Uploaded Python 3Windows x86-64

ast_grep_cli-0.45.0-py3-none-win32.whl (15.2 MB view details)

Uploaded Python 3Windows x86

ast_grep_cli-0.45.0-py3-none-manylinux_2_28_x86_64.whl (8.2 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ x86-64

ast_grep_cli-0.45.0-py3-none-manylinux_2_28_aarch64.whl (7.9 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ ARM64

ast_grep_cli-0.45.0-py3-none-macosx_10_12_x86_64.whl (8.0 MB view details)

Uploaded Python 3macOS 10.12+ x86-64

ast_grep_cli-0.45.0-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (16.0 MB view details)

Uploaded Python 3macOS 10.12+ universal2 (ARM64, x86-64)macOS 10.12+ x86-64macOS 11.0+ ARM64

File details

Details for the file ast_grep_cli-0.45.0.tar.gz.

File metadata

  • Download URL: ast_grep_cli-0.45.0.tar.gz
  • Upload date:
  • Size: 279.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.31 {"installer":{"name":"uv","version":"0.11.31","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}

File hashes

Hashes for ast_grep_cli-0.45.0.tar.gz
Algorithm Hash digest
SHA256 d07bfe87c09697e5011b2ce065f61cbc165ca5b0c0a567000f4ce9e62f2fde32
MD5 887e5312466c09075a81387076535d35
BLAKE2b-256 e2c3a4f4dba7f601d096e69539a3e569346ab05a5d0f3177fa971526b410a012

See more details on using hashes here.

File details

Details for the file ast_grep_cli-0.45.0-py3-none-win_arm64.whl.

File metadata

  • Download URL: ast_grep_cli-0.45.0-py3-none-win_arm64.whl
  • Upload date:
  • Size: 15.6 MB
  • Tags: Python 3, Windows ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.31 {"installer":{"name":"uv","version":"0.11.31","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}

File hashes

Hashes for ast_grep_cli-0.45.0-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 5c3aea8e2a06938350f342f0c5be4399a4fe05e12ba80a5c037313e28bc18b00
MD5 a126681ae504f77004a98b897b587451
BLAKE2b-256 6428dde9d9c2435faf43b596cc22a6a39ae9a0f952560ab6917abb201d5d15bb

See more details on using hashes here.

File details

Details for the file ast_grep_cli-0.45.0-py3-none-win_amd64.whl.

File metadata

  • Download URL: ast_grep_cli-0.45.0-py3-none-win_amd64.whl
  • Upload date:
  • Size: 16.2 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.31 {"installer":{"name":"uv","version":"0.11.31","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}

File hashes

Hashes for ast_grep_cli-0.45.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 63026862481b2f480792498c8c1bad13f09fccd8fa34ae9913c5743eb3165150
MD5 798888fe4c7fbc4966741f3a6e77006a
BLAKE2b-256 45f703d920cb9c1e850b55b60732ae0b9d9080218b4a5620d1499a77e613446a

See more details on using hashes here.

File details

Details for the file ast_grep_cli-0.45.0-py3-none-win32.whl.

File metadata

  • Download URL: ast_grep_cli-0.45.0-py3-none-win32.whl
  • Upload date:
  • Size: 15.2 MB
  • Tags: Python 3, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.31 {"installer":{"name":"uv","version":"0.11.31","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}

File hashes

Hashes for ast_grep_cli-0.45.0-py3-none-win32.whl
Algorithm Hash digest
SHA256 609e77bfb4a61c047d23c081b5bf862b1e05c9d06e7d26b8e4df4879d508c973
MD5 0979b32a5f10630d6b0b5476b1998858
BLAKE2b-256 7f579073558b1cdf5c735d59695b364f2c8fa03235ee089929a69cad123e7e3c

See more details on using hashes here.

File details

Details for the file ast_grep_cli-0.45.0-py3-none-manylinux_2_28_x86_64.whl.

File metadata

  • Download URL: ast_grep_cli-0.45.0-py3-none-manylinux_2_28_x86_64.whl
  • Upload date:
  • Size: 8.2 MB
  • Tags: Python 3, manylinux: glibc 2.28+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.31 {"installer":{"name":"uv","version":"0.11.31","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}

File hashes

Hashes for ast_grep_cli-0.45.0-py3-none-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a1e9e83e15e7f2290e51770b0629618b68b3fb628396f86648d99015782b7374
MD5 3f0c5fdf19d11d40c583edf2499b5c28
BLAKE2b-256 58cac6140ca63d6a117d83296d46c3052facf893bde899d8f734ee08b9e8864e

See more details on using hashes here.

File details

Details for the file ast_grep_cli-0.45.0-py3-none-manylinux_2_28_aarch64.whl.

File metadata

  • Download URL: ast_grep_cli-0.45.0-py3-none-manylinux_2_28_aarch64.whl
  • Upload date:
  • Size: 7.9 MB
  • Tags: Python 3, manylinux: glibc 2.28+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.31 {"installer":{"name":"uv","version":"0.11.31","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}

File hashes

Hashes for ast_grep_cli-0.45.0-py3-none-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 99c908930e181bcdf4d0e9b8bb4b3e52ee6403f59ff6fc16ba8054bcff90dda2
MD5 d3cca2c61651aad1f1356c8daac78f27
BLAKE2b-256 7fdae6db92897e2f2ffca80604d422f2cd837f7375be5adfb90add30fa94244d

See more details on using hashes here.

File details

Details for the file ast_grep_cli-0.45.0-py3-none-macosx_10_12_x86_64.whl.

File metadata

  • Download URL: ast_grep_cli-0.45.0-py3-none-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 8.0 MB
  • Tags: Python 3, macOS 10.12+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.31 {"installer":{"name":"uv","version":"0.11.31","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}

File hashes

Hashes for ast_grep_cli-0.45.0-py3-none-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a39b1674c8686bf21e252da59d82ec8d5fea543f5c934734f596ddf1c0552961
MD5 d30e47b820917b63cd0311d45269010e
BLAKE2b-256 16a81273fd3a4f7a30d45fbcb0ff051be24e7ee31bfacbd0bb48ea7a8a9b46ef

See more details on using hashes here.

File details

Details for the file ast_grep_cli-0.45.0-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

  • Download URL: ast_grep_cli-0.45.0-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
  • Upload date:
  • Size: 16.0 MB
  • Tags: Python 3, macOS 10.12+ universal2 (ARM64, x86-64), macOS 10.12+ x86-64, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.31 {"installer":{"name":"uv","version":"0.11.31","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}

File hashes

Hashes for ast_grep_cli-0.45.0-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 2fa9b424b0cf82770154cfdfd9aafe1eacd4598073e63c8482962df008c99b41
MD5 794373b473128f18abe4decb157c4491
BLAKE2b-256 9c20abe4ec0cd4f7e1b32c495fc0e4a37243c26c09a1d22959d20311c6894644

See more details on using hashes here.

Release history Release notifications | RSS feed

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page