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 it as your old-friend grep
but it matches 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 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
# 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 install rustup, clone the repository and then
cargo install --path ./crates/cli
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
Hashes for ast_grep_cli-0.20.5-py3-none-win_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2932cc33052ee5d1a72ddc35594883fb5fa4bed8c4b3d5c55c63e454396f6ca5 |
|
MD5 | afbbc1cef4430f6c8431d974d5333234 |
|
BLAKE2b-256 | fc531a3c4a127823a116e3999e71ba5b093655199c36aba8dde1f5ad5d786968 |
Hashes for ast_grep_cli-0.20.5-py3-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75b59f90802a67f7d864847b524af6e55643119cd3b3ed28c3f4b51e55e792c1 |
|
MD5 | 18435d53eac21ae8fbdde9bb8bb59791 |
|
BLAKE2b-256 | 40adf2a40ed036ffaabf1d8a20054634cab4637042ebbe1a7d150b2bd9e9db32 |
Hashes for ast_grep_cli-0.20.5-py3-none-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5dbdd67424a0aab79ca015e95f8821d10cd9ceeac244490a875315c2338b4b7 |
|
MD5 | d996ca9d9b08732a3a30b34fda2e815a |
|
BLAKE2b-256 | f4284568c4e9e598efc716bea9d91984fda10dc6f8b59188a5566e8ad90cc239 |
Hashes for ast_grep_cli-0.20.5-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc67eeaca001bb8c0a8bf6826d7438233b5e8ba74d6ee965a8b6f8b3d1912556 |
|
MD5 | 70a11909507f060ed0455d085c80d0b2 |
|
BLAKE2b-256 | 93938b0d8497ac6de9a63c7440fe43ace98d9545445d164eb7046e99f4affcba |
Hashes for ast_grep_cli-0.20.5-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d00b257a70edb257d90eded0fc93c512c7757eac8e4e3c7ecf68992f2645a700 |
|
MD5 | ef790b62447dae4810e60f93e3a5915f |
|
BLAKE2b-256 | 1172182d1e8a32f7c9ee05cbc2b50d240b36af4ccdf48d16586ef7dddf8bdb9f |
Hashes for ast_grep_cli-0.20.5-py3-none-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38008761e7a3f542ac42f93f86e37b8c2769c1a487b0486400256cded2817e88 |
|
MD5 | 6806c93e6ac61b5165dfe5f9a7c02042 |
|
BLAKE2b-256 | c09823f134644c1034e3e2a01761e354f141e77478a464a5e676e22755443c56 |
Hashes for ast_grep_cli-0.20.5-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 09647a9449db57e4f7bda622728805d6c8cc2b22cece904cceb0548e43bac3ed |
|
MD5 | 71a9599d25aab2bf808a89c9591533ef |
|
BLAKE2b-256 | 078f0a6c16d96960ba73d70ebd8b5acbadf48b7d45df6c540023eb375332d22c |