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.
Files changed (219) hide show
  1. package/__tests__/array.test.ts +270 -151
  2. package/__tests__/badDefinitionType.test.ts +10 -3
  3. package/__tests__/basis.test.ts +6 -2
  4. package/__tests__/bounds.test.ts +282 -352
  5. package/__tests__/cast.test.ts +7 -2
  6. package/__tests__/completions.test.ts +8 -3
  7. package/__tests__/config.test.ts +95 -167
  8. package/__tests__/dateLiteral.test.ts +8 -4
  9. package/__tests__/declared.test.ts +7 -6
  10. package/__tests__/discrimination.test.ts +1 -1
  11. package/__tests__/divisor.test.ts +46 -22
  12. package/__tests__/enclosed.test.ts +39 -25
  13. package/__tests__/examples.test.ts +56 -56
  14. package/__tests__/expressions.test.ts +184 -112
  15. package/__tests__/generic.test.ts +307 -288
  16. package/__tests__/group.test.ts +67 -54
  17. package/__tests__/imports.test.ts +62 -49
  18. package/__tests__/instanceof.test.ts +25 -23
  19. package/__tests__/intersection.test.ts +52 -48
  20. package/__tests__/keyTraversal.test.ts +77 -73
  21. package/__tests__/keyof.test.ts +68 -52
  22. package/__tests__/keywords.test.ts +164 -138
  23. package/__tests__/literal.test.ts +60 -21
  24. package/__tests__/match.bench.ts +31 -0
  25. package/__tests__/match.test.ts +222 -22
  26. package/__tests__/narrow.test.ts +52 -54
  27. package/__tests__/objectLiteral.test.ts +347 -205
  28. package/__tests__/operator.bench.ts +4 -4
  29. package/__tests__/pipe.test.ts +501 -0
  30. package/__tests__/realWorld.test.ts +63 -0
  31. package/__tests__/regex.test.ts +17 -7
  32. package/__tests__/scope.bench.ts +1 -1
  33. package/__tests__/scope.test.ts +257 -23
  34. package/__tests__/serialization.test.ts +19 -0
  35. package/__tests__/string.test.ts +11 -7
  36. package/__tests__/submodule.test.ts +26 -39
  37. package/__tests__/this.test.ts +83 -57
  38. package/__tests__/thunk.test.ts +13 -7
  39. package/__tests__/traverse.test.ts +76 -54
  40. package/__tests__/tsGenerics.test.ts +12 -12
  41. package/__tests__/type.test.ts +36 -0
  42. package/__tests__/typeReference.test.ts +7 -7
  43. package/__tests__/unenclosed.test.ts +116 -100
  44. package/__tests__/union.test.ts +171 -104
  45. package/ark.ts +21 -65
  46. package/generic.ts +69 -0
  47. package/main.ts +6 -18
  48. package/match.ts +195 -93
  49. package/module.ts +15 -0
  50. package/out/ark.d.ts +15 -39
  51. package/out/ark.js +9 -18
  52. package/out/ark.js.map +1 -1
  53. package/out/generic.d.ts +21 -0
  54. package/out/generic.js +24 -0
  55. package/out/generic.js.map +1 -0
  56. package/out/main.d.ts +6 -7
  57. package/out/main.js +4 -3
  58. package/out/main.js.map +1 -1
  59. package/out/match.d.ts +57 -34
  60. package/out/match.js +51 -17
  61. package/out/match.js.map +1 -1
  62. package/out/module.d.ts +10 -0
  63. package/out/module.js +3 -0
  64. package/out/module.js.map +1 -0
  65. package/out/parser/definition.d.ts +11 -13
  66. package/out/parser/definition.js +10 -13
  67. package/out/parser/definition.js.map +1 -1
  68. package/out/parser/generic.d.ts +5 -7
  69. package/out/parser/generic.js +7 -11
  70. package/out/parser/generic.js.map +1 -1
  71. package/out/parser/objectLiteral.d.ts +45 -14
  72. package/out/parser/objectLiteral.js +61 -52
  73. package/out/parser/objectLiteral.js.map +1 -1
  74. package/out/parser/semantic/bounds.d.ts +10 -8
  75. package/out/parser/semantic/bounds.js +0 -1
  76. package/out/parser/semantic/bounds.js.map +1 -1
  77. package/out/parser/semantic/divisor.d.ts +5 -7
  78. package/out/parser/semantic/divisor.js +0 -1
  79. package/out/parser/semantic/infer.d.ts +24 -0
  80. package/out/parser/semantic/infer.js +1 -0
  81. package/out/parser/semantic/infer.js.map +1 -0
  82. package/out/parser/semantic/utils.d.ts +3 -7
  83. package/out/parser/semantic/utils.js +0 -1
  84. package/out/parser/semantic/validate.d.ts +15 -14
  85. package/out/parser/semantic/validate.js +1 -1
  86. package/out/parser/semantic/validate.js.map +1 -1
  87. package/out/parser/string/reduce/dynamic.d.ts +13 -13
  88. package/out/parser/string/reduce/dynamic.js +31 -26
  89. package/out/parser/string/reduce/dynamic.js.map +1 -1
  90. package/out/parser/string/reduce/shared.d.ts +6 -20
  91. package/out/parser/string/reduce/shared.js +0 -1
  92. package/out/parser/string/reduce/shared.js.map +1 -1
  93. package/out/parser/string/reduce/static.d.ts +27 -24
  94. package/out/parser/string/reduce/static.js +0 -1
  95. package/out/parser/string/shift/operand/date.d.ts +3 -3
  96. package/out/parser/string/shift/operand/date.js +4 -7
  97. package/out/parser/string/shift/operand/date.js.map +1 -1
  98. package/out/parser/string/shift/operand/enclosed.d.ts +4 -19
  99. package/out/parser/string/shift/operand/enclosed.js +12 -13
  100. package/out/parser/string/shift/operand/enclosed.js.map +1 -1
  101. package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
  102. package/out/parser/string/shift/operand/genericArgs.js +6 -10
  103. package/out/parser/string/shift/operand/genericArgs.js.map +1 -1
  104. package/out/parser/string/shift/operand/operand.d.ts +3 -4
  105. package/out/parser/string/shift/operand/operand.js +8 -12
  106. package/out/parser/string/shift/operand/operand.js.map +1 -1
  107. package/out/parser/string/shift/operand/unenclosed.d.ts +15 -22
  108. package/out/parser/string/shift/operand/unenclosed.js +24 -36
  109. package/out/parser/string/shift/operand/unenclosed.js.map +1 -1
  110. package/out/parser/string/shift/operator/bounds.d.ts +9 -13
  111. package/out/parser/string/shift/operator/bounds.js +43 -54
  112. package/out/parser/string/shift/operator/bounds.js.map +1 -1
  113. package/out/parser/string/shift/operator/divisor.d.ts +2 -4
  114. package/out/parser/string/shift/operator/divisor.js +1 -3
  115. package/out/parser/string/shift/operator/divisor.js.map +1 -1
  116. package/out/parser/string/shift/operator/operator.d.ts +2 -3
  117. package/out/parser/string/shift/operator/operator.js +12 -19
  118. package/out/parser/string/shift/operator/operator.js.map +1 -1
  119. package/out/parser/string/shift/scanner.d.ts +2 -2
  120. package/out/parser/string/shift/scanner.js +14 -16
  121. package/out/parser/string/shift/scanner.js.map +1 -1
  122. package/out/parser/string/string.d.ts +8 -6
  123. package/out/parser/string/string.js +1 -3
  124. package/out/parser/string/string.js.map +1 -1
  125. package/out/parser/tuple.d.ts +102 -43
  126. package/out/parser/tuple.js +119 -87
  127. package/out/parser/tuple.js.map +1 -1
  128. package/out/scope.d.ts +43 -120
  129. package/out/scope.js +39 -209
  130. package/out/scope.js.map +1 -1
  131. package/out/type.d.ts +43 -64
  132. package/out/type.js +30 -131
  133. package/out/type.js.map +1 -1
  134. package/package.json +7 -3
  135. package/parser/definition.ts +77 -93
  136. package/parser/generic.ts +38 -40
  137. package/parser/objectLiteral.ts +172 -136
  138. package/parser/semantic/bounds.ts +33 -32
  139. package/parser/semantic/divisor.ts +8 -10
  140. package/parser/semantic/infer.ts +141 -0
  141. package/parser/semantic/utils.ts +12 -23
  142. package/parser/semantic/validate.ts +81 -73
  143. package/parser/string/reduce/dynamic.ts +67 -55
  144. package/parser/string/reduce/shared.ts +1 -3
  145. package/parser/string/reduce/static.ts +67 -69
  146. package/parser/string/shift/operand/date.ts +15 -14
  147. package/parser/string/shift/operand/enclosed.ts +30 -25
  148. package/parser/string/shift/operand/genericArgs.ts +46 -45
  149. package/parser/string/shift/operand/operand.ts +32 -38
  150. package/parser/string/shift/operand/unenclosed.ts +107 -158
  151. package/parser/string/shift/operator/bounds.ts +118 -112
  152. package/parser/string/shift/operator/divisor.ts +14 -17
  153. package/parser/string/shift/operator/operator.ts +35 -46
  154. package/parser/string/shift/scanner.ts +51 -50
  155. package/parser/string/string.ts +30 -35
  156. package/parser/tuple.ts +375 -307
  157. package/scope.ts +170 -453
  158. package/type.ts +193 -274
  159. package/__tests__/astUtils.test.ts +0 -17
  160. package/__tests__/cyclic.test.ts +0 -144
  161. package/__tests__/jsdoc.test.ts +0 -64
  162. package/__tests__/morph.test.ts +0 -409
  163. package/__tests__/nodeDefinitions.test.ts +0 -190
  164. package/__tests__/typeMethods.test.ts +0 -69
  165. package/out/ark.d.ts.map +0 -1
  166. package/out/arkConfig.d.ts +0 -2
  167. package/out/arkConfig.d.ts.map +0 -1
  168. package/out/arkConfig.js +0 -10
  169. package/out/arkConfig.js.map +0 -1
  170. package/out/main.d.ts.map +0 -1
  171. package/out/match.d.ts.map +0 -1
  172. package/out/parser/definition.d.ts.map +0 -1
  173. package/out/parser/generic.d.ts.map +0 -1
  174. package/out/parser/objectLiteral.d.ts.map +0 -1
  175. package/out/parser/semantic/bounds.d.ts.map +0 -1
  176. package/out/parser/semantic/divisor.d.ts.map +0 -1
  177. package/out/parser/semantic/intersections.d.ts +0 -14
  178. package/out/parser/semantic/intersections.d.ts.map +0 -1
  179. package/out/parser/semantic/intersections.js +0 -2
  180. package/out/parser/semantic/intersections.js.map +0 -1
  181. package/out/parser/semantic/semantic.d.ts +0 -33
  182. package/out/parser/semantic/semantic.d.ts.map +0 -1
  183. package/out/parser/semantic/semantic.js +0 -2
  184. package/out/parser/semantic/semantic.js.map +0 -1
  185. package/out/parser/semantic/utils.d.ts.map +0 -1
  186. package/out/parser/semantic/validate.d.ts.map +0 -1
  187. package/out/parser/shared.d.ts +0 -54
  188. package/out/parser/shared.d.ts.map +0 -1
  189. package/out/parser/shared.js +0 -47
  190. package/out/parser/shared.js.map +0 -1
  191. package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
  192. package/out/parser/string/reduce/shared.d.ts.map +0 -1
  193. package/out/parser/string/reduce/static.d.ts.map +0 -1
  194. package/out/parser/string/shift/operand/date.d.ts.map +0 -1
  195. package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
  196. package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
  197. package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
  198. package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  199. package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
  200. package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
  201. package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
  202. package/out/parser/string/shift/scanner.d.ts.map +0 -1
  203. package/out/parser/string/string.d.ts.map +0 -1
  204. package/out/parser/tuple.d.ts.map +0 -1
  205. package/out/scope.d.ts.map +0 -1
  206. package/out/test.d.ts +0 -2
  207. package/out/test.d.ts.map +0 -1
  208. package/out/test.js +0 -7
  209. package/out/test.js.map +0 -1
  210. package/out/tsconfig.build.tsbuildinfo +0 -1
  211. package/out/type.d.ts.map +0 -1
  212. package/out/util.d.ts +0 -19
  213. package/out/util.d.ts.map +0 -1
  214. package/out/util.js +0 -7
  215. package/out/util.js.map +0 -1
  216. package/parser/semantic/intersections.ts +0 -46
  217. package/parser/semantic/semantic.ts +0 -144
  218. package/parser/shared.ts +0 -152
  219. package/util.ts +0 -36
package/scope.ts CHANGED
@@ -1,98 +1,86 @@
1
1
  import {
2
- BaseType,
3
- keywords,
4
- type ArkErrorCode,
5
- type KeyCheckKind,
6
- type TypeNode,
7
- type extractIn,
8
- type extractOut
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
- map,
15
- throwParseError,
16
- type Dict,
17
- type evaluate,
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 { createMatchParser, type MatchParser } from "./match.js"
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
- type validateDefinition
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
- type Generic,
49
- type GenericProps,
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 validateScope<def, $> = {
78
- [k in keyof def]: parseScopeKey<k>["params"] extends []
79
- ? // Not including Type here directly breaks inference
80
- def[k] extends Type | PreparsedResolution
81
- ? def[k]
82
- : validateDefinition<def[k], $ & bootstrap<def>, {}>
83
- : parseScopeKey<k>["params"] extends GenericParamsParseError
84
- ? // use the full nominal type here to avoid an overlap between the
85
- // error message and a possible value for the property
86
- parseScopeKey<k>["params"][0]
87
- : validateDefinition<
88
- def[k],
89
- $ & bootstrap<def>,
90
- {
91
- // once we support constraints on generic parameters, we'd use
92
- // the base type here: https://github.com/arktypeio/arktype/issues/796
93
- [param in parseScopeKey<k>["params"][number]]: unknown
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 = Module | GenericProps
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
- ? def[k]
128
- : def[k] extends (() => infer thunkReturn extends PreparsedResolution)
129
- ? thunkReturn
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>]: Generic<
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<r extends Resolutions> = evaluate<{
140
- [name in keyof r["exports"]]: r["exports"][name] extends Def<infer def>
141
- ? inferDefinition<def, $<r>, {}>
142
- : r["exports"][name] extends GenericProps<infer params, infer def>
143
- ? // add the scope in which the generic was defined here
144
- Generic<params, def, $<r>>
145
- : // otherwise should be a submodule
146
- r["exports"][name]
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> = k extends GenericDeclaration<infer name>
150
- ? name
151
- : never
152
-
153
- type extractGenericParameters<k> = k extends GenericDeclaration<
154
- string,
155
- infer params
156
- >
157
- ? params
158
- : never
159
-
160
- type extractPrivateKey<k> = k extends PrivateDeclaration<infer key>
161
- ? key
162
- : never
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
- : never
139
+ : never
177
140
 
178
141
  export type moduleKeyOf<$> = {
179
- [k in keyof $]: $[k] extends Module ? k & string : never
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
- ? subalias extends keyof $[submodule]
185
- ? $[submodule][subalias] extends Type<infer t>
186
- ? t
187
- : never
188
- : never
189
- : never
190
-
191
- type $<r extends Resolutions> = r["exports"] & r["locals"] & r["ambient"]
192
-
193
- type exportedName<r extends Resolutions> = keyof r["exports"] & string
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 type Resolutions = {
211
- exports: unknown
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 type ParseContext = {
217
- baseName: string
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
- type MergedResolutions = Record<string, TypeNode | Generic>
166
+ match: MatchParser<$>
224
167
 
225
- type ParseContextInput = Pick<ParseContext, "baseName" | "args">
168
+ declare: DeclarationParser<$>
226
169
 
227
- export class Scope<r extends Resolutions = any> {
228
- declare infer: extractOut<r["exports"]>
229
- declare inferIn: extractIn<r["exports"]>
170
+ define: DefinitionParser<$>
230
171
 
231
- config: TypeConfig
172
+ import<names extends exportedNameOf<$>[]>(
173
+ ...names: names
174
+ ): Module<show<destructuredImportContext<$, names>>>
232
175
 
233
- private parseCache: Record<string, TypeNode> = {}
234
- private resolutions: MergedResolutions
176
+ export<names extends exportedNameOf<$>[]>(
177
+ ...names: names
178
+ ): Module<show<destructuredExportContext<$, names>>>
179
+ }
235
180
 
236
- /** The set of names defined at the root-level of the scope mapped to their
237
- * corresponding definitions.**/
238
- aliases: Record<string, unknown> = {}
239
- private exportedNames: exportedName<r>[] = []
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: Dict, config: ScopeConfig) {
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
- this.aliases[parsedKey.name] = parsedKey.params.length
246
- ? generic(parsedKey.params, def[k], this)
247
- : def[k]
248
- if (!parsedKey.isLocal) {
249
- this.exportedNames.push(parsedKey.name as never)
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
- this.ambient = config.ambient ?? null
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
- static root: ScopeParser<{}, {}> = (aliases) => {
264
- return new Scope(aliases, {}) as never
265
- }
199
+ type = new RawTypeParser(this as never)
266
200
 
267
- type: TypeParser<$<r>> = createTypeParser(this as never) as never
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
- // TODO: name?
300
- get<name extends keyof r["exports"] & string>(
301
- name: name
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
- private createRootContext(input: ParseContextInput): ParseContext {
307
- return {
308
- path: [],
309
- scope: this,
310
- ...input
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
- parseDefinition(def: unknown, input: ParseContextInput) {
315
- return this.parse(def, this.createRootContext(input))
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): TypeNode {
225
+ parse(def: unknown, ctx: ParseContext): RawSchema {
319
226
  if (typeof def === "string") {
320
- if (ctx.args !== undefined) {
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
- ? parseObject(def, ctx)
332
- : throwParseError(writeBadDefinitionTypeMessage(domainOf(def)))
237
+ return hasDomain(def, "object") ?
238
+ parseObject(def, ctx)
239
+ : throwParseError(writeBadDefinitionTypeMessage(domainOf(def)))
333
240
  }
334
241
 
335
- parseString(def: string, ctx: ParseContext): TypeNode {
242
+ parseString(def: string, ctx: ParseContext): RawSchema {
336
243
  return (
337
- this.maybeResolveNode(def) ??
244
+ this.maybeResolveSchema(def) ??
338
245
  ((def.endsWith("[]") &&
339
- this.maybeResolveNode(def.slice(0, -2))?.array()) ||
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
- type destructuredImportContext<
513
- r extends Resolutions,
514
- name extends exportedName<r>
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
- isLocal,
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
- isLocal,
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> = k extends PrivateDeclaration<infer inner>
558
- ? parsePossibleGenericDeclaration<inner, true>
559
- : parsePossibleGenericDeclaration<k, false>
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
+ }