arktype 1.0.28-alpha → 1.1.0-dev-2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main.d.ts +11 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +8 -0
- package/dist/main.js.map +1 -0
- package/dist/match.d.ts +46 -0
- package/dist/match.d.ts.map +1 -0
- package/dist/match.js +23 -0
- package/dist/match.js.map +1 -0
- package/dist/parser/definition.d.ts +32 -0
- package/dist/parser/definition.d.ts.map +1 -0
- package/dist/parser/definition.js +29 -0
- package/dist/parser/definition.js.map +1 -0
- package/dist/parser/generic.d.ts +44 -0
- package/dist/parser/generic.d.ts.map +1 -0
- package/dist/parser/generic.js +46 -0
- package/dist/parser/generic.js.map +1 -0
- package/dist/parser/objectLiteral.d.ts +24 -0
- package/dist/parser/objectLiteral.d.ts.map +1 -0
- package/dist/parser/objectLiteral.js +38 -0
- package/dist/parser/objectLiteral.js.map +1 -0
- package/dist/parser/semantic/bounds.d.ts +13 -0
- package/dist/parser/semantic/bounds.d.ts.map +1 -0
- package/dist/parser/semantic/bounds.js +2 -0
- package/dist/parser/semantic/bounds.js.map +1 -0
- package/dist/parser/semantic/divisor.d.ts +9 -0
- package/dist/parser/semantic/divisor.d.ts.map +1 -0
- package/dist/parser/semantic/divisor.js +2 -0
- package/dist/parser/semantic/divisor.js.map +1 -0
- package/dist/parser/semantic/intersections.d.ts +15 -0
- package/dist/parser/semantic/intersections.d.ts.map +1 -0
- package/dist/parser/semantic/intersections.js +2 -0
- package/dist/parser/semantic/intersections.js.map +1 -0
- package/dist/parser/semantic/semantic.d.ts +22 -0
- package/dist/parser/semantic/semantic.d.ts.map +1 -0
- package/dist/parser/semantic/semantic.js +2 -0
- package/dist/parser/semantic/semantic.js.map +1 -0
- package/dist/parser/semantic/utils.d.ts +8 -0
- package/dist/parser/semantic/utils.d.ts.map +1 -0
- package/dist/parser/semantic/utils.js +2 -0
- package/dist/parser/semantic/utils.js.map +1 -0
- package/dist/parser/semantic/validate.d.ts +24 -0
- package/dist/parser/semantic/validate.d.ts.map +1 -0
- package/dist/parser/semantic/validate.js +2 -0
- package/dist/parser/semantic/validate.js.map +1 -0
- package/dist/parser/shared.d.ts +48 -0
- package/dist/parser/shared.d.ts.map +1 -0
- package/dist/parser/shared.js +41 -0
- package/dist/parser/shared.js.map +1 -0
- package/dist/parser/string/reduce/dynamic.d.ts +40 -0
- package/dist/parser/string/reduce/dynamic.d.ts.map +1 -0
- package/dist/parser/string/reduce/dynamic.js +122 -0
- package/dist/parser/string/reduce/dynamic.js.map +1 -0
- package/dist/parser/string/reduce/shared.d.ts +29 -0
- package/dist/parser/string/reduce/shared.d.ts.map +1 -0
- package/dist/parser/string/reduce/shared.js +7 -0
- package/dist/parser/string/reduce/shared.js.map +1 -0
- package/dist/parser/string/reduce/static.d.ts +177 -0
- package/dist/parser/string/reduce/static.d.ts.map +1 -0
- package/dist/parser/string/reduce/static.js +2 -0
- package/dist/parser/string/reduce/static.js.map +1 -0
- package/dist/parser/string/shift/operand/date.d.ts +11 -0
- package/dist/parser/string/shift/operand/date.d.ts.map +1 -0
- package/dist/parser/string/shift/operand/date.js +26 -0
- package/dist/parser/string/shift/operand/date.js.map +1 -0
- package/dist/parser/string/shift/operand/enclosed.d.ts +49 -0
- package/dist/parser/string/shift/operand/enclosed.d.ts.map +1 -0
- package/dist/parser/string/shift/operand/enclosed.js +50 -0
- package/dist/parser/string/shift/operand/enclosed.js.map +1 -0
- package/dist/parser/string/shift/operand/operand.d.ts +9 -0
- package/dist/parser/string/shift/operand/operand.d.ts.map +1 -0
- package/dist/parser/string/shift/operand/operand.js +17 -0
- package/dist/parser/string/shift/operand/operand.js.map +1 -0
- package/dist/parser/string/shift/operand/unenclosed.d.ts +36 -0
- package/dist/parser/string/shift/operand/unenclosed.d.ts.map +1 -0
- package/dist/parser/string/shift/operand/unenclosed.js +68 -0
- package/dist/parser/string/shift/operand/unenclosed.js.map +1 -0
- package/dist/parser/string/shift/operator/bounds.d.ts +55 -0
- package/dist/parser/string/shift/operator/bounds.d.ts.map +1 -0
- package/dist/parser/string/shift/operator/bounds.js +95 -0
- package/dist/parser/string/shift/operator/bounds.js.map +1 -0
- package/dist/parser/string/shift/operator/divisor.d.ts +9 -0
- package/dist/parser/string/shift/operator/divisor.d.ts.map +1 -0
- package/dist/parser/string/shift/operator/divisor.js +13 -0
- package/dist/parser/string/shift/operator/divisor.js.map +1 -0
- package/dist/parser/string/shift/operator/operator.d.ts +13 -0
- package/dist/parser/string/shift/operator/operator.d.ts.map +1 -0
- package/dist/parser/string/shift/operator/operator.js +29 -0
- package/dist/parser/string/shift/operator/operator.js.map +1 -0
- package/dist/parser/string/shift/scanner.d.ts +71 -0
- package/dist/parser/string/shift/scanner.d.ts.map +1 -0
- package/dist/parser/string/shift/scanner.js +99 -0
- package/dist/parser/string/shift/scanner.js.map +1 -0
- package/dist/parser/string/string.d.ts +26 -0
- package/dist/parser/string/string.d.ts.map +1 -0
- package/dist/parser/string/string.js +31 -0
- package/dist/parser/string/string.js.map +1 -0
- package/dist/parser/tuple.d.ts +73 -0
- package/dist/parser/tuple.d.ts.map +1 -0
- package/dist/parser/tuple.js +134 -0
- package/dist/parser/tuple.js.map +1 -0
- package/dist/scope.d.ts +159 -0
- package/dist/scope.d.ts.map +1 -0
- package/dist/scope.js +248 -0
- package/dist/scope.js.map +1 -0
- package/dist/scopes/ark.d.ts +38 -0
- package/dist/scopes/ark.d.ts.map +1 -0
- package/dist/scopes/ark.js +24 -0
- package/dist/scopes/ark.js.map +1 -0
- package/dist/scopes/jsObjects.d.ts +19 -0
- package/dist/scopes/jsObjects.d.ts.map +1 -0
- package/dist/scopes/jsObjects.js +15 -0
- package/dist/scopes/jsObjects.js.map +1 -0
- package/dist/scopes/parsing.d.ts +18 -0
- package/dist/scopes/parsing.d.ts.map +1 -0
- package/dist/scopes/parsing.js +61 -0
- package/dist/scopes/parsing.js.map +1 -0
- package/dist/scopes/tsGenerics.d.ts +17 -0
- package/dist/scopes/tsGenerics.d.ts.map +1 -0
- package/dist/scopes/tsGenerics.js +13 -0
- package/dist/scopes/tsGenerics.js.map +1 -0
- package/dist/scopes/tsKeywords.d.ts +24 -0
- package/dist/scopes/tsKeywords.d.ts.map +1 -0
- package/dist/scopes/tsKeywords.js +20 -0
- package/dist/scopes/tsKeywords.js.map +1 -0
- package/dist/scopes/utils/creditCard.d.ts +3 -0
- package/dist/scopes/utils/creditCard.d.ts.map +1 -0
- package/dist/{mjs/scopes/validation → scopes/utils}/creditCard.js +14 -12
- package/dist/scopes/utils/creditCard.js.map +1 -0
- package/dist/{types/scopes/validation → scopes/utils}/date.d.ts +3 -2
- package/dist/scopes/utils/date.d.ts.map +1 -0
- package/dist/{mjs/scopes/validation → scopes/utils}/date.js +24 -19
- package/dist/scopes/utils/date.js.map +1 -0
- package/dist/scopes/validation.d.ts +20 -0
- package/dist/scopes/validation.d.ts.map +1 -0
- package/dist/scopes/validation.js +57 -0
- package/dist/scopes/validation.js.map +1 -0
- package/dist/type.d.ts +101 -0
- package/dist/type.d.ts.map +1 -0
- package/dist/type.js +138 -0
- package/dist/type.js.map +1 -0
- package/main.ts +20 -0
- package/match.ts +137 -0
- package/package.json +41 -108
- package/parser/definition.ts +156 -0
- package/parser/generic.ts +208 -0
- package/parser/objectLiteral.ts +120 -0
- package/parser/semantic/bounds.ts +63 -0
- package/parser/semantic/divisor.ts +13 -0
- package/parser/semantic/intersections.ts +47 -0
- package/parser/semantic/semantic.ts +107 -0
- package/parser/semantic/utils.ts +26 -0
- package/parser/semantic/validate.ts +105 -0
- package/parser/shared.ts +141 -0
- package/parser/string/reduce/dynamic.ts +181 -0
- package/parser/string/reduce/shared.ts +73 -0
- package/parser/string/reduce/static.ts +300 -0
- package/parser/string/shift/operand/date.ts +57 -0
- package/parser/string/shift/operand/enclosed.ts +119 -0
- package/parser/string/shift/operand/operand.ts +48 -0
- package/parser/string/shift/operand/unenclosed.ts +282 -0
- package/parser/string/shift/operator/bounds.ts +229 -0
- package/parser/string/shift/operator/divisor.ts +36 -0
- package/parser/string/shift/operator/operator.ts +82 -0
- package/parser/string/shift/scanner.ts +209 -0
- package/parser/string/string.ts +93 -0
- package/parser/tuple.ts +475 -0
- package/scope.ts +573 -0
- package/scopes/ark.ts +64 -0
- package/scopes/jsObjects.ts +29 -0
- package/scopes/parsing.ts +81 -0
- package/scopes/tsGenerics.ts +28 -0
- package/scopes/tsKeywords.ts +39 -0
- package/scopes/utils/creditCard.ts +37 -0
- package/scopes/utils/date.ts +108 -0
- package/scopes/validation.ts +81 -0
- package/type.ts +403 -0
- package/LICENSE +0 -7
- package/README.md +0 -351
- package/dist/cjs/main.js +0 -38
- package/dist/cjs/nodes/branch.js +0 -140
- package/dist/cjs/nodes/compose.js +0 -163
- package/dist/cjs/nodes/discriminate.js +0 -265
- package/dist/cjs/nodes/node.js +0 -139
- package/dist/cjs/nodes/predicate.js +0 -86
- package/dist/cjs/nodes/rules/class.js +0 -25
- package/dist/cjs/nodes/rules/collapsibleSet.js +0 -48
- package/dist/cjs/nodes/rules/divisor.js +0 -29
- package/dist/cjs/nodes/rules/props.js +0 -167
- package/dist/cjs/nodes/rules/range.js +0 -105
- package/dist/cjs/nodes/rules/regex.js +0 -26
- package/dist/cjs/nodes/rules/rules.js +0 -123
- package/dist/cjs/package.json +0 -3
- package/dist/cjs/parse/ast/ast.js +0 -4
- package/dist/cjs/parse/ast/bound.js +0 -16
- package/dist/cjs/parse/ast/config.js +0 -13
- package/dist/cjs/parse/ast/distributableFunction.js +0 -48
- package/dist/cjs/parse/ast/divisor.js +0 -9
- package/dist/cjs/parse/ast/intersection.js +0 -30
- package/dist/cjs/parse/ast/keyof.js +0 -111
- package/dist/cjs/parse/ast/morph.js +0 -58
- package/dist/cjs/parse/ast/narrow.js +0 -22
- package/dist/cjs/parse/ast/node.js +0 -4
- package/dist/cjs/parse/ast/tuple.js +0 -94
- package/dist/cjs/parse/ast/union.js +0 -9
- package/dist/cjs/parse/ast/utils.js +0 -4
- package/dist/cjs/parse/definition.js +0 -61
- package/dist/cjs/parse/record.js +0 -37
- package/dist/cjs/parse/string/reduce/dynamic.js +0 -167
- package/dist/cjs/parse/string/reduce/shared.js +0 -23
- package/dist/cjs/parse/string/reduce/static.js +0 -4
- package/dist/cjs/parse/string/shift/operand/enclosed.js +0 -54
- package/dist/cjs/parse/string/shift/operand/operand.js +0 -11
- package/dist/cjs/parse/string/shift/operand/unenclosed.js +0 -53
- package/dist/cjs/parse/string/shift/operator/bounds.js +0 -87
- package/dist/cjs/parse/string/shift/operator/divisor.js +0 -35
- package/dist/cjs/parse/string/shift/operator/operator.js +0 -26
- package/dist/cjs/parse/string/shift/scanner.js +0 -114
- package/dist/cjs/parse/string/string.js +0 -43
- package/dist/cjs/scopes/ark.js +0 -45
- package/dist/cjs/scopes/cache.js +0 -52
- package/dist/cjs/scopes/expressions.js +0 -29
- package/dist/cjs/scopes/jsObjects.js +0 -93
- package/dist/cjs/scopes/scope.js +0 -323
- package/dist/cjs/scopes/tsKeywords.js +0 -113
- package/dist/cjs/scopes/type.js +0 -50
- package/dist/cjs/scopes/validation/creditCard.js +0 -47
- package/dist/cjs/scopes/validation/date.js +0 -62
- package/dist/cjs/scopes/validation/validation.js +0 -108
- package/dist/cjs/traverse/problems.js +0 -281
- package/dist/cjs/traverse/traverse.js +0 -390
- package/dist/cjs/utils/chainableNoOpProxy.js +0 -23
- package/dist/cjs/utils/data.js +0 -53
- package/dist/cjs/utils/domains.js +0 -30
- package/dist/cjs/utils/errors.js +0 -26
- package/dist/cjs/utils/freeze.js +0 -15
- package/dist/cjs/utils/generics.js +0 -63
- package/dist/cjs/utils/numericLiterals.js +0 -75
- package/dist/cjs/utils/objectKinds.js +0 -69
- package/dist/cjs/utils/paths.js +0 -32
- package/dist/cjs/utils/serialize.js +0 -74
- package/dist/cjs/utils/unionToTuple.js +0 -4
- package/dist/mjs/main.js +0 -8
- package/dist/mjs/nodes/branch.js +0 -123
- package/dist/mjs/nodes/compose.js +0 -141
- package/dist/mjs/nodes/discriminate.js +0 -250
- package/dist/mjs/nodes/node.js +0 -117
- package/dist/mjs/nodes/predicate.js +0 -69
- package/dist/mjs/nodes/rules/class.js +0 -11
- package/dist/mjs/nodes/rules/collapsibleSet.js +0 -34
- package/dist/mjs/nodes/rules/divisor.js +0 -15
- package/dist/mjs/nodes/rules/props.js +0 -149
- package/dist/mjs/nodes/rules/range.js +0 -86
- package/dist/mjs/nodes/rules/regex.js +0 -11
- package/dist/mjs/nodes/rules/rules.js +0 -106
- package/dist/mjs/package.json +0 -3
- package/dist/mjs/parse/ast/ast.js +0 -1
- package/dist/mjs/parse/ast/bound.js +0 -2
- package/dist/mjs/parse/ast/config.js +0 -9
- package/dist/mjs/parse/ast/distributableFunction.js +0 -34
- package/dist/mjs/parse/ast/divisor.js +0 -1
- package/dist/mjs/parse/ast/intersection.js +0 -16
- package/dist/mjs/parse/ast/keyof.js +0 -103
- package/dist/mjs/parse/ast/morph.js +0 -44
- package/dist/mjs/parse/ast/narrow.js +0 -14
- package/dist/mjs/parse/ast/node.js +0 -1
- package/dist/mjs/parse/ast/tuple.js +0 -80
- package/dist/mjs/parse/ast/union.js +0 -1
- package/dist/mjs/parse/ast/utils.js +0 -1
- package/dist/mjs/parse/definition.js +0 -46
- package/dist/mjs/parse/record.js +0 -29
- package/dist/mjs/parse/string/reduce/dynamic.js +0 -159
- package/dist/mjs/parse/string/reduce/shared.js +0 -6
- package/dist/mjs/parse/string/reduce/static.js +0 -1
- package/dist/mjs/parse/string/shift/operand/enclosed.js +0 -39
- package/dist/mjs/parse/string/shift/operand/operand.js +0 -3
- package/dist/mjs/parse/string/shift/operand/unenclosed.js +0 -36
- package/dist/mjs/parse/string/shift/operator/bounds.js +0 -70
- package/dist/mjs/parse/string/shift/operator/divisor.js +0 -21
- package/dist/mjs/parse/string/shift/operator/operator.js +0 -13
- package/dist/mjs/parse/string/shift/scanner.js +0 -106
- package/dist/mjs/parse/string/string.js +0 -28
- package/dist/mjs/scopes/ark.js +0 -28
- package/dist/mjs/scopes/cache.js +0 -38
- package/dist/mjs/scopes/expressions.js +0 -89
- package/dist/mjs/scopes/jsObjects.js +0 -82
- package/dist/mjs/scopes/scope.js +0 -303
- package/dist/mjs/scopes/tsKeywords.js +0 -116
- package/dist/mjs/scopes/type.js +0 -35
- package/dist/mjs/scopes/validation/validation.js +0 -112
- package/dist/mjs/traverse/problems.js +0 -260
- package/dist/mjs/traverse/traverse.js +0 -373
- package/dist/mjs/utils/chainableNoOpProxy.js +0 -15
- package/dist/mjs/utils/data.js +0 -38
- package/dist/mjs/utils/domains.js +0 -15
- package/dist/mjs/utils/errors.js +0 -10
- package/dist/mjs/utils/freeze.js +0 -7
- package/dist/mjs/utils/generics.js +0 -38
- package/dist/mjs/utils/numericLiterals.js +0 -72
- package/dist/mjs/utils/objectKinds.js +0 -55
- package/dist/mjs/utils/paths.js +0 -18
- package/dist/mjs/utils/serialize.js +0 -58
- package/dist/mjs/utils/unionToTuple.js +0 -1
- package/dist/tsconfig.temp.tsbuildinfo +0 -1
- package/dist/types/main.d.ts +0 -15
- package/dist/types/nodes/branch.d.ts +0 -29
- package/dist/types/nodes/compose.d.ts +0 -98
- package/dist/types/nodes/discriminate.d.ts +0 -26
- package/dist/types/nodes/node.d.ts +0 -69
- package/dist/types/nodes/predicate.d.ts +0 -18
- package/dist/types/nodes/rules/class.d.ts +0 -4
- package/dist/types/nodes/rules/collapsibleSet.d.ts +0 -4
- package/dist/types/nodes/rules/divisor.d.ts +0 -3
- package/dist/types/nodes/rules/props.d.ts +0 -43
- package/dist/types/nodes/rules/range.d.ts +0 -34
- package/dist/types/nodes/rules/regex.d.ts +0 -5
- package/dist/types/nodes/rules/rules.d.ts +0 -33
- package/dist/types/parse/ast/ast.d.ts +0 -24
- package/dist/types/parse/ast/bound.d.ts +0 -27
- package/dist/types/parse/ast/config.d.ts +0 -15
- package/dist/types/parse/ast/distributableFunction.d.ts +0 -18
- package/dist/types/parse/ast/divisor.d.ts +0 -13
- package/dist/types/parse/ast/intersection.d.ts +0 -31
- package/dist/types/parse/ast/keyof.d.ts +0 -10
- package/dist/types/parse/ast/morph.d.ts +0 -18
- package/dist/types/parse/ast/narrow.d.ts +0 -21
- package/dist/types/parse/ast/node.d.ts +0 -35
- package/dist/types/parse/ast/tuple.d.ts +0 -41
- package/dist/types/parse/ast/union.d.ts +0 -17
- package/dist/types/parse/ast/utils.d.ts +0 -7
- package/dist/types/parse/definition.d.ts +0 -30
- package/dist/types/parse/record.d.ts +0 -26
- package/dist/types/parse/string/reduce/dynamic.d.ts +0 -34
- package/dist/types/parse/string/reduce/shared.d.ts +0 -25
- package/dist/types/parse/string/reduce/static.d.ts +0 -130
- package/dist/types/parse/string/shift/operand/enclosed.d.ts +0 -28
- package/dist/types/parse/string/shift/operand/operand.d.ts +0 -9
- package/dist/types/parse/string/shift/operand/unenclosed.d.ts +0 -18
- package/dist/types/parse/string/shift/operator/bounds.d.ts +0 -21
- package/dist/types/parse/string/shift/operator/divisor.d.ts +0 -13
- package/dist/types/parse/string/shift/operator/operator.d.ts +0 -13
- package/dist/types/parse/string/shift/scanner.d.ts +0 -89
- package/dist/types/parse/string/string.d.ts +0 -25
- package/dist/types/scopes/ark.d.ts +0 -209
- package/dist/types/scopes/cache.d.ts +0 -14
- package/dist/types/scopes/expressions.d.ts +0 -136
- package/dist/types/scopes/jsObjects.d.ts +0 -26
- package/dist/types/scopes/scope.d.ts +0 -99
- package/dist/types/scopes/tsKeywords.d.ts +0 -50
- package/dist/types/scopes/type.d.ts +0 -67
- package/dist/types/scopes/validation/creditCard.d.ts +0 -2
- package/dist/types/scopes/validation/validation.d.ts +0 -49
- package/dist/types/traverse/problems.d.ts +0 -88
- package/dist/types/traverse/traverse.d.ts +0 -55
- package/dist/types/utils/chainableNoOpProxy.d.ts +0 -14
- package/dist/types/utils/data.d.ts +0 -13
- package/dist/types/utils/domains.d.ts +0 -32
- package/dist/types/utils/errors.d.ts +0 -6
- package/dist/types/utils/freeze.d.ts +0 -2
- package/dist/types/utils/generics.d.ts +0 -125
- package/dist/types/utils/numericLiterals.d.ts +0 -46
- package/dist/types/utils/objectKinds.d.ts +0 -84
- package/dist/types/utils/paths.d.ts +0 -9
- package/dist/types/utils/serialize.d.ts +0 -37
- package/dist/types/utils/unionToTuple.d.ts +0 -11
package/match.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import type { Morph } from "@arktype/schema"
|
|
2
|
+
import type {
|
|
3
|
+
conform,
|
|
4
|
+
entryOf,
|
|
5
|
+
ErrorMessage,
|
|
6
|
+
evaluate,
|
|
7
|
+
Fn,
|
|
8
|
+
isDisjoint,
|
|
9
|
+
join,
|
|
10
|
+
paramsOf,
|
|
11
|
+
replaceKey,
|
|
12
|
+
returnOf,
|
|
13
|
+
unionToTuple,
|
|
14
|
+
valueOf
|
|
15
|
+
} from "@arktype/util"
|
|
16
|
+
import type { Scope } from "./scope.js"
|
|
17
|
+
import { Type, type inferTypeRoot, type validateTypeRoot } from "./type.js"
|
|
18
|
+
|
|
19
|
+
type cedille = "¸"
|
|
20
|
+
|
|
21
|
+
type serializedWhentry<
|
|
22
|
+
k extends string,
|
|
23
|
+
v extends string
|
|
24
|
+
> = `[${k}${cedille}${v}]`
|
|
25
|
+
|
|
26
|
+
type parseWhentryKey<
|
|
27
|
+
s extends string,
|
|
28
|
+
$,
|
|
29
|
+
result = {}
|
|
30
|
+
> = s extends `${serializedWhentry<infer k, infer v>}${cedille}${infer tail}`
|
|
31
|
+
? validateTypeRoot<v, $> extends ErrorMessage<infer message>
|
|
32
|
+
? ErrorMessage<message>
|
|
33
|
+
: parseWhentryKey<tail, $, result & { [_ in k]: inferTypeRoot<v, $> }>
|
|
34
|
+
: s extends serializedWhentry<infer k, infer v>
|
|
35
|
+
? validateTypeRoot<v, $> extends ErrorMessage<infer message>
|
|
36
|
+
? ErrorMessage<message>
|
|
37
|
+
: evaluate<result & { [_ in k]: inferTypeRoot<v, $> }>
|
|
38
|
+
: validateTypeRoot<s, $> extends ErrorMessage<infer message>
|
|
39
|
+
? ErrorMessage<message>
|
|
40
|
+
: never
|
|
41
|
+
|
|
42
|
+
export type WhenParser<$> = <const def>(
|
|
43
|
+
def: validateTypeRoot<def, $>
|
|
44
|
+
) => join<
|
|
45
|
+
unionToTuple<`[${join<conform<entryOf<def>, [string, string]>, cedille>}]`>,
|
|
46
|
+
cedille
|
|
47
|
+
>
|
|
48
|
+
|
|
49
|
+
export const createWhenParser = <$>(scope: Scope): WhenParser<$> => {
|
|
50
|
+
const parser = (def: unknown) => new Type(def, scope).alias
|
|
51
|
+
return parser as never
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
type MatchContext = {
|
|
55
|
+
inConstraint: unknown
|
|
56
|
+
outConstraint: unknown
|
|
57
|
+
thens: readonly Fn[]
|
|
58
|
+
$: unknown
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
type validateCases<cases, ctx extends MatchContext> = {
|
|
62
|
+
// adding keyof $ explicitly provides key completions for aliases
|
|
63
|
+
[k in keyof cases | keyof ctx["$"]]?: k extends validateTypeRoot<k, ctx["$"]>
|
|
64
|
+
? (
|
|
65
|
+
In: ctx["inConstraint"] & inferTypeRoot<k, ctx["$"]>
|
|
66
|
+
) => ctx["outConstraint"]
|
|
67
|
+
: parseWhentryKey<k & string, ctx["$"]> extends ErrorMessage<infer message>
|
|
68
|
+
? ErrorMessage<message>
|
|
69
|
+
: (In: parseWhentryKey<k & string, ctx["$"]>) => ctx["outConstraint"]
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type MatchParser<$> = {
|
|
73
|
+
<In = unknown, Out = unknown>(): ChainableMatchParser<{
|
|
74
|
+
inConstraint: In
|
|
75
|
+
outConstraint: Out
|
|
76
|
+
thens: []
|
|
77
|
+
$: $
|
|
78
|
+
}>
|
|
79
|
+
} & CaseMatchParser<{
|
|
80
|
+
inConstraint: unknown
|
|
81
|
+
outConstraint: unknown
|
|
82
|
+
thens: []
|
|
83
|
+
$: $
|
|
84
|
+
}>
|
|
85
|
+
|
|
86
|
+
export type CaseMatchParser<ctx extends MatchContext> = <cases>(
|
|
87
|
+
def: conform<cases, validateCases<cases, ctx>>
|
|
88
|
+
) => ChainableMatchParser<
|
|
89
|
+
replaceKey<ctx, "thens", [...ctx["thens"], ...unionToTuple<valueOf<cases>>]>
|
|
90
|
+
>
|
|
91
|
+
|
|
92
|
+
export type WhenMatchParser<ctx extends MatchContext> = <
|
|
93
|
+
def,
|
|
94
|
+
then extends (
|
|
95
|
+
In: ctx["inConstraint"] & inferTypeRoot<def, ctx["$"]>
|
|
96
|
+
) => ctx["outConstraint"]
|
|
97
|
+
>(
|
|
98
|
+
when: validateTypeRoot<def, ctx["$"]>,
|
|
99
|
+
then: then
|
|
100
|
+
) => ChainableMatchParser<replaceKey<ctx, "thens", [...ctx["thens"], then]>>
|
|
101
|
+
|
|
102
|
+
export type MatchInvokation<ctx extends MatchContext> = <
|
|
103
|
+
data extends ctx["inConstraint"]
|
|
104
|
+
>(
|
|
105
|
+
In: data
|
|
106
|
+
) => {
|
|
107
|
+
[i in Extract<keyof ctx["thens"], `${number}`>]: isDisjoint<
|
|
108
|
+
data,
|
|
109
|
+
paramsOf<ctx["thens"][i]>[0]
|
|
110
|
+
> extends true
|
|
111
|
+
? never
|
|
112
|
+
: returnOf<ctx["thens"][i]>
|
|
113
|
+
}[Extract<keyof ctx["thens"], `${number}`>]
|
|
114
|
+
|
|
115
|
+
export type ChainableMatchParser<ctx extends MatchContext> =
|
|
116
|
+
MatchInvokation<ctx> & {
|
|
117
|
+
cases: CaseMatchParser<ctx>
|
|
118
|
+
when: WhenMatchParser<ctx>
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export const createMatchParser = <$>(scope: Scope): MatchParser<$> => {
|
|
122
|
+
// TODO: move to match node, discrimination
|
|
123
|
+
const parser = (cases: Record<string, Morph>) => {
|
|
124
|
+
const caseArray = Object.entries(cases).map(([def, morph]) => ({
|
|
125
|
+
when: new Type(def, scope).allows,
|
|
126
|
+
then: morph
|
|
127
|
+
}))
|
|
128
|
+
return (data: unknown) => {
|
|
129
|
+
for (const c of caseArray) {
|
|
130
|
+
if (c.when(data)) {
|
|
131
|
+
return c.then(data, {} as never)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return parser as never
|
|
137
|
+
}
|
package/package.json
CHANGED
|
@@ -1,109 +1,42 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
"@typescript-eslint/parser": "5.56.0",
|
|
44
|
-
"c8": "7.13.0",
|
|
45
|
-
"eslint": "8.36.0",
|
|
46
|
-
"eslint-config-prettier": "8.8.0",
|
|
47
|
-
"eslint-define-config": "1.17.0",
|
|
48
|
-
"eslint-plugin-only-warn": "1.1.0",
|
|
49
|
-
"eslint-import-resolver-typescript": "3.5.3",
|
|
50
|
-
"eslint-plugin-import": "2.27.5",
|
|
51
|
-
"eslint-plugin-prefer-arrow": "1.2.3",
|
|
52
|
-
"eslint-plugin-unicorn": "46.0.0",
|
|
53
|
-
"husky": "8.0.3",
|
|
54
|
-
"mocha": "10.2.0",
|
|
55
|
-
"prettier": "2.8.6",
|
|
56
|
-
"prettier-plugin-jsdoc": "0.4.2",
|
|
57
|
-
"pretty-quick": "3.1.3",
|
|
58
|
-
"ts-morph": "18.0.0",
|
|
59
|
-
"ts-node": "10.9.1",
|
|
60
|
-
"typescript": "5.0.3"
|
|
61
|
-
},
|
|
62
|
-
"c8": {
|
|
63
|
-
"checkCoverage": true,
|
|
64
|
-
"lines": 96,
|
|
65
|
-
"src": "./src",
|
|
66
|
-
"exclude": [
|
|
67
|
-
"test/**",
|
|
68
|
-
"dev/**"
|
|
69
|
-
],
|
|
70
|
-
"extension": ".ts",
|
|
71
|
-
"reporter": "lcovonly"
|
|
72
|
-
},
|
|
73
|
-
"prettier": {
|
|
74
|
-
"tabWidth": 4,
|
|
75
|
-
"semi": false,
|
|
76
|
-
"trailingComma": "none"
|
|
77
|
-
},
|
|
78
|
-
"mocha": {
|
|
79
|
-
"spec": [
|
|
80
|
-
"./dev/test/*.test.*"
|
|
81
|
-
],
|
|
82
|
-
"ignore": "**/node_modules/*",
|
|
83
|
-
"node-option": [
|
|
84
|
-
"loader=ts-node/esm",
|
|
85
|
-
"no-warnings=ExperimentalWarning"
|
|
86
|
-
]
|
|
87
|
-
},
|
|
88
|
-
"eslintConfig": {
|
|
89
|
-
"extends": "./dev/configs/.eslintrc.cjs"
|
|
90
|
-
},
|
|
91
|
-
"scripts": {
|
|
92
|
-
"prChecks": "pnpm install && pnpm lint && pnpm checkFormat && pnpm testRepo && pnpm buildRepo",
|
|
93
|
-
"build": "pnpm ts ./dev/scripts/build.ts",
|
|
94
|
-
"buildRepo": "pnpm build && pnpm run -r build && pnpm build",
|
|
95
|
-
"test": "pnpm ts ./dev/scripts/test.ts",
|
|
96
|
-
"tnt": "pnpm ts ./dev/scripts/test.ts --skipTypes",
|
|
97
|
-
"testRepo": "pnpm -r test && pnpm testTsVersions",
|
|
98
|
-
"testTsVersions": "pnpm ts ./dev/scripts/testTsVersions.ts",
|
|
99
|
-
"typecheck": "tsc --noEmit",
|
|
100
|
-
"ts": "node --loader=ts-node/esm --no-warnings=ExperimentalWarning",
|
|
101
|
-
"lint": "eslint --max-warnings=0 .",
|
|
102
|
-
"format": "prettier --write --ignore-path ./dev/configs/.prettierignore .",
|
|
103
|
-
"checkFormat": "prettier --check --ignore-path ./dev/configs/.prettierignore .",
|
|
104
|
-
"findUnused": "pnpm ts ./dev/scripts/findUnusedExports.ts",
|
|
105
|
-
"changeset": "pnpm ts ./dev/scripts/changeset.ts",
|
|
106
|
-
"ci:version": "pnpm ts ./dev/scripts/updateVersions.ts",
|
|
107
|
-
"ci:publish": "pnpm ts ./dev/scripts/changeset.ts publish"
|
|
108
|
-
}
|
|
109
|
-
}
|
|
2
|
+
"name": "arktype",
|
|
3
|
+
"description": "TypeScript's 1:1 validator, optimized from editor to runtime",
|
|
4
|
+
"version": "1.1.0-dev-2",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "David Blass",
|
|
8
|
+
"email": "david@arktype.io",
|
|
9
|
+
"url": "https://arktype.io"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/arktypeio/arktype.git"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"arktype-repo": "./main.js",
|
|
19
|
+
"types": "./dist/main.d.ts",
|
|
20
|
+
"default": "./dist/main.js"
|
|
21
|
+
},
|
|
22
|
+
"./internal/*": {
|
|
23
|
+
"arktype-repo": "./*",
|
|
24
|
+
"types": "./dist/*",
|
|
25
|
+
"default": "./dist/*"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"**/*.ts",
|
|
31
|
+
"!**/*.tsBuildInfo",
|
|
32
|
+
"!__tests__"
|
|
33
|
+
],
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsc --build ./tsconfig.build.json",
|
|
36
|
+
"test": "ts ../repo/testPackage.ts"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@arktype/util": "0.0.5",
|
|
40
|
+
"@arktype/schema": "0.0.2"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { isNode, node, type Root } from "@arktype/schema"
|
|
2
|
+
import {
|
|
3
|
+
isThunk,
|
|
4
|
+
objectKindOf,
|
|
5
|
+
stringify,
|
|
6
|
+
throwParseError,
|
|
7
|
+
type defined,
|
|
8
|
+
type Dict,
|
|
9
|
+
type equals,
|
|
10
|
+
type ErrorMessage,
|
|
11
|
+
type evaluate,
|
|
12
|
+
type isAny,
|
|
13
|
+
type isUnknown,
|
|
14
|
+
type List,
|
|
15
|
+
type objectKindOrDomainOf,
|
|
16
|
+
type optionalKeyOf,
|
|
17
|
+
type Primitive,
|
|
18
|
+
type requiredKeyOf
|
|
19
|
+
} from "@arktype/util"
|
|
20
|
+
import type { ParseContext } from "../scope.js"
|
|
21
|
+
import type { type } from "../scopes/ark.js"
|
|
22
|
+
import { Type } from "../type.js"
|
|
23
|
+
import {
|
|
24
|
+
parseObjectLiteral,
|
|
25
|
+
type inferObjectLiteral,
|
|
26
|
+
type validateObjectLiteral
|
|
27
|
+
} from "./objectLiteral.js"
|
|
28
|
+
import type { validateString } from "./semantic/validate.js"
|
|
29
|
+
import type { BaseCompletions, inferString } from "./string/string.js"
|
|
30
|
+
import {
|
|
31
|
+
parseTuple,
|
|
32
|
+
type inferTuple,
|
|
33
|
+
type TupleExpression,
|
|
34
|
+
type validateTuple
|
|
35
|
+
} from "./tuple.js"
|
|
36
|
+
|
|
37
|
+
export const parseObject = (def: object, ctx: ParseContext): Root => {
|
|
38
|
+
const objectKind = objectKindOf(def)
|
|
39
|
+
switch (objectKind) {
|
|
40
|
+
case undefined:
|
|
41
|
+
if (isNode(def)) {
|
|
42
|
+
return def as Root
|
|
43
|
+
}
|
|
44
|
+
return parseObjectLiteral(def as Dict, ctx)
|
|
45
|
+
case "Array":
|
|
46
|
+
return parseTuple(def as List, ctx)
|
|
47
|
+
case "RegExp":
|
|
48
|
+
return node({ basis: "string", pattern: def as RegExp })
|
|
49
|
+
case "Function":
|
|
50
|
+
const resolvedDef = isThunk(def) ? def() : def
|
|
51
|
+
if (resolvedDef instanceof Type) {
|
|
52
|
+
return resolvedDef.root
|
|
53
|
+
}
|
|
54
|
+
return throwParseError(writeBadDefinitionTypeMessage("Function"))
|
|
55
|
+
default:
|
|
56
|
+
return throwParseError(
|
|
57
|
+
writeBadDefinitionTypeMessage(objectKind ?? stringify(def))
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export type inferDefinition<def, $, args> = isAny<def> extends true
|
|
63
|
+
? never
|
|
64
|
+
: def extends type.cast<infer t> | ThunkCast<infer t>
|
|
65
|
+
? t
|
|
66
|
+
: def extends string
|
|
67
|
+
? inferString<def, $, args>
|
|
68
|
+
: def extends readonly unknown[]
|
|
69
|
+
? inferTuple<def, $, args>
|
|
70
|
+
: def extends RegExp
|
|
71
|
+
? string
|
|
72
|
+
: def extends object
|
|
73
|
+
? inferObjectLiteral<def, $, args>
|
|
74
|
+
: never
|
|
75
|
+
|
|
76
|
+
export type validateDefinition<def, $, args> = null extends undefined
|
|
77
|
+
? ErrorMessage<`'strict' or 'strictNullChecks' must be set to true in your tsconfig's 'compilerOptions'`>
|
|
78
|
+
: [def] extends [Terminal]
|
|
79
|
+
? unknown extends def
|
|
80
|
+
? // if def is any, never is the only way we can make validation fail
|
|
81
|
+
// since any would be assignable to a standard error message
|
|
82
|
+
never
|
|
83
|
+
: def
|
|
84
|
+
: def extends string
|
|
85
|
+
? validateString<def, $, args>
|
|
86
|
+
: def extends readonly unknown[]
|
|
87
|
+
? validateTuple<def, $, args>
|
|
88
|
+
: def extends BadDefinitionType
|
|
89
|
+
? writeBadDefinitionTypeMessage<objectKindOrDomainOf<def>>
|
|
90
|
+
: isUnknown<def> extends true
|
|
91
|
+
? // this allows the initial list of autocompletions to be populated when a user writes "type()",
|
|
92
|
+
// before having specified a definition
|
|
93
|
+
BaseCompletions<$, args> | {}
|
|
94
|
+
: validateObjectLiteral<def, $, args>
|
|
95
|
+
|
|
96
|
+
export type validateDeclared<declared, def, $, args> =
|
|
97
|
+
def extends validateDefinition<def, $, args>
|
|
98
|
+
? validateInference<def, declared, $, args>
|
|
99
|
+
: validateDefinition<def, $, args>
|
|
100
|
+
|
|
101
|
+
type validateInference<def, declared, $, args> = def extends
|
|
102
|
+
| RegExp
|
|
103
|
+
| type.cast<unknown>
|
|
104
|
+
| ThunkCast
|
|
105
|
+
| TupleExpression
|
|
106
|
+
? validateShallowInference<def, declared, $, args>
|
|
107
|
+
: def extends readonly unknown[]
|
|
108
|
+
? declared extends readonly unknown[]
|
|
109
|
+
? {
|
|
110
|
+
[i in keyof declared]: i extends keyof def
|
|
111
|
+
? validateInference<def[i], declared[i], $, args>
|
|
112
|
+
: unknown
|
|
113
|
+
}
|
|
114
|
+
: evaluate<declarationMismatch<def, declared, $, args>>
|
|
115
|
+
: def extends object
|
|
116
|
+
? evaluate<
|
|
117
|
+
{
|
|
118
|
+
[k in requiredKeyOf<declared>]: k extends keyof def
|
|
119
|
+
? validateInference<def[k], declared[k], $, args>
|
|
120
|
+
: unknown
|
|
121
|
+
} & {
|
|
122
|
+
[k in optionalKeyOf<declared> &
|
|
123
|
+
string as `${k}?`]: `${k}?` extends keyof def
|
|
124
|
+
? validateInference<def[`${k}?`], defined<declared[k]>, $, args>
|
|
125
|
+
: unknown
|
|
126
|
+
}
|
|
127
|
+
>
|
|
128
|
+
: validateShallowInference<def, declared, $, args>
|
|
129
|
+
|
|
130
|
+
type validateShallowInference<def, declared, $, args> = equals<
|
|
131
|
+
inferDefinition<def, $, args>,
|
|
132
|
+
declared
|
|
133
|
+
> extends true
|
|
134
|
+
? def
|
|
135
|
+
: evaluate<declarationMismatch<def, declared, $, args>>
|
|
136
|
+
|
|
137
|
+
type declarationMismatch<def, declared, $, args> = {
|
|
138
|
+
declared: declared
|
|
139
|
+
inferred: inferDefinition<def, $, args>
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// functions are ignored in validation so that cyclic thunk definitions can be
|
|
143
|
+
// inferred in scopes
|
|
144
|
+
type Terminal = RegExp | type.cast<unknown> | ((...args: never[]) => unknown)
|
|
145
|
+
|
|
146
|
+
export type ThunkCast<t = unknown> = () => type.cast<t>
|
|
147
|
+
|
|
148
|
+
type BadDefinitionType = Exclude<Primitive, string>
|
|
149
|
+
|
|
150
|
+
export const writeBadDefinitionTypeMessage = <actual extends string>(
|
|
151
|
+
actual: actual
|
|
152
|
+
): writeBadDefinitionTypeMessage<actual> =>
|
|
153
|
+
`Type definitions must be strings or objects (was ${actual})`
|
|
154
|
+
|
|
155
|
+
type writeBadDefinitionTypeMessage<actual extends string> =
|
|
156
|
+
`Type definitions must be strings or objects (was ${actual})`
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import type { Root } from "@arktype/schema"
|
|
2
|
+
import {
|
|
3
|
+
throwParseError,
|
|
4
|
+
type ErrorMessage,
|
|
5
|
+
type join,
|
|
6
|
+
type nominal
|
|
7
|
+
} from "@arktype/util"
|
|
8
|
+
import type { ParseContext } from "../scope.js"
|
|
9
|
+
import { DynamicState } from "./string/reduce/dynamic.js"
|
|
10
|
+
import { writeUnclosedGroupMessage } from "./string/reduce/shared.js"
|
|
11
|
+
import type { StaticState, state } from "./string/reduce/static.js"
|
|
12
|
+
import { writeUnexpectedCharacterMessage } from "./string/shift/operator/operator.js"
|
|
13
|
+
import { Scanner } from "./string/shift/scanner.js"
|
|
14
|
+
import { parseUntilFinalizer } from "./string/string.js"
|
|
15
|
+
|
|
16
|
+
export type GenericDeclaration<
|
|
17
|
+
name extends string = string,
|
|
18
|
+
params extends string = string
|
|
19
|
+
> = `${name}<${params}>`
|
|
20
|
+
|
|
21
|
+
// we put the error in a tuple so that parseGenericParams always returns a string[]
|
|
22
|
+
export type GenericParamsParseError<message extends string = string> = [
|
|
23
|
+
nominal<message, "InvalidGenericParameters">
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
export const parseGenericParams = (def: string) =>
|
|
27
|
+
parseGenericParamsRecurse(new Scanner(def))
|
|
28
|
+
|
|
29
|
+
export type parseGenericParams<def extends string> = parseParamsRecurse<
|
|
30
|
+
def,
|
|
31
|
+
"",
|
|
32
|
+
[]
|
|
33
|
+
> extends infer result extends string[]
|
|
34
|
+
? "" extends result[number]
|
|
35
|
+
? GenericParamsParseError<emptyGenericParameterMessage>
|
|
36
|
+
: result
|
|
37
|
+
: never
|
|
38
|
+
|
|
39
|
+
export const emptyGenericParameterMessage = `An empty string is not a valid generic parameter name`
|
|
40
|
+
|
|
41
|
+
export type emptyGenericParameterMessage = typeof emptyGenericParameterMessage
|
|
42
|
+
|
|
43
|
+
const parseGenericParamsRecurse = (scanner: Scanner): string[] => {
|
|
44
|
+
const param = scanner.shiftUntilNextTerminator()
|
|
45
|
+
if (param === "") {
|
|
46
|
+
throwParseError(emptyGenericParameterMessage)
|
|
47
|
+
}
|
|
48
|
+
scanner.shiftUntilNonWhitespace()
|
|
49
|
+
const nextNonWhitespace = scanner.shift()
|
|
50
|
+
return nextNonWhitespace === ""
|
|
51
|
+
? [param]
|
|
52
|
+
: nextNonWhitespace === ","
|
|
53
|
+
? [param, ...parseGenericParamsRecurse(scanner)]
|
|
54
|
+
: throwParseError(writeUnexpectedCharacterMessage(nextNonWhitespace, ","))
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
type parseParamsRecurse<
|
|
58
|
+
unscanned extends string,
|
|
59
|
+
param extends string,
|
|
60
|
+
result extends string[]
|
|
61
|
+
> = unscanned extends `${infer lookahead}${infer nextUnscanned}`
|
|
62
|
+
? lookahead extends ","
|
|
63
|
+
? parseParamsRecurse<nextUnscanned, "", [...result, param]>
|
|
64
|
+
: lookahead extends Scanner.WhiteSpaceToken
|
|
65
|
+
? param extends ""
|
|
66
|
+
? // if the next char is whitespace and we aren't in the middle of a param, skip to the next one
|
|
67
|
+
parseParamsRecurse<Scanner.skipWhitespace<nextUnscanned>, "", result>
|
|
68
|
+
: Scanner.skipWhitespace<nextUnscanned> extends `${infer nextNonWhitespace}${infer rest}`
|
|
69
|
+
? nextNonWhitespace extends ","
|
|
70
|
+
? parseParamsRecurse<rest, "", [...result, param]>
|
|
71
|
+
: GenericParamsParseError<
|
|
72
|
+
writeUnexpectedCharacterMessage<nextNonWhitespace, ",">
|
|
73
|
+
>
|
|
74
|
+
: // params end with a single whitespace character, add the current token
|
|
75
|
+
[...result, param]
|
|
76
|
+
: parseParamsRecurse<nextUnscanned, `${param}${lookahead}`, result>
|
|
77
|
+
: param extends ""
|
|
78
|
+
? result
|
|
79
|
+
: [...result, param]
|
|
80
|
+
|
|
81
|
+
export type ParsedArgs<
|
|
82
|
+
result extends unknown[] = unknown[],
|
|
83
|
+
unscanned extends string = string
|
|
84
|
+
> = {
|
|
85
|
+
result: result
|
|
86
|
+
unscanned: unscanned
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export const parseGenericArgs = (
|
|
90
|
+
name: string,
|
|
91
|
+
params: string[],
|
|
92
|
+
unscanned: string,
|
|
93
|
+
ctx: ParseContext
|
|
94
|
+
) => parseGenericArgsRecurse(name, params, unscanned, ctx, [], [])
|
|
95
|
+
|
|
96
|
+
export type parseGenericArgs<
|
|
97
|
+
name extends string,
|
|
98
|
+
params extends string[],
|
|
99
|
+
unscanned extends string,
|
|
100
|
+
$,
|
|
101
|
+
args
|
|
102
|
+
> = parseGenericArgsRecurse<name, params, unscanned, $, args, [], []>
|
|
103
|
+
|
|
104
|
+
const parseGenericArgsRecurse = (
|
|
105
|
+
name: string,
|
|
106
|
+
params: string[],
|
|
107
|
+
unscanned: string,
|
|
108
|
+
ctx: ParseContext,
|
|
109
|
+
argDefs: string[],
|
|
110
|
+
argNodes: Root[]
|
|
111
|
+
): ParsedArgs<Root[]> => {
|
|
112
|
+
const s = parseUntilFinalizer(new DynamicState(unscanned, ctx))
|
|
113
|
+
// remove the finalizing token from the argDef
|
|
114
|
+
argDefs.push(s.scanner.scanned.slice(0, -1))
|
|
115
|
+
argNodes.push(s.root)
|
|
116
|
+
const nextUnscanned = s.scanner.unscanned
|
|
117
|
+
if (s.finalizer === ">") {
|
|
118
|
+
if (argNodes.length === params.length) {
|
|
119
|
+
return {
|
|
120
|
+
result: argNodes,
|
|
121
|
+
unscanned: nextUnscanned
|
|
122
|
+
}
|
|
123
|
+
} else {
|
|
124
|
+
return s.error(writeInvalidGenericArgsMessage(name, params, argDefs))
|
|
125
|
+
}
|
|
126
|
+
} else if (s.finalizer === ",") {
|
|
127
|
+
return parseGenericArgsRecurse(
|
|
128
|
+
name,
|
|
129
|
+
params,
|
|
130
|
+
nextUnscanned,
|
|
131
|
+
ctx,
|
|
132
|
+
argDefs,
|
|
133
|
+
argNodes
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
return s.error(writeUnclosedGroupMessage(">"))
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
type parseGenericArgsRecurse<
|
|
140
|
+
name extends string,
|
|
141
|
+
params extends string[],
|
|
142
|
+
unscanned extends string,
|
|
143
|
+
$,
|
|
144
|
+
args,
|
|
145
|
+
argDefs extends string[],
|
|
146
|
+
argAsts extends unknown[]
|
|
147
|
+
> = parseUntilFinalizer<
|
|
148
|
+
state.initialize<unscanned>,
|
|
149
|
+
$,
|
|
150
|
+
args
|
|
151
|
+
> extends infer finalArgState extends StaticState
|
|
152
|
+
? {
|
|
153
|
+
defs: [
|
|
154
|
+
...argDefs,
|
|
155
|
+
finalArgState["scanned"] extends `${infer def}${"," | ">"}`
|
|
156
|
+
? def
|
|
157
|
+
: finalArgState["scanned"]
|
|
158
|
+
]
|
|
159
|
+
asts: [...argAsts, finalArgState["root"]]
|
|
160
|
+
unscanned: finalArgState["unscanned"]
|
|
161
|
+
} extends {
|
|
162
|
+
defs: infer nextDefs extends string[]
|
|
163
|
+
asts: infer nextAsts extends unknown[]
|
|
164
|
+
unscanned: infer nextUnscanned extends string
|
|
165
|
+
}
|
|
166
|
+
? finalArgState["finalizer"] extends ">"
|
|
167
|
+
? nextAsts["length"] extends params["length"]
|
|
168
|
+
? ParsedArgs<nextAsts, nextUnscanned>
|
|
169
|
+
: state.error<writeInvalidGenericArgsMessage<name, params, nextDefs>>
|
|
170
|
+
: finalArgState["finalizer"] extends ","
|
|
171
|
+
? parseGenericArgsRecurse<
|
|
172
|
+
name,
|
|
173
|
+
params,
|
|
174
|
+
nextUnscanned,
|
|
175
|
+
$,
|
|
176
|
+
args,
|
|
177
|
+
nextDefs,
|
|
178
|
+
nextAsts
|
|
179
|
+
>
|
|
180
|
+
: finalArgState["finalizer"] extends ErrorMessage
|
|
181
|
+
? finalArgState
|
|
182
|
+
: state.error<writeUnclosedGroupMessage<">">>
|
|
183
|
+
: never
|
|
184
|
+
: never
|
|
185
|
+
|
|
186
|
+
export const writeInvalidGenericArgsMessage = <
|
|
187
|
+
name extends string,
|
|
188
|
+
params extends string[],
|
|
189
|
+
argDefs extends string[]
|
|
190
|
+
>(
|
|
191
|
+
name: name,
|
|
192
|
+
params: params,
|
|
193
|
+
argDefs: argDefs
|
|
194
|
+
) =>
|
|
195
|
+
`${name}<${params.join(", ")}> requires exactly ${params.length} args (got ${
|
|
196
|
+
argDefs.length
|
|
197
|
+
}${argDefs.length === 0 ? "" : ": " + argDefs.join(", ")})`
|
|
198
|
+
|
|
199
|
+
export type writeInvalidGenericArgsMessage<
|
|
200
|
+
name extends string,
|
|
201
|
+
params extends string[],
|
|
202
|
+
argDefs extends string[]
|
|
203
|
+
> = `${name}<${join<
|
|
204
|
+
params,
|
|
205
|
+
", "
|
|
206
|
+
>}> requires exactly ${params["length"]} args (got ${argDefs["length"]}${argDefs["length"] extends 0
|
|
207
|
+
? ""
|
|
208
|
+
: `: ${join<argDefs, ",">}`})`
|