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,33 +1,72 @@
1
- import { type TypeNode } from "@arktype/schema";
2
- import { type Dict, type ErrorMessage, type evaluate, type merge } from "@arktype/util";
1
+ import { type BaseRoot, type Default, type of, type UndeclaredKeyBehavior, type writeInvalidPropertyKeyMessage } from "@arktype/schema";
2
+ import { type anyOrNever, type Dict, type ErrorMessage, type Key, type keyError, type merge, type show } from "@arktype/util";
3
3
  import type { ParseContext } from "../scope.js";
4
4
  import type { inferDefinition, validateDefinition } from "./definition.js";
5
5
  import type { astToString } from "./semantic/utils.js";
6
6
  import type { validateString } from "./semantic/validate.js";
7
- import { parseEntry, type EntryParseResult, type IndexedKey, type OptionalValue, type validateObjectValue } from "./shared.js";
8
- export declare const parseObjectLiteral: (def: Dict, ctx: ParseContext) => TypeNode;
7
+ import { Scanner } from "./string/shift/scanner.js";
8
+ export declare const parseObjectLiteral: (def: Dict, ctx: ParseContext) => BaseRoot;
9
+ export declare const writeInvalidUndeclaredBehaviorMessage: (actual: unknown) => string;
10
+ export declare const nonLeadingSpreadError = "Spread operator may only be used as the first key in an object";
11
+ export type inferObjectLiteral<def extends object, $, args> = show<"..." extends keyof def ? merge<inferDefinition<def["..."], $, args>, _inferObjectLiteral<def, $, args>> : _inferObjectLiteral<def, $, args>>;
9
12
  /**
10
13
  * Infers the contents of an object literal, ignoring a spread definition
11
- * You probably want to use {@link inferObjectLiteral} instead.
12
14
  */
13
- type inferObjectLiteralInner<def extends object, $, args> = {
14
- -readonly [k in keyof def as nonOptionalKeyFrom<k, def[k], $, args>]: inferDefinition<def[k], $, args>;
15
+ type _inferObjectLiteral<def extends object, $, args> = {
16
+ -readonly [k in keyof def as nonOptionalKeyFrom<k, $, args>]: def[k] extends (readonly [infer defaultableDef, "=", infer v]) ? def[k] extends anyOrNever ? def[k] : (In?: inferDefinition<defaultableDef, $, args>) => Default<v> : inferDefinition<def[k], $, args>;
15
17
  } & {
16
- -readonly [k in keyof def as optionalKeyFrom<k, def[k]>]?: inferDefinition<def[k] extends OptionalValue<infer inner> ? inner : def[k], $, args>;
18
+ -readonly [k in keyof def as optionalKeyFrom<k>]?: inferDefinition<def[k], $, args>;
17
19
  };
18
- export type inferObjectLiteral<def extends object, $, args> = evaluate<"..." extends keyof def ? merge<inferDefinition<def["..."], $, args>, inferObjectLiteralInner<def, $, args>> : inferObjectLiteralInner<def, $, args>>;
19
20
  export type validateObjectLiteral<def, $, args> = {
20
- [k in keyof def]: k extends IndexedKey<infer indexDef> ? validateString<indexDef, $, args> extends ErrorMessage<infer message> ? indexParseError<message> : inferDefinition<indexDef, $, args> extends PropertyKey ? validateDefinition<def[k], $, args> : indexParseError<writeInvalidPropertyKeyMessage<indexDef>> : k extends "..." ? inferDefinition<def[k], $, args> extends object ? validateDefinition<def[k], $, args> : indexParseError<writeInvalidSpreadTypeMessage<astToString<def[k]>>> : validateObjectValue<def[k], $, args>;
21
+ [k in keyof def]: k extends IndexKey<infer indexDef> ? validateString<indexDef, $, args> extends ErrorMessage<infer message> ? keyError<message> : inferDefinition<indexDef, $, args> extends (PropertyKey | of<PropertyKey, {}>) ? validateDefinition<def[k], $, args> : keyError<writeInvalidPropertyKeyMessage<indexDef>> : k extends "..." ? inferDefinition<def[k], $, args> extends object ? validateDefinition<def[k], $, args> : keyError<writeInvalidSpreadTypeMessage<astToString<def[k]>>> : k extends "+" ? UndeclaredKeyBehavior : validatePossibleDefaultValue<def, k, $, args>;
21
22
  };
22
- type nonOptionalKeyFrom<k extends PropertyKey, valueDef, $, args> = parseEntry<k, valueDef> extends infer result extends EntryParseResult ? result["kind"] extends "required" ? result["innerKey"] : result["kind"] extends "indexed" ? inferDefinition<result["innerKey"], $, args> & PropertyKey : never : never;
23
- type optionalKeyFrom<k extends PropertyKey, valueDef> = parseEntry<k, valueDef> extends infer result extends EntryParseResult<"optional"> ? result["innerKey"] : never;
24
- declare const indexParseSymbol: unique symbol;
25
- export type indexParseError<message extends string = string> = {
26
- [indexParseSymbol]: message;
23
+ type validatePossibleDefaultValue<def, k extends keyof def, $, args> = def[k] extends readonly [infer defaultDef, "=", unknown] ? parseKey<k>["kind"] extends "required" ? readonly [
24
+ validateDefinition<defaultDef, $, args>,
25
+ "=",
26
+ inferDefinition<defaultDef, $, args>
27
+ ] : ErrorMessage<invalidDefaultKeyKindMessage> : validateDefinition<def[k], $, args>;
28
+ type nonOptionalKeyFrom<k, $, args> = parseKey<k> extends PreparsedKey<"required", infer inner> ? inner : parseKey<k> extends PreparsedKey<"index", infer inner> ? inferDefinition<inner, $, args> extends infer t ? (t extends of<infer inner, any> ? Extract<inner, string> : Key) & t : never : never;
29
+ type optionalKeyFrom<k> = parseKey<k> extends PreparsedKey<"optional", infer inner> ? inner : never;
30
+ type PreparsedKey<kind extends ParsedKeyKind = ParsedKeyKind, inner extends Key = Key> = {
31
+ kind: kind;
32
+ key: inner;
27
33
  };
28
- export declare const writeInvalidPropertyKeyMessage: <indexDef extends string>(indexDef: indexDef) => `Indexed key definition '${indexDef}' must be a string, number or symbol`;
29
- type writeInvalidPropertyKeyMessage<indexDef extends string> = `Indexed key definition '${indexDef}' must be a string, number or symbol`;
34
+ declare namespace PreparsedKey {
35
+ type from<t extends PreparsedKey> = t;
36
+ }
37
+ type ParsedKeyKind = "required" | "optional" | "index" | MetaKey;
38
+ export type MetaKey = "..." | "+";
39
+ export type IndexKey<def extends string = string> = `[${def}]`;
40
+ interface PreparsedEntry extends PreparsedKey {
41
+ value: unknown;
42
+ default: unknown;
43
+ }
44
+ export declare const parseEntry: ([key, value]: readonly [
45
+ Key,
46
+ unknown
47
+ ]) => PreparsedEntry;
48
+ export declare const invalidDefaultKeyKindMessage = "Only required keys may specify default values, e.g. { ark: ['string', '=', '\u26F5'] }";
49
+ export type invalidDefaultKeyKindMessage = typeof invalidDefaultKeyKindMessage;
50
+ declare const parseKey: (key: Key) => PreparsedKey;
51
+ type parseKey<k> = k extends `${infer inner}?` ? inner extends `${infer baseName}${Scanner.EscapeToken}` ? PreparsedKey.from<{
52
+ kind: "required";
53
+ key: `${baseName}?`;
54
+ }> : PreparsedKey.from<{
55
+ kind: "optional";
56
+ key: inner;
57
+ }> : k extends MetaKey ? PreparsedKey.from<{
58
+ kind: k;
59
+ key: k;
60
+ }> : k extends `${Scanner.EscapeToken}${infer escapedMeta extends MetaKey}` ? PreparsedKey.from<{
61
+ kind: "required";
62
+ key: escapedMeta;
63
+ }> : k extends IndexKey<infer def> ? PreparsedKey.from<{
64
+ kind: "index";
65
+ key: def;
66
+ }> : PreparsedKey.from<{
67
+ kind: "required";
68
+ key: k extends (`${Scanner.EscapeToken}${infer escapedIndexKey extends IndexKey}`) ? escapedIndexKey : k extends Key ? k : `${k & number}`;
69
+ }>;
30
70
  export declare const writeInvalidSpreadTypeMessage: <def extends string>(def: def) => `Spread operand must resolve to an object literal type (was ${def})`;
31
71
  type writeInvalidSpreadTypeMessage<def extends string> = `Spread operand must resolve to an object literal type (was ${def})`;
32
72
  export {};
33
- //# sourceMappingURL=objectLiteral.d.ts.map
@@ -1,66 +1,107 @@
1
- import { keywords, schema } from "@arktype/schema";
2
- import { printable, throwParseError } from "@arktype/util";
3
- import { parseEntry } from "./shared.js";
4
- const stringAndSymbolicEntriesOf = (o) => [
5
- ...Object.entries(o),
6
- ...Object.getOwnPropertySymbols(o).map((k) => [k, o[k]])
7
- ];
1
+ import { ArkErrors, normalizeIndex } from "@arktype/schema";
2
+ import { append, isArray, printable, stringAndSymbolicEntriesOf, throwParseError, unset } from "@arktype/util";
3
+ import { Scanner } from "./string/shift/scanner.js";
8
4
  export const parseObjectLiteral = (def, ctx) => {
9
- const required = [];
10
- const optional = [];
5
+ let spread;
6
+ const structure = {};
11
7
  // We only allow a spread operator to be used as the first key in an object
12
8
  // because to match JS behavior any keys before the spread are overwritten
13
9
  // by the values in the target object, so there'd be no useful purpose in having it
14
10
  // anywhere except for the beginning.
15
- // Discussion in ArkType Discord:
16
- // https://discord.com/channels/957797212103016458/1103023445035462678/1182814502471860334
17
- let hasSeenFirstKey = false;
18
- const entries = stringAndSymbolicEntriesOf(def);
19
- for (const entry of entries) {
20
- const result = parseEntry(entry);
21
- if (result.kind === "spread") {
22
- if (hasSeenFirstKey) {
23
- return throwParseError("Spread operator may only be used as the first key in an object");
24
- }
25
- const spreadNode = ctx.scope.parse(result.innerValue, ctx);
26
- if (spreadNode.kind !== "intersection" ||
27
- !spreadNode.extends(keywords.object)) {
28
- return throwParseError(writeInvalidSpreadTypeMessage(printable(result.innerValue)));
29
- }
30
- // For each key on spreadNode, add it to our object.
31
- // We filter out keys from the spreadNode that will be defined later on this same object
32
- // because the currently parsed definition will overwrite them.
33
- const requiredEntriesFromSpread = (spreadNode.props?.required ?? []).filter((e) => !entries.some(([k]) => k === e.key));
34
- const optionalEntriesFromSpread = (spreadNode.props?.optional ?? []).filter((e) => !entries.some(([k]) => k === e.key));
35
- required.push(...requiredEntriesFromSpread);
36
- optional.push(...optionalEntriesFromSpread);
11
+ const parsedEntries = stringAndSymbolicEntriesOf(def).map(parseEntry);
12
+ if (parsedEntries[0]?.kind === "...") {
13
+ // remove the spread entry so we can iterate over the remaining entries
14
+ // expecting non-spread entries
15
+ const spreadEntry = parsedEntries.shift();
16
+ const spreadNode = ctx.$.parse(spreadEntry.value, ctx);
17
+ if (!spreadNode.hasKind("intersection") || !spreadNode.structure) {
18
+ return throwParseError(writeInvalidSpreadTypeMessage(typeof spreadEntry.value === "string" ?
19
+ spreadEntry.value
20
+ : printable(spreadEntry.value)));
21
+ }
22
+ spread = spreadNode.structure;
23
+ }
24
+ for (const entry of parsedEntries) {
25
+ if (entry.kind === "...")
26
+ return throwParseError(nonLeadingSpreadError);
27
+ if (entry.kind === "+") {
28
+ if (entry.value !== "reject" &&
29
+ entry.value !== "delete" &&
30
+ entry.value !== "ignore")
31
+ throwParseError(writeInvalidUndeclaredBehaviorMessage(entry.value));
32
+ structure.undeclared = entry.value;
37
33
  continue;
38
34
  }
39
- ctx.path.push(`${typeof result.innerKey === "symbol"
40
- ? `[${printable(result.innerKey)}]`
41
- : result.innerKey}`);
42
- const valueNode = ctx.scope.parse(result.innerValue, ctx);
43
- if (result.kind === "optional") {
44
- optional.push({
45
- key: result.innerKey,
46
- value: valueNode
47
- });
35
+ if (entry.kind === "index") {
36
+ // handle key parsing first to match type behavior
37
+ const key = ctx.$.parse(entry.key, ctx);
38
+ const value = ctx.$.parse(entry.value, ctx);
39
+ const normalizedSignature = normalizeIndex(key, value, ctx.$);
40
+ if (normalizedSignature.required) {
41
+ structure.required = append(structure.required, normalizedSignature.required);
42
+ }
43
+ if (normalizedSignature.index)
44
+ structure.index = append(structure.index, normalizedSignature.index);
48
45
  }
49
46
  else {
50
- required.push({
51
- key: result.innerKey,
52
- value: valueNode
53
- });
47
+ const value = ctx.$.parse(entry.value, ctx);
48
+ const inner = { key: entry.key, value };
49
+ if (entry.default !== unset) {
50
+ const out = value(entry.default);
51
+ if (out instanceof ArkErrors)
52
+ throwParseError(`Default value at ${printable(entry.key)} ${out}`);
53
+ value.assert(entry.default);
54
+ inner.default = entry.default;
55
+ }
56
+ structure[entry.kind] = append(structure[entry.kind], inner);
54
57
  }
55
- ctx.path.pop();
56
- hasSeenFirstKey ||= true;
57
58
  }
58
- return schema({
59
- basis: "object",
60
- required,
61
- optional
59
+ const structureNode = ctx.$.node("structure", structure);
60
+ return ctx.$.schema({
61
+ domain: "object",
62
+ structure: spread?.merge(structureNode) ?? structureNode
62
63
  });
63
64
  };
64
- export const writeInvalidPropertyKeyMessage = (indexDef) => `Indexed key definition '${indexDef}' must be a string, number or symbol`;
65
+ export const writeInvalidUndeclaredBehaviorMessage = (actual) => `Value of '+' key must be 'reject', 'delete', or 'ignore' (was ${printable(actual)})`;
66
+ export const nonLeadingSpreadError = "Spread operator may only be used as the first key in an object";
67
+ export const parseEntry = ([key, value]) => {
68
+ const parsedKey = parseKey(key);
69
+ if (isArray(value) && value[1] === "=") {
70
+ if (parsedKey.kind !== "required")
71
+ throwParseError(invalidDefaultKeyKindMessage);
72
+ return {
73
+ kind: "optional",
74
+ key: parsedKey.key,
75
+ value: value[0],
76
+ default: value[2]
77
+ };
78
+ }
79
+ return {
80
+ kind: parsedKey.kind,
81
+ key: parsedKey.key,
82
+ value,
83
+ default: unset
84
+ };
85
+ };
86
+ // single quote use here is better for TypeScript's inlined error to avoid escapes
87
+ export const invalidDefaultKeyKindMessage = `Only required keys may specify default values, e.g. { ark: ['string', '=', '⛵'] }`;
88
+ const parseKey = (key) => typeof key === "symbol" ? { kind: "required", key }
89
+ : key.at(-1) === "?" ?
90
+ key.at(-2) === Scanner.escapeToken ?
91
+ { kind: "required", key: `${key.slice(0, -2)}?` }
92
+ : {
93
+ kind: "optional",
94
+ key: key.slice(0, -1)
95
+ }
96
+ : key[0] === "[" && key.at(-1) === "]" ?
97
+ { kind: "index", key: key.slice(1, -1) }
98
+ : key[0] === Scanner.escapeToken && key[1] === "[" && key.at(-1) === "]" ?
99
+ { kind: "required", key: key.slice(1) }
100
+ : key === "..." || key === "+" ? { kind: key, key }
101
+ : {
102
+ kind: "required",
103
+ key: key === "\\..." ? "..."
104
+ : key === "\\+" ? "+"
105
+ : key
106
+ };
65
107
  export const writeInvalidSpreadTypeMessage = (def) => `Spread operand must resolve to an object literal type (was ${def})`;
66
- //# sourceMappingURL=objectLiteral.js.map
@@ -1,13 +1,15 @@
1
- import type { NumericallyBoundable } from "@arktype/schema";
2
- import type { ErrorMessage } from "@arktype/util";
3
- import type { Comparator, InvertedComparators, LimitLiteral } from "../string/reduce/shared.js";
4
- import type { BoundExpressionKind, writeInvalidLimitMessage, writeUnboundableMessage } from "../string/shift/operator/bounds.js";
5
- import type { inferAstBase } from "./semantic.js";
1
+ import type { LimitLiteral, writeUnboundableMessage } from "@arktype/schema";
2
+ import type { ErrorMessage, array } from "@arktype/util";
3
+ import type { Comparator } from "../string/reduce/shared.js";
4
+ import type { BoundExpressionKind, writeInvalidLimitMessage } from "../string/shift/operator/bounds.js";
5
+ import type { inferAstIn } from "./infer.js";
6
6
  import type { astToString } from "./utils.js";
7
7
  import type { validateAst } from "./validate.js";
8
- export type validateRange<l, comparator extends Comparator, r, $, args> = l extends LimitLiteral ? validateBound<r, InvertedComparators[comparator], l, "left", $, args> : l extends [infer leftAst, Comparator, unknown] ? ErrorMessage<writeDoubleRightBoundMessage<astToString<leftAst>>> : validateBound<l, comparator, r & LimitLiteral, "right", $, args>;
9
- export type validateBound<boundedAst, comparator extends Comparator, limit extends LimitLiteral, boundKind extends BoundExpressionKind, $, args> = inferAstBase<boundedAst, $, args> extends infer bounded ? [bounded] extends [NumericallyBoundable] ? limit extends number ? validateAst<boundedAst, $, args> : ErrorMessage<writeInvalidLimitMessage<comparator, limit, boundKind>> : bounded extends Date ? validateAst<boundedAst, $, args> : ErrorMessage<writeUnboundableMessage<astToString<boundKind extends "left" ? boundedAst[0 & keyof boundedAst] : boundedAst>>> : never;
8
+ export type validateRange<l, comparator extends Comparator, r, $, args> = l extends LimitLiteral ? validateBound<r, comparator, l, "left", $, args> : l extends [infer leftAst, Comparator, unknown] ? ErrorMessage<writeDoubleRightBoundMessage<astToString<leftAst>>> : validateBound<l, comparator, r & LimitLiteral, "right", $, args>;
9
+ export type validateBound<boundedAst, comparator extends Comparator, limit extends LimitLiteral, boundKind extends BoundExpressionKind, $, args> = inferAstIn<boundedAst, $, args> extends infer bounded ? isNumericallyBoundable<bounded> extends true ? limit extends number ? validateAst<boundedAst, $, args> : ErrorMessage<writeInvalidLimitMessage<comparator, limit, boundKind>> : bounded extends Date ? validateAst<boundedAst, $, args> : ErrorMessage<writeUnboundableMessage<astToString<boundKind extends "left" ? boundedAst[0 & keyof boundedAst] : boundedAst>>> : never;
10
+ type isNumericallyBoundable<bounded> = [
11
+ bounded
12
+ ] extends [number] ? true : [bounded] extends [string] ? true : [bounded] extends [array] ? true : false;
10
13
  export declare const writeDoubleRightBoundMessage: <root extends string>(root: root) => `Expression ${root} must have at most one right bound`;
11
14
  type writeDoubleRightBoundMessage<root extends string> = `Expression ${root} must have at most one right bound`;
12
15
  export {};
13
- //# sourceMappingURL=bounds.d.ts.map
@@ -1,2 +1 @@
1
1
  export const writeDoubleRightBoundMessage = (root) => `Expression ${root} must have at most one right bound`;
2
- //# sourceMappingURL=bounds.js.map
@@ -1,9 +1,7 @@
1
- import type { writeIndivisibleMessage } from "@arktype/schema";
1
+ import type { Root, writeIndivisibleMessage } from "@arktype/schema";
2
2
  import type { ErrorMessage } from "@arktype/util";
3
- import type { inferAstBase } from "./semantic.js";
4
- import type { astToString } from "./utils.js";
3
+ import type { inferAstIn } from "./infer.js";
5
4
  import type { validateAst } from "./validate.js";
6
- export type validateDivisor<l, $, args> = isDivisible<inferAstBase<l, $, args>> extends true ? validateAst<l, $, args> : ErrorMessage<writeIndivisibleMessage<astToString<l>>>;
7
- type isDivisible<data> = [data] extends [number] ? true : false;
8
- export {};
9
- //# sourceMappingURL=divisor.d.ts.map
5
+ export type validateDivisor<l, $, args> = inferAstIn<l, $, args> extends infer data ? [
6
+ data
7
+ ] extends [number] ? validateAst<l, $, args> : ErrorMessage<writeIndivisibleMessage<Root<data>>> : never;
@@ -1,2 +1 @@
1
1
  export {};
2
- //# sourceMappingURL=divisor.js.map
@@ -0,0 +1,24 @@
1
+ import type { Date, DateLiteral, GenericProps, LimitLiteral, RegexLiteral, constrain, distillIn, inferIntersection, normalizeLimit, string } from "@arktype/schema";
2
+ import type { BigintLiteral, array } from "@arktype/util";
3
+ import type { UnparsedScope, resolve, tryInferSubmoduleReference } from "../../scope.js";
4
+ import type { inferDefinition } from "../definition.js";
5
+ import type { Comparator, MinComparator } from "../string/reduce/shared.js";
6
+ import type { StringLiteral } from "../string/shift/operand/enclosed.js";
7
+ export type inferAstRoot<ast, $, args> = inferConstrainableAst<ast, $, args>;
8
+ export type inferAstIn<ast, $, args> = distillIn<inferAstRoot<ast, $, args>>;
9
+ export type inferConstrainableAst<ast, $, args> = ast extends array ? inferExpression<ast, $, args> : ast extends string ? inferTerminal<ast, $, args> : never;
10
+ export type GenericInstantiationAst<generic extends GenericProps = GenericProps, argAsts extends unknown[] = unknown[]> = [generic, "<>", argAsts];
11
+ export type inferExpression<ast extends array, $, args> = ast extends GenericInstantiationAst<infer generic, infer argAsts> ? inferDefinition<generic["def"], generic["$"]["t"] extends UnparsedScope ? $ : generic["$"]["t"], {
12
+ [i in keyof generic["params"] & `${number}` as generic["params"][i]]: inferConstrainableAst<argAsts[i & keyof argAsts], $, args>;
13
+ }> : ast[1] extends "[]" ? inferConstrainableAst<ast[0], $, args>[] : ast[1] extends "|" ? inferConstrainableAst<ast[0], $, args> | inferConstrainableAst<ast[2], $, args> : ast[1] extends "&" ? inferIntersection<inferConstrainableAst<ast[0], $, args>, inferConstrainableAst<ast[2], $, args>> : ast[1] extends Comparator ? ast[0] extends LimitLiteral ? constrainBound<inferConstrainableAst<ast[2], $, args>, ast[1], ast[0]> : constrainBound<inferConstrainableAst<ast[0], $, args>, ast[1], ast[2]> : ast[1] extends "%" ? constrain<inferConstrainableAst<ast[0], $, args>, "divisor", ast[2] & number> : ast[0] extends "keyof" ? keyof inferConstrainableAst<ast[1], $, args> : never;
14
+ export type constrainBound<constrainableIn, comparator extends Comparator, limit> = distillIn<constrainableIn> extends infer In ? comparator extends "==" ? In extends number ? limit : In extends Date ? Date.literal<normalizeLimit<limit>> : constrain<constrainableIn, "exactLength", limit & number> : constrain<constrainableIn, In extends number ? comparator extends MinComparator ? "min" : "max" : In extends string | array ? comparator extends MinComparator ? "minLength" : "maxLength" : comparator extends MinComparator ? "after" : "before", {
15
+ rule: normalizeLimit<limit>;
16
+ exclusive: comparator extends ">" | "<" ? true : false;
17
+ }> : never;
18
+ export type PrefixOperator = "keyof" | "instanceof" | "===" | "node";
19
+ export type PrefixExpression<operator extends PrefixOperator = PrefixOperator, operand = unknown> = [operator, operand];
20
+ export type PostfixOperator = "[]";
21
+ export type PostfixExpression<operator extends PostfixOperator = PostfixOperator, operand = unknown> = readonly [operand, operator];
22
+ export type InfixOperator = "|" | "&" | Comparator | "%" | ":" | "=>" | "@";
23
+ export type InfixExpression<operator extends InfixOperator = InfixOperator, l = unknown, r = unknown> = [l, operator, r];
24
+ export type inferTerminal<token extends string, $, args> = token extends keyof args | keyof $ ? resolve<token, $, args> : `#${token}` extends keyof $ ? resolve<`#${token}`, $, args> : token extends StringLiteral<infer text> ? text : token extends `${infer n extends number}` ? n : token extends BigintLiteral<infer b> ? b : token extends RegexLiteral<infer source> ? string.matching<source> : token extends DateLiteral<infer source> ? Date.literal<source> : tryInferSubmoduleReference<$, token>;
@@ -0,0 +1 @@
1
+ export {};
@@ -1,8 +1,4 @@
1
- import type { List, Stringifiable } from "@arktype/util";
1
+ import type { Stringifiable } from "@arktype/util";
2
2
  import type { Comparator } from "../string/reduce/shared.js";
3
- import type { InfixExpression, PostfixExpression } from "./semantic.js";
4
- export type astToString<ast> = `'${astToStringRecurse<ast>}'`;
5
- type astToStringRecurse<ast> = ast extends PostfixExpression<infer operator, infer operand> ? operator extends "[]" ? `${groupAst<operand>}[]` : never : ast extends InfixExpression<infer operator, infer l, infer r> ? operator extends "&" | "|" | "%" | Comparator ? `${groupAst<l>}${operator}${groupAst<r>}` : never : ast extends Stringifiable ? `${ast extends bigint ? `${ast}n` : ast}` : "...";
6
- type groupAst<ast> = ast extends List ? ast[1] extends "[]" ? astToStringRecurse<ast> : `(${astToStringRecurse<ast>})` : astToStringRecurse<ast>;
7
- export {};
8
- //# sourceMappingURL=utils.d.ts.map
3
+ import type { InfixExpression, PostfixExpression } from "./infer.js";
4
+ export type astToString<ast> = ast extends PostfixExpression<infer operator, infer operand> ? operator extends "[]" ? `${astToString<operand>}[]` : never : ast extends InfixExpression<infer operator, infer l, infer r> ? operator extends "&" | "|" | "%" | Comparator ? `${astToString<l>} ${operator} ${astToString<r>}` : never : ast extends Stringifiable ? `${ast extends bigint ? `${ast}n` : ast}` : "...";
@@ -1,2 +1 @@
1
1
  export {};
2
- //# sourceMappingURL=utils.js.map
@@ -1,24 +1,27 @@
1
- import type { BigintLiteral, Completion, ErrorMessage, NumberLiteral, writeMalformedNumericLiteralMessage } from "@arktype/util";
2
- import type { Module } from "../../scope.js";
3
- import type { GenericProps } from "../../type.js";
1
+ import type { arkKind, GenericProps, PrivateDeclaration, writeMissingSubmoduleAccessMessage } from "@arktype/schema";
2
+ import type { anyOrNever, BigintLiteral, charsAfterFirst, Completion, ErrorMessage, writeMalformedNumericLiteralMessage } from "@arktype/util";
4
3
  import type { Comparator } from "../string/reduce/shared.js";
5
4
  import type { writeInvalidGenericArgsMessage } from "../string/shift/operand/genericArgs.js";
6
- import type { writeMissingSubmoduleAccessMessage } from "../string/shift/operand/unenclosed.js";
7
5
  import type { parseString } from "../string/string.js";
8
6
  import type { validateRange } from "./bounds.js";
9
7
  import type { validateDivisor } from "./divisor.js";
10
- import type { GenericInstantiationAst, InfixExpression, PostfixExpression } from "./semantic.js";
8
+ import type { GenericInstantiationAst, InfixExpression, PostfixExpression } from "./infer.js";
11
9
  import type { astToString } from "./utils.js";
12
- export type validateAst<ast, $, args> = ast extends string ? validateStringAst<ast, $> : ast extends PostfixExpression<infer operator, infer operand> ? operator extends "[]" ? validateAst<operand, $, args> : never : ast extends InfixExpression<infer operator, infer l, infer r> ? operator extends "&" | "|" ? validateInfix<ast, $, args> : operator extends Comparator ? validateRange<l, operator, r, $, args> : operator extends "%" ? validateDivisor<l, $, args> : undefined : ast extends readonly ["keyof", infer operand] ? validateAst<operand, $, args> : ast extends GenericInstantiationAst ? validateGenericArgs<ast["2"], $, args> : ErrorMessage<writeUnexpectedExpressionMessage<astToString<ast>>>;
10
+ export type validateAst<ast, $, args> = ast extends string ? validateStringAst<ast, $> : ast extends PostfixExpression<infer operator, infer operand> ? operator extends "[]" ? validateAst<operand, $, args> : never : ast extends InfixExpression<infer operator, infer l, infer r> ? operator extends "&" | "|" ? validateInfix<ast, $, args> : operator extends Comparator ? validateRange<l, operator, r, $, args> : operator extends "%" ? validateDivisor<l, $, args> : undefined : ast extends readonly ["keyof", infer operand] ? validateAst<operand, $, args> : ast extends GenericInstantiationAst ? validateGenericArgs<ast[2], $, args> : ErrorMessage<writeUnexpectedExpressionMessage<astToString<ast>>> & {
11
+ ast: ast;
12
+ };
13
13
  type writeUnexpectedExpressionMessage<expression extends string> = `Unexpectedly failed to parse the expression resulting from ${expression}`;
14
- type validateGenericArgs<argAsts extends unknown[], $, args> = argAsts extends [
15
- infer head,
16
- ...infer tail
17
- ] ? validateAst<head, $, args> extends ErrorMessage<infer message> ? ErrorMessage<message> : validateGenericArgs<tail, $, args> : undefined;
14
+ type validateGenericArgs<argAsts extends unknown[], $, args> = argAsts extends [infer head, ...infer tail] ? validateAst<head, $, args> extends ErrorMessage<infer message> ? ErrorMessage<message> : validateGenericArgs<tail, $, args> : undefined;
18
15
  export declare const writeUnsatisfiableExpressionError: <expression extends string>(expression: expression) => `${expression} results in an unsatisfiable type`;
19
16
  export type writeUnsatisfiableExpressionError<expression extends string> = `${expression} results in an unsatisfiable type`;
20
- type validateStringAst<def extends string, $> = def extends NumberLiteral<infer value> ? number extends value ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "number">> : undefined : def extends BigintLiteral<infer value> ? bigint extends value ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "bigint">> : undefined : def extends keyof $ ? $[def] extends null ? def : $[def] extends GenericProps ? ErrorMessage<writeInvalidGenericArgsMessage<def, $[def]["parameters"], []>> : $[def] extends Module ? ErrorMessage<writeMissingSubmoduleAccessMessage<def>> : undefined : def extends ErrorMessage ? def : undefined;
21
- export type validateString<def extends string, $, args> = validateAst<parseString<def, $, args>, $, args> extends infer result extends ErrorMessage ? result extends Completion<infer text> ? text : result : def;
17
+ export declare const writePrefixedPrivateReferenceMessage: <def extends `#${string}`>(def: def) => `Private type references should not include '#'. Use '${charsAfterFirst<def>}' instead.`;
18
+ export type writePrefixedPrivateReferenceMessage<def extends PrivateDeclaration> = `Private type references should not include '#'. Use '${charsAfterFirst<def>}' instead.`;
19
+ type validateStringAst<def extends string, $> = def extends `${infer n extends number}` ? number extends n ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "number">> : undefined : def extends BigintLiteral<infer b> ? bigint extends b ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "bigint">> : undefined : maybeExtractAlias<def, $> extends infer alias extends keyof $ ? [
20
+ $[alias]
21
+ ] extends [anyOrNever] ? def : def extends PrivateDeclaration ? ErrorMessage<writePrefixedPrivateReferenceMessage<def>> : $[alias] extends GenericProps ? ErrorMessage<writeInvalidGenericArgsMessage<def, $[alias]["params"], []>> : $[alias] extends {
22
+ [arkKind]: "module";
23
+ } ? ErrorMessage<writeMissingSubmoduleAccessMessage<def>> : undefined : def extends ErrorMessage ? def : undefined;
24
+ export type maybeExtractAlias<def extends string, $> = def extends keyof $ ? def : `#${def}` extends keyof $ ? `#${def}` : null;
25
+ export type validateString<def extends string, $, args> = validateAst<parseString<def, $, args>, $, args> extends (infer result extends ErrorMessage) ? result extends Completion<infer text> ? text : result : def;
22
26
  type validateInfix<ast extends InfixExpression, $, args> = validateAst<ast[0], $, args> extends ErrorMessage<infer message> ? ErrorMessage<message> : validateAst<ast[2], $, args> extends ErrorMessage<infer message> ? ErrorMessage<message> : undefined;
23
27
  export {};
24
- //# sourceMappingURL=validate.d.ts.map
@@ -1,2 +1,2 @@
1
1
  export const writeUnsatisfiableExpressionError = (expression) => `${expression} results in an unsatisfiable type`;
2
- //# sourceMappingURL=validate.js.map
2
+ export const writePrefixedPrivateReferenceMessage = (def) => `Private type references should not include '#'. Use '${def.slice(1)}' instead.`;
@@ -1,32 +1,33 @@
1
- import type { TypeNode } from "@arktype/schema";
1
+ import type { BaseRoot, LimitLiteral } from "@arktype/schema";
2
2
  import { type requireKeys } from "@arktype/util";
3
3
  import type { ParseContext } from "../../../scope.js";
4
+ import type { InfixOperator } from "../../semantic/infer.js";
4
5
  import { Scanner } from "../shift/scanner.js";
5
- import { type Comparator, type LimitLiteral, type OpenLeftBound, type StringifiablePrefixOperator } from "./shared.js";
6
+ import { type Comparator, type MinComparator, type OpenLeftBound, type StringifiablePrefixOperator } from "./shared.js";
6
7
  type BranchState = {
7
8
  prefixes: StringifiablePrefixOperator[];
8
- leftBound?: OpenLeftBound;
9
- "&"?: TypeNode;
10
- "|"?: TypeNode;
9
+ leftBound: OpenLeftBound | null;
10
+ intersection: BaseRoot | null;
11
+ union: BaseRoot | null;
11
12
  };
12
13
  export type DynamicStateWithRoot = requireKeys<DynamicState, "root">;
13
14
  export declare class DynamicState {
14
15
  readonly ctx: ParseContext;
15
16
  readonly scanner: Scanner;
16
- root: TypeNode | undefined;
17
+ root: BaseRoot<any> | undefined;
17
18
  branches: BranchState;
18
19
  finalizer: Scanner.FinalizingLookahead | undefined;
19
20
  groups: BranchState[];
20
21
  constructor(def: string, ctx: ParseContext);
21
22
  error(message: string): never;
22
23
  hasRoot(): this is DynamicStateWithRoot;
24
+ setRoot(root: BaseRoot): void;
23
25
  unsetRoot(): this["root"];
24
- constrainRoot(...args: Parameters<TypeNode["constrain"]>): void;
25
- setRoot(root: TypeNode): void;
26
- finalize(finalizer: Scanner.FinalizingLookahead): undefined;
27
- reduceLeftBound(limit: LimitLiteral, comparator: Comparator): undefined;
26
+ constrainRoot(...args: Parameters<BaseRoot<any>["constrain"]>): void;
27
+ finalize(finalizer: Scanner.FinalizingLookahead): void;
28
+ reduceLeftBound(limit: LimitLiteral, comparator: Comparator): void;
28
29
  finalizeBranches(): void;
29
- finalizeGroup(): undefined;
30
+ finalizeGroup(): void;
30
31
  addPrefix(prefix: StringifiablePrefixOperator): void;
31
32
  applyPrefixes(): void;
32
33
  pushRootToBranch(token: "|" | "&"): void;
@@ -35,8 +36,7 @@ export declare class DynamicState {
35
36
  parseOperand(): void;
36
37
  private assertRangeUnset;
37
38
  reduceGroupOpen(): void;
38
- previousOperator(): ">" | ">=" | "|" | "&" | "keyof" | undefined;
39
+ previousOperator(): MinComparator | StringifiablePrefixOperator | InfixOperator | undefined;
39
40
  shiftedByOne(): this;
40
41
  }
41
42
  export {};
42
- //# sourceMappingURL=dynamic.d.ts.map
@@ -7,9 +7,13 @@ import { invertedComparators, minComparators, writeMultipleLeftBoundsMessage, wr
7
7
  export class DynamicState {
8
8
  ctx;
9
9
  scanner;
10
+ // set root type to `any` so that all constraints can be applied
10
11
  root;
11
12
  branches = {
12
- prefixes: []
13
+ prefixes: [],
14
+ leftBound: null,
15
+ intersection: null,
16
+ union: null
13
17
  };
14
18
  finalizer;
15
19
  groups = [];
@@ -23,29 +27,27 @@ export class DynamicState {
23
27
  hasRoot() {
24
28
  return this.root !== undefined;
25
29
  }
30
+ setRoot(root) {
31
+ this.root = root;
32
+ }
26
33
  unsetRoot() {
27
34
  const value = this.root;
28
35
  this.root = undefined;
29
36
  return value;
30
37
  }
31
38
  constrainRoot(...args) {
32
- this.root = this.root.constrain(...args);
33
- }
34
- setRoot(root) {
35
- this.root = root;
39
+ this.root = this.root.constrain(args[0], args[1]);
36
40
  }
37
41
  finalize(finalizer) {
38
- if (this.groups.length) {
42
+ if (this.groups.length)
39
43
  return this.error(writeUnclosedGroupMessage(")"));
40
- }
41
44
  this.finalizeBranches();
42
45
  this.finalizer = finalizer;
43
46
  }
44
47
  reduceLeftBound(limit, comparator) {
45
48
  const invertedComparator = invertedComparators[comparator];
46
- if (!isKeyOf(invertedComparator, minComparators)) {
49
+ if (!isKeyOf(invertedComparator, minComparators))
47
50
  return this.error(writeUnpairableComparatorMessage(comparator));
48
- }
49
51
  if (this.branches.leftBound) {
50
52
  return this.error(writeMultipleLeftBoundsMessage(this.branches.leftBound.limit, this.branches.leftBound.comparator, limit, invertedComparator));
51
53
  }
@@ -56,24 +58,22 @@ export class DynamicState {
56
58
  }
57
59
  finalizeBranches() {
58
60
  this.assertRangeUnset();
59
- if (this.branches["|"]) {
61
+ if (this.branches.union) {
60
62
  this.pushRootToBranch("|");
61
- this.root = this.branches["|"];
63
+ this.root = this.branches.union;
62
64
  }
63
- else if (this.branches["&"]) {
65
+ else if (this.branches.intersection) {
64
66
  this.pushRootToBranch("&");
65
- this.root = this.branches["&"];
67
+ this.root = this.branches.intersection;
66
68
  }
67
- else {
69
+ else
68
70
  this.applyPrefixes();
69
- }
70
71
  }
71
72
  finalizeGroup() {
72
73
  this.finalizeBranches();
73
74
  const topBranchState = this.groups.pop();
74
- if (!topBranchState) {
75
+ if (!topBranchState)
75
76
  return this.error(writeUnmatchedGroupCloseMessage(this.scanner.unscanned));
76
- }
77
77
  this.branches = topBranchState;
78
78
  }
79
79
  addPrefix(prefix) {
@@ -83,8 +83,8 @@ export class DynamicState {
83
83
  while (this.branches.prefixes.length) {
84
84
  const lastPrefix = this.branches.prefixes.pop();
85
85
  this.root =
86
- lastPrefix === "keyof"
87
- ? this.root.keyof()
86
+ lastPrefix === "keyof" ?
87
+ this.root.keyof()
88
88
  : throwInternalError(`Unexpected prefix '${lastPrefix}'`);
89
89
  }
90
90
  }
@@ -92,11 +92,12 @@ export class DynamicState {
92
92
  this.assertRangeUnset();
93
93
  this.applyPrefixes();
94
94
  const root = this.root;
95
- this.branches["&"] = this.branches["&"]?.and(root) ?? root;
95
+ this.branches.intersection = this.branches.intersection?.and(root) ?? root;
96
96
  if (token === "|") {
97
- this.branches["|"] =
98
- this.branches["|"]?.or(this.branches["&"]) ?? this.branches["&"];
99
- delete this.branches["&"];
97
+ this.branches.union =
98
+ this.branches.union?.or(this.branches.intersection) ??
99
+ this.branches.intersection;
100
+ this.branches.intersection = null;
100
101
  }
101
102
  this.root = undefined;
102
103
  }
@@ -117,17 +118,21 @@ export class DynamicState {
117
118
  reduceGroupOpen() {
118
119
  this.groups.push(this.branches);
119
120
  this.branches = {
120
- prefixes: []
121
+ prefixes: [],
122
+ leftBound: null,
123
+ union: null,
124
+ intersection: null
121
125
  };
122
126
  }
123
127
  previousOperator() {
124
128
  return (this.branches.leftBound?.comparator ??
125
129
  this.branches.prefixes.at(-1) ??
126
- (this.branches["&"] ? "&" : this.branches["|"] ? "|" : undefined));
130
+ (this.branches.intersection ? "&"
131
+ : this.branches.union ? "|"
132
+ : undefined));
127
133
  }
128
134
  shiftedByOne() {
129
135
  this.scanner.shift();
130
136
  return this;
131
137
  }
132
138
  }
133
- //# sourceMappingURL=dynamic.js.map