From c5863b06784c85e5557b225c49633dfae16faac3 Mon Sep 17 00:00:00 2001 From: Henry Dineen Date: Thu, 12 Dec 2024 12:33:38 -0500 Subject: [PATCH 1/3] fix(replaceType): avoid modifying import not from 'react' --- .../__tests__/deprecated-react-node-array.js | 22 +++++++++++++++++-- transforms/utils/replaceType.js | 3 ++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/transforms/__tests__/deprecated-react-node-array.js b/transforms/__tests__/deprecated-react-node-array.js index 725fcef8..62c9be14 100644 --- a/transforms/__tests__/deprecated-react-node-array.js +++ b/transforms/__tests__/deprecated-react-node-array.js @@ -129,11 +129,29 @@ test("namespace import", () => { test("in type parameters", () => { expect( applyTransform(` - import * as React from 'react'; + import { ReactNodeArray } from 'react'; createComponent(); `), ).toMatchInlineSnapshot(` - "import * as React from 'react'; + "import { ReactNode } from 'react'; createComponent>();" `); }); + +test("import from 'prop-types'", () => { + expect( + applyTransform(` + import { ReactNodeArray} from 'prop-types'; + interface Props { + href: string; + children: ReactNodeArray; + }; + `), + ).toMatchInlineSnapshot(` + "import { ReactNodeArray} from 'prop-types'; + interface Props { + href: string; + children: ReactNodeArray; + };" + `); +}); diff --git a/transforms/utils/replaceType.js b/transforms/utils/replaceType.js index 712bcf50..ce0b1527 100644 --- a/transforms/utils/replaceType.js +++ b/transforms/utils/replaceType.js @@ -104,7 +104,8 @@ function replaceReactType( const { typeName } = node; return ( - (typeName.type === "Identifier" && + (sourceIdentifierImports.length && + typeName.type === "Identifier" && typeName.name === sourceIdentifier) || (typeName.type === "TSQualifiedName" && typeName.right.type === "Identifier" && From 4d9d7c39cef73357f0a53fa662391fe3c35588f8 Mon Sep 17 00:00:00 2001 From: "Sebastian \"Sebbie\" Silbermann" Date: Thu, 12 Dec 2024 19:50:04 +0100 Subject: [PATCH 2/3] Add changeset --- .changeset/sharp-dancers-breathe.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/sharp-dancers-breathe.md diff --git a/.changeset/sharp-dancers-breathe.md b/.changeset/sharp-dancers-breathe.md new file mode 100644 index 00000000..5dc7cbdb --- /dev/null +++ b/.changeset/sharp-dancers-breathe.md @@ -0,0 +1,5 @@ +--- +"types-react-codemod": patch +--- + +Avoid modifying import not from `'react'` when replacing types. From fd0aa49fa5a90603feda5aba64d4304c85cf542c Mon Sep 17 00:00:00 2001 From: eps1lon Date: Thu, 12 Dec 2024 19:52:57 +0100 Subject: [PATCH 3/3] style nit --- transforms/utils/replaceType.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transforms/utils/replaceType.js b/transforms/utils/replaceType.js index ce0b1527..c38be5d0 100644 --- a/transforms/utils/replaceType.js +++ b/transforms/utils/replaceType.js @@ -104,7 +104,7 @@ function replaceReactType( const { typeName } = node; return ( - (sourceIdentifierImports.length && + (sourceIdentifierImports.length > 0 && typeName.type === "Identifier" && typeName.name === sourceIdentifier) || (typeName.type === "TSQualifiedName" &&