ast-grep by ast-grep/agent-skill
npx skills add https://github.com/ast-grep/agent-skill --skill ast-grep本技能帮助将自然语言查询转换为 ast-grep 规则,用于结构化代码搜索。ast-grep 使用抽象语法树(AST)模式来基于代码结构而非纯文本进行匹配,从而能够在大型代码库中进行强大而精确的代码搜索。
当用户需要以下情况时使用此技能:
遵循以下流程帮助用户编写有效的 ast-grep 规则:
清楚理解用户想要查找的内容。如有需要,询问澄清问题:
编写一个简单的代码片段,代表用户想要匹配的内容。将其保存到临时文件中进行测试。
示例: 如果搜索“使用 await 的异步函数”,创建一个测试文件:
// test_example.js
async function example() {
const result = await fetchData();
return result;
}
将模式转换为 ast-grep 规则。从简单开始,根据需要增加复杂性。
关键原则:
inside、),始终使用 以确保搜索到达方向的末端广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
hasstopBy: endpatternkind 配合 has/insideall、any 或 not 将复杂查询分解为较小的子规则示例规则文件(test_rule.yml):
id: async-with-await
language: javascript
rule:
kind: function_declaration
has:
pattern: await $EXPR
stopBy: end
有关全面的规则文档,请参阅 references/rule_reference.md。
使用 ast-grep CLI 验证规则是否匹配示例代码。有两种主要方法:
选项 A:使用内联规则测试(用于快速迭代)
echo "async function test() { await fetch(); }" | ast-grep scan --inline-rules "id: test
language: javascript
rule:
kind: function_declaration
has:
pattern: await \$EXPR
stopBy: end" --stdin
选项 B:使用规则文件测试(推荐用于复杂规则)
ast-grep scan --rule test_rule.yml test_example.js
如果没有匹配项的调试方法:
stopBy: end--debug-query 来理解 AST 结构(见下文)kind 值对于该语言是否正确一旦规则正确匹配示例代码,搜索实际的代码库:
对于简单的模式搜索:
ast-grep run --pattern 'console.log($ARG)' --lang javascript /path/to/project
对于基于规则的复杂搜索:
ast-grep scan --rule my_rule.yml /path/to/project
对于内联规则(无需创建文件):
ast-grep scan --inline-rules "id: my-rule
language: javascript
rule:
pattern: \$PATTERN" /path/to/project
转储 AST 结构以理解代码如何被解析:
ast-grep run --pattern 'async function example() { await fetch(); }' \
--lang javascript \
--debug-query=cst
可用格式:
cst:具体语法树(显示所有节点,包括标点符号)ast:抽象语法树(仅显示命名节点)pattern:显示 ast-grep 如何解释你的模式使用此功能来:
kind 值示例:
# 查看目标代码的结构
ast-grep run --pattern 'class User { constructor() {} }' \
--lang javascript \
--debug-query=cst
# 查看 ast-grep 如何解释你的模式
ast-grep run --pattern 'class $NAME { $$$BODY }' \
--lang javascript \
--debug-query=pattern
无需创建文件即可针对代码片段测试规则:
echo "const x = await fetch();" | ast-grep scan --inline-rules "id: test
language: javascript
rule:
pattern: await \$EXPR" --stdin
添加 --json 获取结构化输出:
echo "const x = await fetch();" | ast-grep scan --inline-rules "..." --stdin --json
基于模式的简单搜索,用于匹配单个 AST 节点:
# 基本模式搜索
ast-grep run --pattern 'console.log($ARG)' --lang javascript .
# 搜索特定文件
ast-grep run --pattern 'class $NAME' --lang python /path/to/project
# 用于编程使用的 JSON 输出
ast-grep run --pattern 'function $NAME($$$)' --lang javascript --json .
何时使用:
基于 YAML 规则的搜索,用于复杂的结构化查询:
# 使用规则文件
ast-grep scan --rule my_rule.yml /path/to/project
# 使用内联规则
ast-grep scan --inline-rules "id: find-async
language: javascript
rule:
kind: function_declaration
has:
pattern: await \$EXPR
stopBy: end" /path/to/project
# JSON 输出
ast-grep scan --rule my_rule.yml --json /path/to/project
何时使用:
提示: 对于关系规则(inside/has),始终添加 stopBy: end 以确保完全遍历。
对于关系规则,除非有特定原因,否则始终使用 stopBy: end:
has:
pattern: await $EXPR
stopBy: end
这确保搜索遍历整个子树,而不是在第一个不匹配的节点处停止。
从最简单的可能工作的规则开始:
patternkind 来匹配节点类型has、inside)all、any、not)组合复杂逻辑console.log($ARG))当规则不匹配时:
--debug-query=cst 查看实际的 AST 结构kind 是否符合你的预期使用 --inline-rules 时,在 shell 命令中转义元变量:
\$VAR 而不是 $VAR(shell 将 $ 解释为变量)'$VAR' 有效示例:
# 正确:转义 $
ast-grep scan --inline-rules "rule: {pattern: 'console.log(\$ARG)'}" .
# 或使用单引号
ast-grep scan --inline-rules 'rule: {pattern: "console.log($ARG)"}' .
查找使用 await 的异步函数:
ast-grep scan --inline-rules "id: async-await
language: javascript
rule:
all:
- kind: function_declaration
- has:
pattern: await \$EXPR
stopBy: end" /path/to/project
查找类方法中的 console.log:
ast-grep scan --inline-rules "id: console-in-class
language: javascript
rule:
pattern: console.log(\$\$\$)
inside:
kind: method_definition
stopBy: end" /path/to/project
查找没有 try-catch 的异步函数:
ast-grep scan --inline-rules "id: async-no-trycatch
language: javascript
rule:
all:
- kind: function_declaration
- has:
pattern: await \$EXPR
stopBy: end
- not:
has:
pattern: try { \$\$\$ } catch (\$E) { \$\$\$ }
stopBy: end" /path/to/project
包含 ast-grep 规则语法的详细文档:
rule_reference.md:全面的 ast-grep 规则文档,涵盖原子规则、关系规则、复合规则和元变量需要详细规则语法信息时加载这些参考文档。
每周安装量
1.4K
仓库
GitHub 星标数
557
首次出现
2026年1月22日
安全审计
安装于
claude-code1.2K
opencode739
codex708
github-copilot680
gemini-cli676
amp564
This skill helps translate natural language queries into ast-grep rules for structural code search. ast-grep uses Abstract Syntax Tree (AST) patterns to match code based on its structure rather than just text, enabling powerful and precise code search across large codebases.
Use this skill when users:
Follow this process to help users write effective ast-grep rules:
Clearly understand what the user wants to find. Ask clarifying questions if needed:
Write a simple code snippet that represents what the user wants to match. Save this to a temporary file for testing.
Example: If searching for "async functions that use await", create a test file:
// test_example.js
async function example() {
const result = await fetchData();
return result;
}
Translate the pattern into an ast-grep rule. Start simple and add complexity as needed.
Key principles:
stopBy: end for relational rules (inside, has) to ensure search goes to the end of the directionpattern for simple structureskind with has/inside for complex structuresall, any, or notExample rule file (test_rule.yml):
id: async-with-await
language: javascript
rule:
kind: function_declaration
has:
pattern: await $EXPR
stopBy: end
See references/rule_reference.md for comprehensive rule documentation.
Use ast-grep CLI to verify the rule matches the example code. There are two main approaches:
Option A: Test with inline rules (for quick iterations)
echo "async function test() { await fetch(); }" | ast-grep scan --inline-rules "id: test
language: javascript
rule:
kind: function_declaration
has:
pattern: await \$EXPR
stopBy: end" --stdin
Option B: Test with rule files (recommended for complex rules)
ast-grep scan --rule test_rule.yml test_example.js
Debugging if no matches:
stopBy: end to relational rules if not present--debug-query to understand the AST structure (see below)kind values are correct for the languageOnce the rule matches the example code correctly, search the actual codebase:
For simple pattern searches:
ast-grep run --pattern 'console.log($ARG)' --lang javascript /path/to/project
For complex rule-based searches:
ast-grep scan --rule my_rule.yml /path/to/project
For inline rules (without creating files):
ast-grep scan --inline-rules "id: my-rule
language: javascript
rule:
pattern: \$PATTERN" /path/to/project
Dump the AST structure to understand how code is parsed:
ast-grep run --pattern 'async function example() { await fetch(); }' \
--lang javascript \
--debug-query=cst
Available formats:
cst: Concrete Syntax Tree (shows all nodes including punctuation)ast: Abstract Syntax Tree (shows only named nodes)pattern: Shows how ast-grep interprets your patternUse this to:
kind values for nodesExample:
# See the structure of your target code
ast-grep run --pattern 'class User { constructor() {} }' \
--lang javascript \
--debug-query=cst
# See how ast-grep interprets your pattern
ast-grep run --pattern 'class $NAME { $$$BODY }' \
--lang javascript \
--debug-query=pattern
Test a rule against code snippet without creating files:
echo "const x = await fetch();" | ast-grep scan --inline-rules "id: test
language: javascript
rule:
pattern: await \$EXPR" --stdin
Add --json for structured output:
echo "const x = await fetch();" | ast-grep scan --inline-rules "..." --stdin --json
Simple pattern-based search for single AST node matches:
# Basic pattern search
ast-grep run --pattern 'console.log($ARG)' --lang javascript .
# Search specific files
ast-grep run --pattern 'class $NAME' --lang python /path/to/project
# JSON output for programmatic use
ast-grep run --pattern 'function $NAME($$$)' --lang javascript --json .
When to use:
YAML rule-based search for complex structural queries:
# With rule file
ast-grep scan --rule my_rule.yml /path/to/project
# With inline rules
ast-grep scan --inline-rules "id: find-async
language: javascript
rule:
kind: function_declaration
has:
pattern: await \$EXPR
stopBy: end" /path/to/project
# JSON output
ast-grep scan --rule my_rule.yml --json /path/to/project
When to use:
Tip: For relational rules (inside/has), always add stopBy: end to ensure complete traversal.
For relational rules, always use stopBy: end unless there's a specific reason not to:
has:
pattern: await $EXPR
stopBy: end
This ensures the search traverses the entire subtree rather than stopping at the first non-matching node.
Begin with the simplest rule that could work:
pattern firstkind to match the node typehas, inside) as neededall, any, not) for complex logicconsole.log($ARG))When rules don't match:
--debug-query=cst to see the actual AST structurekind matches what you expectWhen using --inline-rules, escape metavariables in shell commands:
\$VAR instead of $VAR (shell interprets $ as variable)'$VAR' works in most shellsExample:
# Correct: escaped $
ast-grep scan --inline-rules "rule: {pattern: 'console.log(\$ARG)'}" .
# Or use single quotes
ast-grep scan --inline-rules 'rule: {pattern: "console.log($ARG)"}' .
Find async functions that use await:
ast-grep scan --inline-rules "id: async-await
language: javascript
rule:
all:
- kind: function_declaration
- has:
pattern: await \$EXPR
stopBy: end" /path/to/project
Find console.log inside class methods:
ast-grep scan --inline-rules "id: console-in-class
language: javascript
rule:
pattern: console.log(\$\$\$)
inside:
kind: method_definition
stopBy: end" /path/to/project
Find async functions without try-catch:
ast-grep scan --inline-rules "id: async-no-trycatch
language: javascript
rule:
all:
- kind: function_declaration
- has:
pattern: await \$EXPR
stopBy: end
- not:
has:
pattern: try { \$\$\$ } catch (\$E) { \$\$\$ }
stopBy: end" /path/to/project
Contains detailed documentation for ast-grep rule syntax:
rule_reference.md: Comprehensive ast-grep rule documentation covering atomic rules, relational rules, composite rules, and metavariablesLoad these references when detailed rule syntax information is needed.
Weekly Installs
1.4K
Repository
GitHub Stars
557
First Seen
Jan 22, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
claude-code1.2K
opencode739
codex708
github-copilot680
gemini-cli676
amp564
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装