codemod
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCodemod Best Practices
Codemod 最佳实践
Comprehensive best practices guide for Codemod (JSSG, ast-grep, workflows), designed for AI agents and LLMs. Contains 48 rules across 11 categories, prioritized by impact to guide automated refactoring and code generation.
这是为AI Agent和LLM设计的Codemod(JSSG、ast-grep、工作流)综合最佳实践指南。包含11个类别共48条规则,按影响优先级排序,用于指导自动重构和代码生成。
When to Apply
适用场景
Reference these guidelines when:
- Writing new codemods with JSSG or ast-grep
- Designing workflow configurations for migrations
- Debugging pattern matching or AST traversal issues
- Reviewing codemod code for performance and safety
- Setting up test fixtures for transform validation
在以下场景中参考本指南:
- 使用JSSG或ast-grep编写新的codemod
- 设计用于迁移的工作流配置
- 调试模式匹配或AST遍历问题
- 审查codemod代码的性能与安全性
- 设置用于验证转换的测试夹具
Rule Categories by Priority
按优先级排序的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | AST Understanding | CRITICAL | |
| 2 | Pattern Efficiency | CRITICAL | |
| 3 | Parsing Strategy | CRITICAL | |
| 4 | Node Traversal | HIGH | |
| 5 | Semantic Analysis | HIGH | |
| 6 | Edit Operations | MEDIUM-HIGH | |
| 7 | Workflow Design | MEDIUM-HIGH | |
| 8 | Testing Strategy | MEDIUM | |
| 9 | State Management | MEDIUM | |
| 10 | Security and Capabilities | LOW-MEDIUM | |
| 11 | Package Structure | LOW | |
| 优先级 | 类别 | 影响级别 | 前缀 |
|---|---|---|---|
| 1 | AST理解 | 关键 | |
| 2 | 模式效率 | 关键 | |
| 3 | 解析策略 | 关键 | |
| 4 | 节点遍历 | 高 | |
| 5 | 语义分析 | 高 | |
| 6 | 编辑操作 | 中高 | |
| 7 | 工作流设计 | 中高 | |
| 8 | 测试策略 | 中 | |
| 9 | 状态管理 | 中 | |
| 10 | 安全与权限 | 中低 | |
| 11 | 包结构 | 低 | |
Quick Reference
快速参考
1. AST Understanding (CRITICAL)
1. AST理解(关键)
- - Use AST Explorer before writing patterns
ast-explore-before-writing - - Understand named vs anonymous nodes
ast-understand-named-vs-anonymous - - Use kind constraint for precision
ast-use-kind-for-precision - - Use field access for structural queries
ast-field-access-for-structure - - Check null before property access
ast-check-null-before-access
- - 编写模式前先使用AST Explorer
ast-explore-before-writing - - 理解命名节点与匿名节点的区别
ast-understand-named-vs-anonymous - - 使用类型约束提高精度
ast-use-kind-for-precision - - 使用字段访问进行结构化查询
ast-field-access-for-structure - - 访问属性前先检查空值
ast-check-null-before-access
2. Pattern Efficiency (CRITICAL)
2. 模式效率(关键)
- - Use meta variables for flexible matching
pattern-use-meta-variables - - Avoid overly generic patterns
pattern-avoid-overly-generic - - Combine patterns with rule operators
pattern-combine-with-rules - - Use constraints for reusable matching logic
pattern-use-constraints - - Use relational patterns for context
pattern-use-relational-patterns - - Ensure patterns are idempotent
pattern-ensure-idempotency
- - 使用元变量实现灵活匹配
pattern-use-meta-variables - - 避免过于宽泛的模式
pattern-avoid-overly-generic - - 将模式与规则运算符结合使用
pattern-combine-with-rules - - 使用约束实现可复用的匹配逻辑
pattern-use-constraints - - 使用关系模式获取上下文
pattern-use-relational-patterns - - 确保模式具有幂等性
pattern-ensure-idempotency
3. Parsing Strategy (CRITICAL)
3. 解析策略(关键)
- - Select the correct parser for file type
parse-select-correct-parser - - Handle embedded languages with parseAsync
parse-handle-embedded-languages - - Provide context for ambiguous patterns
parse-provide-pattern-context - - Early return for non-applicable files
parse-early-return-non-applicable
- - 根据文件类型选择正确的解析器
parse-select-correct-parser - - 使用parseAsync处理嵌入式语言
parse-handle-embedded-languages - - 为模糊模式提供上下文
parse-provide-pattern-context - - 对不适用的文件提前返回
parse-early-return-non-applicable
4. Node Traversal (HIGH)
4. 节点遍历(高)
- - Use find() for single match, findAll() for multiple
traverse-use-find-vs-findall - - Collect multiple patterns in single traversal
traverse-single-pass-collection - - Use stopBy to control traversal depth
traverse-use-stopby-for-depth - - Use sibling navigation efficiently
traverse-use-siblings-efficiently - - Cache repeated node lookups
traverse-cache-repeated-lookups
- - 单个匹配使用find(),多个匹配使用findAll()
traverse-use-find-vs-findall - - 在单次遍历中收集多个模式
traverse-single-pass-collection - - 使用stopBy控制遍历深度
traverse-use-stopby-for-depth - - 高效使用同级节点导航
traverse-use-siblings-efficiently - - 缓存重复的节点查询
traverse-cache-repeated-lookups
5. Semantic Analysis (HIGH)
5. 语义分析(高)
- - Use file scope semantic analysis first
semantic-use-file-scope-first - - Handle null semantic analysis results
semantic-check-null-results - - Verify file ownership before cross-file edits
semantic-verify-file-ownership - - Cache semantic analysis results
semantic-cache-cross-file-results
- - 优先使用文件作用域语义分析
semantic-use-file-scope-first - - 处理语义分析的空结果
semantic-check-null-results - - 跨文件编辑前验证文件归属
semantic-verify-file-ownership - - 缓存跨文件语义分析结果
semantic-cache-cross-file-results
6. Edit Operations (MEDIUM-HIGH)
6. 编辑操作(中高)
- - Batch edits before committing
edit-batch-before-commit - - Preserve surrounding formatting in edits
edit-preserve-formatting - - Handle overlapping edit ranges
edit-handle-overlapping-ranges - - Use flatMap for conditional edits
edit-use-flatmap-for-conditional - - Add imports at correct position
edit-add-imports-correctly
- - 提交前批量处理编辑操作
edit-batch-before-commit - - 编辑时保留周围的格式
edit-preserve-formatting - - 处理重叠的编辑范围
edit-handle-overlapping-ranges - - 使用flatMap处理条件式编辑
edit-use-flatmap-for-conditional - - 在正确位置添加导入语句
edit-add-imports-correctly
7. Workflow Design (MEDIUM-HIGH)
7. 工作流设计(中高)
- - Order nodes by dependency
workflow-order-nodes-by-dependency - - Use matrix strategy for parallelism
workflow-use-matrix-for-parallelism - - Use manual gates for critical steps
workflow-use-manual-gates - - Validate workflows before running
workflow-validate-before-run - - Use conditional steps for dynamic workflows
workflow-use-conditional-steps
- - 按依赖关系排序节点
workflow-order-nodes-by-dependency - - 使用矩阵策略实现并行处理
workflow-use-matrix-for-parallelism - - 为关键步骤设置手动审核关卡
workflow-use-manual-gates - - 运行前验证工作流
workflow-validate-before-run - - 使用条件步骤实现动态工作流
workflow-use-conditional-steps
8. Testing Strategy (MEDIUM)
8. 测试策略(中)
- - Use input/expected fixture pairs
test-use-fixture-pairs - - Cover edge cases in test fixtures
test-cover-edge-cases - - Choose appropriate test strictness level
test-use-strictness-levels - - Update test fixtures intentionally
test-update-fixtures-intentionally - - Test on file subset before full run
test-run-on-subset-first
- - 使用输入/预期结果的夹具对
test-use-fixture-pairs - - 在测试夹具中覆盖边缘情况
test-cover-edge-cases - - 选择合适的测试严格级别
test-use-strictness-levels - - 有目的地更新测试夹具
test-update-fixtures-intentionally - - 先在文件子集上测试,再全面运行
test-run-on-subset-first
9. State Management (MEDIUM)
9. 状态管理(中)
- - Use state for resumable migrations
state-use-for-resumability - - Make transforms idempotent for safe reruns
state-make-transforms-idempotent - - Log progress for long-running migrations
state-log-progress-for-observability
- - 使用状态实现可恢复的迁移
state-use-for-resumability - - 使转换具有幂等性以支持安全重跑
state-make-transforms-idempotent - - 记录进度以提升可观测性
state-log-progress-for-observability
10. Security and Capabilities (LOW-MEDIUM)
10. 安全与权限(中低)
- - Minimize requested capabilities
security-minimize-capabilities - - Validate external inputs before use
security-validate-external-inputs - - Review third-party codemods before running
security-review-before-running-third-party
- - 最小化请求的权限
security-minimize-capabilities - - 使用前验证外部输入
security-validate-external-inputs - - 运行第三方codemod前先审查
security-review-before-running-third-party
11. Package Structure (LOW)
11. 包结构(低)
- - Use semantic versioning for packages
pkg-use-semantic-versioning - - Write descriptive package metadata
pkg-write-descriptive-metadata - - Organize package by convention
pkg-organize-by-convention
- - 对包使用语义化版本控制
pkg-use-semantic-versioning - - 编写描述性的包元数据
pkg-write-descriptive-metadata - - 按约定组织包结构
pkg-organize-by-convention
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
阅读各个参考文件获取详细说明和代码示例:
- 章节定义 - 类别结构和影响级别说明
- 规则模板 - 添加新规则的模板
Full Compiled Document
完整编译文档
For a complete guide with all rules expanded, see AGENTS.md.
如需包含所有规则扩展内容的完整指南,请查看 AGENTS.md。