SuperAPI AI 工具集

ast-grep

通用与其它

查看项目

更新于 10-15 20:13

技能介绍

用途:ast-grep(命令 sg,也安装为 ast-grep)是按 AST 结构而非文本字节进行搜索与重写的工具,覆盖 25 种语言。当问题取决于代码形态(函数、调用、类、导入、控制流)时使用;若取决于文本(字符串内容、注释、许可证头、文件名、跨语言正则),应改用 grep/rg。

主要能力:支持结构化搜索、按模式重写、运行 YAML 规则(sgconfig.yml、rules/)、自动修复、快照测试等。模式语言中 $VAR 匹配一个 AST 节点,$$$VAR 匹配零个或多个节点;模式本身必须是合法代码,不是正则。

使用方式:本技能提供 scripts/ast_grep_helper.py(Python 3 标准库单文件封装)及 install.sh、install.ps1。常用子命令:search、replace(默认 dry-run,加 --apply 才写入)、scan、validate、langs、doctor、install。示例:python3 scripts/ast_grep_helper.py search 'console.log($MSG)' --lang ts src/;python3 scripts/ast_grep_helper.py replace 'console.log($MSG)' 'logger.info($MSG)' --lang ts src/ --apply。也可直接用 sg:sg run -p 'console.log($MSG)' --lang ts src/、sg scan src/。OmO 原生还注册了 ast-grep MCP 工具:mcp__ast_grep_search、mcp__ast_grep_rewrite、mcp__ast_grep_scan。

限制与注意:--update-all 与 --json 互斥,需分两遍执行;shell 中模式要单引号包裹;--stdin 时必须显式指定 --lang;重写前务必先 dry-run;Linux 上 sg 与 setgroups 冲突,优先用 ast-grep。资料未提供具体收费、价格与平台支持细节。