arktype 1.0.16-alpha → 1.0.17-alpha
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/README.md +3 -3
- package/dist/cjs/src/main.js +38 -0
- package/dist/cjs/src/nodes/branch.js +140 -0
- package/dist/cjs/src/nodes/compose.js +157 -0
- package/dist/{nodes → cjs/src/nodes}/discriminate.js +92 -100
- package/dist/cjs/src/nodes/node.js +139 -0
- package/dist/cjs/src/nodes/predicate.js +86 -0
- package/dist/cjs/src/nodes/rules/class.js +25 -0
- package/dist/cjs/src/nodes/rules/collapsibleSet.js +48 -0
- package/dist/cjs/src/nodes/rules/divisor.js +29 -0
- package/dist/cjs/src/nodes/rules/props.js +160 -0
- package/dist/cjs/src/nodes/rules/range.js +105 -0
- package/dist/cjs/src/nodes/rules/regex.js +26 -0
- package/dist/cjs/src/nodes/rules/rules.js +123 -0
- package/dist/cjs/src/parse/ast/ast.js +4 -0
- package/dist/cjs/src/parse/ast/bound.js +16 -0
- package/dist/cjs/src/parse/ast/config.js +13 -0
- package/dist/cjs/src/parse/ast/distributableFunction.js +48 -0
- package/dist/cjs/src/parse/ast/divisor.js +9 -0
- package/dist/cjs/src/parse/ast/intersection.js +30 -0
- package/dist/cjs/src/parse/ast/keyof.js +111 -0
- package/dist/cjs/src/parse/ast/morph.js +58 -0
- package/dist/cjs/src/parse/ast/narrow.js +22 -0
- package/dist/cjs/src/parse/ast/node.js +4 -0
- package/dist/cjs/src/parse/ast/tuple.js +94 -0
- package/dist/cjs/src/parse/ast/union.js +9 -0
- package/dist/cjs/src/parse/ast/utils.js +4 -0
- package/dist/cjs/src/parse/definition.js +61 -0
- package/dist/cjs/src/parse/record.js +37 -0
- package/dist/cjs/src/parse/string/reduce/dynamic.js +167 -0
- package/dist/cjs/src/parse/string/reduce/shared.js +23 -0
- package/dist/cjs/src/parse/string/reduce/static.js +4 -0
- package/dist/cjs/src/parse/string/shift/operand/enclosed.js +54 -0
- package/dist/cjs/src/parse/string/shift/operand/operand.js +11 -0
- package/dist/cjs/src/parse/string/shift/operand/unenclosed.js +53 -0
- package/dist/cjs/src/parse/string/shift/operator/bounds.js +87 -0
- package/dist/cjs/src/parse/string/shift/operator/divisor.js +35 -0
- package/dist/cjs/src/parse/string/shift/operator/operator.js +26 -0
- package/dist/cjs/src/parse/string/shift/scanner.js +114 -0
- package/dist/cjs/src/parse/string/string.js +43 -0
- package/dist/cjs/src/scopes/ark.js +45 -0
- package/dist/cjs/src/scopes/cache.js +52 -0
- package/dist/cjs/src/scopes/expressions.js +29 -0
- package/dist/cjs/src/scopes/jsObjects.js +93 -0
- package/dist/cjs/src/scopes/scope.js +323 -0
- package/dist/cjs/src/scopes/tsKeywords.js +113 -0
- package/dist/cjs/src/scopes/type.js +50 -0
- package/dist/cjs/src/scopes/validation/creditCard.js +47 -0
- package/dist/cjs/src/scopes/validation/date.js +62 -0
- package/dist/cjs/src/scopes/validation/validation.js +108 -0
- package/dist/cjs/src/traverse/problems.js +281 -0
- package/dist/cjs/src/traverse/traverse.js +390 -0
- package/dist/cjs/src/utils/chainableNoOpProxy.js +23 -0
- package/dist/cjs/src/utils/data.js +53 -0
- package/dist/cjs/src/utils/domains.js +30 -0
- package/dist/cjs/src/utils/errors.js +26 -0
- package/dist/cjs/src/utils/freeze.js +15 -0
- package/dist/cjs/src/utils/generics.js +63 -0
- package/dist/cjs/src/utils/numericLiterals.js +75 -0
- package/dist/cjs/src/utils/objectKinds.js +69 -0
- package/dist/cjs/src/utils/paths.js +32 -0
- package/dist/cjs/src/utils/serialize.js +74 -0
- package/dist/cjs/src/utils/unionToTuple.js +4 -0
- package/dist/mjs/package.json +3 -0
- package/dist/mjs/src/main.js +8 -0
- package/dist/mjs/src/nodes/branch.js +123 -0
- package/dist/mjs/src/nodes/compose.js +135 -0
- package/dist/mjs/src/nodes/discriminate.js +247 -0
- package/dist/mjs/src/nodes/node.js +117 -0
- package/dist/mjs/src/nodes/predicate.js +69 -0
- package/dist/mjs/src/nodes/rules/class.js +11 -0
- package/dist/mjs/src/nodes/rules/collapsibleSet.js +34 -0
- package/dist/mjs/src/nodes/rules/divisor.js +15 -0
- package/dist/mjs/src/nodes/rules/props.js +142 -0
- package/dist/mjs/src/nodes/rules/range.js +86 -0
- package/dist/mjs/src/nodes/rules/regex.js +11 -0
- package/dist/mjs/src/nodes/rules/rules.js +106 -0
- package/dist/mjs/src/parse/ast/ast.js +1 -0
- package/dist/mjs/src/parse/ast/bound.js +2 -0
- package/dist/mjs/src/parse/ast/config.js +9 -0
- package/dist/mjs/src/parse/ast/distributableFunction.js +34 -0
- package/dist/mjs/src/parse/ast/divisor.js +1 -0
- package/dist/mjs/src/parse/ast/intersection.js +16 -0
- package/dist/mjs/src/parse/ast/keyof.js +103 -0
- package/dist/mjs/src/parse/ast/morph.js +44 -0
- package/dist/mjs/src/parse/ast/narrow.js +14 -0
- package/dist/mjs/src/parse/ast/node.js +1 -0
- package/dist/mjs/src/parse/ast/tuple.js +80 -0
- package/dist/mjs/src/parse/ast/union.js +1 -0
- package/dist/mjs/src/parse/ast/utils.js +1 -0
- package/dist/mjs/src/parse/definition.js +46 -0
- package/dist/mjs/src/parse/record.js +29 -0
- package/dist/mjs/src/parse/string/reduce/dynamic.js +159 -0
- package/dist/mjs/src/parse/string/reduce/shared.js +6 -0
- package/dist/mjs/src/parse/string/reduce/static.js +1 -0
- package/dist/mjs/src/parse/string/shift/operand/enclosed.js +39 -0
- package/dist/mjs/src/parse/string/shift/operand/operand.js +3 -0
- package/dist/mjs/src/parse/string/shift/operand/unenclosed.js +36 -0
- package/dist/mjs/src/parse/string/shift/operator/bounds.js +70 -0
- package/dist/mjs/src/parse/string/shift/operator/divisor.js +21 -0
- package/dist/mjs/src/parse/string/shift/operator/operator.js +13 -0
- package/dist/mjs/src/parse/string/shift/scanner.js +106 -0
- package/dist/mjs/src/parse/string/string.js +28 -0
- package/dist/mjs/src/scopes/ark.js +28 -0
- package/dist/mjs/src/scopes/cache.js +38 -0
- package/dist/{scopes → mjs/src/scopes}/expressions.js +9 -20
- package/dist/mjs/src/scopes/jsObjects.js +82 -0
- package/dist/mjs/src/scopes/scope.js +303 -0
- package/dist/mjs/src/scopes/tsKeywords.js +116 -0
- package/dist/mjs/src/scopes/type.js +35 -0
- package/dist/{scopes → mjs/src/scopes}/validation/creditCard.js +11 -15
- package/dist/{scopes → mjs/src/scopes}/validation/date.js +16 -21
- package/dist/mjs/src/scopes/validation/validation.js +112 -0
- package/dist/mjs/src/traverse/problems.js +260 -0
- package/dist/mjs/src/traverse/traverse.js +373 -0
- package/dist/{utils → mjs/src/utils}/chainableNoOpProxy.js +2 -6
- package/dist/mjs/src/utils/data.js +38 -0
- package/dist/mjs/src/utils/domains.js +15 -0
- package/dist/mjs/src/utils/errors.js +10 -0
- package/dist/mjs/src/utils/freeze.js +7 -0
- package/dist/mjs/src/utils/generics.js +38 -0
- package/dist/mjs/src/utils/numericLiterals.js +72 -0
- package/dist/mjs/src/utils/objectKinds.js +55 -0
- package/dist/{utils → mjs/src/utils}/paths.js +3 -8
- package/dist/mjs/src/utils/serialize.js +58 -0
- package/dist/mjs/src/utils/unionToTuple.js +1 -0
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/package.json +13 -5
- package/dist/main.js +0 -29
- package/dist/nodes/branch.js +0 -135
- package/dist/nodes/compose.js +0 -99
- package/dist/nodes/node.js +0 -134
- package/dist/nodes/predicate.js +0 -88
- package/dist/nodes/rules/class.js +0 -23
- package/dist/nodes/rules/collapsibleSet.js +0 -34
- package/dist/nodes/rules/divisor.js +0 -19
- package/dist/nodes/rules/props.js +0 -130
- package/dist/nodes/rules/range.js +0 -108
- package/dist/nodes/rules/regex.js +0 -17
- package/dist/nodes/rules/rules.js +0 -102
- package/dist/parse/ast/ast.js +0 -2
- package/dist/parse/ast/bound.js +0 -7
- package/dist/parse/ast/config.js +0 -14
- package/dist/parse/ast/distributableFunction.js +0 -40
- package/dist/parse/ast/divisor.js +0 -5
- package/dist/parse/ast/intersection.js +0 -21
- package/dist/parse/ast/keyof.js +0 -107
- package/dist/parse/ast/morph.js +0 -50
- package/dist/parse/ast/narrow.js +0 -17
- package/dist/parse/ast/node.js +0 -2
- package/dist/parse/ast/tuple.js +0 -72
- package/dist/parse/ast/union.js +0 -5
- package/dist/parse/ast/utils.js +0 -2
- package/dist/parse/definition.js +0 -47
- package/dist/parse/record.js +0 -31
- package/dist/parse/string/reduce/dynamic.js +0 -159
- package/dist/parse/string/reduce/shared.js +0 -13
- package/dist/parse/string/reduce/static.js +0 -2
- package/dist/parse/string/shift/operand/enclosed.js +0 -37
- package/dist/parse/string/shift/operand/operand.js +0 -15
- package/dist/parse/string/shift/operand/unenclosed.js +0 -45
- package/dist/parse/string/shift/operator/bounds.js +0 -83
- package/dist/parse/string/shift/operator/divisor.js +0 -23
- package/dist/parse/string/shift/operator/operator.js +0 -34
- package/dist/parse/string/shift/scanner.js +0 -97
- package/dist/parse/string/string.js +0 -35
- package/dist/scopes/ark.js +0 -27
- package/dist/scopes/cache.js +0 -28
- package/dist/scopes/jsObjects.js +0 -20
- package/dist/scopes/scope.js +0 -184
- package/dist/scopes/tsKeywords.js +0 -49
- package/dist/scopes/type.js +0 -40
- package/dist/scopes/validation/validation.js +0 -74
- package/dist/traverse/problems.js +0 -234
- package/dist/traverse/traverse.js +0 -330
- package/dist/utils/data.js +0 -39
- package/dist/utils/domains.js +0 -27
- package/dist/utils/errors.js +0 -17
- package/dist/utils/freeze.js +0 -16
- package/dist/utils/generics.js +0 -53
- package/dist/utils/numericLiterals.js +0 -88
- package/dist/utils/objectKinds.js +0 -69
- package/dist/utils/serialize.js +0 -71
- package/dist/utils/unionToTuple.js +0 -2
- /package/dist/{package.json → cjs/package.json} +0 -0
- /package/dist/{main.d.ts → types/main.d.ts} +0 -0
- /package/dist/{nodes → types/nodes}/branch.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/compose.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/discriminate.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/node.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/predicate.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/class.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/collapsibleSet.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/divisor.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/props.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/range.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/regex.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/rules.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/ast.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/bound.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/config.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/distributableFunction.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/divisor.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/intersection.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/keyof.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/morph.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/narrow.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/node.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/tuple.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/union.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/utils.d.ts +0 -0
- /package/dist/{parse → types/parse}/definition.d.ts +0 -0
- /package/dist/{parse → types/parse}/record.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/reduce/dynamic.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/reduce/shared.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/reduce/static.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operand/enclosed.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operand/operand.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operand/unenclosed.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operator/bounds.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operator/divisor.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operator/operator.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/scanner.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/string.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/ark.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/cache.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/expressions.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/jsObjects.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/scope.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/tsKeywords.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/type.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/validation/creditCard.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/validation/date.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/validation/validation.d.ts +0 -0
- /package/dist/{traverse → types/traverse}/problems.d.ts +0 -0
- /package/dist/{traverse → types/traverse}/traverse.d.ts +0 -0
- /package/dist/{utils → types/utils}/chainableNoOpProxy.d.ts +0 -0
- /package/dist/{utils → types/utils}/data.d.ts +0 -0
- /package/dist/{utils → types/utils}/domains.d.ts +0 -0
- /package/dist/{utils → types/utils}/errors.d.ts +0 -0
- /package/dist/{utils → types/utils}/freeze.d.ts +0 -0
- /package/dist/{utils → types/utils}/generics.d.ts +0 -0
- /package/dist/{utils → types/utils}/numericLiterals.d.ts +0 -0
- /package/dist/{utils → types/utils}/objectKinds.d.ts +0 -0
- /package/dist/{utils → types/utils}/paths.d.ts +0 -0
- /package/dist/{utils → types/utils}/serialize.d.ts +0 -0
- /package/dist/{utils → types/utils}/unionToTuple.d.ts +0 -0
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { composeIntersection, composeKeyedIntersection, equality, isDisjoint, isEquality } from "../compose.js";
|
|
2
|
+
import { flattenNode, isLiteralNode, nodeIntersection } from "../node.js";
|
|
3
|
+
export const isOptional = (prop)=>prop[0] === "?";
|
|
4
|
+
export const isPrerequisite = (prop)=>prop[0] === "!";
|
|
5
|
+
export const mappedKeys = {
|
|
6
|
+
index: "[index]"
|
|
7
|
+
};
|
|
8
|
+
export const propToNode = (prop)=>isOptional(prop) || isPrerequisite(prop) ? prop[1] : prop;
|
|
9
|
+
const getTupleLengthIfPresent = (result)=>{
|
|
10
|
+
if (typeof result.length === "object" && isPrerequisite(result.length) && typeof result.length[1] !== "string" && isLiteralNode(result.length[1], "number")) {
|
|
11
|
+
return result.length[1].number.value;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
export const propsIntersection = composeIntersection((l, r, state)=>{
|
|
15
|
+
const result = propKeysIntersection(l, r, state);
|
|
16
|
+
if (typeof result === "symbol") {
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
const lengthValue = getTupleLengthIfPresent(result);
|
|
20
|
+
if (lengthValue === undefined || !(mappedKeys.index in result)) {
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
// if we are at this point, we have an array with an exact length (i.e.
|
|
24
|
+
// a tuple) and an index signature. Intersection each tuple item with
|
|
25
|
+
// the index signature node and remove the index signature via a new
|
|
26
|
+
// updated result, copied from result to avoid mutating existing references.
|
|
27
|
+
const { [mappedKeys.index]: indexProp , ...updatedResult } = result;
|
|
28
|
+
const indexNode = propToNode(indexProp);
|
|
29
|
+
for(let i = 0; i < lengthValue; i++){
|
|
30
|
+
if (!updatedResult[i]) {
|
|
31
|
+
updatedResult[i] = indexNode;
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
const existingNodeAtIndex = propToNode(updatedResult[i]);
|
|
35
|
+
state.path.push(`${i}`);
|
|
36
|
+
const updatedResultAtIndex = nodeIntersection(existingNodeAtIndex, indexNode, state);
|
|
37
|
+
state.path.pop();
|
|
38
|
+
if (isDisjoint(updatedResultAtIndex)) {
|
|
39
|
+
return updatedResultAtIndex;
|
|
40
|
+
} else if (!isEquality(updatedResultAtIndex) && updatedResultAtIndex !== existingNodeAtIndex) {
|
|
41
|
+
updatedResult[i] = updatedResultAtIndex;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return updatedResult;
|
|
45
|
+
});
|
|
46
|
+
const propKeysIntersection = composeKeyedIntersection((propKey, l, r, context)=>{
|
|
47
|
+
if (l === undefined) {
|
|
48
|
+
return r === undefined ? equality() : r;
|
|
49
|
+
}
|
|
50
|
+
if (r === undefined) {
|
|
51
|
+
return l;
|
|
52
|
+
}
|
|
53
|
+
context.path.push(propKey);
|
|
54
|
+
const result = nodeIntersection(propToNode(l), propToNode(r), context);
|
|
55
|
+
context.path.pop();
|
|
56
|
+
const resultIsOptional = isOptional(l) && isOptional(r);
|
|
57
|
+
if (isDisjoint(result) && resultIsOptional) {
|
|
58
|
+
// If an optional key has an empty intersection, the type can
|
|
59
|
+
// still be satisfied as long as the key is not included. Set
|
|
60
|
+
// the node to never rather than invalidating the type.
|
|
61
|
+
return {};
|
|
62
|
+
}
|
|
63
|
+
return result;
|
|
64
|
+
}, {
|
|
65
|
+
onEmpty: "bubble"
|
|
66
|
+
});
|
|
67
|
+
export const flattenProps = (entries, props, ctx)=>{
|
|
68
|
+
const keyConfig = ctx.type.config?.keys ?? ctx.type.scope.config.keys;
|
|
69
|
+
return keyConfig === "loose" ? flattenLooseProps(entries, props, ctx) : flattenPropsRecord(keyConfig, entries, props, ctx);
|
|
70
|
+
};
|
|
71
|
+
const flattenLooseProps = (entries, props, ctx)=>{
|
|
72
|
+
// if we don't care about extraneous keys, flatten props so we can iterate over the definitions directly
|
|
73
|
+
for(const k in props){
|
|
74
|
+
const prop = props[k];
|
|
75
|
+
ctx.path.push(k);
|
|
76
|
+
if (k === mappedKeys.index) {
|
|
77
|
+
entries.push([
|
|
78
|
+
"indexProp",
|
|
79
|
+
flattenNode(propToNode(prop), ctx)
|
|
80
|
+
]);
|
|
81
|
+
} else if (isOptional(prop)) {
|
|
82
|
+
entries.push([
|
|
83
|
+
"optionalProp",
|
|
84
|
+
[
|
|
85
|
+
k,
|
|
86
|
+
flattenNode(prop[1], ctx)
|
|
87
|
+
]
|
|
88
|
+
]);
|
|
89
|
+
} else if (isPrerequisite(prop)) {
|
|
90
|
+
entries.push([
|
|
91
|
+
"prerequisiteProp",
|
|
92
|
+
[
|
|
93
|
+
k,
|
|
94
|
+
flattenNode(prop[1], ctx)
|
|
95
|
+
]
|
|
96
|
+
]);
|
|
97
|
+
} else {
|
|
98
|
+
entries.push([
|
|
99
|
+
"requiredProp",
|
|
100
|
+
[
|
|
101
|
+
k,
|
|
102
|
+
flattenNode(prop, ctx)
|
|
103
|
+
]
|
|
104
|
+
]);
|
|
105
|
+
}
|
|
106
|
+
ctx.path.pop();
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
const flattenPropsRecord = (kind, entries, props, ctx)=>{
|
|
110
|
+
const result = {
|
|
111
|
+
required: {},
|
|
112
|
+
optional: {}
|
|
113
|
+
};
|
|
114
|
+
// if we need to keep track of extraneous keys, either to add problems or
|
|
115
|
+
// remove them, store the props as a Record to optimize for presence
|
|
116
|
+
// checking as we iterate over the data
|
|
117
|
+
for(const k in props){
|
|
118
|
+
const prop = props[k];
|
|
119
|
+
ctx.path.push(k);
|
|
120
|
+
if (k === mappedKeys.index) {
|
|
121
|
+
result.index = flattenNode(propToNode(prop), ctx);
|
|
122
|
+
} else if (isOptional(prop)) {
|
|
123
|
+
result.optional[k] = flattenNode(prop[1], ctx);
|
|
124
|
+
} else if (isPrerequisite(prop)) {
|
|
125
|
+
// we still have to push prerequisite props as normal entries so they can be checked first
|
|
126
|
+
entries.push([
|
|
127
|
+
"prerequisiteProp",
|
|
128
|
+
[
|
|
129
|
+
k,
|
|
130
|
+
flattenNode(prop[1], ctx)
|
|
131
|
+
]
|
|
132
|
+
]);
|
|
133
|
+
} else {
|
|
134
|
+
result.required[k] = flattenNode(prop, ctx);
|
|
135
|
+
}
|
|
136
|
+
ctx.path.pop();
|
|
137
|
+
}
|
|
138
|
+
entries.push([
|
|
139
|
+
`${kind}Props`,
|
|
140
|
+
result
|
|
141
|
+
]);
|
|
142
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { sizeOf } from "../../utils/data.js";
|
|
2
|
+
import { composeIntersection, equality } from "../compose.js";
|
|
3
|
+
export const minComparators = {
|
|
4
|
+
">": true,
|
|
5
|
+
">=": true
|
|
6
|
+
};
|
|
7
|
+
export const maxComparators = {
|
|
8
|
+
"<": true,
|
|
9
|
+
"<=": true
|
|
10
|
+
};
|
|
11
|
+
export const isEqualityRange = (range)=>"comparator" in range;
|
|
12
|
+
export const rangeIntersection = composeIntersection((l, r, state)=>{
|
|
13
|
+
if (isEqualityRange(l)) {
|
|
14
|
+
if (isEqualityRange(r)) {
|
|
15
|
+
return l.limit === r.limit ? equality() : state.addDisjoint("range", l, r);
|
|
16
|
+
}
|
|
17
|
+
return rangeAllows(r, l.limit) ? l : state.addDisjoint("range", l, r);
|
|
18
|
+
}
|
|
19
|
+
if (isEqualityRange(r)) {
|
|
20
|
+
return rangeAllows(l, r.limit) ? r : state.addDisjoint("range", l, r);
|
|
21
|
+
}
|
|
22
|
+
const stricterMin = compareStrictness("min", l.min, r.min);
|
|
23
|
+
const stricterMax = compareStrictness("max", l.max, r.max);
|
|
24
|
+
if (stricterMin === "l") {
|
|
25
|
+
if (stricterMax === "r") {
|
|
26
|
+
return compareStrictness("min", l.min, r.max) === "l" ? state.addDisjoint("range", l, r) : {
|
|
27
|
+
min: l.min,
|
|
28
|
+
max: r.max
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
return l;
|
|
32
|
+
}
|
|
33
|
+
if (stricterMin === "r") {
|
|
34
|
+
if (stricterMax === "l") {
|
|
35
|
+
return compareStrictness("max", l.max, r.min) === "l" ? state.addDisjoint("range", l, r) : {
|
|
36
|
+
min: r.min,
|
|
37
|
+
max: l.max
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
return r;
|
|
41
|
+
}
|
|
42
|
+
return stricterMax === "l" ? l : stricterMax === "r" ? r : equality();
|
|
43
|
+
});
|
|
44
|
+
const rangeAllows = (range, n)=>isEqualityRange(range) ? n === range.limit : minAllows(range.min, n) && maxAllows(range.max, n);
|
|
45
|
+
const minAllows = (min, n)=>!min || n > min.limit || n === min.limit && !isExclusive(min.comparator);
|
|
46
|
+
const maxAllows = (max, n)=>!max || n < max.limit || n === max.limit && !isExclusive(max.comparator);
|
|
47
|
+
export const flattenRange = (entries, range, ctx)=>{
|
|
48
|
+
const units = ctx.lastDomain === "string" ? "characters" : ctx.lastDomain === "object" ? "items long" : undefined;
|
|
49
|
+
if (isEqualityRange(range)) {
|
|
50
|
+
return entries.push([
|
|
51
|
+
"bound",
|
|
52
|
+
units ? {
|
|
53
|
+
...range,
|
|
54
|
+
units
|
|
55
|
+
} : range
|
|
56
|
+
]);
|
|
57
|
+
}
|
|
58
|
+
if (range.min) {
|
|
59
|
+
entries.push([
|
|
60
|
+
"bound",
|
|
61
|
+
units ? {
|
|
62
|
+
...range.min,
|
|
63
|
+
units
|
|
64
|
+
} : range.min
|
|
65
|
+
]);
|
|
66
|
+
}
|
|
67
|
+
if (range.max) {
|
|
68
|
+
entries.push([
|
|
69
|
+
"bound",
|
|
70
|
+
units ? {
|
|
71
|
+
...range.max,
|
|
72
|
+
units
|
|
73
|
+
} : range.max
|
|
74
|
+
]);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
export const checkBound = (bound, state)=>comparatorCheckers[bound.comparator](sizeOf(state.data), bound.limit) || !state.problems.add("bound", bound);
|
|
78
|
+
const comparatorCheckers = {
|
|
79
|
+
"<": (size, limit)=>size < limit,
|
|
80
|
+
">": (size, limit)=>size > limit,
|
|
81
|
+
"<=": (size, limit)=>size <= limit,
|
|
82
|
+
">=": (size, limit)=>size >= limit,
|
|
83
|
+
"==": (size, limit)=>size === limit
|
|
84
|
+
};
|
|
85
|
+
export const compareStrictness = (kind, l, r)=>!l ? !r ? "=" : "r" : !r ? "l" : l.limit === r.limit ? isExclusive(l.comparator) ? isExclusive(r.comparator) ? "=" : "l" : isExclusive(r.comparator) ? "r" : "=" : kind === "min" ? l.limit > r.limit ? "l" : "r" : l.limit < r.limit ? "l" : "r";
|
|
86
|
+
const isExclusive = (comparator)=>comparator.length === 1;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { composeIntersection } from "../compose.js";
|
|
2
|
+
import { collapsibleListUnion } from "./collapsibleSet.js";
|
|
3
|
+
const regexCache = {};
|
|
4
|
+
export const getRegex = (source)=>{
|
|
5
|
+
if (!regexCache[source]) {
|
|
6
|
+
regexCache[source] = new RegExp(source);
|
|
7
|
+
}
|
|
8
|
+
return regexCache[source];
|
|
9
|
+
};
|
|
10
|
+
export const checkRegex = (source, state)=>getRegex(source).test(state.data) || !state.problems.add("regex", `/${source}/`);
|
|
11
|
+
export const regexIntersection = composeIntersection(collapsibleListUnion);
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { listFrom } from "../../utils/generics.js";
|
|
2
|
+
import { composeIntersection, composeKeyedIntersection, equality } from "../compose.js";
|
|
3
|
+
import { classIntersection } from "./class.js";
|
|
4
|
+
import { collapsibleListUnion } from "./collapsibleSet.js";
|
|
5
|
+
import { divisorIntersection } from "./divisor.js";
|
|
6
|
+
import { flattenProps, propsIntersection } from "./props.js";
|
|
7
|
+
import { flattenRange, rangeIntersection } from "./range.js";
|
|
8
|
+
import { regexIntersection } from "./regex.js";
|
|
9
|
+
export const rulesIntersection = (l, r, state)=>"value" in l ? "value" in r ? l.value === r.value ? equality() : state.addDisjoint("value", l.value, r.value) : literalSatisfiesRules(l.value, r, state) ? l : state.addDisjoint("leftAssignability", l, r) : "value" in r ? literalSatisfiesRules(r.value, l, state) ? r : state.addDisjoint("rightAssignability", l, r) : narrowableRulesIntersection(l, r, state);
|
|
10
|
+
const narrowIntersection = composeIntersection(collapsibleListUnion);
|
|
11
|
+
export const narrowableRulesIntersection = composeKeyedIntersection({
|
|
12
|
+
divisor: divisorIntersection,
|
|
13
|
+
regex: regexIntersection,
|
|
14
|
+
props: propsIntersection,
|
|
15
|
+
class: classIntersection,
|
|
16
|
+
range: rangeIntersection,
|
|
17
|
+
narrow: narrowIntersection
|
|
18
|
+
}, {
|
|
19
|
+
onEmpty: "bubble"
|
|
20
|
+
});
|
|
21
|
+
export const flattenRules = (rules, ctx)=>{
|
|
22
|
+
const entries = [];
|
|
23
|
+
let k;
|
|
24
|
+
for(k in rules){
|
|
25
|
+
ruleFlatteners[k](entries, rules[k], ctx);
|
|
26
|
+
}
|
|
27
|
+
// Some entries with the same precedence, e.g. morphs flattened from a list,
|
|
28
|
+
// rely on the fact that JS's builtin sort is stable to behave as expected
|
|
29
|
+
// when traversed:
|
|
30
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
|
|
31
|
+
return entries.sort((l, r)=>precedenceMap[l[0]] - precedenceMap[r[0]]);
|
|
32
|
+
};
|
|
33
|
+
const ruleFlatteners = {
|
|
34
|
+
regex: (entries, rule)=>{
|
|
35
|
+
for (const source of listFrom(rule)){
|
|
36
|
+
entries.push([
|
|
37
|
+
"regex",
|
|
38
|
+
source
|
|
39
|
+
]);
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
divisor: (entries, rule)=>{
|
|
43
|
+
entries.push([
|
|
44
|
+
"divisor",
|
|
45
|
+
rule
|
|
46
|
+
]);
|
|
47
|
+
},
|
|
48
|
+
range: flattenRange,
|
|
49
|
+
class: (entries, rule)=>{
|
|
50
|
+
entries.push([
|
|
51
|
+
"class",
|
|
52
|
+
rule
|
|
53
|
+
]);
|
|
54
|
+
},
|
|
55
|
+
props: flattenProps,
|
|
56
|
+
narrow: (entries, rule)=>{
|
|
57
|
+
for (const narrow of listFrom(rule)){
|
|
58
|
+
entries.push([
|
|
59
|
+
"narrow",
|
|
60
|
+
narrow
|
|
61
|
+
]);
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
value: (entries, rule)=>{
|
|
65
|
+
entries.push([
|
|
66
|
+
"value",
|
|
67
|
+
rule
|
|
68
|
+
]);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
export const precedenceMap = {
|
|
72
|
+
// Config: Applies before any checks
|
|
73
|
+
config: -1,
|
|
74
|
+
// Critical: No other checks are performed if these fail
|
|
75
|
+
domain: 0,
|
|
76
|
+
value: 0,
|
|
77
|
+
domains: 0,
|
|
78
|
+
branches: 0,
|
|
79
|
+
switch: 0,
|
|
80
|
+
alias: 0,
|
|
81
|
+
class: 0,
|
|
82
|
+
// Shallow: All shallow checks will be performed even if one or more fail
|
|
83
|
+
regex: 1,
|
|
84
|
+
divisor: 1,
|
|
85
|
+
bound: 1,
|
|
86
|
+
// Prerequisite: These are deep checks with special priority, e.g. the
|
|
87
|
+
// length of a tuple, which causes other deep props not to be checked if it
|
|
88
|
+
// is invalid
|
|
89
|
+
prerequisiteProp: 2,
|
|
90
|
+
// Deep: Performed if all shallow checks pass, even if one or more deep checks fail
|
|
91
|
+
distilledProps: 3,
|
|
92
|
+
strictProps: 3,
|
|
93
|
+
requiredProp: 3,
|
|
94
|
+
optionalProp: 3,
|
|
95
|
+
indexProp: 3,
|
|
96
|
+
// Narrow: Only performed if all shallow and deep checks pass
|
|
97
|
+
narrow: 4,
|
|
98
|
+
// Morph: Only performed if all validation passes
|
|
99
|
+
morph: 5
|
|
100
|
+
};
|
|
101
|
+
export const literalSatisfiesRules = (data, rules, state)=>!state.type.scope.type([
|
|
102
|
+
"node",
|
|
103
|
+
{
|
|
104
|
+
[state.domain]: rules
|
|
105
|
+
}
|
|
106
|
+
])(data).problems;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { parseDefinition } from "../definition.js";
|
|
2
|
+
/**
|
|
3
|
+
* @operator {@link parseConfigTuple | :}
|
|
4
|
+
* @docgenTable
|
|
5
|
+
* @tuple ["type", ":", config]
|
|
6
|
+
*/ export const parseConfigTuple = (def, ctx)=>({
|
|
7
|
+
node: ctx.type.scope.resolveTypeNode(parseDefinition(def[0], ctx)),
|
|
8
|
+
config: def[2]
|
|
9
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { hasDomain } from "../../utils/domains.js";
|
|
2
|
+
import { throwParseError } from "../../utils/errors.js";
|
|
3
|
+
import { objectKeysOf } from "../../utils/generics.js";
|
|
4
|
+
import { stringify } from "../../utils/serialize.js";
|
|
5
|
+
export const writeMalformedDistributableFunctionMessage = (def)=>`Expected a Function or Record<Domain, Function> operand (${stringify(def)} was invalid)`;
|
|
6
|
+
export const distributeFunctionToNode = (distributableFunction, node, ctx, ruleKey)=>{
|
|
7
|
+
const domains = objectKeysOf(node);
|
|
8
|
+
if (!hasDomain(distributableFunction, "object")) {
|
|
9
|
+
return throwParseError(writeMalformedDistributableFunctionMessage(distributableFunction));
|
|
10
|
+
}
|
|
11
|
+
const distributed = {};
|
|
12
|
+
if (typeof distributableFunction === "function") {
|
|
13
|
+
const domainFunction = {
|
|
14
|
+
[ruleKey]: distributableFunction
|
|
15
|
+
};
|
|
16
|
+
for (const domain of domains){
|
|
17
|
+
distributed[domain] = domainFunction;
|
|
18
|
+
}
|
|
19
|
+
} else {
|
|
20
|
+
for (const domain of domains){
|
|
21
|
+
if (distributableFunction[domain] === undefined) {
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
const functionInDomain = {
|
|
25
|
+
[ruleKey]: distributableFunction[domain]
|
|
26
|
+
};
|
|
27
|
+
if (typeof functionInDomain[ruleKey] !== "function") {
|
|
28
|
+
return throwParseError(writeMalformedDistributableFunctionMessage(functionInDomain));
|
|
29
|
+
}
|
|
30
|
+
distributed[domain] = functionInDomain;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return distributed;
|
|
34
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const writeIndivisibleMessage = (root)=>`Divisibility operand ${root} must be a number`;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { disjointDescriptionWriters } from "../../nodes/compose.js";
|
|
2
|
+
import { objectKeysOf } from "../../utils/generics.js";
|
|
3
|
+
export const compileDisjointReasonsMessage = (disjoints)=>{
|
|
4
|
+
const paths = objectKeysOf(disjoints);
|
|
5
|
+
if (paths.length === 1) {
|
|
6
|
+
const path = paths[0];
|
|
7
|
+
return `${path === "/" ? "" : `At ${path}: `}Intersection of ${disjointDescriptionWriters[disjoints[path].kind](disjoints[path])} results in an unsatisfiable type`;
|
|
8
|
+
}
|
|
9
|
+
let message = `
|
|
10
|
+
"Intersection results in unsatisfiable types at the following paths:\n`;
|
|
11
|
+
for(const path in disjoints){
|
|
12
|
+
message += ` ${path}: ${disjointDescriptionWriters[disjoints[path].kind](disjoints[path])}\n`;
|
|
13
|
+
}
|
|
14
|
+
return message;
|
|
15
|
+
};
|
|
16
|
+
export const writeImplicitNeverMessage = (path, operator, description)=>`${path.length ? `At ${path}: ` : ""}${operator} ${description ? `${description} ` : ""}results in an unsatisfiable type`;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { mappedKeys } from "../../nodes/rules/props.js";
|
|
2
|
+
import { throwInternalError } from "../../utils/errors.js";
|
|
3
|
+
import { deepFreeze } from "../../utils/freeze.js";
|
|
4
|
+
import { listFrom, objectKeysOf, prototypeKeysOf } from "../../utils/generics.js";
|
|
5
|
+
import { tryParseWellFormedInteger, wellFormedNonNegativeIntegerMatcher } from "../../utils/numericLiterals.js";
|
|
6
|
+
import { defaultObjectKinds } from "../../utils/objectKinds.js";
|
|
7
|
+
import { stringify } from "../../utils/serialize.js";
|
|
8
|
+
import { parseDefinition } from "../definition.js";
|
|
9
|
+
import { writeImplicitNeverMessage } from "./intersection.js";
|
|
10
|
+
const arrayIndexStringBranch = deepFreeze({
|
|
11
|
+
regex: wellFormedNonNegativeIntegerMatcher.source
|
|
12
|
+
});
|
|
13
|
+
const arrayIndexNumberBranch = deepFreeze({
|
|
14
|
+
range: {
|
|
15
|
+
min: {
|
|
16
|
+
comparator: ">=",
|
|
17
|
+
limit: 0
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
divisor: 1
|
|
21
|
+
});
|
|
22
|
+
export const parseKeyOfTuple = (def, ctx)=>{
|
|
23
|
+
const resolution = ctx.type.scope.resolveNode(parseDefinition(def[1], ctx));
|
|
24
|
+
const predicateKeys = objectKeysOf(resolution).map((domain)=>keysOfPredicate(domain, resolution[domain]));
|
|
25
|
+
const sharedKeys = sharedKeysOf(predicateKeys);
|
|
26
|
+
if (!sharedKeys.length) {
|
|
27
|
+
return writeImplicitNeverMessage(ctx.path, "keyof");
|
|
28
|
+
}
|
|
29
|
+
const keyNode = {};
|
|
30
|
+
for (const key of sharedKeys){
|
|
31
|
+
const keyType = typeof key;
|
|
32
|
+
if (keyType === "string" || keyType === "number" || keyType === "symbol") {
|
|
33
|
+
var _keyNode, _keyType;
|
|
34
|
+
(_keyNode = keyNode)[_keyType = keyType] ?? (_keyNode[_keyType] = []);
|
|
35
|
+
keyNode[keyType].push({
|
|
36
|
+
value: key
|
|
37
|
+
});
|
|
38
|
+
} else if (key === wellFormedNonNegativeIntegerMatcher) {
|
|
39
|
+
var _keyNode1, _keyNode2;
|
|
40
|
+
(_keyNode1 = keyNode).string ?? (_keyNode1.string = []);
|
|
41
|
+
keyNode.string.push(arrayIndexStringBranch);
|
|
42
|
+
(_keyNode2 = keyNode).number ?? (_keyNode2.number = []);
|
|
43
|
+
keyNode.number.push(arrayIndexNumberBranch);
|
|
44
|
+
} else {
|
|
45
|
+
return throwInternalError(`Unexpected keyof key '${stringify(key)}'`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return Object.fromEntries(Object.entries(keyNode).map(([domain, branches])=>[
|
|
49
|
+
domain,
|
|
50
|
+
branches.length === 1 ? branches[0] : branches
|
|
51
|
+
]));
|
|
52
|
+
};
|
|
53
|
+
const baseKeysByDomain = {
|
|
54
|
+
bigint: prototypeKeysOf(0n),
|
|
55
|
+
boolean: prototypeKeysOf(false),
|
|
56
|
+
null: [],
|
|
57
|
+
number: prototypeKeysOf(0),
|
|
58
|
+
// TS doesn't include the Object prototype in keyof, so keyof object is never
|
|
59
|
+
object: [],
|
|
60
|
+
string: prototypeKeysOf(""),
|
|
61
|
+
symbol: prototypeKeysOf(Symbol()),
|
|
62
|
+
undefined: []
|
|
63
|
+
};
|
|
64
|
+
const keysOfPredicate = (domain, predicate)=>domain !== "object" || predicate === true ? baseKeysByDomain[domain] : sharedKeysOf(listFrom(predicate).map((branch)=>keysOfObjectBranch(branch)));
|
|
65
|
+
const sharedKeysOf = (keyBranches)=>{
|
|
66
|
+
if (!keyBranches.length) {
|
|
67
|
+
return [];
|
|
68
|
+
}
|
|
69
|
+
let sharedKeys = keyBranches[0];
|
|
70
|
+
for(let i = 1; i < keyBranches.length; i++){
|
|
71
|
+
// we can filter directly by equality here because the RegExp we're
|
|
72
|
+
// using will always be reference equal to
|
|
73
|
+
// wellFormedNonNegativeIntegerMatcher
|
|
74
|
+
sharedKeys = sharedKeys.filter((k)=>keyBranches[i].includes(k));
|
|
75
|
+
}
|
|
76
|
+
return sharedKeys;
|
|
77
|
+
};
|
|
78
|
+
const keysOfObjectBranch = (branch)=>{
|
|
79
|
+
const result = [];
|
|
80
|
+
if ("props" in branch) {
|
|
81
|
+
for (const key of Object.keys(branch.props)){
|
|
82
|
+
if (key === mappedKeys.index) {
|
|
83
|
+
// if any number is a valid key push this RegExp
|
|
84
|
+
result.push(wellFormedNonNegativeIntegerMatcher);
|
|
85
|
+
} else if (!result.includes(key)) {
|
|
86
|
+
result.push(key);
|
|
87
|
+
if (wellFormedNonNegativeIntegerMatcher.test(key)) {
|
|
88
|
+
// allow numeric access to keys
|
|
89
|
+
result.push(tryParseWellFormedInteger(key, `Unexpectedly failed to parse an integer from key '${key}'`));
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if ("class" in branch) {
|
|
95
|
+
const constructor = typeof branch.class === "string" ? defaultObjectKinds[branch.class] : branch.class;
|
|
96
|
+
for (const key of prototypeKeysOf(constructor.prototype)){
|
|
97
|
+
if (!result.includes(key)) {
|
|
98
|
+
result.push(key);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return result;
|
|
103
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { isTransformationBranch } from "../../nodes/branch.js";
|
|
2
|
+
import { throwInternalError, throwParseError } from "../../utils/errors.js";
|
|
3
|
+
import { isArray } from "../../utils/objectKinds.js";
|
|
4
|
+
import { stringify } from "../../utils/serialize.js";
|
|
5
|
+
import { parseDefinition } from "../definition.js";
|
|
6
|
+
export const parseMorphTuple = (def, ctx)=>{
|
|
7
|
+
if (typeof def[2] !== "function") {
|
|
8
|
+
return throwParseError(writeMalformedMorphExpressionMessage(def[2]));
|
|
9
|
+
}
|
|
10
|
+
const node = parseDefinition(def[0], ctx);
|
|
11
|
+
const resolution = ctx.type.scope.resolveTypeNode(node);
|
|
12
|
+
const morph = def[2];
|
|
13
|
+
ctx.type.includesMorph = true;
|
|
14
|
+
let domain;
|
|
15
|
+
const result = {};
|
|
16
|
+
for(domain in resolution){
|
|
17
|
+
const predicate = resolution[domain];
|
|
18
|
+
if (predicate === true) {
|
|
19
|
+
result[domain] = {
|
|
20
|
+
rules: {},
|
|
21
|
+
morph
|
|
22
|
+
};
|
|
23
|
+
} else if (typeof predicate === "object") {
|
|
24
|
+
result[domain] = isArray(predicate) ? predicate.map((branch)=>applyMorph(branch, morph)) : applyMorph(predicate, morph);
|
|
25
|
+
} else {
|
|
26
|
+
throwInternalError(`Unexpected predicate value for domain '${domain}': ${stringify(predicate)}`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
const applyMorph = (branch, morph)=>isTransformationBranch(branch) ? {
|
|
32
|
+
...branch,
|
|
33
|
+
morph: branch.morph ? Array.isArray(branch.morph) ? [
|
|
34
|
+
...branch.morph,
|
|
35
|
+
morph
|
|
36
|
+
] : [
|
|
37
|
+
branch.morph,
|
|
38
|
+
morph
|
|
39
|
+
] : morph
|
|
40
|
+
} : {
|
|
41
|
+
rules: branch,
|
|
42
|
+
morph
|
|
43
|
+
};
|
|
44
|
+
export const writeMalformedMorphExpressionMessage = (value)=>`Morph expression requires a function following '|>' (was ${typeof value})`;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { isConfigNode, rootIntersection } from "../../nodes/node.js";
|
|
2
|
+
import { parseDefinition } from "../definition.js";
|
|
3
|
+
import { distributeFunctionToNode } from "./distributableFunction.js";
|
|
4
|
+
export const parseNarrowTuple = (def, ctx)=>{
|
|
5
|
+
const inputNode = parseDefinition(def[0], ctx);
|
|
6
|
+
const resolution = ctx.type.scope.resolveNode(inputNode);
|
|
7
|
+
const hasConfig = isConfigNode(resolution);
|
|
8
|
+
const typeNode = hasConfig ? resolution.node : resolution;
|
|
9
|
+
const result = rootIntersection(inputNode, distributeFunctionToNode(def[2], typeNode, ctx, "narrow"), ctx.type);
|
|
10
|
+
return hasConfig ? {
|
|
11
|
+
config: resolution.config,
|
|
12
|
+
node: result
|
|
13
|
+
} : result;
|
|
14
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { rootIntersection, rootUnion, toArrayNode } from "../../nodes/node.js";
|
|
2
|
+
import { domainOf } from "../../utils/domains.js";
|
|
3
|
+
import { throwParseError } from "../../utils/errors.js";
|
|
4
|
+
import { parseDefinition } from "../definition.js";
|
|
5
|
+
import { writeMissingRightOperandMessage } from "../string/shift/operand/unenclosed.js";
|
|
6
|
+
import { parseConfigTuple } from "./config.js";
|
|
7
|
+
import { parseKeyOfTuple } from "./keyof.js";
|
|
8
|
+
import { parseMorphTuple } from "./morph.js";
|
|
9
|
+
import { parseNarrowTuple } from "./narrow.js";
|
|
10
|
+
export const parseTuple = (def, ctx)=>{
|
|
11
|
+
if (isIndexOneExpression(def)) {
|
|
12
|
+
return indexOneParsers[def[1]](def, ctx);
|
|
13
|
+
}
|
|
14
|
+
if (isIndexZeroExpression(def)) {
|
|
15
|
+
return prefixParsers[def[0]](def, ctx);
|
|
16
|
+
}
|
|
17
|
+
const props = {
|
|
18
|
+
// length is created as a prerequisite prop, ensuring if it is invalid,
|
|
19
|
+
// no other props will be checked, which is usually desirable for tuple
|
|
20
|
+
// definitions.
|
|
21
|
+
length: [
|
|
22
|
+
"!",
|
|
23
|
+
{
|
|
24
|
+
number: {
|
|
25
|
+
value: def.length
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
]
|
|
29
|
+
};
|
|
30
|
+
for(let i = 0; i < def.length; i++){
|
|
31
|
+
ctx.path.push(`${i}`);
|
|
32
|
+
props[i] = parseDefinition(def[i], ctx);
|
|
33
|
+
ctx.path.pop();
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
object: {
|
|
37
|
+
class: Array,
|
|
38
|
+
props
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
const parseBranchTuple = (def, ctx)=>{
|
|
43
|
+
if (def[2] === undefined) {
|
|
44
|
+
return throwParseError(writeMissingRightOperandMessage(def[1], ""));
|
|
45
|
+
}
|
|
46
|
+
const l = parseDefinition(def[0], ctx);
|
|
47
|
+
const r = parseDefinition(def[2], ctx);
|
|
48
|
+
return def[1] === "&" ? rootIntersection(l, r, ctx.type) : rootUnion(l, r, ctx.type);
|
|
49
|
+
};
|
|
50
|
+
const parseArrayTuple = (def, scope)=>toArrayNode(parseDefinition(def[0], scope));
|
|
51
|
+
export const writeMalformedFunctionalExpressionMessage = (operator, rightDef)=>`Expression requires a function following '${operator}' (was ${typeof rightDef})`;
|
|
52
|
+
const isIndexOneExpression = (def)=>indexOneParsers[def[1]] !== undefined;
|
|
53
|
+
const indexOneParsers = {
|
|
54
|
+
"|": parseBranchTuple,
|
|
55
|
+
"&": parseBranchTuple,
|
|
56
|
+
"[]": parseArrayTuple,
|
|
57
|
+
"=>": parseNarrowTuple,
|
|
58
|
+
"|>": parseMorphTuple,
|
|
59
|
+
":": parseConfigTuple
|
|
60
|
+
};
|
|
61
|
+
const prefixParsers = {
|
|
62
|
+
keyof: parseKeyOfTuple,
|
|
63
|
+
instanceof: (def)=>{
|
|
64
|
+
if (typeof def[1] !== "function") {
|
|
65
|
+
return throwParseError(`Expected a constructor following 'instanceof' operator (was ${typeof def[1]}).`);
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
object: {
|
|
69
|
+
class: def[1]
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
},
|
|
73
|
+
"===": (def)=>({
|
|
74
|
+
[domainOf(def[1])]: {
|
|
75
|
+
value: def[1]
|
|
76
|
+
}
|
|
77
|
+
}),
|
|
78
|
+
node: (def)=>def[1]
|
|
79
|
+
};
|
|
80
|
+
const isIndexZeroExpression = (def)=>prefixParsers[def[0]] !== undefined;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const writeUndiscriminatableMorphUnionMessage = (path)=>`${path === "/" ? "A" : `At ${path}, a`} union including one or more morphs must be discriminatable`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|