Structural Search and Rewrite code at large scale using precise AST pattern.
Project description
ast-grep(sg)
ast-grep(sg) is a CLI tool for code structural search, lint, and rewriting.
Introduction
ast-grep is an AST-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 wildcard, e.g. $MATCH
, to match any single AST node. Think of it as REGEX dot .
, except it is not textual.
Try the online playground for a taste!
Demo
See more screenshots on the website.
Installation
You can install it from npm, pip, cargo, homebrew, scoop or MacPorts!
npm install --global @ast-grep/cli
pip install ast-grep-cli
cargo install ast-grep --locked
# install via homebrew, thank @henryhchchc
brew install ast-grep
# install via scoop, thank @brian6932
scoop install main/ast-grep
# install via MacPorts
sudo port install 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.
sg --pattern 'var code = $PATTERN' --rewrite 'let code = new $PATTERN' --lang ts
Example
sg -p '$A && $A()' -l ts -r '$A?.()'
sg -p 'new Zodios($URL, $CONF as const,)' -l ts -r 'new Zodios($URL, $CONF)' -i
Sponsor
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
Built Distributions
File details
Details for the file ast_grep_cli-0.30.0.tar.gz
.
File metadata
- Download URL: ast_grep_cli-0.30.0.tar.gz
- Upload date:
- Size: 182.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fd653ea6ad15db2dc81b042e156f3af7c2082fe7913592c1e659a357de0cf437 |
|
MD5 | ed4a618ebfc649e783ba690a0c199359 |
|
BLAKE2b-256 | e47e3482b7f1ad6fdfc91e75edc98a99e34d02d04fbad7bb6094748410a7ac24 |
Provenance
The following attestation bundles were made for ast_grep_cli-0.30.0.tar.gz
:
Publisher:
pypi.yml
on ast-grep/ast-grep
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
ast_grep_cli-0.30.0.tar.gz
- Subject digest:
fd653ea6ad15db2dc81b042e156f3af7c2082fe7913592c1e659a357de0cf437
- Sigstore transparency entry: 148054360
- Sigstore integration time:
- Predicate type:
File details
Details for the file ast_grep_cli-0.30.0-py3-none-win_arm64.whl
.
File metadata
- Download URL: ast_grep_cli-0.30.0-py3-none-win_arm64.whl
- Upload date:
- Size: 13.6 MB
- Tags: Python 3, Windows ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d83835222a52213b1bd8e0966fcafa29e3b6862b6693c684a37bbee0d758504 |
|
MD5 | 720f3ee023666a4c48cbf81b16f24c3b |
|
BLAKE2b-256 | a6961eed6f11125689ada393234611b146f840bca46d2dd52f06a9ad945920ee |
Provenance
The following attestation bundles were made for ast_grep_cli-0.30.0-py3-none-win_arm64.whl
:
Publisher:
pypi.yml
on ast-grep/ast-grep
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
ast_grep_cli-0.30.0-py3-none-win_arm64.whl
- Subject digest:
3d83835222a52213b1bd8e0966fcafa29e3b6862b6693c684a37bbee0d758504
- Sigstore transparency entry: 148054361
- Sigstore integration time:
- Predicate type:
File details
Details for the file ast_grep_cli-0.30.0-py3-none-win_amd64.whl
.
File metadata
- Download URL: ast_grep_cli-0.30.0-py3-none-win_amd64.whl
- Upload date:
- Size: 14.3 MB
- Tags: Python 3, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 922e6dc33816467d8400c021d9abf6a763bbddafc421b587968fde28ca8c7a26 |
|
MD5 | f5ca7d3b29ce16276fb753c65ed2c7d4 |
|
BLAKE2b-256 | 09849353f210e08aa47480a3f990c7cb58a3cab90b158bc6324ef9fb053c83c7 |
Provenance
The following attestation bundles were made for ast_grep_cli-0.30.0-py3-none-win_amd64.whl
:
Publisher:
pypi.yml
on ast-grep/ast-grep
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
ast_grep_cli-0.30.0-py3-none-win_amd64.whl
- Subject digest:
922e6dc33816467d8400c021d9abf6a763bbddafc421b587968fde28ca8c7a26
- Sigstore transparency entry: 148054369
- Sigstore integration time:
- Predicate type:
File details
Details for the file ast_grep_cli-0.30.0-py3-none-win32.whl
.
File metadata
- Download URL: ast_grep_cli-0.30.0-py3-none-win32.whl
- Upload date:
- Size: 13.7 MB
- Tags: Python 3, Windows x86
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 051b125e2b8ab360d3bed2799e766889258d4f6cf45021da9e4b0a52bfb227b3 |
|
MD5 | 72a7cce1df6618408a48ca2d14be001d |
|
BLAKE2b-256 | 5c568e4bd0c6a73b4c4d25cad3f2c302730acc9fa8cc119c99ea7667436fe9c9 |
Provenance
The following attestation bundles were made for ast_grep_cli-0.30.0-py3-none-win32.whl
:
Publisher:
pypi.yml
on ast-grep/ast-grep
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
ast_grep_cli-0.30.0-py3-none-win32.whl
- Subject digest:
051b125e2b8ab360d3bed2799e766889258d4f6cf45021da9e4b0a52bfb227b3
- Sigstore transparency entry: 148054363
- Sigstore integration time:
- Predicate type:
File details
Details for the file ast_grep_cli-0.30.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: ast_grep_cli-0.30.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 14.6 MB
- Tags: Python 3, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2755090122515642c21b8447f1dae9708c8944a075b588a68c0071399ec851d1 |
|
MD5 | 2b1afb0b55c6a648a4f6f12e37e7b653 |
|
BLAKE2b-256 | 85309a167168a848c6d847a84a7adbfdc2bd7e5d95b2f095547bdcb4f2c720e8 |
Provenance
The following attestation bundles were made for ast_grep_cli-0.30.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
:
Publisher:
pypi.yml
on ast-grep/ast-grep
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
ast_grep_cli-0.30.0-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
- Subject digest:
2755090122515642c21b8447f1dae9708c8944a075b588a68c0071399ec851d1
- Sigstore transparency entry: 148054365
- Sigstore integration time:
- Predicate type:
File details
Details for the file ast_grep_cli-0.30.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
.
File metadata
- Download URL: ast_grep_cli-0.30.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 14.4 MB
- Tags: Python 3, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 71ff6b0d843ad9815ec8a0d10cfd07c9bcfbaaf917f4cd04e9a5b2b3944d754b |
|
MD5 | d27799574a21edac65f938c1e7c437e9 |
|
BLAKE2b-256 | df26a2d41a5720f6b0dd9905a47172865ee71749ad6b848edbfbad76c80903e9 |
Provenance
The following attestation bundles were made for ast_grep_cli-0.30.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
:
Publisher:
pypi.yml
on ast-grep/ast-grep
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
ast_grep_cli-0.30.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
- Subject digest:
71ff6b0d843ad9815ec8a0d10cfd07c9bcfbaaf917f4cd04e9a5b2b3944d754b
- Sigstore transparency entry: 148054368
- Sigstore integration time:
- Predicate type:
File details
Details for the file ast_grep_cli-0.30.0-py3-none-macosx_10_12_x86_64.whl
.
File metadata
- Download URL: ast_grep_cli-0.30.0-py3-none-macosx_10_12_x86_64.whl
- Upload date:
- Size: 14.2 MB
- Tags: Python 3, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 448bf7b917187853753af915544165e6240b66e51ec8812155ba6f5ebdc695da |
|
MD5 | 494ef38101d96923afbef42020ca558c |
|
BLAKE2b-256 | 4f29d2e6ba7015cbce5fafd5eaadf919aab5f60c083ce29158c46d5fd792e0ef |
Provenance
The following attestation bundles were made for ast_grep_cli-0.30.0-py3-none-macosx_10_12_x86_64.whl
:
Publisher:
pypi.yml
on ast-grep/ast-grep
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
ast_grep_cli-0.30.0-py3-none-macosx_10_12_x86_64.whl
- Subject digest:
448bf7b917187853753af915544165e6240b66e51ec8812155ba6f5ebdc695da
- Sigstore transparency entry: 148054370
- Sigstore integration time:
- Predicate type:
File details
Details for the file ast_grep_cli-0.30.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.30.0-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
- Upload date:
- Size: 28.6 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: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e8a42a7962f7e2b93ec5fa0917e624233b76503da38f2cdcb4a2a477de8c2fb |
|
MD5 | 508f1cff789d627a37075e32e33b2aac |
|
BLAKE2b-256 | 36fed28f5fc3013b5e7dc2eb6d0c6397b17e25f2e718caca2ef646c86ce0e906 |
Provenance
The following attestation bundles were made for ast_grep_cli-0.30.0-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
:
Publisher:
pypi.yml
on ast-grep/ast-grep
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
ast_grep_cli-0.30.0-py3-none-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl
- Subject digest:
4e8a42a7962f7e2b93ec5fa0917e624233b76503da38f2cdcb4a2a477de8c2fb
- Sigstore transparency entry: 148054367
- Sigstore integration time:
- Predicate type: