import type { NodePath } from '@babel/core' import { types as t } from '@babel/core' import { context } from 'rntwsc/devtools/babel-plugin-tw/lib/context' import { jsToNode } from 'rntwsc/devtools/babel-plugin-tw/lib/js-to-node' import { transpileClassNameJsx } from 'rntwsc/devtools/babel-plugin-tw/lib/transpile-class-name-jsx' import type { TraverseOptions } from 'rntwsc/devtools/babel-plugin-tw/visitor' export const traverseJSXOpeningElement = ( path: NodePath, options: TraverseOptions, ) => { const classNames = path.get('attributes').filter(a => { if (!t.isJSXAttribute(a.node)) { return false } if (!( t.isStringLiteral(a.node.value) || t.isJSXExpressionContainer(a.node.value) )) { return false } if (!t.isJSXIdentifier(a.node.name)) { return false } const n = a.node.name.name return n === 'className' || n.endsWith('ClassName') }) as NodePath[] if (!classNames.length) { return } for (const c of classNames) { const ctx = context({ ...options, rootPath: path, }) let transpiled: any = transpileClassNameJsx( ctx, c.get('value') as NodePath, ) if (ctx.extract) { continue } transpiled = jsToNode(ctx, transpiled) transpiled = t.jsxAttribute( c.node.name, t.jsxExpressionContainer(transpiled), ) c.replaceWith(transpiled) } }