-
-
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 no-implicit-ref-callback-return transform (#369)
- Loading branch information
Showing
8 changed files
with
154 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 `no-implicit-ref-callback-return` transform | ||
|
||
Ensures you don't accidentally return anything from ref callbacks since the return value was always ignored. | ||
With ref cleanups, this is no longer the case and flagged in types to avoid mistakes. | ||
|
||
```diff | ||
-<div ref={current => (instance = current)} /> | ||
+<div ref={current => {instance = current}} /> | ||
``` | ||
|
||
The transform is opt-in in the `preset-19` in case you already used ref cleanups in Canary releases. |
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,67 @@ | ||
const { expect, test } = require("@jest/globals"); | ||
const dedent = require("dedent"); | ||
const JscodeshiftTestUtils = require("jscodeshift/dist/testUtils"); | ||
const noImplicitRefCallbackReturnTransform = require("../no-implicit-ref-callback-return"); | ||
|
||
function applyTransform(source, options = {}) { | ||
return JscodeshiftTestUtils.applyTransform( | ||
noImplicitRefCallbackReturnTransform, | ||
options, | ||
{ | ||
path: "test.tsx", | ||
source: dedent(source), | ||
}, | ||
); | ||
} | ||
|
||
test("not modified", () => { | ||
expect( | ||
applyTransform(` | ||
<div ref={current => {instance = current}} /> | ||
`), | ||
).toMatchInlineSnapshot(`"<div ref={current => {instance = current}} />"`); | ||
}); | ||
|
||
test("replaces implicit return of assignment expression with block", () => { | ||
expect( | ||
applyTransform(` | ||
<div ref={current => (instance = current)} /> | ||
`), | ||
).toMatchInlineSnapshot(` | ||
"<div ref={current => { | ||
(instance = current); | ||
}} />" | ||
`); | ||
}); | ||
|
||
test("replaces implicit return of identifier with block", () => { | ||
expect( | ||
applyTransform(` | ||
<div ref={current => current} /> | ||
`), | ||
).toMatchInlineSnapshot(` | ||
"<div ref={current => { | ||
current; | ||
}} />" | ||
`); | ||
}); | ||
|
||
test("function expression", () => { | ||
expect( | ||
applyTransform(` | ||
<div ref={function (current) { instance = current }} /> | ||
`), | ||
).toMatchInlineSnapshot( | ||
`"<div ref={function (current) { instance = current }} />"`, | ||
); | ||
}); | ||
|
||
test("only applies to `ref` prop", () => { | ||
expect( | ||
applyTransform(` | ||
<div innerRef={current => (instance = current)} /> | ||
`), | ||
).toMatchInlineSnapshot( | ||
`"<div innerRef={current => (instance = current)} />"`, | ||
); | ||
}); |
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,38 @@ | ||
const parseSync = require("./utils/parseSync"); | ||
|
||
/** | ||
* @type {import('jscodeshift').Transform} | ||
*/ | ||
const noImplicitRefCallbackReturnTransform = (file, api) => { | ||
const j = api.jscodeshift; | ||
const ast = parseSync(file); | ||
|
||
let changedSome = false; | ||
|
||
ast | ||
.find(j.JSXAttribute, (jsxAttribute) => { | ||
return jsxAttribute.name.name === "ref"; | ||
}) | ||
.forEach((jsxAttributePath) => { | ||
const jsxAttribute = jsxAttributePath.node; | ||
if ( | ||
jsxAttribute.value?.type === "JSXExpressionContainer" && | ||
jsxAttribute.value.expression.type === "ArrowFunctionExpression" && | ||
jsxAttribute.value.expression.body.type !== "BlockStatement" | ||
) { | ||
changedSome = true; | ||
|
||
jsxAttribute.value.expression.body = j.blockStatement([ | ||
j.expressionStatement(jsxAttribute.value.expression.body), | ||
]); | ||
} | ||
}); | ||
|
||
// Otherwise some files will be marked as "modified" because formatting changed | ||
if (changedSome) { | ||
return ast.toSource(); | ||
} | ||
return file.source; | ||
}; | ||
|
||
module.exports = noImplicitRefCallbackReturnTransform; |
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