arktype 0.2.0 → 0.3.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.
Files changed (80) hide show
  1. package/dist/cjs/nodes/{branches.js → branch.js} +56 -10
  2. package/dist/cjs/nodes/compose.js +1 -2
  3. package/dist/cjs/nodes/discriminate.js +7 -13
  4. package/dist/cjs/nodes/node.js +12 -2
  5. package/dist/cjs/nodes/predicate.js +7 -9
  6. package/dist/cjs/nodes/rules/class.js +10 -4
  7. package/dist/cjs/nodes/rules/divisor.js +1 -5
  8. package/dist/cjs/nodes/rules/props.js +75 -25
  9. package/dist/cjs/nodes/rules/range.js +1 -5
  10. package/dist/cjs/nodes/rules/regex.js +1 -5
  11. package/dist/cjs/nodes/rules/rules.js +19 -63
  12. package/dist/cjs/parse/string/reduce/dynamic.js +1 -2
  13. package/dist/cjs/parse/string/shift/operator/bounds.js +1 -1
  14. package/dist/cjs/parse/string/string.js +2 -2
  15. package/dist/cjs/parse/tuple/keyof.js +37 -18
  16. package/dist/cjs/parse/tuple/morph.js +2 -2
  17. package/dist/cjs/parse/tuple/tuple.js +16 -9
  18. package/dist/cjs/scopes/jsObjects.js +14 -14
  19. package/dist/cjs/scopes/type.js +4 -7
  20. package/dist/cjs/traverse/problems.js +12 -28
  21. package/dist/cjs/traverse/traverse.js +104 -133
  22. package/dist/cjs/utils/numericLiterals.js +2 -0
  23. package/dist/cjs/utils/objectKinds.js +1 -2
  24. package/dist/mjs/nodes/{branches.js → branch.js} +45 -2
  25. package/dist/mjs/nodes/compose.js +1 -2
  26. package/dist/mjs/nodes/discriminate.js +6 -12
  27. package/dist/mjs/nodes/node.js +10 -1
  28. package/dist/mjs/nodes/predicate.js +2 -3
  29. package/dist/mjs/nodes/rules/class.js +10 -4
  30. package/dist/mjs/nodes/rules/divisor.js +1 -5
  31. package/dist/mjs/nodes/rules/props.js +74 -27
  32. package/dist/mjs/nodes/rules/range.js +1 -5
  33. package/dist/mjs/nodes/rules/regex.js +1 -5
  34. package/dist/mjs/nodes/rules/rules.js +19 -62
  35. package/dist/mjs/parse/string/reduce/dynamic.js +2 -3
  36. package/dist/mjs/parse/string/shift/operator/bounds.js +1 -1
  37. package/dist/mjs/parse/string/string.js +2 -2
  38. package/dist/mjs/parse/tuple/keyof.js +38 -19
  39. package/dist/mjs/parse/tuple/morph.js +2 -2
  40. package/dist/mjs/parse/tuple/tuple.js +17 -10
  41. package/dist/mjs/scopes/jsObjects.js +14 -14
  42. package/dist/mjs/scopes/type.js +4 -7
  43. package/dist/mjs/traverse/problems.js +13 -29
  44. package/dist/mjs/traverse/traverse.js +105 -133
  45. package/dist/mjs/utils/numericLiterals.js +1 -0
  46. package/dist/mjs/utils/objectKinds.js +1 -1
  47. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  48. package/dist/types/nodes/branch.d.ts +36 -0
  49. package/dist/types/nodes/compose.d.ts +5 -10
  50. package/dist/types/nodes/discriminate.d.ts +1 -1
  51. package/dist/types/nodes/infer.d.ts +17 -16
  52. package/dist/types/nodes/node.d.ts +10 -5
  53. package/dist/types/nodes/predicate.d.ts +4 -32
  54. package/dist/types/nodes/rules/class.d.ts +3 -2
  55. package/dist/types/nodes/rules/divisor.d.ts +2 -1
  56. package/dist/types/nodes/rules/props.d.ts +19 -11
  57. package/dist/types/nodes/rules/range.d.ts +3 -2
  58. package/dist/types/nodes/rules/regex.d.ts +2 -1
  59. package/dist/types/nodes/rules/rules.d.ts +11 -13
  60. package/dist/types/parse/string/ast.d.ts +2 -1
  61. package/dist/types/parse/string/reduce/dynamic.d.ts +1 -1
  62. package/dist/types/parse/string/reduce/shared.d.ts +1 -1
  63. package/dist/types/parse/string/shift/operand/unenclosed.d.ts +1 -1
  64. package/dist/types/parse/string/shift/operator/bounds.d.ts +1 -1
  65. package/dist/types/parse/string/string.d.ts +1 -1
  66. package/dist/types/parse/tuple/keyof.d.ts +2 -2
  67. package/dist/types/scopes/type.d.ts +5 -11
  68. package/dist/types/traverse/problems.d.ts +8 -23
  69. package/dist/types/traverse/traverse.d.ts +27 -23
  70. package/dist/types/utils/generics.d.ts +0 -1
  71. package/dist/types/utils/numericLiterals.d.ts +1 -0
  72. package/dist/types/utils/objectKinds.d.ts +43 -43
  73. package/package.json +1 -2
  74. package/dist/cjs/nodes/functors.js +0 -35
  75. package/dist/cjs/nodes/rules/objectKind.js +0 -78
  76. package/dist/mjs/nodes/functors.js +0 -27
  77. package/dist/mjs/nodes/rules/objectKind.js +0 -63
  78. package/dist/types/nodes/branches.d.ts +0 -14
  79. package/dist/types/nodes/functors.d.ts +0 -6
  80. package/dist/types/nodes/rules/objectKind.d.ts +0 -15
@@ -1,8 +1,50 @@
1
- import { composeIntersection, composeKeyedIntersection, equality, isDisjoint } from "../compose.js";
2
- import { flattenNode, nodeIntersection } from "../node.js";
1
+ import { hasKey } from "../../utils/generics.js";
2
+ import { composeIntersection, composeKeyedIntersection, equality, isDisjoint, isEquality } from "../compose.js";
3
+ import { flattenNode, isLiteralNode, nodeIntersection } from "../node.js";
3
4
  export const isOptional = (prop)=>prop[0] === "?";
4
- const nodeFrom = (prop)=>isOptional(prop) ? prop[1] : prop;
5
- export const propsIntersection = composeIntersection(composeKeyedIntersection((propKey, l, r, context)=>{
5
+ export const isPrerequisite = (prop)=>prop[0] === "!";
6
+ export const mappedKeys = {
7
+ index: "[index]"
8
+ };
9
+ export const propToNode = (prop)=>isOptional(prop) || isPrerequisite(prop) ? prop[1] : prop;
10
+ const getTupleLengthIfPresent = (result)=>{
11
+ if (typeof result.length === "object" && isPrerequisite(result.length) && typeof result.length[1] !== "string" && isLiteralNode(result.length[1], "number")) {
12
+ return result.length[1].number.value;
13
+ }
14
+ };
15
+ export const propsIntersection = composeIntersection((l, r, state)=>{
16
+ const result = propKeysIntersection(l, r, state);
17
+ if (typeof result === "symbol") {
18
+ return result;
19
+ }
20
+ const lengthValue = getTupleLengthIfPresent(result);
21
+ if (lengthValue === undefined || !hasKey(result, mappedKeys.index)) {
22
+ return result;
23
+ }
24
+ // if we are at this point, we have an array with an exact length (i.e.
25
+ // a tuple) and an index signature. Intersection each tuple item with
26
+ // the index signature node and remove the index signature via a new
27
+ // updated result, copied from result to avoid mutating existing references.
28
+ const { [mappedKeys.index]: indexProp , ...updatedResult } = result;
29
+ const indexNode = propToNode(indexProp);
30
+ for(let i = 0; i < lengthValue; i++){
31
+ if (!updatedResult[i]) {
32
+ updatedResult[i] = indexNode;
33
+ continue;
34
+ }
35
+ const existingNodeAtIndex = propToNode(updatedResult[i]);
36
+ state.path.push(`${i}`);
37
+ const updatedResultAtIndex = nodeIntersection(existingNodeAtIndex, indexNode, state);
38
+ state.path.pop();
39
+ if (isDisjoint(updatedResultAtIndex)) {
40
+ return updatedResultAtIndex;
41
+ } else if (!isEquality(updatedResultAtIndex) && updatedResultAtIndex !== existingNodeAtIndex) {
42
+ updatedResult[i] = updatedResultAtIndex;
43
+ }
44
+ }
45
+ return updatedResult;
46
+ });
47
+ const propKeysIntersection = composeKeyedIntersection((propKey, l, r, context)=>{
6
48
  if (l === undefined) {
7
49
  return r === undefined ? equality() : r;
8
50
  }
@@ -10,7 +52,7 @@ export const propsIntersection = composeIntersection(composeKeyedIntersection((p
10
52
  return l;
11
53
  }
12
54
  context.path.push(propKey);
13
- const result = nodeIntersection(nodeFrom(l), nodeFrom(r), context);
55
+ const result = nodeIntersection(propToNode(l), propToNode(r), context);
14
56
  context.path.pop();
15
57
  const resultIsOptional = isOptional(l) && isOptional(r);
16
58
  if (isDisjoint(result) && resultIsOptional) {
@@ -22,36 +64,41 @@ export const propsIntersection = composeIntersection(composeKeyedIntersection((p
22
64
  return result;
23
65
  }, {
24
66
  onEmpty: "bubble"
25
- }));
67
+ });
26
68
  export const flattenProps = (entries, props, ctx)=>{
27
- const requiredProps = [];
28
- const optionalProps = [];
29
69
  for(const k in props){
30
70
  const prop = props[k];
31
71
  ctx.path.push(k);
32
- if (isOptional(prop)) {
33
- optionalProps.push([
34
- k,
35
- flattenNode(prop[1], ctx)
72
+ if (k === mappedKeys.index) {
73
+ entries.push([
74
+ "indexProp",
75
+ flattenNode(propToNode(prop), ctx)
76
+ ]);
77
+ } else if (isOptional(prop)) {
78
+ entries.push([
79
+ "optionalProp",
80
+ [
81
+ k,
82
+ flattenNode(prop[1], ctx)
83
+ ]
84
+ ]);
85
+ } else if (isPrerequisite(prop)) {
86
+ entries.push([
87
+ "prerequisiteProp",
88
+ [
89
+ k,
90
+ flattenNode(prop[1], ctx)
91
+ ]
36
92
  ]);
37
93
  } else {
38
- requiredProps.push([
39
- k,
40
- flattenNode(prop, ctx)
94
+ entries.push([
95
+ "requiredProp",
96
+ [
97
+ k,
98
+ flattenNode(prop, ctx)
99
+ ]
41
100
  ]);
42
101
  }
43
102
  ctx.path.pop();
44
103
  }
45
- if (requiredProps.length) {
46
- entries.push([
47
- "requiredProps",
48
- requiredProps
49
- ]);
50
- }
51
- if (optionalProps.length) {
52
- entries.push([
53
- "optionalProps",
54
- optionalProps
55
- ]);
56
- }
57
104
  };
@@ -74,11 +74,7 @@ export const flattenRange = (entries, range, ctx)=>{
74
74
  ]);
75
75
  }
76
76
  };
77
- export const checkBound = (data, bound, state)=>{
78
- if (!comparatorCheckers[bound.comparator](sizeOf(data), bound.limit)) {
79
- state.problems.add("bound", data, bound);
80
- }
81
- };
77
+ export const checkBound = (bound, state)=>comparatorCheckers[bound.comparator](sizeOf(state.data), bound.limit) || state.problems.add("bound", state.data, bound);
82
78
  const comparatorCheckers = {
83
79
  "<": (size, limit)=>size < limit,
84
80
  ">": (size, limit)=>size > limit,
@@ -7,9 +7,5 @@ export const getRegex = (source)=>{
7
7
  }
8
8
  return regexCache[source];
9
9
  };
10
- export const checkRegex = (data, source, state)=>{
11
- if (!getRegex(source).test(data)) {
12
- state.problems.add("regex", data, state.path.length === 0 && state.type.name !== "type" ? state.type.name : `/${source}/`);
13
- }
14
- };
10
+ export const checkRegex = (source, state)=>getRegex(source).test(state.data) || state.problems.add("regex", state.data, `/${source}/`);
15
11
  export const regexIntersection = composeIntersection(collapsibleListUnion);
@@ -1,47 +1,14 @@
1
- import { writeImplicitNeverMessage } from "../../parse/string/ast.js";
2
- import { throwParseError } from "../../utils/errors.js";
3
1
  import { listFrom } from "../../utils/generics.js";
4
- import { composeIntersection, composeKeyedIntersection, equality, isDisjoint, isEquality } from "../compose.js";
5
- import { branchIsTransformation } from "../predicate.js";
2
+ import { composeIntersection, composeKeyedIntersection, equality } from "../compose.js";
6
3
  import { classIntersection } from "./class.js";
7
4
  import { collapsibleListUnion } from "./collapsibleSet.js";
8
5
  import { divisorIntersection } from "./divisor.js";
9
- import { flattenObjectKind, objectKindIntersection } from "./objectKind.js";
10
6
  import { flattenProps, propsIntersection } from "./props.js";
11
7
  import { flattenRange, rangeIntersection } from "./range.js";
12
8
  import { regexIntersection } from "./regex.js";
13
- const rulesOf = (branch)=>branch.rules ?? branch;
14
- export const branchIntersection = (l, r, state)=>{
15
- const lRules = rulesOf(l);
16
- const rRules = rulesOf(r);
17
- const rulesResult = rulesIntersection(lRules, rRules, state);
18
- if ("morph" in l) {
19
- if ("morph" in r) {
20
- if (l.morph === r.morph) {
21
- return isEquality(rulesResult) || isDisjoint(rulesResult) ? rulesResult : {
22
- rules: rulesResult,
23
- morph: l.morph
24
- };
25
- }
26
- return state.lastOperator === "&" ? throwParseError(writeImplicitNeverMessage(state.path, "Intersection", "of morphs")) : {};
27
- }
28
- return isDisjoint(rulesResult) ? rulesResult : {
29
- rules: isEquality(rulesResult) ? l.rules : rulesResult,
30
- morph: l.morph
31
- };
32
- }
33
- if ("morph" in r) {
34
- return isDisjoint(rulesResult) ? rulesResult : {
35
- rules: isEquality(rulesResult) ? r.rules : rulesResult,
36
- morph: r.morph
37
- };
38
- }
39
- return rulesResult;
40
- };
41
9
  export const rulesIntersection = (l, r, state)=>"value" in l ? "value" in r ? l.value === r.value ? equality() : state.addDisjoint("value", l.value, r.value) : literalSatisfiesRules(l.value, r, state) ? l : state.addDisjoint("leftAssignability", l, r) : "value" in r ? literalSatisfiesRules(r.value, l, state) ? r : state.addDisjoint("rightAssignability", l, r) : narrowableRulesIntersection(l, r, state);
42
10
  const narrowIntersection = composeIntersection(collapsibleListUnion);
43
11
  export const narrowableRulesIntersection = composeKeyedIntersection({
44
- objectKind: objectKindIntersection,
45
12
  divisor: divisorIntersection,
46
13
  regex: regexIntersection,
47
14
  props: propsIntersection,
@@ -51,8 +18,15 @@ export const narrowableRulesIntersection = composeKeyedIntersection({
51
18
  }, {
52
19
  onEmpty: "bubble"
53
20
  });
21
+ export const flattenRules = (rules, ctx)=>{
22
+ const entries = [];
23
+ let k;
24
+ for(k in rules){
25
+ ruleFlatteners[k](entries, rules[k], ctx);
26
+ }
27
+ return entries.sort((l, r)=>precedenceMap[l[0]] - precedenceMap[r[0]]);
28
+ };
54
29
  const ruleFlatteners = {
55
- objectKind: flattenObjectKind,
56
30
  regex: (entries, rule)=>{
57
31
  for (const source of listFrom(rule)){
58
32
  entries.push([
@@ -97,42 +71,25 @@ export const precedenceMap = {
97
71
  value: 0,
98
72
  domains: 0,
99
73
  branches: 0,
100
- objectKind: 0,
101
74
  switch: 0,
102
75
  alias: 0,
76
+ class: 0,
103
77
  // Shallow: All shallow checks will be performed even if one or more fail
104
- class: 1,
105
78
  regex: 1,
106
79
  divisor: 1,
107
80
  bound: 1,
81
+ // Prerequisite: These are deep checks with special priority, e.g. the
82
+ // length of a tuple, which causes other deep props not to be checked if it
83
+ // is invalid
84
+ prerequisiteProp: 2,
108
85
  // Deep: Performed if all shallow checks pass, even if one or more deep checks fail
109
- requiredProps: 2,
110
- optionalProps: 2,
86
+ requiredProp: 3,
87
+ optionalProp: 3,
88
+ indexProp: 3,
111
89
  // Narrow: Only performed if all shallow and deep checks pass
112
- narrow: 3,
90
+ narrow: 4,
113
91
  // Morph: Only performed if all validation passes
114
- morph: 4
115
- };
116
- export const flattenBranch = (branch, ctx)=>{
117
- if (branchIsTransformation(branch)) {
118
- const result = flattenRules(branch.rules, ctx);
119
- if (branch.morph) {
120
- result.push([
121
- "morph",
122
- branch.morph
123
- ]);
124
- }
125
- return result;
126
- }
127
- return flattenRules(branch, ctx);
128
- };
129
- const flattenRules = (rules, ctx)=>{
130
- const entries = [];
131
- let k;
132
- for(k in rules){
133
- ruleFlatteners[k](entries, rules[k], ctx);
134
- }
135
- return entries.sort((l, r)=>precedenceMap[l[0]] - precedenceMap[r[0]]);
92
+ morph: 5
136
93
  };
137
94
  export const literalSatisfiesRules = (data, rules, state)=>!state.type.meta.scope.type([
138
95
  "node",
@@ -1,5 +1,4 @@
1
- import { functors } from "../../../nodes/functors.js";
2
- import { intersection, isLiteralNode, union } from "../../../nodes/node.js";
1
+ import { arrayOf, intersection, isLiteralNode, union } from "../../../nodes/node.js";
3
2
  import { minComparators } from "../../../nodes/rules/range.js";
4
3
  import { throwInternalError, throwParseError } from "../../../utils/errors.js";
5
4
  import { isKeyOf } from "../../../utils/generics.js";
@@ -52,7 +51,7 @@ export class DynamicState {
52
51
  this.root = node;
53
52
  }
54
53
  rootToArray() {
55
- this.root = functors.Array(this.ejectRoot());
54
+ this.root = arrayOf(this.ejectRoot());
56
55
  }
57
56
  intersect(node) {
58
57
  this.root = intersection(this.ejectRoot(), node, this.ctx.type);
@@ -54,7 +54,7 @@ const distributeRange = (range, s)=>{
54
54
  if (resolution.object === true) {
55
55
  return false;
56
56
  }
57
- return listFrom(resolution.object).every((branch)=>hasKey(branch, "objectKind") && (branch.objectKind === "Array" || branch.objectKind[0] === "Array"));
57
+ return listFrom(resolution.object).every((branch)=>hasKey(branch, "class") && branch.class === "Array");
58
58
  default:
59
59
  return false;
60
60
  }
@@ -1,4 +1,4 @@
1
- import { functors } from "../../nodes/functors.js";
1
+ import { arrayOf } 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";
@@ -10,7 +10,7 @@ export const maybeNaiveParse = (def, ctx)=>{
10
10
  if (def.endsWith("[]")) {
11
11
  const elementDef = def.slice(0, -2);
12
12
  if (ctx.type.meta.scope.addReferenceIfResolvable(def, ctx)) {
13
- return functors.Array(elementDef);
13
+ return arrayOf(elementDef);
14
14
  }
15
15
  }
16
16
  };
@@ -1,8 +1,13 @@
1
1
  import { resolutionExtendsDomain } from "../../nodes/predicate.js";
2
+ import { mappedKeys } from "../../nodes/rules/props.js";
2
3
  import { throwParseError } from "../../utils/errors.js";
3
- import { hasKey, listFrom } from "../../utils/generics.js";
4
+ import { listFrom } from "../../utils/generics.js";
5
+ import { wellFormedNonNegativeIntegerMatcher } from "../../utils/numericLiterals.js";
4
6
  import { parseDefinition } from "../definition.js";
5
7
  import { writeImplicitNeverMessage } from "../string/ast.js";
8
+ const numericIndexKey = {
9
+ regex: wellFormedNonNegativeIntegerMatcher.source
10
+ };
6
11
  export const parseKeyOfTuple = (def, ctx)=>{
7
12
  const resolution = ctx.type.meta.scope.resolveNode(parseDefinition(def[1], ctx));
8
13
  if (!resolutionExtendsDomain(resolution, "object")) {
@@ -11,27 +16,41 @@ export const parseKeyOfTuple = (def, ctx)=>{
11
16
  if (resolution.object === true) {
12
17
  return writeImplicitNeverMessage(ctx.path, "keyof");
13
18
  }
14
- const branchKeys = [];
15
19
  const objectBranches = listFrom(resolution.object);
16
- const initialKeys = getPropsFromBranch(objectBranches[0]);
20
+ let sharedKeys = keysOfBranch(objectBranches[0]);
17
21
  for(let i = 1; i < objectBranches.length; i++){
18
- const keys = getPropsFromBranch(objectBranches[i]);
19
- if (!keys.length) {
20
- return writeImplicitNeverMessage(ctx.path, "keyof");
21
- }
22
- branchKeys.push(keys);
22
+ const branchKeys = keysOfBranch(objectBranches[i]);
23
+ // we can filter directly by equality here because the RegExp we're
24
+ // using will always be reference equal to
25
+ // wellFormedNonNegativeIntegerMatcher
26
+ sharedKeys = sharedKeys.filter((k)=>branchKeys.includes(k));
23
27
  }
24
- const result = [];
25
- initialKeys.forEach((key)=>{
26
- const hasKey = branchKeys.every((keySet)=>keySet.includes(key));
27
- if (hasKey) {
28
- result.push({
29
- value: key
30
- });
31
- }
32
- });
28
+ const result = sharedKeys.map((k)=>typeof k === "string" ? {
29
+ value: k
30
+ } : {
31
+ regex: k.source
32
+ });
33
33
  return result.length ? {
34
- string: result
34
+ string: result.length === 1 ? result[0] : result
35
35
  } : writeImplicitNeverMessage(ctx.path, "keyof");
36
36
  };
37
- const getPropsFromBranch = (branch)=>hasKey(branch, "props") ? Object.keys(branch.props) : [];
37
+ const keysOfBranch = (branch)=>{
38
+ if (!("props" in branch)) {
39
+ return [];
40
+ }
41
+ if (branch.class !== "Array") {
42
+ return Object.keys(branch.props);
43
+ }
44
+ const arrayProps = [];
45
+ for (const key of Object.keys(branch.props)){
46
+ if (key === "length") {
47
+ continue;
48
+ }
49
+ if (key === mappedKeys.index) {
50
+ arrayProps.push(wellFormedNonNegativeIntegerMatcher);
51
+ } else {
52
+ arrayProps.push(key);
53
+ }
54
+ }
55
+ return arrayProps;
56
+ };
@@ -1,4 +1,4 @@
1
- import { branchIsTransformation } from "../../nodes/predicate.js";
1
+ import { isMorphBranch } 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";
@@ -27,7 +27,7 @@ export const parseMorphTuple = (def, ctx)=>{
27
27
  }
28
28
  return result;
29
29
  };
30
- const applyMorph = (branch, morph)=>branchIsTransformation(branch) ? {
30
+ const applyMorph = (branch, morph)=>isMorphBranch(branch) ? {
31
31
  ...branch,
32
32
  morph: branch.morph ? Array.isArray(branch.morph) ? [
33
33
  ...branch.morph,
@@ -1,5 +1,4 @@
1
- import { functors } from "../../nodes/functors.js";
2
- import { intersection, union } from "../../nodes/node.js";
1
+ import { arrayOf, intersection, union } from "../../nodes/node.js";
3
2
  import { domainOf } from "../../utils/domains.js";
4
3
  import { throwParseError } from "../../utils/errors.js";
5
4
  import { parseDefinition } from "../definition.js";
@@ -14,7 +13,19 @@ export const parseTuple = (def, ctx)=>{
14
13
  if (isPrefixExpression(def)) {
15
14
  return prefixParsers[def[0]](def, ctx);
16
15
  }
17
- const props = {};
16
+ const props = {
17
+ // length is created as a prerequisite prop, ensuring if it is invalid,
18
+ // no other props will be checked, which is usually desirable for tuple
19
+ // definitions.
20
+ length: [
21
+ "!",
22
+ {
23
+ number: {
24
+ value: def.length
25
+ }
26
+ }
27
+ ]
28
+ };
18
29
  for(let i = 0; i < def.length; i++){
19
30
  ctx.path.push(`${i}`);
20
31
  props[i] = parseDefinition(def[i], ctx);
@@ -22,12 +33,8 @@ export const parseTuple = (def, ctx)=>{
22
33
  }
23
34
  return {
24
35
  object: {
25
- objectKind: "Array",
26
- props,
27
- range: {
28
- comparator: "==",
29
- limit: def.length
30
- }
36
+ class: "Array",
37
+ props
31
38
  }
32
39
  };
33
40
  };
@@ -39,7 +46,7 @@ const parseBranchTuple = (def, ctx)=>{
39
46
  const r = parseDefinition(def[2], ctx);
40
47
  return def[1] === "&" ? intersection(l, r, ctx.type) : union(l, r, ctx.type);
41
48
  };
42
- const parseArrayTuple = (def, scope)=>functors.Array(parseDefinition(def[0], scope));
49
+ const parseArrayTuple = (def, scope)=>arrayOf(parseDefinition(def[0], scope));
43
50
  export const writeMalformedFunctionalExpressionMessage = (operator, rightDef)=>`Expression requires a function following '${operator}' (was ${typeof rightDef})`;
44
51
  const isPostfixExpression = (def)=>postfixParsers[def[1]] !== undefined;
45
52
  const postfixParsers = {
@@ -4,7 +4,7 @@ export const jsObjectsScope = scope({
4
4
  "node",
5
5
  {
6
6
  object: {
7
- objectKind: "Function"
7
+ class: "Function"
8
8
  }
9
9
  }
10
10
  ],
@@ -12,7 +12,7 @@ export const jsObjectsScope = scope({
12
12
  "node",
13
13
  {
14
14
  object: {
15
- objectKind: "Array"
15
+ class: "Array"
16
16
  }
17
17
  }
18
18
  ],
@@ -20,7 +20,7 @@ export const jsObjectsScope = scope({
20
20
  "node",
21
21
  {
22
22
  object: {
23
- objectKind: "Date"
23
+ class: "Date"
24
24
  }
25
25
  }
26
26
  ],
@@ -28,7 +28,7 @@ export const jsObjectsScope = scope({
28
28
  "node",
29
29
  {
30
30
  object: {
31
- objectKind: "Error"
31
+ class: "Error"
32
32
  }
33
33
  }
34
34
  ],
@@ -36,7 +36,7 @@ export const jsObjectsScope = scope({
36
36
  "node",
37
37
  {
38
38
  object: {
39
- objectKind: "Map"
39
+ class: "Map"
40
40
  }
41
41
  }
42
42
  ],
@@ -44,7 +44,7 @@ export const jsObjectsScope = scope({
44
44
  "node",
45
45
  {
46
46
  object: {
47
- objectKind: "RegExp"
47
+ class: "RegExp"
48
48
  }
49
49
  }
50
50
  ],
@@ -52,7 +52,7 @@ export const jsObjectsScope = scope({
52
52
  "node",
53
53
  {
54
54
  object: {
55
- objectKind: "Set"
55
+ class: "Set"
56
56
  }
57
57
  }
58
58
  ],
@@ -60,7 +60,7 @@ export const jsObjectsScope = scope({
60
60
  "node",
61
61
  {
62
62
  object: {
63
- objectKind: "Object"
63
+ class: "Object"
64
64
  }
65
65
  }
66
66
  ],
@@ -68,7 +68,7 @@ export const jsObjectsScope = scope({
68
68
  "node",
69
69
  {
70
70
  object: {
71
- objectKind: "String"
71
+ class: "String"
72
72
  }
73
73
  }
74
74
  ],
@@ -76,7 +76,7 @@ export const jsObjectsScope = scope({
76
76
  "node",
77
77
  {
78
78
  object: {
79
- objectKind: "Number"
79
+ class: "Number"
80
80
  }
81
81
  }
82
82
  ],
@@ -84,7 +84,7 @@ export const jsObjectsScope = scope({
84
84
  "node",
85
85
  {
86
86
  object: {
87
- objectKind: "Boolean"
87
+ class: "Boolean"
88
88
  }
89
89
  }
90
90
  ],
@@ -92,7 +92,7 @@ export const jsObjectsScope = scope({
92
92
  "node",
93
93
  {
94
94
  object: {
95
- objectKind: "WeakMap"
95
+ class: "WeakMap"
96
96
  }
97
97
  }
98
98
  ],
@@ -100,7 +100,7 @@ export const jsObjectsScope = scope({
100
100
  "node",
101
101
  {
102
102
  object: {
103
- objectKind: "WeakSet"
103
+ class: "WeakSet"
104
104
  }
105
105
  }
106
106
  ],
@@ -108,7 +108,7 @@ export const jsObjectsScope = scope({
108
108
  "node",
109
109
  {
110
110
  object: {
111
- objectKind: "Promise"
111
+ class: "Promise"
112
112
  }
113
113
  }
114
114
  ]
@@ -37,14 +37,11 @@ export const initializeType = (definition, opts, scope)=>{
37
37
  // dynamically assign a name to the primary traversal function
38
38
  const namedTraverse = {
39
39
  [name]: (data)=>{
40
- const state = new TraversalState(type);
41
- const out = traverse(data, type.flat, state);
42
- return state.problems.length ? {
43
- data,
44
- problems: state.problems
40
+ const state = new TraversalState(data, type);
41
+ return traverse(type.flat, state) ? {
42
+ data: state.data
45
43
  } : {
46
- data,
47
- out
44
+ problems: state.problems
48
45
  };
49
46
  }
50
47
  }[name];