arktype 2.0.0-dev.2 → 2.0.0-dev.21
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/LICENSE +7 -0
- package/out/api.d.ts +6 -0
- package/out/api.js +5 -0
- package/out/ark.d.ts +15 -39
- package/out/ark.js +9 -18
- package/out/config.d.ts +1 -0
- package/out/config.js +2 -0
- package/out/generic.d.ts +21 -0
- package/out/generic.js +24 -0
- package/out/match.d.ts +18 -24
- package/out/match.js +50 -60
- package/out/module.d.ts +11 -0
- package/out/module.js +2 -0
- package/out/parser/definition.d.ts +13 -15
- package/out/parser/definition.js +10 -13
- package/out/parser/generic.d.ts +5 -7
- package/out/parser/generic.js +7 -11
- package/out/parser/objectLiteral.d.ts +57 -18
- package/out/parser/objectLiteral.js +93 -52
- package/out/parser/semantic/bounds.d.ts +10 -8
- package/out/parser/semantic/bounds.js +0 -1
- package/out/parser/semantic/divisor.d.ts +5 -7
- package/out/parser/semantic/divisor.js +0 -1
- package/out/parser/semantic/infer.d.ts +24 -0
- package/out/parser/semantic/infer.js +1 -0
- package/out/parser/semantic/utils.d.ts +3 -7
- package/out/parser/semantic/utils.js +0 -1
- package/out/parser/semantic/validate.d.ts +16 -13
- package/out/parser/semantic/validate.js +1 -1
- package/out/parser/string/reduce/dynamic.d.ts +13 -13
- package/out/parser/string/reduce/dynamic.js +31 -26
- package/out/parser/string/reduce/shared.d.ts +14 -28
- package/out/parser/string/reduce/shared.js +4 -3
- package/out/parser/string/reduce/static.d.ts +26 -23
- package/out/parser/string/reduce/static.js +0 -1
- package/out/parser/string/shift/operand/date.d.ts +2 -2
- package/out/parser/string/shift/operand/date.js +4 -7
- package/out/parser/string/shift/operand/enclosed.d.ts +3 -4
- package/out/parser/string/shift/operand/enclosed.js +14 -14
- package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
- package/out/parser/string/shift/operand/genericArgs.js +6 -10
- package/out/parser/string/shift/operand/operand.d.ts +3 -4
- package/out/parser/string/shift/operand/operand.js +9 -13
- package/out/parser/string/shift/operand/unenclosed.d.ts +15 -20
- package/out/parser/string/shift/operand/unenclosed.js +24 -36
- package/out/parser/string/shift/operator/bounds.d.ts +8 -12
- package/out/parser/string/shift/operator/bounds.js +45 -56
- package/out/parser/string/shift/operator/divisor.d.ts +1 -3
- package/out/parser/string/shift/operator/divisor.js +1 -3
- package/out/parser/string/shift/operator/operator.d.ts +1 -2
- package/out/parser/string/shift/operator/operator.js +12 -19
- package/out/parser/string/shift/scanner.d.ts +8 -7
- package/out/parser/string/shift/scanner.js +17 -16
- package/out/parser/string/string.d.ts +9 -7
- package/out/parser/string/string.js +1 -3
- package/out/parser/tuple.d.ts +101 -42
- package/out/parser/tuple.js +107 -86
- package/out/scope.d.ts +44 -115
- package/out/scope.js +39 -188
- package/out/type.d.ts +63 -61
- package/out/type.js +30 -135
- package/package.json +33 -39
- package/__tests__/array.test.ts +0 -310
- package/__tests__/astUtils.test.ts +0 -17
- package/__tests__/badDefinitionType.test.ts +0 -51
- package/__tests__/basis.test.ts +0 -46
- package/__tests__/bounds.test.ts +0 -388
- package/__tests__/cast.test.ts +0 -32
- package/__tests__/completions.test.ts +0 -46
- package/__tests__/config.test.ts +0 -77
- package/__tests__/cyclic.test.ts +0 -144
- package/__tests__/dateLiteral.test.ts +0 -32
- package/__tests__/declared.test.ts +0 -124
- package/__tests__/discrimination.test.ts +0 -74
- package/__tests__/divisor.test.ts +0 -90
- package/__tests__/enclosed.test.ts +0 -93
- package/__tests__/examples.test.ts +0 -56
- package/__tests__/expressions.test.ts +0 -138
- package/__tests__/generateBenchData.ts +0 -89
- package/__tests__/generated/cyclic.ts +0 -3058
- package/__tests__/generic.test.ts +0 -294
- package/__tests__/group.test.ts +0 -72
- package/__tests__/hkt.test.ts +0 -22
- package/__tests__/imports.test.ts +0 -113
- package/__tests__/instanceof.test.ts +0 -102
- package/__tests__/intersection.test.ts +0 -104
- package/__tests__/jsdoc.test.ts +0 -64
- package/__tests__/keyTraversal.test.ts +0 -74
- package/__tests__/keyof.test.ts +0 -134
- package/__tests__/keywords.test.ts +0 -188
- package/__tests__/literal.test.ts +0 -51
- package/__tests__/match.bench.ts +0 -31
- package/__tests__/match.test.ts +0 -224
- package/__tests__/methods.bench.ts +0 -22
- package/__tests__/morph.test.ts +0 -409
- package/__tests__/narrow.test.ts +0 -102
- package/__tests__/nodeDefinitions.test.ts +0 -190
- package/__tests__/object.bench.ts +0 -28
- package/__tests__/objectLiteral.test.ts +0 -301
- package/__tests__/operand.bench.ts +0 -45
- package/__tests__/operator.bench.ts +0 -130
- package/__tests__/regex.test.ts +0 -58
- package/__tests__/scope.bench.ts +0 -39
- package/__tests__/scope.test.ts +0 -140
- package/__tests__/string.test.ts +0 -48
- package/__tests__/submodule.test.ts +0 -99
- package/__tests__/this.test.ts +0 -57
- package/__tests__/thunk.test.ts +0 -80
- package/__tests__/traverse.test.ts +0 -119
- package/__tests__/tsGenerics.test.ts +0 -12
- package/__tests__/typeMethods.test.ts +0 -69
- package/__tests__/typeReference.test.ts +0 -39
- package/__tests__/unenclosed.test.ts +0 -120
- package/__tests__/union.test.ts +0 -147
- package/ark.ts +0 -91
- package/main.ts +0 -18
- package/match.ts +0 -243
- package/out/ark.d.ts.map +0 -1
- package/out/ark.js.map +0 -1
- package/out/arkConfig.d.ts +0 -2
- package/out/arkConfig.d.ts.map +0 -1
- package/out/arkConfig.js +0 -10
- package/out/arkConfig.js.map +0 -1
- package/out/main.d.ts +0 -7
- package/out/main.d.ts.map +0 -1
- package/out/main.js +0 -4
- package/out/main.js.map +0 -1
- package/out/match.d.ts.map +0 -1
- package/out/match.js.map +0 -1
- package/out/parser/definition.d.ts.map +0 -1
- package/out/parser/definition.js.map +0 -1
- package/out/parser/generic.d.ts.map +0 -1
- package/out/parser/generic.js.map +0 -1
- package/out/parser/objectLiteral.d.ts.map +0 -1
- package/out/parser/objectLiteral.js.map +0 -1
- package/out/parser/semantic/bounds.d.ts.map +0 -1
- package/out/parser/semantic/bounds.js.map +0 -1
- package/out/parser/semantic/divisor.d.ts.map +0 -1
- package/out/parser/semantic/divisor.js.map +0 -1
- package/out/parser/semantic/intersections.d.ts +0 -14
- package/out/parser/semantic/intersections.d.ts.map +0 -1
- package/out/parser/semantic/intersections.js +0 -2
- package/out/parser/semantic/intersections.js.map +0 -1
- package/out/parser/semantic/semantic.d.ts +0 -33
- package/out/parser/semantic/semantic.d.ts.map +0 -1
- package/out/parser/semantic/semantic.js +0 -2
- package/out/parser/semantic/semantic.js.map +0 -1
- package/out/parser/semantic/utils.d.ts.map +0 -1
- package/out/parser/semantic/utils.js.map +0 -1
- package/out/parser/semantic/validate.d.ts.map +0 -1
- package/out/parser/semantic/validate.js.map +0 -1
- package/out/parser/shared.d.ts +0 -56
- package/out/parser/shared.d.ts.map +0 -1
- package/out/parser/shared.js +0 -55
- package/out/parser/shared.js.map +0 -1
- package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
- package/out/parser/string/reduce/dynamic.js.map +0 -1
- package/out/parser/string/reduce/shared.d.ts.map +0 -1
- package/out/parser/string/reduce/shared.js.map +0 -1
- package/out/parser/string/reduce/static.d.ts.map +0 -1
- package/out/parser/string/reduce/static.js.map +0 -1
- package/out/parser/string/shift/operand/date.d.ts.map +0 -1
- package/out/parser/string/shift/operand/date.js.map +0 -1
- package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operand/enclosed.js.map +0 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
- package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
- package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
- package/out/parser/string/shift/operand/operand.js.map +0 -1
- package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
- package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
- package/out/parser/string/shift/operator/bounds.js.map +0 -1
- package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
- package/out/parser/string/shift/operator/divisor.js.map +0 -1
- package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
- package/out/parser/string/shift/operator/operator.js.map +0 -1
- package/out/parser/string/shift/scanner.d.ts.map +0 -1
- package/out/parser/string/shift/scanner.js.map +0 -1
- package/out/parser/string/string.d.ts.map +0 -1
- package/out/parser/string/string.js.map +0 -1
- package/out/parser/tuple.d.ts.map +0 -1
- package/out/parser/tuple.js.map +0 -1
- package/out/scope.d.ts.map +0 -1
- package/out/scope.js.map +0 -1
- package/out/test.d.ts +0 -2
- package/out/test.d.ts.map +0 -1
- package/out/test.js +0 -7
- package/out/test.js.map +0 -1
- package/out/tsconfig.build.tsbuildinfo +0 -1
- package/out/type.d.ts.map +0 -1
- package/out/type.js.map +0 -1
- package/out/util.d.ts +0 -19
- package/out/util.d.ts.map +0 -1
- package/out/util.js +0 -7
- package/out/util.js.map +0 -1
- package/parser/definition.ts +0 -159
- package/parser/generic.ts +0 -68
- package/parser/objectLiteral.ts +0 -197
- package/parser/semantic/bounds.ts +0 -61
- package/parser/semantic/divisor.ts +0 -13
- package/parser/semantic/intersections.ts +0 -46
- package/parser/semantic/semantic.ts +0 -143
- package/parser/semantic/utils.ts +0 -26
- package/parser/semantic/validate.ts +0 -109
- package/parser/shared.ts +0 -167
- package/parser/string/reduce/dynamic.ts +0 -196
- package/parser/string/reduce/shared.ts +0 -101
- package/parser/string/reduce/static.ts +0 -298
- package/parser/string/shift/operand/date.ts +0 -54
- package/parser/string/shift/operand/enclosed.ts +0 -118
- package/parser/string/shift/operand/genericArgs.ts +0 -127
- package/parser/string/shift/operand/operand.ts +0 -48
- package/parser/string/shift/operand/unenclosed.ts +0 -268
- package/parser/string/shift/operator/bounds.ts +0 -264
- package/parser/string/shift/operator/divisor.ts +0 -36
- package/parser/string/shift/operator/operator.ts +0 -82
- package/parser/string/shift/scanner.ts +0 -209
- package/parser/string/string.ts +0 -90
- package/parser/tuple.ts +0 -475
- package/scope.ts +0 -548
- package/type.ts +0 -349
- package/util.ts +0 -36
package/out/parser/tuple.js
CHANGED
|
@@ -1,113 +1,133 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { writeUnsatisfiableExpressionError } from "./semantic/validate.js";
|
|
4
|
-
import { configureShallowDescendants, parseEntry } from "./shared.js";
|
|
1
|
+
import { jsObjects, makeRootAndArrayPropertiesMutable, tsKeywords } from "@arktype/schema";
|
|
2
|
+
import { append, objectKindOrDomainOf, throwParseError } from "@arktype/util";
|
|
5
3
|
import { writeMissingRightOperandMessage } from "./string/shift/operand/unenclosed.js";
|
|
6
4
|
export const parseTuple = (def, ctx) => maybeParseTupleExpression(def, ctx) ?? parseTupleLiteral(def, ctx);
|
|
7
5
|
export const parseTupleLiteral = (def, ctx) => {
|
|
8
|
-
|
|
9
|
-
let
|
|
10
|
-
|
|
11
|
-
let
|
|
12
|
-
|
|
13
|
-
if (
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return throwParseError(multipleVariadicMesage);
|
|
17
|
-
}
|
|
18
|
-
variadicIndex = i;
|
|
6
|
+
let sequences = [{}];
|
|
7
|
+
let i = 0;
|
|
8
|
+
while (i < def.length) {
|
|
9
|
+
let spread = false;
|
|
10
|
+
let optional = false;
|
|
11
|
+
if (def[i] === "..." && i < def.length - 1) {
|
|
12
|
+
spread = true;
|
|
13
|
+
i++;
|
|
19
14
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
variadicIndex = i;
|
|
15
|
+
const element = ctx.$.parse(def[i], ctx);
|
|
16
|
+
i++;
|
|
17
|
+
if (def[i] === "?") {
|
|
18
|
+
if (spread)
|
|
19
|
+
return throwParseError(spreadOptionalMessage);
|
|
20
|
+
optional = true;
|
|
21
|
+
i++;
|
|
28
22
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
props.push({ key: keywords.number, value: elementType });
|
|
23
|
+
if (spread) {
|
|
24
|
+
if (!element.extends(jsObjects.Array))
|
|
25
|
+
return throwParseError(writeNonArraySpreadMessage(element.expression));
|
|
26
|
+
// a spread must be distributed over branches e.g.:
|
|
27
|
+
// def: [string, ...(number[] | [true, false])]
|
|
28
|
+
// nodes: [string, ...number[]] | [string, true, false]
|
|
29
|
+
sequences = sequences.flatMap(base =>
|
|
30
|
+
// since appendElement mutates base, we have to shallow-ish clone it for each branch
|
|
31
|
+
element.branches.map(branch => appendSpreadBranch(makeRootAndArrayPropertiesMutable(base), branch)));
|
|
39
32
|
}
|
|
40
33
|
else {
|
|
41
|
-
|
|
42
|
-
key: {
|
|
43
|
-
name: `${i}`,
|
|
44
|
-
prerequisite: false,
|
|
45
|
-
optional: parsedEntry.kind === "optional"
|
|
46
|
-
},
|
|
47
|
-
value
|
|
48
|
-
});
|
|
34
|
+
sequences = sequences.map(base => appendElement(base, optional ? "optional" : "required", element));
|
|
49
35
|
}
|
|
50
|
-
ctx.path.pop();
|
|
51
36
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
37
|
+
return ctx.$.raw.schema(sequences.map(sequence => ({
|
|
38
|
+
proto: Array,
|
|
39
|
+
sequence
|
|
40
|
+
})));
|
|
41
|
+
};
|
|
42
|
+
const appendElement = (base, kind, element) => {
|
|
43
|
+
switch (kind) {
|
|
44
|
+
case "required":
|
|
45
|
+
if (base.optionals)
|
|
46
|
+
// e.g. [string?, number]
|
|
47
|
+
return throwParseError(requiredPostOptionalMessage);
|
|
48
|
+
if (base.variadic) {
|
|
49
|
+
// e.g. [...string[], number]
|
|
50
|
+
base.postfix = append(base.postfix, element);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
// e.g. [string, number]
|
|
54
|
+
base.prefix = append(base.prefix, element);
|
|
55
|
+
}
|
|
56
|
+
return base;
|
|
57
|
+
case "optional":
|
|
58
|
+
if (base.variadic)
|
|
59
|
+
// e.g. [...string[], number?]
|
|
60
|
+
return throwParseError(optionalPostVariadicMessage);
|
|
61
|
+
// e.g. [string, number?]
|
|
62
|
+
base.optionals = append(base.optionals, element);
|
|
63
|
+
return base;
|
|
64
|
+
case "variadic":
|
|
65
|
+
// e.g. [...string[], number, ...string[]]
|
|
66
|
+
if (base.postfix)
|
|
67
|
+
throwParseError(multipleVariadicMesage);
|
|
68
|
+
if (base.variadic) {
|
|
69
|
+
if (!base.variadic.equals(element)) {
|
|
70
|
+
// e.g. [...string[], ...number[]]
|
|
71
|
+
throwParseError(multipleVariadicMesage);
|
|
72
|
+
}
|
|
73
|
+
// e.g. [...string[], ...string[]]
|
|
74
|
+
// do nothing, second spread doesn't change the type
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
// e.g. [string, ...number[]]
|
|
78
|
+
base.variadic = element.raw;
|
|
79
|
+
}
|
|
80
|
+
return base;
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
const appendSpreadBranch = (base, branch) => {
|
|
84
|
+
const spread = branch.firstReferenceOfKind("sequence");
|
|
85
|
+
if (!spread) {
|
|
86
|
+
// the only array with no sequence reference is unknown[]
|
|
87
|
+
return appendElement(base, "variadic", tsKeywords.unknown);
|
|
61
88
|
}
|
|
62
|
-
|
|
89
|
+
spread.prefix.forEach(node => appendElement(base, "required", node));
|
|
90
|
+
spread.optionals.forEach(node => appendElement(base, "optional", node));
|
|
91
|
+
spread.variadic && appendElement(base, "variadic", spread.variadic);
|
|
92
|
+
spread.postfix.forEach(node => appendElement(base, "required", node));
|
|
93
|
+
return base;
|
|
63
94
|
};
|
|
64
|
-
|
|
65
|
-
const tupleExpressionResult =
|
|
66
|
-
? indexOneParsers[def[1]](def, ctx)
|
|
67
|
-
: isIndexZeroExpression(def)
|
|
68
|
-
? prefixParsers[def[0]](def, ctx)
|
|
95
|
+
const maybeParseTupleExpression = (def, ctx) => {
|
|
96
|
+
const tupleExpressionResult = isIndexZeroExpression(def) ? prefixParsers[def[0]](def, ctx)
|
|
97
|
+
: isIndexOneExpression(def) ? indexOneParsers[def[1]](def, ctx)
|
|
69
98
|
: undefined;
|
|
70
|
-
|
|
71
|
-
return tupleExpressionResult.isNever()
|
|
72
|
-
? throwParseError(writeUnsatisfiableExpressionError(def
|
|
73
|
-
.map((def) => (typeof def === "string" ? def : printable(def)))
|
|
74
|
-
.join(" ")))
|
|
75
|
-
: tupleExpressionResult;
|
|
76
|
-
}
|
|
99
|
+
return tupleExpressionResult;
|
|
77
100
|
};
|
|
78
|
-
export const
|
|
79
|
-
export const multipleVariadicMesage =
|
|
80
|
-
export const
|
|
101
|
+
export const writeNonArraySpreadMessage = (operand) => `Spread element must be an array (was ${operand})`;
|
|
102
|
+
export const multipleVariadicMesage = "A tuple may have at most one variadic element";
|
|
103
|
+
export const requiredPostOptionalMessage = "A required element may not follow an optional element";
|
|
104
|
+
export const optionalPostVariadicMessage = "An optional element may not follow a variadic element";
|
|
105
|
+
export const spreadOptionalMessage = "A spread element cannot be optional";
|
|
106
|
+
export const parseKeyOfTuple = (def, ctx) => ctx.$.parse(def[1], ctx).keyof();
|
|
81
107
|
const parseBranchTuple = (def, ctx) => {
|
|
82
|
-
if (def[2] === undefined)
|
|
108
|
+
if (def[2] === undefined)
|
|
83
109
|
return throwParseError(writeMissingRightOperandMessage(def[1], ""));
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
const r = ctx.scope.parse(def[2], ctx);
|
|
110
|
+
const l = ctx.$.parse(def[0], ctx);
|
|
111
|
+
const r = ctx.$.parse(def[2], ctx);
|
|
87
112
|
return def[1] === "&" ? l.and(r) : l.or(r);
|
|
88
113
|
};
|
|
89
|
-
const parseArrayTuple = (def, ctx) => ctx
|
|
114
|
+
const parseArrayTuple = (def, ctx) => ctx.$.parse(def[0], ctx).array();
|
|
90
115
|
const isIndexOneExpression = (def) => indexOneParsers[def[1]] !== undefined;
|
|
91
116
|
export const parseMorphTuple = (def, ctx) => {
|
|
92
117
|
if (typeof def[2] !== "function") {
|
|
93
118
|
return throwParseError(writeMalformedFunctionalExpressionMessage("=>", def[2]));
|
|
94
119
|
}
|
|
95
120
|
// TODO: nested morphs?
|
|
96
|
-
return
|
|
97
|
-
in: ctx.scope.parse(def[0], ctx),
|
|
98
|
-
morph: def[2]
|
|
99
|
-
});
|
|
121
|
+
return ctx.$.parse(def[0], ctx).pipe(def[2]);
|
|
100
122
|
};
|
|
101
123
|
export const writeMalformedFunctionalExpressionMessage = (operator, value) => `${operator === ":" ? "Narrow" : "Morph"} expression requires a function following '${operator}' (was ${typeof value})`;
|
|
102
124
|
export const parseNarrowTuple = (def, ctx) => {
|
|
103
125
|
if (typeof def[2] !== "function") {
|
|
104
126
|
return throwParseError(writeMalformedFunctionalExpressionMessage(":", def[2]));
|
|
105
127
|
}
|
|
106
|
-
return ctx.
|
|
107
|
-
.parse(def[0], ctx)
|
|
108
|
-
.constrain("predicate", def[2]);
|
|
128
|
+
return ctx.$.parse(def[0], ctx).constrain("predicate", def[2]);
|
|
109
129
|
};
|
|
110
|
-
const parseAttributeTuple = (def, ctx) =>
|
|
130
|
+
const parseAttributeTuple = (def, ctx) => ctx.$.parse(def[0], ctx).configureShallowDescendants(def[2]);
|
|
111
131
|
const indexOneParsers = {
|
|
112
132
|
"|": parseBranchTuple,
|
|
113
133
|
"&": parseBranchTuple,
|
|
@@ -118,19 +138,20 @@ const indexOneParsers = {
|
|
|
118
138
|
};
|
|
119
139
|
const prefixParsers = {
|
|
120
140
|
keyof: parseKeyOfTuple,
|
|
121
|
-
instanceof: (def) => {
|
|
141
|
+
instanceof: (def, ctx) => {
|
|
122
142
|
if (typeof def[1] !== "function") {
|
|
123
143
|
return throwParseError(writeInvalidConstructorMessage(objectKindOrDomainOf(def[1])));
|
|
124
144
|
}
|
|
125
145
|
const branches = def
|
|
126
146
|
.slice(1)
|
|
127
|
-
.map(
|
|
128
|
-
|
|
147
|
+
.map(ctor => typeof ctor === "function" ?
|
|
148
|
+
ctx.$.node("proto", { proto: ctor })
|
|
129
149
|
: throwParseError(writeInvalidConstructorMessage(objectKindOrDomainOf(ctor))));
|
|
130
|
-
return
|
|
150
|
+
return branches.length === 1 ?
|
|
151
|
+
branches[0]
|
|
152
|
+
: ctx.$.node("union", { branches });
|
|
131
153
|
},
|
|
132
|
-
"===": (def) =>
|
|
154
|
+
"===": (def, ctx) => ctx.$.units(def.slice(1))
|
|
133
155
|
};
|
|
134
156
|
const isIndexZeroExpression = (def) => prefixParsers[def[0]] !== undefined;
|
|
135
157
|
export const writeInvalidConstructorMessage = (actual) => `Expected a constructor following 'instanceof' operator (was ${actual})`;
|
|
136
|
-
//# sourceMappingURL=tuple.js.map
|
package/out/scope.d.ts
CHANGED
|
@@ -1,31 +1,14 @@
|
|
|
1
|
-
import { type ArkConfig, type
|
|
2
|
-
import { type
|
|
3
|
-
import
|
|
1
|
+
import { RawRootScope, type ArkConfig, type BaseRoot, type GenericProps, type PreparsedNodeResolution, type PrivateDeclaration, type RawRootResolutions, type RootScope, type UnknownRoot, type ambient, type arkKind, type destructuredExportContext, type destructuredImportContext, type exportedNameOf, type writeDuplicateAliasError } from "@arktype/schema";
|
|
2
|
+
import { type anyOrNever, type keyError, type nominal, type show } from "@arktype/util";
|
|
3
|
+
import { Generic } from "./generic.js";
|
|
4
4
|
import { type MatchParser } from "./match.js";
|
|
5
|
+
import type { Module } from "./module.js";
|
|
5
6
|
import { type inferDefinition, type validateDefinition } from "./parser/definition.js";
|
|
6
7
|
import { parseGenericParams, type GenericDeclaration, type GenericParamsParseError } from "./parser/generic.js";
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
exports: inferBootstrapped<{
|
|
12
|
-
exports: bootstrapExports<def>;
|
|
13
|
-
locals: bootstrapLocals<def> & parent;
|
|
14
|
-
ambient: ambient;
|
|
15
|
-
}>;
|
|
16
|
-
locals: inferBootstrapped<{
|
|
17
|
-
exports: bootstrapLocals<def>;
|
|
18
|
-
locals: bootstrapExports<def> & parent;
|
|
19
|
-
ambient: ambient;
|
|
20
|
-
}>;
|
|
21
|
-
ambient: ambient;
|
|
22
|
-
}>;
|
|
23
|
-
};
|
|
24
|
-
export interface ArkScopeConfig extends ArkConfig {
|
|
25
|
-
ambient?: Scope | null;
|
|
26
|
-
}
|
|
27
|
-
type validateScope<def, $> = {
|
|
28
|
-
[k in keyof def]: parseScopeKey<k>["params"] extends [] ? def[k] extends Type | PreparsedResolution ? def[k] : validateDefinition<def[k], $ & bootstrap<def>, {}> : parseScopeKey<k>["params"] extends GenericParamsParseError ? parseScopeKey<k>["params"][0] : validateDefinition<def[k], $ & bootstrap<def>, {
|
|
8
|
+
import { RawTypeParser, type DeclarationParser, type DefinitionParser, type Type, type TypeParser } from "./type.js";
|
|
9
|
+
export type ScopeParser = <const def>(def: validateScope<def>, config?: ArkConfig) => Scope<inferBootstrapped<bootstrapAliases<def>>>;
|
|
10
|
+
type validateScope<def> = {
|
|
11
|
+
[k in keyof def]: k extends symbol ? unknown : parseScopeKey<k>["params"] extends [] ? def[k] extends Type | PreparsedResolution ? def[k] : k extends PrivateDeclaration<infer name extends keyof def & string> ? keyError<writeDuplicateAliasError<name>> : validateDefinition<def[k], ambient & bootstrapAliases<def>, {}> : parseScopeKey<k>["params"] extends GenericParamsParseError ? parseScopeKey<k>["params"][0] : validateDefinition<def[k], ambient & bootstrapAliases<def>, {
|
|
29
12
|
[param in parseScopeKey<k>["params"][number]]: unknown;
|
|
30
13
|
}>;
|
|
31
14
|
};
|
|
@@ -36,119 +19,65 @@ export type bindThis<def> = {
|
|
|
36
19
|
type Def<def = {}> = nominal<def, "unparsed">;
|
|
37
20
|
/** sentinel indicating a scope that will be associated with a generic has not yet been parsed */
|
|
38
21
|
export type UnparsedScope = "$";
|
|
39
|
-
type bootstrap<def> = bootstrapLocals<def> & bootstrapExports<def>;
|
|
40
|
-
type bootstrapLocals<def> = bootstrapAliases<{
|
|
41
|
-
[k in keyof def & PrivateDeclaration as extractPrivateKey<k>]: def[k];
|
|
42
|
-
}>;
|
|
43
|
-
type bootstrapExports<def> = bootstrapAliases<{
|
|
44
|
-
[k in Exclude<keyof def, PrivateDeclaration>]: def[k];
|
|
45
|
-
}>;
|
|
46
22
|
/** These are legal as values of a scope but not as definitions in other contexts */
|
|
47
|
-
type PreparsedResolution =
|
|
23
|
+
type PreparsedResolution = PreparsedNodeResolution;
|
|
48
24
|
type bootstrapAliases<def> = {
|
|
49
25
|
[k in Exclude<keyof def, GenericDeclaration | symbol>]: def[k] extends PreparsedResolution ? def[k] : def[k] extends (() => infer thunkReturn extends PreparsedResolution) ? thunkReturn : Def<def[k]>;
|
|
50
26
|
} & {
|
|
51
|
-
[k in keyof def & GenericDeclaration as extractGenericName<k>]:
|
|
27
|
+
[k in keyof def & GenericDeclaration as extractGenericName<k>]: GenericProps<parseGenericParams<extractGenericParameters<k>>, def[k], UnparsedScope>;
|
|
52
28
|
};
|
|
53
|
-
type inferBootstrapped
|
|
54
|
-
[name in keyof
|
|
29
|
+
type inferBootstrapped<$> = show<{
|
|
30
|
+
[name in keyof $]: $[name] extends Def<infer def> ? inferDefinition<def, $ & ambient, {}> : $[name] extends GenericProps<infer params, infer def> ? Generic<params, def, $> : $[name];
|
|
55
31
|
}>;
|
|
56
32
|
type extractGenericName<k> = k extends GenericDeclaration<infer name> ? name : never;
|
|
57
33
|
type extractGenericParameters<k> = k extends GenericDeclaration<string, infer params> ? params : never;
|
|
58
|
-
type
|
|
59
|
-
|
|
60
|
-
|
|
34
|
+
export type resolve<reference extends keyof $ | keyof args, $, args> = (reference extends keyof args ? args[reference] : $[reference & keyof $]) extends infer resolution ? [
|
|
35
|
+
resolution
|
|
36
|
+
] extends [anyOrNever] ? resolution : resolution extends Def<infer def> ? inferDefinition<def, $, args> : resolution : never;
|
|
61
37
|
export type moduleKeyOf<$> = {
|
|
62
|
-
[k in keyof $]: $[k] extends
|
|
38
|
+
[k in keyof $]: $[k] extends {
|
|
39
|
+
[arkKind]: "module";
|
|
40
|
+
} ? k & string : never;
|
|
63
41
|
}[keyof $];
|
|
64
|
-
export type tryInferSubmoduleReference<$, token> = token extends `${infer submodule extends moduleKeyOf<$>}.${infer subalias}` ? subalias extends keyof $[submodule] ? $[submodule][subalias]
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
export
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
args: Record<string, TypeNode> | undefined;
|
|
80
|
-
};
|
|
81
|
-
type ParseContextInput = Pick<ParseContext, "baseName" | "args">;
|
|
82
|
-
export declare class Scope<r extends Resolutions = any> {
|
|
83
|
-
infer: extractOut<r["exports"]>;
|
|
84
|
-
inferIn: extractIn<r["exports"]>;
|
|
85
|
-
config: ArkScopeConfig;
|
|
42
|
+
export type tryInferSubmoduleReference<$, token> = token extends `${infer submodule extends moduleKeyOf<$>}.${infer subalias}` ? subalias extends keyof $[submodule] ? $[submodule][subalias] : never : never;
|
|
43
|
+
export interface ParseContext {
|
|
44
|
+
$: RawScope;
|
|
45
|
+
args?: Record<string, UnknownRoot>;
|
|
46
|
+
}
|
|
47
|
+
export declare const scope: ScopeParser;
|
|
48
|
+
export interface Scope<$ = any> extends RootScope<$> {
|
|
49
|
+
type: TypeParser<$>;
|
|
50
|
+
match: MatchParser<$>;
|
|
51
|
+
declare: DeclarationParser<$>;
|
|
52
|
+
define: DefinitionParser<$>;
|
|
53
|
+
import<names extends exportedNameOf<$>[]>(...names: names): Module<show<destructuredImportContext<$, names>>>;
|
|
54
|
+
export<names extends exportedNameOf<$>[]>(...names: names): Module<show<destructuredExportContext<$, names>>>;
|
|
55
|
+
}
|
|
56
|
+
export declare class RawScope<$ extends RawRootResolutions = RawRootResolutions> extends RawRootScope<$> {
|
|
86
57
|
private parseCache;
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
private ambient;
|
|
93
|
-
constructor(def: Dict, config: ArkScopeConfig);
|
|
94
|
-
static root: ScopeParser<{}, {}>;
|
|
95
|
-
type: TypeParser<$<r>>;
|
|
96
|
-
match: MatchParser<$<r>>;
|
|
97
|
-
declare: DeclarationParser<$<r>>;
|
|
98
|
-
scope: ScopeParser<r["exports"], r["ambient"]>;
|
|
99
|
-
define: DefinitionParser<$<r>>;
|
|
100
|
-
toAmbient(): Scope<{
|
|
101
|
-
exports: r["exports"];
|
|
102
|
-
locals: r["locals"];
|
|
103
|
-
ambient: r["exports"];
|
|
104
|
-
}>;
|
|
105
|
-
get<name extends keyof r["exports"] & string>(name: name): Type<r["exports"][name], $<r>>;
|
|
106
|
-
private createRootContext;
|
|
107
|
-
parseDefinition(def: unknown, input: ParseContextInput): TypeNode;
|
|
108
|
-
parse(def: unknown, ctx: ParseContext): TypeNode;
|
|
109
|
-
parseString(def: string, ctx: ParseContext): TypeNode;
|
|
110
|
-
maybeResolve(name: string): TypeNode | Generic | undefined;
|
|
111
|
-
/** If name is a valid reference to a submodule alias, return its resolution */
|
|
112
|
-
private maybeResolveSubalias;
|
|
113
|
-
maybeResolveNode(name: string): TypeNode | undefined;
|
|
114
|
-
import<names extends exportedName<r>[]>(...names: names): destructuredImportContext<r, names extends [] ? keyof r["exports"] & string : names[number]>;
|
|
115
|
-
bindThis(): {
|
|
116
|
-
this: import("@arktype/schema").IntersectionNode<unknown>;
|
|
58
|
+
constructor(def: Record<string, unknown>, config?: ArkConfig);
|
|
59
|
+
type: RawTypeParser;
|
|
60
|
+
match: MatchParser<$>;
|
|
61
|
+
declare: () => {
|
|
62
|
+
type: RawTypeParser;
|
|
117
63
|
};
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
64
|
+
define: (def: unknown) => unknown;
|
|
65
|
+
preparseRoot(def: unknown): unknown;
|
|
66
|
+
parseRoot(def: unknown): BaseRoot;
|
|
67
|
+
parse(def: unknown, ctx: ParseContext): BaseRoot;
|
|
68
|
+
parseString(def: string, ctx: ParseContext): BaseRoot;
|
|
121
69
|
}
|
|
122
|
-
type destructuredExportContext<r extends Resolutions, name extends exportedName<r>> = {
|
|
123
|
-
exports: {
|
|
124
|
-
[k in name]: r["exports"][k];
|
|
125
|
-
};
|
|
126
|
-
locals: r["locals"] & {
|
|
127
|
-
[k in Exclude<keyof r["exports"], name>]: r["exports"][k];
|
|
128
|
-
};
|
|
129
|
-
ambient: r["ambient"];
|
|
130
|
-
};
|
|
131
|
-
type destructuredImportContext<r extends Resolutions, name extends exportedName<r>> = {
|
|
132
|
-
[k in name as `#${k & string}`]: type.cast<r["exports"][k]>;
|
|
133
|
-
};
|
|
134
70
|
export declare const writeShallowCycleErrorMessage: (name: string, seen: string[]) => string;
|
|
135
|
-
export declare const writeDuplicateNameMessage: <name extends string>(name: name) => `Duplicate name '${name}'`;
|
|
136
|
-
type writeDuplicateNameMessage<name extends string> = `Duplicate name '${name}'`;
|
|
137
71
|
export type ParsedScopeKey = {
|
|
138
|
-
isLocal: boolean;
|
|
139
72
|
name: string;
|
|
140
73
|
params: string[];
|
|
141
74
|
};
|
|
142
75
|
export declare const parseScopeKey: (k: string) => ParsedScopeKey;
|
|
143
|
-
type parseScopeKey<k> = k extends
|
|
144
|
-
type parsePossibleGenericDeclaration<k, isLocal extends boolean> = k extends GenericDeclaration<infer name, infer paramString> ? {
|
|
145
|
-
isLocal: isLocal;
|
|
76
|
+
export type parseScopeKey<k> = k extends GenericDeclaration<infer name, infer paramString> ? {
|
|
146
77
|
name: name;
|
|
147
78
|
params: parseGenericParams<paramString>;
|
|
148
79
|
} : {
|
|
149
|
-
isLocal: isLocal;
|
|
150
80
|
name: k;
|
|
151
81
|
params: [];
|
|
152
82
|
};
|
|
153
83
|
export {};
|
|
154
|
-
//# sourceMappingURL=scope.d.ts.map
|