arktype 2.0.0-beta.2 → 2.0.0-beta.4
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/out/ark.d.ts +18 -4
- package/out/ark.js +22 -3
- package/out/ast.d.ts +217 -0
- package/out/ast.js +3 -0
- package/out/config.d.ts +12 -0
- package/out/generic.d.ts +106 -20
- package/out/index.d.ts +6 -5
- package/out/index.js +3 -2
- package/out/intersect.d.ts +28 -0
- package/out/intersect.js +1 -0
- package/out/keys.d.ts +16 -0
- package/out/keys.js +1 -0
- package/out/keywords/arkGenerics.d.ts +19 -0
- package/out/keywords/arkGenerics.js +19 -0
- package/out/keywords/format.d.ts +11 -0
- package/out/keywords/format.js +31 -0
- package/out/keywords/internal.d.ts +11 -0
- package/out/keywords/internal.js +25 -0
- package/out/keywords/jsObjects.d.ts +15 -0
- package/out/keywords/jsObjects.js +13 -0
- package/out/keywords/parsing.d.ts +16 -0
- package/out/keywords/parsing.js +77 -0
- package/out/keywords/platformObjects.d.ts +13 -0
- package/out/keywords/platformObjects.js +12 -0
- package/out/keywords/tsGenerics.d.ts +37 -0
- package/out/keywords/tsGenerics.js +45 -0
- package/out/keywords/tsKeywords.d.ts +18 -0
- package/out/keywords/tsKeywords.js +17 -0
- package/out/keywords/typedArray.d.ts +16 -0
- package/out/keywords/typedArray.js +14 -0
- package/out/keywords/utils/creditCard.d.ts +2 -0
- package/out/keywords/utils/creditCard.js +25 -0
- package/out/keywords/utils/date.d.ts +20 -0
- package/out/keywords/utils/date.js +36 -0
- package/out/keywords/utils/ip.d.ts +3 -0
- package/out/keywords/utils/ip.js +20 -0
- package/out/keywords/utils/regex.d.ts +2 -0
- package/out/keywords/utils/regex.js +9 -0
- package/out/keywords/validation.d.ts +19 -0
- package/out/keywords/validation.js +84 -0
- package/out/module.d.ts +19 -11
- package/out/module.js +1 -0
- package/out/parser/definition.d.ts +5 -4
- package/out/parser/objectLiteral.d.ts +7 -6
- package/out/parser/objectLiteral.js +2 -2
- package/out/parser/semantic/bounds.d.ts +2 -1
- package/out/parser/semantic/divisor.d.ts +2 -2
- package/out/parser/semantic/infer.d.ts +14 -9
- package/out/parser/semantic/keyof.d.ts +7 -0
- package/out/parser/semantic/keyof.js +1 -0
- package/out/parser/semantic/validate.d.ts +4 -5
- package/out/parser/semantic/validate.js +0 -1
- package/out/parser/string/reduce/dynamic.d.ts +2 -1
- package/out/parser/string/reduce/shared.d.ts +1 -1
- package/out/parser/string/reduce/static.d.ts +1 -1
- package/out/parser/string/shift/operand/date.d.ts +3 -1
- package/out/parser/string/shift/operand/enclosed.js +1 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts +4 -4
- package/out/parser/string/shift/operand/unenclosed.d.ts +13 -8
- package/out/parser/string/shift/operator/bounds.d.ts +2 -1
- package/out/parser/string/shift/operator/bounds.js +4 -4
- package/out/parser/string/shift/operator/default.d.ts +2 -1
- package/out/parser/string/string.d.ts +2 -1
- package/out/parser/tuple.d.ts +5 -3
- package/out/parser/tuple.js +5 -5
- package/out/scope.d.ts +47 -26
- package/out/scope.js +2 -7
- package/out/subtypes/array.d.ts +13 -0
- package/out/subtypes/array.js +1 -0
- package/out/subtypes/base.d.ts +83 -0
- package/out/subtypes/base.js +1 -0
- package/out/subtypes/date.d.ts +11 -0
- package/out/subtypes/date.js +1 -0
- package/out/subtypes/instantiate.d.ts +16 -0
- package/out/subtypes/instantiate.js +1 -0
- package/out/subtypes/morph.d.ts +13 -0
- package/out/subtypes/morph.js +1 -0
- package/out/subtypes/number.d.ts +12 -0
- package/out/subtypes/number.js +1 -0
- package/out/subtypes/object.d.ts +27 -0
- package/out/subtypes/object.js +1 -0
- package/out/subtypes/string.d.ts +13 -0
- package/out/subtypes/string.js +1 -0
- package/out/subtypes/validator.d.ts +11 -0
- package/out/subtypes/validator.js +1 -0
- package/out/type.d.ts +17 -71
- package/package.json +3 -3
- package/out/match.d.ts +0 -71
- package/out/match.js +0 -50
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { type BaseRoot, type
|
|
2
|
-
import { type anyOrNever, type Dict, type ErrorMessage, type ErrorType, type EscapeToken, type Key, type listable, type merge, type show } from "@ark/util";
|
|
1
|
+
import { type BaseRoot, type Index, type Optional, type Required, type UndeclaredKeyBehavior, type writeInvalidPropertyKeyMessage } from "@ark/schema";
|
|
2
|
+
import { type anyOrNever, type conform, type Dict, type ErrorMessage, type ErrorType, type EscapeToken, type Key, type listable, type merge, type show } from "@ark/util";
|
|
3
|
+
import type { constrain, Default } from "../ast.js";
|
|
3
4
|
import type { ParseContext } from "../scope.js";
|
|
4
5
|
import type { inferDefinition, validateDefinition } from "./definition.js";
|
|
5
6
|
import type { astToString } from "./semantic/utils.js";
|
|
@@ -17,7 +18,7 @@ type _inferObjectLiteral<def extends object, $, args> = {
|
|
|
17
18
|
-readonly [k in keyof def as optionalKeyFrom<k>]?: inferDefinition<def[k], $, args>;
|
|
18
19
|
};
|
|
19
20
|
export type validateObjectLiteral<def, $, args> = {
|
|
20
|
-
[k in keyof def]: k extends IndexKey<infer indexDef> ? validateString<indexDef, $, args> extends ErrorMessage<infer message> ? ErrorType<message> : inferDefinition<indexDef, $, args> extends
|
|
21
|
+
[k in keyof def]: k extends IndexKey<infer indexDef> ? validateString<indexDef, $, args> extends ErrorMessage<infer message> ? ErrorType<message> : inferDefinition<indexDef, $, args> extends Key | constrain<Key, {}> ? validateDefinition<def[k], $, args> : ErrorType<writeInvalidPropertyKeyMessage<indexDef>> : k extends "..." ? inferDefinition<def[k], $, args> extends object ? validateDefinition<def[k], $, args> : ErrorType<writeInvalidSpreadTypeMessage<astToString<def[k]>>> : k extends "+" ? UndeclaredKeyBehavior : validateDefaultableValue<def, k, $, args>;
|
|
21
22
|
};
|
|
22
23
|
type validateDefaultableValue<def, k extends keyof def, $, args> = def[k] extends DefaultValueTuple ? validateDefaultValueTuple<def[k], k, $, args> : validateDefinition<def[k], $, args>;
|
|
23
24
|
type DefaultValueTuple<baseDef = unknown, defaultValue = unknown> = readonly [
|
|
@@ -25,11 +26,11 @@ type DefaultValueTuple<baseDef = unknown, defaultValue = unknown> = readonly [
|
|
|
25
26
|
"=",
|
|
26
27
|
defaultValue
|
|
27
28
|
];
|
|
28
|
-
type validateDefaultValueTuple<def extends DefaultValueTuple, k extends PropertyKey, $, args> = parseKey<k>["kind"] extends "required" ? readonly [
|
|
29
|
+
type validateDefaultValueTuple<def extends DefaultValueTuple, k extends PropertyKey, $, args> = parseKey<k>["kind"] extends "required" ? conform<def, readonly [
|
|
29
30
|
validateDefinition<def[0], $, args>,
|
|
30
31
|
"=",
|
|
31
32
|
inferDefinition<def[0], $, args>
|
|
32
|
-
] : ErrorMessage<invalidDefaultKeyKindMessage>;
|
|
33
|
+
]> : ErrorMessage<invalidDefaultKeyKindMessage>;
|
|
33
34
|
type nonOptionalKeyFrom<k, $, args> = parseKey<k> extends PreparsedKey<"required", infer inner> ? inner : parseKey<k> extends PreparsedKey<"index", infer inner> ? inferDefinition<inner, $, args> extends infer t extends Key ? t : never : never;
|
|
34
35
|
type optionalKeyFrom<k> = parseKey<k> extends PreparsedKey<"optional", infer inner> ? inner : never;
|
|
35
36
|
type PreparsedKey<kind extends ParsedKeyKind = ParsedKeyKind, key extends Key = Key> = {
|
|
@@ -42,7 +43,7 @@ declare namespace PreparsedKey {
|
|
|
42
43
|
type ParsedKeyKind = "required" | "optional" | "index" | MetaKey;
|
|
43
44
|
export type MetaKey = "..." | "+";
|
|
44
45
|
export type IndexKey<def extends string = string> = `[${def}]`;
|
|
45
|
-
export type ParsedEntry = ParsedUndeclaredEntry | ParsedSpreadEntry |
|
|
46
|
+
export type ParsedEntry = ParsedUndeclaredEntry | ParsedSpreadEntry | Required.Node | Optional.Node | Index.Node;
|
|
46
47
|
export type ParsedUndeclaredEntry = {
|
|
47
48
|
kind: "undeclared";
|
|
48
49
|
behavior: UndeclaredKeyBehavior;
|
|
@@ -29,7 +29,7 @@ export const parseObjectLiteral = (def, ctx) => {
|
|
|
29
29
|
structure[entry.kind] = append(structure[entry.kind], entry);
|
|
30
30
|
}
|
|
31
31
|
const structureNode = ctx.$.node("structure", structure);
|
|
32
|
-
return ctx.$.
|
|
32
|
+
return ctx.$.rootNode({
|
|
33
33
|
domain: "object",
|
|
34
34
|
structure: spread?.merge(structureNode) ?? structureNode
|
|
35
35
|
});
|
|
@@ -68,7 +68,7 @@ export const parseEntry = (key, value, ctx) => {
|
|
|
68
68
|
normalized.required ?
|
|
69
69
|
[normalized.index, ...normalized.required]
|
|
70
70
|
: normalized.index
|
|
71
|
-
: normalized.required ?? []);
|
|
71
|
+
: (normalized.required ?? []));
|
|
72
72
|
}
|
|
73
73
|
return ctx.$.node(parsedKey.kind, {
|
|
74
74
|
key: parsedKey.key,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { writeUnboundableMessage } from "@ark/schema";
|
|
2
2
|
import type { ErrorMessage, array, typeToString } from "@ark/util";
|
|
3
|
+
import type { LimitLiteral } from "../../ast.js";
|
|
3
4
|
import type { Comparator } from "../string/reduce/shared.js";
|
|
4
5
|
import type { BoundExpressionKind, writeInvalidLimitMessage } from "../string/shift/operator/bounds.js";
|
|
5
6
|
import type { inferAstIn } from "./infer.js";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { writeIndivisibleMessage } from "@ark/schema";
|
|
2
2
|
import type { ErrorMessage } from "@ark/util";
|
|
3
3
|
import type { inferAstIn } from "./infer.js";
|
|
4
4
|
import type { validateAst } from "./validate.js";
|
|
5
5
|
export type validateDivisor<l, $, args> = inferAstIn<l, $, args> extends infer data ? [
|
|
6
6
|
data
|
|
7
|
-
] extends [number] ? validateAst<l, $, args> : ErrorMessage<writeIndivisibleMessage<
|
|
7
|
+
] extends [number] ? validateAst<l, $, args> : ErrorMessage<writeIndivisibleMessage<data>> : never;
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { BigintLiteral, array } from "@ark/util";
|
|
1
|
+
import type { GenericAst } from "@ark/schema";
|
|
2
|
+
import type { BigintLiteral, Hkt, array } from "@ark/util";
|
|
3
|
+
import type { Ark } from "../../ark.js";
|
|
4
|
+
import type { Date, DateLiteral, Default, LimitLiteral, RegexLiteral, applyConstraint, distillIn, distillOut, normalizeLimit, string } from "../../ast.js";
|
|
5
|
+
import type { inferIntersection } from "../../intersect.js";
|
|
3
6
|
import type { UnparsedScope, resolve, tryInferSubmoduleReference } from "../../scope.js";
|
|
4
7
|
import type { inferDefinition } from "../definition.js";
|
|
5
8
|
import type { Comparator, MinComparator } from "../string/reduce/shared.js";
|
|
@@ -8,13 +11,14 @@ export type inferAstRoot<ast, $, args> = inferConstrainableAst<ast, $, args>;
|
|
|
8
11
|
export type inferAstIn<ast, $, args> = distillIn<inferAstRoot<ast, $, args>>;
|
|
9
12
|
export type inferAstOut<ast, $, args> = distillOut<inferAstRoot<ast, $, args>>;
|
|
10
13
|
export type inferConstrainableAst<ast, $, args> = ast extends array ? inferExpression<ast, $, args> : ast extends string ? inferTerminal<ast, $, args> : never;
|
|
11
|
-
export type GenericInstantiationAst<generic extends
|
|
12
|
-
|
|
14
|
+
export type GenericInstantiationAst<generic extends GenericAst = GenericAst, argAsts extends unknown[] = unknown[]> = [generic, "<>", argAsts];
|
|
15
|
+
type resolveScope<g$, $> = g$ extends UnparsedScope ? $ : g$;
|
|
16
|
+
export type inferExpression<ast extends array, $, args> = ast extends GenericInstantiationAst<infer g, infer argAsts> ? g["bodyDef"] extends Hkt ? Hkt.apply<g["bodyDef"], {
|
|
13
17
|
[i in keyof argAsts]: inferConstrainableAst<argAsts[i], $, args>;
|
|
14
|
-
}> : inferDefinition<
|
|
15
|
-
[i in keyof
|
|
16
|
-
}> : ast[1] extends "[]" ? inferConstrainableAst<ast[0], $, args>[] : ast[1] extends "|" ? inferConstrainableAst<ast[0], $, args> | inferConstrainableAst<ast[2], $, args> : ast[1] extends "&" ? inferIntersection<inferConstrainableAst<ast[0], $, args>, inferConstrainableAst<ast[2], $, args>> : ast[1] extends "=" ? inferTerminal<ast[2] & string, $, args> extends infer defaultValue ? (In?: inferConstrainableAst<ast[0], $, args>) => Default<defaultValue> : never : ast[1] extends Comparator ? ast[0] extends LimitLiteral ? constrainBound<inferConstrainableAst<ast[2], $, args>, ast[1], ast[0]> : constrainBound<inferConstrainableAst<ast[0], $, args>, ast[1], ast[2]> : ast[1] extends "%" ?
|
|
17
|
-
export type constrainBound<constrainableIn, comparator extends Comparator, limit> = distillIn<constrainableIn> extends infer In ? comparator extends "==" ? In extends number ? limit : In extends Date ? Date.literal<normalizeLimit<limit>> :
|
|
18
|
+
}> : inferDefinition<g["bodyDef"], resolveScope<g["$"], $>, {
|
|
19
|
+
[i in keyof g["names"] & `${number}` as g["names"][i]]: inferConstrainableAst<argAsts[i & keyof argAsts], resolveScope<g["arg$"], $>, args>;
|
|
20
|
+
}> : ast[1] extends "[]" ? inferConstrainableAst<ast[0], $, args>[] : ast[1] extends "|" ? inferConstrainableAst<ast[0], $, args> | inferConstrainableAst<ast[2], $, args> : ast[1] extends "&" ? inferIntersection<inferConstrainableAst<ast[0], $, args>, inferConstrainableAst<ast[2], $, args>> : ast[1] extends "=" ? inferTerminal<ast[2] & string, $, args> extends infer defaultValue ? (In?: inferConstrainableAst<ast[0], $, args>) => Default<defaultValue> : never : ast[1] extends Comparator ? ast[0] extends LimitLiteral ? constrainBound<inferConstrainableAst<ast[2], $, args>, ast[1], ast[0]> : constrainBound<inferConstrainableAst<ast[0], $, args>, ast[1], ast[2]> : ast[1] extends "%" ? applyConstraint<inferConstrainableAst<ast[0], $, args>, "divisor", ast[2] & number> : ast[0] extends "keyof" ? keyof inferConstrainableAst<ast[1], $, args> : never;
|
|
21
|
+
export type constrainBound<constrainableIn, comparator extends Comparator, limit> = distillIn<constrainableIn> extends infer In ? comparator extends "==" ? In extends number ? limit : In extends Date ? Date.literal<normalizeLimit<limit>> : applyConstraint<constrainableIn, "exactLength", limit & number> : applyConstraint<constrainableIn, In extends number ? comparator extends MinComparator ? "min" : "max" : In extends string | array ? comparator extends MinComparator ? "minLength" : "maxLength" : comparator extends MinComparator ? "after" : "before", {
|
|
18
22
|
rule: normalizeLimit<limit>;
|
|
19
23
|
exclusive: comparator extends ">" | "<" ? true : false;
|
|
20
24
|
}> : never;
|
|
@@ -24,4 +28,5 @@ export type PostfixOperator = "[]";
|
|
|
24
28
|
export type PostfixExpression<operator extends PostfixOperator = PostfixOperator, operand = unknown> = readonly [operand, operator];
|
|
25
29
|
export type InfixOperator = "|" | "&" | Comparator | "%" | ":" | "=>" | "@";
|
|
26
30
|
export type InfixExpression<operator extends InfixOperator = InfixOperator, l = unknown, r = unknown> = [l, operator, r];
|
|
27
|
-
export type inferTerminal<token extends string, $, args> = token extends keyof args | keyof $ ? resolve<token, $, args> : token extends keyof
|
|
31
|
+
export type inferTerminal<token extends string, $, args> = token extends keyof args | keyof $ ? resolve<token, $, args> : token extends keyof Ark ? Ark[token] : `#${token}` extends keyof $ ? resolve<`#${token}`, $, args> : token extends StringLiteral<infer text> ? text : token extends `${infer n extends number}` ? n : token extends BigintLiteral<infer b> ? b : token extends RegexLiteral<infer source> ? string.matching<source> : token extends DateLiteral<infer source> ? Date.literal<source> : tryInferSubmoduleReference<$, token>;
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { writeNonStructuralOperandMessage } from "@ark/schema";
|
|
2
|
+
import type { ErrorMessage, typeToString } from "@ark/util";
|
|
3
|
+
import type { inferAstRoot } from "./infer.js";
|
|
4
|
+
import type { validateAst } from "./validate.js";
|
|
5
|
+
export type validateKeyof<operandAst, $, args> = inferAstRoot<operandAst, $, args> extends infer data ? [
|
|
6
|
+
data
|
|
7
|
+
] extends [object] ? validateAst<operandAst, $, args> : ErrorMessage<writeNonStructuralOperandMessage<"keyof", typeToString<data>>> : never;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { arkKind, GenericParamAst,
|
|
1
|
+
import type { arkKind, GenericParamAst, PrivateDeclaration, writeMissingSubmoduleAccessMessage, writeUnsatisfiedParameterConstraintMessage } from "@ark/schema";
|
|
2
2
|
import type { anyOrNever, array, BigintLiteral, charsAfterFirst, Completion, ErrorMessage, typeToString, writeMalformedNumericLiteralMessage } from "@ark/util";
|
|
3
3
|
import type { Generic } from "../../generic.js";
|
|
4
4
|
import type { Comparator } from "../string/reduce/shared.js";
|
|
@@ -9,19 +9,18 @@ import type { validateRange } from "./bounds.js";
|
|
|
9
9
|
import type { validateDefault } from "./default.js";
|
|
10
10
|
import type { validateDivisor } from "./divisor.js";
|
|
11
11
|
import type { GenericInstantiationAst, inferAstRoot, InfixExpression, PostfixExpression } from "./infer.js";
|
|
12
|
+
import type { validateKeyof } from "./keyof.js";
|
|
12
13
|
import type { astToString } from "./utils.js";
|
|
13
|
-
export type validateAst<ast, $, args> = ast extends string ? validateStringAst<ast, $> : ast extends PostfixExpression<infer operator, infer operand> ? operator extends "[]" ? validateAst<operand, $, args> : never : ast extends InfixExpression<infer operator, infer l, infer r> ? operator extends "&" | "|" ? validateInfix<ast, $, args> : operator extends Comparator ? validateRange<l, operator, r, $, args> : operator extends "%" ? validateDivisor<l, $, args> : undefined : ast extends
|
|
14
|
+
export type validateAst<ast, $, args> = ast extends string ? validateStringAst<ast, $> : ast extends PostfixExpression<infer operator, infer operand> ? operator extends "[]" ? validateAst<operand, $, args> : never : ast extends InfixExpression<infer operator, infer l, infer r> ? operator extends "&" | "|" ? validateInfix<ast, $, args> : operator extends Comparator ? validateRange<l, operator, r, $, args> : operator extends "%" ? validateDivisor<l, $, args> : undefined : ast extends [infer baseAst, "=", infer unitLiteral extends UnitLiteral] ? validateDefault<baseAst, unitLiteral, $, args> : ast extends ["keyof", infer operand] ? validateKeyof<operand, $, args> : ast extends GenericInstantiationAst<infer g, infer argAsts> ? validateGenericArgs<g["paramsAst"], argAsts, $, args, []> : ErrorMessage<writeUnexpectedExpressionMessage<astToString<ast>>> & {
|
|
14
15
|
ast: ast;
|
|
15
16
|
};
|
|
16
17
|
type writeUnexpectedExpressionMessage<expression extends string> = `Unexpectedly failed to parse the expression resulting from ${expression}`;
|
|
17
18
|
type validateGenericArgs<params extends array<GenericParamAst>, argAsts extends array, $, args, indices extends 1[]> = argAsts extends readonly [infer arg, ...infer argsTail] ? validateAst<arg, $, args> extends infer e extends ErrorMessage ? e : inferAstRoot<arg, $, args> extends params[indices["length"]][1] ? validateGenericArgs<params, argsTail, $, args, [...indices, 1]> : ErrorMessage<writeUnsatisfiedParameterConstraintMessage<params[indices["length"]][0], typeToString<params[indices["length"]][1]>, astToString<arg>>> : undefined;
|
|
18
|
-
export declare const writeUnsatisfiableExpressionError: <expression extends string>(expression: expression) => writeUnsatisfiableExpressionError<expression>;
|
|
19
|
-
export type writeUnsatisfiableExpressionError<expression extends string> = `${expression} results in an unsatisfiable type`;
|
|
20
19
|
export declare const writePrefixedPrivateReferenceMessage: <def extends PrivateDeclaration>(def: def) => writePrefixedPrivateReferenceMessage<def>;
|
|
21
20
|
export type writePrefixedPrivateReferenceMessage<def extends PrivateDeclaration> = `Private type references should not include '#'. Use '${charsAfterFirst<def>}' instead.`;
|
|
22
21
|
type validateStringAst<def extends string, $> = def extends `${infer n extends number}` ? number extends n ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "number">> : undefined : def extends BigintLiteral<infer b> ? bigint extends b ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "bigint">> : undefined : maybeExtractAlias<def, $> extends infer alias extends keyof $ ? [
|
|
23
22
|
$[alias]
|
|
24
|
-
] extends [anyOrNever] ? def : def extends PrivateDeclaration ? ErrorMessage<writePrefixedPrivateReferenceMessage<def>> : $[alias] extends
|
|
23
|
+
] extends [anyOrNever] ? def : def extends PrivateDeclaration ? ErrorMessage<writePrefixedPrivateReferenceMessage<def>> : $[alias] extends Generic ? ErrorMessage<writeInvalidGenericArgCountMessage<def, $[alias]["names"], []>> : $[alias] extends {
|
|
25
24
|
[arkKind]: "module";
|
|
26
25
|
} ? ErrorMessage<writeMissingSubmoduleAccessMessage<def>> : undefined : def extends ErrorMessage ? def : undefined;
|
|
27
26
|
export type maybeExtractAlias<def extends string, $> = def extends keyof $ ? def : `#${def}` extends keyof $ ? `#${def}` : null;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { BaseRoot
|
|
1
|
+
import type { BaseRoot } from "@ark/schema";
|
|
2
2
|
import { type requireKeys } from "@ark/util";
|
|
3
|
+
import type { LimitLiteral } from "../../../ast.js";
|
|
3
4
|
import type { ParseContext } from "../../../scope.js";
|
|
4
5
|
import type { InfixOperator } from "../../semantic/infer.js";
|
|
5
6
|
import type { Scanner } from "../shift/scanner.js";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { LimitLiteral } from "@ark/schema";
|
|
2
1
|
import type { Completion, ErrorMessage, defined } from "@ark/util";
|
|
2
|
+
import type { LimitLiteral } from "../../../ast.js";
|
|
3
3
|
import type { Scanner } from "../shift/scanner.js";
|
|
4
4
|
import type { Comparator, InvertedComparators, MaxComparator, MinComparator, OpenLeftBound, StringifiablePrefixOperator, writeMultipleLeftBoundsMessage, writeOpenRangeMessage, writeUnclosedGroupMessage, writeUnmatchedGroupCloseMessage, writeUnpairableComparatorMessage } from "./shared.js";
|
|
5
5
|
export type StaticState = {
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import type { DateLiteral } from "
|
|
1
|
+
import type { DateLiteral } from "../../../../ast.js";
|
|
2
2
|
export declare const isDateLiteral: (value: unknown) => value is DateLiteral;
|
|
3
3
|
export declare const isValidDate: (d: Date) => boolean;
|
|
4
4
|
export declare const extractDateLiteralSource: <literal extends DateLiteral>(literal: literal) => extractDateLiteralSource<literal>;
|
|
5
|
+
type extractDateLiteralSource<literal extends DateLiteral> = literal extends DateLiteral<infer source> ? source : never;
|
|
5
6
|
export declare const writeInvalidDateMessage: <source extends string>(source: source) => writeInvalidDateMessage<source>;
|
|
6
7
|
export type writeInvalidDateMessage<source extends string> = `'${source}' could not be parsed by the Date constructor`;
|
|
7
8
|
export type DateInput = ConstructorParameters<typeof Date>[0];
|
|
8
9
|
export type DateParseResult<errorOnFail extends boolean | string = boolean | string> = Date | (errorOnFail extends true | string ? never : undefined);
|
|
9
10
|
export declare const tryParseDate: <errorOnFail extends boolean | string>(source: string, errorOnFail?: errorOnFail) => DateParseResult<errorOnFail>;
|
|
11
|
+
export {};
|
|
@@ -18,7 +18,7 @@ export const parseEnclosed = (s, enclosing) => {
|
|
|
18
18
|
s.root = s.ctx.$.node("unit", { unit: enclosed });
|
|
19
19
|
else {
|
|
20
20
|
const date = tryParseDate(enclosed, writeInvalidDateMessage(enclosed));
|
|
21
|
-
s.root = s.ctx.$.node("unit", {
|
|
21
|
+
s.root = s.ctx.$.node("unit", { meta: enclosed, unit: date });
|
|
22
22
|
}
|
|
23
23
|
};
|
|
24
24
|
export const enclosingQuote = {
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import type { BaseRoot,
|
|
1
|
+
import type { BaseRoot, GenericAst, genericParamNames, GenericRoot } from "@ark/schema";
|
|
2
2
|
import type { array, ErrorMessage, join } from "@ark/util";
|
|
3
3
|
import type { DynamicState } from "../../reduce/dynamic.js";
|
|
4
4
|
import { writeUnclosedGroupMessage } from "../../reduce/shared.js";
|
|
5
5
|
import type { state, StaticState } from "../../reduce/static.js";
|
|
6
6
|
import type { parseUntilFinalizer } from "../../string.js";
|
|
7
7
|
export declare const parseGenericArgs: (name: string, g: GenericRoot, s: DynamicState) => BaseRoot[];
|
|
8
|
-
export type parseGenericArgs<name extends string, g extends
|
|
8
|
+
export type parseGenericArgs<name extends string, g extends GenericAst, unscanned extends string, $, args> = _parseGenericArgs<name, g, unscanned, $, args, [], []>;
|
|
9
9
|
declare const _parseGenericArgs: (name: string, g: GenericRoot, s: DynamicState, argNodes: BaseRoot[]) => BaseRoot[];
|
|
10
10
|
export type ParsedArgs<result extends unknown[] = unknown[], unscanned extends string = string> = {
|
|
11
11
|
result: result;
|
|
12
12
|
unscanned: unscanned;
|
|
13
13
|
};
|
|
14
|
-
type _parseGenericArgs<name extends string, g extends
|
|
14
|
+
type _parseGenericArgs<name extends string, g extends GenericAst, unscanned extends string, $, args, argDefs extends string[], argAsts extends unknown[]> = parseUntilFinalizer<state.initialize<unscanned>, $, args> extends (infer finalArgState extends StaticState) ? {
|
|
15
15
|
defs: [
|
|
16
16
|
...argDefs,
|
|
17
17
|
finalArgState["scanned"] extends `${infer def}${"," | ">"}` ? def : finalArgState["scanned"]
|
|
@@ -22,7 +22,7 @@ type _parseGenericArgs<name extends string, g extends GenericProps, unscanned ex
|
|
|
22
22
|
defs: infer nextDefs extends string[];
|
|
23
23
|
asts: infer nextAsts extends unknown[];
|
|
24
24
|
unscanned: infer nextUnscanned extends string;
|
|
25
|
-
}) ? finalArgState["finalizer"] extends ">" ? nextAsts["length"] extends g["
|
|
25
|
+
}) ? finalArgState["finalizer"] extends ">" ? nextAsts["length"] extends g["paramsAst"]["length"] ? ParsedArgs<nextAsts, nextUnscanned> : state.error<writeInvalidGenericArgCountMessage<name, genericParamNames<g["paramsAst"]>, nextDefs>> : finalArgState["finalizer"] extends "," ? _parseGenericArgs<name, g, nextUnscanned, $, args, nextDefs, nextAsts> : finalArgState["finalizer"] extends ErrorMessage ? finalArgState : state.error<writeUnclosedGroupMessage<">">> : never : never;
|
|
26
26
|
export declare const writeInvalidGenericArgCountMessage: <name extends string, params extends array<string>, argDefs extends array<string>>(name: name, params: params, argDefs: argDefs) => writeInvalidGenericArgCountMessage<name, params, argDefs>;
|
|
27
27
|
export type writeInvalidGenericArgCountMessage<name extends string, params extends array<string>, argDefs extends array<string>> = `${name}<${join<params, ", ">}> requires exactly ${params["length"]} args (got ${argDefs["length"]}${argDefs["length"] extends (0) ? "" : `: ${join<argDefs, ",">}`})`;
|
|
28
28
|
export {};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { BaseRoot, writeUnresolvableMessage, type
|
|
1
|
+
import { BaseRoot, writeUnresolvableMessage, type GenericAst, type GenericRoot, type arkKind, type genericParamNames, type resolvableReferenceIn, type resolveReference, type writeNonSubmoduleDotMessage } from "@ark/schema";
|
|
2
2
|
import { type BigintLiteral, type Completion, type ErrorMessage, type anyOrNever, type join } from "@ark/util";
|
|
3
|
+
import type { Ark } from "../../../../ark.js";
|
|
3
4
|
import type { GenericInstantiationAst } from "../../../semantic/infer.js";
|
|
4
5
|
import type { DynamicState } from "../../reduce/dynamic.js";
|
|
5
6
|
import type { StaticState, state } from "../../reduce/static.js";
|
|
@@ -7,21 +8,25 @@ import type { BaseCompletions } from "../../string.js";
|
|
|
7
8
|
import type { Scanner } from "../scanner.js";
|
|
8
9
|
import { parseGenericArgs, writeInvalidGenericArgCountMessage, type ParsedArgs } from "./genericArgs.js";
|
|
9
10
|
export declare const parseUnenclosed: (s: DynamicState) => void;
|
|
10
|
-
export type parseUnenclosed<s extends StaticState, $, args> = Scanner.shiftUntilNextTerminator<s["unscanned"]> extends (Scanner.shiftResult<infer token, infer unscanned>) ? token extends "keyof" ? state.addPrefix<s, "keyof", unscanned> : tryResolve<s, token, $, args> extends infer result ? result extends ErrorMessage<infer message> ? state.error<message> : result extends resolvableReferenceIn<$> ? parseResolution<s, unscanned, result, resolveReference<result, $>, $, args> : result extends resolvableReferenceIn<
|
|
11
|
+
export type parseUnenclosed<s extends StaticState, $, args> = Scanner.shiftUntilNextTerminator<s["unscanned"]> extends (Scanner.shiftResult<infer token, infer unscanned>) ? token extends "keyof" ? state.addPrefix<s, "keyof", unscanned> : tryResolve<s, token, $, args> extends infer result ? result extends ErrorMessage<infer message> ? state.error<message> : result extends resolvableReferenceIn<$> ? parseResolution<s, unscanned, result, resolveReference<result, $>, $, args> : result extends resolvableReferenceIn<Ark> ? parseResolution<s, unscanned, result, resolveReference<result, Ark>, $, args> : state.setRoot<s, result, unscanned> : never : never;
|
|
11
12
|
type parseResolution<s extends StaticState, unscanned extends string, alias extends string, resolution, $, args> = [
|
|
12
13
|
resolution
|
|
13
|
-
] extends [anyOrNever] ? state.setRoot<s, alias, unscanned> : resolution extends
|
|
14
|
+
] extends [anyOrNever] ? state.setRoot<s, alias, unscanned> : resolution extends GenericAst ? parseGenericInstantiation<alias, resolution, state.scanTo<s, unscanned>, $, args> : state.setRoot<s, alias, unscanned>;
|
|
14
15
|
export declare const parseGenericInstantiation: (name: string, g: GenericRoot, s: DynamicState) => BaseRoot;
|
|
15
|
-
export type parseGenericInstantiation<name extends string, g extends
|
|
16
|
-
|
|
17
|
-
type
|
|
16
|
+
export type parseGenericInstantiation<name extends string, g extends GenericAst, s extends StaticState, $, args> = Scanner.skipWhitespace<s["unscanned"]> extends `<${infer unscanned}` ? parseGenericArgs<name, g, unscanned, $, args> extends infer result ? result extends ParsedArgs<infer argAsts, infer nextUnscanned> ? state.setRoot<s, GenericInstantiationAst<g, argAsts>, nextUnscanned> : result : never : state.error<writeInvalidGenericArgCountMessage<name, genericParamNames<g["paramsAst"]>, [
|
|
17
|
+
]>>;
|
|
18
|
+
type tryResolve<s extends StaticState, token extends string, $, args> = token extends keyof Ark ? token : token extends keyof $ ? token : `#${token}` extends keyof $ ? token : token extends keyof args ? token : token extends `${number}` ? token : token extends BigintLiteral ? token : token extends (`${infer submodule extends (keyof $ | keyof Ark) & string}.${infer reference}`) ? tryResolveSubmodule<token, submodule, reference, s, $ & Ark, [submodule]> : unresolvableError<s, token, $, args, []>;
|
|
19
|
+
type tryResolveSubmodule<token, submodule extends keyof $ & string, reference extends string, s extends StaticState, $, submodulePath extends string[]> = $[submodule] extends {
|
|
18
20
|
[arkKind]: "module";
|
|
19
|
-
} ? reference extends keyof $[submodule] ? token :
|
|
21
|
+
} ? reference extends keyof $[submodule] ? token : reference extends (`${infer nestedSubmodule extends keyof $[submodule] & string}.${infer nestedReference}`) ? tryResolveSubmodule<token, nestedSubmodule, nestedReference, s, $[submodule], [
|
|
22
|
+
...submodulePath,
|
|
23
|
+
nestedSubmodule
|
|
24
|
+
]> : unresolvableError<s, reference, $[submodule], {}, submodulePath> : ErrorMessage<writeNonSubmoduleDotMessage<submodule>>;
|
|
20
25
|
/** Provide valid completions for the current token, or fallback to an
|
|
21
26
|
* unresolvable error if there are none */
|
|
22
27
|
export type unresolvableError<s extends StaticState, token extends string, $, args, submodulePath extends string[]> = validReferenceFromToken<token, $, args, submodulePath> extends never ? ErrorMessage<writeUnresolvableMessage<qualifiedReference<token, submodulePath>>> : Completion<`${s["scanned"]}${qualifiedReference<validReferenceFromToken<token, $, args, submodulePath>, submodulePath>}`>;
|
|
23
28
|
type qualifiedReference<reference extends string, submodulePath extends string[]> = join<[...submodulePath, reference], ".">;
|
|
24
|
-
type validReferenceFromToken<token extends string, $, args, submodulePath extends string[]> = Extract<submodulePath extends
|
|
29
|
+
type validReferenceFromToken<token extends string, $, args, submodulePath extends string[]> = Extract<submodulePath["length"] extends 0 ? BaseCompletions<$, args> : resolvableReferenceIn<$>, `${token}${string}`>;
|
|
25
30
|
export declare const writeMissingOperandMessage: (s: DynamicState) => string;
|
|
26
31
|
export type writeMissingRightOperandMessage<token extends string, unscanned extends string = ""> = `Token '${token}' requires a right operand${unscanned extends "" ? "" : ` before '${unscanned}'`}`;
|
|
27
32
|
export declare const writeMissingRightOperandMessage: <token extends string, unscanned extends string>(token: token, unscanned?: unscanned) => writeMissingRightOperandMessage<token, unscanned>;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { type BaseRoot, type BoundKind
|
|
1
|
+
import { type BaseRoot, type BoundKind } from "@ark/schema";
|
|
2
2
|
import { type keySet } from "@ark/util";
|
|
3
|
+
import type { LimitLiteral } from "../../../../ast.js";
|
|
3
4
|
import type { astToString } from "../../../semantic/utils.js";
|
|
4
5
|
import type { DynamicState, DynamicStateWithRoot } from "../../reduce/dynamic.js";
|
|
5
6
|
import { writeUnpairableComparatorMessage, type Comparator, type InvertedComparators, type MaxComparator } from "../../reduce/shared.js";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { $ark, writeUnboundableMessage } from "@ark/schema";
|
|
2
2
|
import { isKeyOf, throwParseError } from "@ark/util";
|
|
3
3
|
import { invertedComparators, maxComparators, writeUnpairableComparatorMessage } from "../../reduce/shared.js";
|
|
4
4
|
import { extractDateLiteralSource, isDateLiteral } from "../operand/date.js";
|
|
@@ -33,7 +33,7 @@ const shiftComparator = (s, start) => s.scanner.lookaheadIs("=") ?
|
|
|
33
33
|
: start;
|
|
34
34
|
export const writeIncompatibleRangeMessage = (l, r) => `Bound kinds ${l} and ${r} are incompatible`;
|
|
35
35
|
export const getBoundKinds = (comparator, limit, root, boundKind) => {
|
|
36
|
-
if (root.extends(
|
|
36
|
+
if (root.extends($ark.intrinsic.number)) {
|
|
37
37
|
if (typeof limit !== "number") {
|
|
38
38
|
return throwParseError(writeInvalidLimitMessage(comparator, limit, boundKind));
|
|
39
39
|
}
|
|
@@ -41,7 +41,7 @@ export const getBoundKinds = (comparator, limit, root, boundKind) => {
|
|
|
41
41
|
: comparator[0] === ">" ? ["min"]
|
|
42
42
|
: ["max"]);
|
|
43
43
|
}
|
|
44
|
-
if (root.extends(
|
|
44
|
+
if (root.extends($ark.intrinsic.lengthBoundable)) {
|
|
45
45
|
if (typeof limit !== "number") {
|
|
46
46
|
return throwParseError(writeInvalidLimitMessage(comparator, limit, boundKind));
|
|
47
47
|
}
|
|
@@ -49,7 +49,7 @@ export const getBoundKinds = (comparator, limit, root, boundKind) => {
|
|
|
49
49
|
: comparator[0] === ">" ? ["minLength"]
|
|
50
50
|
: ["maxLength"]);
|
|
51
51
|
}
|
|
52
|
-
if (root.extends(
|
|
52
|
+
if (root.extends($ark.intrinsic.Date)) {
|
|
53
53
|
// allow either numeric or date limits
|
|
54
54
|
return (comparator === "==" ? ["after", "before"]
|
|
55
55
|
: comparator[0] === ">" ? ["after"]
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { BaseRoot
|
|
1
|
+
import type { BaseRoot } from "@ark/schema";
|
|
2
2
|
import { type BigintLiteral, type ErrorMessage, type NumberLiteral, type trim } from "@ark/util";
|
|
3
|
+
import type { DateLiteral } from "../../../../ast.js";
|
|
3
4
|
import type { DynamicStateWithRoot } from "../../reduce/dynamic.js";
|
|
4
5
|
import type { StringLiteral } from "../operand/enclosed.js";
|
|
5
6
|
declare const unitLiteralKeywords: {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { BaseRoot, resolvableReferenceIn } from "@ark/schema";
|
|
2
2
|
import { type ErrorMessage } from "@ark/util";
|
|
3
|
+
import type { Ark } from "../../ark.js";
|
|
3
4
|
import type { inferAstRoot } from "../semantic/infer.js";
|
|
4
5
|
import type { DynamicState, DynamicStateWithRoot } from "./reduce/dynamic.js";
|
|
5
6
|
import type { StringifiablePrefixOperator } from "./reduce/shared.js";
|
|
@@ -16,7 +17,7 @@ export type parseString<def extends string, $, args> = def extends keyof $ ? def
|
|
|
16
17
|
"[]"
|
|
17
18
|
] : fullStringParse<state.initialize<def>, $, args> : fullStringParse<state.initialize<def>, $, args>;
|
|
18
19
|
export type inferString<def extends string, $, args> = inferAstRoot<parseString<def, $, args>, $, args>;
|
|
19
|
-
export type BaseCompletions<$, args, otherSuggestions extends string = never> = resolvableReferenceIn<$> | resolvableReferenceIn<
|
|
20
|
+
export type BaseCompletions<$, args, otherSuggestions extends string = never> = resolvableReferenceIn<$> | resolvableReferenceIn<Ark> | (keyof args & string) | StringifiablePrefixOperator | otherSuggestions;
|
|
20
21
|
export type StringParseResult = BaseRoot | ParsedDefault;
|
|
21
22
|
export declare const fullStringParse: (s: DynamicState) => StringParseResult;
|
|
22
23
|
type fullStringParse<s extends StaticState, $, args> = extractFinalizedResult<parseUntilFinalizer<s, $, args>>;
|
package/out/parser/tuple.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import { type
|
|
1
|
+
import { type BaseRoot, type MetaSchema, type Morph, type Predicate } from "@ark/schema";
|
|
2
|
+
import { type array, type BuiltinObjectKind, type conform, type Constructor, type Domain, type ErrorMessage, type show } from "@ark/util";
|
|
3
|
+
import type { distillConstrainableIn, distillOut, inferMorphOut, inferPredicate, Out } from "../ast.js";
|
|
4
|
+
import type { inferIntersection } from "../intersect.js";
|
|
3
5
|
import type { ParseContext } from "../scope.js";
|
|
4
6
|
import type { inferDefinition, validateDefinition } from "./definition.js";
|
|
5
7
|
import type { InfixOperator, PostfixExpression } from "./semantic/infer.js";
|
|
@@ -103,7 +105,7 @@ export type validatePostfixExpression<def extends PostfixExpression, $, args> =
|
|
|
103
105
|
export type validateInfixExpression<def extends InfixExpression, $, args> = def["length"] extends 2 ? readonly [def[0], writeMissingRightOperandMessage<def[1]>] : readonly [
|
|
104
106
|
validateDefinition<def[0], $, args>,
|
|
105
107
|
def[1],
|
|
106
|
-
def[1] extends "|" ? validateDefinition<def[2], $, args> : def[1] extends "&" ? validateDefinition<def[2], $, args> : def[1] extends ":" ? Predicate<distillOut<inferDefinition<def[0], $, args>>> : def[1] extends "=>" ? Morph<distillOut<inferDefinition<def[0], $, args>>, unknown> : def[1] extends "@" ?
|
|
108
|
+
def[1] extends "|" ? validateDefinition<def[2], $, args> : def[1] extends "&" ? validateDefinition<def[2], $, args> : def[1] extends ":" ? Predicate<distillOut<inferDefinition<def[0], $, args>>> : def[1] extends "=>" ? Morph<distillOut<inferDefinition<def[0], $, args>>, unknown> : def[1] extends "@" ? MetaSchema : validateDefinition<def[2], $, args>
|
|
107
109
|
];
|
|
108
110
|
export type UnparsedTupleExpressionInput = {
|
|
109
111
|
instanceof: Constructor;
|
package/out/parser/tuple.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { $ark, makeRootAndArrayPropertiesMutable } from "@ark/schema";
|
|
2
2
|
import { append, objectKindOrDomainOf, throwParseError } from "@ark/util";
|
|
3
3
|
import { writeMissingRightOperandMessage } from "./string/shift/operand/unenclosed.js";
|
|
4
4
|
export const parseTuple = (def, ctx) => maybeParseTupleExpression(def, ctx) ?? parseTupleLiteral(def, ctx);
|
|
@@ -21,20 +21,20 @@ export const parseTupleLiteral = (def, ctx) => {
|
|
|
21
21
|
i++;
|
|
22
22
|
}
|
|
23
23
|
if (spread) {
|
|
24
|
-
if (!element.extends(
|
|
24
|
+
if (!element.extends($ark.intrinsic.Array))
|
|
25
25
|
return throwParseError(writeNonArraySpreadMessage(element.expression));
|
|
26
26
|
// a spread must be distributed over branches e.g.:
|
|
27
27
|
// def: [string, ...(number[] | [true, false])]
|
|
28
28
|
// nodes: [string, ...number[]] | [string, true, false]
|
|
29
29
|
sequences = sequences.flatMap(base =>
|
|
30
30
|
// since appendElement mutates base, we have to shallow-ish clone it for each branch
|
|
31
|
-
element.
|
|
31
|
+
element.distribute(branch => appendSpreadBranch(makeRootAndArrayPropertiesMutable(base), branch)));
|
|
32
32
|
}
|
|
33
33
|
else {
|
|
34
34
|
sequences = sequences.map(base => appendElement(base, optional ? "optional" : "required", element));
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
|
-
return ctx.$.internal.
|
|
37
|
+
return ctx.$.internal.rootNode(sequences.map(sequence => ({
|
|
38
38
|
proto: Array,
|
|
39
39
|
sequence
|
|
40
40
|
})));
|
|
@@ -84,7 +84,7 @@ const appendSpreadBranch = (base, branch) => {
|
|
|
84
84
|
const spread = branch.firstReferenceOfKind("sequence");
|
|
85
85
|
if (!spread) {
|
|
86
86
|
// the only array with no sequence reference is unknown[]
|
|
87
|
-
return appendElement(base, "variadic",
|
|
87
|
+
return appendElement(base, "variadic", $ark.intrinsic.unknown);
|
|
88
88
|
}
|
|
89
89
|
spread.prefix.forEach(node => appendElement(base, "required", node));
|
|
90
90
|
spread.optionals.forEach(node => appendElement(base, "optional", node));
|
package/out/scope.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { type ErrorType, type anyOrNever, type array, type nominal, type show } from "@ark/util";
|
|
3
|
-
import
|
|
4
|
-
import { type
|
|
5
|
-
import type { Module } from "./module.js";
|
|
1
|
+
import { BaseScope, type AliasDefEntry, type ArkScopeConfig, type BaseNode, type BaseRoot, type GenericArgResolutions, type GenericAst, type GenericParamAst, type InternalResolutions, type NodeKind, type NodeParseOptions, type NodeSchema, type PreparsedNodeResolution, type PrivateDeclaration, type RootKind, type RootSchema, type arkKind, type destructuredExportContext, type destructuredImportContext, type exportedNameOf, type nodeOfKind, type reducibleKindOf, type toInternalScope, type writeDuplicateAliasError } from "@ark/schema";
|
|
2
|
+
import { type ErrorType, type Json, type anyOrNever, type array, type flattenListable, type nominal, type show } from "@ark/util";
|
|
3
|
+
import type { Ark } from "./ark.js";
|
|
4
|
+
import { parseGenericParams, type GenericDeclaration, type GenericHktParser, type ParameterString, type baseGenericConstraints, type parseValidGenericParams } from "./generic.js";
|
|
5
|
+
import type { BoundModule, Module, Submodule, instantiateExport } from "./module.js";
|
|
6
6
|
import { type inferDefinition, type validateDefinition } from "./parser/definition.js";
|
|
7
7
|
import type { ParsedDefault } from "./parser/string/shift/operator/default.js";
|
|
8
8
|
import { InternalTypeParser, type DeclarationParser, type DefinitionParser, type Type, type TypeParser } from "./type.js";
|
|
9
|
-
export type ScopeParser = <const def>(def: validateScope<def>, config?:
|
|
9
|
+
export type ScopeParser = <const def>(def: validateScope<def>, config?: ArkScopeConfig) => Scope<inferScope<def>>;
|
|
10
10
|
export type validateScope<def> = {
|
|
11
|
-
[k in keyof def]:
|
|
11
|
+
[k in keyof def]: parseScopeKey<k, def>["params"] extends infer params ? params extends array<GenericParamAst> ? params["length"] extends 0 ? def[k] extends Type.Any | PreparsedResolution ? def[k] : k extends PrivateDeclaration<infer name extends keyof def & string> ? ErrorType<writeDuplicateAliasError<name>> : validateDefinition<def[k], bootstrapAliases<def>, {}> : validateDefinition<def[k], bootstrapAliases<def>, baseGenericConstraints<params>> : params : never;
|
|
12
12
|
};
|
|
13
13
|
export type inferScope<def> = inferBootstrapped<bootstrapAliases<def>>;
|
|
14
14
|
export type bindThis<def> = {
|
|
@@ -21,13 +21,19 @@ export type UnparsedScope = "$";
|
|
|
21
21
|
/** These are legal as values of a scope but not as definitions in other contexts */
|
|
22
22
|
type PreparsedResolution = PreparsedNodeResolution;
|
|
23
23
|
type bootstrapAliases<def> = {
|
|
24
|
-
[k in Exclude<keyof def, GenericDeclaration
|
|
24
|
+
[k in Exclude<keyof def, GenericDeclaration>]: def[k] extends (PreparsedResolution) ? def[k] extends {
|
|
25
|
+
t: infer g extends GenericAst;
|
|
26
|
+
} ? g : def[k] : def[k] extends (() => infer thunkReturn extends PreparsedResolution) ? thunkReturn extends {
|
|
27
|
+
t: infer g extends GenericAst;
|
|
28
|
+
} ? g : thunkReturn : Def<def[k]>;
|
|
25
29
|
} & {
|
|
26
|
-
[k in keyof def & GenericDeclaration as extractGenericName<k>]:
|
|
30
|
+
[k in keyof def & GenericDeclaration as extractGenericName<k>]: GenericAst<parseValidGenericParams<extractGenericParameters<k>, bootstrapAliases<def>>, def[k], UnparsedScope>;
|
|
27
31
|
};
|
|
28
|
-
type inferBootstrapped<$> =
|
|
29
|
-
[name in keyof $]: $[name] extends Def<infer def> ? inferDefinition<def, $, {}> : $[name] extends
|
|
30
|
-
|
|
32
|
+
type inferBootstrapped<$> = {
|
|
33
|
+
[name in keyof $]: $[name] extends Def<infer def> ? inferDefinition<def, $, {}> : $[name] extends {
|
|
34
|
+
t: GenericAst<infer params, infer def, infer body$>;
|
|
35
|
+
} ? GenericAst<params, def, body$ extends UnparsedScope ? $ : body$, $> : $[name] extends Module<infer exports> ? Submodule<exports> : never;
|
|
36
|
+
} & unknown;
|
|
31
37
|
type extractGenericName<k> = k extends GenericDeclaration<infer name> ? name : never;
|
|
32
38
|
type extractGenericParameters<k> = k extends `${string}<${infer params}>` ? ParameterString<params> : never;
|
|
33
39
|
export type resolve<reference extends keyof $ | keyof args, $, args> = (reference extends keyof args ? args[reference] : $[reference & keyof $]) extends infer resolution ? [
|
|
@@ -36,9 +42,11 @@ export type resolve<reference extends keyof $ | keyof args, $, args> = (referenc
|
|
|
36
42
|
export type moduleKeyOf<$> = {
|
|
37
43
|
[k in keyof $]: $[k] extends {
|
|
38
44
|
[arkKind]: "module";
|
|
39
|
-
} ?
|
|
45
|
+
} ? [
|
|
46
|
+
$[k]
|
|
47
|
+
] extends [anyOrNever] ? never : k & string : never;
|
|
40
48
|
}[keyof $];
|
|
41
|
-
export type tryInferSubmoduleReference<$, token> = token extends `${infer submodule extends moduleKeyOf<$>}.${infer subalias}` ? subalias extends keyof $[submodule] ? $[submodule][subalias] :
|
|
49
|
+
export type tryInferSubmoduleReference<$, token> = token extends `${infer submodule extends moduleKeyOf<$>}.${infer subalias}` ? subalias extends keyof $[submodule] ? $[submodule][subalias] : tryInferSubmoduleReference<$[submodule], subalias> : token extends (`${infer submodule extends moduleKeyOf<Ark>}.${infer subalias}`) ? subalias extends keyof Ark[submodule] ? Ark[submodule][subalias] : tryInferSubmoduleReference<Ark[submodule], subalias> : never;
|
|
42
50
|
export interface ParseContext extends TypeParseOptions {
|
|
43
51
|
$: InternalScope;
|
|
44
52
|
}
|
|
@@ -46,11 +54,9 @@ export interface TypeParseOptions {
|
|
|
46
54
|
args?: GenericArgResolutions;
|
|
47
55
|
}
|
|
48
56
|
export declare const scope: ScopeParser;
|
|
49
|
-
export declare class InternalScope<$ extends InternalResolutions = InternalResolutions> extends
|
|
57
|
+
export declare class InternalScope<$ extends InternalResolutions = InternalResolutions> extends BaseScope<$> {
|
|
50
58
|
private parseCache;
|
|
51
|
-
constructor(def: Record<string, unknown>, config?: ArkConfig);
|
|
52
59
|
type: InternalTypeParser;
|
|
53
|
-
match: MatchParser<$>;
|
|
54
60
|
declare: () => {
|
|
55
61
|
type: InternalTypeParser;
|
|
56
62
|
};
|
|
@@ -61,21 +67,36 @@ export declare class InternalScope<$ extends InternalResolutions = InternalResol
|
|
|
61
67
|
parse<defaultable extends boolean = false>(def: unknown, ctx: ParseContext, defaultable?: defaultable): BaseRoot | (defaultable extends false ? never : ParsedDefault);
|
|
62
68
|
parseString<defaultable extends boolean>(def: string, ctx: ParseContext, defaultable: defaultable): BaseRoot | (defaultable extends false ? never : ParsedDefault);
|
|
63
69
|
}
|
|
64
|
-
export interface Scope<$ =
|
|
70
|
+
export interface Scope<$ = {}> {
|
|
71
|
+
t: $;
|
|
72
|
+
[arkKind]: "scope";
|
|
73
|
+
config: ArkScopeConfig;
|
|
74
|
+
references: readonly BaseNode[];
|
|
75
|
+
json: Json;
|
|
76
|
+
exportedNames: array<exportedNameOf<$>>;
|
|
77
|
+
/** The set of names defined at the root-level of the scope mapped to their
|
|
78
|
+
* corresponding definitions.**/
|
|
79
|
+
aliases: Record<string, unknown>;
|
|
80
|
+
internal: toInternalScope<$>;
|
|
81
|
+
defineSchema<const def extends RootSchema>(schema: def): def;
|
|
82
|
+
schema(schema: RootSchema, opts?: NodeParseOptions): BaseRoot;
|
|
83
|
+
node<kinds extends NodeKind | array<RootKind>>(kinds: kinds, schema: NodeSchema<flattenListable<kinds>>, opts?: NodeParseOptions): nodeOfKind<reducibleKindOf<flattenListable<kinds>>>;
|
|
65
84
|
type: TypeParser<$>;
|
|
66
|
-
match: MatchParser<$>;
|
|
67
85
|
declare: DeclarationParser<$>;
|
|
68
86
|
define: DefinitionParser<$>;
|
|
69
87
|
generic: GenericHktParser<$>;
|
|
70
|
-
import
|
|
71
|
-
|
|
88
|
+
import(): Module<{
|
|
89
|
+
[k in exportedNameOf<$> as PrivateDeclaration<k>]: $[k];
|
|
90
|
+
}>;
|
|
91
|
+
import<names extends exportedNameOf<$>[]>(...names: names): BoundModule<destructuredImportContext<$, names>, $>;
|
|
92
|
+
export(): Module<{
|
|
93
|
+
[k in exportedNameOf<$>]: $[k];
|
|
94
|
+
}>;
|
|
95
|
+
export<names extends exportedNameOf<$>[]>(...names: names): BoundModule<show<destructuredExportContext<$, names>>, $>;
|
|
96
|
+
resolve<name extends exportedNameOf<$>>(name: name): instantiateExport<$[name], $>;
|
|
72
97
|
}
|
|
73
|
-
export declare const Scope: new <$ =
|
|
98
|
+
export declare const Scope: new <$ = {}>(...args: ConstructorParameters<typeof InternalScope>) => Scope<$>;
|
|
74
99
|
export declare const writeShallowCycleErrorMessage: (name: string, seen: string[]) => string;
|
|
75
|
-
export type ParsedScopeKey = {
|
|
76
|
-
name: string;
|
|
77
|
-
params: array<GenericParamDef>;
|
|
78
|
-
};
|
|
79
100
|
export type parseScopeKey<k, def> = k extends `${infer name}<${infer params}>` ? parseGenericScopeKey<name, params, def> : {
|
|
80
101
|
name: k;
|
|
81
102
|
params: [];
|