@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/binder.ts ADDED
@@ -0,0 +1,838 @@
1
+ import { memo } from "@alloy-js/core/jsx-runtime";
2
+ import {
3
+ computed,
4
+ effect,
5
+ reactive,
6
+ Ref,
7
+ ShallowRef,
8
+ shallowRef,
9
+ } from "@vue/reactivity";
10
+ import { useMemberScope } from "./context/member-scope.js";
11
+ import { useScope } from "./context/scope.js";
12
+ import { refkey, Refkey } from "./refkey.js";
13
+ export type Metadata = object;
14
+
15
+ /**
16
+ * Flags that describe an output symbol.
17
+ */
18
+ export enum OutputSymbolFlags {
19
+ None = 0,
20
+
21
+ /**
22
+ * The symbol is an instance member container. Symbols with this flag will have a
23
+ * instanceMemberScope property that contains symbols for instance members.
24
+ */
25
+ InstanceMemberContainer = 1 << 0,
26
+
27
+ /**
28
+ * The symbol is a static member container. Symbols with this flag will have a
29
+ * staticMemberScope property that contains symbols for static members.
30
+ */
31
+ StaticMemberContainer = 1 << 1,
32
+
33
+ /**
34
+ * Whether this symbol contains members of any kind.
35
+ */
36
+ MemberContainer = InstanceMemberContainer | StaticMemberContainer,
37
+
38
+ /**
39
+ * Whether this symbol is an instance member of another symbol (i.e that it is
40
+ * stored in an instance member scope).
41
+ */
42
+ InstanceMember = 1 << 2,
43
+
44
+ /**
45
+ * Whether this symbol is a static member of another symbol (i.e that it is
46
+ * stored in a static member scope).
47
+ */
48
+ StaticMember = 1 << 3,
49
+
50
+ /**
51
+ * Whether this is an instance member or static member of another symbol.
52
+ */
53
+ Member = InstanceMember | StaticMember,
54
+ }
55
+
56
+ /**
57
+ * An output symbol is a named entity that can be referenced in your output
58
+ * code. Referencing symbols is generally accomplished by using a
59
+ * {@link Refkey}.
60
+ *
61
+ * @remarks
62
+ *
63
+ * This interface is the base implementation of symbol. Generally, most
64
+ * languages will have subtypes of this interface that provide additional,
65
+ * language-specific metadata about declared symbols. For example, symbols in
66
+ * TypeScript need to track whether they are exported or what kind of symbol
67
+ * they are (e.g. type, value, parameter, etc.)
68
+ *
69
+ * Symbols are reactive values, which allows you to observe changes to the symbol
70
+ * information. For example, a symbol's name may change if it becomes conflicted
71
+ * with a symbol that is added later.
72
+ */
73
+ export interface OutputSymbol {
74
+ /**
75
+ * The original name of the symbol, before any conflicts were resolved.
76
+ */
77
+ originalName: string;
78
+
79
+ /**
80
+ * The name of the symbol.
81
+ */
82
+ name: string;
83
+
84
+ /**
85
+ * The symbol's flags.
86
+ */
87
+ flags: OutputSymbolFlags;
88
+
89
+ /**
90
+ * The scope in which the symbol is defined.
91
+ */
92
+ scope: OutputScope;
93
+
94
+ /**
95
+ * The binder instance that created this symbol.
96
+ */
97
+ binder: Binder;
98
+
99
+ /**
100
+ * A unique value that references this symbol.
101
+ */
102
+ refkey: Refkey;
103
+
104
+ /**
105
+ * The instance members available on this symbol.
106
+ *
107
+ * @remarks
108
+ *
109
+ * Instance members are members that are available when this symbol is
110
+ * instantiated. Instantiation is language-specific. For example, in
111
+ * TypeScript, instance members of symbols for classes are available when the
112
+ * class is instantiated with a new expression.
113
+ *
114
+ * When a symbol cannot have instance members, this is undefined.
115
+ */
116
+ instanceMemberScope?: OutputScope;
117
+
118
+ /**
119
+ * The static members available on this symbol.
120
+ *
121
+ * @remarks
122
+ *
123
+ * Static members are members that are available on the symbol itself. These symbols
124
+ * can be accessed off the parent symbol directly with a refkey. There is only ever
125
+ * one static member symbol in the output (i.e., the symbol is unique).
126
+ */
127
+ staticMemberScope?: OutputScope;
128
+ }
129
+
130
+ /**
131
+ * Flags that describe an output scope.
132
+ */
133
+ export enum OutputScopeFlags {
134
+ None = 0,
135
+
136
+ /**
137
+ * This scope is a static member scope.
138
+ */
139
+ StaticMemberScope = 1 << 0,
140
+
141
+ /**
142
+ * This scope is an instance member scope.
143
+ */
144
+ InstanceMemberScope = 1 << 1,
145
+
146
+ /**
147
+ * This scope is a member scope. Scopes with this flag will have an `owner`
148
+ * property that points to the symbol whose member this scope holds.
149
+ */
150
+ MemberScope = StaticMemberScope | InstanceMemberScope,
151
+ }
152
+
153
+ /**
154
+ * A container of symbols accessible within some scope in your output code.
155
+ *
156
+ * @remarks
157
+ *
158
+ * Scopes form a tree. All scopes except the global scope have a parent scope
159
+ * identified by `parent`. Scopes can have nested scopes which can be accessed
160
+ * using the `children` property. Whether a child scope can be accessed from
161
+ * the parent scope is determined by each language's scoping rules.
162
+ *
163
+ * This interface is the base implementation of scope. Generally, most languages
164
+ * will have subtypes of this interface that provide additional metadata about
165
+ * the scope. For example, TypeScript scopes need to track whether the scope
166
+ * represents a package, module, class, function, etc.
167
+ *
168
+ * Scopes are reactive values, which allows you to observe changes to the scope
169
+ * within a reactive context.
170
+ */
171
+ export interface OutputScope {
172
+ /**
173
+ * The kind of scope. Subtypes will likely provide a set of known scope kinds.
174
+ * The kind is not used by the binder itself.
175
+ */
176
+ kind: string;
177
+
178
+ /**
179
+ * The name of the scope.
180
+ */
181
+ name: string;
182
+
183
+ /**
184
+ * The flags that describe this scope.
185
+ */
186
+ flags: OutputScopeFlags;
187
+
188
+ /**
189
+ * The symbols defined within this scope.
190
+ */
191
+ symbols: Set<OutputSymbol>;
192
+
193
+ /**
194
+ * The symbols defined within this scope, indexed by refkey.
195
+ */
196
+ symbolsByRefkey: Map<Refkey, OutputSymbol>;
197
+
198
+ /**
199
+ * The scopes nested within this scope.
200
+ */
201
+ children: Set<OutputScope>;
202
+
203
+ /**
204
+ * The container of this scope.
205
+ */
206
+ parent: OutputScope | undefined;
207
+
208
+ /**
209
+ * The symbol that owns this scope. This is only defined for scopes that have
210
+ * the {@link OutputScopeFlags.StaticMemberScope} flag.
211
+ */
212
+ owner?: OutputSymbol;
213
+
214
+ /**
215
+ * The binder that created this scope.
216
+ */
217
+ binder: Binder;
218
+
219
+ /**
220
+ * Get the names of all symbols in this scope.
221
+ */
222
+ getSymbolNames(): Set<string>;
223
+ }
224
+
225
+ /**
226
+ * The binder tracks all output scopes and symbols. Scopes are nested containers
227
+ * for symbols.
228
+ *
229
+ * @remarks
230
+ *
231
+ * Symbol information is reactive because in certain situations this data may
232
+ * change. For example, when a symbol becomes conflicted with another symbol,
233
+ * one of the symbol names may change. Ensure that you interact with binder
234
+ * values in a reactive context (i.e. within JSX/code template, or within
235
+ * memo/computed/etc).
236
+ *
237
+ */
238
+ export interface Binder {
239
+ /**
240
+ * Create a new scope. The scope will be added to the parent scope's children.
241
+ * The returned scope object is reactive.
242
+ */
243
+ createScope<T extends OutputScope>(
244
+ args: {
245
+ kind: T["kind"];
246
+ name: string;
247
+ parent?: OutputScope | undefined;
248
+ flags?: OutputScopeFlags;
249
+ owner?: OutputSymbol;
250
+ } & Omit<T, keyof OutputScope>,
251
+ ): T;
252
+
253
+ /**
254
+ * Create a new symbol. The symbol will be added to the parent scope's symbols.
255
+ * The returned symbol object is reactive.
256
+ */
257
+ createSymbol<T extends OutputSymbol>(
258
+ args: {
259
+ name: string;
260
+ scope?: OutputScope;
261
+ refkey?: unknown;
262
+ flags?: OutputSymbolFlags;
263
+ } & Omit<T, keyof OutputSymbol>,
264
+ ): T;
265
+
266
+ /**
267
+ * Instantiate the static members of a symbol into the instance members of
268
+ * another symbol.
269
+ *
270
+ * @param sourceSym - The symbol to add instance members to.
271
+ * @param targetSym - The symbol with the static members to instantiate.
272
+ */
273
+ instantiateSymbolInto(sourceSym: OutputSymbol, targetSym: OutputSymbol): void;
274
+
275
+ /**
276
+ * Add static members to an existing symbol.
277
+ *
278
+ * @param symbol - The symbol to add static members to.
279
+ */
280
+ addStaticMembersToSymbol(symbol: OutputSymbol): void;
281
+
282
+ /**
283
+ * Add instance members to an existing symbol.
284
+ *
285
+ * @param symbol - The symbol to add instance members to.
286
+ */
287
+ addInstanceMembersToSymbol(symbol: OutputSymbol): void;
288
+
289
+ /**
290
+ * Resolve the given refkey in the current scope.
291
+ *
292
+ * @returns a ref for the resolution result.
293
+ * @see {@link resolve} a convenience function that uses the current scope and
294
+ * binder.
295
+ */
296
+ resolveDeclarationByKey<
297
+ TScope extends OutputScope = OutputScope,
298
+ TSymbol extends OutputSymbol = OutputSymbol,
299
+ >(
300
+ currentScope: TScope | undefined,
301
+ currentMemberScope: TScope | undefined,
302
+ key: Refkey,
303
+ ): Ref<ResolutionResult<TScope, TSymbol> | undefined>;
304
+
305
+ globalScope: OutputScope;
306
+ }
307
+
308
+ /**
309
+ * A successful resolution of a refkey.
310
+ *
311
+ * @example
312
+ *
313
+ * Let's say we have the following scopes and symbols:
314
+ *
315
+ * ```
316
+ * scope: global scope
317
+ * ├── scope: namespace scope 1
318
+ * │ └── symbol: foo
319
+ * │ └── static member scope
320
+ * │ └── symbol: bar
321
+ * └── scope: namespace scope 2
322
+ * └── (resolve bar from here)
323
+ * ```
324
+ *
325
+ * When we resolve the refkey for `bar` from within `namespace scope 2`, we will get the following
326
+ * resolution result:
327
+ *
328
+ * * **targetDeclaration**: symbol bar, the symbol we resolved.
329
+ * * **commonScope**: global scope, because this is the most specific scope that contains both the declaration and the reference.
330
+ * * **pathUp**: [namespace scope 2], because this is the scope between the reference and the common scope.
331
+ * * **pathDown**: [namespace scope 1], because this is the scope between the common scope and the declaration
332
+ * * **memberPath**: [foo, bar], because we resolved a member symbol and these are the symbols that lead from the base declaration to the member symbol.
333
+ */
334
+ export interface ResolutionResult<
335
+ TScope extends OutputScope,
336
+ TSymbol extends OutputSymbol,
337
+ > {
338
+ /**
339
+ * The symbol for the resolved declaration.
340
+ */
341
+ targetDeclaration: TSymbol;
342
+ /**
343
+ * The scopes between the common scope and the reference
344
+ */
345
+ pathUp: TScope[];
346
+
347
+ /**
348
+ * The scopes between the common scope and the declaration.
349
+ */
350
+ pathDown: TScope[];
351
+
352
+ /**
353
+ * The scope which contains both the reference and the declaration.
354
+ */
355
+ commonScope: TScope | undefined;
356
+
357
+ /**
358
+ * When resolving a member symbol, this is the path of symbols that lead from
359
+ * the base declaration to the member symbol.
360
+ */
361
+ memberPath?: TSymbol[];
362
+ }
363
+
364
+ export interface NameConflictResolver {
365
+ (name: string, symbols: OutputSymbol[]): void;
366
+ }
367
+
368
+ export interface BinderOptions {
369
+ nameConflictResolver?: NameConflictResolver;
370
+ }
371
+
372
+ export function createOutputBinder(options: BinderOptions = {}): Binder {
373
+ const binder: Binder = {
374
+ createScope,
375
+ createSymbol,
376
+ resolveDeclarationByKey,
377
+ addStaticMembersToSymbol,
378
+ addInstanceMembersToSymbol,
379
+ instantiateSymbolInto,
380
+ globalScope: undefined as any,
381
+ };
382
+
383
+ const globalSymbols = reactive(new Set<OutputSymbol>());
384
+
385
+ binder.globalScope = reactive({
386
+ kind: "global",
387
+ name: "<global>",
388
+ symbols: new Set(),
389
+ symbolsByRefkey: new Map(),
390
+ children: new Set(),
391
+ parent: undefined,
392
+ binder,
393
+ flags: OutputScopeFlags.None,
394
+ getSymbolNames: symbolNames(globalSymbols),
395
+ });
396
+
397
+ const knownDeclarations = new Map<Refkey, OutputSymbol>();
398
+ const waitingDeclarations = new Map<Refkey, Ref<OutputSymbol | undefined>>();
399
+
400
+ return binder;
401
+
402
+ function createScope<T extends OutputScope>(
403
+ args: {
404
+ kind: string;
405
+ name: string;
406
+ parent?: OutputScope;
407
+ flags?: OutputScopeFlags;
408
+ owner?: OutputSymbol;
409
+ } & Omit<T, keyof OutputScope>,
410
+ ): T {
411
+ const {
412
+ kind,
413
+ name,
414
+ parent,
415
+ owner,
416
+ flags = OutputScopeFlags.None,
417
+ ...rest
418
+ } = args;
419
+
420
+ if (flags & OutputScopeFlags.MemberScope) {
421
+ if (!owner) {
422
+ throw new Error("Member scopes must have an owner");
423
+ }
424
+ }
425
+
426
+ // member scopes don't have a parent regular scope. Perhaps in the future
427
+ // their parent would be something like a prototype object.
428
+ let parentScope: OutputScope | undefined;
429
+ if (parent) {
430
+ parentScope = parent;
431
+ } else if (flags & OutputScopeFlags.MemberScope) {
432
+ parentScope = undefined;
433
+ } else {
434
+ parentScope = useScope() ?? binder.globalScope;
435
+ }
436
+
437
+ const symbols = reactive(new Set<OutputSymbol>());
438
+ const symbolsByRefkey = reactive(new Map<Refkey, OutputSymbol>());
439
+ const scope: T = reactive({
440
+ kind: kind,
441
+ name: name,
442
+ symbols,
443
+ symbolsByRefkey,
444
+ children: new Set(),
445
+ parent: parentScope,
446
+ flags,
447
+ owner,
448
+ binder,
449
+ ...rest,
450
+ getSymbolNames: symbolNames(symbols),
451
+ }) as T;
452
+
453
+ if (parentScope) {
454
+ parentScope.children.add(scope);
455
+ }
456
+
457
+ return scope as T;
458
+ }
459
+
460
+ function createSymbol<T extends OutputSymbol>(
461
+ args: {
462
+ name: string;
463
+ scope?: OutputScope;
464
+ refkey?: Refkey;
465
+ flags?: OutputSymbolFlags;
466
+ } & Omit<T, keyof OutputSymbol>,
467
+ ): T {
468
+ const {
469
+ name,
470
+ scope = useDefaultScope(args.flags),
471
+ refkey,
472
+ flags = OutputSymbolFlags.None,
473
+ ...rest
474
+ } = args;
475
+
476
+ if (!scope) {
477
+ throw new Error(
478
+ "No scope was provided and no scope could be found in context",
479
+ );
480
+ }
481
+
482
+ if (
483
+ flags & OutputSymbolFlags.Member &&
484
+ (scope.flags & OutputScopeFlags.MemberScope) === 0
485
+ ) {
486
+ throw new Error("Member symbols must be stored in a member scope.");
487
+ }
488
+
489
+ if (scope.flags & OutputScopeFlags.StaticMemberScope) {
490
+ if (
491
+ ~flags & OutputSymbolFlags.InstanceMember &&
492
+ ~flags & OutputSymbolFlags.StaticMember
493
+ ) {
494
+ throw new Error(
495
+ "Symbols stored in a member scope must have either OutputSymbolFlags.InstanceMember or OutputSymbolFlags.StaticMember flags",
496
+ );
497
+ }
498
+ }
499
+
500
+ const symbol = reactive({
501
+ originalName: name,
502
+ name: name,
503
+ scope,
504
+ refkey,
505
+ binder,
506
+ flags,
507
+ ...rest,
508
+ }) as T;
509
+
510
+ if (args.flags && args.flags & OutputSymbolFlags.InstanceMemberContainer) {
511
+ symbol.instanceMemberScope = createScope({
512
+ kind: "member",
513
+ name: "instance members",
514
+ parent: undefined,
515
+ owner: symbol,
516
+ flags: OutputScopeFlags.InstanceMemberScope,
517
+ });
518
+ }
519
+
520
+ if (args.flags && args.flags & OutputSymbolFlags.StaticMemberContainer) {
521
+ symbol.staticMemberScope = createScope({
522
+ kind: "member",
523
+ name: "static members",
524
+ parent: undefined,
525
+ owner: symbol,
526
+ flags: OutputScopeFlags.StaticMemberScope,
527
+ });
528
+ }
529
+
530
+ scope.symbols.add(symbol);
531
+ scope.symbolsByRefkey.set(symbol.refkey, symbol);
532
+
533
+ deconflict(symbol);
534
+ notifyRefkey(refkey, symbol);
535
+
536
+ return symbol;
537
+ }
538
+
539
+ function instantiateSymbolInto(source: OutputSymbol, target: OutputSymbol) {
540
+ if (~source.flags & OutputSymbolFlags.InstanceMemberContainer) {
541
+ throw new Error("Can only instantiate symbols with instance members");
542
+ }
543
+
544
+ addInstanceMembersToSymbol(target);
545
+
546
+ effect(() => {
547
+ for (const sym of source.instanceMemberScope!.symbols) {
548
+ if (target.instanceMemberScope!.symbols.has(sym)) {
549
+ continue;
550
+ }
551
+
552
+ createSymbol({
553
+ name: sym.name,
554
+ scope: target.instanceMemberScope!,
555
+ refkey: refkey(target.refkey, sym.refkey),
556
+ flags: sym.flags | OutputSymbolFlags.InstanceMember,
557
+ });
558
+ }
559
+ });
560
+ }
561
+
562
+ function addStaticMembersToSymbol(symbol: OutputSymbol) {
563
+ if (symbol.flags & OutputSymbolFlags.StaticMemberContainer) {
564
+ // nothing to do.
565
+ return;
566
+ }
567
+
568
+ symbol.flags |= OutputSymbolFlags.StaticMemberContainer;
569
+ symbol.staticMemberScope = createScope({
570
+ kind: "member",
571
+ name: "static members",
572
+ parent: undefined,
573
+ owner: symbol,
574
+ flags: OutputScopeFlags.StaticMemberScope,
575
+ });
576
+ }
577
+
578
+ function addInstanceMembersToSymbol(symbol: OutputSymbol) {
579
+ if (symbol.flags & OutputSymbolFlags.InstanceMemberContainer) {
580
+ // nothing to do.
581
+ return;
582
+ }
583
+
584
+ symbol.flags |= OutputSymbolFlags.InstanceMemberContainer;
585
+ symbol.instanceMemberScope = createScope({
586
+ kind: "member",
587
+ name: "instance members",
588
+ parent: undefined,
589
+ owner: symbol,
590
+ flags: OutputScopeFlags.InstanceMemberScope,
591
+ });
592
+ }
593
+
594
+ function resolveDeclarationByKey<
595
+ TScope extends OutputScope = OutputScope,
596
+ TSymbol extends OutputSymbol = OutputSymbol,
597
+ >(
598
+ currentScope: TScope | undefined,
599
+ currentMemberScope: TScope | undefined,
600
+ key: Refkey,
601
+ ): Ref<ResolutionResult<TScope, TSymbol> | undefined> {
602
+ const targetDeclaration = knownDeclarations.get(key);
603
+
604
+ if (targetDeclaration) {
605
+ // this any cast hides a ridiculous error, fix it probably
606
+ return shallowRef(
607
+ buildResult(currentScope, currentMemberScope, targetDeclaration),
608
+ ) as any;
609
+ } else {
610
+ return waitForRefkey(key, (symbol) => {
611
+ if (symbol) {
612
+ return buildResult(currentScope, currentMemberScope, symbol);
613
+ } else {
614
+ return undefined;
615
+ }
616
+ });
617
+ }
618
+ }
619
+
620
+ function buildResult<
621
+ TScope extends OutputScope = OutputScope,
622
+ TSymbol extends OutputSymbol = OutputSymbol,
623
+ >(
624
+ currentScope: TScope | undefined,
625
+ currentMemberScope: TScope | undefined,
626
+ targetDeclarationBase: TSymbol,
627
+ ): ResolutionResult<TScope, TSymbol> {
628
+ if (targetDeclarationBase.flags & OutputSymbolFlags.InstanceMember) {
629
+ if (targetDeclarationBase.scope !== currentMemberScope) {
630
+ throw new Error(
631
+ "Cannot resolve member symbols from a different member scope",
632
+ );
633
+ }
634
+
635
+ // todo: handle referencing nested objects by refkey
636
+ return {
637
+ pathUp: [],
638
+ pathDown: [],
639
+ memberPath: [targetDeclarationBase],
640
+ commonScope: currentMemberScope,
641
+ targetDeclaration: targetDeclarationBase,
642
+ };
643
+ }
644
+
645
+ const { memberPath, scopeChain: targetChain } = scopeAndMemberChain<
646
+ TScope,
647
+ TSymbol
648
+ >(targetDeclarationBase);
649
+ const currentChain = scopeChain(currentScope);
650
+ let diffStart = 0;
651
+ while (
652
+ targetChain[diffStart] &&
653
+ currentChain[diffStart] &&
654
+ targetChain[diffStart] === currentChain[diffStart]
655
+ ) {
656
+ diffStart++;
657
+ }
658
+
659
+ const pathUp = currentChain.slice(diffStart);
660
+ const pathDown = targetChain.slice(diffStart);
661
+ const commonScope = targetChain[diffStart - 1] ?? null;
662
+
663
+ return {
664
+ pathUp,
665
+ pathDown,
666
+ memberPath,
667
+ commonScope,
668
+ targetDeclaration:
669
+ memberPath && memberPath.length > 0 ?
670
+ memberPath.at(0)!
671
+ : targetDeclarationBase,
672
+ };
673
+ }
674
+
675
+ function scopeAndMemberChain<
676
+ TScope extends OutputScope,
677
+ TSymbol extends OutputSymbol,
678
+ >(symbol: TSymbol) {
679
+ const result = {
680
+ memberPath: [] as TSymbol[],
681
+ scopeChain: [] as TScope[],
682
+ };
683
+
684
+ let currentSymbol = symbol;
685
+ while (currentSymbol.flags & OutputSymbolFlags.StaticMember) {
686
+ result.memberPath.unshift(currentSymbol);
687
+ currentSymbol = currentSymbol.scope.owner! as TSymbol;
688
+ }
689
+
690
+ if (symbol.flags & OutputSymbolFlags.StaticMember) {
691
+ result.memberPath.unshift(currentSymbol);
692
+ }
693
+
694
+ const startScope = currentSymbol.scope as TScope;
695
+ result.scopeChain = scopeChain(startScope);
696
+
697
+ return result;
698
+ }
699
+
700
+ function scopeChain<TScope extends OutputScope>(scope: TScope | undefined) {
701
+ const chain = [];
702
+ while (scope) {
703
+ chain.unshift(scope);
704
+ scope = scope.parent as TScope;
705
+ }
706
+
707
+ return chain;
708
+ }
709
+
710
+ function deconflict(symbol: OutputSymbol) {
711
+ const scope = symbol.scope;
712
+ const existingNames = [...scope.symbols].filter(
713
+ (sym) => sym.originalName === symbol.name,
714
+ );
715
+
716
+ if (existingNames.length < 2) {
717
+ return;
718
+ }
719
+
720
+ if (options.nameConflictResolver) {
721
+ options.nameConflictResolver(symbol.name, existingNames);
722
+ } else {
723
+ // default disambiguation is first-wins
724
+ for (let i = 1; i < existingNames.length; i++) {
725
+ existingNames[i].name = existingNames[i].originalName + "_" + (i + 1);
726
+ }
727
+ }
728
+ }
729
+
730
+ function waitForRefkey<
731
+ TScope extends OutputScope,
732
+ TSymbol extends OutputSymbol,
733
+ >(
734
+ refkey: Refkey,
735
+ cb: (
736
+ symbol: TSymbol | undefined,
737
+ ) => ResolutionResult<TScope, TSymbol> | undefined,
738
+ ): ShallowRef<ResolutionResult<TScope, TSymbol> | undefined> {
739
+ let declSignal;
740
+ if (waitingDeclarations.has(refkey)) {
741
+ declSignal = waitingDeclarations.get(refkey)! as ShallowRef<TSymbol>;
742
+ } else {
743
+ declSignal = shallowRef();
744
+ waitingDeclarations.set(refkey, declSignal);
745
+ }
746
+
747
+ return computed(() => cb(declSignal.value));
748
+ }
749
+
750
+ function notifyRefkey(
751
+ refkey: Refkey | undefined,
752
+ symbol: OutputSymbol,
753
+ ): void {
754
+ if (!refkey) return;
755
+ knownDeclarations.set(refkey, symbol);
756
+ if (waitingDeclarations.has(refkey)) {
757
+ const signal = waitingDeclarations.get(refkey)!;
758
+ signal.value = symbol;
759
+ }
760
+ }
761
+ }
762
+
763
+ /**
764
+ * Resolve a refkey in the current scope. Returns a Ref for the resolution result.
765
+ * The value of the ref will be undefined if the identifier hasn't been resolved yet.
766
+ *
767
+ * @remarks
768
+ *
769
+ * This API may return a ref for undefined, but that does not mean that the symbol is
770
+ * not found. The symbol you're looking for may not have been declared yet. When the symbol
771
+ * is declared, the ref will be updated with the resolution result.
772
+ */
773
+ export function resolve<
774
+ TScope extends OutputScope,
775
+ TSymbol extends OutputSymbol,
776
+ >(refkey: Refkey): Ref<ResolutionResult<TScope, TSymbol>> {
777
+ const scope = useScope();
778
+ const memberScope = useMemberScope();
779
+ const binder = scope.binder;
780
+
781
+ return binder.resolveDeclarationByKey(
782
+ scope,
783
+ memberScope?.instanceMembers,
784
+ refkey,
785
+ ) as any;
786
+ }
787
+
788
+ const createSymbolsSymbol: unique symbol = Symbol();
789
+ export function getSymbolCreator(
790
+ creator: SymbolCreator,
791
+ ): SymbolCreator[typeof createSymbolsSymbol] {
792
+ return creator[createSymbolsSymbol];
793
+ }
794
+
795
+ export function getSymbolCreatorSymbol(): typeof createSymbolsSymbol {
796
+ return createSymbolsSymbol;
797
+ }
798
+
799
+ export interface SymbolCreator {
800
+ [createSymbolsSymbol](binder: Binder): void;
801
+ }
802
+
803
+ function symbolNames(symbols: Set<OutputSymbol>): () => Set<string> {
804
+ return memo(() => {
805
+ const names = new Set<string>();
806
+ for (const sym of symbols.values()) {
807
+ names.add(sym.name);
808
+ }
809
+
810
+ return names;
811
+ });
812
+ }
813
+
814
+ /**
815
+ * Use symbol flags to determine the scope in which a symbol with those flags
816
+ * should be declared given the current context.
817
+ *
818
+ * @param flags - The symbol flags to use to determine the default scope.
819
+ * @returns an {@link OutputScope} that is the default scope for the given
820
+ * flags.
821
+ */
822
+ export function useDefaultScope(
823
+ flags: OutputSymbolFlags = OutputSymbolFlags.None,
824
+ ) {
825
+ if ((flags & OutputSymbolFlags.Member) === 0) {
826
+ return useScope();
827
+ } else {
828
+ const memberScope = useMemberScope();
829
+ if (!memberScope) {
830
+ throw new Error("Cannot declare member symbols without a member scope");
831
+ }
832
+ if (flags & OutputSymbolFlags.InstanceMember) {
833
+ return memberScope.instanceMembers;
834
+ } else {
835
+ return memberScope.staticMembers;
836
+ }
837
+ }
838
+ }