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/scope.ts
ADDED
|
@@ -0,0 +1,573 @@
|
|
|
1
|
+
import { builtins, type ProblemCode, type Root } from "@arktype/schema"
|
|
2
|
+
import {
|
|
3
|
+
domainOf,
|
|
4
|
+
hasDomain,
|
|
5
|
+
isThunk,
|
|
6
|
+
throwParseError,
|
|
7
|
+
transform,
|
|
8
|
+
type Dict,
|
|
9
|
+
type evaluate,
|
|
10
|
+
type isAny,
|
|
11
|
+
type nominal
|
|
12
|
+
} from "@arktype/util"
|
|
13
|
+
import {
|
|
14
|
+
createMatchParser,
|
|
15
|
+
createWhenParser,
|
|
16
|
+
type MatchParser,
|
|
17
|
+
type WhenParser
|
|
18
|
+
} from "./match.js"
|
|
19
|
+
import {
|
|
20
|
+
parseObject,
|
|
21
|
+
writeBadDefinitionTypeMessage,
|
|
22
|
+
type inferDefinition,
|
|
23
|
+
type validateDefinition
|
|
24
|
+
} from "./parser/definition.js"
|
|
25
|
+
import {
|
|
26
|
+
parseGenericParams,
|
|
27
|
+
type GenericDeclaration,
|
|
28
|
+
type GenericParamsParseError
|
|
29
|
+
} from "./parser/generic.js"
|
|
30
|
+
import {
|
|
31
|
+
writeMissingSubmoduleAccessMessage,
|
|
32
|
+
writeNonSubmoduleDotMessage,
|
|
33
|
+
writeUnresolvableMessage
|
|
34
|
+
} from "./parser/string/shift/operand/unenclosed.js"
|
|
35
|
+
import { parseString } from "./parser/string/string.js"
|
|
36
|
+
import type { type } from "./scopes/ark.js"
|
|
37
|
+
import {
|
|
38
|
+
addArkKind,
|
|
39
|
+
createTypeParser,
|
|
40
|
+
generic,
|
|
41
|
+
hasArkKind,
|
|
42
|
+
Type,
|
|
43
|
+
validateUninstantiatedGeneric,
|
|
44
|
+
type arkKind,
|
|
45
|
+
type DeclarationParser,
|
|
46
|
+
type DefinitionParser,
|
|
47
|
+
type extractIn,
|
|
48
|
+
type extractOut,
|
|
49
|
+
type Generic,
|
|
50
|
+
type GenericProps,
|
|
51
|
+
type KeyCheckKind,
|
|
52
|
+
type TypeConfig,
|
|
53
|
+
type TypeParser
|
|
54
|
+
} from "./type.js"
|
|
55
|
+
|
|
56
|
+
export type ScopeParser<parent, ambient> = {
|
|
57
|
+
<const def>(def: validateScope<def, parent & ambient>): Scope<{
|
|
58
|
+
exports: inferBootstrapped<{
|
|
59
|
+
exports: bootstrapExports<def>
|
|
60
|
+
locals: bootstrapLocals<def> & parent
|
|
61
|
+
ambient: ambient
|
|
62
|
+
}>
|
|
63
|
+
locals: inferBootstrapped<{
|
|
64
|
+
exports: bootstrapLocals<def>
|
|
65
|
+
locals: bootstrapExports<def> & parent
|
|
66
|
+
ambient: ambient
|
|
67
|
+
}>
|
|
68
|
+
ambient: ambient
|
|
69
|
+
}>
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type ScopeConfig = {
|
|
73
|
+
ambient?: Scope | null
|
|
74
|
+
codes?: Record<ProblemCode, { mustBe?: string }>
|
|
75
|
+
keys?: KeyCheckKind
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
type validateScope<def, $> = {
|
|
79
|
+
[k in keyof def]: parseScopeKey<k>["params"] extends []
|
|
80
|
+
? // Not including Type here directly breaks inference
|
|
81
|
+
def[k] extends Type | PreparsedResolution
|
|
82
|
+
? def[k]
|
|
83
|
+
: validateDefinition<def[k], $ & bootstrap<def>, {}>
|
|
84
|
+
: parseScopeKey<k>["params"] extends GenericParamsParseError
|
|
85
|
+
? // use the full nominal type here to avoid an overlap between the
|
|
86
|
+
// error message and a possible value for the property
|
|
87
|
+
parseScopeKey<k>["params"][0]
|
|
88
|
+
: validateDefinition<
|
|
89
|
+
def[k],
|
|
90
|
+
$ & bootstrap<def>,
|
|
91
|
+
{
|
|
92
|
+
// once we support constraints on generic parameters, we'd use
|
|
93
|
+
// the base type here: https://github.com/arktypeio/arktype/issues/796
|
|
94
|
+
[param in parseScopeKey<k>["params"][number]]: unknown
|
|
95
|
+
}
|
|
96
|
+
>
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export type bindThis<def> = { this: Def<def> }
|
|
100
|
+
|
|
101
|
+
export const bindThis = () => ({
|
|
102
|
+
// TODO: fix
|
|
103
|
+
this: builtins.unknown
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
/** nominal type for an unparsed definition used during scope bootstrapping */
|
|
107
|
+
type Def<def = {}> = nominal<def, "unparsed">
|
|
108
|
+
|
|
109
|
+
/** sentinel indicating a scope that will be associated with a generic has not yet been parsed */
|
|
110
|
+
export type UnparsedScope = "$"
|
|
111
|
+
|
|
112
|
+
type bootstrap<def> = bootstrapLocals<def> & bootstrapExports<def>
|
|
113
|
+
|
|
114
|
+
type bootstrapLocals<def> = bootstrapAliases<{
|
|
115
|
+
// intersection seems redundant but it is more efficient for TS to avoid
|
|
116
|
+
// mapping all the keys
|
|
117
|
+
[k in keyof def & PrivateDeclaration as extractPrivateKey<k>]: def[k]
|
|
118
|
+
}>
|
|
119
|
+
|
|
120
|
+
type bootstrapExports<def> = bootstrapAliases<{
|
|
121
|
+
[k in Exclude<keyof def, PrivateDeclaration>]: def[k]
|
|
122
|
+
}>
|
|
123
|
+
|
|
124
|
+
/** These are legal as values of a scope but not as definitions in other contexts */
|
|
125
|
+
type PreparsedResolution = Module | GenericProps
|
|
126
|
+
|
|
127
|
+
type bootstrapAliases<def> = {
|
|
128
|
+
[k in Exclude<
|
|
129
|
+
keyof def,
|
|
130
|
+
// avoid inferring nominal symbols, e.g. arkKind from Module
|
|
131
|
+
GenericDeclaration | symbol
|
|
132
|
+
>]: def[k] extends PreparsedResolution
|
|
133
|
+
? def[k]
|
|
134
|
+
: def[k] extends (() => infer thunkReturn extends PreparsedResolution)
|
|
135
|
+
? thunkReturn
|
|
136
|
+
: Def<def[k]>
|
|
137
|
+
} & {
|
|
138
|
+
[k in keyof def & GenericDeclaration as extractGenericName<k>]: Generic<
|
|
139
|
+
parseGenericParams<extractGenericParameters<k>>,
|
|
140
|
+
def[k],
|
|
141
|
+
UnparsedScope
|
|
142
|
+
>
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
type inferBootstrapped<r extends Resolutions> = evaluate<{
|
|
146
|
+
[name in keyof r["exports"]]: r["exports"][name] extends Def<infer def>
|
|
147
|
+
? inferDefinition<def, $<r>, {}>
|
|
148
|
+
: r["exports"][name] extends GenericProps<infer params, infer def>
|
|
149
|
+
? // add the scope in which the generic was defined here
|
|
150
|
+
Generic<params, def, $<r>>
|
|
151
|
+
: // otherwise should be a submodule
|
|
152
|
+
r["exports"][name]
|
|
153
|
+
}>
|
|
154
|
+
|
|
155
|
+
type extractGenericName<k> = k extends GenericDeclaration<infer name>
|
|
156
|
+
? name
|
|
157
|
+
: never
|
|
158
|
+
|
|
159
|
+
type extractGenericParameters<k> = k extends GenericDeclaration<
|
|
160
|
+
string,
|
|
161
|
+
infer params
|
|
162
|
+
>
|
|
163
|
+
? params
|
|
164
|
+
: never
|
|
165
|
+
|
|
166
|
+
type extractPrivateKey<k> = k extends PrivateDeclaration<infer key>
|
|
167
|
+
? key
|
|
168
|
+
: never
|
|
169
|
+
|
|
170
|
+
type PrivateDeclaration<key extends string = string> = `#${key}`
|
|
171
|
+
|
|
172
|
+
export type resolve<reference extends keyof $ | keyof args, $, args> = (
|
|
173
|
+
reference extends keyof args ? args[reference] : $[reference & keyof $]
|
|
174
|
+
) extends infer resolution
|
|
175
|
+
? [resolution] extends [never]
|
|
176
|
+
? never
|
|
177
|
+
: isAny<resolution> extends true
|
|
178
|
+
? any
|
|
179
|
+
: resolution extends Def<infer def>
|
|
180
|
+
? inferDefinition<def, $, args>
|
|
181
|
+
: resolution
|
|
182
|
+
: never
|
|
183
|
+
|
|
184
|
+
export type moduleKeyOf<$> = {
|
|
185
|
+
[k in keyof $]: $[k] extends Module ? k & string : never
|
|
186
|
+
}[keyof $]
|
|
187
|
+
|
|
188
|
+
export type tryInferSubmoduleReference<$, token> =
|
|
189
|
+
token extends `${infer submodule extends moduleKeyOf<$>}.${infer subalias}`
|
|
190
|
+
? subalias extends keyof $[submodule]
|
|
191
|
+
? $[submodule][subalias] extends Type<infer t>
|
|
192
|
+
? t
|
|
193
|
+
: never
|
|
194
|
+
: never
|
|
195
|
+
: never
|
|
196
|
+
|
|
197
|
+
type $<r extends Resolutions> = r["exports"] & r["locals"] & r["ambient"]
|
|
198
|
+
|
|
199
|
+
type exportedName<r extends Resolutions> = keyof r["exports"] & string
|
|
200
|
+
|
|
201
|
+
export type Module<r extends Resolutions = any> = {
|
|
202
|
+
// just adding the nominal id this way and mapping it is cheaper than an intersection
|
|
203
|
+
[k in exportedName<r> | arkKind]: k extends string
|
|
204
|
+
? [r["exports"][k]] extends [never]
|
|
205
|
+
? Type<never, $<r>>
|
|
206
|
+
: isAny<r["exports"][k]> extends true
|
|
207
|
+
? Type<any, $<r>>
|
|
208
|
+
: r["exports"][k] extends PreparsedResolution
|
|
209
|
+
? r["exports"][k]
|
|
210
|
+
: Type<r["exports"][k], $<r>>
|
|
211
|
+
: // set the nominal symbol's value to something validation won't care about
|
|
212
|
+
// since the inferred type will be omitted anyways
|
|
213
|
+
type.cast<"module">
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export type Resolutions = {
|
|
217
|
+
exports: unknown
|
|
218
|
+
locals: unknown
|
|
219
|
+
ambient: unknown
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export type ParseContext = {
|
|
223
|
+
baseName: string
|
|
224
|
+
path: string[]
|
|
225
|
+
scope: Scope
|
|
226
|
+
args: Record<string, Root> | undefined
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
type MergedResolutions = Record<string, Root | Generic>
|
|
230
|
+
|
|
231
|
+
type ParseContextInput = Pick<ParseContext, "baseName" | "args">
|
|
232
|
+
|
|
233
|
+
export class Scope<r extends Resolutions = any> {
|
|
234
|
+
declare infer: extractOut<r["exports"]>
|
|
235
|
+
declare inferIn: extractIn<r["exports"]>
|
|
236
|
+
|
|
237
|
+
config: TypeConfig
|
|
238
|
+
|
|
239
|
+
private parseCache: Record<string, Root> = {}
|
|
240
|
+
private resolutions: MergedResolutions
|
|
241
|
+
|
|
242
|
+
/** The set of names defined at the root-level of the scope mapped to their
|
|
243
|
+
* corresponding definitions.**/
|
|
244
|
+
aliases: Record<string, unknown> = {}
|
|
245
|
+
private exportedNames: exportedName<r>[] = []
|
|
246
|
+
private ambient: Scope | null
|
|
247
|
+
private references: Root[] = []
|
|
248
|
+
|
|
249
|
+
constructor(def: Dict, config: ScopeConfig) {
|
|
250
|
+
for (const k in def) {
|
|
251
|
+
const parsedKey = parseScopeKey(k)
|
|
252
|
+
this.aliases[parsedKey.name] = parsedKey.params.length
|
|
253
|
+
? generic(parsedKey.params, def[k], this)
|
|
254
|
+
: def[k]
|
|
255
|
+
if (!parsedKey.isLocal) {
|
|
256
|
+
this.exportedNames.push(parsedKey.name as never)
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
this.ambient = config.ambient ?? null
|
|
260
|
+
if (this.ambient) {
|
|
261
|
+
// ensure exportedResolutions is populated
|
|
262
|
+
this.ambient.export()
|
|
263
|
+
this.resolutions = { ...this.ambient.exportedResolutions! }
|
|
264
|
+
} else {
|
|
265
|
+
this.resolutions = {}
|
|
266
|
+
}
|
|
267
|
+
this.config = config
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
static root: ScopeParser<{}, {}> = (aliases) => {
|
|
271
|
+
return new Scope(aliases, {}) as never
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
type: TypeParser<$<r>> = createTypeParser(this as never) as never
|
|
275
|
+
|
|
276
|
+
match: MatchParser<$<r>> = createMatchParser(this as never) as never
|
|
277
|
+
|
|
278
|
+
when: WhenParser<$<r>> = createWhenParser(this as never) as never
|
|
279
|
+
|
|
280
|
+
// TODO: decide if this API will be used for non-validated types
|
|
281
|
+
declare: DeclarationParser<$<r>> = () => ({ type: this.type }) as never
|
|
282
|
+
|
|
283
|
+
scope: ScopeParser<r["exports"], r["ambient"]> = ((
|
|
284
|
+
def: Dict,
|
|
285
|
+
config: TypeConfig = {}
|
|
286
|
+
) => {
|
|
287
|
+
return new Scope(def, {
|
|
288
|
+
ambient: this.ambient,
|
|
289
|
+
...this.config,
|
|
290
|
+
...config
|
|
291
|
+
})
|
|
292
|
+
}) as never
|
|
293
|
+
|
|
294
|
+
define: DefinitionParser<$<r>> = (def) => def as never
|
|
295
|
+
|
|
296
|
+
toAmbient(): Scope<{
|
|
297
|
+
exports: r["exports"]
|
|
298
|
+
locals: r["locals"]
|
|
299
|
+
ambient: r["exports"]
|
|
300
|
+
}> {
|
|
301
|
+
// TODO: private?
|
|
302
|
+
return new Scope(this.aliases, {
|
|
303
|
+
ambient: this,
|
|
304
|
+
...this.config
|
|
305
|
+
})
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// TODO: name?
|
|
309
|
+
get<name extends keyof r["exports"] & string>(
|
|
310
|
+
name: name
|
|
311
|
+
): Type<r["exports"][name], $<r>> {
|
|
312
|
+
return this.export()[name] as never
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
private createRootContext(input: ParseContextInput): ParseContext {
|
|
316
|
+
return {
|
|
317
|
+
path: [],
|
|
318
|
+
scope: this,
|
|
319
|
+
...input
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
parseDefinition(def: unknown, input: ParseContextInput) {
|
|
324
|
+
return this.parse(def, this.createRootContext(input))
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
parse(def: unknown, ctx: ParseContext): Root {
|
|
328
|
+
if (typeof def === "string") {
|
|
329
|
+
if (ctx.args !== undefined) {
|
|
330
|
+
// we can only rely on the cache if there are no contextual
|
|
331
|
+
// resolutions like "this" or generic args
|
|
332
|
+
return parseString(def, ctx)
|
|
333
|
+
}
|
|
334
|
+
if (!this.parseCache[def]) {
|
|
335
|
+
this.parseCache[def] = parseString(def, ctx)
|
|
336
|
+
}
|
|
337
|
+
return this.parseCache[def]
|
|
338
|
+
}
|
|
339
|
+
return hasDomain(def, "object")
|
|
340
|
+
? parseObject(def, ctx)
|
|
341
|
+
: throwParseError(writeBadDefinitionTypeMessage(domainOf(def)))
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
maybeResolve(name: string): Root | Generic | undefined {
|
|
345
|
+
const cached = this.resolutions[name]
|
|
346
|
+
if (cached) {
|
|
347
|
+
return cached
|
|
348
|
+
}
|
|
349
|
+
let def = this.aliases[name]
|
|
350
|
+
if (!def) {
|
|
351
|
+
return this.maybeResolveSubalias(name)
|
|
352
|
+
}
|
|
353
|
+
if (isThunk(def) && !hasArkKind(def, "generic")) {
|
|
354
|
+
def = def()
|
|
355
|
+
}
|
|
356
|
+
// TODO: initialize here?
|
|
357
|
+
const resolution = hasArkKind(def, "generic")
|
|
358
|
+
? validateUninstantiatedGeneric(def)
|
|
359
|
+
: hasArkKind(def, "module")
|
|
360
|
+
? throwParseError(writeMissingSubmoduleAccessMessage(name))
|
|
361
|
+
: this.parseDefinition(
|
|
362
|
+
def,
|
|
363
|
+
this.createRootContext({ baseName: name, args: {} })
|
|
364
|
+
)
|
|
365
|
+
this.resolutions[name] = resolution
|
|
366
|
+
return resolution
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/** If name is a valid reference to a submodule alias, return its resolution */
|
|
370
|
+
private maybeResolveSubalias(name: string) {
|
|
371
|
+
const dotIndex = name.indexOf(".")
|
|
372
|
+
if (dotIndex === -1) {
|
|
373
|
+
return
|
|
374
|
+
}
|
|
375
|
+
const dotPrefix = name.slice(0, dotIndex)
|
|
376
|
+
const prefixDef = this.aliases[dotPrefix]
|
|
377
|
+
if (hasArkKind(prefixDef, "module")) {
|
|
378
|
+
const resolution = prefixDef[name.slice(dotIndex + 1)]?.root
|
|
379
|
+
// if the first part of name is a submodule but the suffix is
|
|
380
|
+
// unresolvable, we can throw immediately
|
|
381
|
+
if (!resolution) {
|
|
382
|
+
return throwParseError(writeUnresolvableMessage(name))
|
|
383
|
+
}
|
|
384
|
+
this.resolutions[name] = resolution
|
|
385
|
+
return resolution
|
|
386
|
+
}
|
|
387
|
+
if (prefixDef !== undefined) {
|
|
388
|
+
return throwParseError(writeNonSubmoduleDotMessage(dotPrefix))
|
|
389
|
+
}
|
|
390
|
+
// if the name includes ".", but the prefix is not an alias, it
|
|
391
|
+
// might be something like a decimal literal, so just fall through to return
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
maybeResolveNode(name: string): Root | undefined {
|
|
395
|
+
const result = this.maybeResolve(name)
|
|
396
|
+
return hasArkKind(result, "node") ? (result as never) : undefined
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
import<names extends exportedName<r>[]>(
|
|
400
|
+
...names: names
|
|
401
|
+
): destructuredImportContext<
|
|
402
|
+
r,
|
|
403
|
+
names extends [] ? keyof r["exports"] : names[number]
|
|
404
|
+
> {
|
|
405
|
+
return addArkKind(
|
|
406
|
+
transform(this.export(...names), ([alias, value]) => [
|
|
407
|
+
`#${alias as string}`,
|
|
408
|
+
value
|
|
409
|
+
]) as never,
|
|
410
|
+
"module"
|
|
411
|
+
) as never
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
compile() {
|
|
415
|
+
return ""
|
|
416
|
+
// this.export()
|
|
417
|
+
// const references: Set<TypeNode> = new Set()
|
|
418
|
+
// for (const k in this.exportedResolutions!) {
|
|
419
|
+
// const resolution = this.exportedResolutions[k]
|
|
420
|
+
// if (hasArkKind(resolution, "node") && !references.has(resolution)) {
|
|
421
|
+
// for (const reference of resolution.references) {
|
|
422
|
+
// references.add(reference)
|
|
423
|
+
// }
|
|
424
|
+
// }
|
|
425
|
+
// }
|
|
426
|
+
// return [...references]
|
|
427
|
+
// .map(
|
|
428
|
+
// (ref) => `const ${ref.alias} = (${In}) => {
|
|
429
|
+
// ${ref.condition}
|
|
430
|
+
// return true
|
|
431
|
+
// }`
|
|
432
|
+
// )
|
|
433
|
+
// .join("\n")
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
private exportedResolutions: MergedResolutions | undefined
|
|
437
|
+
private exportCache: ExportCache | undefined
|
|
438
|
+
export<names extends exportedName<r>[]>(
|
|
439
|
+
...names: names
|
|
440
|
+
): Module<
|
|
441
|
+
names extends [] ? r : destructuredExportContext<r, names[number]>
|
|
442
|
+
> {
|
|
443
|
+
if (!this.exportCache) {
|
|
444
|
+
this.exportCache = {}
|
|
445
|
+
for (const name of this.exportedNames) {
|
|
446
|
+
let def = this.aliases[name]
|
|
447
|
+
if (hasArkKind(def, "generic")) {
|
|
448
|
+
this.exportCache[name] = def
|
|
449
|
+
continue
|
|
450
|
+
}
|
|
451
|
+
// TODO: thunk generics?
|
|
452
|
+
// handle generics before invoking thunks, since they use
|
|
453
|
+
// varargs they will incorrectly be considered thunks
|
|
454
|
+
if (isThunk(def)) {
|
|
455
|
+
def = def()
|
|
456
|
+
}
|
|
457
|
+
if (hasArkKind(def, "module")) {
|
|
458
|
+
this.exportCache[name] = def
|
|
459
|
+
} else {
|
|
460
|
+
this.exportCache[name] = new Type(this.maybeResolve(name), this)
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
this.exportedResolutions = resolutionsOfModule(this.exportCache)
|
|
464
|
+
Object.assign(this.resolutions, this.exportedResolutions)
|
|
465
|
+
}
|
|
466
|
+
const namesToExport = names.length ? names : this.exportedNames
|
|
467
|
+
return addArkKind(
|
|
468
|
+
transform(namesToExport, ([, name]) => [
|
|
469
|
+
name,
|
|
470
|
+
this.exportCache![name]
|
|
471
|
+
]) as never,
|
|
472
|
+
"module"
|
|
473
|
+
) as never
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
type ExportCache = Record<string, Type | Generic | Module>
|
|
478
|
+
|
|
479
|
+
const resolutionsOfModule = (typeSet: ExportCache) => {
|
|
480
|
+
const result: MergedResolutions = {}
|
|
481
|
+
for (const k in typeSet) {
|
|
482
|
+
const v = typeSet[k]
|
|
483
|
+
if (hasArkKind(v, "module")) {
|
|
484
|
+
const innerResolutions = resolutionsOfModule(v as never)
|
|
485
|
+
const prefixedResolutions = transform(
|
|
486
|
+
innerResolutions,
|
|
487
|
+
([innerK, innerV]) => [`${k}.${innerK}`, innerV]
|
|
488
|
+
)
|
|
489
|
+
Object.assign(result, prefixedResolutions)
|
|
490
|
+
} else if (hasArkKind(v, "generic")) {
|
|
491
|
+
result[k] = v
|
|
492
|
+
} else {
|
|
493
|
+
// TODO: needed?
|
|
494
|
+
result[k] = v.root as never
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
return result
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
type destructuredExportContext<
|
|
501
|
+
r extends Resolutions,
|
|
502
|
+
name extends exportedName<r>
|
|
503
|
+
> = {
|
|
504
|
+
exports: { [k in name]: r["exports"][k] }
|
|
505
|
+
locals: r["locals"] & {
|
|
506
|
+
[k in Exclude<keyof r["exports"], name>]: r["exports"][k]
|
|
507
|
+
}
|
|
508
|
+
ambient: r["ambient"]
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
type destructuredImportContext<
|
|
512
|
+
r extends Resolutions,
|
|
513
|
+
name extends exportedName<r>
|
|
514
|
+
> = {
|
|
515
|
+
[k in name as `#${k & string}`]: type.cast<r["exports"][k]>
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
export const writeShallowCycleErrorMessage = (name: string, seen: string[]) =>
|
|
519
|
+
`Alias '${name}' has a shallow resolution cycle: ${[...seen, name].join(":")}`
|
|
520
|
+
|
|
521
|
+
export const writeDuplicateNameMessage = <name extends string>(
|
|
522
|
+
name: name
|
|
523
|
+
): writeDuplicateNameMessage<name> => `Duplicate name '${name}'`
|
|
524
|
+
|
|
525
|
+
type writeDuplicateNameMessage<name extends string> = `Duplicate name '${name}'`
|
|
526
|
+
|
|
527
|
+
export type ParsedScopeKey = {
|
|
528
|
+
isLocal: boolean
|
|
529
|
+
name: string
|
|
530
|
+
params: string[]
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
export const parseScopeKey = (k: string): ParsedScopeKey => {
|
|
534
|
+
const isLocal = k[0] === "#"
|
|
535
|
+
const name = isLocal ? k.slice(1) : k
|
|
536
|
+
const firstParamIndex = k.indexOf("<")
|
|
537
|
+
if (firstParamIndex === -1) {
|
|
538
|
+
return {
|
|
539
|
+
isLocal,
|
|
540
|
+
name,
|
|
541
|
+
params: []
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
if (k.at(-1) !== ">") {
|
|
545
|
+
throwParseError(
|
|
546
|
+
`'>' must be the last character of a generic declaration in a scope`
|
|
547
|
+
)
|
|
548
|
+
}
|
|
549
|
+
return {
|
|
550
|
+
isLocal,
|
|
551
|
+
name: name.slice(0, firstParamIndex),
|
|
552
|
+
params: parseGenericParams(k.slice(firstParamIndex + 1, -1))
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
type parseScopeKey<k> = k extends PrivateDeclaration<infer inner>
|
|
557
|
+
? parsePossibleGenericDeclaration<inner, true>
|
|
558
|
+
: parsePossibleGenericDeclaration<k, false>
|
|
559
|
+
|
|
560
|
+
type parsePossibleGenericDeclaration<
|
|
561
|
+
k,
|
|
562
|
+
isLocal extends boolean
|
|
563
|
+
> = k extends GenericDeclaration<infer name, infer paramString>
|
|
564
|
+
? {
|
|
565
|
+
isLocal: isLocal
|
|
566
|
+
name: name
|
|
567
|
+
params: parseGenericParams<paramString>
|
|
568
|
+
}
|
|
569
|
+
: {
|
|
570
|
+
isLocal: isLocal
|
|
571
|
+
name: k
|
|
572
|
+
params: []
|
|
573
|
+
}
|
package/scopes/ark.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { inferred } from "@arktype/schema"
|
|
2
|
+
import type { MatchParser, WhenParser } from "../match.js"
|
|
3
|
+
import { Scope, type Module, type ScopeParser } from "../scope.js"
|
|
4
|
+
import type {
|
|
5
|
+
DeclarationParser,
|
|
6
|
+
DefinitionParser,
|
|
7
|
+
TypeParser
|
|
8
|
+
} from "../type.js"
|
|
9
|
+
import { jsObjectsModule, type InferredJsObjects } from "./jsObjects.js"
|
|
10
|
+
import { parsingModule, type ParsingModule } from "./parsing.js"
|
|
11
|
+
import { tsGenericsModule, type InferredTsGenerics } from "./tsGenerics.js"
|
|
12
|
+
import { tsKeywordsModule, type InferredTsKeywords } from "./tsKeywords.js"
|
|
13
|
+
import { validationModule, type InferredValidation } from "./validation.js"
|
|
14
|
+
|
|
15
|
+
/** Root scopes can be inferred automatically from node definitions, but
|
|
16
|
+
* explicitly typing them can improve responsiveness */
|
|
17
|
+
export type RootScope<exports> = Scope<{
|
|
18
|
+
exports: exports
|
|
19
|
+
locals: {}
|
|
20
|
+
ambient: {}
|
|
21
|
+
}>
|
|
22
|
+
|
|
23
|
+
export type ArkResolutions = { exports: Ark; locals: {}; ambient: Ark }
|
|
24
|
+
|
|
25
|
+
export const ark: Scope<ArkResolutions> = Scope.root({
|
|
26
|
+
...tsKeywordsModule,
|
|
27
|
+
...jsObjectsModule,
|
|
28
|
+
...validationModule,
|
|
29
|
+
parse: parsingModule,
|
|
30
|
+
// again, unfortunately TS won't handle comparing generics well here, so we
|
|
31
|
+
// have to cast. that said, since each individual root scope is checked,
|
|
32
|
+
// this is low risk
|
|
33
|
+
...tsGenericsModule
|
|
34
|
+
}).toAmbient() as never
|
|
35
|
+
|
|
36
|
+
export const arktypes: Module<ArkResolutions> = ark.export()
|
|
37
|
+
|
|
38
|
+
// this type is redundant with the inferred definition of ark but allow types
|
|
39
|
+
// derived from the default scope to be calulated more efficiently
|
|
40
|
+
export interface Ark
|
|
41
|
+
extends InferredTsKeywords,
|
|
42
|
+
InferredJsObjects,
|
|
43
|
+
InferredValidation,
|
|
44
|
+
InferredTsGenerics {
|
|
45
|
+
parse: ParsingModule
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export const scope: ScopeParser<{}, Ark> = ark.scope as never
|
|
49
|
+
|
|
50
|
+
export const type: TypeParser<Ark> = ark.type
|
|
51
|
+
|
|
52
|
+
export const match: MatchParser<Ark> = ark.match
|
|
53
|
+
|
|
54
|
+
export const when: WhenParser<Ark> = ark.when
|
|
55
|
+
|
|
56
|
+
export namespace type {
|
|
57
|
+
export type cast<to> = {
|
|
58
|
+
[inferred]?: to
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export const define: DefinitionParser<Ark> = ark.define
|
|
63
|
+
|
|
64
|
+
export const declare: DeclarationParser<Ark> = ark.declare
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { node } from "@arktype/schema"
|
|
2
|
+
import { Scope } from "../scope.js"
|
|
3
|
+
import type { RootScope } from "./ark.js"
|
|
4
|
+
|
|
5
|
+
export interface InferredJsObjects {
|
|
6
|
+
Function: Function
|
|
7
|
+
Date: Date
|
|
8
|
+
Error: Error
|
|
9
|
+
Map: Map<unknown, unknown>
|
|
10
|
+
RegExp: RegExp
|
|
11
|
+
Set: Set<unknown>
|
|
12
|
+
WeakMap: WeakMap<object, unknown>
|
|
13
|
+
WeakSet: WeakSet<object>
|
|
14
|
+
Promise: Promise<unknown>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const jsObjects: RootScope<InferredJsObjects> = Scope.root({
|
|
18
|
+
Function: node(Function),
|
|
19
|
+
Date: node(Date),
|
|
20
|
+
Error: node(Error),
|
|
21
|
+
Map: node(Map),
|
|
22
|
+
RegExp: node(RegExp),
|
|
23
|
+
Set: node(Set),
|
|
24
|
+
WeakMap: node(WeakMap),
|
|
25
|
+
WeakSet: node(WeakSet),
|
|
26
|
+
Promise: node(Promise)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
export const jsObjectsModule = jsObjects.export()
|