用途:该技能帮助把自然语言查询翻译为 ast-grep 规则,用 AST 模式按代码结构而非纯文本进行搜索,适用于大型代码库中查找特定语言构造、复杂代码模式或需要理解结构的查询。
主要能力:支持 pattern 简单匹配、kind 加 has/inside 等关系规则、all/any/not 组合逻辑;可用 ast-grep run 做单节点模式搜索,用 ast-grep scan 配合 YAML 规则或 --inline-rules 做复杂结构查询;支持 --json 结构化输出,以及 --debug-query=cst/ast/pattern 检查 AST 结构与模式解释。
使用方式:先明确查询目标与语言,写示例代码,再编写规则并测试。快速测试可用 echo 管道配合 ast-grep scan --inline-rules ... --stdin;复杂规则建议写入规则文件后执行 ast-grep scan --rule test_rule.yml test_example.js。搜索代码库可用 ast-grep run --pattern 'console.log($ARG)' --lang javascript /path/to/project,或 ast-grep scan --rule my_rule.yml /path/to/project。关系规则(inside/has)建议加 stopBy: end。内联规则中元变量需转义,如 $VAR 或使用单引号。
适用场景:查找使用 await 的异步函数、类方法中的 console.log、缺少 try-catch 的异步函数等结构化查询。
限制:资料未提供安装方式、支持语言范围、性能数据或收费信息;规则语法细节需参考 references/rule_reference.md。