Skip to content

Commit 5614a5f

Browse files
fix(replaceType): avoid modifying import not from 'react' (#456)
Co-authored-by: Sebastian "Sebbie" Silbermann <[email protected]> Co-authored-by: eps1lon <[email protected]>
1 parent 8fd6adb commit 5614a5f

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

.changeset/sharp-dancers-breathe.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+
Avoid modifying import not from `'react'` when replacing types.

transforms/__tests__/deprecated-react-node-array.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,29 @@ test("namespace import", () => {
129129
test("in type parameters", () => {
130130
expect(
131131
applyTransform(`
132-
import * as React from 'react';
132+
import { ReactNodeArray } from 'react';
133133
createComponent<ReactNodeArray>();
134134
`),
135135
).toMatchInlineSnapshot(`
136-
"import * as React from 'react';
136+
"import { ReactNode } from 'react';
137137
createComponent<ReadonlyArray<ReactNode>>();"
138138
`);
139139
});
140+
141+
test("import from 'prop-types'", () => {
142+
expect(
143+
applyTransform(`
144+
import { ReactNodeArray} from 'prop-types';
145+
interface Props {
146+
href: string;
147+
children: ReactNodeArray;
148+
};
149+
`),
150+
).toMatchInlineSnapshot(`
151+
"import { ReactNodeArray} from 'prop-types';
152+
interface Props {
153+
href: string;
154+
children: ReactNodeArray;
155+
};"
156+
`);
157+
});

transforms/utils/replaceType.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ function replaceReactType(
104104
const { typeName } = node;
105105

106106
return (
107-
(typeName.type === "Identifier" &&
107+
(sourceIdentifierImports.length > 0 &&
108+
typeName.type === "Identifier" &&
108109
typeName.name === sourceIdentifier) ||
109110
(typeName.type === "TSQualifiedName" &&
110111
typeName.right.type === "Identifier" &&

0 commit comments

Comments
 (0)