arktype 1.0.16-alpha → 1.0.17-alpha

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 (247) hide show
  1. package/README.md +3 -3
  2. package/dist/cjs/src/main.js +38 -0
  3. package/dist/cjs/src/nodes/branch.js +140 -0
  4. package/dist/cjs/src/nodes/compose.js +157 -0
  5. package/dist/{nodes → cjs/src/nodes}/discriminate.js +92 -100
  6. package/dist/cjs/src/nodes/node.js +139 -0
  7. package/dist/cjs/src/nodes/predicate.js +86 -0
  8. package/dist/cjs/src/nodes/rules/class.js +25 -0
  9. package/dist/cjs/src/nodes/rules/collapsibleSet.js +48 -0
  10. package/dist/cjs/src/nodes/rules/divisor.js +29 -0
  11. package/dist/cjs/src/nodes/rules/props.js +160 -0
  12. package/dist/cjs/src/nodes/rules/range.js +105 -0
  13. package/dist/cjs/src/nodes/rules/regex.js +26 -0
  14. package/dist/cjs/src/nodes/rules/rules.js +123 -0
  15. package/dist/cjs/src/parse/ast/ast.js +4 -0
  16. package/dist/cjs/src/parse/ast/bound.js +16 -0
  17. package/dist/cjs/src/parse/ast/config.js +13 -0
  18. package/dist/cjs/src/parse/ast/distributableFunction.js +48 -0
  19. package/dist/cjs/src/parse/ast/divisor.js +9 -0
  20. package/dist/cjs/src/parse/ast/intersection.js +30 -0
  21. package/dist/cjs/src/parse/ast/keyof.js +111 -0
  22. package/dist/cjs/src/parse/ast/morph.js +58 -0
  23. package/dist/cjs/src/parse/ast/narrow.js +22 -0
  24. package/dist/cjs/src/parse/ast/node.js +4 -0
  25. package/dist/cjs/src/parse/ast/tuple.js +94 -0
  26. package/dist/cjs/src/parse/ast/union.js +9 -0
  27. package/dist/cjs/src/parse/ast/utils.js +4 -0
  28. package/dist/cjs/src/parse/definition.js +61 -0
  29. package/dist/cjs/src/parse/record.js +37 -0
  30. package/dist/cjs/src/parse/string/reduce/dynamic.js +167 -0
  31. package/dist/cjs/src/parse/string/reduce/shared.js +23 -0
  32. package/dist/cjs/src/parse/string/reduce/static.js +4 -0
  33. package/dist/cjs/src/parse/string/shift/operand/enclosed.js +54 -0
  34. package/dist/cjs/src/parse/string/shift/operand/operand.js +11 -0
  35. package/dist/cjs/src/parse/string/shift/operand/unenclosed.js +53 -0
  36. package/dist/cjs/src/parse/string/shift/operator/bounds.js +87 -0
  37. package/dist/cjs/src/parse/string/shift/operator/divisor.js +35 -0
  38. package/dist/cjs/src/parse/string/shift/operator/operator.js +26 -0
  39. package/dist/cjs/src/parse/string/shift/scanner.js +114 -0
  40. package/dist/cjs/src/parse/string/string.js +43 -0
  41. package/dist/cjs/src/scopes/ark.js +45 -0
  42. package/dist/cjs/src/scopes/cache.js +52 -0
  43. package/dist/cjs/src/scopes/expressions.js +29 -0
  44. package/dist/cjs/src/scopes/jsObjects.js +93 -0
  45. package/dist/cjs/src/scopes/scope.js +323 -0
  46. package/dist/cjs/src/scopes/tsKeywords.js +113 -0
  47. package/dist/cjs/src/scopes/type.js +50 -0
  48. package/dist/cjs/src/scopes/validation/creditCard.js +47 -0
  49. package/dist/cjs/src/scopes/validation/date.js +62 -0
  50. package/dist/cjs/src/scopes/validation/validation.js +108 -0
  51. package/dist/cjs/src/traverse/problems.js +281 -0
  52. package/dist/cjs/src/traverse/traverse.js +390 -0
  53. package/dist/cjs/src/utils/chainableNoOpProxy.js +23 -0
  54. package/dist/cjs/src/utils/data.js +53 -0
  55. package/dist/cjs/src/utils/domains.js +30 -0
  56. package/dist/cjs/src/utils/errors.js +26 -0
  57. package/dist/cjs/src/utils/freeze.js +15 -0
  58. package/dist/cjs/src/utils/generics.js +63 -0
  59. package/dist/cjs/src/utils/numericLiterals.js +75 -0
  60. package/dist/cjs/src/utils/objectKinds.js +69 -0
  61. package/dist/cjs/src/utils/paths.js +32 -0
  62. package/dist/cjs/src/utils/serialize.js +74 -0
  63. package/dist/cjs/src/utils/unionToTuple.js +4 -0
  64. package/dist/mjs/package.json +3 -0
  65. package/dist/mjs/src/main.js +8 -0
  66. package/dist/mjs/src/nodes/branch.js +123 -0
  67. package/dist/mjs/src/nodes/compose.js +135 -0
  68. package/dist/mjs/src/nodes/discriminate.js +247 -0
  69. package/dist/mjs/src/nodes/node.js +117 -0
  70. package/dist/mjs/src/nodes/predicate.js +69 -0
  71. package/dist/mjs/src/nodes/rules/class.js +11 -0
  72. package/dist/mjs/src/nodes/rules/collapsibleSet.js +34 -0
  73. package/dist/mjs/src/nodes/rules/divisor.js +15 -0
  74. package/dist/mjs/src/nodes/rules/props.js +142 -0
  75. package/dist/mjs/src/nodes/rules/range.js +86 -0
  76. package/dist/mjs/src/nodes/rules/regex.js +11 -0
  77. package/dist/mjs/src/nodes/rules/rules.js +106 -0
  78. package/dist/mjs/src/parse/ast/ast.js +1 -0
  79. package/dist/mjs/src/parse/ast/bound.js +2 -0
  80. package/dist/mjs/src/parse/ast/config.js +9 -0
  81. package/dist/mjs/src/parse/ast/distributableFunction.js +34 -0
  82. package/dist/mjs/src/parse/ast/divisor.js +1 -0
  83. package/dist/mjs/src/parse/ast/intersection.js +16 -0
  84. package/dist/mjs/src/parse/ast/keyof.js +103 -0
  85. package/dist/mjs/src/parse/ast/morph.js +44 -0
  86. package/dist/mjs/src/parse/ast/narrow.js +14 -0
  87. package/dist/mjs/src/parse/ast/node.js +1 -0
  88. package/dist/mjs/src/parse/ast/tuple.js +80 -0
  89. package/dist/mjs/src/parse/ast/union.js +1 -0
  90. package/dist/mjs/src/parse/ast/utils.js +1 -0
  91. package/dist/mjs/src/parse/definition.js +46 -0
  92. package/dist/mjs/src/parse/record.js +29 -0
  93. package/dist/mjs/src/parse/string/reduce/dynamic.js +159 -0
  94. package/dist/mjs/src/parse/string/reduce/shared.js +6 -0
  95. package/dist/mjs/src/parse/string/reduce/static.js +1 -0
  96. package/dist/mjs/src/parse/string/shift/operand/enclosed.js +39 -0
  97. package/dist/mjs/src/parse/string/shift/operand/operand.js +3 -0
  98. package/dist/mjs/src/parse/string/shift/operand/unenclosed.js +36 -0
  99. package/dist/mjs/src/parse/string/shift/operator/bounds.js +70 -0
  100. package/dist/mjs/src/parse/string/shift/operator/divisor.js +21 -0
  101. package/dist/mjs/src/parse/string/shift/operator/operator.js +13 -0
  102. package/dist/mjs/src/parse/string/shift/scanner.js +106 -0
  103. package/dist/mjs/src/parse/string/string.js +28 -0
  104. package/dist/mjs/src/scopes/ark.js +28 -0
  105. package/dist/mjs/src/scopes/cache.js +38 -0
  106. package/dist/{scopes → mjs/src/scopes}/expressions.js +9 -20
  107. package/dist/mjs/src/scopes/jsObjects.js +82 -0
  108. package/dist/mjs/src/scopes/scope.js +303 -0
  109. package/dist/mjs/src/scopes/tsKeywords.js +116 -0
  110. package/dist/mjs/src/scopes/type.js +35 -0
  111. package/dist/{scopes → mjs/src/scopes}/validation/creditCard.js +11 -15
  112. package/dist/{scopes → mjs/src/scopes}/validation/date.js +16 -21
  113. package/dist/mjs/src/scopes/validation/validation.js +112 -0
  114. package/dist/mjs/src/traverse/problems.js +260 -0
  115. package/dist/mjs/src/traverse/traverse.js +373 -0
  116. package/dist/{utils → mjs/src/utils}/chainableNoOpProxy.js +2 -6
  117. package/dist/mjs/src/utils/data.js +38 -0
  118. package/dist/mjs/src/utils/domains.js +15 -0
  119. package/dist/mjs/src/utils/errors.js +10 -0
  120. package/dist/mjs/src/utils/freeze.js +7 -0
  121. package/dist/mjs/src/utils/generics.js +38 -0
  122. package/dist/mjs/src/utils/numericLiterals.js +72 -0
  123. package/dist/mjs/src/utils/objectKinds.js +55 -0
  124. package/dist/{utils → mjs/src/utils}/paths.js +3 -8
  125. package/dist/mjs/src/utils/serialize.js +58 -0
  126. package/dist/mjs/src/utils/unionToTuple.js +1 -0
  127. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  128. package/package.json +13 -5
  129. package/dist/main.js +0 -29
  130. package/dist/nodes/branch.js +0 -135
  131. package/dist/nodes/compose.js +0 -99
  132. package/dist/nodes/node.js +0 -134
  133. package/dist/nodes/predicate.js +0 -88
  134. package/dist/nodes/rules/class.js +0 -23
  135. package/dist/nodes/rules/collapsibleSet.js +0 -34
  136. package/dist/nodes/rules/divisor.js +0 -19
  137. package/dist/nodes/rules/props.js +0 -130
  138. package/dist/nodes/rules/range.js +0 -108
  139. package/dist/nodes/rules/regex.js +0 -17
  140. package/dist/nodes/rules/rules.js +0 -102
  141. package/dist/parse/ast/ast.js +0 -2
  142. package/dist/parse/ast/bound.js +0 -7
  143. package/dist/parse/ast/config.js +0 -14
  144. package/dist/parse/ast/distributableFunction.js +0 -40
  145. package/dist/parse/ast/divisor.js +0 -5
  146. package/dist/parse/ast/intersection.js +0 -21
  147. package/dist/parse/ast/keyof.js +0 -107
  148. package/dist/parse/ast/morph.js +0 -50
  149. package/dist/parse/ast/narrow.js +0 -17
  150. package/dist/parse/ast/node.js +0 -2
  151. package/dist/parse/ast/tuple.js +0 -72
  152. package/dist/parse/ast/union.js +0 -5
  153. package/dist/parse/ast/utils.js +0 -2
  154. package/dist/parse/definition.js +0 -47
  155. package/dist/parse/record.js +0 -31
  156. package/dist/parse/string/reduce/dynamic.js +0 -159
  157. package/dist/parse/string/reduce/shared.js +0 -13
  158. package/dist/parse/string/reduce/static.js +0 -2
  159. package/dist/parse/string/shift/operand/enclosed.js +0 -37
  160. package/dist/parse/string/shift/operand/operand.js +0 -15
  161. package/dist/parse/string/shift/operand/unenclosed.js +0 -45
  162. package/dist/parse/string/shift/operator/bounds.js +0 -83
  163. package/dist/parse/string/shift/operator/divisor.js +0 -23
  164. package/dist/parse/string/shift/operator/operator.js +0 -34
  165. package/dist/parse/string/shift/scanner.js +0 -97
  166. package/dist/parse/string/string.js +0 -35
  167. package/dist/scopes/ark.js +0 -27
  168. package/dist/scopes/cache.js +0 -28
  169. package/dist/scopes/jsObjects.js +0 -20
  170. package/dist/scopes/scope.js +0 -184
  171. package/dist/scopes/tsKeywords.js +0 -49
  172. package/dist/scopes/type.js +0 -40
  173. package/dist/scopes/validation/validation.js +0 -74
  174. package/dist/traverse/problems.js +0 -234
  175. package/dist/traverse/traverse.js +0 -330
  176. package/dist/utils/data.js +0 -39
  177. package/dist/utils/domains.js +0 -27
  178. package/dist/utils/errors.js +0 -17
  179. package/dist/utils/freeze.js +0 -16
  180. package/dist/utils/generics.js +0 -53
  181. package/dist/utils/numericLiterals.js +0 -88
  182. package/dist/utils/objectKinds.js +0 -69
  183. package/dist/utils/serialize.js +0 -71
  184. package/dist/utils/unionToTuple.js +0 -2
  185. /package/dist/{package.json → cjs/package.json} +0 -0
  186. /package/dist/{main.d.ts → types/main.d.ts} +0 -0
  187. /package/dist/{nodes → types/nodes}/branch.d.ts +0 -0
  188. /package/dist/{nodes → types/nodes}/compose.d.ts +0 -0
  189. /package/dist/{nodes → types/nodes}/discriminate.d.ts +0 -0
  190. /package/dist/{nodes → types/nodes}/node.d.ts +0 -0
  191. /package/dist/{nodes → types/nodes}/predicate.d.ts +0 -0
  192. /package/dist/{nodes → types/nodes}/rules/class.d.ts +0 -0
  193. /package/dist/{nodes → types/nodes}/rules/collapsibleSet.d.ts +0 -0
  194. /package/dist/{nodes → types/nodes}/rules/divisor.d.ts +0 -0
  195. /package/dist/{nodes → types/nodes}/rules/props.d.ts +0 -0
  196. /package/dist/{nodes → types/nodes}/rules/range.d.ts +0 -0
  197. /package/dist/{nodes → types/nodes}/rules/regex.d.ts +0 -0
  198. /package/dist/{nodes → types/nodes}/rules/rules.d.ts +0 -0
  199. /package/dist/{parse → types/parse}/ast/ast.d.ts +0 -0
  200. /package/dist/{parse → types/parse}/ast/bound.d.ts +0 -0
  201. /package/dist/{parse → types/parse}/ast/config.d.ts +0 -0
  202. /package/dist/{parse → types/parse}/ast/distributableFunction.d.ts +0 -0
  203. /package/dist/{parse → types/parse}/ast/divisor.d.ts +0 -0
  204. /package/dist/{parse → types/parse}/ast/intersection.d.ts +0 -0
  205. /package/dist/{parse → types/parse}/ast/keyof.d.ts +0 -0
  206. /package/dist/{parse → types/parse}/ast/morph.d.ts +0 -0
  207. /package/dist/{parse → types/parse}/ast/narrow.d.ts +0 -0
  208. /package/dist/{parse → types/parse}/ast/node.d.ts +0 -0
  209. /package/dist/{parse → types/parse}/ast/tuple.d.ts +0 -0
  210. /package/dist/{parse → types/parse}/ast/union.d.ts +0 -0
  211. /package/dist/{parse → types/parse}/ast/utils.d.ts +0 -0
  212. /package/dist/{parse → types/parse}/definition.d.ts +0 -0
  213. /package/dist/{parse → types/parse}/record.d.ts +0 -0
  214. /package/dist/{parse → types/parse}/string/reduce/dynamic.d.ts +0 -0
  215. /package/dist/{parse → types/parse}/string/reduce/shared.d.ts +0 -0
  216. /package/dist/{parse → types/parse}/string/reduce/static.d.ts +0 -0
  217. /package/dist/{parse → types/parse}/string/shift/operand/enclosed.d.ts +0 -0
  218. /package/dist/{parse → types/parse}/string/shift/operand/operand.d.ts +0 -0
  219. /package/dist/{parse → types/parse}/string/shift/operand/unenclosed.d.ts +0 -0
  220. /package/dist/{parse → types/parse}/string/shift/operator/bounds.d.ts +0 -0
  221. /package/dist/{parse → types/parse}/string/shift/operator/divisor.d.ts +0 -0
  222. /package/dist/{parse → types/parse}/string/shift/operator/operator.d.ts +0 -0
  223. /package/dist/{parse → types/parse}/string/shift/scanner.d.ts +0 -0
  224. /package/dist/{parse → types/parse}/string/string.d.ts +0 -0
  225. /package/dist/{scopes → types/scopes}/ark.d.ts +0 -0
  226. /package/dist/{scopes → types/scopes}/cache.d.ts +0 -0
  227. /package/dist/{scopes → types/scopes}/expressions.d.ts +0 -0
  228. /package/dist/{scopes → types/scopes}/jsObjects.d.ts +0 -0
  229. /package/dist/{scopes → types/scopes}/scope.d.ts +0 -0
  230. /package/dist/{scopes → types/scopes}/tsKeywords.d.ts +0 -0
  231. /package/dist/{scopes → types/scopes}/type.d.ts +0 -0
  232. /package/dist/{scopes → types/scopes}/validation/creditCard.d.ts +0 -0
  233. /package/dist/{scopes → types/scopes}/validation/date.d.ts +0 -0
  234. /package/dist/{scopes → types/scopes}/validation/validation.d.ts +0 -0
  235. /package/dist/{traverse → types/traverse}/problems.d.ts +0 -0
  236. /package/dist/{traverse → types/traverse}/traverse.d.ts +0 -0
  237. /package/dist/{utils → types/utils}/chainableNoOpProxy.d.ts +0 -0
  238. /package/dist/{utils → types/utils}/data.d.ts +0 -0
  239. /package/dist/{utils → types/utils}/domains.d.ts +0 -0
  240. /package/dist/{utils → types/utils}/errors.d.ts +0 -0
  241. /package/dist/{utils → types/utils}/freeze.d.ts +0 -0
  242. /package/dist/{utils → types/utils}/generics.d.ts +0 -0
  243. /package/dist/{utils → types/utils}/numericLiterals.d.ts +0 -0
  244. /package/dist/{utils → types/utils}/objectKinds.d.ts +0 -0
  245. /package/dist/{utils → types/utils}/paths.d.ts +0 -0
  246. /package/dist/{utils → types/utils}/serialize.d.ts +0 -0
  247. /package/dist/{utils → types/utils}/unionToTuple.d.ts +0 -0
@@ -0,0 +1,160 @@
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
+ isOptional: ()=>isOptional,
13
+ isPrerequisite: ()=>isPrerequisite,
14
+ mappedKeys: ()=>mappedKeys,
15
+ propToNode: ()=>propToNode,
16
+ propsIntersection: ()=>propsIntersection,
17
+ flattenProps: ()=>flattenProps
18
+ });
19
+ const _composeJs = require("../compose.js");
20
+ const _nodeJs = require("../node.js");
21
+ const isOptional = (prop)=>prop[0] === "?";
22
+ const isPrerequisite = (prop)=>prop[0] === "!";
23
+ const mappedKeys = {
24
+ index: "[index]"
25
+ };
26
+ const propToNode = (prop)=>isOptional(prop) || isPrerequisite(prop) ? prop[1] : prop;
27
+ const getTupleLengthIfPresent = (result)=>{
28
+ if (typeof result.length === "object" && isPrerequisite(result.length) && typeof result.length[1] !== "string" && (0, _nodeJs.isLiteralNode)(result.length[1], "number")) {
29
+ return result.length[1].number.value;
30
+ }
31
+ };
32
+ const propsIntersection = (0, _composeJs.composeIntersection)((l, r, state)=>{
33
+ const result = propKeysIntersection(l, r, state);
34
+ if (typeof result === "symbol") {
35
+ return result;
36
+ }
37
+ const lengthValue = getTupleLengthIfPresent(result);
38
+ if (lengthValue === undefined || !(mappedKeys.index in result)) {
39
+ return result;
40
+ }
41
+ // if we are at this point, we have an array with an exact length (i.e.
42
+ // a tuple) and an index signature. Intersection each tuple item with
43
+ // the index signature node and remove the index signature via a new
44
+ // updated result, copied from result to avoid mutating existing references.
45
+ const { [mappedKeys.index]: indexProp , ...updatedResult } = result;
46
+ const indexNode = propToNode(indexProp);
47
+ for(let i = 0; i < lengthValue; i++){
48
+ if (!updatedResult[i]) {
49
+ updatedResult[i] = indexNode;
50
+ continue;
51
+ }
52
+ const existingNodeAtIndex = propToNode(updatedResult[i]);
53
+ state.path.push(`${i}`);
54
+ const updatedResultAtIndex = (0, _nodeJs.nodeIntersection)(existingNodeAtIndex, indexNode, state);
55
+ state.path.pop();
56
+ if ((0, _composeJs.isDisjoint)(updatedResultAtIndex)) {
57
+ return updatedResultAtIndex;
58
+ } else if (!(0, _composeJs.isEquality)(updatedResultAtIndex) && updatedResultAtIndex !== existingNodeAtIndex) {
59
+ updatedResult[i] = updatedResultAtIndex;
60
+ }
61
+ }
62
+ return updatedResult;
63
+ });
64
+ const propKeysIntersection = (0, _composeJs.composeKeyedIntersection)((propKey, l, r, context)=>{
65
+ if (l === undefined) {
66
+ return r === undefined ? (0, _composeJs.equality)() : r;
67
+ }
68
+ if (r === undefined) {
69
+ return l;
70
+ }
71
+ context.path.push(propKey);
72
+ const result = (0, _nodeJs.nodeIntersection)(propToNode(l), propToNode(r), context);
73
+ context.path.pop();
74
+ const resultIsOptional = isOptional(l) && isOptional(r);
75
+ if ((0, _composeJs.isDisjoint)(result) && resultIsOptional) {
76
+ // If an optional key has an empty intersection, the type can
77
+ // still be satisfied as long as the key is not included. Set
78
+ // the node to never rather than invalidating the type.
79
+ return {};
80
+ }
81
+ return result;
82
+ }, {
83
+ onEmpty: "bubble"
84
+ });
85
+ const flattenProps = (entries, props, ctx)=>{
86
+ const keyConfig = ctx.type.config?.keys ?? ctx.type.scope.config.keys;
87
+ return keyConfig === "loose" ? flattenLooseProps(entries, props, ctx) : flattenPropsRecord(keyConfig, entries, props, ctx);
88
+ };
89
+ const flattenLooseProps = (entries, props, ctx)=>{
90
+ // if we don't care about extraneous keys, flatten props so we can iterate over the definitions directly
91
+ for(const k in props){
92
+ const prop = props[k];
93
+ ctx.path.push(k);
94
+ if (k === mappedKeys.index) {
95
+ entries.push([
96
+ "indexProp",
97
+ (0, _nodeJs.flattenNode)(propToNode(prop), ctx)
98
+ ]);
99
+ } else if (isOptional(prop)) {
100
+ entries.push([
101
+ "optionalProp",
102
+ [
103
+ k,
104
+ (0, _nodeJs.flattenNode)(prop[1], ctx)
105
+ ]
106
+ ]);
107
+ } else if (isPrerequisite(prop)) {
108
+ entries.push([
109
+ "prerequisiteProp",
110
+ [
111
+ k,
112
+ (0, _nodeJs.flattenNode)(prop[1], ctx)
113
+ ]
114
+ ]);
115
+ } else {
116
+ entries.push([
117
+ "requiredProp",
118
+ [
119
+ k,
120
+ (0, _nodeJs.flattenNode)(prop, ctx)
121
+ ]
122
+ ]);
123
+ }
124
+ ctx.path.pop();
125
+ }
126
+ };
127
+ const flattenPropsRecord = (kind, entries, props, ctx)=>{
128
+ const result = {
129
+ required: {},
130
+ optional: {}
131
+ };
132
+ // if we need to keep track of extraneous keys, either to add problems or
133
+ // remove them, store the props as a Record to optimize for presence
134
+ // checking as we iterate over the data
135
+ for(const k in props){
136
+ const prop = props[k];
137
+ ctx.path.push(k);
138
+ if (k === mappedKeys.index) {
139
+ result.index = (0, _nodeJs.flattenNode)(propToNode(prop), ctx);
140
+ } else if (isOptional(prop)) {
141
+ result.optional[k] = (0, _nodeJs.flattenNode)(prop[1], ctx);
142
+ } else if (isPrerequisite(prop)) {
143
+ // we still have to push prerequisite props as normal entries so they can be checked first
144
+ entries.push([
145
+ "prerequisiteProp",
146
+ [
147
+ k,
148
+ (0, _nodeJs.flattenNode)(prop[1], ctx)
149
+ ]
150
+ ]);
151
+ } else {
152
+ result.required[k] = (0, _nodeJs.flattenNode)(prop, ctx);
153
+ }
154
+ ctx.path.pop();
155
+ }
156
+ entries.push([
157
+ `${kind}Props`,
158
+ result
159
+ ]);
160
+ };
@@ -0,0 +1,105 @@
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
+ minComparators: ()=>minComparators,
13
+ maxComparators: ()=>maxComparators,
14
+ isEqualityRange: ()=>isEqualityRange,
15
+ rangeIntersection: ()=>rangeIntersection,
16
+ flattenRange: ()=>flattenRange,
17
+ checkBound: ()=>checkBound,
18
+ compareStrictness: ()=>compareStrictness
19
+ });
20
+ const _dataJs = require("../../utils/data.js");
21
+ const _composeJs = require("../compose.js");
22
+ const minComparators = {
23
+ ">": true,
24
+ ">=": true
25
+ };
26
+ const maxComparators = {
27
+ "<": true,
28
+ "<=": true
29
+ };
30
+ const isEqualityRange = (range)=>"comparator" in range;
31
+ const rangeIntersection = (0, _composeJs.composeIntersection)((l, r, state)=>{
32
+ if (isEqualityRange(l)) {
33
+ if (isEqualityRange(r)) {
34
+ return l.limit === r.limit ? (0, _composeJs.equality)() : state.addDisjoint("range", l, r);
35
+ }
36
+ return rangeAllows(r, l.limit) ? l : state.addDisjoint("range", l, r);
37
+ }
38
+ if (isEqualityRange(r)) {
39
+ return rangeAllows(l, r.limit) ? r : state.addDisjoint("range", l, r);
40
+ }
41
+ const stricterMin = compareStrictness("min", l.min, r.min);
42
+ const stricterMax = compareStrictness("max", l.max, r.max);
43
+ if (stricterMin === "l") {
44
+ if (stricterMax === "r") {
45
+ return compareStrictness("min", l.min, r.max) === "l" ? state.addDisjoint("range", l, r) : {
46
+ min: l.min,
47
+ max: r.max
48
+ };
49
+ }
50
+ return l;
51
+ }
52
+ if (stricterMin === "r") {
53
+ if (stricterMax === "l") {
54
+ return compareStrictness("max", l.max, r.min) === "l" ? state.addDisjoint("range", l, r) : {
55
+ min: r.min,
56
+ max: l.max
57
+ };
58
+ }
59
+ return r;
60
+ }
61
+ return stricterMax === "l" ? l : stricterMax === "r" ? r : (0, _composeJs.equality)();
62
+ });
63
+ const rangeAllows = (range, n)=>isEqualityRange(range) ? n === range.limit : minAllows(range.min, n) && maxAllows(range.max, n);
64
+ const minAllows = (min, n)=>!min || n > min.limit || n === min.limit && !isExclusive(min.comparator);
65
+ const maxAllows = (max, n)=>!max || n < max.limit || n === max.limit && !isExclusive(max.comparator);
66
+ const flattenRange = (entries, range, ctx)=>{
67
+ const units = ctx.lastDomain === "string" ? "characters" : ctx.lastDomain === "object" ? "items long" : undefined;
68
+ if (isEqualityRange(range)) {
69
+ return entries.push([
70
+ "bound",
71
+ units ? {
72
+ ...range,
73
+ units
74
+ } : range
75
+ ]);
76
+ }
77
+ if (range.min) {
78
+ entries.push([
79
+ "bound",
80
+ units ? {
81
+ ...range.min,
82
+ units
83
+ } : range.min
84
+ ]);
85
+ }
86
+ if (range.max) {
87
+ entries.push([
88
+ "bound",
89
+ units ? {
90
+ ...range.max,
91
+ units
92
+ } : range.max
93
+ ]);
94
+ }
95
+ };
96
+ const checkBound = (bound, state)=>comparatorCheckers[bound.comparator]((0, _dataJs.sizeOf)(state.data), bound.limit) || !state.problems.add("bound", bound);
97
+ const comparatorCheckers = {
98
+ "<": (size, limit)=>size < limit,
99
+ ">": (size, limit)=>size > limit,
100
+ "<=": (size, limit)=>size <= limit,
101
+ ">=": (size, limit)=>size >= limit,
102
+ "==": (size, limit)=>size === limit
103
+ };
104
+ const compareStrictness = (kind, l, r)=>!l ? !r ? "=" : "r" : !r ? "l" : l.limit === r.limit ? isExclusive(l.comparator) ? isExclusive(r.comparator) ? "=" : "l" : isExclusive(r.comparator) ? "r" : "=" : kind === "min" ? l.limit > r.limit ? "l" : "r" : l.limit < r.limit ? "l" : "r";
105
+ const isExclusive = (comparator)=>comparator.length === 1;
@@ -0,0 +1,26 @@
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
+ getRegex: ()=>getRegex,
13
+ checkRegex: ()=>checkRegex,
14
+ regexIntersection: ()=>regexIntersection
15
+ });
16
+ const _composeJs = require("../compose.js");
17
+ const _collapsibleSetJs = require("./collapsibleSet.js");
18
+ const regexCache = {};
19
+ const getRegex = (source)=>{
20
+ if (!regexCache[source]) {
21
+ regexCache[source] = new RegExp(source);
22
+ }
23
+ return regexCache[source];
24
+ };
25
+ const checkRegex = (source, state)=>getRegex(source).test(state.data) || !state.problems.add("regex", `/${source}/`);
26
+ const regexIntersection = (0, _composeJs.composeIntersection)(_collapsibleSetJs.collapsibleListUnion);
@@ -0,0 +1,123 @@
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
+ rulesIntersection: ()=>rulesIntersection,
13
+ narrowableRulesIntersection: ()=>narrowableRulesIntersection,
14
+ flattenRules: ()=>flattenRules,
15
+ precedenceMap: ()=>precedenceMap,
16
+ literalSatisfiesRules: ()=>literalSatisfiesRules
17
+ });
18
+ const _genericsJs = require("../../utils/generics.js");
19
+ const _composeJs = require("../compose.js");
20
+ const _classJs = require("./class.js");
21
+ const _collapsibleSetJs = require("./collapsibleSet.js");
22
+ const _divisorJs = require("./divisor.js");
23
+ const _propsJs = require("./props.js");
24
+ const _rangeJs = require("./range.js");
25
+ const _regexJs = require("./regex.js");
26
+ const rulesIntersection = (l, r, state)=>"value" in l ? "value" in r ? l.value === r.value ? (0, _composeJs.equality)() : state.addDisjoint("value", l.value, r.value) : literalSatisfiesRules(l.value, r, state) ? l : state.addDisjoint("leftAssignability", l, r) : "value" in r ? literalSatisfiesRules(r.value, l, state) ? r : state.addDisjoint("rightAssignability", l, r) : narrowableRulesIntersection(l, r, state);
27
+ const narrowIntersection = (0, _composeJs.composeIntersection)(_collapsibleSetJs.collapsibleListUnion);
28
+ const narrowableRulesIntersection = (0, _composeJs.composeKeyedIntersection)({
29
+ divisor: _divisorJs.divisorIntersection,
30
+ regex: _regexJs.regexIntersection,
31
+ props: _propsJs.propsIntersection,
32
+ class: _classJs.classIntersection,
33
+ range: _rangeJs.rangeIntersection,
34
+ narrow: narrowIntersection
35
+ }, {
36
+ onEmpty: "bubble"
37
+ });
38
+ const flattenRules = (rules, ctx)=>{
39
+ const entries = [];
40
+ let k;
41
+ for(k in rules){
42
+ ruleFlatteners[k](entries, rules[k], ctx);
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
48
+ return entries.sort((l, r)=>precedenceMap[l[0]] - precedenceMap[r[0]]);
49
+ };
50
+ const ruleFlatteners = {
51
+ regex: (entries, rule)=>{
52
+ for (const source of (0, _genericsJs.listFrom)(rule)){
53
+ entries.push([
54
+ "regex",
55
+ source
56
+ ]);
57
+ }
58
+ },
59
+ divisor: (entries, rule)=>{
60
+ entries.push([
61
+ "divisor",
62
+ rule
63
+ ]);
64
+ },
65
+ range: _rangeJs.flattenRange,
66
+ class: (entries, rule)=>{
67
+ entries.push([
68
+ "class",
69
+ rule
70
+ ]);
71
+ },
72
+ props: _propsJs.flattenProps,
73
+ narrow: (entries, rule)=>{
74
+ for (const narrow of (0, _genericsJs.listFrom)(rule)){
75
+ entries.push([
76
+ "narrow",
77
+ narrow
78
+ ]);
79
+ }
80
+ },
81
+ value: (entries, rule)=>{
82
+ entries.push([
83
+ "value",
84
+ rule
85
+ ]);
86
+ }
87
+ };
88
+ const precedenceMap = {
89
+ // Config: Applies before any checks
90
+ config: -1,
91
+ // Critical: No other checks are performed if these fail
92
+ domain: 0,
93
+ value: 0,
94
+ domains: 0,
95
+ branches: 0,
96
+ switch: 0,
97
+ alias: 0,
98
+ class: 0,
99
+ // Shallow: All shallow checks will be performed even if one or more fail
100
+ regex: 1,
101
+ divisor: 1,
102
+ bound: 1,
103
+ // Prerequisite: These are deep checks with special priority, e.g. the
104
+ // length of a tuple, which causes other deep props not to be checked if it
105
+ // is invalid
106
+ prerequisiteProp: 2,
107
+ // Deep: Performed if all shallow checks pass, even if one or more deep checks fail
108
+ distilledProps: 3,
109
+ strictProps: 3,
110
+ requiredProp: 3,
111
+ optionalProp: 3,
112
+ indexProp: 3,
113
+ // Narrow: Only performed if all shallow and deep checks pass
114
+ narrow: 4,
115
+ // Morph: Only performed if all validation passes
116
+ morph: 5
117
+ };
118
+ const literalSatisfiesRules = (data, rules, state)=>!state.type.scope.type([
119
+ "node",
120
+ {
121
+ [state.domain]: rules
122
+ }
123
+ ])(data).problems;
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
@@ -0,0 +1,16 @@
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
+ writeDoubleRightBoundMessage: ()=>writeDoubleRightBoundMessage,
13
+ writeUnboundableMessage: ()=>writeUnboundableMessage
14
+ });
15
+ const writeDoubleRightBoundMessage = (root)=>`Expression ${root} must have at most one right bound`;
16
+ const writeUnboundableMessage = (root)=>`Bounded expression ${root} must be a number, string or array`;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "parseConfigTuple", {
6
+ enumerable: true,
7
+ get: ()=>parseConfigTuple
8
+ });
9
+ const _definitionJs = require("../definition.js");
10
+ const parseConfigTuple = (def, ctx)=>({
11
+ node: ctx.type.scope.resolveTypeNode((0, _definitionJs.parseDefinition)(def[0], ctx)),
12
+ config: def[2]
13
+ });
@@ -0,0 +1,48 @@
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
+ writeMalformedDistributableFunctionMessage: ()=>writeMalformedDistributableFunctionMessage,
13
+ distributeFunctionToNode: ()=>distributeFunctionToNode
14
+ });
15
+ const _domainsJs = require("../../utils/domains.js");
16
+ const _errorsJs = require("../../utils/errors.js");
17
+ const _genericsJs = require("../../utils/generics.js");
18
+ const _serializeJs = require("../../utils/serialize.js");
19
+ const writeMalformedDistributableFunctionMessage = (def)=>`Expected a Function or Record<Domain, Function> operand (${(0, _serializeJs.stringify)(def)} was invalid)`;
20
+ const distributeFunctionToNode = (distributableFunction, node, ctx, ruleKey)=>{
21
+ const domains = (0, _genericsJs.objectKeysOf)(node);
22
+ if (!(0, _domainsJs.hasDomain)(distributableFunction, "object")) {
23
+ return (0, _errorsJs.throwParseError)(writeMalformedDistributableFunctionMessage(distributableFunction));
24
+ }
25
+ const distributed = {};
26
+ if (typeof distributableFunction === "function") {
27
+ const domainFunction = {
28
+ [ruleKey]: distributableFunction
29
+ };
30
+ for (const domain of domains){
31
+ distributed[domain] = domainFunction;
32
+ }
33
+ } else {
34
+ for (const domain of domains){
35
+ if (distributableFunction[domain] === undefined) {
36
+ continue;
37
+ }
38
+ const functionInDomain = {
39
+ [ruleKey]: distributableFunction[domain]
40
+ };
41
+ if (typeof functionInDomain[ruleKey] !== "function") {
42
+ return (0, _errorsJs.throwParseError)(writeMalformedDistributableFunctionMessage(functionInDomain));
43
+ }
44
+ distributed[domain] = functionInDomain;
45
+ }
46
+ }
47
+ return distributed;
48
+ };
@@ -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`;
@@ -0,0 +1,30 @@
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
+ compileDisjointReasonsMessage: ()=>compileDisjointReasonsMessage,
13
+ writeImplicitNeverMessage: ()=>writeImplicitNeverMessage
14
+ });
15
+ const _composeJs = require("../../nodes/compose.js");
16
+ const _genericsJs = require("../../utils/generics.js");
17
+ const compileDisjointReasonsMessage = (disjoints)=>{
18
+ const paths = (0, _genericsJs.objectKeysOf)(disjoints);
19
+ if (paths.length === 1) {
20
+ const path = paths[0];
21
+ return `${path === "/" ? "" : `At ${path}: `}Intersection of ${_composeJs.disjointDescriptionWriters[disjoints[path].kind](disjoints[path])} results in an unsatisfiable type`;
22
+ }
23
+ let message = `
24
+ "Intersection results in unsatisfiable types at the following paths:\n`;
25
+ for(const path in disjoints){
26
+ message += ` ${path}: ${_composeJs.disjointDescriptionWriters[disjoints[path].kind](disjoints[path])}\n`;
27
+ }
28
+ return message;
29
+ };
30
+ const writeImplicitNeverMessage = (path, operator, description)=>`${path.length ? `At ${path}: ` : ""}${operator} ${description ? `${description} ` : ""}results in an unsatisfiable type`;
@@ -0,0 +1,111 @@
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 _propsJs = require("../../nodes/rules/props.js");
10
+ const _errorsJs = require("../../utils/errors.js");
11
+ const _freezeJs = require("../../utils/freeze.js");
12
+ const _genericsJs = require("../../utils/generics.js");
13
+ const _numericLiteralsJs = require("../../utils/numericLiterals.js");
14
+ const _objectKindsJs = require("../../utils/objectKinds.js");
15
+ const _serializeJs = require("../../utils/serialize.js");
16
+ const _definitionJs = require("../definition.js");
17
+ const _intersectionJs = require("./intersection.js");
18
+ const arrayIndexStringBranch = (0, _freezeJs.deepFreeze)({
19
+ regex: _numericLiteralsJs.wellFormedNonNegativeIntegerMatcher.source
20
+ });
21
+ const arrayIndexNumberBranch = (0, _freezeJs.deepFreeze)({
22
+ range: {
23
+ min: {
24
+ comparator: ">=",
25
+ limit: 0
26
+ }
27
+ },
28
+ divisor: 1
29
+ });
30
+ const parseKeyOfTuple = (def, ctx)=>{
31
+ const resolution = ctx.type.scope.resolveNode((0, _definitionJs.parseDefinition)(def[1], ctx));
32
+ const predicateKeys = (0, _genericsJs.objectKeysOf)(resolution).map((domain)=>keysOfPredicate(domain, resolution[domain]));
33
+ const sharedKeys = sharedKeysOf(predicateKeys);
34
+ if (!sharedKeys.length) {
35
+ return (0, _intersectionJs.writeImplicitNeverMessage)(ctx.path, "keyof");
36
+ }
37
+ const keyNode = {};
38
+ for (const key of sharedKeys){
39
+ const keyType = typeof key;
40
+ if (keyType === "string" || keyType === "number" || keyType === "symbol") {
41
+ var _keyNode, _keyType;
42
+ (_keyNode = keyNode)[_keyType = keyType] ?? (_keyNode[_keyType] = []);
43
+ keyNode[keyType].push({
44
+ value: key
45
+ });
46
+ } else if (key === _numericLiteralsJs.wellFormedNonNegativeIntegerMatcher) {
47
+ var _keyNode1, _keyNode2;
48
+ (_keyNode1 = keyNode).string ?? (_keyNode1.string = []);
49
+ keyNode.string.push(arrayIndexStringBranch);
50
+ (_keyNode2 = keyNode).number ?? (_keyNode2.number = []);
51
+ keyNode.number.push(arrayIndexNumberBranch);
52
+ } else {
53
+ return (0, _errorsJs.throwInternalError)(`Unexpected keyof key '${(0, _serializeJs.stringify)(key)}'`);
54
+ }
55
+ }
56
+ return Object.fromEntries(Object.entries(keyNode).map(([domain, branches])=>[
57
+ domain,
58
+ branches.length === 1 ? branches[0] : branches
59
+ ]));
60
+ };
61
+ const baseKeysByDomain = {
62
+ bigint: (0, _genericsJs.prototypeKeysOf)(0n),
63
+ boolean: (0, _genericsJs.prototypeKeysOf)(false),
64
+ null: [],
65
+ number: (0, _genericsJs.prototypeKeysOf)(0),
66
+ // TS doesn't include the Object prototype in keyof, so keyof object is never
67
+ object: [],
68
+ string: (0, _genericsJs.prototypeKeysOf)(""),
69
+ symbol: (0, _genericsJs.prototypeKeysOf)(Symbol()),
70
+ undefined: []
71
+ };
72
+ const keysOfPredicate = (domain, predicate)=>domain !== "object" || predicate === true ? baseKeysByDomain[domain] : sharedKeysOf((0, _genericsJs.listFrom)(predicate).map((branch)=>keysOfObjectBranch(branch)));
73
+ const sharedKeysOf = (keyBranches)=>{
74
+ if (!keyBranches.length) {
75
+ return [];
76
+ }
77
+ let sharedKeys = keyBranches[0];
78
+ for(let i = 1; i < keyBranches.length; i++){
79
+ // we can filter directly by equality here because the RegExp we're
80
+ // using will always be reference equal to
81
+ // wellFormedNonNegativeIntegerMatcher
82
+ sharedKeys = sharedKeys.filter((k)=>keyBranches[i].includes(k));
83
+ }
84
+ return sharedKeys;
85
+ };
86
+ const keysOfObjectBranch = (branch)=>{
87
+ const result = [];
88
+ if ("props" in branch) {
89
+ for (const key of Object.keys(branch.props)){
90
+ if (key === _propsJs.mappedKeys.index) {
91
+ // if any number is a valid key push this RegExp
92
+ result.push(_numericLiteralsJs.wellFormedNonNegativeIntegerMatcher);
93
+ } else if (!result.includes(key)) {
94
+ result.push(key);
95
+ if (_numericLiteralsJs.wellFormedNonNegativeIntegerMatcher.test(key)) {
96
+ // allow numeric access to keys
97
+ result.push((0, _numericLiteralsJs.tryParseWellFormedInteger)(key, `Unexpectedly failed to parse an integer from key '${key}'`));
98
+ }
99
+ }
100
+ }
101
+ }
102
+ if ("class" in branch) {
103
+ const constructor = typeof branch.class === "string" ? _objectKindsJs.defaultObjectKinds[branch.class] : branch.class;
104
+ for (const key of (0, _genericsJs.prototypeKeysOf)(constructor.prototype)){
105
+ if (!result.includes(key)) {
106
+ result.push(key);
107
+ }
108
+ }
109
+ }
110
+ return result;
111
+ };