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.44.1.tar.gz (278.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.44.1-py3-none-win_arm64.whl (7.9 MB view details)

Uploaded Python 3Windows ARM64

ast_grep_cli-0.44.1-py3-none-win_amd64.whl (8.2 MB view details)

Uploaded Python 3Windows x86-64

ast_grep_cli-0.44.1-py3-none-win32.whl (7.7 MB view details)

Uploaded Python 3Windows x86

ast_grep_cli-0.44.1-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.44.1-py3-none-manylinux_2_28_aarch64.whl (7.9 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ ARM64

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

Uploaded Python 3macOS 10.12+ x86-64

ast_grep_cli-0.44.1-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (16.1 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.44.1.tar.gz.

File metadata

  • Download URL: ast_grep_cli-0.44.1.tar.gz
  • Upload date:
  • Size: 278.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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.44.1.tar.gz
Algorithm Hash digest
SHA256 391e9d60f118d0a15c905b2de1287a162d1891c2f05d94919a37784230fab559
MD5 e8446af4135baba3cd791dc38fa1325f
BLAKE2b-256 cfb6700b2a79eed7be029d3885ae2dda638604ee34970fc14ec154f5084915b7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.44.1-py3-none-win_arm64.whl
  • Upload date:
  • Size: 7.9 MB
  • Tags: Python 3, Windows ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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.44.1-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 ce18f7f13aee07e9ec169122458ce241910c876ef5536f9115d6fec8c42a4d86
MD5 668515f0a5f6fedd129f066fb92a98a8
BLAKE2b-256 6ba7e078c3c4a200984ac91590fc42f3c51418d02d168dbdb5a96148b11309c7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.44.1-py3-none-win_amd64.whl
  • Upload date:
  • Size: 8.2 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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.44.1-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 43dd6a281bf55269ad101460f291e7447f4dbfced794f92ced896610e5bfa589
MD5 6f6fafac14855a806d33ad77b4c73d86
BLAKE2b-256 23f8cbf9f39ec10be756bafc5f9def09083b6d4c3833f87a2d6818ffd2a53c48

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.44.1-py3-none-win32.whl
  • Upload date:
  • Size: 7.7 MB
  • Tags: Python 3, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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.44.1-py3-none-win32.whl
Algorithm Hash digest
SHA256 47356ab8eef7fb269e63a944427765d642bd93af20bdb966587fd706da0a61bb
MD5 5d402af8ef5a6512d810aef6e8bd023d
BLAKE2b-256 bd7a796166f5ed48e2f665a85d62e260a209beef8a597ea4dd36129e5ef1452b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.44.1-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.26 {"installer":{"name":"uv","version":"0.11.26","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.44.1-py3-none-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ad11a5eb8d7bd557754b21a360a3be1bc1b19e7f4bed95b444ffc25b9451844f
MD5 e062d28169166464a3c34497df9b9f47
BLAKE2b-256 7f841ff86aded236180eedc3c3a3ec4edb2fed13c1928104432501e21a4f2efb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.44.1-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.26 {"installer":{"name":"uv","version":"0.11.26","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.44.1-py3-none-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 54c21864c497275ed99c5fd9cf605aa9758cc3f21fc769fcde8c5ebc7c25d87d
MD5 e382097c7009fb89d60f18c1eab6280a
BLAKE2b-256 a9306f1e5a379854a1be22983f964572a20d2229d695a63c5d6fd03570d7eab2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.44.1-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.26 {"installer":{"name":"uv","version":"0.11.26","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.44.1-py3-none-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ce408938e3e4ba8570102a5a54a674dc18b8fa21bc9560dc859a66f7c96a4291
MD5 dd72a8568ff64df70aea5fe2ccd0ada1
BLAKE2b-256 532e185dff98167af423436556267af472674d400154a238141b4ba2f8f42e35

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.44.1-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
  • Upload date:
  • Size: 16.1 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.26 {"installer":{"name":"uv","version":"0.11.26","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.44.1-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 34fd5494fed41e5461e46a7932cd9a3e8c42136dd6b040ccc5ceca00c1c2e553
MD5 f4322b57cebec124eff7d192c60e2334
BLAKE2b-256 675094f0654fe86cff7ac2f52b94a417c9918632f324cada4a7b02d07906934e

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