3 min read

Exploring the world of AI-powered code mods with Intuita

Exploring the world of AI-powered code mods with Intuita

In a past job life, a lot of the codebase I had been working in was written in Coffeescript. There was a strong desire to move towards Typescript, but the transition was slow because it was done on the initiative of various engineers or whenever it was possible to incorporate the work as part of building a feature.

Anyone who has had to do a language migration in a codebase can attest to how tedious, frustrating, and slow this process can be. Back then, a senior engineer on my team introduced some tools to speed up the process (such as ts-migrate), but we still needed to create code mods to help automate the rest of the migration steps.

Fortunately for us today, we're starting to see the development of AI-powered tools that can help generate these code mods for us, and I thought I'd give one of these (Intuita) a try.

Intuita

This one was intriguing to me because it generates a code mod based on a git diff.

To test this, I made a simple example in CommonJS:

const math = require("./math");

const add = (a, b) => a + b;
const subtract = (a, b) => a - b;

module.exports = {
	add,
	subtract
};

and converted it to ESM as the initial commit in my project:

import math from "./math";

export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;

The first problem I ran into after running intuita learn (the command that generates the codemod) was that I couldn't generate the codemod without having an account with Intuita.

When you run the command, the CLI opens to a web IDE and while the information about needing an account to use the IDE exists in the documentation, from the screen itself it wasn't obvious.

After creating a repository and re-running intuita learn, I then encountered an error about not having a default export in the altered code despite it being valid Javascript.

Fortunately, because there's not much to my example I assumed it was because I was missing an explicit default export. After updating the ESM code to the following:

import math from "./math";

export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;

export default { add, subtract };

there were no errors from Intuita and a code mod was successfully generated!

While most of the output looked good, the import at the top of the file wasn't quite what I was expecting:

const math = import ./math from "./math";;

export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;

export default {
	add,
	subtract
};;

I ended up stopping there in my experiment with the tool but one thing I want to explore further at another time is the possibility of using a branch with multiple commits on it to create a code mod rather than uncommitted changes in my local environment.

Despite the challenges I ran into, it provides a good starting point for a custom code mod and it's something I wish I had a few years ago. I'm sure the tooling will continue to improve over time, and at least now I'll be prepared the next time I have to go through a similar process.


📫
Enjoy this post? Subscribe to be notified when I publish new content!

Updates (Nov. 11 2023):

In my initial post, I wrote about how intuita learn didn't appear to use the diff from my local environment and instead used a diff between your local changes and a remote repository:

The other part that I didn't immediately realize - it doesn't appear to use the diff from your local environment. It uses a diff between your local changes and a remote repository (which I presume is why it makes you create an Intuita account with Github SSO).

It turns out that I made a mistake and redoing the following steps:

  • git init;
  • commit CJS code;
  • change CJS code to ESM, but don't commit it;
  • Run intuita learn

results in a codemod being successfully generated. As a result, I've updated the original post to remove the paragraph mentioned above.

Some extra notes I wanted to mention that came up in a Twitter thread with Alex, the founder and CEO at Intuita:

  • The need for folks to create an account is due to usage limits.
  • The Intuita team quickly made changes based on my comment that it wasn't obvious an account was needed when intuita learn is run on the CLI and the web IDE opens. With the new changes, when the web IDE is opened from the CLI and you aren't signed in, you are now prompted to either sign in or create an account.