Skip to content

Commit

Permalink
fix(replaceType): handle array syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
henryqdineen committed Dec 10, 2024
1 parent 8fd6adb commit 5080e22
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-icons-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"types-react-codemod": patch
---

Handle array syntax in replaceReactType
16 changes: 16 additions & 0 deletions transforms/__tests__/deprecated-react-child.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,19 @@ test("as type parameter", () => {
createAction<React.ReactElement | number | string>()"
`);
});

test("array syntax", () => {
expect(
applyTransform(`
import { ReactChild } from 'react';
interface Props {
children?: ReactChild[];
}
`),
).toMatchInlineSnapshot(`
"import { ReactElement } from 'react';
interface Props {
children?: (ReactElement | number | string)[];
}"
`);
});
12 changes: 10 additions & 2 deletions transforms/utils/replaceType.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5080e22

Please sign in to comment.