Giter Club home page Giter Club logo

ast-grep's Introduction

ast-grep

coverage badge Badge Badge GitHub Sponsors

ast-grep(sg)

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

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 or cargo!

# 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

A brew formula is also available for ast-grep.

brew tap ast-grep/tap
brew install ast-grep/tap/ast-grep

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

ast-grep's People

Contributors

herringtondarkholme avatar renovate[bot] avatar pd4d10 avatar siguremo avatar yyytbd avatar iwanabethatguy avatar ssshuai1999 avatar jack-michaud avatar mkatychev avatar gautamprikshit1 avatar sxzz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.