arktype 2.0.0-dev.12-cjs → 2.0.0-dev.13

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/out/api.js CHANGED
@@ -1,19 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Type = exports.scope = exports.Module = exports.type = exports.match = exports.define = exports.declare = exports.ark = exports.ambient = exports.ArkErrors = exports.ArkError = void 0;
4
- var schema_1 = require("@arktype/schema");
5
- Object.defineProperty(exports, "ArkError", { enumerable: true, get: function () { return schema_1.ArkError; } });
6
- Object.defineProperty(exports, "ArkErrors", { enumerable: true, get: function () { return schema_1.ArkErrors; } });
7
- var ark_js_1 = require("./ark.js");
8
- Object.defineProperty(exports, "ambient", { enumerable: true, get: function () { return ark_js_1.ambient; } });
9
- Object.defineProperty(exports, "ark", { enumerable: true, get: function () { return ark_js_1.ark; } });
10
- Object.defineProperty(exports, "declare", { enumerable: true, get: function () { return ark_js_1.declare; } });
11
- Object.defineProperty(exports, "define", { enumerable: true, get: function () { return ark_js_1.define; } });
12
- Object.defineProperty(exports, "match", { enumerable: true, get: function () { return ark_js_1.match; } });
13
- Object.defineProperty(exports, "type", { enumerable: true, get: function () { return ark_js_1.type; } });
14
- var module_js_1 = require("./module.js");
15
- Object.defineProperty(exports, "Module", { enumerable: true, get: function () { return module_js_1.Module; } });
16
- var scope_js_1 = require("./scope.js");
17
- Object.defineProperty(exports, "scope", { enumerable: true, get: function () { return scope_js_1.scope; } });
18
- var type_js_1 = require("./type.js");
19
- Object.defineProperty(exports, "Type", { enumerable: true, get: function () { return type_js_1.Type; } });
1
+ export { ArkError as ArkError, ArkErrors } from "@arktype/schema";
2
+ export { ambient, ark, declare, define, match, type } from "./ark.js";
3
+ export { Module } from "./module.js";
4
+ export { scope } from "./scope.js";
5
+ export { Type } from "./type.js";
package/out/ark.js CHANGED
@@ -1,13 +1,10 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.declare = exports.define = exports.match = exports.type = exports.ark = exports.ambient = exports.tsGenerics = void 0;
4
- const schema_1 = require("@arktype/schema");
5
- const scope_js_1 = require("./scope.js");
6
- exports.tsGenerics = {};
7
- exports.ambient = (0, scope_js_1.scope)(schema_1.keywordNodes);
8
- scope_js_1.RawScope.ambient = exports.ambient.raw;
9
- exports.ark = exports.ambient.export();
10
- exports.type = exports.ambient.type;
11
- exports.match = exports.ambient.match;
12
- exports.define = exports.ambient.define;
13
- exports.declare = exports.ambient.declare;
1
+ import { keywordNodes } from "@arktype/schema";
2
+ import { RawScope, scope } from "./scope.js";
3
+ export const tsGenerics = {};
4
+ export const ambient = scope(keywordNodes);
5
+ RawScope.ambient = ambient.raw;
6
+ export const ark = ambient.export();
7
+ export const type = ambient.type;
8
+ export const match = ambient.match;
9
+ export const define = ambient.define;
10
+ export const declare = ambient.declare;
package/out/config.js CHANGED
@@ -1,18 +1,2 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
1
  // eslint-disable-next-line @typescript-eslint/no-restricted-imports
18
- __exportStar(require("@arktype/schema/config"), exports);
2
+ export * from "@arktype/schema/config";
package/out/generic.js CHANGED
@@ -1,13 +1,10 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Generic = void 0;
4
- const schema_1 = require("@arktype/schema");
5
- const util_1 = require("@arktype/util");
6
- class Generic extends util_1.Callable {
1
+ import { arkKind } from "@arktype/schema";
2
+ import { Callable } from "@arktype/util";
3
+ export class Generic extends Callable {
7
4
  params;
8
5
  def;
9
6
  $;
10
- [schema_1.arkKind] = "generic";
7
+ [arkKind] = "generic";
11
8
  constructor(params, def,
12
9
  // TODO: should be Scope<$>, but breaks inference
13
10
  $) {
@@ -25,4 +22,3 @@ class Generic extends util_1.Callable {
25
22
  this.$ = $;
26
23
  }
27
24
  }
28
- exports.Generic = Generic;
package/out/match.js CHANGED
@@ -1,7 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createMatchParser = void 0;
4
- const createMatchParser = ($) => {
1
+ export const createMatchParser = ($) => {
5
2
  return (() => { }).bind($);
6
3
  // const matchParser = (isRestricted: boolean) => {
7
4
  // const handledCases: { when: RawRoot; then: Morph }[] = []
@@ -16,17 +13,17 @@ const createMatchParser = ($) => {
16
13
  // const branches = handledCases.flatMap(({ when, then }) => {
17
14
  // if (when.kind === "union") {
18
15
  // return when.branches.map((branch) => ({
19
- // from: branch,
16
+ // in: branch,
20
17
  // morph: then
21
18
  // }))
22
19
  // }
23
20
  // if (when.kind === "morph") {
24
- // return [{ from: when, morph: [when.morph, then] }]
21
+ // return [{ in: when, morph: [when.morph, then] }]
25
22
  // }
26
- // return [{ from: when, morph: then }]
23
+ // return [{ in: when, morph: then }]
27
24
  // })
28
25
  // if (defaultCase) {
29
- // branches.push({ from: keywordNodes.unknown, morph: defaultCase })
26
+ // branches.push({ in: keywordNodes.unknown, morph: defaultCase })
30
27
  // }
31
28
  // const matchers = $.node("union", {
32
29
  // branches,
@@ -53,4 +50,3 @@ const createMatchParser = ($) => {
53
50
  // only: () => matchParser(true)
54
51
  // }) as never
55
52
  };
56
- exports.createMatchParser = createMatchParser;
package/out/module.js CHANGED
@@ -1,5 +1,2 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Module = void 0;
4
- const schema_1 = require("@arktype/schema");
5
- exports.Module = schema_1.RootModule;
1
+ import { RootModule } from "@arktype/schema";
2
+ export const Module = RootModule;
@@ -1,34 +1,29 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.writeBadDefinitionTypeMessage = exports.parseObject = void 0;
4
- const schema_1 = require("@arktype/schema");
5
- const util_1 = require("@arktype/util");
6
- const objectLiteral_js_1 = require("./objectLiteral.js");
7
- const tuple_js_1 = require("./tuple.js");
8
- const parseObject = (def, ctx) => {
9
- const objectKind = (0, util_1.objectKindOf)(def);
1
+ import { hasArkKind } from "@arktype/schema";
2
+ import { isThunk, objectKindOf, printable, throwParseError } from "@arktype/util";
3
+ import { parseObjectLiteral } from "./objectLiteral.js";
4
+ import { parseTuple } from "./tuple.js";
5
+ export const parseObject = (def, ctx) => {
6
+ const objectKind = objectKindOf(def);
10
7
  switch (objectKind) {
11
8
  case undefined:
12
- if ((0, schema_1.hasArkKind)(def, "root"))
9
+ if (hasArkKind(def, "root"))
13
10
  return def;
14
- return (0, objectLiteral_js_1.parseObjectLiteral)(def, ctx);
11
+ return parseObjectLiteral(def, ctx);
15
12
  case "Array":
16
- return (0, tuple_js_1.parseTuple)(def, ctx);
13
+ return parseTuple(def, ctx);
17
14
  case "RegExp":
18
15
  return ctx.$.node("intersection", {
19
16
  domain: "string",
20
17
  regex: def
21
18
  }, { prereduced: true });
22
19
  case "Function": {
23
- const resolvedDef = (0, util_1.isThunk)(def) ? def() : def;
24
- if ((0, schema_1.hasArkKind)(resolvedDef, "root"))
20
+ const resolvedDef = isThunk(def) ? def() : def;
21
+ if (hasArkKind(resolvedDef, "root"))
25
22
  return resolvedDef;
26
- return (0, util_1.throwParseError)((0, exports.writeBadDefinitionTypeMessage)("Function"));
23
+ return throwParseError(writeBadDefinitionTypeMessage("Function"));
27
24
  }
28
25
  default:
29
- return (0, util_1.throwParseError)((0, exports.writeBadDefinitionTypeMessage)(objectKind ?? (0, util_1.printable)(def)));
26
+ return throwParseError(writeBadDefinitionTypeMessage(objectKind ?? printable(def)));
30
27
  }
31
28
  };
32
- exports.parseObject = parseObject;
33
- const writeBadDefinitionTypeMessage = (actual) => `Type definitions must be strings or objects (was ${actual})`;
34
- exports.writeBadDefinitionTypeMessage = writeBadDefinitionTypeMessage;
29
+ export const writeBadDefinitionTypeMessage = (actual) => `Type definitions must be strings or objects (was ${actual})`;
@@ -1,19 +1,15 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.emptyGenericParameterMessage = exports.parseGenericParams = void 0;
4
- const util_1 = require("@arktype/util");
5
- const operator_js_1 = require("./string/shift/operator/operator.js");
6
- const scanner_js_1 = require("./string/shift/scanner.js");
7
- const parseGenericParams = (def) => _parseGenericParams(new scanner_js_1.Scanner(def));
8
- exports.parseGenericParams = parseGenericParams;
9
- exports.emptyGenericParameterMessage = "An empty string is not a valid generic parameter name";
1
+ import { throwParseError } from "@arktype/util";
2
+ import { writeUnexpectedCharacterMessage } from "./string/shift/operator/operator.js";
3
+ import { Scanner } from "./string/shift/scanner.js";
4
+ export const parseGenericParams = (def) => _parseGenericParams(new Scanner(def));
5
+ export const emptyGenericParameterMessage = "An empty string is not a valid generic parameter name";
10
6
  const _parseGenericParams = (scanner) => {
11
7
  const param = scanner.shiftUntilNextTerminator();
12
8
  if (param === "")
13
- (0, util_1.throwParseError)(exports.emptyGenericParameterMessage);
9
+ throwParseError(emptyGenericParameterMessage);
14
10
  scanner.shiftUntilNonWhitespace();
15
11
  const nextNonWhitespace = scanner.shift();
16
12
  return (nextNonWhitespace === "" ? [param]
17
13
  : nextNonWhitespace === "," ? [param, ..._parseGenericParams(scanner)]
18
- : (0, util_1.throwParseError)((0, operator_js_1.writeUnexpectedCharacterMessage)(nextNonWhitespace, ",")));
14
+ : throwParseError(writeUnexpectedCharacterMessage(nextNonWhitespace, ",")));
19
15
  };
@@ -1,58 +1,71 @@
1
- import type { BaseRoot, writeInvalidPropertyKeyMessage } from "@arktype/schema";
2
- import { type Dict, type ErrorMessage, type Key, type keyError, type merge, type show } from "@arktype/util";
1
+ import { type BaseRoot, type Default, type UndeclaredKeyBehavior, type writeInvalidPropertyKeyMessage } from "@arktype/schema";
2
+ import { type Dict, type ErrorMessage, type Key, type anyOrNever, type keyError, type merge, type show } from "@arktype/util";
3
3
  import type { ParseContext } from "../scope.js";
4
4
  import type { inferDefinition, validateDefinition } from "./definition.js";
5
5
  import type { astToString } from "./semantic/utils.js";
6
6
  import type { validateString } from "./semantic/validate.js";
7
7
  import { Scanner } from "./string/shift/scanner.js";
8
8
  export declare const parseObjectLiteral: (def: Dict, ctx: ParseContext) => BaseRoot;
9
+ export declare const writeInvalidUndeclaredBehaviorMessage: (actual: unknown) => string;
9
10
  export declare const nonLeadingSpreadError = "Spread operator may only be used as the first key in an object";
10
11
  export type inferObjectLiteral<def extends object, $, args> = show<"..." extends keyof def ? merge<inferDefinition<def["..."], $, args>, _inferObjectLiteral<def, $, args>> : _inferObjectLiteral<def, $, args>>;
11
12
  /**
12
13
  * Infers the contents of an object literal, ignoring a spread definition
13
14
  */
14
15
  type _inferObjectLiteral<def extends object, $, args> = {
15
- -readonly [k in keyof def as nonOptionalKeyFrom<k, $, args>]: inferDefinition<def[k], $, args>;
16
+ -readonly [k in keyof def as nonOptionalKeyFrom<k, $, args>]: def[k] extends (readonly [infer defaultableDef, "=", infer v]) ? def[k] extends anyOrNever ? def[k] : (In?: inferDefinition<defaultableDef, $, args>) => Default<v> : inferDefinition<def[k], $, args>;
16
17
  } & {
17
18
  -readonly [k in keyof def as optionalKeyFrom<k>]?: inferDefinition<def[k], $, args>;
18
19
  };
19
20
  export type validateObjectLiteral<def, $, args> = {
20
- [k in keyof def]: k extends IndexKey<infer indexDef> ? validateString<indexDef, $, args> extends ErrorMessage<infer message> ? keyError<message> : inferDefinition<indexDef, $, args> extends PropertyKey ? validateDefinition<def[k], $, args> : keyError<writeInvalidPropertyKeyMessage<indexDef>> : k extends "..." ? inferDefinition<def[k], $, args> extends object ? validateDefinition<def[k], $, args> : keyError<writeInvalidSpreadTypeMessage<astToString<def[k]>>> : validateDefinition<def[k], $, args>;
21
+ [k in keyof def]: k extends IndexKey<infer indexDef> ? validateString<indexDef, $, args> extends ErrorMessage<infer message> ? keyError<message> : inferDefinition<indexDef, $, args> extends PropertyKey ? validateDefinition<def[k], $, args> : keyError<writeInvalidPropertyKeyMessage<indexDef>> : k extends "..." ? inferDefinition<def[k], $, args> extends object ? validateDefinition<def[k], $, args> : keyError<writeInvalidSpreadTypeMessage<astToString<def[k]>>> : k extends "+" ? UndeclaredKeyBehavior : validatePossibleDefaultValue<def, k, $, args>;
21
22
  };
23
+ type validatePossibleDefaultValue<def, k extends keyof def, $, args> = def[k] extends readonly [infer defaultDef, "=", unknown] ? parseKey<k>["kind"] extends "required" ? readonly [
24
+ validateDefinition<defaultDef, $, args>,
25
+ "=",
26
+ inferDefinition<defaultDef, $, args>
27
+ ] : ErrorMessage<invalidDefaultKeyKindMessage> : validateDefinition<def[k], $, args>;
22
28
  type nonOptionalKeyFrom<k, $, args> = parseKey<k> extends PreparsedKey<"required", infer inner> ? inner : parseKey<k> extends PreparsedKey<"index", infer inner> ? inferDefinition<inner, $, args> & Key : never;
23
29
  type optionalKeyFrom<k> = parseKey<k> extends PreparsedKey<"optional", infer inner> ? inner : never;
24
30
  type PreparsedKey<kind extends ParsedKeyKind = ParsedKeyKind, inner extends Key = Key> = {
25
31
  kind: kind;
26
- inner: inner;
32
+ key: inner;
27
33
  };
28
34
  declare namespace PreparsedKey {
29
35
  type from<t extends PreparsedKey> = t;
30
36
  }
31
- type ParsedKeyKind = "required" | "optional" | "index" | "spread";
37
+ type ParsedKeyKind = "required" | "optional" | "index" | MetaKey;
38
+ export type MetaKey = "..." | "+";
32
39
  export type IndexKey<def extends string = string> = `[${def}]`;
33
- type PreparsedEntry = PreparsedKey & {
40
+ interface PreparsedEntry extends PreparsedKey {
34
41
  value: unknown;
35
- };
36
- export declare const parseEntry: (entry: readonly [Key, unknown]) => PreparsedEntry;
42
+ default: unknown;
43
+ }
44
+ export declare const parseEntry: ([key, value]: readonly [
45
+ Key,
46
+ unknown
47
+ ]) => PreparsedEntry;
48
+ export declare const invalidDefaultKeyKindMessage = "Only required keys may specify default values, e.g. { ark: ['string', '=', '\u26F5'] }";
49
+ export type invalidDefaultKeyKindMessage = typeof invalidDefaultKeyKindMessage;
37
50
  declare const parseKey: (key: Key) => PreparsedKey;
38
51
  type parseKey<k> = k extends `${infer inner}?` ? inner extends `${infer baseName}${Scanner.EscapeToken}` ? PreparsedKey.from<{
39
52
  kind: "required";
40
- inner: `${baseName}?`;
53
+ key: `${baseName}?`;
41
54
  }> : PreparsedKey.from<{
42
55
  kind: "optional";
43
- inner: inner;
44
- }> : k extends "..." ? PreparsedKey.from<{
45
- kind: "spread";
46
- inner: "...";
47
- }> : k extends `${Scanner.EscapeToken}...` ? PreparsedKey.from<{
56
+ key: inner;
57
+ }> : k extends MetaKey ? PreparsedKey.from<{
58
+ kind: k;
59
+ key: k;
60
+ }> : k extends `${Scanner.EscapeToken}${infer escapedMeta extends MetaKey}` ? PreparsedKey.from<{
48
61
  kind: "required";
49
- inner: "...";
62
+ key: escapedMeta;
50
63
  }> : k extends IndexKey<infer def> ? PreparsedKey.from<{
51
64
  kind: "index";
52
- inner: def;
65
+ key: def;
53
66
  }> : PreparsedKey.from<{
54
67
  kind: "required";
55
- inner: k extends (`${Scanner.EscapeToken}${infer escapedIndexKey extends IndexKey}`) ? escapedIndexKey : k extends Key ? k : `${k & number}`;
68
+ key: k extends (`${Scanner.EscapeToken}${infer escapedIndexKey extends IndexKey}`) ? escapedIndexKey : k extends Key ? k : `${k & number}`;
56
69
  }>;
57
70
  export declare const writeInvalidSpreadTypeMessage: <def extends string>(def: def) => `Spread operand must resolve to an object literal type (was ${def})`;
58
71
  type writeInvalidSpreadTypeMessage<def extends string> = `Spread operand must resolve to an object literal type (was ${def})`;
@@ -1,53 +1,59 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.writeInvalidSpreadTypeMessage = exports.parseEntry = exports.nonLeadingSpreadError = exports.parseObjectLiteral = void 0;
4
- const util_1 = require("@arktype/util");
5
- const scanner_js_1 = require("./string/shift/scanner.js");
6
- const parseObjectLiteral = (def, ctx) => {
1
+ import { ArkErrors, normalizeIndex } from "@arktype/schema";
2
+ import { append, isArray, printable, stringAndSymbolicEntriesOf, throwParseError, unset } from "@arktype/util";
3
+ import { Scanner } from "./string/shift/scanner.js";
4
+ export const parseObjectLiteral = (def, ctx) => {
7
5
  let spread;
8
6
  const structure = {};
9
7
  // We only allow a spread operator to be used as the first key in an object
10
8
  // because to match JS behavior any keys before the spread are overwritten
11
9
  // by the values in the target object, so there'd be no useful purpose in having it
12
10
  // anywhere except for the beginning.
13
- const parsedEntries = (0, util_1.stringAndSymbolicEntriesOf)(def).map(exports.parseEntry);
14
- if (parsedEntries[0]?.kind === "spread") {
11
+ const parsedEntries = stringAndSymbolicEntriesOf(def).map(parseEntry);
12
+ if (parsedEntries[0]?.kind === "...") {
15
13
  // remove the spread entry so we can iterate over the remaining entries
16
14
  // expecting non-spread entries
17
15
  const spreadEntry = parsedEntries.shift();
18
16
  const spreadNode = ctx.$.parse(spreadEntry.value, ctx);
19
17
  if (!spreadNode.hasKind("intersection") || !spreadNode.structure) {
20
- return (0, util_1.throwParseError)((0, exports.writeInvalidSpreadTypeMessage)(typeof spreadEntry.value === "string" ?
18
+ return throwParseError(writeInvalidSpreadTypeMessage(typeof spreadEntry.value === "string" ?
21
19
  spreadEntry.value
22
- : (0, util_1.printable)(spreadEntry.value)));
20
+ : printable(spreadEntry.value)));
23
21
  }
24
22
  spread = spreadNode.structure;
25
23
  }
26
24
  for (const entry of parsedEntries) {
27
- if (entry.kind === "spread")
28
- return (0, util_1.throwParseError)(exports.nonLeadingSpreadError);
25
+ if (entry.kind === "...")
26
+ return throwParseError(nonLeadingSpreadError);
27
+ if (entry.kind === "+") {
28
+ if (entry.value !== "reject" &&
29
+ entry.value !== "delete" &&
30
+ entry.value !== "ignore")
31
+ throwParseError(writeInvalidUndeclaredBehaviorMessage(entry.value));
32
+ structure.undeclared = entry.value;
33
+ continue;
34
+ }
29
35
  if (entry.kind === "index") {
30
36
  // handle key parsing first to match type behavior
31
- const key = ctx.$.parse(entry.inner, ctx);
37
+ const key = ctx.$.parse(entry.key, ctx);
32
38
  const value = ctx.$.parse(entry.value, ctx);
33
- // extract enumerable named props from the index signature
34
- const [enumerable, nonEnumerable] = (0, util_1.spliterate)(key.branches, (k) => k.hasKind("unit"));
35
- if (enumerable.length) {
36
- structure.required = (0, util_1.append)(structure.required, enumerable.map(k => ctx.$.node("required", { key: k.unit, value })));
37
- if (nonEnumerable.length) {
38
- structure.index = (0, util_1.append)(structure.index, ctx.$.node("index", { index: nonEnumerable, value }));
39
- }
40
- }
41
- else {
42
- structure.index = (0, util_1.append)(structure.index, ctx.$.node("index", { index: key, value }));
39
+ const normalizedSignature = normalizeIndex(key, value, ctx.$);
40
+ if (normalizedSignature.required) {
41
+ structure.required = append(structure.required, normalizedSignature.required);
43
42
  }
43
+ if (normalizedSignature.index)
44
+ structure.index = append(structure.index, normalizedSignature.index);
44
45
  }
45
46
  else {
46
47
  const value = ctx.$.parse(entry.value, ctx);
47
- structure[entry.kind] = (0, util_1.append)(structure[entry.kind], {
48
- key: entry.inner,
49
- value
50
- });
48
+ const inner = { key: entry.key, value };
49
+ if (entry.default !== unset) {
50
+ const out = value(entry.default);
51
+ if (out instanceof ArkErrors)
52
+ throwParseError(`Default value at ${printable(entry.key)} ${out}`);
53
+ value.assert(entry.default);
54
+ inner.default = entry.default;
55
+ }
56
+ structure[entry.kind] = append(structure[entry.kind], inner);
51
57
  }
52
58
  }
53
59
  const structureNode = ctx.$.node("structure", structure);
@@ -56,23 +62,46 @@ const parseObjectLiteral = (def, ctx) => {
56
62
  structure: spread?.merge(structureNode) ?? structureNode
57
63
  });
58
64
  };
59
- exports.parseObjectLiteral = parseObjectLiteral;
60
- exports.nonLeadingSpreadError = "Spread operator may only be used as the first key in an object";
61
- const parseEntry = (entry) => Object.assign(parseKey(entry[0]), { value: entry[1] });
62
- exports.parseEntry = parseEntry;
63
- const parseKey = (key) => typeof key === "symbol" ? { inner: key, kind: "required" }
65
+ export const writeInvalidUndeclaredBehaviorMessage = (actual) => `Value of '+' key must be 'reject', 'delete', or 'ignore' (was ${printable(actual)})`;
66
+ export const nonLeadingSpreadError = "Spread operator may only be used as the first key in an object";
67
+ export const parseEntry = ([key, value]) => {
68
+ const parsedKey = parseKey(key);
69
+ if (isArray(value) && value[1] === "=") {
70
+ if (parsedKey.kind !== "required")
71
+ throwParseError(invalidDefaultKeyKindMessage);
72
+ return {
73
+ kind: "optional",
74
+ key: parsedKey.key,
75
+ value: value[0],
76
+ default: value[2]
77
+ };
78
+ }
79
+ return {
80
+ kind: parsedKey.kind,
81
+ key: parsedKey.key,
82
+ value,
83
+ default: unset
84
+ };
85
+ };
86
+ // single quote use here is better for TypeScript's inlined error to avoid escapes
87
+ export const invalidDefaultKeyKindMessage = `Only required keys may specify default values, e.g. { ark: ['string', '=', '⛵'] }`;
88
+ const parseKey = (key) => typeof key === "symbol" ? { kind: "required", key }
64
89
  : key.at(-1) === "?" ?
65
- key.at(-2) === scanner_js_1.Scanner.escapeToken ?
66
- { inner: `${key.slice(0, -2)}?`, kind: "required" }
90
+ key.at(-2) === Scanner.escapeToken ?
91
+ { kind: "required", key: `${key.slice(0, -2)}?` }
67
92
  : {
68
- inner: key.slice(0, -1),
69
- kind: "optional"
93
+ kind: "optional",
94
+ key: key.slice(0, -1)
70
95
  }
71
96
  : key[0] === "[" && key.at(-1) === "]" ?
72
- { inner: key.slice(1, -1), kind: "index" }
73
- : key[0] === scanner_js_1.Scanner.escapeToken && key[1] === "[" && key.at(-1) === "]" ?
74
- { inner: key.slice(1), kind: "required" }
75
- : key === "..." ? { inner: "...", kind: "spread" }
76
- : { inner: key === "\\..." ? "..." : key, kind: "required" };
77
- const writeInvalidSpreadTypeMessage = (def) => `Spread operand must resolve to an object literal type (was ${def})`;
78
- exports.writeInvalidSpreadTypeMessage = writeInvalidSpreadTypeMessage;
97
+ { kind: "index", key: key.slice(1, -1) }
98
+ : key[0] === Scanner.escapeToken && key[1] === "[" && key.at(-1) === "]" ?
99
+ { kind: "required", key: key.slice(1) }
100
+ : key === "..." || key === "+" ? { kind: key, key }
101
+ : {
102
+ kind: "required",
103
+ key: key === "\\..." ? "..."
104
+ : key === "\\+" ? "+"
105
+ : key
106
+ };
107
+ export const writeInvalidSpreadTypeMessage = (def) => `Spread operand must resolve to an object literal type (was ${def})`;
@@ -1,5 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.writeDoubleRightBoundMessage = void 0;
4
- const writeDoubleRightBoundMessage = (root) => `Expression ${root} must have at most one right bound`;
5
- exports.writeDoubleRightBoundMessage = writeDoubleRightBoundMessage;
1
+ export const writeDoubleRightBoundMessage = (root) => `Expression ${root} must have at most one right bound`;
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
@@ -1,7 +1,2 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.writePrefixedPrivateReferenceMessage = exports.writeUnsatisfiableExpressionError = void 0;
4
- const writeUnsatisfiableExpressionError = (expression) => `${expression} results in an unsatisfiable type`;
5
- exports.writeUnsatisfiableExpressionError = writeUnsatisfiableExpressionError;
6
- const writePrefixedPrivateReferenceMessage = (def) => `Private type references should not include '#'. Use '${def.slice(1)}' instead.`;
7
- exports.writePrefixedPrivateReferenceMessage = writePrefixedPrivateReferenceMessage;
1
+ export const writeUnsatisfiableExpressionError = (expression) => `${expression} results in an unsatisfiable type`;
2
+ export const writePrefixedPrivateReferenceMessage = (def) => `Private type references should not include '#'. Use '${def.slice(1)}' instead.`;
@@ -1,13 +1,10 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DynamicState = void 0;
4
- const util_1 = require("@arktype/util");
5
- const operand_js_1 = require("../shift/operand/operand.js");
6
- const operator_js_1 = require("../shift/operator/operator.js");
7
- const scanner_js_1 = require("../shift/scanner.js");
8
- const string_js_1 = require("../string.js");
9
- const shared_js_1 = require("./shared.js");
10
- class DynamicState {
1
+ import { isKeyOf, throwInternalError, throwParseError } from "@arktype/util";
2
+ import { parseOperand } from "../shift/operand/operand.js";
3
+ import { parseOperator } from "../shift/operator/operator.js";
4
+ import { Scanner } from "../shift/scanner.js";
5
+ import { parseUntilFinalizer } from "../string.js";
6
+ import { invertedComparators, minComparators, writeMultipleLeftBoundsMessage, writeOpenRangeMessage, writeUnclosedGroupMessage, writeUnmatchedGroupCloseMessage, writeUnpairableComparatorMessage } from "./shared.js";
7
+ export class DynamicState {
11
8
  ctx;
12
9
  scanner;
13
10
  // set root type to `any` so that all constraints can be applied
@@ -22,10 +19,10 @@ class DynamicState {
22
19
  groups = [];
23
20
  constructor(def, ctx) {
24
21
  this.ctx = ctx;
25
- this.scanner = new scanner_js_1.Scanner(def);
22
+ this.scanner = new Scanner(def);
26
23
  }
27
24
  error(message) {
28
- return (0, util_1.throwParseError)(message);
25
+ return throwParseError(message);
29
26
  }
30
27
  hasRoot() {
31
28
  return this.root !== undefined;
@@ -43,16 +40,16 @@ class DynamicState {
43
40
  }
44
41
  finalize(finalizer) {
45
42
  if (this.groups.length)
46
- return this.error((0, shared_js_1.writeUnclosedGroupMessage)(")"));
43
+ return this.error(writeUnclosedGroupMessage(")"));
47
44
  this.finalizeBranches();
48
45
  this.finalizer = finalizer;
49
46
  }
50
47
  reduceLeftBound(limit, comparator) {
51
- const invertedComparator = shared_js_1.invertedComparators[comparator];
52
- if (!(0, util_1.isKeyOf)(invertedComparator, shared_js_1.minComparators))
53
- return this.error((0, shared_js_1.writeUnpairableComparatorMessage)(comparator));
48
+ const invertedComparator = invertedComparators[comparator];
49
+ if (!isKeyOf(invertedComparator, minComparators))
50
+ return this.error(writeUnpairableComparatorMessage(comparator));
54
51
  if (this.branches.leftBound) {
55
- return this.error((0, shared_js_1.writeMultipleLeftBoundsMessage)(this.branches.leftBound.limit, this.branches.leftBound.comparator, limit, invertedComparator));
52
+ return this.error(writeMultipleLeftBoundsMessage(this.branches.leftBound.limit, this.branches.leftBound.comparator, limit, invertedComparator));
56
53
  }
57
54
  this.branches.leftBound = {
58
55
  comparator: invertedComparator,
@@ -76,7 +73,7 @@ class DynamicState {
76
73
  this.finalizeBranches();
77
74
  const topBranchState = this.groups.pop();
78
75
  if (!topBranchState)
79
- return this.error((0, shared_js_1.writeUnmatchedGroupCloseMessage)(this.scanner.unscanned));
76
+ return this.error(writeUnmatchedGroupCloseMessage(this.scanner.unscanned));
80
77
  this.branches = topBranchState;
81
78
  }
82
79
  addPrefix(prefix) {
@@ -88,7 +85,7 @@ class DynamicState {
88
85
  this.root =
89
86
  lastPrefix === "keyof" ?
90
87
  this.root.keyof()
91
- : (0, util_1.throwInternalError)(`Unexpected prefix '${lastPrefix}'`);
88
+ : throwInternalError(`Unexpected prefix '${lastPrefix}'`);
92
89
  }
93
90
  }
94
91
  pushRootToBranch(token) {
@@ -105,17 +102,17 @@ class DynamicState {
105
102
  this.root = undefined;
106
103
  }
107
104
  parseUntilFinalizer() {
108
- return (0, string_js_1.parseUntilFinalizer)(new DynamicState(this.scanner.unscanned, this.ctx));
105
+ return parseUntilFinalizer(new DynamicState(this.scanner.unscanned, this.ctx));
109
106
  }
110
107
  parseOperator() {
111
- return (0, operator_js_1.parseOperator)(this);
108
+ return parseOperator(this);
112
109
  }
113
110
  parseOperand() {
114
- return (0, operand_js_1.parseOperand)(this);
111
+ return parseOperand(this);
115
112
  }
116
113
  assertRangeUnset() {
117
114
  if (this.branches.leftBound) {
118
- return this.error((0, shared_js_1.writeOpenRangeMessage)(this.branches.leftBound.limit, this.branches.leftBound.comparator));
115
+ return this.error(writeOpenRangeMessage(this.branches.leftBound.limit, this.branches.leftBound.comparator));
119
116
  }
120
117
  }
121
118
  reduceGroupOpen() {
@@ -139,4 +136,3 @@ class DynamicState {
139
136
  return this;
140
137
  }
141
138
  }
142
- exports.DynamicState = DynamicState;