import { ModuleResolutionKind, Node, Project, StringLiteral, SyntaxKind, type ObjectLiteralElementLike } from "ts-morph" import * as fg from "fast-glob" import { basename, dirname, join } from "path" import { writeFileSync, readFileSync } from "fs" const docsMap = { "data-state": "The state of the {{widget}}", "data-orientation": "The orientation of the {{widget}}", "data-active": "Present when active or pressed", "data-focus": "Present when focused", "data-focus-visible": "Present when focused with keyboard", "data-hover": "Present when hovered", "data-disabled": "Present when disabled", "data-readonly": "Present when read-only", "data-required": "Present when required", "data-placement": "The placement of the {{widget}}", "data-side": "The side of the trigger that the {{widget}} is positioned on", "data-invalid": "Present when invalid", "data-current": "Present when current", "data-copied": "Present when copied state is true", "data-channel": "The color channel of the {{widget}}", "data-highlighted": "Present when highlighted", "data-today": "Present when the date represents today", "data-unavailable": "Present when the date is unavailable based on the min and max date", "data-range-start": "Present when is the start of a range", "data-range-end": "Present when is the end of a range", "data-in-range": "Present when is within the range", "data-outside-range": "Present when is outside the range", "data-weekend": "Present when is a weekend day", "data-view": "The view of the {{widget}}", "data-placeholder-shown": "Present when placeholder is shown", "data-dragging": "Present when in the dragging state", "data-axis": "The axis to resize", "data-index": "The index of the item", "data-complete": "Present when the {{widget}} value is complete", "data-depth": "The depth of the item", "data-expanded": "Present when expanded", "data-valuetext": "The human-readable value", "data-inview": "Present when in viewport", "data-type": "The type of the item", "data-selected": "Present when selected", "data-mounted": "Present when mounted", "data-pressed": "Present when pressed", "data-paused": "Present when paused", "data-checked": "Present when checked", "data-overlap": "Present when overlapping", "data-path": "The path of the item", "data-value": "The value of the item", "data-in-hover-range": "Present when in hovered range", "data-hover-range-start": "Present when is the start of the hovered range", "data-hover-range-end": "Present when is the end of the hovered range", "data-loading": "Present when loading", "data-activedescendant": "The id the active descendant of the {{widget}}", "data-scrolling": "Present when scrolling", "data-at-top": "Present when scrolled to the top edge", "data-at-bottom": "Present when scrolled to the bottom edge", "data-at-left": "Present when scrolled to the left edge", "data-at-right": "Present when scrolled to the right edge", "data-behind": "Present when not topmost", "data-minimized": "Present when minimized", "data-maximized": "Present when maximized", "data-staged": "Present when not in default stage", "data-topmost": "Present when topmost", "data-stage": "The stage of the {{widget}}", "data-inline": "Present when the content is inline", "data-empty": "Present when the content is empty", "data-overflow-x": "Present when the content overflows the x-axis", "data-overflow-y": "Present when the content overflows the y-axis", "data-ssr": "Present when not rendered in the browser. Useful for ssr styling", "data-nested": "Present when the {{widget}} is nested within another {{widget}}", "data-has-nested": "Present when the {{widget}} has nested {{widget}}s", "data-has-collapsed-size": "Present when the {{widget}} has collapsed width or height", } const skipAttrs = ["data-ownedby", "data-uid"] // Mapping of widgets to their layer types (only for widgets that use dismissable) const widgetLayerTypes: Record = { dialog: "dialog", popover: "popover", menu: "menu", select: "listbox", combobox: "listbox", "date-picker": "popover", "color-picker": "popover", "hover-card": "popover", toast: "dialog", tour: "popover", } /* ----------------------------------------------------------------------------- * Helpers * -----------------------------------------------------------------------------*/ function hasDismissableDependency(widget: string): boolean { try { const packagePath = join(process.cwd(), "packages", "machines", widget, "package.json") const packageJson = JSON.parse(readFileSync(packagePath, "utf-8")) return "@zag-js/dismissable" in (packageJson.dependencies || {}) } catch { return false } } function titleCase(str: string) { return str.charAt(0).toUpperCase() + str.slice(1) } function dashCase(text: string) { return text.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`).replace(/^-+/, "") } // replace `rootProps` -> `root` // replace `getRootProps` -> `root` function formatName(name: string) { return titleCase(name.replace(/^(get)?(.+?)Props$/, "$2")) } function getTenaryValues(literal: StringLiteral) { const tenaryValues: string[] = [] literal .getParentIfKind(SyntaxKind.PropertyAssignment) ?.getChildrenOfKind(SyntaxKind.ConditionalExpression) ?.forEach((conditional) => { conditional.getDescendantsOfKind(SyntaxKind.StringLiteral).forEach((literal) => { tenaryValues.push(literal.getLiteralValue()) }) }) return tenaryValues } /* ----------------------------------------------------------------------------- * ProcessObjectLiteral * -----------------------------------------------------------------------------*/ interface ProcessLiteralOptions { widget: string cb({ part, name, desc }: { part: string; name: string; desc: string }): void skipIf?(name: string): boolean eval?({ part, name }: { part: string; name: string }): void } function processObjectLiteral(node: ObjectLiteralElementLike, opts: ProcessLiteralOptions) { const { cb, skipIf, widget, eval: _eval } = opts if (Node.isShorthandPropertyAssignment(node)) { const name = node.getName() const part = formatName(name) _eval?.({ part, name }) return } if (!Node.isPropertyAssignment(node) && !Node.isMethodDeclaration(node)) return const name = node.getName() if (skipIf?.(name)) return const part = formatName(name) node.getDescendantsOfKind(SyntaxKind.StringLiteral).forEach((literal) => { const name = literal.getLiteralValue() if (!name.startsWith("data-") || skipAttrs.includes(name)) return const rep = part === "Root" ? widget : part.startsWith("Item") ? "Item" : part let desc = docsMap[name as keyof typeof docsMap]?.replace("{{widget}}", rep.toLowerCase()) ?? "" if (name === "data-state") { const tenaryValues = getTenaryValues(literal) desc = `${tenaryValues.map((x) => JSON.stringify(x)).join(" | ")}` } cb({ part, name, desc }) }) // get all spread properties whose name is dataAttrs node.getDescendantsOfKind(SyntaxKind.SpreadAssignment).forEach((spread) => { spread.getChildrenOfKind(SyntaxKind.Identifier).forEach((identifier) => { _eval?.({ part, name: identifier.getText() }) }) spread.getChildrenOfKind(SyntaxKind.CallExpression).forEach((expr) => { expr.getChildrenOfKind(SyntaxKind.Identifier).forEach((identifier) => { _eval?.({ part, name: identifier.getText() }) }) }) }) } /* ----------------------------------------------------------------------------- * Main function * -----------------------------------------------------------------------------*/ type DataAttrMap = Record async function main() { const project = new Project({ compilerOptions: { moduleResolution: ModuleResolutionKind.NodeNext, }, }) // sorted: glob order follows the filesystem, and it decides the key order of the emitted JSON const files = fg.globSync("packages/machines/*/src/*.connect.ts").sort() const json: Record = {} files.forEach((file) => { const widget = basename(dirname(dirname(file))) project.addSourceFileAtPath(file) const hasDismissable = hasDismissableDependency(widget) const layerType = widgetLayerTypes[widget] const sourceFile = project.getSourceFile(file) if (!sourceFile) return const result: Record = {} sourceFile.getDescendantsOfKind(SyntaxKind.FunctionDeclaration).forEach((node) => { node.getChildrenOfKind(SyntaxKind.Block).forEach((block) => { const dataAttrs: Record = {} const functions: Map = new Map() // check that the variable name is `dataAttrs` block.getChildrenOfKind(SyntaxKind.VariableStatement).forEach((variable) => { const [declaration] = variable.getDeclarations() const name = declaration.getName() if (name !== "dataAttrs") return declaration.getChildrenOfKind(SyntaxKind.ObjectLiteralExpression).forEach((obj) => { obj.getProperties().forEach((property) => { processObjectLiteral(property, { widget, cb({ name, desc }) { dataAttrs[name] = desc }, }) }) }) }) block.getChildrenOfKind(SyntaxKind.FunctionDeclaration).forEach((fn) => { const fnName = fn.getNameOrThrow() if (!fnName.endsWith("Props")) return functions.set(fnName, {}) fn.getDescendantsOfKind(SyntaxKind.ObjectLiteralExpression).forEach((obj) => { obj.getProperties().forEach((property) => { processObjectLiteral(property, { widget, cb({ name, desc }) { functions.get(fnName)![name] = desc }, }) }) }) }) block.getChildrenOfKind(SyntaxKind.ReturnStatement).forEach((returnStatement) => { returnStatement.getChildrenOfKind(SyntaxKind.ObjectLiteralExpression).forEach((obj) => { obj.getProperties().forEach((property) => { processObjectLiteral(property, { widget, skipIf(name) { return !name.endsWith("Props") }, cb({ part, name, desc }) { result[part] ||= { "data-scope": widget, "data-part": dashCase(part) } result[part][name] = desc // Add layer attributes for content part if widget uses dismissable if (hasDismissable && part === "Content" && layerType) { result[part]["data-nested"] = layerType result[part]["data-has-nested"] = layerType } }, eval({ part, name }) { if (name === "dataAttrs") { result[part] ||= {} Object.assign(result[part], dataAttrs) // Add layer attributes for content part if widget uses dismissable if (hasDismissable && part === "Content" && layerType) { result[part]["data-nested"] = layerType result[part]["data-has-nested"] = layerType } return } if (functions.has(name)) { result[part] ||= { "data-scope": widget, "data-part": dashCase(part) } Object.assign(result[part], functions.get(name)) // Add layer attributes for content part if widget uses dismissable if (hasDismissable && part === "Content" && layerType) { result[part]["data-nested"] = layerType result[part]["data-has-nested"] = layerType } return } }, }) }) }) }) }) }) json[widget] = result }) const outPath = join(process.cwd(), "packages", "docs", "data", "data-attr.json") writeFileSync(outPath, JSON.stringify(json, null, 2)) } main()