Skip to content

Commit 5080e22

Browse files
committed
fix(replaceType): handle array syntax
1 parent 8fd6adb commit 5080e22

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

.changeset/wise-icons-wash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"types-react-codemod": patch
3+
---
4+
5+
Handle array syntax in replaceReactType

transforms/__tests__/deprecated-react-child.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,19 @@ test("as type parameter", () => {
121121
createAction<React.ReactElement | number | string>()"
122122
`);
123123
});
124+
125+
test("array syntax", () => {
126+
expect(
127+
applyTransform(`
128+
import { ReactChild } from 'react';
129+
interface Props {
130+
children?: ReactChild[];
131+
}
132+
`),
133+
).toMatchInlineSnapshot(`
134+
"import { ReactElement } from 'react';
135+
interface Props {
136+
children?: (ReactElement | number | string)[];
137+
}"
138+
`);
139+
});

transforms/utils/replaceType.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,16 @@ function replaceReactType(
113113
},
114114
);
115115
for (const typeReferences of sourceIdentifierTypeReferences) {
116-
const changedIdentifiers = typeReferences.replaceWith((path) => {
117-
return buildTargetTypeReference(path.value);
116+
const changedIdentifiers = typeReferences.forEach((path) => {
117+
const targetNode = buildTargetTypeReference(path.value);
118+
if (
119+
targetNode.type === "TSUnionType" &&
120+
path.parentPath.value.type === "TSArrayType"
121+
) {
122+
path.replace(j.tsParenthesizedType(targetNode));
123+
} else {
124+
path.replace(targetNode);
125+
}
118126
});
119127
if (changedIdentifiers.length > 0) {
120128
hasChanges = true;

0 commit comments

Comments
 (0)