arktype 0.0.1 → 0.1.1
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/LICENSE +7 -0
- package/README.md +101 -0
- package/dist/api.cjs +15 -0
- package/dist/api.d.ts +2 -0
- package/dist/api.mjs +1 -0
- package/dist/cjs/main.js +462 -0
- package/dist/cjs/nodes/branches.js +85 -0
- package/dist/cjs/nodes/compose.js +140 -0
- package/dist/cjs/nodes/discriminate.js +199 -0
- package/dist/cjs/nodes/functors.js +35 -0
- package/dist/cjs/nodes/infer.js +4 -0
- package/dist/cjs/nodes/node.js +108 -0
- package/dist/cjs/nodes/predicate.js +87 -0
- package/dist/cjs/nodes/rules/class.js +21 -0
- package/dist/cjs/nodes/rules/collapsibleSet.js +49 -0
- package/dist/cjs/nodes/rules/divisor.js +33 -0
- package/dist/cjs/nodes/rules/props.js +72 -0
- package/dist/cjs/nodes/rules/range.js +109 -0
- package/dist/cjs/nodes/rules/regex.js +68 -0
- package/dist/cjs/nodes/rules/rules.js +156 -0
- package/dist/cjs/nodes/rules/subdomain.js +78 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/parse/definition.js +33 -0
- package/dist/cjs/parse/record.js +37 -0
- package/dist/cjs/parse/string/ast.js +36 -0
- package/dist/cjs/parse/string/reduce/dynamic.js +150 -0
- package/dist/cjs/parse/string/reduce/shared.js +23 -0
- package/dist/cjs/parse/string/reduce/static.js +4 -0
- package/dist/cjs/parse/string/shift/operand/enclosed.js +54 -0
- package/dist/cjs/parse/string/shift/operand/operand.js +11 -0
- package/dist/cjs/parse/string/shift/operand/unenclosed.js +53 -0
- package/dist/cjs/parse/string/shift/operator/bounds.js +87 -0
- package/dist/cjs/parse/string/shift/operator/divisor.js +35 -0
- package/dist/cjs/parse/string/shift/operator/operator.js +26 -0
- package/dist/cjs/parse/string/shift/scanner.js +99 -0
- package/dist/cjs/parse/string/string.js +43 -0
- package/dist/cjs/parse/tuple/keyof.js +46 -0
- package/dist/cjs/parse/tuple/morph.js +56 -0
- package/dist/cjs/parse/tuple/narrow.js +15 -0
- package/dist/cjs/parse/tuple/tuple.js +85 -0
- package/dist/cjs/parse/tuple/utils.js +48 -0
- package/dist/cjs/traverse/problems.js +249 -0
- package/dist/cjs/traverse/traverse.js +268 -0
- package/dist/cjs/utils/chainableNoOpProxy.js +23 -0
- package/dist/cjs/utils/domains.js +67 -0
- package/dist/cjs/utils/errors.js +26 -0
- package/dist/cjs/utils/freeze.js +16 -0
- package/dist/cjs/utils/generics.js +36 -0
- package/dist/cjs/utils/numericLiterals.js +84 -0
- package/dist/cjs/utils/paths.js +32 -0
- package/dist/cjs/utils/serialize.js +98 -0
- package/dist/cjs/utils/unionToTuple.js +4 -0
- package/dist/mjs/main.js +442 -0
- package/dist/mjs/nodes/branches.js +71 -0
- package/dist/mjs/nodes/compose.js +118 -0
- package/dist/mjs/nodes/discriminate.js +184 -0
- package/dist/mjs/nodes/functors.js +27 -0
- package/dist/mjs/nodes/infer.js +1 -0
- package/dist/mjs/nodes/node.js +90 -0
- package/dist/mjs/nodes/predicate.js +69 -0
- package/dist/mjs/nodes/rules/class.js +7 -0
- package/dist/mjs/nodes/rules/collapsibleSet.js +35 -0
- package/dist/mjs/nodes/rules/divisor.js +19 -0
- package/dist/mjs/nodes/rules/props.js +57 -0
- package/dist/mjs/nodes/rules/range.js +90 -0
- package/dist/mjs/nodes/rules/regex.js +53 -0
- package/dist/mjs/nodes/rules/rules.js +138 -0
- package/dist/mjs/nodes/rules/subdomain.js +63 -0
- package/dist/mjs/package.json +3 -0
- package/dist/mjs/parse/definition.js +17 -0
- package/dist/mjs/parse/record.js +29 -0
- package/dist/mjs/parse/string/ast.js +19 -0
- package/dist/mjs/parse/string/reduce/dynamic.js +142 -0
- package/dist/mjs/parse/string/reduce/shared.js +6 -0
- package/dist/mjs/parse/string/reduce/static.js +1 -0
- package/dist/mjs/parse/string/shift/operand/enclosed.js +39 -0
- package/dist/mjs/parse/string/shift/operand/operand.js +3 -0
- package/dist/mjs/parse/string/shift/operand/unenclosed.js +36 -0
- package/dist/mjs/parse/string/shift/operator/bounds.js +70 -0
- package/dist/mjs/parse/string/shift/operator/divisor.js +21 -0
- package/dist/mjs/parse/string/shift/operator/operator.js +11 -0
- package/dist/mjs/parse/string/shift/scanner.js +91 -0
- package/dist/mjs/parse/string/string.js +28 -0
- package/dist/mjs/parse/tuple/keyof.js +38 -0
- package/dist/mjs/parse/tuple/morph.js +42 -0
- package/dist/mjs/parse/tuple/narrow.js +7 -0
- package/dist/mjs/parse/tuple/tuple.js +71 -0
- package/dist/mjs/parse/tuple/utils.js +34 -0
- package/dist/mjs/traverse/problems.js +229 -0
- package/dist/mjs/traverse/traverse.js +252 -0
- package/dist/mjs/utils/chainableNoOpProxy.js +15 -0
- package/dist/mjs/utils/domains.js +48 -0
- package/dist/mjs/utils/errors.js +10 -0
- package/dist/mjs/utils/freeze.js +8 -0
- package/dist/mjs/utils/generics.js +15 -0
- package/dist/mjs/utils/numericLiterals.js +71 -0
- package/dist/mjs/utils/paths.js +18 -0
- package/dist/mjs/utils/serialize.js +81 -0
- package/dist/mjs/utils/unionToTuple.js +1 -0
- package/dist/tsconfig.temp.tsbuildinfo +1 -0
- package/dist/types/main.d.ts +257 -0
- package/dist/types/nodes/branches.d.ts +14 -0
- package/dist/types/nodes/compose.d.ts +92 -0
- package/dist/types/nodes/discriminate.d.ts +27 -0
- package/dist/types/nodes/functors.d.ts +6 -0
- package/dist/types/nodes/infer.d.ts +35 -0
- package/dist/types/nodes/node.d.ts +51 -0
- package/dist/types/nodes/predicate.d.ts +88 -0
- package/dist/types/nodes/rules/class.d.ts +3 -0
- package/dist/types/nodes/rules/collapsibleSet.d.ts +4 -0
- package/dist/types/nodes/rules/divisor.d.ts +2 -0
- package/dist/types/nodes/rules/props.d.ts +20 -0
- package/dist/types/nodes/rules/range.d.ts +33 -0
- package/dist/types/nodes/rules/regex.d.ts +14 -0
- package/dist/types/nodes/rules/rules.d.ts +44 -0
- package/dist/types/nodes/rules/subdomain.d.ts +15 -0
- package/dist/types/parse/definition.d.ts +29 -0
- package/dist/types/parse/record.d.ts +24 -0
- package/dist/types/parse/string/ast.d.ts +42 -0
- package/dist/types/parse/string/reduce/dynamic.d.ts +34 -0
- package/dist/types/parse/string/reduce/shared.d.ts +24 -0
- package/dist/types/parse/string/reduce/static.d.ts +115 -0
- package/dist/types/parse/string/shift/operand/enclosed.d.ts +28 -0
- package/dist/types/parse/string/shift/operand/operand.d.ts +9 -0
- package/dist/types/parse/string/shift/operand/unenclosed.d.ts +18 -0
- package/dist/types/parse/string/shift/operator/bounds.d.ts +20 -0
- package/dist/types/parse/string/shift/operator/divisor.d.ts +9 -0
- package/dist/types/parse/string/shift/operator/operator.d.ts +13 -0
- package/dist/types/parse/string/shift/scanner.d.ts +89 -0
- package/dist/types/parse/string/string.d.ts +27 -0
- package/dist/types/parse/tuple/keyof.d.ts +2 -0
- package/dist/types/parse/tuple/morph.d.ts +15 -0
- package/dist/types/parse/tuple/narrow.d.ts +13 -0
- package/dist/types/parse/tuple/tuple.d.ts +37 -0
- package/dist/types/parse/tuple/utils.d.ts +19 -0
- package/dist/types/traverse/problems.d.ts +123 -0
- package/dist/types/traverse/traverse.d.ts +48 -0
- package/dist/types/utils/chainableNoOpProxy.d.ts +14 -0
- package/dist/types/utils/domains.d.ts +58 -0
- package/dist/types/utils/errors.d.ts +6 -0
- package/dist/types/utils/freeze.d.ts +2 -0
- package/dist/types/utils/generics.d.ts +118 -0
- package/dist/types/utils/numericLiterals.d.ts +28 -0
- package/dist/types/utils/paths.d.ts +7 -0
- package/dist/types/utils/serialize.d.ts +38 -0
- package/dist/types/utils/unionToTuple.d.ts +11 -0
- package/package.json +102 -2
package/dist/mjs/main.js
ADDED
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
function _checkPrivateRedeclaration(obj, privateCollection) {
|
|
2
|
+
if (privateCollection.has(obj)) {
|
|
3
|
+
throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
function _classApplyDescriptorGet(receiver, descriptor) {
|
|
7
|
+
if (descriptor.get) {
|
|
8
|
+
return descriptor.get.call(receiver);
|
|
9
|
+
}
|
|
10
|
+
return descriptor.value;
|
|
11
|
+
}
|
|
12
|
+
function _classApplyDescriptorSet(receiver, descriptor, value) {
|
|
13
|
+
if (descriptor.set) {
|
|
14
|
+
descriptor.set.call(receiver, value);
|
|
15
|
+
} else {
|
|
16
|
+
if (!descriptor.writable) {
|
|
17
|
+
throw new TypeError("attempted to set read only private field");
|
|
18
|
+
}
|
|
19
|
+
descriptor.value = value;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function _classApplyDescriptorUpdate(receiver, descriptor) {
|
|
23
|
+
if (descriptor.set) {
|
|
24
|
+
if (!descriptor.get) {
|
|
25
|
+
throw new TypeError("attempted to read set only private field");
|
|
26
|
+
}
|
|
27
|
+
if (!("__destrWrapper" in descriptor)) {
|
|
28
|
+
descriptor.__destrWrapper = {
|
|
29
|
+
set value (v){
|
|
30
|
+
descriptor.set.call(receiver, v);
|
|
31
|
+
},
|
|
32
|
+
get value () {
|
|
33
|
+
return descriptor.get.call(receiver);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return descriptor.__destrWrapper;
|
|
38
|
+
} else {
|
|
39
|
+
if (!descriptor.writable) {
|
|
40
|
+
throw new TypeError("attempted to set read only private field");
|
|
41
|
+
}
|
|
42
|
+
return descriptor;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function _classExtractFieldDescriptor(receiver, privateMap, action) {
|
|
46
|
+
if (!privateMap.has(receiver)) {
|
|
47
|
+
throw new TypeError("attempted to " + action + " private field on non-instance");
|
|
48
|
+
}
|
|
49
|
+
return privateMap.get(receiver);
|
|
50
|
+
}
|
|
51
|
+
function _classPrivateFieldGet(receiver, privateMap) {
|
|
52
|
+
var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get");
|
|
53
|
+
return _classApplyDescriptorGet(receiver, descriptor);
|
|
54
|
+
}
|
|
55
|
+
function _classPrivateFieldInit(obj, privateMap, value) {
|
|
56
|
+
_checkPrivateRedeclaration(obj, privateMap);
|
|
57
|
+
privateMap.set(obj, value);
|
|
58
|
+
}
|
|
59
|
+
function _classPrivateFieldSet(receiver, privateMap, value) {
|
|
60
|
+
var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set");
|
|
61
|
+
_classApplyDescriptorSet(receiver, descriptor, value);
|
|
62
|
+
return value;
|
|
63
|
+
}
|
|
64
|
+
function _classPrivateFieldUpdate(receiver, privateMap) {
|
|
65
|
+
var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "update");
|
|
66
|
+
return _classApplyDescriptorUpdate(receiver, descriptor);
|
|
67
|
+
}
|
|
68
|
+
function _classPrivateMethodGet(receiver, privateSet, fn) {
|
|
69
|
+
if (!privateSet.has(receiver)) {
|
|
70
|
+
throw new TypeError("attempted to get private field on non-instance");
|
|
71
|
+
}
|
|
72
|
+
return fn;
|
|
73
|
+
}
|
|
74
|
+
function _classPrivateMethodInit(obj, privateSet) {
|
|
75
|
+
_checkPrivateRedeclaration(obj, privateSet);
|
|
76
|
+
privateSet.add(obj);
|
|
77
|
+
}
|
|
78
|
+
var _ctx_type_meta;
|
|
79
|
+
import { flattenType } from "./nodes/node.js";
|
|
80
|
+
import { parseDefinition } from "./parse/definition.js";
|
|
81
|
+
import { compileProblemOptions } from "./traverse/problems.js";
|
|
82
|
+
import { TraversalState, traverse } from "./traverse/traverse.js";
|
|
83
|
+
import { chainableNoOpProxy } from "./utils/chainableNoOpProxy.js";
|
|
84
|
+
import { throwInternalError, throwParseError } from "./utils/errors.js";
|
|
85
|
+
import { deepFreeze } from "./utils/freeze.js";
|
|
86
|
+
import { Path } from "./utils/paths.js";
|
|
87
|
+
let anonymousScopeCount = 0;
|
|
88
|
+
const scopeRegistry = {};
|
|
89
|
+
var _resolutions = /*#__PURE__*/ new WeakMap(), _exports = /*#__PURE__*/ new WeakMap(), _anonymousTypeCount = /*#__PURE__*/ new WeakMap(), _register = /*#__PURE__*/ new WeakSet(), _cacheSpaces = /*#__PURE__*/ new WeakSet(), _initializeContext = /*#__PURE__*/ new WeakSet(), _compiled = /*#__PURE__*/ new WeakMap(), _resolveRecurse = /*#__PURE__*/ new WeakSet();
|
|
90
|
+
export class Scope {
|
|
91
|
+
createAnonymousTypeName() {
|
|
92
|
+
return `anonymousType${++_classPrivateFieldUpdate(this, _anonymousTypeCount).value}`;
|
|
93
|
+
}
|
|
94
|
+
get infer() {
|
|
95
|
+
return chainableNoOpProxy;
|
|
96
|
+
}
|
|
97
|
+
compile() {
|
|
98
|
+
if (!_classPrivateFieldGet(this, _compiled)) {
|
|
99
|
+
for(const name in this.aliases){
|
|
100
|
+
this.resolve(name);
|
|
101
|
+
}
|
|
102
|
+
_classPrivateFieldSet(this, _compiled, true);
|
|
103
|
+
}
|
|
104
|
+
return _classPrivateFieldGet(this, _exports).root;
|
|
105
|
+
}
|
|
106
|
+
addReferenceIfResolvable(name, ctx) {
|
|
107
|
+
const resolution = _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, name, "undefined", []);
|
|
108
|
+
if (!resolution) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
(_ctx_type_meta = ctx.type.meta).includesMorph || (_ctx_type_meta.includesMorph = resolution.meta.includesMorph);
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
resolve(name) {
|
|
115
|
+
return _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, name, "throw", []);
|
|
116
|
+
}
|
|
117
|
+
resolveNode(node) {
|
|
118
|
+
if (typeof node === "object") {
|
|
119
|
+
return node;
|
|
120
|
+
}
|
|
121
|
+
return this.resolveNode(this.resolve(node).node);
|
|
122
|
+
}
|
|
123
|
+
constructor(aliases, opts){
|
|
124
|
+
_classPrivateMethodInit(this, _register);
|
|
125
|
+
_classPrivateMethodInit(this, _cacheSpaces);
|
|
126
|
+
_classPrivateMethodInit(this, _initializeContext);
|
|
127
|
+
_classPrivateMethodInit(this, _resolveRecurse);
|
|
128
|
+
_classPrivateFieldInit(this, _resolutions, {
|
|
129
|
+
writable: true,
|
|
130
|
+
value: void 0
|
|
131
|
+
});
|
|
132
|
+
_classPrivateFieldInit(this, _exports, {
|
|
133
|
+
writable: true,
|
|
134
|
+
value: void 0
|
|
135
|
+
});
|
|
136
|
+
_classPrivateFieldInit(this, _anonymousTypeCount, {
|
|
137
|
+
writable: true,
|
|
138
|
+
value: void 0
|
|
139
|
+
});
|
|
140
|
+
_classPrivateFieldInit(this, _compiled, {
|
|
141
|
+
writable: true,
|
|
142
|
+
value: void 0
|
|
143
|
+
});
|
|
144
|
+
this.aliases = aliases;
|
|
145
|
+
this.opts = opts;
|
|
146
|
+
this.parseCache = new FreezingCache();
|
|
147
|
+
_classPrivateFieldSet(this, _resolutions, new Cache());
|
|
148
|
+
_classPrivateFieldSet(this, _exports, new Cache());
|
|
149
|
+
_classPrivateFieldSet(this, _anonymousTypeCount, 0);
|
|
150
|
+
this.type = (def, opts = {})=>{
|
|
151
|
+
const result = initializeType(def, opts, this);
|
|
152
|
+
const ctx = _classPrivateMethodGet(this, _initializeContext, initializeContext).call(this, result);
|
|
153
|
+
result.node = this.resolveNode(parseDefinition(def, ctx));
|
|
154
|
+
result.flat = flattenType(result);
|
|
155
|
+
return result;
|
|
156
|
+
};
|
|
157
|
+
_classPrivateFieldSet(this, _compiled, false);
|
|
158
|
+
this.name = _classPrivateMethodGet(this, _register, register).call(this, opts);
|
|
159
|
+
if (opts.standard !== false) {
|
|
160
|
+
_classPrivateMethodGet(this, _cacheSpaces, cacheSpaces).call(this, [
|
|
161
|
+
standardTypes
|
|
162
|
+
], "imports");
|
|
163
|
+
}
|
|
164
|
+
if (opts.imports) {
|
|
165
|
+
_classPrivateMethodGet(this, _cacheSpaces, cacheSpaces).call(this, opts.imports, "imports");
|
|
166
|
+
}
|
|
167
|
+
if (opts.includes) {
|
|
168
|
+
_classPrivateMethodGet(this, _cacheSpaces, cacheSpaces).call(this, opts.includes, "includes");
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
function register(opts) {
|
|
173
|
+
const name = opts.name ? scopeRegistry[opts.name] ? throwParseError(`A scope named '${opts.name}' already exists`) : opts.name : `anonymousScope${++anonymousScopeCount}`;
|
|
174
|
+
scopeRegistry[name] = this;
|
|
175
|
+
return name;
|
|
176
|
+
}
|
|
177
|
+
function cacheSpaces(spaces, kind) {
|
|
178
|
+
for (const space of spaces){
|
|
179
|
+
for(const name in space){
|
|
180
|
+
if (_classPrivateFieldGet(this, _resolutions).has(name) || name in this.aliases) {
|
|
181
|
+
throwParseError(writeDuplicateAliasMessage(name));
|
|
182
|
+
}
|
|
183
|
+
_classPrivateFieldGet(this, _resolutions).set(name, space[name]);
|
|
184
|
+
if (kind === "includes") {
|
|
185
|
+
_classPrivateFieldGet(this, _exports).set(name, space[name]);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function initializeContext(type) {
|
|
191
|
+
return {
|
|
192
|
+
type,
|
|
193
|
+
path: new Path()
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
function resolveRecurse(name, onUnresolvable, seen) {
|
|
197
|
+
const maybeCacheResult = _classPrivateFieldGet(this, _resolutions).get(name);
|
|
198
|
+
if (maybeCacheResult) {
|
|
199
|
+
return maybeCacheResult;
|
|
200
|
+
}
|
|
201
|
+
if (!this.aliases[name]) {
|
|
202
|
+
return onUnresolvable === "throw" ? throwInternalError(`Unexpectedly failed to resolve alias '${name}'`) : undefined;
|
|
203
|
+
}
|
|
204
|
+
// TODO: opts?
|
|
205
|
+
const type = initializeType(this.aliases[name], {
|
|
206
|
+
name
|
|
207
|
+
}, this);
|
|
208
|
+
_classPrivateFieldGet(this, _resolutions).set(name, type);
|
|
209
|
+
_classPrivateFieldGet(this, _exports).set(name, type);
|
|
210
|
+
const ctx = _classPrivateMethodGet(this, _initializeContext, initializeContext).call(this, type);
|
|
211
|
+
let resolution = parseDefinition(type.meta.definition, ctx);
|
|
212
|
+
if (typeof resolution === "string") {
|
|
213
|
+
if (seen.includes(resolution)) {
|
|
214
|
+
return throwParseError(writeShallowCycleErrorMessage(name, seen));
|
|
215
|
+
}
|
|
216
|
+
seen.push(resolution);
|
|
217
|
+
resolution = _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, resolution, "throw", seen).node;
|
|
218
|
+
}
|
|
219
|
+
type.node = resolution;
|
|
220
|
+
type.flat = flattenType(type);
|
|
221
|
+
return type;
|
|
222
|
+
}
|
|
223
|
+
// TODO: merge scope options
|
|
224
|
+
const initializeType = (definition, opts, scope)=>{
|
|
225
|
+
const name = opts.name ?? scope.createAnonymousTypeName();
|
|
226
|
+
const meta = {
|
|
227
|
+
name,
|
|
228
|
+
id: `${scope.name}.${name}`,
|
|
229
|
+
definition,
|
|
230
|
+
scope,
|
|
231
|
+
problems: compileProblemOptions(opts.problems),
|
|
232
|
+
includesMorph: false
|
|
233
|
+
};
|
|
234
|
+
const root = {
|
|
235
|
+
// temporarily initialize node/flat to aliases that will be included in
|
|
236
|
+
// the final type in case of cyclic resolutions
|
|
237
|
+
node: name,
|
|
238
|
+
flat: [
|
|
239
|
+
[
|
|
240
|
+
"alias",
|
|
241
|
+
name
|
|
242
|
+
]
|
|
243
|
+
],
|
|
244
|
+
meta,
|
|
245
|
+
infer: chainableNoOpProxy
|
|
246
|
+
};
|
|
247
|
+
// dynamically assign a name to the primary traversal function
|
|
248
|
+
const namedTraverse = {
|
|
249
|
+
[name]: (data)=>{
|
|
250
|
+
const state = new TraversalState(type);
|
|
251
|
+
const out = traverse(data, type.flat, state);
|
|
252
|
+
return state.problems.length ? {
|
|
253
|
+
data,
|
|
254
|
+
problems: state.problems
|
|
255
|
+
} : {
|
|
256
|
+
data,
|
|
257
|
+
out
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
}[name];
|
|
261
|
+
// we need to assign this to a variable before returning so we can reference
|
|
262
|
+
// it in namedTraverse
|
|
263
|
+
const type = Object.assign(namedTraverse, root);
|
|
264
|
+
return type;
|
|
265
|
+
};
|
|
266
|
+
class Cache {
|
|
267
|
+
get root() {
|
|
268
|
+
return this.cache;
|
|
269
|
+
}
|
|
270
|
+
has(name) {
|
|
271
|
+
return name in this.cache;
|
|
272
|
+
}
|
|
273
|
+
get(name) {
|
|
274
|
+
return this.cache[name];
|
|
275
|
+
}
|
|
276
|
+
set(name, item) {
|
|
277
|
+
this.cache[name] = item;
|
|
278
|
+
return item;
|
|
279
|
+
}
|
|
280
|
+
constructor(){
|
|
281
|
+
this.cache = {};
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
class FreezingCache extends Cache {
|
|
285
|
+
set(name, item) {
|
|
286
|
+
this.cache[name] = deepFreeze(item);
|
|
287
|
+
return item;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
export const writeShallowCycleErrorMessage = (name, seen)=>`Alias '${name}' has a shallow resolution cycle: ${[
|
|
291
|
+
...seen,
|
|
292
|
+
name
|
|
293
|
+
].join("=>")}`;
|
|
294
|
+
const always = {
|
|
295
|
+
bigint: true,
|
|
296
|
+
boolean: true,
|
|
297
|
+
null: true,
|
|
298
|
+
number: true,
|
|
299
|
+
object: true,
|
|
300
|
+
string: true,
|
|
301
|
+
symbol: true,
|
|
302
|
+
undefined: true
|
|
303
|
+
};
|
|
304
|
+
export const scope = (aliases, opts = {})=>new Scope(aliases, opts);
|
|
305
|
+
const ts = scope({
|
|
306
|
+
any: [
|
|
307
|
+
"node",
|
|
308
|
+
always
|
|
309
|
+
],
|
|
310
|
+
bigint: [
|
|
311
|
+
"node",
|
|
312
|
+
{
|
|
313
|
+
bigint: true
|
|
314
|
+
}
|
|
315
|
+
],
|
|
316
|
+
boolean: [
|
|
317
|
+
"node",
|
|
318
|
+
{
|
|
319
|
+
boolean: true
|
|
320
|
+
}
|
|
321
|
+
],
|
|
322
|
+
false: [
|
|
323
|
+
"node",
|
|
324
|
+
{
|
|
325
|
+
boolean: {
|
|
326
|
+
value: false
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
],
|
|
330
|
+
never: [
|
|
331
|
+
"node",
|
|
332
|
+
{}
|
|
333
|
+
],
|
|
334
|
+
null: [
|
|
335
|
+
"node",
|
|
336
|
+
{
|
|
337
|
+
null: true
|
|
338
|
+
}
|
|
339
|
+
],
|
|
340
|
+
number: [
|
|
341
|
+
"node",
|
|
342
|
+
{
|
|
343
|
+
number: true
|
|
344
|
+
}
|
|
345
|
+
],
|
|
346
|
+
object: [
|
|
347
|
+
"node",
|
|
348
|
+
{
|
|
349
|
+
object: true
|
|
350
|
+
}
|
|
351
|
+
],
|
|
352
|
+
string: [
|
|
353
|
+
"node",
|
|
354
|
+
{
|
|
355
|
+
string: true
|
|
356
|
+
}
|
|
357
|
+
],
|
|
358
|
+
symbol: [
|
|
359
|
+
"node",
|
|
360
|
+
{
|
|
361
|
+
symbol: true
|
|
362
|
+
}
|
|
363
|
+
],
|
|
364
|
+
true: [
|
|
365
|
+
"node",
|
|
366
|
+
{
|
|
367
|
+
boolean: {
|
|
368
|
+
value: true
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
],
|
|
372
|
+
unknown: [
|
|
373
|
+
"node",
|
|
374
|
+
always
|
|
375
|
+
],
|
|
376
|
+
void: [
|
|
377
|
+
"node",
|
|
378
|
+
{
|
|
379
|
+
undefined: true
|
|
380
|
+
}
|
|
381
|
+
],
|
|
382
|
+
undefined: [
|
|
383
|
+
"node",
|
|
384
|
+
{
|
|
385
|
+
undefined: true
|
|
386
|
+
}
|
|
387
|
+
],
|
|
388
|
+
// TODO: Add remaining JS object types
|
|
389
|
+
Function: [
|
|
390
|
+
"node",
|
|
391
|
+
{
|
|
392
|
+
object: {
|
|
393
|
+
subdomain: "Function"
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
]
|
|
397
|
+
}, {
|
|
398
|
+
name: "ts",
|
|
399
|
+
standard: false
|
|
400
|
+
});
|
|
401
|
+
const tsTypes = ts.compile();
|
|
402
|
+
const validation = scope({
|
|
403
|
+
email: /^(.+)@(.+)\.(.+)$/,
|
|
404
|
+
alphanumeric: /^[dA-Za-z]+$/,
|
|
405
|
+
alpha: /^[A-Za-z]+$/,
|
|
406
|
+
lowercase: /^[a-z]*$/,
|
|
407
|
+
uppercase: /^[A-Z]*$/,
|
|
408
|
+
integer: [
|
|
409
|
+
"node",
|
|
410
|
+
{
|
|
411
|
+
number: {
|
|
412
|
+
divisor: 1
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
]
|
|
416
|
+
}, {
|
|
417
|
+
name: "validation",
|
|
418
|
+
standard: false
|
|
419
|
+
});
|
|
420
|
+
const validationTypes = validation.compile();
|
|
421
|
+
const standard = scope({}, {
|
|
422
|
+
name: "standard",
|
|
423
|
+
includes: [
|
|
424
|
+
tsTypes,
|
|
425
|
+
validationTypes
|
|
426
|
+
],
|
|
427
|
+
standard: false
|
|
428
|
+
});
|
|
429
|
+
const standardTypes = standard.compile();
|
|
430
|
+
export const scopes = {
|
|
431
|
+
ts,
|
|
432
|
+
validation,
|
|
433
|
+
standard
|
|
434
|
+
};
|
|
435
|
+
export const spaces = {
|
|
436
|
+
ts: tsTypes,
|
|
437
|
+
validation: validationTypes,
|
|
438
|
+
standard: standardTypes
|
|
439
|
+
};
|
|
440
|
+
export const type = scopes.standard.type;
|
|
441
|
+
export const writeDuplicateAliasMessage = (name)=>`Alias '${name}' is already defined`;
|
|
442
|
+
export const isType = (value)=>value?.infer === chainableNoOpProxy;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { domainOf, hasDomain } from "../utils/domains.js";
|
|
2
|
+
import { throwInternalError } from "../utils/errors.js";
|
|
3
|
+
import { isDisjoint, isEquality } from "./compose.js";
|
|
4
|
+
import { branchIntersection } from "./rules/rules.js";
|
|
5
|
+
export const isBranchComparison = (comparison)=>comparison?.lConditions !== undefined;
|
|
6
|
+
export const compareBranches = (lConditions, rConditions, state)=>{
|
|
7
|
+
const result = {
|
|
8
|
+
lConditions,
|
|
9
|
+
rConditions,
|
|
10
|
+
lSubconditionsOfR: [],
|
|
11
|
+
rSubconditionsOfL: [],
|
|
12
|
+
equalities: [],
|
|
13
|
+
distinctIntersections: []
|
|
14
|
+
};
|
|
15
|
+
const pairs = rConditions.map((condition)=>({
|
|
16
|
+
condition,
|
|
17
|
+
distinct: []
|
|
18
|
+
}));
|
|
19
|
+
lConditions.forEach((l, lIndex)=>{
|
|
20
|
+
let lImpliesR = false;
|
|
21
|
+
const distinct = pairs.map((rPairs, rIndex)=>{
|
|
22
|
+
if (lImpliesR || !rPairs.distinct) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
const r = rPairs.condition;
|
|
26
|
+
const subresult = branchIntersection(l, r, state);
|
|
27
|
+
if (isDisjoint(subresult)) {
|
|
28
|
+
// doesn't tell us about any redundancies or add a distinct pair
|
|
29
|
+
return null;
|
|
30
|
+
} else if (subresult === l) {
|
|
31
|
+
result.lSubconditionsOfR.push(lIndex);
|
|
32
|
+
// If l is a subtype of the current r branch, intersections
|
|
33
|
+
// with the remaining branches of r won't lead to distinct
|
|
34
|
+
// branches, so we set a flag indicating we can skip them.
|
|
35
|
+
lImpliesR = true;
|
|
36
|
+
return null;
|
|
37
|
+
} else if (subresult === r) {
|
|
38
|
+
result.rSubconditionsOfL.push(rIndex);
|
|
39
|
+
// If r is a subtype of the current l branch, it is removed
|
|
40
|
+
// from pairsByR because future intersections won't lead to
|
|
41
|
+
// distinct branches.
|
|
42
|
+
rPairs.distinct = null;
|
|
43
|
+
return null;
|
|
44
|
+
} else if (isEquality(subresult)) {
|
|
45
|
+
// Combination of l and r subtype cases.
|
|
46
|
+
result.equalities.push([
|
|
47
|
+
lIndex,
|
|
48
|
+
rIndex
|
|
49
|
+
]);
|
|
50
|
+
lImpliesR = true;
|
|
51
|
+
rPairs.distinct = null;
|
|
52
|
+
return null;
|
|
53
|
+
} else if (hasDomain(subresult, "object")) {
|
|
54
|
+
// Neither branch is a subtype of the other, return
|
|
55
|
+
// the result of the intersection as a candidate
|
|
56
|
+
// branch for the final union
|
|
57
|
+
return subresult;
|
|
58
|
+
}
|
|
59
|
+
return throwInternalError(`Unexpected predicate intersection result of type '${domainOf(subresult)}'`);
|
|
60
|
+
});
|
|
61
|
+
if (!lImpliesR) {
|
|
62
|
+
for(let i = 0; i < pairs.length; i++){
|
|
63
|
+
if (distinct[i]) {
|
|
64
|
+
pairs[i].distinct?.push(distinct[i]);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
result.distinctIntersections = pairs.flatMap((pairs)=>pairs.distinct ?? []);
|
|
70
|
+
return result;
|
|
71
|
+
};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
function _checkPrivateRedeclaration(obj, privateCollection) {
|
|
2
|
+
if (privateCollection.has(obj)) {
|
|
3
|
+
throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
function _classApplyDescriptorGet(receiver, descriptor) {
|
|
7
|
+
if (descriptor.get) {
|
|
8
|
+
return descriptor.get.call(receiver);
|
|
9
|
+
}
|
|
10
|
+
return descriptor.value;
|
|
11
|
+
}
|
|
12
|
+
function _classApplyDescriptorSet(receiver, descriptor, value) {
|
|
13
|
+
if (descriptor.set) {
|
|
14
|
+
descriptor.set.call(receiver, value);
|
|
15
|
+
} else {
|
|
16
|
+
if (!descriptor.writable) {
|
|
17
|
+
throw new TypeError("attempted to set read only private field");
|
|
18
|
+
}
|
|
19
|
+
descriptor.value = value;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function _classExtractFieldDescriptor(receiver, privateMap, action) {
|
|
23
|
+
if (!privateMap.has(receiver)) {
|
|
24
|
+
throw new TypeError("attempted to " + action + " private field on non-instance");
|
|
25
|
+
}
|
|
26
|
+
return privateMap.get(receiver);
|
|
27
|
+
}
|
|
28
|
+
function _classPrivateFieldGet(receiver, privateMap) {
|
|
29
|
+
var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get");
|
|
30
|
+
return _classApplyDescriptorGet(receiver, descriptor);
|
|
31
|
+
}
|
|
32
|
+
function _classPrivateFieldInit(obj, privateMap, value) {
|
|
33
|
+
_checkPrivateRedeclaration(obj, privateMap);
|
|
34
|
+
privateMap.set(obj, value);
|
|
35
|
+
}
|
|
36
|
+
function _classPrivateFieldSet(receiver, privateMap, value) {
|
|
37
|
+
var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set");
|
|
38
|
+
_classApplyDescriptorSet(receiver, descriptor, value);
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
import { throwInternalError } from "../utils/errors.js";
|
|
42
|
+
import { keysOf } from "../utils/generics.js";
|
|
43
|
+
import { Path } from "../utils/paths.js";
|
|
44
|
+
import { serialize } from "../utils/serialize.js";
|
|
45
|
+
export const composeIntersection = (reducer)=>(l, r, state)=>l === undefined ? r === undefined ? throwUndefinedOperandsError() : r : r === undefined ? l : reducer(l, r, state);
|
|
46
|
+
export const throwUndefinedOperandsError = ()=>throwInternalError(`Unexpected operation two undefined operands`);
|
|
47
|
+
export const disjointDescriptionWriters = {
|
|
48
|
+
domain: ({ l , r })=>`${l.join(", ")} and ${r.join(", ")}`,
|
|
49
|
+
subdomain: ({ l , r })=>`${l} and ${r}`,
|
|
50
|
+
range: ({ l , r })=>`${stringifyRange(l)} and ${stringifyRange(r)}`,
|
|
51
|
+
class: ({ l , r })=>`classes ${l.name} and ${r.name}`,
|
|
52
|
+
tupleLength: ({ l , r })=>`tuples of length ${l} and ${r}`,
|
|
53
|
+
value: ({ l , r })=>`literal values ${serialize(l)} and ${serialize(r)}`,
|
|
54
|
+
leftAssignability: ({ l , r })=>`literal value ${serialize(l.value)} and ${serialize(r)}`,
|
|
55
|
+
rightAssignability: ({ l , r })=>`literal value ${serialize(r.value)} and ${serialize(l)}`
|
|
56
|
+
};
|
|
57
|
+
export const stringifyRange = (range)=>"limit" in range ? `the range of exactly ${range.limit}` : range.min ? range.max ? `the range bounded by ${range.min.comparator}${range.min.limit} and ${range.max.comparator}${range.max.limit}` : `${range.min.comparator}${range.min.limit}` : range.max ? `${range.max.comparator}${range.max.limit}` : "the unbounded range";
|
|
58
|
+
var _disjoints = /*#__PURE__*/ new WeakMap();
|
|
59
|
+
export class IntersectionState {
|
|
60
|
+
get disjoints() {
|
|
61
|
+
return _classPrivateFieldGet(this, _disjoints);
|
|
62
|
+
}
|
|
63
|
+
addDisjoint(kind, l, r) {
|
|
64
|
+
_classPrivateFieldGet(this, _disjoints)[`${this.path}`] = {
|
|
65
|
+
kind,
|
|
66
|
+
l,
|
|
67
|
+
r
|
|
68
|
+
};
|
|
69
|
+
return empty;
|
|
70
|
+
}
|
|
71
|
+
constructor(type, lastOperator){
|
|
72
|
+
_classPrivateFieldInit(this, _disjoints, {
|
|
73
|
+
writable: true,
|
|
74
|
+
value: void 0
|
|
75
|
+
});
|
|
76
|
+
this.type = type;
|
|
77
|
+
this.lastOperator = lastOperator;
|
|
78
|
+
this.path = new Path();
|
|
79
|
+
_classPrivateFieldSet(this, _disjoints, {});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const empty = Symbol("empty");
|
|
83
|
+
export const anonymousDisjoint = ()=>empty;
|
|
84
|
+
export const isDisjoint = (result)=>result === empty;
|
|
85
|
+
const equal = Symbol("equal");
|
|
86
|
+
export const equality = ()=>equal;
|
|
87
|
+
export const isEquality = (result)=>result === equal;
|
|
88
|
+
export const composeKeyedIntersection = (reducer, config)=>(l, r, state)=>{
|
|
89
|
+
const result = {};
|
|
90
|
+
const keys = keysOf({
|
|
91
|
+
...l,
|
|
92
|
+
...r
|
|
93
|
+
});
|
|
94
|
+
let lImpliesR = true;
|
|
95
|
+
let rImpliesL = true;
|
|
96
|
+
for (const k of keys){
|
|
97
|
+
const keyResult = typeof reducer === "function" ? reducer(k, l[k], r[k], state) : reducer[k](l[k], r[k], state);
|
|
98
|
+
if (isEquality(keyResult)) {
|
|
99
|
+
if (l[k] !== undefined) {
|
|
100
|
+
result[k] = l[k];
|
|
101
|
+
}
|
|
102
|
+
} else if (isDisjoint(keyResult)) {
|
|
103
|
+
if (config.onEmpty === "omit") {
|
|
104
|
+
lImpliesR = false;
|
|
105
|
+
rImpliesL = false;
|
|
106
|
+
} else {
|
|
107
|
+
return empty;
|
|
108
|
+
}
|
|
109
|
+
} else {
|
|
110
|
+
if (keyResult !== undefined) {
|
|
111
|
+
result[k] = keyResult;
|
|
112
|
+
}
|
|
113
|
+
lImpliesR && (lImpliesR = keyResult === l[k]);
|
|
114
|
+
rImpliesL && (rImpliesL = keyResult === r[k]);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return lImpliesR ? rImpliesL ? equality() : l : rImpliesL ? r : result;
|
|
118
|
+
};
|