Refactoring with Codemods to Automate API Adjustments

As a library developer, chances are you’ll create a well-liked utility that a whole lot of
1000’s of builders depend on day by day, comparable to lodash or React. Over time,
utilization patterns may emerge that transcend your preliminary design. When this
occurs, chances are you’ll want to increase an API by including parameters or modifying
perform signatures to repair edge circumstances. The problem lies in rolling out
these breaking adjustments with out disrupting your customers’ workflows.

That is the place codemods are available in—a robust device for automating
large-scale code transformations, permitting builders to introduce breaking
API adjustments, refactor legacy codebases, and preserve code hygiene with
minimal handbook effort.

On this article, we’ll discover what codemods are and the instruments you’ll be able to
use to create them, comparable to jscodeshift, hypermod.io, and codemod.com. We’ll stroll by real-world examples,
from cleansing up characteristic toggles to refactoring part hierarchies.
You’ll additionally discover ways to break down complicated transformations into smaller,
testable items—a apply referred to as codemod composition—to make sure
flexibility and maintainability.

By the top, you’ll see how codemods can grow to be an important a part of your
toolkit for managing large-scale codebases, serving to you retain your code clear
and maintainable whereas dealing with even probably the most difficult refactoring
duties.

Breaking Adjustments in APIs

Returning to the state of affairs of the library developer, after the preliminary
launch, new utilization patterns emerge, prompting the necessity to prolong an
API—maybe by including a parameter or modifying a perform signature to
make it simpler to make use of.

For easy adjustments, a primary find-and-replace within the IDE may work. In
extra complicated circumstances, you may resort to utilizing instruments like sed
or awk. Nevertheless, when your library is broadly adopted, the
scope of such adjustments turns into more durable to handle. You possibly can’t make sure how
extensively the modification will affect your customers, and the very last thing
you need is to interrupt current performance that doesn’t want
updating.

A standard method is to announce the breaking change, launch a brand new
model, and ask customers emigrate at their very own tempo. However this workflow,
whereas acquainted, usually would not scale properly, particularly for main shifts.
Contemplate React’s transition from class elements to perform elements
with hooks—a paradigm shift that took years for giant codebases to totally
undertake. By the point groups managed emigrate, extra breaking adjustments have been
usually already on the horizon.

For library builders, this case creates a burden. Sustaining
a number of older variations to help customers who haven’t migrated is each
expensive and time-consuming. For customers, frequent adjustments danger eroding belief.
They could hesitate to improve or begin exploring extra secure options,
which perpetuating the cycle.

However what in case you might assist customers handle these adjustments routinely?
What in case you might launch a device alongside your replace that refactors
their code for them—renaming features, updating parameter order, and
eradicating unused code with out requiring handbook intervention?

That’s the place codemods are available in. A number of libraries, together with React
and Subsequent.js, have already embraced codemods to easy the trail for model
bumps. For instance, React gives codemods to deal with the migration from
older API patterns, just like the previous Context API, to newer ones.

So, what precisely is the codemod we’re speaking about right here?

What’s a Codemod?

A codemod (code modification) is an automatic script used to remodel
code to comply with new APIs, syntax, or coding requirements. Codemods use
Summary Syntax Tree (AST) manipulation to use constant, large-scale
adjustments throughout codebases. Initially developed at Fb, codemods helped
engineers handle refactoring duties for giant tasks like React. As
Fb scaled, sustaining the codebase and updating APIs turned
more and more troublesome, prompting the event of codemods.

Manually updating 1000’s of information throughout completely different repositories was
inefficient and error-prone, so the idea of codemods—automated scripts
that remodel code—was launched to sort out this downside.

The method usually entails three fundamental steps:

  1. Parsing the code into an AST, the place every a part of the code is
    represented as a tree construction.
  2. Modifying the tree by making use of a metamorphosis, comparable to renaming a
    perform or altering parameters.
  3. Rewriting the modified tree again into the supply code.

Through the use of this method, codemods be certain that adjustments are utilized
constantly throughout each file in a codebase, decreasing the prospect of human
error. Codemods also can deal with complicated refactoring eventualities, comparable to
adjustments to deeply nested constructions or eradicating deprecated API utilization.

If we visualize the method, it might look one thing like this:

Determine 1: The three steps of a typical codemod course of

The concept of a program that may “perceive” your code after which carry out
automated transformations isn’t new. That’s how your IDE works whenever you
run refactorings like Extract Function, Rename Variable, or Inline Function.
Primarily, your IDE parses the supply code into ASTs and applies
predefined transformations to the tree, saving the end result again into your
information.

For contemporary IDEs, many issues occur beneath the hood to make sure adjustments
are utilized accurately and effectively, comparable to figuring out the scope of
the change and resolving conflicts like variable title collisions. Some
refactorings even immediate you to enter parameters, comparable to when utilizing
Change Function Declaration, the place you’ll be able to alter the
order of parameters or default values earlier than finalizing the change.

Use jscodeshift in JavaScript Codebases

Let’s have a look at a concrete instance to know how we might run a
codemod in a JavaScript undertaking. The JavaScript group has a number of
instruments that make this work possible, together with parsers that convert supply
code into an AST, in addition to transpilers that may remodel the tree into
different codecs (that is how TypeScript works). Moreover, there are
instruments that assist apply codemods to complete repositories routinely.

Some of the in style instruments for writing codemods is jscodeshift, a toolkit maintained by Fb.
It simplifies the creation of codemods by offering a robust API to
manipulate ASTs. With jscodeshift, builders can seek for particular
patterns within the code and apply transformations at scale.

You should utilize jscodeshift to determine and exchange deprecated API calls
with up to date variations throughout a whole undertaking.

Let’s break down a typical workflow for composing a codemod
manually.

Clear a Stale Function Toggle

Let’s begin with a easy but sensible instance to exhibit the
energy of codemods. Think about you’re utilizing a characteristic
toggle in your
codebase to manage the discharge of unfinished or experimental options.
As soon as the characteristic is dwell in manufacturing and dealing as anticipated, the subsequent
logical step is to scrub up the toggle and any associated logic.

As an example, think about the next code:

const information = featureToggle('feature-new-product-list') ? { title: 'Product' } : undefined;

As soon as the characteristic is totally launched and now not wants a toggle, this
may be simplified to:

const information = { title: 'Product' };

The duty entails discovering all cases of featureToggle within the
codebase, checking whether or not the toggle refers to
feature-new-product-list, and eradicating the conditional logic surrounding
it. On the similar time, different characteristic toggles (like
feature-search-result-refinement, which can nonetheless be in improvement)
ought to stay untouched. The codemod must perceive the construction
of the code to use adjustments selectively.

Understanding the AST

Earlier than we dive into writing the codemod, let’s break down how this
particular code snippet seems in an AST. You should utilize instruments like AST
Explorer
to visualise how supply code and AST
are mapped. It’s useful to know the node sorts you are interacting
with earlier than making use of any adjustments.

The picture under exhibits the syntax tree when it comes to ECMAScript syntax. It
accommodates nodes like Identifier (for variables), StringLiteral (for the
toggle title), and extra summary nodes like CallExpression and
ConditionalExpression.

Determine 2: The Summary Syntax Tree illustration of the characteristic toggle test

On this AST illustration, the variable information is assigned utilizing a
ConditionalExpression. The check a part of the expression calls
featureToggle('feature-new-product-list'). If the check returns true,
the consequent department assigns { title: 'Product' } to information. If
false, the alternate department assigns undefined.

For a process with clear enter and output, I want writing assessments first,
then implementing the codemod. I begin by defining a damaging case to
guarantee we don’t unintentionally change issues we wish to depart untouched,
adopted by an actual case that performs the precise conversion. I start with
a easy state of affairs, implement it, then add a variation (like checking if
featureToggle known as inside an if assertion), implement that case, and
guarantee all assessments move.

This method aligns properly with Check-Pushed Growth (TDD), even
in case you don’t apply TDD often. Understanding precisely what the
transformation’s inputs and outputs are earlier than coding improves security and
effectivity, particularly when tweaking codemods.

With jscodeshift, you’ll be able to write assessments to confirm how the codemod
behaves:

const remodel = require("../remove-feature-new-product-list");

defineInlineTest(
  remodel,
  {},
  `
  const information = featureToggle('feature-new-product-list') ? { title: 'Product' } : undefined;
  `,
  `
  const information = { title: 'Product' };
  `,
  "delete the toggle feature-new-product-list in conditional operator"
);

The defineInlineTest perform from jscodeshift means that you can outline
the enter, anticipated output, and a string describing the check’s intent.
Now, operating the check with a traditional jest command will fail as a result of the
codemod isn’t written but.

The corresponding damaging case would make sure the code stays unchanged
for different characteristic toggles:

defineInlineTest(
  remodel,
  {},
  `
  const information = featureToggle('feature-search-result-refinement') ? { title: 'Product' } : undefined;
  `,
  `
  const information = featureToggle('feature-search-result-refinement') ? { title: 'Product' } : undefined;
  `,
  "don't change different characteristic toggles"
);

Writing the Codemod

Let’s begin by defining a easy remodel perform. Create a file
referred to as remodel.js with the next code construction:

module.exports = perform(fileInfo, api, choices) {
  const j = api.jscodeshift;
  const root = j(fileInfo.supply);

  // manipulate the tree nodes right here

  return root.toSource();
};

This perform reads the file right into a tree and makes use of jscodeshift’s API to
question, modify, and replace the nodes. Lastly, it converts the AST again to
supply code with .toSource().

Now we are able to begin implementing the remodel steps:

  1. Discover all cases of featureToggle.
  2. Confirm that the argument handed is 'feature-new-product-list'.
  3. Substitute your entire conditional expression with the consequent half,
    successfully eradicating the toggle.

Right here’s how we obtain this utilizing jscodeshift:

module.exports = perform (fileInfo, api, choices) {
  const j = api.jscodeshift;
  const root = j(fileInfo.supply);

  // Discover ConditionalExpression the place the check is featureToggle('feature-new-product-list')
  root
    .discover(j.ConditionalExpression, {
      check: {
        callee: { title: "featureToggle" },
        arguments: [{ value: "feature-new-product-list" }],
      },
    })
    .forEach((path) => {
      // Substitute the ConditionalExpression with the 'consequent'
      j(path).replaceWith(path.node.consequent);
    });

  return root.toSource();
};

The codemod above:

  • Finds ConditionalExpression nodes the place the check calls
    featureToggle('feature-new-product-list').
  • Replaces your entire conditional expression with the ensuing (i.e., {
    title: 'Product' }
    ), eradicating the toggle logic and leaving simplified code
    behind.

This instance demonstrates how straightforward it’s to create a helpful
transformation and apply it to a big codebase, considerably decreasing
handbook effort.

You’ll want to write down extra check circumstances to deal with variations like
if-else statements, logical expressions (e.g.,
!featureToggle('feature-new-product-list')), and so forth to make the
codemod sturdy in real-world eventualities.

As soon as the codemod is prepared, you’ll be able to try it out on a goal codebase,
such because the one you are engaged on. jscodeshift gives a command-line
device that you should utilize to use the codemod and report the outcomes.

$ jscodeshift -t transform-name src/

After validating the outcomes, test that every one practical assessments nonetheless
move and that nothing breaks—even in case you’re introducing a breaking change.
As soon as happy, you’ll be able to commit the adjustments and lift a pull request as
a part of your regular workflow.

Codemods Enhance Code High quality and Maintainability

Codemods aren’t simply helpful for managing breaking API adjustments—they’ll
considerably enhance code high quality and maintainability. As codebases
evolve, they usually accumulate technical debt, together with outdated characteristic
toggles, deprecated strategies, or tightly coupled elements. Manually
refactoring these areas may be time-consuming and error-prone.

By automating refactoring duties, codemods assist hold your codebase clear
and freed from legacy patterns. Usually making use of codemods means that you can
implement new coding requirements, take away unused code, and modernize your
codebase with out having to manually modify each file.

Refactoring an Avatar Part

Now, let’s have a look at a extra complicated instance. Suppose you’re working with
a design system that features an Avatar part tightly coupled with a
Tooltip. Each time a consumer passes a title prop into the Avatar, it
routinely wraps the avatar with a tooltip.

Determine 3: A avatar part with a tooltip

Right here’s the present Avatar implementation:

import { Tooltip } from "@design-system/tooltip";

const Avatar = ({ title, picture }: AvatarProps) => {
  if (title) {
    return (
      <Tooltip content material={title}>
        <CircleImage picture={picture} />
      </Tooltip>
    );
  }

  return <CircleImage picture={picture} />;
};

The purpose is to decouple the Tooltip from the Avatar part,
giving builders extra flexibility. Builders ought to be capable to resolve
whether or not to wrap the Avatar in a Tooltip. Within the refactored model,
Avatar will merely render the picture, and customers can apply a Tooltip
manually if wanted.

Right here’s the refactored model of Avatar:

const Avatar = ({ picture }: AvatarProps) => {
  return <CircleImage picture={picture} />;
};

Now, customers can manually wrap the Avatar with a Tooltip as
wanted:

import { Tooltip } from "@design-system/tooltip";
import { Avatar } from "@design-system/avatar";

const UserProfile = () => {
  return (
    <Tooltip content material="Juntao Qiu">
      <Avatar picture="/juntao.qiu.avatar.png" />
    </Tooltip>
  );
};

The problem arises when there are a whole lot of Avatar usages unfold
throughout the codebase. Manually refactoring every occasion could be extremely
inefficient, so we are able to use a codemod to automate this course of.

Utilizing instruments like AST Explorer, we are able to
examine the part and see which nodes symbolize the Avatar utilization
we’re focusing on. An Avatar part with each title and picture props
is parsed into an summary syntax tree as proven under:

Determine 4: AST of the Avatar part utilization

Writing the Codemod

Let’s break down the transformation into smaller duties:

  • Discover Avatar utilization within the part tree.
  • Verify if the title prop is current.
    • If not, do nothing.
    • If current:
      • Create a Tooltip node.
      • Add the title to the Tooltip.
      • Take away the title from Avatar.
      • Add Avatar as a toddler of the Tooltip.
      • Substitute the unique Avatar node with the brand new Tooltip.

To start, we’ll discover all cases of Avatar (I’ll omit a number of the
assessments, however you need to write comparability assessments first).

defineInlineTest(
    { default: remodel, parser: "tsx" },
    {},
    `
    <Avatar title="Juntao Qiu" picture="/juntao.qiu.avatar.png" />
    `,
    `
    <Tooltip content material="Juntao Qiu">
      <Avatar picture="/juntao.qiu.avatar.png" />
    </Tooltip>
    `,
    "wrap avatar with tooltip when title is supplied"
  );

Just like the featureToggle instance, we are able to use root.discover with
search standards to find all Avatar nodes:

root
  .discover(j.JSXElement, {
    openingElement: { title: { title: "Avatar" } },
  })
  .forEach((path) => {
    // now we are able to deal with every Avatar occasion
  });

Subsequent, we test if the title prop is current:

root
  .discover(j.JSXElement, {
    openingElement: { title: { title: "Avatar" } },
  })
  .forEach((path) => {
    const avatarNode = path.node;

    const nameAttr = avatarNode.openingElement.attributes.discover(
      (attr) => attr.title.title === "title"
    );

    if (nameAttr) {
      const tooltipElement = createTooltipElement(
        nameAttr.worth.worth,
        avatarNode
      );
      j(path).replaceWith(tooltipElement);
    }
  });

For the createTooltipElement perform, we use the
jscodeshift API to create a brand new JSX node, with the title
prop utilized to the Tooltip and the Avatar
part as a toddler. Lastly, we name replaceWith to
exchange the present path.

Right here’s a preview of the way it seems in
Hypermod, the place the codemod is written on
the left. The highest half on the fitting is the unique code, and the underside
half is the reworked end result:

Determine 5: Run checks inside hypermod earlier than apply it to your codebase

This codemod searches for all cases of Avatar. If a
title prop is discovered, it removes the title prop
from Avatar, wraps the Avatar inside a
Tooltip, and passes the title prop to the
Tooltip.

By now, I hope it’s clear that codemods are extremely helpful and
that the workflow is intuitive, particularly for large-scale adjustments the place
handbook updates could be an enormous burden. Nevertheless, that is not the entire
image. Within the subsequent part, I’ll make clear a number of the challenges
and the way we are able to tackle these less-than-ideal elements.

Fixing Widespread Pitfalls of Codemods

As a seasoned developer, you already know the “completely happy path” is just a small half
of the total image. There are quite a few eventualities to contemplate when writing
a metamorphosis script to deal with code routinely.

Builders write code in quite a lot of kinds. For instance, somebody
may import the Avatar part however give it a unique title as a result of
they may have one other Avatar part from a unique bundle:

import { Avatar as AKAvatar } from "@design-system/avatar";

const UserInfo = () => (
  <AKAvatar title="Juntao Qiu" picture="/juntao.qiu.avatar.png" />
);

A easy textual content seek for Avatar received’t work on this case. You’ll want
to detect the alias and apply the transformation utilizing the right
title.

One other instance arises when coping with Tooltip imports. If the file
already imports Tooltip however makes use of an alias, the codemod should detect that
alias and apply the adjustments accordingly. You possibly can’t assume that the
part named Tooltip is at all times the one you’re searching for.

Within the feature toggle example, somebody may use
if(featureToggle('feature-new-product-list')), or assign the results of
the toggle perform to a variable earlier than utilizing it:

const shouldEnableNewFeature = featureToggle('feature-new-product-list');

if (shouldEnableNewFeature) {
  //...
}

They could even use the toggle with different situations or apply logical
negation, making the logic extra complicated:

const shouldEnableNewFeature = featureToggle('feature-new-product-list');

if (!shouldEnableNewFeature && someOtherLogic) {
  //...
}

These variations make it troublesome to foresee each edge case,
growing the chance of unintentionally breaking one thing. Relying solely
on the circumstances you’ll be able to anticipate just isn’t sufficient. You want thorough testing
to keep away from breaking unintended elements of the code.

Leveraging Supply Graphs and Check-Pushed Codemods

To deal with these complexities, codemods needs to be used alongside different
strategies. As an example, a couple of years in the past, I participated in a design
system elements rewrite undertaking at Atlassian. We addressed this difficulty by
first looking out the supply graph, which contained nearly all of inside
part utilization. This allowed us to know how elements have been used,
whether or not they have been imported beneath completely different names, or whether or not sure
public props have been continuously used. After this search part, we wrote our
check circumstances upfront, guaranteeing we lined nearly all of use circumstances, and
then developed the codemod.

In conditions the place we could not confidently automate the improve, we
inserted feedback or “TODOs” on the name websites. This allowed the
builders operating the script to deal with particular circumstances manually. Often,
there have been solely a handful of such cases, so this method nonetheless proved
useful for upgrading variations.

Using Present Code Standardization Instruments

As you’ll be able to see, there are many edge circumstances to deal with, particularly in
codebases past your management—comparable to exterior dependencies. This
complexity signifies that utilizing codemods requires cautious supervision and a
overview of the outcomes.

Nevertheless, in case your codebase has standardization instruments in place, comparable to a
linter that enforces a specific coding type, you’ll be able to leverage these
instruments to cut back edge circumstances. By implementing a constant construction, instruments
like linters assist slender down the variations in code, making the
transformation simpler and minimizing sudden points.

As an example, you may use linting guidelines to limit sure patterns,
comparable to avoiding nested conditional (ternary) operators or implementing named
exports over default exports. These guidelines assist streamline the codebase,
making codemods extra predictable and efficient.

Moreover, breaking down complicated transformations into smaller, extra
manageable ones means that you can sort out particular person points extra exactly. As
we’ll quickly see, composing smaller codemods could make dealing with complicated
adjustments extra possible.

Codemod Composition

Let’s revisit the characteristic toggle removing instance mentioned earlier. Within the code snippet
we’ve got a toggle referred to as feature-convert-new must be eliminated:

import { featureToggle } from "./utils/featureToggle";

const convertOld = (enter: string) => {
  return enter.toLowerCase();
};

const convertNew = (enter: string) => {
  return enter.toUpperCase();
};

const end result = featureToggle("feature-convert-new")
  ? convertNew("Good day, world")
  : convertOld("Good day, world");

console.log(end result);

The codemod for take away a given toggle works fantastic, and after operating the codemod,
we would like the supply to appear like this:

const convertNew = (enter: string) => {
  return enter.toUpperCase();
};

const end result = convertNew("Good day, world");

console.log(end result);

Nevertheless, past eradicating the characteristic toggle logic, there are extra duties to
deal with:

  • Take away the unused convertOld perform.
  • Clear up the unused featureToggle import.

In fact, you may write one large codemod to deal with every little thing in a
single move and check it collectively. Nevertheless, a extra maintainable method is
to deal with codemod logic like product code: break the duty into smaller,
unbiased items—identical to how you’d usually refactor manufacturing
code.

Breaking It Down

We will break the large transformation down into smaller codemods and
compose them. The benefit of this method is that every transformation
may be examined individually, masking completely different circumstances with out interference.
Furthermore, it means that you can reuse and compose them for various
functions.

As an example, you may break it down like this:

  • A metamorphosis to take away a particular characteristic toggle.
  • One other transformation to scrub up unused imports.
  • A metamorphosis to take away unused perform declarations.

By composing these, you’ll be able to create a pipeline of transformations:

import { removeFeatureToggle } from "./remove-feature-toggle";
import { removeUnusedImport } from "./remove-unused-import";
import { removeUnusedFunction } from "./remove-unused-function";

import { createTransformer } from "./utils";

const removeFeatureConvertNew = removeFeatureToggle("feature-convert-new");

const remodel = createTransformer([
  removeFeatureConvertNew,
  removeUnusedImport,
  removeUnusedFunction,
]);

export default remodel;

On this pipeline, the transformations work as follows:

  1. Take away the feature-convert-new toggle.
  2. Clear up the unused import assertion.
  3. Take away the convertOld perform because it’s now not used.

Determine 6: Compose transforms into a brand new remodel

You can too extract extra codemods as wanted, combining them in
varied orders relying on the specified final result.

Determine 7: Put completely different transforms right into a pipepline to type one other remodel

The createTransformer Perform

The implementation of the createTransformer perform is comparatively
simple. It acts as a higher-order perform that takes an inventory of
smaller remodel features, iterates by the checklist to use them to
the basis AST, and at last converts the modified AST again into supply
code.

import { API, Assortment, FileInfo, JSCodeshift, Choices } from "jscodeshift";

kind TransformFunction = { (j: JSCodeshift, root: Assortment): void };

const createTransformer =
  (transforms: TransformFunction[]) =>
  (fileInfo: FileInfo, api: API, choices: Choices) => {
    const j = api.jscodeshift;
    const root = j(fileInfo.supply);

    transforms.forEach((remodel) => remodel(j, root));
    return root.toSource(choices.printOptions || { quote: "single" });
  };

export { createTransformer };

For instance, you may have a remodel perform that inlines
expressions assigning the characteristic toggle name to a variable, so in later
transforms you don’t have to fret about these circumstances anymore:

const shouldEnableNewFeature = featureToggle('feature-convert-new');

if (!shouldEnableNewFeature && someOtherLogic) {
  //...
}

Turns into this:

if (!featureToggle('feature-convert-new') && someOtherLogic) {
  //...
}

Over time, you may construct up a group of reusable, smaller
transforms, which may vastly ease the method of dealing with difficult edge
circumstances. This method proved extremely efficient in our work refining design
system elements. As soon as we transformed one bundle—such because the button
part—we had a couple of reusable transforms outlined, like including feedback
at the beginning of features, eradicating deprecated props, or creating aliases
when a bundle is already imported above.

Every of those smaller transforms may be examined and used independently
or mixed for extra complicated transformations, which hastens subsequent
conversions considerably. In consequence, our refinement work turned extra
environment friendly, and these generic codemods at the moment are relevant to different inside
and even exterior React codebases.

Since every remodel is comparatively standalone, you’ll be able to fine-tune them
with out affecting different transforms or the extra complicated, composed ones. For
occasion, you may re-implement a remodel to enhance efficiency—like
decreasing the variety of node-finding rounds—and with complete check
protection, you are able to do this confidently and safely.