arktype 0.3.0 → 0.5.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/api.cjs +9 -0
- package/dist/api.d.ts +10 -0
- package/dist/api.mjs +1 -0
- package/dist/cjs/nodes/branch.js +15 -6
- package/dist/cjs/nodes/compose.js +1 -1
- package/dist/cjs/nodes/discriminate.js +89 -21
- package/dist/cjs/nodes/node.js +11 -11
- package/dist/cjs/nodes/predicate.js +1 -1
- package/dist/cjs/nodes/rules/class.js +2 -2
- package/dist/cjs/nodes/rules/divisor.js +1 -1
- package/dist/cjs/nodes/rules/props.js +1 -2
- package/dist/cjs/nodes/rules/range.js +2 -2
- package/dist/cjs/nodes/rules/regex.js +1 -1
- package/dist/cjs/nodes/rules/rules.js +4 -0
- package/dist/cjs/parse/ast/ast.js +4 -0
- package/dist/cjs/parse/ast/bound.js +9 -0
- package/dist/cjs/parse/{tuple/utils.js → ast/distributableFunction.js} +1 -1
- package/dist/cjs/parse/ast/divisor.js +9 -0
- package/dist/cjs/parse/{string/ast.js → ast/intersection.js} +2 -8
- package/dist/cjs/parse/ast/keyof.js +109 -0
- package/dist/cjs/parse/{tuple → ast}/morph.js +1 -1
- package/dist/cjs/parse/{tuple → ast}/narrow.js +2 -2
- package/dist/cjs/parse/{tuple → ast}/tuple.js +11 -10
- package/dist/cjs/parse/ast/union.js +9 -0
- package/dist/cjs/parse/ast/utils.js +4 -0
- package/dist/cjs/parse/definition.js +12 -2
- package/dist/cjs/parse/string/reduce/dynamic.js +7 -7
- package/dist/cjs/parse/string/shift/operator/bounds.js +4 -4
- package/dist/cjs/parse/string/shift/operator/divisor.js +3 -3
- package/dist/cjs/parse/string/string.js +1 -1
- package/dist/cjs/scopes/expressions.js +29 -0
- package/dist/cjs/scopes/scope.js +59 -6
- package/dist/cjs/scopes/validation.js +16 -1
- package/dist/cjs/traverse/problems.js +78 -46
- package/dist/cjs/traverse/traverse.js +62 -58
- package/dist/cjs/utils/data.js +39 -0
- package/dist/cjs/utils/freeze.js +2 -3
- package/dist/cjs/utils/generics.js +20 -2
- package/dist/cjs/utils/numericLiterals.js +2 -14
- package/dist/mjs/nodes/branch.js +14 -5
- package/dist/mjs/nodes/compose.js +2 -2
- package/dist/mjs/nodes/discriminate.js +89 -21
- package/dist/mjs/nodes/node.js +8 -8
- package/dist/mjs/nodes/predicate.js +2 -2
- package/dist/mjs/nodes/rules/class.js +2 -2
- package/dist/mjs/nodes/rules/divisor.js +1 -1
- package/dist/mjs/nodes/rules/props.js +1 -2
- package/dist/mjs/nodes/rules/range.js +2 -2
- package/dist/mjs/nodes/rules/regex.js +1 -1
- package/dist/mjs/nodes/rules/rules.js +4 -0
- package/dist/mjs/parse/ast/ast.js +1 -0
- package/dist/mjs/parse/ast/bound.js +1 -0
- package/dist/mjs/parse/{tuple/utils.js → ast/distributableFunction.js} +2 -2
- package/dist/mjs/parse/ast/divisor.js +1 -0
- package/dist/mjs/parse/{string/ast.js → ast/intersection.js} +2 -5
- package/dist/mjs/parse/ast/keyof.js +101 -0
- package/dist/mjs/parse/{tuple → ast}/morph.js +1 -1
- package/dist/mjs/parse/ast/narrow.js +7 -0
- package/dist/mjs/parse/{tuple → ast}/tuple.js +12 -11
- package/dist/mjs/parse/ast/union.js +1 -0
- package/dist/mjs/parse/ast/utils.js +1 -0
- package/dist/mjs/parse/definition.js +12 -2
- package/dist/mjs/parse/string/reduce/dynamic.js +8 -8
- package/dist/mjs/parse/string/shift/operator/bounds.js +4 -4
- package/dist/mjs/parse/string/shift/operator/divisor.js +3 -3
- package/dist/mjs/parse/string/string.js +2 -2
- package/dist/mjs/scopes/expressions.js +9 -0
- package/dist/mjs/scopes/scope.js +58 -5
- package/dist/mjs/scopes/validation.js +16 -1
- package/dist/mjs/traverse/problems.js +76 -44
- package/dist/mjs/traverse/traverse.js +37 -34
- package/dist/mjs/utils/data.js +24 -0
- package/dist/mjs/utils/freeze.js +2 -3
- package/dist/mjs/utils/generics.js +18 -1
- package/dist/mjs/utils/numericLiterals.js +1 -1
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/nodes/branch.d.ts +3 -12
- package/dist/types/nodes/discriminate.d.ts +6 -8
- package/dist/types/nodes/infer.d.ts +5 -5
- package/dist/types/nodes/node.d.ts +4 -13
- package/dist/types/nodes/predicate.d.ts +2 -29
- package/dist/types/nodes/rules/range.d.ts +2 -2
- package/dist/types/nodes/rules/rules.d.ts +1 -1
- package/dist/types/parse/ast/ast.d.ts +24 -0
- package/dist/types/parse/ast/bound.d.ts +10 -0
- package/dist/types/parse/{tuple/utils.d.ts → ast/distributableFunction.d.ts} +2 -3
- package/dist/types/parse/ast/divisor.d.ts +8 -0
- package/dist/types/parse/ast/intersection.d.ts +17 -0
- package/dist/types/parse/{tuple → ast}/keyof.d.ts +4 -4
- package/dist/types/parse/ast/morph.d.ts +18 -0
- package/dist/types/parse/ast/narrow.d.ts +21 -0
- package/dist/types/parse/ast/tuple.d.ts +40 -0
- package/dist/types/parse/ast/union.d.ts +12 -0
- package/dist/types/parse/ast/utils.d.ts +7 -0
- package/dist/types/parse/definition.d.ts +9 -7
- package/dist/types/parse/record.d.ts +2 -2
- package/dist/types/parse/string/reduce/shared.d.ts +5 -4
- package/dist/types/parse/string/reduce/static.d.ts +11 -6
- package/dist/types/parse/string/shift/operand/unenclosed.d.ts +1 -1
- package/dist/types/parse/string/shift/operator/bounds.d.ts +3 -2
- package/dist/types/parse/string/shift/operator/divisor.d.ts +5 -1
- package/dist/types/parse/string/shift/scanner.d.ts +1 -1
- package/dist/types/parse/string/string.d.ts +1 -2
- package/dist/types/scopes/expressions.d.ts +48 -0
- package/dist/types/scopes/scope.d.ts +17 -7
- package/dist/types/scopes/standard.d.ts +15 -0
- package/dist/types/scopes/type.d.ts +9 -5
- package/dist/types/scopes/validation.d.ts +4 -0
- package/dist/types/traverse/problems.d.ts +17 -24
- package/dist/types/traverse/traverse.d.ts +67 -37
- package/dist/types/utils/data.d.ts +13 -0
- package/dist/types/utils/domains.d.ts +1 -1
- package/dist/types/utils/generics.d.ts +14 -10
- package/dist/types/utils/numericLiterals.d.ts +16 -0
- package/dist/types/utils/paths.d.ts +2 -0
- package/package.json +1 -1
- package/dist/cjs/parse/tuple/keyof.js +0 -64
- package/dist/cjs/utils/size.js +0 -16
- package/dist/mjs/parse/tuple/keyof.js +0 -56
- package/dist/mjs/parse/tuple/narrow.js +0 -7
- package/dist/mjs/utils/size.js +0 -2
- package/dist/types/parse/string/ast.d.ts +0 -46
- package/dist/types/parse/tuple/morph.d.ts +0 -15
- package/dist/types/parse/tuple/narrow.d.ts +0 -13
- package/dist/types/parse/tuple/tuple.d.ts +0 -31
- package/dist/types/utils/size.d.ts +0 -3
|
@@ -14,17 +14,17 @@ _export(exports, {
|
|
|
14
14
|
checkEntries: ()=>checkEntries,
|
|
15
15
|
checkRequiredProp: ()=>checkRequiredProp
|
|
16
16
|
});
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
const
|
|
17
|
+
const _discriminateTs = require("../nodes/discriminate.js");
|
|
18
|
+
const _classTs = require("../nodes/rules/class.js");
|
|
19
|
+
const _divisorTs = require("../nodes/rules/divisor.js");
|
|
20
|
+
const _rangeTs = require("../nodes/rules/range.js");
|
|
21
|
+
const _regexTs = require("../nodes/rules/regex.js");
|
|
22
|
+
const _rulesTs = require("../nodes/rules/rules.js");
|
|
23
|
+
const _domainsTs = require("../utils/domains.js");
|
|
24
|
+
const _errorsTs = require("../utils/errors.js");
|
|
25
|
+
const _genericsTs = require("../utils/generics.js");
|
|
26
|
+
const _pathsTs = require("../utils/paths.js");
|
|
27
|
+
const _problemsTs = require("./problems.js");
|
|
28
28
|
function _checkPrivateRedeclaration(obj, privateCollection) {
|
|
29
29
|
if (privateCollection.has(obj)) {
|
|
30
30
|
throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
@@ -69,23 +69,22 @@ var _seen = /*#__PURE__*/ new WeakMap();
|
|
|
69
69
|
class TraversalState {
|
|
70
70
|
getConfigForProblemCode(code) {
|
|
71
71
|
if (!this.configs.length) {
|
|
72
|
-
return
|
|
72
|
+
return _problemsTs.defaultProblemWriters[code];
|
|
73
73
|
}
|
|
74
74
|
for(let i = this.configs.length - 1; i >= 0; i--){
|
|
75
75
|
if (this.configs[i][code] || this.configs[i]["defaults"]) {
|
|
76
76
|
return {
|
|
77
|
-
...
|
|
77
|
+
..._problemsTs.defaultProblemWriters[code],
|
|
78
78
|
...this.configs[i]["defaults"],
|
|
79
79
|
...this.configs[i][code]
|
|
80
80
|
};
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
|
-
return
|
|
83
|
+
return _problemsTs.defaultProblemWriters[code];
|
|
84
84
|
}
|
|
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();
|
|
@@ -98,16 +97,17 @@ class TraversalState {
|
|
|
98
97
|
const id = resolution.meta.id;
|
|
99
98
|
// this assignment helps with narrowing
|
|
100
99
|
const data = this.data;
|
|
101
|
-
const isObject = (0,
|
|
100
|
+
const isObject = (0, _domainsTs.hasDomain)(data, "object");
|
|
102
101
|
if (isObject) {
|
|
103
|
-
|
|
104
|
-
|
|
102
|
+
const seenByCurrentType = _classPrivateFieldGet(this, _seen)[id];
|
|
103
|
+
if (seenByCurrentType) {
|
|
104
|
+
if (seenByCurrentType.includes(data)) {
|
|
105
105
|
// if data has already been checked by this alias as part of
|
|
106
106
|
// a resolution higher up on the call stack, it must be valid
|
|
107
107
|
// or we wouldn't be here
|
|
108
108
|
return true;
|
|
109
109
|
}
|
|
110
|
-
|
|
110
|
+
seenByCurrentType.push(data);
|
|
111
111
|
} else {
|
|
112
112
|
_classPrivateFieldGet(this, _seen)[id] = [
|
|
113
113
|
data
|
|
@@ -127,12 +127,12 @@ class TraversalState {
|
|
|
127
127
|
const lastFailFast = this.failFast;
|
|
128
128
|
this.failFast = true;
|
|
129
129
|
const lastProblems = this.problems;
|
|
130
|
-
const branchProblems = new
|
|
130
|
+
const branchProblems = new _problemsTs.Problems(this);
|
|
131
131
|
this.problems = branchProblems;
|
|
132
132
|
const lastPath = this.path;
|
|
133
133
|
let hasValidBranch = false;
|
|
134
134
|
for (const branch of branches){
|
|
135
|
-
this.path = new
|
|
135
|
+
this.path = new _pathsTs.Path();
|
|
136
136
|
if (checkEntries(branch, this)) {
|
|
137
137
|
hasValidBranch = true;
|
|
138
138
|
break;
|
|
@@ -141,7 +141,7 @@ class TraversalState {
|
|
|
141
141
|
this.path = lastPath;
|
|
142
142
|
this.problems = lastProblems;
|
|
143
143
|
this.failFast = lastFailFast;
|
|
144
|
-
return hasValidBranch || this.problems.add("branches",
|
|
144
|
+
return hasValidBranch || !this.problems.add("branches", branchProblems);
|
|
145
145
|
}
|
|
146
146
|
constructor(data, type){
|
|
147
147
|
_classPrivateFieldInit(this, _seen, {
|
|
@@ -150,8 +150,8 @@ class TraversalState {
|
|
|
150
150
|
});
|
|
151
151
|
this.data = data;
|
|
152
152
|
this.type = type;
|
|
153
|
-
this.path = new
|
|
154
|
-
this.problems = new
|
|
153
|
+
this.path = new _pathsTs.Path();
|
|
154
|
+
this.problems = new _problemsTs.Problems(this);
|
|
155
155
|
this.failFast = false;
|
|
156
156
|
_classPrivateFieldSet(this, _seen, {});
|
|
157
157
|
this.configs = type.meta.scope.config ? [
|
|
@@ -159,29 +159,18 @@ class TraversalState {
|
|
|
159
159
|
] : [];
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
|
-
const traverse = (node, state)=>typeof node === "string" ? (0,
|
|
162
|
+
const traverse = (node, state)=>typeof node === "string" ? (0, _domainsTs.domainOf)(state.data) === node || !state.problems.add("domain", node) : checkEntries(node, state);
|
|
163
163
|
const checkEntries = (entries, state)=>{
|
|
164
164
|
let isValid = true;
|
|
165
165
|
for(let i = 0; i < entries.length; i++){
|
|
166
166
|
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
167
|
const entryAllowsData = entryCheckers[k](v, state);
|
|
179
168
|
isValid && (isValid = entryAllowsData);
|
|
180
169
|
if (!isValid) {
|
|
181
170
|
if (state.failFast) {
|
|
182
171
|
return false;
|
|
183
172
|
}
|
|
184
|
-
if (i < entries.length - 1 &&
|
|
173
|
+
if (i < entries.length - 1 && _rulesTs.precedenceMap[k] < _rulesTs.precedenceMap[entries[i + 1][0]]) {
|
|
185
174
|
// if we've encountered a problem, there is at least one entry
|
|
186
175
|
// remaining, and the next entry is of a higher precedence level
|
|
187
176
|
// than the current entry, return immediately
|
|
@@ -195,20 +184,21 @@ const checkRequiredProp = (prop, state)=>{
|
|
|
195
184
|
if (prop[0] in state.data) {
|
|
196
185
|
return state.traverseKey(prop[0], prop[1]);
|
|
197
186
|
}
|
|
198
|
-
|
|
199
|
-
path: state.path.concat(prop[0])
|
|
187
|
+
state.problems.add("missing", undefined, {
|
|
188
|
+
path: state.path.concat(prop[0]),
|
|
189
|
+
data: undefined
|
|
200
190
|
});
|
|
191
|
+
return false;
|
|
201
192
|
};
|
|
202
193
|
const entryCheckers = {
|
|
203
|
-
regex:
|
|
204
|
-
divisor:
|
|
194
|
+
regex: _regexTs.checkRegex,
|
|
195
|
+
divisor: _divisorTs.checkDivisor,
|
|
205
196
|
domains: (domains, state)=>{
|
|
206
|
-
const entries = domains[(0,
|
|
207
|
-
return entries ? checkEntries(entries, state) : state.problems.add("
|
|
197
|
+
const entries = domains[(0, _domainsTs.domainOf)(state.data)];
|
|
198
|
+
return entries ? checkEntries(entries, state) : !state.problems.add("cases", (0, _problemsTs.domainsToDescriptions)((0, _genericsTs.objectKeysOf)(domains)));
|
|
208
199
|
},
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
bound: _rangeJs.checkBound,
|
|
200
|
+
domain: (domain, state)=>(0, _domainsTs.domainOf)(state.data) === domain || !state.problems.add("domain", domain),
|
|
201
|
+
bound: _rangeTs.checkBound,
|
|
212
202
|
optionalProp: (prop, state)=>{
|
|
213
203
|
if (prop[0] in state.data) {
|
|
214
204
|
return state.traverseKey(prop[0], prop[1]);
|
|
@@ -221,7 +211,8 @@ const entryCheckers = {
|
|
|
221
211
|
prerequisiteProp: checkRequiredProp,
|
|
222
212
|
indexProp: (node, state)=>{
|
|
223
213
|
if (!Array.isArray(state.data)) {
|
|
224
|
-
|
|
214
|
+
state.problems.add("class", "Array");
|
|
215
|
+
return false;
|
|
225
216
|
}
|
|
226
217
|
let isValid = true;
|
|
227
218
|
for(let i = 0; i < state.data.length; i++){
|
|
@@ -234,22 +225,22 @@ const entryCheckers = {
|
|
|
234
225
|
},
|
|
235
226
|
branches: (branches, state)=>state.traverseBranches(branches),
|
|
236
227
|
switch: (rule, state)=>{
|
|
237
|
-
const dataAtPath = (0,
|
|
238
|
-
const caseKey = (0,
|
|
239
|
-
if ((0,
|
|
228
|
+
const dataAtPath = (0, _pathsTs.getPath)(state.data, rule.path);
|
|
229
|
+
const caseKey = (0, _discriminateTs.serializeCase)(rule.kind, dataAtPath);
|
|
230
|
+
if ((0, _genericsTs.hasKey)(rule.cases, caseKey)) {
|
|
240
231
|
return checkEntries(rule.cases[caseKey], state);
|
|
241
232
|
}
|
|
242
|
-
const caseKeys = (0,
|
|
233
|
+
const caseKeys = (0, _genericsTs.objectKeysOf)(rule.cases);
|
|
243
234
|
const missingCasePath = state.path.concat(rule.path);
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
235
|
+
const caseDescriptions = rule.kind === "value" ? caseKeys : rule.kind === "domain" ? (0, _problemsTs.domainsToDescriptions)(caseKeys) : rule.kind === "class" ? (0, _problemsTs.objectKindsToDescriptions)(caseKeys) : (0, _errorsTs.throwInternalError)(`Unexpectedly encountered rule kind '${rule.kind}' during traversal`);
|
|
236
|
+
state.problems.add("cases", caseDescriptions, {
|
|
237
|
+
path: missingCasePath,
|
|
238
|
+
data: dataAtPath
|
|
248
239
|
});
|
|
240
|
+
return false;
|
|
249
241
|
},
|
|
250
242
|
alias: (name, state)=>state.traverseResolution(name),
|
|
251
|
-
class:
|
|
252
|
-
// TODO: fix
|
|
243
|
+
class: _classTs.checkClass,
|
|
253
244
|
narrow: (narrow, state)=>narrow(state.data, state.problems),
|
|
254
245
|
config: ({ config , node }, state)=>{
|
|
255
246
|
state.configs.push(config);
|
|
@@ -257,5 +248,18 @@ const entryCheckers = {
|
|
|
257
248
|
state.configs.pop();
|
|
258
249
|
return result;
|
|
259
250
|
},
|
|
260
|
-
value: (value, state)=>state.data === value || state.problems.add("value",
|
|
251
|
+
value: (value, state)=>state.data === value || !state.problems.add("value", value),
|
|
252
|
+
morph: (morph, state)=>{
|
|
253
|
+
const out = morph(state.data, state.problems);
|
|
254
|
+
if (state.problems.length) {
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
if (out instanceof _problemsTs.Problem) {
|
|
258
|
+
// if a problem was returned from the morph but not added, add it
|
|
259
|
+
state.problems.addProblem(out);
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
state.data = out;
|
|
263
|
+
return true;
|
|
264
|
+
}
|
|
261
265
|
};
|
|
@@ -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
|
+
}
|
package/dist/cjs/utils/freeze.js
CHANGED
|
@@ -8,9 +8,8 @@ Object.defineProperty(exports, "deepFreeze", {
|
|
|
8
8
|
});
|
|
9
9
|
const deepFreeze = (value)=>Object.isFrozen(value) ? value : Array.isArray(value) ? Object.freeze(value.map(deepFreeze)) : deepFreezeDictionary(value);
|
|
10
10
|
const deepFreezeDictionary = (value)=>{
|
|
11
|
-
const result = {};
|
|
12
11
|
for(const k in value){
|
|
13
|
-
|
|
12
|
+
deepFreeze(value[k]);
|
|
14
13
|
}
|
|
15
|
-
return
|
|
14
|
+
return value;
|
|
16
15
|
};
|
|
@@ -12,7 +12,8 @@ _export(exports, {
|
|
|
12
12
|
asConst: ()=>asConst,
|
|
13
13
|
isKeyOf: ()=>isKeyOf,
|
|
14
14
|
entriesOf: ()=>entriesOf,
|
|
15
|
-
|
|
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
|
|
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;
|
|
@@ -9,6 +9,7 @@ function _export(target, all) {
|
|
|
9
9
|
});
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
|
+
wellFormedNumberMatcher: ()=>wellFormedNumberMatcher,
|
|
12
13
|
wellFormedIntegerMatcher: ()=>wellFormedIntegerMatcher,
|
|
13
14
|
isWellFormedInteger: ()=>isWellFormedInteger,
|
|
14
15
|
wellFormedNonNegativeIntegerMatcher: ()=>wellFormedNonNegativeIntegerMatcher,
|
|
@@ -18,20 +19,7 @@ _export(exports, {
|
|
|
18
19
|
tryParseWellFormedBigint: ()=>tryParseWellFormedBigint
|
|
19
20
|
});
|
|
20
21
|
const _errorsTs = require("./errors.js");
|
|
21
|
-
|
|
22
|
-
* The goal of the number literal and bigint literal regular expressions is to:
|
|
23
|
-
*
|
|
24
|
-
* 1. Ensure definitions form a bijection with the values they represent.
|
|
25
|
-
* 2. Attempt to mirror TypeScript's own format for stringification of numeric
|
|
26
|
-
* values such that the regex should match a given definition if any only if
|
|
27
|
-
* a precise literal type will be inferred (in TS4.8+).
|
|
28
|
-
*/ /**
|
|
29
|
-
* Matches a well-formatted numeric expression according to the following rules:
|
|
30
|
-
* 1. Must include an integer portion (i.e. '.321' must be written as '0.321')
|
|
31
|
-
* 2. The first digit of the value must not be 0, unless the entire integer portion is 0
|
|
32
|
-
* 3. If the value includes a decimal, its last digit may not be 0
|
|
33
|
-
* 4. The value may not be "-0"
|
|
34
|
-
*/ const wellFormedNumberMatcher = /^(?!^-0$)-?(?:0|[1-9]\d*)(?:\.\d*[1-9])?$/;
|
|
22
|
+
const wellFormedNumberMatcher = /^(?!^-0$)-?(?:0|[1-9]\d*)(?:\.\d*[1-9])?$/;
|
|
35
23
|
const isWellFormedNumber = (s)=>wellFormedNumberMatcher.test(s);
|
|
36
24
|
const numberLikeMatcher = /^-?\d*\.?\d*$/;
|
|
37
25
|
const isNumberLike = (s)=>s.length !== 0 && numberLikeMatcher.test(s);
|
package/dist/mjs/nodes/branch.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { writeImplicitNeverMessage } from "../parse/
|
|
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
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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 {
|
|
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 =
|
|
90
|
+
const keys = objectKeysOf({
|
|
91
91
|
...l,
|
|
92
92
|
...r
|
|
93
93
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { writeUndiscriminatableMorphUnionMessage } from "../parse/
|
|
1
|
+
import { writeUndiscriminatableMorphUnionMessage } from "../parse/ast/union.js";
|
|
2
2
|
import { domainOf } from "../utils/domains.js";
|
|
3
|
-
import { throwParseError } from "../utils/errors.js";
|
|
4
|
-
import {
|
|
3
|
+
import { throwInternalError, throwParseError } from "../utils/errors.js";
|
|
4
|
+
import { isKeyOf, keyCount, 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";
|
|
@@ -27,10 +27,12 @@ const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
|
|
|
27
27
|
];
|
|
28
28
|
}
|
|
29
29
|
const cases = {};
|
|
30
|
-
|
|
31
|
-
for(caseKey in bestDiscriminant.indexCases){
|
|
30
|
+
for(const caseKey in bestDiscriminant.indexCases){
|
|
32
31
|
const nextIndices = bestDiscriminant.indexCases[caseKey];
|
|
33
32
|
cases[caseKey] = discriminate(originalBranches, nextIndices, discriminants, ctx);
|
|
33
|
+
if (caseKey !== "default") {
|
|
34
|
+
pruneDiscriminant(cases[caseKey], bestDiscriminant.path, bestDiscriminant, ctx);
|
|
35
|
+
}
|
|
34
36
|
}
|
|
35
37
|
return [
|
|
36
38
|
[
|
|
@@ -43,9 +45,63 @@ const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
|
|
|
43
45
|
]
|
|
44
46
|
];
|
|
45
47
|
};
|
|
48
|
+
const pruneDiscriminant = (entries, segments, discriminant, ctx)=>{
|
|
49
|
+
for(let i = 0; i < entries.length; i++){
|
|
50
|
+
const [k, v] = entries[i];
|
|
51
|
+
if (!segments.length) {
|
|
52
|
+
if (discriminant.kind === "domain") {
|
|
53
|
+
if (k === "domain" || k === "domains") {
|
|
54
|
+
entries.splice(i, 1);
|
|
55
|
+
return;
|
|
56
|
+
} else if (k === "class" || k === "value") {
|
|
57
|
+
// these keys imply a domain, but also add additional
|
|
58
|
+
// information, so can't be pruned
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
} else if (discriminant.kind === k) {
|
|
62
|
+
entries.splice(i, 1);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
} else if ((k === "requiredProp" || k === "prerequisiteProp" || k === "optionalProp") && v[0] === segments[0]) {
|
|
66
|
+
if (typeof v[1] === "string") {
|
|
67
|
+
if (discriminant.kind !== "domain") {
|
|
68
|
+
return throwPruneFailure(discriminant);
|
|
69
|
+
}
|
|
70
|
+
entries.splice(i, 1);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
pruneDiscriminant(v[1], segments.slice(1), discriminant, ctx);
|
|
74
|
+
if (v[1].length === 0) {
|
|
75
|
+
entries.splice(i, 1);
|
|
76
|
+
}
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
// check for branch keys, which must be traversed even if there are no
|
|
80
|
+
// segments left
|
|
81
|
+
if (k === "domains") {
|
|
82
|
+
if (keyCount(v) !== 1 || !v.object) {
|
|
83
|
+
return throwPruneFailure(discriminant);
|
|
84
|
+
}
|
|
85
|
+
pruneDiscriminant(v.object, segments, discriminant, ctx);
|
|
86
|
+
return;
|
|
87
|
+
} else if (k === "switch") {
|
|
88
|
+
for(const caseKey in v.cases){
|
|
89
|
+
pruneDiscriminant(v.cases[caseKey], segments, discriminant, ctx);
|
|
90
|
+
}
|
|
91
|
+
return;
|
|
92
|
+
} else if (k === "branches") {
|
|
93
|
+
for (const branch of v){
|
|
94
|
+
pruneDiscriminant(branch, segments, discriminant, ctx);
|
|
95
|
+
}
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return throwPruneFailure(discriminant);
|
|
100
|
+
};
|
|
101
|
+
const throwPruneFailure = (discriminant)=>throwInternalError(`Unexpectedly failed to discriminate ${discriminant.kind} at path '${discriminant.path}'`);
|
|
46
102
|
const discriminantKinds = {
|
|
47
103
|
domain: true,
|
|
48
|
-
|
|
104
|
+
class: true,
|
|
49
105
|
value: true
|
|
50
106
|
};
|
|
51
107
|
const calculateDiscriminants = (branches, ctx)=>{
|
|
@@ -68,8 +124,8 @@ const calculateDiscriminants = (branches, ctx)=>{
|
|
|
68
124
|
if (!isKeyOf(kind, discriminantKinds)) {
|
|
69
125
|
continue;
|
|
70
126
|
}
|
|
71
|
-
const lSerialized =
|
|
72
|
-
const rSerialized =
|
|
127
|
+
const lSerialized = serializeDefinitionIfAllowed(kind, l);
|
|
128
|
+
const rSerialized = serializeDefinitionIfAllowed(kind, r);
|
|
73
129
|
if (lSerialized === undefined || rSerialized === undefined) {
|
|
74
130
|
continue;
|
|
75
131
|
}
|
|
@@ -87,19 +143,21 @@ const calculateDiscriminants = (branches, ctx)=>{
|
|
|
87
143
|
continue;
|
|
88
144
|
}
|
|
89
145
|
const cases = discriminants.casesByDisjoint[qualifiedDisjoint];
|
|
90
|
-
|
|
146
|
+
const existingLBranch = cases[lSerialized];
|
|
147
|
+
if (!existingLBranch) {
|
|
91
148
|
cases[lSerialized] = [
|
|
92
149
|
lIndex
|
|
93
150
|
];
|
|
94
|
-
} else if (!
|
|
95
|
-
|
|
151
|
+
} else if (!existingLBranch.includes(lIndex)) {
|
|
152
|
+
existingLBranch.push(lIndex);
|
|
96
153
|
}
|
|
97
|
-
|
|
154
|
+
const existingRBranch = cases[rSerialized];
|
|
155
|
+
if (!existingRBranch) {
|
|
98
156
|
cases[rSerialized] = [
|
|
99
157
|
rIndex
|
|
100
158
|
];
|
|
101
|
-
} else if (!
|
|
102
|
-
|
|
159
|
+
} else if (!existingRBranch.includes(rIndex)) {
|
|
160
|
+
existingRBranch.push(rIndex);
|
|
103
161
|
}
|
|
104
162
|
}
|
|
105
163
|
}
|
|
@@ -127,8 +185,7 @@ const findBestDiscriminant = (remainingIndices, discriminants)=>{
|
|
|
127
185
|
...remainingIndices
|
|
128
186
|
];
|
|
129
187
|
let score = 0;
|
|
130
|
-
|
|
131
|
-
for(caseKey in indexCases){
|
|
188
|
+
for(const caseKey in indexCases){
|
|
132
189
|
const filteredIndices = indexCases[caseKey].filter((i)=>{
|
|
133
190
|
const remainingIndex = remainingIndices.indexOf(i);
|
|
134
191
|
if (remainingIndex !== -1) {
|
|
@@ -142,7 +199,7 @@ const findBestDiscriminant = (remainingIndices, discriminants)=>{
|
|
|
142
199
|
filteredCases[caseKey] = filteredIndices;
|
|
143
200
|
score++;
|
|
144
201
|
}
|
|
145
|
-
const defaultCaseKeys =
|
|
202
|
+
const defaultCaseKeys = objectKeysOf(defaultCases);
|
|
146
203
|
if (defaultCaseKeys.length) {
|
|
147
204
|
filteredCases["default"] = defaultCaseKeys.map((k)=>parseInt(k));
|
|
148
205
|
}
|
|
@@ -164,16 +221,27 @@ const findBestDiscriminant = (remainingIndices, discriminants)=>{
|
|
|
164
221
|
}
|
|
165
222
|
return bestDiscriminant;
|
|
166
223
|
};
|
|
167
|
-
export const
|
|
224
|
+
export const serializeDefinitionIfAllowed = (kind, definition)=>{
|
|
225
|
+
switch(kind){
|
|
226
|
+
case "value":
|
|
227
|
+
return serializeIfPrimitive(definition);
|
|
228
|
+
case "domain":
|
|
229
|
+
return definition;
|
|
230
|
+
case "class":
|
|
231
|
+
return typeof definition === "string" ? definition : undefined;
|
|
232
|
+
default:
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
};
|
|
168
236
|
const serializeIfPrimitive = (data)=>{
|
|
169
237
|
const domain = domainOf(data);
|
|
170
238
|
return domain === "object" || domain === "symbol" ? undefined : serializePrimitive(data);
|
|
171
239
|
};
|
|
172
|
-
const
|
|
240
|
+
const serializeData = {
|
|
173
241
|
value: (data)=>serializeIfPrimitive(data) ?? "default",
|
|
174
|
-
|
|
242
|
+
class: (data)=>objectKindOf(data) ?? "default",
|
|
175
243
|
domain: domainOf
|
|
176
244
|
};
|
|
177
|
-
export const serializeCase = (kind, data)=>
|
|
245
|
+
export const serializeCase = (kind, data)=>serializeData[kind](data);
|
|
178
246
|
const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph(propToNode(prop), $)) : false;
|
|
179
247
|
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
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { compileDisjointReasonsMessage } from "../parse/
|
|
1
|
+
import { compileDisjointReasonsMessage } from "../parse/ast/intersection.js";
|
|
2
2
|
import { throwParseError } from "../utils/errors.js";
|
|
3
|
-
import { hasKey, hasKeys,
|
|
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",
|
|
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
|
|
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
|
|
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 =
|
|
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 =
|
|
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
|
|
110
|
+
export const toArrayNode = (node)=>({
|
|
111
111
|
object: {
|
|
112
112
|
class: "Array",
|
|
113
113
|
props: {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
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 =
|
|
72
|
+
const domains = objectKeysOf(resolution);
|
|
73
73
|
return domains.length === 1 && domains[0] === domain;
|
|
74
74
|
};
|