arktype 1.0.3-alpha β 1.0.5-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 +100 -27
- package/dist/cjs/main.js +1 -1
- package/dist/cjs/nodes/compose.js +3 -3
- package/dist/cjs/nodes/node.js +2 -2
- package/dist/cjs/parse/ast/keyof.js +3 -2
- package/dist/cjs/parse/definition.js +1 -1
- package/dist/cjs/parse/string/shift/operand/operand.js +1 -1
- package/dist/cjs/parse/string/shift/operator/operator.js +1 -1
- package/dist/cjs/scopes/ark.js +5 -5
- package/dist/cjs/scopes/type.js +4 -12
- package/dist/cjs/traverse/problems.js +3 -1
- package/dist/cjs/traverse/traverse.js +56 -11
- package/dist/cjs/utils/generics.js +0 -5
- package/dist/mjs/main.js +1 -1
- package/dist/mjs/nodes/compose.js +2 -2
- package/dist/mjs/nodes/node.js +4 -4
- package/dist/mjs/parse/ast/keyof.js +3 -2
- package/dist/mjs/parse/definition.js +1 -1
- package/dist/mjs/parse/string/shift/operand/operand.js +1 -1
- package/dist/mjs/parse/string/shift/operator/operator.js +3 -1
- package/dist/mjs/scopes/ark.js +4 -4
- package/dist/mjs/scopes/type.js +5 -12
- package/dist/mjs/traverse/problems.js +3 -1
- package/dist/mjs/traverse/traverse.js +55 -11
- package/dist/mjs/utils/generics.js +0 -4
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/main.d.ts +2 -1
- package/dist/types/nodes/compose.d.ts +1 -1
- package/dist/types/parse/ast/morph.d.ts +2 -1
- package/dist/types/parse/definition.d.ts +5 -5
- package/dist/types/parse/string/shift/operand/operand.d.ts +1 -1
- package/dist/types/parse/string/shift/operand/unenclosed.d.ts +2 -2
- package/dist/types/scopes/ark.d.ts +1 -1
- package/dist/types/scopes/type.d.ts +5 -10
- package/dist/types/traverse/problems.d.ts +2 -2
- package/dist/types/traverse/traverse.d.ts +8 -1
- package/dist/types/utils/generics.d.ts +2 -3
- package/dist/types/utils/numericLiterals.d.ts +2 -2
- package/package.json +16 -8
package/README.md
CHANGED
|
@@ -1,31 +1,35 @@
|
|
|
1
|
-
<
|
|
2
|
-
<img src="./dev/arktype.io/static/img/logo.svg" height="64px" />
|
|
3
|
-
<h1>ArkType</h1>
|
|
4
|
-
</div>
|
|
5
|
-
<div align="center">
|
|
1
|
+
<h1 align="center">ArkType <sub><sup>Isomorphic types for TS/JS</sup></sub></h1>
|
|
6
2
|
|
|
7
|
-
|
|
3
|
+
[<img src="./dev/arktype.io/static/img/arktype.gif">](https://arktype.io/try)
|
|
4
|
+
<sub><i>`typescript@4.9.5` in VS Codeβ no extensions or plugins required (<a href="#how">how?</a>)</i></sub>
|
|
8
5
|
|
|
9
|
-
</
|
|
6
|
+
<img src="./dev/arktype.io/static/img/logo.svg" alt="ArkType Icon" height="16px"/><b> Try this demo in-browser at <a href="https://arktype.io/try">arktype.io/try</a></b>
|
|
10
7
|
|
|
11
|
-
|
|
8
|
+
<!-- @snipStart:install -->
|
|
12
9
|
|
|
13
|
-
##
|
|
10
|
+
## Install <sub><sub>π¦`4KB` gzipped, `0` dependencies</sub></sub>
|
|
14
11
|
|
|
15
|
-
|
|
12
|
+
<img src="./dev/arktype.io/static/img/npm.svg" alt="Npm Icon" height="16px" />
|
|
13
|
+
<code>npm install arktype</code>
|
|
14
|
+
<sub>(or whatever package manager you prefer)</sub>
|
|
15
|
+
<br />
|
|
16
|
+
<img src="./dev/arktype.io/static/img/deno.svg" alt="Deno Icon" height="16px" />
|
|
17
|
+
<code>import type from "https://deno.land/x/arktype"</code>
|
|
16
18
|
|
|
17
|
-
|
|
19
|
+
Our types are tested in [strict-mode](https://www.typescriptlang.org/tsconfig#strict) with TypeScript versions `4.8`, `4.9`, and `5.0`.
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
_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!_ β΅
|
|
20
22
|
|
|
21
|
-
|
|
23
|
+
<!-- @snipEnd -->
|
|
22
24
|
|
|
23
|
-
|
|
25
|
+
## Types
|
|
24
26
|
|
|
25
|
-
|
|
26
|
-
import { type } from "../src/main.ts"
|
|
27
|
+
[Try it in-browser.](https://arktype.io/docs/#your-first-type)
|
|
27
28
|
|
|
28
|
-
|
|
29
|
+
```ts @blockFrom:dev/examples/type.ts
|
|
30
|
+
import { type } from "arktype"
|
|
31
|
+
|
|
32
|
+
// Define your type...
|
|
29
33
|
export const user = type({
|
|
30
34
|
name: "string",
|
|
31
35
|
device: {
|
|
@@ -37,7 +41,7 @@ export const user = type({
|
|
|
37
41
|
// Infer it...
|
|
38
42
|
export type User = typeof user.infer
|
|
39
43
|
|
|
40
|
-
//
|
|
44
|
+
// Get validated data or clear, customizable error messages.
|
|
41
45
|
export const { data, problems } = user({
|
|
42
46
|
name: "Alan Turing",
|
|
43
47
|
device: {
|
|
@@ -53,10 +57,10 @@ if (problems) {
|
|
|
53
57
|
|
|
54
58
|
### Scopes
|
|
55
59
|
|
|
56
|
-
[Try it
|
|
60
|
+
[Try it in-browser.](https://arktype.io/docs/scopes)
|
|
57
61
|
|
|
58
|
-
```ts @blockFrom:examples/scope.ts
|
|
59
|
-
import { scope } from "
|
|
62
|
+
```ts @blockFrom:dev/examples/scope.ts
|
|
63
|
+
import { scope } from "arktype"
|
|
60
64
|
|
|
61
65
|
// Scopes are collections of types that can reference each other.
|
|
62
66
|
export const types = scope({
|
|
@@ -86,16 +90,85 @@ packageData.dependencies![0].dependencies = [packageData]
|
|
|
86
90
|
export const { data, problems } = types.package(packageData)
|
|
87
91
|
```
|
|
88
92
|
|
|
89
|
-
|
|
93
|
+
### API
|
|
94
|
+
|
|
95
|
+
<!--@snipStart:api -->
|
|
96
|
+
|
|
97
|
+
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 β΅
|
|
98
|
+
|
|
99
|
+
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) and don't hesitate to reach out on our [Discord channel](https://discord.gg/WSNF3Kc4xh)!
|
|
100
|
+
|
|
101
|
+
<!--@snipEnd -->
|
|
102
|
+
|
|
103
|
+
## How
|
|
104
|
+
|
|
105
|
+
ArkType's isomorphic parser has parallel static and dynamic implementations. This means as soon as you type a definition in your editor, you'll know the eventual result at runtime.
|
|
106
|
+
|
|
107
|
+
If you're curious, below is an example of what that looks like under the hood. If not, close that hood back up, `npm install arktype` and enjoy top-notch developer experience π§βπ»
|
|
108
|
+
|
|
109
|
+
```ts @blockFrom:src/parse/string/shift/operator/operator.ts:parseOperator
|
|
110
|
+
export const parseOperator = (s: DynamicState): void => {
|
|
111
|
+
const lookahead = s.scanner.shift()
|
|
112
|
+
return lookahead === ""
|
|
113
|
+
? s.finalize()
|
|
114
|
+
: lookahead === "["
|
|
115
|
+
? s.scanner.shift() === "]"
|
|
116
|
+
? s.rootToArray()
|
|
117
|
+
: s.error(incompleteArrayTokenMessage)
|
|
118
|
+
: isKeyOf(lookahead, Scanner.branchTokens)
|
|
119
|
+
? s.pushRootToBranch(lookahead)
|
|
120
|
+
: lookahead === ")"
|
|
121
|
+
? s.finalizeGroup()
|
|
122
|
+
: isKeyOf(lookahead, Scanner.comparatorStartChars)
|
|
123
|
+
? parseBound(s, lookahead)
|
|
124
|
+
: lookahead === "%"
|
|
125
|
+
? parseDivisor(s)
|
|
126
|
+
: lookahead === " "
|
|
127
|
+
? parseOperator(s)
|
|
128
|
+
: throwInternalError(writeUnexpectedCharacterMessage(lookahead))
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export type parseOperator<s extends StaticState> =
|
|
132
|
+
s["unscanned"] extends Scanner.shift<infer lookahead, infer unscanned>
|
|
133
|
+
? lookahead extends "["
|
|
134
|
+
? unscanned extends Scanner.shift<"]", infer nextUnscanned>
|
|
135
|
+
? state.setRoot<s, [s["root"], "[]"], nextUnscanned>
|
|
136
|
+
: error<incompleteArrayTokenMessage>
|
|
137
|
+
: lookahead extends Scanner.BranchToken
|
|
138
|
+
? state.reduceBranch<s, lookahead, unscanned>
|
|
139
|
+
: lookahead extends ")"
|
|
140
|
+
? state.finalizeGroup<s, unscanned>
|
|
141
|
+
: lookahead extends Scanner.ComparatorStartChar
|
|
142
|
+
? parseBound<s, lookahead, unscanned>
|
|
143
|
+
: lookahead extends "%"
|
|
144
|
+
? parseDivisor<s, unscanned>
|
|
145
|
+
: lookahead extends " "
|
|
146
|
+
? parseOperator<state.scanTo<s, unscanned>>
|
|
147
|
+
: error<writeUnexpectedCharacterMessage<lookahead>>
|
|
148
|
+
: state.finalize<s>
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Contributions
|
|
152
|
+
|
|
153
|
+
We accept and encourage pull requests from outside ArkType.
|
|
154
|
+
|
|
155
|
+
Depending on your level of familiarity with type systems and TS generics, some parts of the codebase may be hard to jump into. That said, there's plenty of opportunities for more straightforward contributions.
|
|
156
|
+
|
|
157
|
+
If you're planning on submitting a non-trivial fix or a new feature, please [create an issue first](https://github.com/arktypeio/arktype/issues/new) so everyone's on the same page. The last thing we want is for you to spend time on a submission we're unable to merge.
|
|
158
|
+
|
|
159
|
+
When you're ready, check out our [guide](./dev/configs/CONTRIBUTING.md) to get started!
|
|
160
|
+
|
|
161
|
+
## Sponsorship
|
|
162
|
+
|
|
163
|
+
We've been working full-time on this project for over a year and it means a lot to have the community behind us.
|
|
90
164
|
|
|
91
|
-
|
|
165
|
+
If the project has been useful to you and you are in a financial position to do so, please feel free to chip in [via our Patreon](https://www.patreon.com/ArkType240).
|
|
92
166
|
|
|
93
|
-
|
|
167
|
+
Otherwise, consider sending me an email (david@arktype.io) letting me know you're a fan of ArkType. Either would make my day! π
|
|
94
168
|
|
|
95
|
-
|
|
169
|
+
## Collaboration
|
|
96
170
|
|
|
97
|
-
|
|
98
|
-
2. Check out our [guide](./CONTRIBUTING.md) to get started!
|
|
171
|
+
I'd love to hear about what you're working on and how ArkType can help. Please reach out to david@arktype.io.
|
|
99
172
|
|
|
100
173
|
## License
|
|
101
174
|
|
package/dist/cjs/main.js
CHANGED
|
@@ -12,7 +12,7 @@ _export(exports, {
|
|
|
12
12
|
scope: ()=>_scopeTs.scope,
|
|
13
13
|
type: ()=>_arkTs.type,
|
|
14
14
|
ark: ()=>_arkTs.ark,
|
|
15
|
-
|
|
15
|
+
arkScope: ()=>_arkTs.arkScope,
|
|
16
16
|
intersection: ()=>_expressionsTs.intersection,
|
|
17
17
|
union: ()=>_expressionsTs.union,
|
|
18
18
|
arrayOf: ()=>_expressionsTs.arrayOf,
|
|
@@ -10,7 +10,7 @@ function _export(target, all) {
|
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
12
|
composeIntersection: ()=>composeIntersection,
|
|
13
|
-
|
|
13
|
+
undefinedOperandsMessage: ()=>undefinedOperandsMessage,
|
|
14
14
|
disjointDescriptionWriters: ()=>disjointDescriptionWriters,
|
|
15
15
|
stringifyRange: ()=>stringifyRange,
|
|
16
16
|
IntersectionState: ()=>IntersectionState,
|
|
@@ -64,8 +64,8 @@ function _classPrivateFieldSet(receiver, privateMap, value) {
|
|
|
64
64
|
_classApplyDescriptorSet(receiver, descriptor, value);
|
|
65
65
|
return value;
|
|
66
66
|
}
|
|
67
|
-
const composeIntersection = (reducer)=>(l, r, state)=>l === undefined ? r === undefined ?
|
|
68
|
-
const
|
|
67
|
+
const composeIntersection = (reducer)=>(l, r, state)=>l === undefined ? r === undefined ? (0, _errorsTs.throwInternalError)(undefinedOperandsMessage) : r : r === undefined ? l : reducer(l, r, state);
|
|
68
|
+
const undefinedOperandsMessage = `Unexpected operation two undefined operands`;
|
|
69
69
|
const disjointDescriptionWriters = {
|
|
70
70
|
domain: ({ l , r })=>`${l.join(", ")} and ${r.join(", ")}`,
|
|
71
71
|
range: ({ l , r })=>`${stringifyRange(l)} and ${stringifyRange(r)}`,
|
package/dist/cjs/nodes/node.js
CHANGED
|
@@ -40,7 +40,7 @@ const nodeIntersection = (l, r, state)=>{
|
|
|
40
40
|
};
|
|
41
41
|
const typeNodeIntersection = (0, _composeTs.composeKeyedIntersection)((domain, l, r, context)=>{
|
|
42
42
|
if (l === undefined) {
|
|
43
|
-
return r === undefined ? (0,
|
|
43
|
+
return r === undefined ? (0, _errorsTs.throwInternalError)(_composeTs.undefinedOperandsMessage) : undefined;
|
|
44
44
|
}
|
|
45
45
|
if (r === undefined) {
|
|
46
46
|
return undefined;
|
|
@@ -63,7 +63,7 @@ const rootUnion = (l, r, type)=>{
|
|
|
63
63
|
...rDomains
|
|
64
64
|
});
|
|
65
65
|
for (const domain of domains){
|
|
66
|
-
result[domain] = (0, _genericsTs.hasKey)(lDomains, domain) ? (0, _genericsTs.hasKey)(rDomains, domain) ? (0, _predicateTs.predicateUnion)(domain, lDomains[domain], rDomains[domain], type) : lDomains[domain] : (0, _genericsTs.hasKey)(rDomains, domain) ? rDomains[domain] : (0,
|
|
66
|
+
result[domain] = (0, _genericsTs.hasKey)(lDomains, domain) ? (0, _genericsTs.hasKey)(rDomains, domain) ? (0, _predicateTs.predicateUnion)(domain, lDomains[domain], rDomains[domain], type) : lDomains[domain] : (0, _genericsTs.hasKey)(rDomains, domain) ? rDomains[domain] : (0, _errorsTs.throwInternalError)(_composeTs.undefinedOperandsMessage);
|
|
67
67
|
}
|
|
68
68
|
return result;
|
|
69
69
|
};
|
|
@@ -23,7 +23,7 @@ const arrayIndexNumberBranch = (0, _freezeTs.deepFreeze)({
|
|
|
23
23
|
range: {
|
|
24
24
|
min: {
|
|
25
25
|
comparator: ">=",
|
|
26
|
-
limit:
|
|
26
|
+
limit: 0
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
29
|
divisor: 1
|
|
@@ -51,7 +51,7 @@ const parseKeyOfTuple = (def, ctx)=>{
|
|
|
51
51
|
} else {
|
|
52
52
|
return (0, _errorsTs.throwInternalError)(`Unexpected keyof key '${(0, _serializeTs.stringify)(key)}'`);
|
|
53
53
|
}
|
|
54
|
-
}
|
|
54
|
+
/* c8 ignore stop*/ }
|
|
55
55
|
return Object.fromEntries(Object.entries(keyNode).map(([domain, branches])=>[
|
|
56
56
|
domain,
|
|
57
57
|
branches.length === 1 ? branches[0] : branches
|
|
@@ -87,6 +87,7 @@ const keysOfObjectBranch = (branch)=>{
|
|
|
87
87
|
if ("props" in branch) {
|
|
88
88
|
for (const key of Object.keys(branch.props)){
|
|
89
89
|
if (key === _propsTs.mappedKeys.index) {
|
|
90
|
+
// if any number is a valid key push this RegExp
|
|
90
91
|
result.push(_numericLiteralsTs.wellFormedNonNegativeIntegerMatcher);
|
|
91
92
|
} else if (!result.includes(key)) {
|
|
92
93
|
result.push(key);
|
|
@@ -58,6 +58,6 @@ const parseDefinition = (def, ctx)=>{
|
|
|
58
58
|
}
|
|
59
59
|
};
|
|
60
60
|
const as = Symbol("as");
|
|
61
|
-
const unknownDefinitionMessage = "Cannot statically parse a definition inferred as unknown. Consider using 'as
|
|
61
|
+
const unknownDefinitionMessage = "Cannot statically parse a definition inferred as unknown. Consider using 'as Infer<...>' to cast it.";
|
|
62
62
|
const isThunk = (def)=>typeof def === "function" && def.length === 0;
|
|
63
63
|
const writeBadDefinitionTypeMessage = (actual)=>`Type definitions must be strings or objects (was ${actual})`;
|
|
@@ -8,4 +8,4 @@ Object.defineProperty(exports, "parseOperand", {
|
|
|
8
8
|
});
|
|
9
9
|
const _enclosedTs = require("./enclosed.js");
|
|
10
10
|
const _unenclosedTs = require("./unenclosed.js");
|
|
11
|
-
const parseOperand = (s)=>s.scanner.lookahead === "" ? s.error((0, _unenclosedTs.writeMissingOperandMessage)(s)) : s.scanner.lookahead === "(" ? s.shiftedByOne().reduceGroupOpen() : s.scanner.lookaheadIsIn(_enclosedTs.enclosingChar) ? (0, _enclosedTs.parseEnclosed)(s, s.scanner.shift()) : (0, _unenclosedTs.parseUnenclosed)(s);
|
|
11
|
+
const parseOperand = (s)=>s.scanner.lookahead === "" ? s.error((0, _unenclosedTs.writeMissingOperandMessage)(s)) : s.scanner.lookahead === "(" ? s.shiftedByOne().reduceGroupOpen() : s.scanner.lookaheadIsIn(_enclosedTs.enclosingChar) ? (0, _enclosedTs.parseEnclosed)(s, s.scanner.shift()) : s.scanner.lookahead === " " ? parseOperand(s.shiftedByOne()) : (0, _unenclosedTs.parseUnenclosed)(s);
|
|
@@ -20,7 +20,7 @@ const _boundsTs = require("./bounds.js");
|
|
|
20
20
|
const _divisorTs = require("./divisor.js");
|
|
21
21
|
const parseOperator = (s)=>{
|
|
22
22
|
const lookahead = s.scanner.shift();
|
|
23
|
-
return lookahead === "" ? s.finalize() : lookahead === "[" ? s.scanner.shift() === "]" ? s.rootToArray() : s.error(incompleteArrayTokenMessage) : (0, _genericsTs.isKeyOf)(lookahead, _scannerTs.Scanner.branchTokens) ? s.pushRootToBranch(lookahead) : lookahead === ")" ? s.finalizeGroup() : (0, _genericsTs.isKeyOf)(lookahead, _scannerTs.Scanner.comparatorStartChars) ? (0, _boundsTs.parseBound)(s, lookahead) : lookahead === "%" ? (0, _divisorTs.parseDivisor)(s) : lookahead === " " ? parseOperator(s) :
|
|
23
|
+
return lookahead === "" ? s.finalize() : lookahead === "[" ? s.scanner.shift() === "]" ? s.rootToArray() : s.error(incompleteArrayTokenMessage) : (0, _genericsTs.isKeyOf)(lookahead, _scannerTs.Scanner.branchTokens) ? s.pushRootToBranch(lookahead) : lookahead === ")" ? s.finalizeGroup() : (0, _genericsTs.isKeyOf)(lookahead, _scannerTs.Scanner.comparatorStartChars) ? (0, _boundsTs.parseBound)(s, lookahead) : lookahead === "%" ? (0, _divisorTs.parseDivisor)(s) : lookahead === " " ? parseOperator(s) : (0, _errorsTs.throwInternalError)(writeUnexpectedCharacterMessage(lookahead));
|
|
24
24
|
};
|
|
25
25
|
const writeUnexpectedCharacterMessage = (char)=>`Unexpected character '${char}'`;
|
|
26
26
|
const incompleteArrayTokenMessage = `Missing expected ']'`;
|
package/dist/cjs/scopes/ark.js
CHANGED
|
@@ -9,7 +9,7 @@ function _export(target, all) {
|
|
|
9
9
|
});
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
|
-
|
|
12
|
+
arkScope: ()=>arkScope,
|
|
13
13
|
ark: ()=>ark,
|
|
14
14
|
scopes: ()=>scopes,
|
|
15
15
|
spaces: ()=>spaces,
|
|
@@ -19,7 +19,7 @@ const _jsObjectsTs = require("./jsObjects.js");
|
|
|
19
19
|
const _scopeTs = require("./scope.js");
|
|
20
20
|
const _tsKeywordsTs = require("./tsKeywords.js");
|
|
21
21
|
const _validationTs = require("./validation/validation.js");
|
|
22
|
-
const
|
|
22
|
+
const arkScope = (0, _scopeTs.scope)({}, {
|
|
23
23
|
name: "standard",
|
|
24
24
|
includes: [
|
|
25
25
|
_tsKeywordsTs.tsKeywords,
|
|
@@ -28,13 +28,13 @@ const arkscope = (0, _scopeTs.scope)({}, {
|
|
|
28
28
|
],
|
|
29
29
|
standard: false
|
|
30
30
|
});
|
|
31
|
-
const ark =
|
|
31
|
+
const ark = arkScope.compile();
|
|
32
32
|
const scopes = {
|
|
33
33
|
root: _scopeTs.rootScope,
|
|
34
34
|
tsKeywords: _tsKeywordsTs.tsKeywordsScope,
|
|
35
35
|
jsObjects: _jsObjectsTs.jsObjectsScope,
|
|
36
36
|
validation: _validationTs.validationScope,
|
|
37
|
-
ark:
|
|
37
|
+
ark: arkScope
|
|
38
38
|
};
|
|
39
39
|
const spaces = {
|
|
40
40
|
tsKeywords: _tsKeywordsTs.tsKeywords,
|
|
@@ -42,4 +42,4 @@ const spaces = {
|
|
|
42
42
|
validation: _validationTs.validation,
|
|
43
43
|
ark
|
|
44
44
|
};
|
|
45
|
-
const type =
|
|
45
|
+
const type = arkScope.type;
|
package/dist/cjs/scopes/type.js
CHANGED
|
@@ -11,22 +11,11 @@ function _export(target, all) {
|
|
|
11
11
|
_export(exports, {
|
|
12
12
|
initializeType: ()=>initializeType,
|
|
13
13
|
isType: ()=>isType,
|
|
14
|
-
isCheckResult: ()=>isCheckResult,
|
|
15
14
|
isAnonymousName: ()=>isAnonymousName
|
|
16
15
|
});
|
|
17
16
|
const _traverseTs = require("../traverse/traverse.js");
|
|
18
17
|
const _chainableNoOpProxyTs = require("../utils/chainableNoOpProxy.js");
|
|
19
|
-
const _genericsTs = require("../utils/generics.js");
|
|
20
18
|
const initializeType = (name, definition, config, scope)=>{
|
|
21
|
-
const namedTraverse = (0, _genericsTs.composeNamed)((data)=>{
|
|
22
|
-
const state = new _traverseTs.TraversalState(data, t);
|
|
23
|
-
(0, _traverseTs.traverse)(t.flat, state);
|
|
24
|
-
return state.problems.count ? {
|
|
25
|
-
problems: state.problems
|
|
26
|
-
} : {
|
|
27
|
-
data: state.data
|
|
28
|
-
};
|
|
29
|
-
}, name);
|
|
30
19
|
const root = {
|
|
31
20
|
// temporarily initialize node/flat to aliases that will be included in
|
|
32
21
|
// the final type in case of cyclic resolutions
|
|
@@ -45,9 +34,12 @@ const initializeType = (name, definition, config, scope)=>{
|
|
|
45
34
|
includesMorph: false,
|
|
46
35
|
config
|
|
47
36
|
};
|
|
37
|
+
// define within a key to dynamically assign a name to the function
|
|
38
|
+
const namedTraverse = {
|
|
39
|
+
[name]: (data)=>(0, _traverseTs.traverseRoot)(namedTraverse, data)
|
|
40
|
+
}[name];
|
|
48
41
|
const t = Object.assign(namedTraverse, root);
|
|
49
42
|
return t;
|
|
50
43
|
};
|
|
51
44
|
const isType = (value)=>value?.infer === _chainableNoOpProxyTs.chainableNoOpProxy;
|
|
52
|
-
const isCheckResult = (value)=>(0, _genericsTs.getNominalId)(value) !== undefined;
|
|
53
45
|
const isAnonymousName = (name)=>name[0] === "Ξ»";
|
|
@@ -108,7 +108,9 @@ class ProblemArray extends Array {
|
|
|
108
108
|
const data = // we have to check for the presence of the key explicitly since the
|
|
109
109
|
// data could be undefined or null
|
|
110
110
|
opts && "data" in opts ? opts.data : _classPrivateFieldGet(this, _state).data;
|
|
111
|
-
const problem = new Problem(
|
|
111
|
+
const problem = new Problem(// avoid a bunch of errors from TS trying to discriminate the
|
|
112
|
+
// problem input based on the code
|
|
113
|
+
code, path, data, source, _classPrivateFieldGet(this, _state).getProblemConfig(code));
|
|
112
114
|
this.addProblem(problem);
|
|
113
115
|
return problem;
|
|
114
116
|
}
|
|
@@ -9,6 +9,7 @@ function _export(target, all) {
|
|
|
9
9
|
});
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
|
+
traverseRoot: ()=>traverseRoot,
|
|
12
13
|
TraversalState: ()=>TraversalState,
|
|
13
14
|
traverse: ()=>traverse,
|
|
14
15
|
checkEntries: ()=>checkEntries,
|
|
@@ -20,7 +21,6 @@ const _divisorTs = require("../nodes/rules/divisor.js");
|
|
|
20
21
|
const _rangeTs = require("../nodes/rules/range.js");
|
|
21
22
|
const _regexTs = require("../nodes/rules/regex.js");
|
|
22
23
|
const _rulesTs = require("../nodes/rules/rules.js");
|
|
23
|
-
const _typeTs = require("../scopes/type.js");
|
|
24
24
|
const _domainsTs = require("../utils/domains.js");
|
|
25
25
|
const _errorsTs = require("../utils/errors.js");
|
|
26
26
|
const _genericsTs = require("../utils/generics.js");
|
|
@@ -77,6 +77,22 @@ const problemWriterKeys = [
|
|
|
77
77
|
"writeReason",
|
|
78
78
|
"addContext"
|
|
79
79
|
];
|
|
80
|
+
const traverseRoot = (t, data)=>{
|
|
81
|
+
const state = new TraversalState(data, t);
|
|
82
|
+
traverse(t.flat, state);
|
|
83
|
+
const result = new CheckResult(state);
|
|
84
|
+
if (state.problems.count) {
|
|
85
|
+
result.problems = state.problems;
|
|
86
|
+
} else {
|
|
87
|
+
for (const [o, k] of state.entriesToPrune){
|
|
88
|
+
delete o[k];
|
|
89
|
+
}
|
|
90
|
+
result.data = state.data;
|
|
91
|
+
}
|
|
92
|
+
return result;
|
|
93
|
+
};
|
|
94
|
+
const CheckResult = class {
|
|
95
|
+
};
|
|
80
96
|
var _seen = /*#__PURE__*/ new WeakMap();
|
|
81
97
|
class TraversalState {
|
|
82
98
|
getProblemConfig(code) {
|
|
@@ -105,7 +121,9 @@ class TraversalState {
|
|
|
105
121
|
this.path.push(key);
|
|
106
122
|
const isValid = traverse(node, this);
|
|
107
123
|
this.path.pop();
|
|
108
|
-
lastData[key]
|
|
124
|
+
if (lastData[key] !== this.data) {
|
|
125
|
+
lastData[key] = this.data;
|
|
126
|
+
}
|
|
109
127
|
this.data = lastData;
|
|
110
128
|
return isValid;
|
|
111
129
|
}
|
|
@@ -147,15 +165,19 @@ class TraversalState {
|
|
|
147
165
|
const branchProblems = new _problemsTs.Problems(this);
|
|
148
166
|
this.problems = branchProblems;
|
|
149
167
|
const lastPath = this.path;
|
|
168
|
+
const lastKeysToPrune = this.entriesToPrune;
|
|
150
169
|
let hasValidBranch = false;
|
|
151
170
|
for (const branch of branches){
|
|
152
171
|
this.path = new _pathsTs.Path();
|
|
172
|
+
this.entriesToPrune = [];
|
|
153
173
|
if (checkEntries(branch, this)) {
|
|
154
174
|
hasValidBranch = true;
|
|
175
|
+
lastKeysToPrune.push(...this.entriesToPrune);
|
|
155
176
|
break;
|
|
156
177
|
}
|
|
157
178
|
}
|
|
158
179
|
this.path = lastPath;
|
|
180
|
+
this.entriesToPrune = lastKeysToPrune;
|
|
159
181
|
this.problems = lastProblems;
|
|
160
182
|
this.failFast = lastFailFast;
|
|
161
183
|
return hasValidBranch || !this.problems.add("branches", branchProblems);
|
|
@@ -169,6 +191,7 @@ class TraversalState {
|
|
|
169
191
|
this.type = type;
|
|
170
192
|
this.path = new _pathsTs.Path();
|
|
171
193
|
this.problems = new _problemsTs.Problems(this);
|
|
194
|
+
this.entriesToPrune = [];
|
|
172
195
|
this.failFast = false;
|
|
173
196
|
this.traversalConfig = initializeTraversalConfig();
|
|
174
197
|
_classPrivateFieldSet(this, _seen, {});
|
|
@@ -208,27 +231,49 @@ const checkRequiredProp = (prop, state)=>{
|
|
|
208
231
|
};
|
|
209
232
|
const createPropChecker = (kind)=>(props, state)=>{
|
|
210
233
|
let isValid = true;
|
|
211
|
-
const
|
|
234
|
+
const remainingUnseenRequired = {
|
|
212
235
|
...props.required
|
|
213
236
|
};
|
|
214
237
|
for(const k in state.data){
|
|
215
238
|
if (props.required[k]) {
|
|
216
|
-
isValid
|
|
217
|
-
delete
|
|
239
|
+
isValid && (isValid = state.traverseKey(k, props.required[k]));
|
|
240
|
+
delete remainingUnseenRequired[k];
|
|
218
241
|
} else if (props.optional[k]) {
|
|
219
|
-
isValid
|
|
242
|
+
isValid && (isValid = state.traverseKey(k, props.optional[k]));
|
|
220
243
|
} else if (kind === "distilledProps") {
|
|
221
|
-
|
|
244
|
+
if (state.failFast) {
|
|
245
|
+
// If we're in a union (i.e. failFast is enabled) in
|
|
246
|
+
// distilled mode, we need to wait to prune distilled keys
|
|
247
|
+
// to avoid mutating data based on a branch that will not be
|
|
248
|
+
// included in the final result. Instead, we push the object
|
|
249
|
+
// and key to state to handle after traversal is complete.
|
|
250
|
+
state.entriesToPrune.push([
|
|
251
|
+
state.data,
|
|
252
|
+
k
|
|
253
|
+
]);
|
|
254
|
+
} else {
|
|
255
|
+
// If we're not in a union, we can safely distill right away
|
|
256
|
+
delete state.data[k];
|
|
257
|
+
}
|
|
222
258
|
} else {
|
|
259
|
+
isValid = false;
|
|
223
260
|
state.problems.add("extraneous", state.data[k], {
|
|
224
261
|
path: state.path.concat(k)
|
|
225
262
|
});
|
|
226
|
-
isValid = false;
|
|
227
263
|
}
|
|
228
264
|
if (!isValid && state.failFast) {
|
|
229
265
|
return false;
|
|
230
266
|
}
|
|
231
267
|
}
|
|
268
|
+
const unseenRequired = Object.keys(remainingUnseenRequired);
|
|
269
|
+
if (unseenRequired.length) {
|
|
270
|
+
for (const k of unseenRequired){
|
|
271
|
+
state.problems.add("missing", undefined, {
|
|
272
|
+
path: state.path.concat(k)
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
232
277
|
return isValid;
|
|
233
278
|
};
|
|
234
279
|
const entryCheckers = {
|
|
@@ -273,8 +318,8 @@ const entryCheckers = {
|
|
|
273
318
|
}
|
|
274
319
|
const caseKeys = (0, _genericsTs.objectKeysOf)(rule.cases);
|
|
275
320
|
const missingCasePath = state.path.concat(rule.path);
|
|
276
|
-
const caseDescriptions = rule.kind === "value" ? caseKeys : rule.kind === "domain" ? (0, _problemsTs.domainsToDescriptions)(caseKeys) : rule.kind === "class" ? (0, _problemsTs.objectKindsToDescriptions)(caseKeys) : (0, _errorsTs.throwInternalError)(`Unexpectedly encountered rule kind '${rule.kind}' during traversal`);
|
|
277
|
-
state.problems.add("cases", caseDescriptions, {
|
|
321
|
+
const caseDescriptions = rule.kind === "value" ? caseKeys : rule.kind === "domain" ? (0, _problemsTs.domainsToDescriptions)(caseKeys) : rule.kind === "class" ? (0, _problemsTs.objectKindsToDescriptions)(caseKeys) : /* c8 ignore start*/ (0, _errorsTs.throwInternalError)(`Unexpectedly encountered rule kind '${rule.kind}' during traversal`);
|
|
322
|
+
/* c8 ignore stop*/ state.problems.add("cases", caseDescriptions, {
|
|
278
323
|
path: missingCasePath,
|
|
279
324
|
data: dataAtPath
|
|
280
325
|
});
|
|
@@ -302,7 +347,7 @@ const entryCheckers = {
|
|
|
302
347
|
state.problems.addProblem(out);
|
|
303
348
|
return false;
|
|
304
349
|
}
|
|
305
|
-
if (
|
|
350
|
+
if (out instanceof CheckResult) {
|
|
306
351
|
if (out.problems) {
|
|
307
352
|
for (const problem of out.problems){
|
|
308
353
|
state.problems.addProblem(problem);
|
|
@@ -19,7 +19,6 @@ _export(exports, {
|
|
|
19
19
|
keyCount: ()=>keyCount,
|
|
20
20
|
hasKeys: ()=>hasKeys,
|
|
21
21
|
nominal: ()=>nominal,
|
|
22
|
-
composeNamed: ()=>composeNamed,
|
|
23
22
|
getNominalId: ()=>getNominalId,
|
|
24
23
|
hasNominalId: ()=>hasNominalId,
|
|
25
24
|
listFrom: ()=>listFrom
|
|
@@ -57,10 +56,6 @@ const id = Symbol("id");
|
|
|
57
56
|
const nominal = (o, name)=>Object.assign(o, {
|
|
58
57
|
[id]: name
|
|
59
58
|
});
|
|
60
|
-
const composeNamed = (f, name)=>// define within a key to dynamically assign a name to the function
|
|
61
|
-
({
|
|
62
|
-
[name]: (...args)=>nominal(f(...args), name)
|
|
63
|
-
})[name];
|
|
64
59
|
const getNominalId = (data)=>(0, _domainsTs.hasDomain)(data, "object") && id in data ? data[id] : undefined;
|
|
65
60
|
const hasNominalId = (data, name)=>getNominalId(data) === name;
|
|
66
61
|
const listFrom = (data)=>Array.isArray(data) ? data : [
|
package/dist/mjs/main.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { scope } from "./scopes/scope.js";
|
|
2
|
-
export { type, ark,
|
|
2
|
+
export { type, ark, arkScope } from "./scopes/ark.js";
|
|
3
3
|
export { intersection, union, arrayOf, instanceOf, valueOf, morph, narrow, keyOf } from "./scopes/expressions.js";
|
|
4
4
|
export { Problems, Problem } from "./traverse/problems.js";
|
|
@@ -42,8 +42,8 @@ import { throwInternalError } from "../utils/errors.js";
|
|
|
42
42
|
import { objectKeysOf } from "../utils/generics.js";
|
|
43
43
|
import { Path } from "../utils/paths.js";
|
|
44
44
|
import { stringify } from "../utils/serialize.js";
|
|
45
|
-
export const composeIntersection = (reducer)=>(l, r, state)=>l === undefined ? r === undefined ?
|
|
46
|
-
export const
|
|
45
|
+
export const composeIntersection = (reducer)=>(l, r, state)=>l === undefined ? r === undefined ? throwInternalError(undefinedOperandsMessage) : r : r === undefined ? l : reducer(l, r, state);
|
|
46
|
+
export const undefinedOperandsMessage = `Unexpected operation two undefined operands`;
|
|
47
47
|
export const disjointDescriptionWriters = {
|
|
48
48
|
domain: ({ l , r })=>`${l.join(", ")} and ${r.join(", ")}`,
|
|
49
49
|
range: ({ l , r })=>`${stringifyRange(l)} and ${stringifyRange(r)}`,
|
package/dist/mjs/nodes/node.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { compileDisjointReasonsMessage } from "../parse/ast/intersection.js";
|
|
2
|
-
import { throwParseError } from "../utils/errors.js";
|
|
2
|
+
import { throwInternalError, throwParseError } from "../utils/errors.js";
|
|
3
3
|
import { entriesOf, hasKey, hasKeys, objectKeysOf } from "../utils/generics.js";
|
|
4
4
|
import { Path } from "../utils/paths.js";
|
|
5
|
-
import { anonymousDisjoint, composeKeyedIntersection, IntersectionState, isDisjoint, isEquality,
|
|
5
|
+
import { anonymousDisjoint, composeKeyedIntersection, IntersectionState, isDisjoint, isEquality, undefinedOperandsMessage } from "./compose.js";
|
|
6
6
|
import { flattenPredicate, isLiteralCondition, predicateIntersection, predicateUnion } from "./predicate.js";
|
|
7
7
|
import { mappedKeys } from "./rules/props.js";
|
|
8
8
|
export const isConfigNode = (node)=>"config" in node;
|
|
@@ -18,7 +18,7 @@ export const nodeIntersection = (l, r, state)=>{
|
|
|
18
18
|
};
|
|
19
19
|
const typeNodeIntersection = composeKeyedIntersection((domain, l, r, context)=>{
|
|
20
20
|
if (l === undefined) {
|
|
21
|
-
return r === undefined ?
|
|
21
|
+
return r === undefined ? throwInternalError(undefinedOperandsMessage) : undefined;
|
|
22
22
|
}
|
|
23
23
|
if (r === undefined) {
|
|
24
24
|
return undefined;
|
|
@@ -41,7 +41,7 @@ export const rootUnion = (l, r, type)=>{
|
|
|
41
41
|
...rDomains
|
|
42
42
|
});
|
|
43
43
|
for (const domain of domains){
|
|
44
|
-
result[domain] = hasKey(lDomains, domain) ? hasKey(rDomains, domain) ? predicateUnion(domain, lDomains[domain], rDomains[domain], type) : lDomains[domain] : hasKey(rDomains, domain) ? rDomains[domain] :
|
|
44
|
+
result[domain] = hasKey(lDomains, domain) ? hasKey(rDomains, domain) ? predicateUnion(domain, lDomains[domain], rDomains[domain], type) : lDomains[domain] : hasKey(rDomains, domain) ? rDomains[domain] : throwInternalError(undefinedOperandsMessage);
|
|
45
45
|
}
|
|
46
46
|
return result;
|
|
47
47
|
};
|
|
@@ -15,7 +15,7 @@ const arrayIndexNumberBranch = deepFreeze({
|
|
|
15
15
|
range: {
|
|
16
16
|
min: {
|
|
17
17
|
comparator: ">=",
|
|
18
|
-
limit:
|
|
18
|
+
limit: 0
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
21
|
divisor: 1
|
|
@@ -43,7 +43,7 @@ export const parseKeyOfTuple = (def, ctx)=>{
|
|
|
43
43
|
} else {
|
|
44
44
|
return throwInternalError(`Unexpected keyof key '${stringify(key)}'`);
|
|
45
45
|
}
|
|
46
|
-
}
|
|
46
|
+
/* c8 ignore stop*/ }
|
|
47
47
|
return Object.fromEntries(Object.entries(keyNode).map(([domain, branches])=>[
|
|
48
48
|
domain,
|
|
49
49
|
branches.length === 1 ? branches[0] : branches
|
|
@@ -79,6 +79,7 @@ const keysOfObjectBranch = (branch)=>{
|
|
|
79
79
|
if ("props" in branch) {
|
|
80
80
|
for (const key of Object.keys(branch.props)){
|
|
81
81
|
if (key === mappedKeys.index) {
|
|
82
|
+
// if any number is a valid key push this RegExp
|
|
82
83
|
result.push(wellFormedNonNegativeIntegerMatcher);
|
|
83
84
|
} else if (!result.includes(key)) {
|
|
84
85
|
result.push(key);
|
|
@@ -42,6 +42,6 @@ export const parseDefinition = (def, ctx)=>{
|
|
|
42
42
|
}
|
|
43
43
|
};
|
|
44
44
|
export const as = Symbol("as");
|
|
45
|
-
export const unknownDefinitionMessage = "Cannot statically parse a definition inferred as unknown. Consider using 'as
|
|
45
|
+
export const unknownDefinitionMessage = "Cannot statically parse a definition inferred as unknown. Consider using 'as Infer<...>' to cast it.";
|
|
46
46
|
const isThunk = (def)=>typeof def === "function" && def.length === 0;
|
|
47
47
|
export const writeBadDefinitionTypeMessage = (actual)=>`Type definitions must be strings or objects (was ${actual})`;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { enclosingChar, parseEnclosed } from "./enclosed.js";
|
|
2
2
|
import { parseUnenclosed, writeMissingOperandMessage } from "./unenclosed.js";
|
|
3
|
-
export const parseOperand = (s)=>s.scanner.lookahead === "" ? s.error(writeMissingOperandMessage(s)) : s.scanner.lookahead === "(" ? s.shiftedByOne().reduceGroupOpen() : s.scanner.lookaheadIsIn(enclosingChar) ? parseEnclosed(s, s.scanner.shift()) : parseUnenclosed(s);
|
|
3
|
+
export const parseOperand = (s)=>s.scanner.lookahead === "" ? s.error(writeMissingOperandMessage(s)) : s.scanner.lookahead === "(" ? s.shiftedByOne().reduceGroupOpen() : s.scanner.lookaheadIsIn(enclosingChar) ? parseEnclosed(s, s.scanner.shift()) : s.scanner.lookahead === " " ? parseOperand(s.shiftedByOne()) : parseUnenclosed(s);
|
|
@@ -3,9 +3,11 @@ import { isKeyOf } from "../../../../utils/generics.js";
|
|
|
3
3
|
import { Scanner } from "../scanner.js";
|
|
4
4
|
import { parseBound } from "./bounds.js";
|
|
5
5
|
import { parseDivisor } from "./divisor.js";
|
|
6
|
+
// @snipStart:parseOperator
|
|
6
7
|
export const parseOperator = (s)=>{
|
|
7
8
|
const lookahead = s.scanner.shift();
|
|
8
|
-
return lookahead === "" ? s.finalize() : lookahead === "[" ? s.scanner.shift() === "]" ? s.rootToArray() : s.error(incompleteArrayTokenMessage) : isKeyOf(lookahead, Scanner.branchTokens) ? s.pushRootToBranch(lookahead) : lookahead === ")" ? s.finalizeGroup() : isKeyOf(lookahead, Scanner.comparatorStartChars) ? parseBound(s, lookahead) : lookahead === "%" ? parseDivisor(s) : lookahead === " " ? parseOperator(s) :
|
|
9
|
+
return lookahead === "" ? s.finalize() : lookahead === "[" ? s.scanner.shift() === "]" ? s.rootToArray() : s.error(incompleteArrayTokenMessage) : isKeyOf(lookahead, Scanner.branchTokens) ? s.pushRootToBranch(lookahead) : lookahead === ")" ? s.finalizeGroup() : isKeyOf(lookahead, Scanner.comparatorStartChars) ? parseBound(s, lookahead) : lookahead === "%" ? parseDivisor(s) : lookahead === " " ? parseOperator(s) : throwInternalError(writeUnexpectedCharacterMessage(lookahead));
|
|
9
10
|
};
|
|
11
|
+
// @snipEnd
|
|
10
12
|
export const writeUnexpectedCharacterMessage = (char)=>`Unexpected character '${char}'`;
|
|
11
13
|
export const incompleteArrayTokenMessage = `Missing expected ']'`;
|