arktype 0.3.0 → 0.4.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 +3 -3
- 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/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 +0 -0
- package/dist/cjs/parse/{tuple → ast}/narrow.js +2 -2
- package/dist/cjs/parse/{tuple → ast}/tuple.js +8 -8
- 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 +3 -3
- 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 +56 -4
- package/dist/cjs/traverse/problems.js +59 -29
- package/dist/cjs/traverse/traverse.js +24 -27
- package/dist/cjs/utils/data.js +39 -0
- package/dist/cjs/utils/generics.js +20 -2
- package/dist/mjs/nodes/branch.js +14 -5
- package/dist/mjs/nodes/compose.js +2 -2
- package/dist/mjs/nodes/discriminate.js +3 -3
- 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/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 +0 -0
- package/dist/mjs/parse/ast/narrow.js +7 -0
- package/dist/mjs/parse/{tuple → ast}/tuple.js +9 -9
- 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 +3 -3
- 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 +55 -3
- package/dist/mjs/traverse/problems.js +59 -28
- package/dist/mjs/traverse/traverse.js +26 -30
- package/dist/mjs/utils/data.js +24 -0
- package/dist/mjs/utils/generics.js +18 -1
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/nodes/branch.d.ts +3 -12
- package/dist/types/nodes/discriminate.d.ts +2 -2
- 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/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/{tuple → ast}/morph.d.ts +3 -3
- 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/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/type.d.ts +9 -5
- package/dist/types/traverse/problems.d.ts +12 -21
- package/dist/types/traverse/traverse.d.ts +66 -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 +13 -9
- 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/narrow.d.ts +0 -13
- package/dist/types/parse/tuple/tuple.d.ts +0 -31
- package/dist/types/utils/size.d.ts +0 -3
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { hasDomain } from "../../utils/domains.js";
|
|
2
2
|
import { throwParseError } from "../../utils/errors.js";
|
|
3
|
-
import {
|
|
3
|
+
import { objectKeysOf } from "../../utils/generics.js";
|
|
4
4
|
import { stringify } from "../../utils/serialize.js";
|
|
5
5
|
export const writeMalformedDistributableFunctionMessage = (def)=>`Expected a Function or Record<Domain, Function> operand (${stringify(def)} was invalid)`;
|
|
6
6
|
export const distributeFunctionToNode = (distributableFunction, inputNode, ctx, ruleKey)=>{
|
|
7
|
-
const domains =
|
|
7
|
+
const domains = objectKeysOf(ctx.type.meta.scope.resolveNode(inputNode));
|
|
8
8
|
if (!hasDomain(distributableFunction, "object")) {
|
|
9
9
|
return throwParseError(writeMalformedDistributableFunctionMessage(distributableFunction));
|
|
10
10
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const writeIndivisibleMessage = (root)=>`Divisibility operand ${root} must be a number`;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { disjointDescriptionWriters } from "../../nodes/compose.js";
|
|
2
|
-
import {
|
|
2
|
+
import { objectKeysOf } from "../../utils/generics.js";
|
|
3
3
|
export const compileDisjointReasonsMessage = (disjoints)=>{
|
|
4
|
-
const paths =
|
|
4
|
+
const paths = objectKeysOf(disjoints);
|
|
5
5
|
if (paths.length === 1) {
|
|
6
6
|
const path = paths[0];
|
|
7
7
|
return `${path === "/" ? "" : `At ${path}: `}Intersection of ${disjointDescriptionWriters[disjoints[path].kind](disjoints[path])} results in an unsatisfiable type`;
|
|
@@ -14,6 +14,3 @@ export const compileDisjointReasonsMessage = (disjoints)=>{
|
|
|
14
14
|
return message;
|
|
15
15
|
};
|
|
16
16
|
export const writeImplicitNeverMessage = (path, operator, description)=>`${path.length ? `At ${path}: ` : ""}${operator} ${description ? `${description} ` : ""}results in an unsatisfiable type`;
|
|
17
|
-
export const writeUndiscriminatableMorphUnionMessage = (path)=>`${path === "/" ? "A" : `At ${path}, a`} union including one or more morphs must be discriminatable`;
|
|
18
|
-
export const writeIndivisibleMessage = (root)=>`Divisibility operand ${root} must be a number`;
|
|
19
|
-
export const writeUnboundableMessage = (root)=>`Bounded expression ${root} must be a number, string or array`;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
var _keyNode, _keyType, _keyNode1, _keyNode2;
|
|
2
|
+
import { mappedKeys } from "../../nodes/rules/props.js";
|
|
3
|
+
import { throwInternalError } from "../../utils/errors.js";
|
|
4
|
+
import { deepFreeze } from "../../utils/freeze.js";
|
|
5
|
+
import { listFrom, objectKeysOf, prototypeKeysOf } from "../../utils/generics.js";
|
|
6
|
+
import { tryParseWellFormedInteger, wellFormedNonNegativeIntegerMatcher } from "../../utils/numericLiterals.js";
|
|
7
|
+
import { defaultObjectKinds } from "../../utils/objectKinds.js";
|
|
8
|
+
import { stringify } from "../../utils/serialize.js";
|
|
9
|
+
import { parseDefinition } from "../definition.js";
|
|
10
|
+
import { writeImplicitNeverMessage } from "./intersection.js";
|
|
11
|
+
const arrayIndexStringBranch = deepFreeze({
|
|
12
|
+
regex: wellFormedNonNegativeIntegerMatcher.source
|
|
13
|
+
});
|
|
14
|
+
const arrayIndexNumberBranch = deepFreeze({
|
|
15
|
+
range: {
|
|
16
|
+
min: {
|
|
17
|
+
comparator: ">=",
|
|
18
|
+
limit: 1
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
divisor: 1
|
|
22
|
+
});
|
|
23
|
+
export const parseKeyOfTuple = (def, ctx)=>{
|
|
24
|
+
const resolution = ctx.type.meta.scope.resolveNode(parseDefinition(def[1], ctx));
|
|
25
|
+
const predicateKeys = objectKeysOf(resolution).map((domain)=>keysOfPredicate(domain, resolution[domain]));
|
|
26
|
+
const sharedKeys = sharedKeysOf(predicateKeys);
|
|
27
|
+
if (!sharedKeys.length) {
|
|
28
|
+
return writeImplicitNeverMessage(ctx.path, "keyof");
|
|
29
|
+
}
|
|
30
|
+
const keyNode = {};
|
|
31
|
+
for (const key of sharedKeys){
|
|
32
|
+
const keyType = typeof key;
|
|
33
|
+
if (keyType === "string" || keyType === "number" || keyType === "symbol") {
|
|
34
|
+
(_keyNode = keyNode)[_keyType = keyType] ?? (_keyNode[_keyType] = []);
|
|
35
|
+
keyNode[keyType].push({
|
|
36
|
+
value: key
|
|
37
|
+
});
|
|
38
|
+
} else if (key === wellFormedNonNegativeIntegerMatcher) {
|
|
39
|
+
(_keyNode1 = keyNode).string ?? (_keyNode1.string = []);
|
|
40
|
+
keyNode.string.push(arrayIndexStringBranch);
|
|
41
|
+
(_keyNode2 = keyNode).number ?? (_keyNode2.number = []);
|
|
42
|
+
keyNode.number.push(arrayIndexNumberBranch);
|
|
43
|
+
} else {
|
|
44
|
+
return throwInternalError(`Unexpected keyof key '${stringify(key)}'`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return Object.fromEntries(Object.entries(keyNode).map(([domain, branches])=>[
|
|
48
|
+
domain,
|
|
49
|
+
branches.length === 1 ? branches[0] : branches
|
|
50
|
+
]));
|
|
51
|
+
};
|
|
52
|
+
const baseKeysByDomain = {
|
|
53
|
+
bigint: prototypeKeysOf(0n),
|
|
54
|
+
boolean: prototypeKeysOf(false),
|
|
55
|
+
null: [],
|
|
56
|
+
number: prototypeKeysOf(0),
|
|
57
|
+
// TS doesn't include the Object prototype in keyof, so keyof object is never
|
|
58
|
+
object: [],
|
|
59
|
+
string: prototypeKeysOf(""),
|
|
60
|
+
symbol: prototypeKeysOf(Symbol()),
|
|
61
|
+
undefined: []
|
|
62
|
+
};
|
|
63
|
+
const keysOfPredicate = (domain, predicate)=>domain !== "object" || predicate === true ? baseKeysByDomain[domain] : sharedKeysOf(listFrom(predicate).map((branch)=>keysOfObjectBranch(branch)));
|
|
64
|
+
const sharedKeysOf = (keyBranches)=>{
|
|
65
|
+
if (!keyBranches.length) {
|
|
66
|
+
return [];
|
|
67
|
+
}
|
|
68
|
+
let sharedKeys = keyBranches[0];
|
|
69
|
+
for(let i = 1; i < keyBranches.length; i++){
|
|
70
|
+
// we can filter directly by equality here because the RegExp we're
|
|
71
|
+
// using will always be reference equal to
|
|
72
|
+
// wellFormedNonNegativeIntegerMatcher
|
|
73
|
+
sharedKeys = sharedKeys.filter((k)=>keyBranches[i].includes(k));
|
|
74
|
+
}
|
|
75
|
+
return sharedKeys;
|
|
76
|
+
};
|
|
77
|
+
const keysOfObjectBranch = (branch)=>{
|
|
78
|
+
const result = [];
|
|
79
|
+
if ("props" in branch) {
|
|
80
|
+
for (const key of Object.keys(branch.props)){
|
|
81
|
+
if (key === mappedKeys.index) {
|
|
82
|
+
result.push(wellFormedNonNegativeIntegerMatcher);
|
|
83
|
+
} else if (!result.includes(key)) {
|
|
84
|
+
result.push(key);
|
|
85
|
+
if (wellFormedNonNegativeIntegerMatcher.test(key)) {
|
|
86
|
+
// allow numeric access to keys
|
|
87
|
+
result.push(tryParseWellFormedInteger(key, `Unexpectedly failed to parse an integer from key '${key}'`));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if ("class" in branch) {
|
|
93
|
+
const constructor = typeof branch.class === "string" ? defaultObjectKinds[branch.class] : branch.class;
|
|
94
|
+
for (const key1 of prototypeKeysOf(constructor.prototype)){
|
|
95
|
+
if (!result.includes(key1)) {
|
|
96
|
+
result.push(key1);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { rootIntersection } from "../../nodes/node.js";
|
|
2
|
+
import { parseDefinition } from "../definition.js";
|
|
3
|
+
import { distributeFunctionToNode } from "./distributableFunction.js";
|
|
4
|
+
export const parseNarrowTuple = (def, ctx)=>{
|
|
5
|
+
const inputNode = parseDefinition(def[0], ctx);
|
|
6
|
+
return rootIntersection(inputNode, distributeFunctionToNode(def[2], inputNode, ctx, "narrow"), ctx.type);
|
|
7
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { rootIntersection, rootUnion, toArrayNode } from "../../nodes/node.js";
|
|
2
2
|
import { domainOf } from "../../utils/domains.js";
|
|
3
3
|
import { throwParseError } from "../../utils/errors.js";
|
|
4
4
|
import { parseDefinition } from "../definition.js";
|
|
@@ -7,10 +7,10 @@ import { parseKeyOfTuple } from "./keyof.js";
|
|
|
7
7
|
import { parseMorphTuple } from "./morph.js";
|
|
8
8
|
import { parseNarrowTuple } from "./narrow.js";
|
|
9
9
|
export const parseTuple = (def, ctx)=>{
|
|
10
|
-
if (
|
|
11
|
-
return
|
|
10
|
+
if (isIndexOneExpression(def)) {
|
|
11
|
+
return indexOneParsers[def[1]](def, ctx);
|
|
12
12
|
}
|
|
13
|
-
if (
|
|
13
|
+
if (isIndexZeroExpression(def)) {
|
|
14
14
|
return prefixParsers[def[0]](def, ctx);
|
|
15
15
|
}
|
|
16
16
|
const props = {
|
|
@@ -44,12 +44,12 @@ const parseBranchTuple = (def, ctx)=>{
|
|
|
44
44
|
}
|
|
45
45
|
const l = parseDefinition(def[0], ctx);
|
|
46
46
|
const r = parseDefinition(def[2], ctx);
|
|
47
|
-
return def[1] === "&" ?
|
|
47
|
+
return def[1] === "&" ? rootIntersection(l, r, ctx.type) : rootUnion(l, r, ctx.type);
|
|
48
48
|
};
|
|
49
|
-
const parseArrayTuple = (def, scope)=>
|
|
49
|
+
const parseArrayTuple = (def, scope)=>toArrayNode(parseDefinition(def[0], scope));
|
|
50
50
|
export const writeMalformedFunctionalExpressionMessage = (operator, rightDef)=>`Expression requires a function following '${operator}' (was ${typeof rightDef})`;
|
|
51
|
-
const
|
|
52
|
-
const
|
|
51
|
+
const isIndexOneExpression = (def)=>indexOneParsers[def[1]] !== undefined;
|
|
52
|
+
const indexOneParsers = {
|
|
53
53
|
"|": parseBranchTuple,
|
|
54
54
|
"&": parseBranchTuple,
|
|
55
55
|
"[]": parseArrayTuple,
|
|
@@ -75,4 +75,4 @@ const prefixParsers = {
|
|
|
75
75
|
}),
|
|
76
76
|
node: (def)=>def[1]
|
|
77
77
|
};
|
|
78
|
-
const
|
|
78
|
+
const isIndexZeroExpression = (def)=>prefixParsers[def[0]] !== undefined;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const writeUndiscriminatableMorphUnionMessage = (path)=>`${path === "/" ? "A" : `At ${path}, a`} union including one or more morphs must be discriminatable`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -3,9 +3,9 @@ import { domainOf } from "../utils/domains.js";
|
|
|
3
3
|
import { throwParseError } from "../utils/errors.js";
|
|
4
4
|
import { objectKindOf } from "../utils/objectKinds.js";
|
|
5
5
|
import { stringify } from "../utils/serialize.js";
|
|
6
|
+
import { parseTuple } from "./ast/tuple.js";
|
|
6
7
|
import { parseRecord } from "./record.js";
|
|
7
8
|
import { parseString } from "./string/string.js";
|
|
8
|
-
import { parseTuple } from "./tuple/tuple.js";
|
|
9
9
|
export const parseDefinition = (def, ctx)=>{
|
|
10
10
|
const domain = domainOf(def);
|
|
11
11
|
if (domain === "string") {
|
|
@@ -27,11 +27,21 @@ export const parseDefinition = (def, ctx)=>{
|
|
|
27
27
|
}
|
|
28
28
|
};
|
|
29
29
|
case "Function":
|
|
30
|
-
|
|
30
|
+
if (isType(def)) {
|
|
31
|
+
return def.node;
|
|
32
|
+
}
|
|
33
|
+
if (isThunk(def)) {
|
|
34
|
+
const returned = def();
|
|
35
|
+
if (isType(returned)) {
|
|
36
|
+
return returned.node;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return throwParseError(writeBadDefinitionTypeMessage("Function"));
|
|
31
40
|
default:
|
|
32
41
|
return throwParseError(writeBadDefinitionTypeMessage(objectKind ?? stringify(def)));
|
|
33
42
|
}
|
|
34
43
|
};
|
|
35
44
|
export const as = Symbol("as");
|
|
36
45
|
export const unknownDefinitionMessage = "Cannot statically parse a definition inferred as unknown. Consider using 'as inferred<...>' to cast it.";
|
|
46
|
+
const isThunk = (def)=>typeof def === "function" && def.length === 0;
|
|
37
47
|
export const writeBadDefinitionTypeMessage = (actual)=>`Type definitions must be strings or objects (was ${actual})`;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isLiteralNode, rootIntersection, rootUnion, toArrayNode } from "../../../nodes/node.js";
|
|
2
2
|
import { minComparators } from "../../../nodes/rules/range.js";
|
|
3
3
|
import { throwInternalError, throwParseError } from "../../../utils/errors.js";
|
|
4
4
|
import { isKeyOf } from "../../../utils/generics.js";
|
|
@@ -51,10 +51,10 @@ export class DynamicState {
|
|
|
51
51
|
this.root = node;
|
|
52
52
|
}
|
|
53
53
|
rootToArray() {
|
|
54
|
-
this.root =
|
|
54
|
+
this.root = toArrayNode(this.ejectRoot());
|
|
55
55
|
}
|
|
56
56
|
intersect(node) {
|
|
57
|
-
this.root =
|
|
57
|
+
this.root = rootIntersection(this.ejectRoot(), node, this.ctx.type);
|
|
58
58
|
}
|
|
59
59
|
ejectRoot() {
|
|
60
60
|
this.assertHasRoot();
|
|
@@ -81,7 +81,7 @@ export class DynamicState {
|
|
|
81
81
|
return this.error(writeUnpairableComparatorMessage(comparator));
|
|
82
82
|
}
|
|
83
83
|
if (this.branches.range) {
|
|
84
|
-
return this.error(writeMultipleLeftBoundsMessage(this.branches.range.limit
|
|
84
|
+
return this.error(writeMultipleLeftBoundsMessage(`${this.branches.range.limit}`, this.branches.range.comparator, `${limit}`, invertedComparator));
|
|
85
85
|
}
|
|
86
86
|
this.branches.range = {
|
|
87
87
|
limit,
|
|
@@ -94,7 +94,7 @@ export class DynamicState {
|
|
|
94
94
|
this.pushRootToBranch("|");
|
|
95
95
|
this.setRoot(this.branches.union);
|
|
96
96
|
} else if (this.branches.intersection) {
|
|
97
|
-
this.setRoot(
|
|
97
|
+
this.setRoot(rootIntersection(this.branches.intersection, this.ejectRoot(), this.ctx.type));
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
finalizeGroup() {
|
|
@@ -107,15 +107,15 @@ export class DynamicState {
|
|
|
107
107
|
}
|
|
108
108
|
pushRootToBranch(token) {
|
|
109
109
|
this.assertRangeUnset();
|
|
110
|
-
this.branches.intersection = this.branches.intersection ?
|
|
110
|
+
this.branches.intersection = this.branches.intersection ? rootIntersection(this.branches.intersection, this.ejectRoot(), this.ctx.type) : this.ejectRoot();
|
|
111
111
|
if (token === "|") {
|
|
112
|
-
this.branches.union = this.branches.union ?
|
|
112
|
+
this.branches.union = this.branches.union ? rootUnion(this.branches.union, this.branches.intersection, this.ctx.type) : this.branches.intersection;
|
|
113
113
|
delete this.branches.intersection;
|
|
114
114
|
}
|
|
115
115
|
}
|
|
116
116
|
assertRangeUnset() {
|
|
117
117
|
if (this.branches.range) {
|
|
118
|
-
return this.error(writeOpenRangeMessage(this.branches.range.limit
|
|
118
|
+
return this.error(writeOpenRangeMessage(`${this.branches.range.limit}`, this.branches.range.comparator));
|
|
119
119
|
}
|
|
120
120
|
}
|
|
121
121
|
reduceGroupOpen() {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { stringifyRange } from "../../../../nodes/compose.js";
|
|
2
2
|
import { compareStrictness, maxComparators, minComparators } from "../../../../nodes/rules/range.js";
|
|
3
3
|
import { throwInternalError } from "../../../../utils/errors.js";
|
|
4
|
-
import { hasKey, isKeyOf,
|
|
4
|
+
import { hasKey, isKeyOf, listFrom, objectKeysOf } from "../../../../utils/generics.js";
|
|
5
5
|
import { tryParseWellFormedNumber } from "../../../../utils/numericLiterals.js";
|
|
6
|
-
import { writeUnboundableMessage } from "
|
|
6
|
+
import { writeUnboundableMessage } from "../../../ast/bound.js";
|
|
7
7
|
import { writeUnpairableComparatorMessage } from "../../reduce/shared.js";
|
|
8
8
|
import { Scanner } from "../scanner.js";
|
|
9
9
|
export const parseBound = (s, start)=>{
|
|
@@ -36,7 +36,7 @@ export const parseRightBound = (s, comparator)=>{
|
|
|
36
36
|
};
|
|
37
37
|
const distributeRange = (range, s)=>{
|
|
38
38
|
const resolution = s.resolveRoot();
|
|
39
|
-
const domains =
|
|
39
|
+
const domains = objectKeysOf(resolution);
|
|
40
40
|
const distributedRange = {};
|
|
41
41
|
const rangePredicate = {
|
|
42
42
|
range
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { objectKeysOf } from "../../../../utils/generics.js";
|
|
2
2
|
import { tryParseWellFormedInteger } from "../../../../utils/numericLiterals.js";
|
|
3
|
-
import { writeIndivisibleMessage } from "
|
|
3
|
+
import { writeIndivisibleMessage } from "../../../ast/divisor.js";
|
|
4
4
|
export const parseDivisor = (s)=>{
|
|
5
5
|
const divisorToken = s.scanner.shiftUntilNextTerminator();
|
|
6
6
|
const divisor = tryParseWellFormedInteger(divisorToken, writeInvalidDivisorMessage(divisorToken));
|
|
7
7
|
if (divisor === 0) {
|
|
8
8
|
s.error(writeInvalidDivisorMessage(0));
|
|
9
9
|
}
|
|
10
|
-
const rootDomains =
|
|
10
|
+
const rootDomains = objectKeysOf(s.resolveRoot());
|
|
11
11
|
if (rootDomains.length === 1 && rootDomains[0] === "number") {
|
|
12
12
|
s.intersect({
|
|
13
13
|
number: {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { toArrayNode } from "../../nodes/node.js";
|
|
2
2
|
import { DynamicState } from "./reduce/dynamic.js";
|
|
3
3
|
import { parseOperand } from "./shift/operand/operand.js";
|
|
4
4
|
import { parseOperator } from "./shift/operator/operator.js";
|
|
@@ -10,7 +10,7 @@ export const maybeNaiveParse = (def, ctx)=>{
|
|
|
10
10
|
if (def.endsWith("[]")) {
|
|
11
11
|
const elementDef = def.slice(0, -2);
|
|
12
12
|
if (ctx.type.meta.scope.addReferenceIfResolvable(def, ctx)) {
|
|
13
|
-
return
|
|
13
|
+
return toArrayNode(elementDef);
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { scopes } from "./standard.js";
|
|
2
|
+
export const intersection = scopes.standard.intersection;
|
|
3
|
+
export const union = scopes.standard.union;
|
|
4
|
+
export const arrayOf = scopes.standard.arrayOf;
|
|
5
|
+
export const keyOf = scopes.standard.keyOf;
|
|
6
|
+
export const instanceOf = scopes.standard.instanceOf;
|
|
7
|
+
export const valueOf = scopes.standard.valueOf;
|
|
8
|
+
export const narrow = scopes.standard.narrow;
|
|
9
|
+
export const morph = scopes.standard.morph;
|
package/dist/mjs/scopes/scope.js
CHANGED
|
@@ -143,7 +143,57 @@ export class Scope {
|
|
|
143
143
|
_classPrivateFieldSet(this, _resolutions, new Cache());
|
|
144
144
|
_classPrivateFieldSet(this, _exports, new Cache());
|
|
145
145
|
_classPrivateFieldSet(this, _anonymousTypeCount, 0);
|
|
146
|
-
this.
|
|
146
|
+
this.expressions = {
|
|
147
|
+
intersection: (l, r, opts)=>this.type([
|
|
148
|
+
l,
|
|
149
|
+
"&",
|
|
150
|
+
r
|
|
151
|
+
], opts),
|
|
152
|
+
union: (l, r, opts)=>this.type([
|
|
153
|
+
l,
|
|
154
|
+
"|",
|
|
155
|
+
r
|
|
156
|
+
], opts),
|
|
157
|
+
arrayOf: (def, opts)=>this.type([
|
|
158
|
+
def,
|
|
159
|
+
"[]"
|
|
160
|
+
], opts),
|
|
161
|
+
keyOf: (def, opts)=>this.type([
|
|
162
|
+
"keyof",
|
|
163
|
+
def
|
|
164
|
+
], opts),
|
|
165
|
+
fromNode: (def, opts)=>this.type([
|
|
166
|
+
"node",
|
|
167
|
+
def
|
|
168
|
+
], opts),
|
|
169
|
+
instanceOf: (def, opts)=>this.type([
|
|
170
|
+
"instanceof",
|
|
171
|
+
def
|
|
172
|
+
], opts),
|
|
173
|
+
valueOf: (def, opts)=>this.type([
|
|
174
|
+
"===",
|
|
175
|
+
def
|
|
176
|
+
], opts),
|
|
177
|
+
narrow: (def, fn, opts)=>this.type([
|
|
178
|
+
def,
|
|
179
|
+
":",
|
|
180
|
+
fn
|
|
181
|
+
], opts),
|
|
182
|
+
morph: (def, fn, opts)=>this.type([
|
|
183
|
+
def,
|
|
184
|
+
"=>",
|
|
185
|
+
fn
|
|
186
|
+
], opts)
|
|
187
|
+
};
|
|
188
|
+
this.intersection = this.expressions.intersection;
|
|
189
|
+
this.union = this.expressions.union;
|
|
190
|
+
this.arrayOf = this.expressions.arrayOf;
|
|
191
|
+
this.keyOf = this.expressions.keyOf;
|
|
192
|
+
this.valueOf = this.expressions.valueOf;
|
|
193
|
+
this.instanceOf = this.expressions.instanceOf;
|
|
194
|
+
this.narrow = this.expressions.narrow;
|
|
195
|
+
this.morph = this.expressions.morph;
|
|
196
|
+
this.type = Object.assign((def, opts = {})=>{
|
|
147
197
|
if (opts.name && this.aliases[opts.name]) {
|
|
148
198
|
return throwParseError(writeDuplicateAliasesMessage(opts.name));
|
|
149
199
|
}
|
|
@@ -152,7 +202,9 @@ export class Scope {
|
|
|
152
202
|
result.node = this.resolveNode(parseDefinition(def, ctx));
|
|
153
203
|
result.flat = flattenType(result);
|
|
154
204
|
return result;
|
|
155
|
-
}
|
|
205
|
+
}, {
|
|
206
|
+
from: this.expressions.fromNode
|
|
207
|
+
});
|
|
156
208
|
this.name = _classPrivateMethodGet(this, _register, register).call(this, opts);
|
|
157
209
|
if (opts.standard !== false) {
|
|
158
210
|
_classPrivateMethodGet(this, _cacheSpaces, cacheSpaces).call(this, [
|
|
@@ -220,9 +272,9 @@ function resolveRecurse(name, onUnresolvable, seen) {
|
|
|
220
272
|
type.flat = flattenType(type);
|
|
221
273
|
return type;
|
|
222
274
|
}
|
|
275
|
+
export const scope = (aliases, opts = {})=>new Scope(aliases, opts);
|
|
223
276
|
export const writeShallowCycleErrorMessage = (name, seen)=>`Alias '${name}' has a shallow resolution cycle: ${[
|
|
224
277
|
...seen,
|
|
225
278
|
name
|
|
226
279
|
].join("=>")}`;
|
|
227
|
-
export const scope = (aliases, opts = {})=>new Scope(aliases, opts);
|
|
228
280
|
export const writeDuplicateAliasesMessage = (name)=>`Alias '${name}' is already defined`;
|
|
@@ -1,6 +1,47 @@
|
|
|
1
|
+
function _checkPrivateRedeclaration(obj, privateCollection) {
|
|
2
|
+
if (privateCollection.has(obj)) {
|
|
3
|
+
throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
function _classApplyDescriptorGet(receiver, descriptor) {
|
|
7
|
+
if (descriptor.get) {
|
|
8
|
+
return descriptor.get.call(receiver);
|
|
9
|
+
}
|
|
10
|
+
return descriptor.value;
|
|
11
|
+
}
|
|
12
|
+
function _classApplyDescriptorSet(receiver, descriptor, value) {
|
|
13
|
+
if (descriptor.set) {
|
|
14
|
+
descriptor.set.call(receiver, value);
|
|
15
|
+
} else {
|
|
16
|
+
if (!descriptor.writable) {
|
|
17
|
+
throw new TypeError("attempted to set read only private field");
|
|
18
|
+
}
|
|
19
|
+
descriptor.value = value;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function _classExtractFieldDescriptor(receiver, privateMap, action) {
|
|
23
|
+
if (!privateMap.has(receiver)) {
|
|
24
|
+
throw new TypeError("attempted to " + action + " private field on non-instance");
|
|
25
|
+
}
|
|
26
|
+
return privateMap.get(receiver);
|
|
27
|
+
}
|
|
28
|
+
function _classPrivateFieldGet(receiver, privateMap) {
|
|
29
|
+
var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get");
|
|
30
|
+
return _classApplyDescriptorGet(receiver, descriptor);
|
|
31
|
+
}
|
|
32
|
+
function _classPrivateFieldInit(obj, privateMap, value) {
|
|
33
|
+
_checkPrivateRedeclaration(obj, privateMap);
|
|
34
|
+
privateMap.set(obj, value);
|
|
35
|
+
}
|
|
36
|
+
function _classPrivateFieldSet(receiver, privateMap, value) {
|
|
37
|
+
var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set");
|
|
38
|
+
_classApplyDescriptorSet(receiver, descriptor, value);
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
1
41
|
var _definitions_code, _definitions_code1;
|
|
2
42
|
import { Scanner } from "../parse/string/shift/scanner.js";
|
|
3
|
-
import {
|
|
43
|
+
import { DataWrapper } from "../utils/data.js";
|
|
44
|
+
import { domainDescriptions } from "../utils/domains.js";
|
|
4
45
|
import { isWellFormedInteger } from "../utils/numericLiterals.js";
|
|
5
46
|
import { objectKindDescriptions } from "../utils/objectKinds.js";
|
|
6
47
|
import { Path } from "../utils/paths.js";
|
|
@@ -35,21 +76,27 @@ export class Problem {
|
|
|
35
76
|
}
|
|
36
77
|
}
|
|
37
78
|
}
|
|
79
|
+
var _state = /*#__PURE__*/ new WeakMap();
|
|
38
80
|
class ProblemArray extends Array {
|
|
39
|
-
|
|
81
|
+
create(code, source, opts) {
|
|
40
82
|
// copy the path to avoid future mutations affecting it
|
|
41
|
-
const path = opts?.path ?? Path.from(this.
|
|
42
|
-
const
|
|
43
|
-
|
|
83
|
+
const path = opts?.path ?? Path.from(_classPrivateFieldGet(this, _state).path);
|
|
84
|
+
const data = // we have to check for the presence of the key explicitly since the
|
|
85
|
+
// data could be undefined or null
|
|
86
|
+
opts && "data" in opts ? opts.data : _classPrivateFieldGet(this, _state).data;
|
|
87
|
+
return this.add(new Problem(code, path, data, source, _classPrivateFieldGet(this, _state).getConfigForProblemCode(code)));
|
|
88
|
+
}
|
|
89
|
+
add(problem) {
|
|
90
|
+
const pathKey = `${problem.path}`;
|
|
44
91
|
const existing = this.byPath[pathKey];
|
|
45
92
|
if (existing) {
|
|
46
93
|
if (existing.parts) {
|
|
47
94
|
existing.parts.push(problem);
|
|
48
95
|
} else {
|
|
49
|
-
const problemIntersection = new Problem("multi", existing.path, data, [
|
|
96
|
+
const problemIntersection = new Problem("multi", existing.path, existing.data, [
|
|
50
97
|
existing,
|
|
51
98
|
problem
|
|
52
|
-
], this.
|
|
99
|
+
], _classPrivateFieldGet(this, _state).getConfigForProblemCode("multi"));
|
|
53
100
|
const existingIndex = this.indexOf(existing);
|
|
54
101
|
// If existing is found (which it always should be unless this was externally mutated),
|
|
55
102
|
// replace it with the new problem intersection. In case it isn't for whatever reason,
|
|
@@ -74,8 +121,12 @@ class ProblemArray extends Array {
|
|
|
74
121
|
}
|
|
75
122
|
constructor(state){
|
|
76
123
|
super();
|
|
77
|
-
this.state = state;
|
|
78
124
|
this.byPath = {};
|
|
125
|
+
_classPrivateFieldInit(this, _state, {
|
|
126
|
+
writable: true,
|
|
127
|
+
value: void 0
|
|
128
|
+
});
|
|
129
|
+
_classPrivateFieldSet(this, _state, state);
|
|
79
130
|
}
|
|
80
131
|
}
|
|
81
132
|
export const Problems = ProblemArray;
|
|
@@ -100,26 +151,6 @@ export const describeBranches = (descriptions)=>{
|
|
|
100
151
|
description += ` or ${descriptions[descriptions.length - 1]}`;
|
|
101
152
|
return description;
|
|
102
153
|
};
|
|
103
|
-
export class DataWrapper {
|
|
104
|
-
toString() {
|
|
105
|
-
return stringify(this.value);
|
|
106
|
-
}
|
|
107
|
-
get domain() {
|
|
108
|
-
return domainOf(this.value);
|
|
109
|
-
}
|
|
110
|
-
get size() {
|
|
111
|
-
return typeof this.value === "string" || Array.isArray(this.value) ? this.value.length : typeof this.value === "number" ? this.value : 0;
|
|
112
|
-
}
|
|
113
|
-
get units() {
|
|
114
|
-
return typeof this.value === "string" ? "characters" : Array.isArray(this.value) ? "items" : "";
|
|
115
|
-
}
|
|
116
|
-
get className() {
|
|
117
|
-
return Object(this.value).constructor.name;
|
|
118
|
-
}
|
|
119
|
-
constructor(value){
|
|
120
|
-
this.value = value;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
154
|
const writeDefaultReason = (mustBe, was)=>`must be ${mustBe}${was && ` (was ${was})`}`;
|
|
124
155
|
const addDefaultContext = (reason, path)=>path.length === 0 ? capitalize(reason) : path.length === 1 && isWellFormedInteger(path[0]) ? `Item at index ${path[0]} ${reason}` : `${path} ${reason}`;
|
|
125
156
|
const compileDefaultProblemWriters = (definitions)=>{
|