arktype 2.0.0-dev.0 → 2.0.0-dev.10
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/__tests__/array.test.ts +270 -151
- package/__tests__/badDefinitionType.test.ts +10 -3
- package/__tests__/basis.test.ts +6 -2
- package/__tests__/bounds.test.ts +282 -352
- package/__tests__/cast.test.ts +7 -2
- package/__tests__/completions.test.ts +8 -3
- package/__tests__/config.test.ts +95 -167
- package/__tests__/dateLiteral.test.ts +8 -4
- package/__tests__/declared.test.ts +7 -6
- package/__tests__/discrimination.test.ts +1 -1
- package/__tests__/divisor.test.ts +46 -22
- package/__tests__/enclosed.test.ts +39 -25
- package/__tests__/examples.test.ts +56 -56
- package/__tests__/expressions.test.ts +184 -112
- package/__tests__/generic.test.ts +307 -288
- package/__tests__/group.test.ts +67 -54
- package/__tests__/imports.test.ts +62 -49
- package/__tests__/instanceof.test.ts +25 -23
- package/__tests__/intersection.test.ts +52 -48
- package/__tests__/keyTraversal.test.ts +77 -73
- package/__tests__/keyof.test.ts +68 -52
- package/__tests__/keywords.test.ts +164 -138
- package/__tests__/literal.test.ts +60 -21
- package/__tests__/match.bench.ts +31 -0
- package/__tests__/match.test.ts +222 -22
- package/__tests__/narrow.test.ts +52 -54
- package/__tests__/objectLiteral.test.ts +347 -205
- package/__tests__/operator.bench.ts +4 -4
- package/__tests__/pipe.test.ts +501 -0
- package/__tests__/realWorld.test.ts +63 -0
- package/__tests__/regex.test.ts +17 -7
- package/__tests__/scope.bench.ts +1 -1
- package/__tests__/scope.test.ts +257 -23
- package/__tests__/serialization.test.ts +19 -0
- package/__tests__/string.test.ts +11 -7
- package/__tests__/submodule.test.ts +26 -39
- package/__tests__/this.test.ts +83 -57
- package/__tests__/thunk.test.ts +13 -7
- package/__tests__/traverse.test.ts +76 -54
- package/__tests__/tsGenerics.test.ts +12 -12
- package/__tests__/type.test.ts +36 -0
- package/__tests__/typeReference.test.ts +7 -7
- package/__tests__/unenclosed.test.ts +116 -100
- package/__tests__/union.test.ts +171 -104
- package/ark.ts +21 -65
- package/generic.ts +69 -0
- package/main.ts +6 -18
- package/match.ts +195 -93
- package/module.ts +15 -0
- package/out/ark.d.ts +15 -39
- package/out/ark.js +9 -18
- package/out/ark.js.map +1 -1
- package/out/generic.d.ts +21 -0
- package/out/generic.js +24 -0
- package/out/generic.js.map +1 -0
- package/out/main.d.ts +6 -7
- package/out/main.js +4 -3
- package/out/main.js.map +1 -1
- package/out/match.d.ts +57 -34
- package/out/match.js +51 -17
- package/out/match.js.map +1 -1
- package/out/module.d.ts +10 -0
- package/out/module.js +3 -0
- package/out/module.js.map +1 -0
- package/out/parser/definition.d.ts +11 -13
- package/out/parser/definition.js +10 -13
- package/out/parser/definition.js.map +1 -1
- package/out/parser/generic.d.ts +5 -7
- package/out/parser/generic.js +7 -11
- package/out/parser/generic.js.map +1 -1
- package/out/parser/objectLiteral.d.ts +45 -14
- package/out/parser/objectLiteral.js +61 -52
- package/out/parser/objectLiteral.js.map +1 -1
- package/out/parser/semantic/bounds.d.ts +10 -8
- package/out/parser/semantic/bounds.js +0 -1
- package/out/parser/semantic/bounds.js.map +1 -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/infer.js.map +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 +15 -14
- package/out/parser/semantic/validate.js +1 -1
- package/out/parser/semantic/validate.js.map +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/dynamic.js.map +1 -1
- package/out/parser/string/reduce/shared.d.ts +6 -20
- package/out/parser/string/reduce/shared.js +0 -1
- package/out/parser/string/reduce/shared.js.map +1 -1
- package/out/parser/string/reduce/static.d.ts +27 -24
- package/out/parser/string/reduce/static.js +0 -1
- package/out/parser/string/shift/operand/date.d.ts +3 -3
- package/out/parser/string/shift/operand/date.js +4 -7
- package/out/parser/string/shift/operand/date.js.map +1 -1
- package/out/parser/string/shift/operand/enclosed.d.ts +4 -19
- package/out/parser/string/shift/operand/enclosed.js +12 -13
- package/out/parser/string/shift/operand/enclosed.js.map +1 -1
- 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/genericArgs.js.map +1 -1
- package/out/parser/string/shift/operand/operand.d.ts +3 -4
- package/out/parser/string/shift/operand/operand.js +8 -12
- package/out/parser/string/shift/operand/operand.js.map +1 -1
- package/out/parser/string/shift/operand/unenclosed.d.ts +15 -22
- package/out/parser/string/shift/operand/unenclosed.js +24 -36
- package/out/parser/string/shift/operand/unenclosed.js.map +1 -1
- package/out/parser/string/shift/operator/bounds.d.ts +9 -13
- package/out/parser/string/shift/operator/bounds.js +43 -54
- package/out/parser/string/shift/operator/bounds.js.map +1 -1
- package/out/parser/string/shift/operator/divisor.d.ts +2 -4
- package/out/parser/string/shift/operator/divisor.js +1 -3
- package/out/parser/string/shift/operator/divisor.js.map +1 -1
- package/out/parser/string/shift/operator/operator.d.ts +2 -3
- package/out/parser/string/shift/operator/operator.js +12 -19
- package/out/parser/string/shift/operator/operator.js.map +1 -1
- package/out/parser/string/shift/scanner.d.ts +2 -2
- package/out/parser/string/shift/scanner.js +14 -16
- package/out/parser/string/shift/scanner.js.map +1 -1
- package/out/parser/string/string.d.ts +8 -6
- package/out/parser/string/string.js +1 -3
- package/out/parser/string/string.js.map +1 -1
- package/out/parser/tuple.d.ts +102 -43
- package/out/parser/tuple.js +119 -87
- package/out/parser/tuple.js.map +1 -1
- package/out/scope.d.ts +43 -120
- package/out/scope.js +39 -209
- package/out/scope.js.map +1 -1
- package/out/type.d.ts +43 -64
- package/out/type.js +30 -131
- package/out/type.js.map +1 -1
- package/package.json +7 -3
- package/parser/definition.ts +77 -93
- package/parser/generic.ts +38 -40
- package/parser/objectLiteral.ts +172 -136
- package/parser/semantic/bounds.ts +33 -32
- package/parser/semantic/divisor.ts +8 -10
- package/parser/semantic/infer.ts +141 -0
- package/parser/semantic/utils.ts +12 -23
- package/parser/semantic/validate.ts +81 -73
- package/parser/string/reduce/dynamic.ts +67 -55
- package/parser/string/reduce/shared.ts +1 -3
- package/parser/string/reduce/static.ts +67 -69
- package/parser/string/shift/operand/date.ts +15 -14
- package/parser/string/shift/operand/enclosed.ts +30 -25
- package/parser/string/shift/operand/genericArgs.ts +46 -45
- package/parser/string/shift/operand/operand.ts +32 -38
- package/parser/string/shift/operand/unenclosed.ts +107 -158
- package/parser/string/shift/operator/bounds.ts +118 -112
- package/parser/string/shift/operator/divisor.ts +14 -17
- package/parser/string/shift/operator/operator.ts +35 -46
- package/parser/string/shift/scanner.ts +51 -50
- package/parser/string/string.ts +30 -35
- package/parser/tuple.ts +375 -307
- package/scope.ts +170 -453
- package/type.ts +193 -274
- package/__tests__/astUtils.test.ts +0 -17
- package/__tests__/cyclic.test.ts +0 -144
- package/__tests__/jsdoc.test.ts +0 -64
- package/__tests__/morph.test.ts +0 -409
- package/__tests__/nodeDefinitions.test.ts +0 -190
- package/__tests__/typeMethods.test.ts +0 -69
- package/out/ark.d.ts.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.map +0 -1
- package/out/match.d.ts.map +0 -1
- package/out/parser/definition.d.ts.map +0 -1
- package/out/parser/generic.d.ts.map +0 -1
- package/out/parser/objectLiteral.d.ts.map +0 -1
- package/out/parser/semantic/bounds.d.ts.map +0 -1
- package/out/parser/semantic/divisor.d.ts.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/validate.d.ts.map +0 -1
- package/out/parser/shared.d.ts +0 -54
- package/out/parser/shared.d.ts.map +0 -1
- package/out/parser/shared.js +0 -47
- package/out/parser/shared.js.map +0 -1
- package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
- package/out/parser/string/reduce/shared.d.ts.map +0 -1
- package/out/parser/string/reduce/static.d.ts.map +0 -1
- package/out/parser/string/shift/operand/date.d.ts.map +0 -1
- package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
- package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
- package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
- package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
- package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
- package/out/parser/string/shift/scanner.d.ts.map +0 -1
- package/out/parser/string/string.d.ts.map +0 -1
- package/out/parser/tuple.d.ts.map +0 -1
- package/out/scope.d.ts.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/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/semantic/intersections.ts +0 -46
- package/parser/semantic/semantic.ts +0 -144
- package/parser/shared.ts +0 -152
- package/util.ts +0 -36
package/scope.ts
CHANGED
|
@@ -1,98 +1,86 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
type
|
|
5
|
-
type
|
|
6
|
-
type
|
|
7
|
-
type
|
|
8
|
-
|
|
2
|
+
type ArkConfig,
|
|
3
|
+
type GenericProps,
|
|
4
|
+
type PreparsedNodeResolution,
|
|
5
|
+
type PrivateDeclaration,
|
|
6
|
+
type RawSchema,
|
|
7
|
+
type RawSchemaResolutions,
|
|
8
|
+
RawSchemaScope,
|
|
9
|
+
type SchemaScope,
|
|
10
|
+
type UnknownSchema,
|
|
11
|
+
type ambient,
|
|
12
|
+
type arkKind,
|
|
13
|
+
type destructuredExportContext,
|
|
14
|
+
type destructuredImportContext,
|
|
15
|
+
type exportedNameOf,
|
|
16
|
+
hasArkKind,
|
|
17
|
+
type writeDuplicateAliasError
|
|
9
18
|
} from "@arktype/schema"
|
|
10
19
|
import {
|
|
20
|
+
type Dict,
|
|
11
21
|
domainOf,
|
|
12
22
|
hasDomain,
|
|
23
|
+
type isAnyOrNever,
|
|
13
24
|
isThunk,
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
type
|
|
17
|
-
|
|
18
|
-
type isAny,
|
|
19
|
-
type nominal
|
|
25
|
+
type keyError,
|
|
26
|
+
type nominal,
|
|
27
|
+
type show,
|
|
28
|
+
throwParseError
|
|
20
29
|
} from "@arktype/util"
|
|
21
30
|
import type { type } from "./ark.js"
|
|
22
|
-
import {
|
|
31
|
+
import { Generic } from "./generic.js"
|
|
32
|
+
import { type MatchParser, createMatchParser } from "./match.js"
|
|
33
|
+
import type { Module } from "./module.js"
|
|
23
34
|
import {
|
|
24
|
-
parseObject,
|
|
25
|
-
writeBadDefinitionTypeMessage,
|
|
26
35
|
type inferDefinition,
|
|
27
|
-
|
|
36
|
+
parseObject,
|
|
37
|
+
type validateDefinition,
|
|
38
|
+
writeBadDefinitionTypeMessage
|
|
28
39
|
} from "./parser/definition.js"
|
|
29
40
|
import {
|
|
30
|
-
parseGenericParams,
|
|
31
41
|
type GenericDeclaration,
|
|
32
|
-
type GenericParamsParseError
|
|
42
|
+
type GenericParamsParseError,
|
|
43
|
+
parseGenericParams
|
|
33
44
|
} from "./parser/generic.js"
|
|
34
45
|
import { DynamicState } from "./parser/string/reduce/dynamic.js"
|
|
35
|
-
import {
|
|
36
|
-
writeMissingSubmoduleAccessMessage,
|
|
37
|
-
writeNonSubmoduleDotMessage,
|
|
38
|
-
writeUnresolvableMessage
|
|
39
|
-
} from "./parser/string/shift/operand/unenclosed.js"
|
|
40
46
|
import { fullStringParse } from "./parser/string/string.js"
|
|
41
47
|
import {
|
|
42
|
-
Type,
|
|
43
|
-
createTypeParser,
|
|
44
|
-
generic,
|
|
45
|
-
validateUninstantiatedGeneric,
|
|
46
48
|
type DeclarationParser,
|
|
47
49
|
type DefinitionParser,
|
|
48
|
-
|
|
49
|
-
type
|
|
50
|
-
type TypeConfig,
|
|
50
|
+
RawTypeParser,
|
|
51
|
+
type Type,
|
|
51
52
|
type TypeParser
|
|
52
53
|
} from "./type.js"
|
|
53
|
-
import { addArkKind, hasArkKind, type arkKind } from "./util.js"
|
|
54
|
-
|
|
55
|
-
export type ScopeParser<parent, ambient> = {
|
|
56
|
-
<const def>(def: validateScope<def, parent & ambient>): Scope<{
|
|
57
|
-
exports: inferBootstrapped<{
|
|
58
|
-
exports: bootstrapExports<def>
|
|
59
|
-
locals: bootstrapLocals<def> & parent
|
|
60
|
-
ambient: ambient
|
|
61
|
-
}>
|
|
62
|
-
locals: inferBootstrapped<{
|
|
63
|
-
exports: bootstrapLocals<def>
|
|
64
|
-
locals: bootstrapExports<def> & parent
|
|
65
|
-
ambient: ambient
|
|
66
|
-
}>
|
|
67
|
-
ambient: ambient
|
|
68
|
-
}>
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export type ScopeConfig = {
|
|
72
|
-
ambient?: Scope | null
|
|
73
|
-
codes?: Record<ArkErrorCode, { mustBe?: string }>
|
|
74
|
-
keys?: KeyCheckKind
|
|
75
|
-
}
|
|
76
54
|
|
|
77
|
-
type
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
55
|
+
export type ScopeParser = <const def>(
|
|
56
|
+
def: validateScope<def>,
|
|
57
|
+
config?: ArkConfig
|
|
58
|
+
) => Scope<inferBootstrapped<bootstrapAliases<def>>>
|
|
59
|
+
|
|
60
|
+
type validateScope<def> = {
|
|
61
|
+
[k in keyof def]: k extends symbol ?
|
|
62
|
+
// this should only occur when importing/exporting modules, and those
|
|
63
|
+
// keys should be ignored
|
|
64
|
+
unknown
|
|
65
|
+
: parseScopeKey<k>["params"] extends [] ?
|
|
66
|
+
// Not including Type here directly breaks inference
|
|
67
|
+
def[k] extends Type | PreparsedResolution ? def[k]
|
|
68
|
+
: k extends PrivateDeclaration<infer name extends keyof def & string> ?
|
|
69
|
+
keyError<writeDuplicateAliasError<name>>
|
|
70
|
+
: validateDefinition<def[k], ambient & bootstrapAliases<def>, {}>
|
|
71
|
+
: parseScopeKey<k>["params"] extends GenericParamsParseError ?
|
|
72
|
+
// use the full nominal type here to avoid an overlap between the
|
|
73
|
+
// error message and a possible value for the property
|
|
74
|
+
parseScopeKey<k>["params"][0]
|
|
75
|
+
: validateDefinition<
|
|
76
|
+
def[k],
|
|
77
|
+
ambient & bootstrapAliases<def>,
|
|
78
|
+
{
|
|
79
|
+
// once we support constraints on generic parameters, we'd use
|
|
80
|
+
// the base type here: https://github.com/arktypeio/arktype/issues/796
|
|
81
|
+
[param in parseScopeKey<k>["params"][number]]: unknown
|
|
82
|
+
}
|
|
83
|
+
>
|
|
96
84
|
}
|
|
97
85
|
|
|
98
86
|
export type bindThis<def> = { this: Def<def> }
|
|
@@ -103,442 +91,180 @@ type Def<def = {}> = nominal<def, "unparsed">
|
|
|
103
91
|
/** sentinel indicating a scope that will be associated with a generic has not yet been parsed */
|
|
104
92
|
export type UnparsedScope = "$"
|
|
105
93
|
|
|
106
|
-
type bootstrap<def> = bootstrapLocals<def> & bootstrapExports<def>
|
|
107
|
-
|
|
108
|
-
type bootstrapLocals<def> = bootstrapAliases<{
|
|
109
|
-
// intersection seems redundant but it is more efficient for TS to avoid
|
|
110
|
-
// mapping all the keys
|
|
111
|
-
[k in keyof def & PrivateDeclaration as extractPrivateKey<k>]: def[k]
|
|
112
|
-
}>
|
|
113
|
-
|
|
114
|
-
type bootstrapExports<def> = bootstrapAliases<{
|
|
115
|
-
[k in Exclude<keyof def, PrivateDeclaration>]: def[k]
|
|
116
|
-
}>
|
|
117
|
-
|
|
118
94
|
/** These are legal as values of a scope but not as definitions in other contexts */
|
|
119
|
-
type PreparsedResolution =
|
|
95
|
+
type PreparsedResolution = PreparsedNodeResolution
|
|
120
96
|
|
|
121
97
|
type bootstrapAliases<def> = {
|
|
122
98
|
[k in Exclude<
|
|
123
99
|
keyof def,
|
|
124
100
|
// avoid inferring nominal symbols, e.g. arkKind from Module
|
|
125
101
|
GenericDeclaration | symbol
|
|
126
|
-
>]: def[k] extends PreparsedResolution
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
: Def<def[k]>
|
|
102
|
+
>]: def[k] extends PreparsedResolution ? def[k]
|
|
103
|
+
: def[k] extends (() => infer thunkReturn extends PreparsedResolution) ?
|
|
104
|
+
thunkReturn
|
|
105
|
+
: Def<def[k]>
|
|
131
106
|
} & {
|
|
132
|
-
[k in keyof def & GenericDeclaration as extractGenericName<k>]:
|
|
107
|
+
[k in keyof def & GenericDeclaration as extractGenericName<k>]: GenericProps<
|
|
133
108
|
parseGenericParams<extractGenericParameters<k>>,
|
|
134
109
|
def[k],
|
|
135
110
|
UnparsedScope
|
|
136
111
|
>
|
|
137
112
|
}
|
|
138
113
|
|
|
139
|
-
type inferBootstrapped
|
|
140
|
-
[name in keyof
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
114
|
+
type inferBootstrapped<$> = show<{
|
|
115
|
+
[name in keyof $]: $[name] extends Def<infer def> ?
|
|
116
|
+
inferDefinition<def, $ & ambient, {}>
|
|
117
|
+
: $[name] extends GenericProps<infer params, infer def> ?
|
|
118
|
+
// add the scope in which the generic was defined here
|
|
119
|
+
Generic<params, def, $>
|
|
120
|
+
: // otherwise should be a submodule
|
|
121
|
+
$[name]
|
|
147
122
|
}>
|
|
148
123
|
|
|
149
|
-
type extractGenericName<k> =
|
|
150
|
-
? name
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
type PrivateDeclaration<key extends string = string> = `#${key}`
|
|
165
|
-
|
|
166
|
-
export type resolve<reference extends keyof $ | keyof args, $, args> = (
|
|
167
|
-
reference extends keyof args ? args[reference] : $[reference & keyof $]
|
|
168
|
-
) extends infer resolution
|
|
169
|
-
? [resolution] extends [never]
|
|
170
|
-
? never
|
|
171
|
-
: isAny<resolution> extends true
|
|
172
|
-
? any
|
|
173
|
-
: resolution extends Def<infer def>
|
|
174
|
-
? inferDefinition<def, $, args>
|
|
124
|
+
type extractGenericName<k> =
|
|
125
|
+
k extends GenericDeclaration<infer name> ? name : never
|
|
126
|
+
|
|
127
|
+
type extractGenericParameters<k> =
|
|
128
|
+
k extends GenericDeclaration<string, infer params> ? params : never
|
|
129
|
+
|
|
130
|
+
export type resolve<reference extends keyof $ | keyof args, $, args> =
|
|
131
|
+
(
|
|
132
|
+
reference extends keyof args ?
|
|
133
|
+
args[reference]
|
|
134
|
+
: $[reference & keyof $]
|
|
135
|
+
) extends infer resolution ?
|
|
136
|
+
isAnyOrNever<resolution> extends true ? resolution
|
|
137
|
+
: resolution extends Def<infer def> ? inferDefinition<def, $, args>
|
|
175
138
|
: resolution
|
|
176
|
-
:
|
|
139
|
+
: never
|
|
177
140
|
|
|
178
141
|
export type moduleKeyOf<$> = {
|
|
179
|
-
|
|
142
|
+
// TODO: check against module directly?
|
|
143
|
+
[k in keyof $]: $[k] extends { [arkKind]: "module" } ? k & string : never
|
|
180
144
|
}[keyof $]
|
|
181
145
|
|
|
182
146
|
export type tryInferSubmoduleReference<$, token> =
|
|
183
|
-
token extends `${infer submodule extends moduleKeyOf<$>}.${infer subalias}`
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
export type Module<r extends Resolutions = any> = {
|
|
196
|
-
// just adding the nominal id this way and mapping it is cheaper than an intersection
|
|
197
|
-
[k in exportedName<r> | arkKind]: k extends string
|
|
198
|
-
? [r["exports"][k]] extends [never]
|
|
199
|
-
? Type<never, $<r>>
|
|
200
|
-
: isAny<r["exports"][k]> extends true
|
|
201
|
-
? Type<any, $<r>>
|
|
202
|
-
: r["exports"][k] extends PreparsedResolution
|
|
203
|
-
? r["exports"][k]
|
|
204
|
-
: Type<r["exports"][k], $<r>>
|
|
205
|
-
: // set the nominal symbol's value to something validation won't care about
|
|
206
|
-
// since the inferred type will be omitted anyways
|
|
207
|
-
type.cast<"module">
|
|
147
|
+
token extends `${infer submodule extends moduleKeyOf<$>}.${infer subalias}` ?
|
|
148
|
+
subalias extends keyof $[submodule] ?
|
|
149
|
+
$[submodule][subalias] extends type.cast<infer t> ?
|
|
150
|
+
t
|
|
151
|
+
: never
|
|
152
|
+
: never
|
|
153
|
+
: never
|
|
154
|
+
|
|
155
|
+
export interface ParseContext {
|
|
156
|
+
$: RawScope
|
|
157
|
+
args?: Record<string, UnknownSchema>
|
|
208
158
|
}
|
|
209
159
|
|
|
210
|
-
export
|
|
211
|
-
|
|
212
|
-
locals: unknown
|
|
213
|
-
ambient: unknown
|
|
214
|
-
}
|
|
160
|
+
export const scope: ScopeParser = ((def: Dict, config: ArkConfig = {}) =>
|
|
161
|
+
new RawScope(def, config)) as never
|
|
215
162
|
|
|
216
|
-
export
|
|
217
|
-
|
|
218
|
-
path: string[]
|
|
219
|
-
scope: Scope
|
|
220
|
-
args: Record<string, TypeNode> | undefined
|
|
221
|
-
}
|
|
163
|
+
export interface Scope<$ = any> extends SchemaScope<$> {
|
|
164
|
+
type: TypeParser<$>
|
|
222
165
|
|
|
223
|
-
|
|
166
|
+
match: MatchParser<$>
|
|
224
167
|
|
|
225
|
-
|
|
168
|
+
declare: DeclarationParser<$>
|
|
226
169
|
|
|
227
|
-
|
|
228
|
-
declare infer: extractOut<r["exports"]>
|
|
229
|
-
declare inferIn: extractIn<r["exports"]>
|
|
170
|
+
define: DefinitionParser<$>
|
|
230
171
|
|
|
231
|
-
|
|
172
|
+
import<names extends exportedNameOf<$>[]>(
|
|
173
|
+
...names: names
|
|
174
|
+
): Module<show<destructuredImportContext<$, names>>>
|
|
232
175
|
|
|
233
|
-
|
|
234
|
-
|
|
176
|
+
export<names extends exportedNameOf<$>[]>(
|
|
177
|
+
...names: names
|
|
178
|
+
): Module<show<destructuredExportContext<$, names>>>
|
|
179
|
+
}
|
|
235
180
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
private
|
|
240
|
-
private ambient: Scope | null
|
|
181
|
+
export class RawScope<
|
|
182
|
+
$ extends RawSchemaResolutions = RawSchemaResolutions
|
|
183
|
+
> extends RawSchemaScope<$> {
|
|
184
|
+
private parseCache: Record<string, RawSchema> = {}
|
|
241
185
|
|
|
242
|
-
constructor(def:
|
|
186
|
+
constructor(def: Record<string, unknown>, config?: ArkConfig) {
|
|
187
|
+
const aliases: Record<string, unknown> = {}
|
|
243
188
|
for (const k in def) {
|
|
244
189
|
const parsedKey = parseScopeKey(k)
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
}
|
|
190
|
+
aliases[parsedKey.name] =
|
|
191
|
+
parsedKey.params.length ?
|
|
192
|
+
// TODO: this
|
|
193
|
+
new Generic(parsedKey.params, def[k], {} as never)
|
|
194
|
+
: def[k]
|
|
251
195
|
}
|
|
252
|
-
|
|
253
|
-
if (this.ambient) {
|
|
254
|
-
// ensure exportedResolutions is populated
|
|
255
|
-
this.ambient.export()
|
|
256
|
-
this.resolutions = { ...this.ambient.exportedResolutions! }
|
|
257
|
-
} else {
|
|
258
|
-
this.resolutions = {}
|
|
259
|
-
}
|
|
260
|
-
this.config = config
|
|
196
|
+
super(aliases, config)
|
|
261
197
|
}
|
|
262
198
|
|
|
263
|
-
|
|
264
|
-
return new Scope(aliases, {}) as never
|
|
265
|
-
}
|
|
199
|
+
type = new RawTypeParser(this as never)
|
|
266
200
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
match: MatchParser<$<r>> = createMatchParser(this as never) as never
|
|
270
|
-
|
|
271
|
-
// TODO: decide if this API will be used for non-validated types
|
|
272
|
-
declare: DeclarationParser<$<r>> = () => ({ type: this.type }) as never
|
|
273
|
-
|
|
274
|
-
scope: ScopeParser<r["exports"], r["ambient"]> = ((
|
|
275
|
-
def: Dict,
|
|
276
|
-
config: TypeConfig = {}
|
|
277
|
-
) => {
|
|
278
|
-
return new Scope(def, {
|
|
279
|
-
ambient: this.ambient,
|
|
280
|
-
...this.config,
|
|
281
|
-
...config
|
|
282
|
-
})
|
|
283
|
-
}) as never
|
|
284
|
-
|
|
285
|
-
define: DefinitionParser<$<r>> = (def) => def as never
|
|
286
|
-
|
|
287
|
-
toAmbient(): Scope<{
|
|
288
|
-
exports: r["exports"]
|
|
289
|
-
locals: r["locals"]
|
|
290
|
-
ambient: r["exports"]
|
|
291
|
-
}> {
|
|
292
|
-
// TODO: private?
|
|
293
|
-
return new Scope(this.aliases, {
|
|
294
|
-
ambient: this,
|
|
295
|
-
...this.config
|
|
296
|
-
})
|
|
297
|
-
}
|
|
201
|
+
match: MatchParser<$> = createMatchParser(this as never) as never
|
|
298
202
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
): Type<r["exports"][name], $<r>> {
|
|
303
|
-
return this.export()[name] as never
|
|
304
|
-
}
|
|
203
|
+
declare = (() => ({
|
|
204
|
+
type: this.type
|
|
205
|
+
})).bind(this)
|
|
305
206
|
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
207
|
+
define = ((def: unknown) => def).bind(this)
|
|
208
|
+
|
|
209
|
+
override preparseRoot(def: unknown): unknown {
|
|
210
|
+
if (isThunk(def) && !hasArkKind(def, "generic")) return def()
|
|
211
|
+
|
|
212
|
+
return def
|
|
312
213
|
}
|
|
313
214
|
|
|
314
|
-
|
|
315
|
-
|
|
215
|
+
override parseRoot(def: unknown): RawSchema {
|
|
216
|
+
// args: { this: {} as RawSchema },
|
|
217
|
+
return this.parse(def, {
|
|
218
|
+
$: this as never,
|
|
219
|
+
args: {}
|
|
220
|
+
// type parsing can bypass nodes if it hits the cache,
|
|
221
|
+
// so bind it directly (could be optimized)
|
|
222
|
+
}).bindScope(this)
|
|
316
223
|
}
|
|
317
224
|
|
|
318
|
-
parse(def: unknown, ctx: ParseContext):
|
|
225
|
+
parse(def: unknown, ctx: ParseContext): RawSchema {
|
|
319
226
|
if (typeof def === "string") {
|
|
320
|
-
if (ctx.args
|
|
227
|
+
if (ctx.args && Object.keys(ctx.args).every(k => !def.includes(k))) {
|
|
321
228
|
// we can only rely on the cache if there are no contextual
|
|
322
229
|
// resolutions like "this" or generic args
|
|
323
230
|
return this.parseString(def, ctx)
|
|
324
231
|
}
|
|
325
|
-
if (!this.parseCache[def])
|
|
232
|
+
if (!this.parseCache[def])
|
|
326
233
|
this.parseCache[def] = this.parseString(def, ctx)
|
|
327
|
-
|
|
234
|
+
|
|
328
235
|
return this.parseCache[def]
|
|
329
236
|
}
|
|
330
|
-
return hasDomain(def, "object")
|
|
331
|
-
|
|
332
|
-
:
|
|
237
|
+
return hasDomain(def, "object") ?
|
|
238
|
+
parseObject(def, ctx)
|
|
239
|
+
: throwParseError(writeBadDefinitionTypeMessage(domainOf(def)))
|
|
333
240
|
}
|
|
334
241
|
|
|
335
|
-
parseString(def: string, ctx: ParseContext):
|
|
242
|
+
parseString(def: string, ctx: ParseContext): RawSchema {
|
|
336
243
|
return (
|
|
337
|
-
this.
|
|
244
|
+
this.maybeResolveSchema(def) ??
|
|
338
245
|
((def.endsWith("[]") &&
|
|
339
|
-
this.
|
|
246
|
+
this.maybeResolveSchema(def.slice(0, -2))?.array()) ||
|
|
340
247
|
fullStringParse(new DynamicState(def, ctx)))
|
|
341
248
|
)
|
|
342
249
|
}
|
|
343
|
-
|
|
344
|
-
maybeResolve(name: string): TypeNode | 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): TypeNode | undefined {
|
|
395
|
-
const result = this.maybeResolve(name)
|
|
396
|
-
return result instanceof BaseType ? (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"] & string : names[number]
|
|
404
|
-
> {
|
|
405
|
-
return addArkKind(
|
|
406
|
-
map(this.export(...names) as Dict, (alias, value) => [
|
|
407
|
-
`#${alias}`,
|
|
408
|
-
value
|
|
409
|
-
]) as never,
|
|
410
|
-
"module"
|
|
411
|
-
) as never
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
compile() {
|
|
415
|
-
return ""
|
|
416
|
-
// this.export()
|
|
417
|
-
// const references: Set<Root> = 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
|
-
bindThis() {
|
|
437
|
-
// TODO: fix
|
|
438
|
-
return { this: keywords.unknown }
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
private exportedResolutions: MergedResolutions | undefined
|
|
442
|
-
private exportCache: ExportCache | undefined
|
|
443
|
-
export<names extends exportedName<r>[]>(
|
|
444
|
-
...names: names
|
|
445
|
-
): Module<
|
|
446
|
-
names extends [] ? r : destructuredExportContext<r, names[number]>
|
|
447
|
-
> {
|
|
448
|
-
if (!this.exportCache) {
|
|
449
|
-
this.exportCache = {}
|
|
450
|
-
for (const name of this.exportedNames) {
|
|
451
|
-
let def = this.aliases[name]
|
|
452
|
-
if (hasArkKind(def, "generic")) {
|
|
453
|
-
this.exportCache[name] = def
|
|
454
|
-
continue
|
|
455
|
-
}
|
|
456
|
-
// TODO: thunk generics?
|
|
457
|
-
// handle generics before invoking thunks, since they use
|
|
458
|
-
// varargs they will incorrectly be considered thunks
|
|
459
|
-
if (isThunk(def)) {
|
|
460
|
-
def = def()
|
|
461
|
-
}
|
|
462
|
-
if (hasArkKind(def, "module")) {
|
|
463
|
-
this.exportCache[name] = def
|
|
464
|
-
} else {
|
|
465
|
-
this.exportCache[name] = new Type(this.maybeResolve(name), this)
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
this.exportedResolutions = resolutionsOfModule(this.exportCache)
|
|
469
|
-
Object.assign(this.resolutions, this.exportedResolutions)
|
|
470
|
-
}
|
|
471
|
-
const namesToExport = names.length ? names : this.exportedNames
|
|
472
|
-
return addArkKind(
|
|
473
|
-
map(namesToExport, (_, name) => [name, this.exportCache![name]]) as never,
|
|
474
|
-
"module"
|
|
475
|
-
) as never
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
type ExportCache = Record<string, Type | Generic | Module>
|
|
480
|
-
|
|
481
|
-
const resolutionsOfModule = (typeSet: ExportCache) => {
|
|
482
|
-
const result: MergedResolutions = {}
|
|
483
|
-
for (const k in typeSet) {
|
|
484
|
-
const v = typeSet[k]
|
|
485
|
-
if (hasArkKind(v, "module")) {
|
|
486
|
-
const innerResolutions = resolutionsOfModule(v as never)
|
|
487
|
-
const prefixedResolutions = map(innerResolutions, (innerK, innerV) => [
|
|
488
|
-
`${k}.${innerK}`,
|
|
489
|
-
innerV
|
|
490
|
-
])
|
|
491
|
-
Object.assign(result, prefixedResolutions)
|
|
492
|
-
} else if (hasArkKind(v, "generic")) {
|
|
493
|
-
result[k] = v
|
|
494
|
-
} else {
|
|
495
|
-
result[k] = (v as Type).root
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
return result
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
type destructuredExportContext<
|
|
502
|
-
r extends Resolutions,
|
|
503
|
-
name extends exportedName<r>
|
|
504
|
-
> = {
|
|
505
|
-
exports: { [k in name]: r["exports"][k] }
|
|
506
|
-
locals: r["locals"] & {
|
|
507
|
-
[k in Exclude<keyof r["exports"], name>]: r["exports"][k]
|
|
508
|
-
}
|
|
509
|
-
ambient: r["ambient"]
|
|
510
250
|
}
|
|
511
251
|
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
[k in name as `#${k & string}`]: type.cast<r["exports"][k]>
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
export const writeShallowCycleErrorMessage = (name: string, seen: string[]) =>
|
|
252
|
+
export const writeShallowCycleErrorMessage = (
|
|
253
|
+
name: string,
|
|
254
|
+
seen: string[]
|
|
255
|
+
): string =>
|
|
520
256
|
`Alias '${name}' has a shallow resolution cycle: ${[...seen, name].join(":")}`
|
|
521
257
|
|
|
522
|
-
export const writeDuplicateNameMessage = <name extends string>(
|
|
523
|
-
name: name
|
|
524
|
-
): writeDuplicateNameMessage<name> => `Duplicate name '${name}'`
|
|
525
|
-
|
|
526
|
-
type writeDuplicateNameMessage<name extends string> = `Duplicate name '${name}'`
|
|
527
|
-
|
|
528
258
|
export type ParsedScopeKey = {
|
|
529
|
-
isLocal: boolean
|
|
530
259
|
name: string
|
|
531
260
|
params: string[]
|
|
532
261
|
}
|
|
533
262
|
|
|
534
263
|
export const parseScopeKey = (k: string): ParsedScopeKey => {
|
|
535
|
-
const isLocal = k[0] === "#"
|
|
536
|
-
const name = isLocal ? k.slice(1) : k
|
|
537
264
|
const firstParamIndex = k.indexOf("<")
|
|
538
265
|
if (firstParamIndex === -1) {
|
|
539
266
|
return {
|
|
540
|
-
|
|
541
|
-
name,
|
|
267
|
+
name: k,
|
|
542
268
|
params: []
|
|
543
269
|
}
|
|
544
270
|
}
|
|
@@ -548,27 +274,18 @@ export const parseScopeKey = (k: string): ParsedScopeKey => {
|
|
|
548
274
|
)
|
|
549
275
|
}
|
|
550
276
|
return {
|
|
551
|
-
|
|
552
|
-
name: name.slice(0, firstParamIndex),
|
|
277
|
+
name: k.slice(0, firstParamIndex),
|
|
553
278
|
params: parseGenericParams(k.slice(firstParamIndex + 1, -1))
|
|
554
279
|
}
|
|
555
280
|
}
|
|
556
281
|
|
|
557
|
-
type parseScopeKey<k> =
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
type parsePossibleGenericDeclaration<
|
|
562
|
-
k,
|
|
563
|
-
isLocal extends boolean
|
|
564
|
-
> = k extends GenericDeclaration<infer name, infer paramString>
|
|
565
|
-
? {
|
|
566
|
-
isLocal: isLocal
|
|
282
|
+
export type parseScopeKey<k> =
|
|
283
|
+
k extends GenericDeclaration<infer name, infer paramString> ?
|
|
284
|
+
{
|
|
567
285
|
name: name
|
|
568
286
|
params: parseGenericParams<paramString>
|
|
569
|
-
|
|
570
|
-
:
|
|
571
|
-
isLocal: isLocal
|
|
287
|
+
}
|
|
288
|
+
: {
|
|
572
289
|
name: k
|
|
573
290
|
params: []
|
|
574
|
-
|
|
291
|
+
}
|