arktype 2.0.0-dev.2 → 2.0.0-dev.20

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,113 +1,133 @@
1
- import { keywords, schema } from "@arktype/schema";
2
- import { isArray, objectKindOrDomainOf, printable, throwParseError } from "@arktype/util";
3
- import { writeUnsatisfiableExpressionError } from "./semantic/validate.js";
4
- import { configureShallowDescendants, parseEntry } from "./shared.js";
1
+ import { jsObjects, makeRootAndArrayPropertiesMutable, tsKeywords } from "@arktype/schema";
2
+ import { append, objectKindOrDomainOf, throwParseError } from "@arktype/util";
5
3
  import { writeMissingRightOperandMessage } from "./string/shift/operand/unenclosed.js";
6
4
  export const parseTuple = (def, ctx) => maybeParseTupleExpression(def, ctx) ?? parseTupleLiteral(def, ctx);
7
5
  export const parseTupleLiteral = (def, ctx) => {
8
- const props = [];
9
- let variadicIndex;
10
- for (let i = 0; i < def.length; i++) {
11
- let elementDef = def[i];
12
- ctx.path.push(`${i}`);
13
- if (typeof elementDef === "string" && elementDef.startsWith("...")) {
14
- elementDef = elementDef.slice(3);
15
- if (variadicIndex !== undefined) {
16
- return throwParseError(multipleVariadicMesage);
17
- }
18
- variadicIndex = i;
6
+ let sequences = [{}];
7
+ let i = 0;
8
+ while (i < def.length) {
9
+ let spread = false;
10
+ let optional = false;
11
+ if (def[i] === "..." && i < def.length - 1) {
12
+ spread = true;
13
+ i++;
19
14
  }
20
- else if (isArray(elementDef) &&
21
- elementDef.length === 2 &&
22
- elementDef[0] === "...") {
23
- elementDef = elementDef[1];
24
- if (variadicIndex !== undefined) {
25
- return throwParseError(multipleVariadicMesage);
26
- }
27
- variadicIndex = i;
15
+ const element = ctx.$.parse(def[i], ctx);
16
+ i++;
17
+ if (def[i] === "?") {
18
+ if (spread)
19
+ return throwParseError(spreadOptionalMessage);
20
+ optional = true;
21
+ i++;
28
22
  }
29
- const parsedEntry = parseEntry([`${i}`, elementDef]);
30
- const value = ctx.scope.parse(parsedEntry.innerValue, ctx);
31
- if (variadicIndex === i) {
32
- if (!value.extends(keywords.Array)) {
33
- return throwParseError(writeNonArrayRestMessage(elementDef));
34
- }
35
- // TODO: Fix builtins.arrayIndexTypeNode()
36
- const elementType = value.getPath();
37
- // TODO: first variadic i
38
- props.push({ key: keywords.number, value: elementType });
23
+ if (spread) {
24
+ if (!element.extends(jsObjects.Array))
25
+ return throwParseError(writeNonArraySpreadMessage(element.expression));
26
+ // a spread must be distributed over branches e.g.:
27
+ // def: [string, ...(number[] | [true, false])]
28
+ // nodes: [string, ...number[]] | [string, true, false]
29
+ sequences = sequences.flatMap(base =>
30
+ // since appendElement mutates base, we have to shallow-ish clone it for each branch
31
+ element.branches.map(branch => appendSpreadBranch(makeRootAndArrayPropertiesMutable(base), branch)));
39
32
  }
40
33
  else {
41
- props.push({
42
- key: {
43
- name: `${i}`,
44
- prerequisite: false,
45
- optional: parsedEntry.kind === "optional"
46
- },
47
- value
48
- });
34
+ sequences = sequences.map(base => appendElement(base, optional ? "optional" : "required", element));
49
35
  }
50
- ctx.path.pop();
51
36
  }
52
- if (variadicIndex === undefined) {
53
- props.push({
54
- key: {
55
- name: "length",
56
- prerequisite: true,
57
- optional: false
58
- },
59
- value: schema({ unit: def.length })
60
- });
37
+ return ctx.$.raw.schema(sequences.map(sequence => ({
38
+ proto: Array,
39
+ sequence
40
+ })));
41
+ };
42
+ const appendElement = (base, kind, element) => {
43
+ switch (kind) {
44
+ case "required":
45
+ if (base.optionals)
46
+ // e.g. [string?, number]
47
+ return throwParseError(requiredPostOptionalMessage);
48
+ if (base.variadic) {
49
+ // e.g. [...string[], number]
50
+ base.postfix = append(base.postfix, element);
51
+ }
52
+ else {
53
+ // e.g. [string, number]
54
+ base.prefix = append(base.prefix, element);
55
+ }
56
+ return base;
57
+ case "optional":
58
+ if (base.variadic)
59
+ // e.g. [...string[], number?]
60
+ return throwParseError(optionalPostVariadicMessage);
61
+ // e.g. [string, number?]
62
+ base.optionals = append(base.optionals, element);
63
+ return base;
64
+ case "variadic":
65
+ // e.g. [...string[], number, ...string[]]
66
+ if (base.postfix)
67
+ throwParseError(multipleVariadicMesage);
68
+ if (base.variadic) {
69
+ if (!base.variadic.equals(element)) {
70
+ // e.g. [...string[], ...number[]]
71
+ throwParseError(multipleVariadicMesage);
72
+ }
73
+ // e.g. [...string[], ...string[]]
74
+ // do nothing, second spread doesn't change the type
75
+ }
76
+ else {
77
+ // e.g. [string, ...number[]]
78
+ base.variadic = element.raw;
79
+ }
80
+ return base;
81
+ }
82
+ };
83
+ const appendSpreadBranch = (base, branch) => {
84
+ const spread = branch.firstReferenceOfKind("sequence");
85
+ if (!spread) {
86
+ // the only array with no sequence reference is unknown[]
87
+ return appendElement(base, "variadic", tsKeywords.unknown);
61
88
  }
62
- return schema(Array);
89
+ spread.prefix.forEach(node => appendElement(base, "required", node));
90
+ spread.optionals.forEach(node => appendElement(base, "optional", node));
91
+ spread.variadic && appendElement(base, "variadic", spread.variadic);
92
+ spread.postfix.forEach(node => appendElement(base, "required", node));
93
+ return base;
63
94
  };
64
- export const maybeParseTupleExpression = (def, ctx) => {
65
- const tupleExpressionResult = isIndexOneExpression(def)
66
- ? indexOneParsers[def[1]](def, ctx)
67
- : isIndexZeroExpression(def)
68
- ? prefixParsers[def[0]](def, ctx)
95
+ const maybeParseTupleExpression = (def, ctx) => {
96
+ const tupleExpressionResult = isIndexZeroExpression(def) ? prefixParsers[def[0]](def, ctx)
97
+ : isIndexOneExpression(def) ? indexOneParsers[def[1]](def, ctx)
69
98
  : undefined;
70
- if (tupleExpressionResult) {
71
- return tupleExpressionResult.isNever()
72
- ? throwParseError(writeUnsatisfiableExpressionError(def
73
- .map((def) => (typeof def === "string" ? def : printable(def)))
74
- .join(" ")))
75
- : tupleExpressionResult;
76
- }
99
+ return tupleExpressionResult;
77
100
  };
78
- export const writeNonArrayRestMessage = (operand) => `Rest element ${typeof operand === "string" ? `'${operand}'` : ""} must be an array`;
79
- export const multipleVariadicMesage = `A tuple may have at most one variadic element`;
80
- export const parseKeyOfTuple = (def, ctx) => ctx.scope.parse(def[1], ctx).keyof();
101
+ export const writeNonArraySpreadMessage = (operand) => `Spread element must be an array (was ${operand})`;
102
+ export const multipleVariadicMesage = "A tuple may have at most one variadic element";
103
+ export const requiredPostOptionalMessage = "A required element may not follow an optional element";
104
+ export const optionalPostVariadicMessage = "An optional element may not follow a variadic element";
105
+ export const spreadOptionalMessage = "A spread element cannot be optional";
106
+ export const parseKeyOfTuple = (def, ctx) => ctx.$.parse(def[1], ctx).keyof();
81
107
  const parseBranchTuple = (def, ctx) => {
82
- if (def[2] === undefined) {
108
+ if (def[2] === undefined)
83
109
  return throwParseError(writeMissingRightOperandMessage(def[1], ""));
84
- }
85
- const l = ctx.scope.parse(def[0], ctx);
86
- const r = ctx.scope.parse(def[2], ctx);
110
+ const l = ctx.$.parse(def[0], ctx);
111
+ const r = ctx.$.parse(def[2], ctx);
87
112
  return def[1] === "&" ? l.and(r) : l.or(r);
88
113
  };
89
- const parseArrayTuple = (def, ctx) => ctx.scope.parse(def[0], ctx).array();
114
+ const parseArrayTuple = (def, ctx) => ctx.$.parse(def[0], ctx).array();
90
115
  const isIndexOneExpression = (def) => indexOneParsers[def[1]] !== undefined;
91
116
  export const parseMorphTuple = (def, ctx) => {
92
117
  if (typeof def[2] !== "function") {
93
118
  return throwParseError(writeMalformedFunctionalExpressionMessage("=>", def[2]));
94
119
  }
95
120
  // TODO: nested morphs?
96
- return schema({
97
- in: ctx.scope.parse(def[0], ctx),
98
- morph: def[2]
99
- });
121
+ return ctx.$.parse(def[0], ctx).pipe(def[2]);
100
122
  };
101
123
  export const writeMalformedFunctionalExpressionMessage = (operator, value) => `${operator === ":" ? "Narrow" : "Morph"} expression requires a function following '${operator}' (was ${typeof value})`;
102
124
  export const parseNarrowTuple = (def, ctx) => {
103
125
  if (typeof def[2] !== "function") {
104
126
  return throwParseError(writeMalformedFunctionalExpressionMessage(":", def[2]));
105
127
  }
106
- return ctx.scope
107
- .parse(def[0], ctx)
108
- .constrain("predicate", def[2]);
128
+ return ctx.$.parse(def[0], ctx).constrain("predicate", def[2]);
109
129
  };
110
- const parseAttributeTuple = (def, ctx) => configureShallowDescendants(ctx.scope.parse(def[0], ctx), def[2]);
130
+ const parseAttributeTuple = (def, ctx) => ctx.$.parse(def[0], ctx).configureShallowDescendants(def[2]);
111
131
  const indexOneParsers = {
112
132
  "|": parseBranchTuple,
113
133
  "&": parseBranchTuple,
@@ -118,19 +138,20 @@ const indexOneParsers = {
118
138
  };
119
139
  const prefixParsers = {
120
140
  keyof: parseKeyOfTuple,
121
- instanceof: (def) => {
141
+ instanceof: (def, ctx) => {
122
142
  if (typeof def[1] !== "function") {
123
143
  return throwParseError(writeInvalidConstructorMessage(objectKindOrDomainOf(def[1])));
124
144
  }
125
145
  const branches = def
126
146
  .slice(1)
127
- .map((ctor) => typeof ctor === "function"
128
- ? { proto: ctor }
147
+ .map(ctor => typeof ctor === "function" ?
148
+ ctx.$.node("proto", { proto: ctor })
129
149
  : throwParseError(writeInvalidConstructorMessage(objectKindOrDomainOf(ctor))));
130
- return schema(...branches);
150
+ return branches.length === 1 ?
151
+ branches[0]
152
+ : ctx.$.node("union", { branches });
131
153
  },
132
- "===": (def) => schema.units(...def.slice(1))
154
+ "===": (def, ctx) => ctx.$.units(def.slice(1))
133
155
  };
134
156
  const isIndexZeroExpression = (def) => prefixParsers[def[0]] !== undefined;
135
157
  export const writeInvalidConstructorMessage = (actual) => `Expected a constructor following 'instanceof' operator (was ${actual})`;
136
- //# sourceMappingURL=tuple.js.map
package/out/scope.d.ts CHANGED
@@ -1,31 +1,14 @@
1
- import { type ArkConfig, type TypeNode, type extractIn, type extractOut } from "@arktype/schema";
2
- import { type Dict, type evaluate, type isAny, type nominal } from "@arktype/util";
3
- import type { type } from "./ark.js";
1
+ import { RawRootScope, type ArkConfig, type BaseRoot, type GenericProps, type PreparsedNodeResolution, type PrivateDeclaration, type RawRootResolutions, type RootScope, type UnknownRoot, type ambient, type arkKind, type destructuredExportContext, type destructuredImportContext, type exportedNameOf, type writeDuplicateAliasError } from "@arktype/schema";
2
+ import { type anyOrNever, type keyError, type nominal, type show } from "@arktype/util";
3
+ import { Generic } from "./generic.js";
4
4
  import { type MatchParser } from "./match.js";
5
+ import type { Module } from "./module.js";
5
6
  import { type inferDefinition, type validateDefinition } from "./parser/definition.js";
6
7
  import { parseGenericParams, type GenericDeclaration, type GenericParamsParseError } from "./parser/generic.js";
7
- import { Type, type DeclarationParser, type DefinitionParser, type Generic, type GenericProps, type TypeParser } from "./type.js";
8
- import { type arkKind } from "./util.js";
9
- export type ScopeParser<parent, ambient> = {
10
- <const def>(def: validateScope<def, parent & ambient>): Scope<{
11
- exports: inferBootstrapped<{
12
- exports: bootstrapExports<def>;
13
- locals: bootstrapLocals<def> & parent;
14
- ambient: ambient;
15
- }>;
16
- locals: inferBootstrapped<{
17
- exports: bootstrapLocals<def>;
18
- locals: bootstrapExports<def> & parent;
19
- ambient: ambient;
20
- }>;
21
- ambient: ambient;
22
- }>;
23
- };
24
- export interface ArkScopeConfig extends ArkConfig {
25
- ambient?: Scope | null;
26
- }
27
- type validateScope<def, $> = {
28
- [k in keyof def]: parseScopeKey<k>["params"] extends [] ? def[k] extends Type | PreparsedResolution ? def[k] : validateDefinition<def[k], $ & bootstrap<def>, {}> : parseScopeKey<k>["params"] extends GenericParamsParseError ? parseScopeKey<k>["params"][0] : validateDefinition<def[k], $ & bootstrap<def>, {
8
+ import { RawTypeParser, type DeclarationParser, type DefinitionParser, type Type, type TypeParser } from "./type.js";
9
+ export type ScopeParser = <const def>(def: validateScope<def>, config?: ArkConfig) => Scope<inferBootstrapped<bootstrapAliases<def>>>;
10
+ type validateScope<def> = {
11
+ [k in keyof def]: k extends symbol ? unknown : parseScopeKey<k>["params"] extends [] ? def[k] extends Type | PreparsedResolution ? def[k] : k extends PrivateDeclaration<infer name extends keyof def & string> ? keyError<writeDuplicateAliasError<name>> : validateDefinition<def[k], ambient & bootstrapAliases<def>, {}> : parseScopeKey<k>["params"] extends GenericParamsParseError ? parseScopeKey<k>["params"][0] : validateDefinition<def[k], ambient & bootstrapAliases<def>, {
29
12
  [param in parseScopeKey<k>["params"][number]]: unknown;
30
13
  }>;
31
14
  };
@@ -36,119 +19,65 @@ export type bindThis<def> = {
36
19
  type Def<def = {}> = nominal<def, "unparsed">;
37
20
  /** sentinel indicating a scope that will be associated with a generic has not yet been parsed */
38
21
  export type UnparsedScope = "$";
39
- type bootstrap<def> = bootstrapLocals<def> & bootstrapExports<def>;
40
- type bootstrapLocals<def> = bootstrapAliases<{
41
- [k in keyof def & PrivateDeclaration as extractPrivateKey<k>]: def[k];
42
- }>;
43
- type bootstrapExports<def> = bootstrapAliases<{
44
- [k in Exclude<keyof def, PrivateDeclaration>]: def[k];
45
- }>;
46
22
  /** These are legal as values of a scope but not as definitions in other contexts */
47
- type PreparsedResolution = Module | GenericProps;
23
+ type PreparsedResolution = PreparsedNodeResolution;
48
24
  type bootstrapAliases<def> = {
49
25
  [k in Exclude<keyof def, GenericDeclaration | symbol>]: def[k] extends PreparsedResolution ? def[k] : def[k] extends (() => infer thunkReturn extends PreparsedResolution) ? thunkReturn : Def<def[k]>;
50
26
  } & {
51
- [k in keyof def & GenericDeclaration as extractGenericName<k>]: Generic<parseGenericParams<extractGenericParameters<k>>, def[k], UnparsedScope>;
27
+ [k in keyof def & GenericDeclaration as extractGenericName<k>]: GenericProps<parseGenericParams<extractGenericParameters<k>>, def[k], UnparsedScope>;
52
28
  };
53
- type inferBootstrapped<r extends Resolutions> = evaluate<{
54
- [name in keyof r["exports"]]: r["exports"][name] extends Def<infer def> ? inferDefinition<def, $<r>, {}> : r["exports"][name] extends GenericProps<infer params, infer def> ? Generic<params, def, $<r>> : r["exports"][name];
29
+ type inferBootstrapped<$> = show<{
30
+ [name in keyof $]: $[name] extends Def<infer def> ? inferDefinition<def, $ & ambient, {}> : $[name] extends GenericProps<infer params, infer def> ? Generic<params, def, $> : $[name];
55
31
  }>;
56
32
  type extractGenericName<k> = k extends GenericDeclaration<infer name> ? name : never;
57
33
  type extractGenericParameters<k> = k extends GenericDeclaration<string, infer params> ? params : never;
58
- type extractPrivateKey<k> = k extends PrivateDeclaration<infer key> ? key : never;
59
- type PrivateDeclaration<key extends string = string> = `#${key}`;
60
- export type resolve<reference extends keyof $ | keyof args, $, args> = (reference extends keyof args ? args[reference] : $[reference & keyof $]) extends infer resolution ? [resolution] extends [never] ? never : isAny<resolution> extends true ? any : resolution extends Def<infer def> ? inferDefinition<def, $, args> : resolution : never;
34
+ export type resolve<reference extends keyof $ | keyof args, $, args> = (reference extends keyof args ? args[reference] : $[reference & keyof $]) extends infer resolution ? [
35
+ resolution
36
+ ] extends [anyOrNever] ? resolution : resolution extends Def<infer def> ? inferDefinition<def, $, args> : resolution : never;
61
37
  export type moduleKeyOf<$> = {
62
- [k in keyof $]: $[k] extends Module ? k & string : never;
38
+ [k in keyof $]: $[k] extends {
39
+ [arkKind]: "module";
40
+ } ? k & string : never;
63
41
  }[keyof $];
64
- export type tryInferSubmoduleReference<$, token> = token extends `${infer submodule extends moduleKeyOf<$>}.${infer subalias}` ? subalias extends keyof $[submodule] ? $[submodule][subalias] extends Type<infer t> ? t : never : never : never;
65
- type $<r extends Resolutions> = r["exports"] & r["locals"] & r["ambient"];
66
- type exportedName<r extends Resolutions> = keyof r["exports"] & string;
67
- export type Module<r extends Resolutions = any> = {
68
- [k in exportedName<r> | arkKind]: k extends string ? [r["exports"][k]] extends [never] ? Type<never, $<r>> : isAny<r["exports"][k]> extends true ? Type<any, $<r>> : r["exports"][k] extends PreparsedResolution ? r["exports"][k] : Type<r["exports"][k], $<r>> : type.cast<"module">;
69
- };
70
- export type Resolutions = {
71
- exports: unknown;
72
- locals: unknown;
73
- ambient: unknown;
74
- };
75
- export type ParseContext = {
76
- baseName: string;
77
- path: string[];
78
- scope: Scope;
79
- args: Record<string, TypeNode> | undefined;
80
- };
81
- type ParseContextInput = Pick<ParseContext, "baseName" | "args">;
82
- export declare class Scope<r extends Resolutions = any> {
83
- infer: extractOut<r["exports"]>;
84
- inferIn: extractIn<r["exports"]>;
85
- config: ArkScopeConfig;
42
+ export type tryInferSubmoduleReference<$, token> = token extends `${infer submodule extends moduleKeyOf<$>}.${infer subalias}` ? subalias extends keyof $[submodule] ? $[submodule][subalias] : never : never;
43
+ export interface ParseContext {
44
+ $: RawScope;
45
+ args?: Record<string, UnknownRoot>;
46
+ }
47
+ export declare const scope: ScopeParser;
48
+ export interface Scope<$ = any> extends RootScope<$> {
49
+ type: TypeParser<$>;
50
+ match: MatchParser<$>;
51
+ declare: DeclarationParser<$>;
52
+ define: DefinitionParser<$>;
53
+ import<names extends exportedNameOf<$>[]>(...names: names): Module<show<destructuredImportContext<$, names>>>;
54
+ export<names extends exportedNameOf<$>[]>(...names: names): Module<show<destructuredExportContext<$, names>>>;
55
+ }
56
+ export declare class RawScope<$ extends RawRootResolutions = RawRootResolutions> extends RawRootScope<$> {
86
57
  private parseCache;
87
- private resolutions;
88
- /** The set of names defined at the root-level of the scope mapped to their
89
- * corresponding definitions.**/
90
- aliases: Record<string, unknown>;
91
- private exportedNames;
92
- private ambient;
93
- constructor(def: Dict, config: ArkScopeConfig);
94
- static root: ScopeParser<{}, {}>;
95
- type: TypeParser<$<r>>;
96
- match: MatchParser<$<r>>;
97
- declare: DeclarationParser<$<r>>;
98
- scope: ScopeParser<r["exports"], r["ambient"]>;
99
- define: DefinitionParser<$<r>>;
100
- toAmbient(): Scope<{
101
- exports: r["exports"];
102
- locals: r["locals"];
103
- ambient: r["exports"];
104
- }>;
105
- get<name extends keyof r["exports"] & string>(name: name): Type<r["exports"][name], $<r>>;
106
- private createRootContext;
107
- parseDefinition(def: unknown, input: ParseContextInput): TypeNode;
108
- parse(def: unknown, ctx: ParseContext): TypeNode;
109
- parseString(def: string, ctx: ParseContext): TypeNode;
110
- maybeResolve(name: string): TypeNode | Generic | undefined;
111
- /** If name is a valid reference to a submodule alias, return its resolution */
112
- private maybeResolveSubalias;
113
- maybeResolveNode(name: string): TypeNode | undefined;
114
- import<names extends exportedName<r>[]>(...names: names): destructuredImportContext<r, names extends [] ? keyof r["exports"] & string : names[number]>;
115
- bindThis(): {
116
- this: import("@arktype/schema").IntersectionNode<unknown>;
58
+ constructor(def: Record<string, unknown>, config?: ArkConfig);
59
+ type: RawTypeParser;
60
+ match: MatchParser<$>;
61
+ declare: () => {
62
+ type: RawTypeParser;
117
63
  };
118
- private exportedResolutions;
119
- private exportCache;
120
- export<names extends exportedName<r>[]>(...names: names): Module<names extends [] ? r : destructuredExportContext<r, names[number]>>;
64
+ define: (def: unknown) => unknown;
65
+ preparseRoot(def: unknown): unknown;
66
+ parseRoot(def: unknown): BaseRoot;
67
+ parse(def: unknown, ctx: ParseContext): BaseRoot;
68
+ parseString(def: string, ctx: ParseContext): BaseRoot;
121
69
  }
122
- type destructuredExportContext<r extends Resolutions, name extends exportedName<r>> = {
123
- exports: {
124
- [k in name]: r["exports"][k];
125
- };
126
- locals: r["locals"] & {
127
- [k in Exclude<keyof r["exports"], name>]: r["exports"][k];
128
- };
129
- ambient: r["ambient"];
130
- };
131
- type destructuredImportContext<r extends Resolutions, name extends exportedName<r>> = {
132
- [k in name as `#${k & string}`]: type.cast<r["exports"][k]>;
133
- };
134
70
  export declare const writeShallowCycleErrorMessage: (name: string, seen: string[]) => string;
135
- export declare const writeDuplicateNameMessage: <name extends string>(name: name) => `Duplicate name '${name}'`;
136
- type writeDuplicateNameMessage<name extends string> = `Duplicate name '${name}'`;
137
71
  export type ParsedScopeKey = {
138
- isLocal: boolean;
139
72
  name: string;
140
73
  params: string[];
141
74
  };
142
75
  export declare const parseScopeKey: (k: string) => ParsedScopeKey;
143
- type parseScopeKey<k> = k extends PrivateDeclaration<infer inner> ? parsePossibleGenericDeclaration<inner, true> : parsePossibleGenericDeclaration<k, false>;
144
- type parsePossibleGenericDeclaration<k, isLocal extends boolean> = k extends GenericDeclaration<infer name, infer paramString> ? {
145
- isLocal: isLocal;
76
+ export type parseScopeKey<k> = k extends GenericDeclaration<infer name, infer paramString> ? {
146
77
  name: name;
147
78
  params: parseGenericParams<paramString>;
148
79
  } : {
149
- isLocal: isLocal;
150
80
  name: k;
151
81
  params: [];
152
82
  };
153
83
  export {};
154
- //# sourceMappingURL=scope.d.ts.map