Codemod Studio allows you to create, analyze, and improve code automation bots with the help of Codemod AI. Codemod AI is an innovative tool designed to streamline the process of creating codemods using before and after examples.

By leveraging advanced AI technologies and deterministic software engineering tools, Codemod AI significantly improves the accuracy and efficiency of autogenerated codemods.

This guide shows you how to build codemods using Codemod Studio. During this process, you will learn different concepts such as:

Adding/Inspecting test cases

Test cases allow you to see how your codemod affects different code patterns. In Codemod Studio, you can:

  • Add test cases
  • Inspect a test case’s AST tree
  • Inspect the diff view of how your codemod affects the test case
  • Inspect the matched code patterns

Selecting language

Under the hood, Codemod Studio uses ast-grep, a fast and polyglot tool for code structural search, lint, and rewriting at large-scale. ast-grep allows Codemod Studio to support building and transforming codemods with many languages.

Sharing codemods

After building a codemod, you can share and test your codemods with friends, team members, and the community.

Managing sessions

Codemod Studio allows you to save and use multiple sessions. This helps make working on several codemods at once more manageable.

Using Codemod AI

The AI assistant provides support and guidance throughout the codemod creation process. It leverages AI to help you generate codemod scripts, troubleshoot issues, and refine code transformations.

To use Codemod AI, you can:

1

Open Codemod AI

Click Ask Codemod AI to open the AI helper pane.

2

Prompt required changes

Prompt the change(s) for which you want to build a codemod. Try to focus on building codemods for a few granular changes at a time. Packing too many changes into one codemod significantly increases the chance of broken outputs.

3

Inspect test cases

Codemod AI will understand your prompt and recommend extra test cases that may improve coverage. You can inspect the generated test cases and make sure that the codemod you built transforms these test cases correctly.

4

Iterate and improve

If the codemod is not perfect on the first try, you can iterate and improve the codemod with the help of Codemod AI until you reach a satisfactory result.

Running codemods

Codemod Studio allows you to run codemods over your codebases without having to leave your web environment.

To run codemods, you can:

1

Choose target repository

Make sure you are logged into Codemod using your GitHub account and pick a target repository.

2

Run codemod

Click Run to start running your codemod. Note that this will only dry-run the codemod and will not directly affect your codebase.

3

Inspect changes

You can click on each affected file and inspect the diff view of the changes made. If you catch any false positives or false negatives, you can go back to Codemod AI and iteratively improve your codemod.

Tips for building better codemods

  1. Clarity is Key: Try to prompt clear requirements when using Codemod AI. Ensure that the transformation logic is clear for both humans and AI. Well-defined logic leads to more accurate codemods.

  2. Follow the MECE Principle: When designing test cases, aim for them to be Mutually Exclusive and Collectively Exhaustive (MECE). This means:

    1. Mutually Exclusive: Each test case should cover a unique scenario without overlap. Ideally, you only need one clear example with a proper description for each pattern. Break down complex transformations into smaller, more manageable parts.

    • Collectively Exhaustive: Together, your test cases should cover all possible scenarios for the codemod.

    For example, if you’re writing a codemod to update function syntax:

    • Test case 1: Regular function to arrow function
    • Test case 2: Function with parameters
    • Test case 3: Function with default parameters
    • Test case 4: Async function

    Each case covers a distinct scenario, and together they cover a wide range of function types.

  3. Balance Simplicity and Complexity: While it’s possible to create complex transformation logics that support all cases, simpler, more focused codemods are often more maintainable and easier to understand.

Was this page helpful?