@alloy-js/core 0.16.0 → 0.18.0-dev.2

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 (249) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/src/binder.d.ts +19 -235
  3. package/dist/src/binder.d.ts.map +1 -1
  4. package/dist/src/binder.js +85 -386
  5. package/dist/src/code.d.ts +1 -1
  6. package/dist/src/code.d.ts.map +1 -1
  7. package/dist/src/components/Block.d.ts +1 -1
  8. package/dist/src/components/Block.d.ts.map +1 -1
  9. package/dist/src/components/Block.js +3 -1
  10. package/dist/src/components/Declaration.d.ts +2 -2
  11. package/dist/src/components/Declaration.d.ts.map +1 -1
  12. package/dist/src/components/Declaration.js +6 -5
  13. package/dist/src/components/For.d.ts +1 -1
  14. package/dist/src/components/For.d.ts.map +1 -1
  15. package/dist/src/components/For.js +1 -1
  16. package/dist/src/components/Indent.d.ts +1 -1
  17. package/dist/src/components/Indent.d.ts.map +1 -1
  18. package/dist/src/components/List.d.ts +1 -1
  19. package/dist/src/components/List.d.ts.map +1 -1
  20. package/dist/src/components/List.js +3 -1
  21. package/dist/src/components/MemberDeclaration.d.ts +3 -3
  22. package/dist/src/components/MemberDeclaration.d.ts.map +1 -1
  23. package/dist/src/components/MemberDeclaration.js +4 -4
  24. package/dist/src/components/MemberScope.d.ts +2 -2
  25. package/dist/src/components/MemberScope.d.ts.map +1 -1
  26. package/dist/src/components/MemberScope.js +0 -2
  27. package/dist/src/components/Output.d.ts +1 -1
  28. package/dist/src/components/Output.d.ts.map +1 -1
  29. package/dist/src/components/Output.js +1 -1
  30. package/dist/src/components/Prose.d.ts +1 -1
  31. package/dist/src/components/Prose.d.ts.map +1 -1
  32. package/dist/src/components/ReferenceOrContent.d.ts +1 -1
  33. package/dist/src/components/ReferenceOrContent.d.ts.map +1 -1
  34. package/dist/src/components/Scope.d.ts +2 -2
  35. package/dist/src/components/Scope.d.ts.map +1 -1
  36. package/dist/src/components/Scope.js +3 -6
  37. package/dist/src/components/Show.d.ts +1 -1
  38. package/dist/src/components/Show.d.ts.map +1 -1
  39. package/dist/src/components/SourceDirectory.d.ts +1 -1
  40. package/dist/src/components/SourceDirectory.d.ts.map +1 -1
  41. package/dist/src/components/SourceDirectory.js +1 -1
  42. package/dist/src/components/SourceFile.d.ts +1 -1
  43. package/dist/src/components/SourceFile.d.ts.map +1 -1
  44. package/dist/src/components/SourceFile.js +1 -1
  45. package/dist/src/components/StatementList.d.ts +1 -1
  46. package/dist/src/components/StatementList.d.ts.map +1 -1
  47. package/dist/src/components/StatementList.js +1 -1
  48. package/dist/src/components/Switch.d.ts +2 -2
  49. package/dist/src/components/Switch.d.ts.map +1 -1
  50. package/dist/src/components/Switch.js +2 -1
  51. package/dist/src/components/Wrap.d.ts +1 -1
  52. package/dist/src/components/Wrap.d.ts.map +1 -1
  53. package/dist/src/context/assignment.d.ts +1 -1
  54. package/dist/src/context/assignment.d.ts.map +1 -1
  55. package/dist/src/context/binder.d.ts +3 -3
  56. package/dist/src/context/binder.d.ts.map +1 -1
  57. package/dist/src/context/declaration.d.ts +1 -1
  58. package/dist/src/context/declaration.d.ts.map +1 -1
  59. package/dist/src/context/member-declaration.d.ts +1 -1
  60. package/dist/src/context/member-declaration.d.ts.map +1 -1
  61. package/dist/src/context/member-declaration.js +0 -1
  62. package/dist/src/context/member-scope.d.ts +1 -1
  63. package/dist/src/context/member-scope.d.ts.map +1 -1
  64. package/dist/src/context/name-policy.d.ts +1 -1
  65. package/dist/src/context/name-policy.d.ts.map +1 -1
  66. package/dist/src/context/scope.d.ts +1 -1
  67. package/dist/src/context/scope.d.ts.map +1 -1
  68. package/dist/src/context/source-directory.d.ts +1 -1
  69. package/dist/src/context/source-directory.d.ts.map +1 -1
  70. package/dist/src/context/source-file.d.ts +2 -2
  71. package/dist/src/context/source-file.d.ts.map +1 -1
  72. package/dist/src/context.d.ts +2 -2
  73. package/dist/src/context.d.ts.map +1 -1
  74. package/dist/src/context.js +1 -1
  75. package/dist/src/debug.d.ts +1 -0
  76. package/dist/src/debug.d.ts.map +1 -1
  77. package/dist/src/debug.js +4 -1
  78. package/dist/src/index.d.ts +8 -2
  79. package/dist/src/index.d.ts.map +1 -1
  80. package/dist/src/index.js +8 -2
  81. package/dist/src/jsx-runtime.d.ts +4 -272
  82. package/dist/src/jsx-runtime.d.ts.map +1 -1
  83. package/dist/src/jsx-runtime.js +3 -315
  84. package/dist/src/props-combinators.d.ts +19 -0
  85. package/dist/src/props-combinators.d.ts.map +1 -0
  86. package/dist/src/props-combinators.js +108 -0
  87. package/dist/src/reactive-union-set.d.ts +29 -0
  88. package/dist/src/reactive-union-set.d.ts.map +1 -0
  89. package/dist/src/reactive-union-set.js +183 -0
  90. package/dist/src/reactivity.d.ts +75 -0
  91. package/dist/src/reactivity.d.ts.map +1 -0
  92. package/dist/src/reactivity.js +141 -0
  93. package/dist/src/refkey.d.ts +36 -0
  94. package/dist/src/refkey.d.ts.map +1 -1
  95. package/dist/src/refkey.js +40 -0
  96. package/dist/src/render.d.ts +5 -1
  97. package/dist/src/render.d.ts.map +1 -1
  98. package/dist/src/render.js +68 -15
  99. package/dist/src/runtime/component.d.ts +24 -0
  100. package/dist/src/runtime/component.d.ts.map +1 -0
  101. package/dist/src/runtime/component.js +19 -0
  102. package/dist/src/runtime/intrinsic.d.ts +168 -0
  103. package/dist/src/runtime/intrinsic.d.ts.map +1 -0
  104. package/dist/src/runtime/intrinsic.js +11 -0
  105. package/dist/src/scheduler.d.ts +2 -2
  106. package/dist/src/scheduler.d.ts.map +1 -1
  107. package/dist/src/scheduler.js +27 -6
  108. package/dist/src/slot.d.ts +2 -2
  109. package/dist/src/slot.d.ts.map +1 -1
  110. package/dist/src/slot.js +1 -1
  111. package/dist/src/stc.d.ts +1 -1
  112. package/dist/src/stc.d.ts.map +1 -1
  113. package/dist/src/sti.d.ts +7 -6
  114. package/dist/src/sti.d.ts.map +1 -1
  115. package/dist/src/sti.js +1 -1
  116. package/dist/src/symbols/flags.d.ts +70 -0
  117. package/dist/src/symbols/flags.d.ts.map +1 -0
  118. package/dist/src/symbols/flags.js +72 -0
  119. package/dist/src/symbols/index.d.ts +7 -0
  120. package/dist/src/symbols/index.d.ts.map +1 -0
  121. package/dist/src/symbols/index.js +6 -0
  122. package/dist/src/symbols/output-scope.d.ts +94 -0
  123. package/dist/src/symbols/output-scope.d.ts.map +1 -0
  124. package/dist/src/symbols/output-scope.js +222 -0
  125. package/dist/src/symbols/output-symbol.d.ts +89 -0
  126. package/dist/src/symbols/output-symbol.d.ts.map +1 -0
  127. package/dist/src/symbols/output-symbol.js +333 -0
  128. package/dist/src/symbols/symbol-flow.d.ts +13 -0
  129. package/dist/src/symbols/symbol-flow.d.ts.map +1 -0
  130. package/dist/src/symbols/symbol-flow.js +74 -0
  131. package/dist/src/symbols/symbol-slot.d.ts +12 -0
  132. package/dist/src/symbols/symbol-slot.d.ts.map +1 -0
  133. package/dist/src/symbols/symbol-slot.js +36 -0
  134. package/dist/src/symbols/symbol-table.d.ts +14 -0
  135. package/dist/src/symbols/symbol-table.d.ts.map +1 -0
  136. package/dist/src/symbols/symbol-table.js +42 -0
  137. package/dist/src/tap.d.ts +3 -2
  138. package/dist/src/tap.d.ts.map +1 -1
  139. package/dist/src/tracer.d.ts +238 -0
  140. package/dist/src/tracer.d.ts.map +1 -0
  141. package/dist/src/tracer.js +496 -0
  142. package/dist/src/tsdoc-metadata.json +1 -1
  143. package/dist/src/utils.d.ts +4 -3
  144. package/dist/src/utils.d.ts.map +1 -1
  145. package/dist/src/utils.js +2 -1
  146. package/dist/test/components/slot.test.js +5 -7
  147. package/dist/test/props-with-defaults.test.js +1 -1
  148. package/dist/test/reactive-union-set.test.d.ts +2 -0
  149. package/dist/test/reactive-union-set.test.d.ts.map +1 -0
  150. package/dist/test/reactive-union-set.test.js +170 -0
  151. package/dist/test/reactivity/cleanup.test.js +2 -1
  152. package/dist/test/reactivity/memo.test.js +1 -1
  153. package/dist/test/reactivity/untrack.test.js +1 -1
  154. package/dist/test/rendering/memoization.test.js +2 -1
  155. package/dist/test/split-props.test.js +1 -1
  156. package/dist/test/symbols/output-scope.test.d.ts +2 -0
  157. package/dist/test/symbols/output-scope.test.d.ts.map +1 -0
  158. package/dist/test/symbols/output-scope.test.js +343 -0
  159. package/dist/test/symbols/output-symbol.test.d.ts +2 -0
  160. package/dist/test/symbols/output-symbol.test.d.ts.map +1 -0
  161. package/dist/test/symbols/output-symbol.test.js +447 -0
  162. package/dist/test/symbols/resolution.test.d.ts +2 -0
  163. package/dist/test/symbols/resolution.test.d.ts.map +1 -0
  164. package/dist/test/symbols/resolution.test.js +141 -0
  165. package/dist/test/symbols/utils.d.ts +25 -0
  166. package/dist/test/symbols/utils.d.ts.map +1 -0
  167. package/dist/test/symbols/utils.js +47 -0
  168. package/dist/testing/extend-expect.d.ts +15 -0
  169. package/dist/testing/extend-expect.d.ts.map +1 -1
  170. package/dist/testing/extend-expect.js +2 -1
  171. package/dist/testing/render.d.ts +1 -1
  172. package/dist/testing/render.d.ts.map +1 -1
  173. package/dist/tsconfig.tsbuildinfo +1 -1
  174. package/package.json +28 -24
  175. package/src/binder.ts +146 -672
  176. package/src/code.ts +1 -1
  177. package/src/components/Block.tsx +4 -1
  178. package/src/components/Declaration.tsx +8 -7
  179. package/src/components/For.tsx +2 -1
  180. package/src/components/Indent.tsx +1 -1
  181. package/src/components/List.tsx +3 -1
  182. package/src/components/MemberDeclaration.tsx +7 -6
  183. package/src/components/MemberScope.tsx +2 -5
  184. package/src/components/Output.tsx +2 -1
  185. package/src/components/Prose.tsx +1 -1
  186. package/src/components/ReferenceOrContent.tsx +1 -1
  187. package/src/components/Scope.tsx +3 -7
  188. package/src/components/Show.tsx +1 -1
  189. package/src/components/SourceDirectory.tsx +2 -1
  190. package/src/components/SourceFile.tsx +2 -1
  191. package/src/components/StatementList.tsx +2 -1
  192. package/src/components/Switch.tsx +2 -1
  193. package/src/components/Wrap.tsx +1 -1
  194. package/src/context/assignment.ts +1 -1
  195. package/src/context/binder.ts +3 -3
  196. package/src/context/declaration.ts +1 -1
  197. package/src/context/member-declaration.ts +1 -1
  198. package/src/context/member-scope.ts +1 -1
  199. package/src/context/name-policy.ts +1 -1
  200. package/src/context/scope.ts +1 -1
  201. package/src/context/source-directory.ts +1 -1
  202. package/src/context/source-file.ts +2 -2
  203. package/src/context.ts +3 -7
  204. package/src/debug.ts +5 -1
  205. package/src/index.ts +15 -1
  206. package/src/jsx-runtime.ts +15 -674
  207. package/src/props-combinators.ts +148 -0
  208. package/src/reactive-union-set.ts +238 -0
  209. package/src/reactivity.ts +230 -0
  210. package/src/refkey.ts +40 -0
  211. package/src/render.ts +97 -26
  212. package/src/runtime/component.ts +67 -0
  213. package/src/runtime/intrinsic.ts +199 -0
  214. package/src/scheduler.ts +31 -6
  215. package/src/slot.ts +3 -4
  216. package/src/stc.ts +2 -2
  217. package/src/sti.ts +11 -11
  218. package/src/symbols/flags.ts +82 -0
  219. package/src/symbols/index.ts +6 -0
  220. package/src/symbols/output-scope.ts +296 -0
  221. package/src/symbols/output-symbol.ts +459 -0
  222. package/src/symbols/symbol-flow.ts +100 -0
  223. package/src/symbols/symbol-slot.tsx +48 -0
  224. package/src/symbols/symbol-table.ts +72 -0
  225. package/src/tap.ts +3 -2
  226. package/src/tracer.ts +474 -0
  227. package/src/utils.tsx +7 -5
  228. package/temp/api.json +7446 -5133
  229. package/test/components/slot.test.tsx +8 -11
  230. package/test/props-with-defaults.test.ts +1 -1
  231. package/test/reactive-union-set.test.tsx +191 -0
  232. package/test/reactivity/cleanup.test.tsx +2 -1
  233. package/test/reactivity/memo.test.tsx +1 -1
  234. package/test/reactivity/untrack.test.ts +1 -1
  235. package/test/rendering/basic.test.tsx +1 -1
  236. package/test/rendering/memoization.test.tsx +1 -1
  237. package/test/split-props.test.ts +1 -1
  238. package/test/symbols/output-scope.test.ts +300 -0
  239. package/test/symbols/output-symbol.test.ts +456 -0
  240. package/test/symbols/resolution.test.ts +170 -0
  241. package/test/symbols/utils.ts +93 -0
  242. package/test/utils.test.tsx +1 -1
  243. package/testing/extend-expect.ts +16 -0
  244. package/testing/render.ts +1 -2
  245. package/LICENSE +0 -7
  246. package/dist/test/symbols.test.d.ts +0 -2
  247. package/dist/test/symbols.test.d.ts.map +0 -1
  248. package/dist/test/symbols.test.js +0 -884
  249. package/test/symbols.test.ts +0 -1006
@@ -1,309 +1,19 @@
1
1
  // Much of the implementations in this file are inspired by vuerx-js
2
2
  // See: https://github.com/ryansolid/vuerx-jsx.
3
- import {
4
- computed,
5
- isReactive,
6
- pauseTracking,
7
- proxyRefs,
8
- ReactiveEffectRunner,
9
- Ref,
10
- resetTracking,
11
- shallowRef,
12
- stop,
13
- toRefs,
14
- effect as vueEffect,
15
- } from "@vue/reactivity";
16
- import { Refkey } from "./refkey.js";
17
- import { RenderedTextTree } from "./render.js";
18
- import { scheduler } from "./scheduler.js";
19
-
20
- if ((globalThis as any).ALLOY) {
21
- throw new Error(
22
- "Multiple versions of the JSX Runtime have been loaded. This will likely cause undesirable behavior.",
23
- );
24
- }
25
- (globalThis as any).ALLOY = true;
26
-
27
- export interface Disposable {
28
- (): void;
29
- }
30
-
31
- export interface Context {
32
- disposables: Disposable[];
33
- owner: Context | null;
34
-
35
- // context providers
36
- context?: Record<symbol, unknown>;
37
-
38
- // store random info about the node
39
- meta?: Record<string, any>;
40
-
41
- /**
42
- * A cache of RenderTextTree nodes created within this context,
43
- * indexed by the component or function which created them.
44
- */
45
- elementCache: ElementCache;
46
- /**
47
- * When this context was created by a component, this will
48
- * be the component that created it.
49
- */
50
- componentOwner?: ComponentCreator<unknown>;
51
- }
52
-
53
- let globalContext: Context | null = null;
54
- export function getContext() {
55
- return globalContext;
56
- }
57
-
58
- export function getElementCache() {
59
- return getContext()!.elementCache;
60
- }
61
-
62
- export type ElementCacheKey =
63
- | ComponentCreator
64
- | (() => unknown)
65
- | CustomContext;
66
- export type ElementCache = Map<ElementCacheKey, RenderedTextTree>;
67
-
68
- export interface RootOptions {
69
- componentOwner?: ComponentCreator<any>;
70
- }
71
-
72
- export function root<T>(fn: (d: Disposable) => T, options?: RootOptions): T {
73
- const context: Context = {
74
- componentOwner: options?.componentOwner,
75
- disposables: [],
76
- owner: globalContext,
77
- context: {},
78
- elementCache: new Map(),
79
- };
80
-
81
- globalContext = context;
82
- let ret;
83
- try {
84
- ret = untrack(() =>
85
- fn(() => {
86
- for (const d of context!.disposables) {
87
- d();
88
- }
89
- }),
90
- );
91
- } finally {
92
- globalContext = globalContext!.owner;
93
- }
94
-
95
- return ret;
96
- }
97
-
98
- export function untrack<T>(fn: () => T): T {
99
- pauseTracking();
100
- const v = fn();
101
- resetTracking();
102
- return v;
103
- }
104
-
105
- export function memo<T>(fn: () => T, equal?: boolean): () => T {
106
- const o = shallowRef();
107
- effect((prev) => {
108
- const res = fn();
109
- // eslint-disable-next-line @typescript-eslint/no-unused-expressions
110
- (!equal || prev !== res) && (o.value = res);
111
- return res;
112
- }, undefined as T);
113
- return () => o.value;
114
- }
115
-
116
- export function effect<T>(fn: (prev?: T) => T, current?: T) {
117
- const context: Context = {
118
- context: {},
119
- disposables: [] as (() => void)[],
120
- owner: globalContext,
121
- elementCache: new Map(),
122
- };
123
-
124
- const cleanupFn = (final: boolean) => {
125
- const d = context.disposables;
126
- context.disposables = [];
127
- for (let k = 0, len = d.length; k < len; k++) d[k]();
128
-
129
- // eslint-disable-next-line @typescript-eslint/no-unused-expressions
130
- final && stop(runner);
131
- };
132
-
133
- onCleanup(() => cleanupFn(true));
134
- const runner: ReactiveEffectRunner<void> = vueEffect(
135
- () => {
136
- cleanupFn(false);
137
-
138
- const oldContext = globalContext;
139
- globalContext = context;
140
- try {
141
- current = fn(current);
142
- } finally {
143
- globalContext = oldContext;
144
- }
145
- },
146
- {
147
- scheduler: scheduler(() => runner),
148
- },
149
- );
150
-
151
- // allow recursive effects (recursive option does nothing, possible bug)
152
- (runner as any).effect.flags |= 1 << 5;
153
- }
154
-
155
- /**
156
- * Register a cleanup function which is called when the current reactive scope
157
- * is recalculated or disposed. This is useful to clean up any side effects
158
- * created in the reactive scope.
159
- *
160
- * @remarks
161
- *
162
- * When onCleanup is called inside a component definition, the provided function
163
- * is called when the component is removed from the tree. This can be useful to
164
- * clean up any side effects created as a result of rendering the component. For
165
- * example, if rendering a component creates a symbol, `onCleanup` can be used
166
- * to remove the symbol when the component is removed from the tree.
167
- *
168
- * When onCleanup is called inside a memo or effect, the function is called when
169
- * the effect is refreshed (e.g. when a memo or computed recalculates) or
170
- * disposed (e.g. it is no longer needed because it is attached to a component
171
- * which was removed).
172
- */
173
- export function onCleanup(fn: Disposable) {
174
- if (globalContext != null) {
175
- globalContext.disposables.push(fn);
176
- }
177
- }
178
-
179
- /**
180
- * Create a custom reactive context for the children returned by
181
- * the provided context.
182
- */
183
- export interface CustomContext {
184
- [CUSTOM_CONTEXT_SYM]: true;
185
- useCustomContext: (useCb: CustomContextChildrenCallback) => void;
186
- }
187
-
188
- export type CustomContextChildrenCallback = (child: Children) => void;
189
- const CUSTOM_CONTEXT_SYM = Symbol();
190
-
191
- export function createCustomContext(
192
- useCallback: (useChildren: CustomContextChildrenCallback) => void,
193
- ): CustomContext {
194
- return {
195
- [CUSTOM_CONTEXT_SYM]: true,
196
- useCustomContext(useCb: (children: Children) => void): void {
197
- useCallback(useCb);
198
- },
199
- };
200
- }
201
-
202
- export function isCustomContext(child: Children): child is CustomContext {
203
- return (
204
- typeof child === "object" &&
205
- child !== null &&
206
- Object.hasOwn(child, CUSTOM_CONTEXT_SYM)
207
- );
208
- }
209
-
210
- export type Child =
211
- | string
212
- | boolean
213
- | number
214
- | undefined
215
- | null
216
- | void
217
- | (() => Children)
218
- | Ref
219
- | Refkey
220
- | CustomContext
221
- | IntrinsicElement;
222
-
223
- export type Children = Child | Children[];
224
- export type Props = Record<string, any>;
225
-
226
- export interface ComponentDefinition<TProps = Props> {
227
- (props: TProps): Children;
228
- }
229
- export interface Component<TProps = Props> {
230
- (props: TProps): Children;
231
- tag?: symbol;
232
- }
233
-
234
- export interface ComponentCreator<TProps = Props> {
235
- component: Component<TProps>;
236
- (): Children;
237
- props: TProps;
238
- tag?: symbol;
239
- }
240
-
241
- // debugging utilities
242
- const renderStack: {
243
- component: Component<any>;
244
- props: Props;
245
- }[] = [];
246
-
247
- export function pushStack(component: Component<any>, props: Props) {
248
- if (!shouldDebug()) return;
249
- renderStack.push({ component, props });
250
- }
251
-
252
- export function popStack() {
253
- if (!shouldDebug()) return;
254
- renderStack.pop();
255
- }
256
-
257
- export function printRenderStack() {
258
- if (!shouldDebug()) return;
259
-
260
- // eslint-disable-next-line no-console
261
- console.error("Error rendering:");
262
- for (let i = renderStack.length - 1; i >= 0; i--) {
263
- const { component, props } = renderStack[i];
264
- // eslint-disable-next-line no-console
265
- console.error(` at ${component.name}(${inspectProps(props)})`);
266
- }
267
- }
268
-
269
- function inspectProps(props: Props) {
270
- return JSON.stringify(
271
- Object.fromEntries(
272
- Object.entries(props).map(([key, value]) => {
273
- let safeValue;
274
- switch (typeof value) {
275
- case "string":
276
- case "number":
277
- case "boolean":
278
- safeValue = value;
279
- break;
280
- case "undefined":
281
- safeValue = "undefined";
282
- break;
283
- case "object":
284
- safeValue = value ? "{...}" : null;
285
- break;
286
- case "function":
287
- safeValue = "function";
288
- break;
289
- }
290
- return [key, safeValue];
291
- }),
292
- ),
293
- );
294
- }
295
-
296
- // These can be removed with a smarter transform that encodes the information we
297
- // need in the compiled JSX output.
298
- export function isComponentCreator<TProps = any>(
299
- item: unknown,
300
- component?: Component<TProps>,
301
- ): item is ComponentCreator<TProps> {
302
- if (!component) {
303
- return typeof item === "function" && (item as any).component;
304
- }
305
- return typeof item === "function" && (item as any).component === component;
306
- }
3
+ import type {
4
+ Children,
5
+ Component,
6
+ ComponentDefinition,
7
+ IntrinsicElements as CoreIntrinsicElements,
8
+ } from "@alloy-js/core";
9
+ import { createComponent } from "@alloy-js/core";
10
+ export {
11
+ createComponent,
12
+ createIntrinsic,
13
+ memo,
14
+ mergeProps,
15
+ type Children,
16
+ } from "@alloy-js/core";
307
17
 
308
18
  /**
309
19
  * This namespace is predominantly for interop with React tooling in VSCode
@@ -311,134 +21,7 @@ export function isComponentCreator<TProps = any>(
311
21
  */
312
22
  // eslint-disable-next-line @typescript-eslint/no-namespace
313
23
  export namespace JSX {
314
- export interface IntrinsicElements {
315
- /**
316
- * Attempt to render the children on a single line if possible. If a group
317
- * contains `<breakParent />` or a hard line, or if the group exceeds the
318
- * print width, all linebreaks in the group will be broken.
319
- */
320
- group: { shouldBreak?: boolean; id?: symbol; children: Children };
321
-
322
- /**
323
- * A regular line break. This will break if the line exceeds the print
324
- * width, otherwise it will be a space.
325
- */
326
- line: {};
327
-
328
- /**
329
- * A regular line break. This will break if the line exceeds the print
330
- * width, otherwise it will be a space.
331
- */
332
- br: {};
333
-
334
- /**
335
- * A hard line break. This is a line that will always break, even if the
336
- * group does not exceed print width.
337
- */
338
- hardline: {};
339
-
340
- /**
341
- * A hard line break. This is a line that will always break, even if the
342
- * group does not exceed print width.
343
- */
344
- hbr: {};
345
-
346
- /**
347
- * A soft line break. This will break if the line exceeds the print width,
348
- * otherwise it will be be nothing.
349
- */
350
- softline: {};
351
-
352
- /**
353
- * A soft line break. This will break if the line exceeds the print width,
354
- * otherwise it will be be nothing.
355
- */
356
- sbr: {};
357
-
358
- /**
359
- * A literal line break. This will always break, even if the group does not
360
- * exceed print width. The new line will ignore indentation.
361
- */
362
- literalline: {};
363
-
364
- /**
365
- * A literal line break. This will always break, even if the group does not
366
- * exceed print width. The new line will ignore indentation.
367
- */
368
- lbr: {};
369
-
370
- /**
371
- * Increase the indentation level of the children of this component.
372
- * Indentation is determined by the print options provided to the Output
373
- * component or source file.
374
- */
375
- indent: { children: Children };
376
-
377
- /**
378
- * Indent the children of this component if the group specified by `groupId`
379
- * is broken (or not broken if `negate` is passed). The specified group must
380
- * already be printed.
381
- */
382
- indentIfBreak: { children: Children; groupId: symbol; negate?: boolean };
383
-
384
- /**
385
- * Similar to `group`, but will only place a line break before the last
386
- * segment to exceed the print width. This is useful for formatting
387
- * paragraphs of text where breaks are inserted prior to words which would
388
- * otherwise exceed the print width.
389
- */
390
- fill: { children: Children };
391
-
392
- /**
393
- * Force the parent group to break.
394
- */
395
- breakParent: {};
396
-
397
- /**
398
- * Print children if the current group or already printed group specified by
399
- * `groupId` is broken. Otherwise, `flatContents` is printed instead.
400
- */
401
- ifBreak: { children: Children; flatContents?: Children; groupId?: symbol };
402
-
403
- /**
404
- * Print this content at the end of the line. Useful for things like line
405
- * comments.
406
- */
407
- lineSuffix: { children: Children };
408
-
409
- /**
410
- * Force any line suffixes to print at this point.
411
- */
412
- lineSuffixBoundary: {};
413
-
414
- /**
415
- * Decrease the indentation level of the children of this component.
416
- * Indentation is determined by the print options provided to the Output
417
- * component or source file.
418
- */
419
- dedent: { children: Children };
420
-
421
- /**
422
- * Indent the children of this component by either the number of characters
423
- * indicated by the `width` prop, or by the provided string indicated by the
424
- * `string` prop.
425
- */
426
- align:
427
- | { children: Children; width: number }
428
- | { children: Children; string: string };
429
-
430
- /**
431
- * Mark the current indentation level as "root" for the purposes of literal
432
- * line breaks and `dedentToRoot`.
433
- */
434
- markAsRoot: { children: Children };
435
-
436
- /**
437
- * Decrease the indentation level to the root level specified by
438
- * `<markAsRoot />`, or else to no indentation.
439
- */
440
- dedentToRoot: { children: Children };
441
- }
24
+ export type IntrinsicElements = CoreIntrinsicElements;
442
25
  export type ElementType = string | ComponentDefinition<any>;
443
26
  export type Element = Children;
444
27
  export interface ElementChildrenAttribute {
@@ -454,245 +37,3 @@ export function jsx(type: Component<any>, props: Record<string, unknown>) {
454
37
  }
455
38
 
456
39
  export const jsxs = jsx;
457
-
458
- export function createComponent<TProps extends Props = Props>(
459
- C: Component<TProps>,
460
- props: TProps,
461
- ): ComponentCreator<TProps> {
462
- const creator: ComponentCreator<TProps> = () => /* */ C(props);
463
- creator.props = props;
464
- creator.component = C;
465
- if (C.tag) {
466
- creator.tag = C.tag;
467
- }
468
-
469
- return creator;
470
- }
471
-
472
- export function taggedComponent<TProps = Props>(
473
- tag: symbol,
474
- component: Component<TProps>,
475
- ): Component<TProps> & Required<Pick<Component<TProps>, "tag">> {
476
- component.tag = tag;
477
- return component as any;
478
- }
479
-
480
- export const intrinsicElementKey = Symbol();
481
-
482
- export type IndentIntrinsicElement = IntrinsicElementBase<"indent">;
483
- export type IndentIfBreakIntrinsicElement =
484
- IntrinsicElementBase<"indentIfBreak">;
485
- export type BrIntrinsicElement = IntrinsicElementBase<"br">;
486
- export type LineIntrinsicElement = IntrinsicElementBase<"line">;
487
- export type HbrIntrinsicElement = IntrinsicElementBase<"hbr">;
488
- export type HardlineIntrinsicElement = IntrinsicElementBase<"hardline">;
489
- export type SbrIntrinsicElement = IntrinsicElementBase<"sbr">;
490
- export type SoftlineIntrinsicElement = IntrinsicElementBase<"softline">;
491
- export type GroupIntrinsicElement = IntrinsicElementBase<"group">;
492
- export type AlignIntrinsicElement = IntrinsicElementBase<"align">;
493
- export type FillIntrinsicElement = IntrinsicElementBase<"fill">;
494
- export type BreakParentIntrinsicElement = IntrinsicElementBase<"breakParent">;
495
- export type LineSuffixIntrinsicElement = IntrinsicElementBase<"lineSuffix">;
496
- export type LineSuffixBoundaryIntrinsicElement =
497
- IntrinsicElementBase<"lineSuffixBoundary">;
498
- export type DedentIntrinsicElement = IntrinsicElementBase<"dedent">;
499
- export type DedentToRootIntrinsicElement = IntrinsicElementBase<"dedentToRoot">;
500
- export type MarkAsRootIntrinsicElement = IntrinsicElementBase<"markAsRoot">;
501
- export type LiterallineIntrinsicElement = IntrinsicElementBase<"literalline">;
502
- export type LbrIntrinsicElement = IntrinsicElementBase<"lbr">;
503
- export type IfBreakIntrinsicElement = IntrinsicElementBase<"ifBreak">;
504
-
505
- export type IntrinsicElement =
506
- | IndentIntrinsicElement
507
- | IndentIfBreakIntrinsicElement
508
- | BrIntrinsicElement
509
- | LineIntrinsicElement
510
- | HbrIntrinsicElement
511
- | HardlineIntrinsicElement
512
- | SbrIntrinsicElement
513
- | SoftlineIntrinsicElement
514
- | GroupIntrinsicElement
515
- | AlignIntrinsicElement
516
- | FillIntrinsicElement
517
- | BreakParentIntrinsicElement
518
- | LineSuffixIntrinsicElement
519
- | LineSuffixBoundaryIntrinsicElement
520
- | DedentIntrinsicElement
521
- | LiterallineIntrinsicElement
522
- | LbrIntrinsicElement
523
- | DedentToRootIntrinsicElement
524
- | MarkAsRootIntrinsicElement
525
- | IfBreakIntrinsicElement;
526
-
527
- export interface IntrinsicElementBase<
528
- TKey extends keyof JSX.IntrinsicElements = keyof JSX.IntrinsicElements,
529
- > {
530
- [intrinsicElementKey]: true;
531
- name: TKey;
532
- props: JSX.IntrinsicElements[TKey];
533
- }
534
- export function createIntrinsic<TKey extends keyof JSX.IntrinsicElements>(
535
- name: TKey,
536
- props: JSX.IntrinsicElements[TKey],
537
- ): IntrinsicElementBase<TKey> {
538
- return {
539
- [intrinsicElementKey]: true,
540
- name,
541
- props,
542
- };
543
- }
544
-
545
- export function isIntrinsicElement(type: unknown): type is IntrinsicElement {
546
- return (
547
- typeof type === "object" && type !== null && intrinsicElementKey in type
548
- );
549
- }
550
- export function mergeProps<T, U>(source: T, source1: U): T & U;
551
- export function mergeProps<T, U, V>(
552
- source: T,
553
- source1: U,
554
- source2: V,
555
- ): T & U & V;
556
- export function mergeProps<T, U, V, W>(
557
- source: T,
558
- source1: U,
559
- source2: V,
560
- source3: W,
561
- ): T & U & V & W;
562
- export function mergeProps(...sources: any): any {
563
- const target = {};
564
- for (let i = 0; i < sources.length; i++) {
565
- let source = sources[i];
566
- if (typeof source === "function") source = source();
567
- if (source) {
568
- const descriptors = Object.getOwnPropertyDescriptors(source);
569
- for (const key in descriptors) {
570
- if (key in target) continue;
571
- Object.defineProperty(target, key, {
572
- enumerable: true,
573
- get() {
574
- for (let i = sources.length - 1; i >= 0; i--) {
575
- let s = sources[i];
576
- if (typeof s === "function") s = s();
577
- const v = (s || {})[key];
578
- if (v !== undefined) return v;
579
- }
580
- },
581
- });
582
- }
583
- }
584
- }
585
- return target;
586
- }
587
-
588
- export type SplitProps<T, K extends (readonly (keyof T)[])[]> = [
589
- ...{
590
- [P in keyof K]: P extends `${number}` ?
591
- Pick<T, Extract<K[P], readonly (keyof T)[]>[number]>
592
- : never;
593
- },
594
- { [P in keyof T as Exclude<P, K[number][number]>]: T[P] },
595
- ];
596
-
597
- export function splitProps<
598
- T extends Record<any, any>,
599
- K extends [readonly (keyof T)[], ...(readonly (keyof T)[])[]],
600
- >(props: T, ...keys: K): SplitProps<T, K> {
601
- if (isReactive(props)) {
602
- const refs = untrack(() => toRefs(props));
603
- const remainingKeys = new Set(Object.keys(refs));
604
-
605
- const result: any = keys.map((keySet) => {
606
- const resultSet: any = {};
607
- for (const key of keySet) {
608
- resultSet[key] = refs[key];
609
- remainingKeys.delete(key as string);
610
- }
611
- return proxyRefs(resultSet);
612
- });
613
-
614
- const remaining: any = {};
615
- for (const key of remainingKeys) {
616
- remaining[key] = refs[key];
617
- }
618
-
619
- return [...result, proxyRefs(remaining)] as any;
620
- }
621
-
622
- const descriptors = Object.getOwnPropertyDescriptors(props);
623
- const remainingKeys = new Set(Object.keys(descriptors));
624
- const result: any = keys.map((keySet) => {
625
- const resultSet: any = {};
626
- for (const key of keySet) {
627
- if (key in descriptors) {
628
- Object.defineProperty(resultSet, key, descriptors[key]);
629
- remainingKeys.delete(key as string);
630
- }
631
- }
632
- return resultSet;
633
- });
634
-
635
- const remaining: any = {};
636
- for (const key of remainingKeys) {
637
- Object.defineProperty(remaining, key, descriptors[key]);
638
- }
639
-
640
- return [...result, remaining] as any;
641
- }
642
-
643
- /**
644
- * Applies default values to a props object. Reactive props are handled properly
645
- * by ensuring that their value is not accessed by `defaultProps`, avoiding any
646
- * unintended side effects.
647
- */
648
- export function defaultProps<T extends {}>(props: T, defaults: Partial<T>): T {
649
- if (isReactive(props)) {
650
- const refs = untrack(() => toRefs(props));
651
- for (const key in defaults) {
652
- const originalRef = refs[key];
653
- refs[key] = computed(() =>
654
- originalRef.value === undefined ? defaults[key] : originalRef.value,
655
- ) as any;
656
- }
657
-
658
- return proxyRefs(refs);
659
- }
660
- const withDefaults = {} as T;
661
- const copied = new Set<string>();
662
- for (const key in defaults) {
663
- copied.add(key);
664
- const desc = Object.getOwnPropertyDescriptor(props, key);
665
- if (!desc) {
666
- withDefaults[key] = defaults[key]!;
667
- continue;
668
- }
669
-
670
- if (desc.get) {
671
- const originalGet = desc.get;
672
- desc.get = function () {
673
- const value = originalGet.call(this);
674
- return value === undefined ? defaults[key as keyof T] : value;
675
- };
676
- Object.defineProperty(withDefaults, key, desc);
677
- } else {
678
- desc.value =
679
- desc.value === undefined ? defaults[key as keyof T] : desc.value;
680
- Object.defineProperty(withDefaults, key, desc);
681
- }
682
- }
683
-
684
- const descriptors = Object.getOwnPropertyDescriptors(props);
685
- for (const key in descriptors) {
686
- if (copied.has(key)) {
687
- continue;
688
- }
689
-
690
- Object.defineProperty(withDefaults, key, descriptors[key]);
691
- }
692
-
693
- return withDefaults;
694
- }
695
-
696
- function shouldDebug() {
697
- return typeof process !== "undefined" && !!process.env?.ALLOY_DEBUG;
698
- }