arktype 1.1.0-dev-1 → 2.0.0-dev.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (213) hide show
  1. package/__tests__/array.test.ts +272 -0
  2. package/__tests__/astUtils.test.ts +17 -0
  3. package/__tests__/badDefinitionType.test.ts +51 -0
  4. package/__tests__/basis.test.ts +46 -0
  5. package/__tests__/bounds.test.ts +388 -0
  6. package/__tests__/cast.test.ts +32 -0
  7. package/__tests__/completions.test.ts +46 -0
  8. package/__tests__/config.test.ts +168 -0
  9. package/__tests__/cyclic.test.ts +144 -0
  10. package/__tests__/dateLiteral.test.ts +32 -0
  11. package/__tests__/declared.test.ts +123 -0
  12. package/__tests__/discrimination.test.ts +74 -0
  13. package/__tests__/divisor.test.ts +90 -0
  14. package/__tests__/enclosed.test.ts +93 -0
  15. package/__tests__/examples.test.ts +56 -0
  16. package/__tests__/expressions.test.ts +138 -0
  17. package/__tests__/generateBenchData.ts +89 -0
  18. package/__tests__/generated/cyclic.ts +3058 -0
  19. package/__tests__/generic.test.ts +294 -0
  20. package/__tests__/group.test.ts +72 -0
  21. package/__tests__/hkt.test.ts +22 -0
  22. package/__tests__/imports.test.ts +113 -0
  23. package/__tests__/instanceof.test.ts +102 -0
  24. package/__tests__/intersection.test.ts +104 -0
  25. package/__tests__/jsdoc.test.ts +64 -0
  26. package/__tests__/keyTraversal.test.ts +73 -0
  27. package/__tests__/keyof.test.ts +134 -0
  28. package/__tests__/keywords.test.ts +188 -0
  29. package/__tests__/literal.test.ts +51 -0
  30. package/__tests__/match.test.ts +34 -0
  31. package/__tests__/methods.bench.ts +22 -0
  32. package/__tests__/morph.test.ts +409 -0
  33. package/__tests__/narrow.test.ts +102 -0
  34. package/__tests__/nodeDefinitions.test.ts +190 -0
  35. package/__tests__/object.bench.ts +28 -0
  36. package/__tests__/objectLiteral.test.ts +300 -0
  37. package/__tests__/operand.bench.ts +45 -0
  38. package/__tests__/operator.bench.ts +130 -0
  39. package/__tests__/regex.test.ts +58 -0
  40. package/__tests__/scope.bench.ts +39 -0
  41. package/__tests__/scope.test.ts +140 -0
  42. package/__tests__/string.test.ts +48 -0
  43. package/__tests__/submodule.test.ts +99 -0
  44. package/__tests__/this.test.ts +57 -0
  45. package/__tests__/thunk.test.ts +80 -0
  46. package/__tests__/traverse.test.ts +119 -0
  47. package/__tests__/tsGenerics.test.ts +12 -0
  48. package/__tests__/typeMethods.test.ts +69 -0
  49. package/__tests__/typeReference.test.ts +39 -0
  50. package/__tests__/unenclosed.test.ts +120 -0
  51. package/__tests__/union.test.ts +147 -0
  52. package/ark.ts +91 -0
  53. package/main.ts +18 -0
  54. package/match.ts +116 -0
  55. package/out/ark.d.ts +52 -0
  56. package/out/ark.d.ts.map +1 -0
  57. package/out/ark.js +19 -0
  58. package/out/ark.js.map +1 -0
  59. package/out/arkConfig.d.ts +2 -0
  60. package/out/arkConfig.d.ts.map +1 -0
  61. package/out/arkConfig.js +10 -0
  62. package/out/arkConfig.js.map +1 -0
  63. package/out/main.d.ts +7 -0
  64. package/out/main.d.ts.map +1 -0
  65. package/out/main.js +4 -0
  66. package/out/main.js.map +1 -0
  67. package/out/match.d.ts +48 -0
  68. package/out/match.d.ts.map +1 -0
  69. package/out/match.js +18 -0
  70. package/out/match.js.map +1 -0
  71. package/out/parser/definition.d.ts +32 -0
  72. package/out/parser/definition.d.ts.map +1 -0
  73. package/out/parser/definition.js +32 -0
  74. package/out/parser/definition.js.map +1 -0
  75. package/out/parser/generic.d.ts +18 -0
  76. package/out/parser/generic.d.ts.map +1 -0
  77. package/out/parser/generic.js +19 -0
  78. package/out/parser/generic.js.map +1 -0
  79. package/out/parser/objectLiteral.d.ts +33 -0
  80. package/out/parser/objectLiteral.d.ts.map +1 -0
  81. package/out/parser/objectLiteral.js +66 -0
  82. package/out/parser/objectLiteral.js.map +1 -0
  83. package/out/parser/semantic/bounds.d.ts +13 -0
  84. package/out/parser/semantic/bounds.d.ts.map +1 -0
  85. package/out/parser/semantic/bounds.js +2 -0
  86. package/out/parser/semantic/bounds.js.map +1 -0
  87. package/out/parser/semantic/divisor.d.ts +9 -0
  88. package/out/parser/semantic/divisor.d.ts.map +1 -0
  89. package/out/parser/semantic/divisor.js +2 -0
  90. package/out/parser/semantic/divisor.js.map +1 -0
  91. package/out/parser/semantic/intersections.d.ts +14 -0
  92. package/out/parser/semantic/intersections.d.ts.map +1 -0
  93. package/out/parser/semantic/intersections.js +2 -0
  94. package/out/parser/semantic/intersections.js.map +1 -0
  95. package/out/parser/semantic/semantic.d.ts +33 -0
  96. package/out/parser/semantic/semantic.d.ts.map +1 -0
  97. package/out/parser/semantic/semantic.js +2 -0
  98. package/out/parser/semantic/semantic.js.map +1 -0
  99. package/out/parser/semantic/utils.d.ts +8 -0
  100. package/out/parser/semantic/utils.d.ts.map +1 -0
  101. package/out/parser/semantic/utils.js +2 -0
  102. package/out/parser/semantic/utils.js.map +1 -0
  103. package/out/parser/semantic/validate.d.ts +24 -0
  104. package/out/parser/semantic/validate.d.ts.map +1 -0
  105. package/out/parser/semantic/validate.js +2 -0
  106. package/out/parser/semantic/validate.js.map +1 -0
  107. package/out/parser/shared.d.ts +54 -0
  108. package/out/parser/shared.d.ts.map +1 -0
  109. package/out/parser/shared.js +47 -0
  110. package/out/parser/shared.js.map +1 -0
  111. package/out/parser/string/reduce/dynamic.d.ts +42 -0
  112. package/out/parser/string/reduce/dynamic.d.ts.map +1 -0
  113. package/out/parser/string/reduce/dynamic.js +133 -0
  114. package/out/parser/string/reduce/dynamic.js.map +1 -0
  115. package/out/parser/string/reduce/shared.d.ts +56 -0
  116. package/out/parser/string/reduce/shared.d.ts.map +1 -0
  117. package/out/parser/string/reduce/shared.js +26 -0
  118. package/out/parser/string/reduce/shared.js.map +1 -0
  119. package/out/parser/string/reduce/static.d.ts +176 -0
  120. package/out/parser/string/reduce/static.d.ts.map +1 -0
  121. package/out/parser/string/reduce/static.js +2 -0
  122. package/out/parser/string/reduce/static.js.map +1 -0
  123. package/out/parser/string/shift/operand/date.d.ts +11 -0
  124. package/out/parser/string/shift/operand/date.d.ts.map +1 -0
  125. package/out/parser/string/shift/operand/date.js +26 -0
  126. package/out/parser/string/shift/operand/date.js.map +1 -0
  127. package/out/parser/string/shift/operand/enclosed.d.ts +49 -0
  128. package/out/parser/string/shift/operand/enclosed.d.ts.map +1 -0
  129. package/out/parser/string/shift/operand/enclosed.js +50 -0
  130. package/out/parser/string/shift/operand/enclosed.js.map +1 -0
  131. package/out/parser/string/shift/operand/genericArgs.d.ts +29 -0
  132. package/out/parser/string/shift/operand/genericArgs.d.ts.map +1 -0
  133. package/out/parser/string/shift/operand/genericArgs.js +25 -0
  134. package/out/parser/string/shift/operand/genericArgs.js.map +1 -0
  135. package/out/parser/string/shift/operand/operand.d.ts +9 -0
  136. package/out/parser/string/shift/operand/operand.d.ts.map +1 -0
  137. package/out/parser/string/shift/operand/operand.js +17 -0
  138. package/out/parser/string/shift/operand/operand.js.map +1 -0
  139. package/out/parser/string/shift/operand/unenclosed.d.ts +34 -0
  140. package/out/parser/string/shift/operand/unenclosed.d.ts.map +1 -0
  141. package/out/parser/string/shift/operand/unenclosed.js +68 -0
  142. package/out/parser/string/shift/operand/unenclosed.js.map +1 -0
  143. package/out/parser/string/shift/operator/bounds.d.ts +33 -0
  144. package/out/parser/string/shift/operator/bounds.d.ts.map +1 -0
  145. package/out/parser/string/shift/operator/bounds.js +106 -0
  146. package/out/parser/string/shift/operator/bounds.js.map +1 -0
  147. package/out/parser/string/shift/operator/divisor.d.ts +9 -0
  148. package/out/parser/string/shift/operator/divisor.d.ts.map +1 -0
  149. package/out/parser/string/shift/operator/divisor.js +13 -0
  150. package/out/parser/string/shift/operator/divisor.js.map +1 -0
  151. package/out/parser/string/shift/operator/operator.d.ts +13 -0
  152. package/out/parser/string/shift/operator/operator.d.ts.map +1 -0
  153. package/out/parser/string/shift/operator/operator.js +29 -0
  154. package/out/parser/string/shift/operator/operator.js.map +1 -0
  155. package/out/parser/string/shift/scanner.d.ts +71 -0
  156. package/out/parser/string/shift/scanner.d.ts.map +1 -0
  157. package/out/parser/string/shift/scanner.js +99 -0
  158. package/out/parser/string/shift/scanner.js.map +1 -0
  159. package/out/parser/string/string.d.ts +24 -0
  160. package/out/parser/string/string.d.ts.map +1 -0
  161. package/out/parser/string/string.js +23 -0
  162. package/out/parser/string/string.js.map +1 -0
  163. package/out/parser/tuple.d.ts +71 -0
  164. package/out/parser/tuple.d.ts.map +1 -0
  165. package/out/parser/tuple.js +137 -0
  166. package/out/parser/tuple.js.map +1 -0
  167. package/out/scope.d.ts +159 -0
  168. package/out/scope.d.ts.map +1 -0
  169. package/out/scope.js +253 -0
  170. package/out/scope.js.map +1 -0
  171. package/out/test.d.ts +2 -0
  172. package/out/test.d.ts.map +1 -0
  173. package/out/test.js +7 -0
  174. package/out/test.js.map +1 -0
  175. package/out/tsconfig.build.tsbuildinfo +1 -0
  176. package/out/type.d.ts +77 -0
  177. package/out/type.d.ts.map +1 -0
  178. package/out/type.js +134 -0
  179. package/out/type.js.map +1 -0
  180. package/out/util.d.ts +19 -0
  181. package/out/util.d.ts.map +1 -0
  182. package/out/util.js +7 -0
  183. package/out/util.js.map +1 -0
  184. package/package.json +13 -10
  185. package/parser/definition.ts +159 -0
  186. package/parser/generic.ts +68 -0
  187. package/parser/objectLiteral.ts +197 -0
  188. package/parser/semantic/bounds.ts +61 -0
  189. package/parser/semantic/divisor.ts +13 -0
  190. package/parser/semantic/intersections.ts +46 -0
  191. package/parser/semantic/semantic.ts +144 -0
  192. package/parser/semantic/utils.ts +26 -0
  193. package/parser/semantic/validate.ts +109 -0
  194. package/parser/shared.ts +152 -0
  195. package/parser/string/reduce/dynamic.ts +196 -0
  196. package/parser/string/reduce/shared.ts +101 -0
  197. package/parser/string/reduce/static.ts +298 -0
  198. package/parser/string/shift/operand/date.ts +54 -0
  199. package/parser/string/shift/operand/enclosed.ts +118 -0
  200. package/parser/string/shift/operand/genericArgs.ts +127 -0
  201. package/parser/string/shift/operand/operand.ts +48 -0
  202. package/parser/string/shift/operand/unenclosed.ts +268 -0
  203. package/parser/string/shift/operator/bounds.ts +264 -0
  204. package/parser/string/shift/operator/divisor.ts +36 -0
  205. package/parser/string/shift/operator/operator.ts +82 -0
  206. package/parser/string/shift/scanner.ts +209 -0
  207. package/parser/string/string.ts +90 -0
  208. package/parser/tuple.ts +474 -0
  209. package/scope.ts +574 -0
  210. package/type.ts +347 -0
  211. package/util.ts +36 -0
  212. package/dist/main.d.ts +0 -1018
  213. package/dist/main.js +0 -1450
@@ -0,0 +1,209 @@
1
+ import { isKeyOf, type Dict } from "@arktype/util"
2
+ import type { Comparator } from "../reduce/shared.js"
3
+
4
+ export class Scanner<Lookahead extends string = string> {
5
+ private chars: string[]
6
+ private i: number
7
+
8
+ constructor(def: string) {
9
+ this.chars = [...def]
10
+ this.i = 0
11
+ }
12
+
13
+ /** Get lookahead and advance scanner by one */
14
+ shift() {
15
+ return (this.chars[this.i++] ?? "") as Lookahead
16
+ }
17
+
18
+ get lookahead() {
19
+ return (this.chars[this.i] ?? "") as Lookahead
20
+ }
21
+
22
+ get length() {
23
+ return this.chars.length
24
+ }
25
+
26
+ shiftUntil(condition: Scanner.UntilCondition): string {
27
+ let shifted = ""
28
+ while (this.lookahead) {
29
+ if (condition(this, shifted)) {
30
+ if (shifted[shifted.length - 1] === Scanner.escapeToken) {
31
+ shifted = shifted.slice(0, -1)
32
+ } else {
33
+ break
34
+ }
35
+ }
36
+ shifted += this.shift()
37
+ }
38
+ return shifted
39
+ }
40
+
41
+ shiftUntilNextTerminator() {
42
+ this.shiftUntilNonWhitespace()
43
+ return this.shiftUntil(Scanner.lookaheadIsTerminator)
44
+ }
45
+
46
+ shiftUntilNonWhitespace() {
47
+ return this.shiftUntil(Scanner.lookaheadIsNotWhitespace)
48
+ }
49
+
50
+ jumpToIndex(i: number) {
51
+ this.i = i < 0 ? this.length + i : i
52
+ }
53
+
54
+ get location() {
55
+ return this.i
56
+ }
57
+
58
+ get unscanned() {
59
+ return this.chars.slice(this.i, this.length).join("")
60
+ }
61
+
62
+ get scanned() {
63
+ return this.chars.slice(0, this.i).join("")
64
+ }
65
+
66
+ sliceChars(start: number, end?: number) {
67
+ return this.chars.slice(start, end).join("")
68
+ }
69
+
70
+ lookaheadIs<Char extends Lookahead>(char: Char): this is Scanner<Char> {
71
+ return this.lookahead === char
72
+ }
73
+
74
+ lookaheadIsIn<Tokens extends Dict>(
75
+ tokens: Tokens
76
+ ): this is Scanner<Extract<keyof Tokens, string>> {
77
+ return this.lookahead in tokens
78
+ }
79
+ }
80
+
81
+ export namespace Scanner {
82
+ export type UntilCondition = (scanner: Scanner, shifted: string) => boolean
83
+
84
+ export type OnInputEndFn = (scanner: Scanner, shifted: string) => string
85
+
86
+ export type ShiftUntilOptions = {
87
+ onInputEnd?: OnInputEndFn
88
+ }
89
+
90
+ export const lookaheadIsTerminator: UntilCondition = (scanner: Scanner) =>
91
+ scanner.lookahead in terminatingChars
92
+
93
+ export const lookaheadIsNotWhitespace: UntilCondition = (scanner: Scanner) =>
94
+ !(scanner.lookahead in whiteSpaceTokens)
95
+
96
+ export const terminatingChars = {
97
+ "<": true,
98
+ ">": true,
99
+ "=": true,
100
+ "|": true,
101
+ "&": true,
102
+ ")": true,
103
+ "[": true,
104
+ "%": true,
105
+ " ": true,
106
+ ",": true
107
+ } as const
108
+
109
+ export type TerminatingChar = keyof typeof terminatingChars
110
+
111
+ export const finalizingLookaheads = {
112
+ ">": true,
113
+ ",": true,
114
+ "": true
115
+ } as const
116
+
117
+ export type FinalizingLookahead = keyof typeof finalizingLookaheads
118
+
119
+ export type InfixToken = Comparator | "|" | "&" | "%" | ":" | "=>"
120
+
121
+ export type PostfixToken = "[]"
122
+
123
+ export type OperatorToken = InfixToken | PostfixToken
124
+
125
+ export const escapeToken = "\\"
126
+
127
+ export type EscapeToken = typeof escapeToken
128
+
129
+ export const whiteSpaceTokens = {
130
+ " ": true,
131
+ "\n": true
132
+ } as const
133
+
134
+ export type WhiteSpaceToken = keyof typeof whiteSpaceTokens
135
+
136
+ export const lookaheadIsFinalizing = (
137
+ lookahead: string,
138
+ unscanned: string
139
+ ): lookahead is ">" | "," =>
140
+ lookahead === ">"
141
+ ? unscanned[0] === "="
142
+ ? // >== would only occur in an expression like Array<number>==5
143
+ // otherwise, >= would only occur as part of a bound like number>=5
144
+ unscanned[1] === "="
145
+ : // if > is the end of a generic instantiation, the next token will be an operator or the end of the string
146
+ unscanned.trimStart() === "" ||
147
+ isKeyOf(unscanned.trimStart()[0], Scanner.terminatingChars)
148
+ : // if the lookahead is a finalizing token but not >, it's unambiguously a finalizer (currently just ",")
149
+ lookahead === ","
150
+
151
+ export type lookaheadIsFinalizing<
152
+ lookahead extends string,
153
+ unscanned extends string
154
+ > = lookahead extends ">"
155
+ ? unscanned extends `=${infer nextUnscanned}`
156
+ ? nextUnscanned extends `=${string}`
157
+ ? true
158
+ : false
159
+ : Scanner.skipWhitespace<unscanned> extends
160
+ | ""
161
+ | `${TerminatingChar}${string}`
162
+ ? true
163
+ : false
164
+ : lookahead extends ","
165
+ ? true
166
+ : false
167
+
168
+ export type shift<
169
+ lookahead extends string,
170
+ unscanned extends string
171
+ > = `${lookahead}${unscanned}`
172
+
173
+ export type shiftUntil<
174
+ unscanned extends string,
175
+ terminator extends string,
176
+ scanned extends string = ""
177
+ > = unscanned extends shift<infer lookahead, infer nextUnscanned>
178
+ ? lookahead extends terminator
179
+ ? scanned extends `${infer base}${EscapeToken}`
180
+ ? shiftUntil<nextUnscanned, terminator, `${base}${lookahead}`>
181
+ : [scanned, unscanned]
182
+ : shiftUntil<nextUnscanned, terminator, `${scanned}${lookahead}`>
183
+ : [scanned, ""]
184
+
185
+ export type shiftUntilNot<
186
+ unscanned extends string,
187
+ nonTerminator extends string,
188
+ scanned extends string = ""
189
+ > = unscanned extends shift<infer lookahead, infer nextUnscanned>
190
+ ? lookahead extends nonTerminator
191
+ ? shiftUntilNot<nextUnscanned, nonTerminator, `${scanned}${lookahead}`>
192
+ : [scanned, unscanned]
193
+ : [scanned, ""]
194
+
195
+ export type shiftUntilNextTerminator<unscanned extends string> = shiftUntil<
196
+ unscanned,
197
+ TerminatingChar
198
+ >
199
+
200
+ export type skipWhitespace<unscanned extends string> = shiftUntilNot<
201
+ unscanned,
202
+ WhiteSpaceToken
203
+ >[1]
204
+
205
+ export type shiftResult<scanned extends string, unscanned extends string> = [
206
+ scanned,
207
+ unscanned
208
+ ]
209
+ }
@@ -0,0 +1,90 @@
1
+ import type { TypeNode } from "@arktype/schema"
2
+ import {
3
+ throwInternalError,
4
+ throwParseError,
5
+ type ErrorMessage
6
+ } from "@arktype/util"
7
+ import type { inferAstRoot } from "../semantic/semantic.js"
8
+ import type { DynamicState, DynamicStateWithRoot } from "./reduce/dynamic.js"
9
+ import type { StringifiablePrefixOperator } from "./reduce/shared.js"
10
+ import type { StaticState, state } from "./reduce/static.js"
11
+ import type { parseOperand } from "./shift/operand/operand.js"
12
+ import {
13
+ writeUnexpectedCharacterMessage,
14
+ type parseOperator
15
+ } from "./shift/operator/operator.js"
16
+
17
+ /**
18
+ * Try to parse the definition from right to left using the most common syntax.
19
+ * This can be much more efficient for simple definitions.
20
+ */
21
+ export type parseString<def extends string, $, args> = def extends keyof $
22
+ ? // def could also be a generic reference here, in which case it will
23
+ // fail semantic validation because it has no args
24
+ def
25
+ : def extends `${infer child}[]`
26
+ ? child extends keyof $
27
+ ? [child, "[]"]
28
+ : fullStringParse<state.initialize<def>, $, args>
29
+ : fullStringParse<state.initialize<def>, $, args>
30
+
31
+ export type inferString<def extends string, $, args> = inferAstRoot<
32
+ parseString<def, $, args>,
33
+ $,
34
+ args
35
+ >
36
+
37
+ export type BaseCompletions<$, args, otherSuggestions extends string = never> =
38
+ | (keyof $ & string)
39
+ | (keyof args & string)
40
+ | StringifiablePrefixOperator
41
+ | otherSuggestions
42
+
43
+ export const fullStringParse = (s: DynamicState): TypeNode => {
44
+ s.parseOperand()
45
+ const result = parseUntilFinalizer(s).root
46
+ if (!result) {
47
+ return throwInternalError(
48
+ `Root was unexpectedly unset after parsing string '${s.scanner.scanned}'`
49
+ )
50
+ }
51
+ s.scanner.shiftUntilNonWhitespace()
52
+ if (s.scanner.lookahead) {
53
+ // throw a parse error if non-whitespace characters made it here without being parsed
54
+ throwParseError(writeUnexpectedCharacterMessage(s.scanner.lookahead))
55
+ }
56
+ return result
57
+ }
58
+
59
+ type fullStringParse<s extends StaticState, $, args> = extractFinalizedResult<
60
+ parseUntilFinalizer<s, $, args>
61
+ >
62
+
63
+ export const parseUntilFinalizer = (s: DynamicState) => {
64
+ while (s.finalizer === undefined) {
65
+ next(s)
66
+ }
67
+ return s as DynamicStateWithRoot
68
+ }
69
+
70
+ export type parseUntilFinalizer<
71
+ s extends StaticState,
72
+ $,
73
+ args
74
+ > = s["finalizer"] extends undefined
75
+ ? parseUntilFinalizer<next<s, $, args>, $, args>
76
+ : s
77
+
78
+ const next = (s: DynamicState) =>
79
+ s.hasRoot() ? s.parseOperator() : s.parseOperand()
80
+
81
+ type next<s extends StaticState, $, args> = s["root"] extends undefined
82
+ ? parseOperand<s, $, args>
83
+ : parseOperator<s, $, args>
84
+
85
+ export type extractFinalizedResult<s extends StaticState> =
86
+ s["finalizer"] extends ErrorMessage
87
+ ? s["finalizer"]
88
+ : s["finalizer"] extends ""
89
+ ? s["root"]
90
+ : state.error<writeUnexpectedCharacterMessage<`${s["finalizer"]}`>>