@alloy-js/core 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (229) hide show
  1. package/LICENSE.txt +7 -0
  2. package/api-extractor.json +11 -0
  3. package/babel.config.cjs +4 -0
  4. package/dist/src/binder.d.ts +333 -0
  5. package/dist/src/binder.d.ts.map +1 -0
  6. package/dist/src/binder.js +444 -0
  7. package/dist/src/binder.js.map +1 -0
  8. package/dist/src/code.d.ts +3 -0
  9. package/dist/src/code.d.ts.map +1 -0
  10. package/dist/src/code.js +156 -0
  11. package/dist/src/code.js.map +1 -0
  12. package/dist/src/components/Declaration.d.ts +29 -0
  13. package/dist/src/components/Declaration.d.ts.map +1 -0
  14. package/dist/src/components/Declaration.js +47 -0
  15. package/dist/src/components/Declaration.js.map +1 -0
  16. package/dist/src/components/Indent.d.ts +13 -0
  17. package/dist/src/components/Indent.d.ts.map +1 -0
  18. package/dist/src/components/Indent.js +23 -0
  19. package/dist/src/components/Indent.js.map +1 -0
  20. package/dist/src/components/MemberDeclaration.d.ts +30 -0
  21. package/dist/src/components/MemberDeclaration.d.ts.map +1 -0
  22. package/dist/src/components/MemberDeclaration.js +52 -0
  23. package/dist/src/components/MemberDeclaration.js.map +1 -0
  24. package/dist/src/components/MemberName.d.ts +2 -0
  25. package/dist/src/components/MemberName.d.ts.map +1 -0
  26. package/dist/src/components/MemberName.js +11 -0
  27. package/dist/src/components/MemberName.js.map +1 -0
  28. package/dist/src/components/MemberScope.d.ts +27 -0
  29. package/dist/src/components/MemberScope.d.ts.map +1 -0
  30. package/dist/src/components/MemberScope.js +28 -0
  31. package/dist/src/components/MemberScope.js.map +1 -0
  32. package/dist/src/components/Name.d.ts +2 -0
  33. package/dist/src/components/Name.d.ts.map +1 -0
  34. package/dist/src/components/Name.js +11 -0
  35. package/dist/src/components/Name.js.map +1 -0
  36. package/dist/src/components/Output.d.ts +31 -0
  37. package/dist/src/components/Output.d.ts.map +1 -0
  38. package/dist/src/components/Output.js +44 -0
  39. package/dist/src/components/Output.js.map +1 -0
  40. package/dist/src/components/Scope.d.ts +10 -0
  41. package/dist/src/components/Scope.d.ts.map +1 -0
  42. package/dist/src/components/Scope.js +25 -0
  43. package/dist/src/components/Scope.js.map +1 -0
  44. package/dist/src/components/SourceDirectory.d.ts +7 -0
  45. package/dist/src/components/SourceDirectory.d.ts.map +1 -0
  46. package/dist/src/components/SourceDirectory.js +38 -0
  47. package/dist/src/components/SourceDirectory.js.map +1 -0
  48. package/dist/src/components/SourceFile.d.ts +12 -0
  49. package/dist/src/components/SourceFile.d.ts.map +1 -0
  50. package/dist/src/components/SourceFile.js +26 -0
  51. package/dist/src/components/SourceFile.js.map +1 -0
  52. package/dist/src/components/index.d.ts +11 -0
  53. package/dist/src/components/index.d.ts.map +1 -0
  54. package/dist/src/components/index.js +11 -0
  55. package/dist/src/components/index.js.map +1 -0
  56. package/dist/src/components/stc/index.d.ts +26 -0
  57. package/dist/src/components/stc/index.d.ts.map +1 -0
  58. package/dist/src/components/stc/index.js +9 -0
  59. package/dist/src/components/stc/index.js.map +1 -0
  60. package/dist/src/context/assignment.d.ts +39 -0
  61. package/dist/src/context/assignment.d.ts.map +1 -0
  62. package/dist/src/context/assignment.js +39 -0
  63. package/dist/src/context/assignment.js.map +1 -0
  64. package/dist/src/context/binder.d.ts +9 -0
  65. package/dist/src/context/binder.d.ts.map +1 -0
  66. package/dist/src/context/binder.js +12 -0
  67. package/dist/src/context/binder.js.map +1 -0
  68. package/dist/src/context/declaration.d.ts +4 -0
  69. package/dist/src/context/declaration.d.ts.map +1 -0
  70. package/dist/src/context/declaration.js +3 -0
  71. package/dist/src/context/declaration.js.map +1 -0
  72. package/dist/src/context/indent.d.ts +5 -0
  73. package/dist/src/context/indent.d.ts.map +1 -0
  74. package/dist/src/context/indent.js +8 -0
  75. package/dist/src/context/indent.js.map +1 -0
  76. package/dist/src/context/index.d.ts +11 -0
  77. package/dist/src/context/index.d.ts.map +1 -0
  78. package/dist/src/context/index.js +11 -0
  79. package/dist/src/context/index.js.map +1 -0
  80. package/dist/src/context/member-declaration.d.ts +9 -0
  81. package/dist/src/context/member-declaration.d.ts.map +1 -0
  82. package/dist/src/context/member-declaration.js +9 -0
  83. package/dist/src/context/member-declaration.js.map +1 -0
  84. package/dist/src/context/member-scope.d.ts +13 -0
  85. package/dist/src/context/member-scope.d.ts.map +1 -0
  86. package/dist/src/context/member-scope.js +12 -0
  87. package/dist/src/context/member-scope.js.map +1 -0
  88. package/dist/src/context/name-policy.d.ts +5 -0
  89. package/dist/src/context/name-policy.d.ts.map +1 -0
  90. package/dist/src/context/name-policy.js +10 -0
  91. package/dist/src/context/name-policy.js.map +1 -0
  92. package/dist/src/context/scope.d.ts +5 -0
  93. package/dist/src/context/scope.d.ts.map +1 -0
  94. package/dist/src/context/scope.js +6 -0
  95. package/dist/src/context/scope.js.map +1 -0
  96. package/dist/src/context/source-directory.d.ts +9 -0
  97. package/dist/src/context/source-directory.d.ts.map +1 -0
  98. package/dist/src/context/source-directory.js +3 -0
  99. package/dist/src/context/source-directory.js.map +1 -0
  100. package/dist/src/context/source-file.d.ts +12 -0
  101. package/dist/src/context/source-file.d.ts.map +1 -0
  102. package/dist/src/context/source-file.js +3 -0
  103. package/dist/src/context/source-file.js.map +1 -0
  104. package/dist/src/context.d.ts +13 -0
  105. package/dist/src/context.d.ts.map +1 -0
  106. package/dist/src/context.js +30 -0
  107. package/dist/src/context.js.map +1 -0
  108. package/dist/src/index.d.ts +13 -0
  109. package/dist/src/index.d.ts.map +1 -0
  110. package/dist/src/index.js +13 -0
  111. package/dist/src/index.js.map +1 -0
  112. package/dist/src/jsx-runtime.d.ts +43 -0
  113. package/dist/src/jsx-runtime.d.ts.map +1 -0
  114. package/dist/src/jsx-runtime.js +172 -0
  115. package/dist/src/jsx-runtime.js.map +1 -0
  116. package/dist/src/name-policy.d.ts +5 -0
  117. package/dist/src/name-policy.d.ts.map +1 -0
  118. package/dist/src/name-policy.js +8 -0
  119. package/dist/src/name-policy.js.map +1 -0
  120. package/dist/src/refkey.d.ts +9 -0
  121. package/dist/src/refkey.d.ts.map +1 -0
  122. package/dist/src/refkey.js +44 -0
  123. package/dist/src/refkey.js.map +1 -0
  124. package/dist/src/render.d.ts +147 -0
  125. package/dist/src/render.d.ts.map +1 -0
  126. package/dist/src/render.js +317 -0
  127. package/dist/src/render.js.map +1 -0
  128. package/dist/src/tsdoc-metadata.json +11 -0
  129. package/dist/src/utils.d.ts +80 -0
  130. package/dist/src/utils.d.ts.map +1 -0
  131. package/dist/src/utils.js +219 -0
  132. package/dist/src/utils.js.map +1 -0
  133. package/dist/test/children.test.d.ts +2 -0
  134. package/dist/test/children.test.d.ts.map +1 -0
  135. package/dist/test/components/source-file.test.d.ts +2 -0
  136. package/dist/test/components/source-file.test.d.ts.map +1 -0
  137. package/dist/test/name-policy.test.d.ts +2 -0
  138. package/dist/test/name-policy.test.d.ts.map +1 -0
  139. package/dist/test/reactivity/ref-rendering.test.d.ts +2 -0
  140. package/dist/test/reactivity/ref-rendering.test.d.ts.map +1 -0
  141. package/dist/test/reactivity/test.test.d.ts +2 -0
  142. package/dist/test/reactivity/test.test.d.ts.map +1 -0
  143. package/dist/test/refkey.test.d.ts +2 -0
  144. package/dist/test/refkey.test.d.ts.map +1 -0
  145. package/dist/test/rendering/basic.test.d.ts +2 -0
  146. package/dist/test/rendering/basic.test.d.ts.map +1 -0
  147. package/dist/test/rendering/code.test.d.ts +2 -0
  148. package/dist/test/rendering/code.test.d.ts.map +1 -0
  149. package/dist/test/rendering/indent.test.d.ts +2 -0
  150. package/dist/test/rendering/indent.test.d.ts.map +1 -0
  151. package/dist/test/rendering/linebreaks.test.d.ts +2 -0
  152. package/dist/test/rendering/linebreaks.test.d.ts.map +1 -0
  153. package/dist/test/rendering/refkeys.test.d.ts +2 -0
  154. package/dist/test/rendering/refkeys.test.d.ts.map +1 -0
  155. package/dist/test/stc.test.d.ts +2 -0
  156. package/dist/test/stc.test.d.ts.map +1 -0
  157. package/dist/test/symbols.test.d.ts +2 -0
  158. package/dist/test/symbols.test.d.ts.map +1 -0
  159. package/dist/test/utils.test.d.ts +2 -0
  160. package/dist/test/utils.test.d.ts.map +1 -0
  161. package/dist/testing/extend-expect.d.ts +2 -0
  162. package/dist/testing/extend-expect.d.ts.map +1 -0
  163. package/dist/testing/extend-expect.js +22 -0
  164. package/dist/testing/extend-expect.js.map +1 -0
  165. package/dist/testing/index.d.ts +3 -0
  166. package/dist/testing/index.d.ts.map +1 -0
  167. package/dist/testing/index.js +3 -0
  168. package/dist/testing/index.js.map +1 -0
  169. package/dist/testing/render.d.ts +7 -0
  170. package/dist/testing/render.d.ts.map +1 -0
  171. package/dist/testing/render.js +25 -0
  172. package/dist/testing/render.js.map +1 -0
  173. package/dist/testing/vitest.d.js +1 -0
  174. package/dist/testing/vitest.d.js.map +1 -0
  175. package/dist/tsconfig.tsbuildinfo +1 -0
  176. package/package.json +64 -0
  177. package/src/binder.ts +838 -0
  178. package/src/code.ts +220 -0
  179. package/src/components/Declaration.tsx +53 -0
  180. package/src/components/Indent.tsx +33 -0
  181. package/src/components/MemberDeclaration.tsx +62 -0
  182. package/src/components/MemberName.tsx +11 -0
  183. package/src/components/MemberScope.tsx +40 -0
  184. package/src/components/Name.tsx +11 -0
  185. package/src/components/Output.tsx +69 -0
  186. package/src/components/Scope.tsx +27 -0
  187. package/src/components/SourceDirectory.tsx +43 -0
  188. package/src/components/SourceFile.tsx +33 -0
  189. package/src/components/index.tsx +10 -0
  190. package/src/components/stc/index.ts +9 -0
  191. package/src/context/assignment.ts +57 -0
  192. package/src/context/binder.ts +14 -0
  193. package/src/context/declaration.ts +5 -0
  194. package/src/context/indent.ts +10 -0
  195. package/src/context/index.ts +10 -0
  196. package/src/context/member-declaration.ts +10 -0
  197. package/src/context/member-scope.ts +17 -0
  198. package/src/context/name-policy.ts +13 -0
  199. package/src/context/scope.ts +8 -0
  200. package/src/context/source-directory.ts +11 -0
  201. package/src/context/source-file.ts +12 -0
  202. package/src/context.ts +53 -0
  203. package/src/index.ts +21 -0
  204. package/src/jsx-runtime.ts +266 -0
  205. package/src/name-policy.ts +13 -0
  206. package/src/refkey.ts +62 -0
  207. package/src/render.ts +389 -0
  208. package/src/utils.ts +288 -0
  209. package/temp/api.json +8840 -0
  210. package/test/children.test.tsx +33 -0
  211. package/test/components/source-file.test.tsx +45 -0
  212. package/test/name-policy.test.tsx +19 -0
  213. package/test/reactivity/ref-rendering.test.tsx +50 -0
  214. package/test/reactivity/test.test.tsx +83 -0
  215. package/test/refkey.test.ts +32 -0
  216. package/test/rendering/basic.test.tsx +156 -0
  217. package/test/rendering/code.test.tsx +62 -0
  218. package/test/rendering/indent.test.tsx +608 -0
  219. package/test/rendering/linebreaks.test.tsx +72 -0
  220. package/test/rendering/refkeys.test.tsx +35 -0
  221. package/test/stc.test.tsx +21 -0
  222. package/test/symbols.test.ts +406 -0
  223. package/test/utils.test.tsx +150 -0
  224. package/testing/extend-expect.ts +20 -0
  225. package/testing/index.ts +2 -0
  226. package/testing/render.ts +37 -0
  227. package/testing/vitest.d.ts +10 -0
  228. package/tsconfig.json +17 -0
  229. package/vitest.config.ts +18 -0
package/src/render.ts ADDED
@@ -0,0 +1,389 @@
1
+ import {
2
+ Child,
3
+ Children,
4
+ Context,
5
+ effect,
6
+ getContext,
7
+ isComponentCreator,
8
+ popStack,
9
+ printRenderStack,
10
+ pushStack,
11
+ root,
12
+ untrack,
13
+ } from "@alloy-js/core/jsx-runtime";
14
+ import { isRef } from "@vue/reactivity";
15
+ import { Indent, IndentState } from "./components/Indent.js";
16
+ import { useContext } from "./context.js";
17
+ import { IndentContext } from "./context/indent.js";
18
+ import { SourceFileContext } from "./context/source-file.js";
19
+ import { isRefkey } from "./refkey.js";
20
+
21
+ /**
22
+ * The component tree is constructed as the result of transforming JSX with
23
+ * `@alloy-js/babel-preset`. Elements in the component tree (represented by the type
24
+ * Children) are three distinct types of things:
25
+ *
26
+ * 1. Primitive data types, which are either literal JSX or substitutions
27
+ * 2. Components, which are created via `createComponent`
28
+ * 3. Memos, which are created via `memo`, and represent substitutions like
29
+ * property accesses and function calls that might be reactive.
30
+ *
31
+ * This tree is then compiled into a render tree, which is a normalized form of
32
+ * the component tree. The render tree is constructed by traversing the
33
+ * component tree, invoking components, wrapping memos, doing whitespace
34
+ * normalization, and other activities. There are four types of nodes in the
35
+ * render tree.
36
+ *
37
+ * 1. Strings, which are either literal JSX or substitutions. Other primitive
38
+ * types are either converted to the empty string or stringified as
39
+ * appropriate.
40
+ * 2. Components, which are possibly wrapped if they are indented.
41
+ * 3. Memos, which are wrapped in a reactive effect which updates its render
42
+ * tree nodes when its value changes.
43
+ * 4. Arrays of these things.
44
+ *
45
+ * The render tree is whitespace normalized and indentation preserving. When the
46
+ * component increases the literal indent level and then embeds a component,
47
+ * memo, or array, the contents of that substitution are indented appropriately.
48
+ * This is accomplished by wrapping those substitutions in an indent component.
49
+ *
50
+ * So the high level process for rendering while normalizing whitespace is as
51
+ * follows:
52
+ *
53
+ * 1. For an array of elements in the render tree (which may be a component or
54
+ * array of elements):
55
+ * 1. Normalize all primitive values other than strings to strings.
56
+ * Recursively normalize nested array elements.
57
+ * 2. Use the first text node to determine the literal indent level of the
58
+ * children. Remove all preceding whitespace - any indent of the first
59
+ * line is provided in the text nodes preceding the reference to this
60
+ * component. If the first element is not a literal string, then no
61
+ * literal indent is applied, and all indentation within the component
62
+ * becomes significant.
63
+ * 3. For each child of the component, render it:
64
+ * 1. If it is a string, reindent it by splitting on lines and replacing
65
+ * the detected literal whitespace with the current indent level,
66
+ * skipping the first line. If the string ends with a larger literal
67
+ * indent than the detected literal indent, then a subsequent child
68
+ * will be indented.
69
+ * 2. If it's a component, if the next child should be indented, create an
70
+ * Indent component and wrap the component's children in it.
71
+ * 3. If it's a function, if the next child should be indented, wrap it in
72
+ * an indent component. Any elements processed as a result of executing
73
+ * the memo are treated as first elements in a child array are with
74
+ * respect to establishing literal indent level and whitespace trimming
75
+ * behavior.
76
+ * 4. If it's an array, if the next child should be indented, create an
77
+ * Indent component and wrap it the array in it.
78
+ *
79
+ * Let's look at a few examples of each of these phases:
80
+ *
81
+ * ## Explicit indentation
82
+ *
83
+ * ### Input
84
+ * ```
85
+ * <Indent>
86
+ * <Foo />
87
+ * <Foo />
88
+ * </Indent>
89
+ * ```
90
+ *
91
+ * ### Compiled tree
92
+ * ```
93
+ * [
94
+ * createComponent(Indent, {
95
+ * get children() {
96
+ * return [
97
+ * "\n ",
98
+ * createComponent(Foo, {}),
99
+ * "\n ",
100
+ * createComponent(Foo, {}),
101
+ * "\n"
102
+ * ]
103
+ * }
104
+ * })
105
+ * ]
106
+ * ```
107
+ *
108
+ * ### Render tree
109
+ * ```
110
+ * [ // node for Indent
111
+ * [ // node for Context Provider
112
+ * " ", // indent from the children of Indent
113
+ * [ // component for Foo
114
+ * "Foo" // result of calling Foo
115
+ * ],
116
+ * "\n ", // indent and line break from the children of Ident
117
+ * [ "Foo" ] // second foo component
118
+ * ]
119
+ * ]
120
+ * ```
121
+ * ### Rendered text
122
+ * ```
123
+ * FooFoo
124
+ * ```
125
+ *
126
+ * ## Implicit indentation
127
+ *
128
+ * ### Input
129
+ * ```
130
+ * <>
131
+ * base
132
+ * <Foo /> <Foo />
133
+ * </>
134
+ * ```
135
+ *
136
+ * ### Render tree
137
+ * ```
138
+ * [ // node for top-level fragment
139
+ * "base\n ", // contents of fragment, including trailing indent
140
+ * [ // node for implicitly created Indent component
141
+ * [ // node for its context provider [ "Foo" ], // contents of Foo "\n"
142
+ * ]
143
+ * ```
144
+ * ## Rendered text
145
+ * ```
146
+ * base
147
+ * Foo Foo
148
+ * ```
149
+ */
150
+
151
+ //
152
+ export interface OutputDirectory {
153
+ kind: "directory";
154
+ path: string;
155
+ contents: (OutputDirectory | OutputFile)[];
156
+ }
157
+
158
+ export interface OutputFile {
159
+ kind: "file";
160
+ contents: string;
161
+ path: string;
162
+ filetype: string;
163
+ }
164
+
165
+ const nodesToContext = new WeakMap<RenderTextTree, Context>();
166
+
167
+ export function getContextForRenderNode(node: RenderTextTree) {
168
+ return nodesToContext.get(node);
169
+ }
170
+ export type RenderStructure = {};
171
+
172
+ export type RenderTextTree = (string | RenderTextTree)[];
173
+
174
+ function traceRender(phase: string, message: string) {
175
+ return false;
176
+ // console.log(`[\x1b[34m${phase}\x1b[0m]: ${message}`);
177
+ }
178
+
179
+ export function render(children: Children): OutputDirectory {
180
+ const tree = renderTree(children);
181
+ let rootDirectory: OutputDirectory | undefined = undefined;
182
+ collectSourceFiles(undefined, tree);
183
+
184
+ if (!rootDirectory) {
185
+ throw new Error(
186
+ "No root directory found. Make sure you are using the Output component.",
187
+ );
188
+ }
189
+
190
+ return rootDirectory;
191
+
192
+ function collectSourceFiles(
193
+ currentDirectory: OutputDirectory | undefined,
194
+ root: RenderTextTree,
195
+ ) {
196
+ if (!Array.isArray(root)) {
197
+ return;
198
+ }
199
+ const context = getContextForRenderNode(root);
200
+
201
+ if (!context) {
202
+ return recurse(currentDirectory);
203
+ }
204
+
205
+ if (context.meta?.directory) {
206
+ const directory: OutputDirectory = {
207
+ kind: "directory",
208
+ path: context.meta?.directory.path,
209
+ contents: [],
210
+ };
211
+
212
+ if (currentDirectory) {
213
+ currentDirectory.contents.push(directory);
214
+ } else {
215
+ rootDirectory = directory;
216
+ }
217
+ recurse(directory);
218
+ } else if (context.meta?.sourceFile) {
219
+ if (!currentDirectory) {
220
+ // This shouldn't happen if you're using the Output component.
221
+ throw new Error(
222
+ "Source file doesn't have parent directory. Make sure you have used the Output component.",
223
+ );
224
+ }
225
+ const sourceFile: OutputFile = {
226
+ kind: "file",
227
+ path: context.meta?.sourceFile.path,
228
+ filetype: context.meta?.sourceFile.filetype,
229
+ contents: (root as any).flat(Infinity).join(""),
230
+ };
231
+
232
+ currentDirectory.contents.push(sourceFile);
233
+ } else {
234
+ recurse(currentDirectory);
235
+ }
236
+
237
+ function recurse(cwd: OutputDirectory | undefined) {
238
+ for (const child of root) {
239
+ collectSourceFiles(cwd, child as RenderTextTree);
240
+ }
241
+ }
242
+ }
243
+ }
244
+
245
+ export function renderTree(children: Children) {
246
+ const rootElem: RenderTextTree = [];
247
+ const state: RenderState = {
248
+ newline: false,
249
+ };
250
+ try {
251
+ root(() => {
252
+ renderWorker(rootElem, children, state);
253
+ }, "render worker");
254
+ } catch (e) {
255
+ printRenderStack();
256
+ throw e;
257
+ }
258
+
259
+ return rootElem;
260
+ }
261
+
262
+ interface RenderState {
263
+ newline: boolean;
264
+ }
265
+
266
+ function renderWorker(
267
+ node: RenderTextTree,
268
+ children: Children,
269
+ state: RenderState,
270
+ ) {
271
+ traceRender("render", dumpChildren(children));
272
+
273
+ if (Array.isArray(node)) {
274
+ nodesToContext.set(node, getContext()!);
275
+ }
276
+
277
+ const indent = useContext(IndentContext)!;
278
+ if (Array.isArray(children)) {
279
+ for (const child of children) {
280
+ appendChild(node, child, indent, state);
281
+ }
282
+ } else {
283
+ appendChild(node, children, indent, state);
284
+ }
285
+ }
286
+
287
+ function appendChild(
288
+ node: RenderTextTree,
289
+ rawChild: Child,
290
+ indentState: IndentState,
291
+ state: RenderState,
292
+ ) {
293
+ traceRender("appendChild", printChild(rawChild));
294
+ const child = normalizeChild(rawChild);
295
+
296
+ if (typeof child === "string") {
297
+ if (child.match(/\n\s*$/)) {
298
+ state.newline = true;
299
+ } else {
300
+ state.newline = false;
301
+ }
302
+ const reindented = reindent(child, indentState.indentString);
303
+ traceRender("appendChild:string", JSON.stringify(reindented));
304
+ node.push(reindented);
305
+ } else if (isComponentCreator(child)) {
306
+ root(() => {
307
+ traceRender("appendChild:component", printChild(child));
308
+ if (child.component === Indent && state.newline) {
309
+ node.push(indentState.indent);
310
+ }
311
+ const componentRoot: RenderTextTree = [];
312
+ pushStack(child.component, child.props);
313
+ renderWorker(componentRoot, untrack(child), state);
314
+ popStack();
315
+ node.push(componentRoot);
316
+ traceRender("appendChild:component-done", printChild(child));
317
+ }, child.component.name);
318
+ } else if (typeof child === "function") {
319
+ traceRender("appendChild:memo", child.toString());
320
+ const index = node.length;
321
+ effect((prev: any) => {
322
+ traceRender("memoEffect:run", "");
323
+ let res = child();
324
+ while (typeof res === "function" && !isComponentCreator(res)) {
325
+ res = res();
326
+ }
327
+ const newNodes: RenderTextTree = [];
328
+ renderWorker(newNodes, res, state);
329
+ //node.splice(index, prev ? prev.length : 0, ...newNodes);
330
+ node[index] = newNodes;
331
+ return newNodes;
332
+ });
333
+ traceRender("appendChild:memo-done", "");
334
+ } else {
335
+ traceRender("appendChild:array", dumpChildren(child));
336
+ renderWorker(node, child, state);
337
+ traceRender("appendChild:array-done", dumpChildren(child));
338
+ }
339
+ }
340
+
341
+ function reindent(str: string, indent: string) {
342
+ const lines = str.split("\n");
343
+ return [lines[0], ...lines.slice(1).map((line) => indent + line)].join("\n");
344
+ }
345
+ type NormalizedChild = string | (() => Child | Children) | NormalizedChild[];
346
+
347
+ function normalizeChild(child: Child): NormalizedChild {
348
+ if (Array.isArray(child)) {
349
+ return child.map(normalizeChild);
350
+ } else if (typeof child === "string" || typeof child === "function") {
351
+ return child as NormalizedChild;
352
+ } else if (
353
+ typeof child === "undefined" ||
354
+ child === null ||
355
+ typeof child === "boolean"
356
+ ) {
357
+ return "";
358
+ } else if (isRef(child)) {
359
+ return () => child.value as () => Child;
360
+ } else if (isRefkey(child)) {
361
+ return () => {
362
+ const sfContext = useContext(SourceFileContext);
363
+ if (!sfContext || !sfContext.reference) {
364
+ throw new Error("Can only emit references inside of source files");
365
+ }
366
+
367
+ return sfContext.reference({ refkey: child });
368
+ };
369
+ } else {
370
+ return String(child);
371
+ }
372
+ }
373
+
374
+ function dumpChildren(children: Child | Children): string {
375
+ if (Array.isArray(children)) {
376
+ return `[ ${children.map(printChild).join(", ")} ]`;
377
+ }
378
+ return printChild(children);
379
+ }
380
+
381
+ function printChild(child: Child): string {
382
+ if (isComponentCreator(child)) {
383
+ return "<" + child.component.name + ">";
384
+ } else if (typeof child === "function") {
385
+ return "$memo";
386
+ } else {
387
+ return JSON.stringify(child);
388
+ }
389
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,288 @@
1
+ import {
2
+ Child,
3
+ Children,
4
+ ComponentCreator,
5
+ ComponentDefinition,
6
+ isComponentCreator,
7
+ memo,
8
+ } from "@alloy-js/core/jsx-runtime";
9
+ import { mkdirSync, statSync, writeFileSync } from "node:fs";
10
+ import { relative, resolve } from "pathe";
11
+ import { code } from "./code.js";
12
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
13
+ import { OutputDirectory, OutputFile, render } from "./render.js";
14
+
15
+ export interface JoinOptions {
16
+ /**
17
+ * The string to place between each element.
18
+ */
19
+ joiner?: string;
20
+
21
+ /**
22
+ * When true, the joiner is placed at the end of the array. When a string,
23
+ * that string is placed at the end of the array. The ender is only emitted
24
+ * when the array has at least one element.
25
+ */
26
+ ender?: string | boolean;
27
+ }
28
+ const defaultJoinOptions: JoinOptions = {
29
+ joiner: "\n",
30
+ ender: false,
31
+ };
32
+
33
+ /**
34
+ * Map a Map to an array using a mapper and place a joiner between each element.
35
+ * Defaults to joining with a newline.
36
+ *
37
+ * @see {@link join} for joining without mapping.
38
+ * @param src - Source map.
39
+ * @param cb - Mapper function.
40
+ * @param options - Join options.
41
+ * @returns The mapped and joined array.
42
+ *
43
+ */
44
+ export function mapJoin<T, U, V>(
45
+ src: Map<T, U>,
46
+ cb: (key: T, value: U) => V,
47
+ options?: JoinOptions,
48
+ ): (V | string)[];
49
+ /**
50
+ * Map a array or iterator to another array using a mapper and place a joiner
51
+ * between each element. Defaults to joining with a newline.
52
+ *
53
+ * @see {@link join} for joining without mapping.
54
+ * @param src - Source array.
55
+ * @param cb - Mapper function.
56
+ * @param options - Join options.
57
+ * @returns The mapped and joined array.
58
+ */
59
+ export function mapJoin<T, V>(
60
+ src: T[] | IterableIterator<T>,
61
+ cb: (value: T) => V,
62
+ options?: JoinOptions,
63
+ ): (V | string)[];
64
+ export function mapJoin<T, U, V>(
65
+ src: Map<T, U> | T[] | Iterable<T>,
66
+ cb: (key: T, value?: U) => V,
67
+ rawOptions: JoinOptions = {},
68
+ ): (V | string)[] {
69
+ const options = { ...defaultJoinOptions, ...rawOptions };
70
+ const ender = options.ender === true ? options.joiner : options.ender;
71
+
72
+ const mapped: (V | string)[] = [];
73
+ if (typeof (src as any).next === "function") {
74
+ src = Array.from(src as Iterable<T>);
75
+ }
76
+
77
+ if (Array.isArray(src)) {
78
+ for (const [index, item] of src.entries()) {
79
+ mapped.push(cb(item));
80
+ if (index !== src.length - 1) {
81
+ mapped.push(options.joiner!);
82
+ }
83
+ }
84
+ if (src.length > 0 && ender) {
85
+ mapped.push(ender);
86
+ }
87
+ } else {
88
+ const entries = [...(src as Map<T, U>).entries()];
89
+ for (const [index, [key, value]] of entries.entries()) {
90
+ mapped.push(cb(key, value));
91
+ if (index !== entries.length - 1) {
92
+ mapped.push(options.joiner!);
93
+ }
94
+ }
95
+ if (entries.length > 0 && ender) {
96
+ mapped.push(ender);
97
+ }
98
+ }
99
+
100
+ return mapped;
101
+ }
102
+
103
+ /**
104
+ * Place a joiner between each element of an array or iterator. Defaults to
105
+ * joining with a newline.
106
+ *
107
+ * @see mapJoin for mapping before joining.
108
+ * @returns The joined array
109
+ */
110
+ export function join<T>(
111
+ src: T[] | Iterator<T>,
112
+ options: JoinOptions = {},
113
+ ): (T | string)[] {
114
+ const mergedOptions = { ...defaultJoinOptions, ...options };
115
+ const joined = [];
116
+ const ender =
117
+ mergedOptions.ender === true ? mergedOptions.joiner : mergedOptions.ender;
118
+ src = Array.from(src as Iterable<T>);
119
+
120
+ for (const [index, item] of src.entries()) {
121
+ joined.push(item);
122
+ if (index !== src.length - 1) {
123
+ joined.push(mergedOptions.joiner!);
124
+ }
125
+ }
126
+
127
+ if (src.length > 0 && ender) {
128
+ joined.push(ender);
129
+ }
130
+
131
+ return joined;
132
+ }
133
+
134
+ /**
135
+ * Returns a memo which is a list of all the provided children.
136
+ * If you want this as an array, see {@link childrenArray}.
137
+ */
138
+ export function children(fn: () => Children): () => Children {
139
+ return memo(() => collectChildren(fn()));
140
+
141
+ function collectChildren(children: Children): Children {
142
+ if (Array.isArray(children)) {
143
+ return children.map(collectChildren).flat();
144
+ } else if (
145
+ typeof children === "function" &&
146
+ !isComponentCreator(children)
147
+ ) {
148
+ return collectChildren(children());
149
+ } else {
150
+ return children;
151
+ }
152
+ }
153
+ }
154
+
155
+ export function childrenArray(fn: () => Children): Child[] {
156
+ const c = children(fn)();
157
+ if (Array.isArray(c)) {
158
+ return c;
159
+ } else if (c === undefined) {
160
+ return [];
161
+ } else {
162
+ return [c];
163
+ }
164
+ }
165
+
166
+ export function findKeyedChild(children: Child[], tag: symbol) {
167
+ for (const child of children) {
168
+ if (isKeyedChild(child) && child.tag === tag) {
169
+ return child;
170
+ }
171
+ }
172
+
173
+ return null;
174
+ }
175
+
176
+ export function findUnkeyedChildren(children: Child[]) {
177
+ return children.filter((child) => !isKeyedChild(child));
178
+ }
179
+
180
+ export function isKeyedChild(child: Child): child is ComponentCreator {
181
+ return isComponentCreator(child) && !!child.tag;
182
+ }
183
+
184
+ export function stc<T extends {}>(Component: ComponentDefinition<T>) {
185
+ return (
186
+ ...args: unknown extends T ? []
187
+ : {} extends Omit<T, "children"> ? [props?: T]
188
+ : [props: T]
189
+ ) => {
190
+ const fn: ComponentCreator<T> & {
191
+ code(
192
+ template: TemplateStringsArray,
193
+ ...substitutions: Children[]
194
+ ): ComponentCreator<T>;
195
+ children(...children: Children[]): ComponentCreator<T>;
196
+ } = () => Component(args[0]!);
197
+ fn.component = Component;
198
+ fn.props = args[0]!;
199
+ fn.code = (
200
+ template: TemplateStringsArray,
201
+ ...substitutions: Children[]
202
+ ): ComponentCreator<T> => {
203
+ const propsWithChildren = {
204
+ ...(args[0] ?? {}),
205
+ children: code(template, ...substitutions),
206
+ };
207
+
208
+ const fn = () => Component(propsWithChildren as any);
209
+ fn.component = Component;
210
+ fn.props = args[0]!;
211
+ return fn;
212
+ };
213
+
214
+ fn.children = (...children: Children[]): ComponentCreator<T> => {
215
+ const propsWithChildren = {
216
+ ...(args[0] ?? {}),
217
+ children,
218
+ };
219
+
220
+ const fn = () => Component(propsWithChildren as any);
221
+ fn.component = Component;
222
+ fn.props = args[0]!;
223
+ return fn;
224
+ };
225
+
226
+ return fn;
227
+ };
228
+ }
229
+
230
+ /**
231
+ * A visitor to collect the output from {@link render}. Used by
232
+ * {@link traverseOutput}.
233
+ */
234
+ export interface OutputVisitor {
235
+ visitDirectory(directory: OutputDirectory): void;
236
+ visitFile(file: OutputFile): void;
237
+ }
238
+
239
+ /**
240
+ * Traverse the output from {@link render} and call the visitor for each
241
+ * file and directory within it.
242
+ *
243
+ * @param sourceDirectory - The root directory to traverse.
244
+ * @param visitor - The visitor to call for each file and directory.
245
+ */
246
+ export function traverseOutput(
247
+ sourceDirectory: OutputDirectory,
248
+ visitor: OutputVisitor,
249
+ ) {
250
+ visitor.visitDirectory(sourceDirectory);
251
+ for (const item of sourceDirectory.contents) {
252
+ if (item.kind === "directory") {
253
+ traverseOutput(item, visitor);
254
+ } else {
255
+ visitor.visitFile(item);
256
+ }
257
+ }
258
+ }
259
+
260
+ /**
261
+ * Write the output from {@link render} to the file system.
262
+ *
263
+ */
264
+ export function writeOutput(output: OutputDirectory, basePath: string = "") {
265
+ traverseOutput(output, {
266
+ visitDirectory(directory) {
267
+ const path = resolve(basePath, directory.path);
268
+ if (statSync(path)) {
269
+ return;
270
+ }
271
+ // eslint-disable-next-line no-console
272
+ console.log("create", relative(process.cwd(), path));
273
+ mkdirSync(path, { recursive: true });
274
+ },
275
+ visitFile(file) {
276
+ const path = resolve(basePath, file.path);
277
+ if (statSync(path)) {
278
+ // eslint-disable-next-line no-console
279
+ console.log("overwrite", relative(process.cwd(), path));
280
+ } else {
281
+ // eslint-disable-next-line no-console
282
+ console.log("create", relative(process.cwd(), path));
283
+ }
284
+
285
+ writeFileSync(path, file.contents);
286
+ },
287
+ });
288
+ }