arktype 0.5.1 → 0.6.0
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/dist/api.cjs +4 -2
- package/dist/api.d.ts +1 -1
- package/dist/api.mjs +1 -1
- package/dist/cjs/nodes/branch.js +3 -3
- package/dist/cjs/nodes/discriminate.js +2 -2
- package/dist/cjs/nodes/node.js +13 -18
- package/dist/cjs/nodes/predicate.js +1 -6
- package/dist/cjs/nodes/rules/props.js +39 -0
- package/dist/cjs/nodes/rules/rules.js +5 -2
- package/dist/cjs/parse/ast/config.js +15 -0
- package/dist/cjs/parse/ast/distributableFunction.js +1 -1
- package/dist/cjs/parse/ast/keyof.js +1 -1
- package/dist/cjs/parse/ast/morph.js +3 -3
- package/dist/cjs/{nodes/infer.js → parse/ast/node.js} +0 -0
- package/dist/cjs/parse/ast/tuple.js +2 -1
- package/dist/cjs/parse/definition.js +2 -2
- package/dist/cjs/parse/string/reduce/dynamic.js +2 -2
- package/dist/cjs/parse/string/shift/operand/unenclosed.js +1 -1
- package/dist/cjs/parse/string/string.js +3 -3
- package/dist/cjs/scopes/{standard.js → ark.js} +9 -8
- package/dist/cjs/scopes/expressions.js +9 -9
- package/dist/cjs/scopes/scope.js +73 -63
- package/dist/cjs/scopes/type.js +10 -29
- package/dist/cjs/scopes/validation/creditCard.js +47 -0
- package/dist/cjs/scopes/validation/date.js +63 -0
- package/dist/cjs/scopes/validation/validation.js +90 -0
- package/dist/cjs/traverse/problems.js +45 -16
- package/dist/cjs/traverse/traverse.js +63 -26
- package/dist/mjs/nodes/branch.js +2 -2
- package/dist/mjs/nodes/discriminate.js +2 -2
- package/dist/mjs/nodes/node.js +13 -19
- package/dist/mjs/nodes/predicate.js +1 -5
- package/dist/mjs/nodes/rules/props.js +39 -0
- package/dist/mjs/nodes/rules/rules.js +5 -2
- package/dist/mjs/parse/ast/config.js +7 -0
- package/dist/mjs/parse/ast/distributableFunction.js +1 -1
- package/dist/mjs/parse/ast/keyof.js +1 -1
- package/dist/mjs/parse/ast/morph.js +4 -4
- package/dist/mjs/{nodes/infer.js → parse/ast/node.js} +0 -0
- package/dist/mjs/parse/ast/tuple.js +2 -1
- package/dist/mjs/parse/definition.js +2 -2
- package/dist/mjs/parse/string/reduce/dynamic.js +2 -2
- package/dist/mjs/parse/string/shift/operand/unenclosed.js +1 -1
- package/dist/mjs/parse/string/string.js +3 -3
- package/dist/mjs/scopes/{standard.js → ark.js} +8 -7
- package/dist/mjs/scopes/expressions.js +9 -9
- package/dist/mjs/scopes/scope.js +69 -63
- package/dist/mjs/scopes/type.js +10 -29
- package/dist/mjs/scopes/validation/creditCard.js +34 -0
- package/dist/mjs/scopes/validation/date.js +49 -0
- package/dist/mjs/scopes/validation/validation.js +76 -0
- package/dist/mjs/traverse/problems.js +42 -15
- package/dist/mjs/traverse/traverse.js +64 -27
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/nodes/branch.d.ts +6 -6
- package/dist/types/nodes/node.d.ts +10 -5
- package/dist/types/nodes/predicate.d.ts +2 -6
- package/dist/types/nodes/rules/props.d.ts +15 -0
- package/dist/types/nodes/rules/rules.d.ts +2 -2
- package/dist/types/parse/ast/ast.d.ts +1 -1
- package/dist/types/parse/ast/config.d.ts +10 -0
- package/dist/types/parse/ast/narrow.d.ts +1 -1
- package/dist/types/{nodes/infer.d.ts → parse/ast/node.d.ts} +12 -12
- package/dist/types/parse/ast/tuple.d.ts +4 -3
- package/dist/types/scopes/{standard.d.ts → ark.d.ts} +12 -4
- package/dist/types/scopes/expressions.d.ts +3 -3
- package/dist/types/scopes/scope.d.ts +19 -7
- package/dist/types/scopes/type.d.ts +20 -12
- package/dist/types/scopes/validation/creditCard.d.ts +2 -0
- package/dist/types/scopes/validation/date.d.ts +23 -0
- package/dist/types/scopes/validation/validation.d.ts +26 -0
- package/dist/types/traverse/problems.d.ts +14 -8
- package/dist/types/traverse/traverse.d.ts +13 -3
- package/dist/types/utils/generics.d.ts +5 -5
- package/package.json +1 -1
- package/dist/cjs/scopes/validation.js +0 -79
- package/dist/mjs/scopes/validation.js +0 -66
- package/dist/types/scopes/validation.d.ts +0 -25
|
@@ -65,22 +65,38 @@ function _classPrivateFieldSet(receiver, privateMap, value) {
|
|
|
65
65
|
_classApplyDescriptorSet(receiver, descriptor, value);
|
|
66
66
|
return value;
|
|
67
67
|
}
|
|
68
|
+
const initializeTraversalConfig = ()=>({
|
|
69
|
+
mustBe: [],
|
|
70
|
+
writeReason: [],
|
|
71
|
+
addContext: [],
|
|
72
|
+
keys: []
|
|
73
|
+
});
|
|
74
|
+
const problemWriterKeys = [
|
|
75
|
+
"mustBe",
|
|
76
|
+
"writeReason",
|
|
77
|
+
"addContext"
|
|
78
|
+
];
|
|
68
79
|
var _seen = /*#__PURE__*/ new WeakMap();
|
|
69
80
|
class TraversalState {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
81
|
+
getProblemConfig(code) {
|
|
82
|
+
const result = {};
|
|
83
|
+
for (const k of problemWriterKeys){
|
|
84
|
+
result[k] = this.traversalConfig[k][0] ?? this.rootScope.config.codes[code][k];
|
|
73
85
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
getConfigKey(k) {
|
|
89
|
+
return this.traversalConfig[k][0];
|
|
90
|
+
}
|
|
91
|
+
traverseConfig(configEntries, node) {
|
|
92
|
+
for (const entry of configEntries){
|
|
93
|
+
this.traversalConfig[entry[0]].unshift(entry[1]);
|
|
82
94
|
}
|
|
83
|
-
|
|
95
|
+
const isValid = traverse(node, this);
|
|
96
|
+
for (const entry1 of configEntries){
|
|
97
|
+
this.traversalConfig[entry1[0]].shift();
|
|
98
|
+
}
|
|
99
|
+
return isValid;
|
|
84
100
|
}
|
|
85
101
|
traverseKey(key, node) {
|
|
86
102
|
const lastData = this.data;
|
|
@@ -93,8 +109,8 @@ class TraversalState {
|
|
|
93
109
|
return isValid;
|
|
94
110
|
}
|
|
95
111
|
traverseResolution(name) {
|
|
96
|
-
const resolution = this.type.
|
|
97
|
-
const id = resolution.
|
|
112
|
+
const resolution = this.type.scope.resolve(name);
|
|
113
|
+
const id = resolution.qualifiedName;
|
|
98
114
|
// this assignment helps with narrowing
|
|
99
115
|
const data = this.data;
|
|
100
116
|
const isObject = (0, _domainsTs.hasDomain)(data, "object");
|
|
@@ -114,10 +130,10 @@ class TraversalState {
|
|
|
114
130
|
];
|
|
115
131
|
}
|
|
116
132
|
}
|
|
117
|
-
const
|
|
133
|
+
const lastType = this.type;
|
|
118
134
|
this.type = resolution;
|
|
119
135
|
const isValid = traverse(resolution.flat, this);
|
|
120
|
-
this.type =
|
|
136
|
+
this.type = lastType;
|
|
121
137
|
if (isObject) {
|
|
122
138
|
_classPrivateFieldGet(this, _seen)[id].pop();
|
|
123
139
|
}
|
|
@@ -153,10 +169,9 @@ class TraversalState {
|
|
|
153
169
|
this.path = new _pathsTs.Path();
|
|
154
170
|
this.problems = new _problemsTs.Problems(this);
|
|
155
171
|
this.failFast = false;
|
|
172
|
+
this.traversalConfig = initializeTraversalConfig();
|
|
156
173
|
_classPrivateFieldSet(this, _seen, {});
|
|
157
|
-
this.
|
|
158
|
-
type.meta.scope.config
|
|
159
|
-
] : [];
|
|
174
|
+
this.rootScope = type.scope;
|
|
160
175
|
}
|
|
161
176
|
}
|
|
162
177
|
const traverse = (node, state)=>typeof node === "string" ? (0, _domainsTs.domainOf)(state.data) === node || !state.problems.add("domain", node) : checkEntries(node, state);
|
|
@@ -190,6 +205,31 @@ const checkRequiredProp = (prop, state)=>{
|
|
|
190
205
|
});
|
|
191
206
|
return false;
|
|
192
207
|
};
|
|
208
|
+
const createPropChecker = (kind)=>(props, state)=>{
|
|
209
|
+
let isValid = true;
|
|
210
|
+
const unseenRequired = {
|
|
211
|
+
...props.required
|
|
212
|
+
};
|
|
213
|
+
for(const k in state.data){
|
|
214
|
+
if (props.required[k]) {
|
|
215
|
+
isValid || (isValid = state.traverseKey(k, props.required[k]));
|
|
216
|
+
delete unseenRequired[k];
|
|
217
|
+
} else if (props.optional[k]) {
|
|
218
|
+
isValid || (isValid = state.traverseKey(k, props.optional[k]));
|
|
219
|
+
} else if (kind === "distilledProps") {
|
|
220
|
+
delete state.data[k];
|
|
221
|
+
} else {
|
|
222
|
+
state.problems.add("extraneous", state.data[k], {
|
|
223
|
+
path: state.path.concat(k)
|
|
224
|
+
});
|
|
225
|
+
isValid = false;
|
|
226
|
+
}
|
|
227
|
+
if (!isValid && state.failFast) {
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return isValid;
|
|
232
|
+
};
|
|
193
233
|
const entryCheckers = {
|
|
194
234
|
regex: _regexTs.checkRegex,
|
|
195
235
|
divisor: _divisorTs.checkDivisor,
|
|
@@ -242,12 +282,7 @@ const entryCheckers = {
|
|
|
242
282
|
alias: (name, state)=>state.traverseResolution(name),
|
|
243
283
|
class: _classTs.checkClass,
|
|
244
284
|
narrow: (narrow, state)=>narrow(state.data, state.problems),
|
|
245
|
-
config: ({ config , node }, state)=>
|
|
246
|
-
state.configs.push(config);
|
|
247
|
-
const result = traverse(node, state);
|
|
248
|
-
state.configs.pop();
|
|
249
|
-
return result;
|
|
250
|
-
},
|
|
285
|
+
config: ({ config , node }, state)=>state.traverseConfig(config, node),
|
|
251
286
|
value: (value, state)=>state.data === value || !state.problems.add("value", value),
|
|
252
287
|
morph: (morph, state)=>{
|
|
253
288
|
const out = morph(state.data, state.problems);
|
|
@@ -261,5 +296,7 @@ const entryCheckers = {
|
|
|
261
296
|
}
|
|
262
297
|
state.data = out;
|
|
263
298
|
return true;
|
|
264
|
-
}
|
|
299
|
+
},
|
|
300
|
+
distilledProps: createPropChecker("distilledProps"),
|
|
301
|
+
strictProps: createPropChecker("strictProps")
|
|
265
302
|
};
|
package/dist/mjs/nodes/branch.js
CHANGED
|
@@ -70,9 +70,9 @@ export const compareBranches = (lConditions, rConditions, state)=>{
|
|
|
70
70
|
result.distinctIntersections = pairs.flatMap((pairs)=>pairs.distinct ?? []);
|
|
71
71
|
return result;
|
|
72
72
|
};
|
|
73
|
-
export const
|
|
73
|
+
export const isTransformationBranch = (branch)=>"rules" in branch;
|
|
74
74
|
export const flattenBranch = (branch, ctx)=>{
|
|
75
|
-
if (
|
|
75
|
+
if (isTransformationBranch(branch)) {
|
|
76
76
|
const result = flattenRules(branch.rules, ctx);
|
|
77
77
|
if (branch.morph) {
|
|
78
78
|
if (typeof branch.morph === "function") {
|
|
@@ -22,7 +22,7 @@ const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
|
|
|
22
22
|
return [
|
|
23
23
|
[
|
|
24
24
|
"branches",
|
|
25
|
-
remainingIndices.map((i)=>branchIncludesMorph(originalBranches[i], ctx.type.
|
|
25
|
+
remainingIndices.map((i)=>branchIncludesMorph(originalBranches[i], ctx.type.scope) ? throwParseError(writeUndiscriminatableMorphUnionMessage(`${ctx.path}`)) : flattenBranch(originalBranches[i], ctx))
|
|
26
26
|
]
|
|
27
27
|
];
|
|
28
28
|
}
|
|
@@ -244,4 +244,4 @@ const serializeData = {
|
|
|
244
244
|
};
|
|
245
245
|
export const serializeCase = (kind, data)=>serializeData[kind](data);
|
|
246
246
|
const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph(propToNode(prop), $)) : false;
|
|
247
|
-
const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).
|
|
247
|
+
const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).includesMorph : Object.values(node).some((predicate)=>predicate === true ? false : isArray(predicate) ? predicate.some((branch)=>branchIncludesMorph(branch, $)) : branchIncludesMorph(predicate, $));
|
package/dist/mjs/nodes/node.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { compileDisjointReasonsMessage } from "../parse/ast/intersection.js";
|
|
2
2
|
import { throwParseError } from "../utils/errors.js";
|
|
3
|
-
import { hasKey, hasKeys, objectKeysOf } from "../utils/generics.js";
|
|
3
|
+
import { entriesOf, hasKey, hasKeys, objectKeysOf } from "../utils/generics.js";
|
|
4
4
|
import { Path } from "../utils/paths.js";
|
|
5
5
|
import { anonymousDisjoint, composeKeyedIntersection, IntersectionState, isDisjoint, isEquality, throwUndefinedOperandsError } from "./compose.js";
|
|
6
|
-
import { flattenPredicate, isLiteralCondition, predicateIntersection, predicateUnion
|
|
6
|
+
import { flattenPredicate, isLiteralCondition, predicateIntersection, predicateUnion } from "./predicate.js";
|
|
7
7
|
import { mappedKeys } from "./rules/props.js";
|
|
8
8
|
export const nodeIntersection = (l, r, state)=>{
|
|
9
9
|
state.domain = undefined;
|
|
10
|
-
const lResolution = state.type.
|
|
11
|
-
const rResolution = state.type.
|
|
10
|
+
const lResolution = state.type.scope.resolveNode(l);
|
|
11
|
+
const rResolution = state.type.scope.resolveNode(r);
|
|
12
12
|
const result = resolutionIntersection(lResolution, rResolution, state);
|
|
13
13
|
if (typeof result === "object" && !hasKeys(result)) {
|
|
14
14
|
return hasKeys(state.disjoints) ? anonymousDisjoint() : state.addDisjoint("domain", objectKeysOf(lResolution), objectKeysOf(rResolution));
|
|
@@ -32,8 +32,8 @@ export const rootIntersection = (l, r, type)=>{
|
|
|
32
32
|
return isDisjoint(result) ? throwParseError(compileDisjointReasonsMessage(state.disjoints)) : isEquality(result) ? l : result;
|
|
33
33
|
};
|
|
34
34
|
export const rootUnion = (l, r, type)=>{
|
|
35
|
-
const lResolution = type.
|
|
36
|
-
const rResolution = type.
|
|
35
|
+
const lResolution = type.scope.resolveNode(l);
|
|
36
|
+
const rResolution = type.scope.resolveNode(r);
|
|
37
37
|
const result = {};
|
|
38
38
|
const domains = objectKeysOf({
|
|
39
39
|
...lResolution,
|
|
@@ -51,11 +51,11 @@ export const flattenType = (type)=>{
|
|
|
51
51
|
path: new Path(),
|
|
52
52
|
lastDomain: "undefined"
|
|
53
53
|
};
|
|
54
|
-
return type.
|
|
54
|
+
return type.config ? [
|
|
55
55
|
[
|
|
56
56
|
"config",
|
|
57
57
|
{
|
|
58
|
-
config: type.
|
|
58
|
+
config: entriesOf(type.config),
|
|
59
59
|
node: flattenNode(type.node, ctx)
|
|
60
60
|
}
|
|
61
61
|
]
|
|
@@ -63,17 +63,7 @@ export const flattenType = (type)=>{
|
|
|
63
63
|
};
|
|
64
64
|
export const flattenNode = (node, ctx)=>{
|
|
65
65
|
if (typeof node === "string") {
|
|
66
|
-
|
|
67
|
-
const updatedScopeConfig = resolution.meta.scope !== ctx.type.meta.scope && resolution.meta.scope.config;
|
|
68
|
-
return updatedScopeConfig ? [
|
|
69
|
-
[
|
|
70
|
-
"config",
|
|
71
|
-
{
|
|
72
|
-
config: updatedScopeConfig,
|
|
73
|
-
node: resolution.flat
|
|
74
|
-
}
|
|
75
|
-
]
|
|
76
|
-
] : resolution.flat;
|
|
66
|
+
return ctx.type.scope.resolve(node).flat;
|
|
77
67
|
}
|
|
78
68
|
const domains = objectKeysOf(node);
|
|
79
69
|
if (domains.length === 1) {
|
|
@@ -107,6 +97,10 @@ export const flattenNode = (node, ctx)=>{
|
|
|
107
97
|
export const isLiteralNode = (node, domain)=>{
|
|
108
98
|
return resolutionExtendsDomain(node, domain) && isLiteralCondition(node[domain]);
|
|
109
99
|
};
|
|
100
|
+
export const resolutionExtendsDomain = (resolution, domain)=>{
|
|
101
|
+
const domains = objectKeysOf(resolution);
|
|
102
|
+
return domains.length === 1 && domains[0] === domain;
|
|
103
|
+
};
|
|
110
104
|
export const toArrayNode = (node)=>({
|
|
111
105
|
object: {
|
|
112
106
|
class: "Array",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { listFrom
|
|
1
|
+
import { listFrom } from "../utils/generics.js";
|
|
2
2
|
import { isArray } from "../utils/objectKinds.js";
|
|
3
3
|
import { branchIntersection, compareBranches, flattenBranch, isBranchComparison } from "./branch.js";
|
|
4
4
|
import { equality, IntersectionState, isEquality } from "./compose.js";
|
|
@@ -68,7 +68,3 @@ export const flattenPredicate = (predicate, context)=>{
|
|
|
68
68
|
return isArray(predicate) ? flattenBranches(predicate, context) : flattenBranch(predicate, context);
|
|
69
69
|
};
|
|
70
70
|
export const isLiteralCondition = (predicate)=>typeof predicate === "object" && "value" in predicate;
|
|
71
|
-
export const resolutionExtendsDomain = (resolution, domain)=>{
|
|
72
|
-
const domains = objectKeysOf(resolution);
|
|
73
|
-
return domains.length === 1 && domains[0] === domain;
|
|
74
|
-
};
|
|
@@ -65,6 +65,11 @@ const propKeysIntersection = composeKeyedIntersection((propKey, l, r, context)=>
|
|
|
65
65
|
onEmpty: "bubble"
|
|
66
66
|
});
|
|
67
67
|
export const flattenProps = (entries, props, ctx)=>{
|
|
68
|
+
const keyConfig = ctx.type.config?.keys ?? ctx.type.scope.config.keys;
|
|
69
|
+
return keyConfig === "loose" ? flattenLooseProps(entries, props, ctx) : flattenPropsRecord(keyConfig, entries, props, ctx);
|
|
70
|
+
};
|
|
71
|
+
const flattenLooseProps = (entries, props, ctx)=>{
|
|
72
|
+
// if we don't care about extraneous keys, flatten props so we can iterate over the definitions directly
|
|
68
73
|
for(const k in props){
|
|
69
74
|
const prop = props[k];
|
|
70
75
|
ctx.path.push(k);
|
|
@@ -101,3 +106,37 @@ export const flattenProps = (entries, props, ctx)=>{
|
|
|
101
106
|
ctx.path.pop();
|
|
102
107
|
}
|
|
103
108
|
};
|
|
109
|
+
const flattenPropsRecord = (kind, entries, props, ctx)=>{
|
|
110
|
+
const result = {
|
|
111
|
+
required: {},
|
|
112
|
+
optional: {}
|
|
113
|
+
};
|
|
114
|
+
// if we need to keep track of extraneous keys, either to add problems or
|
|
115
|
+
// remove them, store the props as a Record to optimize for presence
|
|
116
|
+
// checking as we iterate over the data
|
|
117
|
+
for(const k in props){
|
|
118
|
+
const prop = props[k];
|
|
119
|
+
ctx.path.push(k);
|
|
120
|
+
if (k === mappedKeys.index) {
|
|
121
|
+
result.index = flattenNode(propToNode(prop), ctx);
|
|
122
|
+
} else if (isOptional(prop)) {
|
|
123
|
+
result.optional[k] = flattenNode(prop[1], ctx);
|
|
124
|
+
} else if (isPrerequisite(prop)) {
|
|
125
|
+
// we still have to push prerequisite props as normal entries so they can be checked first
|
|
126
|
+
entries.push([
|
|
127
|
+
"prerequisiteProp",
|
|
128
|
+
[
|
|
129
|
+
k,
|
|
130
|
+
flattenNode(prop[1], ctx)
|
|
131
|
+
]
|
|
132
|
+
]);
|
|
133
|
+
} else {
|
|
134
|
+
result.required[k] = flattenNode(prop, ctx);
|
|
135
|
+
}
|
|
136
|
+
ctx.path.pop();
|
|
137
|
+
}
|
|
138
|
+
entries.push([
|
|
139
|
+
`${kind}Props`,
|
|
140
|
+
result
|
|
141
|
+
]);
|
|
142
|
+
};
|
|
@@ -69,8 +69,9 @@ const ruleFlatteners = {
|
|
|
69
69
|
}
|
|
70
70
|
};
|
|
71
71
|
export const precedenceMap = {
|
|
72
|
+
// Config: Applies before any checks
|
|
73
|
+
config: -1,
|
|
72
74
|
// Critical: No other checks are performed if these fail
|
|
73
|
-
config: 0,
|
|
74
75
|
domain: 0,
|
|
75
76
|
value: 0,
|
|
76
77
|
domains: 0,
|
|
@@ -87,6 +88,8 @@ export const precedenceMap = {
|
|
|
87
88
|
// is invalid
|
|
88
89
|
prerequisiteProp: 2,
|
|
89
90
|
// Deep: Performed if all shallow checks pass, even if one or more deep checks fail
|
|
91
|
+
distilledProps: 3,
|
|
92
|
+
strictProps: 3,
|
|
90
93
|
requiredProp: 3,
|
|
91
94
|
optionalProp: 3,
|
|
92
95
|
indexProp: 3,
|
|
@@ -95,7 +98,7 @@ export const precedenceMap = {
|
|
|
95
98
|
// Morph: Only performed if all validation passes
|
|
96
99
|
morph: 5
|
|
97
100
|
};
|
|
98
|
-
export const literalSatisfiesRules = (data, rules, state)=>!state.type.
|
|
101
|
+
export const literalSatisfiesRules = (data, rules, state)=>!state.type.scope.type([
|
|
99
102
|
"node",
|
|
100
103
|
{
|
|
101
104
|
[state.domain]: rules
|
|
@@ -4,7 +4,7 @@ import { objectKeysOf } from "../../utils/generics.js";
|
|
|
4
4
|
import { stringify } from "../../utils/serialize.js";
|
|
5
5
|
export const writeMalformedDistributableFunctionMessage = (def)=>`Expected a Function or Record<Domain, Function> operand (${stringify(def)} was invalid)`;
|
|
6
6
|
export const distributeFunctionToNode = (distributableFunction, inputNode, ctx, ruleKey)=>{
|
|
7
|
-
const domains = objectKeysOf(ctx.type.
|
|
7
|
+
const domains = objectKeysOf(ctx.type.scope.resolveNode(inputNode));
|
|
8
8
|
if (!hasDomain(distributableFunction, "object")) {
|
|
9
9
|
return throwParseError(writeMalformedDistributableFunctionMessage(distributableFunction));
|
|
10
10
|
}
|
|
@@ -21,7 +21,7 @@ const arrayIndexNumberBranch = deepFreeze({
|
|
|
21
21
|
divisor: 1
|
|
22
22
|
});
|
|
23
23
|
export const parseKeyOfTuple = (def, ctx)=>{
|
|
24
|
-
const resolution = ctx.type.
|
|
24
|
+
const resolution = ctx.type.scope.resolveNode(parseDefinition(def[1], ctx));
|
|
25
25
|
const predicateKeys = objectKeysOf(resolution).map((domain)=>keysOfPredicate(domain, resolution[domain]));
|
|
26
26
|
const sharedKeys = sharedKeysOf(predicateKeys);
|
|
27
27
|
if (!sharedKeys.length) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isTransformationBranch } from "../../nodes/branch.js";
|
|
2
2
|
import { throwInternalError, throwParseError } from "../../utils/errors.js";
|
|
3
3
|
import { isArray } from "../../utils/objectKinds.js";
|
|
4
4
|
import { stringify } from "../../utils/serialize.js";
|
|
@@ -7,9 +7,9 @@ export const parseMorphTuple = (def, ctx)=>{
|
|
|
7
7
|
if (typeof def[2] !== "function") {
|
|
8
8
|
return throwParseError(writeMalformedMorphExpressionMessage(def[2]));
|
|
9
9
|
}
|
|
10
|
-
const resolution = ctx.type.
|
|
10
|
+
const resolution = ctx.type.scope.resolveNode(parseDefinition(def[0], ctx));
|
|
11
11
|
const morph = def[2];
|
|
12
|
-
ctx.type.
|
|
12
|
+
ctx.type.includesMorph = true;
|
|
13
13
|
let domain;
|
|
14
14
|
const result = {};
|
|
15
15
|
for(domain in resolution){
|
|
@@ -27,7 +27,7 @@ export const parseMorphTuple = (def, ctx)=>{
|
|
|
27
27
|
}
|
|
28
28
|
return result;
|
|
29
29
|
};
|
|
30
|
-
const applyMorph = (branch, morph)=>
|
|
30
|
+
const applyMorph = (branch, morph)=>isTransformationBranch(branch) ? {
|
|
31
31
|
...branch,
|
|
32
32
|
morph: branch.morph ? Array.isArray(branch.morph) ? [
|
|
33
33
|
...branch.morph,
|
|
File without changes
|
|
@@ -3,6 +3,7 @@ import { domainOf } from "../../utils/domains.js";
|
|
|
3
3
|
import { throwParseError } from "../../utils/errors.js";
|
|
4
4
|
import { parseDefinition } from "../definition.js";
|
|
5
5
|
import { writeMissingRightOperandMessage } from "../string/shift/operand/unenclosed.js";
|
|
6
|
+
import { parseConfigTuple } from "./config.js";
|
|
6
7
|
import { parseKeyOfTuple } from "./keyof.js";
|
|
7
8
|
import { parseMorphTuple } from "./morph.js";
|
|
8
9
|
import { parseNarrowTuple } from "./narrow.js";
|
|
@@ -55,7 +56,7 @@ const indexOneParsers = {
|
|
|
55
56
|
"[]": parseArrayTuple,
|
|
56
57
|
"=>": parseNarrowTuple,
|
|
57
58
|
"|>": parseMorphTuple,
|
|
58
|
-
":":
|
|
59
|
+
":": parseConfigTuple
|
|
59
60
|
};
|
|
60
61
|
const prefixParsers = {
|
|
61
62
|
keyof: parseKeyOfTuple,
|
|
@@ -28,12 +28,12 @@ export const parseDefinition = (def, ctx)=>{
|
|
|
28
28
|
};
|
|
29
29
|
case "Function":
|
|
30
30
|
if (isType(def)) {
|
|
31
|
-
return def
|
|
31
|
+
return ctx.type.scope.addAnonymous(def, ctx);
|
|
32
32
|
}
|
|
33
33
|
if (isThunk(def)) {
|
|
34
34
|
const returned = def();
|
|
35
35
|
if (isType(returned)) {
|
|
36
|
-
return returned
|
|
36
|
+
return ctx.type.scope.addAnonymous(returned, ctx);
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
return throwParseError(writeBadDefinitionTypeMessage("Function"));
|
|
@@ -14,7 +14,7 @@ export class DynamicState {
|
|
|
14
14
|
}
|
|
15
15
|
resolveRoot() {
|
|
16
16
|
this.assertHasRoot();
|
|
17
|
-
return this.ctx.type.
|
|
17
|
+
return this.ctx.type.scope.resolveNode(this.root);
|
|
18
18
|
}
|
|
19
19
|
rootToString() {
|
|
20
20
|
this.assertHasRoot();
|
|
@@ -22,7 +22,7 @@ export class DynamicState {
|
|
|
22
22
|
}
|
|
23
23
|
ejectRootIfLimit() {
|
|
24
24
|
this.assertHasRoot();
|
|
25
|
-
const resolution = typeof this.root === "string" ? this.ctx.type.
|
|
25
|
+
const resolution = typeof this.root === "string" ? this.ctx.type.scope.resolveNode(this.root) : this.root;
|
|
26
26
|
if (isLiteralNode(resolution, "number")) {
|
|
27
27
|
const limit = resolution.number.value;
|
|
28
28
|
this.root = undefined;
|
|
@@ -4,7 +4,7 @@ export const parseUnenclosed = (s)=>{
|
|
|
4
4
|
s.setRoot(unenclosedToNode(s, token));
|
|
5
5
|
};
|
|
6
6
|
const unenclosedToNode = (s, token)=>{
|
|
7
|
-
if (s.ctx.type.
|
|
7
|
+
if (s.ctx.type.scope.addReferenceIfResolvable(token, s.ctx)) {
|
|
8
8
|
return token;
|
|
9
9
|
}
|
|
10
10
|
return maybeParseUnenclosedLiteral(token) ?? s.error(token === "" ? writeMissingOperandMessage(s) : writeUnresolvableMessage(token));
|
|
@@ -2,14 +2,14 @@ import { toArrayNode } from "../../nodes/node.js";
|
|
|
2
2
|
import { DynamicState } from "./reduce/dynamic.js";
|
|
3
3
|
import { parseOperand } from "./shift/operand/operand.js";
|
|
4
4
|
import { parseOperator } from "./shift/operator/operator.js";
|
|
5
|
-
export const parseString = (def, ctx)=>ctx.type.
|
|
5
|
+
export const parseString = (def, ctx)=>ctx.type.scope.parseCache.get(def) ?? ctx.type.scope.parseCache.set(def, maybeNaiveParse(def, ctx) ?? fullStringParse(def, ctx));
|
|
6
6
|
export const maybeNaiveParse = (def, ctx)=>{
|
|
7
|
-
if (ctx.type.
|
|
7
|
+
if (ctx.type.scope.addReferenceIfResolvable(def, ctx)) {
|
|
8
8
|
return def;
|
|
9
9
|
}
|
|
10
10
|
if (def.endsWith("[]")) {
|
|
11
11
|
const elementDef = def.slice(0, -2);
|
|
12
|
-
if (ctx.type.
|
|
12
|
+
if (ctx.type.scope.addReferenceIfResolvable(def, ctx)) {
|
|
13
13
|
return toArrayNode(elementDef);
|
|
14
14
|
}
|
|
15
15
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { jsObjects, jsObjectsScope } from "./jsObjects.js";
|
|
2
|
-
import { scope } from "./scope.js";
|
|
2
|
+
import { rootScope, scope } from "./scope.js";
|
|
3
3
|
import { tsKeywords, tsKeywordsScope } from "./tsKeywords.js";
|
|
4
|
-
import { validation, validationScope } from "./validation.js";
|
|
5
|
-
export const
|
|
4
|
+
import { validation, validationScope } from "./validation/validation.js";
|
|
5
|
+
export const ark = scope({}, {
|
|
6
6
|
name: "standard",
|
|
7
7
|
includes: [
|
|
8
8
|
tsKeywords,
|
|
@@ -11,17 +11,18 @@ export const standardScope = scope({}, {
|
|
|
11
11
|
],
|
|
12
12
|
standard: false
|
|
13
13
|
});
|
|
14
|
-
export const
|
|
14
|
+
export const arktypes = ark.compile();
|
|
15
15
|
export const scopes = {
|
|
16
|
+
root: rootScope,
|
|
16
17
|
tsKeywords: tsKeywordsScope,
|
|
17
18
|
jsObjects: jsObjectsScope,
|
|
18
19
|
validation: validationScope,
|
|
19
|
-
|
|
20
|
+
ark
|
|
20
21
|
};
|
|
21
22
|
export const spaces = {
|
|
22
23
|
tsKeywords,
|
|
23
24
|
jsObjects,
|
|
24
25
|
validation,
|
|
25
|
-
|
|
26
|
+
ark: arktypes
|
|
26
27
|
};
|
|
27
|
-
export const type =
|
|
28
|
+
export const type = ark.type;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { scopes } from "./
|
|
2
|
-
export const intersection = scopes.
|
|
3
|
-
export const union = scopes.
|
|
4
|
-
export const arrayOf = scopes.
|
|
5
|
-
export const keyOf = scopes.
|
|
6
|
-
export const instanceOf = scopes.
|
|
7
|
-
export const valueOf = scopes.
|
|
8
|
-
export const narrow = scopes.
|
|
9
|
-
export const morph = scopes.
|
|
1
|
+
import { scopes } from "./ark.js";
|
|
2
|
+
export const intersection = scopes.ark.intersection;
|
|
3
|
+
export const union = scopes.ark.union;
|
|
4
|
+
export const arrayOf = scopes.ark.arrayOf;
|
|
5
|
+
export const keyOf = scopes.ark.keyOf;
|
|
6
|
+
export const instanceOf = scopes.ark.instanceOf;
|
|
7
|
+
export const valueOf = scopes.ark.valueOf;
|
|
8
|
+
export const narrow = scopes.ark.narrow;
|
|
9
|
+
export const morph = scopes.ark.morph;
|