arktype 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.cjs +9 -0
- package/dist/api.d.ts +10 -0
- package/dist/api.mjs +1 -0
- package/dist/cjs/nodes/branch.js +15 -6
- package/dist/cjs/nodes/compose.js +1 -1
- package/dist/cjs/nodes/discriminate.js +89 -21
- package/dist/cjs/nodes/node.js +11 -11
- package/dist/cjs/nodes/predicate.js +1 -1
- package/dist/cjs/nodes/rules/class.js +2 -2
- package/dist/cjs/nodes/rules/divisor.js +1 -1
- package/dist/cjs/nodes/rules/props.js +1 -2
- package/dist/cjs/nodes/rules/range.js +2 -2
- package/dist/cjs/nodes/rules/regex.js +1 -1
- package/dist/cjs/nodes/rules/rules.js +4 -0
- package/dist/cjs/parse/ast/ast.js +4 -0
- package/dist/cjs/parse/ast/bound.js +9 -0
- package/dist/cjs/parse/{tuple/utils.js → ast/distributableFunction.js} +1 -1
- package/dist/cjs/parse/ast/divisor.js +9 -0
- package/dist/cjs/parse/{string/ast.js → ast/intersection.js} +2 -8
- package/dist/cjs/parse/ast/keyof.js +109 -0
- package/dist/cjs/parse/{tuple → ast}/morph.js +1 -1
- package/dist/cjs/parse/{tuple → ast}/narrow.js +2 -2
- package/dist/cjs/parse/{tuple → ast}/tuple.js +11 -10
- package/dist/cjs/parse/ast/union.js +9 -0
- package/dist/cjs/parse/ast/utils.js +4 -0
- package/dist/cjs/parse/definition.js +12 -2
- package/dist/cjs/parse/string/reduce/dynamic.js +7 -7
- package/dist/cjs/parse/string/shift/operator/bounds.js +4 -4
- package/dist/cjs/parse/string/shift/operator/divisor.js +3 -3
- package/dist/cjs/parse/string/string.js +1 -1
- package/dist/cjs/scopes/expressions.js +29 -0
- package/dist/cjs/scopes/scope.js +59 -6
- package/dist/cjs/scopes/validation.js +16 -1
- package/dist/cjs/traverse/problems.js +78 -46
- package/dist/cjs/traverse/traverse.js +62 -58
- package/dist/cjs/utils/data.js +39 -0
- package/dist/cjs/utils/freeze.js +2 -3
- package/dist/cjs/utils/generics.js +20 -2
- package/dist/cjs/utils/numericLiterals.js +2 -14
- package/dist/mjs/nodes/branch.js +14 -5
- package/dist/mjs/nodes/compose.js +2 -2
- package/dist/mjs/nodes/discriminate.js +89 -21
- package/dist/mjs/nodes/node.js +8 -8
- package/dist/mjs/nodes/predicate.js +2 -2
- package/dist/mjs/nodes/rules/class.js +2 -2
- package/dist/mjs/nodes/rules/divisor.js +1 -1
- package/dist/mjs/nodes/rules/props.js +1 -2
- package/dist/mjs/nodes/rules/range.js +2 -2
- package/dist/mjs/nodes/rules/regex.js +1 -1
- package/dist/mjs/nodes/rules/rules.js +4 -0
- package/dist/mjs/parse/ast/ast.js +1 -0
- package/dist/mjs/parse/ast/bound.js +1 -0
- package/dist/mjs/parse/{tuple/utils.js → ast/distributableFunction.js} +2 -2
- package/dist/mjs/parse/ast/divisor.js +1 -0
- package/dist/mjs/parse/{string/ast.js → ast/intersection.js} +2 -5
- package/dist/mjs/parse/ast/keyof.js +101 -0
- package/dist/mjs/parse/{tuple → ast}/morph.js +1 -1
- package/dist/mjs/parse/ast/narrow.js +7 -0
- package/dist/mjs/parse/{tuple → ast}/tuple.js +12 -11
- package/dist/mjs/parse/ast/union.js +1 -0
- package/dist/mjs/parse/ast/utils.js +1 -0
- package/dist/mjs/parse/definition.js +12 -2
- package/dist/mjs/parse/string/reduce/dynamic.js +8 -8
- package/dist/mjs/parse/string/shift/operator/bounds.js +4 -4
- package/dist/mjs/parse/string/shift/operator/divisor.js +3 -3
- package/dist/mjs/parse/string/string.js +2 -2
- package/dist/mjs/scopes/expressions.js +9 -0
- package/dist/mjs/scopes/scope.js +58 -5
- package/dist/mjs/scopes/validation.js +16 -1
- package/dist/mjs/traverse/problems.js +76 -44
- package/dist/mjs/traverse/traverse.js +37 -34
- package/dist/mjs/utils/data.js +24 -0
- package/dist/mjs/utils/freeze.js +2 -3
- package/dist/mjs/utils/generics.js +18 -1
- package/dist/mjs/utils/numericLiterals.js +1 -1
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/nodes/branch.d.ts +3 -12
- package/dist/types/nodes/discriminate.d.ts +6 -8
- package/dist/types/nodes/infer.d.ts +5 -5
- package/dist/types/nodes/node.d.ts +4 -13
- package/dist/types/nodes/predicate.d.ts +2 -29
- package/dist/types/nodes/rules/range.d.ts +2 -2
- package/dist/types/nodes/rules/rules.d.ts +1 -1
- package/dist/types/parse/ast/ast.d.ts +24 -0
- package/dist/types/parse/ast/bound.d.ts +10 -0
- package/dist/types/parse/{tuple/utils.d.ts → ast/distributableFunction.d.ts} +2 -3
- package/dist/types/parse/ast/divisor.d.ts +8 -0
- package/dist/types/parse/ast/intersection.d.ts +17 -0
- package/dist/types/parse/{tuple → ast}/keyof.d.ts +4 -4
- package/dist/types/parse/ast/morph.d.ts +18 -0
- package/dist/types/parse/ast/narrow.d.ts +21 -0
- package/dist/types/parse/ast/tuple.d.ts +40 -0
- package/dist/types/parse/ast/union.d.ts +12 -0
- package/dist/types/parse/ast/utils.d.ts +7 -0
- package/dist/types/parse/definition.d.ts +9 -7
- package/dist/types/parse/record.d.ts +2 -2
- package/dist/types/parse/string/reduce/shared.d.ts +5 -4
- package/dist/types/parse/string/reduce/static.d.ts +11 -6
- package/dist/types/parse/string/shift/operand/unenclosed.d.ts +1 -1
- package/dist/types/parse/string/shift/operator/bounds.d.ts +3 -2
- package/dist/types/parse/string/shift/operator/divisor.d.ts +5 -1
- package/dist/types/parse/string/shift/scanner.d.ts +1 -1
- package/dist/types/parse/string/string.d.ts +1 -2
- package/dist/types/scopes/expressions.d.ts +48 -0
- package/dist/types/scopes/scope.d.ts +17 -7
- package/dist/types/scopes/standard.d.ts +15 -0
- package/dist/types/scopes/type.d.ts +9 -5
- package/dist/types/scopes/validation.d.ts +4 -0
- package/dist/types/traverse/problems.d.ts +17 -24
- package/dist/types/traverse/traverse.d.ts +67 -37
- package/dist/types/utils/data.d.ts +13 -0
- package/dist/types/utils/domains.d.ts +1 -1
- package/dist/types/utils/generics.d.ts +14 -10
- package/dist/types/utils/numericLiterals.d.ts +16 -0
- package/dist/types/utils/paths.d.ts +2 -0
- package/package.json +1 -1
- package/dist/cjs/parse/tuple/keyof.js +0 -64
- package/dist/cjs/utils/size.js +0 -16
- package/dist/mjs/parse/tuple/keyof.js +0 -56
- package/dist/mjs/parse/tuple/narrow.js +0 -7
- package/dist/mjs/utils/size.js +0 -2
- package/dist/types/parse/string/ast.d.ts +0 -46
- package/dist/types/parse/tuple/morph.d.ts +0 -15
- package/dist/types/parse/tuple/narrow.d.ts +0 -13
- package/dist/types/parse/tuple/tuple.d.ts +0 -31
- package/dist/types/utils/size.d.ts +0 -3
|
@@ -21,10 +21,10 @@ const _keyofTs = require("./keyof.js");
|
|
|
21
21
|
const _morphTs = require("./morph.js");
|
|
22
22
|
const _narrowTs = require("./narrow.js");
|
|
23
23
|
const parseTuple = (def, ctx)=>{
|
|
24
|
-
if (
|
|
25
|
-
return
|
|
24
|
+
if (isIndexOneExpression(def)) {
|
|
25
|
+
return indexOneParsers[def[1]](def, ctx);
|
|
26
26
|
}
|
|
27
|
-
if (
|
|
27
|
+
if (isIndexZeroExpression(def)) {
|
|
28
28
|
return prefixParsers[def[0]](def, ctx);
|
|
29
29
|
}
|
|
30
30
|
const props = {
|
|
@@ -58,17 +58,18 @@ const parseBranchTuple = (def, ctx)=>{
|
|
|
58
58
|
}
|
|
59
59
|
const l = (0, _definitionTs.parseDefinition)(def[0], ctx);
|
|
60
60
|
const r = (0, _definitionTs.parseDefinition)(def[2], ctx);
|
|
61
|
-
return def[1] === "&" ? (0, _nodeTs.
|
|
61
|
+
return def[1] === "&" ? (0, _nodeTs.rootIntersection)(l, r, ctx.type) : (0, _nodeTs.rootUnion)(l, r, ctx.type);
|
|
62
62
|
};
|
|
63
|
-
const parseArrayTuple = (def, scope)=>(0, _nodeTs.
|
|
63
|
+
const parseArrayTuple = (def, scope)=>(0, _nodeTs.toArrayNode)((0, _definitionTs.parseDefinition)(def[0], scope));
|
|
64
64
|
const writeMalformedFunctionalExpressionMessage = (operator, rightDef)=>`Expression requires a function following '${operator}' (was ${typeof rightDef})`;
|
|
65
|
-
const
|
|
66
|
-
const
|
|
65
|
+
const isIndexOneExpression = (def)=>indexOneParsers[def[1]] !== undefined;
|
|
66
|
+
const indexOneParsers = {
|
|
67
67
|
"|": parseBranchTuple,
|
|
68
68
|
"&": parseBranchTuple,
|
|
69
69
|
"[]": parseArrayTuple,
|
|
70
|
-
"
|
|
71
|
-
"
|
|
70
|
+
"=>": _narrowTs.parseNarrowTuple,
|
|
71
|
+
"|>": _morphTs.parseMorphTuple,
|
|
72
|
+
":": ()=>({})
|
|
72
73
|
};
|
|
73
74
|
const prefixParsers = {
|
|
74
75
|
keyof: _keyofTs.parseKeyOfTuple,
|
|
@@ -89,4 +90,4 @@ const prefixParsers = {
|
|
|
89
90
|
}),
|
|
90
91
|
node: (def)=>def[1]
|
|
91
92
|
};
|
|
92
|
-
const
|
|
93
|
+
const isIndexZeroExpression = (def)=>prefixParsers[def[0]] !== undefined;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "writeUndiscriminatableMorphUnionMessage", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: ()=>writeUndiscriminatableMorphUnionMessage
|
|
8
|
+
});
|
|
9
|
+
const writeUndiscriminatableMorphUnionMessage = (path)=>`${path === "/" ? "A" : `At ${path}, a`} union including one or more morphs must be discriminatable`;
|
|
@@ -19,9 +19,9 @@ const _domainsTs = require("../utils/domains.js");
|
|
|
19
19
|
const _errorsTs = require("../utils/errors.js");
|
|
20
20
|
const _objectKindsTs = require("../utils/objectKinds.js");
|
|
21
21
|
const _serializeTs = require("../utils/serialize.js");
|
|
22
|
+
const _tupleTs = require("./ast/tuple.js");
|
|
22
23
|
const _recordTs = require("./record.js");
|
|
23
24
|
const _stringTs = require("./string/string.js");
|
|
24
|
-
const _tupleTs = require("./tuple/tuple.js");
|
|
25
25
|
const parseDefinition = (def, ctx)=>{
|
|
26
26
|
const domain = (0, _domainsTs.domainOf)(def);
|
|
27
27
|
if (domain === "string") {
|
|
@@ -43,11 +43,21 @@ const parseDefinition = (def, ctx)=>{
|
|
|
43
43
|
}
|
|
44
44
|
};
|
|
45
45
|
case "Function":
|
|
46
|
-
|
|
46
|
+
if ((0, _typeTs.isType)(def)) {
|
|
47
|
+
return def.node;
|
|
48
|
+
}
|
|
49
|
+
if (isThunk(def)) {
|
|
50
|
+
const returned = def();
|
|
51
|
+
if ((0, _typeTs.isType)(returned)) {
|
|
52
|
+
return returned.node;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return (0, _errorsTs.throwParseError)(writeBadDefinitionTypeMessage("Function"));
|
|
47
56
|
default:
|
|
48
57
|
return (0, _errorsTs.throwParseError)(writeBadDefinitionTypeMessage(objectKind ?? (0, _serializeTs.stringify)(def)));
|
|
49
58
|
}
|
|
50
59
|
};
|
|
51
60
|
const as = Symbol("as");
|
|
52
61
|
const unknownDefinitionMessage = "Cannot statically parse a definition inferred as unknown. Consider using 'as inferred<...>' to cast it.";
|
|
62
|
+
const isThunk = (def)=>typeof def === "function" && def.length === 0;
|
|
53
63
|
const writeBadDefinitionTypeMessage = (actual)=>`Type definitions must be strings or objects (was ${actual})`;
|
|
@@ -59,10 +59,10 @@ class DynamicState {
|
|
|
59
59
|
this.root = node;
|
|
60
60
|
}
|
|
61
61
|
rootToArray() {
|
|
62
|
-
this.root = (0, _nodeTs.
|
|
62
|
+
this.root = (0, _nodeTs.toArrayNode)(this.ejectRoot());
|
|
63
63
|
}
|
|
64
64
|
intersect(node) {
|
|
65
|
-
this.root = (0, _nodeTs.
|
|
65
|
+
this.root = (0, _nodeTs.rootIntersection)(this.ejectRoot(), node, this.ctx.type);
|
|
66
66
|
}
|
|
67
67
|
ejectRoot() {
|
|
68
68
|
this.assertHasRoot();
|
|
@@ -89,7 +89,7 @@ class DynamicState {
|
|
|
89
89
|
return this.error((0, _sharedTs.writeUnpairableComparatorMessage)(comparator));
|
|
90
90
|
}
|
|
91
91
|
if (this.branches.range) {
|
|
92
|
-
return this.error((0, _sharedTs.writeMultipleLeftBoundsMessage)(this.branches.range.limit
|
|
92
|
+
return this.error((0, _sharedTs.writeMultipleLeftBoundsMessage)(`${this.branches.range.limit}`, this.branches.range.comparator, `${limit}`, invertedComparator));
|
|
93
93
|
}
|
|
94
94
|
this.branches.range = {
|
|
95
95
|
limit,
|
|
@@ -102,7 +102,7 @@ class DynamicState {
|
|
|
102
102
|
this.pushRootToBranch("|");
|
|
103
103
|
this.setRoot(this.branches.union);
|
|
104
104
|
} else if (this.branches.intersection) {
|
|
105
|
-
this.setRoot((0, _nodeTs.
|
|
105
|
+
this.setRoot((0, _nodeTs.rootIntersection)(this.branches.intersection, this.ejectRoot(), this.ctx.type));
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
108
|
finalizeGroup() {
|
|
@@ -115,15 +115,15 @@ class DynamicState {
|
|
|
115
115
|
}
|
|
116
116
|
pushRootToBranch(token) {
|
|
117
117
|
this.assertRangeUnset();
|
|
118
|
-
this.branches.intersection = this.branches.intersection ? (0, _nodeTs.
|
|
118
|
+
this.branches.intersection = this.branches.intersection ? (0, _nodeTs.rootIntersection)(this.branches.intersection, this.ejectRoot(), this.ctx.type) : this.ejectRoot();
|
|
119
119
|
if (token === "|") {
|
|
120
|
-
this.branches.union = this.branches.union ? (0, _nodeTs.
|
|
120
|
+
this.branches.union = this.branches.union ? (0, _nodeTs.rootUnion)(this.branches.union, this.branches.intersection, this.ctx.type) : this.branches.intersection;
|
|
121
121
|
delete this.branches.intersection;
|
|
122
122
|
}
|
|
123
123
|
}
|
|
124
124
|
assertRangeUnset() {
|
|
125
125
|
if (this.branches.range) {
|
|
126
|
-
return this.error((0, _sharedTs.writeOpenRangeMessage)(this.branches.range.limit
|
|
126
|
+
return this.error((0, _sharedTs.writeOpenRangeMessage)(`${this.branches.range.limit}`, this.branches.range.comparator));
|
|
127
127
|
}
|
|
128
128
|
}
|
|
129
129
|
reduceGroupOpen() {
|
|
@@ -20,7 +20,7 @@ const _rangeTs = require("../../../../nodes/rules/range.js");
|
|
|
20
20
|
const _errorsTs = require("../../../../utils/errors.js");
|
|
21
21
|
const _genericsTs = require("../../../../utils/generics.js");
|
|
22
22
|
const _numericLiteralsTs = require("../../../../utils/numericLiterals.js");
|
|
23
|
-
const
|
|
23
|
+
const _boundTs = require("../../../ast/bound.js");
|
|
24
24
|
const _sharedTs = require("../../reduce/shared.js");
|
|
25
25
|
const _scannerTs = require("../scanner.js");
|
|
26
26
|
const parseBound = (s, start)=>{
|
|
@@ -53,7 +53,7 @@ const parseRightBound = (s, comparator)=>{
|
|
|
53
53
|
};
|
|
54
54
|
const distributeRange = (range, s)=>{
|
|
55
55
|
const resolution = s.resolveRoot();
|
|
56
|
-
const domains = (0, _genericsTs.
|
|
56
|
+
const domains = (0, _genericsTs.objectKeysOf)(resolution);
|
|
57
57
|
const distributedRange = {};
|
|
58
58
|
const rangePredicate = {
|
|
59
59
|
range
|
|
@@ -71,13 +71,13 @@ const distributeRange = (range, s)=>{
|
|
|
71
71
|
if (resolution.object === true) {
|
|
72
72
|
return false;
|
|
73
73
|
}
|
|
74
|
-
return (0, _genericsTs.listFrom)(resolution.object).every((branch)=>
|
|
74
|
+
return (0, _genericsTs.listFrom)(resolution.object).every((branch)=>"class" in branch && branch.class === "Array");
|
|
75
75
|
default:
|
|
76
76
|
return false;
|
|
77
77
|
}
|
|
78
78
|
});
|
|
79
79
|
if (!isBoundable) {
|
|
80
|
-
s.error((0,
|
|
80
|
+
s.error((0, _boundTs.writeUnboundableMessage)(s.rootToString()));
|
|
81
81
|
}
|
|
82
82
|
return distributedRange;
|
|
83
83
|
};
|
|
@@ -14,14 +14,14 @@ _export(exports, {
|
|
|
14
14
|
});
|
|
15
15
|
const _genericsTs = require("../../../../utils/generics.js");
|
|
16
16
|
const _numericLiteralsTs = require("../../../../utils/numericLiterals.js");
|
|
17
|
-
const
|
|
17
|
+
const _divisorTs = require("../../../ast/divisor.js");
|
|
18
18
|
const parseDivisor = (s)=>{
|
|
19
19
|
const divisorToken = s.scanner.shiftUntilNextTerminator();
|
|
20
20
|
const divisor = (0, _numericLiteralsTs.tryParseWellFormedInteger)(divisorToken, writeInvalidDivisorMessage(divisorToken));
|
|
21
21
|
if (divisor === 0) {
|
|
22
22
|
s.error(writeInvalidDivisorMessage(0));
|
|
23
23
|
}
|
|
24
|
-
const rootDomains = (0, _genericsTs.
|
|
24
|
+
const rootDomains = (0, _genericsTs.objectKeysOf)(s.resolveRoot());
|
|
25
25
|
if (rootDomains.length === 1 && rootDomains[0] === "number") {
|
|
26
26
|
s.intersect({
|
|
27
27
|
number: {
|
|
@@ -29,7 +29,7 @@ const parseDivisor = (s)=>{
|
|
|
29
29
|
}
|
|
30
30
|
});
|
|
31
31
|
} else {
|
|
32
|
-
s.error((0,
|
|
32
|
+
s.error((0, _divisorTs.writeIndivisibleMessage)(s.rootToString()));
|
|
33
33
|
}
|
|
34
34
|
};
|
|
35
35
|
const writeInvalidDivisorMessage = (divisor)=>`% operator must be followed by a non-zero integer literal (was ${divisor})`;
|
|
@@ -25,7 +25,7 @@ const maybeNaiveParse = (def, ctx)=>{
|
|
|
25
25
|
if (def.endsWith("[]")) {
|
|
26
26
|
const elementDef = def.slice(0, -2);
|
|
27
27
|
if (ctx.type.meta.scope.addReferenceIfResolvable(def, ctx)) {
|
|
28
|
-
return (0, _nodeTs.
|
|
28
|
+
return (0, _nodeTs.toArrayNode)(elementDef);
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
intersection: ()=>intersection,
|
|
13
|
+
union: ()=>union,
|
|
14
|
+
arrayOf: ()=>arrayOf,
|
|
15
|
+
keyOf: ()=>keyOf,
|
|
16
|
+
instanceOf: ()=>instanceOf,
|
|
17
|
+
valueOf: ()=>valueOf,
|
|
18
|
+
narrow: ()=>narrow,
|
|
19
|
+
morph: ()=>morph
|
|
20
|
+
});
|
|
21
|
+
const _standardTs = require("./standard.js");
|
|
22
|
+
const intersection = _standardTs.scopes.standard.intersection;
|
|
23
|
+
const union = _standardTs.scopes.standard.union;
|
|
24
|
+
const arrayOf = _standardTs.scopes.standard.arrayOf;
|
|
25
|
+
const keyOf = _standardTs.scopes.standard.keyOf;
|
|
26
|
+
const instanceOf = _standardTs.scopes.standard.instanceOf;
|
|
27
|
+
const valueOf = _standardTs.scopes.standard.valueOf;
|
|
28
|
+
const narrow = _standardTs.scopes.standard.narrow;
|
|
29
|
+
const morph = _standardTs.scopes.standard.morph;
|
package/dist/cjs/scopes/scope.js
CHANGED
|
@@ -10,14 +10,15 @@ function _export(target, all) {
|
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
12
|
Scope: ()=>Scope,
|
|
13
|
-
writeShallowCycleErrorMessage: ()=>writeShallowCycleErrorMessage,
|
|
14
13
|
scope: ()=>scope,
|
|
14
|
+
writeShallowCycleErrorMessage: ()=>writeShallowCycleErrorMessage,
|
|
15
15
|
writeDuplicateAliasesMessage: ()=>writeDuplicateAliasesMessage
|
|
16
16
|
});
|
|
17
17
|
const _nodeTs = require("../nodes/node.js");
|
|
18
18
|
const _definitionTs = require("../parse/definition.js");
|
|
19
19
|
const _chainableNoOpProxyTs = require("../utils/chainableNoOpProxy.js");
|
|
20
20
|
const _errorsTs = require("../utils/errors.js");
|
|
21
|
+
const _freezeTs = require("../utils/freeze.js");
|
|
21
22
|
const _pathsTs = require("../utils/paths.js");
|
|
22
23
|
const _cacheTs = require("./cache.js");
|
|
23
24
|
const _typeTs = require("./type.js");
|
|
@@ -159,7 +160,57 @@ class Scope {
|
|
|
159
160
|
_classPrivateFieldSet(this, _resolutions, new _cacheTs.Cache());
|
|
160
161
|
_classPrivateFieldSet(this, _exports, new _cacheTs.Cache());
|
|
161
162
|
_classPrivateFieldSet(this, _anonymousTypeCount, 0);
|
|
162
|
-
this.
|
|
163
|
+
this.expressions = {
|
|
164
|
+
intersection: (l, r, opts)=>this.type([
|
|
165
|
+
l,
|
|
166
|
+
"&",
|
|
167
|
+
r
|
|
168
|
+
], opts),
|
|
169
|
+
union: (l, r, opts)=>this.type([
|
|
170
|
+
l,
|
|
171
|
+
"|",
|
|
172
|
+
r
|
|
173
|
+
], opts),
|
|
174
|
+
arrayOf: (def, opts)=>this.type([
|
|
175
|
+
def,
|
|
176
|
+
"[]"
|
|
177
|
+
], opts),
|
|
178
|
+
keyOf: (def, opts)=>this.type([
|
|
179
|
+
"keyof",
|
|
180
|
+
def
|
|
181
|
+
], opts),
|
|
182
|
+
fromNode: (def, opts)=>this.type([
|
|
183
|
+
"node",
|
|
184
|
+
def
|
|
185
|
+
], opts),
|
|
186
|
+
instanceOf: (def, opts)=>this.type([
|
|
187
|
+
"instanceof",
|
|
188
|
+
def
|
|
189
|
+
], opts),
|
|
190
|
+
valueOf: (def, opts)=>this.type([
|
|
191
|
+
"===",
|
|
192
|
+
def
|
|
193
|
+
], opts),
|
|
194
|
+
narrow: (def, fn, opts)=>this.type([
|
|
195
|
+
def,
|
|
196
|
+
"=>",
|
|
197
|
+
fn
|
|
198
|
+
], opts),
|
|
199
|
+
morph: (def, fn, opts)=>this.type([
|
|
200
|
+
def,
|
|
201
|
+
"|>",
|
|
202
|
+
fn
|
|
203
|
+
], opts)
|
|
204
|
+
};
|
|
205
|
+
this.intersection = this.expressions.intersection;
|
|
206
|
+
this.union = this.expressions.union;
|
|
207
|
+
this.arrayOf = this.expressions.arrayOf;
|
|
208
|
+
this.keyOf = this.expressions.keyOf;
|
|
209
|
+
this.valueOf = this.expressions.valueOf;
|
|
210
|
+
this.instanceOf = this.expressions.instanceOf;
|
|
211
|
+
this.narrow = this.expressions.narrow;
|
|
212
|
+
this.morph = this.expressions.morph;
|
|
213
|
+
this.type = Object.assign((def, opts = {})=>{
|
|
163
214
|
if (opts.name && this.aliases[opts.name]) {
|
|
164
215
|
return (0, _errorsTs.throwParseError)(writeDuplicateAliasesMessage(opts.name));
|
|
165
216
|
}
|
|
@@ -168,7 +219,9 @@ class Scope {
|
|
|
168
219
|
result.node = this.resolveNode((0, _definitionTs.parseDefinition)(def, ctx));
|
|
169
220
|
result.flat = (0, _nodeTs.flattenType)(result);
|
|
170
221
|
return result;
|
|
171
|
-
}
|
|
222
|
+
}, {
|
|
223
|
+
from: this.expressions.fromNode
|
|
224
|
+
});
|
|
172
225
|
this.name = _classPrivateMethodGet(this, _register, register).call(this, opts);
|
|
173
226
|
if (opts.standard !== false) {
|
|
174
227
|
_classPrivateMethodGet(this, _cacheSpaces, cacheSpaces).call(this, [
|
|
@@ -232,13 +285,13 @@ function resolveRecurse(name, onUnresolvable, seen) {
|
|
|
232
285
|
seen.push(resolution);
|
|
233
286
|
resolution = _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, resolution, "throw", seen).node;
|
|
234
287
|
}
|
|
235
|
-
type.node = resolution;
|
|
236
|
-
type.flat = (0, _nodeTs.flattenType)(type);
|
|
288
|
+
type.node = (0, _freezeTs.deepFreeze)(resolution);
|
|
289
|
+
type.flat = (0, _freezeTs.deepFreeze)((0, _nodeTs.flattenType)(type));
|
|
237
290
|
return type;
|
|
238
291
|
}
|
|
292
|
+
const scope = (aliases, opts = {})=>new Scope(aliases, opts);
|
|
239
293
|
const writeShallowCycleErrorMessage = (name, seen)=>`Alias '${name}' has a shallow resolution cycle: ${[
|
|
240
294
|
...seen,
|
|
241
295
|
name
|
|
242
296
|
].join("=>")}`;
|
|
243
|
-
const scope = (aliases, opts = {})=>new Scope(aliases, opts);
|
|
244
297
|
const writeDuplicateAliasesMessage = (name)=>`Alias '${name}' is already defined`;
|
|
@@ -13,6 +13,7 @@ _export(exports, {
|
|
|
13
13
|
validationScope: ()=>validationScope,
|
|
14
14
|
validation: ()=>validation
|
|
15
15
|
});
|
|
16
|
+
const _numericLiteralsTs = require("../utils/numericLiterals.js");
|
|
16
17
|
const _scopeTs = require("./scope.js");
|
|
17
18
|
const isLuhnValid = (creditCardInput)=>{
|
|
18
19
|
const sanitized = creditCardInput.replace(/[- ]+/g, "");
|
|
@@ -44,11 +45,25 @@ const validationScope = (0, _scopeTs.scope)({
|
|
|
44
45
|
lowercase: /^[a-z]*$/,
|
|
45
46
|
uppercase: /^[A-Z]*$/,
|
|
46
47
|
// https://github.com/validatorjs/validator.js
|
|
47
|
-
creditCard:
|
|
48
|
+
creditCard: [
|
|
49
|
+
/^(?:4[0-9]{12}(?:[0-9]{3,6})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12,15}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11}|6[27][0-9]{14}|^(81[0-9]{14,17}))$/,
|
|
50
|
+
"=>",
|
|
51
|
+
(s, problems)=>isLuhnValid(s) || !problems.add("mustBe", "a valid credit card number")
|
|
52
|
+
],
|
|
48
53
|
// https://www.regular-expressions.info
|
|
49
54
|
email: /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/,
|
|
50
55
|
// https://github.com/validatorjs/validator.js
|
|
51
56
|
uuid: /^[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}$/,
|
|
57
|
+
parsedNumber: [
|
|
58
|
+
_numericLiteralsTs.wellFormedNumberMatcher,
|
|
59
|
+
"|>",
|
|
60
|
+
(s)=>parseFloat(s)
|
|
61
|
+
],
|
|
62
|
+
parsedInteger: [
|
|
63
|
+
_numericLiteralsTs.wellFormedIntegerMatcher,
|
|
64
|
+
"|>",
|
|
65
|
+
(s)=>parseInt(s)
|
|
66
|
+
],
|
|
52
67
|
integer: [
|
|
53
68
|
"node",
|
|
54
69
|
{
|
|
@@ -12,17 +12,58 @@ _export(exports, {
|
|
|
12
12
|
ArkTypeError: ()=>ArkTypeError,
|
|
13
13
|
Problem: ()=>Problem,
|
|
14
14
|
Problems: ()=>Problems,
|
|
15
|
-
|
|
15
|
+
domainsToDescriptions: ()=>domainsToDescriptions,
|
|
16
|
+
objectKindsToDescriptions: ()=>objectKindsToDescriptions,
|
|
16
17
|
describeBranches: ()=>describeBranches,
|
|
17
|
-
DataWrapper: ()=>DataWrapper,
|
|
18
18
|
defaultProblemWriters: ()=>defaultProblemWriters
|
|
19
19
|
});
|
|
20
20
|
const _scannerTs = require("../parse/string/shift/scanner.js");
|
|
21
|
+
const _dataTs = require("../utils/data.js");
|
|
21
22
|
const _domainsTs = require("../utils/domains.js");
|
|
22
23
|
const _numericLiteralsTs = require("../utils/numericLiterals.js");
|
|
23
24
|
const _objectKindsTs = require("../utils/objectKinds.js");
|
|
24
25
|
const _pathsTs = require("../utils/paths.js");
|
|
25
26
|
const _serializeTs = require("../utils/serialize.js");
|
|
27
|
+
function _checkPrivateRedeclaration(obj, privateCollection) {
|
|
28
|
+
if (privateCollection.has(obj)) {
|
|
29
|
+
throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function _classApplyDescriptorGet(receiver, descriptor) {
|
|
33
|
+
if (descriptor.get) {
|
|
34
|
+
return descriptor.get.call(receiver);
|
|
35
|
+
}
|
|
36
|
+
return descriptor.value;
|
|
37
|
+
}
|
|
38
|
+
function _classApplyDescriptorSet(receiver, descriptor, value) {
|
|
39
|
+
if (descriptor.set) {
|
|
40
|
+
descriptor.set.call(receiver, value);
|
|
41
|
+
} else {
|
|
42
|
+
if (!descriptor.writable) {
|
|
43
|
+
throw new TypeError("attempted to set read only private field");
|
|
44
|
+
}
|
|
45
|
+
descriptor.value = value;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function _classExtractFieldDescriptor(receiver, privateMap, action) {
|
|
49
|
+
if (!privateMap.has(receiver)) {
|
|
50
|
+
throw new TypeError("attempted to " + action + " private field on non-instance");
|
|
51
|
+
}
|
|
52
|
+
return privateMap.get(receiver);
|
|
53
|
+
}
|
|
54
|
+
function _classPrivateFieldGet(receiver, privateMap) {
|
|
55
|
+
var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get");
|
|
56
|
+
return _classApplyDescriptorGet(receiver, descriptor);
|
|
57
|
+
}
|
|
58
|
+
function _classPrivateFieldInit(obj, privateMap, value) {
|
|
59
|
+
_checkPrivateRedeclaration(obj, privateMap);
|
|
60
|
+
privateMap.set(obj, value);
|
|
61
|
+
}
|
|
62
|
+
function _classPrivateFieldSet(receiver, privateMap, value) {
|
|
63
|
+
var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set");
|
|
64
|
+
_classApplyDescriptorSet(receiver, descriptor, value);
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
26
67
|
var _definitions_code, _definitions_code1;
|
|
27
68
|
class ArkTypeError extends TypeError {
|
|
28
69
|
constructor(problems){
|
|
@@ -38,7 +79,7 @@ class Problem {
|
|
|
38
79
|
return this.writers.addContext(this.reason, this.path);
|
|
39
80
|
}
|
|
40
81
|
get reason() {
|
|
41
|
-
return this.writers.writeReason(this.mustBe, new DataWrapper(this.data));
|
|
82
|
+
return this.writers.writeReason(this.mustBe, new _dataTs.DataWrapper(this.data));
|
|
42
83
|
}
|
|
43
84
|
get mustBe() {
|
|
44
85
|
return this.writers.mustBe(this.source);
|
|
@@ -54,21 +95,29 @@ class Problem {
|
|
|
54
95
|
}
|
|
55
96
|
}
|
|
56
97
|
}
|
|
98
|
+
var _state = /*#__PURE__*/ new WeakMap();
|
|
57
99
|
class ProblemArray extends Array {
|
|
58
|
-
add(code,
|
|
100
|
+
add(code, source, opts) {
|
|
59
101
|
// copy the path to avoid future mutations affecting it
|
|
60
|
-
const path = opts?.path ?? _pathsTs.Path.from(this.
|
|
61
|
-
const
|
|
62
|
-
|
|
102
|
+
const path = opts?.path ?? _pathsTs.Path.from(_classPrivateFieldGet(this, _state).path);
|
|
103
|
+
const data = // we have to check for the presence of the key explicitly since the
|
|
104
|
+
// data could be undefined or null
|
|
105
|
+
opts && "data" in opts ? opts.data : _classPrivateFieldGet(this, _state).data;
|
|
106
|
+
const problem = new Problem(code, path, data, source, _classPrivateFieldGet(this, _state).getConfigForProblemCode(code));
|
|
107
|
+
this.addProblem(problem);
|
|
108
|
+
return problem;
|
|
109
|
+
}
|
|
110
|
+
addProblem(problem) {
|
|
111
|
+
const pathKey = `${problem.path}`;
|
|
63
112
|
const existing = this.byPath[pathKey];
|
|
64
113
|
if (existing) {
|
|
65
114
|
if (existing.parts) {
|
|
66
115
|
existing.parts.push(problem);
|
|
67
116
|
} else {
|
|
68
|
-
const problemIntersection = new Problem("multi", existing.path, data, [
|
|
117
|
+
const problemIntersection = new Problem("multi", existing.path, existing.data, [
|
|
69
118
|
existing,
|
|
70
119
|
problem
|
|
71
|
-
], this.
|
|
120
|
+
], _classPrivateFieldGet(this, _state).getConfigForProblemCode("multi"));
|
|
72
121
|
const existingIndex = this.indexOf(existing);
|
|
73
122
|
// If existing is found (which it always should be unless this was externally mutated),
|
|
74
123
|
// replace it with the new problem intersection. In case it isn't for whatever reason,
|
|
@@ -80,7 +129,7 @@ class ProblemArray extends Array {
|
|
|
80
129
|
this.byPath[pathKey] = problem;
|
|
81
130
|
this.push(problem);
|
|
82
131
|
}
|
|
83
|
-
|
|
132
|
+
this.count++;
|
|
84
133
|
}
|
|
85
134
|
get summary() {
|
|
86
135
|
return this.join("\n");
|
|
@@ -93,22 +142,26 @@ class ProblemArray extends Array {
|
|
|
93
142
|
}
|
|
94
143
|
constructor(state){
|
|
95
144
|
super();
|
|
96
|
-
this.state = state;
|
|
97
145
|
this.byPath = {};
|
|
146
|
+
this.count = 0;
|
|
147
|
+
_classPrivateFieldInit(this, _state, {
|
|
148
|
+
writable: true,
|
|
149
|
+
value: void 0
|
|
150
|
+
});
|
|
151
|
+
_classPrivateFieldSet(this, _state, state);
|
|
98
152
|
}
|
|
99
153
|
}
|
|
100
154
|
const Problems = ProblemArray;
|
|
101
155
|
const capitalize = (s)=>s[0].toUpperCase() + s.slice(1);
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
if (domains.length === 0) {
|
|
156
|
+
const domainsToDescriptions = (domains)=>domains.map((objectKind)=>_domainsTs.domainDescriptions[objectKind]);
|
|
157
|
+
const objectKindsToDescriptions = (kinds)=>kinds.map((objectKind)=>_objectKindsTs.objectKindDescriptions[objectKind]);
|
|
158
|
+
const describeBranches = (descriptions)=>{
|
|
159
|
+
if (descriptions.length === 0) {
|
|
107
160
|
return "never";
|
|
108
161
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
162
|
+
if (descriptions.length === 1) {
|
|
163
|
+
return descriptions[0];
|
|
164
|
+
}
|
|
112
165
|
let description = "";
|
|
113
166
|
for(let i = 0; i < descriptions.length - 1; i++){
|
|
114
167
|
description += descriptions[i];
|
|
@@ -119,26 +172,6 @@ const describeBranches = (descriptions)=>{
|
|
|
119
172
|
description += ` or ${descriptions[descriptions.length - 1]}`;
|
|
120
173
|
return description;
|
|
121
174
|
};
|
|
122
|
-
class DataWrapper {
|
|
123
|
-
toString() {
|
|
124
|
-
return (0, _serializeTs.stringify)(this.value);
|
|
125
|
-
}
|
|
126
|
-
get domain() {
|
|
127
|
-
return (0, _domainsTs.domainOf)(this.value);
|
|
128
|
-
}
|
|
129
|
-
get size() {
|
|
130
|
-
return typeof this.value === "string" || Array.isArray(this.value) ? this.value.length : typeof this.value === "number" ? this.value : 0;
|
|
131
|
-
}
|
|
132
|
-
get units() {
|
|
133
|
-
return typeof this.value === "string" ? "characters" : Array.isArray(this.value) ? "items" : "";
|
|
134
|
-
}
|
|
135
|
-
get className() {
|
|
136
|
-
return Object(this.value).constructor.name;
|
|
137
|
-
}
|
|
138
|
-
constructor(value){
|
|
139
|
-
this.value = value;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
175
|
const writeDefaultReason = (mustBe, was)=>`must be ${mustBe}${was && ` (was ${was})`}`;
|
|
143
176
|
const addDefaultContext = (reason, path)=>path.length === 0 ? capitalize(reason) : path.length === 1 && (0, _numericLiteralsTs.isWellFormedInteger)(path[0]) ? `Item at index ${path[0]} ${reason}` : `${path} ${reason}`;
|
|
144
177
|
const compileDefaultProblemWriters = (definitions)=>{
|
|
@@ -161,10 +194,6 @@ const defaultProblemWriters = compileDefaultProblemWriters({
|
|
|
161
194
|
mustBe: (domain)=>_domainsTs.domainDescriptions[domain],
|
|
162
195
|
writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.domain)
|
|
163
196
|
},
|
|
164
|
-
domainBranches: {
|
|
165
|
-
mustBe: (domains)=>describeDomains(domains),
|
|
166
|
-
writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.domain)
|
|
167
|
-
},
|
|
168
197
|
missing: {
|
|
169
198
|
mustBe: ()=>"defined",
|
|
170
199
|
writeReason: (mustBe)=>writeDefaultReason(mustBe, "")
|
|
@@ -179,9 +208,6 @@ const defaultProblemWriters = compileDefaultProblemWriters({
|
|
|
179
208
|
value: {
|
|
180
209
|
mustBe: _serializeTs.stringify
|
|
181
210
|
},
|
|
182
|
-
valueBranches: {
|
|
183
|
-
mustBe: (values)=>describeBranches(values.map(_serializeTs.stringify))
|
|
184
|
-
},
|
|
185
211
|
branches: {
|
|
186
212
|
mustBe: (branchProblems)=>describeBranches(branchProblems.map((problem)=>`${problem.path} must be ${problem.parts ? describeBranches(problem.parts.map((part)=>part.mustBe)) : problem.mustBe}`)),
|
|
187
213
|
writeReason: (mustBe, data)=>`${mustBe} (was ${data})`,
|
|
@@ -191,5 +217,11 @@ const defaultProblemWriters = compileDefaultProblemWriters({
|
|
|
191
217
|
mustBe: (problems)=>"• " + problems.map((_)=>_.mustBe).join("\n• "),
|
|
192
218
|
writeReason: (mustBe, data)=>`${data} must be...\n${mustBe}`,
|
|
193
219
|
addContext: (reason, path)=>path.length ? `At ${path}, ${reason}` : reason
|
|
220
|
+
},
|
|
221
|
+
mustBe: {
|
|
222
|
+
mustBe: (mustBe)=>mustBe
|
|
223
|
+
},
|
|
224
|
+
cases: {
|
|
225
|
+
mustBe: (cases)=>describeBranches(cases)
|
|
194
226
|
}
|
|
195
227
|
});
|