|new 0.2.3||Jan 26, 2023|
|0.1.18||Jan 16, 2023|
|0.1.13||Dec 18, 2022|
|0.1.9||Nov 5, 2022|
|0.1.0||Jul 7, 2022|
#66 in Command line utilities
305 downloads per month
ast-grep(sg) is a lightning fast and user friendly tool for code searching, linting, rewriting at large scale.
ast-grep is a 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!
# install via pnpm npm install --global @ast-grep/cli # install via cargo cargo 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
Command line usage example
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.
An intuitive pattern to find and replace AST. ast-grep's pattern looks like ordinary code you would write every day. (You can call 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 helpn you write rules much faster.