ast-grep

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

Updated Aug 22, 2026
One-click install
npx skills add https://github.com/vivekgoquest/hermes-agent-stable --skill ast-grep-vivekgoquest
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: ast-grep
Source: https://github.com/vivekgoquest/hermes-agent-stable/tree/main/optional-skills/software-development/ast-grep
Command: npx skills add https://github.com/vivekgoquest/hermes-agent-stable --skill ast-grep-vivekgoquest

SYSTEM DOCUMENTATION & REQUIREMENTS

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

What problem does it solve? Text-based search tools like grep and regex fail when the question depends on code structure rather than raw bytes, leading to false positives, missed matches, and unsafe bulk rewrites across large codebases. ## Core Features & Use Cases - Structural Code Search: Find functions, calls, imports, and control-flow patterns by AST shape using meta-variables like $VAR and $$$ across 25 languages. - Pattern-Driven Rewrites: Perform codemods such as migrating console.log to logger.info or Optional[X] to X | None, with mandatory dry-run previews before applying changes. - YAML Rule Scanning: Run reusable lint rules with auto-fixes via sg scan, including CI-friendly GitHub and SARIF output formats. - Use Case: When refactoring a TypeScript monorepo, use the helper to find every console.log call, preview the replacement with logger.info across all affected files, then apply the rewrite in a second pass and review with git diff. ## Quick Start Ask the agent to find every console.log call in the src directory and rewrite them to logger.info using the ast-grep helper with a dry-run preview 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 in the target language's syntax with meta-variables: $VAR matches one AST node and $$$ matches zero or more nodes. For example, the pattern console.log($MSG) finds every console.log call structurally, ignoring formatting differences.

How to run a codemod across many files with ast-grep?▼

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

When should I use ast-grep vs grep or ripgrep?▼

Use ast-grep when the answer depends on the language's syntax tree, such as finding function shapes, call patterns, or type assertions. Use ripgrep for text-shaped questions like string contents, comments, file names, or cross-language regex alternation.

Why does ast-grep return 0 matches when the code exists?▼

Common causes are regex syntax in the pattern, incomplete AST nodes like a Python def with a trailing colon, or a wrong --lang flag such as ts instead of tsx for JSX files. 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 purely structural matcher and cannot resolve variable references, shadowing, or data flow. For semantic questions, use type-aware tools like the TypeScript compiler, Pyright, or Semgrep with type inference.

Why does sg run with --json and --update-all not modify files?▼

ast-grep silently ignores --update-all when --json is set, returning JSON without mutating anything. Run two separate passes: one with --json=compact to preview, then one with --update-all to apply, which the helper's replace --apply does automatically.