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.
- package/README.md +68 -11
- package/dist/main.js +29 -0
- package/dist/nodes/branch.js +135 -0
- package/dist/nodes/compose.js +99 -0
- package/dist/{cjs/nodes → nodes}/discriminate.js +100 -92
- package/dist/nodes/node.js +134 -0
- package/dist/nodes/predicate.js +88 -0
- package/dist/nodes/rules/class.js +23 -0
- package/dist/nodes/rules/collapsibleSet.js +34 -0
- package/dist/nodes/rules/divisor.js +19 -0
- package/dist/nodes/rules/props.js +130 -0
- package/dist/nodes/rules/range.js +108 -0
- package/dist/nodes/rules/regex.js +17 -0
- package/dist/nodes/rules/rules.js +102 -0
- package/dist/parse/ast/ast.js +2 -0
- package/dist/{types/parse → parse}/ast/bound.d.ts +4 -1
- package/dist/parse/ast/bound.js +7 -0
- package/dist/parse/ast/config.js +14 -0
- package/dist/parse/ast/distributableFunction.js +40 -0
- package/dist/parse/ast/divisor.js +5 -0
- package/dist/parse/ast/intersection.js +21 -0
- package/dist/parse/ast/keyof.js +107 -0
- package/dist/parse/ast/morph.js +50 -0
- package/dist/parse/ast/narrow.js +17 -0
- package/dist/{types/parse → parse}/ast/node.d.ts +3 -1
- package/dist/parse/ast/node.js +2 -0
- package/dist/parse/ast/tuple.js +72 -0
- package/dist/parse/ast/union.js +5 -0
- package/dist/parse/ast/utils.js +2 -0
- package/dist/parse/definition.js +47 -0
- package/dist/parse/record.js +31 -0
- package/dist/parse/string/reduce/dynamic.js +159 -0
- package/dist/parse/string/reduce/shared.js +13 -0
- package/dist/parse/string/reduce/static.js +2 -0
- package/dist/parse/string/shift/operand/enclosed.js +37 -0
- package/dist/parse/string/shift/operand/operand.js +15 -0
- package/dist/parse/string/shift/operand/unenclosed.js +45 -0
- package/dist/parse/string/shift/operator/bounds.js +83 -0
- package/dist/parse/string/shift/operator/divisor.js +23 -0
- package/dist/parse/string/shift/operator/operator.js +34 -0
- package/dist/parse/string/shift/scanner.js +97 -0
- package/dist/parse/string/string.js +35 -0
- package/dist/scopes/ark.js +27 -0
- package/dist/scopes/cache.js +28 -0
- package/dist/{mjs/scopes → scopes}/expressions.js +20 -9
- package/dist/scopes/jsObjects.js +20 -0
- package/dist/scopes/scope.js +184 -0
- package/dist/scopes/tsKeywords.js +49 -0
- package/dist/scopes/type.js +40 -0
- package/dist/{mjs/scopes → scopes}/validation/creditCard.js +15 -11
- package/dist/{mjs/scopes → scopes}/validation/date.js +21 -16
- package/dist/scopes/validation/validation.js +74 -0
- package/dist/traverse/problems.js +234 -0
- package/dist/traverse/traverse.js +330 -0
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/{mjs/utils → utils}/chainableNoOpProxy.js +6 -2
- package/dist/utils/data.js +39 -0
- package/dist/utils/domains.js +27 -0
- package/dist/utils/errors.js +17 -0
- package/dist/utils/freeze.js +16 -0
- package/dist/utils/generics.js +53 -0
- package/dist/utils/numericLiterals.js +88 -0
- package/dist/utils/objectKinds.js +69 -0
- package/dist/{mjs/utils → utils}/paths.js +8 -3
- package/dist/utils/serialize.js +71 -0
- package/dist/utils/unionToTuple.js +2 -0
- package/package.json +25 -39
- package/dist/cjs/main.js +0 -38
- package/dist/cjs/nodes/branch.js +0 -140
- package/dist/cjs/nodes/compose.js +0 -157
- package/dist/cjs/nodes/node.js +0 -139
- package/dist/cjs/nodes/predicate.js +0 -86
- package/dist/cjs/nodes/rules/class.js +0 -25
- package/dist/cjs/nodes/rules/collapsibleSet.js +0 -48
- package/dist/cjs/nodes/rules/divisor.js +0 -29
- package/dist/cjs/nodes/rules/props.js +0 -160
- package/dist/cjs/nodes/rules/range.js +0 -105
- package/dist/cjs/nodes/rules/regex.js +0 -26
- package/dist/cjs/nodes/rules/rules.js +0 -123
- package/dist/cjs/package.json +0 -3
- package/dist/cjs/parse/ast/ast.js +0 -4
- package/dist/cjs/parse/ast/bound.js +0 -9
- package/dist/cjs/parse/ast/config.js +0 -13
- package/dist/cjs/parse/ast/distributableFunction.js +0 -48
- package/dist/cjs/parse/ast/divisor.js +0 -9
- package/dist/cjs/parse/ast/intersection.js +0 -30
- package/dist/cjs/parse/ast/keyof.js +0 -111
- package/dist/cjs/parse/ast/morph.js +0 -58
- package/dist/cjs/parse/ast/narrow.js +0 -22
- package/dist/cjs/parse/ast/node.js +0 -4
- package/dist/cjs/parse/ast/tuple.js +0 -94
- package/dist/cjs/parse/ast/union.js +0 -9
- package/dist/cjs/parse/ast/utils.js +0 -4
- package/dist/cjs/parse/definition.js +0 -61
- package/dist/cjs/parse/record.js +0 -37
- package/dist/cjs/parse/string/reduce/dynamic.js +0 -167
- package/dist/cjs/parse/string/reduce/shared.js +0 -23
- package/dist/cjs/parse/string/reduce/static.js +0 -4
- package/dist/cjs/parse/string/shift/operand/enclosed.js +0 -54
- package/dist/cjs/parse/string/shift/operand/operand.js +0 -11
- package/dist/cjs/parse/string/shift/operand/unenclosed.js +0 -53
- package/dist/cjs/parse/string/shift/operator/bounds.js +0 -87
- package/dist/cjs/parse/string/shift/operator/divisor.js +0 -35
- package/dist/cjs/parse/string/shift/operator/operator.js +0 -26
- package/dist/cjs/parse/string/shift/scanner.js +0 -114
- package/dist/cjs/parse/string/string.js +0 -43
- package/dist/cjs/scopes/ark.js +0 -45
- package/dist/cjs/scopes/cache.js +0 -52
- package/dist/cjs/scopes/expressions.js +0 -29
- package/dist/cjs/scopes/jsObjects.js +0 -93
- package/dist/cjs/scopes/scope.js +0 -323
- package/dist/cjs/scopes/tsKeywords.js +0 -113
- package/dist/cjs/scopes/type.js +0 -50
- package/dist/cjs/scopes/validation/creditCard.js +0 -47
- package/dist/cjs/scopes/validation/date.js +0 -62
- package/dist/cjs/scopes/validation/validation.js +0 -104
- package/dist/cjs/traverse/problems.js +0 -281
- package/dist/cjs/traverse/traverse.js +0 -387
- package/dist/cjs/utils/chainableNoOpProxy.js +0 -23
- package/dist/cjs/utils/data.js +0 -53
- package/dist/cjs/utils/domains.js +0 -30
- package/dist/cjs/utils/errors.js +0 -26
- package/dist/cjs/utils/freeze.js +0 -15
- package/dist/cjs/utils/generics.js +0 -63
- package/dist/cjs/utils/numericLiterals.js +0 -75
- package/dist/cjs/utils/objectKinds.js +0 -69
- package/dist/cjs/utils/paths.js +0 -32
- package/dist/cjs/utils/serialize.js +0 -74
- package/dist/cjs/utils/unionToTuple.js +0 -4
- package/dist/mjs/main.js +0 -8
- package/dist/mjs/nodes/branch.js +0 -123
- package/dist/mjs/nodes/compose.js +0 -135
- package/dist/mjs/nodes/discriminate.js +0 -247
- package/dist/mjs/nodes/node.js +0 -117
- package/dist/mjs/nodes/predicate.js +0 -69
- package/dist/mjs/nodes/rules/class.js +0 -11
- package/dist/mjs/nodes/rules/collapsibleSet.js +0 -34
- package/dist/mjs/nodes/rules/divisor.js +0 -15
- package/dist/mjs/nodes/rules/props.js +0 -142
- package/dist/mjs/nodes/rules/range.js +0 -86
- package/dist/mjs/nodes/rules/regex.js +0 -11
- package/dist/mjs/nodes/rules/rules.js +0 -106
- package/dist/mjs/package.json +0 -3
- package/dist/mjs/parse/ast/ast.js +0 -1
- package/dist/mjs/parse/ast/bound.js +0 -1
- package/dist/mjs/parse/ast/config.js +0 -9
- package/dist/mjs/parse/ast/distributableFunction.js +0 -34
- package/dist/mjs/parse/ast/divisor.js +0 -1
- package/dist/mjs/parse/ast/intersection.js +0 -16
- package/dist/mjs/parse/ast/keyof.js +0 -103
- package/dist/mjs/parse/ast/morph.js +0 -44
- package/dist/mjs/parse/ast/narrow.js +0 -14
- package/dist/mjs/parse/ast/node.js +0 -1
- package/dist/mjs/parse/ast/tuple.js +0 -80
- package/dist/mjs/parse/ast/union.js +0 -1
- package/dist/mjs/parse/ast/utils.js +0 -1
- package/dist/mjs/parse/definition.js +0 -46
- package/dist/mjs/parse/record.js +0 -29
- package/dist/mjs/parse/string/reduce/dynamic.js +0 -159
- package/dist/mjs/parse/string/reduce/shared.js +0 -6
- package/dist/mjs/parse/string/reduce/static.js +0 -1
- package/dist/mjs/parse/string/shift/operand/enclosed.js +0 -39
- package/dist/mjs/parse/string/shift/operand/operand.js +0 -3
- package/dist/mjs/parse/string/shift/operand/unenclosed.js +0 -36
- package/dist/mjs/parse/string/shift/operator/bounds.js +0 -70
- package/dist/mjs/parse/string/shift/operator/divisor.js +0 -21
- package/dist/mjs/parse/string/shift/operator/operator.js +0 -13
- package/dist/mjs/parse/string/shift/scanner.js +0 -106
- package/dist/mjs/parse/string/string.js +0 -28
- package/dist/mjs/scopes/ark.js +0 -28
- package/dist/mjs/scopes/cache.js +0 -38
- package/dist/mjs/scopes/jsObjects.js +0 -82
- package/dist/mjs/scopes/scope.js +0 -303
- package/dist/mjs/scopes/tsKeywords.js +0 -116
- package/dist/mjs/scopes/type.js +0 -35
- package/dist/mjs/scopes/validation/validation.js +0 -108
- package/dist/mjs/traverse/problems.js +0 -260
- package/dist/mjs/traverse/traverse.js +0 -370
- package/dist/mjs/utils/data.js +0 -38
- package/dist/mjs/utils/domains.js +0 -15
- package/dist/mjs/utils/errors.js +0 -10
- package/dist/mjs/utils/freeze.js +0 -7
- package/dist/mjs/utils/generics.js +0 -38
- package/dist/mjs/utils/numericLiterals.js +0 -72
- package/dist/mjs/utils/objectKinds.js +0 -55
- package/dist/mjs/utils/serialize.js +0 -58
- package/dist/mjs/utils/unionToTuple.js +0 -1
- /package/dist/{types/main.d.ts → main.d.ts} +0 -0
- /package/dist/{types/nodes → nodes}/branch.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/compose.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/discriminate.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/node.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/predicate.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/rules/class.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/rules/collapsibleSet.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/rules/divisor.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/rules/props.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/rules/range.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/rules/regex.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/rules/rules.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/ast.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/config.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/distributableFunction.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/divisor.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/intersection.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/keyof.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/morph.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/narrow.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/tuple.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/union.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/utils.d.ts +0 -0
- /package/dist/{types/parse → parse}/definition.d.ts +0 -0
- /package/dist/{types/parse → parse}/record.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/reduce/dynamic.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/reduce/shared.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/reduce/static.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/shift/operand/enclosed.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/shift/operand/operand.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/shift/operand/unenclosed.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/shift/operator/bounds.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/shift/operator/divisor.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/shift/operator/operator.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/shift/scanner.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/string.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/ark.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/cache.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/expressions.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/jsObjects.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/scope.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/tsKeywords.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/type.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/validation/creditCard.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/validation/date.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/validation/validation.d.ts +0 -0
- /package/dist/{types/traverse → traverse}/problems.d.ts +0 -0
- /package/dist/{types/traverse → traverse}/traverse.d.ts +0 -0
- /package/dist/{types/utils → utils}/chainableNoOpProxy.d.ts +0 -0
- /package/dist/{types/utils → utils}/data.d.ts +0 -0
- /package/dist/{types/utils → utils}/domains.d.ts +0 -0
- /package/dist/{types/utils → utils}/errors.d.ts +0 -0
- /package/dist/{types/utils → utils}/freeze.d.ts +0 -0
- /package/dist/{types/utils → utils}/generics.d.ts +0 -0
- /package/dist/{types/utils → utils}/numericLiterals.d.ts +0 -0
- /package/dist/{types/utils → utils}/objectKinds.d.ts +0 -0
- /package/dist/{types/utils → utils}/paths.d.ts +0 -0
- /package/dist/{types/utils → utils}/serialize.d.ts +0 -0
- /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 "
|
|
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 "
|
|
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/
|
|
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/
|
|
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
|
-
[
|
|
208
|
-
|
|
209
|
-
|
|
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;
|