Skip to content

Instantly share code, notes, and snippets.

View elderbas's full-sized avatar

brian schermerhorn elderbas

View GitHub Profile
@elderbas
elderbas / removeUnusedUseEffectImport.js
Created May 5, 2021 21:15
codemod script to remove useEffect if it's not being used
module.exports = function (file, api) {
const j = api.jscodeshift;
const { CallExpression, Identifier } = j;
return getReactImportStatementNode()
.forEach((node, index) => {
node.value.specifiers.forEach((specifier, index) => {
if (specifier.local.name === "useEffect") {
if (!shouldKeepUseEffectImport()) {
delete node.value.specifiers[index];
@elderbas
elderbas / add_import_statement.js
Created May 5, 2021 21:16
codemod script to add import statement of `useEffectOnce`
export default (fileInfo, api) => {
const j = api.jscodeshift;
const root = j(fileInfo.source);
const importLine = `import useEffectOnce from 'utils/useEffectOnce';`;
const imports = root.find(j.ImportDeclaration);
const n = imports.length;
j(imports.at(n - 1).get()).insertAfter(importLine); // after the imports
return root.toSource();
@elderbas
elderbas / callexpressions.js
Last active May 5, 2021 21:17
codemodscript to refactor `useEffect` with empty array to `useEffectOnce` without an array arg
module.exports = function (file, api) {
const j = api.jscodeshift;
const { CallExpression, Identifier } = j;
return (
j(file.source)
.find(CallExpression, {
callee: {
name: "useEffect",
},