SuperAPI AI 工具集

codemod-runner

编程与开发

查看项目

更新于 10-16 06:19

技能介绍

Codemod Runner 用于大规模自动化代码变换,强调安全与幂等。先做范围评估:用 rg 统计受影响文件与总出现次数,超过 50 个文件时用 batch.list 按每批 25 个文件拆分。工具选择:Python 重命名、API 迁移等用 LibCST(pip install libcst),轻量搜索替换用 comby;JavaScript/TypeScript 需要完整类型信息用 ts-morph,仅结构匹配用 ast-grep(npm install -g @ast-grep/cli),已有 codemod 生态用 jscodeshift(npx jscodeshift -t transform.js src/)。执行时逐批运行,示例包括 sg run -p 'old_fn($A)' -r 'new_fn($A)' --lang python $(cat batch-aa)、comby 'old_fn(:[args])' 'new_fn(:[args])' -f .py $(cat batch-aa)、npx jscodeshift -t transform.js $(cat batch-aa)。每批后用 git diff --stat 校验二次运行是否零差异,确认幂等;若仍有差异需检查变换逻辑再继续。随后用 rg 'old-pattern' | wc -l 跟踪剩余数,必须单调下降并最终为 0;全部批次完成且验证通过后再 git add -p 与 git commit。限制:工具安装与平台细节以官方文档为准,本文未提供;大型代码库可参考 /swarm-patterns 的并行批处理模式。