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
@@ -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 (0, _nodeTs.arrayOf)(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;
@@ -10,8 +10,8 @@ function _export(target, all) {
10
10
  }
11
11
  _export(exports, {
12
12
  Scope: ()=>Scope,
13
- writeShallowCycleErrorMessage: ()=>writeShallowCycleErrorMessage,
14
13
  scope: ()=>scope,
14
+ writeShallowCycleErrorMessage: ()=>writeShallowCycleErrorMessage,
15
15
  writeDuplicateAliasesMessage: ()=>writeDuplicateAliasesMessage
16
16
  });
17
17
  const _nodeTs = require("../nodes/node.js");
@@ -159,7 +159,57 @@ class Scope {
159
159
  _classPrivateFieldSet(this, _resolutions, new _cacheTs.Cache());
160
160
  _classPrivateFieldSet(this, _exports, new _cacheTs.Cache());
161
161
  _classPrivateFieldSet(this, _anonymousTypeCount, 0);
162
- this.type = (def, opts = {})=>{
162
+ this.expressions = {
163
+ intersection: (l, r, opts)=>this.type([
164
+ l,
165
+ "&",
166
+ r
167
+ ], opts),
168
+ union: (l, r, opts)=>this.type([
169
+ l,
170
+ "|",
171
+ r
172
+ ], opts),
173
+ arrayOf: (def, opts)=>this.type([
174
+ def,
175
+ "[]"
176
+ ], opts),
177
+ keyOf: (def, opts)=>this.type([
178
+ "keyof",
179
+ def
180
+ ], opts),
181
+ fromNode: (def, opts)=>this.type([
182
+ "node",
183
+ def
184
+ ], opts),
185
+ instanceOf: (def, opts)=>this.type([
186
+ "instanceof",
187
+ def
188
+ ], opts),
189
+ valueOf: (def, opts)=>this.type([
190
+ "===",
191
+ def
192
+ ], opts),
193
+ narrow: (def, fn, opts)=>this.type([
194
+ def,
195
+ ":",
196
+ fn
197
+ ], opts),
198
+ morph: (def, fn, opts)=>this.type([
199
+ def,
200
+ "=>",
201
+ fn
202
+ ], opts)
203
+ };
204
+ this.intersection = this.expressions.intersection;
205
+ this.union = this.expressions.union;
206
+ this.arrayOf = this.expressions.arrayOf;
207
+ this.keyOf = this.expressions.keyOf;
208
+ this.valueOf = this.expressions.valueOf;
209
+ this.instanceOf = this.expressions.instanceOf;
210
+ this.narrow = this.expressions.narrow;
211
+ this.morph = this.expressions.morph;
212
+ this.type = Object.assign((def, opts = {})=>{
163
213
  if (opts.name && this.aliases[opts.name]) {
164
214
  return (0, _errorsTs.throwParseError)(writeDuplicateAliasesMessage(opts.name));
165
215
  }
@@ -168,7 +218,9 @@ class Scope {
168
218
  result.node = this.resolveNode((0, _definitionTs.parseDefinition)(def, ctx));
169
219
  result.flat = (0, _nodeTs.flattenType)(result);
170
220
  return result;
171
- };
221
+ }, {
222
+ from: this.expressions.fromNode
223
+ });
172
224
  this.name = _classPrivateMethodGet(this, _register, register).call(this, opts);
173
225
  if (opts.standard !== false) {
174
226
  _classPrivateMethodGet(this, _cacheSpaces, cacheSpaces).call(this, [
@@ -236,9 +288,9 @@ function resolveRecurse(name, onUnresolvable, seen) {
236
288
  type.flat = (0, _nodeTs.flattenType)(type);
237
289
  return type;
238
290
  }
291
+ const scope = (aliases, opts = {})=>new Scope(aliases, opts);
239
292
  const writeShallowCycleErrorMessage = (name, seen)=>`Alias '${name}' has a shallow resolution cycle: ${[
240
293
  ...seen,
241
294
  name
242
295
  ].join("=>")}`;
243
- const scope = (aliases, opts = {})=>new Scope(aliases, opts);
244
296
  const writeDuplicateAliasesMessage = (name)=>`Alias '${name}' is already defined`;
@@ -14,15 +14,55 @@ _export(exports, {
14
14
  Problems: ()=>Problems,
15
15
  describeDomains: ()=>describeDomains,
16
16
  describeBranches: ()=>describeBranches,
17
- DataWrapper: ()=>DataWrapper,
18
17
  defaultProblemWriters: ()=>defaultProblemWriters
19
18
  });
20
19
  const _scannerTs = require("../parse/string/shift/scanner.js");
20
+ const _dataTs = require("../utils/data.js");
21
21
  const _domainsTs = require("../utils/domains.js");
22
22
  const _numericLiteralsTs = require("../utils/numericLiterals.js");
23
23
  const _objectKindsTs = require("../utils/objectKinds.js");
24
24
  const _pathsTs = require("../utils/paths.js");
25
25
  const _serializeTs = require("../utils/serialize.js");
26
+ function _checkPrivateRedeclaration(obj, privateCollection) {
27
+ if (privateCollection.has(obj)) {
28
+ throw new TypeError("Cannot initialize the same private elements twice on an object");
29
+ }
30
+ }
31
+ function _classApplyDescriptorGet(receiver, descriptor) {
32
+ if (descriptor.get) {
33
+ return descriptor.get.call(receiver);
34
+ }
35
+ return descriptor.value;
36
+ }
37
+ function _classApplyDescriptorSet(receiver, descriptor, value) {
38
+ if (descriptor.set) {
39
+ descriptor.set.call(receiver, value);
40
+ } else {
41
+ if (!descriptor.writable) {
42
+ throw new TypeError("attempted to set read only private field");
43
+ }
44
+ descriptor.value = value;
45
+ }
46
+ }
47
+ function _classExtractFieldDescriptor(receiver, privateMap, action) {
48
+ if (!privateMap.has(receiver)) {
49
+ throw new TypeError("attempted to " + action + " private field on non-instance");
50
+ }
51
+ return privateMap.get(receiver);
52
+ }
53
+ function _classPrivateFieldGet(receiver, privateMap) {
54
+ var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get");
55
+ return _classApplyDescriptorGet(receiver, descriptor);
56
+ }
57
+ function _classPrivateFieldInit(obj, privateMap, value) {
58
+ _checkPrivateRedeclaration(obj, privateMap);
59
+ privateMap.set(obj, value);
60
+ }
61
+ function _classPrivateFieldSet(receiver, privateMap, value) {
62
+ var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set");
63
+ _classApplyDescriptorSet(receiver, descriptor, value);
64
+ return value;
65
+ }
26
66
  var _definitions_code, _definitions_code1;
27
67
  class ArkTypeError extends TypeError {
28
68
  constructor(problems){
@@ -38,7 +78,7 @@ class Problem {
38
78
  return this.writers.addContext(this.reason, this.path);
39
79
  }
40
80
  get reason() {
41
- return this.writers.writeReason(this.mustBe, new DataWrapper(this.data));
81
+ return this.writers.writeReason(this.mustBe, new _dataTs.DataWrapper(this.data));
42
82
  }
43
83
  get mustBe() {
44
84
  return this.writers.mustBe(this.source);
@@ -54,21 +94,27 @@ class Problem {
54
94
  }
55
95
  }
56
96
  }
97
+ var _state = /*#__PURE__*/ new WeakMap();
57
98
  class ProblemArray extends Array {
58
- add(code, data, source, opts) {
99
+ create(code, source, opts) {
59
100
  // copy the path to avoid future mutations affecting it
60
- const path = opts?.path ?? _pathsTs.Path.from(this.state.path);
61
- const problem = new Problem(code, path, data, source, this.state.getConfigForProblemCode(code));
62
- const pathKey = `${path}`;
101
+ const path = opts?.path ?? _pathsTs.Path.from(_classPrivateFieldGet(this, _state).path);
102
+ const data = // we have to check for the presence of the key explicitly since the
103
+ // data could be undefined or null
104
+ opts && "data" in opts ? opts.data : _classPrivateFieldGet(this, _state).data;
105
+ return this.add(new Problem(code, path, data, source, _classPrivateFieldGet(this, _state).getConfigForProblemCode(code)));
106
+ }
107
+ add(problem) {
108
+ const pathKey = `${problem.path}`;
63
109
  const existing = this.byPath[pathKey];
64
110
  if (existing) {
65
111
  if (existing.parts) {
66
112
  existing.parts.push(problem);
67
113
  } else {
68
- const problemIntersection = new Problem("multi", existing.path, data, [
114
+ const problemIntersection = new Problem("multi", existing.path, existing.data, [
69
115
  existing,
70
116
  problem
71
- ], this.state.getConfigForProblemCode("multi"));
117
+ ], _classPrivateFieldGet(this, _state).getConfigForProblemCode("multi"));
72
118
  const existingIndex = this.indexOf(existing);
73
119
  // If existing is found (which it always should be unless this was externally mutated),
74
120
  // replace it with the new problem intersection. In case it isn't for whatever reason,
@@ -93,8 +139,12 @@ class ProblemArray extends Array {
93
139
  }
94
140
  constructor(state){
95
141
  super();
96
- this.state = state;
97
142
  this.byPath = {};
143
+ _classPrivateFieldInit(this, _state, {
144
+ writable: true,
145
+ value: void 0
146
+ });
147
+ _classPrivateFieldSet(this, _state, state);
98
148
  }
99
149
  }
100
150
  const Problems = ProblemArray;
@@ -119,26 +169,6 @@ const describeBranches = (descriptions)=>{
119
169
  description += ` or ${descriptions[descriptions.length - 1]}`;
120
170
  return description;
121
171
  };
122
- class DataWrapper {
123
- toString() {
124
- return (0, _serializeTs.stringify)(this.value);
125
- }
126
- get domain() {
127
- return (0, _domainsTs.domainOf)(this.value);
128
- }
129
- get size() {
130
- return typeof this.value === "string" || Array.isArray(this.value) ? this.value.length : typeof this.value === "number" ? this.value : 0;
131
- }
132
- get units() {
133
- return typeof this.value === "string" ? "characters" : Array.isArray(this.value) ? "items" : "";
134
- }
135
- get className() {
136
- return Object(this.value).constructor.name;
137
- }
138
- constructor(value){
139
- this.value = value;
140
- }
141
- }
142
172
  const writeDefaultReason = (mustBe, was)=>`must be ${mustBe}${was && ` (was ${was})`}`;
143
173
  const addDefaultContext = (reason, path)=>path.length === 0 ? capitalize(reason) : path.length === 1 && (0, _numericLiteralsTs.isWellFormedInteger)(path[0]) ? `Item at index ${path[0]} ${reason}` : `${path} ${reason}`;
144
174
  const compileDefaultProblemWriters = (definitions)=>{
@@ -85,7 +85,6 @@ class TraversalState {
85
85
  traverseKey(key, node) {
86
86
  const lastData = this.data;
87
87
  this.data = this.data[key];
88
- // TODO: make path externally readonly
89
88
  this.path.push(key);
90
89
  const isValid = traverse(node, this);
91
90
  this.path.pop();
@@ -141,7 +140,7 @@ class TraversalState {
141
140
  this.path = lastPath;
142
141
  this.problems = lastProblems;
143
142
  this.failFast = lastFailFast;
144
- return hasValidBranch || this.problems.add("branches", this.data, branchProblems);
143
+ return hasValidBranch || this.problems.create("branches", branchProblems);
145
144
  }
146
145
  constructor(data, type){
147
146
  _classPrivateFieldInit(this, _seen, {
@@ -159,22 +158,11 @@ class TraversalState {
159
158
  ] : [];
160
159
  }
161
160
  }
162
- const traverse = (node, state)=>typeof node === "string" ? (0, _domainsJs.domainOf)(state.data) === node || state.problems.add("domain", state.data, node) : checkEntries(node, state);
161
+ const traverse = (node, state)=>typeof node === "string" ? (0, _domainsJs.domainOf)(state.data) === node || state.problems.create("domain", node) : checkEntries(node, state);
163
162
  const checkEntries = (entries, state)=>{
164
163
  let isValid = true;
165
164
  for(let i = 0; i < entries.length; i++){
166
165
  const [k, v] = entries[i];
167
- if (k === "morph") {
168
- if (typeof v === "function") {
169
- // TODO: allow problem from morph
170
- state.data = v(state.data);
171
- } else {
172
- for (const morph of v){
173
- state.data = morph(state.data);
174
- }
175
- }
176
- return true;
177
- }
178
166
  const entryAllowsData = entryCheckers[k](v, state);
179
167
  isValid && (isValid = entryAllowsData);
180
168
  if (!isValid) {
@@ -195,8 +183,9 @@ const checkRequiredProp = (prop, state)=>{
195
183
  if (prop[0] in state.data) {
196
184
  return state.traverseKey(prop[0], prop[1]);
197
185
  }
198
- return state.problems.add("missing", undefined, undefined, {
199
- path: state.path.concat(prop[0])
186
+ return state.problems.create("missing", undefined, {
187
+ path: state.path.concat(prop[0]),
188
+ data: undefined
200
189
  });
201
190
  };
202
191
  const entryCheckers = {
@@ -204,10 +193,9 @@ const entryCheckers = {
204
193
  divisor: _divisorJs.checkDivisor,
205
194
  domains: (domains, state)=>{
206
195
  const entries = domains[(0, _domainsJs.domainOf)(state.data)];
207
- return entries ? checkEntries(entries, state) : state.problems.add("domainBranches", state.data, (0, _genericsJs.keysOf)(domains));
196
+ return entries ? checkEntries(entries, state) : state.problems.create("domainBranches", (0, _genericsJs.objectKeysOf)(domains));
208
197
  },
209
- // TODO: remove data from problem params
210
- domain: (domain, state)=>(0, _domainsJs.domainOf)(state.data) === domain || state.problems.add("domain", state.data, domain),
198
+ domain: (domain, state)=>(0, _domainsJs.domainOf)(state.data) === domain || state.problems.create("domain", domain),
211
199
  bound: _rangeJs.checkBound,
212
200
  optionalProp: (prop, state)=>{
213
201
  if (prop[0] in state.data) {
@@ -221,7 +209,7 @@ const entryCheckers = {
221
209
  prerequisiteProp: checkRequiredProp,
222
210
  indexProp: (node, state)=>{
223
211
  if (!Array.isArray(state.data)) {
224
- return state.problems.add("class", state.data, "Array");
212
+ return state.problems.create("class", "Array");
225
213
  }
226
214
  let isValid = true;
227
215
  for(let i = 0; i < state.data.length; i++){
@@ -239,17 +227,18 @@ const entryCheckers = {
239
227
  if ((0, _genericsJs.hasKey)(rule.cases, caseKey)) {
240
228
  return checkEntries(rule.cases[caseKey], state);
241
229
  }
242
- const caseKeys = (0, _genericsJs.keysOf)(rule.cases);
230
+ const caseKeys = (0, _genericsJs.objectKeysOf)(rule.cases);
243
231
  const missingCasePath = state.path.concat(rule.path);
244
- return rule.kind === "value" ? state.problems.add("valueBranches", dataAtPath, caseKeys.map((k)=>(0, _serializeJs.deserializePrimitive)(k)), {
245
- path: missingCasePath
246
- }) : state.problems.add("domainBranches", dataAtPath, caseKeys, {
247
- path: missingCasePath
232
+ return rule.kind === "value" ? state.problems.create("valueBranches", caseKeys.map((k)=>(0, _serializeJs.deserializePrimitive)(k)), {
233
+ path: missingCasePath,
234
+ data: dataAtPath
235
+ }) : state.problems.create("domainBranches", caseKeys, {
236
+ path: missingCasePath,
237
+ data: dataAtPath
248
238
  });
249
239
  },
250
240
  alias: (name, state)=>state.traverseResolution(name),
251
241
  class: _classJs.checkClass,
252
- // TODO: fix
253
242
  narrow: (narrow, state)=>narrow(state.data, state.problems),
254
243
  config: ({ config , node }, state)=>{
255
244
  state.configs.push(config);
@@ -257,5 +246,13 @@ const entryCheckers = {
257
246
  state.configs.pop();
258
247
  return result;
259
248
  },
260
- value: (value, state)=>state.data === value || state.problems.add("value", state.data, value)
249
+ value: (value, state)=>state.data === value || state.problems.create("value", value),
250
+ morph: (morph, state)=>{
251
+ const out = morph(state.data);
252
+ if (out instanceof _problemsJs.Problem) {
253
+ return state.problems.add(out);
254
+ }
255
+ state.data = out;
256
+ return true;
257
+ }
261
258
  };
@@ -0,0 +1,39 @@
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
+ sizeOf: ()=>sizeOf,
13
+ unitsOf: ()=>unitsOf,
14
+ DataWrapper: ()=>DataWrapper
15
+ });
16
+ const _domainsTs = require("./domains.js");
17
+ const _serializeTs = require("./serialize.js");
18
+ const sizeOf = (data)=>typeof data === "string" || Array.isArray(data) ? data.length : typeof data === "number" ? data : 0;
19
+ const unitsOf = (data)=>typeof data === "string" ? "characters" : Array.isArray(data) ? "items" : "";
20
+ class DataWrapper {
21
+ toString() {
22
+ return (0, _serializeTs.stringify)(this.value);
23
+ }
24
+ get domain() {
25
+ return (0, _domainsTs.domainOf)(this.value);
26
+ }
27
+ get size() {
28
+ return sizeOf(this.value);
29
+ }
30
+ get units() {
31
+ return unitsOf(this.value);
32
+ }
33
+ get className() {
34
+ return Object(this.value).constructor.name;
35
+ }
36
+ constructor(value){
37
+ this.value = value;
38
+ }
39
+ }
@@ -12,7 +12,8 @@ _export(exports, {
12
12
  asConst: ()=>asConst,
13
13
  isKeyOf: ()=>isKeyOf,
14
14
  entriesOf: ()=>entriesOf,
15
- keysOf: ()=>keysOf,
15
+ objectKeysOf: ()=>objectKeysOf,
16
+ prototypeKeysOf: ()=>prototypeKeysOf,
16
17
  hasKey: ()=>hasKey,
17
18
  keyCount: ()=>keyCount,
18
19
  hasKeys: ()=>hasKeys,
@@ -22,7 +23,24 @@ const _domainsTs = require("./domains.js");
22
23
  const asConst = (t)=>t;
23
24
  const isKeyOf = (k, obj)=>k in obj;
24
25
  const entriesOf = (o)=>Object.entries(o);
25
- const keysOf = (o)=>Object.keys(o);
26
+ const objectKeysOf = (o)=>Object.keys(o);
27
+ const prototypeKeysOf = (value)=>{
28
+ const result = [];
29
+ while(value !== Object.prototype && value !== null && value !== undefined){
30
+ for (const k of Object.getOwnPropertyNames(value)){
31
+ if (!result.includes(k)) {
32
+ result.push(k);
33
+ }
34
+ }
35
+ for (const symbol of Object.getOwnPropertySymbols(value)){
36
+ if (!result.includes(symbol)) {
37
+ result.push(symbol);
38
+ }
39
+ }
40
+ value = Object.getPrototypeOf(value);
41
+ }
42
+ return result;
43
+ };
26
44
  const hasKey = (o, k)=>{
27
45
  const valueAtKey = o?.[k];
28
46
  return valueAtKey !== undefined && valueAtKey !== null;
@@ -1,4 +1,4 @@
1
- import { writeImplicitNeverMessage } from "../parse/string/ast.js";
1
+ import { writeImplicitNeverMessage } from "../parse/ast/intersection.js";
2
2
  import { domainOf, hasDomain } from "../utils/domains.js";
3
3
  import { throwInternalError, throwParseError } from "../utils/errors.js";
4
4
  import { isDisjoint, isEquality } from "./compose.js";
@@ -75,10 +75,19 @@ export const flattenBranch = (branch, ctx)=>{
75
75
  if (isMorphBranch(branch)) {
76
76
  const result = flattenRules(branch.rules, ctx);
77
77
  if (branch.morph) {
78
- result.push([
79
- "morph",
80
- branch.morph
81
- ]);
78
+ if (typeof branch.morph === "function") {
79
+ result.push([
80
+ "morph",
81
+ branch.morph
82
+ ]);
83
+ } else {
84
+ for (const morph of branch.morph){
85
+ result.push([
86
+ "morph",
87
+ morph
88
+ ]);
89
+ }
90
+ }
82
91
  }
83
92
  return result;
84
93
  }
@@ -39,7 +39,7 @@ function _classPrivateFieldSet(receiver, privateMap, value) {
39
39
  return value;
40
40
  }
41
41
  import { throwInternalError } from "../utils/errors.js";
42
- import { keysOf } from "../utils/generics.js";
42
+ import { objectKeysOf } from "../utils/generics.js";
43
43
  import { Path } from "../utils/paths.js";
44
44
  import { serialize } from "../utils/serialize.js";
45
45
  export const composeIntersection = (reducer)=>(l, r, state)=>l === undefined ? r === undefined ? throwUndefinedOperandsError() : r : r === undefined ? l : reducer(l, r, state);
@@ -87,7 +87,7 @@ export const equality = ()=>equal;
87
87
  export const isEquality = (result)=>result === equal;
88
88
  export const composeKeyedIntersection = (reducer, config)=>(l, r, state)=>{
89
89
  const result = {};
90
- const keys = keysOf({
90
+ const keys = objectKeysOf({
91
91
  ...l,
92
92
  ...r
93
93
  });
@@ -1,7 +1,7 @@
1
- import { writeUndiscriminatableMorphUnionMessage } from "../parse/string/ast.js";
1
+ import { writeUndiscriminatableMorphUnionMessage } from "../parse/ast/union.js";
2
2
  import { domainOf } from "../utils/domains.js";
3
3
  import { throwParseError } from "../utils/errors.js";
4
- import { hasKey, isKeyOf, keysOf } from "../utils/generics.js";
4
+ import { hasKey, isKeyOf, objectKeysOf } from "../utils/generics.js";
5
5
  import { isArray, objectKindOf } from "../utils/objectKinds.js";
6
6
  import { Path } from "../utils/paths.js";
7
7
  import { serializePrimitive } from "../utils/serialize.js";
@@ -142,7 +142,7 @@ const findBestDiscriminant = (remainingIndices, discriminants)=>{
142
142
  filteredCases[caseKey] = filteredIndices;
143
143
  score++;
144
144
  }
145
- const defaultCaseKeys = keysOf(defaultCases);
145
+ const defaultCaseKeys = objectKeysOf(defaultCases);
146
146
  if (defaultCaseKeys.length) {
147
147
  filteredCases["default"] = defaultCaseKeys.map((k)=>parseInt(k));
148
148
  }
@@ -1,6 +1,6 @@
1
- import { compileDisjointReasonsMessage } from "../parse/string/ast.js";
1
+ import { compileDisjointReasonsMessage } from "../parse/ast/intersection.js";
2
2
  import { throwParseError } from "../utils/errors.js";
3
- import { hasKey, hasKeys, keysOf } from "../utils/generics.js";
3
+ import { hasKey, hasKeys, objectKeysOf } from "../utils/generics.js";
4
4
  import { Path } from "../utils/paths.js";
5
5
  import { anonymousDisjoint, composeKeyedIntersection, IntersectionState, isDisjoint, isEquality, throwUndefinedOperandsError } from "./compose.js";
6
6
  import { flattenPredicate, isLiteralCondition, predicateIntersection, predicateUnion, resolutionExtendsDomain } from "./predicate.js";
@@ -11,7 +11,7 @@ export const nodeIntersection = (l, r, state)=>{
11
11
  const rResolution = state.type.meta.scope.resolveNode(r);
12
12
  const result = resolutionIntersection(lResolution, rResolution, state);
13
13
  if (typeof result === "object" && !hasKeys(result)) {
14
- return hasKeys(state.disjoints) ? anonymousDisjoint() : state.addDisjoint("domain", keysOf(lResolution), keysOf(rResolution));
14
+ return hasKeys(state.disjoints) ? anonymousDisjoint() : state.addDisjoint("domain", objectKeysOf(lResolution), objectKeysOf(rResolution));
15
15
  }
16
16
  return result === lResolution ? l : result === rResolution ? r : result;
17
17
  };
@@ -26,16 +26,16 @@ const resolutionIntersection = composeKeyedIntersection((domain, l, r, context)=
26
26
  }, {
27
27
  onEmpty: "omit"
28
28
  });
29
- export const intersection = (l, r, type)=>{
29
+ export const rootIntersection = (l, r, type)=>{
30
30
  const state = new IntersectionState(type, "&");
31
31
  const result = nodeIntersection(l, r, state);
32
32
  return isDisjoint(result) ? throwParseError(compileDisjointReasonsMessage(state.disjoints)) : isEquality(result) ? l : result;
33
33
  };
34
- export const union = (l, r, type)=>{
34
+ export const rootUnion = (l, r, type)=>{
35
35
  const lResolution = type.meta.scope.resolveNode(l);
36
36
  const rResolution = type.meta.scope.resolveNode(r);
37
37
  const result = {};
38
- const domains = keysOf({
38
+ const domains = objectKeysOf({
39
39
  ...lResolution,
40
40
  ...rResolution
41
41
  });
@@ -75,7 +75,7 @@ export const flattenNode = (node, ctx)=>{
75
75
  ]
76
76
  ] : resolution.flat;
77
77
  }
78
- const domains = keysOf(node);
78
+ const domains = objectKeysOf(node);
79
79
  if (domains.length === 1) {
80
80
  const domain = domains[0];
81
81
  const predicate = node[domain];
@@ -107,7 +107,7 @@ export const flattenNode = (node, ctx)=>{
107
107
  export const isLiteralNode = (node, domain)=>{
108
108
  return resolutionExtendsDomain(node, domain) && isLiteralCondition(node[domain]);
109
109
  };
110
- export const arrayOf = (node)=>({
110
+ export const toArrayNode = (node)=>({
111
111
  object: {
112
112
  class: "Array",
113
113
  props: {
@@ -1,4 +1,4 @@
1
- import { keysOf, listFrom } from "../utils/generics.js";
1
+ import { listFrom, objectKeysOf } from "../utils/generics.js";
2
2
  import { isArray } from "../utils/objectKinds.js";
3
3
  import { branchIntersection, compareBranches, flattenBranch, isBranchComparison } from "./branch.js";
4
4
  import { equality, IntersectionState, isEquality } from "./compose.js";
@@ -69,6 +69,6 @@ export const flattenPredicate = (predicate, context)=>{
69
69
  };
70
70
  export const isLiteralCondition = (predicate)=>typeof predicate === "object" && "value" in predicate;
71
71
  export const resolutionExtendsDomain = (resolution, domain)=>{
72
- const domains = keysOf(resolution);
72
+ const domains = objectKeysOf(resolution);
73
73
  return domains.length === 1 && domains[0] === domain;
74
74
  };
@@ -7,7 +7,7 @@ export const classIntersection = composeIntersection((l, r, state)=>{
7
7
  });
8
8
  export const checkClass = (expectedClass, state)=>{
9
9
  if (typeof expectedClass === "string") {
10
- return objectKindOf(state.data) === expectedClass || state.problems.add("class", state.data, expectedClass);
10
+ return objectKindOf(state.data) === expectedClass || state.problems.create("class", expectedClass);
11
11
  }
12
- return state.data instanceof expectedClass || state.problems.add("class", state.data, expectedClass);
12
+ return state.data instanceof expectedClass || state.problems.create("class", expectedClass);
13
13
  };
@@ -12,4 +12,4 @@ const greatestCommonDivisor = (l, r)=>{
12
12
  }
13
13
  return greatestCommonDivisor;
14
14
  };
15
- export const checkDivisor = (divisor, state)=>state.data % divisor === 0 || state.problems.add("divisor", state.data, divisor);
15
+ export const checkDivisor = (divisor, state)=>state.data % divisor === 0 || state.problems.create("divisor", divisor);
@@ -1,4 +1,4 @@
1
- import { sizeOf } from "../../utils/size.js";
1
+ import { sizeOf } from "../../utils/data.js";
2
2
  import { composeIntersection, equality } from "../compose.js";
3
3
  export const minComparators = {
4
4
  ">": true,
@@ -74,7 +74,7 @@ export const flattenRange = (entries, range, ctx)=>{
74
74
  ]);
75
75
  }
76
76
  };
77
- export const checkBound = (bound, state)=>comparatorCheckers[bound.comparator](sizeOf(state.data), bound.limit) || state.problems.add("bound", state.data, bound);
77
+ export const checkBound = (bound, state)=>comparatorCheckers[bound.comparator](sizeOf(state.data), bound.limit) || state.problems.create("bound", bound);
78
78
  const comparatorCheckers = {
79
79
  "<": (size, limit)=>size < limit,
80
80
  ">": (size, limit)=>size > limit,
@@ -7,5 +7,5 @@ export const getRegex = (source)=>{
7
7
  }
8
8
  return regexCache[source];
9
9
  };
10
- export const checkRegex = (source, state)=>getRegex(source).test(state.data) || state.problems.add("regex", state.data, `/${source}/`);
10
+ export const checkRegex = (source, state)=>getRegex(source).test(state.data) || state.problems.create("regex", `/${source}/`);
11
11
  export const regexIntersection = composeIntersection(collapsibleListUnion);
@@ -24,6 +24,10 @@ export const flattenRules = (rules, ctx)=>{
24
24
  for(k in rules){
25
25
  ruleFlatteners[k](entries, rules[k], ctx);
26
26
  }
27
+ // Some entries with the same precedence, e.g. morphs flattened from a list,
28
+ // rely on the fact that JS's builtin sort is stable to behave as expected
29
+ // when traversed:
30
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
27
31
  return entries.sort((l, r)=>precedenceMap[l[0]] - precedenceMap[r[0]]);
28
32
  };
29
33
  const ruleFlatteners = {
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1 @@
1
+ export const writeUnboundableMessage = (root)=>`Bounded expression ${root} must be a number, string or array`;