arktype 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/dist/cjs/nodes/{branches.js → branch.js} +56 -10
  2. package/dist/cjs/nodes/compose.js +1 -2
  3. package/dist/cjs/nodes/discriminate.js +7 -13
  4. package/dist/cjs/nodes/node.js +12 -2
  5. package/dist/cjs/nodes/predicate.js +7 -9
  6. package/dist/cjs/nodes/rules/class.js +10 -4
  7. package/dist/cjs/nodes/rules/divisor.js +1 -5
  8. package/dist/cjs/nodes/rules/props.js +75 -25
  9. package/dist/cjs/nodes/rules/range.js +1 -5
  10. package/dist/cjs/nodes/rules/regex.js +1 -5
  11. package/dist/cjs/nodes/rules/rules.js +19 -63
  12. package/dist/cjs/parse/string/reduce/dynamic.js +1 -2
  13. package/dist/cjs/parse/string/shift/operator/bounds.js +1 -1
  14. package/dist/cjs/parse/string/string.js +2 -2
  15. package/dist/cjs/parse/tuple/keyof.js +37 -18
  16. package/dist/cjs/parse/tuple/morph.js +2 -2
  17. package/dist/cjs/parse/tuple/tuple.js +16 -9
  18. package/dist/cjs/scopes/jsObjects.js +14 -14
  19. package/dist/cjs/scopes/type.js +4 -7
  20. package/dist/cjs/traverse/problems.js +12 -28
  21. package/dist/cjs/traverse/traverse.js +104 -133
  22. package/dist/cjs/utils/numericLiterals.js +2 -0
  23. package/dist/cjs/utils/objectKinds.js +1 -2
  24. package/dist/mjs/nodes/{branches.js → branch.js} +45 -2
  25. package/dist/mjs/nodes/compose.js +1 -2
  26. package/dist/mjs/nodes/discriminate.js +6 -12
  27. package/dist/mjs/nodes/node.js +10 -1
  28. package/dist/mjs/nodes/predicate.js +2 -3
  29. package/dist/mjs/nodes/rules/class.js +10 -4
  30. package/dist/mjs/nodes/rules/divisor.js +1 -5
  31. package/dist/mjs/nodes/rules/props.js +74 -27
  32. package/dist/mjs/nodes/rules/range.js +1 -5
  33. package/dist/mjs/nodes/rules/regex.js +1 -5
  34. package/dist/mjs/nodes/rules/rules.js +19 -62
  35. package/dist/mjs/parse/string/reduce/dynamic.js +2 -3
  36. package/dist/mjs/parse/string/shift/operator/bounds.js +1 -1
  37. package/dist/mjs/parse/string/string.js +2 -2
  38. package/dist/mjs/parse/tuple/keyof.js +38 -19
  39. package/dist/mjs/parse/tuple/morph.js +2 -2
  40. package/dist/mjs/parse/tuple/tuple.js +17 -10
  41. package/dist/mjs/scopes/jsObjects.js +14 -14
  42. package/dist/mjs/scopes/type.js +4 -7
  43. package/dist/mjs/traverse/problems.js +13 -29
  44. package/dist/mjs/traverse/traverse.js +105 -133
  45. package/dist/mjs/utils/numericLiterals.js +1 -0
  46. package/dist/mjs/utils/objectKinds.js +1 -1
  47. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  48. package/dist/types/nodes/branch.d.ts +36 -0
  49. package/dist/types/nodes/compose.d.ts +5 -10
  50. package/dist/types/nodes/discriminate.d.ts +1 -1
  51. package/dist/types/nodes/infer.d.ts +17 -16
  52. package/dist/types/nodes/node.d.ts +10 -5
  53. package/dist/types/nodes/predicate.d.ts +4 -32
  54. package/dist/types/nodes/rules/class.d.ts +3 -2
  55. package/dist/types/nodes/rules/divisor.d.ts +2 -1
  56. package/dist/types/nodes/rules/props.d.ts +19 -11
  57. package/dist/types/nodes/rules/range.d.ts +3 -2
  58. package/dist/types/nodes/rules/regex.d.ts +2 -1
  59. package/dist/types/nodes/rules/rules.d.ts +11 -13
  60. package/dist/types/parse/string/ast.d.ts +2 -1
  61. package/dist/types/parse/string/reduce/dynamic.d.ts +1 -1
  62. package/dist/types/parse/string/reduce/shared.d.ts +1 -1
  63. package/dist/types/parse/string/shift/operand/unenclosed.d.ts +1 -1
  64. package/dist/types/parse/string/shift/operator/bounds.d.ts +1 -1
  65. package/dist/types/parse/string/string.d.ts +1 -1
  66. package/dist/types/parse/tuple/keyof.d.ts +2 -2
  67. package/dist/types/scopes/type.d.ts +5 -11
  68. package/dist/types/traverse/problems.d.ts +8 -23
  69. package/dist/types/traverse/traverse.d.ts +27 -23
  70. package/dist/types/utils/generics.d.ts +0 -1
  71. package/dist/types/utils/numericLiterals.d.ts +1 -0
  72. package/dist/types/utils/objectKinds.d.ts +43 -43
  73. package/package.json +1 -2
  74. package/dist/cjs/nodes/functors.js +0 -35
  75. package/dist/cjs/nodes/rules/objectKind.js +0 -78
  76. package/dist/mjs/nodes/functors.js +0 -27
  77. package/dist/mjs/nodes/rules/objectKind.js +0 -63
  78. package/dist/types/nodes/branches.d.ts +0 -14
  79. package/dist/types/nodes/functors.d.ts +0 -6
  80. package/dist/types/nodes/rules/objectKind.d.ts +0 -15
@@ -10,12 +10,16 @@ function _export(target, all) {
10
10
  }
11
11
  _export(exports, {
12
12
  isBranchComparison: ()=>isBranchComparison,
13
- compareBranches: ()=>compareBranches
13
+ compareBranches: ()=>compareBranches,
14
+ isMorphBranch: ()=>isMorphBranch,
15
+ flattenBranch: ()=>flattenBranch,
16
+ branchIntersection: ()=>branchIntersection
14
17
  });
15
- const _domainsTs = require("../utils/domains.js");
16
- const _errorsTs = require("../utils/errors.js");
17
- const _composeTs = require("./compose.js");
18
- const _rulesTs = require("./rules/rules.js");
18
+ const _astJs = require("../parse/string/ast.js");
19
+ const _domainsJs = require("../utils/domains.js");
20
+ const _errorsJs = require("../utils/errors.js");
21
+ const _composeJs = require("./compose.js");
22
+ const _rulesJs = require("./rules/rules.js");
19
23
  const isBranchComparison = (comparison)=>comparison?.lBranches !== undefined;
20
24
  const compareBranches = (lConditions, rConditions, state)=>{
21
25
  const result = {
@@ -37,8 +41,8 @@ const compareBranches = (lConditions, rConditions, state)=>{
37
41
  return null;
38
42
  }
39
43
  const r = rPairs.condition;
40
- const subresult = (0, _rulesTs.branchIntersection)(l, r, state);
41
- if ((0, _composeTs.isDisjoint)(subresult)) {
44
+ const subresult = branchIntersection(l, r, state);
45
+ if ((0, _composeJs.isDisjoint)(subresult)) {
42
46
  // doesn't tell us about any redundancies or add a distinct pair
43
47
  return null;
44
48
  } else if (subresult === l) {
@@ -55,7 +59,7 @@ const compareBranches = (lConditions, rConditions, state)=>{
55
59
  // distinct branches.
56
60
  rPairs.distinct = null;
57
61
  return null;
58
- } else if ((0, _composeTs.isEquality)(subresult)) {
62
+ } else if ((0, _composeJs.isEquality)(subresult)) {
59
63
  // Combination of l and r subtype cases.
60
64
  result.equalities.push([
61
65
  lIndex,
@@ -64,13 +68,13 @@ const compareBranches = (lConditions, rConditions, state)=>{
64
68
  lImpliesR = true;
65
69
  rPairs.distinct = null;
66
70
  return null;
67
- } else if ((0, _domainsTs.hasDomain)(subresult, "object")) {
71
+ } else if ((0, _domainsJs.hasDomain)(subresult, "object")) {
68
72
  // Neither branch is a subtype of the other, return
69
73
  // the result of the intersection as a candidate
70
74
  // branch for the final union
71
75
  return subresult;
72
76
  }
73
- return (0, _errorsTs.throwInternalError)(`Unexpected predicate intersection result of type '${(0, _domainsTs.domainOf)(subresult)}'`);
77
+ return (0, _errorsJs.throwInternalError)(`Unexpected predicate intersection result of type '${(0, _domainsJs.domainOf)(subresult)}'`);
74
78
  });
75
79
  if (!lImpliesR) {
76
80
  for(let i = 0; i < pairs.length; i++){
@@ -83,3 +87,45 @@ const compareBranches = (lConditions, rConditions, state)=>{
83
87
  result.distinctIntersections = pairs.flatMap((pairs)=>pairs.distinct ?? []);
84
88
  return result;
85
89
  };
90
+ const isMorphBranch = (branch)=>"morph" in branch;
91
+ const flattenBranch = (branch, ctx)=>{
92
+ if (isMorphBranch(branch)) {
93
+ const result = (0, _rulesJs.flattenRules)(branch.rules, ctx);
94
+ if (branch.morph) {
95
+ result.push([
96
+ "morph",
97
+ branch.morph
98
+ ]);
99
+ }
100
+ return result;
101
+ }
102
+ return (0, _rulesJs.flattenRules)(branch, ctx);
103
+ };
104
+ const rulesOf = (branch)=>branch.rules ?? branch;
105
+ const branchIntersection = (l, r, state)=>{
106
+ const lRules = rulesOf(l);
107
+ const rRules = rulesOf(r);
108
+ const rulesResult = (0, _rulesJs.rulesIntersection)(lRules, rRules, state);
109
+ if ("morph" in l) {
110
+ if ("morph" in r) {
111
+ if (l.morph === r.morph) {
112
+ return (0, _composeJs.isEquality)(rulesResult) || (0, _composeJs.isDisjoint)(rulesResult) ? rulesResult : {
113
+ rules: rulesResult,
114
+ morph: l.morph
115
+ };
116
+ }
117
+ return state.lastOperator === "&" ? (0, _errorsJs.throwParseError)((0, _astJs.writeImplicitNeverMessage)(state.path, "Intersection", "of morphs")) : {};
118
+ }
119
+ return (0, _composeJs.isDisjoint)(rulesResult) ? rulesResult : {
120
+ rules: (0, _composeJs.isEquality)(rulesResult) ? l.rules : rulesResult,
121
+ morph: l.morph
122
+ };
123
+ }
124
+ if ("morph" in r) {
125
+ return (0, _composeJs.isDisjoint)(rulesResult) ? rulesResult : {
126
+ rules: (0, _composeJs.isEquality)(rulesResult) ? r.rules : rulesResult,
127
+ morph: r.morph
128
+ };
129
+ }
130
+ return rulesResult;
131
+ };
@@ -68,9 +68,8 @@ const composeIntersection = (reducer)=>(l, r, state)=>l === undefined ? r === un
68
68
  const throwUndefinedOperandsError = ()=>(0, _errorsTs.throwInternalError)(`Unexpected operation two undefined operands`);
69
69
  const disjointDescriptionWriters = {
70
70
  domain: ({ l , r })=>`${l.join(", ")} and ${r.join(", ")}`,
71
- objectKind: ({ l , r })=>`${l} and ${r}`,
72
71
  range: ({ l , r })=>`${stringifyRange(l)} and ${stringifyRange(r)}`,
73
- class: ({ l , r })=>`classes ${l.name} and ${r.name}`,
72
+ class: ({ l , r })=>`classes ${typeof l === "string" ? l : l.name} and ${typeof r === "string" ? r : r.name}`,
74
73
  tupleLength: ({ l , r })=>`tuples of length ${l} and ${r}`,
75
74
  value: ({ l , r })=>`literal values ${(0, _serializeTs.serialize)(l)} and ${(0, _serializeTs.serialize)(r)}`,
76
75
  leftAssignability: ({ l , r })=>`literal value ${(0, _serializeTs.serialize)(l.value)} and ${(0, _serializeTs.serialize)(r)}`,
@@ -20,9 +20,9 @@ const _genericsTs = require("../utils/generics.js");
20
20
  const _objectKindsTs = require("../utils/objectKinds.js");
21
21
  const _pathsTs = require("../utils/paths.js");
22
22
  const _serializeTs = require("../utils/serialize.js");
23
+ const _branchTs = require("./branch.js");
23
24
  const _composeTs = require("./compose.js");
24
25
  const _propsTs = require("./rules/props.js");
25
- const _rulesTs = require("./rules/rules.js");
26
26
  const flattenBranches = (branches, ctx)=>{
27
27
  const discriminants = calculateDiscriminants(branches, ctx);
28
28
  const indices = branches.map((_, i)=>i);
@@ -30,14 +30,14 @@ const flattenBranches = (branches, ctx)=>{
30
30
  };
31
31
  const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
32
32
  if (remainingIndices.length === 1) {
33
- return (0, _rulesTs.flattenBranch)(originalBranches[remainingIndices[0]], ctx);
33
+ return (0, _branchTs.flattenBranch)(originalBranches[remainingIndices[0]], ctx);
34
34
  }
35
35
  const bestDiscriminant = findBestDiscriminant(remainingIndices, discriminants);
36
36
  if (!bestDiscriminant) {
37
37
  return [
38
38
  [
39
39
  "branches",
40
- remainingIndices.map((i)=>(0, _rulesTs.flattenBranch)(originalBranches[i], ctx))
40
+ remainingIndices.map((i)=>branchIncludesMorph(originalBranches[i], ctx.type.meta.scope) ? (0, _errorsTs.throwParseError)((0, _astTs.writeUndiscriminatableMorphUnionMessage)(`${ctx.path}`)) : (0, _branchTs.flattenBranch)(originalBranches[i], ctx))
41
41
  ]
42
42
  ];
43
43
  }
@@ -68,17 +68,15 @@ const calculateDiscriminants = (branches, ctx)=>{
68
68
  disjointsByPair: {},
69
69
  casesByDisjoint: {}
70
70
  };
71
- const morphBranches = branches.map((branch)=>branchIncludesMorph(branch, ctx.type.meta.scope));
72
71
  for(let lIndex = 0; lIndex < branches.length - 1; lIndex++){
73
72
  for(let rIndex = lIndex + 1; rIndex < branches.length; rIndex++){
74
73
  const pairKey = `${lIndex}/${rIndex}`;
75
74
  const pairDisjoints = [];
76
75
  discriminants.disjointsByPair[pairKey] = pairDisjoints;
77
76
  const intersectionState = new _composeTs.IntersectionState(ctx.type, "|");
78
- (0, _rulesTs.branchIntersection)(branches[lIndex], branches[rIndex], intersectionState);
77
+ (0, _branchTs.branchIntersection)(branches[lIndex], branches[rIndex], intersectionState);
79
78
  for(const path in intersectionState.disjoints){
80
- // designed to match objectKind path segments like "${number}"
81
- if (path.includes("${")) {
79
+ if (path.includes(_propsTs.mappedKeys.index)) {
82
80
  continue;
83
81
  }
84
82
  const { l , r , kind } = intersectionState.disjoints[path];
@@ -119,9 +117,6 @@ const calculateDiscriminants = (branches, ctx)=>{
119
117
  cases[rSerialized].push(rIndex);
120
118
  }
121
119
  }
122
- if ((morphBranches[lIndex] === true || morphBranches[rIndex] === true) && pairDisjoints.length === 0) {
123
- return (0, _errorsTs.throwParseError)((0, _astTs.writeUndiscriminatableMorphUnionMessage)(`${ctx.path}`));
124
- }
125
120
  }
126
121
  }
127
122
  return discriminants;
@@ -164,8 +159,7 @@ const findBestDiscriminant = (remainingIndices, discriminants)=>{
164
159
  }
165
160
  const defaultCaseKeys = (0, _genericsTs.keysOf)(defaultCases);
166
161
  if (defaultCaseKeys.length) {
167
- const defaultIndices = defaultCaseKeys.map((k)=>parseInt(k));
168
- filteredCases["default"] = defaultIndices;
162
+ filteredCases["default"] = defaultCaseKeys.map((k)=>parseInt(k));
169
163
  }
170
164
  if (!bestDiscriminant || score > bestDiscriminant.score) {
171
165
  const [path, kind] = parseQualifiedDisjoint(qualifiedDisjoint);
@@ -196,5 +190,5 @@ const caseSerializers = {
196
190
  domain: _domainsTs.domainOf
197
191
  };
198
192
  const serializeCase = (kind, data)=>caseSerializers[kind](data);
199
- const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph((0, _propsTs.isOptional)(prop) ? prop[1] : prop, $)) : false;
193
+ const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph((0, _propsTs.propToNode)(prop), $)) : false;
200
194
  const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).meta.includesMorph : Object.values(node).some((predicate)=>predicate === true ? false : (0, _objectKindsTs.isArray)(predicate) ? predicate.some((branch)=>branchIncludesMorph(branch, $)) : branchIncludesMorph(predicate, $));
@@ -14,7 +14,8 @@ _export(exports, {
14
14
  union: ()=>union,
15
15
  flattenType: ()=>flattenType,
16
16
  flattenNode: ()=>flattenNode,
17
- isLiteralNode: ()=>isLiteralNode
17
+ isLiteralNode: ()=>isLiteralNode,
18
+ arrayOf: ()=>arrayOf
18
19
  });
19
20
  const _astTs = require("../parse/string/ast.js");
20
21
  const _errorsTs = require("../utils/errors.js");
@@ -22,6 +23,7 @@ const _genericsTs = require("../utils/generics.js");
22
23
  const _pathsTs = require("../utils/paths.js");
23
24
  const _composeTs = require("./compose.js");
24
25
  const _predicateTs = require("./predicate.js");
26
+ const _propsTs = require("./rules/props.js");
25
27
  const nodeIntersection = (l, r, state)=>{
26
28
  state.domain = undefined;
27
29
  const lResolution = state.type.meta.scope.resolveNode(l);
@@ -61,7 +63,7 @@ const union = (l, r, type)=>{
61
63
  }
62
64
  return result;
63
65
  };
64
- const hasImpliedDomain = (flatPredicate)=>flatPredicate[0] && (flatPredicate[0][0] === "objectKind" || flatPredicate[0][0] === "value");
66
+ const hasImpliedDomain = (flatPredicate)=>flatPredicate[0] && (flatPredicate[0][0] === "value" || flatPredicate[0][0] === "class");
65
67
  const flattenType = (type)=>{
66
68
  const ctx = {
67
69
  type,
@@ -124,3 +126,11 @@ const flattenNode = (node, ctx)=>{
124
126
  const isLiteralNode = (node, domain)=>{
125
127
  return (0, _predicateTs.resolutionExtendsDomain)(node, domain) && (0, _predicateTs.isLiteralCondition)(node[domain]);
126
128
  };
129
+ const arrayOf = (node)=>({
130
+ object: {
131
+ class: "Array",
132
+ props: {
133
+ [_propsTs.mappedKeys.index]: node
134
+ }
135
+ }
136
+ });
@@ -9,7 +9,6 @@ function _export(target, all) {
9
9
  });
10
10
  }
11
11
  _export(exports, {
12
- branchIsTransformation: ()=>branchIsTransformation,
13
12
  comparePredicates: ()=>comparePredicates,
14
13
  predicateIntersection: ()=>predicateIntersection,
15
14
  predicateUnion: ()=>predicateUnion,
@@ -19,23 +18,22 @@ _export(exports, {
19
18
  });
20
19
  const _genericsTs = require("../utils/generics.js");
21
20
  const _objectKindsTs = require("../utils/objectKinds.js");
22
- const _branchesTs = require("./branches.js");
21
+ const _branchTs = require("./branch.js");
23
22
  const _composeTs = require("./compose.js");
24
23
  const _discriminateTs = require("./discriminate.js");
25
- const _rulesTs = require("./rules/rules.js");
26
- const branchIsTransformation = (branch)=>"rules" in branch;
24
+ require("./rules/rules.js");
27
25
  const emptyRulesIfTrue = (predicate)=>predicate === true ? {} : predicate;
28
26
  const comparePredicates = (l, r, context)=>{
29
27
  if (l === true && r === true) {
30
28
  return (0, _composeTs.equality)();
31
29
  }
32
30
  if (!(0, _objectKindsTs.isArray)(l) && !(0, _objectKindsTs.isArray)(r)) {
33
- const result = (0, _rulesTs.branchIntersection)(emptyRulesIfTrue(l), emptyRulesIfTrue(r), context);
31
+ const result = (0, _branchTs.branchIntersection)(emptyRulesIfTrue(l), emptyRulesIfTrue(r), context);
34
32
  return result === l ? l : result === r ? r : result;
35
33
  }
36
34
  const lBranches = (0, _genericsTs.listFrom)(emptyRulesIfTrue(l));
37
35
  const rBranches = (0, _genericsTs.listFrom)(emptyRulesIfTrue(r));
38
- const comparison = (0, _branchesTs.compareBranches)(lBranches, rBranches, context);
36
+ const comparison = (0, _branchTs.compareBranches)(lBranches, rBranches, context);
39
37
  if (comparison.equalities.length === lBranches.length && comparison.equalities.length === rBranches.length) {
40
38
  return (0, _composeTs.equality)();
41
39
  }
@@ -50,7 +48,7 @@ const comparePredicates = (l, r, context)=>{
50
48
  const predicateIntersection = (domain, l, r, state)=>{
51
49
  state.domain = domain;
52
50
  const comparison = comparePredicates(l, r, state);
53
- if (!(0, _branchesTs.isBranchComparison)(comparison)) {
51
+ if (!(0, _branchTs.isBranchComparison)(comparison)) {
54
52
  return comparison;
55
53
  }
56
54
  const resultBranches = [
@@ -67,7 +65,7 @@ const predicateIntersection = (domain, l, r, state)=>{
67
65
  const predicateUnion = (domain, l, r, type)=>{
68
66
  const state = new _composeTs.IntersectionState(type, "|");
69
67
  const comparison = comparePredicates(l, r, state);
70
- if (!(0, _branchesTs.isBranchComparison)(comparison)) {
68
+ if (!(0, _branchTs.isBranchComparison)(comparison)) {
71
69
  return (0, _composeTs.isEquality)(comparison) || comparison === l ? r : comparison === r ? l : // subtype of the other, it consists of two opposite literals
72
70
  // and can be simplified to a non-literal boolean.
73
71
  domain === "boolean" ? true : [
@@ -85,7 +83,7 @@ const flattenPredicate = (predicate, context)=>{
85
83
  if (predicate === true) {
86
84
  return [];
87
85
  }
88
- return (0, _objectKindsTs.isArray)(predicate) ? (0, _discriminateTs.flattenBranches)(predicate, context) : (0, _rulesTs.flattenBranch)(predicate, context);
86
+ return (0, _objectKindsTs.isArray)(predicate) ? (0, _discriminateTs.flattenBranches)(predicate, context) : (0, _branchTs.flattenBranch)(predicate, context);
89
87
  };
90
88
  const isLiteralCondition = (predicate)=>typeof predicate === "object" && "value" in predicate;
91
89
  const resolutionExtendsDomain = (resolution, domain)=>{
@@ -12,10 +12,16 @@ _export(exports, {
12
12
  classIntersection: ()=>classIntersection,
13
13
  checkClass: ()=>checkClass
14
14
  });
15
+ const _objectKindsTs = require("../../utils/objectKinds.js");
15
16
  const _composeTs = require("../compose.js");
16
- const classIntersection = (0, _composeTs.composeIntersection)((l, r, state)=>l === r ? (0, _composeTs.equality)() : l instanceof r ? l : r instanceof l ? r : state.addDisjoint("class", l, r));
17
- const checkClass = (data, expectedClass, state)=>{
18
- if (!(data instanceof expectedClass)) {
19
- state.problems.add("class", data, expectedClass);
17
+ const classIntersection = (0, _composeTs.composeIntersection)((l, r, state)=>{
18
+ const lClass = typeof l === "string" ? _objectKindsTs.defaultObjectKinds[l] : l;
19
+ const rClass = typeof r === "string" ? _objectKindsTs.defaultObjectKinds[r] : r;
20
+ return lClass === rClass ? (0, _composeTs.equality)() : lClass instanceof rClass ? l : rClass instanceof lClass ? r : state.addDisjoint("class", l, r);
21
+ });
22
+ const checkClass = (expectedClass, state)=>{
23
+ if (typeof expectedClass === "string") {
24
+ return (0, _objectKindsTs.objectKindOf)(state.data) === expectedClass || state.problems.add("class", state.data, expectedClass);
20
25
  }
26
+ return state.data instanceof expectedClass || state.problems.add("class", state.data, expectedClass);
21
27
  };
@@ -26,8 +26,4 @@ const greatestCommonDivisor = (l, r)=>{
26
26
  }
27
27
  return greatestCommonDivisor;
28
28
  };
29
- const checkDivisor = (data, divisor, state)=>{
30
- if (data % divisor !== 0) {
31
- state.problems.add("divisor", data, divisor);
32
- }
33
- };
29
+ const checkDivisor = (divisor, state)=>state.data % divisor === 0 || state.problems.add("divisor", state.data, divisor);
@@ -10,14 +10,59 @@ function _export(target, all) {
10
10
  }
11
11
  _export(exports, {
12
12
  isOptional: ()=>isOptional,
13
+ isPrerequisite: ()=>isPrerequisite,
14
+ mappedKeys: ()=>mappedKeys,
15
+ propToNode: ()=>propToNode,
13
16
  propsIntersection: ()=>propsIntersection,
14
17
  flattenProps: ()=>flattenProps
15
18
  });
19
+ const _genericsTs = require("../../utils/generics.js");
16
20
  const _composeTs = require("../compose.js");
17
21
  const _nodeTs = require("../node.js");
18
22
  const isOptional = (prop)=>prop[0] === "?";
19
- const nodeFrom = (prop)=>isOptional(prop) ? prop[1] : prop;
20
- const propsIntersection = (0, _composeTs.composeIntersection)((0, _composeTs.composeKeyedIntersection)((propKey, l, r, context)=>{
23
+ const isPrerequisite = (prop)=>prop[0] === "!";
24
+ const mappedKeys = {
25
+ index: "[index]"
26
+ };
27
+ const propToNode = (prop)=>isOptional(prop) || isPrerequisite(prop) ? prop[1] : prop;
28
+ const getTupleLengthIfPresent = (result)=>{
29
+ if (typeof result.length === "object" && isPrerequisite(result.length) && typeof result.length[1] !== "string" && (0, _nodeTs.isLiteralNode)(result.length[1], "number")) {
30
+ return result.length[1].number.value;
31
+ }
32
+ };
33
+ const propsIntersection = (0, _composeTs.composeIntersection)((l, r, state)=>{
34
+ const result = propKeysIntersection(l, r, state);
35
+ if (typeof result === "symbol") {
36
+ return result;
37
+ }
38
+ const lengthValue = getTupleLengthIfPresent(result);
39
+ if (lengthValue === undefined || !(0, _genericsTs.hasKey)(result, mappedKeys.index)) {
40
+ return result;
41
+ }
42
+ // if we are at this point, we have an array with an exact length (i.e.
43
+ // a tuple) and an index signature. Intersection each tuple item with
44
+ // the index signature node and remove the index signature via a new
45
+ // updated result, copied from result to avoid mutating existing references.
46
+ const { [mappedKeys.index]: indexProp , ...updatedResult } = result;
47
+ const indexNode = propToNode(indexProp);
48
+ for(let i = 0; i < lengthValue; i++){
49
+ if (!updatedResult[i]) {
50
+ updatedResult[i] = indexNode;
51
+ continue;
52
+ }
53
+ const existingNodeAtIndex = propToNode(updatedResult[i]);
54
+ state.path.push(`${i}`);
55
+ const updatedResultAtIndex = (0, _nodeTs.nodeIntersection)(existingNodeAtIndex, indexNode, state);
56
+ state.path.pop();
57
+ if ((0, _composeTs.isDisjoint)(updatedResultAtIndex)) {
58
+ return updatedResultAtIndex;
59
+ } else if (!(0, _composeTs.isEquality)(updatedResultAtIndex) && updatedResultAtIndex !== existingNodeAtIndex) {
60
+ updatedResult[i] = updatedResultAtIndex;
61
+ }
62
+ }
63
+ return updatedResult;
64
+ });
65
+ const propKeysIntersection = (0, _composeTs.composeKeyedIntersection)((propKey, l, r, context)=>{
21
66
  if (l === undefined) {
22
67
  return r === undefined ? (0, _composeTs.equality)() : r;
23
68
  }
@@ -25,7 +70,7 @@ const propsIntersection = (0, _composeTs.composeIntersection)((0, _composeTs.com
25
70
  return l;
26
71
  }
27
72
  context.path.push(propKey);
28
- const result = (0, _nodeTs.nodeIntersection)(nodeFrom(l), nodeFrom(r), context);
73
+ const result = (0, _nodeTs.nodeIntersection)(propToNode(l), propToNode(r), context);
29
74
  context.path.pop();
30
75
  const resultIsOptional = isOptional(l) && isOptional(r);
31
76
  if ((0, _composeTs.isDisjoint)(result) && resultIsOptional) {
@@ -37,36 +82,41 @@ const propsIntersection = (0, _composeTs.composeIntersection)((0, _composeTs.com
37
82
  return result;
38
83
  }, {
39
84
  onEmpty: "bubble"
40
- }));
85
+ });
41
86
  const flattenProps = (entries, props, ctx)=>{
42
- const requiredProps = [];
43
- const optionalProps = [];
44
87
  for(const k in props){
45
88
  const prop = props[k];
46
89
  ctx.path.push(k);
47
- if (isOptional(prop)) {
48
- optionalProps.push([
49
- k,
50
- (0, _nodeTs.flattenNode)(prop[1], ctx)
90
+ if (k === mappedKeys.index) {
91
+ entries.push([
92
+ "indexProp",
93
+ (0, _nodeTs.flattenNode)(propToNode(prop), ctx)
94
+ ]);
95
+ } else if (isOptional(prop)) {
96
+ entries.push([
97
+ "optionalProp",
98
+ [
99
+ k,
100
+ (0, _nodeTs.flattenNode)(prop[1], ctx)
101
+ ]
102
+ ]);
103
+ } else if (isPrerequisite(prop)) {
104
+ entries.push([
105
+ "prerequisiteProp",
106
+ [
107
+ k,
108
+ (0, _nodeTs.flattenNode)(prop[1], ctx)
109
+ ]
51
110
  ]);
52
111
  } else {
53
- requiredProps.push([
54
- k,
55
- (0, _nodeTs.flattenNode)(prop, ctx)
112
+ entries.push([
113
+ "requiredProp",
114
+ [
115
+ k,
116
+ (0, _nodeTs.flattenNode)(prop, ctx)
117
+ ]
56
118
  ]);
57
119
  }
58
120
  ctx.path.pop();
59
121
  }
60
- if (requiredProps.length) {
61
- entries.push([
62
- "requiredProps",
63
- requiredProps
64
- ]);
65
- }
66
- if (optionalProps.length) {
67
- entries.push([
68
- "optionalProps",
69
- optionalProps
70
- ]);
71
- }
72
122
  };
@@ -93,11 +93,7 @@ const flattenRange = (entries, range, ctx)=>{
93
93
  ]);
94
94
  }
95
95
  };
96
- const checkBound = (data, bound, state)=>{
97
- if (!comparatorCheckers[bound.comparator]((0, _sizeTs.sizeOf)(data), bound.limit)) {
98
- state.problems.add("bound", data, bound);
99
- }
100
- };
96
+ const checkBound = (bound, state)=>comparatorCheckers[bound.comparator]((0, _sizeTs.sizeOf)(state.data), bound.limit) || state.problems.add("bound", state.data, bound);
101
97
  const comparatorCheckers = {
102
98
  "<": (size, limit)=>size < limit,
103
99
  ">": (size, limit)=>size > limit,
@@ -22,9 +22,5 @@ const getRegex = (source)=>{
22
22
  }
23
23
  return regexCache[source];
24
24
  };
25
- const checkRegex = (data, source, state)=>{
26
- if (!getRegex(source).test(data)) {
27
- state.problems.add("regex", data, state.path.length === 0 && state.type.name !== "type" ? state.type.name : `/${source}/`);
28
- }
29
- };
25
+ const checkRegex = (source, state)=>getRegex(source).test(state.data) || state.problems.add("regex", state.data, `/${source}/`);
30
26
  const regexIntersection = (0, _composeTs.composeIntersection)(_collapsibleSetTs.collapsibleListUnion);
@@ -9,57 +9,23 @@ function _export(target, all) {
9
9
  });
10
10
  }
11
11
  _export(exports, {
12
- branchIntersection: ()=>branchIntersection,
13
12
  rulesIntersection: ()=>rulesIntersection,
14
13
  narrowableRulesIntersection: ()=>narrowableRulesIntersection,
14
+ flattenRules: ()=>flattenRules,
15
15
  precedenceMap: ()=>precedenceMap,
16
- flattenBranch: ()=>flattenBranch,
17
16
  literalSatisfiesRules: ()=>literalSatisfiesRules
18
17
  });
19
- const _astTs = require("../../parse/string/ast.js");
20
- const _errorsTs = require("../../utils/errors.js");
21
18
  const _genericsTs = require("../../utils/generics.js");
22
19
  const _composeTs = require("../compose.js");
23
- const _predicateTs = require("../predicate.js");
24
20
  const _classTs = require("./class.js");
25
21
  const _collapsibleSetTs = require("./collapsibleSet.js");
26
22
  const _divisorTs = require("./divisor.js");
27
- const _objectKindTs = require("./objectKind.js");
28
23
  const _propsTs = require("./props.js");
29
24
  const _rangeTs = require("./range.js");
30
25
  const _regexTs = require("./regex.js");
31
- const rulesOf = (branch)=>branch.rules ?? branch;
32
- const branchIntersection = (l, r, state)=>{
33
- const lRules = rulesOf(l);
34
- const rRules = rulesOf(r);
35
- const rulesResult = rulesIntersection(lRules, rRules, state);
36
- if ("morph" in l) {
37
- if ("morph" in r) {
38
- if (l.morph === r.morph) {
39
- return (0, _composeTs.isEquality)(rulesResult) || (0, _composeTs.isDisjoint)(rulesResult) ? rulesResult : {
40
- rules: rulesResult,
41
- morph: l.morph
42
- };
43
- }
44
- return state.lastOperator === "&" ? (0, _errorsTs.throwParseError)((0, _astTs.writeImplicitNeverMessage)(state.path, "Intersection", "of morphs")) : {};
45
- }
46
- return (0, _composeTs.isDisjoint)(rulesResult) ? rulesResult : {
47
- rules: (0, _composeTs.isEquality)(rulesResult) ? l.rules : rulesResult,
48
- morph: l.morph
49
- };
50
- }
51
- if ("morph" in r) {
52
- return (0, _composeTs.isDisjoint)(rulesResult) ? rulesResult : {
53
- rules: (0, _composeTs.isEquality)(rulesResult) ? r.rules : rulesResult,
54
- morph: r.morph
55
- };
56
- }
57
- return rulesResult;
58
- };
59
26
  const rulesIntersection = (l, r, state)=>"value" in l ? "value" in r ? l.value === r.value ? (0, _composeTs.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);
60
27
  const narrowIntersection = (0, _composeTs.composeIntersection)(_collapsibleSetTs.collapsibleListUnion);
61
28
  const narrowableRulesIntersection = (0, _composeTs.composeKeyedIntersection)({
62
- objectKind: _objectKindTs.objectKindIntersection,
63
29
  divisor: _divisorTs.divisorIntersection,
64
30
  regex: _regexTs.regexIntersection,
65
31
  props: _propsTs.propsIntersection,
@@ -69,8 +35,15 @@ const narrowableRulesIntersection = (0, _composeTs.composeKeyedIntersection)({
69
35
  }, {
70
36
  onEmpty: "bubble"
71
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
+ return entries.sort((l, r)=>precedenceMap[l[0]] - precedenceMap[r[0]]);
45
+ };
72
46
  const ruleFlatteners = {
73
- objectKind: _objectKindTs.flattenObjectKind,
74
47
  regex: (entries, rule)=>{
75
48
  for (const source of (0, _genericsTs.listFrom)(rule)){
76
49
  entries.push([
@@ -115,42 +88,25 @@ const precedenceMap = {
115
88
  value: 0,
116
89
  domains: 0,
117
90
  branches: 0,
118
- objectKind: 0,
119
91
  switch: 0,
120
92
  alias: 0,
93
+ class: 0,
121
94
  // Shallow: All shallow checks will be performed even if one or more fail
122
- class: 1,
123
95
  regex: 1,
124
96
  divisor: 1,
125
97
  bound: 1,
98
+ // Prerequisite: These are deep checks with special priority, e.g. the
99
+ // length of a tuple, which causes other deep props not to be checked if it
100
+ // is invalid
101
+ prerequisiteProp: 2,
126
102
  // Deep: Performed if all shallow checks pass, even if one or more deep checks fail
127
- requiredProps: 2,
128
- optionalProps: 2,
103
+ requiredProp: 3,
104
+ optionalProp: 3,
105
+ indexProp: 3,
129
106
  // Narrow: Only performed if all shallow and deep checks pass
130
- narrow: 3,
107
+ narrow: 4,
131
108
  // Morph: Only performed if all validation passes
132
- morph: 4
133
- };
134
- const flattenBranch = (branch, ctx)=>{
135
- if ((0, _predicateTs.branchIsTransformation)(branch)) {
136
- const result = flattenRules(branch.rules, ctx);
137
- if (branch.morph) {
138
- result.push([
139
- "morph",
140
- branch.morph
141
- ]);
142
- }
143
- return result;
144
- }
145
- return flattenRules(branch, ctx);
146
- };
147
- const flattenRules = (rules, ctx)=>{
148
- const entries = [];
149
- let k;
150
- for(k in rules){
151
- ruleFlatteners[k](entries, rules[k], ctx);
152
- }
153
- return entries.sort((l, r)=>precedenceMap[l[0]] - precedenceMap[r[0]]);
109
+ morph: 5
154
110
  };
155
111
  const literalSatisfiesRules = (data, rules, state)=>!state.type.meta.scope.type([
156
112
  "node",
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "DynamicState", {
6
6
  enumerable: true,
7
7
  get: ()=>DynamicState
8
8
  });
9
- const _functorsTs = require("../../../nodes/functors.js");
10
9
  const _nodeTs = require("../../../nodes/node.js");
11
10
  const _rangeTs = require("../../../nodes/rules/range.js");
12
11
  const _errorsTs = require("../../../utils/errors.js");
@@ -60,7 +59,7 @@ class DynamicState {
60
59
  this.root = node;
61
60
  }
62
61
  rootToArray() {
63
- this.root = _functorsTs.functors.Array(this.ejectRoot());
62
+ this.root = (0, _nodeTs.arrayOf)(this.ejectRoot());
64
63
  }
65
64
  intersect(node) {
66
65
  this.root = (0, _nodeTs.intersection)(this.ejectRoot(), node, this.ctx.type);
@@ -71,7 +71,7 @@ const distributeRange = (range, s)=>{
71
71
  if (resolution.object === true) {
72
72
  return false;
73
73
  }
74
- return (0, _genericsTs.listFrom)(resolution.object).every((branch)=>(0, _genericsTs.hasKey)(branch, "objectKind") && (branch.objectKind === "Array" || branch.objectKind[0] === "Array"));
74
+ return (0, _genericsTs.listFrom)(resolution.object).every((branch)=>(0, _genericsTs.hasKey)(branch, "class") && branch.class === "Array");
75
75
  default:
76
76
  return false;
77
77
  }
@@ -13,7 +13,7 @@ _export(exports, {
13
13
  maybeNaiveParse: ()=>maybeNaiveParse,
14
14
  fullStringParse: ()=>fullStringParse
15
15
  });
16
- const _functorsTs = require("../../nodes/functors.js");
16
+ const _nodeTs = require("../../nodes/node.js");
17
17
  const _dynamicTs = require("./reduce/dynamic.js");
18
18
  const _operandTs = require("./shift/operand/operand.js");
19
19
  const _operatorTs = require("./shift/operator/operator.js");
@@ -25,7 +25,7 @@ const maybeNaiveParse = (def, ctx)=>{
25
25
  if (def.endsWith("[]")) {
26
26
  const elementDef = def.slice(0, -2);
27
27
  if (ctx.type.meta.scope.addReferenceIfResolvable(def, ctx)) {
28
- return _functorsTs.functors.Array(elementDef);
28
+ return (0, _nodeTs.arrayOf)(elementDef);
29
29
  }
30
30
  }
31
31
  };