Crates.io | ast-grep |
lib.rs | ast-grep |
version | |
source | src |
created_at | 2022-07-07 03:28:53.509936 |
updated_at | 2024-12-08 20:37:14.520105 |
description | Search and Rewrite code at large scale using precise AST pattern |
homepage | https://ast-grep.github.io/ |
repository | https://github.com/ast-grep/ast-grep |
max_upload_size | |
id | 620897 |
Cargo.toml error: | TOML parse error at line 19, column 1 | 19 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
ast-grep(sg) is a CLI tool for code structural search, lint, and rewriting.
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!
See more screenshots on the website.
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.
ast-grep has following form.
sg --pattern 'var code = $PATTERN' --rewrite 'let code = new $PATTERN' --lang ts
sg -p '$A && $A()' -l ts -r '$A?.()'
sg -p 'new Zodios($URL, $CONF as const,)' -l ts -r 'new Zodios($URL, $CONF)' -i
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!
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!