arktype 1.0.4-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 +86 -17
- 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/type.js +4 -12
- package/dist/cjs/traverse/problems.js +3 -1
- package/dist/cjs/traverse/traverse.js +53 -10
- package/dist/cjs/utils/generics.js +0 -5
- 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/type.js +5 -12
- package/dist/mjs/traverse/problems.js +3 -1
- package/dist/mjs/traverse/traverse.js +52 -10
- package/dist/mjs/utils/generics.js +0 -4
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/main.d.ts +1 -0
- 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/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 +10 -6
package/README.md
CHANGED
|
@@ -1,21 +1,27 @@
|
|
|
1
1
|
<h1 align="center">ArkType <sub><sup>Isomorphic types for TS/JS</sup></sub></h1>
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[<img src="./dev/arktype.io/static/img/arktype.gif">](https://arktype.io/try)
|
|
4
4
|
<sub><i>`typescript@4.9.5` in VS Codeβ no extensions or plugins required (<a href="#how">how?</a>)</i></sub>
|
|
5
5
|
|
|
6
|
-
|
|
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>
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
<!-- @snipStart:install -->
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
## Install <sub><sub>π¦`4KB` gzipped, `0` dependencies</sub></sub>
|
|
11
11
|
|
|
12
|
-
<img src="./dev/arktype.io/static/img/
|
|
13
|
-
|
|
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>
|
|
14
18
|
|
|
15
19
|
Our types are tested in [strict-mode](https://www.typescriptlang.org/tsconfig#strict) with TypeScript versions `4.8`, `4.9`, and `5.0`.
|
|
16
20
|
|
|
17
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!_ β΅
|
|
18
22
|
|
|
23
|
+
<!-- @snipEnd -->
|
|
24
|
+
|
|
19
25
|
## Types
|
|
20
26
|
|
|
21
27
|
[Try it in-browser.](https://arktype.io/docs/#your-first-type)
|
|
@@ -23,7 +29,7 @@ _Our APIs have mostly stabilized, but details may still change during the alpha/
|
|
|
23
29
|
```ts @blockFrom:dev/examples/type.ts
|
|
24
30
|
import { type } from "arktype"
|
|
25
31
|
|
|
26
|
-
// Define
|
|
32
|
+
// Define your type...
|
|
27
33
|
export const user = type({
|
|
28
34
|
name: "string",
|
|
29
35
|
device: {
|
|
@@ -35,7 +41,7 @@ export const user = type({
|
|
|
35
41
|
// Infer it...
|
|
36
42
|
export type User = typeof user.infer
|
|
37
43
|
|
|
38
|
-
//
|
|
44
|
+
// Get validated data or clear, customizable error messages.
|
|
39
45
|
export const { data, problems } = user({
|
|
40
46
|
name: "Alan Turing",
|
|
41
47
|
device: {
|
|
@@ -84,22 +90,85 @@ packageData.dependencies![0].dependencies = [packageData]
|
|
|
84
90
|
export const { data, problems } = types.package(packageData)
|
|
85
91
|
```
|
|
86
92
|
|
|
87
|
-
|
|
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.
|
|
88
158
|
|
|
89
|
-
|
|
159
|
+
When you're ready, check out our [guide](./dev/configs/CONTRIBUTING.md) to get started!
|
|
90
160
|
|
|
91
|
-
##
|
|
161
|
+
## Sponsorship
|
|
92
162
|
|
|
93
|
-
|
|
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.
|
|
94
164
|
|
|
95
|
-
|
|
96
|
-
2. Check out our [guide](./dev/configs/CONTRIBUTING.md) to get started!
|
|
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).
|
|
97
166
|
|
|
98
|
-
|
|
167
|
+
Otherwise, consider sending me an email (david@arktype.io) letting me know you're a fan of ArkType. Either would make my day! π
|
|
99
168
|
|
|
100
|
-
|
|
169
|
+
## Collaboration
|
|
101
170
|
|
|
102
|
-
|
|
171
|
+
I'd love to hear about what you're working on and how ArkType can help. Please reach out to david@arktype.io.
|
|
103
172
|
|
|
104
173
|
## License
|
|
105
174
|
|
|
@@ -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/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) {
|
|
@@ -149,15 +165,19 @@ class TraversalState {
|
|
|
149
165
|
const branchProblems = new _problemsTs.Problems(this);
|
|
150
166
|
this.problems = branchProblems;
|
|
151
167
|
const lastPath = this.path;
|
|
168
|
+
const lastKeysToPrune = this.entriesToPrune;
|
|
152
169
|
let hasValidBranch = false;
|
|
153
170
|
for (const branch of branches){
|
|
154
171
|
this.path = new _pathsTs.Path();
|
|
172
|
+
this.entriesToPrune = [];
|
|
155
173
|
if (checkEntries(branch, this)) {
|
|
156
174
|
hasValidBranch = true;
|
|
175
|
+
lastKeysToPrune.push(...this.entriesToPrune);
|
|
157
176
|
break;
|
|
158
177
|
}
|
|
159
178
|
}
|
|
160
179
|
this.path = lastPath;
|
|
180
|
+
this.entriesToPrune = lastKeysToPrune;
|
|
161
181
|
this.problems = lastProblems;
|
|
162
182
|
this.failFast = lastFailFast;
|
|
163
183
|
return hasValidBranch || !this.problems.add("branches", branchProblems);
|
|
@@ -171,6 +191,7 @@ class TraversalState {
|
|
|
171
191
|
this.type = type;
|
|
172
192
|
this.path = new _pathsTs.Path();
|
|
173
193
|
this.problems = new _problemsTs.Problems(this);
|
|
194
|
+
this.entriesToPrune = [];
|
|
174
195
|
this.failFast = false;
|
|
175
196
|
this.traversalConfig = initializeTraversalConfig();
|
|
176
197
|
_classPrivateFieldSet(this, _seen, {});
|
|
@@ -210,27 +231,49 @@ const checkRequiredProp = (prop, state)=>{
|
|
|
210
231
|
};
|
|
211
232
|
const createPropChecker = (kind)=>(props, state)=>{
|
|
212
233
|
let isValid = true;
|
|
213
|
-
const
|
|
234
|
+
const remainingUnseenRequired = {
|
|
214
235
|
...props.required
|
|
215
236
|
};
|
|
216
237
|
for(const k in state.data){
|
|
217
238
|
if (props.required[k]) {
|
|
218
|
-
isValid
|
|
219
|
-
delete
|
|
239
|
+
isValid && (isValid = state.traverseKey(k, props.required[k]));
|
|
240
|
+
delete remainingUnseenRequired[k];
|
|
220
241
|
} else if (props.optional[k]) {
|
|
221
|
-
isValid
|
|
242
|
+
isValid && (isValid = state.traverseKey(k, props.optional[k]));
|
|
222
243
|
} else if (kind === "distilledProps") {
|
|
223
|
-
|
|
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
|
+
}
|
|
224
258
|
} else {
|
|
259
|
+
isValid = false;
|
|
225
260
|
state.problems.add("extraneous", state.data[k], {
|
|
226
261
|
path: state.path.concat(k)
|
|
227
262
|
});
|
|
228
|
-
isValid = false;
|
|
229
263
|
}
|
|
230
264
|
if (!isValid && state.failFast) {
|
|
231
265
|
return false;
|
|
232
266
|
}
|
|
233
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
|
+
}
|
|
234
277
|
return isValid;
|
|
235
278
|
};
|
|
236
279
|
const entryCheckers = {
|
|
@@ -275,8 +318,8 @@ const entryCheckers = {
|
|
|
275
318
|
}
|
|
276
319
|
const caseKeys = (0, _genericsTs.objectKeysOf)(rule.cases);
|
|
277
320
|
const missingCasePath = state.path.concat(rule.path);
|
|
278
|
-
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`);
|
|
279
|
-
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, {
|
|
280
323
|
path: missingCasePath,
|
|
281
324
|
data: dataAtPath
|
|
282
325
|
});
|
|
@@ -304,7 +347,7 @@ const entryCheckers = {
|
|
|
304
347
|
state.problems.addProblem(out);
|
|
305
348
|
return false;
|
|
306
349
|
}
|
|
307
|
-
if (
|
|
350
|
+
if (out instanceof CheckResult) {
|
|
308
351
|
if (out.problems) {
|
|
309
352
|
for (const problem of out.problems){
|
|
310
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 : [
|
|
@@ -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 ']'`;
|
package/dist/mjs/scopes/type.js
CHANGED
|
@@ -1,16 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { traverseRoot } from "../traverse/traverse.js";
|
|
2
2
|
import { chainableNoOpProxy } from "../utils/chainableNoOpProxy.js";
|
|
3
|
-
import { composeNamed, getNominalId } from "../utils/generics.js";
|
|
4
3
|
export const initializeType = (name, definition, config, scope)=>{
|
|
5
|
-
const namedTraverse = composeNamed((data)=>{
|
|
6
|
-
const state = new TraversalState(data, t);
|
|
7
|
-
traverse(t.flat, state);
|
|
8
|
-
return state.problems.count ? {
|
|
9
|
-
problems: state.problems
|
|
10
|
-
} : {
|
|
11
|
-
data: state.data
|
|
12
|
-
};
|
|
13
|
-
}, name);
|
|
14
4
|
const root = {
|
|
15
5
|
// temporarily initialize node/flat to aliases that will be included in
|
|
16
6
|
// the final type in case of cyclic resolutions
|
|
@@ -29,9 +19,12 @@ export const initializeType = (name, definition, config, scope)=>{
|
|
|
29
19
|
includesMorph: false,
|
|
30
20
|
config
|
|
31
21
|
};
|
|
22
|
+
// define within a key to dynamically assign a name to the function
|
|
23
|
+
const namedTraverse = {
|
|
24
|
+
[name]: (data)=>traverseRoot(namedTraverse, data)
|
|
25
|
+
}[name];
|
|
32
26
|
const t = Object.assign(namedTraverse, root);
|
|
33
27
|
return t;
|
|
34
28
|
};
|
|
35
29
|
export const isType = (value)=>value?.infer === chainableNoOpProxy;
|
|
36
|
-
export const isCheckResult = (value)=>getNominalId(value) !== undefined;
|
|
37
30
|
export const isAnonymousName = (name)=>name[0] === "Ξ»";
|
|
@@ -87,7 +87,9 @@ class ProblemArray extends Array {
|
|
|
87
87
|
const data = // we have to check for the presence of the key explicitly since the
|
|
88
88
|
// data could be undefined or null
|
|
89
89
|
opts && "data" in opts ? opts.data : _classPrivateFieldGet(this, _state).data;
|
|
90
|
-
const problem = new Problem(
|
|
90
|
+
const problem = new Problem(// avoid a bunch of errors from TS trying to discriminate the
|
|
91
|
+
// problem input based on the code
|
|
92
|
+
code, path, data, source, _classPrivateFieldGet(this, _state).getProblemConfig(code));
|
|
91
93
|
this.addProblem(problem);
|
|
92
94
|
return problem;
|
|
93
95
|
}
|