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,149 +0,0 @@
1
- import { composeIntersection, composeKeyedIntersection, equality, isDisjoint, isEquality } from "../compose.js";
2
- import { flattenNode, isLiteralNode, nodeIntersection } from "../node.js";
3
- export const isOptional = (prop)=>prop[0] === "?";
4
- export const isPrerequisite = (prop)=>prop[0] === "!";
5
- export const mappedKeys = {
6
- index: "[index]"
7
- };
8
- export const propToNode = (prop)=>isOptional(prop) || isPrerequisite(prop) ? prop[1] : prop;
9
- const getTupleLengthIfPresent = (result)=>{
10
- if (typeof result.length === "object" && isPrerequisite(result.length) && typeof result.length[1] !== "string" && isLiteralNode(result.length[1], "number")) {
11
- return result.length[1].number.value;
12
- }
13
- };
14
- export const propsIntersection = composeIntersection((l, r, state)=>{
15
- const result = propKeysIntersection(l, r, state);
16
- if (typeof result === "symbol") {
17
- return result;
18
- }
19
- const lengthValue = getTupleLengthIfPresent(result);
20
- if (lengthValue === undefined || !(mappedKeys.index in result)) {
21
- return result;
22
- }
23
- // if we are at this point, we have an array with an exact length (i.e.
24
- // a tuple) and an index signature. Intersection each tuple item with
25
- // the index signature node and remove the index signature via a new
26
- // updated result, copied from result to avoid mutating existing references.
27
- const { [mappedKeys.index]: indexProp , ...updatedResult } = result;
28
- const indexNode = propToNode(indexProp);
29
- for(let i = 0; i < lengthValue; i++){
30
- if (!updatedResult[i]) {
31
- updatedResult[i] = indexNode;
32
- continue;
33
- }
34
- const existingNodeAtIndex = propToNode(updatedResult[i]);
35
- state.path.push(`${i}`);
36
- const updatedResultAtIndex = nodeIntersection(existingNodeAtIndex, indexNode, state);
37
- state.path.pop();
38
- if (isDisjoint(updatedResultAtIndex)) {
39
- return updatedResultAtIndex;
40
- } else if (!isEquality(updatedResultAtIndex) && updatedResultAtIndex !== existingNodeAtIndex) {
41
- updatedResult[i] = updatedResultAtIndex;
42
- }
43
- }
44
- return updatedResult;
45
- });
46
- const propKeysIntersection = composeKeyedIntersection((propKey, l, r, context)=>{
47
- if (l === undefined) {
48
- return r === undefined ? equality() : r;
49
- }
50
- if (r === undefined) {
51
- return l;
52
- }
53
- context.path.push(propKey);
54
- const previousLOptional = context.lOptional;
55
- const previousROptional = context.rOptional;
56
- (_context = context).lOptional || (_context.lOptional = isOptional(l));
57
- (_context1 = context).rOptional || (_context1.rOptional = isOptional(r));
58
- const result = nodeIntersection(propToNode(l), propToNode(r), context);
59
- const resultIsOptional = context.lOptional && context.rOptional;
60
- context.rOptional = previousROptional;
61
- context.lOptional = previousLOptional;
62
- context.path.pop();
63
- if (isDisjoint(result) && resultIsOptional) {
64
- var _context, _context1;
65
- // If an optional key has an empty intersection, the type can
66
- // still be satisfied as long as the key is not included. Set
67
- // the node to never rather than invalidating the type.
68
- return {};
69
- }
70
- return result;
71
- }, {
72
- onEmpty: "bubble"
73
- });
74
- export const flattenProps = (entries, props, ctx)=>{
75
- const keyConfig = ctx.type.config?.keys ?? ctx.type.scope.config.keys;
76
- return keyConfig === "loose" ? flattenLooseProps(entries, props, ctx) : flattenPropsRecord(keyConfig, entries, props, ctx);
77
- };
78
- const flattenLooseProps = (entries, props, ctx)=>{
79
- // if we don't care about extraneous keys, flatten props so we can iterate over the definitions directly
80
- for(const k in props){
81
- const prop = props[k];
82
- ctx.path.push(k);
83
- if (k === mappedKeys.index) {
84
- entries.push([
85
- "indexProp",
86
- flattenNode(propToNode(prop), ctx)
87
- ]);
88
- } else if (isOptional(prop)) {
89
- entries.push([
90
- "optionalProp",
91
- [
92
- k,
93
- flattenNode(prop[1], ctx)
94
- ]
95
- ]);
96
- } else if (isPrerequisite(prop)) {
97
- entries.push([
98
- "prerequisiteProp",
99
- [
100
- k,
101
- flattenNode(prop[1], ctx)
102
- ]
103
- ]);
104
- } else {
105
- entries.push([
106
- "requiredProp",
107
- [
108
- k,
109
- flattenNode(prop, ctx)
110
- ]
111
- ]);
112
- }
113
- ctx.path.pop();
114
- }
115
- };
116
- const flattenPropsRecord = (kind, entries, props, ctx)=>{
117
- const result = {
118
- required: {},
119
- optional: {}
120
- };
121
- // if we need to keep track of extraneous keys, either to add problems or
122
- // remove them, store the props as a Record to optimize for presence
123
- // checking as we iterate over the data
124
- for(const k in props){
125
- const prop = props[k];
126
- ctx.path.push(k);
127
- if (k === mappedKeys.index) {
128
- result.index = flattenNode(propToNode(prop), ctx);
129
- } else if (isOptional(prop)) {
130
- result.optional[k] = flattenNode(prop[1], ctx);
131
- } else if (isPrerequisite(prop)) {
132
- // we still have to push prerequisite props as normal entries so they can be checked first
133
- entries.push([
134
- "prerequisiteProp",
135
- [
136
- k,
137
- flattenNode(prop[1], ctx)
138
- ]
139
- ]);
140
- } else {
141
- result.required[k] = flattenNode(prop, ctx);
142
- }
143
- ctx.path.pop();
144
- }
145
- entries.push([
146
- `${kind}Props`,
147
- result
148
- ]);
149
- };
@@ -1,86 +0,0 @@
1
- import { sizeOf } from "../../utils/data.js";
2
- import { composeIntersection, equality } from "../compose.js";
3
- export const minComparators = {
4
- ">": true,
5
- ">=": true
6
- };
7
- export const maxComparators = {
8
- "<": true,
9
- "<=": true
10
- };
11
- export const isEqualityRange = (range)=>"comparator" in range;
12
- export const rangeIntersection = composeIntersection((l, r, state)=>{
13
- if (isEqualityRange(l)) {
14
- if (isEqualityRange(r)) {
15
- return l.limit === r.limit ? equality() : state.addDisjoint("range", l, r);
16
- }
17
- return rangeAllows(r, l.limit) ? l : state.addDisjoint("range", l, r);
18
- }
19
- if (isEqualityRange(r)) {
20
- return rangeAllows(l, r.limit) ? r : state.addDisjoint("range", l, r);
21
- }
22
- const stricterMin = compareStrictness("min", l.min, r.min);
23
- const stricterMax = compareStrictness("max", l.max, r.max);
24
- if (stricterMin === "l") {
25
- if (stricterMax === "r") {
26
- return compareStrictness("min", l.min, r.max) === "l" ? state.addDisjoint("range", l, r) : {
27
- min: l.min,
28
- max: r.max
29
- };
30
- }
31
- return l;
32
- }
33
- if (stricterMin === "r") {
34
- if (stricterMax === "l") {
35
- return compareStrictness("max", l.max, r.min) === "l" ? state.addDisjoint("range", l, r) : {
36
- min: r.min,
37
- max: l.max
38
- };
39
- }
40
- return r;
41
- }
42
- return stricterMax === "l" ? l : stricterMax === "r" ? r : equality();
43
- });
44
- const rangeAllows = (range, n)=>isEqualityRange(range) ? n === range.limit : minAllows(range.min, n) && maxAllows(range.max, n);
45
- const minAllows = (min, n)=>!min || n > min.limit || n === min.limit && !isExclusive(min.comparator);
46
- const maxAllows = (max, n)=>!max || n < max.limit || n === max.limit && !isExclusive(max.comparator);
47
- export const flattenRange = (entries, range, ctx)=>{
48
- const units = ctx.lastDomain === "string" ? "characters" : ctx.lastDomain === "object" ? "items long" : undefined;
49
- if (isEqualityRange(range)) {
50
- return entries.push([
51
- "bound",
52
- units ? {
53
- ...range,
54
- units
55
- } : range
56
- ]);
57
- }
58
- if (range.min) {
59
- entries.push([
60
- "bound",
61
- units ? {
62
- ...range.min,
63
- units
64
- } : range.min
65
- ]);
66
- }
67
- if (range.max) {
68
- entries.push([
69
- "bound",
70
- units ? {
71
- ...range.max,
72
- units
73
- } : range.max
74
- ]);
75
- }
76
- };
77
- export const checkBound = (bound, state)=>comparatorCheckers[bound.comparator](sizeOf(state.data), bound.limit) || !state.problems.add("bound", bound);
78
- const comparatorCheckers = {
79
- "<": (size, limit)=>size < limit,
80
- ">": (size, limit)=>size > limit,
81
- "<=": (size, limit)=>size <= limit,
82
- ">=": (size, limit)=>size >= limit,
83
- "==": (size, limit)=>size === limit
84
- };
85
- export 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";
86
- const isExclusive = (comparator)=>comparator.length === 1;
@@ -1,11 +0,0 @@
1
- import { composeIntersection } from "../compose.js";
2
- import { collapsibleListUnion } from "./collapsibleSet.js";
3
- const regexCache = {};
4
- export const getRegex = (source)=>{
5
- if (!regexCache[source]) {
6
- regexCache[source] = new RegExp(source);
7
- }
8
- return regexCache[source];
9
- };
10
- export const checkRegex = (source, state)=>getRegex(source).test(state.data) || !state.problems.add("regex", `/${source}/`);
11
- export const regexIntersection = composeIntersection(collapsibleListUnion);
@@ -1,106 +0,0 @@
1
- import { listFrom } from "../../utils/generics.js";
2
- import { composeIntersection, composeKeyedIntersection, equality } from "../compose.js";
3
- import { classIntersection } from "./class.js";
4
- import { collapsibleListUnion } from "./collapsibleSet.js";
5
- import { divisorIntersection } from "./divisor.js";
6
- import { flattenProps, propsIntersection } from "./props.js";
7
- import { flattenRange, rangeIntersection } from "./range.js";
8
- import { regexIntersection } from "./regex.js";
9
- export const rulesIntersection = (l, r, state)=>"value" in l ? "value" in r ? l.value === r.value ? 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);
10
- const narrowIntersection = composeIntersection(collapsibleListUnion);
11
- export const narrowableRulesIntersection = composeKeyedIntersection({
12
- divisor: divisorIntersection,
13
- regex: regexIntersection,
14
- props: propsIntersection,
15
- class: classIntersection,
16
- range: rangeIntersection,
17
- narrow: narrowIntersection
18
- }, {
19
- onEmpty: "bubble"
20
- });
21
- export const flattenRules = (rules, ctx)=>{
22
- const entries = [];
23
- let k;
24
- for(k in rules){
25
- ruleFlatteners[k](entries, rules[k], ctx);
26
- }
27
- // Some entries with the same precedence, e.g. morphs flattened from a list,
28
- // rely on the fact that JS's builtin sort is stable to behave as expected
29
- // when traversed:
30
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
31
- return entries.sort((l, r)=>precedenceMap[l[0]] - precedenceMap[r[0]]);
32
- };
33
- const ruleFlatteners = {
34
- regex: (entries, rule)=>{
35
- for (const source of listFrom(rule)){
36
- entries.push([
37
- "regex",
38
- source
39
- ]);
40
- }
41
- },
42
- divisor: (entries, rule)=>{
43
- entries.push([
44
- "divisor",
45
- rule
46
- ]);
47
- },
48
- range: flattenRange,
49
- class: (entries, rule)=>{
50
- entries.push([
51
- "class",
52
- rule
53
- ]);
54
- },
55
- props: flattenProps,
56
- narrow: (entries, rule)=>{
57
- for (const narrow of listFrom(rule)){
58
- entries.push([
59
- "narrow",
60
- narrow
61
- ]);
62
- }
63
- },
64
- value: (entries, rule)=>{
65
- entries.push([
66
- "value",
67
- rule
68
- ]);
69
- }
70
- };
71
- export const precedenceMap = {
72
- // Config: Applies before any checks
73
- config: -1,
74
- // Critical: No other checks are performed if these fail
75
- domain: 0,
76
- value: 0,
77
- domains: 0,
78
- branches: 0,
79
- switch: 0,
80
- alias: 0,
81
- class: 0,
82
- // Shallow: All shallow checks will be performed even if one or more fail
83
- regex: 1,
84
- divisor: 1,
85
- bound: 1,
86
- // Prerequisite: These are deep checks with special priority, e.g. the
87
- // length of a tuple, which causes other deep props not to be checked if it
88
- // is invalid
89
- prerequisiteProp: 2,
90
- // Deep: Performed if all shallow checks pass, even if one or more deep checks fail
91
- distilledProps: 3,
92
- strictProps: 3,
93
- requiredProp: 3,
94
- optionalProp: 3,
95
- indexProp: 3,
96
- // Narrow: Only performed if all shallow and deep checks pass
97
- narrow: 4,
98
- // Morph: Only performed if all validation passes
99
- morph: 5
100
- };
101
- export const literalSatisfiesRules = (data, rules, state)=>!state.type.scope.type([
102
- "node",
103
- {
104
- [state.domain]: rules
105
- }
106
- ])(data).problems;
@@ -1,3 +0,0 @@
1
- {
2
- "type": "module"
3
- }
@@ -1 +0,0 @@
1
- export { };
@@ -1,2 +0,0 @@
1
- export const writeDoubleRightBoundMessage = (root)=>`Expression ${root} must have at most one right bound`;
2
- export const writeUnboundableMessage = (root)=>`Bounded expression ${root} must be a number, string or array`;
@@ -1,9 +0,0 @@
1
- import { parseDefinition } from "../definition.js";
2
- /**
3
- * @operator {@link parseConfigTuple | :}
4
- * @docgenTable
5
- * @tuple ["type", ":", config]
6
- */ export const parseConfigTuple = (def, ctx)=>({
7
- node: ctx.type.scope.resolveTypeNode(parseDefinition(def[0], ctx)),
8
- config: def[2]
9
- });
@@ -1,34 +0,0 @@
1
- import { hasDomain } from "../../utils/domains.js";
2
- import { throwParseError } from "../../utils/errors.js";
3
- import { objectKeysOf } from "../../utils/generics.js";
4
- import { stringify } from "../../utils/serialize.js";
5
- export const writeMalformedDistributableFunctionMessage = (def)=>`Expected a Function or Record<Domain, Function> operand (${stringify(def)} was invalid)`;
6
- export const distributeFunctionToNode = (distributableFunction, node, ctx, ruleKey)=>{
7
- const domains = objectKeysOf(node);
8
- if (!hasDomain(distributableFunction, "object")) {
9
- return throwParseError(writeMalformedDistributableFunctionMessage(distributableFunction));
10
- }
11
- const distributed = {};
12
- if (typeof distributableFunction === "function") {
13
- const domainFunction = {
14
- [ruleKey]: distributableFunction
15
- };
16
- for (const domain of domains){
17
- distributed[domain] = domainFunction;
18
- }
19
- } else {
20
- for (const domain of domains){
21
- if (distributableFunction[domain] === undefined) {
22
- continue;
23
- }
24
- const functionInDomain = {
25
- [ruleKey]: distributableFunction[domain]
26
- };
27
- if (typeof functionInDomain[ruleKey] !== "function") {
28
- return throwParseError(writeMalformedDistributableFunctionMessage(functionInDomain));
29
- }
30
- distributed[domain] = functionInDomain;
31
- }
32
- }
33
- return distributed;
34
- };
@@ -1 +0,0 @@
1
- export const writeIndivisibleMessage = (root)=>`Divisibility operand ${root} must be a number`;
@@ -1,16 +0,0 @@
1
- import { disjointDescriptionWriters } from "../../nodes/compose.js";
2
- import { objectKeysOf } from "../../utils/generics.js";
3
- export const compileDisjointReasonsMessage = (disjoints)=>{
4
- const paths = objectKeysOf(disjoints);
5
- if (paths.length === 1) {
6
- const path = paths[0];
7
- return `${path === "/" ? "" : `At ${path}: `}Intersection of ${disjointDescriptionWriters[disjoints[path].kind](disjoints[path])} results in an unsatisfiable type`;
8
- }
9
- let message = `
10
- "Intersection results in unsatisfiable types at the following paths:\n`;
11
- for(const path in disjoints){
12
- message += ` ${path}: ${disjointDescriptionWriters[disjoints[path].kind](disjoints[path])}\n`;
13
- }
14
- return message;
15
- };
16
- export const writeImplicitNeverMessage = (path, operator, description)=>`${path.length ? `At ${path}: ` : ""}${operator} ${description ? `${description} ` : ""}results in an unsatisfiable type`;
@@ -1,103 +0,0 @@
1
- import { mappedKeys } from "../../nodes/rules/props.js";
2
- import { throwInternalError } from "../../utils/errors.js";
3
- import { deepFreeze } from "../../utils/freeze.js";
4
- import { listFrom, objectKeysOf, prototypeKeysOf } from "../../utils/generics.js";
5
- import { tryParseWellFormedInteger, wellFormedNonNegativeIntegerMatcher } from "../../utils/numericLiterals.js";
6
- import { defaultObjectKinds } from "../../utils/objectKinds.js";
7
- import { stringify } from "../../utils/serialize.js";
8
- import { parseDefinition } from "../definition.js";
9
- import { writeImplicitNeverMessage } from "./intersection.js";
10
- const arrayIndexStringBranch = deepFreeze({
11
- regex: wellFormedNonNegativeIntegerMatcher.source
12
- });
13
- const arrayIndexNumberBranch = deepFreeze({
14
- range: {
15
- min: {
16
- comparator: ">=",
17
- limit: 0
18
- }
19
- },
20
- divisor: 1
21
- });
22
- export const parseKeyOfTuple = (def, ctx)=>{
23
- const resolution = ctx.type.scope.resolveNode(parseDefinition(def[1], ctx));
24
- const predicateKeys = objectKeysOf(resolution).map((domain)=>keysOfPredicate(domain, resolution[domain]));
25
- const sharedKeys = sharedKeysOf(predicateKeys);
26
- if (!sharedKeys.length) {
27
- return writeImplicitNeverMessage(ctx.path, "keyof");
28
- }
29
- const keyNode = {};
30
- for (const key of sharedKeys){
31
- const keyType = typeof key;
32
- if (keyType === "string" || keyType === "number" || keyType === "symbol") {
33
- var _keyNode, _keyType;
34
- (_keyNode = keyNode)[_keyType = keyType] ?? (_keyNode[_keyType] = []);
35
- keyNode[keyType].push({
36
- value: key
37
- });
38
- } else if (key === wellFormedNonNegativeIntegerMatcher) {
39
- var _keyNode1, _keyNode2;
40
- (_keyNode1 = keyNode).string ?? (_keyNode1.string = []);
41
- keyNode.string.push(arrayIndexStringBranch);
42
- (_keyNode2 = keyNode).number ?? (_keyNode2.number = []);
43
- keyNode.number.push(arrayIndexNumberBranch);
44
- } else {
45
- return throwInternalError(`Unexpected keyof key '${stringify(key)}'`);
46
- }
47
- }
48
- return Object.fromEntries(Object.entries(keyNode).map(([domain, branches])=>[
49
- domain,
50
- branches.length === 1 ? branches[0] : branches
51
- ]));
52
- };
53
- const baseKeysByDomain = {
54
- bigint: prototypeKeysOf(0n),
55
- boolean: prototypeKeysOf(false),
56
- null: [],
57
- number: prototypeKeysOf(0),
58
- // TS doesn't include the Object prototype in keyof, so keyof object is never
59
- object: [],
60
- string: prototypeKeysOf(""),
61
- symbol: prototypeKeysOf(Symbol()),
62
- undefined: []
63
- };
64
- const keysOfPredicate = (domain, predicate)=>domain !== "object" || predicate === true ? baseKeysByDomain[domain] : sharedKeysOf(listFrom(predicate).map((branch)=>keysOfObjectBranch(branch)));
65
- const sharedKeysOf = (keyBranches)=>{
66
- if (!keyBranches.length) {
67
- return [];
68
- }
69
- let sharedKeys = keyBranches[0];
70
- for(let i = 1; i < keyBranches.length; i++){
71
- // we can filter directly by equality here because the RegExp we're
72
- // using will always be reference equal to
73
- // wellFormedNonNegativeIntegerMatcher
74
- sharedKeys = sharedKeys.filter((k)=>keyBranches[i].includes(k));
75
- }
76
- return sharedKeys;
77
- };
78
- const keysOfObjectBranch = (branch)=>{
79
- const result = [];
80
- if ("props" in branch) {
81
- for (const key of Object.keys(branch.props)){
82
- if (key === mappedKeys.index) {
83
- // if any number is a valid key push this RegExp
84
- result.push(wellFormedNonNegativeIntegerMatcher);
85
- } else if (!result.includes(key)) {
86
- result.push(key);
87
- if (wellFormedNonNegativeIntegerMatcher.test(key)) {
88
- // allow numeric access to keys
89
- result.push(tryParseWellFormedInteger(key, `Unexpectedly failed to parse an integer from key '${key}'`));
90
- }
91
- }
92
- }
93
- }
94
- if ("class" in branch) {
95
- const constructor = typeof branch.class === "string" ? defaultObjectKinds[branch.class] : branch.class;
96
- for (const key of prototypeKeysOf(constructor.prototype)){
97
- if (!result.includes(key)) {
98
- result.push(key);
99
- }
100
- }
101
- }
102
- return result;
103
- };
@@ -1,44 +0,0 @@
1
- import { isTransformationBranch } from "../../nodes/branch.js";
2
- import { throwInternalError, throwParseError } from "../../utils/errors.js";
3
- import { isArray } from "../../utils/objectKinds.js";
4
- import { stringify } from "../../utils/serialize.js";
5
- import { parseDefinition } from "../definition.js";
6
- export const parseMorphTuple = (def, ctx)=>{
7
- if (typeof def[2] !== "function") {
8
- return throwParseError(writeMalformedMorphExpressionMessage(def[2]));
9
- }
10
- const node = parseDefinition(def[0], ctx);
11
- const resolution = ctx.type.scope.resolveTypeNode(node);
12
- const morph = def[2];
13
- ctx.type.includesMorph = true;
14
- let domain;
15
- const result = {};
16
- for(domain in resolution){
17
- const predicate = resolution[domain];
18
- if (predicate === true) {
19
- result[domain] = {
20
- rules: {},
21
- morph
22
- };
23
- } else if (typeof predicate === "object") {
24
- result[domain] = isArray(predicate) ? predicate.map((branch)=>applyMorph(branch, morph)) : applyMorph(predicate, morph);
25
- } else {
26
- throwInternalError(`Unexpected predicate value for domain '${domain}': ${stringify(predicate)}`);
27
- }
28
- }
29
- return result;
30
- };
31
- const applyMorph = (branch, morph)=>isTransformationBranch(branch) ? {
32
- ...branch,
33
- morph: branch.morph ? Array.isArray(branch.morph) ? [
34
- ...branch.morph,
35
- morph
36
- ] : [
37
- branch.morph,
38
- morph
39
- ] : morph
40
- } : {
41
- rules: branch,
42
- morph
43
- };
44
- export const writeMalformedMorphExpressionMessage = (value)=>`Morph expression requires a function following '|>' (was ${typeof value})`;
@@ -1,14 +0,0 @@
1
- import { isConfigNode, rootIntersection } from "../../nodes/node.js";
2
- import { parseDefinition } from "../definition.js";
3
- import { distributeFunctionToNode } from "./distributableFunction.js";
4
- export const parseNarrowTuple = (def, ctx)=>{
5
- const inputNode = parseDefinition(def[0], ctx);
6
- const resolution = ctx.type.scope.resolveNode(inputNode);
7
- const hasConfig = isConfigNode(resolution);
8
- const typeNode = hasConfig ? resolution.node : resolution;
9
- const result = rootIntersection(inputNode, distributeFunctionToNode(def[2], typeNode, ctx, "narrow"), ctx.type);
10
- return hasConfig ? {
11
- config: resolution.config,
12
- node: result
13
- } : result;
14
- };
@@ -1 +0,0 @@
1
- export { };