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
@@ -1,26 +0,0 @@
1
- import type { List, Stringifiable } from "@arktype/util"
2
- import type { Comparator } from "../string/reduce/shared.js"
3
- import type { InfixExpression, PostfixExpression } from "./semantic.js"
4
-
5
- export type astToString<ast> = `'${astToStringRecurse<ast>}'`
6
-
7
- type astToStringRecurse<ast> = ast extends PostfixExpression<
8
- infer operator,
9
- infer operand
10
- >
11
- ? operator extends "[]"
12
- ? `${groupAst<operand>}[]`
13
- : never
14
- : ast extends InfixExpression<infer operator, infer l, infer r>
15
- ? operator extends "&" | "|" | "%" | Comparator
16
- ? `${groupAst<l>}${operator}${groupAst<r>}`
17
- : never
18
- : ast extends Stringifiable
19
- ? `${ast extends bigint ? `${ast}n` : ast}`
20
- : "..."
21
-
22
- type groupAst<ast> = ast extends List
23
- ? ast[1] extends "[]"
24
- ? astToStringRecurse<ast>
25
- : `(${astToStringRecurse<ast>})`
26
- : astToStringRecurse<ast>
@@ -1,109 +0,0 @@
1
- import type { Refinements } from "@arktype/schema"
2
- import type {
3
- BigintLiteral,
4
- Completion,
5
- ErrorMessage,
6
- NumberLiteral,
7
- writeMalformedNumericLiteralMessage
8
- } from "@arktype/util"
9
- import type { Module } from "../../scope.js"
10
- import type { GenericProps } from "../../type.js"
11
- import type { Comparator } from "../string/reduce/shared.js"
12
- import type { writeInvalidGenericArgsMessage } from "../string/shift/operand/genericArgs.js"
13
- import type { writeMissingSubmoduleAccessMessage } from "../string/shift/operand/unenclosed.js"
14
- import type { parseString } from "../string/string.js"
15
- import type { validateRange } from "./bounds.js"
16
- import type { validateDivisor } from "./divisor.js"
17
- import type {
18
- GenericInstantiationAst,
19
- InfixExpression,
20
- PostfixExpression
21
- } from "./semantic.js"
22
- import type { astToString } from "./utils.js"
23
-
24
- export type validateAst<ast, $, args> = ast extends string
25
- ? validateStringAst<ast, $>
26
- : ast extends PostfixExpression<infer operator, infer operand>
27
- ? operator extends "[]"
28
- ? validateAst<operand, $, args>
29
- : never
30
- : ast extends InfixExpression<infer operator, infer l, infer r>
31
- ? operator extends "&" | "|"
32
- ? validateInfix<ast, $, args>
33
- : operator extends Comparator
34
- ? validateRange<l, operator, r, $, args>
35
- : operator extends "%"
36
- ? validateDivisor<l, $, args>
37
- : undefined
38
- : ast extends readonly ["keyof", infer operand]
39
- ? validateAst<operand, $, args>
40
- : ast extends GenericInstantiationAst
41
- ? validateGenericArgs<ast["2"], $, args>
42
- : ErrorMessage<writeUnexpectedExpressionMessage<astToString<ast>>>
43
-
44
- type writeUnexpectedExpressionMessage<expression extends string> =
45
- `Unexpectedly failed to parse the expression resulting from ${expression}`
46
-
47
- type validateGenericArgs<argAsts extends unknown[], $, args> = argAsts extends [
48
- infer head,
49
- ...infer tail
50
- ]
51
- ? validateAst<head, $, args> extends ErrorMessage<infer message>
52
- ? ErrorMessage<message>
53
- : validateGenericArgs<tail, $, args>
54
- : undefined
55
-
56
- export const writeUnsatisfiableExpressionError = <expression extends string>(
57
- expression: expression
58
- ): writeUnsatisfiableExpressionError<expression> =>
59
- `${expression} results in an unsatisfiable type`
60
-
61
- export type writeUnsatisfiableExpressionError<expression extends string> =
62
- `${expression} results in an unsatisfiable type`
63
-
64
- type validateStringAst<def extends string, $> = def extends NumberLiteral<
65
- infer value
66
- >
67
- ? number extends value
68
- ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "number">>
69
- : undefined
70
- : def extends BigintLiteral<infer value>
71
- ? bigint extends value
72
- ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "bigint">>
73
- : undefined
74
- : def extends keyof $
75
- ? $[def] extends null
76
- ? // handle any/never
77
- def
78
- : // these problems would've been caught during a fullStringParse, but it's most
79
- // efficient to check for them here in case the string was naively parsed
80
- $[def] extends GenericProps
81
- ? ErrorMessage<
82
- writeInvalidGenericArgsMessage<def, $[def]["parameters"], []>
83
- >
84
- : $[def] extends Module
85
- ? ErrorMessage<writeMissingSubmoduleAccessMessage<def>>
86
- : undefined
87
- : def extends ErrorMessage
88
- ? def
89
- : undefined
90
-
91
- export type validateString<def extends string, $, args> = validateAst<
92
- parseString<def, $, args>,
93
- $,
94
- args
95
- > extends infer result extends ErrorMessage
96
- ? result extends Completion<infer text>
97
- ? text
98
- : result
99
- : def
100
-
101
- type validateInfix<ast extends InfixExpression, $, args> = validateAst<
102
- ast[0],
103
- $,
104
- args
105
- > extends ErrorMessage<infer message>
106
- ? ErrorMessage<message>
107
- : validateAst<ast[2], $, args> extends ErrorMessage<infer message>
108
- ? ErrorMessage<message>
109
- : undefined
package/parser/shared.ts DELETED
@@ -1,167 +0,0 @@
1
- import type { BaseMeta, Node } from "@arktype/schema"
2
- import type { evaluate } from "@arktype/util"
3
- import type { validateDefinition } from "./definition.js"
4
- import { Scanner } from "./string/shift/scanner.js"
5
-
6
- type parsedKey<result extends KeyParseResult> = result
7
-
8
- type OptionalTuple<value> = readonly [value, "?"]
9
-
10
- type KeyParseResult<kind extends ParsedKeyKind = ParsedKeyKind> = {
11
- kind: kind
12
- innerKey: string | symbol
13
- }
14
-
15
- type ValueParseResult<kind extends ParsedValueKind = ParsedValueKind> = {
16
- kind: kind
17
- innerValue: unknown
18
- }
19
-
20
- export type EntryParseResult<kind extends ParsedKeyKind = ParsedKeyKind> =
21
- evaluate<
22
- KeyParseResult<kind> & {
23
- innerValue: unknown
24
- }
25
- >
26
-
27
- type ParsedValueKind = "required" | "optional"
28
-
29
- type ParsedKeyKind = "required" | "optional" | "indexed" | "spread"
30
-
31
- type parsedEntry<result extends EntryParseResult> = result
32
-
33
- export type OptionalValue<value> =
34
- | OptionalStringDefinition<value & string>
35
- | OptionalTuple<value>
36
-
37
- export type OptionalStringDefinition<name extends string = string> = `${name}?`
38
-
39
- export type IndexedKey<def extends string = string> = `[${def}]`
40
-
41
- export type validateObjectValue<def, $, args> = def extends OptionalTuple<
42
- infer value
43
- >
44
- ? readonly [validateObjectValueString<value, $, args>, "?"]
45
- : validateObjectValueString<def, $, args>
46
-
47
- type validateObjectValueString<def, $, args> =
48
- def extends OptionalStringDefinition<infer innerValue>
49
- ? `${validateDefinition<innerValue, $, args>}?`
50
- : validateDefinition<def, $, args>
51
-
52
- type DefinitionEntry = readonly [string | symbol, unknown]
53
-
54
- const getInnerValue = (value: unknown): ValueParseResult => {
55
- if (typeof value === "string") {
56
- if (value[value.length - 1] === "?") {
57
- return {
58
- kind: "optional",
59
- innerValue: value.slice(0, -1)
60
- }
61
- }
62
- } else if (Array.isArray(value)) {
63
- if (value.length === 2 && value[1] === "?") {
64
- return {
65
- kind: "optional",
66
- innerValue: getInnerValue(value[0]).innerValue
67
- }
68
- }
69
- }
70
- return {
71
- kind: "required",
72
- innerValue: value
73
- }
74
- }
75
-
76
- // these methods are also used in tuple parsing, however the keys of a tuple will always be 0, 1,
77
- // 2, etc and never be `...`, meaning `"spread"` as a kind will never occur in tuples
78
-
79
- export const parseEntry = ([key, value]: DefinitionEntry): EntryParseResult => {
80
- const keyParseResult: KeyParseResult =
81
- typeof key === "string" && key.at(-1) === "?"
82
- ? key.at(-2) === Scanner.escapeToken
83
- ? { innerKey: `${key.slice(0, -2)}?`, kind: "required" }
84
- : { innerKey: key.slice(0, -1), kind: "optional" }
85
- : key === "..."
86
- ? { innerKey: "...", kind: "spread" }
87
- : key === "\\..."
88
- ? { innerKey: "...", kind: "required" }
89
- : { innerKey: key, kind: "required" }
90
- const valueParseResult = getInnerValue(value)
91
- return {
92
- innerKey: keyParseResult.innerKey,
93
- innerValue: valueParseResult.innerValue,
94
- kind:
95
- keyParseResult.kind === "indexed"
96
- ? "indexed"
97
- : valueParseResult.kind === "optional"
98
- ? "optional"
99
- : keyParseResult.kind
100
- }
101
- }
102
-
103
- export type parseEntry<
104
- keyDef extends PropertyKey,
105
- valueDef
106
- > = parseKey<keyDef> extends infer keyParseResult extends KeyParseResult
107
- ? valueDef extends OptionalValue<infer innerValue>
108
- ? parsedEntry<{
109
- kind: "optional"
110
- innerKey: keyParseResult["innerKey"]
111
- innerValue: innerValue extends OptionalStringDefinition<
112
- infer innerStringValue
113
- >
114
- ? innerStringValue
115
- : innerValue
116
- }>
117
- : parsedEntry<{
118
- kind: keyParseResult["kind"]
119
- innerKey: keyParseResult["innerKey"]
120
- innerValue: valueDef
121
- }>
122
- : never
123
-
124
- type parseKey<k> = k extends OptionalStringDefinition<infer inner>
125
- ? inner extends `${infer baseName}${Scanner.EscapeToken}`
126
- ? parsedKey<{
127
- kind: "required"
128
- innerKey: OptionalStringDefinition<baseName>
129
- }>
130
- : parsedKey<{
131
- kind: "optional"
132
- innerKey: inner
133
- }>
134
- : k extends "..."
135
- ? parsedKey<{ kind: "spread"; innerKey: "..." }>
136
- : k extends "\\..."
137
- ? parsedKey<{ kind: "required"; innerKey: "..." }>
138
- : k extends IndexedKey<infer def>
139
- ? parsedKey<{
140
- kind: "indexed"
141
- innerKey: def
142
- }>
143
- : k extends `${Scanner.EscapeToken}${infer escapedIndexKey extends
144
- IndexedKey}`
145
- ? parsedKey<{
146
- kind: "required"
147
- innerKey: escapedIndexKey
148
- }>
149
- : parsedKey<{
150
- kind: "required"
151
- innerKey: k & (string | symbol)
152
- }>
153
-
154
- export const configureShallowDescendants = <node extends Node>(
155
- node: node,
156
- configOrDescription: BaseMeta | string
157
- ): node => {
158
- const config: BaseMeta =
159
- typeof configOrDescription === "string"
160
- ? { description: configOrDescription }
161
- : (configOrDescription as never)
162
- return node.transform(
163
- (kind, inner) => ({ ...inner, ...config }),
164
- // TODO: change to props
165
- (node) => node.kind !== "required" && node.kind !== "optional"
166
- ) as never
167
- }
@@ -1,196 +0,0 @@
1
- import type { TypeNode } from "@arktype/schema"
2
- import {
3
- isKeyOf,
4
- throwInternalError,
5
- throwParseError,
6
- type requireKeys
7
- } from "@arktype/util"
8
- import type { ParseContext } from "../../../scope.js"
9
- import { parseOperand } from "../shift/operand/operand.js"
10
- import { parseOperator } from "../shift/operator/operator.js"
11
- import { Scanner } from "../shift/scanner.js"
12
- import { parseUntilFinalizer } from "../string.js"
13
- import {
14
- invertedComparators,
15
- minComparators,
16
- writeMultipleLeftBoundsMessage,
17
- writeOpenRangeMessage,
18
- writeUnclosedGroupMessage,
19
- writeUnmatchedGroupCloseMessage,
20
- writeUnpairableComparatorMessage,
21
- type Comparator,
22
- type LimitLiteral,
23
- type OpenLeftBound,
24
- type StringifiablePrefixOperator
25
- } from "./shared.js"
26
-
27
- type BranchState = {
28
- prefixes: StringifiablePrefixOperator[]
29
- leftBound?: OpenLeftBound
30
- "&"?: TypeNode
31
- "|"?: TypeNode
32
- }
33
-
34
- export type DynamicStateWithRoot = requireKeys<DynamicState, "root">
35
-
36
- export class DynamicState {
37
- readonly scanner: Scanner
38
- root: TypeNode | undefined
39
- branches: BranchState = {
40
- prefixes: []
41
- }
42
- finalizer: Scanner.FinalizingLookahead | undefined
43
- groups: BranchState[] = []
44
-
45
- constructor(
46
- def: string,
47
- public readonly ctx: ParseContext
48
- ) {
49
- this.scanner = new Scanner(def)
50
- }
51
-
52
- error(message: string) {
53
- return throwParseError(message)
54
- }
55
-
56
- hasRoot(): this is DynamicStateWithRoot {
57
- return this.root !== undefined
58
- }
59
-
60
- unsetRoot(): this["root"] {
61
- const value = this.root
62
- this.root = undefined
63
- return value
64
- }
65
-
66
- constrainRoot(...args: Parameters<TypeNode["constrain"]>) {
67
- this.root = this.root!.constrain(...args)
68
- }
69
-
70
- setRoot(root: TypeNode) {
71
- this.root = root
72
- }
73
-
74
- finalize(finalizer: Scanner.FinalizingLookahead) {
75
- if (this.groups.length) {
76
- return this.error(writeUnclosedGroupMessage(")"))
77
- }
78
- this.finalizeBranches()
79
- this.finalizer = finalizer
80
- }
81
-
82
- reduceLeftBound(limit: LimitLiteral, comparator: Comparator) {
83
- const invertedComparator = invertedComparators[comparator]
84
- if (!isKeyOf(invertedComparator, minComparators)) {
85
- return this.error(writeUnpairableComparatorMessage(comparator))
86
- }
87
- if (this.branches.leftBound) {
88
- return this.error(
89
- writeMultipleLeftBoundsMessage(
90
- this.branches.leftBound.limit,
91
- this.branches.leftBound.comparator,
92
- limit,
93
- invertedComparator
94
- )
95
- )
96
- }
97
- this.branches.leftBound = {
98
- comparator: invertedComparator,
99
- limit
100
- }
101
- }
102
-
103
- finalizeBranches() {
104
- this.assertRangeUnset()
105
- if (this.branches["|"]) {
106
- this.pushRootToBranch("|")
107
- this.root = this.branches["|"]
108
- } else if (this.branches["&"]) {
109
- this.pushRootToBranch("&")
110
- this.root = this.branches["&"]
111
- } else {
112
- this.applyPrefixes()
113
- }
114
- }
115
-
116
- finalizeGroup() {
117
- this.finalizeBranches()
118
- const topBranchState = this.groups.pop()
119
- if (!topBranchState) {
120
- return this.error(writeUnmatchedGroupCloseMessage(this.scanner.unscanned))
121
- }
122
- this.branches = topBranchState
123
- }
124
-
125
- addPrefix(prefix: StringifiablePrefixOperator) {
126
- this.branches.prefixes.push(prefix)
127
- }
128
-
129
- applyPrefixes() {
130
- while (this.branches.prefixes.length) {
131
- const lastPrefix = this.branches.prefixes.pop()!
132
- this.root =
133
- lastPrefix === "keyof"
134
- ? this.root!.keyof()
135
- : throwInternalError(`Unexpected prefix '${lastPrefix}'`)
136
- }
137
- }
138
-
139
- pushRootToBranch(token: "|" | "&") {
140
- this.assertRangeUnset()
141
- this.applyPrefixes()
142
- const root = this.root!
143
- this.branches["&"] = this.branches["&"]?.and(root) ?? root
144
- if (token === "|") {
145
- this.branches["|"] =
146
- this.branches["|"]?.or(this.branches["&"]) ?? this.branches["&"]
147
- delete this.branches["&"]
148
- }
149
- this.root = undefined
150
- }
151
-
152
- parseUntilFinalizer() {
153
- return parseUntilFinalizer(
154
- new DynamicState(this.scanner.unscanned, this.ctx)
155
- )
156
- }
157
-
158
- parseOperator(this: DynamicStateWithRoot) {
159
- return parseOperator(this)
160
- }
161
-
162
- parseOperand() {
163
- return parseOperand(this)
164
- }
165
-
166
- private assertRangeUnset() {
167
- if (this.branches.leftBound) {
168
- return this.error(
169
- writeOpenRangeMessage(
170
- this.branches.leftBound.limit,
171
- this.branches.leftBound.comparator
172
- )
173
- )
174
- }
175
- }
176
-
177
- reduceGroupOpen() {
178
- this.groups.push(this.branches)
179
- this.branches = {
180
- prefixes: []
181
- }
182
- }
183
-
184
- previousOperator() {
185
- return (
186
- this.branches.leftBound?.comparator ??
187
- this.branches.prefixes.at(-1) ??
188
- (this.branches["&"] ? "&" : this.branches["|"] ? "|" : undefined)
189
- )
190
- }
191
-
192
- shiftedByOne() {
193
- this.scanner.shift()
194
- return this
195
- }
196
- }
@@ -1,101 +0,0 @@
1
- import type { DateLiteral } from "@arktype/schema"
2
-
3
- export type StringifiablePrefixOperator = "keyof"
4
-
5
- export const minComparators = {
6
- ">": true,
7
- ">=": true
8
- } as const
9
-
10
- export type MinComparator = keyof typeof minComparators
11
-
12
- export const maxComparators = {
13
- "<": true,
14
- "<=": true
15
- } as const
16
-
17
- export type MaxComparator = keyof typeof maxComparators
18
-
19
- export const comparators = {
20
- ...minComparators,
21
- ...maxComparators,
22
- "==": true
23
- }
24
-
25
- export type Comparator = keyof typeof comparators
26
-
27
- export const invertedComparators = {
28
- "<": ">",
29
- ">": "<",
30
- "<=": ">=",
31
- ">=": "<=",
32
- "==": "=="
33
- } as const satisfies Record<Comparator, Comparator>
34
-
35
- export type InvertedComparators = typeof invertedComparators
36
-
37
- export type LimitLiteral = number | DateLiteral
38
-
39
- export type OpenLeftBound = { limit: LimitLiteral; comparator: MinComparator }
40
-
41
- export const writeUnmatchedGroupCloseMessage = <unscanned extends string>(
42
- unscanned: unscanned
43
- ): writeUnmatchedGroupCloseMessage<unscanned> =>
44
- `Unmatched )${(unscanned === "" ? "" : ` before ${unscanned}`) as any}`
45
-
46
- export type writeUnmatchedGroupCloseMessage<unscanned extends string> =
47
- `Unmatched )${unscanned extends "" ? "" : ` before ${unscanned}`}`
48
-
49
- export const writeUnclosedGroupMessage = <missingChar extends string>(
50
- missingChar: missingChar
51
- ): writeUnclosedGroupMessage<missingChar> => `Missing ${missingChar}`
52
-
53
- export type writeUnclosedGroupMessage<missingChar extends string> =
54
- `Missing ${missingChar}`
55
-
56
- export const writeOpenRangeMessage = <
57
- min extends LimitLiteral,
58
- comparator extends MinComparator
59
- >(
60
- min: min,
61
- comparator: comparator
62
- ): writeOpenRangeMessage<min, comparator> =>
63
- `Left bounds are only valid when paired with right bounds (try ...${comparator}${min})`
64
-
65
- export type writeOpenRangeMessage<
66
- min extends LimitLiteral,
67
- comparator extends MinComparator
68
- > = `Left bounds are only valid when paired with right bounds (try ...${comparator}${min})`
69
-
70
- export type writeUnpairableComparatorMessage<comparator extends Comparator> =
71
- `Left-bounded expressions must specify their limits using < or <= (was ${comparator})`
72
-
73
- export const writeUnpairableComparatorMessage = <comparator extends Comparator>(
74
- comparator: comparator
75
- ): writeUnpairableComparatorMessage<comparator> =>
76
- `Left-bounded expressions must specify their limits using < or <= (was ${comparator})`
77
-
78
- export const writeMultipleLeftBoundsMessage = <
79
- openLimit extends LimitLiteral,
80
- openComparator extends MinComparator,
81
- limit extends LimitLiteral,
82
- comparator extends MinComparator
83
- >(
84
- openLimit: openLimit,
85
- openComparator: openComparator,
86
- limit: limit,
87
- comparator: comparator
88
- ): writeMultipleLeftBoundsMessage<
89
- openLimit,
90
- openComparator,
91
- limit,
92
- comparator
93
- > =>
94
- `An expression may have at most one left bound (parsed ${openLimit}${invertedComparators[openComparator]}, ${limit}${invertedComparators[comparator]})`
95
-
96
- export type writeMultipleLeftBoundsMessage<
97
- openLimit extends LimitLiteral,
98
- openComparator extends MinComparator,
99
- limit extends LimitLiteral,
100
- comparator extends MinComparator
101
- > = `An expression may have at most one left bound (parsed ${openLimit}${InvertedComparators[openComparator]}, ${limit}${InvertedComparators[comparator]})`