arktype 2.0.0-dev.2 → 2.0.0-dev.21

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