arktype 2.0.0-rc.26 → 2.0.0-rc.28
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/attributes.d.ts +61 -241
- package/out/attributes.js +2 -1
- package/out/generic.d.ts +4 -4
- package/out/keywords/{constructors/Array.d.ts → Array.d.ts} +2 -2
- package/out/keywords/{constructors/Array.js → Array.js} +1 -1
- package/out/keywords/{constructors/FormData.d.ts → FormData.d.ts} +2 -2
- package/out/keywords/{constructors/FormData.js → FormData.js} +1 -1
- package/out/keywords/{constructors/TypedArray.d.ts → TypedArray.d.ts} +1 -1
- package/out/keywords/{constructors/TypedArray.js → TypedArray.js} +1 -1
- package/out/keywords/{constructors/constructors.d.ts → constructors.d.ts} +1 -1
- package/out/keywords/{constructors/constructors.js → constructors.js} +1 -1
- package/out/keywords/keywords.d.ts +6 -9
- package/out/keywords/keywords.js +3 -3
- package/out/keywords/number.d.ts +23 -0
- package/out/keywords/{number/epoch.js → number.js} +23 -1
- package/out/keywords/string.d.ts +327 -0
- package/out/keywords/string.js +398 -0
- package/out/methods/array.d.ts +6 -7
- package/out/methods/base.d.ts +10 -14
- package/out/methods/date.d.ts +5 -6
- package/out/methods/number.d.ts +6 -7
- package/out/methods/object.d.ts +5 -5
- package/out/methods/string.d.ts +7 -8
- package/out/parser/ast/generic.d.ts +17 -0
- package/out/parser/ast/infer.d.ts +4 -18
- package/out/parser/ast/validate.d.ts +10 -6
- package/out/parser/ast/validate.js +2 -0
- package/out/parser/definition.d.ts +17 -8
- package/out/parser/definition.js +20 -2
- package/out/parser/objectLiteral.d.ts +39 -39
- package/out/parser/objectLiteral.js +75 -49
- package/out/parser/property.d.ts +37 -0
- package/out/parser/property.js +16 -0
- package/out/parser/reduce/dynamic.d.ts +1 -1
- package/out/parser/shift/operand/enclosed.d.ts +1 -3
- package/out/parser/shift/operand/unenclosed.d.ts +2 -1
- package/out/parser/shift/operator/default.d.ts +2 -1
- package/out/parser/shift/operator/default.js +1 -1
- package/out/parser/string.d.ts +5 -4
- package/out/parser/string.js +18 -1
- package/out/parser/tupleExpressions.d.ts +47 -0
- package/out/parser/tupleExpressions.js +67 -0
- package/out/parser/tupleLiteral.d.ts +80 -0
- package/out/parser/tupleLiteral.js +119 -0
- package/out/scope.d.ts +8 -7
- package/out/scope.js +12 -32
- package/out/type.d.ts +5 -5
- package/package.json +3 -3
- package/out/keywords/constructors/Date.d.ts +0 -37
- package/out/keywords/number/epoch.d.ts +0 -10
- package/out/keywords/number/integer.d.ts +0 -3
- package/out/keywords/number/integer.js +0 -5
- package/out/keywords/number/number.d.ts +0 -58
- package/out/keywords/number/number.js +0 -21
- package/out/keywords/string/alpha.d.ts +0 -7
- package/out/keywords/string/alpha.js +0 -2
- package/out/keywords/string/alphanumeric.d.ts +0 -7
- package/out/keywords/string/alphanumeric.js +0 -2
- package/out/keywords/string/base64.d.ts +0 -21
- package/out/keywords/string/base64.js +0 -6
- package/out/keywords/string/capitalize.d.ts +0 -15
- package/out/keywords/string/capitalize.js +0 -12
- package/out/keywords/string/creditCard.d.ts +0 -8
- package/out/keywords/string/creditCard.js +0 -37
- package/out/keywords/string/date.d.ts +0 -55
- package/out/keywords/string/date.js +0 -98
- package/out/keywords/string/digits.d.ts +0 -7
- package/out/keywords/string/digits.js +0 -2
- package/out/keywords/string/email.d.ts +0 -7
- package/out/keywords/string/email.js +0 -4
- package/out/keywords/string/integer.d.ts +0 -16
- package/out/keywords/string/integer.js +0 -16
- package/out/keywords/string/ip.d.ts +0 -20
- package/out/keywords/string/ip.js +0 -23
- package/out/keywords/string/json.d.ts +0 -16
- package/out/keywords/string/json.js +0 -54
- package/out/keywords/string/lower.d.ts +0 -15
- package/out/keywords/string/lower.js +0 -12
- package/out/keywords/string/normalize.d.ts +0 -88
- package/out/keywords/string/normalize.js +0 -43
- package/out/keywords/string/numeric.d.ts +0 -15
- package/out/keywords/string/numeric.js +0 -13
- package/out/keywords/string/semver.d.ts +0 -7
- package/out/keywords/string/semver.js +0 -4
- package/out/keywords/string/string.d.ts +0 -135
- package/out/keywords/string/string.js +0 -43
- package/out/keywords/string/trim.d.ts +0 -15
- package/out/keywords/string/trim.js +0 -14
- package/out/keywords/string/upper.d.ts +0 -15
- package/out/keywords/string/upper.js +0 -12
- package/out/keywords/string/url.d.ts +0 -15
- package/out/keywords/string/url.js +0 -37
- package/out/keywords/string/utils.d.ts +0 -2
- package/out/keywords/string/utils.js +0 -10
- package/out/keywords/string/uuid.d.ts +0 -42
- package/out/keywords/string/uuid.js +0 -19
- package/out/parser/tuple.d.ts +0 -148
- package/out/parser/tuple.js +0 -164
- /package/out/{keywords/constructors/Date.js → parser/ast/generic.js} +0 -0
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import type { After, AtLeast, AtLeastLength, AtMost, AtMostLength, AtOrAfter, AtOrBefore, Before, Default, DivisibleBy, ExactlyLength, LessThan, LessThanLength, LimitLiteral, MoreThan, MoreThanLength, Nominal, Optional, associateAttributes, brandAttributes, distill, inferIntersection, normalizeLimit } from "../../attributes.ts";
|
|
4
|
-
import type { Date } from "../../keywords/constructors/Date.ts";
|
|
1
|
+
import type { arkKeyOf, array } from "@ark/util";
|
|
2
|
+
import type { distill, inferIntersection, LimitLiteral, withDefault } from "../../attributes.ts";
|
|
5
3
|
import type { type } from "../../keywords/keywords.ts";
|
|
6
|
-
import type { UnparsedScope } from "../../scope.ts";
|
|
7
4
|
import type { inferDefinition } from "../definition.ts";
|
|
8
5
|
import type { Comparator } from "../reduce/shared.ts";
|
|
9
6
|
import type { ArkTypeScanner } from "../shift/scanner.ts";
|
|
7
|
+
import type { GenericInstantiationAst, inferGenericInstantiation } from "./generic.ts";
|
|
10
8
|
export type inferAstRoot<ast, $, args> = ast extends array ? inferExpression<ast, $, args> : never;
|
|
11
9
|
export type inferAstIn<ast, $, args> = distill.In<inferAstRoot<ast, $, args>>;
|
|
12
10
|
export type inferAstOut<ast, $, args> = distill.Out<inferAstRoot<ast, $, args>>;
|
|
@@ -20,20 +18,8 @@ export type InferredAst<t = unknown, def extends string = string> = [
|
|
|
20
18
|
"inferred",
|
|
21
19
|
def
|
|
22
20
|
];
|
|
23
|
-
export type
|
|
24
|
-
type resolveScope<g$, $> = g$ extends UnparsedScope ? $ : g$;
|
|
25
|
-
export type inferExpression<ast, $, args> = ast extends array ? ast extends InferredAst<infer resolution> ? resolution : ast extends DefAst<infer def> ? inferDefinition<def, $, args> : ast extends GenericInstantiationAst<infer g, infer argAsts> ? g["bodyDef"] extends Hkt ? Hkt.apply<g["bodyDef"], {
|
|
26
|
-
[i in keyof argAsts]: inferExpression<argAsts[i], $, args>;
|
|
27
|
-
}> : inferDefinition<g["bodyDef"], resolveScope<g["$"], $>, {
|
|
28
|
-
[i in keyof g["names"] & `${number}` as g["names"][i]]: inferExpression<argAsts[i & keyof argAsts], resolveScope<g["arg$"], $>, args>;
|
|
29
|
-
}> : ast[1] extends "[]" ? inferExpression<ast[0], $, args>[] : ast[1] extends "|" ? inferExpression<ast[0], $, args> | inferExpression<ast[2], $, args> : ast[1] extends "&" ? inferIntersection<inferExpression<ast[0], $, args>, inferExpression<ast[2], $, args>> : ast[1] extends "=" ? type.infer<ast[2]> extends infer defaultValue ? associateAttributes<inferExpression<ast[0], $, args>, Default<defaultValue>> : never : ast[1] extends "#" ? brandAttributes<inferExpression<ast[0], $, args>, Nominal<ast[2]>> : ast[1] extends Comparator ? ast[0] extends LimitLiteral ? attachBoundAttributes<inferExpression<ast[2], $, args>, ast[1], ast[0]> : attachBoundAttributes<inferExpression<ast[0], $, args>, ast[1], ast[2] & LimitLiteral> : ast[1] extends "%" ? associateAttributes<inferExpression<ast[0], $, args>, DivisibleBy<ast[2]>> : ast[1] extends "?" ? associateAttributes<inferExpression<ast[0], $, args>, Optional> : ast[0] extends "keyof" ? arkKeyOf<inferExpression<ast[1], $, args>> : never : never;
|
|
30
|
-
export type attachBoundAttributes<inWithAttributes, comparator extends Comparator, limit extends LimitLiteral> = distill.In<inWithAttributes> extends infer In ? comparator extends "==" ? In extends number ? limit : In extends Date ? Date.nominal<normalizeLimit<limit>> : associateAttributes<inWithAttributes, ExactlyLength<limit>> : associateAttributes<inWithAttributes, boundToAttributes<In, comparator, limit>> : never;
|
|
31
|
-
export type boundToAttributes<In, comparator extends Comparator, limit extends LimitLiteral> = In extends string | array ? lengthBoundToAttributes<comparator, limit & number> : In extends Date ? dateBoundToAttributes<comparator, normalizeLimit<limit>> : numericBoundToAttributes<comparator, limit & number>;
|
|
32
|
-
type lengthBoundToAttributes<comparator extends Comparator, limit extends number> = comparator extends "<" ? LessThanLength<limit> : comparator extends "<=" ? AtMostLength<limit> : comparator extends ">" ? MoreThanLength<limit> : AtLeastLength<limit>;
|
|
33
|
-
type dateBoundToAttributes<comparator extends Comparator, limit extends string | number> = comparator extends "<" ? Before<limit> : comparator extends "<=" ? AtOrBefore<limit> : comparator extends ">" ? After<limit> : AtOrAfter<limit>;
|
|
34
|
-
type numericBoundToAttributes<comparator extends Comparator, limit extends number> = comparator extends "<" ? LessThan<limit> : comparator extends "<=" ? AtMost<limit> : comparator extends ">" ? MoreThan<limit> : AtLeast<limit>;
|
|
21
|
+
export type inferExpression<ast, $, args> = ast extends array ? ast extends InferredAst<infer resolution> ? resolution : ast extends DefAst<infer def> ? inferDefinition<def, $, args> : ast extends GenericInstantiationAst<infer g, infer argAsts> ? inferGenericInstantiation<g, argAsts, $, args> : ast[1] extends "[]" ? inferExpression<ast[0], $, args>[] : ast[1] extends "|" ? inferExpression<ast[0], $, args> | inferExpression<ast[2], $, args> : ast[1] extends "&" ? inferIntersection<inferExpression<ast[0], $, args>, inferExpression<ast[2], $, args>> : ast[1] extends "=" ? type.infer<ast[2]> extends infer defaultValue ? withDefault<inferExpression<ast[0], $, args>, defaultValue> : never : ast[1] extends "#" ? type.brand<inferExpression<ast[0], $, args>, ast[2]> : ast[1] extends Comparator ? ast[0] extends LimitLiteral ? inferExpression<ast[2], $, args> : inferExpression<ast[0], $, args> : ast[1] extends "%" ? inferExpression<ast[0], $, args> : ast[1] extends "?" ? inferExpression<ast[0], $, args> : ast[0] extends "keyof" ? arkKeyOf<inferExpression<ast[1], $, args>> : never : never;
|
|
35
22
|
export type PrefixOperator = "keyof" | "instanceof" | "===" | "node";
|
|
36
23
|
export type PrefixExpression<operator extends PrefixOperator = PrefixOperator, operand = unknown> = [operator, operand];
|
|
37
24
|
export type PostfixExpression<operator extends ArkTypeScanner.PostfixToken = ArkTypeScanner.PostfixToken, operand = unknown> = readonly [operand, operator];
|
|
38
25
|
export type InfixExpression<operator extends ArkTypeScanner.InfixToken = ArkTypeScanner.InfixToken, l = unknown, r = unknown> = [l, operator, r];
|
|
39
|
-
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { arkKind,
|
|
2
|
-
import type { anyOrNever,
|
|
1
|
+
import type { arkKind, PrivateDeclaration, writeMissingSubmoduleAccessMessage } from "@ark/schema";
|
|
2
|
+
import type { anyOrNever, BigintLiteral, Completion, ErrorMessage, NumberLiteral, writeMalformedNumericLiteralMessage } from "@ark/util";
|
|
3
3
|
import type { Generic } from "../../generic.ts";
|
|
4
4
|
import type { Comparator } from "../reduce/shared.ts";
|
|
5
5
|
import type { writeInvalidGenericArgCountMessage } from "../shift/operand/genericArgs.ts";
|
|
@@ -8,19 +8,23 @@ import type { parseString } from "../string.ts";
|
|
|
8
8
|
import type { validateRange } from "./bounds.ts";
|
|
9
9
|
import type { validateDefault } from "./default.ts";
|
|
10
10
|
import type { validateDivisor } from "./divisor.ts";
|
|
11
|
-
import type {
|
|
11
|
+
import type { GenericInstantiationAst, validateGenericInstantiation } from "./generic.ts";
|
|
12
|
+
import type { DefAst, InferredAst, InfixExpression, PostfixExpression } from "./infer.ts";
|
|
12
13
|
import type { validateKeyof } from "./keyof.ts";
|
|
13
14
|
import type { astToString } from "./utils.ts";
|
|
14
|
-
export type validateAst<ast, $, args> = ast extends ErrorMessage ? ast : ast extends InferredAst ? validateInferredAst<ast[0], ast[2]> : ast extends DefAst ? ast[2] extends PrivateDeclaration<infer name> ? ErrorMessage<writePrefixedPrivateReferenceMessage<name>> : undefined : ast extends PostfixExpression<
|
|
15
|
+
export type validateAst<ast, $, args> = ast extends ErrorMessage ? ast : ast extends InferredAst ? validateInferredAst<ast[0], ast[2]> : ast extends DefAst ? ast[2] extends PrivateDeclaration<infer name> ? ErrorMessage<writePrefixedPrivateReferenceMessage<name>> : undefined : ast extends PostfixExpression<"[]" | "?", infer operand> ? validateAst<operand, $, args> : 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> : operator extends "=" ? validateDefault<l, r & UnitLiteral, $, args> : operator extends "#" ? validateAst<l, $, args> : ErrorMessage<writeUnexpectedExpressionMessage<astToString<ast>>> : ast extends ["keyof", infer operand] ? validateKeyof<operand, $, args> : ast extends GenericInstantiationAst<infer g, infer argAsts> ? validateGenericInstantiation<g, argAsts, $, args> : ErrorMessage<writeUnexpectedExpressionMessage<astToString<ast>>> & {
|
|
15
16
|
ast: ast;
|
|
16
17
|
};
|
|
17
18
|
type writeUnexpectedExpressionMessage<expression extends string> = `Unexpectedly failed to parse the expression resulting from ${expression}`;
|
|
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;
|
|
19
19
|
export declare const writePrefixedPrivateReferenceMessage: <name extends string>(name: name) => writePrefixedPrivateReferenceMessage<name>;
|
|
20
20
|
export type writePrefixedPrivateReferenceMessage<name extends string> = `Private type references should not include '#'. Use '${name}' instead.`;
|
|
21
21
|
type validateInferredAst<inferred, def extends string> = def extends NumberLiteral ? number extends inferred ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "number">> : undefined : def extends BigintLiteral ? bigint extends inferred ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "bigint">> : undefined : [inferred] extends [anyOrNever] ? undefined : def extends PrivateDeclaration<infer name> ? ErrorMessage<writePrefixedPrivateReferenceMessage<name>> : inferred extends Generic ? ErrorMessage<writeInvalidGenericArgCountMessage<def, inferred["names"], []>> : inferred extends {
|
|
22
22
|
[arkKind]: "module";
|
|
23
23
|
} ? "root" extends keyof inferred ? undefined : ErrorMessage<writeMissingSubmoduleAccessMessage<def>> : def extends ErrorMessage ? def : undefined;
|
|
24
|
-
export type validateString<def extends string, $, args> =
|
|
24
|
+
export type validateString<def extends string, $, args> = parseString<def, $, args> extends infer ast ? validateAst<ast, $, args> extends infer result extends ErrorMessage ? result extends Completion<infer text> ? text : result : def : never;
|
|
25
25
|
type validateInfix<ast extends InfixExpression, $, args> = validateAst<ast[0], $, args> extends infer e extends ErrorMessage ? e : validateAst<ast[2], $, args> extends infer e extends ErrorMessage ? e : undefined;
|
|
26
|
+
export declare const shallowOptionalMessage = "Optional definitions like 'string?' are only valid as properties in an object or tuple";
|
|
27
|
+
export type shallowOptionalMessage = typeof shallowOptionalMessage;
|
|
28
|
+
export declare const shallowDefaultableMessage = "Defaultable definitions like 'number = 0' are only valid as properties in an object or tuple";
|
|
29
|
+
export type shallowDefaultableMessage = typeof shallowDefaultableMessage;
|
|
26
30
|
export {};
|
|
@@ -1 +1,3 @@
|
|
|
1
1
|
export const writePrefixedPrivateReferenceMessage = (name) => `Private type references should not include '#'. Use '${name}' instead.`;
|
|
2
|
+
export const shallowOptionalMessage = "Optional definitions like 'string?' are only valid as properties in an object or tuple";
|
|
3
|
+
export const shallowDefaultableMessage = "Defaultable definitions like 'number = 0' are only valid as properties in an object or tuple";
|
|
@@ -1,17 +1,26 @@
|
|
|
1
1
|
import { type BaseParseContext, type BaseRoot } from "@ark/schema";
|
|
2
|
-
import { type
|
|
2
|
+
import { type anyOrNever, type array, type defined, type equals, type ErrorMessage, type Fn, type ifEmptyObjectLiteral, type objectKindOrDomainOf, type optionalKeyOf, type Primitive, type requiredKeyOf, type show } from "@ark/util";
|
|
3
3
|
import type { type } from "../keywords/keywords.ts";
|
|
4
|
-
import type {
|
|
5
|
-
import type { validateString } from "./ast/validate.ts";
|
|
4
|
+
import type { InnerParseResult } from "../scope.ts";
|
|
5
|
+
import type { shallowDefaultableMessage, shallowOptionalMessage, validateString } from "./ast/validate.ts";
|
|
6
6
|
import { type inferObjectLiteral, type validateObjectLiteral } from "./objectLiteral.ts";
|
|
7
|
-
import type {
|
|
8
|
-
import { type
|
|
7
|
+
import type { DefaultablePropertyTuple, OptionalPropertyDefinition, PossibleDefaultableStringDefinition, validatePossibleStringDefault } from "./property.ts";
|
|
8
|
+
import { type BaseCompletions, type inferString } from "./string.ts";
|
|
9
|
+
import { type inferTupleExpression, type maybeValidateTupleExpression, type TupleExpression } from "./tupleExpressions.ts";
|
|
10
|
+
import { type inferTupleLiteral, type validateTupleLiteral } from "./tupleLiteral.ts";
|
|
11
|
+
export declare const parseInnerDefinition: (def: unknown, ctx: BaseParseContext) => InnerParseResult;
|
|
9
12
|
export declare const parseObject: (def: object, ctx: BaseParseContext) => BaseRoot;
|
|
10
13
|
export type inferDefinition<def, $, args> = [
|
|
11
14
|
def
|
|
12
|
-
] extends [anyOrNever] ? def : def extends type.cast<infer t> ? ifEmptyObjectLiteral<def, object, t> : def extends ThunkCast<infer t> ? t : def extends string ? inferString<def, $, args> : def extends array ? inferTuple<def, $, args> : def extends RegExp ? string
|
|
13
|
-
export type validateDefinition<def, $, args> = null extends undefined ? ErrorMessage<`'strict' or 'strictNullChecks' must be set to true in your tsconfig's 'compilerOptions'`> : [def] extends [
|
|
14
|
-
export type
|
|
15
|
+
] extends [anyOrNever] ? def : def extends type.cast<infer t> ? ifEmptyObjectLiteral<def, object, t> : def extends ThunkCast<infer t> ? t : def extends string ? inferString<def, $, args> : def extends array ? inferTuple<def, $, args> : def extends RegExp ? string : def extends object ? inferObjectLiteral<def, $, args> : never;
|
|
16
|
+
export type validateDefinition<def, $, args> = null extends undefined ? ErrorMessage<`'strict' or 'strictNullChecks' must be set to true in your tsconfig's 'compilerOptions'`> : [def] extends [anyOrNever] ? def : def extends OptionalPropertyDefinition ? ErrorMessage<shallowOptionalMessage> : def extends DefaultablePropertyTuple ? ErrorMessage<shallowDefaultableMessage> : def extends PossibleDefaultableStringDefinition ? validatePossibleStringDefault<def, $, args, shallowDefaultableMessage> : validateInnerDefinition<def, $, args>;
|
|
17
|
+
export type validateInnerDefinition<def, $, args> = [
|
|
18
|
+
def
|
|
19
|
+
] extends [Terminal] ? def : def extends string ? validateString<def, $, args> : def extends array ? validateTuple<def, $, args> : def extends BadDefinitionType ? ErrorMessage<writeBadDefinitionTypeMessage<objectKindOrDomainOf<def>>> : unknown extends def ? BaseCompletions<$, args> | {} : RegExp extends def ? def : validateObjectLiteral<def, $, args>;
|
|
20
|
+
export declare const parseTuple: (def: array, ctx: BaseParseContext) => BaseRoot;
|
|
21
|
+
export type validateTuple<def extends array, $, args> = maybeValidateTupleExpression<def, $, args> extends infer result ? result extends null ? validateTupleLiteral<def, $, args> : result : never;
|
|
22
|
+
export type inferTuple<def extends array, $, args> = def extends TupleExpression ? inferTupleExpression<def, $, args> : inferTupleLiteral<def, $, args>;
|
|
23
|
+
export type validateDeclared<declared, def, $, args> = def extends type.validate<def, $, args> ? validateInference<def, declared, $, args> : type.validate<def, $, args>;
|
|
15
24
|
type validateInference<def, declared, $, args> = def extends RegExp | type.cast<unknown> | ThunkCast | TupleExpression ? validateShallowInference<def, declared, $, args> : def extends array ? declared extends array ? {
|
|
16
25
|
[i in keyof declared]: i extends keyof def ? validateInference<def[i], declared[i], $, args> : declared[i];
|
|
17
26
|
} : show<declarationMismatch<def, declared, $, args>> : def extends object ? show<{
|
package/out/parser/definition.js
CHANGED
|
@@ -1,7 +1,24 @@
|
|
|
1
1
|
import { hasArkKind } from "@ark/schema";
|
|
2
|
-
import { isThunk, objectKindOf, printable, throwParseError } from "@ark/util";
|
|
2
|
+
import { domainOf, hasDomain, isThunk, objectKindOf, printable, throwParseError } from "@ark/util";
|
|
3
3
|
import { parseObjectLiteral } from "./objectLiteral.js";
|
|
4
|
-
import {
|
|
4
|
+
import { parseString } from "./string.js";
|
|
5
|
+
import { maybeParseTupleExpression } from "./tupleExpressions.js";
|
|
6
|
+
import { parseTupleLiteral } from "./tupleLiteral.js";
|
|
7
|
+
const parseCache = {};
|
|
8
|
+
export const parseInnerDefinition = (def, ctx) => {
|
|
9
|
+
if (typeof def === "string") {
|
|
10
|
+
if (ctx.args && Object.keys(ctx.args).some(k => def.includes(k))) {
|
|
11
|
+
// we can only rely on the cache if there are no contextual
|
|
12
|
+
// resolutions like "this" or generic args
|
|
13
|
+
return parseString(def, ctx);
|
|
14
|
+
}
|
|
15
|
+
const scopeCache = (parseCache[ctx.$.id] ??= {});
|
|
16
|
+
return (scopeCache[def] ??= parseString(def, ctx));
|
|
17
|
+
}
|
|
18
|
+
return hasDomain(def, "object") ?
|
|
19
|
+
parseObject(def, ctx)
|
|
20
|
+
: throwParseError(writeBadDefinitionTypeMessage(domainOf(def)));
|
|
21
|
+
};
|
|
5
22
|
export const parseObject = (def, ctx) => {
|
|
6
23
|
const objectKind = objectKindOf(def);
|
|
7
24
|
switch (objectKind) {
|
|
@@ -26,4 +43,5 @@ export const parseObject = (def, ctx) => {
|
|
|
26
43
|
return throwParseError(writeBadDefinitionTypeMessage(objectKind ?? printable(def)));
|
|
27
44
|
}
|
|
28
45
|
};
|
|
46
|
+
export const parseTuple = (def, ctx) => maybeParseTupleExpression(def, ctx) ?? parseTupleLiteral(def, ctx);
|
|
29
47
|
export const writeBadDefinitionTypeMessage = (actual) => `Type definitions must be strings or objects (was ${actual})`;
|
|
@@ -1,68 +1,68 @@
|
|
|
1
|
-
import { type BaseParseContext, type BaseRoot, type
|
|
2
|
-
import { type anyOrNever, type Dict, type ErrorMessage, type ErrorType, type EscapeChar, type Key, type
|
|
3
|
-
import type { of } from "../attributes.ts";
|
|
4
|
-
import type { astToString } from "./ast/utils.ts";
|
|
1
|
+
import { type BaseParseContext, type BaseRoot, type writeInvalidPropertyKeyMessage } from "@ark/schema";
|
|
2
|
+
import { type anyOrNever, type Dict, type ErrorMessage, type ErrorType, type EscapeChar, type Key, type merge, type show } from "@ark/util";
|
|
5
3
|
import type { validateString } from "./ast/validate.ts";
|
|
6
|
-
import type { inferDefinition
|
|
4
|
+
import type { inferDefinition } from "./definition.ts";
|
|
5
|
+
import { type OptionalPropertyDefinition, type validateProperty } from "./property.ts";
|
|
7
6
|
export declare const parseObjectLiteral: (def: Dict, ctx: BaseParseContext) => BaseRoot;
|
|
8
|
-
export declare const writeInvalidUndeclaredBehaviorMessage: (actual: unknown) => string;
|
|
9
|
-
export declare const nonLeadingSpreadError = "Spread operator may only be used as the first key in an object";
|
|
10
7
|
export type inferObjectLiteral<def extends object, $, args> = show<"..." extends keyof def ? merge<inferDefinition<def["..."], $, args>, _inferObjectLiteral<def, $, args>> : _inferObjectLiteral<def, $, args>>;
|
|
11
8
|
/**
|
|
12
9
|
* Infers the contents of an object literal, ignoring a spread definition
|
|
13
10
|
*/
|
|
14
11
|
type _inferObjectLiteral<def extends object, $, args> = {
|
|
15
|
-
-readonly [k in keyof def as
|
|
16
|
-
def[k]
|
|
17
|
-
] extends [anyOrNever] ? def[k] : inferDefinition<def[k], $, args>;
|
|
12
|
+
-readonly [k in keyof def as nonOptionalKeyFromEntry<k, def[k], $, args>]: inferDefinition<def[k], $, args>;
|
|
18
13
|
} & {
|
|
19
|
-
-readonly [k in keyof def as
|
|
14
|
+
-readonly [k in keyof def as optionalKeyFromEntry<k, def[k]>]?: def[k] extends OptionalPropertyDefinition<infer baseDef> ? inferDefinition<baseDef, $, args> : inferDefinition<def[k], $, args>;
|
|
20
15
|
};
|
|
21
16
|
export type validateObjectLiteral<def, $, args> = {
|
|
22
|
-
[k in keyof def]: k extends
|
|
17
|
+
[k in keyof def]: preparseKey<k> extends (infer parsedKey extends PreparsedKey) ? parsedKey extends PreparsedEntryKey<"index"> ? validateString<parsedKey["normalized"], $, args> extends (ErrorMessage<infer message>) ? ErrorType<message> : inferDefinition<parsedKey["normalized"], $, args> extends Key ? validateProperty<def[k], parsedKey["kind"], $, args> : ErrorMessage<writeInvalidPropertyKeyMessage<parsedKey["normalized"]>> : validateProperty<def[k], parsedKey["kind"], $, args> : never;
|
|
23
18
|
};
|
|
24
|
-
type
|
|
25
|
-
|
|
26
|
-
|
|
19
|
+
type nonOptionalKeyFromEntry<k extends PropertyKey, v, $, args> = preparseKey<k> extends (infer parsedKey extends PreparsedEntryKey<"required" | "index">) ? parsedKey["kind"] extends "index" ? inferDefinition<parsedKey["normalized"], $, args> & Key : [v] extends [OptionalPropertyDefinition] ? [
|
|
20
|
+
v
|
|
21
|
+
] extends [anyOrNever] ? parsedKey["normalized"] : never : parsedKey["normalized"] : never;
|
|
22
|
+
type optionalKeyFromEntry<k extends PropertyKey, v> = preparseKey<k> extends PreparsedEntryKey<"optional", infer name> ? name : v extends OptionalPropertyDefinition ? k : never;
|
|
23
|
+
export declare const writeInvalidUndeclaredBehaviorMessage: (actual: unknown) => string;
|
|
24
|
+
export declare const nonLeadingSpreadError = "Spread operator may only be used as the first key in an object";
|
|
25
|
+
export type PreparsedKey = PreparsedEntryKey | PreparsedSpecialKey;
|
|
26
|
+
type normalizedKeyKind<kind extends EntryKeyKind> = kind extends "index" ? string : Key;
|
|
27
|
+
export type PreparsedEntryKey<kind extends EntryKeyKind = EntryKeyKind, normalized extends normalizedKeyKind<kind> = normalizedKeyKind<kind>> = {
|
|
28
|
+
kind: kind;
|
|
29
|
+
normalized: normalized;
|
|
30
|
+
};
|
|
31
|
+
export type PreparsedSpecialKey<kind extends SpecialKeyKind = SpecialKeyKind> = {
|
|
27
32
|
kind: kind;
|
|
28
|
-
key: key;
|
|
29
33
|
};
|
|
30
34
|
declare namespace PreparsedKey {
|
|
31
35
|
type from<t extends PreparsedKey> = t;
|
|
32
36
|
}
|
|
33
|
-
type ParsedKeyKind =
|
|
37
|
+
export type ParsedKeyKind = EntryKeyKind | SpecialKeyKind;
|
|
38
|
+
export type EntryKeyKind = "required" | "optional" | "index";
|
|
39
|
+
export type SpecialKeyKind = "spread" | "undeclared";
|
|
34
40
|
export type MetaKey = "..." | "+";
|
|
35
41
|
export type IndexKey<def extends string = string> = `[${def}]`;
|
|
36
|
-
export
|
|
37
|
-
export type
|
|
38
|
-
kind: "undeclared";
|
|
39
|
-
behavior: UndeclaredKeyBehavior;
|
|
40
|
-
};
|
|
41
|
-
export type ParsedSpreadEntry = {
|
|
42
|
-
kind: "spread";
|
|
43
|
-
node: BaseRoot;
|
|
44
|
-
};
|
|
45
|
-
export declare const parseEntry: (key: Key, value: unknown, ctx: BaseParseContext) => listable<ParsedEntry>;
|
|
46
|
-
declare const parseKey: (key: Key) => PreparsedKey;
|
|
47
|
-
type parseKey<k> = k extends `${infer inner}?` ? inner extends `${infer baseName}${EscapeChar}` ? PreparsedKey.from<{
|
|
42
|
+
export declare const preparseKey: (key: Key) => PreparsedKey;
|
|
43
|
+
export type preparseKey<k> = k extends symbol ? PreparsedKey.from<{
|
|
48
44
|
kind: "required";
|
|
49
|
-
|
|
45
|
+
normalized: k;
|
|
46
|
+
}> : k extends `${infer inner}?` ? inner extends `${infer baseName}${EscapeChar}` ? PreparsedKey.from<{
|
|
47
|
+
kind: "required";
|
|
48
|
+
normalized: `${baseName}?`;
|
|
50
49
|
}> : PreparsedKey.from<{
|
|
51
50
|
kind: "optional";
|
|
52
|
-
|
|
53
|
-
}> : k extends
|
|
54
|
-
kind:
|
|
55
|
-
|
|
56
|
-
|
|
51
|
+
normalized: inner;
|
|
52
|
+
}> : k extends "+" ? {
|
|
53
|
+
kind: "undeclared";
|
|
54
|
+
} : k extends "..." ? {
|
|
55
|
+
kind: "spread";
|
|
56
|
+
} : k extends `${EscapeChar}${infer escapedMeta extends MetaKey}` ? PreparsedKey.from<{
|
|
57
57
|
kind: "required";
|
|
58
|
-
|
|
58
|
+
normalized: escapedMeta;
|
|
59
59
|
}> : k extends IndexKey<infer def> ? PreparsedKey.from<{
|
|
60
60
|
kind: "index";
|
|
61
|
-
|
|
61
|
+
normalized: def;
|
|
62
62
|
}> : PreparsedKey.from<{
|
|
63
63
|
kind: "required";
|
|
64
|
-
|
|
64
|
+
normalized: k extends (`${EscapeChar}${infer escapedIndexKey extends IndexKey}`) ? escapedIndexKey : k extends Key ? k : `${k & number}`;
|
|
65
65
|
}>;
|
|
66
66
|
export declare const writeInvalidSpreadTypeMessage: <def extends string>(def: def) => writeInvalidSpreadTypeMessage<def>;
|
|
67
|
-
type writeInvalidSpreadTypeMessage<def extends string> = `Spread operand must resolve to an object literal type (was ${def})`;
|
|
67
|
+
export type writeInvalidSpreadTypeMessage<def extends string> = `Spread operand must resolve to an object literal type (was ${def})`;
|
|
68
68
|
export {};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { normalizeIndex } from "@ark/schema";
|
|
2
|
-
import { append, escapeChar, printable, stringAndSymbolicEntriesOf, throwParseError } from "@ark/util";
|
|
2
|
+
import { append, escapeChar, isArray, isEmptyObject, printable, stringAndSymbolicEntriesOf, throwParseError } from "@ark/util";
|
|
3
|
+
import { invalidDefaultableKeyKindMessage, invalidOptionalKeyKindMessage, parseProperty } from "./property.js";
|
|
3
4
|
export const parseObjectLiteral = (def, ctx) => {
|
|
4
5
|
let spread;
|
|
5
6
|
const structure = {};
|
|
@@ -7,25 +8,69 @@ export const parseObjectLiteral = (def, ctx) => {
|
|
|
7
8
|
// because to match JS behavior any keys before the spread are overwritten
|
|
8
9
|
// by the values in the target object, so there'd be no useful purpose in having it
|
|
9
10
|
// anywhere except for the beginning.
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
const defEntries = stringAndSymbolicEntriesOf(def);
|
|
12
|
+
for (const [k, v] of defEntries) {
|
|
13
|
+
const parsedKey = preparseKey(k);
|
|
14
|
+
if (parsedKey.kind === "spread") {
|
|
15
|
+
if (!isEmptyObject(structure))
|
|
16
|
+
return throwParseError(nonLeadingSpreadError);
|
|
17
|
+
const operand = ctx.$.parseOwnDefinitionFormat(v, ctx);
|
|
18
|
+
if (!operand.hasKind("intersection") || !operand.structure) {
|
|
19
|
+
return throwParseError(writeInvalidSpreadTypeMessage(operand.expression));
|
|
20
|
+
}
|
|
21
|
+
spread = operand.structure;
|
|
22
|
+
continue;
|
|
18
23
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return throwParseError(nonLeadingSpreadError);
|
|
24
|
-
if (entry.kind === "undeclared") {
|
|
25
|
-
structure.undeclared = entry.behavior;
|
|
24
|
+
if (parsedKey.kind === "undeclared") {
|
|
25
|
+
if (v !== "reject" && v !== "delete" && v !== "ignore")
|
|
26
|
+
throwParseError(writeInvalidUndeclaredBehaviorMessage(v));
|
|
27
|
+
structure.undeclared = v;
|
|
26
28
|
continue;
|
|
27
29
|
}
|
|
28
|
-
|
|
30
|
+
const parsedValue = parseProperty(v, ctx);
|
|
31
|
+
const parsedEntryKey = parsedKey;
|
|
32
|
+
if (parsedKey.kind === "required") {
|
|
33
|
+
if (!isArray(parsedValue)) {
|
|
34
|
+
appendNamedProp(structure, "required", {
|
|
35
|
+
key: parsedKey.normalized,
|
|
36
|
+
value: parsedValue
|
|
37
|
+
}, ctx);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
appendNamedProp(structure, "optional", parsedValue[1] === "=" ?
|
|
41
|
+
{
|
|
42
|
+
key: parsedKey.normalized,
|
|
43
|
+
value: parsedValue[0],
|
|
44
|
+
default: parsedValue[2]
|
|
45
|
+
}
|
|
46
|
+
: {
|
|
47
|
+
key: parsedKey.normalized,
|
|
48
|
+
value: parsedValue[0]
|
|
49
|
+
}, ctx);
|
|
50
|
+
}
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (isArray(parsedValue)) {
|
|
54
|
+
if (parsedValue[1] === "?")
|
|
55
|
+
throwParseError(invalidOptionalKeyKindMessage);
|
|
56
|
+
if (parsedValue[1] === "=")
|
|
57
|
+
throwParseError(invalidDefaultableKeyKindMessage);
|
|
58
|
+
}
|
|
59
|
+
// value must be a BaseRoot at this point
|
|
60
|
+
if (parsedKey.kind === "optional") {
|
|
61
|
+
appendNamedProp(structure, "optional", {
|
|
62
|
+
key: parsedKey.normalized,
|
|
63
|
+
value: parsedValue
|
|
64
|
+
}, ctx);
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
// must be index at this point
|
|
68
|
+
const signature = ctx.$.parseOwnDefinitionFormat(parsedEntryKey.normalized, ctx);
|
|
69
|
+
const normalized = normalizeIndex(signature, parsedValue, ctx.$);
|
|
70
|
+
if (normalized.index)
|
|
71
|
+
structure.index = append(structure.index, normalized.index);
|
|
72
|
+
if (normalized.required)
|
|
73
|
+
structure.required = append(structure.required, normalized.required);
|
|
29
74
|
}
|
|
30
75
|
const structureNode = ctx.$.node("structure", structure);
|
|
31
76
|
return ctx.$.parseSchema({
|
|
@@ -33,49 +78,30 @@ export const parseObjectLiteral = (def, ctx) => {
|
|
|
33
78
|
structure: spread?.merge(structureNode) ?? structureNode
|
|
34
79
|
});
|
|
35
80
|
};
|
|
81
|
+
const appendNamedProp = (structure, kind, inner, ctx) => {
|
|
82
|
+
structure[kind] = append(
|
|
83
|
+
// doesn't seem like this cast should be necessary
|
|
84
|
+
structure[kind], ctx.$.node(kind, inner));
|
|
85
|
+
};
|
|
36
86
|
export const writeInvalidUndeclaredBehaviorMessage = (actual) => `Value of '+' key must be 'reject', 'delete', or 'ignore' (was ${printable(actual)})`;
|
|
37
87
|
export const nonLeadingSpreadError = "Spread operator may only be used as the first key in an object";
|
|
38
|
-
export const
|
|
39
|
-
const parsedKey = parseKey(key);
|
|
40
|
-
if (parsedKey.kind === "+") {
|
|
41
|
-
if (value !== "reject" && value !== "delete" && value !== "ignore")
|
|
42
|
-
throwParseError(writeInvalidUndeclaredBehaviorMessage(value));
|
|
43
|
-
return { kind: "undeclared", behavior: value };
|
|
44
|
-
}
|
|
45
|
-
if (parsedKey.kind === "...")
|
|
46
|
-
return { kind: "spread", node: ctx.$.parseOwnDefinitionFormat(value, ctx) };
|
|
47
|
-
const parsedValue = ctx.$.parseOwnDefinitionFormat(value, ctx);
|
|
48
|
-
if (parsedKey.kind === "index") {
|
|
49
|
-
const signature = ctx.$.parseOwnDefinitionFormat(parsedKey.key, ctx);
|
|
50
|
-
const normalized = normalizeIndex(signature, parsedValue, ctx.$);
|
|
51
|
-
return (normalized.index ?
|
|
52
|
-
normalized.required ?
|
|
53
|
-
[normalized.index, ...normalized.required]
|
|
54
|
-
: normalized.index
|
|
55
|
-
: (normalized.required ?? []));
|
|
56
|
-
}
|
|
57
|
-
return ctx.$.node(parsedKey.kind, {
|
|
58
|
-
key: parsedKey.key,
|
|
59
|
-
value: parsedValue
|
|
60
|
-
});
|
|
61
|
-
};
|
|
62
|
-
const parseKey = (key) => typeof key === "symbol" ? { kind: "required", key }
|
|
88
|
+
export const preparseKey = (key) => typeof key === "symbol" ? { kind: "required", normalized: key }
|
|
63
89
|
: key.at(-1) === "?" ?
|
|
64
90
|
key.at(-2) === escapeChar ?
|
|
65
|
-
{ kind: "required",
|
|
91
|
+
{ kind: "required", normalized: `${key.slice(0, -2)}?` }
|
|
66
92
|
: {
|
|
67
93
|
kind: "optional",
|
|
68
|
-
|
|
94
|
+
normalized: key.slice(0, -1)
|
|
69
95
|
}
|
|
70
96
|
: key[0] === "[" && key.at(-1) === "]" ?
|
|
71
|
-
{ kind: "index",
|
|
97
|
+
{ kind: "index", normalized: key.slice(1, -1) }
|
|
72
98
|
: key[0] === escapeChar && key[1] === "[" && key.at(-1) === "]" ?
|
|
73
|
-
{ kind: "required",
|
|
74
|
-
: key === "..." ? { kind:
|
|
75
|
-
: key === "+" ? { kind:
|
|
99
|
+
{ kind: "required", normalized: key.slice(1) }
|
|
100
|
+
: key === "..." ? { kind: "spread" }
|
|
101
|
+
: key === "+" ? { kind: "undeclared" }
|
|
76
102
|
: {
|
|
77
103
|
kind: "required",
|
|
78
|
-
|
|
104
|
+
normalized: key === "\\..." ? "..."
|
|
79
105
|
: key === "\\+" ? "+"
|
|
80
106
|
: key
|
|
81
107
|
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { BaseParseContext, BaseRoot, UndeclaredKeyBehavior } from "@ark/schema";
|
|
2
|
+
import { type anyOrNever, type ErrorMessage, type ErrorType, type typeToString } from "@ark/util";
|
|
3
|
+
import type { validateString } from "./ast/validate.ts";
|
|
4
|
+
import { type inferDefinition, type validateInnerDefinition } from "./definition.ts";
|
|
5
|
+
import type { ParsedKeyKind, writeInvalidSpreadTypeMessage } from "./objectLiteral.ts";
|
|
6
|
+
import type { ParsedDefaultableProperty } from "./shift/operator/default.ts";
|
|
7
|
+
import type { parseString } from "./string.ts";
|
|
8
|
+
export type ParsedPropertyKind = "plain" | "optional" | "defaultable";
|
|
9
|
+
export type ParsedProperty = ParsedRequiredProperty | ParsedOptionalProperty | ParsedDefaultableProperty;
|
|
10
|
+
export type ParsedRequiredProperty = BaseRoot;
|
|
11
|
+
export type ParsedOptionalProperty = readonly [BaseRoot, "?"];
|
|
12
|
+
export declare const parseProperty: (def: unknown, ctx: BaseParseContext) => ParsedProperty;
|
|
13
|
+
export type validateProperty<def, keyKind extends ParsedKeyKind, $, args> = [
|
|
14
|
+
def
|
|
15
|
+
] extends [anyOrNever] ?
|
|
16
|
+
/** this extra [anyOrNever] check is required to ensure that nested `type` invocations
|
|
17
|
+
* like the following are not prematurely validated by the outer call:
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* type({
|
|
21
|
+
* "test?": type("string").pipe(x => x === "true")
|
|
22
|
+
* })
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
def : keyKind extends "spread" ? validateSpread<def, inferDefinition<def, $, args>, $, args> : keyKind extends "undeclared" ? UndeclaredKeyBehavior : keyKind extends "required" ? validateInnerDefinition<def, $, args> : def extends OptionalPropertyDefinition ? ErrorMessage<invalidOptionalKeyKindMessage> : def extends DefaultablePropertyTuple ? ErrorMessage<invalidDefaultableKeyKindMessage> : def extends PossibleDefaultableStringDefinition ? validatePossibleStringDefault<def, $, args, invalidDefaultableKeyKindMessage> : validateInnerDefinition<def, $, args>;
|
|
26
|
+
export type validatePossibleStringDefault<def extends string, $, args, errorMessage extends string> = parseString<def, $, args> extends DefaultablePropertyTuple ? ErrorMessage<errorMessage> : validateString<def, $, args>;
|
|
27
|
+
type validateSpread<def, inferredProperty, $, args> = inferredProperty extends object ? validateInnerDefinition<def, $, args> : ErrorType<writeInvalidSpreadTypeMessage<typeToString<inferredProperty>>>;
|
|
28
|
+
export type OptionalPropertyDefinition<baseDef = unknown> = OptionalPropertyTuple<baseDef> | OptionalPropertyString<baseDef & string>;
|
|
29
|
+
export type OptionalPropertyString<baseDef extends string = string> = `${baseDef}?`;
|
|
30
|
+
export type OptionalPropertyTuple<baseDef = unknown> = readonly [baseDef, "?"];
|
|
31
|
+
export type PossibleDefaultableStringDefinition = `${string}=${string}`;
|
|
32
|
+
export type DefaultablePropertyTuple<baseDef = unknown, thunkableProperty = unknown> = readonly [baseDef, "=", thunkableProperty];
|
|
33
|
+
export declare const invalidOptionalKeyKindMessage = "Only required keys may make their values optional, e.g. { [mySymbol]: ['number', '?'] }";
|
|
34
|
+
export type invalidOptionalKeyKindMessage = typeof invalidOptionalKeyKindMessage;
|
|
35
|
+
export declare const invalidDefaultableKeyKindMessage = "Only required keys may specify default values, e.g. { value: 'number = 0' }";
|
|
36
|
+
export type invalidDefaultableKeyKindMessage = typeof invalidDefaultableKeyKindMessage;
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { isArray } from "@ark/util";
|
|
2
|
+
import { parseInnerDefinition } from "./definition.js";
|
|
3
|
+
export const parseProperty = (def, ctx) => {
|
|
4
|
+
if (isArray(def)) {
|
|
5
|
+
if (def[1] === "=")
|
|
6
|
+
return [ctx.$.parseOwnDefinitionFormat(def[0], ctx), "=", def[2]];
|
|
7
|
+
if (def[1] === "?")
|
|
8
|
+
return [ctx.$.parseOwnDefinitionFormat(def[0], ctx), "?"];
|
|
9
|
+
}
|
|
10
|
+
// string-embedded defaults/optionals are handled by the string parser
|
|
11
|
+
return parseInnerDefinition(def, ctx);
|
|
12
|
+
};
|
|
13
|
+
// single quote use here is better for TypeScript's inlined error to avoid escapes
|
|
14
|
+
export const invalidOptionalKeyKindMessage = `Only required keys may make their values optional, e.g. { [mySymbol]: ['number', '?'] }`;
|
|
15
|
+
// single quote use here is better for TypeScript's inlined error to avoid escapes
|
|
16
|
+
export const invalidDefaultableKeyKindMessage = `Only required keys may specify default values, e.g. { value: 'number = 0' }`;
|
|
@@ -11,7 +11,7 @@ type BranchState = {
|
|
|
11
11
|
};
|
|
12
12
|
export type DynamicStateWithRoot = requireKeys<DynamicState, "root">;
|
|
13
13
|
export declare class DynamicState {
|
|
14
|
-
root: BaseRoot
|
|
14
|
+
root: BaseRoot | undefined;
|
|
15
15
|
branches: BranchState;
|
|
16
16
|
finalizer: ArkTypeScanner.FinalizingLookahead | undefined;
|
|
17
17
|
groups: BranchState[];
|
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import { type Scanner } from "@ark/util";
|
|
2
|
-
import type { string } from "../../../attributes.ts";
|
|
3
|
-
import type { Date } from "../../../keywords/constructors/Date.ts";
|
|
4
2
|
import type { InferredAst } from "../../ast/infer.ts";
|
|
5
3
|
import type { DynamicState } from "../../reduce/dynamic.ts";
|
|
6
4
|
import type { StaticState, state } from "../../reduce/static.ts";
|
|
@@ -9,7 +7,7 @@ export type StringLiteral<Text extends string = string> = DoubleQuotedStringLite
|
|
|
9
7
|
export type DoubleQuotedStringLiteral<Text extends string = string> = `"${Text}"`;
|
|
10
8
|
export type SingleQuotedStringLiteral<Text extends string = string> = `'${Text}'`;
|
|
11
9
|
export declare const parseEnclosed: (s: DynamicState, enclosing: EnclosingStartToken) => void;
|
|
12
|
-
export type parseEnclosed<s extends StaticState, enclosingStart extends EnclosingStartToken, unscanned extends string> = ArkTypeScanner.shiftUntil<unscanned, EnclosingTokens[enclosingStart]> extends ArkTypeScanner.shiftResult<infer scanned, infer nextUnscanned> ? nextUnscanned extends "" ? state.error<writeUnterminatedEnclosedMessage<scanned, enclosingStart>> : state.setRoot<s, InferredAst<enclosingStart extends EnclosingQuote ? scanned : enclosingStart extends "/" ? string
|
|
10
|
+
export type parseEnclosed<s extends StaticState, enclosingStart extends EnclosingStartToken, unscanned extends string> = ArkTypeScanner.shiftUntil<unscanned, EnclosingTokens[enclosingStart]> extends ArkTypeScanner.shiftResult<infer scanned, infer nextUnscanned> ? nextUnscanned extends "" ? state.error<writeUnterminatedEnclosedMessage<scanned, enclosingStart>> : state.setRoot<s, InferredAst<enclosingStart extends EnclosingQuote ? scanned : enclosingStart extends "/" ? string : Date, `${enclosingStart}${scanned}${EnclosingTokens[enclosingStart]}`>, nextUnscanned extends ArkTypeScanner.shift<string, infer unscanned> ? unscanned : ""> : never;
|
|
13
11
|
export declare const enclosingQuote: {
|
|
14
12
|
readonly "'": 1;
|
|
15
13
|
readonly '"': 1;
|
|
@@ -2,7 +2,8 @@ import { writeUnresolvableMessage, type BaseRoot, type GenericAst, type GenericR
|
|
|
2
2
|
import { type BigintLiteral, type NumberLiteral, type join, type lastOf } from "@ark/util";
|
|
3
3
|
import type { ArkAmbient } from "../../../config.ts";
|
|
4
4
|
import type { resolutionToAst } from "../../../scope.ts";
|
|
5
|
-
import type { GenericInstantiationAst
|
|
5
|
+
import type { GenericInstantiationAst } from "../../ast/generic.ts";
|
|
6
|
+
import type { InferredAst } from "../../ast/infer.ts";
|
|
6
7
|
import { writePrefixedPrivateReferenceMessage } from "../../ast/validate.ts";
|
|
7
8
|
import type { DynamicState } from "../../reduce/dynamic.ts";
|
|
8
9
|
import type { StaticState, state } from "../../reduce/static.ts";
|
|
@@ -5,7 +5,8 @@ import type { DynamicStateWithRoot } from "../../reduce/dynamic.ts";
|
|
|
5
5
|
import type { StringLiteral } from "../operand/enclosed.ts";
|
|
6
6
|
type UnitLiteralKeyword = "null" | "undefined" | "true" | "false";
|
|
7
7
|
export type UnitLiteral = StringLiteral | BigintLiteral | NumberLiteral | DateLiteral | UnitLiteralKeyword;
|
|
8
|
-
export
|
|
8
|
+
export type ParsedDefaultableProperty = readonly [BaseRoot, "=", unknown];
|
|
9
|
+
export declare const parseDefault: (s: DynamicStateWithRoot) => ParsedDefaultableProperty;
|
|
9
10
|
export type parseDefault<root, unscanned extends string> = trim<unscanned> extends infer defaultValue extends UnitLiteral ? [
|
|
10
11
|
root,
|
|
11
12
|
"=",
|
|
@@ -10,6 +10,6 @@ export const parseDefault = (s) => {
|
|
|
10
10
|
const defaultValue = defaultNode.unit instanceof Date ?
|
|
11
11
|
() => new Date(defaultNode.unit)
|
|
12
12
|
: defaultNode.unit;
|
|
13
|
-
return baseNode
|
|
13
|
+
return [baseNode, "=", defaultValue];
|
|
14
14
|
};
|
|
15
15
|
export const writeNonLiteralDefaultMessage = (defaultDef) => `Default value '${defaultDef}' must a literal value`;
|
package/out/parser/string.d.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { BaseParseContext, resolvableReferenceIn } from "@ark/schema";
|
|
2
2
|
import { type ErrorMessage } from "@ark/util";
|
|
3
3
|
import type { ArkAmbient } from "../config.ts";
|
|
4
|
-
import type { resolutionToAst } from "../scope.ts";
|
|
4
|
+
import type { InnerParseResult, resolutionToAst } from "../scope.ts";
|
|
5
5
|
import type { inferAstRoot } from "./ast/infer.ts";
|
|
6
|
-
import
|
|
6
|
+
import { DynamicState, type DynamicStateWithRoot } from "./reduce/dynamic.ts";
|
|
7
7
|
import type { StringifiablePrefixOperator } from "./reduce/shared.ts";
|
|
8
8
|
import type { state, StaticState } from "./reduce/static.ts";
|
|
9
9
|
import type { parseOperand } from "./shift/operand/operand.ts";
|
|
10
10
|
import { parseDefault } from "./shift/operator/default.ts";
|
|
11
11
|
import { writeUnexpectedCharacterMessage, type parseOperator } from "./shift/operator/operator.ts";
|
|
12
|
+
export declare const parseString: (def: string, ctx: BaseParseContext) => InnerParseResult;
|
|
12
13
|
/**
|
|
13
14
|
* Try to parse the definition from right to left using the most common syntax.
|
|
14
15
|
* This can be much more efficient for simple definitions.
|
|
@@ -19,7 +20,7 @@ export type parseString<def extends string, $, args> = def extends keyof $ ? res
|
|
|
19
20
|
] : fullStringParse<state.initialize<def>, $, args> : fullStringParse<state.initialize<def>, $, args>;
|
|
20
21
|
export type inferString<def extends string, $, args> = inferAstRoot<parseString<def, $, args>, $, args>;
|
|
21
22
|
export type BaseCompletions<$, args, otherSuggestions extends string = never> = resolvableReferenceIn<$> | resolvableReferenceIn<ArkAmbient.$> | (keyof args & string) | StringifiablePrefixOperator | otherSuggestions;
|
|
22
|
-
export declare const fullStringParse: (s: DynamicState) =>
|
|
23
|
+
export declare const fullStringParse: (s: DynamicState) => InnerParseResult;
|
|
23
24
|
type fullStringParse<s extends StaticState, $, args> = extractFinalizedResult<parseUntilFinalizer<s, $, args>>;
|
|
24
25
|
export declare const parseUntilFinalizer: (s: DynamicState) => DynamicStateWithRoot;
|
|
25
26
|
export type parseUntilFinalizer<s extends StaticState, $, args> = s["finalizer"] extends undefined ? parseUntilFinalizer<next<s, $, args>, $, args> : s;
|