import type { BabelPlugin } from "./esbuild-babel" export const componentName = (): BabelPlugin => ({ types: t }) => ({ name: "babel-plugin-component-name", visitor: { ReturnStatement(path) { if (!t.isJSXElement(path.node.argument)) return const funcParent = path.getFunctionParent() if (funcParent == null || !t.isArrowFunctionExpression(funcParent.node)) return let id: string | undefined if ( t.isCallExpression(funcParent.parent) && t.isIdentifier(funcParent.parent.callee) && t.isVariableDeclarator(funcParent.parentPath.parent) && t.isIdentifier(funcParent.parentPath.parent.id) ) { id = funcParent.parentPath.parent.id.name } else if ( t.isVariableDeclarator(funcParent.parent) && t.isIdentifier(funcParent.parent.id) ) { id = funcParent.parent.id.name } if (id != null) { path.node.argument.openingElement.attributes.unshift( t.jsxAttribute(t.jsxIdentifier("data-component"), t.stringLiteral(id)), ) } }, }, })