arktype 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.cjs +9 -0
- package/dist/api.d.ts +10 -0
- package/dist/api.mjs +1 -0
- package/dist/cjs/nodes/{branches.js → branch.js} +65 -10
- package/dist/cjs/nodes/compose.js +2 -3
- package/dist/cjs/nodes/discriminate.js +9 -15
- package/dist/cjs/nodes/node.js +21 -11
- package/dist/cjs/nodes/predicate.js +8 -10
- package/dist/cjs/nodes/rules/class.js +10 -4
- package/dist/cjs/nodes/rules/divisor.js +1 -5
- package/dist/cjs/nodes/rules/props.js +75 -25
- package/dist/cjs/nodes/rules/range.js +2 -6
- package/dist/cjs/nodes/rules/regex.js +1 -5
- package/dist/cjs/nodes/rules/rules.js +23 -63
- 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 +2 -2
- package/dist/cjs/parse/{tuple → ast}/narrow.js +2 -2
- package/dist/cjs/parse/{tuple → ast}/tuple.js +23 -16
- 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 -8
- 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 +2 -2
- package/dist/cjs/scopes/expressions.js +29 -0
- package/dist/cjs/scopes/jsObjects.js +14 -14
- package/dist/cjs/scopes/scope.js +56 -4
- package/dist/cjs/scopes/type.js +4 -7
- package/dist/cjs/traverse/problems.js +67 -53
- package/dist/cjs/traverse/traverse.js +103 -135
- package/dist/cjs/utils/data.js +39 -0
- package/dist/cjs/utils/generics.js +20 -2
- package/dist/cjs/utils/numericLiterals.js +2 -0
- package/dist/cjs/utils/objectKinds.js +1 -2
- package/dist/mjs/nodes/{branches.js → branch.js} +54 -2
- package/dist/mjs/nodes/compose.js +3 -4
- package/dist/mjs/nodes/discriminate.js +9 -15
- package/dist/mjs/nodes/node.js +17 -8
- package/dist/mjs/nodes/predicate.js +4 -5
- package/dist/mjs/nodes/rules/class.js +10 -4
- package/dist/mjs/nodes/rules/divisor.js +1 -5
- package/dist/mjs/nodes/rules/props.js +74 -27
- package/dist/mjs/nodes/rules/range.js +2 -6
- package/dist/mjs/nodes/rules/regex.js +1 -5
- package/dist/mjs/nodes/rules/rules.js +23 -62
- 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 +2 -2
- package/dist/mjs/parse/ast/narrow.js +7 -0
- package/dist/mjs/parse/{tuple → ast}/tuple.js +24 -17
- 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 -9
- 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/jsObjects.js +14 -14
- package/dist/mjs/scopes/scope.js +55 -3
- package/dist/mjs/scopes/type.js +4 -7
- package/dist/mjs/traverse/problems.js +68 -53
- package/dist/mjs/traverse/traverse.js +107 -139
- package/dist/mjs/utils/data.js +24 -0
- package/dist/mjs/utils/generics.js +18 -1
- package/dist/mjs/utils/numericLiterals.js +1 -0
- package/dist/mjs/utils/objectKinds.js +1 -1
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/nodes/branch.d.ts +27 -0
- package/dist/types/nodes/compose.d.ts +5 -10
- package/dist/types/nodes/discriminate.d.ts +3 -3
- package/dist/types/nodes/infer.d.ts +21 -20
- package/dist/types/nodes/node.d.ts +12 -16
- package/dist/types/nodes/predicate.d.ts +4 -59
- package/dist/types/nodes/rules/class.d.ts +3 -2
- package/dist/types/nodes/rules/divisor.d.ts +2 -1
- package/dist/types/nodes/rules/props.d.ts +19 -11
- package/dist/types/nodes/rules/range.d.ts +3 -2
- package/dist/types/nodes/rules/regex.d.ts +2 -1
- package/dist/types/nodes/rules/rules.d.ts +12 -14
- 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/{tuple → ast}/morph.d.ts +3 -3
- 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/dynamic.d.ts +1 -1
- package/dist/types/parse/string/reduce/shared.d.ts +6 -5
- package/dist/types/parse/string/reduce/static.d.ts +11 -6
- package/dist/types/parse/string/shift/operand/unenclosed.d.ts +2 -2
- package/dist/types/parse/string/shift/operator/bounds.d.ts +4 -3
- package/dist/types/parse/string/shift/operator/divisor.d.ts +5 -1
- package/dist/types/parse/string/string.d.ts +2 -3
- package/dist/types/scopes/expressions.d.ts +48 -0
- package/dist/types/scopes/scope.d.ts +17 -7
- package/dist/types/scopes/type.d.ts +13 -15
- package/dist/types/traverse/problems.d.ts +17 -41
- package/dist/types/traverse/traverse.d.ts +85 -52
- 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 +13 -10
- package/dist/types/utils/numericLiterals.d.ts +1 -0
- package/dist/types/utils/objectKinds.d.ts +43 -43
- package/dist/types/utils/paths.d.ts +2 -0
- package/package.json +1 -2
- package/dist/cjs/nodes/functors.js +0 -35
- package/dist/cjs/nodes/rules/objectKind.js +0 -78
- package/dist/cjs/parse/tuple/keyof.js +0 -45
- package/dist/cjs/utils/size.js +0 -16
- package/dist/mjs/nodes/functors.js +0 -27
- package/dist/mjs/nodes/rules/objectKind.js +0 -63
- package/dist/mjs/parse/tuple/keyof.js +0 -37
- package/dist/mjs/parse/tuple/narrow.js +0 -7
- package/dist/mjs/utils/size.js +0 -2
- package/dist/types/nodes/branches.d.ts +0 -14
- package/dist/types/nodes/functors.d.ts +0 -6
- package/dist/types/nodes/rules/objectKind.d.ts +0 -15
- package/dist/types/parse/string/ast.d.ts +0 -45
- 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
package/dist/mjs/nodes/node.js
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
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";
|
|
7
|
+
import { mappedKeys } from "./rules/props.js";
|
|
7
8
|
export const nodeIntersection = (l, r, state)=>{
|
|
8
9
|
state.domain = undefined;
|
|
9
10
|
const lResolution = state.type.meta.scope.resolveNode(l);
|
|
10
11
|
const rResolution = state.type.meta.scope.resolveNode(r);
|
|
11
12
|
const result = resolutionIntersection(lResolution, rResolution, state);
|
|
12
13
|
if (typeof result === "object" && !hasKeys(result)) {
|
|
13
|
-
return hasKeys(state.disjoints) ? anonymousDisjoint() : state.addDisjoint("domain",
|
|
14
|
+
return hasKeys(state.disjoints) ? anonymousDisjoint() : state.addDisjoint("domain", objectKeysOf(lResolution), objectKeysOf(rResolution));
|
|
14
15
|
}
|
|
15
16
|
return result === lResolution ? l : result === rResolution ? r : result;
|
|
16
17
|
};
|
|
@@ -25,16 +26,16 @@ const resolutionIntersection = composeKeyedIntersection((domain, l, r, context)=
|
|
|
25
26
|
}, {
|
|
26
27
|
onEmpty: "omit"
|
|
27
28
|
});
|
|
28
|
-
export const
|
|
29
|
+
export const rootIntersection = (l, r, type)=>{
|
|
29
30
|
const state = new IntersectionState(type, "&");
|
|
30
31
|
const result = nodeIntersection(l, r, state);
|
|
31
32
|
return isDisjoint(result) ? throwParseError(compileDisjointReasonsMessage(state.disjoints)) : isEquality(result) ? l : result;
|
|
32
33
|
};
|
|
33
|
-
export const
|
|
34
|
+
export const rootUnion = (l, r, type)=>{
|
|
34
35
|
const lResolution = type.meta.scope.resolveNode(l);
|
|
35
36
|
const rResolution = type.meta.scope.resolveNode(r);
|
|
36
37
|
const result = {};
|
|
37
|
-
const domains =
|
|
38
|
+
const domains = objectKeysOf({
|
|
38
39
|
...lResolution,
|
|
39
40
|
...rResolution
|
|
40
41
|
});
|
|
@@ -43,7 +44,7 @@ export const union = (l, r, type)=>{
|
|
|
43
44
|
}
|
|
44
45
|
return result;
|
|
45
46
|
};
|
|
46
|
-
const hasImpliedDomain = (flatPredicate)=>flatPredicate[0] && (flatPredicate[0][0] === "
|
|
47
|
+
const hasImpliedDomain = (flatPredicate)=>flatPredicate[0] && (flatPredicate[0][0] === "value" || flatPredicate[0][0] === "class");
|
|
47
48
|
export const flattenType = (type)=>{
|
|
48
49
|
const ctx = {
|
|
49
50
|
type,
|
|
@@ -74,7 +75,7 @@ export const flattenNode = (node, ctx)=>{
|
|
|
74
75
|
]
|
|
75
76
|
] : resolution.flat;
|
|
76
77
|
}
|
|
77
|
-
const domains =
|
|
78
|
+
const domains = objectKeysOf(node);
|
|
78
79
|
if (domains.length === 1) {
|
|
79
80
|
const domain = domains[0];
|
|
80
81
|
const predicate = node[domain];
|
|
@@ -106,3 +107,11 @@ export const flattenNode = (node, ctx)=>{
|
|
|
106
107
|
export const isLiteralNode = (node, domain)=>{
|
|
107
108
|
return resolutionExtendsDomain(node, domain) && isLiteralCondition(node[domain]);
|
|
108
109
|
};
|
|
110
|
+
export const toArrayNode = (node)=>({
|
|
111
|
+
object: {
|
|
112
|
+
class: "Array",
|
|
113
|
+
props: {
|
|
114
|
+
[mappedKeys.index]: node
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
});
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { listFrom, objectKeysOf } from "../utils/generics.js";
|
|
2
2
|
import { isArray } from "../utils/objectKinds.js";
|
|
3
|
-
import { compareBranches, isBranchComparison } from "./
|
|
3
|
+
import { branchIntersection, compareBranches, flattenBranch, isBranchComparison } from "./branch.js";
|
|
4
4
|
import { equality, IntersectionState, isEquality } from "./compose.js";
|
|
5
5
|
import { flattenBranches } from "./discriminate.js";
|
|
6
|
-
import
|
|
7
|
-
export const branchIsTransformation = (branch)=>"rules" in branch;
|
|
6
|
+
import "./rules/rules.js";
|
|
8
7
|
const emptyRulesIfTrue = (predicate)=>predicate === true ? {} : predicate;
|
|
9
8
|
export const comparePredicates = (l, r, context)=>{
|
|
10
9
|
if (l === true && r === true) {
|
|
@@ -70,6 +69,6 @@ export const flattenPredicate = (predicate, context)=>{
|
|
|
70
69
|
};
|
|
71
70
|
export const isLiteralCondition = (predicate)=>typeof predicate === "object" && "value" in predicate;
|
|
72
71
|
export const resolutionExtendsDomain = (resolution, domain)=>{
|
|
73
|
-
const domains =
|
|
72
|
+
const domains = objectKeysOf(resolution);
|
|
74
73
|
return domains.length === 1 && domains[0] === domain;
|
|
75
74
|
};
|
|
@@ -1,7 +1,13 @@
|
|
|
1
|
+
import { defaultObjectKinds, objectKindOf } from "../../utils/objectKinds.js";
|
|
1
2
|
import { composeIntersection, equality } from "../compose.js";
|
|
2
|
-
export const classIntersection = composeIntersection((l, r, state)=>
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
export const classIntersection = composeIntersection((l, r, state)=>{
|
|
4
|
+
const lClass = typeof l === "string" ? defaultObjectKinds[l] : l;
|
|
5
|
+
const rClass = typeof r === "string" ? defaultObjectKinds[r] : r;
|
|
6
|
+
return lClass === rClass ? equality() : lClass instanceof rClass ? l : rClass instanceof lClass ? r : state.addDisjoint("class", l, r);
|
|
7
|
+
});
|
|
8
|
+
export const checkClass = (expectedClass, state)=>{
|
|
9
|
+
if (typeof expectedClass === "string") {
|
|
10
|
+
return objectKindOf(state.data) === expectedClass || state.problems.create("class", expectedClass);
|
|
6
11
|
}
|
|
12
|
+
return state.data instanceof expectedClass || state.problems.create("class", expectedClass);
|
|
7
13
|
};
|
|
@@ -12,8 +12,4 @@ const greatestCommonDivisor = (l, r)=>{
|
|
|
12
12
|
}
|
|
13
13
|
return greatestCommonDivisor;
|
|
14
14
|
};
|
|
15
|
-
export const checkDivisor = (
|
|
16
|
-
if (data % divisor !== 0) {
|
|
17
|
-
state.problems.add("divisor", data, divisor);
|
|
18
|
-
}
|
|
19
|
-
};
|
|
15
|
+
export const checkDivisor = (divisor, state)=>state.data % divisor === 0 || state.problems.create("divisor", divisor);
|
|
@@ -1,8 +1,50 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { hasKey } from "../../utils/generics.js";
|
|
2
|
+
import { composeIntersection, composeKeyedIntersection, equality, isDisjoint, isEquality } from "../compose.js";
|
|
3
|
+
import { flattenNode, isLiteralNode, nodeIntersection } from "../node.js";
|
|
3
4
|
export const isOptional = (prop)=>prop[0] === "?";
|
|
4
|
-
const
|
|
5
|
-
export const
|
|
5
|
+
export const isPrerequisite = (prop)=>prop[0] === "!";
|
|
6
|
+
export const mappedKeys = {
|
|
7
|
+
index: "[index]"
|
|
8
|
+
};
|
|
9
|
+
export const propToNode = (prop)=>isOptional(prop) || isPrerequisite(prop) ? prop[1] : prop;
|
|
10
|
+
const getTupleLengthIfPresent = (result)=>{
|
|
11
|
+
if (typeof result.length === "object" && isPrerequisite(result.length) && typeof result.length[1] !== "string" && isLiteralNode(result.length[1], "number")) {
|
|
12
|
+
return result.length[1].number.value;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
export const propsIntersection = composeIntersection((l, r, state)=>{
|
|
16
|
+
const result = propKeysIntersection(l, r, state);
|
|
17
|
+
if (typeof result === "symbol") {
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
20
|
+
const lengthValue = getTupleLengthIfPresent(result);
|
|
21
|
+
if (lengthValue === undefined || !hasKey(result, mappedKeys.index)) {
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
// if we are at this point, we have an array with an exact length (i.e.
|
|
25
|
+
// a tuple) and an index signature. Intersection each tuple item with
|
|
26
|
+
// the index signature node and remove the index signature via a new
|
|
27
|
+
// updated result, copied from result to avoid mutating existing references.
|
|
28
|
+
const { [mappedKeys.index]: indexProp , ...updatedResult } = result;
|
|
29
|
+
const indexNode = propToNode(indexProp);
|
|
30
|
+
for(let i = 0; i < lengthValue; i++){
|
|
31
|
+
if (!updatedResult[i]) {
|
|
32
|
+
updatedResult[i] = indexNode;
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const existingNodeAtIndex = propToNode(updatedResult[i]);
|
|
36
|
+
state.path.push(`${i}`);
|
|
37
|
+
const updatedResultAtIndex = nodeIntersection(existingNodeAtIndex, indexNode, state);
|
|
38
|
+
state.path.pop();
|
|
39
|
+
if (isDisjoint(updatedResultAtIndex)) {
|
|
40
|
+
return updatedResultAtIndex;
|
|
41
|
+
} else if (!isEquality(updatedResultAtIndex) && updatedResultAtIndex !== existingNodeAtIndex) {
|
|
42
|
+
updatedResult[i] = updatedResultAtIndex;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return updatedResult;
|
|
46
|
+
});
|
|
47
|
+
const propKeysIntersection = composeKeyedIntersection((propKey, l, r, context)=>{
|
|
6
48
|
if (l === undefined) {
|
|
7
49
|
return r === undefined ? equality() : r;
|
|
8
50
|
}
|
|
@@ -10,7 +52,7 @@ export const propsIntersection = composeIntersection(composeKeyedIntersection((p
|
|
|
10
52
|
return l;
|
|
11
53
|
}
|
|
12
54
|
context.path.push(propKey);
|
|
13
|
-
const result = nodeIntersection(
|
|
55
|
+
const result = nodeIntersection(propToNode(l), propToNode(r), context);
|
|
14
56
|
context.path.pop();
|
|
15
57
|
const resultIsOptional = isOptional(l) && isOptional(r);
|
|
16
58
|
if (isDisjoint(result) && resultIsOptional) {
|
|
@@ -22,36 +64,41 @@ export const propsIntersection = composeIntersection(composeKeyedIntersection((p
|
|
|
22
64
|
return result;
|
|
23
65
|
}, {
|
|
24
66
|
onEmpty: "bubble"
|
|
25
|
-
})
|
|
67
|
+
});
|
|
26
68
|
export const flattenProps = (entries, props, ctx)=>{
|
|
27
|
-
const requiredProps = [];
|
|
28
|
-
const optionalProps = [];
|
|
29
69
|
for(const k in props){
|
|
30
70
|
const prop = props[k];
|
|
31
71
|
ctx.path.push(k);
|
|
32
|
-
if (
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
flattenNode(prop
|
|
72
|
+
if (k === mappedKeys.index) {
|
|
73
|
+
entries.push([
|
|
74
|
+
"indexProp",
|
|
75
|
+
flattenNode(propToNode(prop), ctx)
|
|
76
|
+
]);
|
|
77
|
+
} else if (isOptional(prop)) {
|
|
78
|
+
entries.push([
|
|
79
|
+
"optionalProp",
|
|
80
|
+
[
|
|
81
|
+
k,
|
|
82
|
+
flattenNode(prop[1], ctx)
|
|
83
|
+
]
|
|
84
|
+
]);
|
|
85
|
+
} else if (isPrerequisite(prop)) {
|
|
86
|
+
entries.push([
|
|
87
|
+
"prerequisiteProp",
|
|
88
|
+
[
|
|
89
|
+
k,
|
|
90
|
+
flattenNode(prop[1], ctx)
|
|
91
|
+
]
|
|
36
92
|
]);
|
|
37
93
|
} else {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
94
|
+
entries.push([
|
|
95
|
+
"requiredProp",
|
|
96
|
+
[
|
|
97
|
+
k,
|
|
98
|
+
flattenNode(prop, ctx)
|
|
99
|
+
]
|
|
41
100
|
]);
|
|
42
101
|
}
|
|
43
102
|
ctx.path.pop();
|
|
44
103
|
}
|
|
45
|
-
if (requiredProps.length) {
|
|
46
|
-
entries.push([
|
|
47
|
-
"requiredProps",
|
|
48
|
-
requiredProps
|
|
49
|
-
]);
|
|
50
|
-
}
|
|
51
|
-
if (optionalProps.length) {
|
|
52
|
-
entries.push([
|
|
53
|
-
"optionalProps",
|
|
54
|
-
optionalProps
|
|
55
|
-
]);
|
|
56
|
-
}
|
|
57
104
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { sizeOf } from "../../utils/
|
|
1
|
+
import { sizeOf } from "../../utils/data.js";
|
|
2
2
|
import { composeIntersection, equality } from "../compose.js";
|
|
3
3
|
export const minComparators = {
|
|
4
4
|
">": true,
|
|
@@ -74,11 +74,7 @@ export const flattenRange = (entries, range, ctx)=>{
|
|
|
74
74
|
]);
|
|
75
75
|
}
|
|
76
76
|
};
|
|
77
|
-
export const checkBound = (data, bound,
|
|
78
|
-
if (!comparatorCheckers[bound.comparator](sizeOf(data), bound.limit)) {
|
|
79
|
-
state.problems.add("bound", data, bound);
|
|
80
|
-
}
|
|
81
|
-
};
|
|
77
|
+
export const checkBound = (bound, state)=>comparatorCheckers[bound.comparator](sizeOf(state.data), bound.limit) || state.problems.create("bound", bound);
|
|
82
78
|
const comparatorCheckers = {
|
|
83
79
|
"<": (size, limit)=>size < limit,
|
|
84
80
|
">": (size, limit)=>size > limit,
|
|
@@ -7,9 +7,5 @@ export const getRegex = (source)=>{
|
|
|
7
7
|
}
|
|
8
8
|
return regexCache[source];
|
|
9
9
|
};
|
|
10
|
-
export const checkRegex = (
|
|
11
|
-
if (!getRegex(source).test(data)) {
|
|
12
|
-
state.problems.add("regex", data, state.path.length === 0 && state.type.name !== "type" ? state.type.name : `/${source}/`);
|
|
13
|
-
}
|
|
14
|
-
};
|
|
10
|
+
export const checkRegex = (source, state)=>getRegex(source).test(state.data) || state.problems.create("regex", `/${source}/`);
|
|
15
11
|
export const regexIntersection = composeIntersection(collapsibleListUnion);
|
|
@@ -1,47 +1,14 @@
|
|
|
1
|
-
import { writeImplicitNeverMessage } from "../../parse/string/ast.js";
|
|
2
|
-
import { throwParseError } from "../../utils/errors.js";
|
|
3
1
|
import { listFrom } from "../../utils/generics.js";
|
|
4
|
-
import { composeIntersection, composeKeyedIntersection, equality
|
|
5
|
-
import { branchIsTransformation } from "../predicate.js";
|
|
2
|
+
import { composeIntersection, composeKeyedIntersection, equality } from "../compose.js";
|
|
6
3
|
import { classIntersection } from "./class.js";
|
|
7
4
|
import { collapsibleListUnion } from "./collapsibleSet.js";
|
|
8
5
|
import { divisorIntersection } from "./divisor.js";
|
|
9
|
-
import { flattenObjectKind, objectKindIntersection } from "./objectKind.js";
|
|
10
6
|
import { flattenProps, propsIntersection } from "./props.js";
|
|
11
7
|
import { flattenRange, rangeIntersection } from "./range.js";
|
|
12
8
|
import { regexIntersection } from "./regex.js";
|
|
13
|
-
const rulesOf = (branch)=>branch.rules ?? branch;
|
|
14
|
-
export const branchIntersection = (l, r, state)=>{
|
|
15
|
-
const lRules = rulesOf(l);
|
|
16
|
-
const rRules = rulesOf(r);
|
|
17
|
-
const rulesResult = rulesIntersection(lRules, rRules, state);
|
|
18
|
-
if ("morph" in l) {
|
|
19
|
-
if ("morph" in r) {
|
|
20
|
-
if (l.morph === r.morph) {
|
|
21
|
-
return isEquality(rulesResult) || isDisjoint(rulesResult) ? rulesResult : {
|
|
22
|
-
rules: rulesResult,
|
|
23
|
-
morph: l.morph
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
return state.lastOperator === "&" ? throwParseError(writeImplicitNeverMessage(state.path, "Intersection", "of morphs")) : {};
|
|
27
|
-
}
|
|
28
|
-
return isDisjoint(rulesResult) ? rulesResult : {
|
|
29
|
-
rules: isEquality(rulesResult) ? l.rules : rulesResult,
|
|
30
|
-
morph: l.morph
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
if ("morph" in r) {
|
|
34
|
-
return isDisjoint(rulesResult) ? rulesResult : {
|
|
35
|
-
rules: isEquality(rulesResult) ? r.rules : rulesResult,
|
|
36
|
-
morph: r.morph
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
return rulesResult;
|
|
40
|
-
};
|
|
41
9
|
export const rulesIntersection = (l, r, state)=>"value" in l ? "value" in r ? l.value === r.value ? equality() : state.addDisjoint("value", l.value, r.value) : literalSatisfiesRules(l.value, r, state) ? l : state.addDisjoint("leftAssignability", l, r) : "value" in r ? literalSatisfiesRules(r.value, l, state) ? r : state.addDisjoint("rightAssignability", l, r) : narrowableRulesIntersection(l, r, state);
|
|
42
10
|
const narrowIntersection = composeIntersection(collapsibleListUnion);
|
|
43
11
|
export const narrowableRulesIntersection = composeKeyedIntersection({
|
|
44
|
-
objectKind: objectKindIntersection,
|
|
45
12
|
divisor: divisorIntersection,
|
|
46
13
|
regex: regexIntersection,
|
|
47
14
|
props: propsIntersection,
|
|
@@ -51,8 +18,19 @@ export const narrowableRulesIntersection = composeKeyedIntersection({
|
|
|
51
18
|
}, {
|
|
52
19
|
onEmpty: "bubble"
|
|
53
20
|
});
|
|
21
|
+
export const flattenRules = (rules, ctx)=>{
|
|
22
|
+
const entries = [];
|
|
23
|
+
let k;
|
|
24
|
+
for(k in rules){
|
|
25
|
+
ruleFlatteners[k](entries, rules[k], ctx);
|
|
26
|
+
}
|
|
27
|
+
// Some entries with the same precedence, e.g. morphs flattened from a list,
|
|
28
|
+
// rely on the fact that JS's builtin sort is stable to behave as expected
|
|
29
|
+
// when traversed:
|
|
30
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
|
|
31
|
+
return entries.sort((l, r)=>precedenceMap[l[0]] - precedenceMap[r[0]]);
|
|
32
|
+
};
|
|
54
33
|
const ruleFlatteners = {
|
|
55
|
-
objectKind: flattenObjectKind,
|
|
56
34
|
regex: (entries, rule)=>{
|
|
57
35
|
for (const source of listFrom(rule)){
|
|
58
36
|
entries.push([
|
|
@@ -97,42 +75,25 @@ export const precedenceMap = {
|
|
|
97
75
|
value: 0,
|
|
98
76
|
domains: 0,
|
|
99
77
|
branches: 0,
|
|
100
|
-
objectKind: 0,
|
|
101
78
|
switch: 0,
|
|
102
79
|
alias: 0,
|
|
80
|
+
class: 0,
|
|
103
81
|
// Shallow: All shallow checks will be performed even if one or more fail
|
|
104
|
-
class: 1,
|
|
105
82
|
regex: 1,
|
|
106
83
|
divisor: 1,
|
|
107
84
|
bound: 1,
|
|
85
|
+
// Prerequisite: These are deep checks with special priority, e.g. the
|
|
86
|
+
// length of a tuple, which causes other deep props not to be checked if it
|
|
87
|
+
// is invalid
|
|
88
|
+
prerequisiteProp: 2,
|
|
108
89
|
// Deep: Performed if all shallow checks pass, even if one or more deep checks fail
|
|
109
|
-
|
|
110
|
-
|
|
90
|
+
requiredProp: 3,
|
|
91
|
+
optionalProp: 3,
|
|
92
|
+
indexProp: 3,
|
|
111
93
|
// Narrow: Only performed if all shallow and deep checks pass
|
|
112
|
-
narrow:
|
|
94
|
+
narrow: 4,
|
|
113
95
|
// Morph: Only performed if all validation passes
|
|
114
|
-
morph:
|
|
115
|
-
};
|
|
116
|
-
export const flattenBranch = (branch, ctx)=>{
|
|
117
|
-
if (branchIsTransformation(branch)) {
|
|
118
|
-
const result = flattenRules(branch.rules, ctx);
|
|
119
|
-
if (branch.morph) {
|
|
120
|
-
result.push([
|
|
121
|
-
"morph",
|
|
122
|
-
branch.morph
|
|
123
|
-
]);
|
|
124
|
-
}
|
|
125
|
-
return result;
|
|
126
|
-
}
|
|
127
|
-
return flattenRules(branch, ctx);
|
|
128
|
-
};
|
|
129
|
-
const flattenRules = (rules, ctx)=>{
|
|
130
|
-
const entries = [];
|
|
131
|
-
let k;
|
|
132
|
-
for(k in rules){
|
|
133
|
-
ruleFlatteners[k](entries, rules[k], ctx);
|
|
134
|
-
}
|
|
135
|
-
return entries.sort((l, r)=>precedenceMap[l[0]] - precedenceMap[r[0]]);
|
|
96
|
+
morph: 5
|
|
136
97
|
};
|
|
137
98
|
export const literalSatisfiesRules = (data, rules, state)=>!state.type.meta.scope.type([
|
|
138
99
|
"node",
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const writeUnboundableMessage = (root)=>`Bounded expression ${root} must be a number, string or array`;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { hasDomain } from "../../utils/domains.js";
|
|
2
2
|
import { throwParseError } from "../../utils/errors.js";
|
|
3
|
-
import {
|
|
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
6
|
export const distributeFunctionToNode = (distributableFunction, inputNode, ctx, ruleKey)=>{
|
|
7
|
-
const domains =
|
|
7
|
+
const domains = objectKeysOf(ctx.type.meta.scope.resolveNode(inputNode));
|
|
8
8
|
if (!hasDomain(distributableFunction, "object")) {
|
|
9
9
|
return throwParseError(writeMalformedDistributableFunctionMessage(distributableFunction));
|
|
10
10
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const writeIndivisibleMessage = (root)=>`Divisibility operand ${root} must be a number`;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { disjointDescriptionWriters } from "../../nodes/compose.js";
|
|
2
|
-
import {
|
|
2
|
+
import { objectKeysOf } from "../../utils/generics.js";
|
|
3
3
|
export const compileDisjointReasonsMessage = (disjoints)=>{
|
|
4
|
-
const paths =
|
|
4
|
+
const paths = objectKeysOf(disjoints);
|
|
5
5
|
if (paths.length === 1) {
|
|
6
6
|
const path = paths[0];
|
|
7
7
|
return `${path === "/" ? "" : `At ${path}: `}Intersection of ${disjointDescriptionWriters[disjoints[path].kind](disjoints[path])} results in an unsatisfiable type`;
|
|
@@ -14,6 +14,3 @@ export const compileDisjointReasonsMessage = (disjoints)=>{
|
|
|
14
14
|
return message;
|
|
15
15
|
};
|
|
16
16
|
export const writeImplicitNeverMessage = (path, operator, description)=>`${path.length ? `At ${path}: ` : ""}${operator} ${description ? `${description} ` : ""}results in an unsatisfiable type`;
|
|
17
|
-
export const writeUndiscriminatableMorphUnionMessage = (path)=>`${path === "/" ? "A" : `At ${path}, a`} union including one or more morphs must be discriminatable`;
|
|
18
|
-
export const writeIndivisibleMessage = (root)=>`Divisibility operand ${root} must be a number`;
|
|
19
|
-
export const writeUnboundableMessage = (root)=>`Bounded expression ${root} must be a number, string or array`;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
var _keyNode, _keyType, _keyNode1, _keyNode2;
|
|
2
|
+
import { mappedKeys } from "../../nodes/rules/props.js";
|
|
3
|
+
import { throwInternalError } from "../../utils/errors.js";
|
|
4
|
+
import { deepFreeze } from "../../utils/freeze.js";
|
|
5
|
+
import { listFrom, objectKeysOf, prototypeKeysOf } from "../../utils/generics.js";
|
|
6
|
+
import { tryParseWellFormedInteger, wellFormedNonNegativeIntegerMatcher } from "../../utils/numericLiterals.js";
|
|
7
|
+
import { defaultObjectKinds } from "../../utils/objectKinds.js";
|
|
8
|
+
import { stringify } from "../../utils/serialize.js";
|
|
9
|
+
import { parseDefinition } from "../definition.js";
|
|
10
|
+
import { writeImplicitNeverMessage } from "./intersection.js";
|
|
11
|
+
const arrayIndexStringBranch = deepFreeze({
|
|
12
|
+
regex: wellFormedNonNegativeIntegerMatcher.source
|
|
13
|
+
});
|
|
14
|
+
const arrayIndexNumberBranch = deepFreeze({
|
|
15
|
+
range: {
|
|
16
|
+
min: {
|
|
17
|
+
comparator: ">=",
|
|
18
|
+
limit: 1
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
divisor: 1
|
|
22
|
+
});
|
|
23
|
+
export const parseKeyOfTuple = (def, ctx)=>{
|
|
24
|
+
const resolution = ctx.type.meta.scope.resolveNode(parseDefinition(def[1], ctx));
|
|
25
|
+
const predicateKeys = objectKeysOf(resolution).map((domain)=>keysOfPredicate(domain, resolution[domain]));
|
|
26
|
+
const sharedKeys = sharedKeysOf(predicateKeys);
|
|
27
|
+
if (!sharedKeys.length) {
|
|
28
|
+
return writeImplicitNeverMessage(ctx.path, "keyof");
|
|
29
|
+
}
|
|
30
|
+
const keyNode = {};
|
|
31
|
+
for (const key of sharedKeys){
|
|
32
|
+
const keyType = typeof key;
|
|
33
|
+
if (keyType === "string" || keyType === "number" || keyType === "symbol") {
|
|
34
|
+
(_keyNode = keyNode)[_keyType = keyType] ?? (_keyNode[_keyType] = []);
|
|
35
|
+
keyNode[keyType].push({
|
|
36
|
+
value: key
|
|
37
|
+
});
|
|
38
|
+
} else if (key === wellFormedNonNegativeIntegerMatcher) {
|
|
39
|
+
(_keyNode1 = keyNode).string ?? (_keyNode1.string = []);
|
|
40
|
+
keyNode.string.push(arrayIndexStringBranch);
|
|
41
|
+
(_keyNode2 = keyNode).number ?? (_keyNode2.number = []);
|
|
42
|
+
keyNode.number.push(arrayIndexNumberBranch);
|
|
43
|
+
} else {
|
|
44
|
+
return throwInternalError(`Unexpected keyof key '${stringify(key)}'`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return Object.fromEntries(Object.entries(keyNode).map(([domain, branches])=>[
|
|
48
|
+
domain,
|
|
49
|
+
branches.length === 1 ? branches[0] : branches
|
|
50
|
+
]));
|
|
51
|
+
};
|
|
52
|
+
const baseKeysByDomain = {
|
|
53
|
+
bigint: prototypeKeysOf(0n),
|
|
54
|
+
boolean: prototypeKeysOf(false),
|
|
55
|
+
null: [],
|
|
56
|
+
number: prototypeKeysOf(0),
|
|
57
|
+
// TS doesn't include the Object prototype in keyof, so keyof object is never
|
|
58
|
+
object: [],
|
|
59
|
+
string: prototypeKeysOf(""),
|
|
60
|
+
symbol: prototypeKeysOf(Symbol()),
|
|
61
|
+
undefined: []
|
|
62
|
+
};
|
|
63
|
+
const keysOfPredicate = (domain, predicate)=>domain !== "object" || predicate === true ? baseKeysByDomain[domain] : sharedKeysOf(listFrom(predicate).map((branch)=>keysOfObjectBranch(branch)));
|
|
64
|
+
const sharedKeysOf = (keyBranches)=>{
|
|
65
|
+
if (!keyBranches.length) {
|
|
66
|
+
return [];
|
|
67
|
+
}
|
|
68
|
+
let sharedKeys = keyBranches[0];
|
|
69
|
+
for(let i = 1; i < keyBranches.length; i++){
|
|
70
|
+
// we can filter directly by equality here because the RegExp we're
|
|
71
|
+
// using will always be reference equal to
|
|
72
|
+
// wellFormedNonNegativeIntegerMatcher
|
|
73
|
+
sharedKeys = sharedKeys.filter((k)=>keyBranches[i].includes(k));
|
|
74
|
+
}
|
|
75
|
+
return sharedKeys;
|
|
76
|
+
};
|
|
77
|
+
const keysOfObjectBranch = (branch)=>{
|
|
78
|
+
const result = [];
|
|
79
|
+
if ("props" in branch) {
|
|
80
|
+
for (const key of Object.keys(branch.props)){
|
|
81
|
+
if (key === mappedKeys.index) {
|
|
82
|
+
result.push(wellFormedNonNegativeIntegerMatcher);
|
|
83
|
+
} else if (!result.includes(key)) {
|
|
84
|
+
result.push(key);
|
|
85
|
+
if (wellFormedNonNegativeIntegerMatcher.test(key)) {
|
|
86
|
+
// allow numeric access to keys
|
|
87
|
+
result.push(tryParseWellFormedInteger(key, `Unexpectedly failed to parse an integer from key '${key}'`));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if ("class" in branch) {
|
|
93
|
+
const constructor = typeof branch.class === "string" ? defaultObjectKinds[branch.class] : branch.class;
|
|
94
|
+
for (const key1 of prototypeKeysOf(constructor.prototype)){
|
|
95
|
+
if (!result.includes(key1)) {
|
|
96
|
+
result.push(key1);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isMorphBranch } from "../../nodes/branch.js";
|
|
2
2
|
import { throwInternalError, throwParseError } from "../../utils/errors.js";
|
|
3
3
|
import { isArray } from "../../utils/objectKinds.js";
|
|
4
4
|
import { stringify } from "../../utils/serialize.js";
|
|
@@ -27,7 +27,7 @@ export const parseMorphTuple = (def, ctx)=>{
|
|
|
27
27
|
}
|
|
28
28
|
return result;
|
|
29
29
|
};
|
|
30
|
-
const applyMorph = (branch, morph)=>
|
|
30
|
+
const applyMorph = (branch, morph)=>isMorphBranch(branch) ? {
|
|
31
31
|
...branch,
|
|
32
32
|
morph: branch.morph ? Array.isArray(branch.morph) ? [
|
|
33
33
|
...branch.morph,
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { rootIntersection } from "../../nodes/node.js";
|
|
2
|
+
import { parseDefinition } from "../definition.js";
|
|
3
|
+
import { distributeFunctionToNode } from "./distributableFunction.js";
|
|
4
|
+
export const parseNarrowTuple = (def, ctx)=>{
|
|
5
|
+
const inputNode = parseDefinition(def[0], ctx);
|
|
6
|
+
return rootIntersection(inputNode, distributeFunctionToNode(def[2], inputNode, ctx, "narrow"), ctx.type);
|
|
7
|
+
};
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { intersection, union } from "../../nodes/node.js";
|
|
1
|
+
import { rootIntersection, rootUnion, toArrayNode } from "../../nodes/node.js";
|
|
3
2
|
import { domainOf } from "../../utils/domains.js";
|
|
4
3
|
import { throwParseError } from "../../utils/errors.js";
|
|
5
4
|
import { parseDefinition } from "../definition.js";
|
|
@@ -8,13 +7,25 @@ import { parseKeyOfTuple } from "./keyof.js";
|
|
|
8
7
|
import { parseMorphTuple } from "./morph.js";
|
|
9
8
|
import { parseNarrowTuple } from "./narrow.js";
|
|
10
9
|
export const parseTuple = (def, ctx)=>{
|
|
11
|
-
if (
|
|
12
|
-
return
|
|
10
|
+
if (isIndexOneExpression(def)) {
|
|
11
|
+
return indexOneParsers[def[1]](def, ctx);
|
|
13
12
|
}
|
|
14
|
-
if (
|
|
13
|
+
if (isIndexZeroExpression(def)) {
|
|
15
14
|
return prefixParsers[def[0]](def, ctx);
|
|
16
15
|
}
|
|
17
|
-
const props = {
|
|
16
|
+
const props = {
|
|
17
|
+
// length is created as a prerequisite prop, ensuring if it is invalid,
|
|
18
|
+
// no other props will be checked, which is usually desirable for tuple
|
|
19
|
+
// definitions.
|
|
20
|
+
length: [
|
|
21
|
+
"!",
|
|
22
|
+
{
|
|
23
|
+
number: {
|
|
24
|
+
value: def.length
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
};
|
|
18
29
|
for(let i = 0; i < def.length; i++){
|
|
19
30
|
ctx.path.push(`${i}`);
|
|
20
31
|
props[i] = parseDefinition(def[i], ctx);
|
|
@@ -22,12 +33,8 @@ export const parseTuple = (def, ctx)=>{
|
|
|
22
33
|
}
|
|
23
34
|
return {
|
|
24
35
|
object: {
|
|
25
|
-
|
|
26
|
-
props
|
|
27
|
-
range: {
|
|
28
|
-
comparator: "==",
|
|
29
|
-
limit: def.length
|
|
30
|
-
}
|
|
36
|
+
class: "Array",
|
|
37
|
+
props
|
|
31
38
|
}
|
|
32
39
|
};
|
|
33
40
|
};
|
|
@@ -37,12 +44,12 @@ const parseBranchTuple = (def, ctx)=>{
|
|
|
37
44
|
}
|
|
38
45
|
const l = parseDefinition(def[0], ctx);
|
|
39
46
|
const r = parseDefinition(def[2], ctx);
|
|
40
|
-
return def[1] === "&" ?
|
|
47
|
+
return def[1] === "&" ? rootIntersection(l, r, ctx.type) : rootUnion(l, r, ctx.type);
|
|
41
48
|
};
|
|
42
|
-
const parseArrayTuple = (def, scope)=>
|
|
49
|
+
const parseArrayTuple = (def, scope)=>toArrayNode(parseDefinition(def[0], scope));
|
|
43
50
|
export const writeMalformedFunctionalExpressionMessage = (operator, rightDef)=>`Expression requires a function following '${operator}' (was ${typeof rightDef})`;
|
|
44
|
-
const
|
|
45
|
-
const
|
|
51
|
+
const isIndexOneExpression = (def)=>indexOneParsers[def[1]] !== undefined;
|
|
52
|
+
const indexOneParsers = {
|
|
46
53
|
"|": parseBranchTuple,
|
|
47
54
|
"&": parseBranchTuple,
|
|
48
55
|
"[]": parseArrayTuple,
|
|
@@ -68,4 +75,4 @@ const prefixParsers = {
|
|
|
68
75
|
}),
|
|
69
76
|
node: (def)=>def[1]
|
|
70
77
|
};
|
|
71
|
-
const
|
|
78
|
+
const isIndexZeroExpression = (def)=>prefixParsers[def[0]] !== undefined;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const writeUndiscriminatableMorphUnionMessage = (path)=>`${path === "/" ? "A" : `At ${path}, a`} union including one or more morphs must be discriminatable`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|