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/api.cjs
CHANGED
|
@@ -11,9 +11,18 @@ function _export(target, all) {
|
|
|
11
11
|
_export(exports, {
|
|
12
12
|
scope: ()=>_scopeTs.scope,
|
|
13
13
|
type: ()=>_standardTs.type,
|
|
14
|
+
intersection: ()=>_expressionsTs.intersection,
|
|
15
|
+
union: ()=>_expressionsTs.union,
|
|
16
|
+
arrayOf: ()=>_expressionsTs.arrayOf,
|
|
17
|
+
instanceOf: ()=>_expressionsTs.instanceOf,
|
|
18
|
+
valueOf: ()=>_expressionsTs.valueOf,
|
|
19
|
+
morph: ()=>_expressionsTs.morph,
|
|
20
|
+
narrow: ()=>_expressionsTs.narrow,
|
|
21
|
+
keyOf: ()=>_expressionsTs.keyOf,
|
|
14
22
|
Problems: ()=>_problemsTs.Problems,
|
|
15
23
|
Problem: ()=>_problemsTs.Problem
|
|
16
24
|
});
|
|
17
25
|
const _scopeTs = require("./cjs/scopes/scope.js");
|
|
18
26
|
const _standardTs = require("./cjs/scopes/standard.js");
|
|
27
|
+
const _expressionsTs = require("./cjs/scopes/expressions.js");
|
|
19
28
|
const _problemsTs = require("./cjs/traverse/problems.js");
|
package/dist/api.d.ts
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
export { scope } from "./types/scopes/scope.js"
|
|
2
2
|
export { type } from "./types/scopes/standard.js"
|
|
3
|
+
export {
|
|
4
|
+
intersection,
|
|
5
|
+
union,
|
|
6
|
+
arrayOf,
|
|
7
|
+
instanceOf,
|
|
8
|
+
valueOf,
|
|
9
|
+
morph,
|
|
10
|
+
narrow,
|
|
11
|
+
keyOf
|
|
12
|
+
} from "./types/scopes/expressions.js"
|
|
3
13
|
export type { Type } from "./types/scopes/type.js"
|
|
4
14
|
export { Problems, Problem } from "./types/traverse/problems.js"
|
package/dist/api.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { scope } from "./mjs/scopes/scope.js";
|
|
2
2
|
export { type } from "./mjs/scopes/standard.js";
|
|
3
|
+
export { intersection, union, arrayOf, instanceOf, valueOf, morph, narrow, keyOf } from "./mjs/scopes/expressions.js";
|
|
3
4
|
export { Problems, Problem } from "./mjs/traverse/problems.js";
|
|
@@ -10,12 +10,16 @@ function _export(target, all) {
|
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
12
|
isBranchComparison: ()=>isBranchComparison,
|
|
13
|
-
compareBranches: ()=>compareBranches
|
|
13
|
+
compareBranches: ()=>compareBranches,
|
|
14
|
+
isMorphBranch: ()=>isMorphBranch,
|
|
15
|
+
flattenBranch: ()=>flattenBranch,
|
|
16
|
+
branchIntersection: ()=>branchIntersection
|
|
14
17
|
});
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
const
|
|
18
|
+
const _intersectionJs = require("../parse/ast/intersection.js");
|
|
19
|
+
const _domainsJs = require("../utils/domains.js");
|
|
20
|
+
const _errorsJs = require("../utils/errors.js");
|
|
21
|
+
const _composeJs = require("./compose.js");
|
|
22
|
+
const _rulesJs = require("./rules/rules.js");
|
|
19
23
|
const isBranchComparison = (comparison)=>comparison?.lBranches !== undefined;
|
|
20
24
|
const compareBranches = (lConditions, rConditions, state)=>{
|
|
21
25
|
const result = {
|
|
@@ -37,8 +41,8 @@ const compareBranches = (lConditions, rConditions, state)=>{
|
|
|
37
41
|
return null;
|
|
38
42
|
}
|
|
39
43
|
const r = rPairs.condition;
|
|
40
|
-
const subresult =
|
|
41
|
-
if ((0,
|
|
44
|
+
const subresult = branchIntersection(l, r, state);
|
|
45
|
+
if ((0, _composeJs.isDisjoint)(subresult)) {
|
|
42
46
|
// doesn't tell us about any redundancies or add a distinct pair
|
|
43
47
|
return null;
|
|
44
48
|
} else if (subresult === l) {
|
|
@@ -55,7 +59,7 @@ const compareBranches = (lConditions, rConditions, state)=>{
|
|
|
55
59
|
// distinct branches.
|
|
56
60
|
rPairs.distinct = null;
|
|
57
61
|
return null;
|
|
58
|
-
} else if ((0,
|
|
62
|
+
} else if ((0, _composeJs.isEquality)(subresult)) {
|
|
59
63
|
// Combination of l and r subtype cases.
|
|
60
64
|
result.equalities.push([
|
|
61
65
|
lIndex,
|
|
@@ -64,13 +68,13 @@ const compareBranches = (lConditions, rConditions, state)=>{
|
|
|
64
68
|
lImpliesR = true;
|
|
65
69
|
rPairs.distinct = null;
|
|
66
70
|
return null;
|
|
67
|
-
} else if ((0,
|
|
71
|
+
} else if ((0, _domainsJs.hasDomain)(subresult, "object")) {
|
|
68
72
|
// Neither branch is a subtype of the other, return
|
|
69
73
|
// the result of the intersection as a candidate
|
|
70
74
|
// branch for the final union
|
|
71
75
|
return subresult;
|
|
72
76
|
}
|
|
73
|
-
return (0,
|
|
77
|
+
return (0, _errorsJs.throwInternalError)(`Unexpected predicate intersection result of type '${(0, _domainsJs.domainOf)(subresult)}'`);
|
|
74
78
|
});
|
|
75
79
|
if (!lImpliesR) {
|
|
76
80
|
for(let i = 0; i < pairs.length; i++){
|
|
@@ -83,3 +87,54 @@ const compareBranches = (lConditions, rConditions, state)=>{
|
|
|
83
87
|
result.distinctIntersections = pairs.flatMap((pairs)=>pairs.distinct ?? []);
|
|
84
88
|
return result;
|
|
85
89
|
};
|
|
90
|
+
const isMorphBranch = (branch)=>"morph" in branch;
|
|
91
|
+
const flattenBranch = (branch, ctx)=>{
|
|
92
|
+
if (isMorphBranch(branch)) {
|
|
93
|
+
const result = (0, _rulesJs.flattenRules)(branch.rules, ctx);
|
|
94
|
+
if (branch.morph) {
|
|
95
|
+
if (typeof branch.morph === "function") {
|
|
96
|
+
result.push([
|
|
97
|
+
"morph",
|
|
98
|
+
branch.morph
|
|
99
|
+
]);
|
|
100
|
+
} else {
|
|
101
|
+
for (const morph of branch.morph){
|
|
102
|
+
result.push([
|
|
103
|
+
"morph",
|
|
104
|
+
morph
|
|
105
|
+
]);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
111
|
+
return (0, _rulesJs.flattenRules)(branch, ctx);
|
|
112
|
+
};
|
|
113
|
+
const rulesOf = (branch)=>branch.rules ?? branch;
|
|
114
|
+
const branchIntersection = (l, r, state)=>{
|
|
115
|
+
const lRules = rulesOf(l);
|
|
116
|
+
const rRules = rulesOf(r);
|
|
117
|
+
const rulesResult = (0, _rulesJs.rulesIntersection)(lRules, rRules, state);
|
|
118
|
+
if ("morph" in l) {
|
|
119
|
+
if ("morph" in r) {
|
|
120
|
+
if (l.morph === r.morph) {
|
|
121
|
+
return (0, _composeJs.isEquality)(rulesResult) || (0, _composeJs.isDisjoint)(rulesResult) ? rulesResult : {
|
|
122
|
+
rules: rulesResult,
|
|
123
|
+
morph: l.morph
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
return state.lastOperator === "&" ? (0, _errorsJs.throwParseError)((0, _intersectionJs.writeImplicitNeverMessage)(state.path, "Intersection", "of morphs")) : {};
|
|
127
|
+
}
|
|
128
|
+
return (0, _composeJs.isDisjoint)(rulesResult) ? rulesResult : {
|
|
129
|
+
rules: (0, _composeJs.isEquality)(rulesResult) ? l.rules : rulesResult,
|
|
130
|
+
morph: l.morph
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
if ("morph" in r) {
|
|
134
|
+
return (0, _composeJs.isDisjoint)(rulesResult) ? rulesResult : {
|
|
135
|
+
rules: (0, _composeJs.isEquality)(rulesResult) ? r.rules : rulesResult,
|
|
136
|
+
morph: r.morph
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
return rulesResult;
|
|
140
|
+
};
|
|
@@ -68,9 +68,8 @@ const composeIntersection = (reducer)=>(l, r, state)=>l === undefined ? r === un
|
|
|
68
68
|
const throwUndefinedOperandsError = ()=>(0, _errorsTs.throwInternalError)(`Unexpected operation two undefined operands`);
|
|
69
69
|
const disjointDescriptionWriters = {
|
|
70
70
|
domain: ({ l , r })=>`${l.join(", ")} and ${r.join(", ")}`,
|
|
71
|
-
objectKind: ({ l , r })=>`${l} and ${r}`,
|
|
72
71
|
range: ({ l , r })=>`${stringifyRange(l)} and ${stringifyRange(r)}`,
|
|
73
|
-
class: ({ l , r })=>`classes ${l.name} and ${r.name}`,
|
|
72
|
+
class: ({ l , r })=>`classes ${typeof l === "string" ? l : l.name} and ${typeof r === "string" ? r : r.name}`,
|
|
74
73
|
tupleLength: ({ l , r })=>`tuples of length ${l} and ${r}`,
|
|
75
74
|
value: ({ l , r })=>`literal values ${(0, _serializeTs.serialize)(l)} and ${(0, _serializeTs.serialize)(r)}`,
|
|
76
75
|
leftAssignability: ({ l , r })=>`literal value ${(0, _serializeTs.serialize)(l.value)} and ${(0, _serializeTs.serialize)(r)}`,
|
|
@@ -110,7 +109,7 @@ const equality = ()=>equal;
|
|
|
110
109
|
const isEquality = (result)=>result === equal;
|
|
111
110
|
const composeKeyedIntersection = (reducer, config)=>(l, r, state)=>{
|
|
112
111
|
const result = {};
|
|
113
|
-
const keys = (0, _genericsTs.
|
|
112
|
+
const keys = (0, _genericsTs.objectKeysOf)({
|
|
114
113
|
...l,
|
|
115
114
|
...r
|
|
116
115
|
});
|
|
@@ -13,16 +13,16 @@ _export(exports, {
|
|
|
13
13
|
serializeIfAllowed: ()=>serializeIfAllowed,
|
|
14
14
|
serializeCase: ()=>serializeCase
|
|
15
15
|
});
|
|
16
|
-
const
|
|
16
|
+
const _unionTs = require("../parse/ast/union.js");
|
|
17
17
|
const _domainsTs = require("../utils/domains.js");
|
|
18
18
|
const _errorsTs = require("../utils/errors.js");
|
|
19
19
|
const _genericsTs = require("../utils/generics.js");
|
|
20
20
|
const _objectKindsTs = require("../utils/objectKinds.js");
|
|
21
21
|
const _pathsTs = require("../utils/paths.js");
|
|
22
22
|
const _serializeTs = require("../utils/serialize.js");
|
|
23
|
+
const _branchTs = require("./branch.js");
|
|
23
24
|
const _composeTs = require("./compose.js");
|
|
24
25
|
const _propsTs = require("./rules/props.js");
|
|
25
|
-
const _rulesTs = require("./rules/rules.js");
|
|
26
26
|
const flattenBranches = (branches, ctx)=>{
|
|
27
27
|
const discriminants = calculateDiscriminants(branches, ctx);
|
|
28
28
|
const indices = branches.map((_, i)=>i);
|
|
@@ -30,14 +30,14 @@ const flattenBranches = (branches, ctx)=>{
|
|
|
30
30
|
};
|
|
31
31
|
const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
|
|
32
32
|
if (remainingIndices.length === 1) {
|
|
33
|
-
return (0,
|
|
33
|
+
return (0, _branchTs.flattenBranch)(originalBranches[remainingIndices[0]], ctx);
|
|
34
34
|
}
|
|
35
35
|
const bestDiscriminant = findBestDiscriminant(remainingIndices, discriminants);
|
|
36
36
|
if (!bestDiscriminant) {
|
|
37
37
|
return [
|
|
38
38
|
[
|
|
39
39
|
"branches",
|
|
40
|
-
remainingIndices.map((i)=>(0,
|
|
40
|
+
remainingIndices.map((i)=>branchIncludesMorph(originalBranches[i], ctx.type.meta.scope) ? (0, _errorsTs.throwParseError)((0, _unionTs.writeUndiscriminatableMorphUnionMessage)(`${ctx.path}`)) : (0, _branchTs.flattenBranch)(originalBranches[i], ctx))
|
|
41
41
|
]
|
|
42
42
|
];
|
|
43
43
|
}
|
|
@@ -68,17 +68,15 @@ const calculateDiscriminants = (branches, ctx)=>{
|
|
|
68
68
|
disjointsByPair: {},
|
|
69
69
|
casesByDisjoint: {}
|
|
70
70
|
};
|
|
71
|
-
const morphBranches = branches.map((branch)=>branchIncludesMorph(branch, ctx.type.meta.scope));
|
|
72
71
|
for(let lIndex = 0; lIndex < branches.length - 1; lIndex++){
|
|
73
72
|
for(let rIndex = lIndex + 1; rIndex < branches.length; rIndex++){
|
|
74
73
|
const pairKey = `${lIndex}/${rIndex}`;
|
|
75
74
|
const pairDisjoints = [];
|
|
76
75
|
discriminants.disjointsByPair[pairKey] = pairDisjoints;
|
|
77
76
|
const intersectionState = new _composeTs.IntersectionState(ctx.type, "|");
|
|
78
|
-
(0,
|
|
77
|
+
(0, _branchTs.branchIntersection)(branches[lIndex], branches[rIndex], intersectionState);
|
|
79
78
|
for(const path in intersectionState.disjoints){
|
|
80
|
-
|
|
81
|
-
if (path.includes("${")) {
|
|
79
|
+
if (path.includes(_propsTs.mappedKeys.index)) {
|
|
82
80
|
continue;
|
|
83
81
|
}
|
|
84
82
|
const { l , r , kind } = intersectionState.disjoints[path];
|
|
@@ -119,9 +117,6 @@ const calculateDiscriminants = (branches, ctx)=>{
|
|
|
119
117
|
cases[rSerialized].push(rIndex);
|
|
120
118
|
}
|
|
121
119
|
}
|
|
122
|
-
if ((morphBranches[lIndex] === true || morphBranches[rIndex] === true) && pairDisjoints.length === 0) {
|
|
123
|
-
return (0, _errorsTs.throwParseError)((0, _astTs.writeUndiscriminatableMorphUnionMessage)(`${ctx.path}`));
|
|
124
|
-
}
|
|
125
120
|
}
|
|
126
121
|
}
|
|
127
122
|
return discriminants;
|
|
@@ -162,10 +157,9 @@ const findBestDiscriminant = (remainingIndices, discriminants)=>{
|
|
|
162
157
|
filteredCases[caseKey] = filteredIndices;
|
|
163
158
|
score++;
|
|
164
159
|
}
|
|
165
|
-
const defaultCaseKeys = (0, _genericsTs.
|
|
160
|
+
const defaultCaseKeys = (0, _genericsTs.objectKeysOf)(defaultCases);
|
|
166
161
|
if (defaultCaseKeys.length) {
|
|
167
|
-
|
|
168
|
-
filteredCases["default"] = defaultIndices;
|
|
162
|
+
filteredCases["default"] = defaultCaseKeys.map((k)=>parseInt(k));
|
|
169
163
|
}
|
|
170
164
|
if (!bestDiscriminant || score > bestDiscriminant.score) {
|
|
171
165
|
const [path, kind] = parseQualifiedDisjoint(qualifiedDisjoint);
|
|
@@ -196,5 +190,5 @@ const caseSerializers = {
|
|
|
196
190
|
domain: _domainsTs.domainOf
|
|
197
191
|
};
|
|
198
192
|
const serializeCase = (kind, data)=>caseSerializers[kind](data);
|
|
199
|
-
const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph((0, _propsTs.
|
|
193
|
+
const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph((0, _propsTs.propToNode)(prop), $)) : false;
|
|
200
194
|
const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).meta.includesMorph : Object.values(node).some((predicate)=>predicate === true ? false : (0, _objectKindsTs.isArray)(predicate) ? predicate.some((branch)=>branchIncludesMorph(branch, $)) : branchIncludesMorph(predicate, $));
|
package/dist/cjs/nodes/node.js
CHANGED
|
@@ -10,25 +10,27 @@ function _export(target, all) {
|
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
12
|
nodeIntersection: ()=>nodeIntersection,
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
rootIntersection: ()=>rootIntersection,
|
|
14
|
+
rootUnion: ()=>rootUnion,
|
|
15
15
|
flattenType: ()=>flattenType,
|
|
16
16
|
flattenNode: ()=>flattenNode,
|
|
17
|
-
isLiteralNode: ()=>isLiteralNode
|
|
17
|
+
isLiteralNode: ()=>isLiteralNode,
|
|
18
|
+
toArrayNode: ()=>toArrayNode
|
|
18
19
|
});
|
|
19
|
-
const
|
|
20
|
+
const _intersectionTs = require("../parse/ast/intersection.js");
|
|
20
21
|
const _errorsTs = require("../utils/errors.js");
|
|
21
22
|
const _genericsTs = require("../utils/generics.js");
|
|
22
23
|
const _pathsTs = require("../utils/paths.js");
|
|
23
24
|
const _composeTs = require("./compose.js");
|
|
24
25
|
const _predicateTs = require("./predicate.js");
|
|
26
|
+
const _propsTs = require("./rules/props.js");
|
|
25
27
|
const nodeIntersection = (l, r, state)=>{
|
|
26
28
|
state.domain = undefined;
|
|
27
29
|
const lResolution = state.type.meta.scope.resolveNode(l);
|
|
28
30
|
const rResolution = state.type.meta.scope.resolveNode(r);
|
|
29
31
|
const result = resolutionIntersection(lResolution, rResolution, state);
|
|
30
32
|
if (typeof result === "object" && !(0, _genericsTs.hasKeys)(result)) {
|
|
31
|
-
return (0, _genericsTs.hasKeys)(state.disjoints) ? (0, _composeTs.anonymousDisjoint)() : state.addDisjoint("domain", (0, _genericsTs.
|
|
33
|
+
return (0, _genericsTs.hasKeys)(state.disjoints) ? (0, _composeTs.anonymousDisjoint)() : state.addDisjoint("domain", (0, _genericsTs.objectKeysOf)(lResolution), (0, _genericsTs.objectKeysOf)(rResolution));
|
|
32
34
|
}
|
|
33
35
|
return result === lResolution ? l : result === rResolution ? r : result;
|
|
34
36
|
};
|
|
@@ -43,16 +45,16 @@ const resolutionIntersection = (0, _composeTs.composeKeyedIntersection)((domain,
|
|
|
43
45
|
}, {
|
|
44
46
|
onEmpty: "omit"
|
|
45
47
|
});
|
|
46
|
-
const
|
|
48
|
+
const rootIntersection = (l, r, type)=>{
|
|
47
49
|
const state = new _composeTs.IntersectionState(type, "&");
|
|
48
50
|
const result = nodeIntersection(l, r, state);
|
|
49
|
-
return (0, _composeTs.isDisjoint)(result) ? (0, _errorsTs.throwParseError)((0,
|
|
51
|
+
return (0, _composeTs.isDisjoint)(result) ? (0, _errorsTs.throwParseError)((0, _intersectionTs.compileDisjointReasonsMessage)(state.disjoints)) : (0, _composeTs.isEquality)(result) ? l : result;
|
|
50
52
|
};
|
|
51
|
-
const
|
|
53
|
+
const rootUnion = (l, r, type)=>{
|
|
52
54
|
const lResolution = type.meta.scope.resolveNode(l);
|
|
53
55
|
const rResolution = type.meta.scope.resolveNode(r);
|
|
54
56
|
const result = {};
|
|
55
|
-
const domains = (0, _genericsTs.
|
|
57
|
+
const domains = (0, _genericsTs.objectKeysOf)({
|
|
56
58
|
...lResolution,
|
|
57
59
|
...rResolution
|
|
58
60
|
});
|
|
@@ -61,7 +63,7 @@ const union = (l, r, type)=>{
|
|
|
61
63
|
}
|
|
62
64
|
return result;
|
|
63
65
|
};
|
|
64
|
-
const hasImpliedDomain = (flatPredicate)=>flatPredicate[0] && (flatPredicate[0][0] === "
|
|
66
|
+
const hasImpliedDomain = (flatPredicate)=>flatPredicate[0] && (flatPredicate[0][0] === "value" || flatPredicate[0][0] === "class");
|
|
65
67
|
const flattenType = (type)=>{
|
|
66
68
|
const ctx = {
|
|
67
69
|
type,
|
|
@@ -92,7 +94,7 @@ const flattenNode = (node, ctx)=>{
|
|
|
92
94
|
]
|
|
93
95
|
] : resolution.flat;
|
|
94
96
|
}
|
|
95
|
-
const domains = (0, _genericsTs.
|
|
97
|
+
const domains = (0, _genericsTs.objectKeysOf)(node);
|
|
96
98
|
if (domains.length === 1) {
|
|
97
99
|
const domain = domains[0];
|
|
98
100
|
const predicate = node[domain];
|
|
@@ -124,3 +126,11 @@ const flattenNode = (node, ctx)=>{
|
|
|
124
126
|
const isLiteralNode = (node, domain)=>{
|
|
125
127
|
return (0, _predicateTs.resolutionExtendsDomain)(node, domain) && (0, _predicateTs.isLiteralCondition)(node[domain]);
|
|
126
128
|
};
|
|
129
|
+
const toArrayNode = (node)=>({
|
|
130
|
+
object: {
|
|
131
|
+
class: "Array",
|
|
132
|
+
props: {
|
|
133
|
+
[_propsTs.mappedKeys.index]: node
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
});
|
|
@@ -9,7 +9,6 @@ function _export(target, all) {
|
|
|
9
9
|
});
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
|
-
branchIsTransformation: ()=>branchIsTransformation,
|
|
13
12
|
comparePredicates: ()=>comparePredicates,
|
|
14
13
|
predicateIntersection: ()=>predicateIntersection,
|
|
15
14
|
predicateUnion: ()=>predicateUnion,
|
|
@@ -19,23 +18,22 @@ _export(exports, {
|
|
|
19
18
|
});
|
|
20
19
|
const _genericsTs = require("../utils/generics.js");
|
|
21
20
|
const _objectKindsTs = require("../utils/objectKinds.js");
|
|
22
|
-
const
|
|
21
|
+
const _branchTs = require("./branch.js");
|
|
23
22
|
const _composeTs = require("./compose.js");
|
|
24
23
|
const _discriminateTs = require("./discriminate.js");
|
|
25
|
-
|
|
26
|
-
const branchIsTransformation = (branch)=>"rules" in branch;
|
|
24
|
+
require("./rules/rules.js");
|
|
27
25
|
const emptyRulesIfTrue = (predicate)=>predicate === true ? {} : predicate;
|
|
28
26
|
const comparePredicates = (l, r, context)=>{
|
|
29
27
|
if (l === true && r === true) {
|
|
30
28
|
return (0, _composeTs.equality)();
|
|
31
29
|
}
|
|
32
30
|
if (!(0, _objectKindsTs.isArray)(l) && !(0, _objectKindsTs.isArray)(r)) {
|
|
33
|
-
const result = (0,
|
|
31
|
+
const result = (0, _branchTs.branchIntersection)(emptyRulesIfTrue(l), emptyRulesIfTrue(r), context);
|
|
34
32
|
return result === l ? l : result === r ? r : result;
|
|
35
33
|
}
|
|
36
34
|
const lBranches = (0, _genericsTs.listFrom)(emptyRulesIfTrue(l));
|
|
37
35
|
const rBranches = (0, _genericsTs.listFrom)(emptyRulesIfTrue(r));
|
|
38
|
-
const comparison = (0,
|
|
36
|
+
const comparison = (0, _branchTs.compareBranches)(lBranches, rBranches, context);
|
|
39
37
|
if (comparison.equalities.length === lBranches.length && comparison.equalities.length === rBranches.length) {
|
|
40
38
|
return (0, _composeTs.equality)();
|
|
41
39
|
}
|
|
@@ -50,7 +48,7 @@ const comparePredicates = (l, r, context)=>{
|
|
|
50
48
|
const predicateIntersection = (domain, l, r, state)=>{
|
|
51
49
|
state.domain = domain;
|
|
52
50
|
const comparison = comparePredicates(l, r, state);
|
|
53
|
-
if (!(0,
|
|
51
|
+
if (!(0, _branchTs.isBranchComparison)(comparison)) {
|
|
54
52
|
return comparison;
|
|
55
53
|
}
|
|
56
54
|
const resultBranches = [
|
|
@@ -67,7 +65,7 @@ const predicateIntersection = (domain, l, r, state)=>{
|
|
|
67
65
|
const predicateUnion = (domain, l, r, type)=>{
|
|
68
66
|
const state = new _composeTs.IntersectionState(type, "|");
|
|
69
67
|
const comparison = comparePredicates(l, r, state);
|
|
70
|
-
if (!(0,
|
|
68
|
+
if (!(0, _branchTs.isBranchComparison)(comparison)) {
|
|
71
69
|
return (0, _composeTs.isEquality)(comparison) || comparison === l ? r : comparison === r ? l : // subtype of the other, it consists of two opposite literals
|
|
72
70
|
// and can be simplified to a non-literal boolean.
|
|
73
71
|
domain === "boolean" ? true : [
|
|
@@ -85,10 +83,10 @@ const flattenPredicate = (predicate, context)=>{
|
|
|
85
83
|
if (predicate === true) {
|
|
86
84
|
return [];
|
|
87
85
|
}
|
|
88
|
-
return (0, _objectKindsTs.isArray)(predicate) ? (0, _discriminateTs.flattenBranches)(predicate, context) : (0,
|
|
86
|
+
return (0, _objectKindsTs.isArray)(predicate) ? (0, _discriminateTs.flattenBranches)(predicate, context) : (0, _branchTs.flattenBranch)(predicate, context);
|
|
89
87
|
};
|
|
90
88
|
const isLiteralCondition = (predicate)=>typeof predicate === "object" && "value" in predicate;
|
|
91
89
|
const resolutionExtendsDomain = (resolution, domain)=>{
|
|
92
|
-
const domains = (0, _genericsTs.
|
|
90
|
+
const domains = (0, _genericsTs.objectKeysOf)(resolution);
|
|
93
91
|
return domains.length === 1 && domains[0] === domain;
|
|
94
92
|
};
|
|
@@ -12,10 +12,16 @@ _export(exports, {
|
|
|
12
12
|
classIntersection: ()=>classIntersection,
|
|
13
13
|
checkClass: ()=>checkClass
|
|
14
14
|
});
|
|
15
|
+
const _objectKindsTs = require("../../utils/objectKinds.js");
|
|
15
16
|
const _composeTs = require("../compose.js");
|
|
16
|
-
const classIntersection = (0, _composeTs.composeIntersection)((l, r, state)=>
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
const classIntersection = (0, _composeTs.composeIntersection)((l, r, state)=>{
|
|
18
|
+
const lClass = typeof l === "string" ? _objectKindsTs.defaultObjectKinds[l] : l;
|
|
19
|
+
const rClass = typeof r === "string" ? _objectKindsTs.defaultObjectKinds[r] : r;
|
|
20
|
+
return lClass === rClass ? (0, _composeTs.equality)() : lClass instanceof rClass ? l : rClass instanceof lClass ? r : state.addDisjoint("class", l, r);
|
|
21
|
+
});
|
|
22
|
+
const checkClass = (expectedClass, state)=>{
|
|
23
|
+
if (typeof expectedClass === "string") {
|
|
24
|
+
return (0, _objectKindsTs.objectKindOf)(state.data) === expectedClass || state.problems.create("class", expectedClass);
|
|
20
25
|
}
|
|
26
|
+
return state.data instanceof expectedClass || state.problems.create("class", expectedClass);
|
|
21
27
|
};
|
|
@@ -26,8 +26,4 @@ const greatestCommonDivisor = (l, r)=>{
|
|
|
26
26
|
}
|
|
27
27
|
return greatestCommonDivisor;
|
|
28
28
|
};
|
|
29
|
-
const checkDivisor = (
|
|
30
|
-
if (data % divisor !== 0) {
|
|
31
|
-
state.problems.add("divisor", data, divisor);
|
|
32
|
-
}
|
|
33
|
-
};
|
|
29
|
+
const checkDivisor = (divisor, state)=>state.data % divisor === 0 || state.problems.create("divisor", divisor);
|
|
@@ -10,14 +10,59 @@ function _export(target, all) {
|
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
12
|
isOptional: ()=>isOptional,
|
|
13
|
+
isPrerequisite: ()=>isPrerequisite,
|
|
14
|
+
mappedKeys: ()=>mappedKeys,
|
|
15
|
+
propToNode: ()=>propToNode,
|
|
13
16
|
propsIntersection: ()=>propsIntersection,
|
|
14
17
|
flattenProps: ()=>flattenProps
|
|
15
18
|
});
|
|
19
|
+
const _genericsTs = require("../../utils/generics.js");
|
|
16
20
|
const _composeTs = require("../compose.js");
|
|
17
21
|
const _nodeTs = require("../node.js");
|
|
18
22
|
const isOptional = (prop)=>prop[0] === "?";
|
|
19
|
-
const
|
|
20
|
-
const
|
|
23
|
+
const isPrerequisite = (prop)=>prop[0] === "!";
|
|
24
|
+
const mappedKeys = {
|
|
25
|
+
index: "[index]"
|
|
26
|
+
};
|
|
27
|
+
const propToNode = (prop)=>isOptional(prop) || isPrerequisite(prop) ? prop[1] : prop;
|
|
28
|
+
const getTupleLengthIfPresent = (result)=>{
|
|
29
|
+
if (typeof result.length === "object" && isPrerequisite(result.length) && typeof result.length[1] !== "string" && (0, _nodeTs.isLiteralNode)(result.length[1], "number")) {
|
|
30
|
+
return result.length[1].number.value;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const propsIntersection = (0, _composeTs.composeIntersection)((l, r, state)=>{
|
|
34
|
+
const result = propKeysIntersection(l, r, state);
|
|
35
|
+
if (typeof result === "symbol") {
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
const lengthValue = getTupleLengthIfPresent(result);
|
|
39
|
+
if (lengthValue === undefined || !(0, _genericsTs.hasKey)(result, mappedKeys.index)) {
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
// if we are at this point, we have an array with an exact length (i.e.
|
|
43
|
+
// a tuple) and an index signature. Intersection each tuple item with
|
|
44
|
+
// the index signature node and remove the index signature via a new
|
|
45
|
+
// updated result, copied from result to avoid mutating existing references.
|
|
46
|
+
const { [mappedKeys.index]: indexProp , ...updatedResult } = result;
|
|
47
|
+
const indexNode = propToNode(indexProp);
|
|
48
|
+
for(let i = 0; i < lengthValue; i++){
|
|
49
|
+
if (!updatedResult[i]) {
|
|
50
|
+
updatedResult[i] = indexNode;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
const existingNodeAtIndex = propToNode(updatedResult[i]);
|
|
54
|
+
state.path.push(`${i}`);
|
|
55
|
+
const updatedResultAtIndex = (0, _nodeTs.nodeIntersection)(existingNodeAtIndex, indexNode, state);
|
|
56
|
+
state.path.pop();
|
|
57
|
+
if ((0, _composeTs.isDisjoint)(updatedResultAtIndex)) {
|
|
58
|
+
return updatedResultAtIndex;
|
|
59
|
+
} else if (!(0, _composeTs.isEquality)(updatedResultAtIndex) && updatedResultAtIndex !== existingNodeAtIndex) {
|
|
60
|
+
updatedResult[i] = updatedResultAtIndex;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return updatedResult;
|
|
64
|
+
});
|
|
65
|
+
const propKeysIntersection = (0, _composeTs.composeKeyedIntersection)((propKey, l, r, context)=>{
|
|
21
66
|
if (l === undefined) {
|
|
22
67
|
return r === undefined ? (0, _composeTs.equality)() : r;
|
|
23
68
|
}
|
|
@@ -25,7 +70,7 @@ const propsIntersection = (0, _composeTs.composeIntersection)((0, _composeTs.com
|
|
|
25
70
|
return l;
|
|
26
71
|
}
|
|
27
72
|
context.path.push(propKey);
|
|
28
|
-
const result = (0, _nodeTs.nodeIntersection)(
|
|
73
|
+
const result = (0, _nodeTs.nodeIntersection)(propToNode(l), propToNode(r), context);
|
|
29
74
|
context.path.pop();
|
|
30
75
|
const resultIsOptional = isOptional(l) && isOptional(r);
|
|
31
76
|
if ((0, _composeTs.isDisjoint)(result) && resultIsOptional) {
|
|
@@ -37,36 +82,41 @@ const propsIntersection = (0, _composeTs.composeIntersection)((0, _composeTs.com
|
|
|
37
82
|
return result;
|
|
38
83
|
}, {
|
|
39
84
|
onEmpty: "bubble"
|
|
40
|
-
})
|
|
85
|
+
});
|
|
41
86
|
const flattenProps = (entries, props, ctx)=>{
|
|
42
|
-
const requiredProps = [];
|
|
43
|
-
const optionalProps = [];
|
|
44
87
|
for(const k in props){
|
|
45
88
|
const prop = props[k];
|
|
46
89
|
ctx.path.push(k);
|
|
47
|
-
if (
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
(0, _nodeTs.flattenNode)(prop
|
|
90
|
+
if (k === mappedKeys.index) {
|
|
91
|
+
entries.push([
|
|
92
|
+
"indexProp",
|
|
93
|
+
(0, _nodeTs.flattenNode)(propToNode(prop), ctx)
|
|
94
|
+
]);
|
|
95
|
+
} else if (isOptional(prop)) {
|
|
96
|
+
entries.push([
|
|
97
|
+
"optionalProp",
|
|
98
|
+
[
|
|
99
|
+
k,
|
|
100
|
+
(0, _nodeTs.flattenNode)(prop[1], ctx)
|
|
101
|
+
]
|
|
102
|
+
]);
|
|
103
|
+
} else if (isPrerequisite(prop)) {
|
|
104
|
+
entries.push([
|
|
105
|
+
"prerequisiteProp",
|
|
106
|
+
[
|
|
107
|
+
k,
|
|
108
|
+
(0, _nodeTs.flattenNode)(prop[1], ctx)
|
|
109
|
+
]
|
|
51
110
|
]);
|
|
52
111
|
} else {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
112
|
+
entries.push([
|
|
113
|
+
"requiredProp",
|
|
114
|
+
[
|
|
115
|
+
k,
|
|
116
|
+
(0, _nodeTs.flattenNode)(prop, ctx)
|
|
117
|
+
]
|
|
56
118
|
]);
|
|
57
119
|
}
|
|
58
120
|
ctx.path.pop();
|
|
59
121
|
}
|
|
60
|
-
if (requiredProps.length) {
|
|
61
|
-
entries.push([
|
|
62
|
-
"requiredProps",
|
|
63
|
-
requiredProps
|
|
64
|
-
]);
|
|
65
|
-
}
|
|
66
|
-
if (optionalProps.length) {
|
|
67
|
-
entries.push([
|
|
68
|
-
"optionalProps",
|
|
69
|
-
optionalProps
|
|
70
|
-
]);
|
|
71
|
-
}
|
|
72
122
|
};
|
|
@@ -17,7 +17,7 @@ _export(exports, {
|
|
|
17
17
|
checkBound: ()=>checkBound,
|
|
18
18
|
compareStrictness: ()=>compareStrictness
|
|
19
19
|
});
|
|
20
|
-
const
|
|
20
|
+
const _dataTs = require("../../utils/data.js");
|
|
21
21
|
const _composeTs = require("../compose.js");
|
|
22
22
|
const minComparators = {
|
|
23
23
|
">": true,
|
|
@@ -93,11 +93,7 @@ const flattenRange = (entries, range, ctx)=>{
|
|
|
93
93
|
]);
|
|
94
94
|
}
|
|
95
95
|
};
|
|
96
|
-
const checkBound = (
|
|
97
|
-
if (!comparatorCheckers[bound.comparator]((0, _sizeTs.sizeOf)(data), bound.limit)) {
|
|
98
|
-
state.problems.add("bound", data, bound);
|
|
99
|
-
}
|
|
100
|
-
};
|
|
96
|
+
const checkBound = (bound, state)=>comparatorCheckers[bound.comparator]((0, _dataTs.sizeOf)(state.data), bound.limit) || state.problems.create("bound", bound);
|
|
101
97
|
const comparatorCheckers = {
|
|
102
98
|
"<": (size, limit)=>size < limit,
|
|
103
99
|
">": (size, limit)=>size > limit,
|
|
@@ -22,9 +22,5 @@ const getRegex = (source)=>{
|
|
|
22
22
|
}
|
|
23
23
|
return regexCache[source];
|
|
24
24
|
};
|
|
25
|
-
const checkRegex = (
|
|
26
|
-
if (!getRegex(source).test(data)) {
|
|
27
|
-
state.problems.add("regex", data, state.path.length === 0 && state.type.name !== "type" ? state.type.name : `/${source}/`);
|
|
28
|
-
}
|
|
29
|
-
};
|
|
25
|
+
const checkRegex = (source, state)=>getRegex(source).test(state.data) || state.problems.create("regex", `/${source}/`);
|
|
30
26
|
const regexIntersection = (0, _composeTs.composeIntersection)(_collapsibleSetTs.collapsibleListUnion);
|