ast-grep

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

Updated Jul 17, 2026
One-click install
npx skills add https://github.com/unnxt30/skills --skill ast-grep-unnxt30
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: ast-grep
Source: https://github.com/unnxt30/skills/tree/main/ast-grep
Command: npx skills add https://github.com/unnxt30/skills --skill ast-grep-unnxt30

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 calls inside specific contexts. This Skill translates natural language queries into ast-grep rules that match code by its Abstract Syntax Tree, enabling precise structural search across large codebases. ## Core Features & Use Cases - Rule Authoring Workflow: Guides you from a natural language query through example code, rule writing, testing, and full codebase scanning. - Structural Pattern Matching: Covers atomic rules (pattern, kind, regex), relational rules (inside, has, precedes, follows), and composite rules (all, any, not) with metavariables like $VAR and $$$ARGS. - CLI Testing and Debugging: Shows how to test rules with --inline-rules and --stdin, and debug mismatches using --debug-query to inspect the AST. - Use Case: Find all async JavaScript functions that use await but lack try-catch blocks, by composing a rule with kind, has, and not clauses, then scanning the entire project. ## Quick Start Ask the assistant to write and test an ast-grep rule that finds a specific code pattern in your project, for example all console.log calls inside class methods.

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 has or inside add structural context.

How do I write an ast-grep rule to find functions containing await?

Combine kind: function_declaration with a has relational rule containing pattern: await $EXPR and stopBy: end. The stopBy: end setting ensures the search traverses the entire function subtree instead of stopping at the first non-matching node.

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

First add stopBy: end to relational rules, then run ast-grep with --debug-query=cst to inspect the actual AST structure and verify node kind names. Also check that metavariables are the only content in their AST node and simplify the rule to isolate the failing condition.

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

ast-grep run performs simple single-pattern searches via --pattern, suitable for quick one-node matches. ast-grep scan executes full YAML rules with relational and composite logic, either from a rule file or via --inline-rules, 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.