Skip to content

Commit

Permalink
[React19/replace-use-form-state] Fix import updates (#1382)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohebifar authored Nov 11, 2024
1 parent e04fa82 commit 4a64b99
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react/19/replace-use-form-state",
"version": "1.0.7",
"version": "1.0.9",
"engine": "jscodeshift",
"private": false,
"applicability": {
Expand Down
6 changes: 3 additions & 3 deletions packages/codemods/react/19/replace-use-form-state/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function StatefulForm({}) {
### After:

```ts
import { useActionState } from "react-dom";
import { useActionState } from "react";

async function increment(previousState, formData) {
return previousState + 1;
Expand Down Expand Up @@ -57,10 +57,10 @@ function StatefulForm({}) {
### After:

```ts
import * as ReactDOM from "react-dom";
import { useActionState } from "react";

function StatefulForm({}) {
const [state, formAction] = ReactDOM.useActionState(increment, 0);
const [state, formAction] = useActionState(increment, 0);
return <form></form>;
}
```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use client";

import React from "react";
import { useFormState } from "react-dom";

export const UserRegistrationFormComponent = () => {
const [formState, formAction] = useFormState(signupAction, initialState);

return <form>{/* Form fields go here */}</form>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use client";

import React, { useActionState } from "react";

export const UserRegistrationFormComponent = () => {
const [formState, formAction] = useActionState(signupAction, initialState);

return <form>{/* Form fields go here */}</form>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as ReactDOM from "react-dom";

function StatefulForm({}) {
const [state, formAction] = ReactDOM.useFormState(increment, 0);
return <form></form>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

import { useActionState } from "react";

function StatefulForm({}) {
const [state, formAction] = useActionState(increment, 0);
return <form></form>;
}
20 changes: 9 additions & 11 deletions packages/codemods/react/19/replace-use-form-state/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,24 @@ export default function transform(
let importFromReact = root.find(j.ImportDeclaration, {
source: { value: "react" },
});
const specifier = name
? j.importSpecifier(
j.identifier("useActionState"),
name ? j.identifier(name) : undefined,
)
: j.importSpecifier(j.identifier("useActionState"));
if (importFromReact.length === 0) {
isDirty = true;
root
.get()
.node.program.body.unshift(
j.importDeclaration(
[
name
? j.importSpecifier(
j.identifier("useActionState"),
name ? j.identifier(name) : undefined,
)
: j.importSpecifier(j.identifier("useActionState")),
],
j.literal("react"),
),
j.importDeclaration([specifier], j.literal("react")),
);
importFromReact = root.find(j.ImportDeclaration, {
source: { value: "react" },
});
} else {
importFromReact.get("specifiers").push(specifier);
}
}

Expand Down
50 changes: 50 additions & 0 deletions packages/codemods/react/19/replace-use-form-state/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,54 @@ describe("react/19/replace-use-form-state: useFormState() -> useActionState()",
OUTPUT.replace(/\W/gm, ""),
);
});

it("should correctly transform useFormState import to useActionState", async () => {
const INPUT = await readFile(
join(__dirname, "..", "__testfixtures__/fixture7.input.js"),
"utf-8",
);
const OUTPUT = await readFile(
join(__dirname, "..", "__testfixtures__/fixture7.output.js"),
"utf-8",
);

const fileInfo: FileInfo = {
path: "index.ts",
source: INPUT,
};

const actualOutput = transform(fileInfo, buildApi("js"), {
quote: "single",
});

assert.deepEqual(
actualOutput?.replace(/\W/gm, ""),
OUTPUT.replace(/\W/gm, ""),
);
});

it("should correctly transform import all from ReactDOM", async () => {
const INPUT = await readFile(
join(__dirname, "..", "__testfixtures__/fixture8.input.js"),
"utf-8",
);
const OUTPUT = await readFile(
join(__dirname, "..", "__testfixtures__/fixture8.output.js"),
"utf-8",
);

const fileInfo: FileInfo = {
path: "index.ts",
source: INPUT,
};

const actualOutput = transform(fileInfo, buildApi("js"), {
quote: "single",
});

assert.deepEqual(
actualOutput?.replace(/\W/gm, ""),
OUTPUT.replace(/\W/gm, ""),
);
});
});

0 comments on commit 4a64b99

Please sign in to comment.