125 releases (29 breaking)

0.30.0 Nov 11, 2024
0.28.1 Oct 20, 2024
0.25.4 Jul 26, 2024
0.20.2 Mar 27, 2024
0.1.0 Jul 7, 2022

#182 in Parser implementations

Download history 754/week @ 2024-07-31 633/week @ 2024-08-07 319/week @ 2024-08-14 361/week @ 2024-08-21 164/week @ 2024-08-28 573/week @ 2024-09-04 177/week @ 2024-09-11 501/week @ 2024-09-18 323/week @ 2024-09-25 434/week @ 2024-10-02 923/week @ 2024-10-09 1225/week @ 2024-10-16 1255/week @ 2024-10-23 1314/week @ 2024-10-30 386/week @ 2024-11-06 249/week @ 2024-11-13

3,397 downloads per month
Used in sljit-sys

Custom license

690KB
20K SLoC

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 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

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

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.

Dependencies

~20–35MB
~543K SLoC