import { Transformer } from "@/src/utils/transformers" import { SyntaxKind } from "ts-morph" interface TransformInfo { elementStart: number elementEnd: number newText: string } export const transformRender: Transformer = async ({ sourceFile, config }) => { // Only run for base- styles. if (!config.style?.startsWith("base-")) { return sourceFile } // Collect all transformations first, then apply them in reverse order. // This prevents issues with invalidated nodes when modifying the tree. const transformations: TransformInfo[] = [] // Find all JSX elements with render attribute. const jsxElements = sourceFile.getDescendantsOfKind(SyntaxKind.JsxElement) for (const jsxElement of jsxElements) { const openingElement = jsxElement.getOpeningElement() const renderAttr = openingElement.getAttribute("render") if (!renderAttr) { continue } // Get the children of the parent element. const children = jsxElement.getJsxChildren() const childrenText = children .map((c) => c.getText()) .join("") .trim() // If there are no children, nothing to transform. if (!childrenText) { continue } // Get the render attribute value. if (renderAttr.getKind() !== SyntaxKind.JsxAttribute) { continue } const jsxAttr = renderAttr.asKindOrThrow(SyntaxKind.JsxAttribute) const initializer = jsxAttr.getInitializer() if (!initializer) { continue } // The render value should be a JSX expression like {