-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add react-element-default-any-props codemod (#371)
- Loading branch information
Showing
7 changed files
with
212 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
"types-react-codemod": minor | ||
--- | ||
|
||
Add `react-element-default-any-props` codemod | ||
|
||
Opt-in codemod in `preset-19`. | ||
|
||
```diff | ||
// implies `React.ReactElement<unknown>` in React 19 as opposed to `React.ReactElement<any>` in prior versions. | ||
-declare const element: React.ReactElement | ||
+declare const element: React.ReactElement<any> | ||
``` | ||
|
||
Only meant to migrate old code not a recommendation for how to type React elements. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
const { expect, test } = require("@jest/globals"); | ||
const dedent = require("dedent"); | ||
const JscodeshiftTestUtils = require("jscodeshift/dist/testUtils"); | ||
const reactElementDefaultAnyPropsTransform = require("../react-element-default-any-props"); | ||
|
||
function applyTransform(source, options = {}) { | ||
return JscodeshiftTestUtils.applyTransform( | ||
reactElementDefaultAnyPropsTransform, | ||
options, | ||
{ | ||
path: "test.d.ts", | ||
source: dedent(source), | ||
}, | ||
); | ||
} | ||
|
||
test("not modified", () => { | ||
expect( | ||
applyTransform(` | ||
import * as React from 'react'; | ||
declare const element: React.ReactElement<unknown> | ||
`), | ||
).toMatchInlineSnapshot(` | ||
"import * as React from 'react'; | ||
declare const element: React.ReactElement<unknown>" | ||
`); | ||
}); | ||
|
||
test("named import", () => { | ||
expect( | ||
applyTransform(` | ||
import { ReactElement } from 'react'; | ||
declare const element: ReactElement | ||
`), | ||
).toMatchInlineSnapshot(` | ||
"import { ReactElement } from 'react'; | ||
declare const element: ReactElement<any>" | ||
`); | ||
}); | ||
|
||
test("named type import", () => { | ||
expect( | ||
applyTransform(` | ||
import { type ReactElement } from 'react'; | ||
declare const element: ReactElement | ||
`), | ||
).toMatchInlineSnapshot(` | ||
"import { type ReactElement } from 'react'; | ||
declare const element: ReactElement<any>" | ||
`); | ||
}); | ||
|
||
test("false-negative named renamed import", () => { | ||
expect( | ||
applyTransform(` | ||
import { type ReactElement as MyReactElement } from 'react'; | ||
declare const element: MyReactElement | ||
`), | ||
).toMatchInlineSnapshot(` | ||
"import { type ReactElement as MyReactElement } from 'react'; | ||
declare const element: MyReactElement" | ||
`); | ||
}); | ||
|
||
test("namespace import", () => { | ||
expect( | ||
applyTransform(` | ||
import * as React from 'react'; | ||
declare const element: React.ReactElement | ||
`), | ||
).toMatchInlineSnapshot(` | ||
"import * as React from 'react'; | ||
declare const element: React.ReactElement<any>" | ||
`); | ||
}); | ||
|
||
test("as type parameter", () => { | ||
expect( | ||
applyTransform(` | ||
import * as React from 'react'; | ||
createAction<React.ReactElement>() | ||
createAction<React.ReactElement<unknown>>() | ||
`), | ||
).toMatchInlineSnapshot(` | ||
"import * as React from 'react'; | ||
createAction<React.ReactElement<any>>() | ||
createAction<React.ReactElement<unknown>>()" | ||
`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const parseSync = require("./utils/parseSync"); | ||
const { | ||
findTSTypeReferenceCollections, | ||
} = require("./utils/jscodeshift-bugfixes"); | ||
|
||
/** | ||
* @type {import('jscodeshift').Transform} | ||
*/ | ||
const reactElementDefaultAnyPropsTransform = (file, api) => { | ||
const j = api.jscodeshift; | ||
const ast = parseSync(file); | ||
|
||
let hasChanges = false; | ||
|
||
const reactElementTypeReferences = findTSTypeReferenceCollections( | ||
j, | ||
ast, | ||
(typeReference) => { | ||
const { typeName, typeParameters } = typeReference; | ||
if (typeParameters != null) { | ||
return false; | ||
} | ||
|
||
if (typeName.type === "TSQualifiedName") { | ||
// `React.ReactElement` | ||
if ( | ||
typeName.left.type === "Identifier" && | ||
typeName.left.name === "React" && | ||
typeName.right.type === "Identifier" && | ||
typeName.right.name === "ReactElement" | ||
) { | ||
return true; | ||
} | ||
} else { | ||
// `ReactElement` | ||
if (typeName.name === "ReactElement") { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
}, | ||
); | ||
|
||
for (const typeReferences of reactElementTypeReferences) { | ||
const changedTypes = typeReferences.replaceWith((path) => { | ||
return j.tsTypeReference( | ||
path.get("typeName").value, | ||
j.tsTypeParameterInstantiation([ | ||
j.tsTypeReference(j.identifier("any")), | ||
]), | ||
); | ||
}); | ||
|
||
hasChanges = hasChanges || changedTypes.length > 0; | ||
} | ||
|
||
// Otherwise some files will be marked as "modified" because formatting changed | ||
if (hasChanges) { | ||
return ast.toSource(); | ||
} | ||
return file.source; | ||
}; | ||
|
||
module.exports = reactElementDefaultAnyPropsTransform; |