-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add codemod for required initial value in
useRef
(#217)
* Add codemod for required initial value in `useRef` * Rebase * Format
- Loading branch information
Showing
7 changed files
with
162 additions
and
6 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,8 @@ | ||
--- | ||
"types-react-codemod": minor | ||
--- | ||
|
||
Add codemod for required initial value in `useRef` | ||
|
||
Added as `experimental-useRef-required-initial`. | ||
Can be used on 18.x types but only intended for once https://github.com/DefinitelyTyped/DefinitelyTyped/pull/64920 lands. |
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,65 @@ | ||
const { describe, expect, test } = require("@jest/globals"); | ||
const dedent = require("dedent"); | ||
const JscodeshiftTestUtils = require("jscodeshift/dist/testUtils"); | ||
const useRefRequiredInitial = require("../experimental-useRef-required-initial"); | ||
|
||
function applyTransform(source, options = {}) { | ||
return JscodeshiftTestUtils.applyTransform(useRefRequiredInitial, options, { | ||
path: "test.tsx", | ||
source: dedent(source), | ||
}); | ||
} | ||
|
||
describe("transform useRef-required-initial", () => { | ||
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 { useRef } from 'react'; | ||
const myRef = useRef<number>(); | ||
`), | ||
).toMatchInlineSnapshot(` | ||
"import { useRef } from 'react'; | ||
const myRef = useRef<number>(undefined);" | ||
`); | ||
}); | ||
|
||
test("false-negative named renamed import", () => { | ||
expect( | ||
applyTransform(` | ||
import { useRef as useReactRef } from 'react'; | ||
const myRef = useReactRef<number>(); | ||
`), | ||
).toMatchInlineSnapshot(` | ||
"import { useRef as useReactRef } from 'react'; | ||
const myRef = useReactRef<number>();" | ||
`); | ||
}); | ||
|
||
test("namespace import", () => { | ||
expect( | ||
applyTransform(` | ||
import * as React from 'react'; | ||
const myRef = React.useRef<number>(); | ||
`), | ||
).toMatchInlineSnapshot(` | ||
"import * as React from 'react'; | ||
const myRef = React.useRef<number>(undefined);" | ||
`); | ||
}); | ||
}); |
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,42 @@ | ||
const parseSync = require("./utils/parseSync"); | ||
const t = require("@babel/types"); | ||
const traverse = require("@babel/traverse").default; | ||
|
||
/** | ||
* @type {import('jscodeshift').Transform} | ||
* | ||
* Summary for Klarna's klapp@? | ||
* TODO | ||
*/ | ||
const useRefRequiredInitialTransform = (file) => { | ||
const ast = parseSync(file); | ||
|
||
let changedSome = false; | ||
|
||
// ast.get("program").value is sufficient for unit tests but not actually running it on files | ||
// TODO: How to test? | ||
const traverseRoot = ast.paths()[0].value; | ||
traverse(traverseRoot, { | ||
CallExpression({ node: callExpression }) { | ||
const isUseRefCall = | ||
(callExpression.callee.type === "Identifier" && | ||
callExpression.callee.name === "useRef") || | ||
(callExpression.callee.type === "MemberExpression" && | ||
callExpression.callee.property.type === "Identifier" && | ||
callExpression.callee.property.name === "useRef"); | ||
|
||
if (isUseRefCall && callExpression.arguments.length === 0) { | ||
changedSome = true; | ||
callExpression.arguments = [t.identifier("undefined")]; | ||
} | ||
}, | ||
}); | ||
|
||
// Otherwise some files will be marked as "modified" because formatting changed | ||
if (changedSome) { | ||
return ast.toSource(); | ||
} | ||
return file.source; | ||
}; | ||
|
||
module.exports = useRefRequiredInitialTransform; |
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