Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
deprecated-react-child
(#50)
* feat: Add `deprecated-react-child` * f * f * Add to preset-19
- Loading branch information
Showing
10 changed files
with
205 additions
and
41 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
"types-react-codemod": patch | ||
"types-react-codemod": minor | ||
--- | ||
|
||
Add `deprecated-react-text` and `preset-19`. |
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,7 @@ | ||
--- | ||
"types-react-codemod": minor | ||
--- | ||
|
||
Add `deprecated-react-child` transform. | ||
|
||
Part of `preset-19`. |
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 was deleted.
Oops, something went wrong.
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,81 @@ | ||
import { describe, expect, test } from "@jest/globals"; | ||
import dedent from "dedent"; | ||
import * as JscodeshiftTestUtils from "jscodeshift/dist/testUtils"; | ||
import deprecatedReactChildTransform from "../deprecated-react-child"; | ||
|
||
function applyTransform(source, options = {}) { | ||
return JscodeshiftTestUtils.applyTransform( | ||
deprecatedReactChildTransform, | ||
options, | ||
{ | ||
path: "test.d.ts", | ||
source: dedent(source), | ||
} | ||
); | ||
} | ||
|
||
describe("transform deprecated-react-child", () => { | ||
test("not modified", () => { | ||
expect( | ||
applyTransform(` | ||
import * as React from 'react'; | ||
interface Props { | ||
children?: ReactNode; | ||
} | ||
`) | ||
).toMatchInlineSnapshot(` | ||
"import * as React from 'react'; | ||
interface Props { | ||
children?: ReactNode; | ||
}" | ||
`); | ||
}); | ||
|
||
test("named import", () => { | ||
expect( | ||
applyTransform(` | ||
import { ReactChild } from 'react'; | ||
interface Props { | ||
children?: ReactChild; | ||
} | ||
`) | ||
).toMatchInlineSnapshot(` | ||
"import { ReactChild } from 'react'; | ||
interface Props { | ||
children?: React.ReactElement | number | string; | ||
}" | ||
`); | ||
}); | ||
|
||
test("false-negative named renamed import", () => { | ||
expect( | ||
applyTransform(` | ||
import { ReactChild as MyReactChild } from 'react'; | ||
interface Props { | ||
children?: MyReactChild; | ||
} | ||
`) | ||
).toMatchInlineSnapshot(` | ||
"import { ReactChild as MyReactChild } from 'react'; | ||
interface Props { | ||
children?: MyReactChild; | ||
}" | ||
`); | ||
}); | ||
|
||
test("namespace import", () => { | ||
expect( | ||
applyTransform(` | ||
import * as React from 'react'; | ||
interface Props { | ||
children?: React.ReactChild; | ||
} | ||
`) | ||
).toMatchInlineSnapshot(` | ||
"import * as React from 'react'; | ||
interface Props { | ||
children?: React.ReactElement | number | string; | ||
}" | ||
`); | ||
}); | ||
}); |
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,47 @@ | ||
const parseSync = require("./utils/parseSync"); | ||
|
||
/** | ||
* @type {import('jscodeshift').Transform} | ||
*/ | ||
const deprecatedReactChildTransform = (file, api) => { | ||
const j = api.jscodeshift; | ||
const ast = parseSync(file); | ||
|
||
const changedIdentifiers = ast | ||
.find(j.TSTypeReference, (node) => { | ||
const { typeName } = node; | ||
/** | ||
* @type {import('jscodeshift').Identifier | null} | ||
*/ | ||
let identifier = null; | ||
if (typeName.type === "Identifier") { | ||
identifier = typeName; | ||
} else if ( | ||
typeName.type === "TSQualifiedName" && | ||
typeName.right.type === "Identifier" | ||
) { | ||
identifier = typeName.right; | ||
} | ||
|
||
return identifier !== null && identifier.name === "ReactChild"; | ||
}) | ||
.replaceWith(() => { | ||
// `React.ReactElement | number | string` | ||
return j.tsUnionType([ | ||
// React.ReactElement | ||
j.tsTypeReference( | ||
j.tsQualifiedName(j.identifier("React"), j.identifier("ReactElement")) | ||
), | ||
j.tsNumberKeyword(), | ||
j.tsStringKeyword(), | ||
]); | ||
}); | ||
|
||
// Otherwise some files will be marked as "modified" because formatting changed | ||
if (changedIdentifiers.length > 0) { | ||
return ast.toSource(); | ||
} | ||
return file.source; | ||
}; | ||
|
||
export default deprecatedReactChildTransform; |
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