arktype 0.1.4 → 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 (106) hide show
  1. package/README.md +1 -1
  2. package/dist/api.cjs +4 -3
  3. package/dist/api.d.ts +3 -2
  4. package/dist/api.mjs +2 -1
  5. package/dist/cjs/nodes/{branches.js → branch.js} +56 -10
  6. package/dist/cjs/nodes/compose.js +1 -2
  7. package/dist/cjs/nodes/discriminate.js +7 -13
  8. package/dist/cjs/nodes/node.js +32 -4
  9. package/dist/cjs/nodes/predicate.js +7 -7
  10. package/dist/cjs/nodes/rules/class.js +10 -4
  11. package/dist/cjs/nodes/rules/divisor.js +1 -5
  12. package/dist/cjs/nodes/rules/props.js +75 -25
  13. package/dist/cjs/nodes/rules/range.js +1 -5
  14. package/dist/cjs/nodes/rules/regex.js +2 -44
  15. package/dist/cjs/nodes/rules/rules.js +21 -61
  16. package/dist/cjs/parse/definition.js +2 -2
  17. package/dist/cjs/parse/string/reduce/dynamic.js +1 -2
  18. package/dist/cjs/parse/string/shift/operator/bounds.js +1 -1
  19. package/dist/cjs/parse/string/string.js +2 -2
  20. package/dist/cjs/parse/tuple/keyof.js +37 -18
  21. package/dist/cjs/parse/tuple/morph.js +7 -6
  22. package/dist/cjs/parse/tuple/tuple.js +16 -9
  23. package/dist/cjs/scopes/cache.js +39 -0
  24. package/dist/cjs/scopes/jsObjects.js +133 -0
  25. package/dist/cjs/{main.js → scopes/scope.js} +31 -364
  26. package/dist/cjs/scopes/standard.js +44 -0
  27. package/dist/cjs/scopes/tsKeywords.js +113 -0
  28. package/dist/cjs/scopes/type.js +67 -0
  29. package/dist/cjs/scopes/validation.js +64 -0
  30. package/dist/cjs/traverse/problems.js +55 -95
  31. package/dist/cjs/traverse/traverse.js +127 -134
  32. package/dist/cjs/utils/numericLiterals.js +3 -0
  33. package/dist/cjs/utils/objectKinds.js +1 -2
  34. package/dist/mjs/nodes/{branches.js → branch.js} +45 -2
  35. package/dist/mjs/nodes/compose.js +1 -2
  36. package/dist/mjs/nodes/discriminate.js +6 -12
  37. package/dist/mjs/nodes/node.js +30 -3
  38. package/dist/mjs/nodes/predicate.js +2 -2
  39. package/dist/mjs/nodes/rules/class.js +10 -4
  40. package/dist/mjs/nodes/rules/divisor.js +1 -5
  41. package/dist/mjs/nodes/rules/props.js +74 -27
  42. package/dist/mjs/nodes/rules/range.js +1 -5
  43. package/dist/mjs/nodes/rules/regex.js +1 -43
  44. package/dist/mjs/nodes/rules/rules.js +22 -61
  45. package/dist/mjs/parse/definition.js +1 -1
  46. package/dist/mjs/parse/string/reduce/dynamic.js +2 -3
  47. package/dist/mjs/parse/string/shift/operator/bounds.js +1 -1
  48. package/dist/mjs/parse/string/string.js +2 -2
  49. package/dist/mjs/parse/tuple/keyof.js +38 -19
  50. package/dist/mjs/parse/tuple/morph.js +7 -6
  51. package/dist/mjs/parse/tuple/tuple.js +17 -10
  52. package/dist/mjs/scopes/cache.js +25 -0
  53. package/dist/mjs/scopes/jsObjects.js +119 -0
  54. package/dist/mjs/{main.js → scopes/scope.js} +15 -344
  55. package/dist/mjs/scopes/standard.js +27 -0
  56. package/dist/mjs/scopes/tsKeywords.js +99 -0
  57. package/dist/mjs/scopes/type.js +53 -0
  58. package/dist/mjs/scopes/validation.js +51 -0
  59. package/dist/mjs/traverse/problems.js +55 -95
  60. package/dist/mjs/traverse/traverse.js +129 -135
  61. package/dist/mjs/utils/numericLiterals.js +2 -1
  62. package/dist/mjs/utils/objectKinds.js +1 -1
  63. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  64. package/dist/types/nodes/branch.d.ts +36 -0
  65. package/dist/types/nodes/compose.d.ts +6 -11
  66. package/dist/types/nodes/discriminate.d.ts +1 -1
  67. package/dist/types/nodes/infer.d.ts +16 -15
  68. package/dist/types/nodes/node.d.ts +18 -9
  69. package/dist/types/nodes/predicate.d.ts +18 -57
  70. package/dist/types/nodes/rules/class.d.ts +3 -2
  71. package/dist/types/nodes/rules/divisor.d.ts +2 -1
  72. package/dist/types/nodes/rules/props.d.ts +19 -11
  73. package/dist/types/nodes/rules/range.d.ts +2 -1
  74. package/dist/types/nodes/rules/regex.d.ts +2 -11
  75. package/dist/types/nodes/rules/rules.d.ts +11 -21
  76. package/dist/types/parse/definition.d.ts +1 -1
  77. package/dist/types/parse/string/ast.d.ts +4 -2
  78. package/dist/types/parse/string/reduce/shared.d.ts +1 -1
  79. package/dist/types/parse/string/reduce/static.d.ts +1 -1
  80. package/dist/types/parse/string/shift/operator/bounds.d.ts +1 -1
  81. package/dist/types/parse/string/string.d.ts +1 -1
  82. package/dist/types/parse/tuple/keyof.d.ts +2 -2
  83. package/dist/types/parse/tuple/morph.d.ts +1 -1
  84. package/dist/types/parse/tuple/narrow.d.ts +1 -1
  85. package/dist/types/parse/tuple/tuple.d.ts +1 -1
  86. package/dist/types/scopes/cache.d.ts +14 -0
  87. package/dist/types/scopes/jsObjects.d.ts +36 -0
  88. package/dist/types/scopes/scope.d.ts +76 -0
  89. package/dist/types/scopes/standard.d.ts +280 -0
  90. package/dist/types/scopes/tsKeywords.d.ts +32 -0
  91. package/dist/types/scopes/type.d.ts +47 -0
  92. package/dist/types/scopes/validation.d.ts +21 -0
  93. package/dist/types/traverse/problems.d.ts +35 -53
  94. package/dist/types/traverse/traverse.d.ts +30 -24
  95. package/dist/types/utils/generics.d.ts +0 -1
  96. package/dist/types/utils/numericLiterals.d.ts +2 -0
  97. package/dist/types/utils/objectKinds.d.ts +43 -43
  98. package/package.json +2 -3
  99. package/dist/cjs/nodes/functors.js +0 -35
  100. package/dist/cjs/nodes/rules/objectKind.js +0 -78
  101. package/dist/mjs/nodes/functors.js +0 -27
  102. package/dist/mjs/nodes/rules/objectKind.js +0 -63
  103. package/dist/types/main.d.ts +0 -324
  104. package/dist/types/nodes/branches.d.ts +0 -14
  105. package/dist/types/nodes/functors.d.ts +0 -6
  106. package/dist/types/nodes/rules/objectKind.d.ts +0 -15
@@ -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,3 +1,4 @@
1
+ import { isMorphBranch } from "../../nodes/branch.js";
1
2
  import { throwInternalError, throwParseError } from "../../utils/errors.js";
2
3
  import { isArray } from "../../utils/objectKinds.js";
3
4
  import { stringify } from "../../utils/serialize.js";
@@ -15,7 +16,7 @@ export const parseMorphTuple = (def, ctx)=>{
15
16
  const predicate = resolution[domain];
16
17
  if (predicate === true) {
17
18
  result[domain] = {
18
- input: {},
19
+ rules: {},
19
20
  morph
20
21
  };
21
22
  } else if (typeof predicate === "object") {
@@ -26,17 +27,17 @@ export const parseMorphTuple = (def, ctx)=>{
26
27
  }
27
28
  return result;
28
29
  };
29
- const applyMorph = (branch, morph)=>branch.morph ? {
30
- input: branch.input,
31
- morph: Array.isArray(branch.morph) ? [
30
+ const applyMorph = (branch, morph)=>isMorphBranch(branch) ? {
31
+ ...branch,
32
+ morph: branch.morph ? Array.isArray(branch.morph) ? [
32
33
  ...branch.morph,
33
34
  morph
34
35
  ] : [
35
36
  branch.morph,
36
37
  morph
37
- ]
38
+ ] : morph
38
39
  } : {
39
- input: branch,
40
+ rules: branch,
40
41
  morph
41
42
  };
42
43
  export const writeMalformedMorphExpressionMessage = (value)=>`Morph expression requires a function following '=>' (was ${typeof value})`;
@@ -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 = {
@@ -0,0 +1,25 @@
1
+ import { deepFreeze } from "../utils/freeze.js";
2
+ export class Cache {
3
+ get root() {
4
+ return this.cache;
5
+ }
6
+ has(name) {
7
+ return name in this.cache;
8
+ }
9
+ get(name) {
10
+ return this.cache[name];
11
+ }
12
+ set(name, item) {
13
+ this.cache[name] = item;
14
+ return item;
15
+ }
16
+ constructor(){
17
+ this.cache = {};
18
+ }
19
+ }
20
+ export class FreezingCache extends Cache {
21
+ set(name, item) {
22
+ this.cache[name] = deepFreeze(item);
23
+ return item;
24
+ }
25
+ }
@@ -0,0 +1,119 @@
1
+ import { scope } from "./scope.js";
2
+ export const jsObjectsScope = scope({
3
+ Function: [
4
+ "node",
5
+ {
6
+ object: {
7
+ class: "Function"
8
+ }
9
+ }
10
+ ],
11
+ Array: [
12
+ "node",
13
+ {
14
+ object: {
15
+ class: "Array"
16
+ }
17
+ }
18
+ ],
19
+ Date: [
20
+ "node",
21
+ {
22
+ object: {
23
+ class: "Date"
24
+ }
25
+ }
26
+ ],
27
+ Error: [
28
+ "node",
29
+ {
30
+ object: {
31
+ class: "Error"
32
+ }
33
+ }
34
+ ],
35
+ Map: [
36
+ "node",
37
+ {
38
+ object: {
39
+ class: "Map"
40
+ }
41
+ }
42
+ ],
43
+ RegExp: [
44
+ "node",
45
+ {
46
+ object: {
47
+ class: "RegExp"
48
+ }
49
+ }
50
+ ],
51
+ Set: [
52
+ "node",
53
+ {
54
+ object: {
55
+ class: "Set"
56
+ }
57
+ }
58
+ ],
59
+ Object: [
60
+ "node",
61
+ {
62
+ object: {
63
+ class: "Object"
64
+ }
65
+ }
66
+ ],
67
+ String: [
68
+ "node",
69
+ {
70
+ object: {
71
+ class: "String"
72
+ }
73
+ }
74
+ ],
75
+ Number: [
76
+ "node",
77
+ {
78
+ object: {
79
+ class: "Number"
80
+ }
81
+ }
82
+ ],
83
+ Boolean: [
84
+ "node",
85
+ {
86
+ object: {
87
+ class: "Boolean"
88
+ }
89
+ }
90
+ ],
91
+ WeakMap: [
92
+ "node",
93
+ {
94
+ object: {
95
+ class: "WeakMap"
96
+ }
97
+ }
98
+ ],
99
+ WeakSet: [
100
+ "node",
101
+ {
102
+ object: {
103
+ class: "WeakSet"
104
+ }
105
+ }
106
+ ],
107
+ Promise: [
108
+ "node",
109
+ {
110
+ object: {
111
+ class: "Promise"
112
+ }
113
+ }
114
+ ]
115
+ }, {
116
+ name: "jsObjects",
117
+ standard: false
118
+ });
119
+ export const jsObjects = jsObjectsScope.compile();