arktype 1.0.28-alpha → 1.1.0-dev-1

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 (194) hide show
  1. package/dist/main.d.ts +1018 -0
  2. package/dist/main.js +1450 -0
  3. package/package.json +36 -108
  4. package/LICENSE +0 -7
  5. package/README.md +0 -351
  6. package/dist/cjs/main.js +0 -38
  7. package/dist/cjs/nodes/branch.js +0 -140
  8. package/dist/cjs/nodes/compose.js +0 -163
  9. package/dist/cjs/nodes/discriminate.js +0 -265
  10. package/dist/cjs/nodes/node.js +0 -139
  11. package/dist/cjs/nodes/predicate.js +0 -86
  12. package/dist/cjs/nodes/rules/class.js +0 -25
  13. package/dist/cjs/nodes/rules/collapsibleSet.js +0 -48
  14. package/dist/cjs/nodes/rules/divisor.js +0 -29
  15. package/dist/cjs/nodes/rules/props.js +0 -167
  16. package/dist/cjs/nodes/rules/range.js +0 -105
  17. package/dist/cjs/nodes/rules/regex.js +0 -26
  18. package/dist/cjs/nodes/rules/rules.js +0 -123
  19. package/dist/cjs/package.json +0 -3
  20. package/dist/cjs/parse/ast/ast.js +0 -4
  21. package/dist/cjs/parse/ast/bound.js +0 -16
  22. package/dist/cjs/parse/ast/config.js +0 -13
  23. package/dist/cjs/parse/ast/distributableFunction.js +0 -48
  24. package/dist/cjs/parse/ast/divisor.js +0 -9
  25. package/dist/cjs/parse/ast/intersection.js +0 -30
  26. package/dist/cjs/parse/ast/keyof.js +0 -111
  27. package/dist/cjs/parse/ast/morph.js +0 -58
  28. package/dist/cjs/parse/ast/narrow.js +0 -22
  29. package/dist/cjs/parse/ast/node.js +0 -4
  30. package/dist/cjs/parse/ast/tuple.js +0 -94
  31. package/dist/cjs/parse/ast/union.js +0 -9
  32. package/dist/cjs/parse/ast/utils.js +0 -4
  33. package/dist/cjs/parse/definition.js +0 -61
  34. package/dist/cjs/parse/record.js +0 -37
  35. package/dist/cjs/parse/string/reduce/dynamic.js +0 -167
  36. package/dist/cjs/parse/string/reduce/shared.js +0 -23
  37. package/dist/cjs/parse/string/reduce/static.js +0 -4
  38. package/dist/cjs/parse/string/shift/operand/enclosed.js +0 -54
  39. package/dist/cjs/parse/string/shift/operand/operand.js +0 -11
  40. package/dist/cjs/parse/string/shift/operand/unenclosed.js +0 -53
  41. package/dist/cjs/parse/string/shift/operator/bounds.js +0 -87
  42. package/dist/cjs/parse/string/shift/operator/divisor.js +0 -35
  43. package/dist/cjs/parse/string/shift/operator/operator.js +0 -26
  44. package/dist/cjs/parse/string/shift/scanner.js +0 -114
  45. package/dist/cjs/parse/string/string.js +0 -43
  46. package/dist/cjs/scopes/ark.js +0 -45
  47. package/dist/cjs/scopes/cache.js +0 -52
  48. package/dist/cjs/scopes/expressions.js +0 -29
  49. package/dist/cjs/scopes/jsObjects.js +0 -93
  50. package/dist/cjs/scopes/scope.js +0 -323
  51. package/dist/cjs/scopes/tsKeywords.js +0 -113
  52. package/dist/cjs/scopes/type.js +0 -50
  53. package/dist/cjs/scopes/validation/creditCard.js +0 -47
  54. package/dist/cjs/scopes/validation/date.js +0 -62
  55. package/dist/cjs/scopes/validation/validation.js +0 -108
  56. package/dist/cjs/traverse/problems.js +0 -281
  57. package/dist/cjs/traverse/traverse.js +0 -390
  58. package/dist/cjs/utils/chainableNoOpProxy.js +0 -23
  59. package/dist/cjs/utils/data.js +0 -53
  60. package/dist/cjs/utils/domains.js +0 -30
  61. package/dist/cjs/utils/errors.js +0 -26
  62. package/dist/cjs/utils/freeze.js +0 -15
  63. package/dist/cjs/utils/generics.js +0 -63
  64. package/dist/cjs/utils/numericLiterals.js +0 -75
  65. package/dist/cjs/utils/objectKinds.js +0 -69
  66. package/dist/cjs/utils/paths.js +0 -32
  67. package/dist/cjs/utils/serialize.js +0 -74
  68. package/dist/cjs/utils/unionToTuple.js +0 -4
  69. package/dist/mjs/main.js +0 -8
  70. package/dist/mjs/nodes/branch.js +0 -123
  71. package/dist/mjs/nodes/compose.js +0 -141
  72. package/dist/mjs/nodes/discriminate.js +0 -250
  73. package/dist/mjs/nodes/node.js +0 -117
  74. package/dist/mjs/nodes/predicate.js +0 -69
  75. package/dist/mjs/nodes/rules/class.js +0 -11
  76. package/dist/mjs/nodes/rules/collapsibleSet.js +0 -34
  77. package/dist/mjs/nodes/rules/divisor.js +0 -15
  78. package/dist/mjs/nodes/rules/props.js +0 -149
  79. package/dist/mjs/nodes/rules/range.js +0 -86
  80. package/dist/mjs/nodes/rules/regex.js +0 -11
  81. package/dist/mjs/nodes/rules/rules.js +0 -106
  82. package/dist/mjs/package.json +0 -3
  83. package/dist/mjs/parse/ast/ast.js +0 -1
  84. package/dist/mjs/parse/ast/bound.js +0 -2
  85. package/dist/mjs/parse/ast/config.js +0 -9
  86. package/dist/mjs/parse/ast/distributableFunction.js +0 -34
  87. package/dist/mjs/parse/ast/divisor.js +0 -1
  88. package/dist/mjs/parse/ast/intersection.js +0 -16
  89. package/dist/mjs/parse/ast/keyof.js +0 -103
  90. package/dist/mjs/parse/ast/morph.js +0 -44
  91. package/dist/mjs/parse/ast/narrow.js +0 -14
  92. package/dist/mjs/parse/ast/node.js +0 -1
  93. package/dist/mjs/parse/ast/tuple.js +0 -80
  94. package/dist/mjs/parse/ast/union.js +0 -1
  95. package/dist/mjs/parse/ast/utils.js +0 -1
  96. package/dist/mjs/parse/definition.js +0 -46
  97. package/dist/mjs/parse/record.js +0 -29
  98. package/dist/mjs/parse/string/reduce/dynamic.js +0 -159
  99. package/dist/mjs/parse/string/reduce/shared.js +0 -6
  100. package/dist/mjs/parse/string/reduce/static.js +0 -1
  101. package/dist/mjs/parse/string/shift/operand/enclosed.js +0 -39
  102. package/dist/mjs/parse/string/shift/operand/operand.js +0 -3
  103. package/dist/mjs/parse/string/shift/operand/unenclosed.js +0 -36
  104. package/dist/mjs/parse/string/shift/operator/bounds.js +0 -70
  105. package/dist/mjs/parse/string/shift/operator/divisor.js +0 -21
  106. package/dist/mjs/parse/string/shift/operator/operator.js +0 -13
  107. package/dist/mjs/parse/string/shift/scanner.js +0 -106
  108. package/dist/mjs/parse/string/string.js +0 -28
  109. package/dist/mjs/scopes/ark.js +0 -28
  110. package/dist/mjs/scopes/cache.js +0 -38
  111. package/dist/mjs/scopes/expressions.js +0 -89
  112. package/dist/mjs/scopes/jsObjects.js +0 -82
  113. package/dist/mjs/scopes/scope.js +0 -303
  114. package/dist/mjs/scopes/tsKeywords.js +0 -116
  115. package/dist/mjs/scopes/type.js +0 -35
  116. package/dist/mjs/scopes/validation/creditCard.js +0 -34
  117. package/dist/mjs/scopes/validation/date.js +0 -48
  118. package/dist/mjs/scopes/validation/validation.js +0 -112
  119. package/dist/mjs/traverse/problems.js +0 -260
  120. package/dist/mjs/traverse/traverse.js +0 -373
  121. package/dist/mjs/utils/chainableNoOpProxy.js +0 -15
  122. package/dist/mjs/utils/data.js +0 -38
  123. package/dist/mjs/utils/domains.js +0 -15
  124. package/dist/mjs/utils/errors.js +0 -10
  125. package/dist/mjs/utils/freeze.js +0 -7
  126. package/dist/mjs/utils/generics.js +0 -38
  127. package/dist/mjs/utils/numericLiterals.js +0 -72
  128. package/dist/mjs/utils/objectKinds.js +0 -55
  129. package/dist/mjs/utils/paths.js +0 -18
  130. package/dist/mjs/utils/serialize.js +0 -58
  131. package/dist/mjs/utils/unionToTuple.js +0 -1
  132. package/dist/tsconfig.temp.tsbuildinfo +0 -1
  133. package/dist/types/main.d.ts +0 -15
  134. package/dist/types/nodes/branch.d.ts +0 -29
  135. package/dist/types/nodes/compose.d.ts +0 -98
  136. package/dist/types/nodes/discriminate.d.ts +0 -26
  137. package/dist/types/nodes/node.d.ts +0 -69
  138. package/dist/types/nodes/predicate.d.ts +0 -18
  139. package/dist/types/nodes/rules/class.d.ts +0 -4
  140. package/dist/types/nodes/rules/collapsibleSet.d.ts +0 -4
  141. package/dist/types/nodes/rules/divisor.d.ts +0 -3
  142. package/dist/types/nodes/rules/props.d.ts +0 -43
  143. package/dist/types/nodes/rules/range.d.ts +0 -34
  144. package/dist/types/nodes/rules/regex.d.ts +0 -5
  145. package/dist/types/nodes/rules/rules.d.ts +0 -33
  146. package/dist/types/parse/ast/ast.d.ts +0 -24
  147. package/dist/types/parse/ast/bound.d.ts +0 -27
  148. package/dist/types/parse/ast/config.d.ts +0 -15
  149. package/dist/types/parse/ast/distributableFunction.d.ts +0 -18
  150. package/dist/types/parse/ast/divisor.d.ts +0 -13
  151. package/dist/types/parse/ast/intersection.d.ts +0 -31
  152. package/dist/types/parse/ast/keyof.d.ts +0 -10
  153. package/dist/types/parse/ast/morph.d.ts +0 -18
  154. package/dist/types/parse/ast/narrow.d.ts +0 -21
  155. package/dist/types/parse/ast/node.d.ts +0 -35
  156. package/dist/types/parse/ast/tuple.d.ts +0 -41
  157. package/dist/types/parse/ast/union.d.ts +0 -17
  158. package/dist/types/parse/ast/utils.d.ts +0 -7
  159. package/dist/types/parse/definition.d.ts +0 -30
  160. package/dist/types/parse/record.d.ts +0 -26
  161. package/dist/types/parse/string/reduce/dynamic.d.ts +0 -34
  162. package/dist/types/parse/string/reduce/shared.d.ts +0 -25
  163. package/dist/types/parse/string/reduce/static.d.ts +0 -130
  164. package/dist/types/parse/string/shift/operand/enclosed.d.ts +0 -28
  165. package/dist/types/parse/string/shift/operand/operand.d.ts +0 -9
  166. package/dist/types/parse/string/shift/operand/unenclosed.d.ts +0 -18
  167. package/dist/types/parse/string/shift/operator/bounds.d.ts +0 -21
  168. package/dist/types/parse/string/shift/operator/divisor.d.ts +0 -13
  169. package/dist/types/parse/string/shift/operator/operator.d.ts +0 -13
  170. package/dist/types/parse/string/shift/scanner.d.ts +0 -89
  171. package/dist/types/parse/string/string.d.ts +0 -25
  172. package/dist/types/scopes/ark.d.ts +0 -209
  173. package/dist/types/scopes/cache.d.ts +0 -14
  174. package/dist/types/scopes/expressions.d.ts +0 -136
  175. package/dist/types/scopes/jsObjects.d.ts +0 -26
  176. package/dist/types/scopes/scope.d.ts +0 -99
  177. package/dist/types/scopes/tsKeywords.d.ts +0 -50
  178. package/dist/types/scopes/type.d.ts +0 -67
  179. package/dist/types/scopes/validation/creditCard.d.ts +0 -2
  180. package/dist/types/scopes/validation/date.d.ts +0 -23
  181. package/dist/types/scopes/validation/validation.d.ts +0 -49
  182. package/dist/types/traverse/problems.d.ts +0 -88
  183. package/dist/types/traverse/traverse.d.ts +0 -55
  184. package/dist/types/utils/chainableNoOpProxy.d.ts +0 -14
  185. package/dist/types/utils/data.d.ts +0 -13
  186. package/dist/types/utils/domains.d.ts +0 -32
  187. package/dist/types/utils/errors.d.ts +0 -6
  188. package/dist/types/utils/freeze.d.ts +0 -2
  189. package/dist/types/utils/generics.d.ts +0 -125
  190. package/dist/types/utils/numericLiterals.d.ts +0 -46
  191. package/dist/types/utils/objectKinds.d.ts +0 -84
  192. package/dist/types/utils/paths.d.ts +0 -9
  193. package/dist/types/utils/serialize.d.ts +0 -37
  194. package/dist/types/utils/unionToTuple.d.ts +0 -11
@@ -1,163 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: all[name]
9
- });
10
- }
11
- _export(exports, {
12
- composeIntersection: ()=>composeIntersection,
13
- undefinedOperandsMessage: ()=>undefinedOperandsMessage,
14
- disjointDescriptionWriters: ()=>disjointDescriptionWriters,
15
- stringifyRange: ()=>stringifyRange,
16
- IntersectionState: ()=>IntersectionState,
17
- anonymousDisjoint: ()=>anonymousDisjoint,
18
- isDisjoint: ()=>isDisjoint,
19
- equality: ()=>equality,
20
- isEquality: ()=>isEquality,
21
- composeKeyedIntersection: ()=>composeKeyedIntersection
22
- });
23
- const _errorsJs = require("../utils/errors.js");
24
- const _genericsJs = require("../utils/generics.js");
25
- const _pathsJs = require("../utils/paths.js");
26
- const _serializeJs = require("../utils/serialize.js");
27
- function _checkPrivateRedeclaration(obj, privateCollection) {
28
- if (privateCollection.has(obj)) {
29
- throw new TypeError("Cannot initialize the same private elements twice on an object");
30
- }
31
- }
32
- function _classApplyDescriptorGet(receiver, descriptor) {
33
- if (descriptor.get) {
34
- return descriptor.get.call(receiver);
35
- }
36
- return descriptor.value;
37
- }
38
- function _classApplyDescriptorSet(receiver, descriptor, value) {
39
- if (descriptor.set) {
40
- descriptor.set.call(receiver, value);
41
- } else {
42
- if (!descriptor.writable) {
43
- throw new TypeError("attempted to set read only private field");
44
- }
45
- descriptor.value = value;
46
- }
47
- }
48
- function _classExtractFieldDescriptor(receiver, privateMap, action) {
49
- if (!privateMap.has(receiver)) {
50
- throw new TypeError("attempted to " + action + " private field on non-instance");
51
- }
52
- return privateMap.get(receiver);
53
- }
54
- function _classPrivateFieldGet(receiver, privateMap) {
55
- var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get");
56
- return _classApplyDescriptorGet(receiver, descriptor);
57
- }
58
- function _classPrivateFieldInit(obj, privateMap, value) {
59
- _checkPrivateRedeclaration(obj, privateMap);
60
- privateMap.set(obj, value);
61
- }
62
- function _classPrivateFieldSet(receiver, privateMap, value) {
63
- var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set");
64
- _classApplyDescriptorSet(receiver, descriptor, value);
65
- return value;
66
- }
67
- function _defineProperty(obj, key, value) {
68
- if (key in obj) {
69
- Object.defineProperty(obj, key, {
70
- value: value,
71
- enumerable: true,
72
- configurable: true,
73
- writable: true
74
- });
75
- } else {
76
- obj[key] = value;
77
- }
78
- return obj;
79
- }
80
- const composeIntersection = (reducer)=>(l, r, state)=>l === undefined ? r === undefined ? (0, _errorsJs.throwInternalError)(undefinedOperandsMessage) : r : r === undefined ? l : reducer(l, r, state);
81
- const undefinedOperandsMessage = `Unexpected operation two undefined operands`;
82
- const disjointDescriptionWriters = {
83
- domain: ({ l , r })=>`${l.join(", ")} and ${r.join(", ")}`,
84
- range: ({ l , r })=>`${stringifyRange(l)} and ${stringifyRange(r)}`,
85
- class: ({ l , r })=>`classes ${typeof l === "string" ? l : l.name} and ${typeof r === "string" ? r : r.name}`,
86
- tupleLength: ({ l , r })=>`tuples of length ${l} and ${r}`,
87
- value: ({ l , r })=>`literal values ${(0, _serializeJs.stringify)(l)} and ${(0, _serializeJs.stringify)(r)}`,
88
- leftAssignability: ({ l , r })=>`literal value ${(0, _serializeJs.stringify)(l.value)} and ${(0, _serializeJs.stringify)(r)}`,
89
- rightAssignability: ({ l , r })=>`literal value ${(0, _serializeJs.stringify)(r.value)} and ${(0, _serializeJs.stringify)(l)}`,
90
- union: ({ l , r })=>`branches ${(0, _serializeJs.stringify)(l)} and branches ${(0, _serializeJs.stringify)(r)}`
91
- };
92
- const stringifyRange = (range)=>"limit" in range ? `the range of exactly ${range.limit}` : range.min ? range.max ? `the range bounded by ${range.min.comparator}${range.min.limit} and ${range.max.comparator}${range.max.limit}` : `${range.min.comparator}${range.min.limit}` : range.max ? `${range.max.comparator}${range.max.limit}` : "the unbounded range";
93
- var _disjoints = /*#__PURE__*/ new WeakMap();
94
- class IntersectionState {
95
- get disjoints() {
96
- return _classPrivateFieldGet(this, _disjoints);
97
- }
98
- addDisjoint(kind, l, r) {
99
- _classPrivateFieldGet(this, _disjoints)[`${this.path}`] = {
100
- kind,
101
- l,
102
- r,
103
- lOptional: this.lOptional,
104
- rOptional: this.rOptional
105
- };
106
- return empty;
107
- }
108
- constructor(type, lastOperator){
109
- _defineProperty(this, "type", void 0);
110
- _defineProperty(this, "lastOperator", void 0);
111
- _defineProperty(this, "path", void 0);
112
- _defineProperty(this, "lOptional", void 0);
113
- _defineProperty(this, "rOptional", void 0);
114
- _defineProperty(this, "domain", void 0);
115
- _classPrivateFieldInit(this, _disjoints, {
116
- writable: true,
117
- value: void 0
118
- });
119
- this.type = type;
120
- this.lastOperator = lastOperator;
121
- this.path = new _pathsJs.Path();
122
- this.lOptional = false;
123
- this.rOptional = false;
124
- _classPrivateFieldSet(this, _disjoints, {});
125
- }
126
- }
127
- const empty = Symbol("empty");
128
- const anonymousDisjoint = ()=>empty;
129
- const isDisjoint = (result)=>result === empty;
130
- const equal = Symbol("equal");
131
- const equality = ()=>equal;
132
- const isEquality = (result)=>result === equal;
133
- const composeKeyedIntersection = (reducer, config)=>(l, r, state)=>{
134
- const result = {};
135
- const keys = (0, _genericsJs.objectKeysOf)({
136
- ...l,
137
- ...r
138
- });
139
- let lImpliesR = true;
140
- let rImpliesL = true;
141
- for (const k of keys){
142
- const keyResult = typeof reducer === "function" ? reducer(k, l[k], r[k], state) : reducer[k](l[k], r[k], state);
143
- if (isEquality(keyResult)) {
144
- if (l[k] !== undefined) {
145
- result[k] = l[k];
146
- }
147
- } else if (isDisjoint(keyResult)) {
148
- if (config.onEmpty === "omit") {
149
- lImpliesR = false;
150
- rImpliesL = false;
151
- } else {
152
- return empty;
153
- }
154
- } else {
155
- if (keyResult !== undefined) {
156
- result[k] = keyResult;
157
- }
158
- lImpliesR && (lImpliesR = keyResult === l[k]);
159
- rImpliesL && (rImpliesL = keyResult === r[k]);
160
- }
161
- }
162
- return lImpliesR ? rImpliesL ? equality() : l : rImpliesL ? r : result;
163
- };
@@ -1,265 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: all[name]
9
- });
10
- }
11
- _export(exports, {
12
- flattenBranches: ()=>flattenBranches,
13
- serializeDefinitionIfAllowed: ()=>serializeDefinitionIfAllowed,
14
- serializeCase: ()=>serializeCase
15
- });
16
- const _unionJs = require("../parse/ast/union.js");
17
- const _domainsJs = require("../utils/domains.js");
18
- const _errorsJs = require("../utils/errors.js");
19
- const _genericsJs = require("../utils/generics.js");
20
- const _objectKindsJs = require("../utils/objectKinds.js");
21
- const _pathsJs = require("../utils/paths.js");
22
- const _serializeJs = require("../utils/serialize.js");
23
- const _branchJs = require("./branch.js");
24
- const _composeJs = require("./compose.js");
25
- const _propsJs = require("./rules/props.js");
26
- const flattenBranches = (branches, ctx)=>{
27
- const discriminants = calculateDiscriminants(branches, ctx);
28
- const indices = branches.map((_, i)=>i);
29
- return discriminate(branches, indices, discriminants, ctx);
30
- };
31
- const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
32
- if (remainingIndices.length === 1) {
33
- return (0, _branchJs.flattenBranch)(originalBranches[remainingIndices[0]], ctx);
34
- }
35
- const bestDiscriminant = findBestDiscriminant(remainingIndices, discriminants);
36
- if (!bestDiscriminant) {
37
- return [
38
- [
39
- "branches",
40
- remainingIndices.map((i)=>branchIncludesMorph(originalBranches[i], ctx.type.scope) ? (0, _errorsJs.throwParseError)((0, _unionJs.writeUndiscriminatableMorphUnionMessage)(`${ctx.path}`)) : (0, _branchJs.flattenBranch)(originalBranches[i], ctx))
41
- ]
42
- ];
43
- }
44
- const cases = {};
45
- for(const caseKey in bestDiscriminant.indexCases){
46
- const nextIndices = bestDiscriminant.indexCases[caseKey];
47
- cases[caseKey] = discriminate(originalBranches, nextIndices, discriminants, ctx);
48
- if (caseKey !== "default") {
49
- pruneDiscriminant(cases[caseKey], bestDiscriminant.path, bestDiscriminant, ctx);
50
- }
51
- }
52
- return [
53
- [
54
- "switch",
55
- {
56
- path: bestDiscriminant.path,
57
- kind: bestDiscriminant.kind,
58
- cases
59
- }
60
- ]
61
- ];
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 throwInternalPruneFailure(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
- /* c8 ignore next */ if ((0, _genericsJs.keyCount)(v) !== 1 || !v.object) {
98
- return throwInternalPruneFailure(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 throwInternalPruneFailure(discriminant);
115
- };
116
- const throwInternalPruneFailure = (discriminant)=>(0, _errorsJs.throwInternalError)(`Unexpectedly failed to discriminate ${discriminant.kind} at path '${discriminant.path}'`);
117
- const discriminantKinds = {
118
- domain: true,
119
- class: true,
120
- value: true
121
- };
122
- const calculateDiscriminants = (branches, ctx)=>{
123
- const discriminants = {
124
- disjointsByPair: {},
125
- casesByDisjoint: {}
126
- };
127
- for(let lIndex = 0; lIndex < branches.length - 1; lIndex++){
128
- for(let rIndex = lIndex + 1; rIndex < branches.length; rIndex++){
129
- const pairKey = `${lIndex}/${rIndex}`;
130
- const pairDisjoints = [];
131
- discriminants.disjointsByPair[pairKey] = pairDisjoints;
132
- const intersectionState = new _composeJs.IntersectionState(ctx.type, "|");
133
- (0, _branchJs.branchIntersection)(branches[lIndex], branches[rIndex], intersectionState);
134
- for(const path in intersectionState.disjoints){
135
- if (path.includes(_propsJs.mappedKeys.index)) {
136
- continue;
137
- }
138
- const { l , r , kind , lOptional , rOptional } = intersectionState.disjoints[path];
139
- if (!(0, _genericsJs.isKeyOf)(kind, discriminantKinds)) {
140
- continue;
141
- }
142
- if (lOptional || rOptional) {
143
- continue;
144
- }
145
- const lSerialized = serializeDefinitionIfAllowed(kind, l);
146
- const rSerialized = serializeDefinitionIfAllowed(kind, r);
147
- if (lSerialized === undefined || rSerialized === undefined) {
148
- continue;
149
- }
150
- const qualifiedDisjoint = path === "/" ? kind : `${path}/${kind}`;
151
- pairDisjoints.push(qualifiedDisjoint);
152
- if (!discriminants.casesByDisjoint[qualifiedDisjoint]) {
153
- discriminants.casesByDisjoint[qualifiedDisjoint] = {
154
- [lSerialized]: [
155
- lIndex
156
- ],
157
- [rSerialized]: [
158
- rIndex
159
- ]
160
- };
161
- continue;
162
- }
163
- const cases = discriminants.casesByDisjoint[qualifiedDisjoint];
164
- const existingLBranch = cases[lSerialized];
165
- if (!existingLBranch) {
166
- cases[lSerialized] = [
167
- lIndex
168
- ];
169
- } else if (!existingLBranch.includes(lIndex)) {
170
- existingLBranch.push(lIndex);
171
- }
172
- const existingRBranch = cases[rSerialized];
173
- if (!existingRBranch) {
174
- cases[rSerialized] = [
175
- rIndex
176
- ];
177
- } else if (!existingRBranch.includes(rIndex)) {
178
- existingRBranch.push(rIndex);
179
- }
180
- }
181
- }
182
- }
183
- return discriminants;
184
- };
185
- const parseQualifiedDisjoint = (qualifiedDisjoint)=>{
186
- const path = _pathsJs.Path.fromString(qualifiedDisjoint);
187
- return [
188
- path,
189
- path.pop()
190
- ];
191
- };
192
- const findBestDiscriminant = (remainingIndices, discriminants)=>{
193
- let bestDiscriminant;
194
- for(let i = 0; i < remainingIndices.length - 1; i++){
195
- const lIndex = remainingIndices[i];
196
- for(let j = i + 1; j < remainingIndices.length; j++){
197
- const rIndex = remainingIndices[j];
198
- const candidates = discriminants.disjointsByPair[`${lIndex}/${rIndex}`];
199
- for (const qualifiedDisjoint of candidates){
200
- const indexCases = discriminants.casesByDisjoint[qualifiedDisjoint];
201
- const filteredCases = {};
202
- const defaultCases = [
203
- ...remainingIndices
204
- ];
205
- let score = 0;
206
- for(const caseKey in indexCases){
207
- const filteredIndices = indexCases[caseKey].filter((i)=>{
208
- const remainingIndex = remainingIndices.indexOf(i);
209
- if (remainingIndex !== -1) {
210
- delete defaultCases[remainingIndex];
211
- return true;
212
- }
213
- });
214
- if (filteredIndices.length === 0) {
215
- continue;
216
- }
217
- filteredCases[caseKey] = filteredIndices;
218
- score++;
219
- }
220
- const defaultCaseKeys = (0, _genericsJs.objectKeysOf)(defaultCases);
221
- if (defaultCaseKeys.length) {
222
- filteredCases["default"] = defaultCaseKeys.map((k)=>parseInt(k));
223
- }
224
- if (!bestDiscriminant || score > bestDiscriminant.score) {
225
- const [path, kind] = parseQualifiedDisjoint(qualifiedDisjoint);
226
- bestDiscriminant = {
227
- path,
228
- kind,
229
- indexCases: filteredCases,
230
- score
231
- };
232
- if (score === remainingIndices.length) {
233
- // if we find a candidate that discriminates all branches, return early
234
- return bestDiscriminant;
235
- }
236
- }
237
- }
238
- }
239
- }
240
- return bestDiscriminant;
241
- };
242
- const serializeDefinitionIfAllowed = (kind, definition)=>{
243
- switch(kind){
244
- case "value":
245
- return serializeIfPrimitive(definition);
246
- case "domain":
247
- return definition;
248
- case "class":
249
- return (0, _objectKindsJs.getExactConstructorObjectKind)(definition);
250
- default:
251
- return;
252
- }
253
- };
254
- const serializeIfPrimitive = (data)=>{
255
- const domain = (0, _domainsJs.domainOf)(data);
256
- return domain === "object" || domain === "symbol" ? undefined : (0, _serializeJs.serializePrimitive)(data);
257
- };
258
- const serializeData = {
259
- value: (data)=>serializeIfPrimitive(data) ?? "default",
260
- class: (data)=>(0, _objectKindsJs.objectKindOf)(data) ?? "default",
261
- domain: _domainsJs.domainOf
262
- };
263
- const serializeCase = (kind, data)=>serializeData[kind](data);
264
- const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph((0, _propsJs.propToNode)(prop), $)) : false;
265
- const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).includesMorph : Object.values($.resolveTypeNode(node)).some((predicate)=>predicate === true ? false : (0, _objectKindsJs.isArray)(predicate) ? predicate.some((branch)=>branchIncludesMorph(branch, $)) : branchIncludesMorph(predicate, $));
@@ -1,139 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: all[name]
9
- });
10
- }
11
- _export(exports, {
12
- isConfigNode: ()=>isConfigNode,
13
- nodeIntersection: ()=>nodeIntersection,
14
- rootIntersection: ()=>rootIntersection,
15
- rootUnion: ()=>rootUnion,
16
- flattenType: ()=>flattenType,
17
- flattenNode: ()=>flattenNode,
18
- flattenTypeNode: ()=>flattenTypeNode,
19
- isLiteralNode: ()=>isLiteralNode,
20
- resolutionExtendsDomain: ()=>resolutionExtendsDomain,
21
- toArrayNode: ()=>toArrayNode
22
- });
23
- const _intersectionJs = require("../parse/ast/intersection.js");
24
- const _errorsJs = require("../utils/errors.js");
25
- const _genericsJs = require("../utils/generics.js");
26
- const _pathsJs = require("../utils/paths.js");
27
- const _composeJs = require("./compose.js");
28
- const _predicateJs = require("./predicate.js");
29
- const _propsJs = require("./rules/props.js");
30
- const isConfigNode = (node)=>"config" in node;
31
- const nodeIntersection = (l, r, state)=>{
32
- state.domain = undefined;
33
- const lDomains = state.type.scope.resolveTypeNode(l);
34
- const rDomains = state.type.scope.resolveTypeNode(r);
35
- const result = typeNodeIntersection(lDomains, rDomains, state);
36
- if (typeof result === "object" && !(0, _genericsJs.hasKeys)(result)) {
37
- return (0, _genericsJs.hasKeys)(state.disjoints) ? (0, _composeJs.anonymousDisjoint)() : state.addDisjoint("domain", (0, _genericsJs.objectKeysOf)(lDomains), (0, _genericsJs.objectKeysOf)(rDomains));
38
- }
39
- return result === lDomains ? l : result === rDomains ? r : result;
40
- };
41
- const typeNodeIntersection = (0, _composeJs.composeKeyedIntersection)((domain, l, r, context)=>{
42
- if (l === undefined) {
43
- return r === undefined ? (0, _errorsJs.throwInternalError)(_composeJs.undefinedOperandsMessage) : undefined;
44
- }
45
- if (r === undefined) {
46
- return undefined;
47
- }
48
- return (0, _predicateJs.predicateIntersection)(domain, l, r, context);
49
- }, {
50
- onEmpty: "omit"
51
- });
52
- const rootIntersection = (l, r, type)=>{
53
- const state = new _composeJs.IntersectionState(type, "&");
54
- const result = nodeIntersection(l, r, state);
55
- return (0, _composeJs.isDisjoint)(result) ? (0, _errorsJs.throwParseError)((0, _intersectionJs.compileDisjointReasonsMessage)(state.disjoints)) : (0, _composeJs.isEquality)(result) ? l : result;
56
- };
57
- const rootUnion = (l, r, type)=>{
58
- const lDomains = type.scope.resolveTypeNode(l);
59
- const rDomains = type.scope.resolveTypeNode(r);
60
- const result = {};
61
- const domains = (0, _genericsJs.objectKeysOf)({
62
- ...lDomains,
63
- ...rDomains
64
- });
65
- for (const domain of domains){
66
- result[domain] = (0, _genericsJs.hasKey)(lDomains, domain) ? (0, _genericsJs.hasKey)(rDomains, domain) ? (0, _predicateJs.predicateUnion)(domain, lDomains[domain], rDomains[domain], type) : lDomains[domain] : (0, _genericsJs.hasKey)(rDomains, domain) ? rDomains[domain] : (0, _errorsJs.throwInternalError)(_composeJs.undefinedOperandsMessage);
67
- }
68
- return result;
69
- };
70
- const hasImpliedDomain = (flatPredicate)=>flatPredicate[0] && (flatPredicate[0][0] === "value" || flatPredicate[0][0] === "class");
71
- const flattenType = (type)=>{
72
- const ctx = {
73
- type,
74
- path: new _pathsJs.Path(),
75
- lastDomain: "undefined"
76
- };
77
- return flattenNode(type.node, ctx);
78
- };
79
- const flattenNode = (node, ctx)=>{
80
- if (typeof node === "string") {
81
- return ctx.type.scope.resolve(node).flat;
82
- }
83
- const hasConfig = isConfigNode(node);
84
- const flattenedTypeNode = flattenTypeNode(hasConfig ? node.node : node, ctx);
85
- return hasConfig ? [
86
- [
87
- "config",
88
- {
89
- config: (0, _genericsJs.entriesOf)(node.config),
90
- node: flattenedTypeNode
91
- }
92
- ]
93
- ] : flattenedTypeNode;
94
- };
95
- const flattenTypeNode = (node, ctx)=>{
96
- const domains = (0, _genericsJs.objectKeysOf)(node);
97
- if (domains.length === 1) {
98
- const domain = domains[0];
99
- const predicate = node[domain];
100
- if (predicate === true) {
101
- return domain;
102
- }
103
- ctx.lastDomain = domain;
104
- const flatPredicate = (0, _predicateJs.flattenPredicate)(predicate, ctx);
105
- return hasImpliedDomain(flatPredicate) ? flatPredicate : [
106
- [
107
- "domain",
108
- domain
109
- ],
110
- ...flatPredicate
111
- ];
112
- }
113
- const result = {};
114
- for (const domain of domains){
115
- ctx.lastDomain = domain;
116
- result[domain] = (0, _predicateJs.flattenPredicate)(node[domain], ctx);
117
- }
118
- return [
119
- [
120
- "domains",
121
- result
122
- ]
123
- ];
124
- };
125
- const isLiteralNode = (node, domain)=>{
126
- return resolutionExtendsDomain(node, domain) && (0, _predicateJs.isLiteralCondition)(node[domain]);
127
- };
128
- const resolutionExtendsDomain = (resolution, domain)=>{
129
- const domains = (0, _genericsJs.objectKeysOf)(resolution);
130
- return domains.length === 1 && domains[0] === domain;
131
- };
132
- const toArrayNode = (node)=>({
133
- object: {
134
- class: Array,
135
- props: {
136
- [_propsJs.mappedKeys.index]: node
137
- }
138
- }
139
- });
@@ -1,86 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: all[name]
9
- });
10
- }
11
- _export(exports, {
12
- comparePredicates: ()=>comparePredicates,
13
- predicateIntersection: ()=>predicateIntersection,
14
- predicateUnion: ()=>predicateUnion,
15
- flattenPredicate: ()=>flattenPredicate,
16
- isLiteralCondition: ()=>isLiteralCondition
17
- });
18
- const _genericsJs = require("../utils/generics.js");
19
- const _objectKindsJs = require("../utils/objectKinds.js");
20
- const _branchJs = require("./branch.js");
21
- const _composeJs = require("./compose.js");
22
- const _discriminateJs = require("./discriminate.js");
23
- const emptyRulesIfTrue = (predicate)=>predicate === true ? {} : predicate;
24
- const comparePredicates = (l, r, context)=>{
25
- if (l === true && r === true) {
26
- return (0, _composeJs.equality)();
27
- }
28
- if (!(0, _objectKindsJs.isArray)(l) && !(0, _objectKindsJs.isArray)(r)) {
29
- const result = (0, _branchJs.branchIntersection)(emptyRulesIfTrue(l), emptyRulesIfTrue(r), context);
30
- return result === l ? l : result === r ? r : result;
31
- }
32
- const lBranches = (0, _genericsJs.listFrom)(emptyRulesIfTrue(l));
33
- const rBranches = (0, _genericsJs.listFrom)(emptyRulesIfTrue(r));
34
- const comparison = (0, _branchJs.compareBranches)(lBranches, rBranches, context);
35
- if (comparison.equalities.length === lBranches.length && comparison.equalities.length === rBranches.length) {
36
- return (0, _composeJs.equality)();
37
- }
38
- if (comparison.lExtendsR.length + comparison.equalities.length === lBranches.length) {
39
- return l;
40
- }
41
- if (comparison.rExtendsL.length + comparison.equalities.length === rBranches.length) {
42
- return r;
43
- }
44
- return comparison;
45
- };
46
- const predicateIntersection = (domain, l, r, state)=>{
47
- state.domain = domain;
48
- const comparison = comparePredicates(l, r, state);
49
- if (!(0, _branchJs.isBranchComparison)(comparison)) {
50
- return comparison;
51
- }
52
- const resultBranches = [
53
- ...comparison.distinctIntersections,
54
- ...comparison.equalities.map((indices)=>comparison.lBranches[indices[0]]),
55
- ...comparison.lExtendsR.map((lIndex)=>comparison.lBranches[lIndex]),
56
- ...comparison.rExtendsL.map((rIndex)=>comparison.rBranches[rIndex])
57
- ];
58
- if (resultBranches.length === 0) {
59
- state.addDisjoint("union", comparison.lBranches, comparison.rBranches);
60
- }
61
- return resultBranches.length === 1 ? resultBranches[0] : resultBranches;
62
- };
63
- const predicateUnion = (domain, l, r, type)=>{
64
- const state = new _composeJs.IntersectionState(type, "|");
65
- const comparison = comparePredicates(l, r, state);
66
- if (!(0, _branchJs.isBranchComparison)(comparison)) {
67
- return (0, _composeJs.isEquality)(comparison) || comparison === l ? r : comparison === r ? l : // subtype of the other, it consists of two opposite literals
68
- // and can be simplified to a non-literal boolean.
69
- domain === "boolean" ? true : [
70
- emptyRulesIfTrue(l),
71
- emptyRulesIfTrue(r)
72
- ];
73
- }
74
- const resultBranches = [
75
- ...comparison.lBranches.filter((_, lIndex)=>!comparison.lExtendsR.includes(lIndex) && !comparison.equalities.some((indexPair)=>indexPair[0] === lIndex)),
76
- ...comparison.rBranches.filter((_, rIndex)=>!comparison.rExtendsL.includes(rIndex) && !comparison.equalities.some((indexPair)=>indexPair[1] === rIndex))
77
- ];
78
- return resultBranches.length === 1 ? resultBranches[0] : resultBranches;
79
- };
80
- const flattenPredicate = (predicate, context)=>{
81
- if (predicate === true) {
82
- return [];
83
- }
84
- return (0, _objectKindsJs.isArray)(predicate) ? (0, _discriminateJs.flattenBranches)(predicate, context) : (0, _branchJs.flattenBranch)(predicate, context);
85
- };
86
- const isLiteralCondition = (predicate)=>typeof predicate === "object" && "value" in predicate;
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: all[name]
9
- });
10
- }
11
- _export(exports, {
12
- classIntersection: ()=>classIntersection,
13
- checkClass: ()=>checkClass
14
- });
15
- const _objectKindsJs = require("../../utils/objectKinds.js");
16
- const _composeJs = require("../compose.js");
17
- const classIntersection = (0, _composeJs.composeIntersection)((l, r, state)=>{
18
- return l === r ? (0, _composeJs.equality)() : l instanceof r ? l : r instanceof l ? r : state.addDisjoint("class", l, r);
19
- });
20
- const checkClass = (expectedClass, state)=>{
21
- if (typeof expectedClass === "string") {
22
- return (0, _objectKindsJs.objectKindOf)(state.data) === expectedClass || !state.problems.add("class", expectedClass);
23
- }
24
- return state.data instanceof expectedClass || !state.problems.add("class", expectedClass);
25
- };