arktype 2.0.0-dev.1 → 2.0.0-dev.11

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 (221) hide show
  1. package/out/api.d.ts +6 -0
  2. package/out/api.js +5 -0
  3. package/out/ark.d.ts +15 -39
  4. package/out/ark.js +9 -18
  5. package/out/config.d.ts +1 -0
  6. package/out/config.js +2 -0
  7. package/out/generic.d.ts +21 -0
  8. package/out/generic.js +24 -0
  9. package/out/main.d.ts +6 -7
  10. package/out/main.js +4 -3
  11. package/out/match.d.ts +57 -34
  12. package/out/match.js +51 -17
  13. package/out/module.d.ts +12 -0
  14. package/out/module.js +3 -0
  15. package/out/parser/definition.d.ts +14 -14
  16. package/out/parser/definition.js +10 -13
  17. package/out/parser/generic.d.ts +5 -7
  18. package/out/parser/generic.js +7 -11
  19. package/out/parser/objectLiteral.d.ts +44 -18
  20. package/out/parser/objectLiteral.js +57 -51
  21. package/out/parser/semantic/bounds.d.ts +10 -8
  22. package/out/parser/semantic/bounds.js +0 -1
  23. package/out/parser/semantic/divisor.d.ts +5 -7
  24. package/out/parser/semantic/divisor.js +0 -1
  25. package/out/parser/semantic/infer.d.ts +24 -0
  26. package/out/parser/semantic/infer.js +1 -0
  27. package/out/parser/semantic/utils.d.ts +3 -7
  28. package/out/parser/semantic/utils.js +0 -1
  29. package/out/parser/semantic/validate.d.ts +16 -13
  30. package/out/parser/semantic/validate.js +1 -1
  31. package/out/parser/string/reduce/dynamic.d.ts +13 -13
  32. package/out/parser/string/reduce/dynamic.js +31 -26
  33. package/out/parser/string/reduce/shared.d.ts +14 -28
  34. package/out/parser/string/reduce/shared.js +4 -3
  35. package/out/parser/string/reduce/static.d.ts +27 -24
  36. package/out/parser/string/reduce/static.js +0 -1
  37. package/out/parser/string/shift/operand/date.d.ts +2 -2
  38. package/out/parser/string/shift/operand/date.js +4 -7
  39. package/out/parser/string/shift/operand/enclosed.d.ts +3 -4
  40. package/out/parser/string/shift/operand/enclosed.js +14 -14
  41. package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
  42. package/out/parser/string/shift/operand/genericArgs.js +6 -10
  43. package/out/parser/string/shift/operand/operand.d.ts +3 -4
  44. package/out/parser/string/shift/operand/operand.js +8 -12
  45. package/out/parser/string/shift/operand/unenclosed.d.ts +16 -21
  46. package/out/parser/string/shift/operand/unenclosed.js +24 -36
  47. package/out/parser/string/shift/operator/bounds.d.ts +8 -12
  48. package/out/parser/string/shift/operator/bounds.js +45 -56
  49. package/out/parser/string/shift/operator/divisor.d.ts +1 -3
  50. package/out/parser/string/shift/operator/divisor.js +1 -3
  51. package/out/parser/string/shift/operator/operator.d.ts +1 -2
  52. package/out/parser/string/shift/operator/operator.js +12 -19
  53. package/out/parser/string/shift/scanner.d.ts +2 -2
  54. package/out/parser/string/shift/scanner.js +14 -16
  55. package/out/parser/string/string.d.ts +8 -6
  56. package/out/parser/string/string.js +1 -3
  57. package/out/parser/tuple.d.ts +102 -43
  58. package/out/parser/tuple.js +119 -87
  59. package/out/scope.d.ts +44 -119
  60. package/out/scope.js +39 -209
  61. package/out/type.d.ts +43 -64
  62. package/out/type.js +30 -131
  63. package/package.json +12 -10
  64. package/__tests__/array.test.ts +0 -272
  65. package/__tests__/astUtils.test.ts +0 -17
  66. package/__tests__/badDefinitionType.test.ts +0 -51
  67. package/__tests__/basis.test.ts +0 -46
  68. package/__tests__/bounds.test.ts +0 -388
  69. package/__tests__/cast.test.ts +0 -32
  70. package/__tests__/completions.test.ts +0 -46
  71. package/__tests__/config.test.ts +0 -168
  72. package/__tests__/cyclic.test.ts +0 -144
  73. package/__tests__/dateLiteral.test.ts +0 -32
  74. package/__tests__/declared.test.ts +0 -123
  75. package/__tests__/discrimination.test.ts +0 -74
  76. package/__tests__/divisor.test.ts +0 -90
  77. package/__tests__/enclosed.test.ts +0 -93
  78. package/__tests__/examples.test.ts +0 -56
  79. package/__tests__/expressions.test.ts +0 -138
  80. package/__tests__/generateBenchData.ts +0 -89
  81. package/__tests__/generated/cyclic.ts +0 -3058
  82. package/__tests__/generic.test.ts +0 -294
  83. package/__tests__/group.test.ts +0 -72
  84. package/__tests__/hkt.test.ts +0 -22
  85. package/__tests__/imports.test.ts +0 -113
  86. package/__tests__/instanceof.test.ts +0 -102
  87. package/__tests__/intersection.test.ts +0 -104
  88. package/__tests__/jsdoc.test.ts +0 -64
  89. package/__tests__/keyTraversal.test.ts +0 -73
  90. package/__tests__/keyof.test.ts +0 -134
  91. package/__tests__/keywords.test.ts +0 -188
  92. package/__tests__/literal.test.ts +0 -51
  93. package/__tests__/match.test.ts +0 -34
  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 -300
  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 -116
  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.map +0 -1
  125. package/out/main.js.map +0 -1
  126. package/out/match.d.ts.map +0 -1
  127. package/out/match.js.map +0 -1
  128. package/out/parser/definition.d.ts.map +0 -1
  129. package/out/parser/definition.js.map +0 -1
  130. package/out/parser/generic.d.ts.map +0 -1
  131. package/out/parser/generic.js.map +0 -1
  132. package/out/parser/objectLiteral.d.ts.map +0 -1
  133. package/out/parser/objectLiteral.js.map +0 -1
  134. package/out/parser/semantic/bounds.d.ts.map +0 -1
  135. package/out/parser/semantic/bounds.js.map +0 -1
  136. package/out/parser/semantic/divisor.d.ts.map +0 -1
  137. package/out/parser/semantic/divisor.js.map +0 -1
  138. package/out/parser/semantic/intersections.d.ts +0 -14
  139. package/out/parser/semantic/intersections.d.ts.map +0 -1
  140. package/out/parser/semantic/intersections.js +0 -2
  141. package/out/parser/semantic/intersections.js.map +0 -1
  142. package/out/parser/semantic/semantic.d.ts +0 -33
  143. package/out/parser/semantic/semantic.d.ts.map +0 -1
  144. package/out/parser/semantic/semantic.js +0 -2
  145. package/out/parser/semantic/semantic.js.map +0 -1
  146. package/out/parser/semantic/utils.d.ts.map +0 -1
  147. package/out/parser/semantic/utils.js.map +0 -1
  148. package/out/parser/semantic/validate.d.ts.map +0 -1
  149. package/out/parser/semantic/validate.js.map +0 -1
  150. package/out/parser/shared.d.ts +0 -54
  151. package/out/parser/shared.d.ts.map +0 -1
  152. package/out/parser/shared.js +0 -47
  153. package/out/parser/shared.js.map +0 -1
  154. package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
  155. package/out/parser/string/reduce/dynamic.js.map +0 -1
  156. package/out/parser/string/reduce/shared.d.ts.map +0 -1
  157. package/out/parser/string/reduce/shared.js.map +0 -1
  158. package/out/parser/string/reduce/static.d.ts.map +0 -1
  159. package/out/parser/string/reduce/static.js.map +0 -1
  160. package/out/parser/string/shift/operand/date.d.ts.map +0 -1
  161. package/out/parser/string/shift/operand/date.js.map +0 -1
  162. package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
  163. package/out/parser/string/shift/operand/enclosed.js.map +0 -1
  164. package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
  165. package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
  166. package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
  167. package/out/parser/string/shift/operand/operand.js.map +0 -1
  168. package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  169. package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
  170. package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
  171. package/out/parser/string/shift/operator/bounds.js.map +0 -1
  172. package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
  173. package/out/parser/string/shift/operator/divisor.js.map +0 -1
  174. package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
  175. package/out/parser/string/shift/operator/operator.js.map +0 -1
  176. package/out/parser/string/shift/scanner.d.ts.map +0 -1
  177. package/out/parser/string/shift/scanner.js.map +0 -1
  178. package/out/parser/string/string.d.ts.map +0 -1
  179. package/out/parser/string/string.js.map +0 -1
  180. package/out/parser/tuple.d.ts.map +0 -1
  181. package/out/parser/tuple.js.map +0 -1
  182. package/out/scope.d.ts.map +0 -1
  183. package/out/scope.js.map +0 -1
  184. package/out/test.d.ts +0 -2
  185. package/out/test.d.ts.map +0 -1
  186. package/out/test.js +0 -7
  187. package/out/test.js.map +0 -1
  188. package/out/tsconfig.build.tsbuildinfo +0 -1
  189. package/out/type.d.ts.map +0 -1
  190. package/out/type.js.map +0 -1
  191. package/out/util.d.ts +0 -19
  192. package/out/util.d.ts.map +0 -1
  193. package/out/util.js +0 -7
  194. package/out/util.js.map +0 -1
  195. package/parser/definition.ts +0 -159
  196. package/parser/generic.ts +0 -68
  197. package/parser/objectLiteral.ts +0 -197
  198. package/parser/semantic/bounds.ts +0 -61
  199. package/parser/semantic/divisor.ts +0 -13
  200. package/parser/semantic/intersections.ts +0 -46
  201. package/parser/semantic/semantic.ts +0 -144
  202. package/parser/semantic/utils.ts +0 -26
  203. package/parser/semantic/validate.ts +0 -109
  204. package/parser/shared.ts +0 -152
  205. package/parser/string/reduce/dynamic.ts +0 -196
  206. package/parser/string/reduce/shared.ts +0 -101
  207. package/parser/string/reduce/static.ts +0 -298
  208. package/parser/string/shift/operand/date.ts +0 -54
  209. package/parser/string/shift/operand/enclosed.ts +0 -118
  210. package/parser/string/shift/operand/genericArgs.ts +0 -127
  211. package/parser/string/shift/operand/operand.ts +0 -48
  212. package/parser/string/shift/operand/unenclosed.ts +0 -268
  213. package/parser/string/shift/operator/bounds.ts +0 -264
  214. package/parser/string/shift/operator/divisor.ts +0 -36
  215. package/parser/string/shift/operator/operator.ts +0 -82
  216. package/parser/string/shift/scanner.ts +0 -209
  217. package/parser/string/string.ts +0 -90
  218. package/parser/tuple.ts +0 -474
  219. package/scope.ts +0 -574
  220. package/type.ts +0 -347
  221. package/util.ts +0 -36
package/parser/tuple.ts DELETED
@@ -1,474 +0,0 @@
1
- import {
2
- keywords,
3
- schema,
4
- type BaseMeta,
5
- type Morph,
6
- type Out,
7
- type Predicate,
8
- type Schema,
9
- type TypeNode,
10
- type ValidatorKind,
11
- type extractIn,
12
- type extractOut,
13
- type inferMorphOut,
14
- type inferNarrow
15
- } from "@arktype/schema"
16
- import {
17
- isArray,
18
- objectKindOrDomainOf,
19
- printable,
20
- throwParseError,
21
- type BuiltinObjectKind,
22
- type Constructor,
23
- type Domain,
24
- type List,
25
- type conform,
26
- type evaluate,
27
- type isAny
28
- } from "@arktype/util"
29
- import type { ParseContext } from "../scope.js"
30
- import type { inferDefinition, validateDefinition } from "./definition.js"
31
- import type { inferIntersection } from "./semantic/intersections.js"
32
- import type { InfixOperator, PostfixExpression } from "./semantic/semantic.js"
33
- import { writeUnsatisfiableExpressionError } from "./semantic/validate.js"
34
- import {
35
- parseEntry,
36
- type EntryParseResult,
37
- type validateObjectValue
38
- } from "./shared.js"
39
- import { writeMissingRightOperandMessage } from "./string/shift/operand/unenclosed.js"
40
- import type { BaseCompletions } from "./string/string.js"
41
-
42
- export const parseTuple = (def: List, ctx: ParseContext) =>
43
- maybeParseTupleExpression(def, ctx) ?? parseTupleLiteral(def, ctx)
44
-
45
- export const parseTupleLiteral = (def: List, ctx: ParseContext): TypeNode => {
46
- const props: unknown[] = []
47
- let isVariadic = false
48
- for (let i = 0; i < def.length; i++) {
49
- let elementDef = def[i]
50
- ctx.path.push(`${i}`)
51
- if (typeof elementDef === "string" && elementDef.startsWith("...")) {
52
- elementDef = elementDef.slice(3)
53
- isVariadic = true
54
- } else if (
55
- isArray(elementDef) &&
56
- elementDef.length === 2 &&
57
- elementDef[0] === "..."
58
- ) {
59
- elementDef = elementDef[1]
60
- isVariadic = true
61
- }
62
- const parsedEntry = parseEntry([`${i}`, elementDef])
63
- const value = ctx.scope.parse(parsedEntry.innerValue, ctx)
64
- if (isVariadic) {
65
- if (!value.extends(keywords.Array)) {
66
- return throwParseError(writeNonArrayRestMessage(elementDef))
67
- }
68
- if (i !== def.length - 1) {
69
- return throwParseError(prematureRestMessage)
70
- }
71
- // TODO: Fix builtins.arrayIndexTypeNode()
72
- const elementType = value.getPath()
73
- // TODO: first variadic i
74
- props.push({ key: keywords.number, value: elementType })
75
- } else {
76
- props.push({
77
- key: {
78
- name: `${i}`,
79
- prerequisite: false,
80
- optional: parsedEntry.kind === "optional"
81
- },
82
- value
83
- })
84
- }
85
- ctx.path.pop()
86
- }
87
- if (!isVariadic) {
88
- props.push({
89
- key: {
90
- name: "length",
91
- prerequisite: true,
92
- optional: false
93
- },
94
- // , ctx
95
- value: schema({ unit: def.length })
96
- })
97
- }
98
- // props , ctx
99
- return schema(Array)
100
- }
101
-
102
- export const maybeParseTupleExpression = (
103
- def: List,
104
- ctx: ParseContext
105
- ): TypeNode | undefined => {
106
- const tupleExpressionResult = isIndexOneExpression(def)
107
- ? indexOneParsers[def[1]](def as never, ctx)
108
- : isIndexZeroExpression(def)
109
- ? prefixParsers[def[0]](def as never, ctx)
110
- : undefined
111
- if (tupleExpressionResult) {
112
- return tupleExpressionResult.isNever()
113
- ? throwParseError(
114
- writeUnsatisfiableExpressionError(
115
- def
116
- .map((def) => (typeof def === "string" ? def : printable(def)))
117
- .join(" ")
118
- )
119
- )
120
- : tupleExpressionResult
121
- }
122
- }
123
-
124
- // It is *extremely* important we use readonly any time we check a tuple against
125
- // something like this. Not doing so will always cause the check to fail, since
126
- // def is declared as a const parameter.
127
- type InfixExpression = readonly [unknown, InfixOperator, ...unknown[]]
128
-
129
- export type validateTuple<
130
- def extends List,
131
- $,
132
- args
133
- > = def extends IndexZeroExpression
134
- ? validatePrefixExpression<def, $, args>
135
- : def extends PostfixExpression
136
- ? validatePostfixExpression<def, $, args>
137
- : def extends InfixExpression
138
- ? validateInfixExpression<def, $, args>
139
- : def extends
140
- | readonly ["", ...unknown[]]
141
- | readonly [unknown, "", ...unknown[]]
142
- ? readonly [
143
- def[0] extends "" ? BaseCompletions<$, args, IndexZeroOperator> : def[0],
144
- def[1] extends "" ? BaseCompletions<$, args, IndexOneOperator> : def[1]
145
- ]
146
- : validateTupleLiteral<def, $, args>
147
-
148
- export type validateTupleLiteral<
149
- def extends List,
150
- $,
151
- args,
152
- result extends unknown[] = []
153
- > = def extends readonly [infer head, ...infer tail]
154
- ? validateTupleLiteral<
155
- tail,
156
- $,
157
- args,
158
- [...result, validateTupleElement<head, tail, $, args>]
159
- >
160
- : result
161
-
162
- type validateTupleElement<head, tail, $, args> =
163
- head extends variadicExpression<infer operand>
164
- ? validateDefinition<operand, $, args> extends infer syntacticResult
165
- ? syntacticResult extends operand
166
- ? semanticallyValidateRestElement<
167
- operand,
168
- $,
169
- args
170
- > extends infer semanticResult
171
- ? semanticResult extends operand
172
- ? tail extends []
173
- ? head
174
- : prematureRestMessage
175
- : semanticResult
176
- : never
177
- : syntacticResult
178
- : never
179
- : validateObjectValue<head, $, args>
180
-
181
- type semanticallyValidateRestElement<operand, $, args> = inferDefinition<
182
- operand,
183
- $,
184
- args
185
- > extends infer result
186
- ? result extends never
187
- ? writeNonArrayRestMessage<operand>
188
- : isAny<result> extends true
189
- ? writeNonArrayRestMessage<operand>
190
- : result extends readonly unknown[]
191
- ? operand
192
- : writeNonArrayRestMessage<operand>
193
- : never
194
-
195
- export const writeNonArrayRestMessage = <operand>(operand: operand) =>
196
- `Rest element ${
197
- typeof operand === "string" ? `'${operand}'` : ""
198
- } must be an array` as writeNonArrayRestMessage<operand>
199
-
200
- type writeNonArrayRestMessage<operand> = `Rest element ${operand extends string
201
- ? `'${operand}'`
202
- : ""} must be an array`
203
-
204
- export const prematureRestMessage = `Rest elements are only allowed at the end of a tuple`
205
-
206
- type prematureRestMessage = typeof prematureRestMessage
207
-
208
- type inferTupleLiteral<
209
- def extends List,
210
- $,
211
- args,
212
- result extends unknown[] = []
213
- > = def extends readonly [infer head, ...infer tail]
214
- ? parseEntry<
215
- result["length"],
216
- head extends variadicExpression<infer operand> ? operand : head
217
- > extends infer entryParseResult extends EntryParseResult
218
- ? inferDefinition<
219
- entryParseResult["innerValue"],
220
- $,
221
- args
222
- > extends infer element
223
- ? head extends variadicExpression
224
- ? element extends readonly unknown[]
225
- ? inferTupleLiteral<tail, $, args, [...result, ...element]>
226
- : never
227
- : inferTupleLiteral<
228
- tail,
229
- $,
230
- args,
231
- entryParseResult["kind"] extends "optional"
232
- ? [...result, element?]
233
- : [...result, element]
234
- >
235
- : never
236
- : never
237
- : result
238
-
239
- type variadicExpression<operandDef = unknown> =
240
- | variadicStringExpression<operandDef & string>
241
- | variadicTupleExpression<operandDef>
242
-
243
- type variadicStringExpression<operandDef extends string = string> =
244
- `...${operandDef}`
245
-
246
- type variadicTupleExpression<operandDef = unknown> = readonly [
247
- "...",
248
- operandDef
249
- ]
250
-
251
- export type inferTuple<def extends List, $, args> = def extends TupleExpression
252
- ? inferTupleExpression<def, $, args>
253
- : inferTupleLiteral<def, $, args>
254
-
255
- export type inferTupleExpression<
256
- def extends TupleExpression,
257
- $,
258
- args
259
- > = def[1] extends "[]"
260
- ? inferDefinition<def[0], $, args>[]
261
- : def[1] extends "&"
262
- ? inferIntersection<
263
- inferDefinition<def[0], $, args>,
264
- inferDefinition<def[2], $, args>
265
- >
266
- : def[1] extends "|"
267
- ? inferDefinition<def[0], $, args> | inferDefinition<def[2], $, args>
268
- : def[1] extends ":"
269
- ? inferNarrow<inferDefinition<def[0], $, args>, def[2]>
270
- : def[1] extends "=>"
271
- ? parseMorph<def[0], def[2], $, args>
272
- : def[1] extends "@"
273
- ? inferDefinition<def[0], $, args>
274
- : def extends readonly ["===", ...infer values]
275
- ? values[number]
276
- : def extends readonly [
277
- "instanceof",
278
- ...infer constructors extends Constructor[]
279
- ]
280
- ? InstanceType<constructors[number]>
281
- : def[0] extends "keyof"
282
- ? inferKeyOfExpression<def[1], $, args>
283
- : never
284
-
285
- export type validatePrefixExpression<
286
- def extends IndexZeroExpression,
287
- $,
288
- args
289
- > = def["length"] extends 1
290
- ? readonly [writeMissingRightOperandMessage<def[0]>]
291
- : def[0] extends "keyof"
292
- ? readonly [def[0], validateDefinition<def[1], $, args>]
293
- : def[0] extends "==="
294
- ? readonly [def[0], ...unknown[]]
295
- : def[0] extends "instanceof"
296
- ? readonly [def[0], ...Constructor[]]
297
- : never
298
-
299
- export type validatePostfixExpression<
300
- def extends PostfixExpression,
301
- $,
302
- args
303
- // conform here is needed to preserve completions for shallow tuple
304
- // expressions at index 1 after TS 5.1
305
- > = conform<def, readonly [validateDefinition<def[0], $, args>, "[]"]>
306
-
307
- export type validateInfixExpression<
308
- def extends InfixExpression,
309
- $,
310
- args
311
- > = def["length"] extends 2
312
- ? readonly [def[0], writeMissingRightOperandMessage<def[1]>]
313
- : readonly [
314
- validateDefinition<def[0], $, args>,
315
- def[1],
316
- def[1] extends "|"
317
- ? validateDefinition<def[2], $, args>
318
- : def[1] extends "&"
319
- ? validateDefinition<def[2], $, args>
320
- : def[1] extends ":"
321
- ? Predicate<extractIn<inferDefinition<def[0], $, args>>>
322
- : def[1] extends "=>"
323
- ? Morph<extractOut<inferDefinition<def[0], $, args>>, unknown>
324
- : def[1] extends "@"
325
- ? BaseMeta | string
326
- : validateDefinition<def[2], $, args>
327
- ]
328
-
329
- export type UnparsedTupleExpressionInput = {
330
- instanceof: Constructor
331
- "===": unknown
332
- }
333
-
334
- export type UnparsedTupleOperator = evaluate<keyof UnparsedTupleExpressionInput>
335
-
336
- export const parseKeyOfTuple: PrefixParser<"keyof"> = (def, ctx) =>
337
- ctx.scope.parse(def[1], ctx).keyof()
338
-
339
- export type inferKeyOfExpression<operandDef, $, args> = evaluate<
340
- keyof inferDefinition<operandDef, $, args>
341
- >
342
-
343
- const parseBranchTuple: PostfixParser<"|" | "&"> = (def, ctx) => {
344
- if (def[2] === undefined) {
345
- return throwParseError(writeMissingRightOperandMessage(def[1], ""))
346
- }
347
- const l = ctx.scope.parse(def[0], ctx)
348
- const r = ctx.scope.parse(def[2], ctx)
349
- return def[1] === "&" ? l.and(r) : l.or(r)
350
- }
351
-
352
- const parseArrayTuple: PostfixParser<"[]"> = (def, ctx) =>
353
- ctx.scope.parse(def[0], ctx).array()
354
-
355
- export type PostfixParser<token extends IndexOneOperator> = (
356
- def: IndexOneExpression<token>,
357
- ctx: ParseContext
358
- ) => TypeNode
359
-
360
- export type PrefixParser<token extends IndexZeroOperator> = (
361
- def: IndexZeroExpression<token>,
362
- ctx: ParseContext
363
- ) => TypeNode
364
-
365
- export type TupleExpression = IndexZeroExpression | IndexOneExpression
366
-
367
- export type TupleExpressionOperator = IndexZeroOperator | IndexOneOperator
368
-
369
- export type IndexOneOperator = TuplePostfixOperator | TupleInfixOperator
370
-
371
- export type TuplePostfixOperator = "[]"
372
-
373
- export type TupleInfixOperator = "&" | "|" | "=>" | ":" | "@"
374
-
375
- export type IndexOneExpression<
376
- token extends IndexOneOperator = IndexOneOperator
377
- > = readonly [unknown, token, ...unknown[]]
378
-
379
- const isIndexOneExpression = (def: List): def is IndexOneExpression =>
380
- indexOneParsers[def[1] as IndexOneOperator] !== undefined
381
-
382
- export const parseMorphTuple: PostfixParser<"=>"> = (def, ctx) => {
383
- if (typeof def[2] !== "function") {
384
- return throwParseError(
385
- writeMalformedFunctionalExpressionMessage("=>", def[2])
386
- )
387
- }
388
- // TODO: nested morphs?
389
- return schema({
390
- in: ctx.scope.parse(def[0], ctx) as Schema<ValidatorKind>,
391
- morph: def[2] as Morph
392
- })
393
- }
394
-
395
- export const writeMalformedFunctionalExpressionMessage = (
396
- operator: FunctionalTupleOperator,
397
- value: unknown
398
- ) =>
399
- `${
400
- operator === ":" ? "Narrow" : "Morph"
401
- } expression requires a function following '${operator}' (was ${typeof value})`
402
-
403
- export type parseMorph<inDef, morph, $, args> = morph extends Morph
404
- ? (
405
- In: extractIn<inferDefinition<inDef, $, args>>
406
- ) => Out<inferMorphOut<ReturnType<morph>>>
407
- : never
408
-
409
- export const parseNarrowTuple: PostfixParser<":"> = (def, ctx) => {
410
- if (typeof def[2] !== "function") {
411
- return throwParseError(
412
- writeMalformedFunctionalExpressionMessage(":", def[2])
413
- )
414
- }
415
- return ctx.scope
416
- .parse(def[0], ctx)
417
- .constrain("predicate", def[2] as Predicate)
418
- }
419
-
420
- const parseAttributeTuple: PostfixParser<"@"> = (def, ctx) => {
421
- return ctx.scope.parse(def[0], ctx)
422
- }
423
-
424
- const indexOneParsers: {
425
- [token in IndexOneOperator]: PostfixParser<token>
426
- } = {
427
- "|": parseBranchTuple,
428
- "&": parseBranchTuple,
429
- "[]": parseArrayTuple,
430
- ":": parseNarrowTuple,
431
- "=>": parseMorphTuple,
432
- "@": parseAttributeTuple
433
- }
434
-
435
- export type FunctionalTupleOperator = ":" | "=>"
436
-
437
- export type IndexZeroOperator = "keyof" | "instanceof" | "==="
438
-
439
- export type IndexZeroExpression<
440
- token extends IndexZeroOperator = IndexZeroOperator
441
- > = readonly [token, ...unknown[]]
442
-
443
- const prefixParsers: {
444
- [token in IndexZeroOperator]: PrefixParser<token>
445
- } = {
446
- keyof: parseKeyOfTuple,
447
- instanceof: (def) => {
448
- if (typeof def[1] !== "function") {
449
- return throwParseError(
450
- writeInvalidConstructorMessage(objectKindOrDomainOf(def[1]))
451
- )
452
- }
453
- const branches = def
454
- .slice(1)
455
- .map((ctor) =>
456
- typeof ctor === "function"
457
- ? { proto: ctor as Constructor }
458
- : throwParseError(
459
- writeInvalidConstructorMessage(objectKindOrDomainOf(ctor))
460
- )
461
- )
462
- return schema(...branches)
463
- },
464
- "===": (def) => schema.units(...def.slice(1))
465
- }
466
-
467
- const isIndexZeroExpression = (def: List): def is IndexZeroExpression =>
468
- prefixParsers[def[0] as IndexZeroOperator] !== undefined
469
-
470
- export const writeInvalidConstructorMessage = <
471
- actual extends Domain | BuiltinObjectKind
472
- >(
473
- actual: actual
474
- ) => `Expected a constructor following 'instanceof' operator (was ${actual})`