用途:该技能帮助把自然语言查询翻译成 ast-grep 规则,用抽象语法树(AST)做结构化代码搜索与分析,而非纯文本匹配,适合在大型代码库中精确定位代码结构。
主要能力:支持用 pattern 匹配简单结构,用 kind 配合 has/inside 等关系规则匹配复杂结构,用 all/any/not 组合逻辑;支持元变量 $NAME(单个节点)与 $$$ITEMS(多个节点)捕获内容;支持通过 --rewrite 或 YAML 规则的 fix 字段改写代码,并可用 rewriters 做多节点转换。
使用方式:先明确查询与语言,写示例代码,再写规则并测试。快速测试可用 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。改写可用 ast-grep run --pattern 'foo' --rewrite 'bar' --lang python,配合 --interactive 或 --update-all。调试可用 --debug-query=cst/ast/pattern 查看 AST 结构。
限制与注意:关系规则(inside/has)建议加 stopBy: end;命令行中 $ 需转义或用单引号;未匹配的元变量在 fix 中变为空字符串;具体语言支持范围与安装方式资料未提供。