babel-plugin-transform-react-class-to-function
Version:
A Babel 7 plugin which transforms React component classes into functions
20 lines (18 loc) • 486 B
JavaScript
module.exports = (propNodes, path) => {
if (propNodes.length === 0) {
return [];
}
if (
propNodes.length === 1
&& propNodes[0].parentPath.isVariableDeclarator()
&& propNodes[0].parentPath.get('id').isObjectPattern()
) {
propNodes[0].parentPath.remove();
return [propNodes[0].parent.id];
}
const identifier = path.scope.generateUidIdentifier('props');
propNodes.forEach((prop) => {
prop.replaceWith(identifier);
});
return [identifier];
};