arktype 2.0.0-dev.1 → 2.0.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.
Files changed (47) hide show
  1. package/__tests__/array.test.ts +42 -4
  2. package/__tests__/config.test.ts +75 -166
  3. package/__tests__/declared.test.ts +5 -4
  4. package/__tests__/keyTraversal.test.ts +71 -70
  5. package/__tests__/match.bench.ts +31 -0
  6. package/__tests__/match.test.ts +216 -26
  7. package/__tests__/narrow.test.ts +3 -3
  8. package/__tests__/objectLiteral.test.ts +19 -18
  9. package/match.ts +206 -79
  10. package/out/match.d.ts +60 -31
  11. package/out/match.d.ts.map +1 -1
  12. package/out/match.js +52 -10
  13. package/out/match.js.map +1 -1
  14. package/out/parser/definition.d.ts +6 -4
  15. package/out/parser/definition.d.ts.map +1 -1
  16. package/out/parser/definition.js.map +1 -1
  17. package/out/parser/objectLiteral.js +2 -2
  18. package/out/parser/objectLiteral.js.map +1 -1
  19. package/out/parser/semantic/semantic.d.ts +3 -3
  20. package/out/parser/semantic/semantic.d.ts.map +1 -1
  21. package/out/parser/shared.d.ts +4 -2
  22. package/out/parser/shared.d.ts.map +1 -1
  23. package/out/parser/shared.js +8 -0
  24. package/out/parser/shared.js.map +1 -1
  25. package/out/parser/string/reduce/static.d.ts +1 -1
  26. package/out/parser/string/reduce/static.d.ts.map +1 -1
  27. package/out/parser/tuple.d.ts +2 -2
  28. package/out/parser/tuple.d.ts.map +1 -1
  29. package/out/parser/tuple.js +14 -15
  30. package/out/parser/tuple.js.map +1 -1
  31. package/out/scope.d.ts +6 -11
  32. package/out/scope.d.ts.map +1 -1
  33. package/out/scope.js +0 -21
  34. package/out/scope.js.map +1 -1
  35. package/out/type.d.ts +5 -8
  36. package/out/type.d.ts.map +1 -1
  37. package/out/type.js +12 -8
  38. package/out/type.js.map +1 -1
  39. package/package.json +3 -3
  40. package/parser/definition.ts +7 -7
  41. package/parser/objectLiteral.ts +6 -6
  42. package/parser/semantic/semantic.ts +3 -4
  43. package/parser/shared.ts +19 -4
  44. package/parser/string/reduce/static.ts +2 -2
  45. package/parser/tuple.ts +18 -17
  46. package/scope.ts +5 -31
  47. package/type.ts +16 -14
package/parser/tuple.ts CHANGED
@@ -3,11 +3,11 @@ import {
3
3
  schema,
4
4
  type BaseMeta,
5
5
  type Morph,
6
+ type MorphChildKind,
6
7
  type Out,
7
8
  type Predicate,
8
9
  type Schema,
9
10
  type TypeNode,
10
- type ValidatorKind,
11
11
  type extractIn,
12
12
  type extractOut,
13
13
  type inferMorphOut,
@@ -32,6 +32,7 @@ import type { inferIntersection } from "./semantic/intersections.js"
32
32
  import type { InfixOperator, PostfixExpression } from "./semantic/semantic.js"
33
33
  import { writeUnsatisfiableExpressionError } from "./semantic/validate.js"
34
34
  import {
35
+ configureShallowDescendants,
35
36
  parseEntry,
36
37
  type EntryParseResult,
37
38
  type validateObjectValue
@@ -44,30 +45,33 @@ export const parseTuple = (def: List, ctx: ParseContext) =>
44
45
 
45
46
  export const parseTupleLiteral = (def: List, ctx: ParseContext): TypeNode => {
46
47
  const props: unknown[] = []
47
- let isVariadic = false
48
+ let variadicIndex: number | undefined
48
49
  for (let i = 0; i < def.length; i++) {
49
50
  let elementDef = def[i]
50
51
  ctx.path.push(`${i}`)
51
52
  if (typeof elementDef === "string" && elementDef.startsWith("...")) {
52
53
  elementDef = elementDef.slice(3)
53
- isVariadic = true
54
+ if (variadicIndex !== undefined) {
55
+ return throwParseError(multipleVariadicMesage)
56
+ }
57
+ variadicIndex = i
54
58
  } else if (
55
59
  isArray(elementDef) &&
56
60
  elementDef.length === 2 &&
57
61
  elementDef[0] === "..."
58
62
  ) {
59
63
  elementDef = elementDef[1]
60
- isVariadic = true
64
+ if (variadicIndex !== undefined) {
65
+ return throwParseError(multipleVariadicMesage)
66
+ }
67
+ variadicIndex = i
61
68
  }
62
69
  const parsedEntry = parseEntry([`${i}`, elementDef])
63
70
  const value = ctx.scope.parse(parsedEntry.innerValue, ctx)
64
- if (isVariadic) {
71
+ if (variadicIndex === i) {
65
72
  if (!value.extends(keywords.Array)) {
66
73
  return throwParseError(writeNonArrayRestMessage(elementDef))
67
74
  }
68
- if (i !== def.length - 1) {
69
- return throwParseError(prematureRestMessage)
70
- }
71
75
  // TODO: Fix builtins.arrayIndexTypeNode()
72
76
  const elementType = value.getPath()
73
77
  // TODO: first variadic i
@@ -84,18 +88,16 @@ export const parseTupleLiteral = (def: List, ctx: ParseContext): TypeNode => {
84
88
  }
85
89
  ctx.path.pop()
86
90
  }
87
- if (!isVariadic) {
91
+ if (variadicIndex === undefined) {
88
92
  props.push({
89
93
  key: {
90
94
  name: "length",
91
95
  prerequisite: true,
92
96
  optional: false
93
97
  },
94
- // , ctx
95
98
  value: schema({ unit: def.length })
96
99
  })
97
100
  }
98
- // props , ctx
99
101
  return schema(Array)
100
102
  }
101
103
 
@@ -201,9 +203,9 @@ type writeNonArrayRestMessage<operand> = `Rest element ${operand extends string
201
203
  ? `'${operand}'`
202
204
  : ""} must be an array`
203
205
 
204
- export const prematureRestMessage = `Rest elements are only allowed at the end of a tuple`
206
+ export const multipleVariadicMesage = `A tuple may have at most one variadic element`
205
207
 
206
- type prematureRestMessage = typeof prematureRestMessage
208
+ type prematureRestMessage = typeof multipleVariadicMesage
207
209
 
208
210
  type inferTupleLiteral<
209
211
  def extends List,
@@ -387,7 +389,7 @@ export const parseMorphTuple: PostfixParser<"=>"> = (def, ctx) => {
387
389
  }
388
390
  // TODO: nested morphs?
389
391
  return schema({
390
- in: ctx.scope.parse(def[0], ctx) as Schema<ValidatorKind>,
392
+ in: ctx.scope.parse(def[0], ctx) as Schema<MorphChildKind>,
391
393
  morph: def[2] as Morph
392
394
  })
393
395
  }
@@ -417,9 +419,8 @@ export const parseNarrowTuple: PostfixParser<":"> = (def, ctx) => {
417
419
  .constrain("predicate", def[2] as Predicate)
418
420
  }
419
421
 
420
- const parseAttributeTuple: PostfixParser<"@"> = (def, ctx) => {
421
- return ctx.scope.parse(def[0], ctx)
422
- }
422
+ const parseAttributeTuple: PostfixParser<"@"> = (def, ctx) =>
423
+ configureShallowDescendants(ctx.scope.parse(def[0], ctx), def[2] as never)
423
424
 
424
425
  const indexOneParsers: {
425
426
  [token in IndexOneOperator]: PostfixParser<token>
package/scope.ts CHANGED
@@ -1,8 +1,7 @@
1
1
  import {
2
2
  BaseType,
3
3
  keywords,
4
- type ArkErrorCode,
5
- type KeyCheckKind,
4
+ type ArkConfig,
6
5
  type TypeNode,
7
6
  type extractIn,
8
7
  type extractOut
@@ -47,7 +46,6 @@ import {
47
46
  type DefinitionParser,
48
47
  type Generic,
49
48
  type GenericProps,
50
- type TypeConfig,
51
49
  type TypeParser
52
50
  } from "./type.js"
53
51
  import { addArkKind, hasArkKind, type arkKind } from "./util.js"
@@ -68,10 +66,8 @@ export type ScopeParser<parent, ambient> = {
68
66
  }>
69
67
  }
70
68
 
71
- export type ScopeConfig = {
69
+ export interface ArkScopeConfig extends ArkConfig {
72
70
  ambient?: Scope | null
73
- codes?: Record<ArkErrorCode, { mustBe?: string }>
74
- keys?: KeyCheckKind
75
71
  }
76
72
 
77
73
  type validateScope<def, $> = {
@@ -228,7 +224,7 @@ export class Scope<r extends Resolutions = any> {
228
224
  declare infer: extractOut<r["exports"]>
229
225
  declare inferIn: extractIn<r["exports"]>
230
226
 
231
- config: TypeConfig
227
+ config: ArkScopeConfig
232
228
 
233
229
  private parseCache: Record<string, TypeNode> = {}
234
230
  private resolutions: MergedResolutions
@@ -239,7 +235,7 @@ export class Scope<r extends Resolutions = any> {
239
235
  private exportedNames: exportedName<r>[] = []
240
236
  private ambient: Scope | null
241
237
 
242
- constructor(def: Dict, config: ScopeConfig) {
238
+ constructor(def: Dict, config: ArkScopeConfig) {
243
239
  for (const k in def) {
244
240
  const parsedKey = parseScopeKey(k)
245
241
  this.aliases[parsedKey.name] = parsedKey.params.length
@@ -273,7 +269,7 @@ export class Scope<r extends Resolutions = any> {
273
269
 
274
270
  scope: ScopeParser<r["exports"], r["ambient"]> = ((
275
271
  def: Dict,
276
- config: TypeConfig = {}
272
+ config: ArkScopeConfig = {}
277
273
  ) => {
278
274
  return new Scope(def, {
279
275
  ambient: this.ambient,
@@ -411,28 +407,6 @@ export class Scope<r extends Resolutions = any> {
411
407
  ) as never
412
408
  }
413
409
 
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
410
  bindThis() {
437
411
  // TODO: fix
438
412
  return { this: keywords.unknown }
package/type.ts CHANGED
@@ -3,7 +3,6 @@ import {
3
3
  keywords,
4
4
  type ArkResult,
5
5
  type BaseMeta,
6
- type KeyCheckKind,
7
6
  type Morph,
8
7
  type Out,
9
8
  type Predicate,
@@ -17,7 +16,6 @@ import {
17
16
  } from "@arktype/schema"
18
17
  import {
19
18
  Callable,
20
- CastableBase,
21
19
  map,
22
20
  type Constructor,
23
21
  type Json,
@@ -33,6 +31,7 @@ import {
33
31
  type GenericParamsParseError
34
32
  } from "./parser/generic.js"
35
33
  import type { inferIntersection } from "./parser/semantic/intersections.js"
34
+ import { configureShallowDescendants } from "./parser/shared.js"
36
35
  import type {
37
36
  IndexOneOperator,
38
37
  IndexZeroOperator,
@@ -121,11 +120,6 @@ export type DefinitionParser<$> = <def>(
121
120
  def: validateDefinition<def, $, bindThis<def>>
122
121
  ) => def
123
122
 
124
- export type TypeConfig = {
125
- keys?: KeyCheckKind
126
- mustBe?: string
127
- }
128
-
129
123
  export class Type<t = unknown, $ = any> extends Callable<
130
124
  (data: unknown) => ArkResult<distill<extractOut<t>>>
131
125
  > {
@@ -133,10 +127,9 @@ export class Type<t = unknown, $ = any> extends Callable<
133
127
  // TODO: in/out?
134
128
  declare infer: distill<extractOut<t>>
135
129
 
136
- config: TypeConfig
137
130
  root: TypeNode<t>
138
131
  allows: this["root"]["allows"]
139
- expected: string
132
+ description: string
140
133
  json: Json
141
134
 
142
135
  constructor(
@@ -147,14 +140,19 @@ export class Type<t = unknown, $ = any> extends Callable<
147
140
  super(root.apply, root)
148
141
  this.root = root
149
142
  this.allows = root.allows
150
- this.config = scope.config
151
143
  this.json = root.json
152
- this.expected = this.root.description
144
+ this.description = this.root.description
153
145
  }
154
146
 
155
- configure(config: TypeConfig) {
156
- this.config = { ...this.config, ...config }
157
- return this
147
+ configure(configOrDescription: BaseMeta | string): this {
148
+ return new Type(
149
+ configureShallowDescendants(this.root, configOrDescription),
150
+ this.scope
151
+ ) as never
152
+ }
153
+
154
+ describe(description: string): this {
155
+ return this.configure(description)
158
156
  }
159
157
 
160
158
  // TODO: should return out
@@ -252,6 +250,10 @@ export class Type<t = unknown, $ = any> extends Callable<
252
250
  this.outCache ??= new Type(this.root.out, this.scope) as never
253
251
  return this.outCache
254
252
  }
253
+
254
+ toString() {
255
+ return this.description
256
+ }
255
257
  }
256
258
 
257
259
  const parseTypeRoot = (def: unknown, scope: Scope, args?: BoundArgs) =>