codemod-com / codemod

The e2e platform for creating, sharing, and running codemods, built on top of your favorite codemod engine. Automate code migrations, cleanups, and refactors for yourself, colleagues, and the community.
https://codemod.com
Apache License 2.0
228 stars 10 forks source link

[codemod][issue] MSW v2 - ctx.json mistakes generic param for body #917

Open Retsam opened 2 weeks ago

Retsam commented 2 weeks ago

Faulty codemod

msw/2/response-usages -> codemod source to be updated

Code before transformation

import { Foo } from "somewhere"; // Foo is { x: string, y: number }

const y = 0;
const handler = http.get("/url", (_req, res, ctx) =>
    res(
        ctx.json<Foo>({
            x: "string",
            y,
        }),
    )
);

Expected code after transformation

import { Foo } from "somewhere"; // Foo is { x: string, y: number }

const y = 0;
const handler = http.get("/url", () =>
    HttpResponse.json<Foo>({
        x: "string",
        y,
    }),
);

Actual code after transformation

import { Foo } from "somewhere"; // Foo is { x: string, y: number }

const handler = http.get("/url", () =>
    HttpResponse.json(Foo),
);

Estimated severity

Fairly low - was easy to find because it's a type error. I had one occurrence of this out of ~50 MSW handlers and it wasn't too hard to put the original body back, and put back any references that were wrongly "cleaned up" (const y = 0; in my above example)

Environment:

Additional context

Add any other context about the problem here. This might include the target project, explanation of faulty output related to business logic, relevant links, etc.

alexbit-codemod commented 2 weeks ago

thank you for the detailed issue description.