Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add deprecated-void-function-component transform #52

Merged
merged 2 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/young-panthers-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"types-react-codemod": minor
---

Add `deprecated-void-function-component` transform.

Part of `preset-19`.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,16 @@ interface Props {
label?: MyReactText;
}
```

### `deprecated-void-function-component`

WARNING: Only apply to codebases using `@types/react@^18.0.0`.
In earlier versions of `@types/react` this codemod would change the typings.

```diff
import * as React from "react";
-const Component: React.VFC = () => {}
+const Component: React.FC = () => {}
-const Component: React.VoidFunctionComponent = () => {}
+const Component: React.FunctionComponent = () => {}
```
5 changes: 3 additions & 2 deletions bin/__tests__/types-react-codemod.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ describe("types-react-codemod", () => {
Positionals:
codemod [string] [required] [choices: \\"context-any\\", \\"deprecated-react-child\\",
\\"deprecated-react-text\\", \\"deprecated-react-type\\", \\"deprecated-sfc-element\\",
\\"deprecated-sfc\\", \\"deprecated-stateless-component\\", \\"implicit-children\\",
\\"preset-18\\", \\"preset-19\\", \\"useCallback-implicit-any\\"]
\\"deprecated-sfc\\", \\"deprecated-stateless-component\\",
\\"deprecated-void-function-component\\", \\"implicit-children\\", \\"preset-18\\",
\\"preset-19\\", \\"useCallback-implicit-any\\"]
paths [string] [required]

Options:
Expand Down
1 change: 1 addition & 0 deletions bin/types-react-codemod.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ async function main() {
choices: [
{ checked: true, value: "deprecated-react-child" },
{ checked: true, value: "deprecated-react-text" },
{ checked: true, value: "deprecated-void-function-component" },
],
},
]);
Expand Down
85 changes: 85 additions & 0 deletions transforms/__tests__/deprecated-void-function-component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { describe, expect, test } from "@jest/globals";
import dedent from "dedent";
import * as JscodeshiftTestUtils from "jscodeshift/dist/testUtils";
import deprecatedVoidFunctionComponentTransform from "../deprecated-void-function-component";

function applyTransform(source, options = {}) {
return JscodeshiftTestUtils.applyTransform(
deprecatedVoidFunctionComponentTransform,
options,
{
path: "test.d.ts",
source: dedent(source),
}
);
}

describe("transform deprecated-void-function-component", () => {
test("not modified", () => {
expect(
applyTransform(`
import { FC } from 'react';
FC;
`)
).toMatchInlineSnapshot(`
"import { FC } from 'react';
FC;"
`);
});

test("named import", () => {
expect(
applyTransform(`
import { VFC, VoidFunctionComponent } from 'react';
VFC;
VoidFunctionComponent;
`)
).toMatchInlineSnapshot(`
"import { FC, FunctionComponent } from 'react';
FC;
FunctionComponent;"
`);
});

test("named renamed import", () => {
expect(
applyTransform(`
import { VFC as MyVFC, VoidFunctionComponent as MyVoidFunctionComponent } from 'react';
MyVFC;
MyVoidFunctionComponent;
`)
).toMatchInlineSnapshot(`
"import { FC as MyVFC, FunctionComponent as MyVoidFunctionComponent } from 'react';
MyVFC;
MyVoidFunctionComponent;"
`);
});

test("namespace import", () => {
expect(
applyTransform(`
import * as React from 'react';
React.VFC;
React.VoidFunctionComponent;
`)
).toMatchInlineSnapshot(`
"import * as React from 'react';
React.FC;
React.FunctionComponent;"
`);
});

test("false-positive rename on different namespace", () => {
expect(
applyTransform(`
import * as Preact from 'preact';
Preact.VFC;
Preact.VoidFunctionComponent;
`)
).toMatchInlineSnapshot(`
"import * as Preact from 'preact';
Preact.FC;
Preact.FunctionComponent;"
`);
});
});
7 changes: 7 additions & 0 deletions transforms/__tests__/preset-19.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ describe("preset-19", () => {
let preset19Transform;
let deprecatedReactChildTransform;
let deprecatedReactTextTransform;
let deprecatedVoidFunctionComponentTransform;

function applyTransform(source, options = {}) {
return JscodeshiftTestUtils.applyTransform(preset19Transform, options, {
Expand All @@ -32,6 +33,9 @@ describe("preset-19", () => {

deprecatedReactChildTransform = mockTransform("../deprecated-react-child");
deprecatedReactTextTransform = mockTransform("../deprecated-react-text");
deprecatedVoidFunctionComponentTransform = mockTransform(
"../deprecated-void-function-component"
);

preset19Transform = require("../preset-19");
});
Expand All @@ -43,17 +47,20 @@ describe("preset-19", () => {

expect(deprecatedReactChildTransform).not.toHaveBeenCalled();
expect(deprecatedReactTextTransform).toHaveBeenCalled();
expect(deprecatedVoidFunctionComponentTransform).not.toHaveBeenCalled();
});

test("applies all", () => {
applyTransform("", {
preset19Transforms: [
"deprecated-react-child",
"deprecated-react-text",
"deprecated-void-function-component",
].join(","),
});

expect(deprecatedReactChildTransform).toHaveBeenCalled();
expect(deprecatedReactTextTransform).toHaveBeenCalled();
expect(deprecatedVoidFunctionComponentTransform).toHaveBeenCalled();
});
});
27 changes: 27 additions & 0 deletions transforms/deprecated-void-function-component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const parseSync = require("./utils/parseSync");

/**
* @type {import('jscodeshift').Transform}
*/
const deprecatedVoidFunctionComponentTransform = (file, api) => {
const j = api.jscodeshift;
const ast = parseSync(file);

const changedIdentifiers = ast
.find(j.Identifier, (node) => {
return node.name === "VFC" || node.name === "VoidFunctionComponent";
})
.replaceWith((path) => {
return j.identifier(
path.node.name === "VFC" ? "FC" : "FunctionComponent"
);
});

// Otherwise some files will be marked as "modified" because formatting changed
if (changedIdentifiers.length > 0) {
return ast.toSource();
}
return file.source;
};

export default deprecatedVoidFunctionComponentTransform;
4 changes: 4 additions & 0 deletions transforms/preset-19.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import deprecatedReactChildTransform from "./deprecated-react-child";
import deprecatedReactTextTransform from "./deprecated-react-text";
import deprecatedVoidFunctionComponentTransform from "./deprecated-void-function-component";

/**
* @type {import('jscodeshift').Transform}
Expand All @@ -18,6 +19,9 @@ const transform = (file, api, options) => {
if (transformNames.has("deprecated-react-text")) {
transforms.push(deprecatedReactTextTransform);
}
if (transformNames.has("deprecated-void-function-component")) {
transforms.push(deprecatedVoidFunctionComponentTransform);
}

let wasAlwaysSkipped = true;
const newSource = transforms.reduce((currentFileSource, transform) => {
Expand Down