jscodeshift
Original:🇺🇸 English
Translated
jscodeshift codemod development best practices from Facebook/Meta. This skill should be used when writing, reviewing, or debugging jscodeshift codemods. Triggers on tasks involving AST transformation, code migration, automated refactoring, or codemod development.
9installs
Sourcepproenca/dot-skills
Added on
NPX Install
npx skill4agent add pproenca/dot-skills jscodeshiftTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Facebook/Meta jscodeshift Best Practices
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.
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
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 | |
Quick Reference
1. Parser Configuration (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
2. AST Traversal Patterns (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
3. Node Filtering (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
4. AST Transformation (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
5. Code Generation (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
6. Testing Strategies (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
7. Runner Optimization (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
8. Advanced Patterns (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
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.
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 |