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

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.3.tar.gz (232.4 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.3-py3-none-win_arm64.whl (7.5 MB view details)

Uploaded Python 3Windows ARM64

ast_grep_cli-0.42.3-py3-none-win_amd64.whl (7.8 MB view details)

Uploaded Python 3Windows x86-64

ast_grep_cli-0.42.3-py3-none-win32.whl (7.4 MB view details)

Uploaded Python 3Windows x86

ast_grep_cli-0.42.3-py3-none-manylinux_2_28_x86_64.whl (7.8 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ x86-64

ast_grep_cli-0.42.3-py3-none-manylinux_2_28_aarch64.whl (7.5 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ ARM64

ast_grep_cli-0.42.3-py3-none-macosx_10_12_x86_64.whl (7.6 MB view details)

Uploaded Python 3macOS 10.12+ x86-64

ast_grep_cli-0.42.3-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (15.2 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.3.tar.gz.

File metadata

  • Download URL: ast_grep_cli-0.42.3.tar.gz
  • Upload date:
  • Size: 232.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","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.3.tar.gz
Algorithm Hash digest
SHA256 81c08507cc3e9b18afdf417b704a8bd3e0bf0d956b2c5e62332e747e554adc70
MD5 ab0a24b8ae88708dd5547f01d3688cec
BLAKE2b-256 3ff9cf0347fe62c76c2af727a7c8e94a146891248dbc58973dc603fd38ef19e1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.42.3-py3-none-win_arm64.whl
  • Upload date:
  • Size: 7.5 MB
  • Tags: Python 3, Windows ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","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.3-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 4bccbfa124eae9d7ee437505fa4d26b5de6348a8c4e68b39ecb016af5c7871e2
MD5 0499cb4ec1c68df269c1c968835b30ae
BLAKE2b-256 a8f367679a13c82068bb31617d1c79c9e9e9205fbec24b5a3d3bae256b307da0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.42.3-py3-none-win_amd64.whl
  • Upload date:
  • Size: 7.8 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","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.3-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 e3dd282b60c547e916b0e77a33a01131b2a52a488cf44960547c64cffe09a444
MD5 9b63b69b3bdcc76fcc543a679a1ed91a
BLAKE2b-256 87366fe4212243a7ebb3d997203a213e1ad60ca15426121f90fb6e23b1bf31c3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.42.3-py3-none-win32.whl
  • Upload date:
  • Size: 7.4 MB
  • Tags: Python 3, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","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.3-py3-none-win32.whl
Algorithm Hash digest
SHA256 650ea2b65b7059603a5d0872d85ce976cdc8173bfb7994648a9dab1b5caee568
MD5 ba05f1b3552862c573d57229601fd927
BLAKE2b-256 7e02f2fa0760eefe5765365ad65e46b8e561cac4b40607c62c03854b2fe73000

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.42.3-py3-none-manylinux_2_28_x86_64.whl
  • Upload date:
  • Size: 7.8 MB
  • Tags: Python 3, manylinux: glibc 2.28+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","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.3-py3-none-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0a23ff5bcbeac165a44b1bb3a9c14270cdfea4916fe62364b4c3cf8a64910b1c
MD5 e8ef9d54fbf494538694a51f70366465
BLAKE2b-256 8da5a3e40d49b32a6ec545efbe2be1d0ebabe5fa3abe2dcc8588577fd2fb2741

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.42.3-py3-none-manylinux_2_28_aarch64.whl
  • Upload date:
  • Size: 7.5 MB
  • Tags: Python 3, manylinux: glibc 2.28+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","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.3-py3-none-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 52394e24b3caf00266e41b894561fdd250668c56f4879f11102b0c0dc9d559dc
MD5 001b56eae093ece338895812f34dc13e
BLAKE2b-256 9b3d80d1333089033a49514017536ee90a5b0c13afb93b6f12520740c4b9d31f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.42.3-py3-none-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 7.6 MB
  • Tags: Python 3, macOS 10.12+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","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.3-py3-none-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ae81004c465b5a53585e50acc6a700fee16c51a5b8cd38938854def2bc03bd2c
MD5 a87712a3241baf1dba8cd9927e14f16d
BLAKE2b-256 bb615dffed31617d49f81010869fe370524e22fdc9270c9012c40e26fbb8043e

See more details on using hashes here.

File details

Details for the file ast_grep_cli-0.42.3-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.3-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
  • Upload date:
  • Size: 15.2 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.15 {"installer":{"name":"uv","version":"0.11.15","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.3-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 ea4c65222d2794c0d1d9165d5d77464e94e51d91209968040fbf2eb3e6a4ac50
MD5 70e0149369c381fe5a534f84bc7e6daa
BLAKE2b-256 3f763fa41358a8be5c1ce3ec90eb5830ad48f26f1fa3582c2a3933078a42d95a

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