arktype 0.0.1 → 0.1.1
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/LICENSE +7 -0
- package/README.md +101 -0
- package/dist/api.cjs +15 -0
- package/dist/api.d.ts +2 -0
- package/dist/api.mjs +1 -0
- package/dist/cjs/main.js +462 -0
- package/dist/cjs/nodes/branches.js +85 -0
- package/dist/cjs/nodes/compose.js +140 -0
- package/dist/cjs/nodes/discriminate.js +199 -0
- package/dist/cjs/nodes/functors.js +35 -0
- package/dist/cjs/nodes/infer.js +4 -0
- package/dist/cjs/nodes/node.js +108 -0
- package/dist/cjs/nodes/predicate.js +87 -0
- package/dist/cjs/nodes/rules/class.js +21 -0
- package/dist/cjs/nodes/rules/collapsibleSet.js +49 -0
- package/dist/cjs/nodes/rules/divisor.js +33 -0
- package/dist/cjs/nodes/rules/props.js +72 -0
- package/dist/cjs/nodes/rules/range.js +109 -0
- package/dist/cjs/nodes/rules/regex.js +68 -0
- package/dist/cjs/nodes/rules/rules.js +156 -0
- package/dist/cjs/nodes/rules/subdomain.js +78 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/parse/definition.js +33 -0
- package/dist/cjs/parse/record.js +37 -0
- package/dist/cjs/parse/string/ast.js +36 -0
- package/dist/cjs/parse/string/reduce/dynamic.js +150 -0
- package/dist/cjs/parse/string/reduce/shared.js +23 -0
- package/dist/cjs/parse/string/reduce/static.js +4 -0
- package/dist/cjs/parse/string/shift/operand/enclosed.js +54 -0
- package/dist/cjs/parse/string/shift/operand/operand.js +11 -0
- package/dist/cjs/parse/string/shift/operand/unenclosed.js +53 -0
- package/dist/cjs/parse/string/shift/operator/bounds.js +87 -0
- package/dist/cjs/parse/string/shift/operator/divisor.js +35 -0
- package/dist/cjs/parse/string/shift/operator/operator.js +26 -0
- package/dist/cjs/parse/string/shift/scanner.js +99 -0
- package/dist/cjs/parse/string/string.js +43 -0
- package/dist/cjs/parse/tuple/keyof.js +46 -0
- package/dist/cjs/parse/tuple/morph.js +56 -0
- package/dist/cjs/parse/tuple/narrow.js +15 -0
- package/dist/cjs/parse/tuple/tuple.js +85 -0
- package/dist/cjs/parse/tuple/utils.js +48 -0
- package/dist/cjs/traverse/problems.js +249 -0
- package/dist/cjs/traverse/traverse.js +268 -0
- package/dist/cjs/utils/chainableNoOpProxy.js +23 -0
- package/dist/cjs/utils/domains.js +67 -0
- package/dist/cjs/utils/errors.js +26 -0
- package/dist/cjs/utils/freeze.js +16 -0
- package/dist/cjs/utils/generics.js +36 -0
- package/dist/cjs/utils/numericLiterals.js +84 -0
- package/dist/cjs/utils/paths.js +32 -0
- package/dist/cjs/utils/serialize.js +98 -0
- package/dist/cjs/utils/unionToTuple.js +4 -0
- package/dist/mjs/main.js +442 -0
- package/dist/mjs/nodes/branches.js +71 -0
- package/dist/mjs/nodes/compose.js +118 -0
- package/dist/mjs/nodes/discriminate.js +184 -0
- package/dist/mjs/nodes/functors.js +27 -0
- package/dist/mjs/nodes/infer.js +1 -0
- package/dist/mjs/nodes/node.js +90 -0
- package/dist/mjs/nodes/predicate.js +69 -0
- package/dist/mjs/nodes/rules/class.js +7 -0
- package/dist/mjs/nodes/rules/collapsibleSet.js +35 -0
- package/dist/mjs/nodes/rules/divisor.js +19 -0
- package/dist/mjs/nodes/rules/props.js +57 -0
- package/dist/mjs/nodes/rules/range.js +90 -0
- package/dist/mjs/nodes/rules/regex.js +53 -0
- package/dist/mjs/nodes/rules/rules.js +138 -0
- package/dist/mjs/nodes/rules/subdomain.js +63 -0
- package/dist/mjs/package.json +3 -0
- package/dist/mjs/parse/definition.js +17 -0
- package/dist/mjs/parse/record.js +29 -0
- package/dist/mjs/parse/string/ast.js +19 -0
- package/dist/mjs/parse/string/reduce/dynamic.js +142 -0
- package/dist/mjs/parse/string/reduce/shared.js +6 -0
- package/dist/mjs/parse/string/reduce/static.js +1 -0
- package/dist/mjs/parse/string/shift/operand/enclosed.js +39 -0
- package/dist/mjs/parse/string/shift/operand/operand.js +3 -0
- package/dist/mjs/parse/string/shift/operand/unenclosed.js +36 -0
- package/dist/mjs/parse/string/shift/operator/bounds.js +70 -0
- package/dist/mjs/parse/string/shift/operator/divisor.js +21 -0
- package/dist/mjs/parse/string/shift/operator/operator.js +11 -0
- package/dist/mjs/parse/string/shift/scanner.js +91 -0
- package/dist/mjs/parse/string/string.js +28 -0
- package/dist/mjs/parse/tuple/keyof.js +38 -0
- package/dist/mjs/parse/tuple/morph.js +42 -0
- package/dist/mjs/parse/tuple/narrow.js +7 -0
- package/dist/mjs/parse/tuple/tuple.js +71 -0
- package/dist/mjs/parse/tuple/utils.js +34 -0
- package/dist/mjs/traverse/problems.js +229 -0
- package/dist/mjs/traverse/traverse.js +252 -0
- package/dist/mjs/utils/chainableNoOpProxy.js +15 -0
- package/dist/mjs/utils/domains.js +48 -0
- package/dist/mjs/utils/errors.js +10 -0
- package/dist/mjs/utils/freeze.js +8 -0
- package/dist/mjs/utils/generics.js +15 -0
- package/dist/mjs/utils/numericLiterals.js +71 -0
- package/dist/mjs/utils/paths.js +18 -0
- package/dist/mjs/utils/serialize.js +81 -0
- package/dist/mjs/utils/unionToTuple.js +1 -0
- package/dist/tsconfig.temp.tsbuildinfo +1 -0
- package/dist/types/main.d.ts +257 -0
- package/dist/types/nodes/branches.d.ts +14 -0
- package/dist/types/nodes/compose.d.ts +92 -0
- package/dist/types/nodes/discriminate.d.ts +27 -0
- package/dist/types/nodes/functors.d.ts +6 -0
- package/dist/types/nodes/infer.d.ts +35 -0
- package/dist/types/nodes/node.d.ts +51 -0
- package/dist/types/nodes/predicate.d.ts +88 -0
- package/dist/types/nodes/rules/class.d.ts +3 -0
- package/dist/types/nodes/rules/collapsibleSet.d.ts +4 -0
- package/dist/types/nodes/rules/divisor.d.ts +2 -0
- package/dist/types/nodes/rules/props.d.ts +20 -0
- package/dist/types/nodes/rules/range.d.ts +33 -0
- package/dist/types/nodes/rules/regex.d.ts +14 -0
- package/dist/types/nodes/rules/rules.d.ts +44 -0
- package/dist/types/nodes/rules/subdomain.d.ts +15 -0
- package/dist/types/parse/definition.d.ts +29 -0
- package/dist/types/parse/record.d.ts +24 -0
- package/dist/types/parse/string/ast.d.ts +42 -0
- package/dist/types/parse/string/reduce/dynamic.d.ts +34 -0
- package/dist/types/parse/string/reduce/shared.d.ts +24 -0
- package/dist/types/parse/string/reduce/static.d.ts +115 -0
- package/dist/types/parse/string/shift/operand/enclosed.d.ts +28 -0
- package/dist/types/parse/string/shift/operand/operand.d.ts +9 -0
- package/dist/types/parse/string/shift/operand/unenclosed.d.ts +18 -0
- package/dist/types/parse/string/shift/operator/bounds.d.ts +20 -0
- package/dist/types/parse/string/shift/operator/divisor.d.ts +9 -0
- package/dist/types/parse/string/shift/operator/operator.d.ts +13 -0
- package/dist/types/parse/string/shift/scanner.d.ts +89 -0
- package/dist/types/parse/string/string.d.ts +27 -0
- package/dist/types/parse/tuple/keyof.d.ts +2 -0
- package/dist/types/parse/tuple/morph.d.ts +15 -0
- package/dist/types/parse/tuple/narrow.d.ts +13 -0
- package/dist/types/parse/tuple/tuple.d.ts +37 -0
- package/dist/types/parse/tuple/utils.d.ts +19 -0
- package/dist/types/traverse/problems.d.ts +123 -0
- package/dist/types/traverse/traverse.d.ts +48 -0
- package/dist/types/utils/chainableNoOpProxy.d.ts +14 -0
- package/dist/types/utils/domains.d.ts +58 -0
- package/dist/types/utils/errors.d.ts +6 -0
- package/dist/types/utils/freeze.d.ts +2 -0
- package/dist/types/utils/generics.d.ts +118 -0
- package/dist/types/utils/numericLiterals.d.ts +28 -0
- package/dist/types/utils/paths.d.ts +7 -0
- package/dist/types/utils/serialize.d.ts +38 -0
- package/dist/types/utils/unionToTuple.d.ts +11 -0
- package/package.json +102 -2
|
@@ -0,0 +1,56 @@
|
|
|
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
|
+
parseMorphTuple: ()=>parseMorphTuple,
|
|
13
|
+
writeMalformedMorphExpressionMessage: ()=>writeMalformedMorphExpressionMessage
|
|
14
|
+
});
|
|
15
|
+
const _domainsTs = require("../../utils/domains.js");
|
|
16
|
+
const _errorsTs = require("../../utils/errors.js");
|
|
17
|
+
const _serializeTs = require("../../utils/serialize.js");
|
|
18
|
+
const _definitionTs = require("../definition.js");
|
|
19
|
+
const parseMorphTuple = (def, ctx)=>{
|
|
20
|
+
if (typeof def[2] !== "function") {
|
|
21
|
+
return (0, _errorsTs.throwParseError)(writeMalformedMorphExpressionMessage(def[2]));
|
|
22
|
+
}
|
|
23
|
+
const resolution = ctx.type.meta.scope.resolveNode((0, _definitionTs.parseDefinition)(def[0], ctx));
|
|
24
|
+
const morph = def[2];
|
|
25
|
+
ctx.type.meta.includesMorph = true;
|
|
26
|
+
let domain;
|
|
27
|
+
const result = {};
|
|
28
|
+
for(domain in resolution){
|
|
29
|
+
const predicate = resolution[domain];
|
|
30
|
+
if (predicate === true) {
|
|
31
|
+
result[domain] = {
|
|
32
|
+
input: {},
|
|
33
|
+
morph
|
|
34
|
+
};
|
|
35
|
+
} else if (typeof predicate === "object") {
|
|
36
|
+
result[domain] = (0, _domainsTs.hasSubdomain)(predicate, "Array") ? predicate.map((branch)=>applyMorph(branch, morph)) : applyMorph(predicate, morph);
|
|
37
|
+
} else {
|
|
38
|
+
(0, _errorsTs.throwInternalError)(`Unexpected predicate value for domain '${domain}': ${(0, _serializeTs.stringify)(predicate)}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return result;
|
|
42
|
+
};
|
|
43
|
+
const applyMorph = (branch, morph)=>branch.morph ? {
|
|
44
|
+
input: branch.input,
|
|
45
|
+
morph: Array.isArray(branch.morph) ? [
|
|
46
|
+
...branch.morph,
|
|
47
|
+
morph
|
|
48
|
+
] : [
|
|
49
|
+
branch.morph,
|
|
50
|
+
morph
|
|
51
|
+
]
|
|
52
|
+
} : {
|
|
53
|
+
input: branch,
|
|
54
|
+
morph
|
|
55
|
+
};
|
|
56
|
+
const writeMalformedMorphExpressionMessage = (value)=>`Morph expression requires a function following '=>' (was ${typeof value})`;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "parseNarrowTuple", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: ()=>parseNarrowTuple
|
|
8
|
+
});
|
|
9
|
+
const _nodeTs = require("../../nodes/node.js");
|
|
10
|
+
const _definitionTs = require("../definition.js");
|
|
11
|
+
const _utilsTs = require("./utils.js");
|
|
12
|
+
const parseNarrowTuple = (def, ctx)=>{
|
|
13
|
+
const inputNode = (0, _definitionTs.parseDefinition)(def[0], ctx);
|
|
14
|
+
return (0, _nodeTs.intersection)(inputNode, (0, _utilsTs.distributeFunctionToNode)(def[2], inputNode, ctx, "narrow"), ctx.type);
|
|
15
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
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
|
+
parseTuple: ()=>parseTuple,
|
|
13
|
+
writeMalformedFunctionalExpressionMessage: ()=>writeMalformedFunctionalExpressionMessage
|
|
14
|
+
});
|
|
15
|
+
const _functorsTs = require("../../nodes/functors.js");
|
|
16
|
+
const _nodeTs = require("../../nodes/node.js");
|
|
17
|
+
const _domainsTs = require("../../utils/domains.js");
|
|
18
|
+
const _errorsTs = require("../../utils/errors.js");
|
|
19
|
+
const _definitionTs = require("../definition.js");
|
|
20
|
+
const _unenclosedTs = require("../string/shift/operand/unenclosed.js");
|
|
21
|
+
const _keyofTs = require("./keyof.js");
|
|
22
|
+
const _morphTs = require("./morph.js");
|
|
23
|
+
const _narrowTs = require("./narrow.js");
|
|
24
|
+
const parseTuple = (def, ctx)=>{
|
|
25
|
+
if (isPostfixExpression(def)) {
|
|
26
|
+
return postfixParsers[def[1]](def, ctx);
|
|
27
|
+
}
|
|
28
|
+
if (isPrefixExpression(def)) {
|
|
29
|
+
return prefixParsers[def[0]](def, ctx);
|
|
30
|
+
}
|
|
31
|
+
const props = {};
|
|
32
|
+
for(let i = 0; i < def.length; i++){
|
|
33
|
+
ctx.path.push(`${i}`);
|
|
34
|
+
props[i] = (0, _definitionTs.parseDefinition)(def[i], ctx);
|
|
35
|
+
ctx.path.pop();
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
object: {
|
|
39
|
+
subdomain: "Array",
|
|
40
|
+
props,
|
|
41
|
+
range: {
|
|
42
|
+
comparator: "==",
|
|
43
|
+
limit: def.length
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
const parseBranchTuple = (def, ctx)=>{
|
|
49
|
+
if (def[2] === undefined) {
|
|
50
|
+
return (0, _errorsTs.throwParseError)((0, _unenclosedTs.writeMissingRightOperandMessage)(def[1], ""));
|
|
51
|
+
}
|
|
52
|
+
const l = (0, _definitionTs.parseDefinition)(def[0], ctx);
|
|
53
|
+
const r = (0, _definitionTs.parseDefinition)(def[2], ctx);
|
|
54
|
+
return def[1] === "&" ? (0, _nodeTs.intersection)(l, r, ctx.type) : (0, _nodeTs.union)(l, r, ctx.type);
|
|
55
|
+
};
|
|
56
|
+
const parseArrayTuple = (def, scope)=>_functorsTs.functors.Array((0, _definitionTs.parseDefinition)(def[0], scope));
|
|
57
|
+
const writeMalformedFunctionalExpressionMessage = (operator, rightDef)=>`Expression requires a function following '${operator}' (was ${typeof rightDef})`;
|
|
58
|
+
const isPostfixExpression = (def)=>postfixParsers[def[1]] !== undefined;
|
|
59
|
+
const postfixParsers = {
|
|
60
|
+
"|": parseBranchTuple,
|
|
61
|
+
"&": parseBranchTuple,
|
|
62
|
+
"[]": parseArrayTuple,
|
|
63
|
+
":": _narrowTs.parseNarrowTuple,
|
|
64
|
+
"=>": _morphTs.parseMorphTuple
|
|
65
|
+
};
|
|
66
|
+
const prefixParsers = {
|
|
67
|
+
keyof: _keyofTs.parseKeyOfTuple,
|
|
68
|
+
instanceof: (def)=>{
|
|
69
|
+
if (typeof def[1] !== "function") {
|
|
70
|
+
return (0, _errorsTs.throwParseError)(`Expected a constructor following 'instanceof' operator (was ${typeof def[1]}).`);
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
object: {
|
|
74
|
+
class: def[1]
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
"===": (def)=>({
|
|
79
|
+
[(0, _domainsTs.domainOf)(def[1])]: {
|
|
80
|
+
value: def[1]
|
|
81
|
+
}
|
|
82
|
+
}),
|
|
83
|
+
node: (def)=>def[1]
|
|
84
|
+
};
|
|
85
|
+
const isPrefixExpression = (def)=>prefixParsers[def[0]] !== undefined;
|
|
@@ -0,0 +1,48 @@
|
|
|
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
|
+
writeMalformedDistributableFunctionMessage: ()=>writeMalformedDistributableFunctionMessage,
|
|
13
|
+
distributeFunctionToNode: ()=>distributeFunctionToNode
|
|
14
|
+
});
|
|
15
|
+
const _domainsTs = require("../../utils/domains.js");
|
|
16
|
+
const _errorsTs = require("../../utils/errors.js");
|
|
17
|
+
const _genericsTs = require("../../utils/generics.js");
|
|
18
|
+
const _serializeTs = require("../../utils/serialize.js");
|
|
19
|
+
const writeMalformedDistributableFunctionMessage = (def)=>`Expected a Function or Record<Domain, Function> operand (${(0, _serializeTs.stringify)(def)} was invalid)`;
|
|
20
|
+
const distributeFunctionToNode = (distributableFunction, inputNode, ctx, ruleKey)=>{
|
|
21
|
+
const domains = (0, _genericsTs.keysOf)(ctx.type.meta.scope.resolveNode(inputNode));
|
|
22
|
+
if (!(0, _domainsTs.hasDomain)(distributableFunction, "object")) {
|
|
23
|
+
return (0, _errorsTs.throwParseError)(writeMalformedDistributableFunctionMessage(distributableFunction));
|
|
24
|
+
}
|
|
25
|
+
const distributed = {};
|
|
26
|
+
if (typeof distributableFunction === "function") {
|
|
27
|
+
const domainFunction = {
|
|
28
|
+
[ruleKey]: distributableFunction
|
|
29
|
+
};
|
|
30
|
+
for (const domain of domains){
|
|
31
|
+
distributed[domain] = domainFunction;
|
|
32
|
+
}
|
|
33
|
+
} else {
|
|
34
|
+
for (const domain1 of domains){
|
|
35
|
+
if (distributableFunction[domain1] === undefined) {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
const functionInDomain = {
|
|
39
|
+
[ruleKey]: distributableFunction[domain1]
|
|
40
|
+
};
|
|
41
|
+
if (typeof functionInDomain[ruleKey] !== "function") {
|
|
42
|
+
return (0, _errorsTs.throwParseError)(writeMalformedDistributableFunctionMessage(functionInDomain));
|
|
43
|
+
}
|
|
44
|
+
distributed[domain1] = functionInDomain;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return distributed;
|
|
48
|
+
};
|
|
@@ -0,0 +1,249 @@
|
|
|
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
|
+
ArkTypeError: ()=>ArkTypeError,
|
|
13
|
+
Problem: ()=>Problem,
|
|
14
|
+
Problems: ()=>Problems,
|
|
15
|
+
describeSubdomains: ()=>describeSubdomains,
|
|
16
|
+
describeBranches: ()=>describeBranches,
|
|
17
|
+
subdomainDescriptions: ()=>subdomainDescriptions,
|
|
18
|
+
DataWrapper: ()=>DataWrapper,
|
|
19
|
+
defaultProblemWriters: ()=>defaultProblemWriters,
|
|
20
|
+
compileProblemOptions: ()=>compileProblemOptions
|
|
21
|
+
});
|
|
22
|
+
const _scannerTs = require("../parse/string/shift/scanner.js");
|
|
23
|
+
const _domainsTs = require("../utils/domains.js");
|
|
24
|
+
const _genericsTs = require("../utils/generics.js");
|
|
25
|
+
const _pathsTs = require("../utils/paths.js");
|
|
26
|
+
const _serializeTs = require("../utils/serialize.js");
|
|
27
|
+
var _definitions_code, _definitions_code1, _definitions_code2, _clausesByPath, _pathKey;
|
|
28
|
+
class ArkTypeError extends TypeError {
|
|
29
|
+
constructor(problems){
|
|
30
|
+
super(problems.summary);
|
|
31
|
+
this.cause = problems;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
class Problem {
|
|
35
|
+
toString() {
|
|
36
|
+
return this.message;
|
|
37
|
+
}
|
|
38
|
+
get message() {
|
|
39
|
+
return this.writers.message(this.reason, this.path);
|
|
40
|
+
}
|
|
41
|
+
get reason() {
|
|
42
|
+
return this.writers.reason(this.mustBe, this.was);
|
|
43
|
+
}
|
|
44
|
+
get mustBe() {
|
|
45
|
+
return this.writers.mustBe(this.rule);
|
|
46
|
+
}
|
|
47
|
+
get was() {
|
|
48
|
+
return this.writers.was(new DataWrapper(this.data));
|
|
49
|
+
}
|
|
50
|
+
constructor(code, path, data, rule, writers){
|
|
51
|
+
this.code = code;
|
|
52
|
+
this.path = path;
|
|
53
|
+
this.data = data;
|
|
54
|
+
this.rule = rule;
|
|
55
|
+
this.writers = writers;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
class ProblemArray extends Array {
|
|
59
|
+
add(code, data, input) {
|
|
60
|
+
const problem = new Problem(code, // copy the path to avoid future mutations affecting it
|
|
61
|
+
_pathsTs.Path.from(this.state.path), data, input, this.state.type.meta.problems[code]);
|
|
62
|
+
const pathKey = `${this.state.path}`;
|
|
63
|
+
// TODO: Fix multi
|
|
64
|
+
// const existing = this.byPath[pathKey]
|
|
65
|
+
// if (existing) {
|
|
66
|
+
// if (existing.code === "multi") {
|
|
67
|
+
// existing.message += `\n• ${problem.message}`
|
|
68
|
+
// } else {
|
|
69
|
+
// this.byPath[pathKey] = new Problem(
|
|
70
|
+
// "multi",
|
|
71
|
+
// existing.path,
|
|
72
|
+
// `• ${existing.message}\n• ${problem.message}`
|
|
73
|
+
// )
|
|
74
|
+
// }
|
|
75
|
+
// } else {
|
|
76
|
+
// }
|
|
77
|
+
this.byPath[pathKey] = problem;
|
|
78
|
+
this.push(problem);
|
|
79
|
+
}
|
|
80
|
+
get summary() {
|
|
81
|
+
if (this.length === 1) {
|
|
82
|
+
return this[0].message;
|
|
83
|
+
}
|
|
84
|
+
return this.join("\n");
|
|
85
|
+
}
|
|
86
|
+
toString() {
|
|
87
|
+
return this.summary;
|
|
88
|
+
}
|
|
89
|
+
throw() {
|
|
90
|
+
throw new ArkTypeError(this);
|
|
91
|
+
}
|
|
92
|
+
constructor(state){
|
|
93
|
+
super();
|
|
94
|
+
this.state = state;
|
|
95
|
+
this.byPath = {};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const Problems = ProblemArray;
|
|
99
|
+
const capitalize = (s)=>s[0].toUpperCase() + s.slice(1);
|
|
100
|
+
const describeSubdomains = (subdomains)=>{
|
|
101
|
+
if (subdomains.length === 1) {
|
|
102
|
+
return subdomainDescriptions[subdomains[0]];
|
|
103
|
+
}
|
|
104
|
+
if (subdomains.length === 0) {
|
|
105
|
+
return "never";
|
|
106
|
+
}
|
|
107
|
+
return describeBranches(subdomains.map((subdomain)=>subdomainDescriptions[subdomain]));
|
|
108
|
+
};
|
|
109
|
+
const describeBranches = (descriptions)=>{
|
|
110
|
+
let description = "";
|
|
111
|
+
for(let i = 0; i < descriptions.length - 1; i++){
|
|
112
|
+
description += descriptions[i];
|
|
113
|
+
if (i < descriptions.length - 2) {
|
|
114
|
+
description += ", ";
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
description += ` or ${descriptions[descriptions.length - 1]}`;
|
|
118
|
+
return description;
|
|
119
|
+
};
|
|
120
|
+
const subdomainDescriptions = {
|
|
121
|
+
bigint: "a bigint",
|
|
122
|
+
boolean: "boolean",
|
|
123
|
+
null: "null",
|
|
124
|
+
number: "a number",
|
|
125
|
+
object: "an object",
|
|
126
|
+
string: "a string",
|
|
127
|
+
symbol: "a symbol",
|
|
128
|
+
undefined: "undefined",
|
|
129
|
+
Array: "an array",
|
|
130
|
+
Function: "a function",
|
|
131
|
+
Date: "a Date",
|
|
132
|
+
RegExp: "a RegExp",
|
|
133
|
+
Error: "an Error",
|
|
134
|
+
Map: "a Map",
|
|
135
|
+
Set: "a Set"
|
|
136
|
+
};
|
|
137
|
+
class DataWrapper {
|
|
138
|
+
toString() {
|
|
139
|
+
return (0, _serializeTs.stringify)(this.value);
|
|
140
|
+
}
|
|
141
|
+
get domain() {
|
|
142
|
+
return (0, _domainsTs.domainOf)(this.value);
|
|
143
|
+
}
|
|
144
|
+
// TODO: object kind?
|
|
145
|
+
get subdomain() {
|
|
146
|
+
return (0, _domainsTs.subdomainOf)(this.value);
|
|
147
|
+
}
|
|
148
|
+
get size() {
|
|
149
|
+
return (0, _domainsTs.sizeOf)(this.value);
|
|
150
|
+
}
|
|
151
|
+
get units() {
|
|
152
|
+
return (0, _domainsTs.unitsOf)(this.value);
|
|
153
|
+
}
|
|
154
|
+
get className() {
|
|
155
|
+
return (0, _domainsTs.classNameOf)(this.value);
|
|
156
|
+
}
|
|
157
|
+
constructor(value){
|
|
158
|
+
this.value = value;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
const writeDefaultWasDescription = (data)=>`${data}`;
|
|
162
|
+
const writeDefaultProblemReason = (mustBe, was)=>`must be ${mustBe}${was && ` (was ${was})`}`;
|
|
163
|
+
const writeDefaultProblemMessage = (reason, path)=>path.length ? `${path} ${reason}` : capitalize(reason);
|
|
164
|
+
const compileDefaultProblemWriters = (definitions)=>{
|
|
165
|
+
let code;
|
|
166
|
+
for(code in definitions){
|
|
167
|
+
(_definitions_code = definitions[code]).was ?? (_definitions_code.was = writeDefaultWasDescription);
|
|
168
|
+
(_definitions_code1 = definitions[code]).reason ?? (_definitions_code1.reason = writeDefaultProblemReason);
|
|
169
|
+
(_definitions_code2 = definitions[code]).message ?? (_definitions_code2.message = writeDefaultProblemMessage);
|
|
170
|
+
}
|
|
171
|
+
return definitions;
|
|
172
|
+
};
|
|
173
|
+
const defaultProblemWriters = compileDefaultProblemWriters({
|
|
174
|
+
divisor: {
|
|
175
|
+
mustBe: (divisor)=>divisor === 1 ? `an integer` : `divisible by ${divisor}`
|
|
176
|
+
},
|
|
177
|
+
class: {
|
|
178
|
+
mustBe: (constructor)=>`an instance of ${constructor.name}`,
|
|
179
|
+
was: (data)=>data.className
|
|
180
|
+
},
|
|
181
|
+
domain: {
|
|
182
|
+
mustBe: (domain)=>subdomainDescriptions[domain],
|
|
183
|
+
was: (data)=>data.domain
|
|
184
|
+
},
|
|
185
|
+
domainBranches: {
|
|
186
|
+
mustBe: (domains)=>describeSubdomains(domains),
|
|
187
|
+
was: (data)=>data.domain
|
|
188
|
+
},
|
|
189
|
+
missing: {
|
|
190
|
+
mustBe: ()=>"defined",
|
|
191
|
+
was: ()=>""
|
|
192
|
+
},
|
|
193
|
+
bound: {
|
|
194
|
+
mustBe: (bound)=>`${_scannerTs.Scanner.comparatorDescriptions[bound.comparator]} ${bound.limit}${bound.units ? ` ${bound.units}` : ""}`,
|
|
195
|
+
was: (data)=>`${data.size}`
|
|
196
|
+
},
|
|
197
|
+
regex: {
|
|
198
|
+
mustBe: (regex)=>`a string matching /${regex.source}/`
|
|
199
|
+
},
|
|
200
|
+
value: {
|
|
201
|
+
mustBe: _serializeTs.stringify
|
|
202
|
+
},
|
|
203
|
+
valueBranches: {
|
|
204
|
+
mustBe: (values)=>describeBranches(values.map(_serializeTs.stringify))
|
|
205
|
+
},
|
|
206
|
+
branches: {
|
|
207
|
+
mustBe: (branchProblems)=>{
|
|
208
|
+
const clausesByPath = {};
|
|
209
|
+
for (const problem of branchProblems){
|
|
210
|
+
const pathKey = `${problem.path}`;
|
|
211
|
+
(_clausesByPath = clausesByPath)[_pathKey = pathKey] ?? (_clausesByPath[_pathKey] = []);
|
|
212
|
+
clausesByPath[pathKey].push(problem.mustBe);
|
|
213
|
+
}
|
|
214
|
+
return describeBranches(Object.entries(clausesByPath).map(([path, clauses])=>`${path} must be ${clauses.length === 1 ? clauses[0] : describeBranches(clauses)}`));
|
|
215
|
+
},
|
|
216
|
+
reason: (mustBe, data)=>`${mustBe} (was ${data})`,
|
|
217
|
+
message: (reason, path)=>path.length ? `At ${path}, ${reason}` : reason
|
|
218
|
+
},
|
|
219
|
+
multi: {
|
|
220
|
+
mustBe: (problems)=>"• " + problems.join("\n• "),
|
|
221
|
+
reason: (mustBe)=>mustBe
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
const codes = (0, _genericsTs.keysOf)(defaultProblemWriters);
|
|
225
|
+
const compileProblemOptions = (opts, base = defaultProblemWriters)=>{
|
|
226
|
+
if (!opts) {
|
|
227
|
+
return base;
|
|
228
|
+
}
|
|
229
|
+
const { all , ...byCode } = opts;
|
|
230
|
+
const result = {};
|
|
231
|
+
let code;
|
|
232
|
+
if (all) {
|
|
233
|
+
for (code of codes){
|
|
234
|
+
result[code] = {
|
|
235
|
+
...base[code],
|
|
236
|
+
...all,
|
|
237
|
+
...byCode[code]
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
} else {
|
|
241
|
+
for (code of codes){
|
|
242
|
+
result[code] = byCode[code] ? {
|
|
243
|
+
...base[code],
|
|
244
|
+
...byCode[code]
|
|
245
|
+
} : base[code];
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return result;
|
|
249
|
+
};
|