arktype 2.0.0-dev.2 → 2.0.0-dev.20

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 (223) hide show
  1. package/LICENSE +7 -0
  2. package/out/api.d.ts +6 -0
  3. package/out/api.js +5 -0
  4. package/out/ark.d.ts +15 -39
  5. package/out/ark.js +9 -18
  6. package/out/config.d.ts +1 -0
  7. package/out/config.js +2 -0
  8. package/out/generic.d.ts +21 -0
  9. package/out/generic.js +24 -0
  10. package/out/match.d.ts +18 -24
  11. package/out/match.js +50 -60
  12. package/out/module.d.ts +11 -0
  13. package/out/module.js +2 -0
  14. package/out/parser/definition.d.ts +13 -15
  15. package/out/parser/definition.js +10 -13
  16. package/out/parser/generic.d.ts +5 -7
  17. package/out/parser/generic.js +7 -11
  18. package/out/parser/objectLiteral.d.ts +57 -18
  19. package/out/parser/objectLiteral.js +93 -52
  20. package/out/parser/semantic/bounds.d.ts +10 -8
  21. package/out/parser/semantic/bounds.js +0 -1
  22. package/out/parser/semantic/divisor.d.ts +5 -7
  23. package/out/parser/semantic/divisor.js +0 -1
  24. package/out/parser/semantic/infer.d.ts +24 -0
  25. package/out/parser/semantic/infer.js +1 -0
  26. package/out/parser/semantic/utils.d.ts +3 -7
  27. package/out/parser/semantic/utils.js +0 -1
  28. package/out/parser/semantic/validate.d.ts +16 -13
  29. package/out/parser/semantic/validate.js +1 -1
  30. package/out/parser/string/reduce/dynamic.d.ts +13 -13
  31. package/out/parser/string/reduce/dynamic.js +31 -26
  32. package/out/parser/string/reduce/shared.d.ts +14 -28
  33. package/out/parser/string/reduce/shared.js +4 -3
  34. package/out/parser/string/reduce/static.d.ts +26 -23
  35. package/out/parser/string/reduce/static.js +0 -1
  36. package/out/parser/string/shift/operand/date.d.ts +2 -2
  37. package/out/parser/string/shift/operand/date.js +4 -7
  38. package/out/parser/string/shift/operand/enclosed.d.ts +3 -4
  39. package/out/parser/string/shift/operand/enclosed.js +14 -14
  40. package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
  41. package/out/parser/string/shift/operand/genericArgs.js +6 -10
  42. package/out/parser/string/shift/operand/operand.d.ts +3 -4
  43. package/out/parser/string/shift/operand/operand.js +9 -13
  44. package/out/parser/string/shift/operand/unenclosed.d.ts +15 -20
  45. package/out/parser/string/shift/operand/unenclosed.js +24 -36
  46. package/out/parser/string/shift/operator/bounds.d.ts +8 -12
  47. package/out/parser/string/shift/operator/bounds.js +45 -56
  48. package/out/parser/string/shift/operator/divisor.d.ts +1 -3
  49. package/out/parser/string/shift/operator/divisor.js +1 -3
  50. package/out/parser/string/shift/operator/operator.d.ts +1 -2
  51. package/out/parser/string/shift/operator/operator.js +12 -19
  52. package/out/parser/string/shift/scanner.d.ts +8 -7
  53. package/out/parser/string/shift/scanner.js +17 -16
  54. package/out/parser/string/string.d.ts +9 -7
  55. package/out/parser/string/string.js +1 -3
  56. package/out/parser/tuple.d.ts +101 -42
  57. package/out/parser/tuple.js +107 -86
  58. package/out/scope.d.ts +44 -115
  59. package/out/scope.js +39 -188
  60. package/out/type.d.ts +63 -61
  61. package/out/type.js +30 -135
  62. package/package.json +33 -39
  63. package/__tests__/array.test.ts +0 -310
  64. package/__tests__/astUtils.test.ts +0 -17
  65. package/__tests__/badDefinitionType.test.ts +0 -51
  66. package/__tests__/basis.test.ts +0 -46
  67. package/__tests__/bounds.test.ts +0 -388
  68. package/__tests__/cast.test.ts +0 -32
  69. package/__tests__/completions.test.ts +0 -46
  70. package/__tests__/config.test.ts +0 -77
  71. package/__tests__/cyclic.test.ts +0 -144
  72. package/__tests__/dateLiteral.test.ts +0 -32
  73. package/__tests__/declared.test.ts +0 -124
  74. package/__tests__/discrimination.test.ts +0 -74
  75. package/__tests__/divisor.test.ts +0 -90
  76. package/__tests__/enclosed.test.ts +0 -93
  77. package/__tests__/examples.test.ts +0 -56
  78. package/__tests__/expressions.test.ts +0 -138
  79. package/__tests__/generateBenchData.ts +0 -89
  80. package/__tests__/generated/cyclic.ts +0 -3058
  81. package/__tests__/generic.test.ts +0 -294
  82. package/__tests__/group.test.ts +0 -72
  83. package/__tests__/hkt.test.ts +0 -22
  84. package/__tests__/imports.test.ts +0 -113
  85. package/__tests__/instanceof.test.ts +0 -102
  86. package/__tests__/intersection.test.ts +0 -104
  87. package/__tests__/jsdoc.test.ts +0 -64
  88. package/__tests__/keyTraversal.test.ts +0 -74
  89. package/__tests__/keyof.test.ts +0 -134
  90. package/__tests__/keywords.test.ts +0 -188
  91. package/__tests__/literal.test.ts +0 -51
  92. package/__tests__/match.bench.ts +0 -31
  93. package/__tests__/match.test.ts +0 -224
  94. package/__tests__/methods.bench.ts +0 -22
  95. package/__tests__/morph.test.ts +0 -409
  96. package/__tests__/narrow.test.ts +0 -102
  97. package/__tests__/nodeDefinitions.test.ts +0 -190
  98. package/__tests__/object.bench.ts +0 -28
  99. package/__tests__/objectLiteral.test.ts +0 -301
  100. package/__tests__/operand.bench.ts +0 -45
  101. package/__tests__/operator.bench.ts +0 -130
  102. package/__tests__/regex.test.ts +0 -58
  103. package/__tests__/scope.bench.ts +0 -39
  104. package/__tests__/scope.test.ts +0 -140
  105. package/__tests__/string.test.ts +0 -48
  106. package/__tests__/submodule.test.ts +0 -99
  107. package/__tests__/this.test.ts +0 -57
  108. package/__tests__/thunk.test.ts +0 -80
  109. package/__tests__/traverse.test.ts +0 -119
  110. package/__tests__/tsGenerics.test.ts +0 -12
  111. package/__tests__/typeMethods.test.ts +0 -69
  112. package/__tests__/typeReference.test.ts +0 -39
  113. package/__tests__/unenclosed.test.ts +0 -120
  114. package/__tests__/union.test.ts +0 -147
  115. package/ark.ts +0 -91
  116. package/main.ts +0 -18
  117. package/match.ts +0 -243
  118. package/out/ark.d.ts.map +0 -1
  119. package/out/ark.js.map +0 -1
  120. package/out/arkConfig.d.ts +0 -2
  121. package/out/arkConfig.d.ts.map +0 -1
  122. package/out/arkConfig.js +0 -10
  123. package/out/arkConfig.js.map +0 -1
  124. package/out/main.d.ts +0 -7
  125. package/out/main.d.ts.map +0 -1
  126. package/out/main.js +0 -4
  127. package/out/main.js.map +0 -1
  128. package/out/match.d.ts.map +0 -1
  129. package/out/match.js.map +0 -1
  130. package/out/parser/definition.d.ts.map +0 -1
  131. package/out/parser/definition.js.map +0 -1
  132. package/out/parser/generic.d.ts.map +0 -1
  133. package/out/parser/generic.js.map +0 -1
  134. package/out/parser/objectLiteral.d.ts.map +0 -1
  135. package/out/parser/objectLiteral.js.map +0 -1
  136. package/out/parser/semantic/bounds.d.ts.map +0 -1
  137. package/out/parser/semantic/bounds.js.map +0 -1
  138. package/out/parser/semantic/divisor.d.ts.map +0 -1
  139. package/out/parser/semantic/divisor.js.map +0 -1
  140. package/out/parser/semantic/intersections.d.ts +0 -14
  141. package/out/parser/semantic/intersections.d.ts.map +0 -1
  142. package/out/parser/semantic/intersections.js +0 -2
  143. package/out/parser/semantic/intersections.js.map +0 -1
  144. package/out/parser/semantic/semantic.d.ts +0 -33
  145. package/out/parser/semantic/semantic.d.ts.map +0 -1
  146. package/out/parser/semantic/semantic.js +0 -2
  147. package/out/parser/semantic/semantic.js.map +0 -1
  148. package/out/parser/semantic/utils.d.ts.map +0 -1
  149. package/out/parser/semantic/utils.js.map +0 -1
  150. package/out/parser/semantic/validate.d.ts.map +0 -1
  151. package/out/parser/semantic/validate.js.map +0 -1
  152. package/out/parser/shared.d.ts +0 -56
  153. package/out/parser/shared.d.ts.map +0 -1
  154. package/out/parser/shared.js +0 -55
  155. package/out/parser/shared.js.map +0 -1
  156. package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
  157. package/out/parser/string/reduce/dynamic.js.map +0 -1
  158. package/out/parser/string/reduce/shared.d.ts.map +0 -1
  159. package/out/parser/string/reduce/shared.js.map +0 -1
  160. package/out/parser/string/reduce/static.d.ts.map +0 -1
  161. package/out/parser/string/reduce/static.js.map +0 -1
  162. package/out/parser/string/shift/operand/date.d.ts.map +0 -1
  163. package/out/parser/string/shift/operand/date.js.map +0 -1
  164. package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
  165. package/out/parser/string/shift/operand/enclosed.js.map +0 -1
  166. package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
  167. package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
  168. package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
  169. package/out/parser/string/shift/operand/operand.js.map +0 -1
  170. package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  171. package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
  172. package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
  173. package/out/parser/string/shift/operator/bounds.js.map +0 -1
  174. package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
  175. package/out/parser/string/shift/operator/divisor.js.map +0 -1
  176. package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
  177. package/out/parser/string/shift/operator/operator.js.map +0 -1
  178. package/out/parser/string/shift/scanner.d.ts.map +0 -1
  179. package/out/parser/string/shift/scanner.js.map +0 -1
  180. package/out/parser/string/string.d.ts.map +0 -1
  181. package/out/parser/string/string.js.map +0 -1
  182. package/out/parser/tuple.d.ts.map +0 -1
  183. package/out/parser/tuple.js.map +0 -1
  184. package/out/scope.d.ts.map +0 -1
  185. package/out/scope.js.map +0 -1
  186. package/out/test.d.ts +0 -2
  187. package/out/test.d.ts.map +0 -1
  188. package/out/test.js +0 -7
  189. package/out/test.js.map +0 -1
  190. package/out/tsconfig.build.tsbuildinfo +0 -1
  191. package/out/type.d.ts.map +0 -1
  192. package/out/type.js.map +0 -1
  193. package/out/util.d.ts +0 -19
  194. package/out/util.d.ts.map +0 -1
  195. package/out/util.js +0 -7
  196. package/out/util.js.map +0 -1
  197. package/parser/definition.ts +0 -159
  198. package/parser/generic.ts +0 -68
  199. package/parser/objectLiteral.ts +0 -197
  200. package/parser/semantic/bounds.ts +0 -61
  201. package/parser/semantic/divisor.ts +0 -13
  202. package/parser/semantic/intersections.ts +0 -46
  203. package/parser/semantic/semantic.ts +0 -143
  204. package/parser/semantic/utils.ts +0 -26
  205. package/parser/semantic/validate.ts +0 -109
  206. package/parser/shared.ts +0 -167
  207. package/parser/string/reduce/dynamic.ts +0 -196
  208. package/parser/string/reduce/shared.ts +0 -101
  209. package/parser/string/reduce/static.ts +0 -298
  210. package/parser/string/shift/operand/date.ts +0 -54
  211. package/parser/string/shift/operand/enclosed.ts +0 -118
  212. package/parser/string/shift/operand/genericArgs.ts +0 -127
  213. package/parser/string/shift/operand/operand.ts +0 -48
  214. package/parser/string/shift/operand/unenclosed.ts +0 -268
  215. package/parser/string/shift/operator/bounds.ts +0 -264
  216. package/parser/string/shift/operator/divisor.ts +0 -36
  217. package/parser/string/shift/operator/operator.ts +0 -82
  218. package/parser/string/shift/scanner.ts +0 -209
  219. package/parser/string/string.ts +0 -90
  220. package/parser/tuple.ts +0 -475
  221. package/scope.ts +0 -548
  222. package/type.ts +0 -349
  223. package/util.ts +0 -36
package/scope.ts DELETED
@@ -1,548 +0,0 @@
1
- import {
2
- BaseType,
3
- keywords,
4
- type ArkConfig,
5
- type TypeNode,
6
- type extractIn,
7
- type extractOut
8
- } from "@arktype/schema"
9
- import {
10
- domainOf,
11
- hasDomain,
12
- isThunk,
13
- map,
14
- throwParseError,
15
- type Dict,
16
- type evaluate,
17
- type isAny,
18
- type nominal
19
- } from "@arktype/util"
20
- import type { type } from "./ark.js"
21
- import { createMatchParser, type MatchParser } from "./match.js"
22
- import {
23
- parseObject,
24
- writeBadDefinitionTypeMessage,
25
- type inferDefinition,
26
- type validateDefinition
27
- } from "./parser/definition.js"
28
- import {
29
- parseGenericParams,
30
- type GenericDeclaration,
31
- type GenericParamsParseError
32
- } from "./parser/generic.js"
33
- import { DynamicState } from "./parser/string/reduce/dynamic.js"
34
- import {
35
- writeMissingSubmoduleAccessMessage,
36
- writeNonSubmoduleDotMessage,
37
- writeUnresolvableMessage
38
- } from "./parser/string/shift/operand/unenclosed.js"
39
- import { fullStringParse } from "./parser/string/string.js"
40
- import {
41
- Type,
42
- createTypeParser,
43
- generic,
44
- validateUninstantiatedGeneric,
45
- type DeclarationParser,
46
- type DefinitionParser,
47
- type Generic,
48
- type GenericProps,
49
- type TypeParser
50
- } from "./type.js"
51
- import { addArkKind, hasArkKind, type arkKind } from "./util.js"
52
-
53
- export type ScopeParser<parent, ambient> = {
54
- <const def>(def: validateScope<def, parent & ambient>): Scope<{
55
- exports: inferBootstrapped<{
56
- exports: bootstrapExports<def>
57
- locals: bootstrapLocals<def> & parent
58
- ambient: ambient
59
- }>
60
- locals: inferBootstrapped<{
61
- exports: bootstrapLocals<def>
62
- locals: bootstrapExports<def> & parent
63
- ambient: ambient
64
- }>
65
- ambient: ambient
66
- }>
67
- }
68
-
69
- export interface ArkScopeConfig extends ArkConfig {
70
- ambient?: Scope | null
71
- }
72
-
73
- type validateScope<def, $> = {
74
- [k in keyof def]: parseScopeKey<k>["params"] extends []
75
- ? // Not including Type here directly breaks inference
76
- def[k] extends Type | PreparsedResolution
77
- ? def[k]
78
- : validateDefinition<def[k], $ & bootstrap<def>, {}>
79
- : parseScopeKey<k>["params"] extends GenericParamsParseError
80
- ? // use the full nominal type here to avoid an overlap between the
81
- // error message and a possible value for the property
82
- parseScopeKey<k>["params"][0]
83
- : validateDefinition<
84
- def[k],
85
- $ & bootstrap<def>,
86
- {
87
- // once we support constraints on generic parameters, we'd use
88
- // the base type here: https://github.com/arktypeio/arktype/issues/796
89
- [param in parseScopeKey<k>["params"][number]]: unknown
90
- }
91
- >
92
- }
93
-
94
- export type bindThis<def> = { this: Def<def> }
95
-
96
- /** nominal type for an unparsed definition used during scope bootstrapping */
97
- type Def<def = {}> = nominal<def, "unparsed">
98
-
99
- /** sentinel indicating a scope that will be associated with a generic has not yet been parsed */
100
- export type UnparsedScope = "$"
101
-
102
- type bootstrap<def> = bootstrapLocals<def> & bootstrapExports<def>
103
-
104
- type bootstrapLocals<def> = bootstrapAliases<{
105
- // intersection seems redundant but it is more efficient for TS to avoid
106
- // mapping all the keys
107
- [k in keyof def & PrivateDeclaration as extractPrivateKey<k>]: def[k]
108
- }>
109
-
110
- type bootstrapExports<def> = bootstrapAliases<{
111
- [k in Exclude<keyof def, PrivateDeclaration>]: def[k]
112
- }>
113
-
114
- /** These are legal as values of a scope but not as definitions in other contexts */
115
- type PreparsedResolution = Module | GenericProps
116
-
117
- type bootstrapAliases<def> = {
118
- [k in Exclude<
119
- keyof def,
120
- // avoid inferring nominal symbols, e.g. arkKind from Module
121
- GenericDeclaration | symbol
122
- >]: def[k] extends PreparsedResolution
123
- ? def[k]
124
- : def[k] extends (() => infer thunkReturn extends PreparsedResolution)
125
- ? thunkReturn
126
- : Def<def[k]>
127
- } & {
128
- [k in keyof def & GenericDeclaration as extractGenericName<k>]: Generic<
129
- parseGenericParams<extractGenericParameters<k>>,
130
- def[k],
131
- UnparsedScope
132
- >
133
- }
134
-
135
- type inferBootstrapped<r extends Resolutions> = evaluate<{
136
- [name in keyof r["exports"]]: r["exports"][name] extends Def<infer def>
137
- ? inferDefinition<def, $<r>, {}>
138
- : r["exports"][name] extends GenericProps<infer params, infer def>
139
- ? // add the scope in which the generic was defined here
140
- Generic<params, def, $<r>>
141
- : // otherwise should be a submodule
142
- r["exports"][name]
143
- }>
144
-
145
- type extractGenericName<k> = k extends GenericDeclaration<infer name>
146
- ? name
147
- : never
148
-
149
- type extractGenericParameters<k> = k extends GenericDeclaration<
150
- string,
151
- infer params
152
- >
153
- ? params
154
- : never
155
-
156
- type extractPrivateKey<k> = k extends PrivateDeclaration<infer key>
157
- ? key
158
- : never
159
-
160
- type PrivateDeclaration<key extends string = string> = `#${key}`
161
-
162
- export type resolve<reference extends keyof $ | keyof args, $, args> = (
163
- reference extends keyof args ? args[reference] : $[reference & keyof $]
164
- ) extends infer resolution
165
- ? [resolution] extends [never]
166
- ? never
167
- : isAny<resolution> extends true
168
- ? any
169
- : resolution extends Def<infer def>
170
- ? inferDefinition<def, $, args>
171
- : resolution
172
- : never
173
-
174
- export type moduleKeyOf<$> = {
175
- [k in keyof $]: $[k] extends Module ? k & string : never
176
- }[keyof $]
177
-
178
- export type tryInferSubmoduleReference<$, token> =
179
- token extends `${infer submodule extends moduleKeyOf<$>}.${infer subalias}`
180
- ? subalias extends keyof $[submodule]
181
- ? $[submodule][subalias] extends Type<infer t>
182
- ? t
183
- : never
184
- : never
185
- : never
186
-
187
- type $<r extends Resolutions> = r["exports"] & r["locals"] & r["ambient"]
188
-
189
- type exportedName<r extends Resolutions> = keyof r["exports"] & string
190
-
191
- export type Module<r extends Resolutions = any> = {
192
- // just adding the nominal id this way and mapping it is cheaper than an intersection
193
- [k in exportedName<r> | arkKind]: k extends string
194
- ? [r["exports"][k]] extends [never]
195
- ? Type<never, $<r>>
196
- : isAny<r["exports"][k]> extends true
197
- ? Type<any, $<r>>
198
- : r["exports"][k] extends PreparsedResolution
199
- ? r["exports"][k]
200
- : Type<r["exports"][k], $<r>>
201
- : // set the nominal symbol's value to something validation won't care about
202
- // since the inferred type will be omitted anyways
203
- type.cast<"module">
204
- }
205
-
206
- export type Resolutions = {
207
- exports: unknown
208
- locals: unknown
209
- ambient: unknown
210
- }
211
-
212
- export type ParseContext = {
213
- baseName: string
214
- path: string[]
215
- scope: Scope
216
- args: Record<string, TypeNode> | undefined
217
- }
218
-
219
- type MergedResolutions = Record<string, TypeNode | Generic>
220
-
221
- type ParseContextInput = Pick<ParseContext, "baseName" | "args">
222
-
223
- export class Scope<r extends Resolutions = any> {
224
- declare infer: extractOut<r["exports"]>
225
- declare inferIn: extractIn<r["exports"]>
226
-
227
- config: ArkScopeConfig
228
-
229
- private parseCache: Record<string, TypeNode> = {}
230
- private resolutions: MergedResolutions
231
-
232
- /** The set of names defined at the root-level of the scope mapped to their
233
- * corresponding definitions.**/
234
- aliases: Record<string, unknown> = {}
235
- private exportedNames: exportedName<r>[] = []
236
- private ambient: Scope | null
237
-
238
- constructor(def: Dict, config: ArkScopeConfig) {
239
- for (const k in def) {
240
- const parsedKey = parseScopeKey(k)
241
- this.aliases[parsedKey.name] = parsedKey.params.length
242
- ? generic(parsedKey.params, def[k], this)
243
- : def[k]
244
- if (!parsedKey.isLocal) {
245
- this.exportedNames.push(parsedKey.name as never)
246
- }
247
- }
248
- this.ambient = config.ambient ?? null
249
- if (this.ambient) {
250
- // ensure exportedResolutions is populated
251
- this.ambient.export()
252
- this.resolutions = { ...this.ambient.exportedResolutions! }
253
- } else {
254
- this.resolutions = {}
255
- }
256
- this.config = config
257
- }
258
-
259
- static root: ScopeParser<{}, {}> = (aliases) => {
260
- return new Scope(aliases, {}) as never
261
- }
262
-
263
- type: TypeParser<$<r>> = createTypeParser(this as never) as never
264
-
265
- match: MatchParser<$<r>> = createMatchParser(this as never) as never
266
-
267
- // TODO: decide if this API will be used for non-validated types
268
- declare: DeclarationParser<$<r>> = () => ({ type: this.type }) as never
269
-
270
- scope: ScopeParser<r["exports"], r["ambient"]> = ((
271
- def: Dict,
272
- config: ArkScopeConfig = {}
273
- ) => {
274
- return new Scope(def, {
275
- ambient: this.ambient,
276
- ...this.config,
277
- ...config
278
- })
279
- }) as never
280
-
281
- define: DefinitionParser<$<r>> = (def) => def as never
282
-
283
- toAmbient(): Scope<{
284
- exports: r["exports"]
285
- locals: r["locals"]
286
- ambient: r["exports"]
287
- }> {
288
- // TODO: private?
289
- return new Scope(this.aliases, {
290
- ambient: this,
291
- ...this.config
292
- })
293
- }
294
-
295
- // TODO: name?
296
- get<name extends keyof r["exports"] & string>(
297
- name: name
298
- ): Type<r["exports"][name], $<r>> {
299
- return this.export()[name] as never
300
- }
301
-
302
- private createRootContext(input: ParseContextInput): ParseContext {
303
- return {
304
- path: [],
305
- scope: this,
306
- ...input
307
- }
308
- }
309
-
310
- parseDefinition(def: unknown, input: ParseContextInput) {
311
- return this.parse(def, this.createRootContext(input))
312
- }
313
-
314
- parse(def: unknown, ctx: ParseContext): TypeNode {
315
- if (typeof def === "string") {
316
- if (ctx.args !== undefined) {
317
- // we can only rely on the cache if there are no contextual
318
- // resolutions like "this" or generic args
319
- return this.parseString(def, ctx)
320
- }
321
- if (!this.parseCache[def]) {
322
- this.parseCache[def] = this.parseString(def, ctx)
323
- }
324
- return this.parseCache[def]
325
- }
326
- return hasDomain(def, "object")
327
- ? parseObject(def, ctx)
328
- : throwParseError(writeBadDefinitionTypeMessage(domainOf(def)))
329
- }
330
-
331
- parseString(def: string, ctx: ParseContext): TypeNode {
332
- return (
333
- this.maybeResolveNode(def) ??
334
- ((def.endsWith("[]") &&
335
- this.maybeResolveNode(def.slice(0, -2))?.array()) ||
336
- fullStringParse(new DynamicState(def, ctx)))
337
- )
338
- }
339
-
340
- maybeResolve(name: string): TypeNode | Generic | undefined {
341
- const cached = this.resolutions[name]
342
- if (cached) {
343
- return cached
344
- }
345
- let def = this.aliases[name]
346
- if (!def) {
347
- return this.maybeResolveSubalias(name)
348
- }
349
- if (isThunk(def) && !hasArkKind(def, "generic")) {
350
- def = def()
351
- }
352
- // TODO: initialize here?
353
- const resolution = hasArkKind(def, "generic")
354
- ? validateUninstantiatedGeneric(def)
355
- : hasArkKind(def, "module")
356
- ? throwParseError(writeMissingSubmoduleAccessMessage(name))
357
- : this.parseDefinition(
358
- def,
359
- this.createRootContext({ baseName: name, args: {} })
360
- )
361
- this.resolutions[name] = resolution
362
- return resolution
363
- }
364
-
365
- /** If name is a valid reference to a submodule alias, return its resolution */
366
- private maybeResolveSubalias(name: string) {
367
- const dotIndex = name.indexOf(".")
368
- if (dotIndex === -1) {
369
- return
370
- }
371
- const dotPrefix = name.slice(0, dotIndex)
372
- const prefixDef = this.aliases[dotPrefix]
373
- if (hasArkKind(prefixDef, "module")) {
374
- const resolution = prefixDef[name.slice(dotIndex + 1)]?.root
375
- // if the first part of name is a submodule but the suffix is
376
- // unresolvable, we can throw immediately
377
- if (!resolution) {
378
- return throwParseError(writeUnresolvableMessage(name))
379
- }
380
- this.resolutions[name] = resolution
381
- return resolution
382
- }
383
- if (prefixDef !== undefined) {
384
- return throwParseError(writeNonSubmoduleDotMessage(dotPrefix))
385
- }
386
- // if the name includes ".", but the prefix is not an alias, it
387
- // might be something like a decimal literal, so just fall through to return
388
- }
389
-
390
- maybeResolveNode(name: string): TypeNode | undefined {
391
- const result = this.maybeResolve(name)
392
- return result instanceof BaseType ? (result as never) : undefined
393
- }
394
-
395
- import<names extends exportedName<r>[]>(
396
- ...names: names
397
- ): destructuredImportContext<
398
- r,
399
- names extends [] ? keyof r["exports"] & string : names[number]
400
- > {
401
- return addArkKind(
402
- map(this.export(...names) as Dict, (alias, value) => [
403
- `#${alias}`,
404
- value
405
- ]) as never,
406
- "module"
407
- ) as never
408
- }
409
-
410
- bindThis() {
411
- // TODO: fix
412
- return { this: keywords.unknown }
413
- }
414
-
415
- private exportedResolutions: MergedResolutions | undefined
416
- private exportCache: ExportCache | undefined
417
- export<names extends exportedName<r>[]>(
418
- ...names: names
419
- ): Module<
420
- names extends [] ? r : destructuredExportContext<r, names[number]>
421
- > {
422
- if (!this.exportCache) {
423
- this.exportCache = {}
424
- for (const name of this.exportedNames) {
425
- let def = this.aliases[name]
426
- if (hasArkKind(def, "generic")) {
427
- this.exportCache[name] = def
428
- continue
429
- }
430
- // TODO: thunk generics?
431
- // handle generics before invoking thunks, since they use
432
- // varargs they will incorrectly be considered thunks
433
- if (isThunk(def)) {
434
- def = def()
435
- }
436
- if (hasArkKind(def, "module")) {
437
- this.exportCache[name] = def
438
- } else {
439
- this.exportCache[name] = new Type(this.maybeResolve(name), this)
440
- }
441
- }
442
- this.exportedResolutions = resolutionsOfModule(this.exportCache)
443
- Object.assign(this.resolutions, this.exportedResolutions)
444
- }
445
- const namesToExport = names.length ? names : this.exportedNames
446
- return addArkKind(
447
- map(namesToExport, (_, name) => [name, this.exportCache![name]]) as never,
448
- "module"
449
- ) as never
450
- }
451
- }
452
-
453
- type ExportCache = Record<string, Type | Generic | Module>
454
-
455
- const resolutionsOfModule = (typeSet: ExportCache) => {
456
- const result: MergedResolutions = {}
457
- for (const k in typeSet) {
458
- const v = typeSet[k]
459
- if (hasArkKind(v, "module")) {
460
- const innerResolutions = resolutionsOfModule(v as never)
461
- const prefixedResolutions = map(innerResolutions, (innerK, innerV) => [
462
- `${k}.${innerK}`,
463
- innerV
464
- ])
465
- Object.assign(result, prefixedResolutions)
466
- } else if (hasArkKind(v, "generic")) {
467
- result[k] = v
468
- } else {
469
- result[k] = (v as Type).root
470
- }
471
- }
472
- return result
473
- }
474
-
475
- type destructuredExportContext<
476
- r extends Resolutions,
477
- name extends exportedName<r>
478
- > = {
479
- exports: { [k in name]: r["exports"][k] }
480
- locals: r["locals"] & {
481
- [k in Exclude<keyof r["exports"], name>]: r["exports"][k]
482
- }
483
- ambient: r["ambient"]
484
- }
485
-
486
- type destructuredImportContext<
487
- r extends Resolutions,
488
- name extends exportedName<r>
489
- > = {
490
- [k in name as `#${k & string}`]: type.cast<r["exports"][k]>
491
- }
492
-
493
- export const writeShallowCycleErrorMessage = (name: string, seen: string[]) =>
494
- `Alias '${name}' has a shallow resolution cycle: ${[...seen, name].join(":")}`
495
-
496
- export const writeDuplicateNameMessage = <name extends string>(
497
- name: name
498
- ): writeDuplicateNameMessage<name> => `Duplicate name '${name}'`
499
-
500
- type writeDuplicateNameMessage<name extends string> = `Duplicate name '${name}'`
501
-
502
- export type ParsedScopeKey = {
503
- isLocal: boolean
504
- name: string
505
- params: string[]
506
- }
507
-
508
- export const parseScopeKey = (k: string): ParsedScopeKey => {
509
- const isLocal = k[0] === "#"
510
- const name = isLocal ? k.slice(1) : k
511
- const firstParamIndex = k.indexOf("<")
512
- if (firstParamIndex === -1) {
513
- return {
514
- isLocal,
515
- name,
516
- params: []
517
- }
518
- }
519
- if (k.at(-1) !== ">") {
520
- throwParseError(
521
- `'>' must be the last character of a generic declaration in a scope`
522
- )
523
- }
524
- return {
525
- isLocal,
526
- name: name.slice(0, firstParamIndex),
527
- params: parseGenericParams(k.slice(firstParamIndex + 1, -1))
528
- }
529
- }
530
-
531
- type parseScopeKey<k> = k extends PrivateDeclaration<infer inner>
532
- ? parsePossibleGenericDeclaration<inner, true>
533
- : parsePossibleGenericDeclaration<k, false>
534
-
535
- type parsePossibleGenericDeclaration<
536
- k,
537
- isLocal extends boolean
538
- > = k extends GenericDeclaration<infer name, infer paramString>
539
- ? {
540
- isLocal: isLocal
541
- name: name
542
- params: parseGenericParams<paramString>
543
- }
544
- : {
545
- isLocal: isLocal
546
- name: k
547
- params: []
548
- }