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.40.5.tar.gz (215.7 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.40.5-py3-none-win_arm64.whl (7.1 MB view details)

Uploaded Python 3Windows ARM64

ast_grep_cli-0.40.5-py3-none-win_amd64.whl (7.4 MB view details)

Uploaded Python 3Windows x86-64

ast_grep_cli-0.40.5-py3-none-win32.whl (6.9 MB view details)

Uploaded Python 3Windows x86

ast_grep_cli-0.40.5-py3-none-manylinux_2_28_x86_64.whl (7.4 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ x86-64

ast_grep_cli-0.40.5-py3-none-manylinux_2_28_aarch64.whl (7.1 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ ARM64

ast_grep_cli-0.40.5-py3-none-macosx_10_12_x86_64.whl (7.2 MB view details)

Uploaded Python 3macOS 10.12+ x86-64

ast_grep_cli-0.40.5-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (14.4 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.40.5.tar.gz.

File metadata

  • Download URL: ast_grep_cli-0.40.5.tar.gz
  • Upload date:
  • Size: 215.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","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.40.5.tar.gz
Algorithm Hash digest
SHA256 30226f9461232cece568d9072b297cee94baeac7bbaefe31080e502bf64f6db8
MD5 57619fc3a958ee03fcc6599928847cc5
BLAKE2b-256 631a136b60ea7c2a5d813f1cb2e11f41c68cb8c1a0e594b48d16400b3f4f96a2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.40.5-py3-none-win_arm64.whl
  • Upload date:
  • Size: 7.1 MB
  • Tags: Python 3, Windows ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","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.40.5-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 ba42cdf8148b58fb763ad20a3bb5e84d487141327f41fed5276d04e637194be6
MD5 5ef9ee56621a3ded11110841dc054869
BLAKE2b-256 bab91d02935e30692d044205e4dca30367c4db16146836bbd25de5705992c3a1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.40.5-py3-none-win_amd64.whl
  • Upload date:
  • Size: 7.4 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","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.40.5-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 a2253c898384728393bf587a530b27f44ce8077e6a0bcd20fc2c07f89681620d
MD5 1ad3abe04b1f468af86ca8c3b587d3b2
BLAKE2b-256 9c8b50d74a777b3617b4a2e17a471433c983822506bb7d077e18ee6d6dd263bb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.40.5-py3-none-win32.whl
  • Upload date:
  • Size: 6.9 MB
  • Tags: Python 3, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","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.40.5-py3-none-win32.whl
Algorithm Hash digest
SHA256 cfc4c7c16c5f68d6f0d79bc128d6c7185c17887d75b94682458a890c3d8319e3
MD5 d09778e5ae4e4a068c55c56b1aa7c82a
BLAKE2b-256 b3f3e411fd81d7ab7811a29d0f89ee5f81980a942fdeb298c3d67f373d1d7d55

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.40.5-py3-none-manylinux_2_28_x86_64.whl
  • Upload date:
  • Size: 7.4 MB
  • Tags: Python 3, manylinux: glibc 2.28+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","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.40.5-py3-none-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 881ba2a43036f21464764978590712d5a1280f6f43880a6ff6cfa4538338e559
MD5 5c75e470362882f4a441b2c9dd14abf2
BLAKE2b-256 6139004493cbc63fe5b3d7feb178ed99b3e285237cfb4bfcf8f1605cc3fa702d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.40.5-py3-none-manylinux_2_28_aarch64.whl
  • Upload date:
  • Size: 7.1 MB
  • Tags: Python 3, manylinux: glibc 2.28+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","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.40.5-py3-none-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 6ec8c0899cc0545353b311101227dca74014230160c01c1197b74b6c83c77833
MD5 31dad99014dbf159d61ce2dced15dbc8
BLAKE2b-256 46766cf29923359d583150477d4929a88389283ec73f7ea592e66d247bd6edb9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.40.5-py3-none-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 7.2 MB
  • Tags: Python 3, macOS 10.12+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","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.40.5-py3-none-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 b9fd71d545708f2101212ce6c9e69abecd645702646665f9718d499179ff9f9e
MD5 4593bd2440096021d95bffba0ff0be60
BLAKE2b-256 077b93f13c7992105f00bf913cd45111b28366d8369d898916d2f47909eb1cf1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.40.5-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
  • Upload date:
  • Size: 14.4 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.9.24 {"installer":{"name":"uv","version":"0.9.24","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.40.5-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 3c1d7a8efef7f3a86aa7451cfcd991db02f596ad90ca72044a5b6eb7209d3639
MD5 1ab53eed6e5189a835c9f4eb71229d0b
BLAKE2b-256 5bde0114c873ade9634a96bdd73d360fa8e7066ecacf46e73b7eabc363a980c7

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