arktype 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/main.js +135 -20
- package/dist/cjs/nodes/branches.js +7 -7
- package/dist/cjs/nodes/compose.js +3 -2
- package/dist/cjs/nodes/discriminate.js +5 -4
- package/dist/cjs/nodes/functors.js +3 -3
- package/dist/cjs/nodes/node.js +1 -1
- package/dist/cjs/nodes/predicate.js +19 -14
- package/dist/cjs/nodes/rules/collapsibleSet.js +3 -4
- package/dist/cjs/nodes/rules/objectKind.js +78 -0
- package/dist/cjs/nodes/rules/range.js +2 -2
- package/dist/cjs/nodes/rules/rules.js +5 -5
- package/dist/cjs/parse/definition.js +26 -6
- package/dist/cjs/parse/string/ast.js +1 -1
- package/dist/cjs/parse/string/shift/operator/bounds.js +1 -1
- package/dist/cjs/parse/tuple/keyof.js +6 -7
- package/dist/cjs/parse/tuple/morph.js +2 -2
- package/dist/cjs/parse/tuple/tuple.js +1 -1
- package/dist/cjs/traverse/problems.js +18 -32
- package/dist/cjs/traverse/traverse.js +12 -12
- package/dist/cjs/utils/domains.js +10 -47
- package/dist/cjs/utils/generics.js +1 -3
- package/dist/cjs/utils/objectKinds.js +64 -0
- package/dist/cjs/utils/size.js +16 -0
- package/dist/mjs/main.js +134 -19
- package/dist/mjs/nodes/branches.js +7 -7
- package/dist/mjs/nodes/compose.js +3 -2
- package/dist/mjs/nodes/discriminate.js +6 -5
- package/dist/mjs/nodes/functors.js +3 -3
- package/dist/mjs/nodes/node.js +1 -1
- package/dist/mjs/nodes/predicate.js +20 -15
- package/dist/mjs/nodes/rules/collapsibleSet.js +3 -4
- package/dist/mjs/nodes/rules/{subdomain.js → objectKind.js} +10 -10
- package/dist/mjs/nodes/rules/range.js +1 -1
- package/dist/mjs/nodes/rules/rules.js +5 -5
- package/dist/mjs/parse/definition.js +27 -7
- package/dist/mjs/parse/string/ast.js +1 -1
- package/dist/mjs/parse/string/shift/operator/bounds.js +1 -1
- package/dist/mjs/parse/tuple/keyof.js +6 -7
- package/dist/mjs/parse/tuple/morph.js +2 -2
- package/dist/mjs/parse/tuple/tuple.js +1 -1
- package/dist/mjs/traverse/problems.js +18 -31
- package/dist/mjs/traverse/traverse.js +12 -12
- package/dist/mjs/utils/domains.js +9 -42
- package/dist/mjs/utils/generics.js +0 -1
- package/dist/mjs/utils/objectKinds.js +48 -0
- package/dist/mjs/utils/size.js +2 -0
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/main.d.ts +82 -15
- package/dist/types/nodes/branches.d.ts +4 -4
- package/dist/types/nodes/compose.d.ts +12 -5
- package/dist/types/nodes/discriminate.d.ts +3 -2
- package/dist/types/nodes/infer.d.ts +11 -10
- package/dist/types/nodes/predicate.d.ts +2 -2
- package/dist/types/nodes/rules/objectKind.d.ts +15 -0
- package/dist/types/nodes/rules/range.d.ts +1 -1
- package/dist/types/nodes/rules/rules.d.ts +4 -4
- package/dist/types/parse/definition.d.ts +6 -5
- package/dist/types/parse/record.d.ts +2 -2
- package/dist/types/parse/string/ast.d.ts +7 -5
- package/dist/types/parse/string/reduce/shared.d.ts +1 -1
- package/dist/types/parse/string/shift/operator/bounds.d.ts +1 -1
- package/dist/types/parse/tuple/keyof.d.ts +8 -0
- package/dist/types/parse/tuple/tuple.d.ts +6 -12
- package/dist/types/traverse/problems.d.ts +25 -41
- package/dist/types/traverse/traverse.d.ts +24 -4
- package/dist/types/utils/domains.d.ts +11 -37
- package/dist/types/utils/generics.d.ts +2 -2
- package/dist/types/utils/objectKinds.d.ts +83 -0
- package/dist/types/utils/size.d.ts +3 -0
- package/package.json +4 -4
- package/dist/cjs/nodes/rules/subdomain.js +0 -78
- package/dist/types/nodes/rules/subdomain.d.ts +0 -15
|
@@ -2,13 +2,13 @@ import { domainOf, hasDomain } from "../utils/domains.js";
|
|
|
2
2
|
import { throwInternalError } from "../utils/errors.js";
|
|
3
3
|
import { isDisjoint, isEquality } from "./compose.js";
|
|
4
4
|
import { branchIntersection } from "./rules/rules.js";
|
|
5
|
-
export const isBranchComparison = (comparison)=>comparison?.
|
|
5
|
+
export const isBranchComparison = (comparison)=>comparison?.lBranches !== undefined;
|
|
6
6
|
export const compareBranches = (lConditions, rConditions, state)=>{
|
|
7
7
|
const result = {
|
|
8
|
-
lConditions,
|
|
9
|
-
rConditions,
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
lBranches: lConditions,
|
|
9
|
+
rBranches: rConditions,
|
|
10
|
+
lExtendsR: [],
|
|
11
|
+
rExtendsL: [],
|
|
12
12
|
equalities: [],
|
|
13
13
|
distinctIntersections: []
|
|
14
14
|
};
|
|
@@ -28,14 +28,14 @@ export const compareBranches = (lConditions, rConditions, state)=>{
|
|
|
28
28
|
// doesn't tell us about any redundancies or add a distinct pair
|
|
29
29
|
return null;
|
|
30
30
|
} else if (subresult === l) {
|
|
31
|
-
result.
|
|
31
|
+
result.lExtendsR.push(lIndex);
|
|
32
32
|
// If l is a subtype of the current r branch, intersections
|
|
33
33
|
// with the remaining branches of r won't lead to distinct
|
|
34
34
|
// branches, so we set a flag indicating we can skip them.
|
|
35
35
|
lImpliesR = true;
|
|
36
36
|
return null;
|
|
37
37
|
} else if (subresult === r) {
|
|
38
|
-
result.
|
|
38
|
+
result.rExtendsL.push(rIndex);
|
|
39
39
|
// If r is a subtype of the current l branch, it is removed
|
|
40
40
|
// from pairsByR because future intersections won't lead to
|
|
41
41
|
// distinct branches.
|
|
@@ -46,13 +46,14 @@ export const composeIntersection = (reducer)=>(l, r, state)=>l === undefined ? r
|
|
|
46
46
|
export const throwUndefinedOperandsError = ()=>throwInternalError(`Unexpected operation two undefined operands`);
|
|
47
47
|
export const disjointDescriptionWriters = {
|
|
48
48
|
domain: ({ l , r })=>`${l.join(", ")} and ${r.join(", ")}`,
|
|
49
|
-
|
|
49
|
+
objectKind: ({ l , r })=>`${l} and ${r}`,
|
|
50
50
|
range: ({ l , r })=>`${stringifyRange(l)} and ${stringifyRange(r)}`,
|
|
51
51
|
class: ({ l , r })=>`classes ${l.name} and ${r.name}`,
|
|
52
52
|
tupleLength: ({ l , r })=>`tuples of length ${l} and ${r}`,
|
|
53
53
|
value: ({ l , r })=>`literal values ${serialize(l)} and ${serialize(r)}`,
|
|
54
54
|
leftAssignability: ({ l , r })=>`literal value ${serialize(l.value)} and ${serialize(r)}`,
|
|
55
|
-
rightAssignability: ({ l , r })=>`literal value ${serialize(r.value)} and ${serialize(l)}
|
|
55
|
+
rightAssignability: ({ l , r })=>`literal value ${serialize(r.value)} and ${serialize(l)}`,
|
|
56
|
+
union: ({ l , r })=>`branches ${serialize(l)} and branches ${serialize(r)}`
|
|
56
57
|
};
|
|
57
58
|
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
59
|
var _disjoints = /*#__PURE__*/ new WeakMap();
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { writeUndiscriminatableMorphUnionMessage } from "../parse/string/ast.js";
|
|
2
|
-
import { domainOf
|
|
2
|
+
import { domainOf } from "../utils/domains.js";
|
|
3
3
|
import { throwParseError } from "../utils/errors.js";
|
|
4
4
|
import { hasKey, isKeyOf, keysOf } from "../utils/generics.js";
|
|
5
|
+
import { isArray, objectKindOf } from "../utils/objectKinds.js";
|
|
5
6
|
import { Path } from "../utils/paths.js";
|
|
6
7
|
import { serializePrimitive } from "../utils/serialize.js";
|
|
7
8
|
import { IntersectionState } from "./compose.js";
|
|
@@ -44,7 +45,7 @@ const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
|
|
|
44
45
|
};
|
|
45
46
|
const discriminantKinds = {
|
|
46
47
|
domain: true,
|
|
47
|
-
|
|
48
|
+
objectKind: true,
|
|
48
49
|
value: true
|
|
49
50
|
};
|
|
50
51
|
const calculateDiscriminants = (branches, ctx)=>{
|
|
@@ -61,7 +62,7 @@ const calculateDiscriminants = (branches, ctx)=>{
|
|
|
61
62
|
const intersectionState = new IntersectionState(ctx.type, "|");
|
|
62
63
|
branchIntersection(branches[lIndex], branches[rIndex], intersectionState);
|
|
63
64
|
for(const path in intersectionState.disjoints){
|
|
64
|
-
// designed to match
|
|
65
|
+
// designed to match objectKind path segments like "${number}"
|
|
65
66
|
if (path.includes("${")) {
|
|
66
67
|
continue;
|
|
67
68
|
}
|
|
@@ -176,9 +177,9 @@ const serializeIfPrimitive = (data)=>{
|
|
|
176
177
|
};
|
|
177
178
|
const caseSerializers = {
|
|
178
179
|
value: (data)=>serializeIfPrimitive(data) ?? "default",
|
|
179
|
-
|
|
180
|
+
objectKind: (data)=>objectKindOf(data) ?? "default",
|
|
180
181
|
domain: domainOf
|
|
181
182
|
};
|
|
182
183
|
export const serializeCase = (kind, data)=>caseSerializers[kind](data);
|
|
183
184
|
const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph(isOptional(prop) ? prop[1] : prop, $)) : false;
|
|
184
|
-
const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).meta.includesMorph : Object.values(node).some((predicate)=>predicate === true ? false :
|
|
185
|
+
const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).meta.includesMorph : Object.values(node).some((predicate)=>predicate === true ? false : isArray(predicate) ? predicate.some((branch)=>branchIncludesMorph(branch, $)) : branchIncludesMorph(predicate, $));
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export const functors = {
|
|
2
2
|
Array: (node)=>({
|
|
3
3
|
object: {
|
|
4
|
-
|
|
4
|
+
objectKind: [
|
|
5
5
|
"Array",
|
|
6
6
|
node
|
|
7
7
|
]
|
|
@@ -9,7 +9,7 @@ export const functors = {
|
|
|
9
9
|
}),
|
|
10
10
|
Set: (node)=>({
|
|
11
11
|
object: {
|
|
12
|
-
|
|
12
|
+
objectKind: [
|
|
13
13
|
"Set",
|
|
14
14
|
node
|
|
15
15
|
]
|
|
@@ -17,7 +17,7 @@ export const functors = {
|
|
|
17
17
|
}),
|
|
18
18
|
Map: (k, v)=>({
|
|
19
19
|
object: {
|
|
20
|
-
|
|
20
|
+
objectKind: [
|
|
21
21
|
"Map",
|
|
22
22
|
k,
|
|
23
23
|
v
|
package/dist/mjs/nodes/node.js
CHANGED
|
@@ -43,7 +43,7 @@ export const union = (l, r, type)=>{
|
|
|
43
43
|
}
|
|
44
44
|
return result;
|
|
45
45
|
};
|
|
46
|
-
const hasImpliedDomain = (flatPredicate)=>flatPredicate[0] && (flatPredicate[0][0] === "
|
|
46
|
+
const hasImpliedDomain = (flatPredicate)=>flatPredicate[0] && (flatPredicate[0][0] === "objectKind" || flatPredicate[0][0] === "value");
|
|
47
47
|
export const flattenType = (type)=>{
|
|
48
48
|
const ctx = {
|
|
49
49
|
type,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { keysOf, listFrom } from "../utils/generics.js";
|
|
2
|
+
import { isArray } from "../utils/objectKinds.js";
|
|
3
3
|
import { compareBranches, isBranchComparison } from "./branches.js";
|
|
4
4
|
import { equality, IntersectionState, isEquality } from "./compose.js";
|
|
5
5
|
import { flattenBranches } from "./discriminate.js";
|
|
@@ -9,7 +9,7 @@ export const comparePredicates = (l, r, context)=>{
|
|
|
9
9
|
if (l === true && r === true) {
|
|
10
10
|
return equality();
|
|
11
11
|
}
|
|
12
|
-
if (!
|
|
12
|
+
if (!isArray(l) && !isArray(r)) {
|
|
13
13
|
const result = branchIntersection(emptyRulesIfTrue(l), emptyRulesIfTrue(r), context);
|
|
14
14
|
return result === l ? l : result === r ? r : result;
|
|
15
15
|
}
|
|
@@ -19,10 +19,10 @@ export const comparePredicates = (l, r, context)=>{
|
|
|
19
19
|
if (comparison.equalities.length === lBranches.length && comparison.equalities.length === rBranches.length) {
|
|
20
20
|
return equality();
|
|
21
21
|
}
|
|
22
|
-
if (comparison.
|
|
22
|
+
if (comparison.lExtendsR.length + comparison.equalities.length === lBranches.length) {
|
|
23
23
|
return l;
|
|
24
24
|
}
|
|
25
|
-
if (comparison.
|
|
25
|
+
if (comparison.rExtendsL.length + comparison.equalities.length === rBranches.length) {
|
|
26
26
|
return r;
|
|
27
27
|
}
|
|
28
28
|
return comparison;
|
|
@@ -33,12 +33,16 @@ export const predicateIntersection = (domain, l, r, state)=>{
|
|
|
33
33
|
if (!isBranchComparison(comparison)) {
|
|
34
34
|
return comparison;
|
|
35
35
|
}
|
|
36
|
-
|
|
36
|
+
const resultBranches = [
|
|
37
37
|
...comparison.distinctIntersections,
|
|
38
|
-
...comparison.equalities.map((indices)=>comparison.
|
|
39
|
-
...comparison.
|
|
40
|
-
...comparison.
|
|
41
|
-
]
|
|
38
|
+
...comparison.equalities.map((indices)=>comparison.lBranches[indices[0]]),
|
|
39
|
+
...comparison.lExtendsR.map((lIndex)=>comparison.lBranches[lIndex]),
|
|
40
|
+
...comparison.rExtendsL.map((rIndex)=>comparison.rBranches[rIndex])
|
|
41
|
+
];
|
|
42
|
+
if (resultBranches.length === 0) {
|
|
43
|
+
state.addDisjoint("union", comparison.lBranches, comparison.rBranches);
|
|
44
|
+
}
|
|
45
|
+
return resultBranches.length === 1 ? resultBranches[0] : resultBranches;
|
|
42
46
|
};
|
|
43
47
|
export const predicateUnion = (domain, l, r, type)=>{
|
|
44
48
|
const state = new IntersectionState(type, "|");
|
|
@@ -51,16 +55,17 @@ export const predicateUnion = (domain, l, r, type)=>{
|
|
|
51
55
|
emptyRulesIfTrue(r)
|
|
52
56
|
];
|
|
53
57
|
}
|
|
54
|
-
|
|
55
|
-
...comparison.
|
|
56
|
-
...comparison.
|
|
57
|
-
]
|
|
58
|
+
const resultBranches = [
|
|
59
|
+
...comparison.lBranches.filter((_, lIndex)=>!comparison.lExtendsR.includes(lIndex) && !comparison.equalities.some((indexPair)=>indexPair[0] === lIndex)),
|
|
60
|
+
...comparison.rBranches.filter((_, rIndex)=>!comparison.rExtendsL.includes(rIndex) && !comparison.equalities.some((indexPair)=>indexPair[1] === rIndex))
|
|
61
|
+
];
|
|
62
|
+
return resultBranches.length === 1 ? resultBranches[0] : resultBranches;
|
|
58
63
|
};
|
|
59
64
|
export const flattenPredicate = (predicate, context)=>{
|
|
60
65
|
if (predicate === true) {
|
|
61
66
|
return [];
|
|
62
67
|
}
|
|
63
|
-
return
|
|
68
|
+
return isArray(predicate) ? flattenBranches(predicate, context) : flattenBranch(predicate, context);
|
|
64
69
|
};
|
|
65
70
|
export const isLiteralCondition = (predicate)=>typeof predicate === "object" && "value" in predicate;
|
|
66
71
|
export const resolutionExtendsDomain = (resolution, domain)=>{
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { hasSubdomain } from "../../utils/domains.js";
|
|
2
1
|
import { equality } from "../compose.js";
|
|
3
2
|
export const collapsibleListUnion = (l, r)=>{
|
|
4
|
-
if (
|
|
5
|
-
if (
|
|
3
|
+
if (Array.isArray(l)) {
|
|
4
|
+
if (Array.isArray(r)) {
|
|
6
5
|
const result = listUnion(l, r);
|
|
7
6
|
return result.length === l.length ? result.length === r.length ? equality() : l : result.length === r.length ? r : result;
|
|
8
7
|
}
|
|
@@ -11,7 +10,7 @@ export const collapsibleListUnion = (l, r)=>{
|
|
|
11
10
|
r
|
|
12
11
|
];
|
|
13
12
|
}
|
|
14
|
-
if (
|
|
13
|
+
if (Array.isArray(r)) {
|
|
15
14
|
return r.includes(l) ? r : [
|
|
16
15
|
...r,
|
|
17
16
|
l
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import { throwInternalError } from "../../utils/errors.js";
|
|
2
2
|
import { composeIntersection, equality, isDisjoint, isEquality } from "../compose.js";
|
|
3
3
|
import { flattenNode, nodeIntersection } from "../node.js";
|
|
4
|
-
export const
|
|
4
|
+
export const objectKindIntersection = composeIntersection((l, r, state)=>{
|
|
5
5
|
if (typeof l === "string") {
|
|
6
6
|
if (typeof r === "string") {
|
|
7
|
-
return l === r ? equality() : state.addDisjoint("
|
|
7
|
+
return l === r ? equality() : state.addDisjoint("objectKind", l, r);
|
|
8
8
|
}
|
|
9
|
-
return l === r[0] ? r : state.addDisjoint("
|
|
9
|
+
return l === r[0] ? r : state.addDisjoint("objectKind", l, r[0]);
|
|
10
10
|
}
|
|
11
11
|
if (typeof r === "string") {
|
|
12
|
-
return l[0] === r ? l : state.addDisjoint("
|
|
12
|
+
return l[0] === r ? l : state.addDisjoint("objectKind", l[0], r);
|
|
13
13
|
}
|
|
14
14
|
if (l[0] !== r[0]) {
|
|
15
|
-
return state.addDisjoint("
|
|
15
|
+
return state.addDisjoint("objectKind", l[0], r[0]);
|
|
16
16
|
}
|
|
17
17
|
const result = [
|
|
18
18
|
l[0]
|
|
@@ -22,7 +22,7 @@ export const subdomainIntersection = composeIntersection((l, r, state)=>{
|
|
|
22
22
|
for(let i = 1; i < l.length; i++){
|
|
23
23
|
const lNode = l[i];
|
|
24
24
|
const rNode = r[i];
|
|
25
|
-
state.path.push(
|
|
25
|
+
state.path.push(objectKindParameterToPathSegment(l[0], i));
|
|
26
26
|
const parameterResult = nodeIntersection(lNode, rNode, state);
|
|
27
27
|
state.path.pop();
|
|
28
28
|
if (isEquality(parameterResult)) {
|
|
@@ -43,15 +43,15 @@ export const subdomainIntersection = composeIntersection((l, r, state)=>{
|
|
|
43
43
|
}
|
|
44
44
|
return lImpliesR ? rImpliesL ? equality() : l : rImpliesL ? r : result;
|
|
45
45
|
});
|
|
46
|
-
const
|
|
47
|
-
export const
|
|
46
|
+
const objectKindParameterToPathSegment = (objectKind, i)=>objectKind === "Array" ? "${number}" : objectKind === "Set" ? "${item}" : objectKind === "Map" ? i === 1 ? "${key}" : "${value}" : throwInternalError(`Unexpected parameterized objectKind '${objectKind}'`);
|
|
47
|
+
export const objectKindPathSegments = {
|
|
48
48
|
"${number}": true,
|
|
49
49
|
"${item}": true,
|
|
50
50
|
"${key}": true,
|
|
51
51
|
"${value}": true
|
|
52
52
|
};
|
|
53
|
-
export const
|
|
54
|
-
"
|
|
53
|
+
export const flattenObjectKind = (entries, rule, ctx)=>entries.push([
|
|
54
|
+
"objectKind",
|
|
55
55
|
typeof rule === "string" ? rule : rule[0] === "Map" ? [
|
|
56
56
|
rule[0],
|
|
57
57
|
flattenNode(rule[1], ctx),
|
|
@@ -5,10 +5,10 @@ import { composeIntersection, composeKeyedIntersection, equality, isDisjoint, is
|
|
|
5
5
|
import { classIntersection } from "./class.js";
|
|
6
6
|
import { collapsibleListUnion } from "./collapsibleSet.js";
|
|
7
7
|
import { divisorIntersection } from "./divisor.js";
|
|
8
|
+
import { flattenObjectKind, objectKindIntersection } from "./objectKind.js";
|
|
8
9
|
import { flattenProps, propsIntersection } from "./props.js";
|
|
9
10
|
import { flattenRange, rangeIntersection } from "./range.js";
|
|
10
11
|
import { getRegex, regexIntersection } from "./regex.js";
|
|
11
|
-
import { flattenSubdomain, subdomainIntersection } from "./subdomain.js";
|
|
12
12
|
const rulesOf = (branch)=>branch.input ?? branch;
|
|
13
13
|
export const branchIntersection = (l, r, state)=>{
|
|
14
14
|
const lRules = rulesOf(l);
|
|
@@ -22,7 +22,7 @@ export const branchIntersection = (l, r, state)=>{
|
|
|
22
22
|
morph: l.morph
|
|
23
23
|
};
|
|
24
24
|
}
|
|
25
|
-
return state.lastOperator === "&" ? throwParseError(writeImplicitNeverMessage(state.path, "of morphs")) : {};
|
|
25
|
+
return state.lastOperator === "&" ? throwParseError(writeImplicitNeverMessage(state.path, "Intersection", "of morphs")) : {};
|
|
26
26
|
}
|
|
27
27
|
return isDisjoint(rulesResult) ? rulesResult : {
|
|
28
28
|
input: isEquality(rulesResult) ? l.input : rulesResult,
|
|
@@ -40,7 +40,7 @@ export const branchIntersection = (l, r, state)=>{
|
|
|
40
40
|
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);
|
|
41
41
|
const narrowIntersection = composeIntersection(collapsibleListUnion);
|
|
42
42
|
export const narrowableRulesIntersection = composeKeyedIntersection({
|
|
43
|
-
|
|
43
|
+
objectKind: objectKindIntersection,
|
|
44
44
|
divisor: divisorIntersection,
|
|
45
45
|
regex: regexIntersection,
|
|
46
46
|
props: propsIntersection,
|
|
@@ -51,7 +51,7 @@ export const narrowableRulesIntersection = composeKeyedIntersection({
|
|
|
51
51
|
onEmpty: "bubble"
|
|
52
52
|
});
|
|
53
53
|
const ruleFlatteners = {
|
|
54
|
-
|
|
54
|
+
objectKind: flattenObjectKind,
|
|
55
55
|
regex: (entries, rule)=>{
|
|
56
56
|
for (const source of listFrom(rule)){
|
|
57
57
|
entries.push([
|
|
@@ -95,7 +95,7 @@ export const precedenceMap = {
|
|
|
95
95
|
value: 0,
|
|
96
96
|
domains: 0,
|
|
97
97
|
branches: 0,
|
|
98
|
-
|
|
98
|
+
objectKind: 0,
|
|
99
99
|
switch: 0,
|
|
100
100
|
alias: 0,
|
|
101
101
|
// Shallow: All shallow checks will be performed even if one or more fail
|
|
@@ -1,16 +1,36 @@
|
|
|
1
1
|
import { isType } from "../main.js";
|
|
2
|
-
import {
|
|
2
|
+
import { domainOf } from "../utils/domains.js";
|
|
3
3
|
import { throwParseError } from "../utils/errors.js";
|
|
4
|
+
import { objectKindOf } from "../utils/objectKinds.js";
|
|
5
|
+
import { stringify } from "../utils/serialize.js";
|
|
4
6
|
import { parseRecord } from "./record.js";
|
|
5
7
|
import { parseString } from "./string/string.js";
|
|
6
8
|
import { parseTuple } from "./tuple/tuple.js";
|
|
7
9
|
export const parseDefinition = (def, ctx)=>{
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
const domain = domainOf(def);
|
|
11
|
+
if (domain === "string") {
|
|
12
|
+
return parseString(def, ctx);
|
|
13
|
+
}
|
|
14
|
+
if (domain !== "object") {
|
|
15
|
+
return throwParseError(writeBadDefinitionTypeMessage(domain));
|
|
16
|
+
}
|
|
17
|
+
const objectKind = objectKindOf(def);
|
|
18
|
+
switch(objectKind){
|
|
19
|
+
case "Object":
|
|
20
|
+
return parseRecord(def, ctx);
|
|
21
|
+
case "Array":
|
|
22
|
+
return parseTuple(def, ctx);
|
|
23
|
+
case "RegExp":
|
|
24
|
+
return {
|
|
25
|
+
string: {
|
|
26
|
+
regex: def.source
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
case "Function":
|
|
30
|
+
return isType(def) ? def.node : throwParseError(writeBadDefinitionTypeMessage("Function"));
|
|
31
|
+
default:
|
|
32
|
+
return throwParseError(writeBadDefinitionTypeMessage(objectKind ?? stringify(def)));
|
|
33
|
+
}
|
|
14
34
|
};
|
|
15
35
|
export const as = Symbol("as");
|
|
16
36
|
export const unknownDefinitionMessage = "Cannot statically parse a definition inferred as unknown. Consider using 'as inferred<...>' to cast it.";
|
|
@@ -13,7 +13,7 @@ export const compileDisjointReasonsMessage = (disjoints)=>{
|
|
|
13
13
|
}
|
|
14
14
|
return message;
|
|
15
15
|
};
|
|
16
|
-
export const writeImplicitNeverMessage = (path, description)=>`${path.length ? `At ${path}: ` : ""}
|
|
16
|
+
export const writeImplicitNeverMessage = (path, operator, description)=>`${path.length ? `At ${path}: ` : ""}${operator} ${description ? `${description} ` : ""}results in an unsatisfiable type`;
|
|
17
17
|
export const writeUndiscriminatableMorphUnionMessage = (path)=>`${path === "/" ? "A" : `At ${path}, a`} union including one or more morphs must be discriminatable`;
|
|
18
18
|
export const writeIndivisibleMessage = (root)=>`Divisibility operand ${root} must be a number`;
|
|
19
19
|
export const writeUnboundableMessage = (root)=>`Bounded expression ${root} must be a number, string or array`;
|
|
@@ -54,7 +54,7 @@ const distributeRange = (range, s)=>{
|
|
|
54
54
|
if (resolution.object === true) {
|
|
55
55
|
return false;
|
|
56
56
|
}
|
|
57
|
-
return listFrom(resolution.object).every((branch)=>hasKey(branch, "
|
|
57
|
+
return listFrom(resolution.object).every((branch)=>hasKey(branch, "objectKind") && (branch.objectKind === "Array" || branch.objectKind[0] === "Array"));
|
|
58
58
|
default:
|
|
59
59
|
return false;
|
|
60
60
|
}
|
|
@@ -2,15 +2,14 @@ import { resolutionExtendsDomain } from "../../nodes/predicate.js";
|
|
|
2
2
|
import { throwParseError } from "../../utils/errors.js";
|
|
3
3
|
import { hasKey, listFrom } from "../../utils/generics.js";
|
|
4
4
|
import { parseDefinition } from "../definition.js";
|
|
5
|
+
import { writeImplicitNeverMessage } from "../string/ast.js";
|
|
5
6
|
export const parseKeyOfTuple = (def, ctx)=>{
|
|
6
7
|
const resolution = ctx.type.meta.scope.resolveNode(parseDefinition(def[1], ctx));
|
|
7
8
|
if (!resolutionExtendsDomain(resolution, "object")) {
|
|
8
|
-
return throwParseError("
|
|
9
|
+
return throwParseError(writeImplicitNeverMessage(ctx.path, "keyof"));
|
|
9
10
|
}
|
|
10
11
|
if (resolution.object === true) {
|
|
11
|
-
return
|
|
12
|
-
string: true
|
|
13
|
-
};
|
|
12
|
+
return writeImplicitNeverMessage(ctx.path, "keyof");
|
|
14
13
|
}
|
|
15
14
|
const branchKeys = [];
|
|
16
15
|
const objectBranches = listFrom(resolution.object);
|
|
@@ -18,7 +17,7 @@ export const parseKeyOfTuple = (def, ctx)=>{
|
|
|
18
17
|
for(let i = 1; i < objectBranches.length; i++){
|
|
19
18
|
const keys = getPropsFromBranch(objectBranches[i]);
|
|
20
19
|
if (!keys.length) {
|
|
21
|
-
return
|
|
20
|
+
return writeImplicitNeverMessage(ctx.path, "keyof");
|
|
22
21
|
}
|
|
23
22
|
branchKeys.push(keys);
|
|
24
23
|
}
|
|
@@ -31,8 +30,8 @@ export const parseKeyOfTuple = (def, ctx)=>{
|
|
|
31
30
|
});
|
|
32
31
|
}
|
|
33
32
|
});
|
|
34
|
-
return {
|
|
33
|
+
return result.length ? {
|
|
35
34
|
string: result
|
|
36
|
-
};
|
|
35
|
+
} : writeImplicitNeverMessage(ctx.path, "keyof");
|
|
37
36
|
};
|
|
38
37
|
const getPropsFromBranch = (branch)=>hasKey(branch, "props") ? Object.keys(branch.props) : [];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { hasSubdomain } from "../../utils/domains.js";
|
|
2
1
|
import { throwInternalError, throwParseError } from "../../utils/errors.js";
|
|
2
|
+
import { isArray } from "../../utils/objectKinds.js";
|
|
3
3
|
import { stringify } from "../../utils/serialize.js";
|
|
4
4
|
import { parseDefinition } from "../definition.js";
|
|
5
5
|
export const parseMorphTuple = (def, ctx)=>{
|
|
@@ -19,7 +19,7 @@ export const parseMorphTuple = (def, ctx)=>{
|
|
|
19
19
|
morph
|
|
20
20
|
};
|
|
21
21
|
} else if (typeof predicate === "object") {
|
|
22
|
-
result[domain] =
|
|
22
|
+
result[domain] = isArray(predicate) ? predicate.map((branch)=>applyMorph(branch, morph)) : applyMorph(predicate, morph);
|
|
23
23
|
} else {
|
|
24
24
|
throwInternalError(`Unexpected predicate value for domain '${domain}': ${stringify(predicate)}`);
|
|
25
25
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
var _definitions_code, _definitions_code1, _definitions_code2, _clausesByPath, _pathKey;
|
|
2
2
|
import { Scanner } from "../parse/string/shift/scanner.js";
|
|
3
|
-
import {
|
|
3
|
+
import { domainDescriptions, domainOf } from "../utils/domains.js";
|
|
4
4
|
import { keysOf } from "../utils/generics.js";
|
|
5
|
+
import { objectKindDescriptions, objectKindOf } from "../utils/objectKinds.js";
|
|
5
6
|
import { Path } from "../utils/paths.js";
|
|
6
7
|
import { stringify } from "../utils/serialize.js";
|
|
7
8
|
export class ArkTypeError extends TypeError {
|
|
@@ -76,14 +77,14 @@ class ProblemArray extends Array {
|
|
|
76
77
|
}
|
|
77
78
|
export const Problems = ProblemArray;
|
|
78
79
|
const capitalize = (s)=>s[0].toUpperCase() + s.slice(1);
|
|
79
|
-
export const
|
|
80
|
-
if (
|
|
81
|
-
return
|
|
80
|
+
export const describeDomains = (domains)=>{
|
|
81
|
+
if (domains.length === 1) {
|
|
82
|
+
return domainDescriptions[domains[0]];
|
|
82
83
|
}
|
|
83
|
-
if (
|
|
84
|
+
if (domains.length === 0) {
|
|
84
85
|
return "never";
|
|
85
86
|
}
|
|
86
|
-
return describeBranches(
|
|
87
|
+
return describeBranches(domains.map((objectKind)=>domainDescriptions[objectKind]));
|
|
87
88
|
};
|
|
88
89
|
export const describeBranches = (descriptions)=>{
|
|
89
90
|
let description = "";
|
|
@@ -96,23 +97,6 @@ export const describeBranches = (descriptions)=>{
|
|
|
96
97
|
description += ` or ${descriptions[descriptions.length - 1]}`;
|
|
97
98
|
return description;
|
|
98
99
|
};
|
|
99
|
-
/** Each Subdomain's completion for the phrase "Must be _____" */ export const subdomainDescriptions = {
|
|
100
|
-
bigint: "a bigint",
|
|
101
|
-
boolean: "boolean",
|
|
102
|
-
null: "null",
|
|
103
|
-
number: "a number",
|
|
104
|
-
object: "an object",
|
|
105
|
-
string: "a string",
|
|
106
|
-
symbol: "a symbol",
|
|
107
|
-
undefined: "undefined",
|
|
108
|
-
Array: "an array",
|
|
109
|
-
Function: "a function",
|
|
110
|
-
Date: "a Date",
|
|
111
|
-
RegExp: "a RegExp",
|
|
112
|
-
Error: "an Error",
|
|
113
|
-
Map: "a Map",
|
|
114
|
-
Set: "a Set"
|
|
115
|
-
};
|
|
116
100
|
export class DataWrapper {
|
|
117
101
|
toString() {
|
|
118
102
|
return stringify(this.value);
|
|
@@ -120,18 +104,17 @@ export class DataWrapper {
|
|
|
120
104
|
get domain() {
|
|
121
105
|
return domainOf(this.value);
|
|
122
106
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
return subdomainOf(this.value);
|
|
107
|
+
get objectKind() {
|
|
108
|
+
return objectKindOf(this.value) ?? "non-object";
|
|
126
109
|
}
|
|
127
110
|
get size() {
|
|
128
|
-
return
|
|
111
|
+
return typeof this.value === "string" || Array.isArray(this.value) ? this.value.length : typeof this.value === "number" ? this.value : 0;
|
|
129
112
|
}
|
|
130
113
|
get units() {
|
|
131
|
-
return
|
|
114
|
+
return typeof this.value === "string" ? "characters" : Array.isArray(this.value) ? "items" : "";
|
|
132
115
|
}
|
|
133
116
|
get className() {
|
|
134
|
-
return
|
|
117
|
+
return Object(this.value).constructor.name;
|
|
135
118
|
}
|
|
136
119
|
constructor(value){
|
|
137
120
|
this.value = value;
|
|
@@ -158,13 +141,17 @@ export const defaultProblemWriters = compileDefaultProblemWriters({
|
|
|
158
141
|
was: (data)=>data.className
|
|
159
142
|
},
|
|
160
143
|
domain: {
|
|
161
|
-
mustBe: (domain)=>
|
|
144
|
+
mustBe: (domain)=>domainDescriptions[domain],
|
|
162
145
|
was: (data)=>data.domain
|
|
163
146
|
},
|
|
164
147
|
domainBranches: {
|
|
165
|
-
mustBe: (domains)=>
|
|
148
|
+
mustBe: (domains)=>describeDomains(domains),
|
|
166
149
|
was: (data)=>data.domain
|
|
167
150
|
},
|
|
151
|
+
objectKind: {
|
|
152
|
+
mustBe: (kind)=>objectKindDescriptions[kind],
|
|
153
|
+
was: (data)=>data.objectKind
|
|
154
|
+
},
|
|
168
155
|
missing: {
|
|
169
156
|
mustBe: ()=>"defined",
|
|
170
157
|
was: ()=>""
|
|
@@ -44,9 +44,10 @@ import { checkDivisor } from "../nodes/rules/divisor.js";
|
|
|
44
44
|
import { checkBound } from "../nodes/rules/range.js";
|
|
45
45
|
import { checkRegex } from "../nodes/rules/regex.js";
|
|
46
46
|
import { precedenceMap } from "../nodes/rules/rules.js";
|
|
47
|
-
import { domainOf, hasDomain
|
|
47
|
+
import { domainOf, hasDomain } from "../utils/domains.js";
|
|
48
48
|
import { throwInternalError } from "../utils/errors.js";
|
|
49
49
|
import { hasKey, keysOf } from "../utils/generics.js";
|
|
50
|
+
import { objectKindOf } from "../utils/objectKinds.js";
|
|
50
51
|
import { getPath, Path } from "../utils/paths.js";
|
|
51
52
|
import { deserializePrimitive, stringify } from "../utils/serialize.js";
|
|
52
53
|
import { Problems } from "./problems.js";
|
|
@@ -157,7 +158,6 @@ const createPropChecker = (propKind)=>(data, props, state)=>{
|
|
|
157
158
|
state.path.push(propKey);
|
|
158
159
|
if (!hasKey(data, propKey)) {
|
|
159
160
|
if (propKind !== "optionalProps") {
|
|
160
|
-
// TODO: update
|
|
161
161
|
state.problems.add("missing", undefined, undefined);
|
|
162
162
|
}
|
|
163
163
|
} else {
|
|
@@ -169,18 +169,18 @@ const createPropChecker = (propKind)=>(data, props, state)=>{
|
|
|
169
169
|
};
|
|
170
170
|
const checkRequiredProps = createPropChecker("requiredProps");
|
|
171
171
|
const checkOptionalProps = createPropChecker("optionalProps");
|
|
172
|
-
export const
|
|
173
|
-
const
|
|
172
|
+
export const checkObjectKind = (data, rule, state)=>{
|
|
173
|
+
const dataObjectKind = objectKindOf(data);
|
|
174
174
|
if (typeof rule === "string") {
|
|
175
|
-
if (
|
|
176
|
-
return state.problems.add("
|
|
175
|
+
if (dataObjectKind !== rule) {
|
|
176
|
+
return state.problems.add("objectKind", data, rule);
|
|
177
177
|
}
|
|
178
178
|
return;
|
|
179
179
|
}
|
|
180
|
-
if (
|
|
181
|
-
return state.problems.add("
|
|
180
|
+
if (dataObjectKind !== rule[0]) {
|
|
181
|
+
return state.problems.add("objectKind", data, rule[0]);
|
|
182
182
|
}
|
|
183
|
-
if (
|
|
183
|
+
if (dataObjectKind === "Array" && typeof rule[2] === "number") {
|
|
184
184
|
const actual = data.length;
|
|
185
185
|
const expected = rule[2];
|
|
186
186
|
if (expected !== actual) {
|
|
@@ -191,7 +191,7 @@ export const checkSubdomain = (data, rule, state)=>{
|
|
|
191
191
|
});
|
|
192
192
|
}
|
|
193
193
|
}
|
|
194
|
-
if (
|
|
194
|
+
if (dataObjectKind === "Array" || dataObjectKind === "Set") {
|
|
195
195
|
let i = 0;
|
|
196
196
|
for (const item of data){
|
|
197
197
|
state.path.push(`${i}`);
|
|
@@ -200,7 +200,7 @@ export const checkSubdomain = (data, rule, state)=>{
|
|
|
200
200
|
i++;
|
|
201
201
|
}
|
|
202
202
|
} else {
|
|
203
|
-
return throwInternalError(`Unexpected
|
|
203
|
+
return throwInternalError(`Unexpected objectKind entry ${stringify(rule)}`);
|
|
204
204
|
}
|
|
205
205
|
return true;
|
|
206
206
|
};
|
|
@@ -220,7 +220,7 @@ const checkers = {
|
|
|
220
220
|
state.problems.add("domain", data, domain);
|
|
221
221
|
}
|
|
222
222
|
},
|
|
223
|
-
|
|
223
|
+
objectKind: checkObjectKind,
|
|
224
224
|
bound: checkBound,
|
|
225
225
|
requiredProps: checkRequiredProps,
|
|
226
226
|
optionalProps: checkOptionalProps,
|