arktype 0.3.0 → 0.5.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/branch.js +15 -6
- package/dist/cjs/nodes/compose.js +1 -1
- package/dist/cjs/nodes/discriminate.js +89 -21
- package/dist/cjs/nodes/node.js +11 -11
- package/dist/cjs/nodes/predicate.js +1 -1
- package/dist/cjs/nodes/rules/class.js +2 -2
- package/dist/cjs/nodes/rules/divisor.js +1 -1
- package/dist/cjs/nodes/rules/props.js +1 -2
- package/dist/cjs/nodes/rules/range.js +2 -2
- package/dist/cjs/nodes/rules/regex.js +1 -1
- package/dist/cjs/nodes/rules/rules.js +4 -0
- 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 +1 -1
- package/dist/cjs/parse/{tuple → ast}/narrow.js +2 -2
- package/dist/cjs/parse/{tuple → ast}/tuple.js +11 -10
- 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 -7
- 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 +1 -1
- package/dist/cjs/scopes/expressions.js +29 -0
- package/dist/cjs/scopes/scope.js +59 -6
- package/dist/cjs/scopes/validation.js +16 -1
- package/dist/cjs/traverse/problems.js +78 -46
- package/dist/cjs/traverse/traverse.js +62 -58
- package/dist/cjs/utils/data.js +39 -0
- package/dist/cjs/utils/freeze.js +2 -3
- package/dist/cjs/utils/generics.js +20 -2
- package/dist/cjs/utils/numericLiterals.js +2 -14
- package/dist/mjs/nodes/branch.js +14 -5
- package/dist/mjs/nodes/compose.js +2 -2
- package/dist/mjs/nodes/discriminate.js +89 -21
- package/dist/mjs/nodes/node.js +8 -8
- package/dist/mjs/nodes/predicate.js +2 -2
- package/dist/mjs/nodes/rules/class.js +2 -2
- package/dist/mjs/nodes/rules/divisor.js +1 -1
- package/dist/mjs/nodes/rules/props.js +1 -2
- package/dist/mjs/nodes/rules/range.js +2 -2
- package/dist/mjs/nodes/rules/regex.js +1 -1
- package/dist/mjs/nodes/rules/rules.js +4 -0
- 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 +1 -1
- package/dist/mjs/parse/ast/narrow.js +7 -0
- package/dist/mjs/parse/{tuple → ast}/tuple.js +12 -11
- 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 -8
- 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/scope.js +58 -5
- package/dist/mjs/scopes/validation.js +16 -1
- package/dist/mjs/traverse/problems.js +76 -44
- package/dist/mjs/traverse/traverse.js +37 -34
- package/dist/mjs/utils/data.js +24 -0
- package/dist/mjs/utils/freeze.js +2 -3
- package/dist/mjs/utils/generics.js +18 -1
- package/dist/mjs/utils/numericLiterals.js +1 -1
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/nodes/branch.d.ts +3 -12
- package/dist/types/nodes/discriminate.d.ts +6 -8
- package/dist/types/nodes/infer.d.ts +5 -5
- package/dist/types/nodes/node.d.ts +4 -13
- package/dist/types/nodes/predicate.d.ts +2 -29
- package/dist/types/nodes/rules/range.d.ts +2 -2
- package/dist/types/nodes/rules/rules.d.ts +1 -1
- 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/ast/morph.d.ts +18 -0
- 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/shared.d.ts +5 -4
- package/dist/types/parse/string/reduce/static.d.ts +11 -6
- package/dist/types/parse/string/shift/operand/unenclosed.d.ts +1 -1
- package/dist/types/parse/string/shift/operator/bounds.d.ts +3 -2
- package/dist/types/parse/string/shift/operator/divisor.d.ts +5 -1
- package/dist/types/parse/string/shift/scanner.d.ts +1 -1
- package/dist/types/parse/string/string.d.ts +1 -2
- package/dist/types/scopes/expressions.d.ts +48 -0
- package/dist/types/scopes/scope.d.ts +17 -7
- package/dist/types/scopes/standard.d.ts +15 -0
- package/dist/types/scopes/type.d.ts +9 -5
- package/dist/types/scopes/validation.d.ts +4 -0
- package/dist/types/traverse/problems.d.ts +17 -24
- package/dist/types/traverse/traverse.d.ts +67 -37
- 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 +14 -10
- package/dist/types/utils/numericLiterals.d.ts +16 -0
- package/dist/types/utils/paths.d.ts +2 -0
- package/package.json +1 -1
- package/dist/cjs/parse/tuple/keyof.js +0 -64
- package/dist/cjs/utils/size.js +0 -16
- package/dist/mjs/parse/tuple/keyof.js +0 -56
- package/dist/mjs/parse/tuple/narrow.js +0 -7
- package/dist/mjs/utils/size.js +0 -2
- package/dist/types/parse/string/ast.d.ts +0 -46
- package/dist/types/parse/tuple/morph.d.ts +0 -15
- 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";
|
package/dist/cjs/nodes/branch.js
CHANGED
|
@@ -15,7 +15,7 @@ _export(exports, {
|
|
|
15
15
|
flattenBranch: ()=>flattenBranch,
|
|
16
16
|
branchIntersection: ()=>branchIntersection
|
|
17
17
|
});
|
|
18
|
-
const
|
|
18
|
+
const _intersectionJs = require("../parse/ast/intersection.js");
|
|
19
19
|
const _domainsJs = require("../utils/domains.js");
|
|
20
20
|
const _errorsJs = require("../utils/errors.js");
|
|
21
21
|
const _composeJs = require("./compose.js");
|
|
@@ -92,10 +92,19 @@ const flattenBranch = (branch, ctx)=>{
|
|
|
92
92
|
if (isMorphBranch(branch)) {
|
|
93
93
|
const result = (0, _rulesJs.flattenRules)(branch.rules, ctx);
|
|
94
94
|
if (branch.morph) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
+
}
|
|
99
108
|
}
|
|
100
109
|
return result;
|
|
101
110
|
}
|
|
@@ -114,7 +123,7 @@ const branchIntersection = (l, r, state)=>{
|
|
|
114
123
|
morph: l.morph
|
|
115
124
|
};
|
|
116
125
|
}
|
|
117
|
-
return state.lastOperator === "&" ? (0, _errorsJs.throwParseError)((0,
|
|
126
|
+
return state.lastOperator === "&" ? (0, _errorsJs.throwParseError)((0, _intersectionJs.writeImplicitNeverMessage)(state.path, "Intersection", "of morphs")) : {};
|
|
118
127
|
}
|
|
119
128
|
return (0, _composeJs.isDisjoint)(rulesResult) ? rulesResult : {
|
|
120
129
|
rules: (0, _composeJs.isEquality)(rulesResult) ? l.rules : rulesResult,
|
|
@@ -109,7 +109,7 @@ const equality = ()=>equal;
|
|
|
109
109
|
const isEquality = (result)=>result === equal;
|
|
110
110
|
const composeKeyedIntersection = (reducer, config)=>(l, r, state)=>{
|
|
111
111
|
const result = {};
|
|
112
|
-
const keys = (0, _genericsTs.
|
|
112
|
+
const keys = (0, _genericsTs.objectKeysOf)({
|
|
113
113
|
...l,
|
|
114
114
|
...r
|
|
115
115
|
});
|
|
@@ -10,10 +10,10 @@ function _export(target, all) {
|
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
12
|
flattenBranches: ()=>flattenBranches,
|
|
13
|
-
|
|
13
|
+
serializeDefinitionIfAllowed: ()=>serializeDefinitionIfAllowed,
|
|
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");
|
|
@@ -37,15 +37,17 @@ const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
|
|
|
37
37
|
return [
|
|
38
38
|
[
|
|
39
39
|
"branches",
|
|
40
|
-
remainingIndices.map((i)=>branchIncludesMorph(originalBranches[i], ctx.type.meta.scope) ? (0, _errorsTs.throwParseError)((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
|
}
|
|
44
44
|
const cases = {};
|
|
45
|
-
|
|
46
|
-
for(caseKey in bestDiscriminant.indexCases){
|
|
45
|
+
for(const caseKey in bestDiscriminant.indexCases){
|
|
47
46
|
const nextIndices = bestDiscriminant.indexCases[caseKey];
|
|
48
47
|
cases[caseKey] = discriminate(originalBranches, nextIndices, discriminants, ctx);
|
|
48
|
+
if (caseKey !== "default") {
|
|
49
|
+
pruneDiscriminant(cases[caseKey], bestDiscriminant.path, bestDiscriminant, ctx);
|
|
50
|
+
}
|
|
49
51
|
}
|
|
50
52
|
return [
|
|
51
53
|
[
|
|
@@ -58,9 +60,63 @@ const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
|
|
|
58
60
|
]
|
|
59
61
|
];
|
|
60
62
|
};
|
|
63
|
+
const pruneDiscriminant = (entries, segments, discriminant, ctx)=>{
|
|
64
|
+
for(let i = 0; i < entries.length; i++){
|
|
65
|
+
const [k, v] = entries[i];
|
|
66
|
+
if (!segments.length) {
|
|
67
|
+
if (discriminant.kind === "domain") {
|
|
68
|
+
if (k === "domain" || k === "domains") {
|
|
69
|
+
entries.splice(i, 1);
|
|
70
|
+
return;
|
|
71
|
+
} else if (k === "class" || k === "value") {
|
|
72
|
+
// these keys imply a domain, but also add additional
|
|
73
|
+
// information, so can't be pruned
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
} else if (discriminant.kind === k) {
|
|
77
|
+
entries.splice(i, 1);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
} else if ((k === "requiredProp" || k === "prerequisiteProp" || k === "optionalProp") && v[0] === segments[0]) {
|
|
81
|
+
if (typeof v[1] === "string") {
|
|
82
|
+
if (discriminant.kind !== "domain") {
|
|
83
|
+
return throwPruneFailure(discriminant);
|
|
84
|
+
}
|
|
85
|
+
entries.splice(i, 1);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
pruneDiscriminant(v[1], segments.slice(1), discriminant, ctx);
|
|
89
|
+
if (v[1].length === 0) {
|
|
90
|
+
entries.splice(i, 1);
|
|
91
|
+
}
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
// check for branch keys, which must be traversed even if there are no
|
|
95
|
+
// segments left
|
|
96
|
+
if (k === "domains") {
|
|
97
|
+
if ((0, _genericsTs.keyCount)(v) !== 1 || !v.object) {
|
|
98
|
+
return throwPruneFailure(discriminant);
|
|
99
|
+
}
|
|
100
|
+
pruneDiscriminant(v.object, segments, discriminant, ctx);
|
|
101
|
+
return;
|
|
102
|
+
} else if (k === "switch") {
|
|
103
|
+
for(const caseKey in v.cases){
|
|
104
|
+
pruneDiscriminant(v.cases[caseKey], segments, discriminant, ctx);
|
|
105
|
+
}
|
|
106
|
+
return;
|
|
107
|
+
} else if (k === "branches") {
|
|
108
|
+
for (const branch of v){
|
|
109
|
+
pruneDiscriminant(branch, segments, discriminant, ctx);
|
|
110
|
+
}
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return throwPruneFailure(discriminant);
|
|
115
|
+
};
|
|
116
|
+
const throwPruneFailure = (discriminant)=>(0, _errorsTs.throwInternalError)(`Unexpectedly failed to discriminate ${discriminant.kind} at path '${discriminant.path}'`);
|
|
61
117
|
const discriminantKinds = {
|
|
62
118
|
domain: true,
|
|
63
|
-
|
|
119
|
+
class: true,
|
|
64
120
|
value: true
|
|
65
121
|
};
|
|
66
122
|
const calculateDiscriminants = (branches, ctx)=>{
|
|
@@ -83,8 +139,8 @@ const calculateDiscriminants = (branches, ctx)=>{
|
|
|
83
139
|
if (!(0, _genericsTs.isKeyOf)(kind, discriminantKinds)) {
|
|
84
140
|
continue;
|
|
85
141
|
}
|
|
86
|
-
const lSerialized =
|
|
87
|
-
const rSerialized =
|
|
142
|
+
const lSerialized = serializeDefinitionIfAllowed(kind, l);
|
|
143
|
+
const rSerialized = serializeDefinitionIfAllowed(kind, r);
|
|
88
144
|
if (lSerialized === undefined || rSerialized === undefined) {
|
|
89
145
|
continue;
|
|
90
146
|
}
|
|
@@ -102,19 +158,21 @@ const calculateDiscriminants = (branches, ctx)=>{
|
|
|
102
158
|
continue;
|
|
103
159
|
}
|
|
104
160
|
const cases = discriminants.casesByDisjoint[qualifiedDisjoint];
|
|
105
|
-
|
|
161
|
+
const existingLBranch = cases[lSerialized];
|
|
162
|
+
if (!existingLBranch) {
|
|
106
163
|
cases[lSerialized] = [
|
|
107
164
|
lIndex
|
|
108
165
|
];
|
|
109
|
-
} else if (!
|
|
110
|
-
|
|
166
|
+
} else if (!existingLBranch.includes(lIndex)) {
|
|
167
|
+
existingLBranch.push(lIndex);
|
|
111
168
|
}
|
|
112
|
-
|
|
169
|
+
const existingRBranch = cases[rSerialized];
|
|
170
|
+
if (!existingRBranch) {
|
|
113
171
|
cases[rSerialized] = [
|
|
114
172
|
rIndex
|
|
115
173
|
];
|
|
116
|
-
} else if (!
|
|
117
|
-
|
|
174
|
+
} else if (!existingRBranch.includes(rIndex)) {
|
|
175
|
+
existingRBranch.push(rIndex);
|
|
118
176
|
}
|
|
119
177
|
}
|
|
120
178
|
}
|
|
@@ -142,8 +200,7 @@ const findBestDiscriminant = (remainingIndices, discriminants)=>{
|
|
|
142
200
|
...remainingIndices
|
|
143
201
|
];
|
|
144
202
|
let score = 0;
|
|
145
|
-
|
|
146
|
-
for(caseKey in indexCases){
|
|
203
|
+
for(const caseKey in indexCases){
|
|
147
204
|
const filteredIndices = indexCases[caseKey].filter((i)=>{
|
|
148
205
|
const remainingIndex = remainingIndices.indexOf(i);
|
|
149
206
|
if (remainingIndex !== -1) {
|
|
@@ -157,7 +214,7 @@ const findBestDiscriminant = (remainingIndices, discriminants)=>{
|
|
|
157
214
|
filteredCases[caseKey] = filteredIndices;
|
|
158
215
|
score++;
|
|
159
216
|
}
|
|
160
|
-
const defaultCaseKeys = (0, _genericsTs.
|
|
217
|
+
const defaultCaseKeys = (0, _genericsTs.objectKeysOf)(defaultCases);
|
|
161
218
|
if (defaultCaseKeys.length) {
|
|
162
219
|
filteredCases["default"] = defaultCaseKeys.map((k)=>parseInt(k));
|
|
163
220
|
}
|
|
@@ -179,16 +236,27 @@ const findBestDiscriminant = (remainingIndices, discriminants)=>{
|
|
|
179
236
|
}
|
|
180
237
|
return bestDiscriminant;
|
|
181
238
|
};
|
|
182
|
-
const
|
|
239
|
+
const serializeDefinitionIfAllowed = (kind, definition)=>{
|
|
240
|
+
switch(kind){
|
|
241
|
+
case "value":
|
|
242
|
+
return serializeIfPrimitive(definition);
|
|
243
|
+
case "domain":
|
|
244
|
+
return definition;
|
|
245
|
+
case "class":
|
|
246
|
+
return typeof definition === "string" ? definition : undefined;
|
|
247
|
+
default:
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
};
|
|
183
251
|
const serializeIfPrimitive = (data)=>{
|
|
184
252
|
const domain = (0, _domainsTs.domainOf)(data);
|
|
185
253
|
return domain === "object" || domain === "symbol" ? undefined : (0, _serializeTs.serializePrimitive)(data);
|
|
186
254
|
};
|
|
187
|
-
const
|
|
255
|
+
const serializeData = {
|
|
188
256
|
value: (data)=>serializeIfPrimitive(data) ?? "default",
|
|
189
|
-
|
|
257
|
+
class: (data)=>(0, _objectKindsTs.objectKindOf)(data) ?? "default",
|
|
190
258
|
domain: _domainsTs.domainOf
|
|
191
259
|
};
|
|
192
|
-
const serializeCase = (kind, data)=>
|
|
260
|
+
const serializeCase = (kind, data)=>serializeData[kind](data);
|
|
193
261
|
const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph((0, _propsTs.propToNode)(prop), $)) : false;
|
|
194
262
|
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,14 +10,14 @@ 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
17
|
isLiteralNode: ()=>isLiteralNode,
|
|
18
|
-
|
|
18
|
+
toArrayNode: ()=>toArrayNode
|
|
19
19
|
});
|
|
20
|
-
const
|
|
20
|
+
const _intersectionTs = require("../parse/ast/intersection.js");
|
|
21
21
|
const _errorsTs = require("../utils/errors.js");
|
|
22
22
|
const _genericsTs = require("../utils/generics.js");
|
|
23
23
|
const _pathsTs = require("../utils/paths.js");
|
|
@@ -30,7 +30,7 @@ const nodeIntersection = (l, r, state)=>{
|
|
|
30
30
|
const rResolution = state.type.meta.scope.resolveNode(r);
|
|
31
31
|
const result = resolutionIntersection(lResolution, rResolution, state);
|
|
32
32
|
if (typeof result === "object" && !(0, _genericsTs.hasKeys)(result)) {
|
|
33
|
-
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));
|
|
34
34
|
}
|
|
35
35
|
return result === lResolution ? l : result === rResolution ? r : result;
|
|
36
36
|
};
|
|
@@ -45,16 +45,16 @@ const resolutionIntersection = (0, _composeTs.composeKeyedIntersection)((domain,
|
|
|
45
45
|
}, {
|
|
46
46
|
onEmpty: "omit"
|
|
47
47
|
});
|
|
48
|
-
const
|
|
48
|
+
const rootIntersection = (l, r, type)=>{
|
|
49
49
|
const state = new _composeTs.IntersectionState(type, "&");
|
|
50
50
|
const result = nodeIntersection(l, r, state);
|
|
51
|
-
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;
|
|
52
52
|
};
|
|
53
|
-
const
|
|
53
|
+
const rootUnion = (l, r, type)=>{
|
|
54
54
|
const lResolution = type.meta.scope.resolveNode(l);
|
|
55
55
|
const rResolution = type.meta.scope.resolveNode(r);
|
|
56
56
|
const result = {};
|
|
57
|
-
const domains = (0, _genericsTs.
|
|
57
|
+
const domains = (0, _genericsTs.objectKeysOf)({
|
|
58
58
|
...lResolution,
|
|
59
59
|
...rResolution
|
|
60
60
|
});
|
|
@@ -94,7 +94,7 @@ const flattenNode = (node, ctx)=>{
|
|
|
94
94
|
]
|
|
95
95
|
] : resolution.flat;
|
|
96
96
|
}
|
|
97
|
-
const domains = (0, _genericsTs.
|
|
97
|
+
const domains = (0, _genericsTs.objectKeysOf)(node);
|
|
98
98
|
if (domains.length === 1) {
|
|
99
99
|
const domain = domains[0];
|
|
100
100
|
const predicate = node[domain];
|
|
@@ -126,7 +126,7 @@ const flattenNode = (node, ctx)=>{
|
|
|
126
126
|
const isLiteralNode = (node, domain)=>{
|
|
127
127
|
return (0, _predicateTs.resolutionExtendsDomain)(node, domain) && (0, _predicateTs.isLiteralCondition)(node[domain]);
|
|
128
128
|
};
|
|
129
|
-
const
|
|
129
|
+
const toArrayNode = (node)=>({
|
|
130
130
|
object: {
|
|
131
131
|
class: "Array",
|
|
132
132
|
props: {
|
|
@@ -87,6 +87,6 @@ const flattenPredicate = (predicate, context)=>{
|
|
|
87
87
|
};
|
|
88
88
|
const isLiteralCondition = (predicate)=>typeof predicate === "object" && "value" in predicate;
|
|
89
89
|
const resolutionExtendsDomain = (resolution, domain)=>{
|
|
90
|
-
const domains = (0, _genericsTs.
|
|
90
|
+
const domains = (0, _genericsTs.objectKeysOf)(resolution);
|
|
91
91
|
return domains.length === 1 && domains[0] === domain;
|
|
92
92
|
};
|
|
@@ -21,7 +21,7 @@ const classIntersection = (0, _composeTs.composeIntersection)((l, r, state)=>{
|
|
|
21
21
|
});
|
|
22
22
|
const checkClass = (expectedClass, state)=>{
|
|
23
23
|
if (typeof expectedClass === "string") {
|
|
24
|
-
return (0, _objectKindsTs.objectKindOf)(state.data) === expectedClass || state.problems.add("class",
|
|
24
|
+
return (0, _objectKindsTs.objectKindOf)(state.data) === expectedClass || !state.problems.add("class", expectedClass);
|
|
25
25
|
}
|
|
26
|
-
return state.data instanceof expectedClass || state.problems.add("class",
|
|
26
|
+
return state.data instanceof expectedClass || !state.problems.add("class", expectedClass);
|
|
27
27
|
};
|
|
@@ -26,4 +26,4 @@ const greatestCommonDivisor = (l, r)=>{
|
|
|
26
26
|
}
|
|
27
27
|
return greatestCommonDivisor;
|
|
28
28
|
};
|
|
29
|
-
const checkDivisor = (divisor, state)=>state.data % divisor === 0 || state.problems.add("divisor",
|
|
29
|
+
const checkDivisor = (divisor, state)=>state.data % divisor === 0 || !state.problems.add("divisor", divisor);
|
|
@@ -16,7 +16,6 @@ _export(exports, {
|
|
|
16
16
|
propsIntersection: ()=>propsIntersection,
|
|
17
17
|
flattenProps: ()=>flattenProps
|
|
18
18
|
});
|
|
19
|
-
const _genericsTs = require("../../utils/generics.js");
|
|
20
19
|
const _composeTs = require("../compose.js");
|
|
21
20
|
const _nodeTs = require("../node.js");
|
|
22
21
|
const isOptional = (prop)=>prop[0] === "?";
|
|
@@ -36,7 +35,7 @@ const propsIntersection = (0, _composeTs.composeIntersection)((l, r, state)=>{
|
|
|
36
35
|
return result;
|
|
37
36
|
}
|
|
38
37
|
const lengthValue = getTupleLengthIfPresent(result);
|
|
39
|
-
if (lengthValue === undefined || !(
|
|
38
|
+
if (lengthValue === undefined || !(mappedKeys.index in result)) {
|
|
40
39
|
return result;
|
|
41
40
|
}
|
|
42
41
|
// if we are at this point, we have an array with an exact length (i.e.
|
|
@@ -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,7 +93,7 @@ const flattenRange = (entries, range, ctx)=>{
|
|
|
93
93
|
]);
|
|
94
94
|
}
|
|
95
95
|
};
|
|
96
|
-
const checkBound = (bound, state)=>comparatorCheckers[bound.comparator]((0,
|
|
96
|
+
const checkBound = (bound, state)=>comparatorCheckers[bound.comparator]((0, _dataTs.sizeOf)(state.data), bound.limit) || !state.problems.add("bound", bound);
|
|
97
97
|
const comparatorCheckers = {
|
|
98
98
|
"<": (size, limit)=>size < limit,
|
|
99
99
|
">": (size, limit)=>size > limit,
|
|
@@ -22,5 +22,5 @@ const getRegex = (source)=>{
|
|
|
22
22
|
}
|
|
23
23
|
return regexCache[source];
|
|
24
24
|
};
|
|
25
|
-
const checkRegex = (source, state)=>getRegex(source).test(state.data) || state.problems.add("regex",
|
|
25
|
+
const checkRegex = (source, state)=>getRegex(source).test(state.data) || !state.problems.add("regex", `/${source}/`);
|
|
26
26
|
const regexIntersection = (0, _composeTs.composeIntersection)(_collapsibleSetTs.collapsibleListUnion);
|
|
@@ -41,6 +41,10 @@ const flattenRules = (rules, ctx)=>{
|
|
|
41
41
|
for(k in rules){
|
|
42
42
|
ruleFlatteners[k](entries, rules[k], ctx);
|
|
43
43
|
}
|
|
44
|
+
// Some entries with the same precedence, e.g. morphs flattened from a list,
|
|
45
|
+
// rely on the fact that JS's builtin sort is stable to behave as expected
|
|
46
|
+
// when traversed:
|
|
47
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
|
|
44
48
|
return entries.sort((l, r)=>precedenceMap[l[0]] - precedenceMap[r[0]]);
|
|
45
49
|
};
|
|
46
50
|
const ruleFlatteners = {
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "writeUnboundableMessage", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: ()=>writeUnboundableMessage
|
|
8
|
+
});
|
|
9
|
+
const writeUnboundableMessage = (root)=>`Bounded expression ${root} must be a number, string or array`;
|
|
@@ -18,7 +18,7 @@ const _genericsTs = require("../../utils/generics.js");
|
|
|
18
18
|
const _serializeTs = require("../../utils/serialize.js");
|
|
19
19
|
const writeMalformedDistributableFunctionMessage = (def)=>`Expected a Function or Record<Domain, Function> operand (${(0, _serializeTs.stringify)(def)} was invalid)`;
|
|
20
20
|
const distributeFunctionToNode = (distributableFunction, inputNode, ctx, ruleKey)=>{
|
|
21
|
-
const domains = (0, _genericsTs.
|
|
21
|
+
const domains = (0, _genericsTs.objectKeysOf)(ctx.type.meta.scope.resolveNode(inputNode));
|
|
22
22
|
if (!(0, _domainsTs.hasDomain)(distributableFunction, "object")) {
|
|
23
23
|
return (0, _errorsTs.throwParseError)(writeMalformedDistributableFunctionMessage(distributableFunction));
|
|
24
24
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "writeIndivisibleMessage", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: ()=>writeIndivisibleMessage
|
|
8
|
+
});
|
|
9
|
+
const writeIndivisibleMessage = (root)=>`Divisibility operand ${root} must be a number`;
|
|
@@ -10,15 +10,12 @@ function _export(target, all) {
|
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
12
|
compileDisjointReasonsMessage: ()=>compileDisjointReasonsMessage,
|
|
13
|
-
writeImplicitNeverMessage: ()=>writeImplicitNeverMessage
|
|
14
|
-
writeUndiscriminatableMorphUnionMessage: ()=>writeUndiscriminatableMorphUnionMessage,
|
|
15
|
-
writeIndivisibleMessage: ()=>writeIndivisibleMessage,
|
|
16
|
-
writeUnboundableMessage: ()=>writeUnboundableMessage
|
|
13
|
+
writeImplicitNeverMessage: ()=>writeImplicitNeverMessage
|
|
17
14
|
});
|
|
18
15
|
const _composeTs = require("../../nodes/compose.js");
|
|
19
16
|
const _genericsTs = require("../../utils/generics.js");
|
|
20
17
|
const compileDisjointReasonsMessage = (disjoints)=>{
|
|
21
|
-
const paths = (0, _genericsTs.
|
|
18
|
+
const paths = (0, _genericsTs.objectKeysOf)(disjoints);
|
|
22
19
|
if (paths.length === 1) {
|
|
23
20
|
const path = paths[0];
|
|
24
21
|
return `${path === "/" ? "" : `At ${path}: `}Intersection of ${_composeTs.disjointDescriptionWriters[disjoints[path].kind](disjoints[path])} results in an unsatisfiable type`;
|
|
@@ -31,6 +28,3 @@ const compileDisjointReasonsMessage = (disjoints)=>{
|
|
|
31
28
|
return message;
|
|
32
29
|
};
|
|
33
30
|
const writeImplicitNeverMessage = (path, operator, description)=>`${path.length ? `At ${path}: ` : ""}${operator} ${description ? `${description} ` : ""}results in an unsatisfiable type`;
|
|
34
|
-
const writeUndiscriminatableMorphUnionMessage = (path)=>`${path === "/" ? "A" : `At ${path}, a`} union including one or more morphs must be discriminatable`;
|
|
35
|
-
const writeIndivisibleMessage = (root)=>`Divisibility operand ${root} must be a number`;
|
|
36
|
-
const writeUnboundableMessage = (root)=>`Bounded expression ${root} must be a number, string or array`;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "parseKeyOfTuple", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: ()=>parseKeyOfTuple
|
|
8
|
+
});
|
|
9
|
+
const _propsTs = require("../../nodes/rules/props.js");
|
|
10
|
+
const _errorsTs = require("../../utils/errors.js");
|
|
11
|
+
const _freezeTs = require("../../utils/freeze.js");
|
|
12
|
+
const _genericsTs = require("../../utils/generics.js");
|
|
13
|
+
const _numericLiteralsTs = require("../../utils/numericLiterals.js");
|
|
14
|
+
const _objectKindsTs = require("../../utils/objectKinds.js");
|
|
15
|
+
const _serializeTs = require("../../utils/serialize.js");
|
|
16
|
+
const _definitionTs = require("../definition.js");
|
|
17
|
+
const _intersectionTs = require("./intersection.js");
|
|
18
|
+
var _keyNode, _keyType, _keyNode1, _keyNode2;
|
|
19
|
+
const arrayIndexStringBranch = (0, _freezeTs.deepFreeze)({
|
|
20
|
+
regex: _numericLiteralsTs.wellFormedNonNegativeIntegerMatcher.source
|
|
21
|
+
});
|
|
22
|
+
const arrayIndexNumberBranch = (0, _freezeTs.deepFreeze)({
|
|
23
|
+
range: {
|
|
24
|
+
min: {
|
|
25
|
+
comparator: ">=",
|
|
26
|
+
limit: 1
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
divisor: 1
|
|
30
|
+
});
|
|
31
|
+
const parseKeyOfTuple = (def, ctx)=>{
|
|
32
|
+
const resolution = ctx.type.meta.scope.resolveNode((0, _definitionTs.parseDefinition)(def[1], ctx));
|
|
33
|
+
const predicateKeys = (0, _genericsTs.objectKeysOf)(resolution).map((domain)=>keysOfPredicate(domain, resolution[domain]));
|
|
34
|
+
const sharedKeys = sharedKeysOf(predicateKeys);
|
|
35
|
+
if (!sharedKeys.length) {
|
|
36
|
+
return (0, _intersectionTs.writeImplicitNeverMessage)(ctx.path, "keyof");
|
|
37
|
+
}
|
|
38
|
+
const keyNode = {};
|
|
39
|
+
for (const key of sharedKeys){
|
|
40
|
+
const keyType = typeof key;
|
|
41
|
+
if (keyType === "string" || keyType === "number" || keyType === "symbol") {
|
|
42
|
+
(_keyNode = keyNode)[_keyType = keyType] ?? (_keyNode[_keyType] = []);
|
|
43
|
+
keyNode[keyType].push({
|
|
44
|
+
value: key
|
|
45
|
+
});
|
|
46
|
+
} else if (key === _numericLiteralsTs.wellFormedNonNegativeIntegerMatcher) {
|
|
47
|
+
(_keyNode1 = keyNode).string ?? (_keyNode1.string = []);
|
|
48
|
+
keyNode.string.push(arrayIndexStringBranch);
|
|
49
|
+
(_keyNode2 = keyNode).number ?? (_keyNode2.number = []);
|
|
50
|
+
keyNode.number.push(arrayIndexNumberBranch);
|
|
51
|
+
} else {
|
|
52
|
+
return (0, _errorsTs.throwInternalError)(`Unexpected keyof key '${(0, _serializeTs.stringify)(key)}'`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return Object.fromEntries(Object.entries(keyNode).map(([domain, branches])=>[
|
|
56
|
+
domain,
|
|
57
|
+
branches.length === 1 ? branches[0] : branches
|
|
58
|
+
]));
|
|
59
|
+
};
|
|
60
|
+
const baseKeysByDomain = {
|
|
61
|
+
bigint: (0, _genericsTs.prototypeKeysOf)(0n),
|
|
62
|
+
boolean: (0, _genericsTs.prototypeKeysOf)(false),
|
|
63
|
+
null: [],
|
|
64
|
+
number: (0, _genericsTs.prototypeKeysOf)(0),
|
|
65
|
+
// TS doesn't include the Object prototype in keyof, so keyof object is never
|
|
66
|
+
object: [],
|
|
67
|
+
string: (0, _genericsTs.prototypeKeysOf)(""),
|
|
68
|
+
symbol: (0, _genericsTs.prototypeKeysOf)(Symbol()),
|
|
69
|
+
undefined: []
|
|
70
|
+
};
|
|
71
|
+
const keysOfPredicate = (domain, predicate)=>domain !== "object" || predicate === true ? baseKeysByDomain[domain] : sharedKeysOf((0, _genericsTs.listFrom)(predicate).map((branch)=>keysOfObjectBranch(branch)));
|
|
72
|
+
const sharedKeysOf = (keyBranches)=>{
|
|
73
|
+
if (!keyBranches.length) {
|
|
74
|
+
return [];
|
|
75
|
+
}
|
|
76
|
+
let sharedKeys = keyBranches[0];
|
|
77
|
+
for(let i = 1; i < keyBranches.length; i++){
|
|
78
|
+
// we can filter directly by equality here because the RegExp we're
|
|
79
|
+
// using will always be reference equal to
|
|
80
|
+
// wellFormedNonNegativeIntegerMatcher
|
|
81
|
+
sharedKeys = sharedKeys.filter((k)=>keyBranches[i].includes(k));
|
|
82
|
+
}
|
|
83
|
+
return sharedKeys;
|
|
84
|
+
};
|
|
85
|
+
const keysOfObjectBranch = (branch)=>{
|
|
86
|
+
const result = [];
|
|
87
|
+
if ("props" in branch) {
|
|
88
|
+
for (const key of Object.keys(branch.props)){
|
|
89
|
+
if (key === _propsTs.mappedKeys.index) {
|
|
90
|
+
result.push(_numericLiteralsTs.wellFormedNonNegativeIntegerMatcher);
|
|
91
|
+
} else if (!result.includes(key)) {
|
|
92
|
+
result.push(key);
|
|
93
|
+
if (_numericLiteralsTs.wellFormedNonNegativeIntegerMatcher.test(key)) {
|
|
94
|
+
// allow numeric access to keys
|
|
95
|
+
result.push((0, _numericLiteralsTs.tryParseWellFormedInteger)(key, `Unexpectedly failed to parse an integer from key '${key}'`));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if ("class" in branch) {
|
|
101
|
+
const constructor = typeof branch.class === "string" ? _objectKindsTs.defaultObjectKinds[branch.class] : branch.class;
|
|
102
|
+
for (const key1 of (0, _genericsTs.prototypeKeysOf)(constructor.prototype)){
|
|
103
|
+
if (!result.includes(key1)) {
|
|
104
|
+
result.push(key1);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return result;
|
|
109
|
+
};
|
|
@@ -54,4 +54,4 @@ const applyMorph = (branch, morph)=>(0, _branchTs.isMorphBranch)(branch) ? {
|
|
|
54
54
|
rules: branch,
|
|
55
55
|
morph
|
|
56
56
|
};
|
|
57
|
-
const writeMalformedMorphExpressionMessage = (value)=>`Morph expression requires a function following '
|
|
57
|
+
const writeMalformedMorphExpressionMessage = (value)=>`Morph expression requires a function following '|>' (was ${typeof value})`;
|
|
@@ -8,8 +8,8 @@ Object.defineProperty(exports, "parseNarrowTuple", {
|
|
|
8
8
|
});
|
|
9
9
|
const _nodeTs = require("../../nodes/node.js");
|
|
10
10
|
const _definitionTs = require("../definition.js");
|
|
11
|
-
const
|
|
11
|
+
const _distributableFunctionTs = require("./distributableFunction.js");
|
|
12
12
|
const parseNarrowTuple = (def, ctx)=>{
|
|
13
13
|
const inputNode = (0, _definitionTs.parseDefinition)(def[0], ctx);
|
|
14
|
-
return (0, _nodeTs.
|
|
14
|
+
return (0, _nodeTs.rootIntersection)(inputNode, (0, _distributableFunctionTs.distributeFunctionToNode)(def[2], inputNode, ctx, "narrow"), ctx.type);
|
|
15
15
|
};
|