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.2.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.2-py3-none-win_arm64.whl (7.5 MB view details)

Uploaded Python 3Windows ARM64

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

Uploaded Python 3Windows x86-64

ast_grep_cli-0.42.2-py3-none-win32.whl (7.3 MB view details)

Uploaded Python 3Windows x86

ast_grep_cli-0.42.2-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.2-py3-none-manylinux_2_28_aarch64.whl (7.5 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ ARM64

ast_grep_cli-0.42.2-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.2-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.2.tar.gz.

File metadata

  • Download URL: ast_grep_cli-0.42.2.tar.gz
  • Upload date:
  • Size: 232.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.12 {"installer":{"name":"uv","version":"0.11.12","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.2.tar.gz
Algorithm Hash digest
SHA256 ce778a10286c23b403b0611b63d97e03f9f98d238a56198e821711229226823a
MD5 8ff018e2b1b6599c44116ab7020d5fa4
BLAKE2b-256 43c97787f03166a7b929f80cab49e3c1bb2a8937a757a019fb5331a8dee038d2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.42.2-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.12 {"installer":{"name":"uv","version":"0.11.12","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.2-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 04af685e244a974654a1e6d1c73a65dd624eb46495351e4eeb06f06bb099ac9a
MD5 628b1c53fcb4f517a1688699335e2f81
BLAKE2b-256 af777f5ab54535b7ca4f92a20b7f6628d25d9248dce28fd78040935504bb77bd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.42.2-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.12 {"installer":{"name":"uv","version":"0.11.12","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.2-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 1192fb01359f5685310cce5adff62f31cee9e98315c0577821c9b49ebb830c9e
MD5 b5f6adedd93ab593978be6293caf01c4
BLAKE2b-256 d0b24c83ca5eb017134ac7958d0c55bd85cfd16b9b4b5f5c16e91327d644a710

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.42.2-py3-none-win32.whl
  • Upload date:
  • Size: 7.3 MB
  • Tags: Python 3, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.12 {"installer":{"name":"uv","version":"0.11.12","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.2-py3-none-win32.whl
Algorithm Hash digest
SHA256 c31ff738565039bd84b498e4cc1a4f887bc34e214e3459e32b920e9251e9587a
MD5 199f1cf432b64bccdaeee9ca80f1a3b6
BLAKE2b-256 4bf182b5568035bba69b9244e4f37732ca46248f7be43812b939b1cb9c7f6545

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.42.2-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.12 {"installer":{"name":"uv","version":"0.11.12","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.2-py3-none-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a2f4728d6cc75a67265afa0801dade15649eb9a5188333d1b0c51b4833f975d3
MD5 81af4bea121e3155321ad853673f2a6b
BLAKE2b-256 457b73a0b1537f90cd56a3dc563dcf6c8857d8e37e472b27c3d1dfbc57545648

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.42.2-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.12 {"installer":{"name":"uv","version":"0.11.12","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.2-py3-none-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 26adbb32aed0093a37128cc7fa0230b13dc5f1b806e3b526479f55bef65946d2
MD5 eae12c590c04d5fa3a94265902333223
BLAKE2b-256 42db04d26b1d89cc1a59cded67a564a1f6afa9de56fa3528ced1ca0cb5e359df

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.42.2-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.12 {"installer":{"name":"uv","version":"0.11.12","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.2-py3-none-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 683dc491a072164a031b8e0800561229eef8ae0c7c3e90cb5bc9f4c34ffbd494
MD5 fdf4291a0d2e45960188801e3ca360e2
BLAKE2b-256 2a90cd7e7a2bf2661b78f10fccf23efd6181d859b2a7708936a0b0e7af4c5a9a

See more details on using hashes here.

File details

Details for the file ast_grep_cli-0.42.2-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.2-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.12 {"installer":{"name":"uv","version":"0.11.12","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.2-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 f158078525a770c5ec8298c3f0405bddd6d8a47ff0e22e893648a3422630595b
MD5 ba8683abbd53d533f2cd8f39a7f04425
BLAKE2b-256 6cdfe82bb88929c449c7b94d6d7012c8cff782d7649ad2aa624a9cc8363a0aab

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