arktype 1.1.0-dev-2 → 2.0.0-beta.1

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 +19 -0
  3. package/out/ark.js +9 -0
  4. package/out/config.d.ts +1 -0
  5. package/out/config.js +2 -0
  6. package/out/generic.d.ts +65 -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 +14 -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 +27 -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 +86 -0
  61. package/out/scope.js +132 -0
  62. package/out/type.d.ts +83 -0
  63. package/out/type.js +36 -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/match.ts DELETED
@@ -1,137 +0,0 @@
1
- import type { Morph } from "@arktype/schema"
2
- import type {
3
- conform,
4
- entryOf,
5
- ErrorMessage,
6
- evaluate,
7
- Fn,
8
- isDisjoint,
9
- join,
10
- paramsOf,
11
- replaceKey,
12
- returnOf,
13
- unionToTuple,
14
- valueOf
15
- } from "@arktype/util"
16
- import type { Scope } from "./scope.js"
17
- import { Type, type inferTypeRoot, type validateTypeRoot } from "./type.js"
18
-
19
- type cedille = "¸"
20
-
21
- type serializedWhentry<
22
- k extends string,
23
- v extends string
24
- > = `[${k}${cedille}${v}]`
25
-
26
- type parseWhentryKey<
27
- s extends string,
28
- $,
29
- result = {}
30
- > = s extends `${serializedWhentry<infer k, infer v>}${cedille}${infer tail}`
31
- ? validateTypeRoot<v, $> extends ErrorMessage<infer message>
32
- ? ErrorMessage<message>
33
- : parseWhentryKey<tail, $, result & { [_ in k]: inferTypeRoot<v, $> }>
34
- : s extends serializedWhentry<infer k, infer v>
35
- ? validateTypeRoot<v, $> extends ErrorMessage<infer message>
36
- ? ErrorMessage<message>
37
- : evaluate<result & { [_ in k]: inferTypeRoot<v, $> }>
38
- : validateTypeRoot<s, $> extends ErrorMessage<infer message>
39
- ? ErrorMessage<message>
40
- : never
41
-
42
- export type WhenParser<$> = <const def>(
43
- def: validateTypeRoot<def, $>
44
- ) => join<
45
- unionToTuple<`[${join<conform<entryOf<def>, [string, string]>, cedille>}]`>,
46
- cedille
47
- >
48
-
49
- export const createWhenParser = <$>(scope: Scope): WhenParser<$> => {
50
- const parser = (def: unknown) => new Type(def, scope).alias
51
- return parser as never
52
- }
53
-
54
- type MatchContext = {
55
- inConstraint: unknown
56
- outConstraint: unknown
57
- thens: readonly Fn[]
58
- $: unknown
59
- }
60
-
61
- type validateCases<cases, ctx extends MatchContext> = {
62
- // adding keyof $ explicitly provides key completions for aliases
63
- [k in keyof cases | keyof ctx["$"]]?: k extends validateTypeRoot<k, ctx["$"]>
64
- ? (
65
- In: ctx["inConstraint"] & inferTypeRoot<k, ctx["$"]>
66
- ) => ctx["outConstraint"]
67
- : parseWhentryKey<k & string, ctx["$"]> extends ErrorMessage<infer message>
68
- ? ErrorMessage<message>
69
- : (In: parseWhentryKey<k & string, ctx["$"]>) => ctx["outConstraint"]
70
- }
71
-
72
- export type MatchParser<$> = {
73
- <In = unknown, Out = unknown>(): ChainableMatchParser<{
74
- inConstraint: In
75
- outConstraint: Out
76
- thens: []
77
- $: $
78
- }>
79
- } & CaseMatchParser<{
80
- inConstraint: unknown
81
- outConstraint: unknown
82
- thens: []
83
- $: $
84
- }>
85
-
86
- export type CaseMatchParser<ctx extends MatchContext> = <cases>(
87
- def: conform<cases, validateCases<cases, ctx>>
88
- ) => ChainableMatchParser<
89
- replaceKey<ctx, "thens", [...ctx["thens"], ...unionToTuple<valueOf<cases>>]>
90
- >
91
-
92
- export type WhenMatchParser<ctx extends MatchContext> = <
93
- def,
94
- then extends (
95
- In: ctx["inConstraint"] & inferTypeRoot<def, ctx["$"]>
96
- ) => ctx["outConstraint"]
97
- >(
98
- when: validateTypeRoot<def, ctx["$"]>,
99
- then: then
100
- ) => ChainableMatchParser<replaceKey<ctx, "thens", [...ctx["thens"], then]>>
101
-
102
- export type MatchInvokation<ctx extends MatchContext> = <
103
- data extends ctx["inConstraint"]
104
- >(
105
- In: data
106
- ) => {
107
- [i in Extract<keyof ctx["thens"], `${number}`>]: isDisjoint<
108
- data,
109
- paramsOf<ctx["thens"][i]>[0]
110
- > extends true
111
- ? never
112
- : returnOf<ctx["thens"][i]>
113
- }[Extract<keyof ctx["thens"], `${number}`>]
114
-
115
- export type ChainableMatchParser<ctx extends MatchContext> =
116
- MatchInvokation<ctx> & {
117
- cases: CaseMatchParser<ctx>
118
- when: WhenMatchParser<ctx>
119
- }
120
-
121
- export const createMatchParser = <$>(scope: Scope): MatchParser<$> => {
122
- // TODO: move to match node, discrimination
123
- const parser = (cases: Record<string, Morph>) => {
124
- const caseArray = Object.entries(cases).map(([def, morph]) => ({
125
- when: new Type(def, scope).allows,
126
- then: morph
127
- }))
128
- return (data: unknown) => {
129
- for (const c of caseArray) {
130
- if (c.when(data)) {
131
- return c.then(data, {} as never)
132
- }
133
- }
134
- }
135
- }
136
- return parser as never
137
- }
@@ -1,156 +0,0 @@
1
- import { isNode, node, type Root } from "@arktype/schema"
2
- import {
3
- isThunk,
4
- objectKindOf,
5
- stringify,
6
- throwParseError,
7
- type defined,
8
- type Dict,
9
- type equals,
10
- type ErrorMessage,
11
- type evaluate,
12
- type isAny,
13
- type isUnknown,
14
- type List,
15
- type objectKindOrDomainOf,
16
- type optionalKeyOf,
17
- type Primitive,
18
- type requiredKeyOf
19
- } from "@arktype/util"
20
- import type { ParseContext } from "../scope.js"
21
- import type { type } from "../scopes/ark.js"
22
- import { Type } from "../type.js"
23
- import {
24
- parseObjectLiteral,
25
- type inferObjectLiteral,
26
- type validateObjectLiteral
27
- } from "./objectLiteral.js"
28
- import type { validateString } from "./semantic/validate.js"
29
- import type { BaseCompletions, inferString } from "./string/string.js"
30
- import {
31
- parseTuple,
32
- type inferTuple,
33
- type TupleExpression,
34
- type validateTuple
35
- } from "./tuple.js"
36
-
37
- export const parseObject = (def: object, ctx: ParseContext): Root => {
38
- const objectKind = objectKindOf(def)
39
- switch (objectKind) {
40
- case undefined:
41
- if (isNode(def)) {
42
- return def as Root
43
- }
44
- return parseObjectLiteral(def as Dict, ctx)
45
- case "Array":
46
- return parseTuple(def as List, ctx)
47
- case "RegExp":
48
- return node({ basis: "string", pattern: def as RegExp })
49
- case "Function":
50
- const resolvedDef = isThunk(def) ? def() : def
51
- if (resolvedDef instanceof Type) {
52
- return resolvedDef.root
53
- }
54
- return throwParseError(writeBadDefinitionTypeMessage("Function"))
55
- default:
56
- return throwParseError(
57
- writeBadDefinitionTypeMessage(objectKind ?? stringify(def))
58
- )
59
- }
60
- }
61
-
62
- export type inferDefinition<def, $, args> = isAny<def> extends true
63
- ? never
64
- : def extends type.cast<infer t> | ThunkCast<infer t>
65
- ? t
66
- : def extends string
67
- ? inferString<def, $, args>
68
- : def extends readonly unknown[]
69
- ? inferTuple<def, $, args>
70
- : def extends RegExp
71
- ? string
72
- : def extends object
73
- ? inferObjectLiteral<def, $, args>
74
- : never
75
-
76
- export type validateDefinition<def, $, args> = null extends undefined
77
- ? ErrorMessage<`'strict' or 'strictNullChecks' must be set to true in your tsconfig's 'compilerOptions'`>
78
- : [def] extends [Terminal]
79
- ? unknown extends def
80
- ? // if def is any, never is the only way we can make validation fail
81
- // since any would be assignable to a standard error message
82
- never
83
- : def
84
- : def extends string
85
- ? validateString<def, $, args>
86
- : def extends readonly unknown[]
87
- ? validateTuple<def, $, args>
88
- : def extends BadDefinitionType
89
- ? writeBadDefinitionTypeMessage<objectKindOrDomainOf<def>>
90
- : isUnknown<def> extends true
91
- ? // this allows the initial list of autocompletions to be populated when a user writes "type()",
92
- // before having specified a definition
93
- BaseCompletions<$, args> | {}
94
- : validateObjectLiteral<def, $, args>
95
-
96
- export type validateDeclared<declared, def, $, args> =
97
- def extends validateDefinition<def, $, args>
98
- ? validateInference<def, declared, $, args>
99
- : validateDefinition<def, $, args>
100
-
101
- type validateInference<def, declared, $, args> = def extends
102
- | RegExp
103
- | type.cast<unknown>
104
- | ThunkCast
105
- | TupleExpression
106
- ? validateShallowInference<def, declared, $, args>
107
- : def extends readonly unknown[]
108
- ? declared extends readonly unknown[]
109
- ? {
110
- [i in keyof declared]: i extends keyof def
111
- ? validateInference<def[i], declared[i], $, args>
112
- : unknown
113
- }
114
- : evaluate<declarationMismatch<def, declared, $, args>>
115
- : def extends object
116
- ? evaluate<
117
- {
118
- [k in requiredKeyOf<declared>]: k extends keyof def
119
- ? validateInference<def[k], declared[k], $, args>
120
- : unknown
121
- } & {
122
- [k in optionalKeyOf<declared> &
123
- string as `${k}?`]: `${k}?` extends keyof def
124
- ? validateInference<def[`${k}?`], defined<declared[k]>, $, args>
125
- : unknown
126
- }
127
- >
128
- : validateShallowInference<def, declared, $, args>
129
-
130
- type validateShallowInference<def, declared, $, args> = equals<
131
- inferDefinition<def, $, args>,
132
- declared
133
- > extends true
134
- ? def
135
- : evaluate<declarationMismatch<def, declared, $, args>>
136
-
137
- type declarationMismatch<def, declared, $, args> = {
138
- declared: declared
139
- inferred: inferDefinition<def, $, args>
140
- }
141
-
142
- // functions are ignored in validation so that cyclic thunk definitions can be
143
- // inferred in scopes
144
- type Terminal = RegExp | type.cast<unknown> | ((...args: never[]) => unknown)
145
-
146
- export type ThunkCast<t = unknown> = () => type.cast<t>
147
-
148
- type BadDefinitionType = Exclude<Primitive, string>
149
-
150
- export const writeBadDefinitionTypeMessage = <actual extends string>(
151
- actual: actual
152
- ): writeBadDefinitionTypeMessage<actual> =>
153
- `Type definitions must be strings or objects (was ${actual})`
154
-
155
- type writeBadDefinitionTypeMessage<actual extends string> =
156
- `Type definitions must be strings or objects (was ${actual})`
package/parser/generic.ts DELETED
@@ -1,208 +0,0 @@
1
- import type { Root } from "@arktype/schema"
2
- import {
3
- throwParseError,
4
- type ErrorMessage,
5
- type join,
6
- type nominal
7
- } from "@arktype/util"
8
- import type { ParseContext } from "../scope.js"
9
- import { DynamicState } from "./string/reduce/dynamic.js"
10
- import { writeUnclosedGroupMessage } from "./string/reduce/shared.js"
11
- import type { StaticState, state } from "./string/reduce/static.js"
12
- import { writeUnexpectedCharacterMessage } from "./string/shift/operator/operator.js"
13
- import { Scanner } from "./string/shift/scanner.js"
14
- import { parseUntilFinalizer } from "./string/string.js"
15
-
16
- export type GenericDeclaration<
17
- name extends string = string,
18
- params extends string = string
19
- > = `${name}<${params}>`
20
-
21
- // we put the error in a tuple so that parseGenericParams always returns a string[]
22
- export type GenericParamsParseError<message extends string = string> = [
23
- nominal<message, "InvalidGenericParameters">
24
- ]
25
-
26
- export const parseGenericParams = (def: string) =>
27
- parseGenericParamsRecurse(new Scanner(def))
28
-
29
- export type parseGenericParams<def extends string> = parseParamsRecurse<
30
- def,
31
- "",
32
- []
33
- > extends infer result extends string[]
34
- ? "" extends result[number]
35
- ? GenericParamsParseError<emptyGenericParameterMessage>
36
- : result
37
- : never
38
-
39
- export const emptyGenericParameterMessage = `An empty string is not a valid generic parameter name`
40
-
41
- export type emptyGenericParameterMessage = typeof emptyGenericParameterMessage
42
-
43
- const parseGenericParamsRecurse = (scanner: Scanner): string[] => {
44
- const param = scanner.shiftUntilNextTerminator()
45
- if (param === "") {
46
- throwParseError(emptyGenericParameterMessage)
47
- }
48
- scanner.shiftUntilNonWhitespace()
49
- const nextNonWhitespace = scanner.shift()
50
- return nextNonWhitespace === ""
51
- ? [param]
52
- : nextNonWhitespace === ","
53
- ? [param, ...parseGenericParamsRecurse(scanner)]
54
- : throwParseError(writeUnexpectedCharacterMessage(nextNonWhitespace, ","))
55
- }
56
-
57
- type parseParamsRecurse<
58
- unscanned extends string,
59
- param extends string,
60
- result extends string[]
61
- > = unscanned extends `${infer lookahead}${infer nextUnscanned}`
62
- ? lookahead extends ","
63
- ? parseParamsRecurse<nextUnscanned, "", [...result, param]>
64
- : lookahead extends Scanner.WhiteSpaceToken
65
- ? param extends ""
66
- ? // if the next char is whitespace and we aren't in the middle of a param, skip to the next one
67
- parseParamsRecurse<Scanner.skipWhitespace<nextUnscanned>, "", result>
68
- : Scanner.skipWhitespace<nextUnscanned> extends `${infer nextNonWhitespace}${infer rest}`
69
- ? nextNonWhitespace extends ","
70
- ? parseParamsRecurse<rest, "", [...result, param]>
71
- : GenericParamsParseError<
72
- writeUnexpectedCharacterMessage<nextNonWhitespace, ",">
73
- >
74
- : // params end with a single whitespace character, add the current token
75
- [...result, param]
76
- : parseParamsRecurse<nextUnscanned, `${param}${lookahead}`, result>
77
- : param extends ""
78
- ? result
79
- : [...result, param]
80
-
81
- export type ParsedArgs<
82
- result extends unknown[] = unknown[],
83
- unscanned extends string = string
84
- > = {
85
- result: result
86
- unscanned: unscanned
87
- }
88
-
89
- export const parseGenericArgs = (
90
- name: string,
91
- params: string[],
92
- unscanned: string,
93
- ctx: ParseContext
94
- ) => parseGenericArgsRecurse(name, params, unscanned, ctx, [], [])
95
-
96
- export type parseGenericArgs<
97
- name extends string,
98
- params extends string[],
99
- unscanned extends string,
100
- $,
101
- args
102
- > = parseGenericArgsRecurse<name, params, unscanned, $, args, [], []>
103
-
104
- const parseGenericArgsRecurse = (
105
- name: string,
106
- params: string[],
107
- unscanned: string,
108
- ctx: ParseContext,
109
- argDefs: string[],
110
- argNodes: Root[]
111
- ): ParsedArgs<Root[]> => {
112
- const s = parseUntilFinalizer(new DynamicState(unscanned, ctx))
113
- // remove the finalizing token from the argDef
114
- argDefs.push(s.scanner.scanned.slice(0, -1))
115
- argNodes.push(s.root)
116
- const nextUnscanned = s.scanner.unscanned
117
- if (s.finalizer === ">") {
118
- if (argNodes.length === params.length) {
119
- return {
120
- result: argNodes,
121
- unscanned: nextUnscanned
122
- }
123
- } else {
124
- return s.error(writeInvalidGenericArgsMessage(name, params, argDefs))
125
- }
126
- } else if (s.finalizer === ",") {
127
- return parseGenericArgsRecurse(
128
- name,
129
- params,
130
- nextUnscanned,
131
- ctx,
132
- argDefs,
133
- argNodes
134
- )
135
- }
136
- return s.error(writeUnclosedGroupMessage(">"))
137
- }
138
-
139
- type parseGenericArgsRecurse<
140
- name extends string,
141
- params extends string[],
142
- unscanned extends string,
143
- $,
144
- args,
145
- argDefs extends string[],
146
- argAsts extends unknown[]
147
- > = parseUntilFinalizer<
148
- state.initialize<unscanned>,
149
- $,
150
- args
151
- > extends infer finalArgState extends StaticState
152
- ? {
153
- defs: [
154
- ...argDefs,
155
- finalArgState["scanned"] extends `${infer def}${"," | ">"}`
156
- ? def
157
- : finalArgState["scanned"]
158
- ]
159
- asts: [...argAsts, finalArgState["root"]]
160
- unscanned: finalArgState["unscanned"]
161
- } extends {
162
- defs: infer nextDefs extends string[]
163
- asts: infer nextAsts extends unknown[]
164
- unscanned: infer nextUnscanned extends string
165
- }
166
- ? finalArgState["finalizer"] extends ">"
167
- ? nextAsts["length"] extends params["length"]
168
- ? ParsedArgs<nextAsts, nextUnscanned>
169
- : state.error<writeInvalidGenericArgsMessage<name, params, nextDefs>>
170
- : finalArgState["finalizer"] extends ","
171
- ? parseGenericArgsRecurse<
172
- name,
173
- params,
174
- nextUnscanned,
175
- $,
176
- args,
177
- nextDefs,
178
- nextAsts
179
- >
180
- : finalArgState["finalizer"] extends ErrorMessage
181
- ? finalArgState
182
- : state.error<writeUnclosedGroupMessage<">">>
183
- : never
184
- : never
185
-
186
- export const writeInvalidGenericArgsMessage = <
187
- name extends string,
188
- params extends string[],
189
- argDefs extends string[]
190
- >(
191
- name: name,
192
- params: params,
193
- argDefs: argDefs
194
- ) =>
195
- `${name}<${params.join(", ")}> requires exactly ${params.length} args (got ${
196
- argDefs.length
197
- }${argDefs.length === 0 ? "" : ": " + argDefs.join(", ")})`
198
-
199
- export type writeInvalidGenericArgsMessage<
200
- name extends string,
201
- params extends string[],
202
- argDefs extends string[]
203
- > = `${name}<${join<
204
- params,
205
- ", "
206
- >}> requires exactly ${params["length"]} args (got ${argDefs["length"]}${argDefs["length"] extends 0
207
- ? ""
208
- : `: ${join<argDefs, ",">}`})`
@@ -1,120 +0,0 @@
1
- import { node, type Inner } from "@arktype/schema"
2
- import {
3
- stringify,
4
- type Dict,
5
- type ErrorMessage,
6
- type evaluate
7
- } from "@arktype/util"
8
- import type { ParseContext } from "../scope.js"
9
- import type { inferDefinition, validateDefinition } from "./definition.js"
10
- import type { validateString } from "./semantic/validate.js"
11
- import {
12
- parseEntry,
13
- type EntryParseResult,
14
- type IndexedKey,
15
- type OptionalValue,
16
- type validateObjectValue
17
- } from "./shared.js"
18
-
19
- const stringAndSymbolicEntriesOf = (o: Record<string | symbol, unknown>) => [
20
- ...Object.entries(o),
21
- ...Object.getOwnPropertySymbols(o).map((k) => [k, o[k]] as const)
22
- ]
23
-
24
- export const parseObjectLiteral = (def: Dict, ctx: ParseContext) => {
25
- const required: Inner<"required">[] = []
26
- const optional: Inner<"optional">[] = []
27
- for (const entry of stringAndSymbolicEntriesOf(def)) {
28
- const result = parseEntry(entry)
29
- ctx.path.push(
30
- `${
31
- typeof result.innerKey === "symbol"
32
- ? `[${stringify(result.innerKey)}]`
33
- : result.innerKey
34
- }`
35
- )
36
- const valueNode = ctx.scope.parse(result.innerValue, ctx)
37
- if (result.kind === "optional") {
38
- optional.push({
39
- key: result.innerKey,
40
- value: valueNode
41
- })
42
- } else {
43
- required.push({
44
- key: result.innerKey,
45
- value: valueNode
46
- })
47
- }
48
- ctx.path.pop()
49
- }
50
- return node({
51
- basis: "object",
52
- required,
53
- optional
54
- })
55
- }
56
-
57
- export type inferObjectLiteral<def extends object, $, args> = evaluate<
58
- {
59
- // since def is a const parameter, we remove the readonly modifier here
60
- // support for builtin readonly tracked here:
61
- // https://github.com/arktypeio/arktype/issues/808
62
- -readonly [k in keyof def as nonOptionalKeyFrom<
63
- k,
64
- def[k],
65
- $,
66
- args
67
- >]: inferDefinition<def[k], $, args>
68
- } & {
69
- -readonly [k in keyof def as optionalKeyFrom<k, def[k]>]?: inferDefinition<
70
- def[k] extends OptionalValue<infer inner> ? inner : def[k],
71
- $,
72
- args
73
- >
74
- }
75
- >
76
-
77
- export type validateObjectLiteral<def, $, args> = {
78
- [k in keyof def]: k extends IndexedKey<infer indexDef>
79
- ? validateString<indexDef, $, args> extends ErrorMessage<infer message>
80
- ? // add a nominal type here to avoid allowing the error message as input
81
- indexParseError<message>
82
- : inferDefinition<indexDef, $, args> extends PropertyKey
83
- ? // if the indexDef is syntactically and semantically valid,
84
- // move on to the validating the value definition
85
- validateDefinition<def[k], $, args>
86
- : indexParseError<writeInvalidPropertyKeyMessage<indexDef>>
87
- : validateObjectValue<def[k], $, args>
88
- }
89
-
90
- type nonOptionalKeyFrom<k extends PropertyKey, valueDef, $, args> = parseEntry<
91
- k,
92
- valueDef
93
- > extends infer result extends EntryParseResult
94
- ? result["kind"] extends "required"
95
- ? result["innerKey"]
96
- : result["kind"] extends "indexed"
97
- ? inferDefinition<result["innerKey"], $, args> & PropertyKey
98
- : never
99
- : never
100
-
101
- type optionalKeyFrom<k extends PropertyKey, valueDef> = parseEntry<
102
- k,
103
- valueDef
104
- > extends infer result extends EntryParseResult<"optional">
105
- ? result["innerKey"]
106
- : never
107
-
108
- declare const indexParseSymbol: unique symbol
109
-
110
- export type indexParseError<message extends string = string> = {
111
- [indexParseSymbol]: message
112
- }
113
-
114
- export const writeInvalidPropertyKeyMessage = <indexDef extends string>(
115
- indexDef: indexDef
116
- ): writeInvalidPropertyKeyMessage<indexDef> =>
117
- `Indexed key definition '${indexDef}' must be a string, number or symbol`
118
-
119
- type writeInvalidPropertyKeyMessage<indexDef extends string> =
120
- `Indexed key definition '${indexDef}' must be a string, number or symbol`
@@ -1,63 +0,0 @@
1
- import type {
2
- NumericallyBoundable,
3
- writeUnboundableMessage
4
- } from "@arktype/schema"
5
- import type { ErrorMessage } from "@arktype/util"
6
- import type { DateLiteral } from "../string/shift/operand/date.js"
7
- import type {
8
- BoundKind,
9
- Comparator,
10
- InvertedComparators,
11
- LimitLiteral,
12
- writeInvalidLimitMessage
13
- } from "../string/shift/operator/bounds.js"
14
- import type { inferAst } from "./semantic.js"
15
- import type { astToString } from "./utils.js"
16
- import type { validateAst } from "./validate.js"
17
-
18
- export type validateRange<
19
- l,
20
- comparator extends Comparator,
21
- r,
22
- $,
23
- args
24
- > = l extends LimitLiteral
25
- ? validateBound<r, InvertedComparators[comparator], l, "left", $, args>
26
- : l extends [infer leftAst, Comparator, unknown]
27
- ? ErrorMessage<writeDoubleRightBoundMessage<astToString<leftAst>>>
28
- : validateBound<l, comparator, r & LimitLiteral, "right", $, args>
29
-
30
- export type validateBound<
31
- boundedAst,
32
- comparator extends Comparator,
33
- limit extends LimitLiteral,
34
- boundKind extends BoundKind,
35
- $,
36
- args
37
- > = inferAst<boundedAst, $, args> extends infer bounded
38
- ? [bounded] extends [NumericallyBoundable]
39
- ? limit extends number
40
- ? validateAst<boundedAst, $, args>
41
- : ErrorMessage<writeInvalidLimitMessage<comparator, limit, boundKind>>
42
- : bounded extends Date
43
- ? limit extends DateLiteral
44
- ? validateAst<boundedAst, $, args>
45
- : ErrorMessage<writeInvalidLimitMessage<comparator, limit, boundKind>>
46
- : ErrorMessage<
47
- writeUnboundableMessage<
48
- astToString<
49
- boundKind extends "left"
50
- ? boundedAst[0 & keyof boundedAst]
51
- : boundedAst
52
- >
53
- >
54
- >
55
- : never
56
-
57
- export const writeDoubleRightBoundMessage = <root extends string>(
58
- root: root
59
- ): writeDoubleRightBoundMessage<root> =>
60
- `Expression ${root} must have at most one right bound`
61
-
62
- type writeDoubleRightBoundMessage<root extends string> =
63
- `Expression ${root} must have at most one right bound`
@@ -1,13 +0,0 @@
1
- import type { writeIndivisibleMessage } from "@arktype/schema"
2
- import type { ErrorMessage } from "@arktype/util"
3
- import type { inferAst } from "./semantic.js"
4
- import type { astToString } from "./utils.js"
5
- import type { validateAst } from "./validate.js"
6
-
7
- export type validateDivisor<l, $, args> = isDivisible<
8
- inferAst<l, $, args>
9
- > extends true
10
- ? validateAst<l, $, args>
11
- : ErrorMessage<writeIndivisibleMessage<astToString<l>>>
12
-
13
- type isDivisible<data> = [data] extends [number] ? true : false