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.44.0.tar.gz (274.8 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.44.0-py3-none-win_arm64.whl (7.9 MB view details)

Uploaded Python 3Windows ARM64

ast_grep_cli-0.44.0-py3-none-win_amd64.whl (8.2 MB view details)

Uploaded Python 3Windows x86-64

ast_grep_cli-0.44.0-py3-none-win32.whl (7.7 MB view details)

Uploaded Python 3Windows x86

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

Uploaded Python 3manylinux: glibc 2.28+ ARM64

ast_grep_cli-0.44.0-py3-none-macosx_10_12_x86_64.whl (8.0 MB view details)

Uploaded Python 3macOS 10.12+ x86-64

ast_grep_cli-0.44.0-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (16.1 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.44.0.tar.gz.

File metadata

  • Download URL: ast_grep_cli-0.44.0.tar.gz
  • Upload date:
  • Size: 274.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","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.44.0.tar.gz
Algorithm Hash digest
SHA256 d65a9ea18cee3368d3319b83aba234d49b0bb366cb22b35264cfe844091ab77f
MD5 4c17926fbe044b6d5d45ba39fc15f877
BLAKE2b-256 f7234437c1e2f91bba450a62c35334c67fb3beddc6900806dff837ef1a5599a1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.44.0-py3-none-win_arm64.whl
  • Upload date:
  • Size: 7.9 MB
  • Tags: Python 3, Windows ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","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.44.0-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 a6173eaf7ff7b8990094d6392bfa1566cc219dcf4cad502d661ee5438d55fe10
MD5 1408bd0a67a5c578a86bb7d434abb2d2
BLAKE2b-256 d9ebf62d70cccbaed6ff820e4f149ba6cc82c3907181f16624d8b4bfa53bcc9c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.44.0-py3-none-win_amd64.whl
  • Upload date:
  • Size: 8.2 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","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.44.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 e12ab5a6f6276f729385c05c4349f55eccb86fa6fc8469d8cf2155cd85d99f56
MD5 0aff5ace178819fe970428e4de1d01b7
BLAKE2b-256 2f8551e98cd5d510a27208275a7c0fb36e0e0d1da0b0deeb9992ddd7f48387b6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.44.0-py3-none-win32.whl
  • Upload date:
  • Size: 7.7 MB
  • Tags: Python 3, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","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.44.0-py3-none-win32.whl
Algorithm Hash digest
SHA256 72be9a8eff8472e1c3f9941a59680a372d16981ee8dbafc4804a1bac30b83044
MD5 3bc78b025599f65b6d63d2a3176a9d19
BLAKE2b-256 d878d64369ad514e47d0895cd88feb4d66c5b8d4918632c24725360e5e8e5b55

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.44.0-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.11.23 {"installer":{"name":"uv","version":"0.11.23","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.44.0-py3-none-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f5841719ac9593112b6bb4eb50646a37e7a0a35b4b72c84c9f4cc34a259734fd
MD5 d9f146c7ef647dfda1e1320cc3fa8c79
BLAKE2b-256 56d4e7d09fd8c05b82c8bc9537dd2d389065a6b6c7ddb1f59928d75fa2c5c6f3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.44.0-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.11.23 {"installer":{"name":"uv","version":"0.11.23","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.44.0-py3-none-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 c30e01f432186b17d6d2f27c09bf47d3521a503a17b27161492e653c04cb2295
MD5 81a21c4a5cc9237ec1b9d6d3dbda7e98
BLAKE2b-256 8baa46f08c2631f1db4b23cde3164646691efe060bb078e309adbbeb91da58dc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.44.0-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.11.23 {"installer":{"name":"uv","version":"0.11.23","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.44.0-py3-none-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c42b41c06759c5cf2254343c89a97c63ac0480d0d8f021b5092d43958d27851d
MD5 c299df196767e30e995a819e6591f4f1
BLAKE2b-256 65c498fb6a547d6dd1155bf778855643cff265c78b7892d1fb9087b332d3e910

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ast_grep_cli-0.44.0-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
  • Upload date:
  • Size: 16.1 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.23 {"installer":{"name":"uv","version":"0.11.23","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.44.0-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 719c0716c3d14bf905e078ee6862216f2c3cfcdeb3202a12ade26deda99e1555
MD5 150e0bbe996ebd0ddca5b3c95489e43e
BLAKE2b-256 a616b2a3e1a189fc8ede5e7e955ce62065fa99361796ecea8cadbc1bf019527f

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