use-ast-grep

Search, outline, and rewrite code by syntax tree using ast-grep rules.

Updated Jun 30, 2026
One-click install
npx skills add https://github.com/bsamiee/Rasm --skill use-ast-grep-bsamiee
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: use-ast-grep
Source: https://github.com/bsamiee/Rasm/tree/main/.claude/skills/use-ast-grep
Command: npx skills add https://github.com/bsamiee/Rasm --skill use-ast-grep-bsamiee

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Text-based search cannot distinguish a declaration from a use or match code by structure, making large-scale code search, linting, and rewriting error-prone. This Skill provides operational knowledge for using ast-grep's MCP tools and CLI to match, inspect, and rewrite code by its syntax tree. ## Core Features & Use Cases - Structural Code Search: Match code by tree-sitter patterns with metavariables, relational rules (has, inside, follows), and constraints across Python, Bash, YAML, C#, XML, SQL, and more. - Rule Authoring and Hardening: Derive lint rules from diffs, code smells, or project principles, then widen, collapse, and attach behavior-preserving fixes with transforms and rewriters. - Declaration Outlines: Use ast-grep outline and custom extractors to map declarations and members to source ranges in files too long to read whole. - Use Case: You need to find every hand-written counterpart of a library function across a monorepo and rewrite each call site. Write an ast-grep rule with a fix template, prove it on every sibling form, and apply it with ast-grep scan -U. ## Quick Start Ask the agent to list the declarations in a file or search for a code pattern by syntax tree, for example: outline the functions in src/app.py or find all calls matching a given pattern.

Frequently Asked Questions about use-ast-grep

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

FAQPage Schema
How do I search code by syntax tree with ast-grep?▼

Write a pattern that is valid code under the language's tree-sitter grammar, using metavariables like $VAR for one node and $$$MULTI for zero-or-more nodes. Run it with ast-grep run -l <lang> -p '<code>' or the find_code MCP tool to get a match list.

How do I write an ast-grep lint rule with an automatic fix?▼

Create a YAML rule file with id, language, a rule object combining pattern, kind, and relational keys, plus a fix template that substitutes captured metavariables. Place durable rules under the ruleDirs of sgconfig.yml and apply fixes with ast-grep scan -U.

When should I use ast-grep instead of ripgrep for code search?▼

Use ast-grep when you need structural matching, such as distinguishing declarations from uses or matching a call regardless of argument formatting. Plain text search fits literal strings, but cannot express syntax-tree relations like has, inside, or follows.

Why does my ast-grep rule fail to load with exit 8?▼

Common causes include a rule with no inferable kind set, an anonymous token passed to kind, duplicate keys in one map, or a cyclic dependency through matches. Run with --debug-query=cst to inspect how the pattern parsed and check the load error message.

Does ast-grep support languages beyond the built-in ones?▼

Yes, customLanguages in sgconfig.yml registers a language with a libraryPath, extensions, and optional outlineRules. languageGlobs can also map file globs to a language ahead of extension detection.

How do I list a file's declarations without reading the whole file?▼

Run ast-grep outline on the file to get each declaration's parsed range, using --match to filter by name and --view expanded for member signatures. Custom extractor rules can be added with --outline-rules for constructs the bundled extractors miss.