const { declare } = require('@babel/helper-plugin-utils') const BabelPluginReacthookFormNoMemo = declare(({ types: t }) => ({ visitor: { Program(programPath, pass) { if (pass.file.opts.filename?.includes('node_modules')) { return } const { bindings } = programPath.scope for (const key in bindings) { const binding = bindings[key] if ( !t.isImportSpecifier(binding.path.node) || !t.isIdentifier(binding.path.node.imported) || !t.isImportDeclaration(binding.path.parentPath?.node) || binding.path.parentPath?.node.source.value !== 'react-hook-form' || binding.path.node.imported.name !== 'useForm' ) { continue } binding.referencePaths.forEach((refPath) => { const function_ = refPath.getFunctionParent() if (!function_) { return } if (!t.isBlockStatement(function_.node.body)) { function_ .get('body') .replaceWith( t.blockStatement([t.returnStatement(function_.node.body)]), ) } if (!t.isBlockStatement(function_.node.body)) { return } const directives = function_.node.body.directives ?? [] const hasManualOptOut = directives.some( (directive) => directive.value.value === 'use no memo', ) if (hasManualOptOut) { return } function_.node.body.directives = [ ...directives, t.directive(t.directiveLiteral('use no memo')), ] }) } }, }, })) module.exports = BabelPluginReacthookFormNoMemo