Skip to main content

Structural Search and Rewrite code at large scale using precise AST pattern.

Project description

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

Project details


Release history Release notifications | RSS feed

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.42.1.tar.gz (232.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.42.1-py3-none-win_arm64.whl (7.3 MB view details)

Uploaded Python 3Windows ARM64

ast_grep_cli-0.42.1-py3-none-win_amd64.whl (7.6 MB view details)

Uploaded Python 3Windows x86-64

ast_grep_cli-0.42.1-py3-none-win32.whl (7.2 MB view details)

Uploaded Python 3Windows x86

ast_grep_cli-0.42.1-py3-none-manylinux_2_28_x86_64.whl (7.6 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ x86-64

ast_grep_cli-0.42.1-py3-none-manylinux_2_28_aarch64.whl (7.3 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ ARM64

ast_grep_cli-0.42.1-py3-none-macosx_10_12_x86_64.whl (7.4 MB view details)

Uploaded Python 3macOS 10.12+ x86-64

ast_grep_cli-0.42.1-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (14.9 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.42.1.tar.gz.

File metadata

  • Download URL: ast_grep_cli-0.42.1.tar.gz
  • Upload date:
  • Size: 232.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","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.42.1.tar.gz
Algorithm Hash digest
SHA256 01b7e4dc99c24cc75e26e054f471a42b14b996e853314f3a4059a8af76cb6859
MD5 afed4386569ef22f632d9582922fa6f4
BLAKE2b-256 5e2359f07c0d92393a1920597b28b1535157062ef916c723ca3c4ae946b5884d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.42.1-py3-none-win_arm64.whl
  • Upload date:
  • Size: 7.3 MB
  • Tags: Python 3, Windows ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","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.42.1-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 ef95af6410f7cbee96d2ad4e05eff22257e858d1da785994b6f649e309e806d8
MD5 72fbff5427bb4a9a62919d8ae692ccaa
BLAKE2b-256 4d8af2d1559d3ac8dd22234200f932e47fa2e5cc458e0e256c141c00970420eb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.42.1-py3-none-win_amd64.whl
  • Upload date:
  • Size: 7.6 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","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.42.1-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 b8ce8f32cd0c9d4afa21445f4ff523ac4987812543352695e0070ee3abdce7fa
MD5 186e4b3369d5c47f7d614a43907e2bdb
BLAKE2b-256 737689cbe5338c385370ea91d0eb5f7e02020a59db9dfa2e1118efba73aab83c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.42.1-py3-none-win32.whl
  • Upload date:
  • Size: 7.2 MB
  • Tags: Python 3, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","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.42.1-py3-none-win32.whl
Algorithm Hash digest
SHA256 10e76a36a1370e3b43ed1e9531f0d49775de47ce36c07298e70979ace08b5f4b
MD5 5f99181c02dd009f08e92da80205df83
BLAKE2b-256 ffed781473f8ffa5f2a04d3e8d785a8e600b6e3e9e60dae055910900d9b9b252

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.42.1-py3-none-manylinux_2_28_x86_64.whl
  • Upload date:
  • Size: 7.6 MB
  • Tags: Python 3, manylinux: glibc 2.28+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","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.42.1-py3-none-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 728ad2cd4b328d5b179e0ff8344115a0744cd37a5f3836fc5380e609ea188135
MD5 ac010a30681f3a8e9d6e514c26bf9ba2
BLAKE2b-256 68e05d5f7d396688f74491028c61e542aa9308cf1faccfb8df50ea5f674ffed4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.42.1-py3-none-manylinux_2_28_aarch64.whl
  • Upload date:
  • Size: 7.3 MB
  • Tags: Python 3, manylinux: glibc 2.28+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","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.42.1-py3-none-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 5472942bfe59a3603a28b48a6c83c71b70a6074b675cee864fec87fbcad61a26
MD5 3f4b4160f21867b1fde399de52042060
BLAKE2b-256 983b52d70dabc57c545aee07fdffee6a9a48683c0c33d24dea2416debc429910

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.42.1-py3-none-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 7.4 MB
  • Tags: Python 3, macOS 10.12+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","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.42.1-py3-none-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 8e735def2926d7e1a6a2b3cd0cfbe6a48e334dc2e19167285e08aeac359a401f
MD5 a9e36ba74a8fd49f9173816d2bee9d42
BLAKE2b-256 02b0523fcb7380ecf6629eca1c3d4e58d37d520a213885273e378295ca10046d

See more details on using hashes here.

File details

Details for the file ast_grep_cli-0.42.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.42.1-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
  • Upload date:
  • Size: 14.9 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.3 {"installer":{"name":"uv","version":"0.11.3","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.42.1-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 7704d9d6a7bfdaa0729eb31536878b63ad5161c4669b6ee4d783970ac4de1829
MD5 9ba685d5db65af71b4a3c555379c8a96
BLAKE2b-256 22d12e0483598fe1dcb3932682d3c7dc2428d793af81e328b715fbd762235e56

See more details on using hashes here.

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