howto-ast-grep

Search and transform code patterns using AST structure with ast-grep.

13|Updated Jan 18, 2026
One-click install
npx skills add https://github.com/pbdeuchler/llm-plugins --skill howto-ast-grep
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: howto-ast-grep
Source: https://github.com/pbdeuchler/llm-plugins/tree/main/plugins/tooling/skills/howto-ast-grep
Command: npx skills add https://github.com/pbdeuchler/llm-plugins --skill howto-ast-grep

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

ast-grep matches code by AST structure, not text, enabling precise searches and safe rewrites across languages.

Core Features & Use Cases

  • AST-based pattern matching with metavariables
  • Language-specific patterns for JavaScript/TypeScript, Python, Go, and Rust
  • JSON output options and YAML rules for complex matching

Quick Start

Run sg with a language flag to search for an AST pattern in your codebase, for example sg run -p 'use$HOOK($$$)' -l typescript src/.

Frequently Asked Questions about howto-ast-grep

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I search and refactor code by AST structure instead of text?

Code search and refactoring by AST structure uses abstract syntax trees to match precise code patterns, ignoring text formatting. This enables safe rewrites and anti-pattern detection across multi-language codebases using metavariables.

Can I use ast-grep for static analysis across multiple programming languages?

Static analysis with ast-grep applies across multiple programming languages, specifically supporting JavaScript, TypeScript, Python, Go, and Rust. Language-specific patterns ensure accurate code analysis for each unique syntax structure.

What is the best way to find and replace function calls during code migrations?

Function call detection and replacement during migrations is best handled using AST pattern matching with metavariables. Describing concrete AST shapes allows you to identify exact function structures and transform them safely.

Do I need the sg CLI installed to run AST pattern matching?

The sg CLI is required to run AST pattern matching. You must execute it with a valid language flag and structural patterns describing the code shapes you want to search or transform within your project directory.

How does AST-based code analysis differ from text-based grep?

AST-based code analysis matches structural syntax shapes rather than literal text strings like grep. This prevents false positives from variable naming or formatting differences and enables precise structural transformations.

How do I define complex matching rules for anti-pattern detection?

Complex matching rules for anti-pattern detection are defined using YAML rules and JSON output options. These configurations allow you to specify intricate structural patterns and metavariables for accurate codebase scanning.