arktype 1.0.13-alpha → 1.0.15-alpha

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 (247) hide show
  1. package/README.md +68 -11
  2. package/dist/main.js +29 -0
  3. package/dist/nodes/branch.js +135 -0
  4. package/dist/nodes/compose.js +99 -0
  5. package/dist/{cjs/nodes → nodes}/discriminate.js +100 -92
  6. package/dist/nodes/node.js +134 -0
  7. package/dist/nodes/predicate.js +88 -0
  8. package/dist/nodes/rules/class.js +23 -0
  9. package/dist/nodes/rules/collapsibleSet.js +34 -0
  10. package/dist/nodes/rules/divisor.js +19 -0
  11. package/dist/nodes/rules/props.js +130 -0
  12. package/dist/nodes/rules/range.js +108 -0
  13. package/dist/nodes/rules/regex.js +17 -0
  14. package/dist/nodes/rules/rules.js +102 -0
  15. package/dist/parse/ast/ast.js +2 -0
  16. package/dist/{types/parse → parse}/ast/bound.d.ts +4 -1
  17. package/dist/parse/ast/bound.js +7 -0
  18. package/dist/parse/ast/config.js +14 -0
  19. package/dist/parse/ast/distributableFunction.js +40 -0
  20. package/dist/parse/ast/divisor.js +5 -0
  21. package/dist/parse/ast/intersection.js +21 -0
  22. package/dist/parse/ast/keyof.js +107 -0
  23. package/dist/parse/ast/morph.js +50 -0
  24. package/dist/parse/ast/narrow.js +17 -0
  25. package/dist/{types/parse → parse}/ast/node.d.ts +3 -1
  26. package/dist/parse/ast/node.js +2 -0
  27. package/dist/parse/ast/tuple.js +72 -0
  28. package/dist/parse/ast/union.js +5 -0
  29. package/dist/parse/ast/utils.js +2 -0
  30. package/dist/parse/definition.js +47 -0
  31. package/dist/parse/record.js +31 -0
  32. package/dist/parse/string/reduce/dynamic.js +159 -0
  33. package/dist/parse/string/reduce/shared.js +13 -0
  34. package/dist/parse/string/reduce/static.js +2 -0
  35. package/dist/parse/string/shift/operand/enclosed.js +37 -0
  36. package/dist/parse/string/shift/operand/operand.js +15 -0
  37. package/dist/parse/string/shift/operand/unenclosed.js +45 -0
  38. package/dist/parse/string/shift/operator/bounds.js +83 -0
  39. package/dist/parse/string/shift/operator/divisor.js +23 -0
  40. package/dist/parse/string/shift/operator/operator.js +34 -0
  41. package/dist/parse/string/shift/scanner.js +97 -0
  42. package/dist/parse/string/string.js +35 -0
  43. package/dist/scopes/ark.js +27 -0
  44. package/dist/scopes/cache.js +28 -0
  45. package/dist/{mjs/scopes → scopes}/expressions.js +20 -9
  46. package/dist/scopes/jsObjects.js +20 -0
  47. package/dist/scopes/scope.js +184 -0
  48. package/dist/scopes/tsKeywords.js +49 -0
  49. package/dist/scopes/type.js +40 -0
  50. package/dist/{mjs/scopes → scopes}/validation/creditCard.js +15 -11
  51. package/dist/{mjs/scopes → scopes}/validation/date.js +21 -16
  52. package/dist/scopes/validation/validation.js +74 -0
  53. package/dist/traverse/problems.js +234 -0
  54. package/dist/traverse/traverse.js +330 -0
  55. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  56. package/dist/{mjs/utils → utils}/chainableNoOpProxy.js +6 -2
  57. package/dist/utils/data.js +39 -0
  58. package/dist/utils/domains.js +27 -0
  59. package/dist/utils/errors.js +17 -0
  60. package/dist/utils/freeze.js +16 -0
  61. package/dist/utils/generics.js +53 -0
  62. package/dist/utils/numericLiterals.js +88 -0
  63. package/dist/utils/objectKinds.js +69 -0
  64. package/dist/{mjs/utils → utils}/paths.js +8 -3
  65. package/dist/utils/serialize.js +71 -0
  66. package/dist/utils/unionToTuple.js +2 -0
  67. package/package.json +25 -39
  68. package/dist/cjs/main.js +0 -38
  69. package/dist/cjs/nodes/branch.js +0 -140
  70. package/dist/cjs/nodes/compose.js +0 -157
  71. package/dist/cjs/nodes/node.js +0 -139
  72. package/dist/cjs/nodes/predicate.js +0 -86
  73. package/dist/cjs/nodes/rules/class.js +0 -25
  74. package/dist/cjs/nodes/rules/collapsibleSet.js +0 -48
  75. package/dist/cjs/nodes/rules/divisor.js +0 -29
  76. package/dist/cjs/nodes/rules/props.js +0 -160
  77. package/dist/cjs/nodes/rules/range.js +0 -105
  78. package/dist/cjs/nodes/rules/regex.js +0 -26
  79. package/dist/cjs/nodes/rules/rules.js +0 -123
  80. package/dist/cjs/package.json +0 -3
  81. package/dist/cjs/parse/ast/ast.js +0 -4
  82. package/dist/cjs/parse/ast/bound.js +0 -9
  83. package/dist/cjs/parse/ast/config.js +0 -13
  84. package/dist/cjs/parse/ast/distributableFunction.js +0 -48
  85. package/dist/cjs/parse/ast/divisor.js +0 -9
  86. package/dist/cjs/parse/ast/intersection.js +0 -30
  87. package/dist/cjs/parse/ast/keyof.js +0 -111
  88. package/dist/cjs/parse/ast/morph.js +0 -58
  89. package/dist/cjs/parse/ast/narrow.js +0 -22
  90. package/dist/cjs/parse/ast/node.js +0 -4
  91. package/dist/cjs/parse/ast/tuple.js +0 -94
  92. package/dist/cjs/parse/ast/union.js +0 -9
  93. package/dist/cjs/parse/ast/utils.js +0 -4
  94. package/dist/cjs/parse/definition.js +0 -61
  95. package/dist/cjs/parse/record.js +0 -37
  96. package/dist/cjs/parse/string/reduce/dynamic.js +0 -167
  97. package/dist/cjs/parse/string/reduce/shared.js +0 -23
  98. package/dist/cjs/parse/string/reduce/static.js +0 -4
  99. package/dist/cjs/parse/string/shift/operand/enclosed.js +0 -54
  100. package/dist/cjs/parse/string/shift/operand/operand.js +0 -11
  101. package/dist/cjs/parse/string/shift/operand/unenclosed.js +0 -53
  102. package/dist/cjs/parse/string/shift/operator/bounds.js +0 -87
  103. package/dist/cjs/parse/string/shift/operator/divisor.js +0 -35
  104. package/dist/cjs/parse/string/shift/operator/operator.js +0 -26
  105. package/dist/cjs/parse/string/shift/scanner.js +0 -114
  106. package/dist/cjs/parse/string/string.js +0 -43
  107. package/dist/cjs/scopes/ark.js +0 -45
  108. package/dist/cjs/scopes/cache.js +0 -52
  109. package/dist/cjs/scopes/expressions.js +0 -29
  110. package/dist/cjs/scopes/jsObjects.js +0 -93
  111. package/dist/cjs/scopes/scope.js +0 -323
  112. package/dist/cjs/scopes/tsKeywords.js +0 -113
  113. package/dist/cjs/scopes/type.js +0 -50
  114. package/dist/cjs/scopes/validation/creditCard.js +0 -47
  115. package/dist/cjs/scopes/validation/date.js +0 -62
  116. package/dist/cjs/scopes/validation/validation.js +0 -104
  117. package/dist/cjs/traverse/problems.js +0 -281
  118. package/dist/cjs/traverse/traverse.js +0 -387
  119. package/dist/cjs/utils/chainableNoOpProxy.js +0 -23
  120. package/dist/cjs/utils/data.js +0 -53
  121. package/dist/cjs/utils/domains.js +0 -30
  122. package/dist/cjs/utils/errors.js +0 -26
  123. package/dist/cjs/utils/freeze.js +0 -15
  124. package/dist/cjs/utils/generics.js +0 -63
  125. package/dist/cjs/utils/numericLiterals.js +0 -75
  126. package/dist/cjs/utils/objectKinds.js +0 -69
  127. package/dist/cjs/utils/paths.js +0 -32
  128. package/dist/cjs/utils/serialize.js +0 -74
  129. package/dist/cjs/utils/unionToTuple.js +0 -4
  130. package/dist/mjs/main.js +0 -8
  131. package/dist/mjs/nodes/branch.js +0 -123
  132. package/dist/mjs/nodes/compose.js +0 -135
  133. package/dist/mjs/nodes/discriminate.js +0 -247
  134. package/dist/mjs/nodes/node.js +0 -117
  135. package/dist/mjs/nodes/predicate.js +0 -69
  136. package/dist/mjs/nodes/rules/class.js +0 -11
  137. package/dist/mjs/nodes/rules/collapsibleSet.js +0 -34
  138. package/dist/mjs/nodes/rules/divisor.js +0 -15
  139. package/dist/mjs/nodes/rules/props.js +0 -142
  140. package/dist/mjs/nodes/rules/range.js +0 -86
  141. package/dist/mjs/nodes/rules/regex.js +0 -11
  142. package/dist/mjs/nodes/rules/rules.js +0 -106
  143. package/dist/mjs/package.json +0 -3
  144. package/dist/mjs/parse/ast/ast.js +0 -1
  145. package/dist/mjs/parse/ast/bound.js +0 -1
  146. package/dist/mjs/parse/ast/config.js +0 -9
  147. package/dist/mjs/parse/ast/distributableFunction.js +0 -34
  148. package/dist/mjs/parse/ast/divisor.js +0 -1
  149. package/dist/mjs/parse/ast/intersection.js +0 -16
  150. package/dist/mjs/parse/ast/keyof.js +0 -103
  151. package/dist/mjs/parse/ast/morph.js +0 -44
  152. package/dist/mjs/parse/ast/narrow.js +0 -14
  153. package/dist/mjs/parse/ast/node.js +0 -1
  154. package/dist/mjs/parse/ast/tuple.js +0 -80
  155. package/dist/mjs/parse/ast/union.js +0 -1
  156. package/dist/mjs/parse/ast/utils.js +0 -1
  157. package/dist/mjs/parse/definition.js +0 -46
  158. package/dist/mjs/parse/record.js +0 -29
  159. package/dist/mjs/parse/string/reduce/dynamic.js +0 -159
  160. package/dist/mjs/parse/string/reduce/shared.js +0 -6
  161. package/dist/mjs/parse/string/reduce/static.js +0 -1
  162. package/dist/mjs/parse/string/shift/operand/enclosed.js +0 -39
  163. package/dist/mjs/parse/string/shift/operand/operand.js +0 -3
  164. package/dist/mjs/parse/string/shift/operand/unenclosed.js +0 -36
  165. package/dist/mjs/parse/string/shift/operator/bounds.js +0 -70
  166. package/dist/mjs/parse/string/shift/operator/divisor.js +0 -21
  167. package/dist/mjs/parse/string/shift/operator/operator.js +0 -13
  168. package/dist/mjs/parse/string/shift/scanner.js +0 -106
  169. package/dist/mjs/parse/string/string.js +0 -28
  170. package/dist/mjs/scopes/ark.js +0 -28
  171. package/dist/mjs/scopes/cache.js +0 -38
  172. package/dist/mjs/scopes/jsObjects.js +0 -82
  173. package/dist/mjs/scopes/scope.js +0 -303
  174. package/dist/mjs/scopes/tsKeywords.js +0 -116
  175. package/dist/mjs/scopes/type.js +0 -35
  176. package/dist/mjs/scopes/validation/validation.js +0 -108
  177. package/dist/mjs/traverse/problems.js +0 -260
  178. package/dist/mjs/traverse/traverse.js +0 -370
  179. package/dist/mjs/utils/data.js +0 -38
  180. package/dist/mjs/utils/domains.js +0 -15
  181. package/dist/mjs/utils/errors.js +0 -10
  182. package/dist/mjs/utils/freeze.js +0 -7
  183. package/dist/mjs/utils/generics.js +0 -38
  184. package/dist/mjs/utils/numericLiterals.js +0 -72
  185. package/dist/mjs/utils/objectKinds.js +0 -55
  186. package/dist/mjs/utils/serialize.js +0 -58
  187. package/dist/mjs/utils/unionToTuple.js +0 -1
  188. /package/dist/{types/main.d.ts → main.d.ts} +0 -0
  189. /package/dist/{types/nodes → nodes}/branch.d.ts +0 -0
  190. /package/dist/{types/nodes → nodes}/compose.d.ts +0 -0
  191. /package/dist/{types/nodes → nodes}/discriminate.d.ts +0 -0
  192. /package/dist/{types/nodes → nodes}/node.d.ts +0 -0
  193. /package/dist/{types/nodes → nodes}/predicate.d.ts +0 -0
  194. /package/dist/{types/nodes → nodes}/rules/class.d.ts +0 -0
  195. /package/dist/{types/nodes → nodes}/rules/collapsibleSet.d.ts +0 -0
  196. /package/dist/{types/nodes → nodes}/rules/divisor.d.ts +0 -0
  197. /package/dist/{types/nodes → nodes}/rules/props.d.ts +0 -0
  198. /package/dist/{types/nodes → nodes}/rules/range.d.ts +0 -0
  199. /package/dist/{types/nodes → nodes}/rules/regex.d.ts +0 -0
  200. /package/dist/{types/nodes → nodes}/rules/rules.d.ts +0 -0
  201. /package/dist/{types/parse → parse}/ast/ast.d.ts +0 -0
  202. /package/dist/{types/parse → parse}/ast/config.d.ts +0 -0
  203. /package/dist/{types/parse → parse}/ast/distributableFunction.d.ts +0 -0
  204. /package/dist/{types/parse → parse}/ast/divisor.d.ts +0 -0
  205. /package/dist/{types/parse → parse}/ast/intersection.d.ts +0 -0
  206. /package/dist/{types/parse → parse}/ast/keyof.d.ts +0 -0
  207. /package/dist/{types/parse → parse}/ast/morph.d.ts +0 -0
  208. /package/dist/{types/parse → parse}/ast/narrow.d.ts +0 -0
  209. /package/dist/{types/parse → parse}/ast/tuple.d.ts +0 -0
  210. /package/dist/{types/parse → parse}/ast/union.d.ts +0 -0
  211. /package/dist/{types/parse → parse}/ast/utils.d.ts +0 -0
  212. /package/dist/{types/parse → parse}/definition.d.ts +0 -0
  213. /package/dist/{types/parse → parse}/record.d.ts +0 -0
  214. /package/dist/{types/parse → parse}/string/reduce/dynamic.d.ts +0 -0
  215. /package/dist/{types/parse → parse}/string/reduce/shared.d.ts +0 -0
  216. /package/dist/{types/parse → parse}/string/reduce/static.d.ts +0 -0
  217. /package/dist/{types/parse → parse}/string/shift/operand/enclosed.d.ts +0 -0
  218. /package/dist/{types/parse → parse}/string/shift/operand/operand.d.ts +0 -0
  219. /package/dist/{types/parse → parse}/string/shift/operand/unenclosed.d.ts +0 -0
  220. /package/dist/{types/parse → parse}/string/shift/operator/bounds.d.ts +0 -0
  221. /package/dist/{types/parse → parse}/string/shift/operator/divisor.d.ts +0 -0
  222. /package/dist/{types/parse → parse}/string/shift/operator/operator.d.ts +0 -0
  223. /package/dist/{types/parse → parse}/string/shift/scanner.d.ts +0 -0
  224. /package/dist/{types/parse → parse}/string/string.d.ts +0 -0
  225. /package/dist/{types/scopes → scopes}/ark.d.ts +0 -0
  226. /package/dist/{types/scopes → scopes}/cache.d.ts +0 -0
  227. /package/dist/{types/scopes → scopes}/expressions.d.ts +0 -0
  228. /package/dist/{types/scopes → scopes}/jsObjects.d.ts +0 -0
  229. /package/dist/{types/scopes → scopes}/scope.d.ts +0 -0
  230. /package/dist/{types/scopes → scopes}/tsKeywords.d.ts +0 -0
  231. /package/dist/{types/scopes → scopes}/type.d.ts +0 -0
  232. /package/dist/{types/scopes → scopes}/validation/creditCard.d.ts +0 -0
  233. /package/dist/{types/scopes → scopes}/validation/date.d.ts +0 -0
  234. /package/dist/{types/scopes → scopes}/validation/validation.d.ts +0 -0
  235. /package/dist/{types/traverse → traverse}/problems.d.ts +0 -0
  236. /package/dist/{types/traverse → traverse}/traverse.d.ts +0 -0
  237. /package/dist/{types/utils → utils}/chainableNoOpProxy.d.ts +0 -0
  238. /package/dist/{types/utils → utils}/data.d.ts +0 -0
  239. /package/dist/{types/utils → utils}/domains.d.ts +0 -0
  240. /package/dist/{types/utils → utils}/errors.d.ts +0 -0
  241. /package/dist/{types/utils → utils}/freeze.d.ts +0 -0
  242. /package/dist/{types/utils → utils}/generics.d.ts +0 -0
  243. /package/dist/{types/utils → utils}/numericLiterals.d.ts +0 -0
  244. /package/dist/{types/utils → utils}/objectKinds.d.ts +0 -0
  245. /package/dist/{types/utils → utils}/paths.d.ts +0 -0
  246. /package/dist/{types/utils → utils}/serialize.d.ts +0 -0
  247. /package/dist/{types/utils → utils}/unionToTuple.d.ts +0 -0
package/README.md CHANGED
@@ -18,8 +18,8 @@
18
18
 
19
19
  <!-- @snipEnd -->
20
20
 
21
- ```ts @blockFrom:dev/examples/type.ts
22
- import { type } from "../../src/main.js"
21
+ ```ts @blockFrom:dev/test/examples/type.ts
22
+ import { type } from "arktype"
23
23
 
24
24
  // Definitions are statically parsed and inferred as TS.
25
25
  export const user = type({
@@ -54,8 +54,6 @@ Check out [how it works](#how), [try it in-browser](https://arktype.io/docs/#you
54
54
 
55
55
  Our types are tested in [strict-mode](https://www.typescriptlang.org/tsconfig#strict) with TypeScript versions `4.8`, `4.9`, and `5.0`.
56
56
 
57
- If you're using VSCode, setting `"editor.quickSuggestions"` to `{"strings": true}` will improve its autocomplete suggestions.
58
-
59
57
  _Our APIs have mostly stabilized, but details may still change during the alpha/beta stages of our 1.0 release. If you have suggestions that may require a breaking change, now is the time to let us know!_ ⛵
60
58
 
61
59
  <!-- @snipEnd -->
@@ -64,8 +62,8 @@ _Our APIs have mostly stabilized, but details may still change during the alpha/
64
62
 
65
63
  [Try this example in-browser.](https://arktype.io/docs/scopes)
66
64
 
67
- ```ts @blockFrom:dev/examples/scope.ts
68
- import { scope } from "../../src/main.js"
65
+ ```ts @blockFrom:dev/test/examples/scope.ts
66
+ import { scope } from "arktype"
69
67
 
70
68
  // Scopes are collections of types that can reference each other.
71
69
  export const types = scope({
@@ -95,13 +93,72 @@ packageData.dependencies![0].dependencies = [packageData]
95
93
  export const { data, problems } = types.package(packageData)
96
94
  ```
97
95
 
96
+ ## Syntax
97
+
98
+ This is an informal, non-exhaustive list of current and upcoming ArkType syntax.
99
+
100
+ There are some subjects it doesn't cover, primarily tuple expressions and scopes. As mentioned below, keep an eye out for comprehensive docs coming with the upcoming beta release. In the meantime, join [our Discord](https://discord.gg/xEzdc3fJQC) or head to our [GitHub Discussions](https://github.com/arktypeio/arktype/discussions) to ask a question and there's a good chance you'll see a response within the hour 😊
101
+
102
+ ```ts
103
+ export const currentTsSyntax = type({
104
+ keyword: "null",
105
+ stringLiteral: "'TS'",
106
+ numberLiteral: "5",
107
+ bigintLiteral: "5n",
108
+ union: "string|number",
109
+ intersection: "boolean&true",
110
+ array: "Date[]",
111
+ grouping: "(0|1)[]",
112
+ objectLiteral: {
113
+ nested: "string",
114
+ "optional?": "number"
115
+ },
116
+ tuple: ["number", "number"]
117
+ })
118
+
119
+ // these features will be available in the upcoming release
120
+
121
+ export const upcomingTsSyntax = type({
122
+ keyof: "keyof bigint",
123
+ thisKeyword: "this", // recurses to the root of the current type
124
+ variadicTuples: ["true", "...false[]"]
125
+ })
126
+
127
+ export const validationSyntax = type({
128
+ keywords: "email|uuid|creditCard|integer", // and many more
129
+ builtinParsers: "parsedDate", // parses a Date from a string
130
+ nativeRegexLiteral: /@arktype\.io/,
131
+ embeddedRegexLiteral: "email&/@arktype\\.io/",
132
+ divisibility: "number%10", // a multiple of 10
133
+ bound: "alpha>10", // an alpha-only string with more than 10 characters
134
+ range: "1<=email[]<99", // a list of 1 to 99 emails
135
+ narrows: ["number", "=>", (n) => n % 2 === 1], // an odd integer
136
+ morphs: ["string", "|>", parseFloat] // validates a string input then parses it to a number
137
+ })
138
+
139
+ // in the upcoming release, you can use chaining to define expressions directly
140
+ // that use objects or functions that can't be embedded in strings
141
+
142
+ export const parseBigintLiteral = type({ value: "string" })
143
+ .and({
144
+ format: "'bigint'"
145
+ })
146
+ .narrow((data): data is { value: `${string}n`; format: "bigint" } =>
147
+ data.value.endsWith("n")
148
+ )
149
+ .morph((data) => BigInt(data.value.slice(-1)))
150
+
151
+ export const { data, problems } = parseBigintLiteral("999n")
152
+ // ^ bigint | undefined
153
+ ```
154
+
98
155
  ## API
99
156
 
100
157
  <!--@snipStart:api -->
101
158
 
102
159
  ArkType supports many of TypeScript's built-in types and operators, as well as some new ones dedicated exclusively to runtime validation. In fact, we got a little ahead of ourselves and built a ton of cool features, but we're still working on getting caught up syntax and API docs. Keep an eye out for more in the next couple weeks ⛵
103
160
 
104
- In the meantime, check out the examples here and use the type hints you get to learn how you can customize your types and scopes. If you want to explore some of the more advanced features, take a look at [our unit tests](./dev/test) or ask us [on Discord](https://discord.gg/WSNF3Kc4xh) if your functionality is supported. If not, [create a GitHub issue](https://github.com/arktypeio/arktype/issues/new) so we can prioritize it!
161
+ In the meantime, check out the examples here and use the type hints you get to learn how you can customize your types and scopes. If you want to explore some of the more advanced features, take a look at [our unit tests](./dev/test) or ask us [on Discord](https://discord.gg/xEzdc3fJQC) if your functionality is supported. If not, [create a GitHub issue](https://github.com/arktypeio/arktype/issues/new) so we can prioritize it!
105
162
 
106
163
  <!--@snipEnd -->
107
164
 
@@ -200,10 +257,10 @@ We've been working full-time on this project for over a year and it means a lot
200
257
 
201
258
  If the project has been useful to you and you are in a financial position to do so, please chip in via [GitHub Sponsors](https://github.com/sponsors/arktypeio).
202
259
 
203
- Otherwise, consider sending me an email (david@arktype.io) or [message me on Discord](https://discord.gg/WSNF3Kc4xh) to let me know you're a fan of ArkType. Either would make my day!
260
+ Otherwise, consider sending me an email (david@arktype.io) or [message me on Discord](https://discord.gg/xEzdc3fJQC) to let me know you're a fan of ArkType. Either would make my day!
204
261
 
205
262
  ### Current Sponsors 🥰
206
263
 
207
- [<img height="64px" src="https://avatars.githubusercontent.com/u/6759464">](https://github.com/tmm)
208
-
209
- [tmm](https://github.com/tmm)
264
+ | [tmm](https://github.com/tmm) | [xrexy](https://github.com/xrexy) | [thomasballinger](https://github.com/thomasballinger) | [codeandcats](https://github.com/codeandcats) |
265
+ | ------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ------------------------------------------------------------------------ | ------------------------------------------------------------------------- |
266
+ | <img height="64px" src="https://avatars.githubusercontent.com/u/6759464"> | <img height="64px" src="https://avatars.githubusercontent.com/u/71969236"> | <img height="64px" src="https://avatars.githubusercontent.com/u/458879"> | <img height="64px" src="https://avatars.githubusercontent.com/u/6035934"> |
package/dist/main.js ADDED
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseConfigTuple = exports.Problem = exports.Problems = exports.validationScope = exports.tsKeywordsScope = exports.jsObjectsScope = exports.valueOf = exports.union = exports.narrow = exports.morph = exports.keyOf = exports.intersection = exports.instanceOf = exports.arrayOf = exports.arkScope = exports.ark = exports.type = exports.scope = void 0;
4
+ var scope_js_1 = require("./scopes/scope.js");
5
+ Object.defineProperty(exports, "scope", { enumerable: true, get: function () { return scope_js_1.scope; } });
6
+ var ark_js_1 = require("./scopes/ark.js");
7
+ Object.defineProperty(exports, "type", { enumerable: true, get: function () { return ark_js_1.type; } });
8
+ Object.defineProperty(exports, "ark", { enumerable: true, get: function () { return ark_js_1.ark; } });
9
+ Object.defineProperty(exports, "arkScope", { enumerable: true, get: function () { return ark_js_1.arkScope; } });
10
+ var expressions_js_1 = require("./scopes/expressions.js");
11
+ Object.defineProperty(exports, "arrayOf", { enumerable: true, get: function () { return expressions_js_1.arrayOf; } });
12
+ Object.defineProperty(exports, "instanceOf", { enumerable: true, get: function () { return expressions_js_1.instanceOf; } });
13
+ Object.defineProperty(exports, "intersection", { enumerable: true, get: function () { return expressions_js_1.intersection; } });
14
+ Object.defineProperty(exports, "keyOf", { enumerable: true, get: function () { return expressions_js_1.keyOf; } });
15
+ Object.defineProperty(exports, "morph", { enumerable: true, get: function () { return expressions_js_1.morph; } });
16
+ Object.defineProperty(exports, "narrow", { enumerable: true, get: function () { return expressions_js_1.narrow; } });
17
+ Object.defineProperty(exports, "union", { enumerable: true, get: function () { return expressions_js_1.union; } });
18
+ Object.defineProperty(exports, "valueOf", { enumerable: true, get: function () { return expressions_js_1.valueOf; } });
19
+ var jsObjects_js_1 = require("./scopes/jsObjects.js");
20
+ Object.defineProperty(exports, "jsObjectsScope", { enumerable: true, get: function () { return jsObjects_js_1.jsObjectsScope; } });
21
+ var tsKeywords_js_1 = require("./scopes/tsKeywords.js");
22
+ Object.defineProperty(exports, "tsKeywordsScope", { enumerable: true, get: function () { return tsKeywords_js_1.tsKeywordsScope; } });
23
+ var validation_js_1 = require("./scopes/validation/validation.js");
24
+ Object.defineProperty(exports, "validationScope", { enumerable: true, get: function () { return validation_js_1.validationScope; } });
25
+ var problems_js_1 = require("./traverse/problems.js");
26
+ Object.defineProperty(exports, "Problems", { enumerable: true, get: function () { return problems_js_1.Problems; } });
27
+ Object.defineProperty(exports, "Problem", { enumerable: true, get: function () { return problems_js_1.Problem; } });
28
+ var config_js_1 = require("./parse/ast/config.js");
29
+ Object.defineProperty(exports, "parseConfigTuple", { enumerable: true, get: function () { return config_js_1.parseConfigTuple; } });
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.branchIntersection = exports.flattenBranch = exports.isTransformationBranch = exports.compareBranches = exports.isBranchComparison = void 0;
4
+ const intersection_js_1 = require("../parse/ast/intersection.js");
5
+ const domains_js_1 = require("../utils/domains.js");
6
+ const errors_js_1 = require("../utils/errors.js");
7
+ const compose_js_1 = require("./compose.js");
8
+ const rules_js_1 = require("./rules/rules.js");
9
+ const isBranchComparison = (comparison) => comparison?.lBranches !== undefined;
10
+ exports.isBranchComparison = isBranchComparison;
11
+ const compareBranches = (lConditions, rConditions, state) => {
12
+ const result = {
13
+ lBranches: lConditions,
14
+ rBranches: rConditions,
15
+ lExtendsR: [],
16
+ rExtendsL: [],
17
+ equalities: [],
18
+ distinctIntersections: []
19
+ };
20
+ const pairs = rConditions.map((condition) => ({
21
+ condition,
22
+ distinct: []
23
+ }));
24
+ lConditions.forEach((l, lIndex) => {
25
+ let lImpliesR = false;
26
+ const distinct = pairs.map((rPairs, rIndex) => {
27
+ if (lImpliesR || !rPairs.distinct) {
28
+ return null;
29
+ }
30
+ const r = rPairs.condition;
31
+ const subresult = (0, exports.branchIntersection)(l, r, state);
32
+ if ((0, compose_js_1.isDisjoint)(subresult)) {
33
+ // doesn't tell us about any redundancies or add a distinct pair
34
+ return null;
35
+ }
36
+ else if (subresult === l) {
37
+ result.lExtendsR.push(lIndex);
38
+ // If l is a subtype of the current r branch, intersections
39
+ // with the remaining branches of r won't lead to distinct
40
+ // branches, so we set a flag indicating we can skip them.
41
+ lImpliesR = true;
42
+ return null;
43
+ }
44
+ else if (subresult === r) {
45
+ result.rExtendsL.push(rIndex);
46
+ // If r is a subtype of the current l branch, it is removed
47
+ // from pairsByR because future intersections won't lead to
48
+ // distinct branches.
49
+ rPairs.distinct = null;
50
+ return null;
51
+ }
52
+ else if ((0, compose_js_1.isEquality)(subresult)) {
53
+ // Combination of l and r subtype cases.
54
+ result.equalities.push([lIndex, rIndex]);
55
+ lImpliesR = true;
56
+ rPairs.distinct = null;
57
+ return null;
58
+ }
59
+ else if ((0, domains_js_1.hasDomain)(subresult, "object")) {
60
+ // Neither branch is a subtype of the other, return
61
+ // the result of the intersection as a candidate
62
+ // branch for the final union
63
+ return subresult;
64
+ }
65
+ return (0, errors_js_1.throwInternalError)(`Unexpected predicate intersection result of type '${(0, domains_js_1.domainOf)(subresult)}'`);
66
+ });
67
+ if (!lImpliesR) {
68
+ for (let i = 0; i < pairs.length; i++) {
69
+ if (distinct[i]) {
70
+ pairs[i].distinct?.push(distinct[i]);
71
+ }
72
+ }
73
+ }
74
+ });
75
+ result.distinctIntersections = pairs.flatMap((pairs) => pairs.distinct ?? []);
76
+ return result;
77
+ };
78
+ exports.compareBranches = compareBranches;
79
+ const isTransformationBranch = (branch) => "rules" in branch;
80
+ exports.isTransformationBranch = isTransformationBranch;
81
+ const flattenBranch = (branch, ctx) => {
82
+ if ((0, exports.isTransformationBranch)(branch)) {
83
+ const result = (0, rules_js_1.flattenRules)(branch.rules, ctx);
84
+ if (branch.morph) {
85
+ if (typeof branch.morph === "function") {
86
+ result.push(["morph", branch.morph]);
87
+ }
88
+ else {
89
+ for (const morph of branch.morph) {
90
+ result.push(["morph", morph]);
91
+ }
92
+ }
93
+ }
94
+ return result;
95
+ }
96
+ return (0, rules_js_1.flattenRules)(branch, ctx);
97
+ };
98
+ exports.flattenBranch = flattenBranch;
99
+ const rulesOf = (branch) => branch.rules ?? branch;
100
+ const branchIntersection = (l, r, state) => {
101
+ const lRules = rulesOf(l);
102
+ const rRules = rulesOf(r);
103
+ const rulesResult = (0, rules_js_1.rulesIntersection)(lRules, rRules, state);
104
+ if ("morph" in l) {
105
+ if ("morph" in r) {
106
+ if (l.morph === r.morph) {
107
+ return (0, compose_js_1.isEquality)(rulesResult) || (0, compose_js_1.isDisjoint)(rulesResult)
108
+ ? rulesResult
109
+ : {
110
+ rules: rulesResult,
111
+ morph: l.morph
112
+ };
113
+ }
114
+ return state.lastOperator === "&"
115
+ ? (0, errors_js_1.throwParseError)((0, intersection_js_1.writeImplicitNeverMessage)(state.path, "Intersection", "of morphs"))
116
+ : {};
117
+ }
118
+ return (0, compose_js_1.isDisjoint)(rulesResult)
119
+ ? rulesResult
120
+ : {
121
+ rules: (0, compose_js_1.isEquality)(rulesResult) ? l.rules : rulesResult,
122
+ morph: l.morph
123
+ };
124
+ }
125
+ if ("morph" in r) {
126
+ return (0, compose_js_1.isDisjoint)(rulesResult)
127
+ ? rulesResult
128
+ : {
129
+ rules: (0, compose_js_1.isEquality)(rulesResult) ? r.rules : rulesResult,
130
+ morph: r.morph
131
+ };
132
+ }
133
+ return rulesResult;
134
+ };
135
+ exports.branchIntersection = branchIntersection;
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.composeKeyedIntersection = exports.isEquality = exports.equality = exports.isDisjoint = exports.anonymousDisjoint = exports.IntersectionState = exports.stringifyRange = exports.disjointDescriptionWriters = exports.undefinedOperandsMessage = exports.composeIntersection = void 0;
4
+ const errors_js_1 = require("../utils/errors.js");
5
+ const generics_js_1 = require("../utils/generics.js");
6
+ const paths_js_1 = require("../utils/paths.js");
7
+ const serialize_js_1 = require("../utils/serialize.js");
8
+ const composeIntersection = (reducer) => ((l, r, state) => l === undefined
9
+ ? r === undefined
10
+ ? (0, errors_js_1.throwInternalError)(exports.undefinedOperandsMessage)
11
+ : r
12
+ : r === undefined
13
+ ? l
14
+ : reducer(l, r, state));
15
+ exports.composeIntersection = composeIntersection;
16
+ exports.undefinedOperandsMessage = `Unexpected operation two undefined operands`;
17
+ exports.disjointDescriptionWriters = {
18
+ domain: ({ l, r }) => `${l.join(", ")} and ${r.join(", ")}`,
19
+ range: ({ l, r }) => `${(0, exports.stringifyRange)(l)} and ${(0, exports.stringifyRange)(r)}`,
20
+ class: ({ l, r }) => `classes ${typeof l === "string" ? l : l.name} and ${typeof r === "string" ? r : r.name}`,
21
+ tupleLength: ({ l, r }) => `tuples of length ${l} and ${r}`,
22
+ value: ({ l, r }) => `literal values ${(0, serialize_js_1.stringify)(l)} and ${(0, serialize_js_1.stringify)(r)}`,
23
+ leftAssignability: ({ l, r }) => `literal value ${(0, serialize_js_1.stringify)(l.value)} and ${(0, serialize_js_1.stringify)(r)}`,
24
+ rightAssignability: ({ l, r }) => `literal value ${(0, serialize_js_1.stringify)(r.value)} and ${(0, serialize_js_1.stringify)(l)}`,
25
+ union: ({ l, r }) => `branches ${(0, serialize_js_1.stringify)(l)} and branches ${(0, serialize_js_1.stringify)(r)}`
26
+ };
27
+ const stringifyRange = (range) => "limit" in range
28
+ ? `the range of exactly ${range.limit}`
29
+ : range.min
30
+ ? range.max
31
+ ? `the range bounded by ${range.min.comparator}${range.min.limit} and ${range.max.comparator}${range.max.limit}`
32
+ : `${range.min.comparator}${range.min.limit}`
33
+ : range.max
34
+ ? `${range.max.comparator}${range.max.limit}`
35
+ : "the unbounded range";
36
+ exports.stringifyRange = stringifyRange;
37
+ class IntersectionState {
38
+ type;
39
+ lastOperator;
40
+ path = new paths_js_1.Path();
41
+ domain;
42
+ #disjoints = {};
43
+ constructor(type, lastOperator) {
44
+ this.type = type;
45
+ this.lastOperator = lastOperator;
46
+ }
47
+ get disjoints() {
48
+ return this.#disjoints;
49
+ }
50
+ addDisjoint(kind, l, r) {
51
+ this.#disjoints[`${this.path}`] = { kind, l, r };
52
+ return empty;
53
+ }
54
+ }
55
+ exports.IntersectionState = IntersectionState;
56
+ const empty = Symbol("empty");
57
+ const anonymousDisjoint = () => empty;
58
+ exports.anonymousDisjoint = anonymousDisjoint;
59
+ const isDisjoint = (result) => result === empty;
60
+ exports.isDisjoint = isDisjoint;
61
+ const equal = Symbol("equal");
62
+ const equality = () => equal;
63
+ exports.equality = equality;
64
+ const isEquality = (result) => result === equal;
65
+ exports.isEquality = isEquality;
66
+ const composeKeyedIntersection = (reducer, config) => (l, r, state) => {
67
+ const result = {};
68
+ const keys = (0, generics_js_1.objectKeysOf)({ ...l, ...r });
69
+ let lImpliesR = true;
70
+ let rImpliesL = true;
71
+ for (const k of keys) {
72
+ const keyResult = typeof reducer === "function"
73
+ ? reducer(k, l[k], r[k], state)
74
+ : reducer[k](l[k], r[k], state);
75
+ if ((0, exports.isEquality)(keyResult)) {
76
+ if (l[k] !== undefined) {
77
+ result[k] = l[k];
78
+ }
79
+ }
80
+ else if ((0, exports.isDisjoint)(keyResult)) {
81
+ if (config.onEmpty === "omit") {
82
+ lImpliesR = false;
83
+ rImpliesL = false;
84
+ }
85
+ else {
86
+ return empty;
87
+ }
88
+ }
89
+ else {
90
+ if (keyResult !== undefined) {
91
+ result[k] = keyResult;
92
+ }
93
+ lImpliesR &&= keyResult === l[k];
94
+ rImpliesL &&= keyResult === r[k];
95
+ }
96
+ }
97
+ return lImpliesR ? (rImpliesL ? (0, exports.equality)() : l) : rImpliesL ? r : result;
98
+ };
99
+ exports.composeKeyedIntersection = composeKeyedIntersection;