page.mdx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { pageMetadata } from "@/lib/page-metadata"
  2. export const metadata = pageMetadata("docs/api/codegen")
  3. # @json-render/codegen
  4. Utilities for generating code from UI trees.
  5. ## Tree Traversal
  6. ### traverseSpec
  7. Walk the UI spec depth-first.
  8. ```typescript
  9. function traverseSpec(
  10. spec: Spec,
  11. visitor: TreeVisitor,
  12. startKey?: string
  13. ): void
  14. interface TreeVisitor {
  15. (element: UIElement, key: string, depth: number, parent: UIElement | null): void;
  16. }
  17. ```
  18. ### collectUsedComponents
  19. Get all unique component types used in a spec.
  20. ```typescript
  21. function collectUsedComponents(spec: Spec): Set<string>
  22. // Example
  23. const components = collectUsedComponents(spec);
  24. // Set { 'Card', 'Metric', 'Chart' }
  25. ```
  26. ### collectStatePaths
  27. Get all state paths referenced in props (statePath, bindPath, etc.).
  28. ```typescript
  29. function collectStatePaths(spec: Spec): Set<string>
  30. // Example
  31. const paths = collectStatePaths(spec);
  32. // Set { 'analytics/revenue', 'analytics/customers' }
  33. ```
  34. ### collectActions
  35. Get all action names used in the spec.
  36. ```typescript
  37. function collectActions(spec: Spec): Set<string>
  38. // Example
  39. const actions = collectActions(spec);
  40. // Set { 'submit_form', 'refresh_data' }
  41. ```
  42. ## Serialization
  43. ### serializePropValue
  44. Serialize a single value to a code string.
  45. ```typescript
  46. function serializePropValue(
  47. value: unknown,
  48. options?: SerializeOptions
  49. ): { value: string; needsBraces: boolean }
  50. // Examples
  51. serializePropValue("hello")
  52. // { value: '"hello"', needsBraces: false }
  53. serializePropValue(42)
  54. // { value: '42', needsBraces: true }
  55. serializePropValue({ $state: '/user/name' })
  56. // { value: '{ $state: "/user/name" }', needsBraces: true }
  57. ```
  58. ### serializeProps
  59. Serialize a props object to a JSX attributes string.
  60. ```typescript
  61. function serializeProps(
  62. props: Record<string, unknown>,
  63. options?: SerializeOptions
  64. ): string
  65. // Example
  66. serializeProps({ title: 'Dashboard', columns: 3, disabled: true })
  67. // 'title="Dashboard" columns={3} disabled'
  68. ```
  69. ### escapeString
  70. Escape a string for use in code.
  71. ```typescript
  72. function escapeString(
  73. str: string,
  74. quotes?: 'single' | 'double'
  75. ): string
  76. ```
  77. ## Types
  78. ### GeneratedFile
  79. ```typescript
  80. interface GeneratedFile {
  81. /** File path relative to project root */
  82. path: string;
  83. /** File contents */
  84. content: string;
  85. }
  86. ```
  87. ### CodeGenerator
  88. ```typescript
  89. interface CodeGenerator {
  90. /** Generate files from a UI spec */
  91. generate(spec: Spec): GeneratedFile[];
  92. }
  93. ```
  94. ### SerializeOptions
  95. ```typescript
  96. interface SerializeOptions {
  97. /** Quote style for strings */
  98. quotes?: 'single' | 'double';
  99. /** Indent for objects/arrays */
  100. indent?: number;
  101. }
  102. ```