ast-grep

Write ast-grep rules to search codebases using Abstract Syntax Tree patterns.

Updated Oct 16, 2025
One-click install
npx skills add https://github.com/hugotown/dotfiles --skill ast-grep-hugotown
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: ast-grep
Source: https://github.com/hugotown/dotfiles/tree/main/agents/skills/ast-grep
Command: npx skills add https://github.com/hugotown/dotfiles --skill ast-grep-hugotown

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Text-based search cannot reliably find code by structure, such as all async functions missing error handling or console.log calls inside class methods. This Skill translates natural language queries into ast-grep rules that match code by its Abstract Syntax Tree structure. ## Core Features & Use Cases - Rule Authoring Workflow: Guides you from a natural language query through example code, rule creation, testing, and full codebase search. - Structural Code Search: Uses atomic, relational, and composite rules (pattern, kind, inside, has, all, any, not) with metavariables to match complex code structures. - Debugging Support: Uses --debug-query to inspect the AST/CST and diagnose why rules fail to match. - Use Case: Find all async JavaScript functions that use await but lack try-catch blocks across an entire repository with a single YAML rule. ## Quick Start Ask the assistant to find all async functions that use await but have no try-catch error handling in your JavaScript project.

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 structure instead of text?▼

Use ast-grep with a pattern or YAML rule that matches Abstract Syntax Tree nodes rather than raw text. For example, the pattern console.log($ARG) matches any console.log call regardless of formatting, and relational rules like inside or has add structural context.

How to find functions containing specific code with ast-grep?▼

Combine the kind rule with a has relational rule, such as kind: function_declaration plus has: pattern: await $EXPR with stopBy: end. The stopBy: end option ensures the search traverses the entire subtree instead of stopping at the first non-matching node.

Why is my ast-grep rule not matching any code?▼

Run ast-grep with --debug-query=cst to inspect the actual AST structure and verify the node kind names for your language. Also check that relational rules include stopBy: end and that metavariables are the only content within their AST node.

What is the difference between ast-grep run and ast-grep scan?▼

ast-grep run executes simple single-pattern searches directly from the command line, while ast-grep scan runs YAML rule files or inline rules supporting relational and composite logic. Use run for quick one-node matches and scan for complex structural queries.

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

Use ast-grep when matches depend on code structure, such as calls inside class methods or functions lacking error handling, where text search produces false positives. Plain grep remains simpler for literal string or regex searches without structural context.