arktype 0.6.0 → 1.0.0-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 +1 -1
- package/dist/api.cjs +2 -2
- package/dist/api.d.ts +1 -1
- package/dist/api.mjs +1 -1
- package/dist/cjs/nodes/compose.js +4 -4
- package/dist/cjs/nodes/discriminate.js +1 -1
- package/dist/cjs/nodes/node.js +27 -19
- package/dist/cjs/nodes/predicate.js +0 -1
- package/dist/cjs/parse/ast/config.js +4 -6
- package/dist/cjs/parse/ast/distributableFunction.js +2 -2
- package/dist/cjs/parse/ast/morph.js +2 -1
- package/dist/cjs/parse/ast/narrow.js +8 -1
- package/dist/cjs/parse/definition.js +2 -2
- package/dist/cjs/parse/string/reduce/dynamic.js +1 -1
- package/dist/cjs/parse/string/shift/operand/unenclosed.js +1 -1
- package/dist/cjs/parse/string/string.js +2 -2
- package/dist/cjs/scopes/ark.js +6 -6
- package/dist/cjs/scopes/scope.js +47 -59
- package/dist/cjs/scopes/type.js +19 -14
- package/dist/cjs/scopes/validation/validation.js +5 -0
- package/dist/cjs/traverse/problems.js +3 -3
- package/dist/cjs/traverse/traverse.js +19 -1
- package/dist/cjs/utils/generics.js +16 -0
- package/dist/cjs/utils/serialize.js +29 -53
- package/dist/mjs/nodes/compose.js +5 -5
- package/dist/mjs/nodes/discriminate.js +1 -1
- package/dist/mjs/nodes/node.js +25 -19
- package/dist/mjs/nodes/predicate.js +0 -1
- package/dist/mjs/parse/ast/config.js +4 -6
- package/dist/mjs/parse/ast/distributableFunction.js +2 -2
- package/dist/mjs/parse/ast/morph.js +2 -1
- package/dist/mjs/parse/ast/narrow.js +9 -2
- package/dist/mjs/parse/definition.js +2 -2
- package/dist/mjs/parse/string/reduce/dynamic.js +1 -1
- package/dist/mjs/parse/string/shift/operand/unenclosed.js +1 -1
- package/dist/mjs/parse/string/string.js +2 -2
- package/dist/mjs/scopes/ark.js +5 -5
- package/dist/mjs/scopes/scope.js +48 -60
- package/dist/mjs/scopes/type.js +16 -13
- package/dist/mjs/scopes/validation/validation.js +5 -0
- package/dist/mjs/traverse/problems.js +3 -3
- package/dist/mjs/traverse/traverse.js +19 -1
- package/dist/mjs/utils/generics.js +11 -0
- package/dist/mjs/utils/serialize.js +29 -52
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/nodes/branch.d.ts +5 -3
- package/dist/types/nodes/node.d.ts +15 -8
- package/dist/types/nodes/predicate.d.ts +3 -3
- package/dist/types/nodes/rules/props.d.ts +8 -8
- package/dist/types/parse/ast/distributableFunction.d.ts +1 -1
- package/dist/types/parse/ast/morph.d.ts +2 -2
- package/dist/types/parse/ast/node.d.ts +6 -6
- package/dist/types/parse/ast/tuple.d.ts +4 -4
- package/dist/types/parse/definition.d.ts +2 -2
- package/dist/types/parse/string/reduce/dynamic.d.ts +5 -5
- package/dist/types/parse/string/string.d.ts +3 -3
- package/dist/types/scopes/ark.d.ts +8 -87
- package/dist/types/scopes/scope.d.ts +9 -8
- package/dist/types/scopes/type.d.ts +22 -13
- package/dist/types/scopes/validation/validation.d.ts +2 -0
- package/dist/types/traverse/traverse.d.ts +6 -1
- package/dist/types/utils/generics.d.ts +5 -0
- package/dist/types/utils/serialize.d.ts +0 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ Isomorphic types for TS/JS
|
|
|
14
14
|
|
|
15
15
|
If you're using TypeScript, you'll need at least `4.8`
|
|
16
16
|
|
|
17
|
-
_Note:
|
|
17
|
+
_Note: Our APIs have largely stabilized at this point, but some may still change during the alpha/beta stages of our 1.0 release. If you have feedback that may require a breaking change, now is the time to let us know!_
|
|
18
18
|
|
|
19
19
|
### Your first type
|
|
20
20
|
|
package/dist/api.cjs
CHANGED
|
@@ -11,8 +11,8 @@ function _export(target, all) {
|
|
|
11
11
|
_export(exports, {
|
|
12
12
|
scope: ()=>_scopeTs.scope,
|
|
13
13
|
type: ()=>_arkTs.type,
|
|
14
|
-
ark: ()=>_arkTs.
|
|
15
|
-
|
|
14
|
+
ark: ()=>_arkTs.ark,
|
|
15
|
+
arkscope: ()=>_arkTs.arkscope,
|
|
16
16
|
intersection: ()=>_expressionsTs.intersection,
|
|
17
17
|
union: ()=>_expressionsTs.union,
|
|
18
18
|
arrayOf: ()=>_expressionsTs.arrayOf,
|
package/dist/api.d.ts
CHANGED
package/dist/api.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { scope } from "./mjs/scopes/scope.js";
|
|
2
|
-
export { type,
|
|
2
|
+
export { type, ark, arkscope } from "./mjs/scopes/ark.js";
|
|
3
3
|
export { intersection, union, arrayOf, instanceOf, valueOf, morph, narrow, keyOf } from "./mjs/scopes/expressions.js";
|
|
4
4
|
export { Problems, Problem } from "./mjs/traverse/problems.js";
|
|
@@ -71,10 +71,10 @@ const disjointDescriptionWriters = {
|
|
|
71
71
|
range: ({ l , r })=>`${stringifyRange(l)} and ${stringifyRange(r)}`,
|
|
72
72
|
class: ({ l , r })=>`classes ${typeof l === "string" ? l : l.name} and ${typeof r === "string" ? r : r.name}`,
|
|
73
73
|
tupleLength: ({ l , r })=>`tuples of length ${l} and ${r}`,
|
|
74
|
-
value: ({ l , r })=>`literal values ${(0, _serializeTs.
|
|
75
|
-
leftAssignability: ({ l , r })=>`literal value ${(0, _serializeTs.
|
|
76
|
-
rightAssignability: ({ l , r })=>`literal value ${(0, _serializeTs.
|
|
77
|
-
union: ({ l , r })=>`branches ${(0, _serializeTs.
|
|
74
|
+
value: ({ l , r })=>`literal values ${(0, _serializeTs.stringify)(l)} and ${(0, _serializeTs.stringify)(r)}`,
|
|
75
|
+
leftAssignability: ({ l , r })=>`literal value ${(0, _serializeTs.stringify)(l.value)} and ${(0, _serializeTs.stringify)(r)}`,
|
|
76
|
+
rightAssignability: ({ l , r })=>`literal value ${(0, _serializeTs.stringify)(r.value)} and ${(0, _serializeTs.stringify)(l)}`,
|
|
77
|
+
union: ({ l , r })=>`branches ${(0, _serializeTs.stringify)(l)} and branches ${(0, _serializeTs.stringify)(r)}`
|
|
78
78
|
};
|
|
79
79
|
const stringifyRange = (range)=>"limit" in range ? `the range of exactly ${range.limit}` : range.min ? range.max ? `the range bounded by ${range.min.comparator}${range.min.limit} and ${range.max.comparator}${range.max.limit}` : `${range.min.comparator}${range.min.limit}` : range.max ? `${range.max.comparator}${range.max.limit}` : "the unbounded range";
|
|
80
80
|
var _disjoints = /*#__PURE__*/ new WeakMap();
|
|
@@ -259,4 +259,4 @@ const serializeData = {
|
|
|
259
259
|
};
|
|
260
260
|
const serializeCase = (kind, data)=>serializeData[kind](data);
|
|
261
261
|
const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph((0, _propsTs.propToNode)(prop), $)) : false;
|
|
262
|
-
const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).includesMorph : Object.values(node).some((predicate)=>predicate === true ? false : (0, _objectKindsTs.isArray)(predicate) ? predicate.some((branch)=>branchIncludesMorph(branch, $)) : branchIncludesMorph(predicate, $));
|
|
262
|
+
const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).includesMorph : Object.values($.resolveTypeNode(node)).some((predicate)=>predicate === true ? false : (0, _objectKindsTs.isArray)(predicate) ? predicate.some((branch)=>branchIncludesMorph(branch, $)) : branchIncludesMorph(predicate, $));
|
package/dist/cjs/nodes/node.js
CHANGED
|
@@ -9,11 +9,13 @@ function _export(target, all) {
|
|
|
9
9
|
});
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
|
+
isConfigNode: ()=>isConfigNode,
|
|
12
13
|
nodeIntersection: ()=>nodeIntersection,
|
|
13
14
|
rootIntersection: ()=>rootIntersection,
|
|
14
15
|
rootUnion: ()=>rootUnion,
|
|
15
16
|
flattenType: ()=>flattenType,
|
|
16
17
|
flattenNode: ()=>flattenNode,
|
|
18
|
+
flattenTypeNode: ()=>flattenTypeNode,
|
|
17
19
|
isLiteralNode: ()=>isLiteralNode,
|
|
18
20
|
resolutionExtendsDomain: ()=>resolutionExtendsDomain,
|
|
19
21
|
toArrayNode: ()=>toArrayNode
|
|
@@ -25,17 +27,18 @@ const _pathsTs = require("../utils/paths.js");
|
|
|
25
27
|
const _composeTs = require("./compose.js");
|
|
26
28
|
const _predicateTs = require("./predicate.js");
|
|
27
29
|
const _propsTs = require("./rules/props.js");
|
|
30
|
+
const isConfigNode = (node)=>"config" in node;
|
|
28
31
|
const nodeIntersection = (l, r, state)=>{
|
|
29
32
|
state.domain = undefined;
|
|
30
|
-
const
|
|
31
|
-
const
|
|
32
|
-
const result =
|
|
33
|
+
const lDomains = state.type.scope.resolveTypeNode(l);
|
|
34
|
+
const rDomains = state.type.scope.resolveTypeNode(r);
|
|
35
|
+
const result = typeNodeIntersection(lDomains, rDomains, state);
|
|
33
36
|
if (typeof result === "object" && !(0, _genericsTs.hasKeys)(result)) {
|
|
34
|
-
return (0, _genericsTs.hasKeys)(state.disjoints) ? (0, _composeTs.anonymousDisjoint)() : state.addDisjoint("domain", (0, _genericsTs.objectKeysOf)(
|
|
37
|
+
return (0, _genericsTs.hasKeys)(state.disjoints) ? (0, _composeTs.anonymousDisjoint)() : state.addDisjoint("domain", (0, _genericsTs.objectKeysOf)(lDomains), (0, _genericsTs.objectKeysOf)(rDomains));
|
|
35
38
|
}
|
|
36
|
-
return result ===
|
|
39
|
+
return result === lDomains ? l : result === rDomains ? r : result;
|
|
37
40
|
};
|
|
38
|
-
const
|
|
41
|
+
const typeNodeIntersection = (0, _composeTs.composeKeyedIntersection)((domain, l, r, context)=>{
|
|
39
42
|
if (l === undefined) {
|
|
40
43
|
return r === undefined ? (0, _composeTs.throwUndefinedOperandsError)() : undefined;
|
|
41
44
|
}
|
|
@@ -52,15 +55,15 @@ const rootIntersection = (l, r, type)=>{
|
|
|
52
55
|
return (0, _composeTs.isDisjoint)(result) ? (0, _errorsTs.throwParseError)((0, _intersectionTs.compileDisjointReasonsMessage)(state.disjoints)) : (0, _composeTs.isEquality)(result) ? l : result;
|
|
53
56
|
};
|
|
54
57
|
const rootUnion = (l, r, type)=>{
|
|
55
|
-
const
|
|
56
|
-
const
|
|
58
|
+
const lDomains = type.scope.resolveTypeNode(l);
|
|
59
|
+
const rDomains = type.scope.resolveTypeNode(r);
|
|
57
60
|
const result = {};
|
|
58
61
|
const domains = (0, _genericsTs.objectKeysOf)({
|
|
59
|
-
...
|
|
60
|
-
...
|
|
62
|
+
...lDomains,
|
|
63
|
+
...rDomains
|
|
61
64
|
});
|
|
62
65
|
for (const domain of domains){
|
|
63
|
-
result[domain] = (0, _genericsTs.hasKey)(
|
|
66
|
+
result[domain] = (0, _genericsTs.hasKey)(lDomains, domain) ? (0, _genericsTs.hasKey)(rDomains, domain) ? (0, _predicateTs.predicateUnion)(domain, lDomains[domain], rDomains[domain], type) : lDomains[domain] : (0, _genericsTs.hasKey)(rDomains, domain) ? rDomains[domain] : (0, _composeTs.throwUndefinedOperandsError)();
|
|
64
67
|
}
|
|
65
68
|
return result;
|
|
66
69
|
};
|
|
@@ -71,20 +74,25 @@ const flattenType = (type)=>{
|
|
|
71
74
|
path: new _pathsTs.Path(),
|
|
72
75
|
lastDomain: "undefined"
|
|
73
76
|
};
|
|
74
|
-
return type.
|
|
77
|
+
return flattenNode(type.node, ctx);
|
|
78
|
+
};
|
|
79
|
+
const flattenNode = (node, ctx)=>{
|
|
80
|
+
if (typeof node === "string") {
|
|
81
|
+
return ctx.type.scope.resolve(node).flat;
|
|
82
|
+
}
|
|
83
|
+
const hasConfig = isConfigNode(node);
|
|
84
|
+
const flattenedTypeNode = flattenTypeNode(hasConfig ? node.node : node, ctx);
|
|
85
|
+
return hasConfig ? [
|
|
75
86
|
[
|
|
76
87
|
"config",
|
|
77
88
|
{
|
|
78
|
-
config: (0, _genericsTs.entriesOf)(
|
|
79
|
-
node:
|
|
89
|
+
config: (0, _genericsTs.entriesOf)(node.config),
|
|
90
|
+
node: flattenedTypeNode
|
|
80
91
|
}
|
|
81
92
|
]
|
|
82
|
-
] :
|
|
93
|
+
] : flattenedTypeNode;
|
|
83
94
|
};
|
|
84
|
-
const
|
|
85
|
-
if (typeof node === "string") {
|
|
86
|
-
return ctx.type.scope.resolve(node).flat;
|
|
87
|
-
}
|
|
95
|
+
const flattenTypeNode = (node, ctx)=>{
|
|
88
96
|
const domains = (0, _genericsTs.objectKeysOf)(node);
|
|
89
97
|
if (domains.length === 1) {
|
|
90
98
|
const domain = domains[0];
|
|
@@ -20,7 +20,6 @@ const _objectKindsTs = require("../utils/objectKinds.js");
|
|
|
20
20
|
const _branchTs = require("./branch.js");
|
|
21
21
|
const _composeTs = require("./compose.js");
|
|
22
22
|
const _discriminateTs = require("./discriminate.js");
|
|
23
|
-
require("./rules/rules.js");
|
|
24
23
|
const emptyRulesIfTrue = (predicate)=>predicate === true ? {} : predicate;
|
|
25
24
|
const comparePredicates = (l, r, context)=>{
|
|
26
25
|
if (l === true && r === true) {
|
|
@@ -6,10 +6,8 @@ Object.defineProperty(exports, "parseConfigTuple", {
|
|
|
6
6
|
enumerable: true,
|
|
7
7
|
get: ()=>parseConfigTuple
|
|
8
8
|
});
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
const _definitionTs = require("../definition.js");
|
|
10
|
+
const parseConfigTuple = (def, ctx)=>({
|
|
11
|
+
node: ctx.type.scope.resolveTypeNode((0, _definitionTs.parseDefinition)(def[0], ctx)),
|
|
12
|
+
config: def[2]
|
|
13
13
|
});
|
|
14
|
-
return anonymousType.name;
|
|
15
|
-
};
|
|
@@ -17,8 +17,8 @@ const _errorsTs = require("../../utils/errors.js");
|
|
|
17
17
|
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
|
-
const distributeFunctionToNode = (distributableFunction,
|
|
21
|
-
const domains = (0, _genericsTs.objectKeysOf)(
|
|
20
|
+
const distributeFunctionToNode = (distributableFunction, node, ctx, ruleKey)=>{
|
|
21
|
+
const domains = (0, _genericsTs.objectKeysOf)(node);
|
|
22
22
|
if (!(0, _domainsTs.hasDomain)(distributableFunction, "object")) {
|
|
23
23
|
return (0, _errorsTs.throwParseError)(writeMalformedDistributableFunctionMessage(distributableFunction));
|
|
24
24
|
}
|
|
@@ -21,7 +21,8 @@ const parseMorphTuple = (def, ctx)=>{
|
|
|
21
21
|
if (typeof def[2] !== "function") {
|
|
22
22
|
return (0, _errorsTs.throwParseError)(writeMalformedMorphExpressionMessage(def[2]));
|
|
23
23
|
}
|
|
24
|
-
const
|
|
24
|
+
const node = (0, _definitionTs.parseDefinition)(def[0], ctx);
|
|
25
|
+
const resolution = ctx.type.scope.resolveTypeNode(node);
|
|
25
26
|
const morph = def[2];
|
|
26
27
|
ctx.type.includesMorph = true;
|
|
27
28
|
let domain;
|
|
@@ -11,5 +11,12 @@ const _definitionTs = require("../definition.js");
|
|
|
11
11
|
const _distributableFunctionTs = require("./distributableFunction.js");
|
|
12
12
|
const parseNarrowTuple = (def, ctx)=>{
|
|
13
13
|
const inputNode = (0, _definitionTs.parseDefinition)(def[0], ctx);
|
|
14
|
-
|
|
14
|
+
const resolution = ctx.type.scope.resolveNode(inputNode);
|
|
15
|
+
const hasConfig = (0, _nodeTs.isConfigNode)(resolution);
|
|
16
|
+
const typeNode = hasConfig ? resolution.node : resolution;
|
|
17
|
+
const result = (0, _nodeTs.rootIntersection)(inputNode, (0, _distributableFunctionTs.distributeFunctionToNode)(def[2], typeNode, ctx, "narrow"), ctx.type);
|
|
18
|
+
return hasConfig ? {
|
|
19
|
+
config: resolution.config,
|
|
20
|
+
node: result
|
|
21
|
+
} : result;
|
|
15
22
|
};
|
|
@@ -44,12 +44,12 @@ const parseDefinition = (def, ctx)=>{
|
|
|
44
44
|
};
|
|
45
45
|
case "Function":
|
|
46
46
|
if ((0, _typeTs.isType)(def)) {
|
|
47
|
-
return ctx.type.scope.
|
|
47
|
+
return ctx.type.scope.addAnonymousTypeReference(def, ctx);
|
|
48
48
|
}
|
|
49
49
|
if (isThunk(def)) {
|
|
50
50
|
const returned = def();
|
|
51
51
|
if ((0, _typeTs.isType)(returned)) {
|
|
52
|
-
return ctx.type.scope.
|
|
52
|
+
return ctx.type.scope.addAnonymousTypeReference(returned, ctx);
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
return (0, _errorsTs.throwParseError)(writeBadDefinitionTypeMessage("Function"));
|
|
@@ -21,7 +21,7 @@ const parseUnenclosed = (s)=>{
|
|
|
21
21
|
s.setRoot(unenclosedToNode(s, token));
|
|
22
22
|
};
|
|
23
23
|
const unenclosedToNode = (s, token)=>{
|
|
24
|
-
if (s.ctx.type.scope.
|
|
24
|
+
if (s.ctx.type.scope.addParsedReferenceIfResolvable(token, s.ctx)) {
|
|
25
25
|
return token;
|
|
26
26
|
}
|
|
27
27
|
return maybeParseUnenclosedLiteral(token) ?? s.error(token === "" ? writeMissingOperandMessage(s) : writeUnresolvableMessage(token));
|
|
@@ -19,12 +19,12 @@ const _operandTs = require("./shift/operand/operand.js");
|
|
|
19
19
|
const _operatorTs = require("./shift/operator/operator.js");
|
|
20
20
|
const parseString = (def, ctx)=>ctx.type.scope.parseCache.get(def) ?? ctx.type.scope.parseCache.set(def, maybeNaiveParse(def, ctx) ?? fullStringParse(def, ctx));
|
|
21
21
|
const maybeNaiveParse = (def, ctx)=>{
|
|
22
|
-
if (ctx.type.scope.
|
|
22
|
+
if (ctx.type.scope.addParsedReferenceIfResolvable(def, ctx)) {
|
|
23
23
|
return def;
|
|
24
24
|
}
|
|
25
25
|
if (def.endsWith("[]")) {
|
|
26
26
|
const elementDef = def.slice(0, -2);
|
|
27
|
-
if (ctx.type.scope.
|
|
27
|
+
if (ctx.type.scope.addParsedReferenceIfResolvable(def, ctx)) {
|
|
28
28
|
return (0, _nodeTs.toArrayNode)(elementDef);
|
|
29
29
|
}
|
|
30
30
|
}
|
package/dist/cjs/scopes/ark.js
CHANGED
|
@@ -9,8 +9,8 @@ function _export(target, all) {
|
|
|
9
9
|
});
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
|
+
arkscope: ()=>arkscope,
|
|
12
13
|
ark: ()=>ark,
|
|
13
|
-
arktypes: ()=>arktypes,
|
|
14
14
|
scopes: ()=>scopes,
|
|
15
15
|
spaces: ()=>spaces,
|
|
16
16
|
type: ()=>type
|
|
@@ -19,7 +19,7 @@ const _jsObjectsTs = require("./jsObjects.js");
|
|
|
19
19
|
const _scopeTs = require("./scope.js");
|
|
20
20
|
const _tsKeywordsTs = require("./tsKeywords.js");
|
|
21
21
|
const _validationTs = require("./validation/validation.js");
|
|
22
|
-
const
|
|
22
|
+
const arkscope = (0, _scopeTs.scope)({}, {
|
|
23
23
|
name: "standard",
|
|
24
24
|
includes: [
|
|
25
25
|
_tsKeywordsTs.tsKeywords,
|
|
@@ -28,18 +28,18 @@ const ark = (0, _scopeTs.scope)({}, {
|
|
|
28
28
|
],
|
|
29
29
|
standard: false
|
|
30
30
|
});
|
|
31
|
-
const
|
|
31
|
+
const ark = arkscope.compile();
|
|
32
32
|
const scopes = {
|
|
33
33
|
root: _scopeTs.rootScope,
|
|
34
34
|
tsKeywords: _tsKeywordsTs.tsKeywordsScope,
|
|
35
35
|
jsObjects: _jsObjectsTs.jsObjectsScope,
|
|
36
36
|
validation: _validationTs.validationScope,
|
|
37
|
-
ark
|
|
37
|
+
ark: arkscope
|
|
38
38
|
};
|
|
39
39
|
const spaces = {
|
|
40
40
|
tsKeywords: _tsKeywordsTs.tsKeywords,
|
|
41
41
|
jsObjects: _jsObjectsTs.jsObjects,
|
|
42
42
|
validation: _validationTs.validation,
|
|
43
|
-
ark
|
|
43
|
+
ark
|
|
44
44
|
};
|
|
45
|
-
const type =
|
|
45
|
+
const type = arkscope.type;
|
package/dist/cjs/scopes/scope.js
CHANGED
|
@@ -24,6 +24,7 @@ const _problemsTs = require("../traverse/problems.js");
|
|
|
24
24
|
const _chainableNoOpProxyTs = require("../utils/chainableNoOpProxy.js");
|
|
25
25
|
const _errorsTs = require("../utils/errors.js");
|
|
26
26
|
const _freezeTs = require("../utils/freeze.js");
|
|
27
|
+
const _genericsTs = require("../utils/generics.js");
|
|
27
28
|
const _pathsTs = require("../utils/paths.js");
|
|
28
29
|
const _cacheTs = require("./cache.js");
|
|
29
30
|
const _typeTs = require("./type.js");
|
|
@@ -77,7 +78,7 @@ function _classPrivateMethodInit(obj, privateSet) {
|
|
|
77
78
|
_checkPrivateRedeclaration(obj, privateSet);
|
|
78
79
|
privateSet.add(obj);
|
|
79
80
|
}
|
|
80
|
-
var _ctx_type;
|
|
81
|
+
var _ctx_type, _ctx_type1;
|
|
81
82
|
const compileScopeOptions = (opts)=>({
|
|
82
83
|
codes: (0, _problemsTs.compileProblemWriters)(opts.codes),
|
|
83
84
|
keys: opts.keys ?? "loose"
|
|
@@ -88,34 +89,17 @@ const spaceRegistry = {};
|
|
|
88
89
|
const isConfigTuple = (def)=>Array.isArray(def) && def[1] === ":";
|
|
89
90
|
var _resolutions = /*#__PURE__*/ new WeakMap(), _exports = /*#__PURE__*/ new WeakMap(), _register = /*#__PURE__*/ new WeakSet(), _cacheSpaces = /*#__PURE__*/ new WeakSet(), _initializeContext = /*#__PURE__*/ new WeakSet(), _resolveRecurse = /*#__PURE__*/ new WeakSet();
|
|
90
91
|
class Scope {
|
|
91
|
-
|
|
92
|
-
let
|
|
93
|
-
|
|
94
|
-
|
|
92
|
+
getAnonymousQualifiedName(base) {
|
|
93
|
+
let increment = 0;
|
|
94
|
+
let id = base;
|
|
95
|
+
while(this.isResolvable(id)){
|
|
96
|
+
id = `${base}${increment++}`;
|
|
95
97
|
}
|
|
96
|
-
|
|
97
|
-
let name = base;
|
|
98
|
-
while(this.isInternallyResolvable(name)){
|
|
99
|
-
name = `${base}${++increment}`;
|
|
100
|
-
}
|
|
101
|
-
return name;
|
|
98
|
+
return `${this.name}.${id}`;
|
|
102
99
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
return type.name;
|
|
107
|
-
}
|
|
108
|
-
const name = this.getAnonymousTypeName(ctx);
|
|
109
|
-
this.type([
|
|
110
|
-
"node",
|
|
111
|
-
type.node
|
|
112
|
-
], {
|
|
113
|
-
name
|
|
114
|
-
});
|
|
115
|
-
return name;
|
|
116
|
-
}
|
|
117
|
-
_classPrivateFieldGet(this, _resolutions).set(type.name, type);
|
|
118
|
-
return type.name;
|
|
100
|
+
addAnonymousTypeReference(referencedType, ctx) {
|
|
101
|
+
(_ctx_type = ctx.type).includesMorph || (_ctx_type.includesMorph = referencedType.includesMorph);
|
|
102
|
+
return referencedType.node;
|
|
119
103
|
}
|
|
120
104
|
get infer() {
|
|
121
105
|
return _chainableNoOpProxyTs.chainableNoOpProxy;
|
|
@@ -129,21 +113,29 @@ class Scope {
|
|
|
129
113
|
}
|
|
130
114
|
return _classPrivateFieldGet(this, _exports).root;
|
|
131
115
|
}
|
|
132
|
-
|
|
133
|
-
const resolution = _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, name, "undefined", [
|
|
116
|
+
addParsedReferenceIfResolvable(name, ctx) {
|
|
117
|
+
const resolution = _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, name, "undefined", [
|
|
118
|
+
name
|
|
119
|
+
]);
|
|
134
120
|
if (!resolution) {
|
|
135
121
|
return false;
|
|
136
122
|
}
|
|
137
|
-
(
|
|
123
|
+
(_ctx_type1 = ctx.type).includesMorph || (_ctx_type1.includesMorph = resolution.includesMorph);
|
|
138
124
|
return true;
|
|
139
125
|
}
|
|
140
126
|
resolve(name) {
|
|
141
|
-
return _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, name, "throw", [
|
|
127
|
+
return _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, name, "throw", [
|
|
128
|
+
name
|
|
129
|
+
]);
|
|
142
130
|
}
|
|
143
131
|
resolveNode(node) {
|
|
144
|
-
return typeof node === "
|
|
132
|
+
return typeof node === "string" ? this.resolveNode(this.resolve(node).node) : node;
|
|
133
|
+
}
|
|
134
|
+
resolveTypeNode(node) {
|
|
135
|
+
const resolution = this.resolveNode(node);
|
|
136
|
+
return (0, _nodeTs.isConfigNode)(resolution) ? resolution.node : resolution;
|
|
145
137
|
}
|
|
146
|
-
|
|
138
|
+
isResolvable(name) {
|
|
147
139
|
return _classPrivateFieldGet(this, _resolutions).has(name) || this.aliases[name];
|
|
148
140
|
}
|
|
149
141
|
constructor(aliases, opts = {}){
|
|
@@ -213,16 +205,15 @@ class Scope {
|
|
|
213
205
|
this.instanceOf = this.expressions.instanceOf;
|
|
214
206
|
this.narrow = this.expressions.narrow;
|
|
215
207
|
this.morph = this.expressions.morph;
|
|
216
|
-
this.type = Object.assign((def,
|
|
217
|
-
const
|
|
218
|
-
// remove name from opts since we've already used it and otherwise it will
|
|
219
|
-
// interfere with traversal keys being ProblemOptions
|
|
220
|
-
delete opts?.name;
|
|
221
|
-
const t = (0, _typeTs.initializeType)(name, def, opts, this);
|
|
208
|
+
this.type = Object.assign((def, config = {})=>{
|
|
209
|
+
const t = (0, _typeTs.initializeType)("λtype", def, config, this);
|
|
222
210
|
const ctx = _classPrivateMethodGet(this, _initializeContext, initializeContext).call(this, t);
|
|
223
|
-
|
|
211
|
+
const root = (0, _definitionTs.parseDefinition)(def, ctx);
|
|
212
|
+
t.node = (0, _freezeTs.deepFreeze)((0, _genericsTs.hasKeys)(config) ? {
|
|
213
|
+
config,
|
|
214
|
+
node: this.resolveTypeNode(root)
|
|
215
|
+
} : root);
|
|
224
216
|
t.flat = (0, _freezeTs.deepFreeze)((0, _nodeTs.flattenType)(t));
|
|
225
|
-
_classPrivateFieldGet(this, _resolutions).set(t.name, t);
|
|
226
217
|
return t;
|
|
227
218
|
}, {
|
|
228
219
|
from: this.expressions.node
|
|
@@ -271,32 +262,29 @@ function resolveRecurse(name, onUnresolvable, seen) {
|
|
|
271
262
|
if (maybeCacheResult) {
|
|
272
263
|
return maybeCacheResult;
|
|
273
264
|
}
|
|
274
|
-
const
|
|
275
|
-
if (!
|
|
265
|
+
const aliasDef = this.aliases[name];
|
|
266
|
+
if (!aliasDef) {
|
|
276
267
|
return onUnresolvable === "throw" ? (0, _errorsTs.throwInternalError)(`Unexpectedly failed to resolve alias '${name}'`) : undefined;
|
|
277
268
|
}
|
|
278
|
-
const
|
|
279
|
-
|
|
280
|
-
_classPrivateFieldGet(this,
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
if (typeof
|
|
284
|
-
if (seen.includes(
|
|
269
|
+
const t = (0, _typeTs.initializeType)(name, aliasDef, {}, this);
|
|
270
|
+
const ctx = _classPrivateMethodGet(this, _initializeContext, initializeContext).call(this, t);
|
|
271
|
+
_classPrivateFieldGet(this, _resolutions).set(name, t);
|
|
272
|
+
_classPrivateFieldGet(this, _exports).set(name, t);
|
|
273
|
+
let node = (0, _definitionTs.parseDefinition)(aliasDef, ctx);
|
|
274
|
+
if (typeof node === "string") {
|
|
275
|
+
if (seen.includes(node)) {
|
|
285
276
|
return (0, _errorsTs.throwParseError)(writeShallowCycleErrorMessage(name, seen));
|
|
286
277
|
}
|
|
287
|
-
seen.push(
|
|
288
|
-
|
|
289
|
-
type.node = resolvedType.node;
|
|
290
|
-
type.flat = resolvedType.flat;
|
|
291
|
-
return type;
|
|
278
|
+
seen.push(node);
|
|
279
|
+
node = _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, node, "throw", seen).node;
|
|
292
280
|
}
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
return
|
|
281
|
+
t.node = (0, _freezeTs.deepFreeze)(node);
|
|
282
|
+
t.flat = (0, _freezeTs.deepFreeze)((0, _nodeTs.flattenType)(t));
|
|
283
|
+
return t;
|
|
296
284
|
}
|
|
297
285
|
const scope = (aliases, opts = {})=>new Scope(aliases, opts);
|
|
298
286
|
const rootScope = scope({}, {
|
|
299
|
-
name: "
|
|
287
|
+
name: "root",
|
|
300
288
|
standard: false
|
|
301
289
|
});
|
|
302
290
|
const rootType = rootScope.type;
|
package/dist/cjs/scopes/type.js
CHANGED
|
@@ -10,11 +10,23 @@ function _export(target, all) {
|
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
12
|
initializeType: ()=>initializeType,
|
|
13
|
-
isType: ()=>isType
|
|
13
|
+
isType: ()=>isType,
|
|
14
|
+
isCheckResult: ()=>isCheckResult,
|
|
15
|
+
isAnonymousName: ()=>isAnonymousName
|
|
14
16
|
});
|
|
15
17
|
const _traverseTs = require("../traverse/traverse.js");
|
|
16
18
|
const _chainableNoOpProxyTs = require("../utils/chainableNoOpProxy.js");
|
|
19
|
+
const _genericsTs = require("../utils/generics.js");
|
|
17
20
|
const initializeType = (name, definition, config, scope)=>{
|
|
21
|
+
const namedTraverse = (0, _genericsTs.composeNamed)((data)=>{
|
|
22
|
+
const state = new _traverseTs.TraversalState(data, t);
|
|
23
|
+
(0, _traverseTs.traverse)(t.flat, state);
|
|
24
|
+
return state.problems.count ? {
|
|
25
|
+
problems: state.problems
|
|
26
|
+
} : {
|
|
27
|
+
data: state.data
|
|
28
|
+
};
|
|
29
|
+
}, name);
|
|
18
30
|
const root = {
|
|
19
31
|
// temporarily initialize node/flat to aliases that will be included in
|
|
20
32
|
// the final type in case of cyclic resolutions
|
|
@@ -25,24 +37,17 @@ const initializeType = (name, definition, config, scope)=>{
|
|
|
25
37
|
name
|
|
26
38
|
]
|
|
27
39
|
],
|
|
40
|
+
allows: (data)=>!namedTraverse(data).problems,
|
|
28
41
|
infer: _chainableNoOpProxyTs.chainableNoOpProxy,
|
|
29
|
-
qualifiedName: `${scope.name}.${name}`,
|
|
42
|
+
qualifiedName: isAnonymousName(name) ? scope.getAnonymousQualifiedName(name) : `${scope.name}.${name}`,
|
|
30
43
|
definition,
|
|
31
44
|
scope,
|
|
32
45
|
includesMorph: false,
|
|
33
46
|
config
|
|
34
47
|
};
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
[name]: (data)=>{
|
|
38
|
-
const state = new _traverseTs.TraversalState(data, namedTraverse);
|
|
39
|
-
return (0, _traverseTs.traverse)(namedTraverse.flat, state) ? {
|
|
40
|
-
data: state.data
|
|
41
|
-
} : {
|
|
42
|
-
problems: state.problems
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
}[name];
|
|
46
|
-
return Object.assign(namedTraverse, root);
|
|
48
|
+
const t = Object.assign(namedTraverse, root);
|
|
49
|
+
return t;
|
|
47
50
|
};
|
|
48
51
|
const isType = (value)=>value?.infer === _chainableNoOpProxyTs.chainableNoOpProxy;
|
|
52
|
+
const isCheckResult = (value)=>(0, _genericsTs.getNominalId)(value) !== undefined;
|
|
53
|
+
const isAnonymousName = (name)=>name[0] === "λ";
|
|
@@ -39,6 +39,10 @@ const email = (0, _scopeTs.rootType)(/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z
|
|
|
39
39
|
const uuid = (0, _scopeTs.rootType)(/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/, {
|
|
40
40
|
mustBe: "a valid UUID"
|
|
41
41
|
});
|
|
42
|
+
// https://semver.org/
|
|
43
|
+
const semver = (0, _scopeTs.rootType)(/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/, {
|
|
44
|
+
mustBe: "a valid semantic version (see https://semver.org/)"
|
|
45
|
+
});
|
|
42
46
|
const validationScope = (0, _scopeTs.scope)({
|
|
43
47
|
// Character sets
|
|
44
48
|
alpha: [
|
|
@@ -75,6 +79,7 @@ const validationScope = (0, _scopeTs.scope)({
|
|
|
75
79
|
parsedNumber,
|
|
76
80
|
parsedInteger,
|
|
77
81
|
parsedDate: _dateTs.parsedDate,
|
|
82
|
+
semver,
|
|
78
83
|
integer: [
|
|
79
84
|
"node",
|
|
80
85
|
{
|
|
@@ -69,7 +69,7 @@ function _classPrivateFieldSet(receiver, privateMap, value) {
|
|
|
69
69
|
}
|
|
70
70
|
class ArkTypeError extends TypeError {
|
|
71
71
|
constructor(problems){
|
|
72
|
-
super(problems
|
|
72
|
+
super(`${problems}`);
|
|
73
73
|
this.cause = problems;
|
|
74
74
|
}
|
|
75
75
|
}
|
|
@@ -137,10 +137,10 @@ class ProblemArray extends Array {
|
|
|
137
137
|
this.count++;
|
|
138
138
|
}
|
|
139
139
|
get summary() {
|
|
140
|
-
return this
|
|
140
|
+
return `${this}`;
|
|
141
141
|
}
|
|
142
142
|
toString() {
|
|
143
|
-
return this.
|
|
143
|
+
return this.join("\n");
|
|
144
144
|
}
|
|
145
145
|
throw() {
|
|
146
146
|
throw new ArkTypeError(this);
|
|
@@ -20,6 +20,7 @@ const _divisorTs = require("../nodes/rules/divisor.js");
|
|
|
20
20
|
const _rangeTs = require("../nodes/rules/range.js");
|
|
21
21
|
const _regexTs = require("../nodes/rules/regex.js");
|
|
22
22
|
const _rulesTs = require("../nodes/rules/rules.js");
|
|
23
|
+
const _typeTs = require("../scopes/type.js");
|
|
23
24
|
const _domainsTs = require("../utils/domains.js");
|
|
24
25
|
const _errorsTs = require("../utils/errors.js");
|
|
25
26
|
const _genericsTs = require("../utils/generics.js");
|
|
@@ -281,7 +282,14 @@ const entryCheckers = {
|
|
|
281
282
|
},
|
|
282
283
|
alias: (name, state)=>state.traverseResolution(name),
|
|
283
284
|
class: _classTs.checkClass,
|
|
284
|
-
narrow: (narrow, state)=>
|
|
285
|
+
narrow: (narrow, state)=>{
|
|
286
|
+
const lastProblemsCount = state.problems.count;
|
|
287
|
+
const result = narrow(state.data, state.problems);
|
|
288
|
+
if (!result && state.problems.count === lastProblemsCount) {
|
|
289
|
+
state.problems.mustBe(narrow.name ? `valid according to ${narrow.name}` : "valid");
|
|
290
|
+
}
|
|
291
|
+
return result;
|
|
292
|
+
},
|
|
285
293
|
config: ({ config , node }, state)=>state.traverseConfig(config, node),
|
|
286
294
|
value: (value, state)=>state.data === value || !state.problems.add("value", value),
|
|
287
295
|
morph: (morph, state)=>{
|
|
@@ -294,6 +302,16 @@ const entryCheckers = {
|
|
|
294
302
|
state.problems.addProblem(out);
|
|
295
303
|
return false;
|
|
296
304
|
}
|
|
305
|
+
if ((0, _typeTs.isCheckResult)(out)) {
|
|
306
|
+
if (out.problems) {
|
|
307
|
+
for (const problem of out.problems){
|
|
308
|
+
state.problems.addProblem(problem);
|
|
309
|
+
}
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
state.data = out.data;
|
|
313
|
+
return true;
|
|
314
|
+
}
|
|
297
315
|
state.data = out;
|
|
298
316
|
return true;
|
|
299
317
|
},
|