arktype 0.3.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 (112) 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/branch.js +15 -6
  5. package/dist/cjs/nodes/compose.js +1 -1
  6. package/dist/cjs/nodes/discriminate.js +3 -3
  7. package/dist/cjs/nodes/node.js +11 -11
  8. package/dist/cjs/nodes/predicate.js +1 -1
  9. package/dist/cjs/nodes/rules/class.js +2 -2
  10. package/dist/cjs/nodes/rules/divisor.js +1 -1
  11. package/dist/cjs/nodes/rules/range.js +2 -2
  12. package/dist/cjs/nodes/rules/regex.js +1 -1
  13. package/dist/cjs/nodes/rules/rules.js +4 -0
  14. package/dist/cjs/parse/ast/ast.js +4 -0
  15. package/dist/cjs/parse/ast/bound.js +9 -0
  16. package/dist/cjs/parse/{tuple/utils.js → ast/distributableFunction.js} +1 -1
  17. package/dist/cjs/parse/ast/divisor.js +9 -0
  18. package/dist/cjs/parse/{string/ast.js → ast/intersection.js} +2 -8
  19. package/dist/cjs/parse/ast/keyof.js +109 -0
  20. package/dist/cjs/parse/{tuple → ast}/morph.js +0 -0
  21. package/dist/cjs/parse/{tuple → ast}/narrow.js +2 -2
  22. package/dist/cjs/parse/{tuple → ast}/tuple.js +8 -8
  23. package/dist/cjs/parse/ast/union.js +9 -0
  24. package/dist/cjs/parse/ast/utils.js +4 -0
  25. package/dist/cjs/parse/definition.js +12 -2
  26. package/dist/cjs/parse/string/reduce/dynamic.js +7 -7
  27. package/dist/cjs/parse/string/shift/operator/bounds.js +3 -3
  28. package/dist/cjs/parse/string/shift/operator/divisor.js +3 -3
  29. package/dist/cjs/parse/string/string.js +1 -1
  30. package/dist/cjs/scopes/expressions.js +29 -0
  31. package/dist/cjs/scopes/scope.js +56 -4
  32. package/dist/cjs/traverse/problems.js +59 -29
  33. package/dist/cjs/traverse/traverse.js +24 -27
  34. package/dist/cjs/utils/data.js +39 -0
  35. package/dist/cjs/utils/generics.js +20 -2
  36. package/dist/mjs/nodes/branch.js +14 -5
  37. package/dist/mjs/nodes/compose.js +2 -2
  38. package/dist/mjs/nodes/discriminate.js +3 -3
  39. package/dist/mjs/nodes/node.js +8 -8
  40. package/dist/mjs/nodes/predicate.js +2 -2
  41. package/dist/mjs/nodes/rules/class.js +2 -2
  42. package/dist/mjs/nodes/rules/divisor.js +1 -1
  43. package/dist/mjs/nodes/rules/range.js +2 -2
  44. package/dist/mjs/nodes/rules/regex.js +1 -1
  45. package/dist/mjs/nodes/rules/rules.js +4 -0
  46. package/dist/mjs/parse/ast/ast.js +1 -0
  47. package/dist/mjs/parse/ast/bound.js +1 -0
  48. package/dist/mjs/parse/{tuple/utils.js → ast/distributableFunction.js} +2 -2
  49. package/dist/mjs/parse/ast/divisor.js +1 -0
  50. package/dist/mjs/parse/{string/ast.js → ast/intersection.js} +2 -5
  51. package/dist/mjs/parse/ast/keyof.js +101 -0
  52. package/dist/mjs/parse/{tuple → ast}/morph.js +0 -0
  53. package/dist/mjs/parse/ast/narrow.js +7 -0
  54. package/dist/mjs/parse/{tuple → ast}/tuple.js +9 -9
  55. package/dist/mjs/parse/ast/union.js +1 -0
  56. package/dist/mjs/parse/ast/utils.js +1 -0
  57. package/dist/mjs/parse/definition.js +12 -2
  58. package/dist/mjs/parse/string/reduce/dynamic.js +8 -8
  59. package/dist/mjs/parse/string/shift/operator/bounds.js +3 -3
  60. package/dist/mjs/parse/string/shift/operator/divisor.js +3 -3
  61. package/dist/mjs/parse/string/string.js +2 -2
  62. package/dist/mjs/scopes/expressions.js +9 -0
  63. package/dist/mjs/scopes/scope.js +55 -3
  64. package/dist/mjs/traverse/problems.js +59 -28
  65. package/dist/mjs/traverse/traverse.js +26 -30
  66. package/dist/mjs/utils/data.js +24 -0
  67. package/dist/mjs/utils/generics.js +18 -1
  68. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  69. package/dist/types/nodes/branch.d.ts +3 -12
  70. package/dist/types/nodes/discriminate.d.ts +2 -2
  71. package/dist/types/nodes/infer.d.ts +5 -5
  72. package/dist/types/nodes/node.d.ts +4 -13
  73. package/dist/types/nodes/predicate.d.ts +2 -29
  74. package/dist/types/nodes/rules/rules.d.ts +1 -1
  75. package/dist/types/parse/ast/ast.d.ts +24 -0
  76. package/dist/types/parse/ast/bound.d.ts +10 -0
  77. package/dist/types/parse/{tuple/utils.d.ts → ast/distributableFunction.d.ts} +2 -3
  78. package/dist/types/parse/ast/divisor.d.ts +8 -0
  79. package/dist/types/parse/ast/intersection.d.ts +17 -0
  80. package/dist/types/parse/{tuple → ast}/keyof.d.ts +4 -4
  81. package/dist/types/parse/{tuple → ast}/morph.d.ts +3 -3
  82. package/dist/types/parse/ast/narrow.d.ts +21 -0
  83. package/dist/types/parse/ast/tuple.d.ts +40 -0
  84. package/dist/types/parse/ast/union.d.ts +12 -0
  85. package/dist/types/parse/ast/utils.d.ts +7 -0
  86. package/dist/types/parse/definition.d.ts +9 -7
  87. package/dist/types/parse/record.d.ts +2 -2
  88. package/dist/types/parse/string/reduce/shared.d.ts +5 -4
  89. package/dist/types/parse/string/reduce/static.d.ts +11 -6
  90. package/dist/types/parse/string/shift/operand/unenclosed.d.ts +1 -1
  91. package/dist/types/parse/string/shift/operator/bounds.d.ts +3 -2
  92. package/dist/types/parse/string/shift/operator/divisor.d.ts +5 -1
  93. package/dist/types/parse/string/string.d.ts +1 -2
  94. package/dist/types/scopes/expressions.d.ts +48 -0
  95. package/dist/types/scopes/scope.d.ts +17 -7
  96. package/dist/types/scopes/type.d.ts +9 -5
  97. package/dist/types/traverse/problems.d.ts +12 -21
  98. package/dist/types/traverse/traverse.d.ts +66 -37
  99. package/dist/types/utils/data.d.ts +13 -0
  100. package/dist/types/utils/domains.d.ts +1 -1
  101. package/dist/types/utils/generics.d.ts +13 -9
  102. package/dist/types/utils/paths.d.ts +2 -0
  103. package/package.json +1 -1
  104. package/dist/cjs/parse/tuple/keyof.js +0 -64
  105. package/dist/cjs/utils/size.js +0 -16
  106. package/dist/mjs/parse/tuple/keyof.js +0 -56
  107. package/dist/mjs/parse/tuple/narrow.js +0 -7
  108. package/dist/mjs/utils/size.js +0 -2
  109. package/dist/types/parse/string/ast.d.ts +0 -46
  110. package/dist/types/parse/tuple/narrow.d.ts +0 -13
  111. package/dist/types/parse/tuple/tuple.d.ts +0 -31
  112. package/dist/types/utils/size.d.ts +0 -3
package/dist/api.cjs CHANGED
@@ -11,9 +11,18 @@ function _export(target, all) {
11
11
  _export(exports, {
12
12
  scope: ()=>_scopeTs.scope,
13
13
  type: ()=>_standardTs.type,
14
+ intersection: ()=>_expressionsTs.intersection,
15
+ union: ()=>_expressionsTs.union,
16
+ arrayOf: ()=>_expressionsTs.arrayOf,
17
+ instanceOf: ()=>_expressionsTs.instanceOf,
18
+ valueOf: ()=>_expressionsTs.valueOf,
19
+ morph: ()=>_expressionsTs.morph,
20
+ narrow: ()=>_expressionsTs.narrow,
21
+ keyOf: ()=>_expressionsTs.keyOf,
14
22
  Problems: ()=>_problemsTs.Problems,
15
23
  Problem: ()=>_problemsTs.Problem
16
24
  });
17
25
  const _scopeTs = require("./cjs/scopes/scope.js");
18
26
  const _standardTs = require("./cjs/scopes/standard.js");
27
+ const _expressionsTs = require("./cjs/scopes/expressions.js");
19
28
  const _problemsTs = require("./cjs/traverse/problems.js");
package/dist/api.d.ts CHANGED
@@ -1,4 +1,14 @@
1
1
  export { scope } from "./types/scopes/scope.js"
2
2
  export { type } from "./types/scopes/standard.js"
3
+ export {
4
+ intersection,
5
+ union,
6
+ arrayOf,
7
+ instanceOf,
8
+ valueOf,
9
+ morph,
10
+ narrow,
11
+ keyOf
12
+ } from "./types/scopes/expressions.js"
3
13
  export type { Type } from "./types/scopes/type.js"
4
14
  export { Problems, Problem } from "./types/traverse/problems.js"
package/dist/api.mjs CHANGED
@@ -1,3 +1,4 @@
1
1
  export { scope } from "./mjs/scopes/scope.js";
2
2
  export { type } from "./mjs/scopes/standard.js";
3
+ export { intersection, union, arrayOf, instanceOf, valueOf, morph, narrow, keyOf } from "./mjs/scopes/expressions.js";
3
4
  export { Problems, Problem } from "./mjs/traverse/problems.js";
@@ -15,7 +15,7 @@ _export(exports, {
15
15
  flattenBranch: ()=>flattenBranch,
16
16
  branchIntersection: ()=>branchIntersection
17
17
  });
18
- const _astJs = require("../parse/string/ast.js");
18
+ const _intersectionJs = require("../parse/ast/intersection.js");
19
19
  const _domainsJs = require("../utils/domains.js");
20
20
  const _errorsJs = require("../utils/errors.js");
21
21
  const _composeJs = require("./compose.js");
@@ -92,10 +92,19 @@ const flattenBranch = (branch, ctx)=>{
92
92
  if (isMorphBranch(branch)) {
93
93
  const result = (0, _rulesJs.flattenRules)(branch.rules, ctx);
94
94
  if (branch.morph) {
95
- result.push([
96
- "morph",
97
- branch.morph
98
- ]);
95
+ if (typeof branch.morph === "function") {
96
+ result.push([
97
+ "morph",
98
+ branch.morph
99
+ ]);
100
+ } else {
101
+ for (const morph of branch.morph){
102
+ result.push([
103
+ "morph",
104
+ morph
105
+ ]);
106
+ }
107
+ }
99
108
  }
100
109
  return result;
101
110
  }
@@ -114,7 +123,7 @@ const branchIntersection = (l, r, state)=>{
114
123
  morph: l.morph
115
124
  };
116
125
  }
117
- return state.lastOperator === "&" ? (0, _errorsJs.throwParseError)((0, _astJs.writeImplicitNeverMessage)(state.path, "Intersection", "of morphs")) : {};
126
+ return state.lastOperator === "&" ? (0, _errorsJs.throwParseError)((0, _intersectionJs.writeImplicitNeverMessage)(state.path, "Intersection", "of morphs")) : {};
118
127
  }
119
128
  return (0, _composeJs.isDisjoint)(rulesResult) ? rulesResult : {
120
129
  rules: (0, _composeJs.isEquality)(rulesResult) ? l.rules : rulesResult,
@@ -109,7 +109,7 @@ const equality = ()=>equal;
109
109
  const isEquality = (result)=>result === equal;
110
110
  const composeKeyedIntersection = (reducer, config)=>(l, r, state)=>{
111
111
  const result = {};
112
- const keys = (0, _genericsTs.keysOf)({
112
+ const keys = (0, _genericsTs.objectKeysOf)({
113
113
  ...l,
114
114
  ...r
115
115
  });
@@ -13,7 +13,7 @@ _export(exports, {
13
13
  serializeIfAllowed: ()=>serializeIfAllowed,
14
14
  serializeCase: ()=>serializeCase
15
15
  });
16
- const _astTs = require("../parse/string/ast.js");
16
+ const _unionTs = require("../parse/ast/union.js");
17
17
  const _domainsTs = require("../utils/domains.js");
18
18
  const _errorsTs = require("../utils/errors.js");
19
19
  const _genericsTs = require("../utils/generics.js");
@@ -37,7 +37,7 @@ const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
37
37
  return [
38
38
  [
39
39
  "branches",
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))
40
+ remainingIndices.map((i)=>branchIncludesMorph(originalBranches[i], ctx.type.meta.scope) ? (0, _errorsTs.throwParseError)((0, _unionTs.writeUndiscriminatableMorphUnionMessage)(`${ctx.path}`)) : (0, _branchTs.flattenBranch)(originalBranches[i], ctx))
41
41
  ]
42
42
  ];
43
43
  }
@@ -157,7 +157,7 @@ const findBestDiscriminant = (remainingIndices, discriminants)=>{
157
157
  filteredCases[caseKey] = filteredIndices;
158
158
  score++;
159
159
  }
160
- const defaultCaseKeys = (0, _genericsTs.keysOf)(defaultCases);
160
+ const defaultCaseKeys = (0, _genericsTs.objectKeysOf)(defaultCases);
161
161
  if (defaultCaseKeys.length) {
162
162
  filteredCases["default"] = defaultCaseKeys.map((k)=>parseInt(k));
163
163
  }
@@ -10,14 +10,14 @@ function _export(target, all) {
10
10
  }
11
11
  _export(exports, {
12
12
  nodeIntersection: ()=>nodeIntersection,
13
- intersection: ()=>intersection,
14
- union: ()=>union,
13
+ rootIntersection: ()=>rootIntersection,
14
+ rootUnion: ()=>rootUnion,
15
15
  flattenType: ()=>flattenType,
16
16
  flattenNode: ()=>flattenNode,
17
17
  isLiteralNode: ()=>isLiteralNode,
18
- arrayOf: ()=>arrayOf
18
+ toArrayNode: ()=>toArrayNode
19
19
  });
20
- const _astTs = require("../parse/string/ast.js");
20
+ const _intersectionTs = require("../parse/ast/intersection.js");
21
21
  const _errorsTs = require("../utils/errors.js");
22
22
  const _genericsTs = require("../utils/generics.js");
23
23
  const _pathsTs = require("../utils/paths.js");
@@ -30,7 +30,7 @@ const nodeIntersection = (l, r, state)=>{
30
30
  const rResolution = state.type.meta.scope.resolveNode(r);
31
31
  const result = resolutionIntersection(lResolution, rResolution, state);
32
32
  if (typeof result === "object" && !(0, _genericsTs.hasKeys)(result)) {
33
- return (0, _genericsTs.hasKeys)(state.disjoints) ? (0, _composeTs.anonymousDisjoint)() : state.addDisjoint("domain", (0, _genericsTs.keysOf)(lResolution), (0, _genericsTs.keysOf)(rResolution));
33
+ return (0, _genericsTs.hasKeys)(state.disjoints) ? (0, _composeTs.anonymousDisjoint)() : state.addDisjoint("domain", (0, _genericsTs.objectKeysOf)(lResolution), (0, _genericsTs.objectKeysOf)(rResolution));
34
34
  }
35
35
  return result === lResolution ? l : result === rResolution ? r : result;
36
36
  };
@@ -45,16 +45,16 @@ const resolutionIntersection = (0, _composeTs.composeKeyedIntersection)((domain,
45
45
  }, {
46
46
  onEmpty: "omit"
47
47
  });
48
- const intersection = (l, r, type)=>{
48
+ const rootIntersection = (l, r, type)=>{
49
49
  const state = new _composeTs.IntersectionState(type, "&");
50
50
  const result = nodeIntersection(l, r, state);
51
- return (0, _composeTs.isDisjoint)(result) ? (0, _errorsTs.throwParseError)((0, _astTs.compileDisjointReasonsMessage)(state.disjoints)) : (0, _composeTs.isEquality)(result) ? l : result;
51
+ return (0, _composeTs.isDisjoint)(result) ? (0, _errorsTs.throwParseError)((0, _intersectionTs.compileDisjointReasonsMessage)(state.disjoints)) : (0, _composeTs.isEquality)(result) ? l : result;
52
52
  };
53
- const union = (l, r, type)=>{
53
+ const rootUnion = (l, r, type)=>{
54
54
  const lResolution = type.meta.scope.resolveNode(l);
55
55
  const rResolution = type.meta.scope.resolveNode(r);
56
56
  const result = {};
57
- const domains = (0, _genericsTs.keysOf)({
57
+ const domains = (0, _genericsTs.objectKeysOf)({
58
58
  ...lResolution,
59
59
  ...rResolution
60
60
  });
@@ -94,7 +94,7 @@ const flattenNode = (node, ctx)=>{
94
94
  ]
95
95
  ] : resolution.flat;
96
96
  }
97
- const domains = (0, _genericsTs.keysOf)(node);
97
+ const domains = (0, _genericsTs.objectKeysOf)(node);
98
98
  if (domains.length === 1) {
99
99
  const domain = domains[0];
100
100
  const predicate = node[domain];
@@ -126,7 +126,7 @@ const flattenNode = (node, ctx)=>{
126
126
  const isLiteralNode = (node, domain)=>{
127
127
  return (0, _predicateTs.resolutionExtendsDomain)(node, domain) && (0, _predicateTs.isLiteralCondition)(node[domain]);
128
128
  };
129
- const arrayOf = (node)=>({
129
+ const toArrayNode = (node)=>({
130
130
  object: {
131
131
  class: "Array",
132
132
  props: {
@@ -87,6 +87,6 @@ const flattenPredicate = (predicate, context)=>{
87
87
  };
88
88
  const isLiteralCondition = (predicate)=>typeof predicate === "object" && "value" in predicate;
89
89
  const resolutionExtendsDomain = (resolution, domain)=>{
90
- const domains = (0, _genericsTs.keysOf)(resolution);
90
+ const domains = (0, _genericsTs.objectKeysOf)(resolution);
91
91
  return domains.length === 1 && domains[0] === domain;
92
92
  };
@@ -21,7 +21,7 @@ const classIntersection = (0, _composeTs.composeIntersection)((l, r, state)=>{
21
21
  });
22
22
  const checkClass = (expectedClass, state)=>{
23
23
  if (typeof expectedClass === "string") {
24
- return (0, _objectKindsTs.objectKindOf)(state.data) === expectedClass || state.problems.add("class", state.data, expectedClass);
24
+ return (0, _objectKindsTs.objectKindOf)(state.data) === expectedClass || state.problems.create("class", expectedClass);
25
25
  }
26
- return state.data instanceof expectedClass || state.problems.add("class", state.data, expectedClass);
26
+ return state.data instanceof expectedClass || state.problems.create("class", expectedClass);
27
27
  };
@@ -26,4 +26,4 @@ const greatestCommonDivisor = (l, r)=>{
26
26
  }
27
27
  return greatestCommonDivisor;
28
28
  };
29
- const checkDivisor = (divisor, state)=>state.data % divisor === 0 || state.problems.add("divisor", state.data, divisor);
29
+ const checkDivisor = (divisor, state)=>state.data % divisor === 0 || state.problems.create("divisor", divisor);
@@ -17,7 +17,7 @@ _export(exports, {
17
17
  checkBound: ()=>checkBound,
18
18
  compareStrictness: ()=>compareStrictness
19
19
  });
20
- const _sizeTs = require("../../utils/size.js");
20
+ const _dataTs = require("../../utils/data.js");
21
21
  const _composeTs = require("../compose.js");
22
22
  const minComparators = {
23
23
  ">": true,
@@ -93,7 +93,7 @@ const flattenRange = (entries, range, ctx)=>{
93
93
  ]);
94
94
  }
95
95
  };
96
- const checkBound = (bound, state)=>comparatorCheckers[bound.comparator]((0, _sizeTs.sizeOf)(state.data), bound.limit) || state.problems.add("bound", state.data, bound);
96
+ const checkBound = (bound, state)=>comparatorCheckers[bound.comparator]((0, _dataTs.sizeOf)(state.data), bound.limit) || state.problems.create("bound", bound);
97
97
  const comparatorCheckers = {
98
98
  "<": (size, limit)=>size < limit,
99
99
  ">": (size, limit)=>size > limit,
@@ -22,5 +22,5 @@ const getRegex = (source)=>{
22
22
  }
23
23
  return regexCache[source];
24
24
  };
25
- const checkRegex = (source, state)=>getRegex(source).test(state.data) || state.problems.add("regex", state.data, `/${source}/`);
25
+ const checkRegex = (source, state)=>getRegex(source).test(state.data) || state.problems.create("regex", `/${source}/`);
26
26
  const regexIntersection = (0, _composeTs.composeIntersection)(_collapsibleSetTs.collapsibleListUnion);
@@ -41,6 +41,10 @@ const flattenRules = (rules, ctx)=>{
41
41
  for(k in rules){
42
42
  ruleFlatteners[k](entries, rules[k], ctx);
43
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
44
48
  return entries.sort((l, r)=>precedenceMap[l[0]] - precedenceMap[r[0]]);
45
49
  };
46
50
  const ruleFlatteners = {
@@ -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
+ };
File without changes
@@ -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
  };
@@ -21,10 +21,10 @@ const _keyofTs = require("./keyof.js");
21
21
  const _morphTs = require("./morph.js");
22
22
  const _narrowTs = require("./narrow.js");
23
23
  const parseTuple = (def, ctx)=>{
24
- if (isPostfixExpression(def)) {
25
- return postfixParsers[def[1]](def, ctx);
24
+ if (isIndexOneExpression(def)) {
25
+ return indexOneParsers[def[1]](def, ctx);
26
26
  }
27
- if (isPrefixExpression(def)) {
27
+ if (isIndexZeroExpression(def)) {
28
28
  return prefixParsers[def[0]](def, ctx);
29
29
  }
30
30
  const props = {
@@ -58,12 +58,12 @@ const parseBranchTuple = (def, ctx)=>{
58
58
  }
59
59
  const l = (0, _definitionTs.parseDefinition)(def[0], ctx);
60
60
  const r = (0, _definitionTs.parseDefinition)(def[2], ctx);
61
- 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);
62
62
  };
63
- const parseArrayTuple = (def, scope)=>(0, _nodeTs.arrayOf)((0, _definitionTs.parseDefinition)(def[0], scope));
63
+ const parseArrayTuple = (def, scope)=>(0, _nodeTs.toArrayNode)((0, _definitionTs.parseDefinition)(def[0], scope));
64
64
  const writeMalformedFunctionalExpressionMessage = (operator, rightDef)=>`Expression requires a function following '${operator}' (was ${typeof rightDef})`;
65
- const isPostfixExpression = (def)=>postfixParsers[def[1]] !== undefined;
66
- const postfixParsers = {
65
+ const isIndexOneExpression = (def)=>indexOneParsers[def[1]] !== undefined;
66
+ const indexOneParsers = {
67
67
  "|": parseBranchTuple,
68
68
  "&": parseBranchTuple,
69
69
  "[]": parseArrayTuple,
@@ -89,4 +89,4 @@ const prefixParsers = {
89
89
  }),
90
90
  node: (def)=>def[1]
91
91
  };
92
- 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})`;
@@ -59,10 +59,10 @@ class DynamicState {
59
59
  this.root = node;
60
60
  }
61
61
  rootToArray() {
62
- this.root = (0, _nodeTs.arrayOf)(this.ejectRoot());
62
+ this.root = (0, _nodeTs.toArrayNode)(this.ejectRoot());
63
63
  }
64
64
  intersect(node) {
65
- this.root = (0, _nodeTs.intersection)(this.ejectRoot(), node, this.ctx.type);
65
+ this.root = (0, _nodeTs.rootIntersection)(this.ejectRoot(), node, this.ctx.type);
66
66
  }
67
67
  ejectRoot() {
68
68
  this.assertHasRoot();
@@ -89,7 +89,7 @@ class DynamicState {
89
89
  return this.error((0, _sharedTs.writeUnpairableComparatorMessage)(comparator));
90
90
  }
91
91
  if (this.branches.range) {
92
- 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));
93
93
  }
94
94
  this.branches.range = {
95
95
  limit,
@@ -102,7 +102,7 @@ class DynamicState {
102
102
  this.pushRootToBranch("|");
103
103
  this.setRoot(this.branches.union);
104
104
  } else if (this.branches.intersection) {
105
- 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));
106
106
  }
107
107
  }
108
108
  finalizeGroup() {
@@ -115,15 +115,15 @@ class DynamicState {
115
115
  }
116
116
  pushRootToBranch(token) {
117
117
  this.assertRangeUnset();
118
- 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();
119
119
  if (token === "|") {
120
- 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;
121
121
  delete this.branches.intersection;
122
122
  }
123
123
  }
124
124
  assertRangeUnset() {
125
125
  if (this.branches.range) {
126
- 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));
127
127
  }
128
128
  }
129
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
@@ -77,7 +77,7 @@ const distributeRange = (range, s)=>{
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})`;