ast-grep

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

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

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 calls inside class methods. 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 creation, testing, and full codebase search. - CLI Command Coverage: Documents pattern search with run, rule-based search with scan, inline rules, JSON output parsing, and AST inspection via --debug-query. - Debugging Guidance: Provides checklists for zero-match issues, metavariable escaping, stopBy: end usage, and correct node kind selection. - Use Case: Find every async JavaScript function that uses await but lacks a try-catch block by writing a composite YAML rule with all, 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, such as 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. Run `ast-grep run --pattern 'console.log($ARG)' --lang javascript .` for simple matches, or `ast-grep scan --rule rule.yml` for complex structural queries.

How do I write an ast-grep rule to find functions containing specific code?▼

Combine the `kind` rule with a relational `has` rule, for example `kind: function_declaration` plus `has: { pattern: await $EXPR, stopBy: end }`. Always set `stopBy: end` so the search traverses the entire subtree instead of stopping early.

Why does my ast-grep pattern return zero matches?▼

Patterns match whole AST nodes, not text substrings, so a qualified call like `std::env::var("X")` will not match the pattern `env::var($ENV)`. Test the pattern on a known snippet and inspect parsing with `--debug-query=pattern` to find the correct node shape.

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

`run --pattern` performs simple single-node pattern searches without YAML files, while `scan --rule` executes full YAML rules supporting relational rules like `inside` and `has` plus composite logic with `all`, `any`, and `not`. Use 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` captures zero or more nodes. The metavariable must be the only content in its AST node, so expressions like `obj.on$EVENT` do not work.