arktype 1.0.13-alpha → 1.0.15-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 +68 -11
- package/dist/main.js +29 -0
- package/dist/nodes/branch.js +135 -0
- package/dist/nodes/compose.js +99 -0
- package/dist/{cjs/nodes → nodes}/discriminate.js +100 -92
- package/dist/nodes/node.js +134 -0
- package/dist/nodes/predicate.js +88 -0
- package/dist/nodes/rules/class.js +23 -0
- package/dist/nodes/rules/collapsibleSet.js +34 -0
- package/dist/nodes/rules/divisor.js +19 -0
- package/dist/nodes/rules/props.js +130 -0
- package/dist/nodes/rules/range.js +108 -0
- package/dist/nodes/rules/regex.js +17 -0
- package/dist/nodes/rules/rules.js +102 -0
- package/dist/parse/ast/ast.js +2 -0
- package/dist/{types/parse → parse}/ast/bound.d.ts +4 -1
- package/dist/parse/ast/bound.js +7 -0
- package/dist/parse/ast/config.js +14 -0
- package/dist/parse/ast/distributableFunction.js +40 -0
- package/dist/parse/ast/divisor.js +5 -0
- package/dist/parse/ast/intersection.js +21 -0
- package/dist/parse/ast/keyof.js +107 -0
- package/dist/parse/ast/morph.js +50 -0
- package/dist/parse/ast/narrow.js +17 -0
- package/dist/{types/parse → parse}/ast/node.d.ts +3 -1
- package/dist/parse/ast/node.js +2 -0
- package/dist/parse/ast/tuple.js +72 -0
- package/dist/parse/ast/union.js +5 -0
- package/dist/parse/ast/utils.js +2 -0
- package/dist/parse/definition.js +47 -0
- package/dist/parse/record.js +31 -0
- package/dist/parse/string/reduce/dynamic.js +159 -0
- package/dist/parse/string/reduce/shared.js +13 -0
- package/dist/parse/string/reduce/static.js +2 -0
- package/dist/parse/string/shift/operand/enclosed.js +37 -0
- package/dist/parse/string/shift/operand/operand.js +15 -0
- package/dist/parse/string/shift/operand/unenclosed.js +45 -0
- package/dist/parse/string/shift/operator/bounds.js +83 -0
- package/dist/parse/string/shift/operator/divisor.js +23 -0
- package/dist/parse/string/shift/operator/operator.js +34 -0
- package/dist/parse/string/shift/scanner.js +97 -0
- package/dist/parse/string/string.js +35 -0
- package/dist/scopes/ark.js +27 -0
- package/dist/scopes/cache.js +28 -0
- package/dist/{mjs/scopes → scopes}/expressions.js +20 -9
- package/dist/scopes/jsObjects.js +20 -0
- package/dist/scopes/scope.js +184 -0
- package/dist/scopes/tsKeywords.js +49 -0
- package/dist/scopes/type.js +40 -0
- package/dist/{mjs/scopes → scopes}/validation/creditCard.js +15 -11
- package/dist/{mjs/scopes → scopes}/validation/date.js +21 -16
- package/dist/scopes/validation/validation.js +74 -0
- package/dist/traverse/problems.js +234 -0
- package/dist/traverse/traverse.js +330 -0
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/{mjs/utils → utils}/chainableNoOpProxy.js +6 -2
- package/dist/utils/data.js +39 -0
- package/dist/utils/domains.js +27 -0
- package/dist/utils/errors.js +17 -0
- package/dist/utils/freeze.js +16 -0
- package/dist/utils/generics.js +53 -0
- package/dist/utils/numericLiterals.js +88 -0
- package/dist/utils/objectKinds.js +69 -0
- package/dist/{mjs/utils → utils}/paths.js +8 -3
- package/dist/utils/serialize.js +71 -0
- package/dist/utils/unionToTuple.js +2 -0
- package/package.json +25 -39
- package/dist/cjs/main.js +0 -38
- package/dist/cjs/nodes/branch.js +0 -140
- package/dist/cjs/nodes/compose.js +0 -157
- package/dist/cjs/nodes/node.js +0 -139
- package/dist/cjs/nodes/predicate.js +0 -86
- package/dist/cjs/nodes/rules/class.js +0 -25
- package/dist/cjs/nodes/rules/collapsibleSet.js +0 -48
- package/dist/cjs/nodes/rules/divisor.js +0 -29
- package/dist/cjs/nodes/rules/props.js +0 -160
- package/dist/cjs/nodes/rules/range.js +0 -105
- package/dist/cjs/nodes/rules/regex.js +0 -26
- package/dist/cjs/nodes/rules/rules.js +0 -123
- package/dist/cjs/package.json +0 -3
- package/dist/cjs/parse/ast/ast.js +0 -4
- package/dist/cjs/parse/ast/bound.js +0 -9
- package/dist/cjs/parse/ast/config.js +0 -13
- package/dist/cjs/parse/ast/distributableFunction.js +0 -48
- package/dist/cjs/parse/ast/divisor.js +0 -9
- package/dist/cjs/parse/ast/intersection.js +0 -30
- package/dist/cjs/parse/ast/keyof.js +0 -111
- package/dist/cjs/parse/ast/morph.js +0 -58
- package/dist/cjs/parse/ast/narrow.js +0 -22
- package/dist/cjs/parse/ast/node.js +0 -4
- package/dist/cjs/parse/ast/tuple.js +0 -94
- package/dist/cjs/parse/ast/union.js +0 -9
- package/dist/cjs/parse/ast/utils.js +0 -4
- package/dist/cjs/parse/definition.js +0 -61
- package/dist/cjs/parse/record.js +0 -37
- package/dist/cjs/parse/string/reduce/dynamic.js +0 -167
- package/dist/cjs/parse/string/reduce/shared.js +0 -23
- package/dist/cjs/parse/string/reduce/static.js +0 -4
- package/dist/cjs/parse/string/shift/operand/enclosed.js +0 -54
- package/dist/cjs/parse/string/shift/operand/operand.js +0 -11
- package/dist/cjs/parse/string/shift/operand/unenclosed.js +0 -53
- package/dist/cjs/parse/string/shift/operator/bounds.js +0 -87
- package/dist/cjs/parse/string/shift/operator/divisor.js +0 -35
- package/dist/cjs/parse/string/shift/operator/operator.js +0 -26
- package/dist/cjs/parse/string/shift/scanner.js +0 -114
- package/dist/cjs/parse/string/string.js +0 -43
- package/dist/cjs/scopes/ark.js +0 -45
- package/dist/cjs/scopes/cache.js +0 -52
- package/dist/cjs/scopes/expressions.js +0 -29
- package/dist/cjs/scopes/jsObjects.js +0 -93
- package/dist/cjs/scopes/scope.js +0 -323
- package/dist/cjs/scopes/tsKeywords.js +0 -113
- package/dist/cjs/scopes/type.js +0 -50
- package/dist/cjs/scopes/validation/creditCard.js +0 -47
- package/dist/cjs/scopes/validation/date.js +0 -62
- package/dist/cjs/scopes/validation/validation.js +0 -104
- package/dist/cjs/traverse/problems.js +0 -281
- package/dist/cjs/traverse/traverse.js +0 -387
- package/dist/cjs/utils/chainableNoOpProxy.js +0 -23
- package/dist/cjs/utils/data.js +0 -53
- package/dist/cjs/utils/domains.js +0 -30
- package/dist/cjs/utils/errors.js +0 -26
- package/dist/cjs/utils/freeze.js +0 -15
- package/dist/cjs/utils/generics.js +0 -63
- package/dist/cjs/utils/numericLiterals.js +0 -75
- package/dist/cjs/utils/objectKinds.js +0 -69
- package/dist/cjs/utils/paths.js +0 -32
- package/dist/cjs/utils/serialize.js +0 -74
- package/dist/cjs/utils/unionToTuple.js +0 -4
- package/dist/mjs/main.js +0 -8
- package/dist/mjs/nodes/branch.js +0 -123
- package/dist/mjs/nodes/compose.js +0 -135
- package/dist/mjs/nodes/discriminate.js +0 -247
- package/dist/mjs/nodes/node.js +0 -117
- package/dist/mjs/nodes/predicate.js +0 -69
- package/dist/mjs/nodes/rules/class.js +0 -11
- package/dist/mjs/nodes/rules/collapsibleSet.js +0 -34
- package/dist/mjs/nodes/rules/divisor.js +0 -15
- package/dist/mjs/nodes/rules/props.js +0 -142
- package/dist/mjs/nodes/rules/range.js +0 -86
- package/dist/mjs/nodes/rules/regex.js +0 -11
- package/dist/mjs/nodes/rules/rules.js +0 -106
- package/dist/mjs/package.json +0 -3
- package/dist/mjs/parse/ast/ast.js +0 -1
- package/dist/mjs/parse/ast/bound.js +0 -1
- package/dist/mjs/parse/ast/config.js +0 -9
- package/dist/mjs/parse/ast/distributableFunction.js +0 -34
- package/dist/mjs/parse/ast/divisor.js +0 -1
- package/dist/mjs/parse/ast/intersection.js +0 -16
- package/dist/mjs/parse/ast/keyof.js +0 -103
- package/dist/mjs/parse/ast/morph.js +0 -44
- package/dist/mjs/parse/ast/narrow.js +0 -14
- package/dist/mjs/parse/ast/node.js +0 -1
- package/dist/mjs/parse/ast/tuple.js +0 -80
- package/dist/mjs/parse/ast/union.js +0 -1
- package/dist/mjs/parse/ast/utils.js +0 -1
- package/dist/mjs/parse/definition.js +0 -46
- package/dist/mjs/parse/record.js +0 -29
- package/dist/mjs/parse/string/reduce/dynamic.js +0 -159
- package/dist/mjs/parse/string/reduce/shared.js +0 -6
- package/dist/mjs/parse/string/reduce/static.js +0 -1
- package/dist/mjs/parse/string/shift/operand/enclosed.js +0 -39
- package/dist/mjs/parse/string/shift/operand/operand.js +0 -3
- package/dist/mjs/parse/string/shift/operand/unenclosed.js +0 -36
- package/dist/mjs/parse/string/shift/operator/bounds.js +0 -70
- package/dist/mjs/parse/string/shift/operator/divisor.js +0 -21
- package/dist/mjs/parse/string/shift/operator/operator.js +0 -13
- package/dist/mjs/parse/string/shift/scanner.js +0 -106
- package/dist/mjs/parse/string/string.js +0 -28
- package/dist/mjs/scopes/ark.js +0 -28
- package/dist/mjs/scopes/cache.js +0 -38
- package/dist/mjs/scopes/jsObjects.js +0 -82
- package/dist/mjs/scopes/scope.js +0 -303
- package/dist/mjs/scopes/tsKeywords.js +0 -116
- package/dist/mjs/scopes/type.js +0 -35
- package/dist/mjs/scopes/validation/validation.js +0 -108
- package/dist/mjs/traverse/problems.js +0 -260
- package/dist/mjs/traverse/traverse.js +0 -370
- package/dist/mjs/utils/data.js +0 -38
- package/dist/mjs/utils/domains.js +0 -15
- package/dist/mjs/utils/errors.js +0 -10
- package/dist/mjs/utils/freeze.js +0 -7
- package/dist/mjs/utils/generics.js +0 -38
- package/dist/mjs/utils/numericLiterals.js +0 -72
- package/dist/mjs/utils/objectKinds.js +0 -55
- package/dist/mjs/utils/serialize.js +0 -58
- package/dist/mjs/utils/unionToTuple.js +0 -1
- /package/dist/{types/main.d.ts → main.d.ts} +0 -0
- /package/dist/{types/nodes → nodes}/branch.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/compose.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/discriminate.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/node.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/predicate.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/rules/class.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/rules/collapsibleSet.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/rules/divisor.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/rules/props.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/rules/range.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/rules/regex.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/rules/rules.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/ast.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/config.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/distributableFunction.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/divisor.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/intersection.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/keyof.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/morph.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/narrow.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/tuple.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/union.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/utils.d.ts +0 -0
- /package/dist/{types/parse → parse}/definition.d.ts +0 -0
- /package/dist/{types/parse → parse}/record.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/reduce/dynamic.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/reduce/shared.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/reduce/static.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/shift/operand/enclosed.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/shift/operand/operand.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/shift/operand/unenclosed.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/shift/operator/bounds.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/shift/operator/divisor.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/shift/operator/operator.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/shift/scanner.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/string.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/ark.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/cache.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/expressions.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/jsObjects.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/scope.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/tsKeywords.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/type.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/validation/creditCard.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/validation/date.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/validation/validation.d.ts +0 -0
- /package/dist/{types/traverse → traverse}/problems.d.ts +0 -0
- /package/dist/{types/traverse → traverse}/traverse.d.ts +0 -0
- /package/dist/{types/utils → utils}/chainableNoOpProxy.d.ts +0 -0
- /package/dist/{types/utils → utils}/data.d.ts +0 -0
- /package/dist/{types/utils → utils}/domains.d.ts +0 -0
- /package/dist/{types/utils → utils}/errors.d.ts +0 -0
- /package/dist/{types/utils → utils}/freeze.d.ts +0 -0
- /package/dist/{types/utils → utils}/generics.d.ts +0 -0
- /package/dist/{types/utils → utils}/numericLiterals.d.ts +0 -0
- /package/dist/{types/utils → utils}/objectKinds.d.ts +0 -0
- /package/dist/{types/utils → utils}/paths.d.ts +0 -0
- /package/dist/{types/utils → utils}/serialize.d.ts +0 -0
- /package/dist/{types/utils → utils}/unionToTuple.d.ts +0 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.flattenProps = exports.propsIntersection = exports.propToNode = exports.mappedKeys = exports.isPrerequisite = exports.isOptional = void 0;
|
|
4
|
+
const compose_js_1 = require("../compose.js");
|
|
5
|
+
const node_js_1 = require("../node.js");
|
|
6
|
+
const isOptional = (prop) => prop[0] === "?";
|
|
7
|
+
exports.isOptional = isOptional;
|
|
8
|
+
const isPrerequisite = (prop) => prop[0] === "!";
|
|
9
|
+
exports.isPrerequisite = isPrerequisite;
|
|
10
|
+
exports.mappedKeys = {
|
|
11
|
+
index: "[index]"
|
|
12
|
+
};
|
|
13
|
+
const propToNode = (prop) => (0, exports.isOptional)(prop) || (0, exports.isPrerequisite)(prop) ? prop[1] : prop;
|
|
14
|
+
exports.propToNode = propToNode;
|
|
15
|
+
const getTupleLengthIfPresent = (result) => {
|
|
16
|
+
if (typeof result.length === "object" &&
|
|
17
|
+
(0, exports.isPrerequisite)(result.length) &&
|
|
18
|
+
typeof result.length[1] !== "string" &&
|
|
19
|
+
(0, node_js_1.isLiteralNode)(result.length[1], "number")) {
|
|
20
|
+
return result.length[1].number.value;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
exports.propsIntersection = (0, compose_js_1.composeIntersection)((l, r, state) => {
|
|
24
|
+
const result = propKeysIntersection(l, r, state);
|
|
25
|
+
if (typeof result === "symbol") {
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
const lengthValue = getTupleLengthIfPresent(result);
|
|
29
|
+
if (lengthValue === undefined || !(exports.mappedKeys.index in result)) {
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
// if we are at this point, we have an array with an exact length (i.e.
|
|
33
|
+
// a tuple) and an index signature. Intersection each tuple item with
|
|
34
|
+
// the index signature node and remove the index signature via a new
|
|
35
|
+
// updated result, copied from result to avoid mutating existing references.
|
|
36
|
+
const { [exports.mappedKeys.index]: indexProp, ...updatedResult } = result;
|
|
37
|
+
const indexNode = (0, exports.propToNode)(indexProp);
|
|
38
|
+
for (let i = 0; i < lengthValue; i++) {
|
|
39
|
+
if (!updatedResult[i]) {
|
|
40
|
+
updatedResult[i] = indexNode;
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
const existingNodeAtIndex = (0, exports.propToNode)(updatedResult[i]);
|
|
44
|
+
state.path.push(`${i}`);
|
|
45
|
+
const updatedResultAtIndex = (0, node_js_1.nodeIntersection)(existingNodeAtIndex, indexNode, state);
|
|
46
|
+
state.path.pop();
|
|
47
|
+
if ((0, compose_js_1.isDisjoint)(updatedResultAtIndex)) {
|
|
48
|
+
return updatedResultAtIndex;
|
|
49
|
+
}
|
|
50
|
+
else if (!(0, compose_js_1.isEquality)(updatedResultAtIndex) &&
|
|
51
|
+
updatedResultAtIndex !== existingNodeAtIndex) {
|
|
52
|
+
updatedResult[i] = updatedResultAtIndex;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return updatedResult;
|
|
56
|
+
});
|
|
57
|
+
const propKeysIntersection = (0, compose_js_1.composeKeyedIntersection)((propKey, l, r, context) => {
|
|
58
|
+
if (l === undefined) {
|
|
59
|
+
return r === undefined ? (0, compose_js_1.equality)() : r;
|
|
60
|
+
}
|
|
61
|
+
if (r === undefined) {
|
|
62
|
+
return l;
|
|
63
|
+
}
|
|
64
|
+
context.path.push(propKey);
|
|
65
|
+
const result = (0, node_js_1.nodeIntersection)((0, exports.propToNode)(l), (0, exports.propToNode)(r), context);
|
|
66
|
+
context.path.pop();
|
|
67
|
+
const resultIsOptional = (0, exports.isOptional)(l) && (0, exports.isOptional)(r);
|
|
68
|
+
if ((0, compose_js_1.isDisjoint)(result) && resultIsOptional) {
|
|
69
|
+
// If an optional key has an empty intersection, the type can
|
|
70
|
+
// still be satisfied as long as the key is not included. Set
|
|
71
|
+
// the node to never rather than invalidating the type.
|
|
72
|
+
return {};
|
|
73
|
+
}
|
|
74
|
+
return result;
|
|
75
|
+
}, { onEmpty: "bubble" });
|
|
76
|
+
const flattenProps = (entries, props, ctx) => {
|
|
77
|
+
const keyConfig = ctx.type.config?.keys ?? ctx.type.scope.config.keys;
|
|
78
|
+
return keyConfig === "loose"
|
|
79
|
+
? flattenLooseProps(entries, props, ctx)
|
|
80
|
+
: flattenPropsRecord(keyConfig, entries, props, ctx);
|
|
81
|
+
};
|
|
82
|
+
exports.flattenProps = flattenProps;
|
|
83
|
+
const flattenLooseProps = (entries, props, ctx) => {
|
|
84
|
+
// if we don't care about extraneous keys, flatten props so we can iterate over the definitions directly
|
|
85
|
+
for (const k in props) {
|
|
86
|
+
const prop = props[k];
|
|
87
|
+
ctx.path.push(k);
|
|
88
|
+
if (k === exports.mappedKeys.index) {
|
|
89
|
+
entries.push(["indexProp", (0, node_js_1.flattenNode)((0, exports.propToNode)(prop), ctx)]);
|
|
90
|
+
}
|
|
91
|
+
else if ((0, exports.isOptional)(prop)) {
|
|
92
|
+
entries.push(["optionalProp", [k, (0, node_js_1.flattenNode)(prop[1], ctx)]]);
|
|
93
|
+
}
|
|
94
|
+
else if ((0, exports.isPrerequisite)(prop)) {
|
|
95
|
+
entries.push(["prerequisiteProp", [k, (0, node_js_1.flattenNode)(prop[1], ctx)]]);
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
entries.push(["requiredProp", [k, (0, node_js_1.flattenNode)(prop, ctx)]]);
|
|
99
|
+
}
|
|
100
|
+
ctx.path.pop();
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
const flattenPropsRecord = (kind, entries, props, ctx) => {
|
|
104
|
+
const result = {
|
|
105
|
+
required: {},
|
|
106
|
+
optional: {}
|
|
107
|
+
};
|
|
108
|
+
// if we need to keep track of extraneous keys, either to add problems or
|
|
109
|
+
// remove them, store the props as a Record to optimize for presence
|
|
110
|
+
// checking as we iterate over the data
|
|
111
|
+
for (const k in props) {
|
|
112
|
+
const prop = props[k];
|
|
113
|
+
ctx.path.push(k);
|
|
114
|
+
if (k === exports.mappedKeys.index) {
|
|
115
|
+
result.index = (0, node_js_1.flattenNode)((0, exports.propToNode)(prop), ctx);
|
|
116
|
+
}
|
|
117
|
+
else if ((0, exports.isOptional)(prop)) {
|
|
118
|
+
result.optional[k] = (0, node_js_1.flattenNode)(prop[1], ctx);
|
|
119
|
+
}
|
|
120
|
+
else if ((0, exports.isPrerequisite)(prop)) {
|
|
121
|
+
// we still have to push prerequisite props as normal entries so they can be checked first
|
|
122
|
+
entries.push(["prerequisiteProp", [k, (0, node_js_1.flattenNode)(prop[1], ctx)]]);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
result.required[k] = (0, node_js_1.flattenNode)(prop, ctx);
|
|
126
|
+
}
|
|
127
|
+
ctx.path.pop();
|
|
128
|
+
}
|
|
129
|
+
entries.push([`${kind}Props`, result]);
|
|
130
|
+
};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.compareStrictness = exports.checkBound = exports.flattenRange = exports.rangeIntersection = exports.isEqualityRange = exports.maxComparators = exports.minComparators = void 0;
|
|
4
|
+
const data_js_1 = require("../../utils/data.js");
|
|
5
|
+
const compose_js_1 = require("../compose.js");
|
|
6
|
+
exports.minComparators = {
|
|
7
|
+
">": true,
|
|
8
|
+
">=": true
|
|
9
|
+
};
|
|
10
|
+
exports.maxComparators = {
|
|
11
|
+
"<": true,
|
|
12
|
+
"<=": true
|
|
13
|
+
};
|
|
14
|
+
const isEqualityRange = (range) => "comparator" in range;
|
|
15
|
+
exports.isEqualityRange = isEqualityRange;
|
|
16
|
+
exports.rangeIntersection = (0, compose_js_1.composeIntersection)((l, r, state) => {
|
|
17
|
+
if ((0, exports.isEqualityRange)(l)) {
|
|
18
|
+
if ((0, exports.isEqualityRange)(r)) {
|
|
19
|
+
return l.limit === r.limit
|
|
20
|
+
? (0, compose_js_1.equality)()
|
|
21
|
+
: state.addDisjoint("range", l, r);
|
|
22
|
+
}
|
|
23
|
+
return rangeAllows(r, l.limit) ? l : state.addDisjoint("range", l, r);
|
|
24
|
+
}
|
|
25
|
+
if ((0, exports.isEqualityRange)(r)) {
|
|
26
|
+
return rangeAllows(l, r.limit) ? r : state.addDisjoint("range", l, r);
|
|
27
|
+
}
|
|
28
|
+
const stricterMin = (0, exports.compareStrictness)("min", l.min, r.min);
|
|
29
|
+
const stricterMax = (0, exports.compareStrictness)("max", l.max, r.max);
|
|
30
|
+
if (stricterMin === "l") {
|
|
31
|
+
if (stricterMax === "r") {
|
|
32
|
+
return (0, exports.compareStrictness)("min", l.min, r.max) === "l"
|
|
33
|
+
? state.addDisjoint("range", l, r)
|
|
34
|
+
: {
|
|
35
|
+
min: l.min,
|
|
36
|
+
max: r.max
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
return l;
|
|
40
|
+
}
|
|
41
|
+
if (stricterMin === "r") {
|
|
42
|
+
if (stricterMax === "l") {
|
|
43
|
+
return (0, exports.compareStrictness)("max", l.max, r.min) === "l"
|
|
44
|
+
? state.addDisjoint("range", l, r)
|
|
45
|
+
: {
|
|
46
|
+
min: r.min,
|
|
47
|
+
max: l.max
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
return r;
|
|
51
|
+
}
|
|
52
|
+
return stricterMax === "l" ? l : stricterMax === "r" ? r : (0, compose_js_1.equality)();
|
|
53
|
+
});
|
|
54
|
+
const rangeAllows = (range, n) => (0, exports.isEqualityRange)(range)
|
|
55
|
+
? n === range.limit
|
|
56
|
+
: minAllows(range.min, n) && maxAllows(range.max, n);
|
|
57
|
+
const minAllows = (min, n) => !min || n > min.limit || (n === min.limit && !isExclusive(min.comparator));
|
|
58
|
+
const maxAllows = (max, n) => !max || n < max.limit || (n === max.limit && !isExclusive(max.comparator));
|
|
59
|
+
const flattenRange = (entries, range, ctx) => {
|
|
60
|
+
const units = ctx.lastDomain === "string"
|
|
61
|
+
? "characters"
|
|
62
|
+
: ctx.lastDomain === "object"
|
|
63
|
+
? "items long"
|
|
64
|
+
: undefined;
|
|
65
|
+
if ((0, exports.isEqualityRange)(range)) {
|
|
66
|
+
return entries.push(["bound", units ? { ...range, units } : range]);
|
|
67
|
+
}
|
|
68
|
+
if (range.min) {
|
|
69
|
+
entries.push(["bound", units ? { ...range.min, units } : range.min]);
|
|
70
|
+
}
|
|
71
|
+
if (range.max) {
|
|
72
|
+
entries.push(["bound", units ? { ...range.max, units } : range.max]);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
exports.flattenRange = flattenRange;
|
|
76
|
+
const checkBound = (bound, state) => comparatorCheckers[bound.comparator]((0, data_js_1.sizeOf)(state.data), bound.limit) ||
|
|
77
|
+
!state.problems.add("bound", bound);
|
|
78
|
+
exports.checkBound = checkBound;
|
|
79
|
+
const comparatorCheckers = {
|
|
80
|
+
"<": (size, limit) => size < limit,
|
|
81
|
+
">": (size, limit) => size > limit,
|
|
82
|
+
"<=": (size, limit) => size <= limit,
|
|
83
|
+
">=": (size, limit) => size >= limit,
|
|
84
|
+
"==": (size, limit) => size === limit
|
|
85
|
+
};
|
|
86
|
+
const compareStrictness = (kind, l, r) => !l
|
|
87
|
+
? !r
|
|
88
|
+
? "="
|
|
89
|
+
: "r"
|
|
90
|
+
: !r
|
|
91
|
+
? "l"
|
|
92
|
+
: l.limit === r.limit
|
|
93
|
+
? isExclusive(l.comparator)
|
|
94
|
+
? isExclusive(r.comparator)
|
|
95
|
+
? "="
|
|
96
|
+
: "l"
|
|
97
|
+
: isExclusive(r.comparator)
|
|
98
|
+
? "r"
|
|
99
|
+
: "="
|
|
100
|
+
: kind === "min"
|
|
101
|
+
? l.limit > r.limit
|
|
102
|
+
? "l"
|
|
103
|
+
: "r"
|
|
104
|
+
: l.limit < r.limit
|
|
105
|
+
? "l"
|
|
106
|
+
: "r";
|
|
107
|
+
exports.compareStrictness = compareStrictness;
|
|
108
|
+
const isExclusive = (comparator) => comparator.length === 1;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.regexIntersection = exports.checkRegex = exports.getRegex = void 0;
|
|
4
|
+
const compose_js_1 = require("../compose.js");
|
|
5
|
+
const collapsibleSet_js_1 = require("./collapsibleSet.js");
|
|
6
|
+
const regexCache = {};
|
|
7
|
+
const getRegex = (source) => {
|
|
8
|
+
if (!regexCache[source]) {
|
|
9
|
+
regexCache[source] = new RegExp(source);
|
|
10
|
+
}
|
|
11
|
+
return regexCache[source];
|
|
12
|
+
};
|
|
13
|
+
exports.getRegex = getRegex;
|
|
14
|
+
const checkRegex = (source, state) => (0, exports.getRegex)(source).test(state.data) ||
|
|
15
|
+
!state.problems.add("regex", `/${source}/`);
|
|
16
|
+
exports.checkRegex = checkRegex;
|
|
17
|
+
exports.regexIntersection = (0, compose_js_1.composeIntersection)((collapsibleSet_js_1.collapsibleListUnion));
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.literalSatisfiesRules = exports.precedenceMap = exports.flattenRules = exports.narrowableRulesIntersection = exports.rulesIntersection = void 0;
|
|
4
|
+
const generics_js_1 = require("../../utils/generics.js");
|
|
5
|
+
const compose_js_1 = require("../compose.js");
|
|
6
|
+
const class_js_1 = require("./class.js");
|
|
7
|
+
const collapsibleSet_js_1 = require("./collapsibleSet.js");
|
|
8
|
+
const divisor_js_1 = require("./divisor.js");
|
|
9
|
+
const props_js_1 = require("./props.js");
|
|
10
|
+
const range_js_1 = require("./range.js");
|
|
11
|
+
const regex_js_1 = require("./regex.js");
|
|
12
|
+
const rulesIntersection = (l, r, state) => "value" in l
|
|
13
|
+
? "value" in r
|
|
14
|
+
? l.value === r.value
|
|
15
|
+
? (0, compose_js_1.equality)()
|
|
16
|
+
: state.addDisjoint("value", l.value, r.value)
|
|
17
|
+
: (0, exports.literalSatisfiesRules)(l.value, r, state)
|
|
18
|
+
? l
|
|
19
|
+
: state.addDisjoint("leftAssignability", l, r)
|
|
20
|
+
: "value" in r
|
|
21
|
+
? (0, exports.literalSatisfiesRules)(r.value, l, state)
|
|
22
|
+
? r
|
|
23
|
+
: state.addDisjoint("rightAssignability", l, r)
|
|
24
|
+
: (0, exports.narrowableRulesIntersection)(l, r, state);
|
|
25
|
+
exports.rulesIntersection = rulesIntersection;
|
|
26
|
+
const narrowIntersection = (0, compose_js_1.composeIntersection)(collapsibleSet_js_1.collapsibleListUnion);
|
|
27
|
+
exports.narrowableRulesIntersection = (0, compose_js_1.composeKeyedIntersection)({
|
|
28
|
+
divisor: divisor_js_1.divisorIntersection,
|
|
29
|
+
regex: regex_js_1.regexIntersection,
|
|
30
|
+
props: props_js_1.propsIntersection,
|
|
31
|
+
class: class_js_1.classIntersection,
|
|
32
|
+
range: range_js_1.rangeIntersection,
|
|
33
|
+
narrow: narrowIntersection
|
|
34
|
+
}, { onEmpty: "bubble" });
|
|
35
|
+
const flattenRules = (rules, ctx) => {
|
|
36
|
+
const entries = [];
|
|
37
|
+
let k;
|
|
38
|
+
for (k in rules) {
|
|
39
|
+
ruleFlatteners[k](entries, rules[k], ctx);
|
|
40
|
+
}
|
|
41
|
+
// Some entries with the same precedence, e.g. morphs flattened from a list,
|
|
42
|
+
// rely on the fact that JS's builtin sort is stable to behave as expected
|
|
43
|
+
// when traversed:
|
|
44
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
|
|
45
|
+
return entries.sort((l, r) => exports.precedenceMap[l[0]] - exports.precedenceMap[r[0]]);
|
|
46
|
+
};
|
|
47
|
+
exports.flattenRules = flattenRules;
|
|
48
|
+
const ruleFlatteners = {
|
|
49
|
+
regex: (entries, rule) => {
|
|
50
|
+
for (const source of (0, generics_js_1.listFrom)(rule)) {
|
|
51
|
+
entries.push(["regex", source]);
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
divisor: (entries, rule) => {
|
|
55
|
+
entries.push(["divisor", rule]);
|
|
56
|
+
},
|
|
57
|
+
range: range_js_1.flattenRange,
|
|
58
|
+
class: (entries, rule) => {
|
|
59
|
+
entries.push(["class", rule]);
|
|
60
|
+
},
|
|
61
|
+
props: props_js_1.flattenProps,
|
|
62
|
+
narrow: (entries, rule) => {
|
|
63
|
+
for (const narrow of (0, generics_js_1.listFrom)(rule)) {
|
|
64
|
+
entries.push(["narrow", narrow]);
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
value: (entries, rule) => {
|
|
68
|
+
entries.push(["value", rule]);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
exports.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
|
+
const literalSatisfiesRules = (data, rules, state) => !state.type.scope.type(["node", { [state.domain]: rules }])(data).problems;
|
|
102
|
+
exports.literalSatisfiesRules = literalSatisfiesRules;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { SizedData } from "../../utils/data.js";
|
|
2
2
|
import type { error, isAny } from "../../utils/generics.js";
|
|
3
3
|
import type { NumberLiteral } from "../../utils/numericLiterals.js";
|
|
4
|
+
import type { Scanner } from "../string/shift/scanner.js";
|
|
4
5
|
import type { inferAst, validateAst } from "./ast.js";
|
|
5
6
|
import type { astToString } from "./utils.js";
|
|
6
7
|
/**
|
|
@@ -17,7 +18,9 @@ import type { astToString } from "./utils.js";
|
|
|
17
18
|
* @example string
|
|
18
19
|
* const bound = type("string[]==5")
|
|
19
20
|
*/
|
|
20
|
-
export type validateBound<l, r, $> = l extends NumberLiteral ? validateAst<r, $> : isBoundable<inferAst<l, $>> extends true ? validateAst<l, $> : error<writeUnboundableMessage<astToString<l>>>;
|
|
21
|
+
export type validateBound<l, r, $> = l extends NumberLiteral ? validateAst<r, $> : l extends [infer leftAst, Scanner.Comparator, unknown] ? error<writeDoubleRightBoundMessage<astToString<leftAst>>> : isBoundable<inferAst<l, $>> extends true ? validateAst<l, $> : error<writeUnboundableMessage<astToString<l>>>;
|
|
22
|
+
export declare const writeDoubleRightBoundMessage: <root extends string>(root: root) => `Expression ${root} must have at most one right bound`;
|
|
23
|
+
type writeDoubleRightBoundMessage<root extends string> = `Expression ${root} must have at most one right bound`;
|
|
21
24
|
type isBoundable<data> = isAny<data> extends true ? false : [data] extends [SizedData] ? true : false;
|
|
22
25
|
export declare const writeUnboundableMessage: <root extends string>(root: root) => `Bounded expression ${root} must be a number, string or array`;
|
|
23
26
|
type writeUnboundableMessage<root extends string> = `Bounded expression ${root} must be a number, string or array`;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.writeUnboundableMessage = exports.writeDoubleRightBoundMessage = void 0;
|
|
4
|
+
const writeDoubleRightBoundMessage = (root) => `Expression ${root} must have at most one right bound`;
|
|
5
|
+
exports.writeDoubleRightBoundMessage = writeDoubleRightBoundMessage;
|
|
6
|
+
const writeUnboundableMessage = (root) => `Bounded expression ${root} must be a number, string or array`;
|
|
7
|
+
exports.writeUnboundableMessage = writeUnboundableMessage;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseConfigTuple = void 0;
|
|
4
|
+
const definition_js_1 = require("../definition.js");
|
|
5
|
+
/**
|
|
6
|
+
* @operator {@link parseConfigTuple | :}
|
|
7
|
+
* @docgenTable
|
|
8
|
+
* @tuple ["type", ":", config]
|
|
9
|
+
*/
|
|
10
|
+
const parseConfigTuple = (def, ctx) => ({
|
|
11
|
+
node: ctx.type.scope.resolveTypeNode((0, definition_js_1.parseDefinition)(def[0], ctx)),
|
|
12
|
+
config: def[2]
|
|
13
|
+
});
|
|
14
|
+
exports.parseConfigTuple = parseConfigTuple;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.distributeFunctionToNode = exports.writeMalformedDistributableFunctionMessage = void 0;
|
|
4
|
+
const domains_js_1 = require("../../utils/domains.js");
|
|
5
|
+
const errors_js_1 = require("../../utils/errors.js");
|
|
6
|
+
const generics_js_1 = require("../../utils/generics.js");
|
|
7
|
+
const serialize_js_1 = require("../../utils/serialize.js");
|
|
8
|
+
const writeMalformedDistributableFunctionMessage = (def) => `Expected a Function or Record<Domain, Function> operand (${(0, serialize_js_1.stringify)(def)} was invalid)`;
|
|
9
|
+
exports.writeMalformedDistributableFunctionMessage = writeMalformedDistributableFunctionMessage;
|
|
10
|
+
const distributeFunctionToNode = (distributableFunction, node, ctx, ruleKey) => {
|
|
11
|
+
const domains = (0, generics_js_1.objectKeysOf)(node);
|
|
12
|
+
if (!(0, domains_js_1.hasDomain)(distributableFunction, "object")) {
|
|
13
|
+
return (0, errors_js_1.throwParseError)((0, exports.writeMalformedDistributableFunctionMessage)(distributableFunction));
|
|
14
|
+
}
|
|
15
|
+
const distributed = {};
|
|
16
|
+
if (typeof distributableFunction === "function") {
|
|
17
|
+
const domainFunction = {
|
|
18
|
+
[ruleKey]: distributableFunction
|
|
19
|
+
};
|
|
20
|
+
for (const domain of domains) {
|
|
21
|
+
distributed[domain] = domainFunction;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
for (const domain of domains) {
|
|
26
|
+
if (distributableFunction[domain] === undefined) {
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
const functionInDomain = {
|
|
30
|
+
[ruleKey]: distributableFunction[domain]
|
|
31
|
+
};
|
|
32
|
+
if (typeof functionInDomain[ruleKey] !== "function") {
|
|
33
|
+
return (0, errors_js_1.throwParseError)((0, exports.writeMalformedDistributableFunctionMessage)(functionInDomain));
|
|
34
|
+
}
|
|
35
|
+
distributed[domain] = functionInDomain;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return distributed;
|
|
39
|
+
};
|
|
40
|
+
exports.distributeFunctionToNode = distributeFunctionToNode;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.writeIndivisibleMessage = void 0;
|
|
4
|
+
const writeIndivisibleMessage = (root) => `Divisibility operand ${root} must be a number`;
|
|
5
|
+
exports.writeIndivisibleMessage = writeIndivisibleMessage;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.writeImplicitNeverMessage = exports.compileDisjointReasonsMessage = void 0;
|
|
4
|
+
const compose_js_1 = require("../../nodes/compose.js");
|
|
5
|
+
const generics_js_1 = require("../../utils/generics.js");
|
|
6
|
+
const compileDisjointReasonsMessage = (disjoints) => {
|
|
7
|
+
const paths = (0, generics_js_1.objectKeysOf)(disjoints);
|
|
8
|
+
if (paths.length === 1) {
|
|
9
|
+
const path = paths[0];
|
|
10
|
+
return `${path === "/" ? "" : `At ${path}: `}Intersection of ${compose_js_1.disjointDescriptionWriters[disjoints[path].kind](disjoints[path])} results in an unsatisfiable type`;
|
|
11
|
+
}
|
|
12
|
+
let message = `
|
|
13
|
+
"Intersection results in unsatisfiable types at the following paths:\n`;
|
|
14
|
+
for (const path in disjoints) {
|
|
15
|
+
message += ` ${path}: ${compose_js_1.disjointDescriptionWriters[disjoints[path].kind](disjoints[path])}\n`;
|
|
16
|
+
}
|
|
17
|
+
return message;
|
|
18
|
+
};
|
|
19
|
+
exports.compileDisjointReasonsMessage = compileDisjointReasonsMessage;
|
|
20
|
+
const writeImplicitNeverMessage = (path, operator, description) => `${path.length ? `At ${path}: ` : ""}${operator} ${description ? `${description} ` : ""}results in an unsatisfiable type`;
|
|
21
|
+
exports.writeImplicitNeverMessage = writeImplicitNeverMessage;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseKeyOfTuple = void 0;
|
|
4
|
+
const props_js_1 = require("../../nodes/rules/props.js");
|
|
5
|
+
const errors_js_1 = require("../../utils/errors.js");
|
|
6
|
+
const freeze_js_1 = require("../../utils/freeze.js");
|
|
7
|
+
const generics_js_1 = require("../../utils/generics.js");
|
|
8
|
+
const numericLiterals_js_1 = require("../../utils/numericLiterals.js");
|
|
9
|
+
const objectKinds_js_1 = require("../../utils/objectKinds.js");
|
|
10
|
+
const serialize_js_1 = require("../../utils/serialize.js");
|
|
11
|
+
const definition_js_1 = require("../definition.js");
|
|
12
|
+
const intersection_js_1 = require("./intersection.js");
|
|
13
|
+
const arrayIndexStringBranch = (0, freeze_js_1.deepFreeze)({
|
|
14
|
+
regex: numericLiterals_js_1.wellFormedNonNegativeIntegerMatcher.source
|
|
15
|
+
});
|
|
16
|
+
const arrayIndexNumberBranch = (0, freeze_js_1.deepFreeze)({
|
|
17
|
+
range: { min: { comparator: ">=", limit: 0 } },
|
|
18
|
+
divisor: 1
|
|
19
|
+
});
|
|
20
|
+
const parseKeyOfTuple = (def, ctx) => {
|
|
21
|
+
const resolution = ctx.type.scope.resolveNode((0, definition_js_1.parseDefinition)(def[1], ctx));
|
|
22
|
+
const predicateKeys = (0, generics_js_1.objectKeysOf)(resolution).map((domain) => keysOfPredicate(domain, resolution[domain]));
|
|
23
|
+
const sharedKeys = sharedKeysOf(predicateKeys);
|
|
24
|
+
if (!sharedKeys.length) {
|
|
25
|
+
return (0, intersection_js_1.writeImplicitNeverMessage)(ctx.path, "keyof");
|
|
26
|
+
}
|
|
27
|
+
const keyNode = {};
|
|
28
|
+
for (const key of sharedKeys) {
|
|
29
|
+
const keyType = typeof key;
|
|
30
|
+
if (keyType === "string" ||
|
|
31
|
+
keyType === "number" ||
|
|
32
|
+
keyType === "symbol") {
|
|
33
|
+
keyNode[keyType] ??= [];
|
|
34
|
+
keyNode[keyType].push({ value: key });
|
|
35
|
+
}
|
|
36
|
+
else if (key === numericLiterals_js_1.wellFormedNonNegativeIntegerMatcher) {
|
|
37
|
+
keyNode.string ??= [];
|
|
38
|
+
keyNode.string.push(arrayIndexStringBranch);
|
|
39
|
+
keyNode.number ??= [];
|
|
40
|
+
keyNode.number.push(arrayIndexNumberBranch);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
return (0, errors_js_1.throwInternalError)(`Unexpected keyof key '${(0, serialize_js_1.stringify)(key)}'`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return Object.fromEntries(Object.entries(keyNode).map(([domain, branches]) => [
|
|
47
|
+
domain,
|
|
48
|
+
branches.length === 1 ? branches[0] : branches
|
|
49
|
+
]));
|
|
50
|
+
};
|
|
51
|
+
exports.parseKeyOfTuple = parseKeyOfTuple;
|
|
52
|
+
const baseKeysByDomain = {
|
|
53
|
+
bigint: (0, generics_js_1.prototypeKeysOf)(0n),
|
|
54
|
+
boolean: (0, generics_js_1.prototypeKeysOf)(false),
|
|
55
|
+
null: [],
|
|
56
|
+
number: (0, generics_js_1.prototypeKeysOf)(0),
|
|
57
|
+
// TS doesn't include the Object prototype in keyof, so keyof object is never
|
|
58
|
+
object: [],
|
|
59
|
+
string: (0, generics_js_1.prototypeKeysOf)(""),
|
|
60
|
+
symbol: (0, generics_js_1.prototypeKeysOf)(Symbol()),
|
|
61
|
+
undefined: []
|
|
62
|
+
};
|
|
63
|
+
const keysOfPredicate = (domain, predicate) => domain !== "object" || predicate === true
|
|
64
|
+
? baseKeysByDomain[domain]
|
|
65
|
+
: sharedKeysOf((0, generics_js_1.listFrom)(predicate).map((branch) => keysOfObjectBranch(branch)));
|
|
66
|
+
const sharedKeysOf = (keyBranches) => {
|
|
67
|
+
if (!keyBranches.length) {
|
|
68
|
+
return [];
|
|
69
|
+
}
|
|
70
|
+
let sharedKeys = keyBranches[0];
|
|
71
|
+
for (let i = 1; i < keyBranches.length; i++) {
|
|
72
|
+
// we can filter directly by equality here because the RegExp we're
|
|
73
|
+
// using will always be reference equal to
|
|
74
|
+
// wellFormedNonNegativeIntegerMatcher
|
|
75
|
+
sharedKeys = sharedKeys.filter((k) => keyBranches[i].includes(k));
|
|
76
|
+
}
|
|
77
|
+
return sharedKeys;
|
|
78
|
+
};
|
|
79
|
+
const keysOfObjectBranch = (branch) => {
|
|
80
|
+
const result = [];
|
|
81
|
+
if ("props" in branch) {
|
|
82
|
+
for (const key of Object.keys(branch.props)) {
|
|
83
|
+
if (key === props_js_1.mappedKeys.index) {
|
|
84
|
+
// if any number is a valid key push this RegExp
|
|
85
|
+
result.push(numericLiterals_js_1.wellFormedNonNegativeIntegerMatcher);
|
|
86
|
+
}
|
|
87
|
+
else if (!result.includes(key)) {
|
|
88
|
+
result.push(key);
|
|
89
|
+
if (numericLiterals_js_1.wellFormedNonNegativeIntegerMatcher.test(key)) {
|
|
90
|
+
// allow numeric access to keys
|
|
91
|
+
result.push((0, numericLiterals_js_1.tryParseWellFormedInteger)(key, `Unexpectedly failed to parse an integer from key '${key}'`));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if ("class" in branch) {
|
|
97
|
+
const constructor = typeof branch.class === "string"
|
|
98
|
+
? objectKinds_js_1.defaultObjectKinds[branch.class]
|
|
99
|
+
: branch.class;
|
|
100
|
+
for (const key of (0, generics_js_1.prototypeKeysOf)(constructor.prototype)) {
|
|
101
|
+
if (!result.includes(key)) {
|
|
102
|
+
result.push(key);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return result;
|
|
107
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.writeMalformedMorphExpressionMessage = exports.parseMorphTuple = void 0;
|
|
4
|
+
const branch_js_1 = require("../../nodes/branch.js");
|
|
5
|
+
const errors_js_1 = require("../../utils/errors.js");
|
|
6
|
+
const objectKinds_js_1 = require("../../utils/objectKinds.js");
|
|
7
|
+
const serialize_js_1 = require("../../utils/serialize.js");
|
|
8
|
+
const definition_js_1 = require("../definition.js");
|
|
9
|
+
const parseMorphTuple = (def, ctx) => {
|
|
10
|
+
if (typeof def[2] !== "function") {
|
|
11
|
+
return (0, errors_js_1.throwParseError)((0, exports.writeMalformedMorphExpressionMessage)(def[2]));
|
|
12
|
+
}
|
|
13
|
+
const node = (0, definition_js_1.parseDefinition)(def[0], ctx);
|
|
14
|
+
const resolution = ctx.type.scope.resolveTypeNode(node);
|
|
15
|
+
const morph = def[2];
|
|
16
|
+
ctx.type.includesMorph = true;
|
|
17
|
+
let domain;
|
|
18
|
+
const result = {};
|
|
19
|
+
for (domain in resolution) {
|
|
20
|
+
const predicate = resolution[domain];
|
|
21
|
+
if (predicate === true) {
|
|
22
|
+
result[domain] = { rules: {}, morph };
|
|
23
|
+
}
|
|
24
|
+
else if (typeof predicate === "object") {
|
|
25
|
+
result[domain] = (0, objectKinds_js_1.isArray)(predicate)
|
|
26
|
+
? predicate.map((branch) => applyMorph(branch, morph))
|
|
27
|
+
: applyMorph(predicate, morph);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
(0, errors_js_1.throwInternalError)(`Unexpected predicate value for domain '${domain}': ${(0, serialize_js_1.stringify)(predicate)}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return result;
|
|
34
|
+
};
|
|
35
|
+
exports.parseMorphTuple = parseMorphTuple;
|
|
36
|
+
const applyMorph = (branch, morph) => (0, branch_js_1.isTransformationBranch)(branch)
|
|
37
|
+
? {
|
|
38
|
+
...branch,
|
|
39
|
+
morph: branch.morph
|
|
40
|
+
? Array.isArray(branch.morph)
|
|
41
|
+
? [...branch.morph, morph]
|
|
42
|
+
: [branch.morph, morph]
|
|
43
|
+
: morph
|
|
44
|
+
}
|
|
45
|
+
: {
|
|
46
|
+
rules: branch,
|
|
47
|
+
morph
|
|
48
|
+
};
|
|
49
|
+
const writeMalformedMorphExpressionMessage = (value) => `Morph expression requires a function following '|>' (was ${typeof value})`;
|
|
50
|
+
exports.writeMalformedMorphExpressionMessage = writeMalformedMorphExpressionMessage;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseNarrowTuple = void 0;
|
|
4
|
+
const node_js_1 = require("../../nodes/node.js");
|
|
5
|
+
const definition_js_1 = require("../definition.js");
|
|
6
|
+
const distributableFunction_js_1 = require("./distributableFunction.js");
|
|
7
|
+
const parseNarrowTuple = (def, ctx) => {
|
|
8
|
+
const inputNode = (0, definition_js_1.parseDefinition)(def[0], ctx);
|
|
9
|
+
const resolution = ctx.type.scope.resolveNode(inputNode);
|
|
10
|
+
const hasConfig = (0, node_js_1.isConfigNode)(resolution);
|
|
11
|
+
const typeNode = hasConfig ? resolution.node : resolution;
|
|
12
|
+
const result = (0, node_js_1.rootIntersection)(inputNode, (0, distributableFunction_js_1.distributeFunctionToNode)(def[2], typeNode, ctx, "narrow"), ctx.type);
|
|
13
|
+
return hasConfig
|
|
14
|
+
? { config: resolution.config, node: result }
|
|
15
|
+
: result;
|
|
16
|
+
};
|
|
17
|
+
exports.parseNarrowTuple = parseNarrowTuple;
|
|
@@ -9,7 +9,9 @@ import type { BuiltinClass, inferObjectKind } from "../../utils/objectKinds.js";
|
|
|
9
9
|
import type { inferTerminal } from "./ast.js";
|
|
10
10
|
export type inferNode<node extends Node<$>, $ = {}> = node extends string ? inferTerminal<node, $> : node extends Node<$> ? inferResolution<node, $> extends infer result ? result extends BuiltinClass ? result : evaluateObjectOrFunction<result> : never : never;
|
|
11
11
|
export type inferResolution<node extends Node<$>, $> = {
|
|
12
|
-
[domain in keyof node]: inferPredicate<
|
|
12
|
+
[domain in keyof node]: inferPredicate<
|
|
13
|
+
/** @ts-expect-error Some very odd inference behavior related to domain I can't resolve */
|
|
14
|
+
domain, node[domain], $>;
|
|
13
15
|
}[keyof node];
|
|
14
16
|
type inferPredicate<domain extends Domain, predicate extends Predicate, $> = predicate extends true ? inferDomain<domain> : inferBranch<domain, branchFrom<predicate>, $>;
|
|
15
17
|
type branchFrom<predicate extends Predicate> = predicate extends List ? predicate[number] : predicate;
|