arktype 2.2.10 → 3.0.0-beta.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.
Files changed (164) hide show
  1. package/README.md +181 -15
  2. package/out/attributes.d.ts +50 -14
  3. package/out/checkers.d.ts +172 -0
  4. package/out/chunk-2U2AWCI3.js +626 -0
  5. package/out/chunk-4CUA5XIP.js +7 -0
  6. package/out/chunk-5U2ZMDNI.js +1566 -0
  7. package/out/chunk-DLWCO4RV.js +18 -0
  8. package/out/chunk-E3DZXEZK.js +72 -0
  9. package/out/chunk-EVCML554.js +47 -0
  10. package/out/chunk-G45P2CLE.js +63 -0
  11. package/out/chunk-GPNK5353.js +2529 -0
  12. package/out/chunk-MLKGABMK.js +9 -0
  13. package/out/chunk-NRIGTMPM.js +7 -0
  14. package/out/chunk-PRTMQCHS.js +3 -0
  15. package/out/chunk-R5G57XC7.js +7 -0
  16. package/out/chunk-RAZ3TXOE.js +313 -0
  17. package/out/chunk-SWOOCAHN.js +4 -0
  18. package/out/chunk-TAHPKDNM.js +3 -0
  19. package/out/chunk-UETB6YE2.js +14 -0
  20. package/out/chunk-UK5UGFOX.js +254 -0
  21. package/out/chunk-VV6YDN2D.js +6118 -0
  22. package/out/codecs.d.ts +21 -0
  23. package/out/codecs.js +7 -0
  24. package/out/config.d.ts +8 -3
  25. package/out/config.js +2 -3
  26. package/out/declare.d.ts +17 -3
  27. package/out/deferred.d.ts +10 -0
  28. package/out/errors.d.ts +1 -0
  29. package/out/errors.js +2 -0
  30. package/out/fn.d.ts +1 -1
  31. package/out/generic.d.ts +3 -3
  32. package/out/i18n.d.ts +2 -0
  33. package/out/i18n.js +2 -0
  34. package/out/index.d.ts +5 -4
  35. package/out/index.js +8 -9
  36. package/out/internal.d.ts +70 -0
  37. package/out/internal.js +17 -0
  38. package/out/island.d.ts +2 -0
  39. package/out/island.js +6 -0
  40. package/out/islandDependency.d.ts +2 -0
  41. package/out/islandDependency.js +18 -0
  42. package/out/keywords/Array.d.ts +2 -2
  43. package/out/keywords/builtins.d.ts +5 -3
  44. package/out/keywords/constructors.d.ts +9 -1
  45. package/out/keywords/functions.d.ts +254 -0
  46. package/out/keywords/keywords.d.ts +8 -9
  47. package/out/keywords/number.d.ts +2 -8
  48. package/out/keywords/phrases.d.ts +245 -0
  49. package/out/keywords/string.d.ts +14 -113
  50. package/out/keywords/ts.d.ts +35 -33
  51. package/out/keywords/ts.js +4 -82
  52. package/out/locale.d.ts +19 -0
  53. package/out/locales/de.d.ts +2 -0
  54. package/out/locales/de.js +2 -0
  55. package/out/locales/en.d.ts +4 -0
  56. package/out/locales/en.js +5 -0
  57. package/out/match.d.ts +6 -6
  58. package/out/module.d.ts +1 -1
  59. package/out/mutatesInput.js +3 -0
  60. package/out/parser/ast/generic.d.ts +6 -3
  61. package/out/parser/ast/infer.d.ts +1 -1
  62. package/out/parser/ast/validate.d.ts +11 -6
  63. package/out/parser/definition.d.ts +68 -7
  64. package/out/parser/objectLiteral.d.ts +40 -12
  65. package/out/parser/property.d.ts +3 -5
  66. package/out/parser/reduce/dynamic.d.ts +16 -7
  67. package/out/parser/reduce/shared.d.ts +4 -0
  68. package/out/parser/reduce/static.d.ts +13 -13
  69. package/out/parser/shift/operand/date.d.ts +0 -5
  70. package/out/parser/shift/operand/operand.d.ts +2 -1
  71. package/out/parser/shift/operand/unenclosed.d.ts +9 -6
  72. package/out/parser/shift/operand/unresolvable.d.ts +68 -0
  73. package/out/parser/shift/operator/bounds.d.ts +9 -4
  74. package/out/parser/shift/operator/brand.d.ts +2 -2
  75. package/out/parser/shift/operator/default.d.ts +2 -2
  76. package/out/parser/shift/operator/divisor.d.ts +2 -2
  77. package/out/parser/shift/operator/operator.d.ts +1 -1
  78. package/out/parser/shift/tokens.d.ts +9 -2
  79. package/out/parser/string.d.ts +61 -7
  80. package/out/parser/tupleExpressions.d.ts +7 -3
  81. package/out/precompiled.d.ts +1 -0
  82. package/out/precompiled.js +2 -0
  83. package/out/runtime.d.ts +181 -0
  84. package/out/runtime.js +4 -0
  85. package/out/runtimeDevelopment.d.ts +1 -0
  86. package/out/runtimeDevelopment.js +133 -0
  87. package/out/runtimeExports.d.ts +1 -0
  88. package/out/scope.d.ts +40 -11
  89. package/out/standalone/artifact.d.ts +265 -0
  90. package/out/standalone/baking.d.ts +201 -0
  91. package/out/standalone/combinators.d.ts +13 -0
  92. package/out/standalone/compilers.d.ts +23 -0
  93. package/out/standalone/creators.d.ts +41 -0
  94. package/out/standalone/fallbacks.d.ts +48 -0
  95. package/out/standalone/keys.d.ts +31 -0
  96. package/out/standalone/modules.d.ts +26 -0
  97. package/out/standalone/sites.d.ts +52 -0
  98. package/out/standalone/units.d.ts +31 -0
  99. package/out/standalone/values.d.ts +60 -0
  100. package/out/standalone.d.ts +16 -0
  101. package/out/type.d.ts +45 -6
  102. package/out/variants/array.d.ts +1 -1
  103. package/out/variants/base.d.ts +47 -27
  104. package/out/variants/date.d.ts +1 -1
  105. package/out/variants/instantiate.d.ts +4 -2
  106. package/out/variants/number.d.ts +1 -1
  107. package/out/variants/object.d.ts +17 -12
  108. package/out/variants/string.d.ts +1 -1
  109. package/package.json +169 -8
  110. package/skills/arktype/SKILL.md +278 -43
  111. package/skills/arktype/references/migrating.md +1296 -0
  112. package/skills/arktype/references/precompile.md +191 -0
  113. package/out/attributes.js +0 -3
  114. package/out/fn.js +0 -58
  115. package/out/generic.js +0 -37
  116. package/out/keywords/Array.js +0 -21
  117. package/out/keywords/FormData.js +0 -40
  118. package/out/keywords/TypedArray.js +0 -16
  119. package/out/keywords/builtins.js +0 -11
  120. package/out/keywords/constructors.js +0 -16
  121. package/out/keywords/keywords.js +0 -48
  122. package/out/keywords/number.js +0 -49
  123. package/out/keywords/string.js +0 -467
  124. package/out/match.js +0 -95
  125. package/out/module.js +0 -3
  126. package/out/nary.js +0 -1
  127. package/out/parser/ast/bounds.js +0 -1
  128. package/out/parser/ast/default.js +0 -1
  129. package/out/parser/ast/divisor.js +0 -1
  130. package/out/parser/ast/generic.js +0 -1
  131. package/out/parser/ast/infer.js +0 -1
  132. package/out/parser/ast/keyof.js +0 -1
  133. package/out/parser/ast/utils.js +0 -1
  134. package/out/parser/ast/validate.js +0 -3
  135. package/out/parser/definition.js +0 -78
  136. package/out/parser/objectLiteral.js +0 -113
  137. package/out/parser/property.js +0 -16
  138. package/out/parser/reduce/dynamic.js +0 -153
  139. package/out/parser/reduce/shared.js +0 -25
  140. package/out/parser/reduce/static.js +0 -1
  141. package/out/parser/shift/operand/date.js +0 -23
  142. package/out/parser/shift/operand/enclosed.js +0 -82
  143. package/out/parser/shift/operand/genericArgs.js +0 -16
  144. package/out/parser/shift/operand/operand.js +0 -16
  145. package/out/parser/shift/operand/unenclosed.js +0 -59
  146. package/out/parser/shift/operator/bounds.js +0 -91
  147. package/out/parser/shift/operator/brand.js +0 -6
  148. package/out/parser/shift/operator/default.js +0 -29
  149. package/out/parser/shift/operator/divisor.js +0 -13
  150. package/out/parser/shift/operator/operator.js +0 -28
  151. package/out/parser/shift/tokens.js +0 -37
  152. package/out/parser/string.js +0 -42
  153. package/out/parser/tupleExpressions.js +0 -75
  154. package/out/parser/tupleLiteral.js +0 -128
  155. package/out/scope.js +0 -106
  156. package/out/type.js +0 -55
  157. package/out/variants/array.js +0 -1
  158. package/out/variants/base.js +0 -1
  159. package/out/variants/date.js +0 -1
  160. package/out/variants/instantiate.js +0 -1
  161. package/out/variants/number.js +0 -1
  162. package/out/variants/object.js +0 -1
  163. package/out/variants/string.js +0 -1
  164. /package/out/{declare.js → mutatesInput.d.ts} +0 -0
@@ -1,28 +0,0 @@
1
- import { isKeyOf, whitespaceChars } from "@ark/util";
2
- import { lookaheadIsFinalizing } from "../tokens.js";
3
- import { comparatorStartChars, parseBound } from "./bounds.js";
4
- import { parseBrand } from "./brand.js";
5
- import { parseDivisor } from "./divisor.js";
6
- export const parseOperator = (s) => {
7
- const lookahead = s.scanner.shift();
8
- return (lookahead === "" ? s.finalize("")
9
- : lookahead === "[" ?
10
- s.scanner.shift() === "]" ?
11
- s.setRoot(s.root.rawArray())
12
- : s.error(incompleteArrayTokenMessage)
13
- : lookahead === "|" ?
14
- s.scanner.lookahead === ">" ?
15
- s.shiftedBy(1).pushRootToBranch("|>")
16
- : s.pushRootToBranch(lookahead)
17
- : lookahead === "&" ? s.pushRootToBranch(lookahead)
18
- : lookahead === ")" ? s.finalizeGroup()
19
- : lookaheadIsFinalizing(lookahead, s.scanner.unscanned) ?
20
- s.finalize(lookahead)
21
- : isKeyOf(lookahead, comparatorStartChars) ? parseBound(s, lookahead)
22
- : lookahead === "%" ? parseDivisor(s)
23
- : lookahead === "#" ? parseBrand(s)
24
- : lookahead in whitespaceChars ? parseOperator(s)
25
- : s.error(writeUnexpectedCharacterMessage(lookahead)));
26
- };
27
- export const writeUnexpectedCharacterMessage = (char, shouldBe = "") => `'${char}' is not allowed here${shouldBe && ` (should be ${shouldBe})`}`;
28
- export const incompleteArrayTokenMessage = `Missing expected ']'`;
@@ -1,37 +0,0 @@
1
- import { isKeyOf, whitespaceChars } from "@ark/util";
2
- export const terminatingChars = {
3
- "<": 1,
4
- ">": 1,
5
- "=": 1,
6
- "|": 1,
7
- "&": 1,
8
- ")": 1,
9
- "[": 1,
10
- "%": 1,
11
- ",": 1,
12
- ":": 1,
13
- "?": 1,
14
- "#": 1,
15
- ...whitespaceChars
16
- };
17
- export const finalizingLookaheads = {
18
- ">": 1,
19
- ",": 1,
20
- "": 1,
21
- "=": 1,
22
- "?": 1
23
- };
24
- export const lookaheadIsFinalizing = (lookahead, unscanned) => lookahead === ">" ?
25
- unscanned[0] === "=" ?
26
- // >== would only occur in an expression like Array<number>==5
27
- // otherwise, >= would only occur as part of a bound like number>=5
28
- unscanned[1] === "="
29
- // if > is the end of a generic instantiation, the next token will be
30
- // an operator or the end of the string
31
- : unscanned.trimStart() === "" ||
32
- isKeyOf(unscanned.trimStart()[0], terminatingChars)
33
- // "=" is a finalizer on its own (representing a default value),
34
- // but not with a second "=" (an equality comparator)
35
- : lookahead === "=" ? unscanned[0] !== "="
36
- // "," and "?" are unambiguously finalizers
37
- : lookahead === "," || lookahead === "?";
@@ -1,42 +0,0 @@
1
- import { Scanner, throwInternalError, throwParseError } from "@ark/util";
2
- import { RuntimeState } from "./reduce/dynamic.js";
3
- import { parseDefault } from "./shift/operator/default.js";
4
- import { writeUnexpectedCharacterMessage } from "./shift/operator/operator.js";
5
- export const parseString = (def, ctx) => {
6
- const aliasResolution = ctx.$.maybeResolveRoot(def);
7
- if (aliasResolution)
8
- return aliasResolution;
9
- if (def.endsWith("[]")) {
10
- const possibleElementResolution = ctx.$.maybeResolveRoot(def.slice(0, -2));
11
- if (possibleElementResolution)
12
- return possibleElementResolution.rawArray();
13
- }
14
- const s = new RuntimeState(new Scanner(def), ctx);
15
- const node = fullStringParse(s);
16
- if (s.finalizer === ">")
17
- throwParseError(writeUnexpectedCharacterMessage(">"));
18
- return node;
19
- };
20
- export const fullStringParse = (s) => {
21
- s.parseOperand();
22
- let result = parseUntilFinalizer(s).root;
23
- if (!result) {
24
- return throwInternalError(`Root was unexpectedly unset after parsing string '${s.scanner.scanned}'`);
25
- }
26
- if (s.finalizer === "=")
27
- result = parseDefault(s);
28
- else if (s.finalizer === "?")
29
- result = [result, "?"];
30
- s.scanner.shiftUntilNonWhitespace();
31
- if (s.scanner.lookahead) {
32
- // throw a parse error if non-whitespace characters made it here without being parsed
33
- throwParseError(writeUnexpectedCharacterMessage(s.scanner.lookahead));
34
- }
35
- return result;
36
- };
37
- export const parseUntilFinalizer = (s) => {
38
- while (s.finalizer === undefined)
39
- next(s);
40
- return s;
41
- };
42
- const next = (s) => s.hasRoot() ? s.parseOperator() : s.parseOperand();
@@ -1,75 +0,0 @@
1
- import { Disjoint, intersectNodesRoot, pipeNodesRoot } from "@ark/schema";
2
- import { objectKindOrDomainOf, throwParseError } from "@ark/util";
3
- import { shallowDefaultableMessage, shallowOptionalMessage } from "./ast/validate.js";
4
- import { writeMissingRightOperandMessage } from "./shift/operand/unenclosed.js";
5
- export const maybeParseTupleExpression = (def, ctx) => isIndexZeroExpression(def) ? indexZeroParsers[def[0]](def, ctx)
6
- : isIndexOneExpression(def) ? indexOneParsers[def[1]](def, ctx)
7
- : null;
8
- export const parseKeyOfTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[1], ctx).keyof();
9
- const parseBranchTuple = (def, ctx) => {
10
- if (def[2] === undefined)
11
- return throwParseError(writeMissingRightOperandMessage(def[1], ""));
12
- const l = ctx.$.parseOwnDefinitionFormat(def[0], ctx);
13
- const r = ctx.$.parseOwnDefinitionFormat(def[2], ctx);
14
- if (def[1] === "|")
15
- return ctx.$.node("union", { branches: [l, r] });
16
- const result = def[1] === "&" ?
17
- intersectNodesRoot(l, r, ctx.$)
18
- : pipeNodesRoot(l, r, ctx.$);
19
- if (result instanceof Disjoint)
20
- return result.throw();
21
- return result;
22
- };
23
- const parseArrayTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).rawArray();
24
- export const parseMorphTuple = (def, ctx) => {
25
- if (typeof def[2] !== "function") {
26
- return throwParseError(writeMalformedFunctionalExpressionMessage("=>", def[2]));
27
- }
28
- return ctx.$.parseOwnDefinitionFormat(def[0], ctx).pipe(def[2]);
29
- };
30
- export const writeMalformedFunctionalExpressionMessage = (operator, value) => `${operator === ":" ? "Narrow" : "Morph"} expression requires a function following '${operator}' (was ${typeof value})`;
31
- export const parseNarrowTuple = (def, ctx) => {
32
- if (typeof def[2] !== "function") {
33
- return throwParseError(writeMalformedFunctionalExpressionMessage(":", def[2]));
34
- }
35
- return ctx.$.parseOwnDefinitionFormat(def[0], ctx).constrain("predicate", def[2]);
36
- };
37
- const parseMetaTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).configure(def[2], def[3]);
38
- const defineIndexOneParsers = (parsers) => parsers;
39
- const postfixParsers = defineIndexOneParsers({
40
- "[]": parseArrayTuple,
41
- "?": () => throwParseError(shallowOptionalMessage)
42
- });
43
- const infixParsers = defineIndexOneParsers({
44
- "|": parseBranchTuple,
45
- "&": parseBranchTuple,
46
- ":": parseNarrowTuple,
47
- "=>": parseMorphTuple,
48
- "|>": parseBranchTuple,
49
- "@": parseMetaTuple,
50
- // since object and tuple literals parse there via `parseProperty`,
51
- // they must be shallow if parsed directly as a tuple expression
52
- "=": () => throwParseError(shallowDefaultableMessage)
53
- });
54
- const indexOneParsers = { ...postfixParsers, ...infixParsers };
55
- const isIndexOneExpression = (def) => indexOneParsers[def[1]] !== undefined;
56
- const defineIndexZeroParsers = (parsers) => parsers;
57
- const indexZeroParsers = defineIndexZeroParsers({
58
- keyof: parseKeyOfTuple,
59
- instanceof: (def, ctx) => {
60
- if (typeof def[1] !== "function") {
61
- return throwParseError(writeInvalidConstructorMessage(objectKindOrDomainOf(def[1])));
62
- }
63
- const branches = def
64
- .slice(1)
65
- .map(ctor => typeof ctor === "function" ?
66
- ctx.$.node("proto", { proto: ctor })
67
- : throwParseError(writeInvalidConstructorMessage(objectKindOrDomainOf(ctor))));
68
- return branches.length === 1 ?
69
- branches[0]
70
- : ctx.$.node("union", { branches });
71
- },
72
- "===": (def, ctx) => ctx.$.units(def.slice(1))
73
- });
74
- const isIndexZeroExpression = (def) => indexZeroParsers[def[0]] !== undefined;
75
- export const writeInvalidConstructorMessage = (actual) => `Expected a constructor following 'instanceof' operator (was ${actual})`;
@@ -1,128 +0,0 @@
1
- import { $ark, makeRootAndArrayPropertiesMutable, postfixAfterOptionalOrDefaultableMessage } from "@ark/schema";
2
- import { append, isArray, isEmptyObject, throwParseError } from "@ark/util";
3
- import { parseProperty } from "./property.js";
4
- export const parseTupleLiteral = (def, ctx) => {
5
- let sequences = [{}];
6
- let i = 0;
7
- while (i < def.length) {
8
- let spread = false;
9
- if (def[i] === "..." && i < def.length - 1) {
10
- spread = true;
11
- i++;
12
- }
13
- const parsedProperty = parseProperty(def[i], ctx);
14
- const [valueNode, operator, possibleDefaultValue] = !isArray(parsedProperty) ? [parsedProperty] : parsedProperty;
15
- i++;
16
- if (spread) {
17
- if (!valueNode.extends($ark.intrinsic.Array))
18
- return throwParseError(writeNonArraySpreadMessage(valueNode.expression));
19
- // a spread must be distributed over branches e.g.:
20
- // def: [string, ...(number[] | [true, false])]
21
- // nodes: [string, ...number[]] | [string, true, false]
22
- sequences = sequences.flatMap(base =>
23
- // since appendElement mutates base, we have to shallow-ish clone it for each branch
24
- valueNode.distribute(branch => appendSpreadBranch(makeRootAndArrayPropertiesMutable(base), branch)));
25
- }
26
- else {
27
- sequences = sequences.map(base => {
28
- if (operator === "?")
29
- return appendOptionalElement(base, valueNode);
30
- if (operator === "=")
31
- return appendDefaultableElement(base, valueNode, possibleDefaultValue);
32
- return appendRequiredElement(base, valueNode);
33
- });
34
- }
35
- }
36
- return ctx.$.parseSchema(sequences.map(sequence => isEmptyObject(sequence) ?
37
- {
38
- proto: Array,
39
- exactLength: 0
40
- }
41
- : {
42
- proto: Array,
43
- sequence
44
- }));
45
- };
46
- const appendRequiredElement = (base, element) => {
47
- if (base.defaultables || base.optionals) {
48
- return throwParseError(base.variadic ?
49
- // e.g. [boolean = true, ...string[], number]
50
- postfixAfterOptionalOrDefaultableMessage
51
- // e.g. [string?, number]
52
- : requiredPostOptionalMessage);
53
- }
54
- if (base.variadic) {
55
- // e.g. [...string[], number]
56
- base.postfix = append(base.postfix, element);
57
- }
58
- else {
59
- // e.g. [string, number]
60
- base.prefix = append(base.prefix, element);
61
- }
62
- return base;
63
- };
64
- const appendOptionalElement = (base, element) => {
65
- if (base.variadic)
66
- // e.g. [...string[], number?]
67
- return throwParseError(optionalOrDefaultableAfterVariadicMessage);
68
- // e.g. [string, number?]
69
- base.optionals = append(base.optionals, element);
70
- return base;
71
- };
72
- const appendDefaultableElement = (base, element, value) => {
73
- if (base.variadic)
74
- // e.g. [...string[], number = 0]
75
- return throwParseError(optionalOrDefaultableAfterVariadicMessage);
76
- if (base.optionals)
77
- // e.g. [string?, number = 0]
78
- return throwParseError(defaultablePostOptionalMessage);
79
- // value's assignability to element will be checked when the
80
- // sequence is instantiated by @ark/schema
81
- // e.g. [string, number = 0]
82
- base.defaultables = append(base.defaultables, [[element, value]]);
83
- return base;
84
- };
85
- const appendVariadicElement = (base, element) => {
86
- // e.g. [...string[], number, ...string[]]
87
- if (base.postfix)
88
- throwParseError(multipleVariadicMesage);
89
- if (base.variadic) {
90
- if (!base.variadic.equals(element)) {
91
- // e.g. [...string[], ...number[]]
92
- throwParseError(multipleVariadicMesage);
93
- }
94
- // e.g. [...string[], ...string[]]
95
- // do nothing, second spread doesn't change the type
96
- }
97
- else {
98
- // e.g. [string, ...number[]]
99
- base.variadic = element.internal;
100
- }
101
- return base;
102
- };
103
- const appendSpreadBranch = (base, branch) => {
104
- const spread = branch.select({ method: "find", kind: "sequence" });
105
- if (!spread) {
106
- // the only array with no sequence reference is unknown[]
107
- return appendVariadicElement(base, $ark.intrinsic.unknown);
108
- }
109
- if (spread.prefix)
110
- for (const node of spread.prefix)
111
- appendRequiredElement(base, node);
112
- if (spread.optionals)
113
- for (const node of spread.optionals)
114
- appendOptionalElement(base, node);
115
- if (spread.variadic)
116
- appendVariadicElement(base, spread.variadic);
117
- if (spread.postfix)
118
- for (const node of spread.postfix)
119
- appendRequiredElement(base, node);
120
- return base;
121
- };
122
- export const writeNonArraySpreadMessage = (operand) => `Spread element must be an array (was ${operand})`;
123
- export const multipleVariadicMesage = "A tuple may have at most one variadic element";
124
- export const requiredPostOptionalMessage = "A required element may not follow an optional element";
125
- export const optionalOrDefaultableAfterVariadicMessage = "An optional element may not follow a variadic element";
126
- export const spreadOptionalMessage = "A spread element cannot be optional";
127
- export const spreadDefaultableMessage = "A spread element cannot have a default";
128
- export const defaultablePostOptionalMessage = "A defaultable element may not follow an optional element without a default";
package/out/scope.js DELETED
@@ -1,106 +0,0 @@
1
- import { $ark, BaseScope, hasArkKind, parseGeneric } from "@ark/schema";
2
- import { Scanner, enumValues, flatMorph, isArray, isThunk, throwParseError } from "@ark/util";
3
- import { InternalFnParser } from "./fn.js";
4
- import { parseGenericParamName } from "./generic.js";
5
- import { InternalMatchParser } from "./match.js";
6
- import { shallowDefaultableMessage, shallowOptionalMessage } from "./parser/ast/validate.js";
7
- import { parseInnerDefinition } from "./parser/definition.js";
8
- import { InternalTypeParser } from "./type.js";
9
- export const $arkTypeRegistry = $ark;
10
- export class InternalScope extends BaseScope {
11
- get ambientAttachments() {
12
- if (!$arkTypeRegistry.typeAttachments)
13
- return;
14
- return this.cacheGetter("ambientAttachments", flatMorph($arkTypeRegistry.typeAttachments, (k, v) => [
15
- k,
16
- this.bindReference(v)
17
- ]));
18
- }
19
- preparseOwnAliasEntry(alias, def) {
20
- const firstParamIndex = alias.indexOf("<");
21
- if (firstParamIndex === -1) {
22
- if (hasArkKind(def, "module") || hasArkKind(def, "generic"))
23
- return [alias, def];
24
- const qualifiedName = this.name === "ark" ? alias
25
- : alias === "root" ? this.name
26
- : `${this.name}.${alias}`;
27
- const config = this.resolvedConfig.keywords?.[qualifiedName];
28
- if (config)
29
- def = [def, "@", config];
30
- return [alias, def];
31
- }
32
- if (alias[alias.length - 1] !== ">") {
33
- throwParseError(`'>' must be the last character of a generic declaration in a scope`);
34
- }
35
- const name = alias.slice(0, firstParamIndex);
36
- const paramString = alias.slice(firstParamIndex + 1, -1);
37
- return [
38
- name,
39
- // use a thunk definition for the generic so that we can parse
40
- // constraints within the current scope
41
- () => {
42
- const params = this.parseGenericParams(paramString, { alias: name });
43
- const generic = parseGeneric(params, def, this);
44
- return generic;
45
- }
46
- ];
47
- }
48
- parseGenericParams(def, opts) {
49
- return parseGenericParamName(new Scanner(def), [], this.createParseContext({
50
- ...opts,
51
- def,
52
- prefix: "generic"
53
- }));
54
- }
55
- normalizeRootScopeValue(resolution) {
56
- if (isThunk(resolution) && !hasArkKind(resolution, "generic"))
57
- return resolution();
58
- return resolution;
59
- }
60
- preparseOwnDefinitionFormat(def, opts) {
61
- return {
62
- ...opts,
63
- def,
64
- prefix: opts.alias ?? "type"
65
- };
66
- }
67
- parseOwnDefinitionFormat(def, ctx) {
68
- const isScopeAlias = ctx.alias && ctx.alias in this.aliases;
69
- // if the definition being parsed is not a scope alias and is not a
70
- // generic instantiation (i.e. opts don't include args), add `this` as a resolution.
71
- // if we're parsing a nested string, ctx.args will have already been set
72
- if (!isScopeAlias && !ctx.args)
73
- ctx.args = { this: ctx.id };
74
- const result = parseInnerDefinition(def, ctx);
75
- if (isArray(result)) {
76
- if (result[1] === "=")
77
- return throwParseError(shallowDefaultableMessage);
78
- if (result[1] === "?")
79
- return throwParseError(shallowOptionalMessage);
80
- }
81
- return result;
82
- }
83
- unit = value => this.units([value]);
84
- valueOf = tsEnum => this.units(enumValues(tsEnum));
85
- enumerated = (...values) => this.units(values);
86
- instanceOf = ctor => this.node("proto", { proto: ctor }, { prereduced: true });
87
- or = (...defs) => this.schema(defs.map(def => this.parse(def)));
88
- and = (...defs) => defs.reduce((node, def) => node.and(this.parse(def)), this.intrinsic.unknown);
89
- merge = (...defs) => defs.reduce((node, def) => node.merge(this.parse(def)), this.intrinsic.object);
90
- pipe = (...morphs) => this.intrinsic.unknown.pipe(...morphs);
91
- fn = new InternalFnParser(this);
92
- match = new InternalMatchParser(this);
93
- declare = () => ({
94
- type: this.type
95
- });
96
- define(def) {
97
- return def;
98
- }
99
- type = new InternalTypeParser(this);
100
- static scope = ((def, config = {}) => new InternalScope(def, config));
101
- static module = ((def, config = {}) => InternalScope.scope(def, config).export());
102
- }
103
- export const scope = Object.assign(InternalScope.scope, {
104
- define: (def) => def
105
- });
106
- export const Scope = InternalScope;
package/out/type.js DELETED
@@ -1,55 +0,0 @@
1
- import { ArkErrors, BaseRoot, GenericRoot } from "@ark/schema";
2
- import { Callable, Hkt } from "@ark/util";
3
- export class InternalTypeParser extends Callable {
4
- constructor($) {
5
- const attach = Object.assign({
6
- errors: ArkErrors,
7
- hkt: Hkt,
8
- $: $,
9
- raw: $.parse,
10
- module: $.constructor.module,
11
- scope: $.constructor.scope,
12
- declare: $.declare,
13
- define: $.define,
14
- match: $.match,
15
- generic: $.generic,
16
- schema: $.schema,
17
- // this won't be defined during bootstrapping, but externally always will be
18
- keywords: $.ambient,
19
- unit: $.unit,
20
- enumerated: $.enumerated,
21
- instanceOf: $.instanceOf,
22
- valueOf: $.valueOf,
23
- or: $.or,
24
- and: $.and,
25
- merge: $.merge,
26
- pipe: $.pipe,
27
- fn: $.fn
28
- },
29
- // also won't be defined during bootstrapping
30
- $.ambientAttachments);
31
- super((...args) => {
32
- if (args.length === 1) {
33
- // treat as a simple definition
34
- return $.parse(args[0]);
35
- }
36
- if (args.length === 2 &&
37
- typeof args[0] === "string" &&
38
- args[0][0] === "<" &&
39
- args[0][args[0].length - 1] === ">") {
40
- // if there are exactly two args, the first of which looks like <${string}>,
41
- // treat as a generic
42
- const paramString = args[0].slice(1, -1);
43
- const params = $.parseGenericParams(paramString, {});
44
- return new GenericRoot(params, args[1], $, $, null);
45
- }
46
- // otherwise, treat as a tuple expression. technically, this also allows
47
- // non-expression tuple definitions to be parsed, but it's not a supported
48
- // part of the API as specified by the associated types
49
- return $.parse(args);
50
- }, {
51
- attach
52
- });
53
- }
54
- }
55
- export const Type = BaseRoot;
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
File without changes