# ast-grep

> A fast CLI tool for structural code search, linting, and rewriting based on abstract syntax trees.

ast-grep (sg) is an open-source, MIT-licensed command-line tool written in Rust for searching, linting, and rewriting code by syntax structure rather than text. Patterns are written like ordinary code and matched against tree-sitter-produced syntax trees across many files. It also offers a Node.js binding for programmatic use.

## What It Is

ast-grep works like a syntax-aware grep/sed. You write a code pattern, using wildcards such as `$MATCH` to stand for any single AST node, and the tool finds all code with the same syntactical structure. It can then rewrite the matches with a replacement pattern, either from the command line or interactively.

## Search, Lint, and Rewrite Workflow

A one-line command can locate and modify code, for example rewriting `$A && $A()` to `$A?.()` in TypeScript. For linting, rules are defined in YAML configuration and run with `ast-grep scan`, producing formatted error reports. The project also documents an interactive codemod mode, a language server, and a testing tool for rules.

## Programmatic Usage and Languages

The `@ast-grep/napi` package exposes syntax trees through jQuery-like traversal methods, with opt-in type safety. The site lists 20+ supported languages, and custom tree-sitter parsers can be loaded dynamically. A web playground lets users try patterns in the browser.

## Installation and Intended Users

It can be installed through npm, pip, cargo, cargo-binstall, Homebrew, Scoop, MacPorts, mise, or built from source. The README says it can help library authors ship breaking-change migrations, tech leads enforce coding practices, and security researchers write rules faster.

## Features
- Structural code search using AST patterns
- Code rewriting and codemods
- YAML-based lint rules via ast-grep scan
- Interactive mode
- Language server
- Rule testing tool
- Node.js binding (@ast-grep/napi)
- 20+ supported languages
- Custom tree-sitter language loading
- Online playground
- Parallel multi-core processing

## Integrations
tree-sitter, npm, pip, Cargo, Homebrew, Scoop, MacPorts, mise

## Platforms
WINDOWS, MACOS, LINUX, WEB, API, DEVELOPER_SDK, CLI

## Pricing
Open Source

## Version
0.45.3

## Links
- Website: https://ast-grep.github.io/
- Documentation: https://ast-grep.github.io/guide/introduction
- Repository: https://github.com/ast-grep/ast-grep
- EveryDev.ai: https://www.everydev.ai/tools/ast-grep
