Public commit
This commit is contained in:
37
scripts/plugins/babel-component-name.ts
Normal file
37
scripts/plugins/babel-component-name.ts
Normal file
@ -0,0 +1,37 @@
|
||||
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)),
|
||||
)
|
||||
}
|
||||
},
|
||||
},
|
||||
})
|
Reference in New Issue
Block a user