arktype 0.6.0 → 1.0.1-alpha
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/README.md +4 -2
- package/dist/api.cjs +2 -2
- package/dist/api.d.ts +2 -1
- package/dist/api.mjs +1 -1
- package/dist/cjs/nodes/compose.js +4 -4
- package/dist/cjs/nodes/discriminate.js +1 -1
- package/dist/cjs/nodes/node.js +30 -22
- package/dist/cjs/nodes/predicate.js +0 -1
- package/dist/cjs/nodes/rules/range.js +3 -3
- package/dist/cjs/parse/ast/config.js +4 -6
- package/dist/cjs/parse/ast/distributableFunction.js +6 -6
- package/dist/cjs/parse/ast/intersection.js +2 -2
- package/dist/cjs/parse/ast/keyof.js +3 -3
- package/dist/cjs/parse/ast/morph.js +2 -1
- package/dist/cjs/parse/ast/narrow.js +8 -1
- package/dist/cjs/parse/definition.js +2 -2
- package/dist/cjs/parse/string/reduce/dynamic.js +1 -1
- package/dist/cjs/parse/string/shift/operand/unenclosed.js +1 -1
- package/dist/cjs/parse/string/shift/scanner.js +1 -1
- package/dist/cjs/parse/string/string.js +2 -2
- package/dist/cjs/scopes/ark.js +6 -6
- package/dist/cjs/scopes/scope.js +47 -59
- package/dist/cjs/scopes/type.js +19 -14
- package/dist/cjs/scopes/validation/date.js +2 -3
- package/dist/cjs/scopes/validation/validation.js +14 -0
- package/dist/cjs/traverse/problems.js +3 -3
- package/dist/cjs/traverse/traverse.js +21 -3
- package/dist/cjs/utils/data.js +1 -1
- package/dist/cjs/utils/generics.js +16 -0
- package/dist/cjs/utils/serialize.js +29 -53
- package/dist/mjs/nodes/compose.js +5 -5
- package/dist/mjs/nodes/discriminate.js +1 -1
- package/dist/mjs/nodes/node.js +28 -22
- package/dist/mjs/nodes/predicate.js +0 -1
- package/dist/mjs/nodes/rules/range.js +3 -3
- package/dist/mjs/parse/ast/config.js +4 -6
- package/dist/mjs/parse/ast/distributableFunction.js +6 -6
- package/dist/mjs/parse/ast/intersection.js +2 -2
- package/dist/mjs/parse/ast/keyof.js +3 -3
- package/dist/mjs/parse/ast/morph.js +2 -1
- package/dist/mjs/parse/ast/narrow.js +9 -2
- package/dist/mjs/parse/definition.js +2 -2
- package/dist/mjs/parse/string/reduce/dynamic.js +1 -1
- package/dist/mjs/parse/string/shift/operand/unenclosed.js +1 -1
- package/dist/mjs/parse/string/shift/scanner.js +1 -1
- package/dist/mjs/parse/string/string.js +2 -2
- package/dist/mjs/scopes/ark.js +5 -5
- package/dist/mjs/scopes/scope.js +48 -60
- package/dist/mjs/scopes/type.js +16 -13
- package/dist/mjs/scopes/validation/date.js +2 -3
- package/dist/mjs/scopes/validation/validation.js +14 -0
- package/dist/mjs/traverse/problems.js +3 -3
- package/dist/mjs/traverse/traverse.js +21 -3
- package/dist/mjs/utils/data.js +1 -1
- package/dist/mjs/utils/generics.js +11 -0
- package/dist/mjs/utils/serialize.js +29 -52
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/nodes/branch.d.ts +5 -3
- package/dist/types/nodes/node.d.ts +15 -8
- package/dist/types/nodes/predicate.d.ts +3 -3
- package/dist/types/nodes/rules/props.d.ts +8 -8
- package/dist/types/parse/ast/distributableFunction.d.ts +1 -1
- package/dist/types/parse/ast/intersection.d.ts +2 -2
- package/dist/types/parse/ast/morph.d.ts +3 -5
- package/dist/types/parse/ast/node.d.ts +6 -7
- package/dist/types/parse/ast/tuple.d.ts +4 -4
- package/dist/types/parse/definition.d.ts +2 -2
- package/dist/types/parse/string/reduce/dynamic.d.ts +5 -5
- package/dist/types/parse/string/shift/scanner.d.ts +1 -1
- package/dist/types/parse/string/string.d.ts +3 -3
- package/dist/types/scopes/ark.d.ts +28 -103
- package/dist/types/scopes/expressions.d.ts +9 -9
- package/dist/types/scopes/scope.d.ts +11 -10
- package/dist/types/scopes/type.d.ts +23 -14
- package/dist/types/scopes/validation/date.d.ts +1 -1
- package/dist/types/scopes/validation/validation.d.ts +10 -6
- package/dist/types/traverse/problems.d.ts +3 -3
- package/dist/types/traverse/traverse.d.ts +8 -68
- package/dist/types/utils/data.d.ts +2 -2
- package/dist/types/utils/generics.d.ts +5 -0
- package/dist/types/utils/serialize.d.ts +0 -1
- package/package.json +15 -15
package/dist/cjs/scopes/type.js
CHANGED
|
@@ -10,11 +10,23 @@ function _export(target, all) {
|
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
12
|
initializeType: ()=>initializeType,
|
|
13
|
-
isType: ()=>isType
|
|
13
|
+
isType: ()=>isType,
|
|
14
|
+
isCheckResult: ()=>isCheckResult,
|
|
15
|
+
isAnonymousName: ()=>isAnonymousName
|
|
14
16
|
});
|
|
15
17
|
const _traverseTs = require("../traverse/traverse.js");
|
|
16
18
|
const _chainableNoOpProxyTs = require("../utils/chainableNoOpProxy.js");
|
|
19
|
+
const _genericsTs = require("../utils/generics.js");
|
|
17
20
|
const initializeType = (name, definition, config, scope)=>{
|
|
21
|
+
const namedTraverse = (0, _genericsTs.composeNamed)((data)=>{
|
|
22
|
+
const state = new _traverseTs.TraversalState(data, t);
|
|
23
|
+
(0, _traverseTs.traverse)(t.flat, state);
|
|
24
|
+
return state.problems.count ? {
|
|
25
|
+
problems: state.problems
|
|
26
|
+
} : {
|
|
27
|
+
data: state.data
|
|
28
|
+
};
|
|
29
|
+
}, name);
|
|
18
30
|
const root = {
|
|
19
31
|
// temporarily initialize node/flat to aliases that will be included in
|
|
20
32
|
// the final type in case of cyclic resolutions
|
|
@@ -25,24 +37,17 @@ const initializeType = (name, definition, config, scope)=>{
|
|
|
25
37
|
name
|
|
26
38
|
]
|
|
27
39
|
],
|
|
40
|
+
allows: (data)=>!namedTraverse(data).problems,
|
|
28
41
|
infer: _chainableNoOpProxyTs.chainableNoOpProxy,
|
|
29
|
-
qualifiedName: `${scope.name}.${name}`,
|
|
42
|
+
qualifiedName: isAnonymousName(name) ? scope.getAnonymousQualifiedName(name) : `${scope.name}.${name}`,
|
|
30
43
|
definition,
|
|
31
44
|
scope,
|
|
32
45
|
includesMorph: false,
|
|
33
46
|
config
|
|
34
47
|
};
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
[name]: (data)=>{
|
|
38
|
-
const state = new _traverseTs.TraversalState(data, namedTraverse);
|
|
39
|
-
return (0, _traverseTs.traverse)(namedTraverse.flat, state) ? {
|
|
40
|
-
data: state.data
|
|
41
|
-
} : {
|
|
42
|
-
problems: state.problems
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
}[name];
|
|
46
|
-
return Object.assign(namedTraverse, root);
|
|
48
|
+
const t = Object.assign(namedTraverse, root);
|
|
49
|
+
return t;
|
|
47
50
|
};
|
|
48
51
|
const isType = (value)=>value?.infer === _chainableNoOpProxyTs.chainableNoOpProxy;
|
|
52
|
+
const isCheckResult = (value)=>(0, _genericsTs.getNominalId)(value) !== undefined;
|
|
53
|
+
const isAnonymousName = (name)=>name[0] === "λ";
|
|
@@ -13,6 +13,7 @@ _export(exports, {
|
|
|
13
13
|
parsedDate: ()=>parsedDate
|
|
14
14
|
});
|
|
15
15
|
const _scopeTs = require("../scope.js");
|
|
16
|
+
const _tsKeywordsTs = require("../tsKeywords.js");
|
|
16
17
|
const dayDelimiterMatcher = /^[./-]$/;
|
|
17
18
|
// ISO 8601 date/time modernized from https://github.com/validatorjs/validator.js/blob/master/src/lib/isISO8601.js
|
|
18
19
|
// Based on https://tc39.es/ecma262/#sec-date-time-string-format, the T
|
|
@@ -52,9 +53,7 @@ const tryParseDate = (data, opts)=>{
|
|
|
52
53
|
return writeFormattedMustBe(opts.format);
|
|
53
54
|
};
|
|
54
55
|
const parsedDate = (0, _scopeTs.rootType)([
|
|
55
|
-
|
|
56
|
-
string: true
|
|
57
|
-
}),
|
|
56
|
+
_tsKeywordsTs.tsKeywords.string,
|
|
58
57
|
"|>",
|
|
59
58
|
(s, problems)=>{
|
|
60
59
|
const result = tryParseDate(s);
|
|
@@ -14,6 +14,7 @@ _export(exports, {
|
|
|
14
14
|
});
|
|
15
15
|
const _numericLiteralsTs = require("../../utils/numericLiterals.js");
|
|
16
16
|
const _scopeTs = require("../scope.js");
|
|
17
|
+
const _tsKeywordsTs = require("../tsKeywords.js");
|
|
17
18
|
const _creditCardTs = require("./creditCard.js");
|
|
18
19
|
const _dateTs = require("./date.js");
|
|
19
20
|
// Non-trivial expressions should have an explanation or attribution
|
|
@@ -39,6 +40,17 @@ const email = (0, _scopeTs.rootType)(/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z
|
|
|
39
40
|
const uuid = (0, _scopeTs.rootType)(/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/, {
|
|
40
41
|
mustBe: "a valid UUID"
|
|
41
42
|
});
|
|
43
|
+
// https://semver.org/
|
|
44
|
+
const semver = (0, _scopeTs.rootType)(/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/, {
|
|
45
|
+
mustBe: "a valid semantic version (see https://semver.org/)"
|
|
46
|
+
});
|
|
47
|
+
const json = (0, _scopeTs.rootType)([
|
|
48
|
+
_tsKeywordsTs.tsKeywords.string,
|
|
49
|
+
"|>",
|
|
50
|
+
(s)=>JSON.parse(s)
|
|
51
|
+
], {
|
|
52
|
+
mustBe: "a JSON-parsable string"
|
|
53
|
+
});
|
|
42
54
|
const validationScope = (0, _scopeTs.scope)({
|
|
43
55
|
// Character sets
|
|
44
56
|
alpha: [
|
|
@@ -75,6 +87,8 @@ const validationScope = (0, _scopeTs.scope)({
|
|
|
75
87
|
parsedNumber,
|
|
76
88
|
parsedInteger,
|
|
77
89
|
parsedDate: _dateTs.parsedDate,
|
|
90
|
+
semver,
|
|
91
|
+
json,
|
|
78
92
|
integer: [
|
|
79
93
|
"node",
|
|
80
94
|
{
|
|
@@ -69,7 +69,7 @@ function _classPrivateFieldSet(receiver, privateMap, value) {
|
|
|
69
69
|
}
|
|
70
70
|
class ArkTypeError extends TypeError {
|
|
71
71
|
constructor(problems){
|
|
72
|
-
super(problems
|
|
72
|
+
super(`${problems}`);
|
|
73
73
|
this.cause = problems;
|
|
74
74
|
}
|
|
75
75
|
}
|
|
@@ -137,10 +137,10 @@ class ProblemArray extends Array {
|
|
|
137
137
|
this.count++;
|
|
138
138
|
}
|
|
139
139
|
get summary() {
|
|
140
|
-
return this
|
|
140
|
+
return `${this}`;
|
|
141
141
|
}
|
|
142
142
|
toString() {
|
|
143
|
-
return this.
|
|
143
|
+
return this.join("\n");
|
|
144
144
|
}
|
|
145
145
|
throw() {
|
|
146
146
|
throw new ArkTypeError(this);
|
|
@@ -20,6 +20,7 @@ const _divisorTs = require("../nodes/rules/divisor.js");
|
|
|
20
20
|
const _rangeTs = require("../nodes/rules/range.js");
|
|
21
21
|
const _regexTs = require("../nodes/rules/regex.js");
|
|
22
22
|
const _rulesTs = require("../nodes/rules/rules.js");
|
|
23
|
+
const _typeTs = require("../scopes/type.js");
|
|
23
24
|
const _domainsTs = require("../utils/domains.js");
|
|
24
25
|
const _errorsTs = require("../utils/errors.js");
|
|
25
26
|
const _genericsTs = require("../utils/generics.js");
|
|
@@ -93,8 +94,8 @@ class TraversalState {
|
|
|
93
94
|
this.traversalConfig[entry[0]].unshift(entry[1]);
|
|
94
95
|
}
|
|
95
96
|
const isValid = traverse(node, this);
|
|
96
|
-
for (const
|
|
97
|
-
this.traversalConfig[
|
|
97
|
+
for (const entry of configEntries){
|
|
98
|
+
this.traversalConfig[entry[0]].shift();
|
|
98
99
|
}
|
|
99
100
|
return isValid;
|
|
100
101
|
}
|
|
@@ -281,7 +282,14 @@ const entryCheckers = {
|
|
|
281
282
|
},
|
|
282
283
|
alias: (name, state)=>state.traverseResolution(name),
|
|
283
284
|
class: _classTs.checkClass,
|
|
284
|
-
narrow: (narrow, state)=>
|
|
285
|
+
narrow: (narrow, state)=>{
|
|
286
|
+
const lastProblemsCount = state.problems.count;
|
|
287
|
+
const result = narrow(state.data, state.problems);
|
|
288
|
+
if (!result && state.problems.count === lastProblemsCount) {
|
|
289
|
+
state.problems.mustBe(narrow.name ? `valid according to ${narrow.name}` : "valid");
|
|
290
|
+
}
|
|
291
|
+
return result;
|
|
292
|
+
},
|
|
285
293
|
config: ({ config , node }, state)=>state.traverseConfig(config, node),
|
|
286
294
|
value: (value, state)=>state.data === value || !state.problems.add("value", value),
|
|
287
295
|
morph: (morph, state)=>{
|
|
@@ -294,6 +302,16 @@ const entryCheckers = {
|
|
|
294
302
|
state.problems.addProblem(out);
|
|
295
303
|
return false;
|
|
296
304
|
}
|
|
305
|
+
if ((0, _typeTs.isCheckResult)(out)) {
|
|
306
|
+
if (out.problems) {
|
|
307
|
+
for (const problem of out.problems){
|
|
308
|
+
state.problems.addProblem(problem);
|
|
309
|
+
}
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
state.data = out.data;
|
|
313
|
+
return true;
|
|
314
|
+
}
|
|
297
315
|
state.data = out;
|
|
298
316
|
return true;
|
|
299
317
|
},
|
package/dist/cjs/utils/data.js
CHANGED
|
@@ -16,7 +16,7 @@ _export(exports, {
|
|
|
16
16
|
const _domainsTs = require("./domains.js");
|
|
17
17
|
const _serializeTs = require("./serialize.js");
|
|
18
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" : "";
|
|
19
|
+
const unitsOf = (data)=>typeof data === "string" ? "characters" : Array.isArray(data) ? "items long" : "";
|
|
20
20
|
class DataWrapper {
|
|
21
21
|
toString() {
|
|
22
22
|
return (0, _serializeTs.stringify)(this.value);
|
|
@@ -15,8 +15,13 @@ _export(exports, {
|
|
|
15
15
|
objectKeysOf: ()=>objectKeysOf,
|
|
16
16
|
prototypeKeysOf: ()=>prototypeKeysOf,
|
|
17
17
|
hasKey: ()=>hasKey,
|
|
18
|
+
hasSingleKey: ()=>hasSingleKey,
|
|
18
19
|
keyCount: ()=>keyCount,
|
|
19
20
|
hasKeys: ()=>hasKeys,
|
|
21
|
+
nominal: ()=>nominal,
|
|
22
|
+
composeNamed: ()=>composeNamed,
|
|
23
|
+
getNominalId: ()=>getNominalId,
|
|
24
|
+
hasNominalId: ()=>hasNominalId,
|
|
20
25
|
listFrom: ()=>listFrom
|
|
21
26
|
});
|
|
22
27
|
const _domainsTs = require("./domains.js");
|
|
@@ -45,8 +50,19 @@ const hasKey = (o, k)=>{
|
|
|
45
50
|
const valueAtKey = o?.[k];
|
|
46
51
|
return valueAtKey !== undefined && valueAtKey !== null;
|
|
47
52
|
};
|
|
53
|
+
const hasSingleKey = (o, k)=>k in o && Object.keys(o).length === 1;
|
|
48
54
|
const keyCount = (o)=>Object.keys(o).length;
|
|
49
55
|
const hasKeys = (value)=>(0, _domainsTs.hasDomain)(value, "object") ? Object.keys(value).length !== 0 : false;
|
|
56
|
+
const id = Symbol("id");
|
|
57
|
+
const nominal = (o, name)=>Object.assign(o, {
|
|
58
|
+
[id]: name
|
|
59
|
+
});
|
|
60
|
+
const composeNamed = (f, name)=>// define within a key to dynamically assign a name to the function
|
|
61
|
+
({
|
|
62
|
+
[name]: (...args)=>nominal(f(...args), name)
|
|
63
|
+
})[name];
|
|
64
|
+
const getNominalId = (data)=>(0, _domainsTs.hasDomain)(data, "object") && id in data ? data[id] : undefined;
|
|
65
|
+
const hasNominalId = (data, name)=>getNominalId(data) === name;
|
|
50
66
|
const listFrom = (data)=>Array.isArray(data) ? data : [
|
|
51
67
|
data
|
|
52
68
|
];
|
|
@@ -10,7 +10,6 @@ function _export(target, all) {
|
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
12
|
snapshot: ()=>snapshot,
|
|
13
|
-
serialize: ()=>serialize,
|
|
14
13
|
stringify: ()=>stringify,
|
|
15
14
|
serializePrimitive: ()=>serializePrimitive,
|
|
16
15
|
deserializePrimitive: ()=>deserializePrimitive
|
|
@@ -18,74 +17,51 @@ _export(exports, {
|
|
|
18
17
|
const _domainsTs = require("./domains.js");
|
|
19
18
|
const _genericsTs = require("./generics.js");
|
|
20
19
|
const _numericLiteralsTs = require("./numericLiterals.js");
|
|
21
|
-
const snapshot = (data, opts = {})=>
|
|
22
|
-
const
|
|
23
|
-
switch((0, _domainsTs.domainOf)(
|
|
20
|
+
const snapshot = (data, opts = {})=>serializeRecurse(data, opts, []);
|
|
21
|
+
const stringify = (data, indent)=>{
|
|
22
|
+
switch((0, _domainsTs.domainOf)(data)){
|
|
23
|
+
case "object":
|
|
24
|
+
return JSON.stringify(serializeRecurse(data, stringifyOpts, []), null, indent);
|
|
25
|
+
case "symbol":
|
|
26
|
+
return stringifyOpts.onSymbol(data);
|
|
27
|
+
default:
|
|
28
|
+
return serializePrimitive(data);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
const stringifyOpts = {
|
|
32
|
+
onCycle: ()=>"(cycle)",
|
|
33
|
+
onSymbol: (v)=>`(symbol${v.description && ` ${v.description}`})`,
|
|
34
|
+
onFunction: (v)=>`(function${v.name && ` ${v.name}`})`
|
|
35
|
+
};
|
|
36
|
+
const serializeRecurse = (data, context, seen)=>{
|
|
37
|
+
switch((0, _domainsTs.domainOf)(data)){
|
|
24
38
|
case "object":
|
|
25
|
-
if (typeof
|
|
26
|
-
return stringifyOpts.onFunction(
|
|
39
|
+
if (typeof data === "function") {
|
|
40
|
+
return stringifyOpts.onFunction(data);
|
|
27
41
|
}
|
|
28
|
-
if (seen.includes(
|
|
42
|
+
if (seen.includes(data)) {
|
|
29
43
|
return "(cycle)";
|
|
30
44
|
}
|
|
31
45
|
const nextSeen = [
|
|
32
46
|
...seen,
|
|
33
|
-
|
|
47
|
+
data
|
|
34
48
|
];
|
|
35
|
-
if (Array.isArray(
|
|
36
|
-
return
|
|
49
|
+
if (Array.isArray(data)) {
|
|
50
|
+
return data.map((item)=>serializeRecurse(item, context, nextSeen));
|
|
37
51
|
}
|
|
38
52
|
const result = {};
|
|
39
|
-
for(const k in
|
|
40
|
-
result[k] =
|
|
53
|
+
for(const k in data){
|
|
54
|
+
result[k] = serializeRecurse(data[k], context, nextSeen);
|
|
41
55
|
}
|
|
42
56
|
return result;
|
|
43
57
|
case "symbol":
|
|
44
|
-
return stringifyOpts.onSymbol(
|
|
58
|
+
return stringifyOpts.onSymbol(data);
|
|
45
59
|
case "bigint":
|
|
46
|
-
return `${
|
|
60
|
+
return `${data}n`;
|
|
47
61
|
case "undefined":
|
|
48
62
|
return "undefined";
|
|
49
63
|
default:
|
|
50
|
-
return
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
const serialize = (data, opts = {}, indent)=>{
|
|
54
|
-
const seen = [];
|
|
55
|
-
return JSON.stringify(data, (k, v)=>{
|
|
56
|
-
switch((0, _domainsTs.domainOf)(v)){
|
|
57
|
-
case "object":
|
|
58
|
-
if (seen.includes(v)) {
|
|
59
|
-
return opts.onCycle?.(v);
|
|
60
|
-
}
|
|
61
|
-
if (typeof v === "function") {
|
|
62
|
-
return opts.onFunction?.(v);
|
|
63
|
-
}
|
|
64
|
-
return v;
|
|
65
|
-
case "undefined":
|
|
66
|
-
return "undefined";
|
|
67
|
-
case "symbol":
|
|
68
|
-
return opts.onSymbol?.(v);
|
|
69
|
-
case "bigint":
|
|
70
|
-
return `${v}n`;
|
|
71
|
-
default:
|
|
72
|
-
return v;
|
|
73
|
-
}
|
|
74
|
-
}, indent);
|
|
75
|
-
};
|
|
76
|
-
const stringifyOpts = {
|
|
77
|
-
onCycle: ()=>"(cycle)",
|
|
78
|
-
onSymbol: (v)=>`(symbol${v.description && ` ${v.description}`})`,
|
|
79
|
-
onFunction: (v)=>`(function${v.name && ` ${v.name}`})`
|
|
80
|
-
};
|
|
81
|
-
const stringify = (data, indent)=>{
|
|
82
|
-
switch((0, _domainsTs.domainOf)(data)){
|
|
83
|
-
case "object":
|
|
84
|
-
return serialize(data, stringifyOpts, indent);
|
|
85
|
-
case "symbol":
|
|
86
|
-
return stringifyOpts.onSymbol(data);
|
|
87
|
-
default:
|
|
88
|
-
return serializePrimitive(data);
|
|
64
|
+
return data;
|
|
89
65
|
}
|
|
90
66
|
};
|
|
91
67
|
const serializePrimitive = (value)=>typeof value === "string" ? `'${value}'` : typeof value === "bigint" ? `${value}n` : `${value}`;
|
|
@@ -41,7 +41,7 @@ function _classPrivateFieldSet(receiver, privateMap, value) {
|
|
|
41
41
|
import { throwInternalError } from "../utils/errors.js";
|
|
42
42
|
import { objectKeysOf } from "../utils/generics.js";
|
|
43
43
|
import { Path } from "../utils/paths.js";
|
|
44
|
-
import {
|
|
44
|
+
import { stringify } 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);
|
|
46
46
|
export const throwUndefinedOperandsError = ()=>throwInternalError(`Unexpected operation two undefined operands`);
|
|
47
47
|
export const disjointDescriptionWriters = {
|
|
@@ -49,10 +49,10 @@ export const disjointDescriptionWriters = {
|
|
|
49
49
|
range: ({ l , r })=>`${stringifyRange(l)} and ${stringifyRange(r)}`,
|
|
50
50
|
class: ({ l , r })=>`classes ${typeof l === "string" ? l : l.name} and ${typeof r === "string" ? r : r.name}`,
|
|
51
51
|
tupleLength: ({ l , r })=>`tuples of length ${l} and ${r}`,
|
|
52
|
-
value: ({ l , r })=>`literal values ${
|
|
53
|
-
leftAssignability: ({ l , r })=>`literal value ${
|
|
54
|
-
rightAssignability: ({ l , r })=>`literal value ${
|
|
55
|
-
union: ({ l , r })=>`branches ${
|
|
52
|
+
value: ({ l , r })=>`literal values ${stringify(l)} and ${stringify(r)}`,
|
|
53
|
+
leftAssignability: ({ l , r })=>`literal value ${stringify(l.value)} and ${stringify(r)}`,
|
|
54
|
+
rightAssignability: ({ l , r })=>`literal value ${stringify(r.value)} and ${stringify(l)}`,
|
|
55
|
+
union: ({ l , r })=>`branches ${stringify(l)} and branches ${stringify(r)}`
|
|
56
56
|
};
|
|
57
57
|
export const stringifyRange = (range)=>"limit" in range ? `the range of exactly ${range.limit}` : range.min ? range.max ? `the range bounded by ${range.min.comparator}${range.min.limit} and ${range.max.comparator}${range.max.limit}` : `${range.min.comparator}${range.min.limit}` : range.max ? `${range.max.comparator}${range.max.limit}` : "the unbounded range";
|
|
58
58
|
var _disjoints = /*#__PURE__*/ new WeakMap();
|
|
@@ -244,4 +244,4 @@ const serializeData = {
|
|
|
244
244
|
};
|
|
245
245
|
export const serializeCase = (kind, data)=>serializeData[kind](data);
|
|
246
246
|
const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph(propToNode(prop), $)) : false;
|
|
247
|
-
const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).includesMorph : Object.values(node).some((predicate)=>predicate === true ? false : isArray(predicate) ? predicate.some((branch)=>branchIncludesMorph(branch, $)) : branchIncludesMorph(predicate, $));
|
|
247
|
+
const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).includesMorph : Object.values($.resolveTypeNode(node)).some((predicate)=>predicate === true ? false : isArray(predicate) ? predicate.some((branch)=>branchIncludesMorph(branch, $)) : branchIncludesMorph(predicate, $));
|
package/dist/mjs/nodes/node.js
CHANGED
|
@@ -5,17 +5,18 @@ 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 } from "./predicate.js";
|
|
7
7
|
import { mappedKeys } from "./rules/props.js";
|
|
8
|
+
export const isConfigNode = (node)=>"config" in node;
|
|
8
9
|
export const nodeIntersection = (l, r, state)=>{
|
|
9
10
|
state.domain = undefined;
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const result =
|
|
11
|
+
const lDomains = state.type.scope.resolveTypeNode(l);
|
|
12
|
+
const rDomains = state.type.scope.resolveTypeNode(r);
|
|
13
|
+
const result = typeNodeIntersection(lDomains, rDomains, state);
|
|
13
14
|
if (typeof result === "object" && !hasKeys(result)) {
|
|
14
|
-
return hasKeys(state.disjoints) ? anonymousDisjoint() : state.addDisjoint("domain", objectKeysOf(
|
|
15
|
+
return hasKeys(state.disjoints) ? anonymousDisjoint() : state.addDisjoint("domain", objectKeysOf(lDomains), objectKeysOf(rDomains));
|
|
15
16
|
}
|
|
16
|
-
return result ===
|
|
17
|
+
return result === lDomains ? l : result === rDomains ? r : result;
|
|
17
18
|
};
|
|
18
|
-
const
|
|
19
|
+
const typeNodeIntersection = composeKeyedIntersection((domain, l, r, context)=>{
|
|
19
20
|
if (l === undefined) {
|
|
20
21
|
return r === undefined ? throwUndefinedOperandsError() : undefined;
|
|
21
22
|
}
|
|
@@ -32,15 +33,15 @@ export const rootIntersection = (l, r, type)=>{
|
|
|
32
33
|
return isDisjoint(result) ? throwParseError(compileDisjointReasonsMessage(state.disjoints)) : isEquality(result) ? l : result;
|
|
33
34
|
};
|
|
34
35
|
export const rootUnion = (l, r, type)=>{
|
|
35
|
-
const
|
|
36
|
-
const
|
|
36
|
+
const lDomains = type.scope.resolveTypeNode(l);
|
|
37
|
+
const rDomains = type.scope.resolveTypeNode(r);
|
|
37
38
|
const result = {};
|
|
38
39
|
const domains = objectKeysOf({
|
|
39
|
-
...
|
|
40
|
-
...
|
|
40
|
+
...lDomains,
|
|
41
|
+
...rDomains
|
|
41
42
|
});
|
|
42
43
|
for (const domain of domains){
|
|
43
|
-
result[domain] = hasKey(
|
|
44
|
+
result[domain] = hasKey(lDomains, domain) ? hasKey(rDomains, domain) ? predicateUnion(domain, lDomains[domain], rDomains[domain], type) : lDomains[domain] : hasKey(rDomains, domain) ? rDomains[domain] : throwUndefinedOperandsError();
|
|
44
45
|
}
|
|
45
46
|
return result;
|
|
46
47
|
};
|
|
@@ -51,20 +52,25 @@ export const flattenType = (type)=>{
|
|
|
51
52
|
path: new Path(),
|
|
52
53
|
lastDomain: "undefined"
|
|
53
54
|
};
|
|
54
|
-
return type.
|
|
55
|
+
return flattenNode(type.node, ctx);
|
|
56
|
+
};
|
|
57
|
+
export const flattenNode = (node, ctx)=>{
|
|
58
|
+
if (typeof node === "string") {
|
|
59
|
+
return ctx.type.scope.resolve(node).flat;
|
|
60
|
+
}
|
|
61
|
+
const hasConfig = isConfigNode(node);
|
|
62
|
+
const flattenedTypeNode = flattenTypeNode(hasConfig ? node.node : node, ctx);
|
|
63
|
+
return hasConfig ? [
|
|
55
64
|
[
|
|
56
65
|
"config",
|
|
57
66
|
{
|
|
58
|
-
config: entriesOf(
|
|
59
|
-
node:
|
|
67
|
+
config: entriesOf(node.config),
|
|
68
|
+
node: flattenedTypeNode
|
|
60
69
|
}
|
|
61
70
|
]
|
|
62
|
-
] :
|
|
71
|
+
] : flattenedTypeNode;
|
|
63
72
|
};
|
|
64
|
-
export const
|
|
65
|
-
if (typeof node === "string") {
|
|
66
|
-
return ctx.type.scope.resolve(node).flat;
|
|
67
|
-
}
|
|
73
|
+
export const flattenTypeNode = (node, ctx)=>{
|
|
68
74
|
const domains = objectKeysOf(node);
|
|
69
75
|
if (domains.length === 1) {
|
|
70
76
|
const domain = domains[0];
|
|
@@ -83,9 +89,9 @@ export const flattenNode = (node, ctx)=>{
|
|
|
83
89
|
];
|
|
84
90
|
}
|
|
85
91
|
const result = {};
|
|
86
|
-
for (const
|
|
87
|
-
ctx.lastDomain =
|
|
88
|
-
result[
|
|
92
|
+
for (const domain of domains){
|
|
93
|
+
ctx.lastDomain = domain;
|
|
94
|
+
result[domain] = flattenPredicate(node[domain], ctx);
|
|
89
95
|
}
|
|
90
96
|
return [
|
|
91
97
|
[
|
|
@@ -3,7 +3,6 @@ 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";
|
|
5
5
|
import { flattenBranches } from "./discriminate.js";
|
|
6
|
-
import "./rules/rules.js";
|
|
7
6
|
const emptyRulesIfTrue = (predicate)=>predicate === true ? {} : predicate;
|
|
8
7
|
export const comparePredicates = (l, r, context)=>{
|
|
9
8
|
if (l === true && r === true) {
|
|
@@ -42,10 +42,10 @@ export const rangeIntersection = composeIntersection((l, r, state)=>{
|
|
|
42
42
|
return stricterMax === "l" ? l : stricterMax === "r" ? r : equality();
|
|
43
43
|
});
|
|
44
44
|
const rangeAllows = (range, n)=>isEqualityRange(range) ? n === range.limit : minAllows(range.min, n) && maxAllows(range.max, n);
|
|
45
|
-
const minAllows = (min, n)=>!min || n
|
|
46
|
-
const maxAllows = (max, n)=>!max || n
|
|
45
|
+
const minAllows = (min, n)=>!min || n > min.limit || n === min.limit && !isExclusive(min.comparator);
|
|
46
|
+
const maxAllows = (max, n)=>!max || n < max.limit || n === max.limit && !isExclusive(max.comparator);
|
|
47
47
|
export const flattenRange = (entries, range, ctx)=>{
|
|
48
|
-
const units = ctx.lastDomain === "string" ? "characters" : ctx.lastDomain === "object" ? "items" : undefined;
|
|
48
|
+
const units = ctx.lastDomain === "string" ? "characters" : ctx.lastDomain === "object" ? "items long" : undefined;
|
|
49
49
|
if (isEqualityRange(range)) {
|
|
50
50
|
return entries.push([
|
|
51
51
|
"bound",
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { parseDefinition } from "../definition.js";
|
|
2
|
+
export const parseConfigTuple = (def, ctx)=>({
|
|
3
|
+
node: ctx.type.scope.resolveTypeNode(parseDefinition(def[0], ctx)),
|
|
4
|
+
config: def[2]
|
|
5
5
|
});
|
|
6
|
-
return anonymousType.name;
|
|
7
|
-
};
|
|
@@ -3,8 +3,8 @@ import { throwParseError } from "../../utils/errors.js";
|
|
|
3
3
|
import { objectKeysOf } from "../../utils/generics.js";
|
|
4
4
|
import { stringify } from "../../utils/serialize.js";
|
|
5
5
|
export const writeMalformedDistributableFunctionMessage = (def)=>`Expected a Function or Record<Domain, Function> operand (${stringify(def)} was invalid)`;
|
|
6
|
-
export const distributeFunctionToNode = (distributableFunction,
|
|
7
|
-
const domains = objectKeysOf(
|
|
6
|
+
export const distributeFunctionToNode = (distributableFunction, node, ctx, ruleKey)=>{
|
|
7
|
+
const domains = objectKeysOf(node);
|
|
8
8
|
if (!hasDomain(distributableFunction, "object")) {
|
|
9
9
|
return throwParseError(writeMalformedDistributableFunctionMessage(distributableFunction));
|
|
10
10
|
}
|
|
@@ -17,17 +17,17 @@ export const distributeFunctionToNode = (distributableFunction, inputNode, ctx,
|
|
|
17
17
|
distributed[domain] = domainFunction;
|
|
18
18
|
}
|
|
19
19
|
} else {
|
|
20
|
-
for (const
|
|
21
|
-
if (distributableFunction[
|
|
20
|
+
for (const domain of domains){
|
|
21
|
+
if (distributableFunction[domain] === undefined) {
|
|
22
22
|
continue;
|
|
23
23
|
}
|
|
24
24
|
const functionInDomain = {
|
|
25
|
-
[ruleKey]: distributableFunction[
|
|
25
|
+
[ruleKey]: distributableFunction[domain]
|
|
26
26
|
};
|
|
27
27
|
if (typeof functionInDomain[ruleKey] !== "function") {
|
|
28
28
|
return throwParseError(writeMalformedDistributableFunctionMessage(functionInDomain));
|
|
29
29
|
}
|
|
30
|
-
distributed[
|
|
30
|
+
distributed[domain] = functionInDomain;
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
return distributed;
|
|
@@ -8,8 +8,8 @@ export const compileDisjointReasonsMessage = (disjoints)=>{
|
|
|
8
8
|
}
|
|
9
9
|
let message = `
|
|
10
10
|
"Intersection results in unsatisfiable types at the following paths:\n`;
|
|
11
|
-
for(const
|
|
12
|
-
message += ` ${
|
|
11
|
+
for(const path in disjoints){
|
|
12
|
+
message += ` ${path}: ${disjointDescriptionWriters[disjoints[path].kind](disjoints[path])}\n`;
|
|
13
13
|
}
|
|
14
14
|
return message;
|
|
15
15
|
};
|
|
@@ -91,9 +91,9 @@ const keysOfObjectBranch = (branch)=>{
|
|
|
91
91
|
}
|
|
92
92
|
if ("class" in branch) {
|
|
93
93
|
const constructor = typeof branch.class === "string" ? defaultObjectKinds[branch.class] : branch.class;
|
|
94
|
-
for (const
|
|
95
|
-
if (!result.includes(
|
|
96
|
-
result.push(
|
|
94
|
+
for (const key of prototypeKeysOf(constructor.prototype)){
|
|
95
|
+
if (!result.includes(key)) {
|
|
96
|
+
result.push(key);
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
99
|
}
|
|
@@ -7,7 +7,8 @@ export const parseMorphTuple = (def, ctx)=>{
|
|
|
7
7
|
if (typeof def[2] !== "function") {
|
|
8
8
|
return throwParseError(writeMalformedMorphExpressionMessage(def[2]));
|
|
9
9
|
}
|
|
10
|
-
const
|
|
10
|
+
const node = parseDefinition(def[0], ctx);
|
|
11
|
+
const resolution = ctx.type.scope.resolveTypeNode(node);
|
|
11
12
|
const morph = def[2];
|
|
12
13
|
ctx.type.includesMorph = true;
|
|
13
14
|
let domain;
|
|
@@ -1,7 +1,14 @@
|
|
|
1
|
-
import { rootIntersection } from "../../nodes/node.js";
|
|
1
|
+
import { isConfigNode, rootIntersection } from "../../nodes/node.js";
|
|
2
2
|
import { parseDefinition } from "../definition.js";
|
|
3
3
|
import { distributeFunctionToNode } from "./distributableFunction.js";
|
|
4
4
|
export const parseNarrowTuple = (def, ctx)=>{
|
|
5
5
|
const inputNode = parseDefinition(def[0], ctx);
|
|
6
|
-
|
|
6
|
+
const resolution = ctx.type.scope.resolveNode(inputNode);
|
|
7
|
+
const hasConfig = isConfigNode(resolution);
|
|
8
|
+
const typeNode = hasConfig ? resolution.node : resolution;
|
|
9
|
+
const result = rootIntersection(inputNode, distributeFunctionToNode(def[2], typeNode, ctx, "narrow"), ctx.type);
|
|
10
|
+
return hasConfig ? {
|
|
11
|
+
config: resolution.config,
|
|
12
|
+
node: result
|
|
13
|
+
} : result;
|
|
7
14
|
};
|
|
@@ -28,12 +28,12 @@ export const parseDefinition = (def, ctx)=>{
|
|
|
28
28
|
};
|
|
29
29
|
case "Function":
|
|
30
30
|
if (isType(def)) {
|
|
31
|
-
return ctx.type.scope.
|
|
31
|
+
return ctx.type.scope.addAnonymousTypeReference(def, ctx);
|
|
32
32
|
}
|
|
33
33
|
if (isThunk(def)) {
|
|
34
34
|
const returned = def();
|
|
35
35
|
if (isType(returned)) {
|
|
36
|
-
return ctx.type.scope.
|
|
36
|
+
return ctx.type.scope.addAnonymousTypeReference(returned, ctx);
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
return throwParseError(writeBadDefinitionTypeMessage("Function"));
|
|
@@ -4,7 +4,7 @@ export const parseUnenclosed = (s)=>{
|
|
|
4
4
|
s.setRoot(unenclosedToNode(s, token));
|
|
5
5
|
};
|
|
6
6
|
const unenclosedToNode = (s, token)=>{
|
|
7
|
-
if (s.ctx.type.scope.
|
|
7
|
+
if (s.ctx.type.scope.addParsedReferenceIfResolvable(token, s.ctx)) {
|
|
8
8
|
return token;
|
|
9
9
|
}
|
|
10
10
|
return maybeParseUnenclosedLiteral(token) ?? s.error(token === "" ? writeMissingOperandMessage(s) : writeUnresolvableMessage(token));
|