arktype 0.1.4 → 0.3.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/README.md +1 -1
- package/dist/api.cjs +4 -3
- package/dist/api.d.ts +3 -2
- package/dist/api.mjs +2 -1
- package/dist/cjs/nodes/{branches.js → branch.js} +56 -10
- package/dist/cjs/nodes/compose.js +1 -2
- package/dist/cjs/nodes/discriminate.js +7 -13
- package/dist/cjs/nodes/node.js +32 -4
- package/dist/cjs/nodes/predicate.js +7 -7
- package/dist/cjs/nodes/rules/class.js +10 -4
- package/dist/cjs/nodes/rules/divisor.js +1 -5
- package/dist/cjs/nodes/rules/props.js +75 -25
- package/dist/cjs/nodes/rules/range.js +1 -5
- package/dist/cjs/nodes/rules/regex.js +2 -44
- package/dist/cjs/nodes/rules/rules.js +21 -61
- package/dist/cjs/parse/definition.js +2 -2
- package/dist/cjs/parse/string/reduce/dynamic.js +1 -2
- package/dist/cjs/parse/string/shift/operator/bounds.js +1 -1
- package/dist/cjs/parse/string/string.js +2 -2
- package/dist/cjs/parse/tuple/keyof.js +37 -18
- package/dist/cjs/parse/tuple/morph.js +7 -6
- package/dist/cjs/parse/tuple/tuple.js +16 -9
- package/dist/cjs/scopes/cache.js +39 -0
- package/dist/cjs/scopes/jsObjects.js +133 -0
- package/dist/cjs/{main.js → scopes/scope.js} +31 -364
- package/dist/cjs/scopes/standard.js +44 -0
- package/dist/cjs/scopes/tsKeywords.js +113 -0
- package/dist/cjs/scopes/type.js +67 -0
- package/dist/cjs/scopes/validation.js +64 -0
- package/dist/cjs/traverse/problems.js +55 -95
- package/dist/cjs/traverse/traverse.js +127 -134
- package/dist/cjs/utils/numericLiterals.js +3 -0
- package/dist/cjs/utils/objectKinds.js +1 -2
- package/dist/mjs/nodes/{branches.js → branch.js} +45 -2
- package/dist/mjs/nodes/compose.js +1 -2
- package/dist/mjs/nodes/discriminate.js +6 -12
- package/dist/mjs/nodes/node.js +30 -3
- package/dist/mjs/nodes/predicate.js +2 -2
- package/dist/mjs/nodes/rules/class.js +10 -4
- package/dist/mjs/nodes/rules/divisor.js +1 -5
- package/dist/mjs/nodes/rules/props.js +74 -27
- package/dist/mjs/nodes/rules/range.js +1 -5
- package/dist/mjs/nodes/rules/regex.js +1 -43
- package/dist/mjs/nodes/rules/rules.js +22 -61
- package/dist/mjs/parse/definition.js +1 -1
- package/dist/mjs/parse/string/reduce/dynamic.js +2 -3
- package/dist/mjs/parse/string/shift/operator/bounds.js +1 -1
- package/dist/mjs/parse/string/string.js +2 -2
- package/dist/mjs/parse/tuple/keyof.js +38 -19
- package/dist/mjs/parse/tuple/morph.js +7 -6
- package/dist/mjs/parse/tuple/tuple.js +17 -10
- package/dist/mjs/scopes/cache.js +25 -0
- package/dist/mjs/scopes/jsObjects.js +119 -0
- package/dist/mjs/{main.js → scopes/scope.js} +15 -344
- package/dist/mjs/scopes/standard.js +27 -0
- package/dist/mjs/scopes/tsKeywords.js +99 -0
- package/dist/mjs/scopes/type.js +53 -0
- package/dist/mjs/scopes/validation.js +51 -0
- package/dist/mjs/traverse/problems.js +55 -95
- package/dist/mjs/traverse/traverse.js +129 -135
- package/dist/mjs/utils/numericLiterals.js +2 -1
- package/dist/mjs/utils/objectKinds.js +1 -1
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/nodes/branch.d.ts +36 -0
- package/dist/types/nodes/compose.d.ts +6 -11
- package/dist/types/nodes/discriminate.d.ts +1 -1
- package/dist/types/nodes/infer.d.ts +16 -15
- package/dist/types/nodes/node.d.ts +18 -9
- package/dist/types/nodes/predicate.d.ts +18 -57
- package/dist/types/nodes/rules/class.d.ts +3 -2
- package/dist/types/nodes/rules/divisor.d.ts +2 -1
- package/dist/types/nodes/rules/props.d.ts +19 -11
- package/dist/types/nodes/rules/range.d.ts +2 -1
- package/dist/types/nodes/rules/regex.d.ts +2 -11
- package/dist/types/nodes/rules/rules.d.ts +11 -21
- package/dist/types/parse/definition.d.ts +1 -1
- package/dist/types/parse/string/ast.d.ts +4 -2
- package/dist/types/parse/string/reduce/shared.d.ts +1 -1
- package/dist/types/parse/string/reduce/static.d.ts +1 -1
- package/dist/types/parse/string/shift/operator/bounds.d.ts +1 -1
- package/dist/types/parse/string/string.d.ts +1 -1
- package/dist/types/parse/tuple/keyof.d.ts +2 -2
- package/dist/types/parse/tuple/morph.d.ts +1 -1
- package/dist/types/parse/tuple/narrow.d.ts +1 -1
- package/dist/types/parse/tuple/tuple.d.ts +1 -1
- package/dist/types/scopes/cache.d.ts +14 -0
- package/dist/types/scopes/jsObjects.d.ts +36 -0
- package/dist/types/scopes/scope.d.ts +76 -0
- package/dist/types/scopes/standard.d.ts +280 -0
- package/dist/types/scopes/tsKeywords.d.ts +32 -0
- package/dist/types/scopes/type.d.ts +47 -0
- package/dist/types/scopes/validation.d.ts +21 -0
- package/dist/types/traverse/problems.d.ts +35 -53
- package/dist/types/traverse/traverse.d.ts +30 -24
- package/dist/types/utils/generics.d.ts +0 -1
- package/dist/types/utils/numericLiterals.d.ts +2 -0
- package/dist/types/utils/objectKinds.d.ts +43 -43
- package/package.json +2 -3
- package/dist/cjs/nodes/functors.js +0 -35
- package/dist/cjs/nodes/rules/objectKind.js +0 -78
- package/dist/mjs/nodes/functors.js +0 -27
- package/dist/mjs/nodes/rules/objectKind.js +0 -63
- package/dist/types/main.d.ts +0 -324
- package/dist/types/nodes/branches.d.ts +0 -14
- package/dist/types/nodes/functors.d.ts +0 -6
- package/dist/types/nodes/rules/objectKind.d.ts +0 -15
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import { resolutionExtendsDomain } from "../../nodes/predicate.js";
|
|
2
|
+
import { mappedKeys } from "../../nodes/rules/props.js";
|
|
2
3
|
import { throwParseError } from "../../utils/errors.js";
|
|
3
|
-
import {
|
|
4
|
+
import { listFrom } from "../../utils/generics.js";
|
|
5
|
+
import { wellFormedNonNegativeIntegerMatcher } from "../../utils/numericLiterals.js";
|
|
4
6
|
import { parseDefinition } from "../definition.js";
|
|
5
7
|
import { writeImplicitNeverMessage } from "../string/ast.js";
|
|
8
|
+
const numericIndexKey = {
|
|
9
|
+
regex: wellFormedNonNegativeIntegerMatcher.source
|
|
10
|
+
};
|
|
6
11
|
export const parseKeyOfTuple = (def, ctx)=>{
|
|
7
12
|
const resolution = ctx.type.meta.scope.resolveNode(parseDefinition(def[1], ctx));
|
|
8
13
|
if (!resolutionExtendsDomain(resolution, "object")) {
|
|
@@ -11,27 +16,41 @@ export const parseKeyOfTuple = (def, ctx)=>{
|
|
|
11
16
|
if (resolution.object === true) {
|
|
12
17
|
return writeImplicitNeverMessage(ctx.path, "keyof");
|
|
13
18
|
}
|
|
14
|
-
const branchKeys = [];
|
|
15
19
|
const objectBranches = listFrom(resolution.object);
|
|
16
|
-
|
|
20
|
+
let sharedKeys = keysOfBranch(objectBranches[0]);
|
|
17
21
|
for(let i = 1; i < objectBranches.length; i++){
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
branchKeys.
|
|
22
|
+
const branchKeys = keysOfBranch(objectBranches[i]);
|
|
23
|
+
// we can filter directly by equality here because the RegExp we're
|
|
24
|
+
// using will always be reference equal to
|
|
25
|
+
// wellFormedNonNegativeIntegerMatcher
|
|
26
|
+
sharedKeys = sharedKeys.filter((k)=>branchKeys.includes(k));
|
|
23
27
|
}
|
|
24
|
-
const result =
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
value: key
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
});
|
|
28
|
+
const result = sharedKeys.map((k)=>typeof k === "string" ? {
|
|
29
|
+
value: k
|
|
30
|
+
} : {
|
|
31
|
+
regex: k.source
|
|
32
|
+
});
|
|
33
33
|
return result.length ? {
|
|
34
|
-
string: result
|
|
34
|
+
string: result.length === 1 ? result[0] : result
|
|
35
35
|
} : writeImplicitNeverMessage(ctx.path, "keyof");
|
|
36
36
|
};
|
|
37
|
-
const
|
|
37
|
+
const keysOfBranch = (branch)=>{
|
|
38
|
+
if (!("props" in branch)) {
|
|
39
|
+
return [];
|
|
40
|
+
}
|
|
41
|
+
if (branch.class !== "Array") {
|
|
42
|
+
return Object.keys(branch.props);
|
|
43
|
+
}
|
|
44
|
+
const arrayProps = [];
|
|
45
|
+
for (const key of Object.keys(branch.props)){
|
|
46
|
+
if (key === "length") {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (key === mappedKeys.index) {
|
|
50
|
+
arrayProps.push(wellFormedNonNegativeIntegerMatcher);
|
|
51
|
+
} else {
|
|
52
|
+
arrayProps.push(key);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return arrayProps;
|
|
56
|
+
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isMorphBranch } from "../../nodes/branch.js";
|
|
1
2
|
import { throwInternalError, throwParseError } from "../../utils/errors.js";
|
|
2
3
|
import { isArray } from "../../utils/objectKinds.js";
|
|
3
4
|
import { stringify } from "../../utils/serialize.js";
|
|
@@ -15,7 +16,7 @@ export const parseMorphTuple = (def, ctx)=>{
|
|
|
15
16
|
const predicate = resolution[domain];
|
|
16
17
|
if (predicate === true) {
|
|
17
18
|
result[domain] = {
|
|
18
|
-
|
|
19
|
+
rules: {},
|
|
19
20
|
morph
|
|
20
21
|
};
|
|
21
22
|
} else if (typeof predicate === "object") {
|
|
@@ -26,17 +27,17 @@ export const parseMorphTuple = (def, ctx)=>{
|
|
|
26
27
|
}
|
|
27
28
|
return result;
|
|
28
29
|
};
|
|
29
|
-
const applyMorph = (branch, morph)=>branch
|
|
30
|
-
|
|
31
|
-
morph: Array.isArray(branch.morph) ? [
|
|
30
|
+
const applyMorph = (branch, morph)=>isMorphBranch(branch) ? {
|
|
31
|
+
...branch,
|
|
32
|
+
morph: branch.morph ? Array.isArray(branch.morph) ? [
|
|
32
33
|
...branch.morph,
|
|
33
34
|
morph
|
|
34
35
|
] : [
|
|
35
36
|
branch.morph,
|
|
36
37
|
morph
|
|
37
|
-
]
|
|
38
|
+
] : morph
|
|
38
39
|
} : {
|
|
39
|
-
|
|
40
|
+
rules: branch,
|
|
40
41
|
morph
|
|
41
42
|
};
|
|
42
43
|
export const writeMalformedMorphExpressionMessage = (value)=>`Morph expression requires a function following '=>' (was ${typeof value})`;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { intersection, union } from "../../nodes/node.js";
|
|
1
|
+
import { arrayOf, intersection, union } from "../../nodes/node.js";
|
|
3
2
|
import { domainOf } from "../../utils/domains.js";
|
|
4
3
|
import { throwParseError } from "../../utils/errors.js";
|
|
5
4
|
import { parseDefinition } from "../definition.js";
|
|
@@ -14,7 +13,19 @@ export const parseTuple = (def, ctx)=>{
|
|
|
14
13
|
if (isPrefixExpression(def)) {
|
|
15
14
|
return prefixParsers[def[0]](def, ctx);
|
|
16
15
|
}
|
|
17
|
-
const props = {
|
|
16
|
+
const props = {
|
|
17
|
+
// length is created as a prerequisite prop, ensuring if it is invalid,
|
|
18
|
+
// no other props will be checked, which is usually desirable for tuple
|
|
19
|
+
// definitions.
|
|
20
|
+
length: [
|
|
21
|
+
"!",
|
|
22
|
+
{
|
|
23
|
+
number: {
|
|
24
|
+
value: def.length
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
};
|
|
18
29
|
for(let i = 0; i < def.length; i++){
|
|
19
30
|
ctx.path.push(`${i}`);
|
|
20
31
|
props[i] = parseDefinition(def[i], ctx);
|
|
@@ -22,12 +33,8 @@ export const parseTuple = (def, ctx)=>{
|
|
|
22
33
|
}
|
|
23
34
|
return {
|
|
24
35
|
object: {
|
|
25
|
-
|
|
26
|
-
props
|
|
27
|
-
range: {
|
|
28
|
-
comparator: "==",
|
|
29
|
-
limit: def.length
|
|
30
|
-
}
|
|
36
|
+
class: "Array",
|
|
37
|
+
props
|
|
31
38
|
}
|
|
32
39
|
};
|
|
33
40
|
};
|
|
@@ -39,7 +46,7 @@ const parseBranchTuple = (def, ctx)=>{
|
|
|
39
46
|
const r = parseDefinition(def[2], ctx);
|
|
40
47
|
return def[1] === "&" ? intersection(l, r, ctx.type) : union(l, r, ctx.type);
|
|
41
48
|
};
|
|
42
|
-
const parseArrayTuple = (def, scope)=>
|
|
49
|
+
const parseArrayTuple = (def, scope)=>arrayOf(parseDefinition(def[0], scope));
|
|
43
50
|
export const writeMalformedFunctionalExpressionMessage = (operator, rightDef)=>`Expression requires a function following '${operator}' (was ${typeof rightDef})`;
|
|
44
51
|
const isPostfixExpression = (def)=>postfixParsers[def[1]] !== undefined;
|
|
45
52
|
const postfixParsers = {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { deepFreeze } from "../utils/freeze.js";
|
|
2
|
+
export class Cache {
|
|
3
|
+
get root() {
|
|
4
|
+
return this.cache;
|
|
5
|
+
}
|
|
6
|
+
has(name) {
|
|
7
|
+
return name in this.cache;
|
|
8
|
+
}
|
|
9
|
+
get(name) {
|
|
10
|
+
return this.cache[name];
|
|
11
|
+
}
|
|
12
|
+
set(name, item) {
|
|
13
|
+
this.cache[name] = item;
|
|
14
|
+
return item;
|
|
15
|
+
}
|
|
16
|
+
constructor(){
|
|
17
|
+
this.cache = {};
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export class FreezingCache extends Cache {
|
|
21
|
+
set(name, item) {
|
|
22
|
+
this.cache[name] = deepFreeze(item);
|
|
23
|
+
return item;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { scope } from "./scope.js";
|
|
2
|
+
export const jsObjectsScope = scope({
|
|
3
|
+
Function: [
|
|
4
|
+
"node",
|
|
5
|
+
{
|
|
6
|
+
object: {
|
|
7
|
+
class: "Function"
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
],
|
|
11
|
+
Array: [
|
|
12
|
+
"node",
|
|
13
|
+
{
|
|
14
|
+
object: {
|
|
15
|
+
class: "Array"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
],
|
|
19
|
+
Date: [
|
|
20
|
+
"node",
|
|
21
|
+
{
|
|
22
|
+
object: {
|
|
23
|
+
class: "Date"
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
],
|
|
27
|
+
Error: [
|
|
28
|
+
"node",
|
|
29
|
+
{
|
|
30
|
+
object: {
|
|
31
|
+
class: "Error"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
],
|
|
35
|
+
Map: [
|
|
36
|
+
"node",
|
|
37
|
+
{
|
|
38
|
+
object: {
|
|
39
|
+
class: "Map"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
],
|
|
43
|
+
RegExp: [
|
|
44
|
+
"node",
|
|
45
|
+
{
|
|
46
|
+
object: {
|
|
47
|
+
class: "RegExp"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
],
|
|
51
|
+
Set: [
|
|
52
|
+
"node",
|
|
53
|
+
{
|
|
54
|
+
object: {
|
|
55
|
+
class: "Set"
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
],
|
|
59
|
+
Object: [
|
|
60
|
+
"node",
|
|
61
|
+
{
|
|
62
|
+
object: {
|
|
63
|
+
class: "Object"
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
],
|
|
67
|
+
String: [
|
|
68
|
+
"node",
|
|
69
|
+
{
|
|
70
|
+
object: {
|
|
71
|
+
class: "String"
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
],
|
|
75
|
+
Number: [
|
|
76
|
+
"node",
|
|
77
|
+
{
|
|
78
|
+
object: {
|
|
79
|
+
class: "Number"
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
],
|
|
83
|
+
Boolean: [
|
|
84
|
+
"node",
|
|
85
|
+
{
|
|
86
|
+
object: {
|
|
87
|
+
class: "Boolean"
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
],
|
|
91
|
+
WeakMap: [
|
|
92
|
+
"node",
|
|
93
|
+
{
|
|
94
|
+
object: {
|
|
95
|
+
class: "WeakMap"
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
],
|
|
99
|
+
WeakSet: [
|
|
100
|
+
"node",
|
|
101
|
+
{
|
|
102
|
+
object: {
|
|
103
|
+
class: "WeakSet"
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
],
|
|
107
|
+
Promise: [
|
|
108
|
+
"node",
|
|
109
|
+
{
|
|
110
|
+
object: {
|
|
111
|
+
class: "Promise"
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
]
|
|
115
|
+
}, {
|
|
116
|
+
name: "jsObjects",
|
|
117
|
+
standard: false
|
|
118
|
+
});
|
|
119
|
+
export const jsObjects = jsObjectsScope.compile();
|