arktype 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/nodes/{branches.js → branch.js} +56 -10
- package/dist/cjs/nodes/compose.js +1 -2
- package/dist/cjs/nodes/discriminate.js +7 -13
- package/dist/cjs/nodes/node.js +12 -2
- package/dist/cjs/nodes/predicate.js +7 -9
- package/dist/cjs/nodes/rules/class.js +10 -4
- package/dist/cjs/nodes/rules/divisor.js +1 -5
- package/dist/cjs/nodes/rules/props.js +75 -25
- package/dist/cjs/nodes/rules/range.js +1 -5
- package/dist/cjs/nodes/rules/regex.js +1 -5
- package/dist/cjs/nodes/rules/rules.js +19 -63
- package/dist/cjs/parse/string/reduce/dynamic.js +1 -2
- package/dist/cjs/parse/string/shift/operator/bounds.js +1 -1
- package/dist/cjs/parse/string/string.js +2 -2
- package/dist/cjs/parse/tuple/keyof.js +37 -18
- package/dist/cjs/parse/tuple/morph.js +2 -2
- package/dist/cjs/parse/tuple/tuple.js +16 -9
- package/dist/cjs/scopes/jsObjects.js +14 -14
- package/dist/cjs/scopes/type.js +4 -7
- package/dist/cjs/traverse/problems.js +12 -28
- package/dist/cjs/traverse/traverse.js +104 -133
- package/dist/cjs/utils/numericLiterals.js +2 -0
- package/dist/cjs/utils/objectKinds.js +1 -2
- package/dist/mjs/nodes/{branches.js → branch.js} +45 -2
- package/dist/mjs/nodes/compose.js +1 -2
- package/dist/mjs/nodes/discriminate.js +6 -12
- package/dist/mjs/nodes/node.js +10 -1
- package/dist/mjs/nodes/predicate.js +2 -3
- package/dist/mjs/nodes/rules/class.js +10 -4
- package/dist/mjs/nodes/rules/divisor.js +1 -5
- package/dist/mjs/nodes/rules/props.js +74 -27
- package/dist/mjs/nodes/rules/range.js +1 -5
- package/dist/mjs/nodes/rules/regex.js +1 -5
- package/dist/mjs/nodes/rules/rules.js +19 -62
- package/dist/mjs/parse/string/reduce/dynamic.js +2 -3
- package/dist/mjs/parse/string/shift/operator/bounds.js +1 -1
- package/dist/mjs/parse/string/string.js +2 -2
- package/dist/mjs/parse/tuple/keyof.js +38 -19
- package/dist/mjs/parse/tuple/morph.js +2 -2
- package/dist/mjs/parse/tuple/tuple.js +17 -10
- package/dist/mjs/scopes/jsObjects.js +14 -14
- package/dist/mjs/scopes/type.js +4 -7
- package/dist/mjs/traverse/problems.js +13 -29
- package/dist/mjs/traverse/traverse.js +105 -133
- package/dist/mjs/utils/numericLiterals.js +1 -0
- package/dist/mjs/utils/objectKinds.js +1 -1
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/nodes/branch.d.ts +36 -0
- package/dist/types/nodes/compose.d.ts +5 -10
- package/dist/types/nodes/discriminate.d.ts +1 -1
- package/dist/types/nodes/infer.d.ts +17 -16
- package/dist/types/nodes/node.d.ts +10 -5
- package/dist/types/nodes/predicate.d.ts +4 -32
- package/dist/types/nodes/rules/class.d.ts +3 -2
- package/dist/types/nodes/rules/divisor.d.ts +2 -1
- package/dist/types/nodes/rules/props.d.ts +19 -11
- package/dist/types/nodes/rules/range.d.ts +3 -2
- package/dist/types/nodes/rules/regex.d.ts +2 -1
- package/dist/types/nodes/rules/rules.d.ts +11 -13
- package/dist/types/parse/string/ast.d.ts +2 -1
- package/dist/types/parse/string/reduce/dynamic.d.ts +1 -1
- package/dist/types/parse/string/reduce/shared.d.ts +1 -1
- package/dist/types/parse/string/shift/operand/unenclosed.d.ts +1 -1
- package/dist/types/parse/string/shift/operator/bounds.d.ts +1 -1
- package/dist/types/parse/string/string.d.ts +1 -1
- package/dist/types/parse/tuple/keyof.d.ts +2 -2
- package/dist/types/scopes/type.d.ts +5 -11
- package/dist/types/traverse/problems.d.ts +8 -23
- package/dist/types/traverse/traverse.d.ts +27 -23
- package/dist/types/utils/generics.d.ts +0 -1
- package/dist/types/utils/numericLiterals.d.ts +1 -0
- package/dist/types/utils/objectKinds.d.ts +43 -43
- package/package.json +1 -2
- package/dist/cjs/nodes/functors.js +0 -35
- package/dist/cjs/nodes/rules/objectKind.js +0 -78
- package/dist/mjs/nodes/functors.js +0 -27
- package/dist/mjs/nodes/rules/objectKind.js +0 -63
- package/dist/types/nodes/branches.d.ts +0 -14
- package/dist/types/nodes/functors.d.ts +0 -6
- package/dist/types/nodes/rules/objectKind.d.ts +0 -15
|
@@ -12,7 +12,7 @@ _export(exports, {
|
|
|
12
12
|
TraversalState: ()=>TraversalState,
|
|
13
13
|
traverse: ()=>traverse,
|
|
14
14
|
checkEntries: ()=>checkEntries,
|
|
15
|
-
|
|
15
|
+
checkRequiredProp: ()=>checkRequiredProp
|
|
16
16
|
});
|
|
17
17
|
const _discriminateJs = require("../nodes/discriminate.js");
|
|
18
18
|
const _classJs = require("../nodes/rules/class.js");
|
|
@@ -21,9 +21,7 @@ const _rangeJs = require("../nodes/rules/range.js");
|
|
|
21
21
|
const _regexJs = require("../nodes/rules/regex.js");
|
|
22
22
|
const _rulesJs = require("../nodes/rules/rules.js");
|
|
23
23
|
const _domainsJs = require("../utils/domains.js");
|
|
24
|
-
const _errorsJs = require("../utils/errors.js");
|
|
25
24
|
const _genericsJs = require("../utils/generics.js");
|
|
26
|
-
const _objectKindsJs = require("../utils/objectKinds.js");
|
|
27
25
|
const _pathsJs = require("../utils/paths.js");
|
|
28
26
|
const _serializeJs = require("../utils/serialize.js");
|
|
29
27
|
const _problemsJs = require("./problems.js");
|
|
@@ -84,9 +82,22 @@ class TraversalState {
|
|
|
84
82
|
}
|
|
85
83
|
return _problemsJs.defaultProblemWriters[code];
|
|
86
84
|
}
|
|
87
|
-
|
|
85
|
+
traverseKey(key, node) {
|
|
86
|
+
const lastData = this.data;
|
|
87
|
+
this.data = this.data[key];
|
|
88
|
+
// TODO: make path externally readonly
|
|
89
|
+
this.path.push(key);
|
|
90
|
+
const isValid = traverse(node, this);
|
|
91
|
+
this.path.pop();
|
|
92
|
+
lastData[key] = this.data;
|
|
93
|
+
this.data = lastData;
|
|
94
|
+
return isValid;
|
|
95
|
+
}
|
|
96
|
+
traverseResolution(name) {
|
|
88
97
|
const resolution = this.type.meta.scope.resolve(name);
|
|
89
98
|
const id = resolution.meta.id;
|
|
99
|
+
// this assignment helps with narrowing
|
|
100
|
+
const data = this.data;
|
|
90
101
|
const isObject = (0, _domainsJs.hasDomain)(data, "object");
|
|
91
102
|
if (isObject) {
|
|
92
103
|
if ((0, _genericsJs.hasKey)(_classPrivateFieldGet(this, _seen), id)) {
|
|
@@ -94,7 +105,7 @@ class TraversalState {
|
|
|
94
105
|
// if data has already been checked by this alias as part of
|
|
95
106
|
// a resolution higher up on the call stack, it must be valid
|
|
96
107
|
// or we wouldn't be here
|
|
97
|
-
return;
|
|
108
|
+
return true;
|
|
98
109
|
}
|
|
99
110
|
_classPrivateFieldGet(this, _seen)[id].push(data);
|
|
100
111
|
} else {
|
|
@@ -105,39 +116,39 @@ class TraversalState {
|
|
|
105
116
|
}
|
|
106
117
|
const lastResolution = this.type;
|
|
107
118
|
this.type = resolution;
|
|
108
|
-
traverse(
|
|
119
|
+
const isValid = traverse(resolution.flat, this);
|
|
109
120
|
this.type = lastResolution;
|
|
110
121
|
if (isObject) {
|
|
111
122
|
_classPrivateFieldGet(this, _seen)[id].pop();
|
|
112
123
|
}
|
|
124
|
+
return isValid;
|
|
113
125
|
}
|
|
114
|
-
traverseBranches(
|
|
115
|
-
const lastProblems = this.problems;
|
|
116
|
-
const lastPath = this.path;
|
|
126
|
+
traverseBranches(branches) {
|
|
117
127
|
const lastFailFast = this.failFast;
|
|
118
128
|
this.failFast = true;
|
|
119
|
-
const
|
|
129
|
+
const lastProblems = this.problems;
|
|
130
|
+
const branchProblems = new _problemsJs.Problems(this);
|
|
131
|
+
this.problems = branchProblems;
|
|
132
|
+
const lastPath = this.path;
|
|
133
|
+
let hasValidBranch = false;
|
|
120
134
|
for (const branch of branches){
|
|
121
|
-
this.problems = new _problemsJs.Problems(this);
|
|
122
135
|
this.path = new _pathsJs.Path();
|
|
123
|
-
checkEntries(
|
|
124
|
-
|
|
136
|
+
if (checkEntries(branch, this)) {
|
|
137
|
+
hasValidBranch = true;
|
|
125
138
|
break;
|
|
126
139
|
}
|
|
127
|
-
branchProblems.push(this.problems[0]);
|
|
128
140
|
}
|
|
129
|
-
this.failFast = lastFailFast;
|
|
130
141
|
this.path = lastPath;
|
|
131
142
|
this.problems = lastProblems;
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
}
|
|
143
|
+
this.failFast = lastFailFast;
|
|
144
|
+
return hasValidBranch || this.problems.add("branches", this.data, branchProblems);
|
|
135
145
|
}
|
|
136
|
-
constructor(type){
|
|
146
|
+
constructor(data, type){
|
|
137
147
|
_classPrivateFieldInit(this, _seen, {
|
|
138
148
|
writable: true,
|
|
139
149
|
value: void 0
|
|
140
150
|
});
|
|
151
|
+
this.data = data;
|
|
141
152
|
this.type = type;
|
|
142
153
|
this.path = new _pathsJs.Path();
|
|
143
154
|
this.problems = new _problemsJs.Problems(this);
|
|
@@ -148,143 +159,103 @@ class TraversalState {
|
|
|
148
159
|
] : [];
|
|
149
160
|
}
|
|
150
161
|
}
|
|
151
|
-
const traverse = (data, node, state)
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
return checkEntries(data, node, state);
|
|
159
|
-
};
|
|
160
|
-
const checkEntries = (data, entries, state)=>{
|
|
161
|
-
let problemsPrecedence = Number.POSITIVE_INFINITY;
|
|
162
|
-
const initialProblemsCount = state.problems.length;
|
|
163
|
-
for (const [k, v] of entries){
|
|
164
|
-
if (_rulesJs.precedenceMap[k] > problemsPrecedence) {
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
162
|
+
const traverse = (node, state)=>typeof node === "string" ? (0, _domainsJs.domainOf)(state.data) === node || state.problems.add("domain", state.data, node) : checkEntries(node, state);
|
|
163
|
+
const checkEntries = (entries, state)=>{
|
|
164
|
+
let isValid = true;
|
|
165
|
+
for(let i = 0; i < entries.length; i++){
|
|
166
|
+
const [k, v] = entries[i];
|
|
167
167
|
if (k === "morph") {
|
|
168
168
|
if (typeof v === "function") {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
}
|
|
175
|
-
return out;
|
|
176
|
-
} else {
|
|
177
|
-
checkers[k](data, v, state);
|
|
178
|
-
if (state.problems.length > initialProblemsCount) {
|
|
179
|
-
if (state.failFast) {
|
|
180
|
-
return;
|
|
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);
|
|
181
174
|
}
|
|
182
|
-
problemsPrecedence = _rulesJs.precedenceMap[k];
|
|
183
175
|
}
|
|
176
|
+
return true;
|
|
184
177
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
} else {
|
|
197
|
-
traverse(data[propKey], propNode, state);
|
|
178
|
+
const entryAllowsData = entryCheckers[k](v, state);
|
|
179
|
+
isValid && (isValid = entryAllowsData);
|
|
180
|
+
if (!isValid) {
|
|
181
|
+
if (state.failFast) {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
if (i < entries.length - 1 && _rulesJs.precedenceMap[k] < _rulesJs.precedenceMap[entries[i + 1][0]]) {
|
|
185
|
+
// if we've encountered a problem, there is at least one entry
|
|
186
|
+
// remaining, and the next entry is of a higher precedence level
|
|
187
|
+
// than the current entry, return immediately
|
|
188
|
+
return false;
|
|
198
189
|
}
|
|
199
|
-
state.path.pop();
|
|
200
|
-
}
|
|
201
|
-
state.path = rootPath;
|
|
202
|
-
};
|
|
203
|
-
const checkRequiredProps = createPropChecker("requiredProps");
|
|
204
|
-
const checkOptionalProps = createPropChecker("optionalProps");
|
|
205
|
-
const checkObjectKind = (data, rule, state)=>{
|
|
206
|
-
const dataObjectKind = (0, _objectKindsJs.objectKindOf)(data);
|
|
207
|
-
if (typeof rule === "string") {
|
|
208
|
-
if (dataObjectKind !== rule) {
|
|
209
|
-
return state.problems.add("objectKind", data, rule);
|
|
210
|
-
}
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
if (dataObjectKind !== rule[0]) {
|
|
214
|
-
return state.problems.add("objectKind", data, rule[0]);
|
|
215
|
-
}
|
|
216
|
-
if (dataObjectKind === "Array" && typeof rule[2] === "number") {
|
|
217
|
-
const actual = data.length;
|
|
218
|
-
const expected = rule[2];
|
|
219
|
-
if (expected !== actual) {
|
|
220
|
-
return state.problems.add("bound", data, {
|
|
221
|
-
comparator: "==",
|
|
222
|
-
limit: expected,
|
|
223
|
-
units: "items"
|
|
224
|
-
});
|
|
225
190
|
}
|
|
226
191
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
state.path.pop();
|
|
233
|
-
i++;
|
|
234
|
-
}
|
|
235
|
-
} else {
|
|
236
|
-
return (0, _errorsJs.throwInternalError)(`Unexpected objectKind entry ${(0, _serializeJs.stringify)(rule)}`);
|
|
192
|
+
return isValid;
|
|
193
|
+
};
|
|
194
|
+
const checkRequiredProp = (prop, state)=>{
|
|
195
|
+
if (prop[0] in state.data) {
|
|
196
|
+
return state.traverseKey(prop[0], prop[1]);
|
|
237
197
|
}
|
|
238
|
-
return
|
|
198
|
+
return state.problems.add("missing", undefined, undefined, {
|
|
199
|
+
path: state.path.concat(prop[0])
|
|
200
|
+
});
|
|
239
201
|
};
|
|
240
|
-
const
|
|
202
|
+
const entryCheckers = {
|
|
241
203
|
regex: _regexJs.checkRegex,
|
|
242
204
|
divisor: _divisorJs.checkDivisor,
|
|
243
|
-
domains: (
|
|
244
|
-
const entries = domains[(0, _domainsJs.domainOf)(data)];
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
205
|
+
domains: (domains, state)=>{
|
|
206
|
+
const entries = domains[(0, _domainsJs.domainOf)(state.data)];
|
|
207
|
+
return entries ? checkEntries(entries, state) : state.problems.add("domainBranches", state.data, (0, _genericsJs.keysOf)(domains));
|
|
208
|
+
},
|
|
209
|
+
// TODO: remove data from problem params
|
|
210
|
+
domain: (domain, state)=>(0, _domainsJs.domainOf)(state.data) === domain || state.problems.add("domain", state.data, domain),
|
|
211
|
+
bound: _rangeJs.checkBound,
|
|
212
|
+
optionalProp: (prop, state)=>{
|
|
213
|
+
if (prop[0] in state.data) {
|
|
214
|
+
return state.traverseKey(prop[0], prop[1]);
|
|
249
215
|
}
|
|
216
|
+
return true;
|
|
250
217
|
},
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
218
|
+
// these checks work the same way, the keys are only distinct so that
|
|
219
|
+
// prerequisite props can have a higher precedence
|
|
220
|
+
requiredProp: checkRequiredProp,
|
|
221
|
+
prerequisiteProp: checkRequiredProp,
|
|
222
|
+
indexProp: (node, state)=>{
|
|
223
|
+
if (!Array.isArray(state.data)) {
|
|
224
|
+
return state.problems.add("class", state.data, "Array");
|
|
254
225
|
}
|
|
226
|
+
let isValid = true;
|
|
227
|
+
for(let i = 0; i < state.data.length; i++){
|
|
228
|
+
isValid && (isValid = state.traverseKey(`${i}`, node));
|
|
229
|
+
if (!isValid && state.failFast) {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return isValid;
|
|
255
234
|
},
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
optionalProps: checkOptionalProps,
|
|
260
|
-
branches: (data, branches, state)=>state.traverseBranches(data, branches),
|
|
261
|
-
switch: (data, rule, state)=>{
|
|
262
|
-
const dataAtPath = (0, _pathsJs.getPath)(data, rule.path);
|
|
235
|
+
branches: (branches, state)=>state.traverseBranches(branches),
|
|
236
|
+
switch: (rule, state)=>{
|
|
237
|
+
const dataAtPath = (0, _pathsJs.getPath)(state.data, rule.path);
|
|
263
238
|
const caseKey = (0, _discriminateJs.serializeCase)(rule.kind, dataAtPath);
|
|
264
239
|
if ((0, _genericsJs.hasKey)(rule.cases, caseKey)) {
|
|
265
|
-
return checkEntries(
|
|
240
|
+
return checkEntries(rule.cases[caseKey], state);
|
|
266
241
|
}
|
|
267
242
|
const caseKeys = (0, _genericsJs.keysOf)(rule.cases);
|
|
268
|
-
const
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
}
|
|
275
|
-
state.path = lastPath;
|
|
243
|
+
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
|
|
248
|
+
});
|
|
276
249
|
},
|
|
277
|
-
alias: (
|
|
250
|
+
alias: (name, state)=>state.traverseResolution(name),
|
|
278
251
|
class: _classJs.checkClass,
|
|
279
|
-
|
|
280
|
-
|
|
252
|
+
// TODO: fix
|
|
253
|
+
narrow: (narrow, state)=>narrow(state.data, state.problems),
|
|
254
|
+
config: ({ config , node }, state)=>{
|
|
281
255
|
state.configs.push(config);
|
|
282
|
-
traverse(
|
|
256
|
+
const result = traverse(node, state);
|
|
283
257
|
state.configs.pop();
|
|
258
|
+
return result;
|
|
284
259
|
},
|
|
285
|
-
value: (
|
|
286
|
-
if (data !== value) {
|
|
287
|
-
state.problems.add("value", data, value);
|
|
288
|
-
}
|
|
289
|
-
}
|
|
260
|
+
value: (value, state)=>state.data === value || state.problems.add("value", state.data, value)
|
|
290
261
|
};
|
|
@@ -11,6 +11,7 @@ function _export(target, all) {
|
|
|
11
11
|
_export(exports, {
|
|
12
12
|
wellFormedIntegerMatcher: ()=>wellFormedIntegerMatcher,
|
|
13
13
|
isWellFormedInteger: ()=>isWellFormedInteger,
|
|
14
|
+
wellFormedNonNegativeIntegerMatcher: ()=>wellFormedNonNegativeIntegerMatcher,
|
|
14
15
|
writeMalformedNumericLiteralMessage: ()=>writeMalformedNumericLiteralMessage,
|
|
15
16
|
tryParseWellFormedNumber: ()=>tryParseWellFormedNumber,
|
|
16
17
|
tryParseWellFormedInteger: ()=>tryParseWellFormedInteger,
|
|
@@ -36,6 +37,7 @@ const numberLikeMatcher = /^-?\d*\.?\d*$/;
|
|
|
36
37
|
const isNumberLike = (s)=>s.length !== 0 && numberLikeMatcher.test(s);
|
|
37
38
|
const wellFormedIntegerMatcher = /^(?:0|(?:-?[1-9]\d*))$/;
|
|
38
39
|
const isWellFormedInteger = (s)=>wellFormedIntegerMatcher.test(s);
|
|
40
|
+
const wellFormedNonNegativeIntegerMatcher = /^(?:0|(?:[1-9]\d*))$/;
|
|
39
41
|
const integerLikeMatcher = /^-?\d+$/;
|
|
40
42
|
const isIntegerLike = (s)=>integerLikeMatcher.test(s);
|
|
41
43
|
const numericLiteralDescriptions = {
|
|
@@ -9,14 +9,13 @@ function _export(target, all) {
|
|
|
9
9
|
});
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
|
+
defaultObjectKinds: ()=>defaultObjectKinds,
|
|
12
13
|
objectKindOf: ()=>objectKindOf,
|
|
13
14
|
hasObjectKind: ()=>hasObjectKind,
|
|
14
15
|
isArray: ()=>isArray,
|
|
15
16
|
objectKindDescriptions: ()=>objectKindDescriptions
|
|
16
17
|
});
|
|
17
18
|
const _domainsTs = require("./domains.js");
|
|
18
|
-
// Built-in object constructors based on a subset of:
|
|
19
|
-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
|
|
20
19
|
const defaultObjectKinds = {
|
|
21
20
|
Array,
|
|
22
21
|
Date,
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { writeImplicitNeverMessage } from "../parse/string/ast.js";
|
|
1
2
|
import { domainOf, hasDomain } from "../utils/domains.js";
|
|
2
|
-
import { throwInternalError } from "../utils/errors.js";
|
|
3
|
+
import { throwInternalError, throwParseError } from "../utils/errors.js";
|
|
3
4
|
import { isDisjoint, isEquality } from "./compose.js";
|
|
4
|
-
import {
|
|
5
|
+
import { flattenRules, rulesIntersection } from "./rules/rules.js";
|
|
5
6
|
export const isBranchComparison = (comparison)=>comparison?.lBranches !== undefined;
|
|
6
7
|
export const compareBranches = (lConditions, rConditions, state)=>{
|
|
7
8
|
const result = {
|
|
@@ -69,3 +70,45 @@ export const compareBranches = (lConditions, rConditions, state)=>{
|
|
|
69
70
|
result.distinctIntersections = pairs.flatMap((pairs)=>pairs.distinct ?? []);
|
|
70
71
|
return result;
|
|
71
72
|
};
|
|
73
|
+
export const isMorphBranch = (branch)=>"morph" in branch;
|
|
74
|
+
export const flattenBranch = (branch, ctx)=>{
|
|
75
|
+
if (isMorphBranch(branch)) {
|
|
76
|
+
const result = flattenRules(branch.rules, ctx);
|
|
77
|
+
if (branch.morph) {
|
|
78
|
+
result.push([
|
|
79
|
+
"morph",
|
|
80
|
+
branch.morph
|
|
81
|
+
]);
|
|
82
|
+
}
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
return flattenRules(branch, ctx);
|
|
86
|
+
};
|
|
87
|
+
const rulesOf = (branch)=>branch.rules ?? branch;
|
|
88
|
+
export const branchIntersection = (l, r, state)=>{
|
|
89
|
+
const lRules = rulesOf(l);
|
|
90
|
+
const rRules = rulesOf(r);
|
|
91
|
+
const rulesResult = rulesIntersection(lRules, rRules, state);
|
|
92
|
+
if ("morph" in l) {
|
|
93
|
+
if ("morph" in r) {
|
|
94
|
+
if (l.morph === r.morph) {
|
|
95
|
+
return isEquality(rulesResult) || isDisjoint(rulesResult) ? rulesResult : {
|
|
96
|
+
rules: rulesResult,
|
|
97
|
+
morph: l.morph
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
return state.lastOperator === "&" ? throwParseError(writeImplicitNeverMessage(state.path, "Intersection", "of morphs")) : {};
|
|
101
|
+
}
|
|
102
|
+
return isDisjoint(rulesResult) ? rulesResult : {
|
|
103
|
+
rules: isEquality(rulesResult) ? l.rules : rulesResult,
|
|
104
|
+
morph: l.morph
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
if ("morph" in r) {
|
|
108
|
+
return isDisjoint(rulesResult) ? rulesResult : {
|
|
109
|
+
rules: isEquality(rulesResult) ? r.rules : rulesResult,
|
|
110
|
+
morph: r.morph
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
return rulesResult;
|
|
114
|
+
};
|
|
@@ -46,9 +46,8 @@ export const composeIntersection = (reducer)=>(l, r, state)=>l === undefined ? r
|
|
|
46
46
|
export const throwUndefinedOperandsError = ()=>throwInternalError(`Unexpected operation two undefined operands`);
|
|
47
47
|
export const disjointDescriptionWriters = {
|
|
48
48
|
domain: ({ l , r })=>`${l.join(", ")} and ${r.join(", ")}`,
|
|
49
|
-
objectKind: ({ l , r })=>`${l} and ${r}`,
|
|
50
49
|
range: ({ l , r })=>`${stringifyRange(l)} and ${stringifyRange(r)}`,
|
|
51
|
-
class: ({ l , r })=>`classes ${l.name} and ${r.name}`,
|
|
50
|
+
class: ({ l , r })=>`classes ${typeof l === "string" ? l : l.name} and ${typeof r === "string" ? r : r.name}`,
|
|
52
51
|
tupleLength: ({ l , r })=>`tuples of length ${l} and ${r}`,
|
|
53
52
|
value: ({ l , r })=>`literal values ${serialize(l)} and ${serialize(r)}`,
|
|
54
53
|
leftAssignability: ({ l , r })=>`literal value ${serialize(l.value)} and ${serialize(r)}`,
|
|
@@ -5,9 +5,9 @@ import { hasKey, isKeyOf, keysOf } 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";
|
|
8
|
+
import { branchIntersection, flattenBranch } from "./branch.js";
|
|
8
9
|
import { IntersectionState } from "./compose.js";
|
|
9
|
-
import {
|
|
10
|
-
import { branchIntersection, flattenBranch } from "./rules/rules.js";
|
|
10
|
+
import { mappedKeys, propToNode } from "./rules/props.js";
|
|
11
11
|
export const flattenBranches = (branches, ctx)=>{
|
|
12
12
|
const discriminants = calculateDiscriminants(branches, ctx);
|
|
13
13
|
const indices = branches.map((_, i)=>i);
|
|
@@ -22,7 +22,7 @@ const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
|
|
|
22
22
|
return [
|
|
23
23
|
[
|
|
24
24
|
"branches",
|
|
25
|
-
remainingIndices.map((i)=>flattenBranch(originalBranches[i], ctx))
|
|
25
|
+
remainingIndices.map((i)=>branchIncludesMorph(originalBranches[i], ctx.type.meta.scope) ? throwParseError(writeUndiscriminatableMorphUnionMessage(`${ctx.path}`)) : flattenBranch(originalBranches[i], ctx))
|
|
26
26
|
]
|
|
27
27
|
];
|
|
28
28
|
}
|
|
@@ -53,7 +53,6 @@ const calculateDiscriminants = (branches, ctx)=>{
|
|
|
53
53
|
disjointsByPair: {},
|
|
54
54
|
casesByDisjoint: {}
|
|
55
55
|
};
|
|
56
|
-
const morphBranches = branches.map((branch)=>branchIncludesMorph(branch, ctx.type.meta.scope));
|
|
57
56
|
for(let lIndex = 0; lIndex < branches.length - 1; lIndex++){
|
|
58
57
|
for(let rIndex = lIndex + 1; rIndex < branches.length; rIndex++){
|
|
59
58
|
const pairKey = `${lIndex}/${rIndex}`;
|
|
@@ -62,8 +61,7 @@ const calculateDiscriminants = (branches, ctx)=>{
|
|
|
62
61
|
const intersectionState = new IntersectionState(ctx.type, "|");
|
|
63
62
|
branchIntersection(branches[lIndex], branches[rIndex], intersectionState);
|
|
64
63
|
for(const path in intersectionState.disjoints){
|
|
65
|
-
|
|
66
|
-
if (path.includes("${")) {
|
|
64
|
+
if (path.includes(mappedKeys.index)) {
|
|
67
65
|
continue;
|
|
68
66
|
}
|
|
69
67
|
const { l , r , kind } = intersectionState.disjoints[path];
|
|
@@ -104,9 +102,6 @@ const calculateDiscriminants = (branches, ctx)=>{
|
|
|
104
102
|
cases[rSerialized].push(rIndex);
|
|
105
103
|
}
|
|
106
104
|
}
|
|
107
|
-
if ((morphBranches[lIndex] === true || morphBranches[rIndex] === true) && pairDisjoints.length === 0) {
|
|
108
|
-
return throwParseError(writeUndiscriminatableMorphUnionMessage(`${ctx.path}`));
|
|
109
|
-
}
|
|
110
105
|
}
|
|
111
106
|
}
|
|
112
107
|
return discriminants;
|
|
@@ -149,8 +144,7 @@ const findBestDiscriminant = (remainingIndices, discriminants)=>{
|
|
|
149
144
|
}
|
|
150
145
|
const defaultCaseKeys = keysOf(defaultCases);
|
|
151
146
|
if (defaultCaseKeys.length) {
|
|
152
|
-
|
|
153
|
-
filteredCases["default"] = defaultIndices;
|
|
147
|
+
filteredCases["default"] = defaultCaseKeys.map((k)=>parseInt(k));
|
|
154
148
|
}
|
|
155
149
|
if (!bestDiscriminant || score > bestDiscriminant.score) {
|
|
156
150
|
const [path, kind] = parseQualifiedDisjoint(qualifiedDisjoint);
|
|
@@ -181,5 +175,5 @@ const caseSerializers = {
|
|
|
181
175
|
domain: domainOf
|
|
182
176
|
};
|
|
183
177
|
export const serializeCase = (kind, data)=>caseSerializers[kind](data);
|
|
184
|
-
const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph(
|
|
178
|
+
const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph(propToNode(prop), $)) : false;
|
|
185
179
|
const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).meta.includesMorph : Object.values(node).some((predicate)=>predicate === true ? false : isArray(predicate) ? predicate.some((branch)=>branchIncludesMorph(branch, $)) : branchIncludesMorph(predicate, $));
|
package/dist/mjs/nodes/node.js
CHANGED
|
@@ -4,6 +4,7 @@ import { hasKey, hasKeys, keysOf } 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";
|
|
7
|
+
import { mappedKeys } from "./rules/props.js";
|
|
7
8
|
export const nodeIntersection = (l, r, state)=>{
|
|
8
9
|
state.domain = undefined;
|
|
9
10
|
const lResolution = state.type.meta.scope.resolveNode(l);
|
|
@@ -43,7 +44,7 @@ export const union = (l, r, type)=>{
|
|
|
43
44
|
}
|
|
44
45
|
return result;
|
|
45
46
|
};
|
|
46
|
-
const hasImpliedDomain = (flatPredicate)=>flatPredicate[0] && (flatPredicate[0][0] === "
|
|
47
|
+
const hasImpliedDomain = (flatPredicate)=>flatPredicate[0] && (flatPredicate[0][0] === "value" || flatPredicate[0][0] === "class");
|
|
47
48
|
export const flattenType = (type)=>{
|
|
48
49
|
const ctx = {
|
|
49
50
|
type,
|
|
@@ -106,3 +107,11 @@ export const flattenNode = (node, ctx)=>{
|
|
|
106
107
|
export const isLiteralNode = (node, domain)=>{
|
|
107
108
|
return resolutionExtendsDomain(node, domain) && isLiteralCondition(node[domain]);
|
|
108
109
|
};
|
|
110
|
+
export const arrayOf = (node)=>({
|
|
111
|
+
object: {
|
|
112
|
+
class: "Array",
|
|
113
|
+
props: {
|
|
114
|
+
[mappedKeys.index]: node
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
});
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { keysOf, listFrom } from "../utils/generics.js";
|
|
2
2
|
import { isArray } from "../utils/objectKinds.js";
|
|
3
|
-
import { compareBranches, isBranchComparison } from "./
|
|
3
|
+
import { branchIntersection, compareBranches, flattenBranch, isBranchComparison } from "./branch.js";
|
|
4
4
|
import { equality, IntersectionState, isEquality } from "./compose.js";
|
|
5
5
|
import { flattenBranches } from "./discriminate.js";
|
|
6
|
-
import
|
|
7
|
-
export const branchIsTransformation = (branch)=>"rules" in branch;
|
|
6
|
+
import "./rules/rules.js";
|
|
8
7
|
const emptyRulesIfTrue = (predicate)=>predicate === true ? {} : predicate;
|
|
9
8
|
export const comparePredicates = (l, r, context)=>{
|
|
10
9
|
if (l === true && r === true) {
|
|
@@ -1,7 +1,13 @@
|
|
|
1
|
+
import { defaultObjectKinds, objectKindOf } from "../../utils/objectKinds.js";
|
|
1
2
|
import { composeIntersection, equality } from "../compose.js";
|
|
2
|
-
export const classIntersection = composeIntersection((l, r, state)=>
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
export const classIntersection = composeIntersection((l, r, state)=>{
|
|
4
|
+
const lClass = typeof l === "string" ? defaultObjectKinds[l] : l;
|
|
5
|
+
const rClass = typeof r === "string" ? defaultObjectKinds[r] : r;
|
|
6
|
+
return lClass === rClass ? equality() : lClass instanceof rClass ? l : rClass instanceof lClass ? r : state.addDisjoint("class", l, r);
|
|
7
|
+
});
|
|
8
|
+
export const checkClass = (expectedClass, state)=>{
|
|
9
|
+
if (typeof expectedClass === "string") {
|
|
10
|
+
return objectKindOf(state.data) === expectedClass || state.problems.add("class", state.data, expectedClass);
|
|
6
11
|
}
|
|
12
|
+
return state.data instanceof expectedClass || state.problems.add("class", state.data, expectedClass);
|
|
7
13
|
};
|
|
@@ -12,8 +12,4 @@ const greatestCommonDivisor = (l, r)=>{
|
|
|
12
12
|
}
|
|
13
13
|
return greatestCommonDivisor;
|
|
14
14
|
};
|
|
15
|
-
export const checkDivisor = (
|
|
16
|
-
if (data % divisor !== 0) {
|
|
17
|
-
state.problems.add("divisor", data, divisor);
|
|
18
|
-
}
|
|
19
|
-
};
|
|
15
|
+
export const checkDivisor = (divisor, state)=>state.data % divisor === 0 || state.problems.add("divisor", state.data, divisor);
|