arktype 0.2.0 → 0.4.0

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 (138) hide show
  1. package/dist/api.cjs +9 -0
  2. package/dist/api.d.ts +10 -0
  3. package/dist/api.mjs +1 -0
  4. package/dist/cjs/nodes/{branches.js → branch.js} +65 -10
  5. package/dist/cjs/nodes/compose.js +2 -3
  6. package/dist/cjs/nodes/discriminate.js +9 -15
  7. package/dist/cjs/nodes/node.js +21 -11
  8. package/dist/cjs/nodes/predicate.js +8 -10
  9. package/dist/cjs/nodes/rules/class.js +10 -4
  10. package/dist/cjs/nodes/rules/divisor.js +1 -5
  11. package/dist/cjs/nodes/rules/props.js +75 -25
  12. package/dist/cjs/nodes/rules/range.js +2 -6
  13. package/dist/cjs/nodes/rules/regex.js +1 -5
  14. package/dist/cjs/nodes/rules/rules.js +23 -63
  15. package/dist/cjs/parse/ast/ast.js +4 -0
  16. package/dist/cjs/parse/ast/bound.js +9 -0
  17. package/dist/cjs/parse/{tuple/utils.js → ast/distributableFunction.js} +1 -1
  18. package/dist/cjs/parse/ast/divisor.js +9 -0
  19. package/dist/cjs/parse/{string/ast.js → ast/intersection.js} +2 -8
  20. package/dist/cjs/parse/ast/keyof.js +109 -0
  21. package/dist/cjs/parse/{tuple → ast}/morph.js +2 -2
  22. package/dist/cjs/parse/{tuple → ast}/narrow.js +2 -2
  23. package/dist/cjs/parse/{tuple → ast}/tuple.js +23 -16
  24. package/dist/cjs/parse/ast/union.js +9 -0
  25. package/dist/cjs/parse/ast/utils.js +4 -0
  26. package/dist/cjs/parse/definition.js +12 -2
  27. package/dist/cjs/parse/string/reduce/dynamic.js +7 -8
  28. package/dist/cjs/parse/string/shift/operator/bounds.js +4 -4
  29. package/dist/cjs/parse/string/shift/operator/divisor.js +3 -3
  30. package/dist/cjs/parse/string/string.js +2 -2
  31. package/dist/cjs/scopes/expressions.js +29 -0
  32. package/dist/cjs/scopes/jsObjects.js +14 -14
  33. package/dist/cjs/scopes/scope.js +56 -4
  34. package/dist/cjs/scopes/type.js +4 -7
  35. package/dist/cjs/traverse/problems.js +67 -53
  36. package/dist/cjs/traverse/traverse.js +103 -135
  37. package/dist/cjs/utils/data.js +39 -0
  38. package/dist/cjs/utils/generics.js +20 -2
  39. package/dist/cjs/utils/numericLiterals.js +2 -0
  40. package/dist/cjs/utils/objectKinds.js +1 -2
  41. package/dist/mjs/nodes/{branches.js → branch.js} +54 -2
  42. package/dist/mjs/nodes/compose.js +3 -4
  43. package/dist/mjs/nodes/discriminate.js +9 -15
  44. package/dist/mjs/nodes/node.js +17 -8
  45. package/dist/mjs/nodes/predicate.js +4 -5
  46. package/dist/mjs/nodes/rules/class.js +10 -4
  47. package/dist/mjs/nodes/rules/divisor.js +1 -5
  48. package/dist/mjs/nodes/rules/props.js +74 -27
  49. package/dist/mjs/nodes/rules/range.js +2 -6
  50. package/dist/mjs/nodes/rules/regex.js +1 -5
  51. package/dist/mjs/nodes/rules/rules.js +23 -62
  52. package/dist/mjs/parse/ast/ast.js +1 -0
  53. package/dist/mjs/parse/ast/bound.js +1 -0
  54. package/dist/mjs/parse/{tuple/utils.js → ast/distributableFunction.js} +2 -2
  55. package/dist/mjs/parse/ast/divisor.js +1 -0
  56. package/dist/mjs/parse/{string/ast.js → ast/intersection.js} +2 -5
  57. package/dist/mjs/parse/ast/keyof.js +101 -0
  58. package/dist/mjs/parse/{tuple → ast}/morph.js +2 -2
  59. package/dist/mjs/parse/ast/narrow.js +7 -0
  60. package/dist/mjs/parse/{tuple → ast}/tuple.js +24 -17
  61. package/dist/mjs/parse/ast/union.js +1 -0
  62. package/dist/mjs/parse/ast/utils.js +1 -0
  63. package/dist/mjs/parse/definition.js +12 -2
  64. package/dist/mjs/parse/string/reduce/dynamic.js +8 -9
  65. package/dist/mjs/parse/string/shift/operator/bounds.js +4 -4
  66. package/dist/mjs/parse/string/shift/operator/divisor.js +3 -3
  67. package/dist/mjs/parse/string/string.js +2 -2
  68. package/dist/mjs/scopes/expressions.js +9 -0
  69. package/dist/mjs/scopes/jsObjects.js +14 -14
  70. package/dist/mjs/scopes/scope.js +55 -3
  71. package/dist/mjs/scopes/type.js +4 -7
  72. package/dist/mjs/traverse/problems.js +68 -53
  73. package/dist/mjs/traverse/traverse.js +107 -139
  74. package/dist/mjs/utils/data.js +24 -0
  75. package/dist/mjs/utils/generics.js +18 -1
  76. package/dist/mjs/utils/numericLiterals.js +1 -0
  77. package/dist/mjs/utils/objectKinds.js +1 -1
  78. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  79. package/dist/types/nodes/branch.d.ts +27 -0
  80. package/dist/types/nodes/compose.d.ts +5 -10
  81. package/dist/types/nodes/discriminate.d.ts +3 -3
  82. package/dist/types/nodes/infer.d.ts +21 -20
  83. package/dist/types/nodes/node.d.ts +12 -16
  84. package/dist/types/nodes/predicate.d.ts +4 -59
  85. package/dist/types/nodes/rules/class.d.ts +3 -2
  86. package/dist/types/nodes/rules/divisor.d.ts +2 -1
  87. package/dist/types/nodes/rules/props.d.ts +19 -11
  88. package/dist/types/nodes/rules/range.d.ts +3 -2
  89. package/dist/types/nodes/rules/regex.d.ts +2 -1
  90. package/dist/types/nodes/rules/rules.d.ts +12 -14
  91. package/dist/types/parse/ast/ast.d.ts +24 -0
  92. package/dist/types/parse/ast/bound.d.ts +10 -0
  93. package/dist/types/parse/{tuple/utils.d.ts → ast/distributableFunction.d.ts} +2 -3
  94. package/dist/types/parse/ast/divisor.d.ts +8 -0
  95. package/dist/types/parse/ast/intersection.d.ts +17 -0
  96. package/dist/types/parse/{tuple → ast}/keyof.d.ts +4 -4
  97. package/dist/types/parse/{tuple → ast}/morph.d.ts +3 -3
  98. package/dist/types/parse/ast/narrow.d.ts +21 -0
  99. package/dist/types/parse/ast/tuple.d.ts +40 -0
  100. package/dist/types/parse/ast/union.d.ts +12 -0
  101. package/dist/types/parse/ast/utils.d.ts +7 -0
  102. package/dist/types/parse/definition.d.ts +9 -7
  103. package/dist/types/parse/record.d.ts +2 -2
  104. package/dist/types/parse/string/reduce/dynamic.d.ts +1 -1
  105. package/dist/types/parse/string/reduce/shared.d.ts +6 -5
  106. package/dist/types/parse/string/reduce/static.d.ts +11 -6
  107. package/dist/types/parse/string/shift/operand/unenclosed.d.ts +2 -2
  108. package/dist/types/parse/string/shift/operator/bounds.d.ts +4 -3
  109. package/dist/types/parse/string/shift/operator/divisor.d.ts +5 -1
  110. package/dist/types/parse/string/string.d.ts +2 -3
  111. package/dist/types/scopes/expressions.d.ts +48 -0
  112. package/dist/types/scopes/scope.d.ts +17 -7
  113. package/dist/types/scopes/type.d.ts +13 -15
  114. package/dist/types/traverse/problems.d.ts +17 -41
  115. package/dist/types/traverse/traverse.d.ts +85 -52
  116. package/dist/types/utils/data.d.ts +13 -0
  117. package/dist/types/utils/domains.d.ts +1 -1
  118. package/dist/types/utils/generics.d.ts +13 -10
  119. package/dist/types/utils/numericLiterals.d.ts +1 -0
  120. package/dist/types/utils/objectKinds.d.ts +43 -43
  121. package/dist/types/utils/paths.d.ts +2 -0
  122. package/package.json +1 -2
  123. package/dist/cjs/nodes/functors.js +0 -35
  124. package/dist/cjs/nodes/rules/objectKind.js +0 -78
  125. package/dist/cjs/parse/tuple/keyof.js +0 -45
  126. package/dist/cjs/utils/size.js +0 -16
  127. package/dist/mjs/nodes/functors.js +0 -27
  128. package/dist/mjs/nodes/rules/objectKind.js +0 -63
  129. package/dist/mjs/parse/tuple/keyof.js +0 -37
  130. package/dist/mjs/parse/tuple/narrow.js +0 -7
  131. package/dist/mjs/utils/size.js +0 -2
  132. package/dist/types/nodes/branches.d.ts +0 -14
  133. package/dist/types/nodes/functors.d.ts +0 -6
  134. package/dist/types/nodes/rules/objectKind.d.ts +0 -15
  135. package/dist/types/parse/string/ast.d.ts +0 -45
  136. package/dist/types/parse/tuple/narrow.d.ts +0 -13
  137. package/dist/types/parse/tuple/tuple.d.ts +0 -31
  138. package/dist/types/utils/size.d.ts +0 -3
@@ -3,9 +3,9 @@ import { domainOf } from "../utils/domains.js";
3
3
  import { throwParseError } from "../utils/errors.js";
4
4
  import { objectKindOf } from "../utils/objectKinds.js";
5
5
  import { stringify } from "../utils/serialize.js";
6
+ import { parseTuple } from "./ast/tuple.js";
6
7
  import { parseRecord } from "./record.js";
7
8
  import { parseString } from "./string/string.js";
8
- import { parseTuple } from "./tuple/tuple.js";
9
9
  export const parseDefinition = (def, ctx)=>{
10
10
  const domain = domainOf(def);
11
11
  if (domain === "string") {
@@ -27,11 +27,21 @@ export const parseDefinition = (def, ctx)=>{
27
27
  }
28
28
  };
29
29
  case "Function":
30
- return isType(def) ? def.node : throwParseError(writeBadDefinitionTypeMessage("Function"));
30
+ if (isType(def)) {
31
+ return def.node;
32
+ }
33
+ if (isThunk(def)) {
34
+ const returned = def();
35
+ if (isType(returned)) {
36
+ return returned.node;
37
+ }
38
+ }
39
+ return throwParseError(writeBadDefinitionTypeMessage("Function"));
31
40
  default:
32
41
  return throwParseError(writeBadDefinitionTypeMessage(objectKind ?? stringify(def)));
33
42
  }
34
43
  };
35
44
  export const as = Symbol("as");
36
45
  export const unknownDefinitionMessage = "Cannot statically parse a definition inferred as unknown. Consider using 'as inferred<...>' to cast it.";
46
+ const isThunk = (def)=>typeof def === "function" && def.length === 0;
37
47
  export const writeBadDefinitionTypeMessage = (actual)=>`Type definitions must be strings or objects (was ${actual})`;
@@ -1,5 +1,4 @@
1
- import { functors } from "../../../nodes/functors.js";
2
- import { intersection, isLiteralNode, union } from "../../../nodes/node.js";
1
+ import { isLiteralNode, rootIntersection, rootUnion, toArrayNode } from "../../../nodes/node.js";
3
2
  import { minComparators } from "../../../nodes/rules/range.js";
4
3
  import { throwInternalError, throwParseError } from "../../../utils/errors.js";
5
4
  import { isKeyOf } from "../../../utils/generics.js";
@@ -52,10 +51,10 @@ export class DynamicState {
52
51
  this.root = node;
53
52
  }
54
53
  rootToArray() {
55
- this.root = functors.Array(this.ejectRoot());
54
+ this.root = toArrayNode(this.ejectRoot());
56
55
  }
57
56
  intersect(node) {
58
- this.root = intersection(this.ejectRoot(), node, this.ctx.type);
57
+ this.root = rootIntersection(this.ejectRoot(), node, this.ctx.type);
59
58
  }
60
59
  ejectRoot() {
61
60
  this.assertHasRoot();
@@ -82,7 +81,7 @@ export class DynamicState {
82
81
  return this.error(writeUnpairableComparatorMessage(comparator));
83
82
  }
84
83
  if (this.branches.range) {
85
- return this.error(writeMultipleLeftBoundsMessage(this.branches.range.limit, this.branches.range.comparator, limit, invertedComparator));
84
+ return this.error(writeMultipleLeftBoundsMessage(`${this.branches.range.limit}`, this.branches.range.comparator, `${limit}`, invertedComparator));
86
85
  }
87
86
  this.branches.range = {
88
87
  limit,
@@ -95,7 +94,7 @@ export class DynamicState {
95
94
  this.pushRootToBranch("|");
96
95
  this.setRoot(this.branches.union);
97
96
  } else if (this.branches.intersection) {
98
- this.setRoot(intersection(this.branches.intersection, this.ejectRoot(), this.ctx.type));
97
+ this.setRoot(rootIntersection(this.branches.intersection, this.ejectRoot(), this.ctx.type));
99
98
  }
100
99
  }
101
100
  finalizeGroup() {
@@ -108,15 +107,15 @@ export class DynamicState {
108
107
  }
109
108
  pushRootToBranch(token) {
110
109
  this.assertRangeUnset();
111
- this.branches.intersection = this.branches.intersection ? intersection(this.branches.intersection, this.ejectRoot(), this.ctx.type) : this.ejectRoot();
110
+ this.branches.intersection = this.branches.intersection ? rootIntersection(this.branches.intersection, this.ejectRoot(), this.ctx.type) : this.ejectRoot();
112
111
  if (token === "|") {
113
- this.branches.union = this.branches.union ? union(this.branches.union, this.branches.intersection, this.ctx.type) : this.branches.intersection;
112
+ this.branches.union = this.branches.union ? rootUnion(this.branches.union, this.branches.intersection, this.ctx.type) : this.branches.intersection;
114
113
  delete this.branches.intersection;
115
114
  }
116
115
  }
117
116
  assertRangeUnset() {
118
117
  if (this.branches.range) {
119
- return this.error(writeOpenRangeMessage(this.branches.range.limit, this.branches.range.comparator));
118
+ return this.error(writeOpenRangeMessage(`${this.branches.range.limit}`, this.branches.range.comparator));
120
119
  }
121
120
  }
122
121
  reduceGroupOpen() {
@@ -1,9 +1,9 @@
1
1
  import { stringifyRange } from "../../../../nodes/compose.js";
2
2
  import { compareStrictness, maxComparators, minComparators } from "../../../../nodes/rules/range.js";
3
3
  import { throwInternalError } from "../../../../utils/errors.js";
4
- import { hasKey, isKeyOf, keysOf, listFrom } from "../../../../utils/generics.js";
4
+ import { hasKey, isKeyOf, listFrom, objectKeysOf } from "../../../../utils/generics.js";
5
5
  import { tryParseWellFormedNumber } from "../../../../utils/numericLiterals.js";
6
- import { writeUnboundableMessage } from "../../ast.js";
6
+ import { writeUnboundableMessage } from "../../../ast/bound.js";
7
7
  import { writeUnpairableComparatorMessage } from "../../reduce/shared.js";
8
8
  import { Scanner } from "../scanner.js";
9
9
  export const parseBound = (s, start)=>{
@@ -36,7 +36,7 @@ export const parseRightBound = (s, comparator)=>{
36
36
  };
37
37
  const distributeRange = (range, s)=>{
38
38
  const resolution = s.resolveRoot();
39
- const domains = keysOf(resolution);
39
+ const domains = objectKeysOf(resolution);
40
40
  const distributedRange = {};
41
41
  const rangePredicate = {
42
42
  range
@@ -54,7 +54,7 @@ const distributeRange = (range, s)=>{
54
54
  if (resolution.object === true) {
55
55
  return false;
56
56
  }
57
- return listFrom(resolution.object).every((branch)=>hasKey(branch, "objectKind") && (branch.objectKind === "Array" || branch.objectKind[0] === "Array"));
57
+ return listFrom(resolution.object).every((branch)=>hasKey(branch, "class") && branch.class === "Array");
58
58
  default:
59
59
  return false;
60
60
  }
@@ -1,13 +1,13 @@
1
- import { keysOf } from "../../../../utils/generics.js";
1
+ import { objectKeysOf } from "../../../../utils/generics.js";
2
2
  import { tryParseWellFormedInteger } from "../../../../utils/numericLiterals.js";
3
- import { writeIndivisibleMessage } from "../../ast.js";
3
+ import { writeIndivisibleMessage } from "../../../ast/divisor.js";
4
4
  export const parseDivisor = (s)=>{
5
5
  const divisorToken = s.scanner.shiftUntilNextTerminator();
6
6
  const divisor = tryParseWellFormedInteger(divisorToken, writeInvalidDivisorMessage(divisorToken));
7
7
  if (divisor === 0) {
8
8
  s.error(writeInvalidDivisorMessage(0));
9
9
  }
10
- const rootDomains = keysOf(s.resolveRoot());
10
+ const rootDomains = objectKeysOf(s.resolveRoot());
11
11
  if (rootDomains.length === 1 && rootDomains[0] === "number") {
12
12
  s.intersect({
13
13
  number: {
@@ -1,4 +1,4 @@
1
- import { functors } from "../../nodes/functors.js";
1
+ import { toArrayNode } from "../../nodes/node.js";
2
2
  import { DynamicState } from "./reduce/dynamic.js";
3
3
  import { parseOperand } from "./shift/operand/operand.js";
4
4
  import { parseOperator } from "./shift/operator/operator.js";
@@ -10,7 +10,7 @@ export const maybeNaiveParse = (def, ctx)=>{
10
10
  if (def.endsWith("[]")) {
11
11
  const elementDef = def.slice(0, -2);
12
12
  if (ctx.type.meta.scope.addReferenceIfResolvable(def, ctx)) {
13
- return functors.Array(elementDef);
13
+ return toArrayNode(elementDef);
14
14
  }
15
15
  }
16
16
  };
@@ -0,0 +1,9 @@
1
+ import { scopes } from "./standard.js";
2
+ export const intersection = scopes.standard.intersection;
3
+ export const union = scopes.standard.union;
4
+ export const arrayOf = scopes.standard.arrayOf;
5
+ export const keyOf = scopes.standard.keyOf;
6
+ export const instanceOf = scopes.standard.instanceOf;
7
+ export const valueOf = scopes.standard.valueOf;
8
+ export const narrow = scopes.standard.narrow;
9
+ export const morph = scopes.standard.morph;
@@ -4,7 +4,7 @@ export const jsObjectsScope = scope({
4
4
  "node",
5
5
  {
6
6
  object: {
7
- objectKind: "Function"
7
+ class: "Function"
8
8
  }
9
9
  }
10
10
  ],
@@ -12,7 +12,7 @@ export const jsObjectsScope = scope({
12
12
  "node",
13
13
  {
14
14
  object: {
15
- objectKind: "Array"
15
+ class: "Array"
16
16
  }
17
17
  }
18
18
  ],
@@ -20,7 +20,7 @@ export const jsObjectsScope = scope({
20
20
  "node",
21
21
  {
22
22
  object: {
23
- objectKind: "Date"
23
+ class: "Date"
24
24
  }
25
25
  }
26
26
  ],
@@ -28,7 +28,7 @@ export const jsObjectsScope = scope({
28
28
  "node",
29
29
  {
30
30
  object: {
31
- objectKind: "Error"
31
+ class: "Error"
32
32
  }
33
33
  }
34
34
  ],
@@ -36,7 +36,7 @@ export const jsObjectsScope = scope({
36
36
  "node",
37
37
  {
38
38
  object: {
39
- objectKind: "Map"
39
+ class: "Map"
40
40
  }
41
41
  }
42
42
  ],
@@ -44,7 +44,7 @@ export const jsObjectsScope = scope({
44
44
  "node",
45
45
  {
46
46
  object: {
47
- objectKind: "RegExp"
47
+ class: "RegExp"
48
48
  }
49
49
  }
50
50
  ],
@@ -52,7 +52,7 @@ export const jsObjectsScope = scope({
52
52
  "node",
53
53
  {
54
54
  object: {
55
- objectKind: "Set"
55
+ class: "Set"
56
56
  }
57
57
  }
58
58
  ],
@@ -60,7 +60,7 @@ export const jsObjectsScope = scope({
60
60
  "node",
61
61
  {
62
62
  object: {
63
- objectKind: "Object"
63
+ class: "Object"
64
64
  }
65
65
  }
66
66
  ],
@@ -68,7 +68,7 @@ export const jsObjectsScope = scope({
68
68
  "node",
69
69
  {
70
70
  object: {
71
- objectKind: "String"
71
+ class: "String"
72
72
  }
73
73
  }
74
74
  ],
@@ -76,7 +76,7 @@ export const jsObjectsScope = scope({
76
76
  "node",
77
77
  {
78
78
  object: {
79
- objectKind: "Number"
79
+ class: "Number"
80
80
  }
81
81
  }
82
82
  ],
@@ -84,7 +84,7 @@ export const jsObjectsScope = scope({
84
84
  "node",
85
85
  {
86
86
  object: {
87
- objectKind: "Boolean"
87
+ class: "Boolean"
88
88
  }
89
89
  }
90
90
  ],
@@ -92,7 +92,7 @@ export const jsObjectsScope = scope({
92
92
  "node",
93
93
  {
94
94
  object: {
95
- objectKind: "WeakMap"
95
+ class: "WeakMap"
96
96
  }
97
97
  }
98
98
  ],
@@ -100,7 +100,7 @@ export const jsObjectsScope = scope({
100
100
  "node",
101
101
  {
102
102
  object: {
103
- objectKind: "WeakSet"
103
+ class: "WeakSet"
104
104
  }
105
105
  }
106
106
  ],
@@ -108,7 +108,7 @@ export const jsObjectsScope = scope({
108
108
  "node",
109
109
  {
110
110
  object: {
111
- objectKind: "Promise"
111
+ class: "Promise"
112
112
  }
113
113
  }
114
114
  ]
@@ -143,7 +143,57 @@ export class Scope {
143
143
  _classPrivateFieldSet(this, _resolutions, new Cache());
144
144
  _classPrivateFieldSet(this, _exports, new Cache());
145
145
  _classPrivateFieldSet(this, _anonymousTypeCount, 0);
146
- this.type = (def, opts = {})=>{
146
+ this.expressions = {
147
+ intersection: (l, r, opts)=>this.type([
148
+ l,
149
+ "&",
150
+ r
151
+ ], opts),
152
+ union: (l, r, opts)=>this.type([
153
+ l,
154
+ "|",
155
+ r
156
+ ], opts),
157
+ arrayOf: (def, opts)=>this.type([
158
+ def,
159
+ "[]"
160
+ ], opts),
161
+ keyOf: (def, opts)=>this.type([
162
+ "keyof",
163
+ def
164
+ ], opts),
165
+ fromNode: (def, opts)=>this.type([
166
+ "node",
167
+ def
168
+ ], opts),
169
+ instanceOf: (def, opts)=>this.type([
170
+ "instanceof",
171
+ def
172
+ ], opts),
173
+ valueOf: (def, opts)=>this.type([
174
+ "===",
175
+ def
176
+ ], opts),
177
+ narrow: (def, fn, opts)=>this.type([
178
+ def,
179
+ ":",
180
+ fn
181
+ ], opts),
182
+ morph: (def, fn, opts)=>this.type([
183
+ def,
184
+ "=>",
185
+ fn
186
+ ], opts)
187
+ };
188
+ this.intersection = this.expressions.intersection;
189
+ this.union = this.expressions.union;
190
+ this.arrayOf = this.expressions.arrayOf;
191
+ this.keyOf = this.expressions.keyOf;
192
+ this.valueOf = this.expressions.valueOf;
193
+ this.instanceOf = this.expressions.instanceOf;
194
+ this.narrow = this.expressions.narrow;
195
+ this.morph = this.expressions.morph;
196
+ this.type = Object.assign((def, opts = {})=>{
147
197
  if (opts.name && this.aliases[opts.name]) {
148
198
  return throwParseError(writeDuplicateAliasesMessage(opts.name));
149
199
  }
@@ -152,7 +202,9 @@ export class Scope {
152
202
  result.node = this.resolveNode(parseDefinition(def, ctx));
153
203
  result.flat = flattenType(result);
154
204
  return result;
155
- };
205
+ }, {
206
+ from: this.expressions.fromNode
207
+ });
156
208
  this.name = _classPrivateMethodGet(this, _register, register).call(this, opts);
157
209
  if (opts.standard !== false) {
158
210
  _classPrivateMethodGet(this, _cacheSpaces, cacheSpaces).call(this, [
@@ -220,9 +272,9 @@ function resolveRecurse(name, onUnresolvable, seen) {
220
272
  type.flat = flattenType(type);
221
273
  return type;
222
274
  }
275
+ export const scope = (aliases, opts = {})=>new Scope(aliases, opts);
223
276
  export const writeShallowCycleErrorMessage = (name, seen)=>`Alias '${name}' has a shallow resolution cycle: ${[
224
277
  ...seen,
225
278
  name
226
279
  ].join("=>")}`;
227
- export const scope = (aliases, opts = {})=>new Scope(aliases, opts);
228
280
  export const writeDuplicateAliasesMessage = (name)=>`Alias '${name}' is already defined`;
@@ -37,14 +37,11 @@ export const initializeType = (definition, opts, scope)=>{
37
37
  // dynamically assign a name to the primary traversal function
38
38
  const namedTraverse = {
39
39
  [name]: (data)=>{
40
- const state = new TraversalState(type);
41
- const out = traverse(data, type.flat, state);
42
- return state.problems.length ? {
43
- data,
44
- problems: state.problems
40
+ const state = new TraversalState(data, type);
41
+ return traverse(type.flat, state) ? {
42
+ data: state.data
45
43
  } : {
46
- data,
47
- out
44
+ problems: state.problems
48
45
  };
49
46
  }
50
47
  }[name];
@@ -1,8 +1,49 @@
1
- var _definitions_code, _definitions_code1, _clausesByPath, _pathKey;
1
+ function _checkPrivateRedeclaration(obj, privateCollection) {
2
+ if (privateCollection.has(obj)) {
3
+ throw new TypeError("Cannot initialize the same private elements twice on an object");
4
+ }
5
+ }
6
+ function _classApplyDescriptorGet(receiver, descriptor) {
7
+ if (descriptor.get) {
8
+ return descriptor.get.call(receiver);
9
+ }
10
+ return descriptor.value;
11
+ }
12
+ function _classApplyDescriptorSet(receiver, descriptor, value) {
13
+ if (descriptor.set) {
14
+ descriptor.set.call(receiver, value);
15
+ } else {
16
+ if (!descriptor.writable) {
17
+ throw new TypeError("attempted to set read only private field");
18
+ }
19
+ descriptor.value = value;
20
+ }
21
+ }
22
+ function _classExtractFieldDescriptor(receiver, privateMap, action) {
23
+ if (!privateMap.has(receiver)) {
24
+ throw new TypeError("attempted to " + action + " private field on non-instance");
25
+ }
26
+ return privateMap.get(receiver);
27
+ }
28
+ function _classPrivateFieldGet(receiver, privateMap) {
29
+ var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get");
30
+ return _classApplyDescriptorGet(receiver, descriptor);
31
+ }
32
+ function _classPrivateFieldInit(obj, privateMap, value) {
33
+ _checkPrivateRedeclaration(obj, privateMap);
34
+ privateMap.set(obj, value);
35
+ }
36
+ function _classPrivateFieldSet(receiver, privateMap, value) {
37
+ var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set");
38
+ _classApplyDescriptorSet(receiver, descriptor, value);
39
+ return value;
40
+ }
41
+ var _definitions_code, _definitions_code1;
2
42
  import { Scanner } from "../parse/string/shift/scanner.js";
3
- import { domainDescriptions, domainOf } from "../utils/domains.js";
43
+ import { DataWrapper } from "../utils/data.js";
44
+ import { domainDescriptions } from "../utils/domains.js";
4
45
  import { isWellFormedInteger } from "../utils/numericLiterals.js";
5
- import { objectKindDescriptions, objectKindOf } from "../utils/objectKinds.js";
46
+ import { objectKindDescriptions } from "../utils/objectKinds.js";
6
47
  import { Path } from "../utils/paths.js";
7
48
  import { stringify } from "../utils/serialize.js";
8
49
  export class ArkTypeError extends TypeError {
@@ -35,20 +76,27 @@ export class Problem {
35
76
  }
36
77
  }
37
78
  }
79
+ var _state = /*#__PURE__*/ new WeakMap();
38
80
  class ProblemArray extends Array {
39
- add(code, data, source) {
40
- const problem = new Problem(code, // copy the path to avoid future mutations affecting it
41
- Path.from(this.state.path), data, source, this.state.getConfigForProblemCode(code));
42
- const pathKey = `${this.state.path}`;
81
+ create(code, source, opts) {
82
+ // copy the path to avoid future mutations affecting it
83
+ const path = opts?.path ?? Path.from(_classPrivateFieldGet(this, _state).path);
84
+ const data = // we have to check for the presence of the key explicitly since the
85
+ // data could be undefined or null
86
+ opts && "data" in opts ? opts.data : _classPrivateFieldGet(this, _state).data;
87
+ return this.add(new Problem(code, path, data, source, _classPrivateFieldGet(this, _state).getConfigForProblemCode(code)));
88
+ }
89
+ add(problem) {
90
+ const pathKey = `${problem.path}`;
43
91
  const existing = this.byPath[pathKey];
44
92
  if (existing) {
45
93
  if (existing.parts) {
46
94
  existing.parts.push(problem);
47
95
  } else {
48
- const problemIntersection = new Problem("multi", existing.path, data, [
96
+ const problemIntersection = new Problem("multi", existing.path, existing.data, [
49
97
  existing,
50
98
  problem
51
- ], this.state.getConfigForProblemCode("multi"));
99
+ ], _classPrivateFieldGet(this, _state).getConfigForProblemCode("multi"));
52
100
  const existingIndex = this.indexOf(existing);
53
101
  // If existing is found (which it always should be unless this was externally mutated),
54
102
  // replace it with the new problem intersection. In case it isn't for whatever reason,
@@ -60,11 +108,9 @@ class ProblemArray extends Array {
60
108
  this.byPath[pathKey] = problem;
61
109
  this.push(problem);
62
110
  }
111
+ return false;
63
112
  }
64
113
  get summary() {
65
- if (this.length === 1) {
66
- return this[0].message;
67
- }
68
114
  return this.join("\n");
69
115
  }
70
116
  toString() {
@@ -75,8 +121,12 @@ class ProblemArray extends Array {
75
121
  }
76
122
  constructor(state){
77
123
  super();
78
- this.state = state;
79
124
  this.byPath = {};
125
+ _classPrivateFieldInit(this, _state, {
126
+ writable: true,
127
+ value: void 0
128
+ });
129
+ _classPrivateFieldSet(this, _state, state);
80
130
  }
81
131
  }
82
132
  export const Problems = ProblemArray;
@@ -101,36 +151,13 @@ export const describeBranches = (descriptions)=>{
101
151
  description += ` or ${descriptions[descriptions.length - 1]}`;
102
152
  return description;
103
153
  };
104
- export class DataWrapper {
105
- toString() {
106
- return stringify(this.value);
107
- }
108
- get domain() {
109
- return domainOf(this.value);
110
- }
111
- get objectKind() {
112
- return objectKindOf(this.value) ?? "non-object";
113
- }
114
- get size() {
115
- return typeof this.value === "string" || Array.isArray(this.value) ? this.value.length : typeof this.value === "number" ? this.value : 0;
116
- }
117
- get units() {
118
- return typeof this.value === "string" ? "characters" : Array.isArray(this.value) ? "items" : "";
119
- }
120
- get className() {
121
- return Object(this.value).constructor.name;
122
- }
123
- constructor(value){
124
- this.value = value;
125
- }
126
- }
127
154
  const writeDefaultReason = (mustBe, was)=>`must be ${mustBe}${was && ` (was ${was})`}`;
128
- const writeDefaultProblemMessage = (reason, path)=>path.length === 0 ? capitalize(reason) : path.length === 1 && isWellFormedInteger(path[0]) ? `Item at index ${path[0]} ${reason}` : `${path} ${reason}`;
155
+ const addDefaultContext = (reason, path)=>path.length === 0 ? capitalize(reason) : path.length === 1 && isWellFormedInteger(path[0]) ? `Item at index ${path[0]} ${reason}` : `${path} ${reason}`;
129
156
  const compileDefaultProblemWriters = (definitions)=>{
130
157
  let code;
131
158
  for(code in definitions){
132
159
  (_definitions_code = definitions[code]).writeReason ?? (_definitions_code.writeReason = writeDefaultReason);
133
- (_definitions_code1 = definitions[code]).addContext ?? (_definitions_code1.addContext = writeDefaultProblemMessage);
160
+ (_definitions_code1 = definitions[code]).addContext ?? (_definitions_code1.addContext = addDefaultContext);
134
161
  }
135
162
  return definitions;
136
163
  };
@@ -139,7 +166,7 @@ export const defaultProblemWriters = compileDefaultProblemWriters({
139
166
  mustBe: (divisor)=>divisor === 1 ? `an integer` : `a multiple of ${divisor}`
140
167
  },
141
168
  class: {
142
- mustBe: (constructor)=>`an instance of ${constructor.name}`,
169
+ mustBe: (expected)=>typeof expected === "string" ? objectKindDescriptions[expected] : `an instance of ${expected.name}`,
143
170
  writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.className)
144
171
  },
145
172
  domain: {
@@ -150,10 +177,6 @@ export const defaultProblemWriters = compileDefaultProblemWriters({
150
177
  mustBe: (domains)=>describeDomains(domains),
151
178
  writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.domain)
152
179
  },
153
- objectKind: {
154
- mustBe: (kind)=>objectKindDescriptions[kind],
155
- writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.objectKind)
156
- },
157
180
  missing: {
158
181
  mustBe: ()=>"defined",
159
182
  writeReason: (mustBe)=>writeDefaultReason(mustBe, "")
@@ -163,7 +186,7 @@ export const defaultProblemWriters = compileDefaultProblemWriters({
163
186
  writeReason: (mustBe, data)=>writeDefaultReason(mustBe, `${data.size}`)
164
187
  },
165
188
  regex: {
166
- mustBe: (expression)=>expression[0] === "/" ? `a string matching ${expression}` : `a valid ${expression}`
189
+ mustBe: (expression)=>`a string matching ${expression}`
167
190
  },
168
191
  value: {
169
192
  mustBe: stringify
@@ -172,15 +195,7 @@ export const defaultProblemWriters = compileDefaultProblemWriters({
172
195
  mustBe: (values)=>describeBranches(values.map(stringify))
173
196
  },
174
197
  branches: {
175
- mustBe: (branchProblems)=>{
176
- const clausesByPath = {};
177
- for (const problem of branchProblems){
178
- const pathKey = `${problem.path}`;
179
- (_clausesByPath = clausesByPath)[_pathKey = pathKey] ?? (_clausesByPath[_pathKey] = []);
180
- clausesByPath[pathKey].push(problem.mustBe);
181
- }
182
- return describeBranches(Object.entries(clausesByPath).map(([path, clauses])=>`${path} must be ${clauses.length === 1 ? clauses[0] : describeBranches(clauses)}`));
183
- },
198
+ mustBe: (branchProblems)=>describeBranches(branchProblems.map((problem)=>`${problem.path} must be ${problem.parts ? describeBranches(problem.parts.map((part)=>part.mustBe)) : problem.mustBe}`)),
184
199
  writeReason: (mustBe, data)=>`${mustBe} (was ${data})`,
185
200
  addContext: (reason, path)=>path.length ? `At ${path}, ${reason}` : reason
186
201
  },