ast-grep icon indicating copy to clipboard operation
ast-grep copied to clipboard

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

ast-grep

ast-grep(sg)

ast-grep(sg) is a fast and easy tool for code searching, linting, rewriting at large scale.

output

Introduction

ASTGrep is a lightning fast and user-friendly tool that performs static analysis and automatic code modification at large scale.

ASTGrep's core is searching and rewriting code based on abstract syntax tree produced by tree-sitter. It can help you do static analysis on

Installation

Install from source is the only way to try ast-grep locally at the moment. You need install rustup, clone the repository and then

cargo install --path ./crates/cli

Once the API is stablized, ast-grep will be available via package manager.

Feature Highlight

  • An intuitive pattern to find and replace AST. ASTGrep'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 manipulatioin.

  • YAML configuration to write new linting rules or code modification.

  • Written in compiled language, parsing with tree-sitter and utilizing multiple cores.

  • Beautiful command line interface :)

Democratize abstract syntax tree magic and liberate one from cumbersome AST programming!

CLI Screenshot

Search

image

Rewrite

image

Error report

image

Sponsor

If you find ASTGrep interesting and useful for your work, please buy me a coffee so I can spend more time on the project!

TODO:

Core

  • [x] Add replace
  • [x] Add find_all
  • [x] Add metavar char customization
  • [x] Add per-language customization
  • [x] Add support for vec/sequence matcher
  • [ ] Investigate perf heuristic (e.g. match fixed-string)
  • [ ] Group matching rules based on root pattern kind id
  • [x] View node in context
  • [x] implement iterative DFS mode

Metavariable Matcher

  • [ ] Regex
  • [x] Pattern
  • [x] Kind

Rule

  • [x] every / all
  • [x] either / any
  • [x] inside
  • [x] has
  • [ ] follows
  • [ ] precedes

CLI

  • [x] match against files in directory recursively
  • [x] interactive mode
  • [x] as dry run mode (listing all rewrite)
  • [ ] inplace edit mode
  • [ ] no-color mode
  • [ ] JSON output

Config

  • [x] support YAML config rule
  • [x] Add support for severity
  • [x] Add support for error message
  • [ ] Add support for error labels
  • [ ] Add support for fix

Binding

  • [ ] NAPI binding
  • [ ] WASM binding
  • [ ] Python binding

Playground

  • [ ] build a playground based on WASM binding

Builtin Ruleset

  • [ ] Migrate some ESLint rule (or RSLint rule)