File contents Codemod Best Practices
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.
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
Rule Categories by Priority
Priority
Category
Impact
Prefix
1
AST Understanding
CRITICAL
ast-
2
Pattern Efficiency
CRITICAL
pattern-
3
Parsing Strategy
CRITICAL
parse-
4
Node Traversal
HIGH
traverse-
5
Semantic Analysis
HIGH
semantic-
6
Edit Operations
MEDIUM-HIGH
edit-
7
Workflow Design
MEDIUM-HIGH
workflow-
8
Testing Strategy
MEDIUM
test-
9
State Management
MEDIUM
state-
10
Security and Capabilities
LOW-MEDIUM
security-
11
Package Structure
LOW
pkg-
Quick Reference
1. AST Understanding (CRITICAL)
ast-explore-before-writing - Use AST Explorer before writing patterns
ast-understand-named-vs-anonymous - Understand named vs anonymous nodes
ast-use-kind-for-precision - Use kind constraint for precision
ast-field-access-for-structure - Use field access for structural queries
ast-check-null-before-access - Check null before property access
2. Pattern Efficiency (CRITICAL)
pattern-use-meta-variables - Use meta variables for flexible matching
pattern-avoid-overly-generic - Avoid overly generic patterns
pattern-combine-with-rules - Combine patterns with rule operators
pattern-use-constraints - Use constraints for reusable matching logic
pattern-use-relational-patterns - Use relational patterns for context
pattern-ensure-idempotency - Ensure patterns are idempotent
3. Parsing Strategy (CRITICAL)
parse-select-correct-parser - Select the correct parser for file type
parse-handle-embedded-languages - Handle embedded languages with parseAsync
parse-provide-pattern-context - Provide context for ambiguous patterns
parse-early-return-non-applicable - Early return for non-applicable files
4. Node Traversal (HIGH)
traverse-use-find-vs-findall - Use find() for single match, findAll() for multiple
traverse-single-pass-collection - Collect multiple patterns in single traversal
traverse-use-stopby-for-depth - Use stopBy to control traversal depth
traverse-use-siblings-efficiently - Use sibling navigation efficiently
traverse-cache-repeated-lookups - Cache repeated node lookups
5. Semantic Analysis (HIGH)
semantic-use-file-scope-first - Use file scope semantic analysis first
semantic-check-null-results - Handle null semantic analysis results
semantic-verify-file-ownership - Verify file ownership before cross-file edits
semantic-cache-cross-file-results - Cache semantic analysis results
6. Edit Operations (MEDIUM-HIGH)
edit-batch-before-commit - Batch edits before committing
edit-preserve-formatting - Preserve surrounding formatting in edits
edit-handle-overlapping-ranges - Handle overlapping edit ranges
edit-use-flatmap-for-conditional - Use flatMap for conditional edits
edit-add-imports-correctly - Add imports at correct position
7. Workflow Design (MEDIUM-HIGH)
workflow-order-nodes-by-dependency - Order nodes by dependency
workflow-use-matrix-for-parallelism - Use matrix strategy for parallelism
workflow-use-manual-gates - Use manual gates for critical steps
workflow-validate-before-run - Validate workflows before running
workflow-use-conditional-steps - Use conditional steps for dynamic workflows
8. Testing Strategy (MEDIUM)
test-use-fixture-pairs - Use input/expected fixture pairs
test-cover-edge-cases - Cover edge cases in test fixtures
test-use-strictness-levels - Choose appropriate test strictness level
test-update-fixtures-intentionally - Update test fixtures intentionally
test-run-on-subset-first - Test on file subset before full run
9. State Management (MEDIUM)
state-use-for-resumability - Use state for resumable migrations
state-make-transforms-idempotent - Make transforms idempotent for safe reruns
state-log-progress-for-observability - Log progress for long-running migrations
10. Security and Capabilities (LOW-MEDIUM)
security-minimize-capabilities - Minimize requested capabilities
security-validate-external-inputs - Validate external inputs before use
security-review-before-running-third-party - Review third-party codemods before running
11. Package Structure (LOW)
pkg-use-semantic-versioning - Use semantic versioning for packages
pkg-write-descriptive-metadata - Write descriptive package metadata
pkg-organize-by-convention - Organize package 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.
1 --- 2 name: codemod 3 description: Codemod Best Practices 4 --- 5 # Codemod Best Practices 6 7 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. 8 9 ## When to Apply 10 11 Reference these guidelines when: 12 - Writing new codemods with JSSG or ast-grep 13 - Designing workflow configurations for migrations 14 - Debugging pattern matching or AST traversal issues 15 - Reviewing codemod code for performance and safety 16 - Setting up test fixtures for transform validation 17 18 ## Rule Categories by Priority 19 20 | Priority | Category | Impact | Prefix | 21 |----------|----------|--------|--------| 22 | 1 | AST Understanding | CRITICAL | `ast-` | 23 | 2 | Pattern Efficiency | CRITICAL | `pattern-` | 24 | 3 | Parsing Strategy | CRITICAL | `parse-` | 25 | 4 | Node Traversal | HIGH | `traverse-` | 26 | 5 | Semantic Analysis | HIGH | `semantic-` | 27 | 6 | Edit Operations | MEDIUM-HIGH | `edit-` | 28 | 7 | Workflow Design | MEDIUM-HIGH | `workflow-` | 29 | 8 | Testing Strategy | MEDIUM | `test-` | 30 | 9 | State Management | MEDIUM | `state-` | 31 | 10 | Security and Capabilities | LOW-MEDIUM | `security-` | 32 | 11 | Package Structure | LOW | `pkg-` | 33 34 ## Quick Reference 35 36 ### 1. AST Understanding (CRITICAL) 37 38 - [`ast-explore-before-writing`](references/ast-explore-before-writing.md) - Use AST Explorer before writing patterns 39 - [`ast-understand-named-vs-anonymous`](references/ast-understand-named-vs-anonymous.md) - Understand named vs anonymous nodes 40 - [`ast-use-kind-for-precision`](references/ast-use-kind-for-precision.md) - Use kind constraint for precision 41 - [`ast-field-access-for-structure`](references/ast-field-access-for-structure.md) - Use field access for structural queries 42 - [`ast-check-null-before-access`](references/ast-check-null-before-access.md) - Check null before property access 43 44 ### 2. Pattern Efficiency (CRITICAL) 45 46 - [`pattern-use-meta-variables`](references/pattern-use-meta-variables.md) - Use meta variables for flexible matching 47 - [`pattern-avoid-overly-generic`](references/pattern-avoid-overly-generic.md) - Avoid overly generic patterns 48 - [`pattern-combine-with-rules`](references/pattern-combine-with-rules.md) - Combine patterns with rule operators 49 - [`pattern-use-constraints`](references/pattern-use-constraints.md) - Use constraints for reusable matching logic 50 - [`pattern-use-relational-patterns`](references/pattern-use-relational-patterns.md) - Use relational patterns for context 51 - [`pattern-ensure-idempotency`](references/pattern-ensure-idempotency.md) - Ensure patterns are idempotent 52 53 ### 3. Parsing Strategy (CRITICAL) 54 55 - [`parse-select-correct-parser`](references/parse-select-correct-parser.md) - Select the correct parser for file type 56 - [`parse-handle-embedded-languages`](references/parse-handle-embedded-languages.md) - Handle embedded languages with parseAsync 57 - [`parse-provide-pattern-context`](references/parse-provide-pattern-context.md) - Provide context for ambiguous patterns 58 - [`parse-early-return-non-applicable`](references/parse-early-return-non-applicable.md) - Early return for non-applicable files 59 60 ### 4. Node Traversal (HIGH) 61 62 - [`traverse-use-find-vs-findall`](references/traverse-use-find-vs-findall.md) - Use find() for single match, findAll() for multiple 63 - [`traverse-single-pass-collection`](references/traverse-single-pass-collection.md) - Collect multiple patterns in single traversal 64 - [`traverse-use-stopby-for-depth`](references/traverse-use-stopby-for-depth.md) - Use stopBy to control traversal depth 65 - [`traverse-use-siblings-efficiently`](references/traverse-use-siblings-efficiently.md) - Use sibling navigation efficiently 66 - [`traverse-cache-repeated-lookups`](references/traverse-cache-repeated-lookups.md) - Cache repeated node lookups 67 68 ### 5. Semantic Analysis (HIGH) 69 70 - [`semantic-use-file-scope-first`](references/semantic-use-file-scope-first.md) - Use file scope semantic analysis first 71 - [`semantic-check-null-results`](references/semantic-check-null-results.md) - Handle null semantic analysis results 72 - [`semantic-verify-file-ownership`](references/semantic-verify-file-ownership.md) - Verify file ownership before cross-file edits 73 - [`semantic-cache-cross-file-results`](references/semantic-cache-cross-file-results.md) - Cache semantic analysis results 74 75 ### 6. Edit Operations (MEDIUM-HIGH) 76 77 - [`edit-batch-before-commit`](references/edit-batch-before-commit.md) - Batch edits before committing 78 - [`edit-preserve-formatting`](references/edit-preserve-formatting.md) - Preserve surrounding formatting in edits 79 - [`edit-handle-overlapping-ranges`](references/edit-handle-overlapping-ranges.md) - Handle overlapping edit ranges 80 - [`edit-use-flatmap-for-conditional`](references/edit-use-flatmap-for-conditional.md) - Use flatMap for conditional edits 81 - [`edit-add-imports-correctly`](references/edit-add-imports-correctly.md) - Add imports at correct position 82 83 ### 7. Workflow Design (MEDIUM-HIGH) 84 85 - [`workflow-order-nodes-by-dependency`](references/workflow-order-nodes-by-dependency.md) - Order nodes by dependency 86 - [`workflow-use-matrix-for-parallelism`](references/workflow-use-matrix-for-parallelism.md) - Use matrix strategy for parallelism 87 - [`workflow-use-manual-gates`](references/workflow-use-manual-gates.md) - Use manual gates for critical steps 88 - [`workflow-validate-before-run`](references/workflow-validate-before-run.md) - Validate workflows before running 89 - [`workflow-use-conditional-steps`](references/workflow-use-conditional-steps.md) - Use conditional steps for dynamic workflows 90 91 ### 8. Testing Strategy (MEDIUM) 92 93 - [`test-use-fixture-pairs`](references/test-use-fixture-pairs.md) - Use input/expected fixture pairs 94 - [`test-cover-edge-cases`](references/test-cover-edge-cases.md) - Cover edge cases in test fixtures 95 - [`test-use-strictness-levels`](references/test-use-strictness-levels.md) - Choose appropriate test strictness level 96 - [`test-update-fixtures-intentionally`](references/test-update-fixtures-intentionally.md) - Update test fixtures intentionally 97 - [`test-run-on-subset-first`](references/test-run-on-subset-first.md) - Test on file subset before full run 98 99 ### 9. State Management (MEDIUM) 100 101 - [`state-use-for-resumability`](references/state-use-for-resumability.md) - Use state for resumable migrations 102 - [`state-make-transforms-idempotent`](references/state-make-transforms-idempotent.md) - Make transforms idempotent for safe reruns 103 - [`state-log-progress-for-observability`](references/state-log-progress-for-observability.md) - Log progress for long-running migrations 104 105 ### 10. Security and Capabilities (LOW-MEDIUM) 106 107 - [`security-minimize-capabilities`](references/security-minimize-capabilities.md) - Minimize requested capabilities 108 - [`security-validate-external-inputs`](references/security-validate-external-inputs.md) - Validate external inputs before use 109 - [`security-review-before-running-third-party`](references/security-review-before-running-third-party.md) - Review third-party codemods before running 110 111 ### 11. Package Structure (LOW) 112 113 - [`pkg-use-semantic-versioning`](references/pkg-use-semantic-versioning.md) - Use semantic versioning for packages 114 - [`pkg-write-descriptive-metadata`](references/pkg-write-descriptive-metadata.md) - Write descriptive package metadata 115 - [`pkg-organize-by-convention`](references/pkg-organize-by-convention.md) - Organize package by convention 116 117 ## How to Use 118 119 Read individual reference files for detailed explanations and code examples: 120 121 - [Section definitions](references/_sections.md) - Category structure and impact levels 122 - [Rule template](assets/templates/_template.md) - Template for adding new rules 123 124 ## Full Compiled Document 125 126 For a complete guide with all rules expanded, see [AGENTS.md](AGENTS.md).
ComeOnOliver/skillshub/tree/main/skills/pproenca/dot-skills/codemod commit 4c56bf4501
Frequently asked questions How do I install the Codemod skill? Run npx skillmds@latest add comeonoliver/codemod in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
What does the Codemod skill do? Codemod Best Practices It is listed under Coding & Dev Tools on SkillMD.
Is Codemod safe to use? This skill has not completed SkillMD's automated safety review yet. Independent scanners report: SkillSpector: PASS, Skill Scanner: PASS. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
Which AI agents work with Codemod? This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Is Codemod free to use? Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Who published Codemod? ComeOnOliver (@comeonoliver) published this skill. Their other Agent Skills are listed on their SkillMD profile.