jscodeshift
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseFacebook/Meta jscodeshift Best Practices
Facebook/Meta jscodeshift 最佳实践
Comprehensive best practices guide for jscodeshift codemod development, designed for AI agents and LLMs. Contains 40 rules across 8 categories, prioritized by impact from critical (parser configuration, AST traversal) to incremental (advanced patterns). Each rule includes detailed explanations, real-world examples, and specific impact metrics.
本指南为AI Agent和大语言模型(LLM)提供了jscodeshift codemod开发的全面最佳实践,包含8个类别共40条规则,按影响优先级排序,从关键规则(解析器配置、AST遍历)到进阶规则(高级模式)。每条规则都配有详细说明、真实示例和具体影响指标。
When to Apply
适用场景
Reference these guidelines when:
- Writing new jscodeshift codemods for code migrations
- Debugging transform failures or unexpected behavior
- Optimizing codemod performance on large codebases
- Reviewing codemod code for correctness
- Testing codemods for edge cases and regressions
在以下场景中参考本指南:
- 为代码迁移编写新的jscodeshift codemod
- 调试转换失败或异常行为
- 优化codemod在大型代码库中的性能
- 审查codemod代码的正确性
- 测试codemod的边缘情况和回归问题
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Parser Configuration | CRITICAL | |
| 2 | AST Traversal Patterns | CRITICAL | |
| 3 | Node Filtering | HIGH | |
| 4 | AST Transformation | HIGH | |
| 5 | Code Generation | MEDIUM | |
| 6 | Testing Strategies | MEDIUM | |
| 7 | Runner Optimization | LOW-MEDIUM | |
| 8 | Advanced Patterns | LOW | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 解析器配置 | 关键(CRITICAL) | |
| 2 | AST遍历模式 | 关键(CRITICAL) | |
| 3 | 节点过滤 | 高(HIGH) | |
| 4 | AST转换 | 高(HIGH) | |
| 5 | 代码生成 | 中(MEDIUM) | |
| 6 | 测试策略 | 中(MEDIUM) | |
| 7 | 运行器优化 | 低-中(LOW-MEDIUM) | |
| 8 | 高级模式 | 低(LOW) | |
Quick Reference
快速参考
1. Parser Configuration (CRITICAL)
1. 解析器配置(CRITICAL)
- - Use correct parser for TypeScript files
parser-typescript-config - - Use Flow parser for Flow-typed code
parser-flow-annotation - - Avoid default babel5compat for modern syntax
parser-babel5-compat - - Export parser from transform module
parser-export-declaration - - Match AST Explorer parser to jscodeshift parser
parser-astexplorer-match
- - 为TypeScript文件使用正确的解析器
parser-typescript-config - - 为Flow类型标注的代码使用Flow解析器
parser-flow-annotation - - 现代语法避免使用默认的babel5compat解析器
parser-babel5-compat - - 从转换模块中导出解析器配置
parser-export-declaration - - 保持AST Explorer解析器与jscodeshift解析器一致
parser-astexplorer-match
2. AST Traversal Patterns (CRITICAL)
2. AST遍历模式(CRITICAL)
- - Use specific node types in find() calls
traverse-find-specific-type - - Use two-pass pattern for complex transforms
traverse-two-pass-pattern - - Return early when no transformation needed
traverse-early-return - - Use find() with filter object over filter() chain
traverse-find-filter-pattern - - Use closestScope() for scope-aware transforms
traverse-closest-scope - - Avoid repeated find() calls for same node type
traverse-avoid-repeated-find
- - 在find()调用中使用特定的节点类型
traverse-find-specific-type - - 复杂转换使用两次遍历模式
traverse-two-pass-pattern - - 无需转换时提前返回
traverse-early-return - - 优先使用带过滤对象的find()而非链式filter()调用
traverse-find-filter-pattern - - 依赖作用域的转换使用closestScope()
traverse-closest-scope - - 避免对同一节点类型重复调用find()
traverse-avoid-repeated-find
3. Node Filtering (HIGH)
3. 节点过滤(HIGH)
- - Check parent path before transformation
filter-path-parent-check - - Track import bindings for accurate usage detection
filter-import-binding - - Add nullish checks before property access
filter-nullish-checks - - Distinguish JSX context from regular JavaScript
filter-jsx-context - - Handle computed property keys in filters
filter-computed-properties
- - 转换前检查父节点路径
filter-path-parent-check - - 跟踪导入绑定以准确检测使用情况
filter-import-binding - - 属性访问前添加空值检查
filter-nullish-checks - - 区分JSX上下文与常规JavaScript
filter-jsx-context - - 过滤中处理计算属性键
filter-computed-properties
4. AST Transformation (HIGH)
4. AST转换(HIGH)
- - Use builder API for creating AST nodes
transform-builder-api - - Use replaceWith callback for context-aware transforms
transform-replacewith-callback - - Insert imports at correct position
transform-insert-import - - Preserve comments when replacing nodes
transform-preserve-comments - - Use renameTo for variable renaming
transform-renameto - - Remove unused imports after transformation
transform-remove-unused-imports
- - 使用Builder API创建AST节点
transform-builder-api - - 依赖上下文的转换使用replaceWith回调
transform-replacewith-callback - - 在正确位置插入导入语句
transform-insert-import - - 替换节点时保留注释
transform-preserve-comments - - 变量重命名使用renameTo方法
transform-renameto - - 转换后移除未使用的导入
transform-remove-unused-imports
5. Code Generation (MEDIUM)
5. 代码生成(MEDIUM)
- - Configure toSource() for consistent formatting
codegen-tosource-options - - Preserve original code style with recast
codegen-preserve-style - - Use template literals for complex node creation
codegen-template-literals - - Set appropriate print width for long lines
codegen-print-width
- - 配置toSource()以保证格式一致
codegen-tosource-options - - 使用recast保留原始代码风格
codegen-preserve-style - - 复杂节点创建使用模板字符串
codegen-template-literals - - 为长代码行设置合适的打印宽度
codegen-print-width
6. Testing Strategies (MEDIUM)
6. 测试策略(MEDIUM)
- - Use defineInlineTest for input/output verification
test-inline-snapshots - - Write negative test cases first
test-negative-cases - - Use dry run mode for codebase exploration
test-dry-run-exploration - - Use fixture files for complex test cases
test-fixture-files - - Test for parse error handling
test-parse-errors
- - 使用defineInlineTest验证输入输出
test-inline-snapshots - - 优先编写负面测试用例
test-negative-cases - - 使用试运行模式探索代码库
test-dry-run-exploration - - 复杂测试用例使用测试夹具文件
test-fixture-files - - 测试解析错误的处理逻辑
test-parse-errors
7. Runner Optimization (LOW-MEDIUM)
7. 运行器优化(LOW-MEDIUM)
- - Configure worker count for optimal parallelization
runner-parallel-workers - - Use ignore patterns to skip non-source files
runner-ignore-patterns - - Filter files by extension
runner-extensions-filter - - Process large codebases in batches
runner-batch-processing - - Use verbose output for debugging transforms
runner-verbose-output
- - 配置工作线程数以优化并行处理
runner-parallel-workers - - 使用忽略模式跳过非源码文件
runner-ignore-patterns - - 按文件扩展名过滤文件
runner-extensions-filter - - 大型代码库分批处理
runner-batch-processing - - 调试转换时使用详细输出
runner-verbose-output
8. Advanced Patterns (LOW)
8. 高级模式(LOW)
- - Compose multiple transforms into pipelines
advanced-compose-transforms - - Use scope analysis for safe variable transforms
advanced-scope-analysis - - Share state across files with options
advanced-multi-file-state - - Create custom collection methods
advanced-custom-collections
- - 将多个转换组合为流水线
advanced-compose-transforms - - 使用作用域分析实现安全的变量转换
advanced-scope-analysis - - 通过选项在多文件间共享状态
advanced-multi-file-state - - 创建自定义集合方法
advanced-custom-collections
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 single comprehensive document containing all rules, see AGENTS.md.
如需包含所有规则的综合文档,请查看AGENTS.md。
Reference Files
参考文件
| File | Description |
|---|---|
| AGENTS.md | Complete compiled guide with all rules |
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |
| 文件 | 描述 |
|---|---|
| AGENTS.md | 包含所有规则的完整编译指南 |
| references/_sections.md | 类别定义与排序说明 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本与参考信息 |