ast-grep

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

Updated Aug 28, 2026
One-click install
npx skills add https://github.com/az9713/claude-code-hooks-tutorial --skill ast-grep-az9713
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: ast-grep
Source: https://github.com/az9713/claude-code-hooks-tutorial/tree/main/skills/.claude/skills/ast-grep
Command: npx skills add https://github.com/az9713/claude-code-hooks-tutorial --skill ast-grep-az9713

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Text-based search cannot express structural code queries like "find async functions without try-catch" or "locate console.log calls inside class methods". This Skill translates natural language queries into ast-grep YAML rules that match code by its Abstract Syntax Tree structure rather than raw text. ## Core Features & Use Cases - Rule Authoring Workflow: Guides you through understanding the query, writing example code, drafting a YAML rule, testing it with the ast-grep CLI, and then scanning the full codebase. - Structural Search Patterns: Covers atomic rules (pattern, kind, regex), relational rules (inside, has, precedes, follows), composite rules (all, any, not), and metavariables ($VAR, $$VAR, $$$MULTI). - Debugging Support: Uses --debug-query to inspect the CST/AST when rules fail to match, with guidance on stopBy: end and correct kind values. - Use Case: Ask to find all async JavaScript functions that use await but lack error handling, and get a tested ast-grep rule plus the exact scan command to run against your 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, which matches code against Abstract Syntax Tree patterns rather than raw strings. Write a YAML rule with a pattern like console.log($ARG) or a kind like function_declaration, then run ast-grep scan --rule my_rule.yml on your project.

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

Combine the kind rule with the has relational rule, for example kind: function_declaration with has: pattern: await $EXPR. Always add stopBy: end to relational rules so the search traverses the entire subtree instead of stopping early.

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

First simplify the rule by removing sub-rules, then add stopBy: end to relational rules. Use ast-grep run --debug-query=cst to inspect the actual AST structure and verify that kind values match the language's Tree-sitter grammar.

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

ast-grep run performs simple single-node pattern searches via --pattern, while ast-grep scan executes full YAML rules supporting relational and composite logic. Use run for quick searches and scan for complex structural queries.

How do ast-grep metavariables work in patterns?▼

$VAR captures a single named node, $$VAR captures an unnamed node like an operator, and $$$VAR matches zero or more nodes. Metavariables must be the only text within their AST node, so patterns like obj.on$EVENT do not work.

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

Use ast-grep when the query depends on code structure, such as matching only inside class methods or excluding try-catch blocks. Plain text search is sufficient for literal strings but cannot express relational or negated structural conditions.