function transform(file, { j }) {
const root = j(file.source);
root.find(j.ImportDeclaration, { source: { value: 'react' } })
.forEach(path => {
path.value.specifiers = path.value.specifiers.filter(specifier => specifier.imported.name !== 'forwardRef');
});
root.find(j.CallExpression, { callee: { name: 'forwardRef' } })
.forEach(path => {
const componentFunction = path.node.arguments[0];
if (j.ArrowFunctionExpression.check(componentFunction) || j.FunctionExpression.check(componentFunction)) {
const params = componentFunction.params;
if (params.length === 2) {
componentFunction.params = [params[0]];
j(componentFunction.body)
.find(j.JSXAttribute, { name: { name: 'ref' }})
.replaceWith(
j.jsxAttribute(
j.jsxIdentifier('ref'),
j.jsxExpressionContainer(
j.memberExpression(j.identifier('props'), j.identifier('ref'))
)
)
);
j(path).replaceWith(componentFunction);
}
}
});
return root.toSource();
}
export default transform;