Skip to main content

Structural Search and Rewrite code at large scale using precise AST pattern.

Project description

ast-grep

coverage badge Discord Badge Badge GitHub Sponsors

ast-grep(sg)

ast-grep(sg) is a CLI tool for code structural search, lint, and rewriting.

Introduction

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!

Demo

output

Installation

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

npm install --global @ast-grep/cli
pip install ast-grep-cli
cargo install ast-grep

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

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

Or you can build ast-grep from source. You need install rustup, clone the repository and then

cargo install --path ./crates/cli

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 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 help you write rules much faster.

CLI Screenshot

Search

Feature Command Screenshot
Search sg -p 'Some($A)' -l rs image
Rewrite sg -p '$F && $F($$$ARGS)' -r '$F?.($$$ARGS)' -l ts image
Report sg scan image

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ast_grep_cli-0.7.2.tar.gz (118.6 kB view hashes)

Uploaded Source

Built Distributions

ast_grep_cli-0.7.2-py3-none-win_arm64.whl (9.4 MB view hashes)

Uploaded Python 3 Windows ARM64

ast_grep_cli-0.7.2-py3-none-win_amd64.whl (9.8 MB view hashes)

Uploaded Python 3 Windows x86-64

ast_grep_cli-0.7.2-py3-none-win32.whl (9.3 MB view hashes)

Uploaded Python 3 Windows x86

ast_grep_cli-0.7.2-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (9.6 MB view hashes)

Uploaded Python 3 manylinux: glibc 2.17+ x86-64

ast_grep_cli-0.7.2-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (9.3 MB view hashes)

Uploaded Python 3 manylinux: glibc 2.17+ ARM64

ast_grep_cli-0.7.2-py3-none-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (18.7 MB view hashes)

Uploaded Python 3 macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

ast_grep_cli-0.7.2-py3-none-macosx_10_7_x86_64.whl (9.4 MB view hashes)

Uploaded Python 3 macOS 10.7+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page