arktype 0.3.0 → 0.5.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 (126) hide show
  1. package/dist/api.cjs +9 -0
  2. package/dist/api.d.ts +10 -0
  3. package/dist/api.mjs +1 -0
  4. package/dist/cjs/nodes/branch.js +15 -6
  5. package/dist/cjs/nodes/compose.js +1 -1
  6. package/dist/cjs/nodes/discriminate.js +89 -21
  7. package/dist/cjs/nodes/node.js +11 -11
  8. package/dist/cjs/nodes/predicate.js +1 -1
  9. package/dist/cjs/nodes/rules/class.js +2 -2
  10. package/dist/cjs/nodes/rules/divisor.js +1 -1
  11. package/dist/cjs/nodes/rules/props.js +1 -2
  12. package/dist/cjs/nodes/rules/range.js +2 -2
  13. package/dist/cjs/nodes/rules/regex.js +1 -1
  14. package/dist/cjs/nodes/rules/rules.js +4 -0
  15. package/dist/cjs/parse/ast/ast.js +4 -0
  16. package/dist/cjs/parse/ast/bound.js +9 -0
  17. package/dist/cjs/parse/{tuple/utils.js → ast/distributableFunction.js} +1 -1
  18. package/dist/cjs/parse/ast/divisor.js +9 -0
  19. package/dist/cjs/parse/{string/ast.js → ast/intersection.js} +2 -8
  20. package/dist/cjs/parse/ast/keyof.js +109 -0
  21. package/dist/cjs/parse/{tuple → ast}/morph.js +1 -1
  22. package/dist/cjs/parse/{tuple → ast}/narrow.js +2 -2
  23. package/dist/cjs/parse/{tuple → ast}/tuple.js +11 -10
  24. package/dist/cjs/parse/ast/union.js +9 -0
  25. package/dist/cjs/parse/ast/utils.js +4 -0
  26. package/dist/cjs/parse/definition.js +12 -2
  27. package/dist/cjs/parse/string/reduce/dynamic.js +7 -7
  28. package/dist/cjs/parse/string/shift/operator/bounds.js +4 -4
  29. package/dist/cjs/parse/string/shift/operator/divisor.js +3 -3
  30. package/dist/cjs/parse/string/string.js +1 -1
  31. package/dist/cjs/scopes/expressions.js +29 -0
  32. package/dist/cjs/scopes/scope.js +59 -6
  33. package/dist/cjs/scopes/validation.js +16 -1
  34. package/dist/cjs/traverse/problems.js +78 -46
  35. package/dist/cjs/traverse/traverse.js +62 -58
  36. package/dist/cjs/utils/data.js +39 -0
  37. package/dist/cjs/utils/freeze.js +2 -3
  38. package/dist/cjs/utils/generics.js +20 -2
  39. package/dist/cjs/utils/numericLiterals.js +2 -14
  40. package/dist/mjs/nodes/branch.js +14 -5
  41. package/dist/mjs/nodes/compose.js +2 -2
  42. package/dist/mjs/nodes/discriminate.js +89 -21
  43. package/dist/mjs/nodes/node.js +8 -8
  44. package/dist/mjs/nodes/predicate.js +2 -2
  45. package/dist/mjs/nodes/rules/class.js +2 -2
  46. package/dist/mjs/nodes/rules/divisor.js +1 -1
  47. package/dist/mjs/nodes/rules/props.js +1 -2
  48. package/dist/mjs/nodes/rules/range.js +2 -2
  49. package/dist/mjs/nodes/rules/regex.js +1 -1
  50. package/dist/mjs/nodes/rules/rules.js +4 -0
  51. package/dist/mjs/parse/ast/ast.js +1 -0
  52. package/dist/mjs/parse/ast/bound.js +1 -0
  53. package/dist/mjs/parse/{tuple/utils.js → ast/distributableFunction.js} +2 -2
  54. package/dist/mjs/parse/ast/divisor.js +1 -0
  55. package/dist/mjs/parse/{string/ast.js → ast/intersection.js} +2 -5
  56. package/dist/mjs/parse/ast/keyof.js +101 -0
  57. package/dist/mjs/parse/{tuple → ast}/morph.js +1 -1
  58. package/dist/mjs/parse/ast/narrow.js +7 -0
  59. package/dist/mjs/parse/{tuple → ast}/tuple.js +12 -11
  60. package/dist/mjs/parse/ast/union.js +1 -0
  61. package/dist/mjs/parse/ast/utils.js +1 -0
  62. package/dist/mjs/parse/definition.js +12 -2
  63. package/dist/mjs/parse/string/reduce/dynamic.js +8 -8
  64. package/dist/mjs/parse/string/shift/operator/bounds.js +4 -4
  65. package/dist/mjs/parse/string/shift/operator/divisor.js +3 -3
  66. package/dist/mjs/parse/string/string.js +2 -2
  67. package/dist/mjs/scopes/expressions.js +9 -0
  68. package/dist/mjs/scopes/scope.js +58 -5
  69. package/dist/mjs/scopes/validation.js +16 -1
  70. package/dist/mjs/traverse/problems.js +76 -44
  71. package/dist/mjs/traverse/traverse.js +37 -34
  72. package/dist/mjs/utils/data.js +24 -0
  73. package/dist/mjs/utils/freeze.js +2 -3
  74. package/dist/mjs/utils/generics.js +18 -1
  75. package/dist/mjs/utils/numericLiterals.js +1 -1
  76. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  77. package/dist/types/nodes/branch.d.ts +3 -12
  78. package/dist/types/nodes/discriminate.d.ts +6 -8
  79. package/dist/types/nodes/infer.d.ts +5 -5
  80. package/dist/types/nodes/node.d.ts +4 -13
  81. package/dist/types/nodes/predicate.d.ts +2 -29
  82. package/dist/types/nodes/rules/range.d.ts +2 -2
  83. package/dist/types/nodes/rules/rules.d.ts +1 -1
  84. package/dist/types/parse/ast/ast.d.ts +24 -0
  85. package/dist/types/parse/ast/bound.d.ts +10 -0
  86. package/dist/types/parse/{tuple/utils.d.ts → ast/distributableFunction.d.ts} +2 -3
  87. package/dist/types/parse/ast/divisor.d.ts +8 -0
  88. package/dist/types/parse/ast/intersection.d.ts +17 -0
  89. package/dist/types/parse/{tuple → ast}/keyof.d.ts +4 -4
  90. package/dist/types/parse/ast/morph.d.ts +18 -0
  91. package/dist/types/parse/ast/narrow.d.ts +21 -0
  92. package/dist/types/parse/ast/tuple.d.ts +40 -0
  93. package/dist/types/parse/ast/union.d.ts +12 -0
  94. package/dist/types/parse/ast/utils.d.ts +7 -0
  95. package/dist/types/parse/definition.d.ts +9 -7
  96. package/dist/types/parse/record.d.ts +2 -2
  97. package/dist/types/parse/string/reduce/shared.d.ts +5 -4
  98. package/dist/types/parse/string/reduce/static.d.ts +11 -6
  99. package/dist/types/parse/string/shift/operand/unenclosed.d.ts +1 -1
  100. package/dist/types/parse/string/shift/operator/bounds.d.ts +3 -2
  101. package/dist/types/parse/string/shift/operator/divisor.d.ts +5 -1
  102. package/dist/types/parse/string/shift/scanner.d.ts +1 -1
  103. package/dist/types/parse/string/string.d.ts +1 -2
  104. package/dist/types/scopes/expressions.d.ts +48 -0
  105. package/dist/types/scopes/scope.d.ts +17 -7
  106. package/dist/types/scopes/standard.d.ts +15 -0
  107. package/dist/types/scopes/type.d.ts +9 -5
  108. package/dist/types/scopes/validation.d.ts +4 -0
  109. package/dist/types/traverse/problems.d.ts +17 -24
  110. package/dist/types/traverse/traverse.d.ts +67 -37
  111. package/dist/types/utils/data.d.ts +13 -0
  112. package/dist/types/utils/domains.d.ts +1 -1
  113. package/dist/types/utils/generics.d.ts +14 -10
  114. package/dist/types/utils/numericLiterals.d.ts +16 -0
  115. package/dist/types/utils/paths.d.ts +2 -0
  116. package/package.json +1 -1
  117. package/dist/cjs/parse/tuple/keyof.js +0 -64
  118. package/dist/cjs/utils/size.js +0 -16
  119. package/dist/mjs/parse/tuple/keyof.js +0 -56
  120. package/dist/mjs/parse/tuple/narrow.js +0 -7
  121. package/dist/mjs/utils/size.js +0 -2
  122. package/dist/types/parse/string/ast.d.ts +0 -46
  123. package/dist/types/parse/tuple/morph.d.ts +0 -15
  124. package/dist/types/parse/tuple/narrow.d.ts +0 -13
  125. package/dist/types/parse/tuple/tuple.d.ts +0 -31
  126. package/dist/types/utils/size.d.ts +0 -3
package/dist/api.cjs CHANGED
@@ -11,9 +11,18 @@ function _export(target, all) {
11
11
  _export(exports, {
12
12
  scope: ()=>_scopeTs.scope,
13
13
  type: ()=>_standardTs.type,
14
+ intersection: ()=>_expressionsTs.intersection,
15
+ union: ()=>_expressionsTs.union,
16
+ arrayOf: ()=>_expressionsTs.arrayOf,
17
+ instanceOf: ()=>_expressionsTs.instanceOf,
18
+ valueOf: ()=>_expressionsTs.valueOf,
19
+ morph: ()=>_expressionsTs.morph,
20
+ narrow: ()=>_expressionsTs.narrow,
21
+ keyOf: ()=>_expressionsTs.keyOf,
14
22
  Problems: ()=>_problemsTs.Problems,
15
23
  Problem: ()=>_problemsTs.Problem
16
24
  });
17
25
  const _scopeTs = require("./cjs/scopes/scope.js");
18
26
  const _standardTs = require("./cjs/scopes/standard.js");
27
+ const _expressionsTs = require("./cjs/scopes/expressions.js");
19
28
  const _problemsTs = require("./cjs/traverse/problems.js");
package/dist/api.d.ts CHANGED
@@ -1,4 +1,14 @@
1
1
  export { scope } from "./types/scopes/scope.js"
2
2
  export { type } from "./types/scopes/standard.js"
3
+ export {
4
+ intersection,
5
+ union,
6
+ arrayOf,
7
+ instanceOf,
8
+ valueOf,
9
+ morph,
10
+ narrow,
11
+ keyOf
12
+ } from "./types/scopes/expressions.js"
3
13
  export type { Type } from "./types/scopes/type.js"
4
14
  export { Problems, Problem } from "./types/traverse/problems.js"
package/dist/api.mjs CHANGED
@@ -1,3 +1,4 @@
1
1
  export { scope } from "./mjs/scopes/scope.js";
2
2
  export { type } from "./mjs/scopes/standard.js";
3
+ export { intersection, union, arrayOf, instanceOf, valueOf, morph, narrow, keyOf } from "./mjs/scopes/expressions.js";
3
4
  export { Problems, Problem } from "./mjs/traverse/problems.js";
@@ -15,7 +15,7 @@ _export(exports, {
15
15
  flattenBranch: ()=>flattenBranch,
16
16
  branchIntersection: ()=>branchIntersection
17
17
  });
18
- const _astJs = require("../parse/string/ast.js");
18
+ const _intersectionJs = require("../parse/ast/intersection.js");
19
19
  const _domainsJs = require("../utils/domains.js");
20
20
  const _errorsJs = require("../utils/errors.js");
21
21
  const _composeJs = require("./compose.js");
@@ -92,10 +92,19 @@ const flattenBranch = (branch, ctx)=>{
92
92
  if (isMorphBranch(branch)) {
93
93
  const result = (0, _rulesJs.flattenRules)(branch.rules, ctx);
94
94
  if (branch.morph) {
95
- result.push([
96
- "morph",
97
- branch.morph
98
- ]);
95
+ if (typeof branch.morph === "function") {
96
+ result.push([
97
+ "morph",
98
+ branch.morph
99
+ ]);
100
+ } else {
101
+ for (const morph of branch.morph){
102
+ result.push([
103
+ "morph",
104
+ morph
105
+ ]);
106
+ }
107
+ }
99
108
  }
100
109
  return result;
101
110
  }
@@ -114,7 +123,7 @@ const branchIntersection = (l, r, state)=>{
114
123
  morph: l.morph
115
124
  };
116
125
  }
117
- return state.lastOperator === "&" ? (0, _errorsJs.throwParseError)((0, _astJs.writeImplicitNeverMessage)(state.path, "Intersection", "of morphs")) : {};
126
+ return state.lastOperator === "&" ? (0, _errorsJs.throwParseError)((0, _intersectionJs.writeImplicitNeverMessage)(state.path, "Intersection", "of morphs")) : {};
118
127
  }
119
128
  return (0, _composeJs.isDisjoint)(rulesResult) ? rulesResult : {
120
129
  rules: (0, _composeJs.isEquality)(rulesResult) ? l.rules : rulesResult,
@@ -109,7 +109,7 @@ const equality = ()=>equal;
109
109
  const isEquality = (result)=>result === equal;
110
110
  const composeKeyedIntersection = (reducer, config)=>(l, r, state)=>{
111
111
  const result = {};
112
- const keys = (0, _genericsTs.keysOf)({
112
+ const keys = (0, _genericsTs.objectKeysOf)({
113
113
  ...l,
114
114
  ...r
115
115
  });
@@ -10,10 +10,10 @@ function _export(target, all) {
10
10
  }
11
11
  _export(exports, {
12
12
  flattenBranches: ()=>flattenBranches,
13
- serializeIfAllowed: ()=>serializeIfAllowed,
13
+ serializeDefinitionIfAllowed: ()=>serializeDefinitionIfAllowed,
14
14
  serializeCase: ()=>serializeCase
15
15
  });
16
- const _astTs = require("../parse/string/ast.js");
16
+ const _unionTs = require("../parse/ast/union.js");
17
17
  const _domainsTs = require("../utils/domains.js");
18
18
  const _errorsTs = require("../utils/errors.js");
19
19
  const _genericsTs = require("../utils/generics.js");
@@ -37,15 +37,17 @@ const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
37
37
  return [
38
38
  [
39
39
  "branches",
40
- remainingIndices.map((i)=>branchIncludesMorph(originalBranches[i], ctx.type.meta.scope) ? (0, _errorsTs.throwParseError)((0, _astTs.writeUndiscriminatableMorphUnionMessage)(`${ctx.path}`)) : (0, _branchTs.flattenBranch)(originalBranches[i], ctx))
40
+ remainingIndices.map((i)=>branchIncludesMorph(originalBranches[i], ctx.type.meta.scope) ? (0, _errorsTs.throwParseError)((0, _unionTs.writeUndiscriminatableMorphUnionMessage)(`${ctx.path}`)) : (0, _branchTs.flattenBranch)(originalBranches[i], ctx))
41
41
  ]
42
42
  ];
43
43
  }
44
44
  const cases = {};
45
- let caseKey;
46
- for(caseKey in bestDiscriminant.indexCases){
45
+ for(const caseKey in bestDiscriminant.indexCases){
47
46
  const nextIndices = bestDiscriminant.indexCases[caseKey];
48
47
  cases[caseKey] = discriminate(originalBranches, nextIndices, discriminants, ctx);
48
+ if (caseKey !== "default") {
49
+ pruneDiscriminant(cases[caseKey], bestDiscriminant.path, bestDiscriminant, ctx);
50
+ }
49
51
  }
50
52
  return [
51
53
  [
@@ -58,9 +60,63 @@ const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
58
60
  ]
59
61
  ];
60
62
  };
63
+ const pruneDiscriminant = (entries, segments, discriminant, ctx)=>{
64
+ for(let i = 0; i < entries.length; i++){
65
+ const [k, v] = entries[i];
66
+ if (!segments.length) {
67
+ if (discriminant.kind === "domain") {
68
+ if (k === "domain" || k === "domains") {
69
+ entries.splice(i, 1);
70
+ return;
71
+ } else if (k === "class" || k === "value") {
72
+ // these keys imply a domain, but also add additional
73
+ // information, so can't be pruned
74
+ return;
75
+ }
76
+ } else if (discriminant.kind === k) {
77
+ entries.splice(i, 1);
78
+ return;
79
+ }
80
+ } else if ((k === "requiredProp" || k === "prerequisiteProp" || k === "optionalProp") && v[0] === segments[0]) {
81
+ if (typeof v[1] === "string") {
82
+ if (discriminant.kind !== "domain") {
83
+ return throwPruneFailure(discriminant);
84
+ }
85
+ entries.splice(i, 1);
86
+ return;
87
+ }
88
+ pruneDiscriminant(v[1], segments.slice(1), discriminant, ctx);
89
+ if (v[1].length === 0) {
90
+ entries.splice(i, 1);
91
+ }
92
+ return;
93
+ }
94
+ // check for branch keys, which must be traversed even if there are no
95
+ // segments left
96
+ if (k === "domains") {
97
+ if ((0, _genericsTs.keyCount)(v) !== 1 || !v.object) {
98
+ return throwPruneFailure(discriminant);
99
+ }
100
+ pruneDiscriminant(v.object, segments, discriminant, ctx);
101
+ return;
102
+ } else if (k === "switch") {
103
+ for(const caseKey in v.cases){
104
+ pruneDiscriminant(v.cases[caseKey], segments, discriminant, ctx);
105
+ }
106
+ return;
107
+ } else if (k === "branches") {
108
+ for (const branch of v){
109
+ pruneDiscriminant(branch, segments, discriminant, ctx);
110
+ }
111
+ return;
112
+ }
113
+ }
114
+ return throwPruneFailure(discriminant);
115
+ };
116
+ const throwPruneFailure = (discriminant)=>(0, _errorsTs.throwInternalError)(`Unexpectedly failed to discriminate ${discriminant.kind} at path '${discriminant.path}'`);
61
117
  const discriminantKinds = {
62
118
  domain: true,
63
- objectKind: true,
119
+ class: true,
64
120
  value: true
65
121
  };
66
122
  const calculateDiscriminants = (branches, ctx)=>{
@@ -83,8 +139,8 @@ const calculateDiscriminants = (branches, ctx)=>{
83
139
  if (!(0, _genericsTs.isKeyOf)(kind, discriminantKinds)) {
84
140
  continue;
85
141
  }
86
- const lSerialized = serializeIfAllowed(kind, l);
87
- const rSerialized = serializeIfAllowed(kind, r);
142
+ const lSerialized = serializeDefinitionIfAllowed(kind, l);
143
+ const rSerialized = serializeDefinitionIfAllowed(kind, r);
88
144
  if (lSerialized === undefined || rSerialized === undefined) {
89
145
  continue;
90
146
  }
@@ -102,19 +158,21 @@ const calculateDiscriminants = (branches, ctx)=>{
102
158
  continue;
103
159
  }
104
160
  const cases = discriminants.casesByDisjoint[qualifiedDisjoint];
105
- if (!(0, _genericsTs.hasKey)(cases, lSerialized)) {
161
+ const existingLBranch = cases[lSerialized];
162
+ if (!existingLBranch) {
106
163
  cases[lSerialized] = [
107
164
  lIndex
108
165
  ];
109
- } else if (!cases[lSerialized].includes(lIndex)) {
110
- cases[lSerialized].push(lIndex);
166
+ } else if (!existingLBranch.includes(lIndex)) {
167
+ existingLBranch.push(lIndex);
111
168
  }
112
- if (!(0, _genericsTs.hasKey)(cases, rSerialized)) {
169
+ const existingRBranch = cases[rSerialized];
170
+ if (!existingRBranch) {
113
171
  cases[rSerialized] = [
114
172
  rIndex
115
173
  ];
116
- } else if (!cases[rSerialized].includes(rIndex)) {
117
- cases[rSerialized].push(rIndex);
174
+ } else if (!existingRBranch.includes(rIndex)) {
175
+ existingRBranch.push(rIndex);
118
176
  }
119
177
  }
120
178
  }
@@ -142,8 +200,7 @@ const findBestDiscriminant = (remainingIndices, discriminants)=>{
142
200
  ...remainingIndices
143
201
  ];
144
202
  let score = 0;
145
- let caseKey;
146
- for(caseKey in indexCases){
203
+ for(const caseKey in indexCases){
147
204
  const filteredIndices = indexCases[caseKey].filter((i)=>{
148
205
  const remainingIndex = remainingIndices.indexOf(i);
149
206
  if (remainingIndex !== -1) {
@@ -157,7 +214,7 @@ const findBestDiscriminant = (remainingIndices, discriminants)=>{
157
214
  filteredCases[caseKey] = filteredIndices;
158
215
  score++;
159
216
  }
160
- const defaultCaseKeys = (0, _genericsTs.keysOf)(defaultCases);
217
+ const defaultCaseKeys = (0, _genericsTs.objectKeysOf)(defaultCases);
161
218
  if (defaultCaseKeys.length) {
162
219
  filteredCases["default"] = defaultCaseKeys.map((k)=>parseInt(k));
163
220
  }
@@ -179,16 +236,27 @@ const findBestDiscriminant = (remainingIndices, discriminants)=>{
179
236
  }
180
237
  return bestDiscriminant;
181
238
  };
182
- const serializeIfAllowed = (kind, data)=>kind === "value" ? serializeIfPrimitive(data) : `${data}`;
239
+ const serializeDefinitionIfAllowed = (kind, definition)=>{
240
+ switch(kind){
241
+ case "value":
242
+ return serializeIfPrimitive(definition);
243
+ case "domain":
244
+ return definition;
245
+ case "class":
246
+ return typeof definition === "string" ? definition : undefined;
247
+ default:
248
+ return;
249
+ }
250
+ };
183
251
  const serializeIfPrimitive = (data)=>{
184
252
  const domain = (0, _domainsTs.domainOf)(data);
185
253
  return domain === "object" || domain === "symbol" ? undefined : (0, _serializeTs.serializePrimitive)(data);
186
254
  };
187
- const caseSerializers = {
255
+ const serializeData = {
188
256
  value: (data)=>serializeIfPrimitive(data) ?? "default",
189
- objectKind: (data)=>(0, _objectKindsTs.objectKindOf)(data) ?? "default",
257
+ class: (data)=>(0, _objectKindsTs.objectKindOf)(data) ?? "default",
190
258
  domain: _domainsTs.domainOf
191
259
  };
192
- const serializeCase = (kind, data)=>caseSerializers[kind](data);
260
+ const serializeCase = (kind, data)=>serializeData[kind](data);
193
261
  const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph((0, _propsTs.propToNode)(prop), $)) : false;
194
262
  const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).meta.includesMorph : Object.values(node).some((predicate)=>predicate === true ? false : (0, _objectKindsTs.isArray)(predicate) ? predicate.some((branch)=>branchIncludesMorph(branch, $)) : branchIncludesMorph(predicate, $));
@@ -10,14 +10,14 @@ function _export(target, all) {
10
10
  }
11
11
  _export(exports, {
12
12
  nodeIntersection: ()=>nodeIntersection,
13
- intersection: ()=>intersection,
14
- union: ()=>union,
13
+ rootIntersection: ()=>rootIntersection,
14
+ rootUnion: ()=>rootUnion,
15
15
  flattenType: ()=>flattenType,
16
16
  flattenNode: ()=>flattenNode,
17
17
  isLiteralNode: ()=>isLiteralNode,
18
- arrayOf: ()=>arrayOf
18
+ toArrayNode: ()=>toArrayNode
19
19
  });
20
- const _astTs = require("../parse/string/ast.js");
20
+ const _intersectionTs = require("../parse/ast/intersection.js");
21
21
  const _errorsTs = require("../utils/errors.js");
22
22
  const _genericsTs = require("../utils/generics.js");
23
23
  const _pathsTs = require("../utils/paths.js");
@@ -30,7 +30,7 @@ const nodeIntersection = (l, r, state)=>{
30
30
  const rResolution = state.type.meta.scope.resolveNode(r);
31
31
  const result = resolutionIntersection(lResolution, rResolution, state);
32
32
  if (typeof result === "object" && !(0, _genericsTs.hasKeys)(result)) {
33
- return (0, _genericsTs.hasKeys)(state.disjoints) ? (0, _composeTs.anonymousDisjoint)() : state.addDisjoint("domain", (0, _genericsTs.keysOf)(lResolution), (0, _genericsTs.keysOf)(rResolution));
33
+ return (0, _genericsTs.hasKeys)(state.disjoints) ? (0, _composeTs.anonymousDisjoint)() : state.addDisjoint("domain", (0, _genericsTs.objectKeysOf)(lResolution), (0, _genericsTs.objectKeysOf)(rResolution));
34
34
  }
35
35
  return result === lResolution ? l : result === rResolution ? r : result;
36
36
  };
@@ -45,16 +45,16 @@ const resolutionIntersection = (0, _composeTs.composeKeyedIntersection)((domain,
45
45
  }, {
46
46
  onEmpty: "omit"
47
47
  });
48
- const intersection = (l, r, type)=>{
48
+ const rootIntersection = (l, r, type)=>{
49
49
  const state = new _composeTs.IntersectionState(type, "&");
50
50
  const result = nodeIntersection(l, r, state);
51
- return (0, _composeTs.isDisjoint)(result) ? (0, _errorsTs.throwParseError)((0, _astTs.compileDisjointReasonsMessage)(state.disjoints)) : (0, _composeTs.isEquality)(result) ? l : result;
51
+ return (0, _composeTs.isDisjoint)(result) ? (0, _errorsTs.throwParseError)((0, _intersectionTs.compileDisjointReasonsMessage)(state.disjoints)) : (0, _composeTs.isEquality)(result) ? l : result;
52
52
  };
53
- const union = (l, r, type)=>{
53
+ const rootUnion = (l, r, type)=>{
54
54
  const lResolution = type.meta.scope.resolveNode(l);
55
55
  const rResolution = type.meta.scope.resolveNode(r);
56
56
  const result = {};
57
- const domains = (0, _genericsTs.keysOf)({
57
+ const domains = (0, _genericsTs.objectKeysOf)({
58
58
  ...lResolution,
59
59
  ...rResolution
60
60
  });
@@ -94,7 +94,7 @@ const flattenNode = (node, ctx)=>{
94
94
  ]
95
95
  ] : resolution.flat;
96
96
  }
97
- const domains = (0, _genericsTs.keysOf)(node);
97
+ const domains = (0, _genericsTs.objectKeysOf)(node);
98
98
  if (domains.length === 1) {
99
99
  const domain = domains[0];
100
100
  const predicate = node[domain];
@@ -126,7 +126,7 @@ const flattenNode = (node, ctx)=>{
126
126
  const isLiteralNode = (node, domain)=>{
127
127
  return (0, _predicateTs.resolutionExtendsDomain)(node, domain) && (0, _predicateTs.isLiteralCondition)(node[domain]);
128
128
  };
129
- const arrayOf = (node)=>({
129
+ const toArrayNode = (node)=>({
130
130
  object: {
131
131
  class: "Array",
132
132
  props: {
@@ -87,6 +87,6 @@ const flattenPredicate = (predicate, context)=>{
87
87
  };
88
88
  const isLiteralCondition = (predicate)=>typeof predicate === "object" && "value" in predicate;
89
89
  const resolutionExtendsDomain = (resolution, domain)=>{
90
- const domains = (0, _genericsTs.keysOf)(resolution);
90
+ const domains = (0, _genericsTs.objectKeysOf)(resolution);
91
91
  return domains.length === 1 && domains[0] === domain;
92
92
  };
@@ -21,7 +21,7 @@ const classIntersection = (0, _composeTs.composeIntersection)((l, r, state)=>{
21
21
  });
22
22
  const checkClass = (expectedClass, state)=>{
23
23
  if (typeof expectedClass === "string") {
24
- return (0, _objectKindsTs.objectKindOf)(state.data) === expectedClass || state.problems.add("class", state.data, expectedClass);
24
+ return (0, _objectKindsTs.objectKindOf)(state.data) === expectedClass || !state.problems.add("class", expectedClass);
25
25
  }
26
- return state.data instanceof expectedClass || state.problems.add("class", state.data, expectedClass);
26
+ return state.data instanceof expectedClass || !state.problems.add("class", expectedClass);
27
27
  };
@@ -26,4 +26,4 @@ const greatestCommonDivisor = (l, r)=>{
26
26
  }
27
27
  return greatestCommonDivisor;
28
28
  };
29
- const checkDivisor = (divisor, state)=>state.data % divisor === 0 || state.problems.add("divisor", state.data, divisor);
29
+ const checkDivisor = (divisor, state)=>state.data % divisor === 0 || !state.problems.add("divisor", divisor);
@@ -16,7 +16,6 @@ _export(exports, {
16
16
  propsIntersection: ()=>propsIntersection,
17
17
  flattenProps: ()=>flattenProps
18
18
  });
19
- const _genericsTs = require("../../utils/generics.js");
20
19
  const _composeTs = require("../compose.js");
21
20
  const _nodeTs = require("../node.js");
22
21
  const isOptional = (prop)=>prop[0] === "?";
@@ -36,7 +35,7 @@ const propsIntersection = (0, _composeTs.composeIntersection)((l, r, state)=>{
36
35
  return result;
37
36
  }
38
37
  const lengthValue = getTupleLengthIfPresent(result);
39
- if (lengthValue === undefined || !(0, _genericsTs.hasKey)(result, mappedKeys.index)) {
38
+ if (lengthValue === undefined || !(mappedKeys.index in result)) {
40
39
  return result;
41
40
  }
42
41
  // if we are at this point, we have an array with an exact length (i.e.
@@ -17,7 +17,7 @@ _export(exports, {
17
17
  checkBound: ()=>checkBound,
18
18
  compareStrictness: ()=>compareStrictness
19
19
  });
20
- const _sizeTs = require("../../utils/size.js");
20
+ const _dataTs = require("../../utils/data.js");
21
21
  const _composeTs = require("../compose.js");
22
22
  const minComparators = {
23
23
  ">": true,
@@ -93,7 +93,7 @@ const flattenRange = (entries, range, ctx)=>{
93
93
  ]);
94
94
  }
95
95
  };
96
- const checkBound = (bound, state)=>comparatorCheckers[bound.comparator]((0, _sizeTs.sizeOf)(state.data), bound.limit) || state.problems.add("bound", state.data, bound);
96
+ const checkBound = (bound, state)=>comparatorCheckers[bound.comparator]((0, _dataTs.sizeOf)(state.data), bound.limit) || !state.problems.add("bound", bound);
97
97
  const comparatorCheckers = {
98
98
  "<": (size, limit)=>size < limit,
99
99
  ">": (size, limit)=>size > limit,
@@ -22,5 +22,5 @@ const getRegex = (source)=>{
22
22
  }
23
23
  return regexCache[source];
24
24
  };
25
- const checkRegex = (source, state)=>getRegex(source).test(state.data) || state.problems.add("regex", state.data, `/${source}/`);
25
+ const checkRegex = (source, state)=>getRegex(source).test(state.data) || !state.problems.add("regex", `/${source}/`);
26
26
  const regexIntersection = (0, _composeTs.composeIntersection)(_collapsibleSetTs.collapsibleListUnion);
@@ -41,6 +41,10 @@ const flattenRules = (rules, ctx)=>{
41
41
  for(k in rules){
42
42
  ruleFlatteners[k](entries, rules[k], ctx);
43
43
  }
44
+ // Some entries with the same precedence, e.g. morphs flattened from a list,
45
+ // rely on the fact that JS's builtin sort is stable to behave as expected
46
+ // when traversed:
47
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
44
48
  return entries.sort((l, r)=>precedenceMap[l[0]] - precedenceMap[r[0]]);
45
49
  };
46
50
  const ruleFlatteners = {
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "writeUnboundableMessage", {
6
+ enumerable: true,
7
+ get: ()=>writeUnboundableMessage
8
+ });
9
+ const writeUnboundableMessage = (root)=>`Bounded expression ${root} must be a number, string or array`;
@@ -18,7 +18,7 @@ const _genericsTs = require("../../utils/generics.js");
18
18
  const _serializeTs = require("../../utils/serialize.js");
19
19
  const writeMalformedDistributableFunctionMessage = (def)=>`Expected a Function or Record<Domain, Function> operand (${(0, _serializeTs.stringify)(def)} was invalid)`;
20
20
  const distributeFunctionToNode = (distributableFunction, inputNode, ctx, ruleKey)=>{
21
- const domains = (0, _genericsTs.keysOf)(ctx.type.meta.scope.resolveNode(inputNode));
21
+ const domains = (0, _genericsTs.objectKeysOf)(ctx.type.meta.scope.resolveNode(inputNode));
22
22
  if (!(0, _domainsTs.hasDomain)(distributableFunction, "object")) {
23
23
  return (0, _errorsTs.throwParseError)(writeMalformedDistributableFunctionMessage(distributableFunction));
24
24
  }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "writeIndivisibleMessage", {
6
+ enumerable: true,
7
+ get: ()=>writeIndivisibleMessage
8
+ });
9
+ const writeIndivisibleMessage = (root)=>`Divisibility operand ${root} must be a number`;
@@ -10,15 +10,12 @@ function _export(target, all) {
10
10
  }
11
11
  _export(exports, {
12
12
  compileDisjointReasonsMessage: ()=>compileDisjointReasonsMessage,
13
- writeImplicitNeverMessage: ()=>writeImplicitNeverMessage,
14
- writeUndiscriminatableMorphUnionMessage: ()=>writeUndiscriminatableMorphUnionMessage,
15
- writeIndivisibleMessage: ()=>writeIndivisibleMessage,
16
- writeUnboundableMessage: ()=>writeUnboundableMessage
13
+ writeImplicitNeverMessage: ()=>writeImplicitNeverMessage
17
14
  });
18
15
  const _composeTs = require("../../nodes/compose.js");
19
16
  const _genericsTs = require("../../utils/generics.js");
20
17
  const compileDisjointReasonsMessage = (disjoints)=>{
21
- const paths = (0, _genericsTs.keysOf)(disjoints);
18
+ const paths = (0, _genericsTs.objectKeysOf)(disjoints);
22
19
  if (paths.length === 1) {
23
20
  const path = paths[0];
24
21
  return `${path === "/" ? "" : `At ${path}: `}Intersection of ${_composeTs.disjointDescriptionWriters[disjoints[path].kind](disjoints[path])} results in an unsatisfiable type`;
@@ -31,6 +28,3 @@ const compileDisjointReasonsMessage = (disjoints)=>{
31
28
  return message;
32
29
  };
33
30
  const writeImplicitNeverMessage = (path, operator, description)=>`${path.length ? `At ${path}: ` : ""}${operator} ${description ? `${description} ` : ""}results in an unsatisfiable type`;
34
- const writeUndiscriminatableMorphUnionMessage = (path)=>`${path === "/" ? "A" : `At ${path}, a`} union including one or more morphs must be discriminatable`;
35
- const writeIndivisibleMessage = (root)=>`Divisibility operand ${root} must be a number`;
36
- const writeUnboundableMessage = (root)=>`Bounded expression ${root} must be a number, string or array`;
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "parseKeyOfTuple", {
6
+ enumerable: true,
7
+ get: ()=>parseKeyOfTuple
8
+ });
9
+ const _propsTs = require("../../nodes/rules/props.js");
10
+ const _errorsTs = require("../../utils/errors.js");
11
+ const _freezeTs = require("../../utils/freeze.js");
12
+ const _genericsTs = require("../../utils/generics.js");
13
+ const _numericLiteralsTs = require("../../utils/numericLiterals.js");
14
+ const _objectKindsTs = require("../../utils/objectKinds.js");
15
+ const _serializeTs = require("../../utils/serialize.js");
16
+ const _definitionTs = require("../definition.js");
17
+ const _intersectionTs = require("./intersection.js");
18
+ var _keyNode, _keyType, _keyNode1, _keyNode2;
19
+ const arrayIndexStringBranch = (0, _freezeTs.deepFreeze)({
20
+ regex: _numericLiteralsTs.wellFormedNonNegativeIntegerMatcher.source
21
+ });
22
+ const arrayIndexNumberBranch = (0, _freezeTs.deepFreeze)({
23
+ range: {
24
+ min: {
25
+ comparator: ">=",
26
+ limit: 1
27
+ }
28
+ },
29
+ divisor: 1
30
+ });
31
+ const parseKeyOfTuple = (def, ctx)=>{
32
+ const resolution = ctx.type.meta.scope.resolveNode((0, _definitionTs.parseDefinition)(def[1], ctx));
33
+ const predicateKeys = (0, _genericsTs.objectKeysOf)(resolution).map((domain)=>keysOfPredicate(domain, resolution[domain]));
34
+ const sharedKeys = sharedKeysOf(predicateKeys);
35
+ if (!sharedKeys.length) {
36
+ return (0, _intersectionTs.writeImplicitNeverMessage)(ctx.path, "keyof");
37
+ }
38
+ const keyNode = {};
39
+ for (const key of sharedKeys){
40
+ const keyType = typeof key;
41
+ if (keyType === "string" || keyType === "number" || keyType === "symbol") {
42
+ (_keyNode = keyNode)[_keyType = keyType] ?? (_keyNode[_keyType] = []);
43
+ keyNode[keyType].push({
44
+ value: key
45
+ });
46
+ } else if (key === _numericLiteralsTs.wellFormedNonNegativeIntegerMatcher) {
47
+ (_keyNode1 = keyNode).string ?? (_keyNode1.string = []);
48
+ keyNode.string.push(arrayIndexStringBranch);
49
+ (_keyNode2 = keyNode).number ?? (_keyNode2.number = []);
50
+ keyNode.number.push(arrayIndexNumberBranch);
51
+ } else {
52
+ return (0, _errorsTs.throwInternalError)(`Unexpected keyof key '${(0, _serializeTs.stringify)(key)}'`);
53
+ }
54
+ }
55
+ return Object.fromEntries(Object.entries(keyNode).map(([domain, branches])=>[
56
+ domain,
57
+ branches.length === 1 ? branches[0] : branches
58
+ ]));
59
+ };
60
+ const baseKeysByDomain = {
61
+ bigint: (0, _genericsTs.prototypeKeysOf)(0n),
62
+ boolean: (0, _genericsTs.prototypeKeysOf)(false),
63
+ null: [],
64
+ number: (0, _genericsTs.prototypeKeysOf)(0),
65
+ // TS doesn't include the Object prototype in keyof, so keyof object is never
66
+ object: [],
67
+ string: (0, _genericsTs.prototypeKeysOf)(""),
68
+ symbol: (0, _genericsTs.prototypeKeysOf)(Symbol()),
69
+ undefined: []
70
+ };
71
+ const keysOfPredicate = (domain, predicate)=>domain !== "object" || predicate === true ? baseKeysByDomain[domain] : sharedKeysOf((0, _genericsTs.listFrom)(predicate).map((branch)=>keysOfObjectBranch(branch)));
72
+ const sharedKeysOf = (keyBranches)=>{
73
+ if (!keyBranches.length) {
74
+ return [];
75
+ }
76
+ let sharedKeys = keyBranches[0];
77
+ for(let i = 1; i < keyBranches.length; i++){
78
+ // we can filter directly by equality here because the RegExp we're
79
+ // using will always be reference equal to
80
+ // wellFormedNonNegativeIntegerMatcher
81
+ sharedKeys = sharedKeys.filter((k)=>keyBranches[i].includes(k));
82
+ }
83
+ return sharedKeys;
84
+ };
85
+ const keysOfObjectBranch = (branch)=>{
86
+ const result = [];
87
+ if ("props" in branch) {
88
+ for (const key of Object.keys(branch.props)){
89
+ if (key === _propsTs.mappedKeys.index) {
90
+ result.push(_numericLiteralsTs.wellFormedNonNegativeIntegerMatcher);
91
+ } else if (!result.includes(key)) {
92
+ result.push(key);
93
+ if (_numericLiteralsTs.wellFormedNonNegativeIntegerMatcher.test(key)) {
94
+ // allow numeric access to keys
95
+ result.push((0, _numericLiteralsTs.tryParseWellFormedInteger)(key, `Unexpectedly failed to parse an integer from key '${key}'`));
96
+ }
97
+ }
98
+ }
99
+ }
100
+ if ("class" in branch) {
101
+ const constructor = typeof branch.class === "string" ? _objectKindsTs.defaultObjectKinds[branch.class] : branch.class;
102
+ for (const key1 of (0, _genericsTs.prototypeKeysOf)(constructor.prototype)){
103
+ if (!result.includes(key1)) {
104
+ result.push(key1);
105
+ }
106
+ }
107
+ }
108
+ return result;
109
+ };
@@ -54,4 +54,4 @@ const applyMorph = (branch, morph)=>(0, _branchTs.isMorphBranch)(branch) ? {
54
54
  rules: branch,
55
55
  morph
56
56
  };
57
- const writeMalformedMorphExpressionMessage = (value)=>`Morph expression requires a function following '=>' (was ${typeof value})`;
57
+ const writeMalformedMorphExpressionMessage = (value)=>`Morph expression requires a function following '|>' (was ${typeof value})`;
@@ -8,8 +8,8 @@ Object.defineProperty(exports, "parseNarrowTuple", {
8
8
  });
9
9
  const _nodeTs = require("../../nodes/node.js");
10
10
  const _definitionTs = require("../definition.js");
11
- const _utilsTs = require("./utils.js");
11
+ const _distributableFunctionTs = require("./distributableFunction.js");
12
12
  const parseNarrowTuple = (def, ctx)=>{
13
13
  const inputNode = (0, _definitionTs.parseDefinition)(def[0], ctx);
14
- return (0, _nodeTs.intersection)(inputNode, (0, _utilsTs.distributeFunctionToNode)(def[2], inputNode, ctx, "narrow"), ctx.type);
14
+ return (0, _nodeTs.rootIntersection)(inputNode, (0, _distributableFunctionTs.distributeFunctionToNode)(def[2], inputNode, ctx, "narrow"), ctx.type);
15
15
  };