ast-grep

A CLI tool for code structural search, lint and rewriting. Written in Rust

README

ast-grep

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


output

Installation

You can install it from npm, pip, cargo, homebrew, scoop or MacPorts!

  1. ```bash
  2. npm install --global @ast-grep/cli
  3. pip install ast-grep-cli
  4. cargo install ast-grep

  5. # install via homebrew, thank @henryhchchc
  6. brew install ast-grep

  7. # install via scoop, thank @brian6932
  8. scoop install main/ast-grep

  9. # install via MacPorts
  10. sudo port install ast-grep
  11. ```
Or you can build ast-grep from source. You need install rustup, clone the repository and then
  1. ```bash
  2. cargo install --path ./crates/cli
  3. ```
Packages are available on other platforms too.

Command line usage example


ast-grep has following form.
  1. ```
  2. sg --pattern 'var code = $PATTERN' --rewrite 'let code = new $PATTERN' --lang ts
  3. ```

Example



  1. ```bash
  2. sg -p '$A && $A()' -l ts -r '$A?.()'
  3. ```

  1. ```bash
  2. sg -p 'new Zodios($URL,  $CONF as const,)' -l ts -r 'new Zodios($URL, $CONF)' -i
  3. ```



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.


CLI Screenshot


Search

FeatureCommandScreenshot
------------------------
Search`sg![image](https://github.com/ast-grep/ast-grep/assets/2883231/002db3a2-8a79-4838-ad5c-563634183c3f)
Rewrite`sg![image](https://github.com/ast-grep/ast-grep/assets/2883231/ad9394d8-3aea-4b96-8d54-6e01f06174d2)|
Report`sg![image](https://user-images.githubusercontent.com/2883231/187094977-fd544d4b-64de-4bba-8bea-8c0de047b352.png)