arktype 0.1.4 → 0.2.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/node.js +20 -2
- package/dist/cjs/nodes/predicate.js +2 -0
- package/dist/cjs/nodes/rules/regex.js +4 -42
- package/dist/cjs/nodes/rules/rules.js +15 -11
- package/dist/cjs/parse/definition.js +2 -2
- package/dist/cjs/parse/tuple/morph.js +7 -6
- 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 +70 -0
- package/dist/cjs/scopes/validation.js +64 -0
- package/dist/cjs/traverse/problems.js +50 -74
- package/dist/cjs/traverse/traverse.js +25 -3
- package/dist/cjs/utils/numericLiterals.js +1 -0
- package/dist/mjs/nodes/node.js +20 -2
- package/dist/mjs/nodes/predicate.js +1 -0
- package/dist/mjs/nodes/rules/regex.js +3 -41
- package/dist/mjs/nodes/rules/rules.js +16 -12
- package/dist/mjs/parse/definition.js +1 -1
- package/dist/mjs/parse/tuple/morph.js +7 -6
- 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 +56 -0
- package/dist/mjs/scopes/validation.js +51 -0
- package/dist/mjs/traverse/problems.js +49 -73
- package/dist/mjs/traverse/traverse.js +26 -4
- package/dist/mjs/utils/numericLiterals.js +1 -1
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/nodes/compose.d.ts +1 -1
- package/dist/types/nodes/infer.d.ts +4 -4
- package/dist/types/nodes/node.d.ts +11 -7
- package/dist/types/nodes/predicate.d.ts +43 -54
- package/dist/types/nodes/rules/range.d.ts +1 -1
- package/dist/types/nodes/rules/regex.d.ts +1 -11
- package/dist/types/nodes/rules/rules.d.ts +2 -10
- package/dist/types/parse/definition.d.ts +1 -1
- package/dist/types/parse/string/ast.d.ts +2 -1
- package/dist/types/parse/string/reduce/dynamic.d.ts +1 -1
- 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/operand/unenclosed.d.ts +1 -1
- package/dist/types/parse/string/shift/operator/bounds.d.ts +1 -1
- 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 +53 -0
- package/dist/types/scopes/validation.d.ts +21 -0
- package/dist/types/traverse/problems.d.ts +29 -32
- package/dist/types/traverse/traverse.d.ts +10 -8
- package/dist/types/utils/numericLiterals.d.ts +1 -0
- package/package.json +2 -2
- package/dist/types/main.d.ts +0 -324
|
@@ -1,67 +1,56 @@
|
|
|
1
|
-
import type { Type } from "../main.js";
|
|
2
1
|
import type { Morph } from "../parse/tuple/morph.js";
|
|
2
|
+
import type { Type } from "../scopes/type.js";
|
|
3
3
|
import type { Domain } from "../utils/domains.js";
|
|
4
|
-
import type { CollapsibleList, defined, Dict
|
|
4
|
+
import type { CollapsibleList, defined, Dict } from "../utils/generics.js";
|
|
5
5
|
import type { BranchesComparison } from "./branches.js";
|
|
6
6
|
import type { IntersectionResult, KeyIntersectionFn } from "./compose.js";
|
|
7
7
|
import { IntersectionState } from "./compose.js";
|
|
8
8
|
import type { FlattenContext, ResolvedNode, TraversalEntry } from "./node.js";
|
|
9
|
-
import type { LiteralRules, Rules } from "./rules/rules.js";
|
|
9
|
+
import type { FlatRules, LiteralRules, Rules } from "./rules/rules.js";
|
|
10
|
+
/** If scope is provided, we also narrow each predicate to match its domain.
|
|
11
|
+
* Otherwise, we use a base predicate for all types, which is easier to
|
|
12
|
+
* manipulate.*/
|
|
10
13
|
export type Predicate<domain extends Domain = Domain, $ = Dict> = string extends keyof $ ? true | CollapsibleList<Branch> : true | CollapsibleList<Branch<domain, $>>;
|
|
11
|
-
export type Branch<domain extends Domain = Domain, $ = Dict> =
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
export type Branch<domain extends Domain = Domain, $ = Dict> = Rules<domain, $> | TransformationBranch<domain, $>;
|
|
15
|
+
export type TransformationBranch<domain extends Domain = Domain, $ = Dict> = {
|
|
16
|
+
rules: Rules<domain, $>;
|
|
17
|
+
morph?: CollapsibleList<Morph>;
|
|
18
|
+
};
|
|
19
|
+
export declare const branchIsTransformation: (branch: Branch) => branch is TransformationBranch<keyof {
|
|
20
|
+
bigint: bigint;
|
|
21
|
+
boolean: boolean;
|
|
22
|
+
number: number;
|
|
23
|
+
object: object;
|
|
24
|
+
string: string;
|
|
25
|
+
symbol: symbol;
|
|
26
|
+
undefined: undefined;
|
|
27
|
+
null: null;
|
|
28
|
+
}, Dict>;
|
|
29
|
+
export type FlatBranch = FlatRules | FlatTransformationBranch;
|
|
30
|
+
export type FlatTransformationBranch = [...rules: FlatRules, morph: MorphEntry];
|
|
31
|
+
export type MorphEntry = ["morph", CollapsibleList<Morph>];
|
|
15
32
|
export type PredicateComparison = IntersectionResult<Predicate> | BranchesComparison;
|
|
16
33
|
export declare const comparePredicates: (l: Predicate, r: Predicate, context: IntersectionState) => PredicateComparison;
|
|
17
34
|
export declare const predicateIntersection: KeyIntersectionFn<Required<ResolvedNode>>;
|
|
18
|
-
export declare const predicateUnion: (domain: Domain, l: Predicate, r: Predicate, type: Type) => true | {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
object: object;
|
|
38
|
-
string: string;
|
|
39
|
-
symbol: symbol;
|
|
40
|
-
undefined: undefined;
|
|
41
|
-
null: null;
|
|
42
|
-
}>;
|
|
43
|
-
morph: CollapsibleList<Morph>;
|
|
44
|
-
readonly objectKind?: undefined;
|
|
45
|
-
readonly regex?: undefined;
|
|
46
|
-
readonly divisor?: undefined;
|
|
47
|
-
readonly range?: undefined;
|
|
48
|
-
readonly props?: undefined;
|
|
49
|
-
readonly class?: undefined;
|
|
50
|
-
readonly narrow?: undefined;
|
|
51
|
-
} | {
|
|
52
|
-
input: import("./rules/rules.js").NarrowableRules<Dict> | LiteralRules<keyof {
|
|
53
|
-
bigint: bigint;
|
|
54
|
-
boolean: boolean;
|
|
55
|
-
number: number;
|
|
56
|
-
object: object;
|
|
57
|
-
string: string;
|
|
58
|
-
symbol: symbol;
|
|
59
|
-
undefined: undefined;
|
|
60
|
-
null: null;
|
|
61
|
-
}>;
|
|
62
|
-
morph: CollapsibleList<Morph>;
|
|
63
|
-
readonly value?: undefined;
|
|
64
|
-
} | readonly Branch<keyof {
|
|
35
|
+
export declare const predicateUnion: (domain: Domain, l: Predicate, r: Predicate, type: Type) => true | import("./rules/rules.js").NarrowableRules<Dict> | LiteralRules<keyof {
|
|
36
|
+
bigint: bigint;
|
|
37
|
+
boolean: boolean;
|
|
38
|
+
number: number;
|
|
39
|
+
object: object;
|
|
40
|
+
string: string;
|
|
41
|
+
symbol: symbol;
|
|
42
|
+
undefined: undefined;
|
|
43
|
+
null: null;
|
|
44
|
+
}> | TransformationBranch<keyof {
|
|
45
|
+
bigint: bigint;
|
|
46
|
+
boolean: boolean;
|
|
47
|
+
number: number;
|
|
48
|
+
object: object;
|
|
49
|
+
string: string;
|
|
50
|
+
symbol: symbol;
|
|
51
|
+
undefined: undefined;
|
|
52
|
+
null: null;
|
|
53
|
+
}, Dict> | readonly Branch<keyof {
|
|
65
54
|
bigint: bigint;
|
|
66
55
|
boolean: boolean;
|
|
67
56
|
number: number;
|
|
@@ -30,4 +30,4 @@ export type FlatBound = Bound & {
|
|
|
30
30
|
};
|
|
31
31
|
export declare const flattenRange: FlattenAndPushRule<Range>;
|
|
32
32
|
export declare const checkBound: (data: import("../../utils/size.js").SizedData, bound: FlatBound, state: import("../../traverse/traverse.js").TraversalState) => void;
|
|
33
|
-
export declare const compareStrictness: (kind: "min" | "max", l: Bound | undefined, r: Bound | undefined) => "
|
|
33
|
+
export declare const compareStrictness: (kind: "min" | "max", l: Bound | undefined, r: Bound | undefined) => "r" | "l" | "=";
|
|
@@ -1,14 +1,4 @@
|
|
|
1
1
|
import type { CollapsibleList } from "../../utils/generics.js";
|
|
2
|
-
export declare const sources: {
|
|
3
|
-
alpha: RegExp;
|
|
4
|
-
alphanumeric: RegExp;
|
|
5
|
-
lowercase: RegExp;
|
|
6
|
-
uppercase: RegExp;
|
|
7
|
-
creditCard: RegExp;
|
|
8
|
-
email: RegExp;
|
|
9
|
-
uuid: RegExp;
|
|
10
|
-
};
|
|
11
2
|
export declare const getRegex: (source: string) => RegExp;
|
|
12
|
-
export declare const checkRegex: (data: string,
|
|
3
|
+
export declare const checkRegex: (data: string, source: string, state: import("../../traverse/traverse.js").TraversalState) => void;
|
|
13
4
|
export declare const regexIntersection: import("../compose.js").Intersector<CollapsibleList<string> | undefined>;
|
|
14
|
-
export declare const isLuhnValid: (creditCardInput: string) => boolean;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { Morph } from "../../parse/tuple/morph.js";
|
|
2
1
|
import type { Narrow } from "../../parse/tuple/narrow.js";
|
|
3
2
|
import type { Domain, inferDomain } from "../../utils/domains.js";
|
|
4
3
|
import type { CollapsibleList, constructor, Dict } from "../../utils/generics.js";
|
|
5
4
|
import type { IntersectionState, Intersector } from "../compose.js";
|
|
6
5
|
import type { FlattenContext, TraversalEntry } from "../node.js";
|
|
6
|
+
import type { Branch, FlatBranch } from "../predicate.js";
|
|
7
7
|
import type { ObjectKindRule, TraversalObjectKindRule } from "./objectKind.js";
|
|
8
8
|
import type { PropsRule, TraversalOptionalProps, TraversalRequiredProps } from "./props.js";
|
|
9
9
|
import type { FlatBound, Range } from "./range.js";
|
|
@@ -20,16 +20,8 @@ export type LiteralRules<domain extends Domain = Domain> = {
|
|
|
20
20
|
readonly value: inferDomain<domain>;
|
|
21
21
|
};
|
|
22
22
|
export type NarrowRule = CollapsibleList<Narrow>;
|
|
23
|
-
export type Branch<domain extends Domain = Domain, $ = Dict> = Rules<domain, $> | MorphBranch<domain, $>;
|
|
24
|
-
export type MorphBranch<domain extends Domain = Domain, $ = Dict> = {
|
|
25
|
-
input: Rules<domain, $>;
|
|
26
|
-
morph: CollapsibleList<Morph>;
|
|
27
|
-
};
|
|
28
|
-
export type FlatBranch = FlatRules | FlatMorphedBranch;
|
|
29
23
|
export type FlatRules = RuleEntry[];
|
|
30
|
-
type
|
|
31
|
-
export type RuleEntry = ["objectKind", TraversalObjectKindRule] | ["regex", RegExp] | ["divisor", number] | ["bound", FlatBound] | ["class", constructor] | TraversalRequiredProps | TraversalOptionalProps | ["narrow", Narrow] | ["value", unknown];
|
|
32
|
-
export type MorphEntry = ["morph", CollapsibleList<Morph>];
|
|
24
|
+
export type RuleEntry = ["objectKind", TraversalObjectKindRule] | ["regex", string] | ["divisor", number] | ["bound", FlatBound] | ["class", constructor] | TraversalRequiredProps | TraversalOptionalProps | ["narrow", Narrow] | ["value", unknown];
|
|
33
25
|
export type Rules<domain extends Domain = Domain, $ = Dict> = Domain extends domain ? NarrowableRules | LiteralRules : domain extends "object" ? defineRuleSet<domain, "objectKind" | "props" | "range" | "narrow" | "class", $> : domain extends "string" ? defineRuleSet<domain, "regex" | "range" | "narrow", $> : domain extends "number" ? defineRuleSet<domain, "divisor" | "range" | "narrow", $> : defineRuleSet<domain, "narrow", $>;
|
|
34
26
|
type defineRuleSet<domain extends Domain, keys extends keyof NarrowableRules, $> = Pick<NarrowableRules<$>, keys> | LiteralRules<domain>;
|
|
35
27
|
export declare const branchIntersection: Intersector<Branch>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Type } from "../main.js";
|
|
2
1
|
import type { TypeNode } from "../nodes/node.js";
|
|
2
|
+
import type { Type } from "../scopes/type.js";
|
|
3
3
|
import type { Primitive } from "../utils/domains.js";
|
|
4
4
|
import { domainOf } from "../utils/domains.js";
|
|
5
5
|
import type { Dict, evaluateObject, isAny, isUnknown, List } from "../utils/generics.js";
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { asIn, resolve } from "../../main.js";
|
|
2
1
|
import type { DisjointsByPath } from "../../nodes/compose.js";
|
|
2
|
+
import type { resolve } from "../../scopes/scope.js";
|
|
3
|
+
import type { asIn } from "../../scopes/type.js";
|
|
3
4
|
import type { domainOf } from "../../utils/domains.js";
|
|
4
5
|
import type { asConst, castOnError, Dict, equals, error, evaluate, evaluateObject, extractValues, isAny, List, Literalable, RegexLiteral, requiredKeyOf, stringKeyOf, tryCatch } from "../../utils/generics.js";
|
|
5
6
|
import type { objectKindOf } from "../../utils/objectKinds.js";
|
|
@@ -29,6 +29,6 @@ export declare class DynamicState {
|
|
|
29
29
|
pushRootToBranch(token: Scanner.BranchToken): void;
|
|
30
30
|
private assertRangeUnset;
|
|
31
31
|
reduceGroupOpen(): void;
|
|
32
|
-
previousOperator(): "
|
|
32
|
+
previousOperator(): "&" | "|" | undefined;
|
|
33
33
|
shiftedByOne(): this;
|
|
34
34
|
}
|
|
@@ -7,7 +7,7 @@ export type unclosedGroupMessage = typeof unclosedGroupMessage;
|
|
|
7
7
|
export declare const writeOpenRangeMessage: <min extends number, comparator extends ">" | ">=">(min: min, comparator: comparator) => `Left bounds are only valid when paired with right bounds (try ...${comparator}${min})`;
|
|
8
8
|
export type writeOpenRangeMessage<min extends number, comparator extends MinComparator> = `Left bounds are only valid when paired with right bounds (try ...${comparator}${min})`;
|
|
9
9
|
export type writeUnpairableComparatorMessage<comparator extends Scanner.Comparator> = `Left-bounded expressions must specify their limits using < or <= (was ${comparator})`;
|
|
10
|
-
export declare const writeUnpairableComparatorMessage: <comparator extends "==" | "
|
|
10
|
+
export declare const writeUnpairableComparatorMessage: <comparator extends "==" | "<" | ">" | "<=" | ">=">(comparator: comparator) => `Left-bounded expressions must specify their limits using < or <= (was ${comparator})`;
|
|
11
11
|
export declare const writeMultipleLeftBoundsMessage: <openLimit extends number, openComparator extends ">" | ">=", limit extends number, comparator extends ">" | ">=">(openLimit: openLimit, openComparator: openComparator, limit: limit, comparator: comparator) => `An expression may have at most one left bound (parsed ${openLimit}${{
|
|
12
12
|
readonly "<": ">";
|
|
13
13
|
readonly ">": "<";
|
|
@@ -81,7 +81,7 @@ export declare namespace state {
|
|
|
81
81
|
...stack,
|
|
82
82
|
top
|
|
83
83
|
];
|
|
84
|
-
export type finalizeGroup<s extends StaticState, unscanned extends string> = s["groups"] extends popGroup<infer stack, infer top> ? from<{
|
|
84
|
+
export type finalizeGroup<s extends StaticState, unscanned extends string> = s["branches"]["range"] extends {} ? openRangeError<s["branches"]["range"]> : s["groups"] extends popGroup<infer stack, infer top> ? from<{
|
|
85
85
|
groups: stack;
|
|
86
86
|
branches: top;
|
|
87
87
|
root: mergeToUnion<s>;
|
|
@@ -9,7 +9,7 @@ export type isResolvableIdentifier<token, $> = token extends stringKeyOf<$> ? tr
|
|
|
9
9
|
type tryResolve<s extends StaticState, token extends string, $> = isResolvableIdentifier<token, $> extends true ? token : token extends NumberLiteral<infer value> ? number extends value ? error<writeMalformedNumericLiteralMessage<token, "number">> : value : token extends BigintLiteral<infer value> ? bigint extends value ? error<writeMalformedNumericLiteralMessage<token, "bigint">> : value : error<token extends "" ? writeMissingOperandMessage<s> : writeUnresolvableMessage<token>>;
|
|
10
10
|
export declare const writeUnresolvableMessage: <token extends string>(token: token) => `'${token}' is unresolvable`;
|
|
11
11
|
type writeUnresolvableMessage<token extends string> = `'${token}' is unresolvable`;
|
|
12
|
-
export declare const writeMissingOperandMessage: <s extends DynamicState>(s: s) => `Token '
|
|
12
|
+
export declare const writeMissingOperandMessage: <s extends DynamicState>(s: s) => `Token '&' requires a right operand before '${string}'` | `Token '|' requires a right operand before '${string}'` | `Expected an expression before '${string}'`;
|
|
13
13
|
export type writeMissingOperandMessage<s extends StaticState, operator extends Scanner.InfixToken | undefined = state.previousOperator<s>> = operator extends {} ? writeMissingRightOperandMessage<operator, s["unscanned"]> : writeExpressionExpectedMessage<s["unscanned"]>;
|
|
14
14
|
export type writeMissingRightOperandMessage<token extends Scanner.InfixToken, unscanned extends string> = `Token '${token}' requires a right operand${unscanned extends "" ? "" : ` before '${unscanned}'`}`;
|
|
15
15
|
export declare const writeMissingRightOperandMessage: <token extends Scanner.InfixToken, unscanned extends string>(token: token, unscanned: unscanned) => `Token '${token}' requires a right operand${unscanned extends "" ? "" : ` before '${unscanned}'`}`;
|
|
@@ -13,7 +13,7 @@ export declare const singleEqualsMessage = "= is not a valid comparator. Use ==
|
|
|
13
13
|
type singleEqualsMessage = typeof singleEqualsMessage;
|
|
14
14
|
export declare const parseRightBound: (s: DynamicState, comparator: Scanner.Comparator) => void;
|
|
15
15
|
export type parseRightBound<s extends StaticState, comparator extends Scanner.Comparator, unscanned extends string> = Scanner.shiftUntilNextTerminator<unscanned> extends Scanner.shiftResult<infer scanned, infer nextUnscanned> ? tryParseWellFormedNumber<scanned, writeInvalidLimitMessage<comparator, scanned>> extends infer limit ? limit extends number ? s["branches"]["range"] extends {} ? comparator extends MaxComparator ? state.reduceRange<s, s["branches"]["range"]["limit"], s["branches"]["range"]["comparator"], comparator, limit, nextUnscanned> : error<writeUnpairableComparatorMessage<comparator>> : state.reduceSingleBound<s, comparator, limit, nextUnscanned> : error<limit & string> : never : never;
|
|
16
|
-
export declare const writeInvalidLimitMessage: <comparator extends "==" | "
|
|
16
|
+
export declare const writeInvalidLimitMessage: <comparator extends "==" | "<" | ">" | "<=" | ">=", limit extends string>(comparator: comparator, limit: limit) => `Comparator ${comparator} must be followed by a number literal (was '${limit}')`;
|
|
17
17
|
export type writeInvalidLimitMessage<comparator extends Scanner.Comparator, limit extends string> = `Comparator ${comparator} must be followed by a number literal (was '${limit}')`;
|
|
18
18
|
export declare const writeEmptyRangeMessage: (range: Range) => string;
|
|
19
19
|
export type BoundableDomain = "string" | "number" | "object";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { asOut } from "../../
|
|
1
|
+
import type { asOut } from "../../scopes/type.js";
|
|
2
2
|
import type { nominal } from "../../utils/generics.js";
|
|
3
3
|
import type { inferDefinition, validateDefinition } from "../definition.js";
|
|
4
4
|
import type { PostfixParser, TupleExpression } from "./tuple.js";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { asIn } from "../../
|
|
1
|
+
import type { asIn } from "../../scopes/type.js";
|
|
2
2
|
import type { Problems } from "../../traverse/problems.js";
|
|
3
3
|
import type { inferDefinition, validateDefinition } from "../definition.js";
|
|
4
4
|
import type { PostfixParser, TupleExpression } from "./tuple.js";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { asIn, asOut } from "../../main.js";
|
|
2
1
|
import type { inferNode } from "../../nodes/infer.js";
|
|
3
2
|
import type { ResolvedNode, TypeNode } from "../../nodes/node.js";
|
|
3
|
+
import type { asIn, asOut } from "../../scopes/type.js";
|
|
4
4
|
import type { conform, constructor, error, List, returnOf } from "../../utils/generics.js";
|
|
5
5
|
import type { inferDefinition, ParseContext, validateDefinition } from "../definition.js";
|
|
6
6
|
import type { inferIntersection, inferUnion } from "../string/ast.js";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare class Cache<item = unknown> {
|
|
2
|
+
protected cache: {
|
|
3
|
+
[name in string]?: item;
|
|
4
|
+
};
|
|
5
|
+
get root(): {
|
|
6
|
+
readonly [name in string]?: item;
|
|
7
|
+
};
|
|
8
|
+
has(name: string): boolean;
|
|
9
|
+
get(name: string): item | undefined;
|
|
10
|
+
set(name: string, item: item): item;
|
|
11
|
+
}
|
|
12
|
+
export declare class FreezingCache<item = unknown> extends Cache<item> {
|
|
13
|
+
set(name: string, item: item): item;
|
|
14
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export declare const jsObjectsScope: import("./scope.js").Scope<[{
|
|
2
|
+
Function: (...args: any[]) => unknown;
|
|
3
|
+
Array: unknown[];
|
|
4
|
+
Date: Date;
|
|
5
|
+
Error: Error;
|
|
6
|
+
Map: Map<unknown, unknown>;
|
|
7
|
+
RegExp: RegExp;
|
|
8
|
+
Set: Set<unknown>;
|
|
9
|
+
Object: {
|
|
10
|
+
[x: string]: unknown;
|
|
11
|
+
};
|
|
12
|
+
String: String;
|
|
13
|
+
Number: Number;
|
|
14
|
+
Boolean: Boolean;
|
|
15
|
+
WeakMap: WeakMap<object, unknown>;
|
|
16
|
+
WeakSet: WeakSet<object>;
|
|
17
|
+
Promise: Promise<unknown>;
|
|
18
|
+
}, {}, false]>;
|
|
19
|
+
export declare const jsObjects: import("./scope.js").Space<{
|
|
20
|
+
Function: (...args: any[]) => unknown;
|
|
21
|
+
Array: unknown[];
|
|
22
|
+
Date: Date;
|
|
23
|
+
Error: Error;
|
|
24
|
+
Map: Map<unknown, unknown>;
|
|
25
|
+
RegExp: RegExp;
|
|
26
|
+
Set: Set<unknown>;
|
|
27
|
+
Object: {
|
|
28
|
+
[x: string]: unknown;
|
|
29
|
+
};
|
|
30
|
+
String: String;
|
|
31
|
+
Number: Number;
|
|
32
|
+
Boolean: Boolean;
|
|
33
|
+
WeakMap: WeakMap<object, unknown>;
|
|
34
|
+
WeakSet: WeakSet<object>;
|
|
35
|
+
Promise: Promise<unknown>;
|
|
36
|
+
}>;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { ResolvedNode, TypeNode } from "../nodes/node.js";
|
|
2
|
+
import type { inferDefinition, ParseContext, validateDefinition } from "../parse/definition.js";
|
|
3
|
+
import type { Dict, error, evaluateObject, isAny, List, nominal } from "../utils/generics.js";
|
|
4
|
+
import type { stringifyUnion } from "../utils/unionToTuple.js";
|
|
5
|
+
import { FreezingCache } from "./cache.js";
|
|
6
|
+
import type { PrecompiledDefaults } from "./standard.js";
|
|
7
|
+
import type { Type, TypeConfig, TypeParser } from "./type.js";
|
|
8
|
+
type ScopeParser = {
|
|
9
|
+
<aliases>(aliases: validateAliases<aliases, {}>): Scope<parseScope<aliases, {}>>;
|
|
10
|
+
<aliases, opts extends ScopeOptions>(aliases: validateAliases<aliases, opts>, opts: validateOptions<opts>): Scope<parseScope<aliases, opts>>;
|
|
11
|
+
};
|
|
12
|
+
export type ScopeOptions = {
|
|
13
|
+
imports?: Space[] | [];
|
|
14
|
+
includes?: Space[] | [];
|
|
15
|
+
standard?: boolean;
|
|
16
|
+
name?: string;
|
|
17
|
+
config?: TypeConfig;
|
|
18
|
+
};
|
|
19
|
+
type validateOptions<opts extends ScopeOptions> = {
|
|
20
|
+
[k in keyof opts]: k extends "imports" | "includes" ? mergeSpaces<opts[k], [
|
|
21
|
+
k,
|
|
22
|
+
"imports"
|
|
23
|
+
] extends ["includes", keyof opts] ? mergeSpaces<opts["includes"]> : {}> extends error<infer e> ? e : opts[k] : opts[k];
|
|
24
|
+
};
|
|
25
|
+
export type ScopeContext = Dict | ScopeContextTuple;
|
|
26
|
+
type ScopeContextTuple = [exports: Dict, locals: Dict, standard?: false];
|
|
27
|
+
type parseScope<aliases, opts extends ScopeOptions> = opts["standard"] extends false ? [inferExports<aliases, opts>, importsOf<opts>, false] : opts["imports"] extends Space[] ? [inferExports<aliases, opts>, importsOf<opts>] : inferExports<aliases, opts>;
|
|
28
|
+
type importsOf<opts extends ScopeOptions> = unknown extends opts["imports"] ? {} : mergeSpaces<opts["imports"]>;
|
|
29
|
+
type includesOf<opts extends ScopeOptions> = unknown extends opts["includes"] ? {} : mergeSpaces<opts["includes"]>;
|
|
30
|
+
export type resolve<name extends keyof $, $> = isAny<$[name]> extends true ? any : $[name] extends alias<infer def> ? inferDefinition<def, $> : $[name];
|
|
31
|
+
type exportsOf<context extends ScopeContext> = context extends [
|
|
32
|
+
infer exports,
|
|
33
|
+
...unknown[]
|
|
34
|
+
] ? exports : context;
|
|
35
|
+
type localsOf<context extends ScopeContext> = context extends List ? context["1"] & (context["2"] extends false ? {} : PrecompiledDefaults) : PrecompiledDefaults;
|
|
36
|
+
type mergeSpaces<scopes, base extends Dict = {}> = scopes extends readonly [
|
|
37
|
+
Space<infer head>,
|
|
38
|
+
...infer tail
|
|
39
|
+
] ? keyof base & keyof head extends never ? mergeSpaces<tail, base & head> : error<writeDuplicateAliasesMessage<stringifyUnion<keyof base & keyof head & string>>> : base;
|
|
40
|
+
type validateAliases<aliases, opts extends ScopeOptions> = {
|
|
41
|
+
[name in keyof aliases]: name extends keyof preresolved<opts> ? writeDuplicateAliasesMessage<name & string> : validateDefinition<aliases[name], bootstrapScope<aliases, opts>>;
|
|
42
|
+
};
|
|
43
|
+
type preresolved<opts extends ScopeOptions> = includesOf<opts> & importsOf<opts> & (opts["standard"] extends false ? {} : PrecompiledDefaults);
|
|
44
|
+
type alias<def = {}> = nominal<def, "alias">;
|
|
45
|
+
type bootstrapScope<aliases, opts extends ScopeOptions> = {
|
|
46
|
+
[k in keyof aliases]: alias<aliases[k]>;
|
|
47
|
+
} & preresolved<opts>;
|
|
48
|
+
type inferExports<aliases, opts extends ScopeOptions> = evaluateObject<{
|
|
49
|
+
[k in keyof aliases]: inferDefinition<aliases[k], bootstrapScope<aliases, opts>>;
|
|
50
|
+
} & includesOf<opts>>;
|
|
51
|
+
export type Space<exports = Dict> = {
|
|
52
|
+
[k in keyof exports]: Type<exports[k]>;
|
|
53
|
+
};
|
|
54
|
+
type resolutions<context extends ScopeContext> = localsOf<context> & exportsOf<context>;
|
|
55
|
+
type name<context extends ScopeContext> = keyof resolutions<context> & string;
|
|
56
|
+
export declare class Scope<context extends ScopeContext = any> {
|
|
57
|
+
#private;
|
|
58
|
+
aliases: Dict;
|
|
59
|
+
opts: ScopeOptions;
|
|
60
|
+
name: string;
|
|
61
|
+
config: TypeConfig | undefined;
|
|
62
|
+
parseCache: FreezingCache<TypeNode>;
|
|
63
|
+
constructor(aliases: Dict, opts: ScopeOptions);
|
|
64
|
+
type: TypeParser<resolutions<context>>;
|
|
65
|
+
createAnonymousTypeSuffix(): number;
|
|
66
|
+
get infer(): exportsOf<context>;
|
|
67
|
+
compile(): Space<exportsOf<context>>;
|
|
68
|
+
addReferenceIfResolvable(name: name<context>, ctx: ParseContext): boolean;
|
|
69
|
+
resolve(name: name<context>): Type;
|
|
70
|
+
resolveNode(node: TypeNode): ResolvedNode;
|
|
71
|
+
}
|
|
72
|
+
export declare const writeShallowCycleErrorMessage: (name: string, seen: string[]) => string;
|
|
73
|
+
export declare const scope: ScopeParser;
|
|
74
|
+
export declare const writeDuplicateAliasesMessage: <name_1 extends string>(name: name_1) => `Alias '${name_1}' is already defined`;
|
|
75
|
+
type writeDuplicateAliasesMessage<name extends string> = `Alias '${name}' is already defined`;
|
|
76
|
+
export {};
|