arktype 2.0.0-rc.8 → 2.0.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 (159) hide show
  1. package/out/attributes.d.ts +94 -0
  2. package/out/attributes.js +3 -0
  3. package/out/config.d.ts +1 -1
  4. package/out/generic.d.ts +18 -18
  5. package/out/generic.js +2 -2
  6. package/out/index.d.ts +4 -2
  7. package/out/index.js +3 -2
  8. package/out/keywords/{constructors/Array.d.ts → Array.d.ts} +2 -2
  9. package/out/keywords/{constructors/Array.js → Array.js} +2 -2
  10. package/out/keywords/{constructors/FormData.d.ts → FormData.d.ts} +2 -2
  11. package/out/keywords/{constructors/FormData.js → FormData.js} +2 -2
  12. package/out/keywords/{constructors/TypedArray.d.ts → TypedArray.d.ts} +1 -1
  13. package/out/keywords/{constructors/TypedArray.js → TypedArray.js} +2 -2
  14. package/out/keywords/builtins.js +2 -2
  15. package/out/keywords/{constructors/constructors.d.ts → constructors.d.ts} +1 -1
  16. package/out/keywords/{constructors/constructors.js → constructors.js} +2 -2
  17. package/out/keywords/{ark.d.ts → keywords.d.ts} +16 -15
  18. package/out/keywords/keywords.js +30 -0
  19. package/out/keywords/number.d.ts +23 -0
  20. package/out/keywords/{number/epoch.js → number.js} +22 -1
  21. package/out/keywords/string.d.ts +327 -0
  22. package/out/keywords/string.js +398 -0
  23. package/out/keywords/ts.d.ts +31 -4
  24. package/out/keywords/ts.js +21 -8
  25. package/out/methods/array.d.ts +6 -7
  26. package/out/methods/base.d.ts +461 -34
  27. package/out/methods/date.d.ts +5 -6
  28. package/out/methods/instantiate.d.ts +5 -5
  29. package/out/methods/morph.d.ts +6 -7
  30. package/out/methods/number.d.ts +8 -9
  31. package/out/methods/object.d.ts +80 -31
  32. package/out/methods/string.d.ts +9 -10
  33. package/out/parser/{semantic → ast}/bounds.d.ts +3 -3
  34. package/out/parser/{semantic → ast}/default.d.ts +4 -4
  35. package/out/parser/ast/generic.d.ts +17 -0
  36. package/out/parser/ast/infer.d.ts +25 -0
  37. package/out/parser/{semantic → ast}/utils.d.ts +1 -1
  38. package/out/parser/ast/validate.d.ts +30 -0
  39. package/out/parser/ast/validate.js +3 -0
  40. package/out/parser/definition.d.ts +18 -9
  41. package/out/parser/definition.js +38 -3
  42. package/out/parser/objectLiteral.d.ts +40 -40
  43. package/out/parser/objectLiteral.js +83 -67
  44. package/out/parser/property.d.ts +36 -0
  45. package/out/parser/property.js +16 -0
  46. package/out/parser/{string/reduce → reduce}/dynamic.d.ts +8 -9
  47. package/out/parser/{string/reduce → reduce}/shared.d.ts +1 -1
  48. package/out/parser/{string/reduce → reduce}/static.d.ts +4 -4
  49. package/out/parser/{string/shift → shift}/operand/date.d.ts +1 -1
  50. package/out/parser/{string/shift → shift}/operand/enclosed.d.ts +4 -5
  51. package/out/parser/shift/operand/operand.d.ts +9 -0
  52. package/out/parser/{string/shift → shift}/operand/operand.js +2 -2
  53. package/out/parser/{string/shift → shift}/operand/unenclosed.d.ts +15 -7
  54. package/out/parser/{string/shift → shift}/operand/unenclosed.js +6 -5
  55. package/out/parser/{string/shift → shift}/operator/bounds.d.ts +7 -7
  56. package/out/parser/{string/shift → shift}/operator/bounds.js +5 -4
  57. package/out/parser/shift/operator/brand.d.ts +6 -0
  58. package/out/parser/shift/operator/brand.js +5 -0
  59. package/out/parser/{string/shift → shift}/operator/default.d.ts +3 -2
  60. package/out/parser/{string/shift → shift}/operator/default.js +4 -1
  61. package/out/parser/{string/shift → shift}/operator/divisor.d.ts +2 -2
  62. package/out/parser/shift/operator/operator.d.ts +14 -0
  63. package/out/parser/{string/shift → shift}/operator/operator.js +7 -5
  64. package/out/parser/shift/scanner.d.ts +47 -0
  65. package/out/parser/shift/scanner.js +43 -0
  66. package/out/parser/{string/string.d.ts → string.d.ts} +7 -6
  67. package/out/parser/{string/string.js → string.js} +18 -1
  68. package/out/parser/tupleExpressions.d.ts +48 -0
  69. package/out/parser/tupleExpressions.js +67 -0
  70. package/out/parser/tupleLiteral.d.ts +95 -0
  71. package/out/parser/tupleLiteral.js +122 -0
  72. package/out/scope.d.ts +19 -14
  73. package/out/scope.js +25 -41
  74. package/out/type.d.ts +67 -9
  75. package/out/type.js +4 -3
  76. package/package.json +8 -8
  77. package/out/intersect.d.ts +0 -26
  78. package/out/keys.d.ts +0 -18
  79. package/out/keys.js +0 -1
  80. package/out/keywords/ark.js +0 -26
  81. package/out/keywords/ast.d.ts +0 -198
  82. package/out/keywords/ast.js +0 -2
  83. package/out/keywords/constructors/Date.d.ts +0 -31
  84. package/out/keywords/constructors/Date.js +0 -1
  85. package/out/keywords/number/epoch.d.ts +0 -9
  86. package/out/keywords/number/integer.d.ts +0 -3
  87. package/out/keywords/number/integer.js +0 -5
  88. package/out/keywords/number/number.d.ts +0 -53
  89. package/out/keywords/number/number.js +0 -21
  90. package/out/keywords/string/alpha.d.ts +0 -7
  91. package/out/keywords/string/alpha.js +0 -2
  92. package/out/keywords/string/alphanumeric.d.ts +0 -7
  93. package/out/keywords/string/alphanumeric.js +0 -2
  94. package/out/keywords/string/capitalize.d.ts +0 -15
  95. package/out/keywords/string/capitalize.js +0 -12
  96. package/out/keywords/string/creditCard.d.ts +0 -8
  97. package/out/keywords/string/creditCard.js +0 -37
  98. package/out/keywords/string/date.d.ts +0 -55
  99. package/out/keywords/string/date.js +0 -98
  100. package/out/keywords/string/digits.d.ts +0 -7
  101. package/out/keywords/string/digits.js +0 -2
  102. package/out/keywords/string/email.d.ts +0 -7
  103. package/out/keywords/string/email.js +0 -4
  104. package/out/keywords/string/integer.d.ts +0 -16
  105. package/out/keywords/string/integer.js +0 -16
  106. package/out/keywords/string/ip.d.ts +0 -20
  107. package/out/keywords/string/ip.js +0 -23
  108. package/out/keywords/string/json.d.ts +0 -16
  109. package/out/keywords/string/json.js +0 -53
  110. package/out/keywords/string/lower.d.ts +0 -15
  111. package/out/keywords/string/lower.js +0 -12
  112. package/out/keywords/string/normalize.d.ts +0 -88
  113. package/out/keywords/string/normalize.js +0 -43
  114. package/out/keywords/string/numeric.d.ts +0 -15
  115. package/out/keywords/string/numeric.js +0 -13
  116. package/out/keywords/string/semver.d.ts +0 -7
  117. package/out/keywords/string/semver.js +0 -4
  118. package/out/keywords/string/string.d.ts +0 -108
  119. package/out/keywords/string/string.js +0 -41
  120. package/out/keywords/string/trim.d.ts +0 -15
  121. package/out/keywords/string/trim.js +0 -14
  122. package/out/keywords/string/upper.d.ts +0 -15
  123. package/out/keywords/string/upper.js +0 -12
  124. package/out/keywords/string/url.d.ts +0 -15
  125. package/out/keywords/string/url.js +0 -37
  126. package/out/keywords/string/utils.d.ts +0 -2
  127. package/out/keywords/string/utils.js +0 -10
  128. package/out/keywords/string/uuid.d.ts +0 -42
  129. package/out/keywords/string/uuid.js +0 -19
  130. package/out/keywords/utils.d.ts +0 -1
  131. package/out/keywords/utils.js +0 -2
  132. package/out/methods/validator.d.ts +0 -11
  133. package/out/methods/validator.js +0 -1
  134. package/out/parser/semantic/infer.d.ts +0 -41
  135. package/out/parser/semantic/validate.d.ts +0 -26
  136. package/out/parser/semantic/validate.js +0 -1
  137. package/out/parser/string/shift/operand/operand.d.ts +0 -9
  138. package/out/parser/string/shift/operator/operator.d.ts +0 -13
  139. package/out/parser/string/shift/scanner.d.ts +0 -71
  140. package/out/parser/string/shift/scanner.js +0 -102
  141. package/out/parser/tuple.d.ts +0 -144
  142. package/out/parser/tuple.js +0 -164
  143. /package/out/parser/{semantic → ast}/bounds.js +0 -0
  144. /package/out/parser/{semantic → ast}/default.js +0 -0
  145. /package/out/parser/{semantic → ast}/divisor.d.ts +0 -0
  146. /package/out/parser/{semantic → ast}/divisor.js +0 -0
  147. /package/out/{intersect.js → parser/ast/generic.js} +0 -0
  148. /package/out/parser/{semantic → ast}/infer.js +0 -0
  149. /package/out/parser/{semantic → ast}/keyof.d.ts +0 -0
  150. /package/out/parser/{semantic → ast}/keyof.js +0 -0
  151. /package/out/parser/{semantic → ast}/utils.js +0 -0
  152. /package/out/parser/{string/reduce → reduce}/dynamic.js +0 -0
  153. /package/out/parser/{string/reduce → reduce}/shared.js +0 -0
  154. /package/out/parser/{string/reduce → reduce}/static.js +0 -0
  155. /package/out/parser/{string/shift → shift}/operand/date.js +0 -0
  156. /package/out/parser/{string/shift → shift}/operand/enclosed.js +0 -0
  157. /package/out/parser/{string/shift → shift}/operand/genericArgs.d.ts +0 -0
  158. /package/out/parser/{string/shift → shift}/operand/genericArgs.js +0 -0
  159. /package/out/parser/{string/shift → shift}/operator/divisor.js +0 -0
@@ -1,102 +0,0 @@
1
- import { escapeToken, isKeyOf, whiteSpaceTokens } from "@ark/util";
2
- export class Scanner {
3
- chars;
4
- i;
5
- constructor(def) {
6
- this.chars = [...def];
7
- this.i = 0;
8
- }
9
- /** Get lookahead and advance scanner by one */
10
- shift() {
11
- return (this.chars[this.i++] ?? "");
12
- }
13
- get lookahead() {
14
- return (this.chars[this.i] ?? "");
15
- }
16
- get nextLookahead() {
17
- return this.chars[this.i + 1] ?? "";
18
- }
19
- get length() {
20
- return this.chars.length;
21
- }
22
- shiftUntil(condition) {
23
- let shifted = "";
24
- while (this.lookahead) {
25
- if (condition(this, shifted)) {
26
- if (shifted[shifted.length - 1] === escapeToken)
27
- shifted = shifted.slice(0, -1);
28
- else
29
- break;
30
- }
31
- shifted += this.shift();
32
- }
33
- return shifted;
34
- }
35
- shiftUntilNextTerminator() {
36
- this.shiftUntilNonWhitespace();
37
- return this.shiftUntil(Scanner.lookaheadIsTerminator);
38
- }
39
- shiftUntilNonWhitespace() {
40
- return this.shiftUntil(Scanner.lookaheadIsNotWhitespace);
41
- }
42
- jumpToIndex(i) {
43
- this.i = i < 0 ? this.length + i : i;
44
- }
45
- jumpForward(count) {
46
- this.i += count;
47
- }
48
- get location() {
49
- return this.i;
50
- }
51
- get unscanned() {
52
- return this.chars.slice(this.i, this.length).join("");
53
- }
54
- get scanned() {
55
- return this.chars.slice(0, this.i).join("");
56
- }
57
- sliceChars(start, end) {
58
- return this.chars.slice(start, end).join("");
59
- }
60
- lookaheadIs(char) {
61
- return this.lookahead === char;
62
- }
63
- lookaheadIsIn(tokens) {
64
- return this.lookahead in tokens;
65
- }
66
- static terminatingChars = {
67
- "<": true,
68
- ">": true,
69
- "=": true,
70
- "|": true,
71
- "&": true,
72
- ")": true,
73
- "[": true,
74
- "%": true,
75
- ",": true,
76
- ":": true,
77
- "?": true,
78
- ...whiteSpaceTokens
79
- };
80
- static finalizingLookaheads = {
81
- ">": true,
82
- ",": true,
83
- "": true,
84
- "=": true,
85
- "?": true
86
- };
87
- static lookaheadIsTerminator = (scanner) => scanner.lookahead in this.terminatingChars;
88
- static lookaheadIsNotWhitespace = (scanner) => !(scanner.lookahead in whiteSpaceTokens);
89
- static lookaheadIsFinalizing = (lookahead, unscanned) => lookahead === ">" ?
90
- unscanned[0] === "=" ?
91
- // >== would only occur in an expression like Array<number>==5
92
- // otherwise, >= would only occur as part of a bound like number>=5
93
- unscanned[1] === "="
94
- // if > is the end of a generic instantiation, the next token will be an operator or the end of the string
95
- : unscanned.trimStart() === "" ||
96
- isKeyOf(unscanned.trimStart()[0], Scanner.terminatingChars)
97
- // "=" is a finalizer on its own (representing a default value),
98
- // but not with a second "=" (an equality comparator)
99
- : lookahead === "=" ? unscanned[0] !== "="
100
- // "," and "?" are unambiguously finalizers
101
- : lookahead === "," || lookahead === "?";
102
- }
@@ -1,144 +0,0 @@
1
- import { type BaseParseContext, type BaseRoot, type MetaSchema, type Morph, type Predicate } from "@ark/schema";
2
- import { type anyOrNever, type array, type BuiltinObjectKind, type conform, type Constructor, type Domain, type ErrorMessage, type show } from "@ark/util";
3
- import type { inferIntersection } from "../intersect.ts";
4
- import type { type } from "../keywords/ark.ts";
5
- import type { applyConstraint, Default, distill, inferMorphOut, inferPredicate, Optional, OptionalAst, Out } from "../keywords/ast.ts";
6
- import type { inferDefinition, validateDefinition } from "./definition.ts";
7
- import type { InfixOperator, PostfixExpression } from "./semantic/infer.ts";
8
- import { writeMissingRightOperandMessage } from "./string/shift/operand/unenclosed.ts";
9
- import type { BaseCompletions } from "./string/string.ts";
10
- export declare const parseTuple: (def: array, ctx: BaseParseContext) => BaseRoot;
11
- export declare const parseTupleLiteral: (def: array, ctx: BaseParseContext) => BaseRoot;
12
- type InfixExpression = readonly [unknown, InfixOperator, ...unknown[]];
13
- export type validateTuple<def extends array, $, args> = def extends IndexZeroExpression ? validatePrefixExpression<def, $, args> : def extends PostfixExpression ? validatePostfixExpression<def, $, args> : def extends InfixExpression ? validateInfixExpression<def, $, args> : def extends (readonly ["", ...unknown[]] | readonly [unknown, "", ...unknown[]]) ? readonly [
14
- def[0] extends "" ? BaseCompletions<$, args, IndexZeroOperator | "..."> : def[0],
15
- def[1] extends "" ? BaseCompletions<$, args, IndexOneOperator | "..."> : def[1]
16
- ] : validateTupleLiteral<def, $, args>;
17
- export type validateTupleLiteral<def extends array, $, args> = parseSequence<def, $, args> extends infer s extends SequenceParseState ? Readonly<s["validated"]> : never;
18
- type inferTupleLiteral<def extends array, $, args> = parseSequence<def, $, args> extends infer s extends SequenceParseState ? s["inferred"] : never;
19
- type SequenceParseState = {
20
- unscanned: array;
21
- inferred: array;
22
- validated: array;
23
- includesOptional: boolean;
24
- };
25
- type parseSequence<def extends array, $, args> = parseNextElement<{
26
- unscanned: def;
27
- inferred: [];
28
- validated: [];
29
- includesOptional: false;
30
- }, $, args>;
31
- type PreparsedElement = {
32
- head: unknown;
33
- tail: array;
34
- inferred: unknown;
35
- optional: boolean;
36
- spread: boolean;
37
- };
38
- declare namespace PreparsedElement {
39
- type from<result extends PreparsedElement> = result;
40
- }
41
- type preparseNextElement<s extends SequenceParseState, $, args> = s["unscanned"] extends readonly ["...", infer head, ...infer tail] ? inferDefinition<head, $, args> extends infer t ? [
42
- t
43
- ] extends [anyOrNever] ? PreparsedElement.from<{
44
- head: head;
45
- tail: tail;
46
- inferred: t;
47
- optional: false;
48
- spread: true;
49
- }> : [t] extends [OptionalAst<infer base>] ? PreparsedElement.from<{
50
- head: head;
51
- tail: tail;
52
- inferred: base;
53
- optional: true;
54
- spread: true;
55
- }> : PreparsedElement.from<{
56
- head: head;
57
- tail: tail;
58
- inferred: t;
59
- optional: false;
60
- spread: true;
61
- }> : never : s["unscanned"] extends readonly [infer head, ...infer tail] ? inferDefinition<head, $, args> extends infer t ? [
62
- t
63
- ] extends [anyOrNever] ? PreparsedElement.from<{
64
- head: head;
65
- tail: tail;
66
- inferred: t;
67
- optional: false;
68
- spread: false;
69
- }> : [t] extends [OptionalAst<infer base>] ? PreparsedElement.from<{
70
- head: head;
71
- tail: tail;
72
- inferred: base;
73
- optional: true;
74
- spread: false;
75
- }> : PreparsedElement.from<{
76
- head: head;
77
- tail: tail;
78
- inferred: t;
79
- optional: false;
80
- spread: false;
81
- }> : never : null;
82
- type parseNextElement<s extends SequenceParseState, $, args> = preparseNextElement<s, $, args> extends infer next extends PreparsedElement ? parseNextElement<{
83
- unscanned: next["tail"];
84
- inferred: next["spread"] extends true ? [
85
- ...s["inferred"],
86
- ...conform<next["inferred"], array>
87
- ] : next["optional"] extends true ? [...s["inferred"], next["inferred"]?] : [...s["inferred"], next["inferred"]];
88
- validated: [
89
- ...s["validated"],
90
- ...(next["spread"] extends true ? [
91
- next["inferred"] extends infer spreadOperand extends array ? [
92
- number,
93
- number
94
- ] extends ([
95
- s["inferred"]["length"],
96
- spreadOperand["length"]
97
- ]) ? ErrorMessage<multipleVariadicMessage> : "..." : ErrorMessage<writeNonArraySpreadMessage<next["head"]>>
98
- ] : []),
99
- next["optional"] extends true ? next["spread"] extends true ? ErrorMessage<spreadOptionalMessage> : number extends s["inferred"]["length"] ? ErrorMessage<optionalPostVariadicMessage> : validateDefinition<next["head"], $, args> : [s["includesOptional"], next["spread"]] extends [true, false] ? ErrorMessage<requiredPostOptionalMessage> : validateDefinition<next["head"], $, args>
100
- ];
101
- includesOptional: s["includesOptional"] | next["optional"] extends (false) ? false : true;
102
- }, $, args> : s;
103
- export declare const writeNonArraySpreadMessage: <operand extends string>(operand: operand) => writeNonArraySpreadMessage<operand>;
104
- type writeNonArraySpreadMessage<operand> = `Spread element must be an array${operand extends string ? ` (was ${operand})` : ""}`;
105
- export declare const multipleVariadicMesage = "A tuple may have at most one variadic element";
106
- type multipleVariadicMessage = typeof multipleVariadicMesage;
107
- export declare const requiredPostOptionalMessage = "A required element may not follow an optional element";
108
- type requiredPostOptionalMessage = typeof requiredPostOptionalMessage;
109
- export declare const optionalPostVariadicMessage = "An optional element may not follow a variadic element";
110
- type optionalPostVariadicMessage = typeof optionalPostVariadicMessage;
111
- export declare const spreadOptionalMessage = "A spread element cannot be optional";
112
- type spreadOptionalMessage = typeof optionalPostVariadicMessage;
113
- export type inferTuple<def extends array, $, args> = def extends TupleExpression ? inferTupleExpression<def, $, args> : inferTupleLiteral<def, $, args>;
114
- export type inferTupleExpression<def extends TupleExpression, $, args> = def[1] extends "[]" ? inferDefinition<def[0], $, args>[] : def[1] extends "&" ? inferIntersection<inferDefinition<def[0], $, args>, inferDefinition<def[2], $, args>> : def[1] extends "|" ? inferDefinition<def[0], $, args> | inferDefinition<def[2], $, args> : def[1] extends ":" ? inferPredicate<inferDefinition<def[0], $, args>, def[2]> : def[1] extends "=>" ? parseMorph<def[0], def[2], $, args> : def[1] extends "@" ? inferDefinition<def[0], $, args> : def[1] extends "=" ? (In?: inferDefinition<def[0], $, args>) => Default<def[2]> : def[1] extends "?" ? applyConstraint<inferDefinition<def[0], $, args>, Optional> : def extends readonly ["===", ...infer values] ? values[number] : def extends (readonly ["instanceof", ...infer constructors extends Constructor[]]) ? InstanceType<constructors[number]> : def[0] extends "keyof" ? inferKeyOfExpression<def[1], $, args> : never;
115
- export type validatePrefixExpression<def extends IndexZeroExpression, $, args> = def["length"] extends 1 ? readonly [writeMissingRightOperandMessage<def[0]>] : def[0] extends "keyof" ? readonly [def[0], validateDefinition<def[1], $, args>] : def[0] extends "===" ? readonly [def[0], ...unknown[]] : def[0] extends "instanceof" ? readonly [def[0], ...Constructor[]] : never;
116
- export type validatePostfixExpression<def extends PostfixExpression, $, args> = conform<def, readonly [validateDefinition<def[0], $, args>, "[]" | "?"]>;
117
- export type validateInfixExpression<def extends InfixExpression, $, args> = def["length"] extends 2 ? readonly [def[0], writeMissingRightOperandMessage<def[1]>] : readonly [
118
- validateDefinition<def[0], $, args>,
119
- def[1],
120
- def[1] extends "|" ? validateDefinition<def[2], $, args> : def[1] extends "&" ? validateDefinition<def[2], $, args> : def[1] extends ":" ? Predicate<type.infer.Out<def[0], $, args>> : def[1] extends "=>" ? Morph<type.infer.Out<def[0], $, args>> : def[1] extends "@" ? MetaSchema : def[1] extends "=" ? type.infer.Out<def[0], $, args> : validateDefinition<def[2], $, args>
121
- ];
122
- export type UnparsedTupleExpressionInput = {
123
- instanceof: Constructor;
124
- "===": unknown;
125
- };
126
- export type UnparsedTupleOperator = show<keyof UnparsedTupleExpressionInput>;
127
- export declare const parseKeyOfTuple: PrefixParser<"keyof">;
128
- export type inferKeyOfExpression<operandDef, $, args> = show<keyof inferDefinition<operandDef, $, args>>;
129
- export type IndexOneParser<token extends IndexOneOperator> = (def: IndexOneExpression<token>, ctx: BaseParseContext) => BaseRoot;
130
- export type PrefixParser<token extends IndexZeroOperator> = (def: IndexZeroExpression<token>, ctx: BaseParseContext) => BaseRoot;
131
- export type TupleExpression = IndexZeroExpression | IndexOneExpression;
132
- export type TupleExpressionOperator = IndexZeroOperator | IndexOneOperator;
133
- export type IndexOneOperator = TuplePostfixOperator | TupleInfixOperator;
134
- export type TuplePostfixOperator = "[]" | "?";
135
- export type TupleInfixOperator = "&" | "|" | "=>" | ":" | "@" | "=";
136
- export type IndexOneExpression<token extends IndexOneOperator = IndexOneOperator> = readonly [unknown, token, ...unknown[]];
137
- export declare const parseMorphTuple: IndexOneParser<"=>">;
138
- export declare const writeMalformedFunctionalExpressionMessage: (operator: ":" | "=>", value: unknown) => string;
139
- export type parseMorph<inDef, morph, $, args> = morph extends Morph ? inferMorphOut<morph> extends infer out ? (In: distill.brandable.In<inferDefinition<inDef, $, args>>) => Out<out> : never : never;
140
- export declare const parseNarrowTuple: IndexOneParser<":">;
141
- export type IndexZeroOperator = "keyof" | "instanceof" | "===";
142
- export type IndexZeroExpression<token extends IndexZeroOperator = IndexZeroOperator> = readonly [token, ...unknown[]];
143
- export declare const writeInvalidConstructorMessage: <actual extends Domain | BuiltinObjectKind>(actual: actual) => string;
144
- export {};
@@ -1,164 +0,0 @@
1
- import { $ark, Disjoint, intersectNodesRoot, makeRootAndArrayPropertiesMutable } from "@ark/schema";
2
- import { append, isEmptyObject, objectKindOrDomainOf, throwParseError } from "@ark/util";
3
- import { writeMissingRightOperandMessage } from "./string/shift/operand/unenclosed.js";
4
- export const parseTuple = (def, ctx) => maybeParseTupleExpression(def, ctx) ?? parseTupleLiteral(def, ctx);
5
- export const parseTupleLiteral = (def, ctx) => {
6
- let sequences = [{}];
7
- let i = 0;
8
- while (i < def.length) {
9
- let spread = false;
10
- if (def[i] === "..." && i < def.length - 1) {
11
- spread = true;
12
- i++;
13
- }
14
- const element = ctx.$.parseOwnDefinitionFormat(def[i], ctx);
15
- i++;
16
- if (spread) {
17
- if (!element.extends($ark.intrinsic.Array))
18
- return throwParseError(writeNonArraySpreadMessage(element.expression));
19
- // a spread must be distributed over branches e.g.:
20
- // def: [string, ...(number[] | [true, false])]
21
- // nodes: [string, ...number[]] | [string, true, false]
22
- sequences = sequences.flatMap(base =>
23
- // since appendElement mutates base, we have to shallow-ish clone it for each branch
24
- element.distribute(branch => appendSpreadBranch(makeRootAndArrayPropertiesMutable(base), branch)));
25
- }
26
- else {
27
- sequences = sequences.map(base => appendElement(base, element.meta.optional ? "optional" : "required", element));
28
- }
29
- }
30
- return ctx.$.parseSchema(sequences.map(sequence => isEmptyObject(sequence) ?
31
- {
32
- proto: Array,
33
- exactLength: 0
34
- }
35
- : {
36
- proto: Array,
37
- sequence
38
- }));
39
- };
40
- const appendElement = (base, kind, element) => {
41
- switch (kind) {
42
- case "required":
43
- if (base.optionals)
44
- // e.g. [string?, number]
45
- return throwParseError(requiredPostOptionalMessage);
46
- if (base.variadic) {
47
- // e.g. [...string[], number]
48
- base.postfix = append(base.postfix, element);
49
- }
50
- else {
51
- // e.g. [string, number]
52
- base.prefix = append(base.prefix, element);
53
- }
54
- return base;
55
- case "optional":
56
- if (base.variadic)
57
- // e.g. [...string[], number?]
58
- return throwParseError(optionalPostVariadicMessage);
59
- // e.g. [string, number?]
60
- base.optionals = append(base.optionals, element);
61
- return base;
62
- case "variadic":
63
- // e.g. [...string[], number, ...string[]]
64
- if (base.postfix)
65
- throwParseError(multipleVariadicMesage);
66
- if (base.variadic) {
67
- if (!base.variadic.equals(element)) {
68
- // e.g. [...string[], ...number[]]
69
- throwParseError(multipleVariadicMesage);
70
- }
71
- // e.g. [...string[], ...string[]]
72
- // do nothing, second spread doesn't change the type
73
- }
74
- else {
75
- // e.g. [string, ...number[]]
76
- base.variadic = element.internal;
77
- }
78
- return base;
79
- }
80
- };
81
- const appendSpreadBranch = (base, branch) => {
82
- const spread = branch.firstReferenceOfKind("sequence");
83
- if (!spread) {
84
- // the only array with no sequence reference is unknown[]
85
- return appendElement(base, "variadic", $ark.intrinsic.unknown);
86
- }
87
- spread.prefix?.forEach(node => appendElement(base, "required", node));
88
- spread.optionals?.forEach(node => appendElement(base, "optional", node));
89
- if (spread.variadic)
90
- appendElement(base, "variadic", spread.variadic);
91
- spread.postfix?.forEach(node => appendElement(base, "required", node));
92
- return base;
93
- };
94
- const maybeParseTupleExpression = (def, ctx) => {
95
- const tupleExpressionResult = isIndexZeroExpression(def) ? prefixParsers[def[0]](def, ctx)
96
- : isIndexOneExpression(def) ? indexOneParsers[def[1]](def, ctx)
97
- : undefined;
98
- return tupleExpressionResult;
99
- };
100
- export const writeNonArraySpreadMessage = (operand) => `Spread element must be an array (was ${operand})`;
101
- export const multipleVariadicMesage = "A tuple may have at most one variadic element";
102
- export const requiredPostOptionalMessage = "A required element may not follow an optional element";
103
- export const optionalPostVariadicMessage = "An optional element may not follow a variadic element";
104
- export const spreadOptionalMessage = "A spread element cannot be optional";
105
- export const parseKeyOfTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[1], ctx).keyof();
106
- const parseBranchTuple = (def, ctx) => {
107
- if (def[2] === undefined)
108
- return throwParseError(writeMissingRightOperandMessage(def[1], ""));
109
- const l = ctx.$.parseOwnDefinitionFormat(def[0], ctx);
110
- const r = ctx.$.parseOwnDefinitionFormat(def[2], ctx);
111
- if (def[1] === "|")
112
- return ctx.$.node("union", { branches: [l, r] });
113
- const result = intersectNodesRoot(l, r, ctx.$);
114
- if (result instanceof Disjoint)
115
- return result.throw();
116
- return result;
117
- };
118
- const parseArrayTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).array();
119
- const isIndexOneExpression = (def) => indexOneParsers[def[1]] !== undefined;
120
- export const parseMorphTuple = (def, ctx) => {
121
- if (typeof def[2] !== "function") {
122
- return throwParseError(writeMalformedFunctionalExpressionMessage("=>", def[2]));
123
- }
124
- return ctx.$.parseOwnDefinitionFormat(def[0], ctx).pipe(def[2]);
125
- };
126
- export const writeMalformedFunctionalExpressionMessage = (operator, value) => `${operator === ":" ? "Narrow" : "Morph"} expression requires a function following '${operator}' (was ${typeof value})`;
127
- export const parseNarrowTuple = (def, ctx) => {
128
- if (typeof def[2] !== "function") {
129
- return throwParseError(writeMalformedFunctionalExpressionMessage(":", def[2]));
130
- }
131
- return ctx.$.parseOwnDefinitionFormat(def[0], ctx).constrain("predicate", def[2]);
132
- };
133
- const parseAttributeTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).configureShallowDescendants(def[2]);
134
- const parseDefaultTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).default(def[2]);
135
- const parseOptionalTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).optional();
136
- const indexOneParsers = {
137
- "[]": parseArrayTuple,
138
- "?": parseOptionalTuple,
139
- "|": parseBranchTuple,
140
- "&": parseBranchTuple,
141
- ":": parseNarrowTuple,
142
- "=>": parseMorphTuple,
143
- "@": parseAttributeTuple,
144
- "=": parseDefaultTuple
145
- };
146
- const prefixParsers = {
147
- keyof: parseKeyOfTuple,
148
- instanceof: (def, ctx) => {
149
- if (typeof def[1] !== "function") {
150
- return throwParseError(writeInvalidConstructorMessage(objectKindOrDomainOf(def[1])));
151
- }
152
- const branches = def
153
- .slice(1)
154
- .map(ctor => typeof ctor === "function" ?
155
- ctx.$.node("proto", { proto: ctor })
156
- : throwParseError(writeInvalidConstructorMessage(objectKindOrDomainOf(ctor))));
157
- return branches.length === 1 ?
158
- branches[0]
159
- : ctx.$.node("union", { branches });
160
- },
161
- "===": (def, ctx) => ctx.$.units(def.slice(1))
162
- };
163
- const isIndexZeroExpression = (def) => prefixParsers[def[0]] !== undefined;
164
- export const writeInvalidConstructorMessage = (actual) => `Expected a constructor following 'instanceof' operator (was ${actual})`;
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes