From 5080e220e09779158ac4aacd587c59613ca146c7 Mon Sep 17 00:00:00 2001 From: Henry Dineen Date: Tue, 10 Dec 2024 10:17:04 -0500 Subject: [PATCH 1/3] fix(replaceType): handle array syntax --- .changeset/wise-icons-wash.md | 5 +++++ transforms/__tests__/deprecated-react-child.js | 16 ++++++++++++++++ transforms/utils/replaceType.js | 12 ++++++++++-- 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 .changeset/wise-icons-wash.md diff --git a/.changeset/wise-icons-wash.md b/.changeset/wise-icons-wash.md new file mode 100644 index 00000000..a94fd652 --- /dev/null +++ b/.changeset/wise-icons-wash.md @@ -0,0 +1,5 @@ +--- +"types-react-codemod": patch +--- + +Handle array syntax in replaceReactType diff --git a/transforms/__tests__/deprecated-react-child.js b/transforms/__tests__/deprecated-react-child.js index 355e59a7..a8dd8447 100644 --- a/transforms/__tests__/deprecated-react-child.js +++ b/transforms/__tests__/deprecated-react-child.js @@ -121,3 +121,19 @@ test("as type parameter", () => { createAction()" `); }); + +test("array syntax", () => { + expect( + applyTransform(` + import { ReactChild } from 'react'; + interface Props { + children?: ReactChild[]; + } + `), + ).toMatchInlineSnapshot(` + "import { ReactElement } from 'react'; + interface Props { + children?: (ReactElement | number | string)[]; + }" + `); +}); diff --git a/transforms/utils/replaceType.js b/transforms/utils/replaceType.js index 712bcf50..f06e8994 100644 --- a/transforms/utils/replaceType.js +++ b/transforms/utils/replaceType.js @@ -113,8 +113,16 @@ function replaceReactType( }, ); for (const typeReferences of sourceIdentifierTypeReferences) { - const changedIdentifiers = typeReferences.replaceWith((path) => { - return buildTargetTypeReference(path.value); + const changedIdentifiers = typeReferences.forEach((path) => { + const targetNode = buildTargetTypeReference(path.value); + if ( + targetNode.type === "TSUnionType" && + path.parentPath.value.type === "TSArrayType" + ) { + path.replace(j.tsParenthesizedType(targetNode)); + } else { + path.replace(targetNode); + } }); if (changedIdentifiers.length > 0) { hasChanges = true; From 8c90389979b9e9dee007bd760e09673fd75e5906 Mon Sep 17 00:00:00 2001 From: Henry Dineen Date: Wed, 11 Dec 2024 11:05:13 -0500 Subject: [PATCH 2/3] test Array generic --- .../__tests__/deprecated-react-child.js | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/transforms/__tests__/deprecated-react-child.js b/transforms/__tests__/deprecated-react-child.js index a8dd8447..604b1cc8 100644 --- a/transforms/__tests__/deprecated-react-child.js +++ b/transforms/__tests__/deprecated-react-child.js @@ -122,18 +122,34 @@ test("as type parameter", () => { `); }); -test("array syntax", () => { +test("array type syntax", () => { expect( applyTransform(` - import { ReactChild } from 'react'; - interface Props { + import { ReactChild } from 'react'; + interface Props { children?: ReactChild[]; } - `), + `), ).toMatchInlineSnapshot(` "import { ReactElement } from 'react'; interface Props { - children?: (ReactElement | number | string)[]; - }" + children?: (ReactElement | number | string)[]; + }" + `); +}); + +test("Array generic", () => { + expect( + applyTransform(` + import { ReactChild } from 'react'; + interface Props { + children?: Array; + } + `), + ).toMatchInlineSnapshot(` + "import { ReactElement } from 'react'; + interface Props { + children?: Array; + }" `); }); From 4def217677f9a6c42af3a34921bf8b4e6adab561 Mon Sep 17 00:00:00 2001 From: "Sebastian \"Sebbie\" Silbermann" Date: Thu, 12 Dec 2024 19:56:15 +0100 Subject: [PATCH 3/3] Update wise-icons-wash.md --- .changeset/wise-icons-wash.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.changeset/wise-icons-wash.md b/.changeset/wise-icons-wash.md index a94fd652..97be277a 100644 --- a/.changeset/wise-icons-wash.md +++ b/.changeset/wise-icons-wash.md @@ -2,4 +2,6 @@ "types-react-codemod": patch --- -Handle array syntax in replaceReactType +Fix a bug when replacing types in shorthand array type notations. + +For example, replacing `ReactText` in `ReactText[]` should now result in `(number | string)[]` instead of `number | string[]`.