ast-grep

Search and rewrite code across 25 languages using AST-aware structural patterns.

Updated Jun 18, 2026
One-click install
npx skills add https://github.com/hugefiver/ocmm --skill ast-grep-hugefiver
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: ast-grep
Source: https://github.com/hugefiver/ocmm/tree/main/plugins/deepwork/skills/ast-grep
Command: npx skills add https://github.com/hugefiver/ocmm --skill ast-grep-hugefiver

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve? Text-based search tools like grep cannot reliably match code by its syntactic structure, making tasks like finding every call with a specific signature or applying a deterministic codemod across hundreds of files error-prone and manual. ## Core Features & Use Cases - AST-aware search and rewrite: Match code by syntax-tree shape using meta-variables ($VAR, $$$) instead of regex, across 25 languages including TypeScript, Python, Go, and Rust. - Safe codemods with dry-run: The helper script validates patterns offline, previews rewrites as JSON, and applies changes via a two-pass write trick that avoids the silent --update-all/--json conflict. - YAML rule scanning: Run project lint rules and auto-fixes through sg scan with sgconfig.yml support. - Use Case: Migrate every console.log($MSG) call to logger.info($MSG) across a TypeScript monorepo, previewing the exact files affected before mutating anything. ## Quick Start Ask the agent to find every console.log call in the src directory and rewrite it to logger.info using the ast-grep helper script with a dry-run first.

Frequently Asked Questions about ast-grep

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

FAQPage Schema
How do I search code by AST pattern instead of regex?▼

Use ast-grep patterns written as valid code with meta-variables like $VAR for one node and $$$ for many nodes. Run the helper with search 'console.log($MSG)' --lang ts src/ to match structurally rather than by text.

How to rewrite code across many files with ast-grep?▼

Run the helper's replace subcommand with a pattern and rewrite, which defaults to a dry-run preview. Add --apply to execute a second pass with --update-all that actually mutates the files.

When should I use ast-grep instead of grep or rg?▼

Use ast-grep only when the target is an exact syntax-tree shape, such as a specific call signature or import form. Use rg for text patterns, comments, file names, and regex-style searches that do not depend on code structure.

Why does ast-grep return 0 matches for my pattern?▼

Most failures come from regex syntax like \w or .* in patterns, incomplete code fragments such as a Python def with a trailing colon, or a wrong --lang value. Run the validate subcommand or --debug-query=ast to inspect how the pattern parses.

Does ast-grep support type inference or scope analysis?▼

No, ast-grep is a structural matcher only and cannot resolve references, shadowing, or data flow. For type-aware questions use TypeScript LSP, Pyright, Semgrep with type inference, or CodeQL.

Why does sg --update-all not modify files when using --json?▼

ast-grep silently ignores --update-all whenever --json is set, so the command returns JSON without mutating files. Run two separate invocations, one with --json=compact to preview and one with --update-all to apply.