Skip to main content

ast-grep

Guide for writing ast-grep rules to perform structural code search and analysis. Use when users need to search codebases using Abstract Syntax Tree (AST) patterns, find specific code structures, or perform complex code queries that go beyond simple text search. This skill should be used when users ask to search for code patterns, find specific language constructs, or locate code with particular structural characteristics. Use when this capability is needed.

跳到安装

来源信息

仓库
tomevault-io/skills-registry
最近来源活动
2026年4月28日 22:53
检测到的 SKILL.md 语言
英语
星标
0
分支
0

安装方式

默认使用会先检查来源的 Prompt;你也可以切换为直接命令,或下载本地副本。

检查来源文件

决定是否安装前,请先阅读 SKILL.md,以及 SkillsMP 当前展示的配套文件。

文件资源管理器
2 个文件

正在显示 SKILL.md

SKILL.md
来源说明 · 只读预览
name
ast-grep
description
Guide for writing ast-grep rules to perform structural code search and analysis. Use when users need to search codebases using Abstract Syntax Tree (AST) patterns, find specific code structures, or perform complex code queries that go beyond simple text search. This skill should be used when users ask to search for code patterns, find specific language constructs, or locate code with particular structural characteristics. Use when this capability is needed.
metadata
{"author":"aldoborrero"}
# ast-grep Code Search Use the `ast_grep` tool for all ast-grep operations. It has three modes: `pattern`, `rule`, and `inspect`. The tool description contains the full syntax reference — this skill teaches the *workflow* for writing effective searches. ## Workflow ### 1. Understand the query Before writing anything, clarify: - What code pattern or structure is the target? - Which programming language? - Are there variations or edge cases to include/exclude? ### 2. Start with pattern mode Always try the simplest approach first: ``` ast_grep({ mode: "pattern", pattern: "console.log($MSG)", lang: "javascript" }) ``` If a single pattern matches what you need, stop here. Don't over-engineer. ### 3. Escalate to rule mode only when needed Use rule mode when you need: - **Relational logic**: "X inside Y" or "X containing Y" → `has`/`inside` with `stopBy: end` - **Negation**: "X without Y" → `not` + `has` - **Alternatives**: "X or Y" → `any` - **Combinations**: "X and Y and Z" → `all` ``` ast_grep({ mode: "rule", lang: "typescript", rule: "kind: function_declaration\nhas:\n pattern: await $EXPR\n stopBy: end" }) ``` ### 4. Debug with inspect mode When rules don't match, use inspect to understand the AST: ``` ast_grep({ mode: "inspect", pattern: "async function foo() { await bar(); }", lang: "javascript", inspect_format: "ast" }) ``` This reveals the correct `kind` names. Common mistakes: - Wrong `kind` value (e.g. `arrow_function` vs `function_declaration`) - Missing `stopBy: end` on `has`/`inside` (search stops too early) - Pattern too specific (use metavariables to generalize) ### 5. Iterate The cycle is: **pattern → inspect → rule → inspect → refine**. Each step should make the rule more precise. Don't write a complex rule in one shot. ## Key Principles - **`stopBy: end` is mandatory** on `has` and `inside` rules. Without it, the search stops at the first non-matching node instead of traversing the full subtree. - **Prefer `pattern` over `kind`** when the code structure is unambiguous. `kind` + relational rules are for when patterns can't express the constraint. - **Use `all` for ordered metavariable binding**. If rule B depends on a metavariable captured by rule A, put A before B in an `all` array. - **Non-capturing wildcards (`$_VAR`)** avoid unnecessary binding. Use when you need to match "something" but don't care what. - **`$$$` matches zero or more nodes**. Use in function args (`$$$ARGS`), statement blocks (`$$$BODY`), etc. --- > Converted and distributed by [TomeVault](https://tomevault.io/claim/aldoborrero) — claim your Tome and manage your conversions. <!-- tomevault:4.0:skill_md:2026-04-14 -->
在 GitHub 查看