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.1.tar.gz (280.0 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.1-py3-none-win_arm64.whl (15.6 MB view details)

Uploaded Python 3Windows ARM64

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

Uploaded Python 3Windows x86-64

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

Uploaded Python 3Windows x86

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

Uploaded Python 3manylinux: glibc 2.28+ ARM64

ast_grep_cli-0.45.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.45.1-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.1.tar.gz.

File metadata

  • Download URL: ast_grep_cli-0.45.1.tar.gz
  • Upload date:
  • Size: 280.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.2 {"installer":{"name":"uv","version":"0.12.2","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.1.tar.gz
Algorithm Hash digest
SHA256 3b214f39554cb6fd51d0fd441eab9427c7dd8bfb12dbff5ef7618369c6a22628
MD5 3ed608da9ff70dd9a34007171123e4bb
BLAKE2b-256 bd31e202db9b63bb2851f48d29c01aa27b74c5b7b36bc6c158f80f6443a7dbfa

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.45.1-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.12.2 {"installer":{"name":"uv","version":"0.12.2","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.1-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 0a945ef6495c2ce2e09001e25b81ad14ccbe7afe446467c4d912ac06271c6c13
MD5 e08bb44834c01ab30401c903a3d9a712
BLAKE2b-256 b752d2644a91e78e876af197a05ff7bd499d2e0c615935f31756ab432cd157df

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.45.1-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.12.2 {"installer":{"name":"uv","version":"0.12.2","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.1-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 b3bbcdeb49db5a7942c4687c117c478be8e5eeb4bcfa79851d4154965f6c7402
MD5 abd5290d4b3c00d273ab3d23eb26e5b3
BLAKE2b-256 f9965544e348c00381c7526462332dcb56ed1129de3402b0610a100fc683db84

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.45.1-py3-none-win32.whl
  • Upload date:
  • Size: 15.2 MB
  • Tags: Python 3, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.2 {"installer":{"name":"uv","version":"0.12.2","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.1-py3-none-win32.whl
Algorithm Hash digest
SHA256 bfd8aceeb6d6e8623a18a0804d22f9cdc67ad84d7dd1f03c3e8b6a33990189e3
MD5 e097649698b0ffcee4ae666838807814
BLAKE2b-256 9df10d04cd5e0a5bdd1c1f1c8f78ec1efab30ad1d585bda207657d125e0613ef

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.45.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.12.2 {"installer":{"name":"uv","version":"0.12.2","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.1-py3-none-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 497810f387e9ab0624f72dd300557fb25a2a49b0429c71472e5d08180f16bdbc
MD5 6441aa43bd4de6d079e6673b1e36895b
BLAKE2b-256 f43e193f347a7add7ba413a0f4169cc8185bd58ba0477e6947a683f4887c4872

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.45.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.12.2 {"installer":{"name":"uv","version":"0.12.2","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.1-py3-none-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 80caab89daefa0ddaf69f59f7838e6ff4480fd35daf6ea273fa3055506e992df
MD5 f4837b743698193e3f9c59aacc10aa1f
BLAKE2b-256 9ef82bb3e74a50e75b41a34d8f6f8e6d42867b088147a15fb7e73f5cb94d7f7f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.45.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.12.2 {"installer":{"name":"uv","version":"0.12.2","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.1-py3-none-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 befa0add772663a012d42e22bb28483ecb8558d66634fa364efe09a1c7187542
MD5 14a41d3813137f2dfbbbfc0d21bc10a7
BLAKE2b-256 86524cd380e4fc56f8fc5e9d6526b6c353c7802d96b78624ecb49dca6cfe6a86

See more details on using hashes here.

File details

Details for the file ast_grep_cli-0.45.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.45.1-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.12.2 {"installer":{"name":"uv","version":"0.12.2","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.1-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 3536a040aed9d95b29af27d2eea0c831a48ee151afa8de2c6a842b9fc75e8c87
MD5 8abbe1bad826dc15b0ac8ab581f48ddb
BLAKE2b-256 632f5db21c7c3c61f758f0cde8ebbc60e5876d026857ca5a1ca41acb9607b18b

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