ast-grep

Translate natural language queries into ast-grep structural search rules.

Updated Aug 28, 2021
One-click install
npx skills add https://github.com/XhstormR/dotfiles --skill ast-grep-xhstormr
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: ast-grep
Source: https://github.com/XhstormR/dotfiles/tree/main/.claude/skills/ast-grep
Command: npx skills add https://github.com/XhstormR/dotfiles --skill ast-grep-xhstormr

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

AST-based structural search replaces brittle text searches by matching code structures, enabling precise discovery across languages.

Core Features & Use Cases

  • Translate natural language queries into ast-grep rules to search code by structure.
  • Support complex patterns across multiple languages and Tree-sitter grammars.
  • Use cases include locating specific constructs, nested patterns, and code anti-patterns.

Quick Start

Write a simple rule to match a function declaration containing an await expression

Frequently Asked Questions about ast-grep

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I search for nested code patterns across multiple languages?

To search for nested code patterns across multiple languages, ast-grep translates natural language queries into structural AST rules. It matches code constructs based on syntax trees rather than plain text, using Tree-sitter grammars for precise multi-language discovery.

What is structural code search and when do I need it instead of text search?

Structural code search uses abstract syntax trees to match code constructs instead of raw strings. You need it when text search is too brittle, such as when locating specific language constructs, nested patterns, or code anti-patterns that require understanding syntax structure.

Can I use metavariables to capture specific patterns in a YAML rule object?

Yes, you can use metavariables in a YAML rule object to capture and match specific code patterns. ast-grep supports YAML rules with fields like kind, pattern, inside, has, and composite rules to define complex structural constraints.

How do I control traversal depth when matching complex AST structures?

You can control traversal depth when matching complex AST structures by using the stopBy field in your YAML rule. This allows precise traversal control for composite rules, ensuring structural searches halt at the correct nested pattern level.

Does ast-grep work with Tree-sitter grammars for language-agnostic code analysis?

Yes, ast-grep works with Tree-sitter grammars to provide language-agnostic code analysis. It applies to multi-language codebases by translating structural queries into rules that leverage Tree-sitter syntax trees for accurate pattern matching.

What is the best way to locate specific code constructs and anti-patterns in a repository?

The best way to locate specific code constructs and anti-patterns is using AST-powered structural search. By translating queries into ast-grep YAML rules, you can precisely discover nested patterns and complex structures across multi-language codebases.