Automated code migrations with OpenAI's GPT-4
In the day-to-day world of development, activities such as refactoring and migrating code consume a significant amount of time. Tasks such as restructuring code packages, renaming variables, altering function signatures, among others, are often tedious and fraught with errors. These operations can be effectively automated with the assistance of artificial intelligence. This article explains how to use OpenAI's GPT-4 to automatically refactor and migrate code.
At FireCMS, a prime motivation for adopting this approach is the migration of code from the JavaScript CSS library
emotion
to the CSS framework tailwindcss
. This article specifically covers how to automatically convert code from
emotion
to tailwindcss
, but the approach can be generalized to automate migration among any pair of libraries.
emotion
allows components to be styled using JavaScript andtailwindcss
offers the use of CSS classes to style components. Whileemotion
carries a runtime footprint,tailwindcss
works on a build-time footprint. Consequently, switching fromemotion
totailwindcss
can substantially improve the performance of an application.
This article demonstrates the creation of a basic Node.js script to automate the above-mentioned process, thereby extending the approach to handle any code migration scenario.
Note that you can apply the same approach to any other code migration.
Identifying the Problem
- Emotion defines styles utilizing the
sx
prop, as shown in the code snippet below, which presents a button with a red background:
import {Button} from "@mui/material";
const MyButton = () => {
return <Button sx={{backgroundColor: "red"}}>Click me</Button>
}
- Contrarily, tailwindcss uses CSS classes to define styles, as shown in the following code snippet, which defines a similarly-styled button:
import {Button} from "@mui/material";
const MyButton = () => {
return <Button className="bg-red-500">Click me</Button>
}
The goal is to construct a script capable of automating the conversion of code from emotion
to tailwindcss
style
definitions.
Automating Code Migration through AI
The Node.js script illustrated here traverses the source code's folder structure and replaces emotion
styles with
tailwindcss
styles. This process involves identifying JSX ot TSX elements containing the sx
prop and replacing them with
their corresponding className
prop.
Traversing the Code
Here's a simple function that recursively traverses the source code's folder structure, returning all file paths and their contents:
const getRecursiveFileReads = async (dir: string, onFileRead: (filePath: string, content: string) => void) => {
const files = await fs.readdir(dir);
for (const file of files) {
const filePath = path.join(dir, file);
const stat = await fs.stat(filePath);
if (stat.isDirectory()) {
getRecursiveFileReads(filePath, onFileRead);
continue;
}
const content = await fs.readFile(filePath, { encoding: "utf8" });
onFileRead(filePath, content);
}
}
Detecting Emotion Styles
To identify sx
props within the file content, we use a regular expression as shown:
const sxRegexp = /(sx=\{(?:[^{}]|\{(?:[^{}]|\{(?:[^{}]|\{[^{}]*\})*\})*\})*\})/g;
This regular expression matches the sx
prop as well as its value and can identify the styles defined in sx
as shown
in the earlier sections.