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