arktype 0.2.0 → 0.4.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 (138) hide show
  1. package/dist/api.cjs +9 -0
  2. package/dist/api.d.ts +10 -0
  3. package/dist/api.mjs +1 -0
  4. package/dist/cjs/nodes/{branches.js → branch.js} +65 -10
  5. package/dist/cjs/nodes/compose.js +2 -3
  6. package/dist/cjs/nodes/discriminate.js +9 -15
  7. package/dist/cjs/nodes/node.js +21 -11
  8. package/dist/cjs/nodes/predicate.js +8 -10
  9. package/dist/cjs/nodes/rules/class.js +10 -4
  10. package/dist/cjs/nodes/rules/divisor.js +1 -5
  11. package/dist/cjs/nodes/rules/props.js +75 -25
  12. package/dist/cjs/nodes/rules/range.js +2 -6
  13. package/dist/cjs/nodes/rules/regex.js +1 -5
  14. package/dist/cjs/nodes/rules/rules.js +23 -63
  15. package/dist/cjs/parse/ast/ast.js +4 -0
  16. package/dist/cjs/parse/ast/bound.js +9 -0
  17. package/dist/cjs/parse/{tuple/utils.js → ast/distributableFunction.js} +1 -1
  18. package/dist/cjs/parse/ast/divisor.js +9 -0
  19. package/dist/cjs/parse/{string/ast.js → ast/intersection.js} +2 -8
  20. package/dist/cjs/parse/ast/keyof.js +109 -0
  21. package/dist/cjs/parse/{tuple → ast}/morph.js +2 -2
  22. package/dist/cjs/parse/{tuple → ast}/narrow.js +2 -2
  23. package/dist/cjs/parse/{tuple → ast}/tuple.js +23 -16
  24. package/dist/cjs/parse/ast/union.js +9 -0
  25. package/dist/cjs/parse/ast/utils.js +4 -0
  26. package/dist/cjs/parse/definition.js +12 -2
  27. package/dist/cjs/parse/string/reduce/dynamic.js +7 -8
  28. package/dist/cjs/parse/string/shift/operator/bounds.js +4 -4
  29. package/dist/cjs/parse/string/shift/operator/divisor.js +3 -3
  30. package/dist/cjs/parse/string/string.js +2 -2
  31. package/dist/cjs/scopes/expressions.js +29 -0
  32. package/dist/cjs/scopes/jsObjects.js +14 -14
  33. package/dist/cjs/scopes/scope.js +56 -4
  34. package/dist/cjs/scopes/type.js +4 -7
  35. package/dist/cjs/traverse/problems.js +67 -53
  36. package/dist/cjs/traverse/traverse.js +103 -135
  37. package/dist/cjs/utils/data.js +39 -0
  38. package/dist/cjs/utils/generics.js +20 -2
  39. package/dist/cjs/utils/numericLiterals.js +2 -0
  40. package/dist/cjs/utils/objectKinds.js +1 -2
  41. package/dist/mjs/nodes/{branches.js → branch.js} +54 -2
  42. package/dist/mjs/nodes/compose.js +3 -4
  43. package/dist/mjs/nodes/discriminate.js +9 -15
  44. package/dist/mjs/nodes/node.js +17 -8
  45. package/dist/mjs/nodes/predicate.js +4 -5
  46. package/dist/mjs/nodes/rules/class.js +10 -4
  47. package/dist/mjs/nodes/rules/divisor.js +1 -5
  48. package/dist/mjs/nodes/rules/props.js +74 -27
  49. package/dist/mjs/nodes/rules/range.js +2 -6
  50. package/dist/mjs/nodes/rules/regex.js +1 -5
  51. package/dist/mjs/nodes/rules/rules.js +23 -62
  52. package/dist/mjs/parse/ast/ast.js +1 -0
  53. package/dist/mjs/parse/ast/bound.js +1 -0
  54. package/dist/mjs/parse/{tuple/utils.js → ast/distributableFunction.js} +2 -2
  55. package/dist/mjs/parse/ast/divisor.js +1 -0
  56. package/dist/mjs/parse/{string/ast.js → ast/intersection.js} +2 -5
  57. package/dist/mjs/parse/ast/keyof.js +101 -0
  58. package/dist/mjs/parse/{tuple → ast}/morph.js +2 -2
  59. package/dist/mjs/parse/ast/narrow.js +7 -0
  60. package/dist/mjs/parse/{tuple → ast}/tuple.js +24 -17
  61. package/dist/mjs/parse/ast/union.js +1 -0
  62. package/dist/mjs/parse/ast/utils.js +1 -0
  63. package/dist/mjs/parse/definition.js +12 -2
  64. package/dist/mjs/parse/string/reduce/dynamic.js +8 -9
  65. package/dist/mjs/parse/string/shift/operator/bounds.js +4 -4
  66. package/dist/mjs/parse/string/shift/operator/divisor.js +3 -3
  67. package/dist/mjs/parse/string/string.js +2 -2
  68. package/dist/mjs/scopes/expressions.js +9 -0
  69. package/dist/mjs/scopes/jsObjects.js +14 -14
  70. package/dist/mjs/scopes/scope.js +55 -3
  71. package/dist/mjs/scopes/type.js +4 -7
  72. package/dist/mjs/traverse/problems.js +68 -53
  73. package/dist/mjs/traverse/traverse.js +107 -139
  74. package/dist/mjs/utils/data.js +24 -0
  75. package/dist/mjs/utils/generics.js +18 -1
  76. package/dist/mjs/utils/numericLiterals.js +1 -0
  77. package/dist/mjs/utils/objectKinds.js +1 -1
  78. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  79. package/dist/types/nodes/branch.d.ts +27 -0
  80. package/dist/types/nodes/compose.d.ts +5 -10
  81. package/dist/types/nodes/discriminate.d.ts +3 -3
  82. package/dist/types/nodes/infer.d.ts +21 -20
  83. package/dist/types/nodes/node.d.ts +12 -16
  84. package/dist/types/nodes/predicate.d.ts +4 -59
  85. package/dist/types/nodes/rules/class.d.ts +3 -2
  86. package/dist/types/nodes/rules/divisor.d.ts +2 -1
  87. package/dist/types/nodes/rules/props.d.ts +19 -11
  88. package/dist/types/nodes/rules/range.d.ts +3 -2
  89. package/dist/types/nodes/rules/regex.d.ts +2 -1
  90. package/dist/types/nodes/rules/rules.d.ts +12 -14
  91. package/dist/types/parse/ast/ast.d.ts +24 -0
  92. package/dist/types/parse/ast/bound.d.ts +10 -0
  93. package/dist/types/parse/{tuple/utils.d.ts → ast/distributableFunction.d.ts} +2 -3
  94. package/dist/types/parse/ast/divisor.d.ts +8 -0
  95. package/dist/types/parse/ast/intersection.d.ts +17 -0
  96. package/dist/types/parse/{tuple → ast}/keyof.d.ts +4 -4
  97. package/dist/types/parse/{tuple → ast}/morph.d.ts +3 -3
  98. package/dist/types/parse/ast/narrow.d.ts +21 -0
  99. package/dist/types/parse/ast/tuple.d.ts +40 -0
  100. package/dist/types/parse/ast/union.d.ts +12 -0
  101. package/dist/types/parse/ast/utils.d.ts +7 -0
  102. package/dist/types/parse/definition.d.ts +9 -7
  103. package/dist/types/parse/record.d.ts +2 -2
  104. package/dist/types/parse/string/reduce/dynamic.d.ts +1 -1
  105. package/dist/types/parse/string/reduce/shared.d.ts +6 -5
  106. package/dist/types/parse/string/reduce/static.d.ts +11 -6
  107. package/dist/types/parse/string/shift/operand/unenclosed.d.ts +2 -2
  108. package/dist/types/parse/string/shift/operator/bounds.d.ts +4 -3
  109. package/dist/types/parse/string/shift/operator/divisor.d.ts +5 -1
  110. package/dist/types/parse/string/string.d.ts +2 -3
  111. package/dist/types/scopes/expressions.d.ts +48 -0
  112. package/dist/types/scopes/scope.d.ts +17 -7
  113. package/dist/types/scopes/type.d.ts +13 -15
  114. package/dist/types/traverse/problems.d.ts +17 -41
  115. package/dist/types/traverse/traverse.d.ts +85 -52
  116. package/dist/types/utils/data.d.ts +13 -0
  117. package/dist/types/utils/domains.d.ts +1 -1
  118. package/dist/types/utils/generics.d.ts +13 -10
  119. package/dist/types/utils/numericLiterals.d.ts +1 -0
  120. package/dist/types/utils/objectKinds.d.ts +43 -43
  121. package/dist/types/utils/paths.d.ts +2 -0
  122. package/package.json +1 -2
  123. package/dist/cjs/nodes/functors.js +0 -35
  124. package/dist/cjs/nodes/rules/objectKind.js +0 -78
  125. package/dist/cjs/parse/tuple/keyof.js +0 -45
  126. package/dist/cjs/utils/size.js +0 -16
  127. package/dist/mjs/nodes/functors.js +0 -27
  128. package/dist/mjs/nodes/rules/objectKind.js +0 -63
  129. package/dist/mjs/parse/tuple/keyof.js +0 -37
  130. package/dist/mjs/parse/tuple/narrow.js +0 -7
  131. package/dist/mjs/utils/size.js +0 -2
  132. package/dist/types/nodes/branches.d.ts +0 -14
  133. package/dist/types/nodes/functors.d.ts +0 -6
  134. package/dist/types/nodes/rules/objectKind.d.ts +0 -15
  135. package/dist/types/parse/string/ast.d.ts +0 -45
  136. package/dist/types/parse/tuple/narrow.d.ts +0 -13
  137. package/dist/types/parse/tuple/tuple.d.ts +0 -31
  138. package/dist/types/utils/size.d.ts +0 -3
@@ -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,19 @@ 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
+ // 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
+ };
72
50
  const ruleFlatteners = {
73
- objectKind: _objectKindTs.flattenObjectKind,
74
51
  regex: (entries, rule)=>{
75
52
  for (const source of (0, _genericsTs.listFrom)(rule)){
76
53
  entries.push([
@@ -115,42 +92,25 @@ const precedenceMap = {
115
92
  value: 0,
116
93
  domains: 0,
117
94
  branches: 0,
118
- objectKind: 0,
119
95
  switch: 0,
120
96
  alias: 0,
97
+ class: 0,
121
98
  // Shallow: All shallow checks will be performed even if one or more fail
122
- class: 1,
123
99
  regex: 1,
124
100
  divisor: 1,
125
101
  bound: 1,
102
+ // Prerequisite: These are deep checks with special priority, e.g. the
103
+ // length of a tuple, which causes other deep props not to be checked if it
104
+ // is invalid
105
+ prerequisiteProp: 2,
126
106
  // Deep: Performed if all shallow checks pass, even if one or more deep checks fail
127
- requiredProps: 2,
128
- optionalProps: 2,
107
+ requiredProp: 3,
108
+ optionalProp: 3,
109
+ indexProp: 3,
129
110
  // Narrow: Only performed if all shallow and deep checks pass
130
- narrow: 3,
111
+ narrow: 4,
131
112
  // 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]]);
113
+ morph: 5
154
114
  };
155
115
  const literalSatisfiesRules = (data, rules, state)=>!state.type.meta.scope.type([
156
116
  "node",
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "writeUnboundableMessage", {
6
+ enumerable: true,
7
+ get: ()=>writeUnboundableMessage
8
+ });
9
+ const writeUnboundableMessage = (root)=>`Bounded expression ${root} must be a number, string or array`;
@@ -18,7 +18,7 @@ const _genericsTs = require("../../utils/generics.js");
18
18
  const _serializeTs = require("../../utils/serialize.js");
19
19
  const writeMalformedDistributableFunctionMessage = (def)=>`Expected a Function or Record<Domain, Function> operand (${(0, _serializeTs.stringify)(def)} was invalid)`;
20
20
  const distributeFunctionToNode = (distributableFunction, inputNode, ctx, ruleKey)=>{
21
- const domains = (0, _genericsTs.keysOf)(ctx.type.meta.scope.resolveNode(inputNode));
21
+ const domains = (0, _genericsTs.objectKeysOf)(ctx.type.meta.scope.resolveNode(inputNode));
22
22
  if (!(0, _domainsTs.hasDomain)(distributableFunction, "object")) {
23
23
  return (0, _errorsTs.throwParseError)(writeMalformedDistributableFunctionMessage(distributableFunction));
24
24
  }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "writeIndivisibleMessage", {
6
+ enumerable: true,
7
+ get: ()=>writeIndivisibleMessage
8
+ });
9
+ const writeIndivisibleMessage = (root)=>`Divisibility operand ${root} must be a number`;
@@ -10,15 +10,12 @@ function _export(target, all) {
10
10
  }
11
11
  _export(exports, {
12
12
  compileDisjointReasonsMessage: ()=>compileDisjointReasonsMessage,
13
- writeImplicitNeverMessage: ()=>writeImplicitNeverMessage,
14
- writeUndiscriminatableMorphUnionMessage: ()=>writeUndiscriminatableMorphUnionMessage,
15
- writeIndivisibleMessage: ()=>writeIndivisibleMessage,
16
- writeUnboundableMessage: ()=>writeUnboundableMessage
13
+ writeImplicitNeverMessage: ()=>writeImplicitNeverMessage
17
14
  });
18
15
  const _composeTs = require("../../nodes/compose.js");
19
16
  const _genericsTs = require("../../utils/generics.js");
20
17
  const compileDisjointReasonsMessage = (disjoints)=>{
21
- const paths = (0, _genericsTs.keysOf)(disjoints);
18
+ const paths = (0, _genericsTs.objectKeysOf)(disjoints);
22
19
  if (paths.length === 1) {
23
20
  const path = paths[0];
24
21
  return `${path === "/" ? "" : `At ${path}: `}Intersection of ${_composeTs.disjointDescriptionWriters[disjoints[path].kind](disjoints[path])} results in an unsatisfiable type`;
@@ -31,6 +28,3 @@ const compileDisjointReasonsMessage = (disjoints)=>{
31
28
  return message;
32
29
  };
33
30
  const writeImplicitNeverMessage = (path, operator, description)=>`${path.length ? `At ${path}: ` : ""}${operator} ${description ? `${description} ` : ""}results in an unsatisfiable type`;
34
- const writeUndiscriminatableMorphUnionMessage = (path)=>`${path === "/" ? "A" : `At ${path}, a`} union including one or more morphs must be discriminatable`;
35
- const writeIndivisibleMessage = (root)=>`Divisibility operand ${root} must be a number`;
36
- const writeUnboundableMessage = (root)=>`Bounded expression ${root} must be a number, string or array`;
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "parseKeyOfTuple", {
6
+ enumerable: true,
7
+ get: ()=>parseKeyOfTuple
8
+ });
9
+ const _propsTs = require("../../nodes/rules/props.js");
10
+ const _errorsTs = require("../../utils/errors.js");
11
+ const _freezeTs = require("../../utils/freeze.js");
12
+ const _genericsTs = require("../../utils/generics.js");
13
+ const _numericLiteralsTs = require("../../utils/numericLiterals.js");
14
+ const _objectKindsTs = require("../../utils/objectKinds.js");
15
+ const _serializeTs = require("../../utils/serialize.js");
16
+ const _definitionTs = require("../definition.js");
17
+ const _intersectionTs = require("./intersection.js");
18
+ var _keyNode, _keyType, _keyNode1, _keyNode2;
19
+ const arrayIndexStringBranch = (0, _freezeTs.deepFreeze)({
20
+ regex: _numericLiteralsTs.wellFormedNonNegativeIntegerMatcher.source
21
+ });
22
+ const arrayIndexNumberBranch = (0, _freezeTs.deepFreeze)({
23
+ range: {
24
+ min: {
25
+ comparator: ">=",
26
+ limit: 1
27
+ }
28
+ },
29
+ divisor: 1
30
+ });
31
+ const parseKeyOfTuple = (def, ctx)=>{
32
+ const resolution = ctx.type.meta.scope.resolveNode((0, _definitionTs.parseDefinition)(def[1], ctx));
33
+ const predicateKeys = (0, _genericsTs.objectKeysOf)(resolution).map((domain)=>keysOfPredicate(domain, resolution[domain]));
34
+ const sharedKeys = sharedKeysOf(predicateKeys);
35
+ if (!sharedKeys.length) {
36
+ return (0, _intersectionTs.writeImplicitNeverMessage)(ctx.path, "keyof");
37
+ }
38
+ const keyNode = {};
39
+ for (const key of sharedKeys){
40
+ const keyType = typeof key;
41
+ if (keyType === "string" || keyType === "number" || keyType === "symbol") {
42
+ (_keyNode = keyNode)[_keyType = keyType] ?? (_keyNode[_keyType] = []);
43
+ keyNode[keyType].push({
44
+ value: key
45
+ });
46
+ } else if (key === _numericLiteralsTs.wellFormedNonNegativeIntegerMatcher) {
47
+ (_keyNode1 = keyNode).string ?? (_keyNode1.string = []);
48
+ keyNode.string.push(arrayIndexStringBranch);
49
+ (_keyNode2 = keyNode).number ?? (_keyNode2.number = []);
50
+ keyNode.number.push(arrayIndexNumberBranch);
51
+ } else {
52
+ return (0, _errorsTs.throwInternalError)(`Unexpected keyof key '${(0, _serializeTs.stringify)(key)}'`);
53
+ }
54
+ }
55
+ return Object.fromEntries(Object.entries(keyNode).map(([domain, branches])=>[
56
+ domain,
57
+ branches.length === 1 ? branches[0] : branches
58
+ ]));
59
+ };
60
+ const baseKeysByDomain = {
61
+ bigint: (0, _genericsTs.prototypeKeysOf)(0n),
62
+ boolean: (0, _genericsTs.prototypeKeysOf)(false),
63
+ null: [],
64
+ number: (0, _genericsTs.prototypeKeysOf)(0),
65
+ // TS doesn't include the Object prototype in keyof, so keyof object is never
66
+ object: [],
67
+ string: (0, _genericsTs.prototypeKeysOf)(""),
68
+ symbol: (0, _genericsTs.prototypeKeysOf)(Symbol()),
69
+ undefined: []
70
+ };
71
+ const keysOfPredicate = (domain, predicate)=>domain !== "object" || predicate === true ? baseKeysByDomain[domain] : sharedKeysOf((0, _genericsTs.listFrom)(predicate).map((branch)=>keysOfObjectBranch(branch)));
72
+ const sharedKeysOf = (keyBranches)=>{
73
+ if (!keyBranches.length) {
74
+ return [];
75
+ }
76
+ let sharedKeys = keyBranches[0];
77
+ for(let i = 1; i < keyBranches.length; i++){
78
+ // we can filter directly by equality here because the RegExp we're
79
+ // using will always be reference equal to
80
+ // wellFormedNonNegativeIntegerMatcher
81
+ sharedKeys = sharedKeys.filter((k)=>keyBranches[i].includes(k));
82
+ }
83
+ return sharedKeys;
84
+ };
85
+ const keysOfObjectBranch = (branch)=>{
86
+ const result = [];
87
+ if ("props" in branch) {
88
+ for (const key of Object.keys(branch.props)){
89
+ if (key === _propsTs.mappedKeys.index) {
90
+ result.push(_numericLiteralsTs.wellFormedNonNegativeIntegerMatcher);
91
+ } else if (!result.includes(key)) {
92
+ result.push(key);
93
+ if (_numericLiteralsTs.wellFormedNonNegativeIntegerMatcher.test(key)) {
94
+ // allow numeric access to keys
95
+ result.push((0, _numericLiteralsTs.tryParseWellFormedInteger)(key, `Unexpectedly failed to parse an integer from key '${key}'`));
96
+ }
97
+ }
98
+ }
99
+ }
100
+ if ("class" in branch) {
101
+ const constructor = typeof branch.class === "string" ? _objectKindsTs.defaultObjectKinds[branch.class] : branch.class;
102
+ for (const key1 of (0, _genericsTs.prototypeKeysOf)(constructor.prototype)){
103
+ if (!result.includes(key1)) {
104
+ result.push(key1);
105
+ }
106
+ }
107
+ }
108
+ return result;
109
+ };
@@ -12,7 +12,7 @@ _export(exports, {
12
12
  parseMorphTuple: ()=>parseMorphTuple,
13
13
  writeMalformedMorphExpressionMessage: ()=>writeMalformedMorphExpressionMessage
14
14
  });
15
- const _predicateTs = require("../../nodes/predicate.js");
15
+ const _branchTs = require("../../nodes/branch.js");
16
16
  const _errorsTs = require("../../utils/errors.js");
17
17
  const _objectKindsTs = require("../../utils/objectKinds.js");
18
18
  const _serializeTs = require("../../utils/serialize.js");
@@ -41,7 +41,7 @@ const parseMorphTuple = (def, ctx)=>{
41
41
  }
42
42
  return result;
43
43
  };
44
- const applyMorph = (branch, morph)=>(0, _predicateTs.branchIsTransformation)(branch) ? {
44
+ const applyMorph = (branch, morph)=>(0, _branchTs.isMorphBranch)(branch) ? {
45
45
  ...branch,
46
46
  morph: branch.morph ? Array.isArray(branch.morph) ? [
47
47
  ...branch.morph,
@@ -8,8 +8,8 @@ Object.defineProperty(exports, "parseNarrowTuple", {
8
8
  });
9
9
  const _nodeTs = require("../../nodes/node.js");
10
10
  const _definitionTs = require("../definition.js");
11
- const _utilsTs = require("./utils.js");
11
+ const _distributableFunctionTs = require("./distributableFunction.js");
12
12
  const parseNarrowTuple = (def, ctx)=>{
13
13
  const inputNode = (0, _definitionTs.parseDefinition)(def[0], ctx);
14
- return (0, _nodeTs.intersection)(inputNode, (0, _utilsTs.distributeFunctionToNode)(def[2], inputNode, ctx, "narrow"), ctx.type);
14
+ return (0, _nodeTs.rootIntersection)(inputNode, (0, _distributableFunctionTs.distributeFunctionToNode)(def[2], inputNode, ctx, "narrow"), ctx.type);
15
15
  };
@@ -12,7 +12,6 @@ _export(exports, {
12
12
  parseTuple: ()=>parseTuple,
13
13
  writeMalformedFunctionalExpressionMessage: ()=>writeMalformedFunctionalExpressionMessage
14
14
  });
15
- const _functorsTs = require("../../nodes/functors.js");
16
15
  const _nodeTs = require("../../nodes/node.js");
17
16
  const _domainsTs = require("../../utils/domains.js");
18
17
  const _errorsTs = require("../../utils/errors.js");
@@ -22,13 +21,25 @@ const _keyofTs = require("./keyof.js");
22
21
  const _morphTs = require("./morph.js");
23
22
  const _narrowTs = require("./narrow.js");
24
23
  const parseTuple = (def, ctx)=>{
25
- if (isPostfixExpression(def)) {
26
- return postfixParsers[def[1]](def, ctx);
24
+ if (isIndexOneExpression(def)) {
25
+ return indexOneParsers[def[1]](def, ctx);
27
26
  }
28
- if (isPrefixExpression(def)) {
27
+ if (isIndexZeroExpression(def)) {
29
28
  return prefixParsers[def[0]](def, ctx);
30
29
  }
31
- const props = {};
30
+ const props = {
31
+ // length is created as a prerequisite prop, ensuring if it is invalid,
32
+ // no other props will be checked, which is usually desirable for tuple
33
+ // definitions.
34
+ length: [
35
+ "!",
36
+ {
37
+ number: {
38
+ value: def.length
39
+ }
40
+ }
41
+ ]
42
+ };
32
43
  for(let i = 0; i < def.length; i++){
33
44
  ctx.path.push(`${i}`);
34
45
  props[i] = (0, _definitionTs.parseDefinition)(def[i], ctx);
@@ -36,12 +47,8 @@ const parseTuple = (def, ctx)=>{
36
47
  }
37
48
  return {
38
49
  object: {
39
- objectKind: "Array",
40
- props,
41
- range: {
42
- comparator: "==",
43
- limit: def.length
44
- }
50
+ class: "Array",
51
+ props
45
52
  }
46
53
  };
47
54
  };
@@ -51,12 +58,12 @@ const parseBranchTuple = (def, ctx)=>{
51
58
  }
52
59
  const l = (0, _definitionTs.parseDefinition)(def[0], ctx);
53
60
  const r = (0, _definitionTs.parseDefinition)(def[2], ctx);
54
- return def[1] === "&" ? (0, _nodeTs.intersection)(l, r, ctx.type) : (0, _nodeTs.union)(l, r, ctx.type);
61
+ return def[1] === "&" ? (0, _nodeTs.rootIntersection)(l, r, ctx.type) : (0, _nodeTs.rootUnion)(l, r, ctx.type);
55
62
  };
56
- const parseArrayTuple = (def, scope)=>_functorsTs.functors.Array((0, _definitionTs.parseDefinition)(def[0], scope));
63
+ const parseArrayTuple = (def, scope)=>(0, _nodeTs.toArrayNode)((0, _definitionTs.parseDefinition)(def[0], scope));
57
64
  const writeMalformedFunctionalExpressionMessage = (operator, rightDef)=>`Expression requires a function following '${operator}' (was ${typeof rightDef})`;
58
- const isPostfixExpression = (def)=>postfixParsers[def[1]] !== undefined;
59
- const postfixParsers = {
65
+ const isIndexOneExpression = (def)=>indexOneParsers[def[1]] !== undefined;
66
+ const indexOneParsers = {
60
67
  "|": parseBranchTuple,
61
68
  "&": parseBranchTuple,
62
69
  "[]": parseArrayTuple,
@@ -82,4 +89,4 @@ const prefixParsers = {
82
89
  }),
83
90
  node: (def)=>def[1]
84
91
  };
85
- const isPrefixExpression = (def)=>prefixParsers[def[0]] !== undefined;
92
+ const isIndexZeroExpression = (def)=>prefixParsers[def[0]] !== undefined;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "writeUndiscriminatableMorphUnionMessage", {
6
+ enumerable: true,
7
+ get: ()=>writeUndiscriminatableMorphUnionMessage
8
+ });
9
+ const writeUndiscriminatableMorphUnionMessage = (path)=>`${path === "/" ? "A" : `At ${path}, a`} union including one or more morphs must be discriminatable`;
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
@@ -19,9 +19,9 @@ const _domainsTs = require("../utils/domains.js");
19
19
  const _errorsTs = require("../utils/errors.js");
20
20
  const _objectKindsTs = require("../utils/objectKinds.js");
21
21
  const _serializeTs = require("../utils/serialize.js");
22
+ const _tupleTs = require("./ast/tuple.js");
22
23
  const _recordTs = require("./record.js");
23
24
  const _stringTs = require("./string/string.js");
24
- const _tupleTs = require("./tuple/tuple.js");
25
25
  const parseDefinition = (def, ctx)=>{
26
26
  const domain = (0, _domainsTs.domainOf)(def);
27
27
  if (domain === "string") {
@@ -43,11 +43,21 @@ const parseDefinition = (def, ctx)=>{
43
43
  }
44
44
  };
45
45
  case "Function":
46
- return (0, _typeTs.isType)(def) ? def.node : (0, _errorsTs.throwParseError)(writeBadDefinitionTypeMessage("Function"));
46
+ if ((0, _typeTs.isType)(def)) {
47
+ return def.node;
48
+ }
49
+ if (isThunk(def)) {
50
+ const returned = def();
51
+ if ((0, _typeTs.isType)(returned)) {
52
+ return returned.node;
53
+ }
54
+ }
55
+ return (0, _errorsTs.throwParseError)(writeBadDefinitionTypeMessage("Function"));
47
56
  default:
48
57
  return (0, _errorsTs.throwParseError)(writeBadDefinitionTypeMessage(objectKind ?? (0, _serializeTs.stringify)(def)));
49
58
  }
50
59
  };
51
60
  const as = Symbol("as");
52
61
  const unknownDefinitionMessage = "Cannot statically parse a definition inferred as unknown. Consider using 'as inferred<...>' to cast it.";
62
+ const isThunk = (def)=>typeof def === "function" && def.length === 0;
53
63
  const writeBadDefinitionTypeMessage = (actual)=>`Type definitions must be strings or objects (was ${actual})`;
@@ -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,10 +59,10 @@ 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.toArrayNode)(this.ejectRoot());
64
63
  }
65
64
  intersect(node) {
66
- this.root = (0, _nodeTs.intersection)(this.ejectRoot(), node, this.ctx.type);
65
+ this.root = (0, _nodeTs.rootIntersection)(this.ejectRoot(), node, this.ctx.type);
67
66
  }
68
67
  ejectRoot() {
69
68
  this.assertHasRoot();
@@ -90,7 +89,7 @@ class DynamicState {
90
89
  return this.error((0, _sharedTs.writeUnpairableComparatorMessage)(comparator));
91
90
  }
92
91
  if (this.branches.range) {
93
- return this.error((0, _sharedTs.writeMultipleLeftBoundsMessage)(this.branches.range.limit, this.branches.range.comparator, limit, invertedComparator));
92
+ return this.error((0, _sharedTs.writeMultipleLeftBoundsMessage)(`${this.branches.range.limit}`, this.branches.range.comparator, `${limit}`, invertedComparator));
94
93
  }
95
94
  this.branches.range = {
96
95
  limit,
@@ -103,7 +102,7 @@ class DynamicState {
103
102
  this.pushRootToBranch("|");
104
103
  this.setRoot(this.branches.union);
105
104
  } else if (this.branches.intersection) {
106
- this.setRoot((0, _nodeTs.intersection)(this.branches.intersection, this.ejectRoot(), this.ctx.type));
105
+ this.setRoot((0, _nodeTs.rootIntersection)(this.branches.intersection, this.ejectRoot(), this.ctx.type));
107
106
  }
108
107
  }
109
108
  finalizeGroup() {
@@ -116,15 +115,15 @@ class DynamicState {
116
115
  }
117
116
  pushRootToBranch(token) {
118
117
  this.assertRangeUnset();
119
- this.branches.intersection = this.branches.intersection ? (0, _nodeTs.intersection)(this.branches.intersection, this.ejectRoot(), this.ctx.type) : this.ejectRoot();
118
+ this.branches.intersection = this.branches.intersection ? (0, _nodeTs.rootIntersection)(this.branches.intersection, this.ejectRoot(), this.ctx.type) : this.ejectRoot();
120
119
  if (token === "|") {
121
- this.branches.union = this.branches.union ? (0, _nodeTs.union)(this.branches.union, this.branches.intersection, this.ctx.type) : this.branches.intersection;
120
+ this.branches.union = this.branches.union ? (0, _nodeTs.rootUnion)(this.branches.union, this.branches.intersection, this.ctx.type) : this.branches.intersection;
122
121
  delete this.branches.intersection;
123
122
  }
124
123
  }
125
124
  assertRangeUnset() {
126
125
  if (this.branches.range) {
127
- return this.error((0, _sharedTs.writeOpenRangeMessage)(this.branches.range.limit, this.branches.range.comparator));
126
+ return this.error((0, _sharedTs.writeOpenRangeMessage)(`${this.branches.range.limit}`, this.branches.range.comparator));
128
127
  }
129
128
  }
130
129
  reduceGroupOpen() {
@@ -20,7 +20,7 @@ const _rangeTs = require("../../../../nodes/rules/range.js");
20
20
  const _errorsTs = require("../../../../utils/errors.js");
21
21
  const _genericsTs = require("../../../../utils/generics.js");
22
22
  const _numericLiteralsTs = require("../../../../utils/numericLiterals.js");
23
- const _astTs = require("../../ast.js");
23
+ const _boundTs = require("../../../ast/bound.js");
24
24
  const _sharedTs = require("../../reduce/shared.js");
25
25
  const _scannerTs = require("../scanner.js");
26
26
  const parseBound = (s, start)=>{
@@ -53,7 +53,7 @@ const parseRightBound = (s, comparator)=>{
53
53
  };
54
54
  const distributeRange = (range, s)=>{
55
55
  const resolution = s.resolveRoot();
56
- const domains = (0, _genericsTs.keysOf)(resolution);
56
+ const domains = (0, _genericsTs.objectKeysOf)(resolution);
57
57
  const distributedRange = {};
58
58
  const rangePredicate = {
59
59
  range
@@ -71,13 +71,13 @@ 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
  }
78
78
  });
79
79
  if (!isBoundable) {
80
- s.error((0, _astTs.writeUnboundableMessage)(s.rootToString()));
80
+ s.error((0, _boundTs.writeUnboundableMessage)(s.rootToString()));
81
81
  }
82
82
  return distributedRange;
83
83
  };
@@ -14,14 +14,14 @@ _export(exports, {
14
14
  });
15
15
  const _genericsTs = require("../../../../utils/generics.js");
16
16
  const _numericLiteralsTs = require("../../../../utils/numericLiterals.js");
17
- const _astTs = require("../../ast.js");
17
+ const _divisorTs = require("../../../ast/divisor.js");
18
18
  const parseDivisor = (s)=>{
19
19
  const divisorToken = s.scanner.shiftUntilNextTerminator();
20
20
  const divisor = (0, _numericLiteralsTs.tryParseWellFormedInteger)(divisorToken, writeInvalidDivisorMessage(divisorToken));
21
21
  if (divisor === 0) {
22
22
  s.error(writeInvalidDivisorMessage(0));
23
23
  }
24
- const rootDomains = (0, _genericsTs.keysOf)(s.resolveRoot());
24
+ const rootDomains = (0, _genericsTs.objectKeysOf)(s.resolveRoot());
25
25
  if (rootDomains.length === 1 && rootDomains[0] === "number") {
26
26
  s.intersect({
27
27
  number: {
@@ -29,7 +29,7 @@ const parseDivisor = (s)=>{
29
29
  }
30
30
  });
31
31
  } else {
32
- s.error((0, _astTs.writeIndivisibleMessage)(s.rootToString()));
32
+ s.error((0, _divisorTs.writeIndivisibleMessage)(s.rootToString()));
33
33
  }
34
34
  };
35
35
  const writeInvalidDivisorMessage = (divisor)=>`% operator must be followed by a non-zero integer literal (was ${divisor})`;
@@ -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.toArrayNode)(elementDef);
29
29
  }
30
30
  }
31
31
  };
@@ -0,0 +1,29 @@
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
+ intersection: ()=>intersection,
13
+ union: ()=>union,
14
+ arrayOf: ()=>arrayOf,
15
+ keyOf: ()=>keyOf,
16
+ instanceOf: ()=>instanceOf,
17
+ valueOf: ()=>valueOf,
18
+ narrow: ()=>narrow,
19
+ morph: ()=>morph
20
+ });
21
+ const _standardTs = require("./standard.js");
22
+ const intersection = _standardTs.scopes.standard.intersection;
23
+ const union = _standardTs.scopes.standard.union;
24
+ const arrayOf = _standardTs.scopes.standard.arrayOf;
25
+ const keyOf = _standardTs.scopes.standard.keyOf;
26
+ const instanceOf = _standardTs.scopes.standard.instanceOf;
27
+ const valueOf = _standardTs.scopes.standard.valueOf;
28
+ const narrow = _standardTs.scopes.standard.narrow;
29
+ const morph = _standardTs.scopes.standard.morph;