@atscript/typescript 0.1.109 → 0.1.110
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.cjs +1587 -189
- package/dist/index.cjs +5 -2
- package/dist/index.d.ts +53 -1
- package/dist/index.mjs +3 -3
- package/dist/{json-schema-CioBVi6F.mjs → json-schema-B3iAqkGQ.cjs} +919 -37
- package/dist/{json-schema-9OBvrg9D.cjs → json-schema-BG1SEcjI.mjs} +618 -120
- package/dist/{plugin-D3_BnRrI.mjs → plugin-DpzE8aJr.mjs} +955 -116
- package/dist/{plugin-6r27w6rG.cjs → plugin-mzTvme4k.cjs} +966 -115
- package/dist/test-utils.cjs +4 -4
- package/dist/test-utils.d.ts +53 -0
- package/dist/test-utils.mjs +4 -4
- package/dist/utils.cjs +14 -26
- package/dist/utils.d.ts +81 -2
- package/dist/utils.mjs +13 -27
- package/package.json +3 -3
package/dist/cli.cjs
CHANGED
|
@@ -36,7 +36,7 @@ let node_readline = require("node:readline");
|
|
|
36
36
|
let node_url = require("node:url");
|
|
37
37
|
|
|
38
38
|
//#region packages/typescript/src/codegen/code-printer.ts
|
|
39
|
-
function _define_property$
|
|
39
|
+
function _define_property$10(obj, key, value) {
|
|
40
40
|
if (key in obj) Object.defineProperty(obj, key, {
|
|
41
41
|
value,
|
|
42
42
|
enumerable: true,
|
|
@@ -129,17 +129,17 @@ var CodePrinter = class {
|
|
|
129
129
|
return " ".repeat(this.indentLevel * this.indentSize);
|
|
130
130
|
}
|
|
131
131
|
constructor() {
|
|
132
|
-
_define_property$
|
|
133
|
-
_define_property$
|
|
134
|
-
_define_property$
|
|
135
|
-
_define_property$
|
|
136
|
-
_define_property$
|
|
132
|
+
_define_property$10(this, "lines", []);
|
|
133
|
+
_define_property$10(this, "currentLine", "");
|
|
134
|
+
_define_property$10(this, "indentLevel", 0);
|
|
135
|
+
_define_property$10(this, "indentSize", 2);
|
|
136
|
+
_define_property$10(this, "blockStack", []);
|
|
137
137
|
}
|
|
138
138
|
};
|
|
139
139
|
|
|
140
140
|
//#endregion
|
|
141
141
|
//#region packages/typescript/src/codegen/base-renderer.ts
|
|
142
|
-
function _define_property$
|
|
142
|
+
function _define_property$9(obj, key, value) {
|
|
143
143
|
if (key in obj) Object.defineProperty(obj, key, {
|
|
144
144
|
value,
|
|
145
145
|
enumerable: true,
|
|
@@ -202,7 +202,7 @@ var BaseRenderer = class extends CodePrinter {
|
|
|
202
202
|
}
|
|
203
203
|
}
|
|
204
204
|
constructor(doc) {
|
|
205
|
-
super(), _define_property$
|
|
205
|
+
super(), _define_property$9(this, "doc", void 0), _define_property$9(this, "_unused", void 0), this.doc = doc;
|
|
206
206
|
}
|
|
207
207
|
};
|
|
208
208
|
|
|
@@ -225,7 +225,7 @@ function escapeQuotes(str) {
|
|
|
225
225
|
|
|
226
226
|
//#endregion
|
|
227
227
|
//#region packages/typescript/src/codegen/type-renderer.ts
|
|
228
|
-
function _define_property$
|
|
228
|
+
function _define_property$8(obj, key, value) {
|
|
229
229
|
if (key in obj) Object.defineProperty(obj, key, {
|
|
230
230
|
value,
|
|
231
231
|
enumerable: true,
|
|
@@ -649,7 +649,7 @@ var TypeRenderer = class TypeRenderer extends BaseRenderer {
|
|
|
649
649
|
this.writeln(` */`);
|
|
650
650
|
}
|
|
651
651
|
constructor(doc, opts) {
|
|
652
|
-
super(doc), _define_property$
|
|
652
|
+
super(doc), _define_property$8(this, "opts", void 0), this.opts = opts;
|
|
653
653
|
}
|
|
654
654
|
};
|
|
655
655
|
function renderPrimitiveTypeDef(def) {
|
|
@@ -666,9 +666,512 @@ function renderPrimitiveTypeDef(def) {
|
|
|
666
666
|
}
|
|
667
667
|
}
|
|
668
668
|
|
|
669
|
+
//#endregion
|
|
670
|
+
//#region packages/typescript/src/runtime/validator-keys.ts
|
|
671
|
+
/**
|
|
672
|
+
* Annotation keys the validator pre-check reads, per kind of checked value — shared by the
|
|
673
|
+
* interpreted pre-check (validator-fast-check.ts) and the compiled checks (compiled-check/).
|
|
674
|
+
*/ /** Annotations of a string node, in check order. */ const STRING_KEYS = [
|
|
675
|
+
"meta.required",
|
|
676
|
+
"expect.minLength",
|
|
677
|
+
"expect.maxLength",
|
|
678
|
+
"expect.pattern"
|
|
679
|
+
];
|
|
680
|
+
/** Annotations of a number node, in check order. */ const NUMBER_KEYS = [
|
|
681
|
+
"expect.int",
|
|
682
|
+
"expect.min",
|
|
683
|
+
"expect.max",
|
|
684
|
+
"expect.exclusiveMin",
|
|
685
|
+
"expect.exclusiveMax"
|
|
686
|
+
];
|
|
687
|
+
/** Annotations of a boolean node. */ const BOOLEAN_KEYS = ["meta.required"];
|
|
688
|
+
/** Annotations of an array node, in check order. */ const ARRAY_KEYS = [
|
|
689
|
+
"expect.minLength",
|
|
690
|
+
"expect.maxLength",
|
|
691
|
+
"expect.array.uniqueItems"
|
|
692
|
+
];
|
|
693
|
+
/**
|
|
694
|
+
* Every annotation key the validator pre-check reads. The order is the bit order of
|
|
695
|
+
* compiled-check layouts: changing it changes the layout format.
|
|
696
|
+
*/ const VALIDATOR_KEYS = [
|
|
697
|
+
"meta.required",
|
|
698
|
+
"expect.minLength",
|
|
699
|
+
"expect.maxLength",
|
|
700
|
+
"expect.pattern",
|
|
701
|
+
"expect.int",
|
|
702
|
+
"expect.min",
|
|
703
|
+
"expect.max",
|
|
704
|
+
"expect.exclusiveMin",
|
|
705
|
+
"expect.exclusiveMax",
|
|
706
|
+
"expect.array.uniqueItems"
|
|
707
|
+
];
|
|
708
|
+
/**
|
|
709
|
+
* The number a length / bound annotation compares with: the annotation is either the number
|
|
710
|
+
* itself or an object carrying it in `field` (`{ length }`, `{ minValue }`, …).
|
|
711
|
+
*/ function boundOf(m, field) {
|
|
712
|
+
return typeof m === "number" ? m : m[field];
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
//#endregion
|
|
716
|
+
//#region packages/typescript/src/runtime/compiled-check/tracking.ts
|
|
717
|
+
/**
|
|
718
|
+
* Mutation tracking for build-time compiled validator checks.
|
|
719
|
+
*
|
|
720
|
+
* A compiled check is bound to the live type tree before use (see registry.ts): the bind
|
|
721
|
+
* verifies that the tree still has the structure the check was compiled for and snapshots
|
|
722
|
+
* the constraint values the check reads. Every tracked mutation of an object a bind has
|
|
723
|
+
* read bumps one global epoch, which makes every compiled check re-bind on its next use.
|
|
724
|
+
*
|
|
725
|
+
* Tracked: `annotate()`, the metadata / props `Map` methods (`set` / `delete` / `clear`),
|
|
726
|
+
* the `defineAnnotatedType()` handle methods and `cloneRefProp()`. Raw property writes
|
|
727
|
+
* (`node.optional = …`, `type.items.push(…)`, editing a constraint object in place, …)
|
|
728
|
+
* cannot be seen: call {@link invalidateCompiledValidators} after them.
|
|
729
|
+
*/ const VALIDATOR_KEY_SET = new Set(VALIDATOR_KEYS);
|
|
730
|
+
/** Global mutation epoch: compiled checks bound in an older epoch re-bind before use. */ let epoch = 0;
|
|
731
|
+
/** Some bind has watched an object (until then no mutation can affect a compiled check). */ let tracking = false;
|
|
732
|
+
/** Objects (nodes, type defs, metadata / props maps) read by some compiled-check bind. */ const watched = /* @__PURE__ */ new WeakSet();
|
|
733
|
+
/** The current mutation epoch. */ function currentEpoch() {
|
|
734
|
+
return epoch;
|
|
735
|
+
}
|
|
736
|
+
/** Marks `obj` as read by a bind: tracked mutations of it invalidate compiled checks. */ function watch(obj) {
|
|
737
|
+
tracking = true;
|
|
738
|
+
watched.add(obj);
|
|
739
|
+
}
|
|
740
|
+
/** Records a tracked mutation of `obj` (a no-op unless some bind has read it). */ function touched(obj) {
|
|
741
|
+
if (tracking && watched.has(obj)) epoch++;
|
|
742
|
+
}
|
|
743
|
+
/**
|
|
744
|
+
* Metadata map of runtime type nodes: a `Map` whose writes to validator annotation keys
|
|
745
|
+
* are tracked (see {@link invalidateCompiledValidators}). Reads are the builtin ones.
|
|
746
|
+
*/ var AnnotationMap = class extends Map {
|
|
747
|
+
set(key, value) {
|
|
748
|
+
super.set(key, value);
|
|
749
|
+
if (tracking && VALIDATOR_KEY_SET.has(key)) touched(this);
|
|
750
|
+
return this;
|
|
751
|
+
}
|
|
752
|
+
delete(key) {
|
|
753
|
+
const deleted = super.delete(key);
|
|
754
|
+
if (deleted && tracking && VALIDATOR_KEY_SET.has(key)) touched(this);
|
|
755
|
+
return deleted;
|
|
756
|
+
}
|
|
757
|
+
clear() {
|
|
758
|
+
super.clear();
|
|
759
|
+
touched(this);
|
|
760
|
+
}
|
|
761
|
+
};
|
|
762
|
+
/** Props map of runtime object types: a `Map` whose writes are tracked. */ var PropsMap = class extends Map {
|
|
763
|
+
set(key, value) {
|
|
764
|
+
super.set(key, value);
|
|
765
|
+
touched(this);
|
|
766
|
+
return this;
|
|
767
|
+
}
|
|
768
|
+
delete(key) {
|
|
769
|
+
const deleted = super.delete(key);
|
|
770
|
+
if (deleted) touched(this);
|
|
771
|
+
return deleted;
|
|
772
|
+
}
|
|
773
|
+
clear() {
|
|
774
|
+
super.clear();
|
|
775
|
+
touched(this);
|
|
776
|
+
}
|
|
777
|
+
};
|
|
778
|
+
/** `true` when `key` is an annotation the validator pre-check reads. */ function isValidatorKey(key) {
|
|
779
|
+
return VALIDATOR_KEY_SET.has(key);
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
//#endregion
|
|
783
|
+
//#region packages/typescript/src/runtime/compiled-check/layout.ts
|
|
784
|
+
/**
|
|
785
|
+
* Layout of a compiled validator check: the structure a check was compiled for, as plain
|
|
786
|
+
* data. `buildLayout` (build-layout.ts) derives it from a type tree (the build-time model
|
|
787
|
+
* of a generated module, or a live tree in tests); the binder (registry.ts) verifies a
|
|
788
|
+
* live tree against it and collects the constraint values the compiled code reads into
|
|
789
|
+
* the K slots.
|
|
790
|
+
*
|
|
791
|
+
* ```
|
|
792
|
+
* Layout = [defs: Def[], root: Site, slotCount]
|
|
793
|
+
* Site = [optional: 0|1, keyMask, def: number | EXT | INTERP, patternShape, slot]
|
|
794
|
+
* Def = [FINAL, designType] | [FINAL, designType, literal]
|
|
795
|
+
* | [OBJECT, keys, sites] | [OBJECT_PATTERNS]
|
|
796
|
+
* | [ARRAY, of] | [UNION | INTERSECTION | TUPLE, items] | [UNKNOWN, kind]
|
|
797
|
+
* ```
|
|
798
|
+
* - A site is one annotated-type node (a class root, prop, array element, union / tuple /
|
|
799
|
+
* intersection item). A def is one type-def object: sites sharing a def index share the
|
|
800
|
+
* def object (by identity) at runtime, which is how shared and recursive types are
|
|
801
|
+
* compiled once.
|
|
802
|
+
* - `keyMask` has a bit (by {@link VALIDATOR_KEYS} index) per validator annotation that is
|
|
803
|
+
* *effective* on the node and relevant to its def (present; flags: truthy).
|
|
804
|
+
* `patternShape` has one char per `@expect.pattern` entry: `e` (empty, skipped),
|
|
805
|
+
* `n` (checked), `s` (stateful `g` / `y` regex: the check bails to the walk there).
|
|
806
|
+
* - `slot`: the site's first K slot (see {@link siteSlotCount}); `slotCount` is the total.
|
|
807
|
+
* - `EXT`: an anonymous reference into another module's type; bound to that type's own
|
|
808
|
+
* compiled check (as a template, against the referring node) when it has one, else
|
|
809
|
+
* checked by the interpreted pre-check.
|
|
810
|
+
* - `INTERP`: a node only known at runtime (e.g. a chained reference into another module's
|
|
811
|
+
* type): always checked by the interpreted pre-check.
|
|
812
|
+
*/
|
|
813
|
+
/** Layout format version; checks of another version are ignored (interpreted). */ const LAYOUT_VERSION = 2;
|
|
814
|
+
const EXT = -1;
|
|
815
|
+
const INTERP = -2;
|
|
816
|
+
const D_FINAL = 0;
|
|
817
|
+
const D_OBJECT = 1;
|
|
818
|
+
const D_ARRAY = 2;
|
|
819
|
+
const D_UNION = 3;
|
|
820
|
+
const D_INTERSECTION = 4;
|
|
821
|
+
const D_TUPLE = 5;
|
|
822
|
+
const D_UNKNOWN = 6;
|
|
823
|
+
const D_OBJECT_PATTERNS = 7;
|
|
824
|
+
/** Def codes of the kinds with `items`, by kind name. */ const ITEMS_KIND_CODES = {
|
|
825
|
+
union: D_UNION,
|
|
826
|
+
intersection: D_INTERSECTION,
|
|
827
|
+
tuple: D_TUPLE
|
|
828
|
+
};
|
|
829
|
+
/** Kind names of the def codes with `items`. */ const ITEMS_KIND_NAMES = {
|
|
830
|
+
[D_UNION]: "union",
|
|
831
|
+
[D_INTERSECTION]: "intersection",
|
|
832
|
+
[D_TUPLE]: "tuple"
|
|
833
|
+
};
|
|
834
|
+
/** Bits of a compiled check's `flags` argument: the pre-check's TOP / IN_UNION, then options. */ const F_TOP = 1;
|
|
835
|
+
const F_IN_UNION = 2;
|
|
836
|
+
const F_ERROR = 4;
|
|
837
|
+
const F_STRIP = 8;
|
|
838
|
+
const F_PARTIAL_DEEP = 16;
|
|
839
|
+
const F_PARTIAL_TOP = 32;
|
|
840
|
+
/** Key mask bit of a validator annotation. */ function bit(key) {
|
|
841
|
+
return 1 << VALIDATOR_KEYS.indexOf(key);
|
|
842
|
+
}
|
|
843
|
+
function bitsOf(keys) {
|
|
844
|
+
let bits = 0;
|
|
845
|
+
for (const key of keys) bits |= bit(key);
|
|
846
|
+
return bits;
|
|
847
|
+
}
|
|
848
|
+
const B_REQUIRED = bit("meta.required");
|
|
849
|
+
const B_MIN_LENGTH = bit("expect.minLength");
|
|
850
|
+
const B_MAX_LENGTH = bit("expect.maxLength");
|
|
851
|
+
const B_PATTERN = bit("expect.pattern");
|
|
852
|
+
const B_INT = bit("expect.int");
|
|
853
|
+
const B_MIN = bit("expect.min");
|
|
854
|
+
const B_MAX = bit("expect.max");
|
|
855
|
+
const B_EX_MIN = bit("expect.exclusiveMin");
|
|
856
|
+
const B_EX_MAX = bit("expect.exclusiveMax");
|
|
857
|
+
const B_UNIQUE = bit("expect.array.uniqueItems");
|
|
858
|
+
/** Annotations the pre-check reads per kind of value (see validator-keys.ts). */ const STRING_BITS = bitsOf(STRING_KEYS);
|
|
859
|
+
const NUMBER_BITS = bitsOf(NUMBER_KEYS);
|
|
860
|
+
const ARRAY_BITS = bitsOf(ARRAY_KEYS);
|
|
861
|
+
const BOOLEAN_BITS = bitsOf(BOOLEAN_KEYS);
|
|
862
|
+
/** Keys whose value the compiled code reads (one K slot each). */ const VALUE_BITS = B_MIN_LENGTH | B_MAX_LENGTH | B_MIN | B_MAX | B_EX_MIN | B_EX_MAX;
|
|
863
|
+
const FLAG_BITS = B_REQUIRED | B_INT | B_UNIQUE;
|
|
864
|
+
const ALL_BITS = (1 << VALIDATOR_KEYS.length) - 1;
|
|
865
|
+
/** The field holding the bound of each value key's object form. */ const BOUND_FIELDS = {
|
|
866
|
+
[B_MIN_LENGTH]: "length",
|
|
867
|
+
[B_MAX_LENGTH]: "length",
|
|
868
|
+
[B_MIN]: "minValue",
|
|
869
|
+
[B_MAX]: "maxValue",
|
|
870
|
+
[B_EX_MIN]: "exclusiveMinValue",
|
|
871
|
+
[B_EX_MAX]: "exclusiveMaxValue"
|
|
872
|
+
};
|
|
873
|
+
/**
|
|
874
|
+
* Validator annotations the pre-check reads for a node of this type def (the rest of the
|
|
875
|
+
* node's annotations cannot change its outcome, so they are not part of the layout).
|
|
876
|
+
*/ function relevantBits(type) {
|
|
877
|
+
switch (type.kind) {
|
|
878
|
+
case "":
|
|
879
|
+
if (type.designType === "phantom" || type.value !== void 0) return 0;
|
|
880
|
+
switch (type.designType) {
|
|
881
|
+
case "string": return STRING_BITS;
|
|
882
|
+
case "number": return NUMBER_BITS;
|
|
883
|
+
case "boolean": return BOOLEAN_BITS;
|
|
884
|
+
default: return 0;
|
|
885
|
+
}
|
|
886
|
+
case "array": return ARRAY_BITS;
|
|
887
|
+
case "union": return ALL_BITS;
|
|
888
|
+
default: return 0;
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
/**
|
|
892
|
+
* {@link relevantBits} of a site (`undefined`: a type only known at runtime);
|
|
893
|
+
* `meta.required` also matters on any optional node.
|
|
894
|
+
*/ function siteBits(type, optional) {
|
|
895
|
+
return (type === void 0 ? 0 : relevantBits(type)) | (optional ? B_REQUIRED : 0);
|
|
896
|
+
}
|
|
897
|
+
/**
|
|
898
|
+
* Number of K slots a site takes, from its first slot on:
|
|
899
|
+
* - INTERP: its interpreted check, then whether its type is a literal;
|
|
900
|
+
* - otherwise, for an optional `@meta.required` node ({@link hasRejectSlot}): the node
|
|
901
|
+
* (its `null` is rejected unless its type accepts `null`, decided live);
|
|
902
|
+
* - then EXT: the bound check of the referenced type, and whether that type is a literal;
|
|
903
|
+
* any other site: its annotation values (see {@link keySlots}).
|
|
904
|
+
*/ function siteSlotCount(site) {
|
|
905
|
+
const [, mask, def, shape] = site;
|
|
906
|
+
if (def === INTERP) return 2;
|
|
907
|
+
let n = (hasRejectSlot(site) ? 1 : 0) + (def === EXT ? 2 : 0);
|
|
908
|
+
if (def !== EXT) {
|
|
909
|
+
for (let bits = mask & VALUE_BITS; bits !== 0; bits &= bits - 1) n++;
|
|
910
|
+
for (const c of shape) if (c === "n") n++;
|
|
911
|
+
}
|
|
912
|
+
return n;
|
|
913
|
+
}
|
|
914
|
+
/** An optional node with `@meta.required`: `null` fails unless its type accepts `null`. */ function hasRejectSlot(site) {
|
|
915
|
+
return site[0] === 1 && (site[1] & B_REQUIRED) !== 0;
|
|
916
|
+
}
|
|
917
|
+
/** The K slot of each annotation value of a site whose values start at slot `at`. */ function keySlots(mask, shape, at) {
|
|
918
|
+
let k = at;
|
|
919
|
+
const next = (b) => (mask & b) === 0 ? -1 : k++;
|
|
920
|
+
const minLength = next(B_MIN_LENGTH);
|
|
921
|
+
const maxLength = next(B_MAX_LENGTH);
|
|
922
|
+
const patterns = [];
|
|
923
|
+
for (const c of shape) if (c === "n") patterns.push(k++);
|
|
924
|
+
return {
|
|
925
|
+
minLength,
|
|
926
|
+
maxLength,
|
|
927
|
+
patterns,
|
|
928
|
+
min: next(B_MIN),
|
|
929
|
+
max: next(B_MAX),
|
|
930
|
+
exMin: next(B_EX_MIN),
|
|
931
|
+
exMax: next(B_EX_MAX)
|
|
932
|
+
};
|
|
933
|
+
}
|
|
934
|
+
/** The child sites of a def. */ function childSites(def) {
|
|
935
|
+
switch (def[0]) {
|
|
936
|
+
case D_OBJECT: return def[2];
|
|
937
|
+
case D_ARRAY: return [def[1]];
|
|
938
|
+
case D_UNION:
|
|
939
|
+
case D_INTERSECTION:
|
|
940
|
+
case D_TUPLE: return def[1];
|
|
941
|
+
default: return [];
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
/**
|
|
945
|
+
* Computes a node's key mask and pattern shape over `relevant` bits, and — when `out` is
|
|
946
|
+
* given — writes the normalised annotation values from `out[at]` on (the K slots).
|
|
947
|
+
* Throws on annotation values the pre-check itself would throw on (the bind then fails
|
|
948
|
+
* and the interpreted pre-check runs, which reproduces that behaviour).
|
|
949
|
+
*/ function readKeys(metadata, relevant, reader, out, at = 0) {
|
|
950
|
+
let mask = 0;
|
|
951
|
+
let shape = "";
|
|
952
|
+
let k = at;
|
|
953
|
+
if (metadata.size === 0) return {
|
|
954
|
+
mask,
|
|
955
|
+
shape
|
|
956
|
+
};
|
|
957
|
+
for (let i = 0; i < VALIDATOR_KEYS.length; i++) {
|
|
958
|
+
const b = 1 << i;
|
|
959
|
+
if ((relevant & b) === 0) continue;
|
|
960
|
+
const raw = metadata.get(VALIDATOR_KEYS[i]);
|
|
961
|
+
if ((b & FLAG_BITS) !== 0) {
|
|
962
|
+
if (raw) mask |= b;
|
|
963
|
+
continue;
|
|
964
|
+
}
|
|
965
|
+
if (b === B_PATTERN) {
|
|
966
|
+
if (!raw) continue;
|
|
967
|
+
if (!Array.isArray(raw)) throw new TypeError("expect.pattern is not an array");
|
|
968
|
+
mask |= b;
|
|
969
|
+
for (const entry of raw) {
|
|
970
|
+
const { pattern, flags } = entry;
|
|
971
|
+
if (!pattern) {
|
|
972
|
+
shape += "e";
|
|
973
|
+
continue;
|
|
974
|
+
}
|
|
975
|
+
const re = reader.regex(pattern, flags);
|
|
976
|
+
if (re.global || re.sticky) {
|
|
977
|
+
shape += "s";
|
|
978
|
+
break;
|
|
979
|
+
}
|
|
980
|
+
shape += "n";
|
|
981
|
+
if (out) out[k] = re;
|
|
982
|
+
k++;
|
|
983
|
+
}
|
|
984
|
+
continue;
|
|
985
|
+
}
|
|
986
|
+
if (raw === void 0) continue;
|
|
987
|
+
mask |= b;
|
|
988
|
+
if (out) out[k] = boundOf(raw, BOUND_FIELDS[b]);
|
|
989
|
+
k++;
|
|
990
|
+
}
|
|
991
|
+
return {
|
|
992
|
+
mask,
|
|
993
|
+
shape
|
|
994
|
+
};
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
//#endregion
|
|
998
|
+
//#region packages/typescript/src/runtime/compiled-check/registry.ts
|
|
999
|
+
/**
|
|
1000
|
+
* Runtime registry and binder of build-time compiled validator checks.
|
|
1001
|
+
*
|
|
1002
|
+
* Generated `.as.js` modules (built with `tsPlugin({ compileValidators: true })`) call
|
|
1003
|
+
* {@link registerCompiledCheck} once per class with the class's layout and a factory of
|
|
1004
|
+
* its check. On first use, and again after each mutation epoch (see tracking.ts), the
|
|
1005
|
+
* entry is bound: `bindLayout` verifies the live type tree against the layout and collects
|
|
1006
|
+
* the constraint values into `K`; the factory turns `K` into the check function. A tree
|
|
1007
|
+
* that no longer matches leaves the entry unbound for that epoch, so the interpreted
|
|
1008
|
+
* pre-check runs instead — always correct, never stale.
|
|
1009
|
+
*
|
|
1010
|
+
* A registered entry is also a template: a node referencing a class of another module
|
|
1011
|
+
* (an `EXT` site) binds that class's layout against itself — its own annotations feed `K`
|
|
1012
|
+
* — so cross-module references, aliases of imported types and cloned imported props run
|
|
1013
|
+
* compiled code whenever their tree matches the referenced type's structure.
|
|
1014
|
+
*
|
|
1015
|
+
* The registry is module-local: classes registered with another copy of this runtime
|
|
1016
|
+
* (dual ESM / CJS loads, duplicate installs) and subclasses of registered classes are
|
|
1017
|
+
* simply validated by the interpreted pre-check.
|
|
1018
|
+
*/
|
|
1019
|
+
/** Entries by class (own registrations only: a subclass is not registered). */ const entries = /* @__PURE__ */ new WeakMap();
|
|
1020
|
+
/** Template bindings by referring node. */ const templates = /* @__PURE__ */ new WeakMap();
|
|
1021
|
+
/** Some compiled check is registered (until then lookups return at once). */ let anyCompiled = false;
|
|
1022
|
+
/** The option bits of `opts` for a compiled check's `flags` argument. */ function optionFlags(opts) {
|
|
1023
|
+
const unknownProps = opts.unknownProps;
|
|
1024
|
+
const partial = opts.partial;
|
|
1025
|
+
return (unknownProps === "error" ? F_ERROR : unknownProps === "strip" ? F_STRIP : 0) | (partial === "deep" ? F_PARTIAL_DEEP : partial === true ? F_PARTIAL_TOP : 0);
|
|
1026
|
+
}
|
|
1027
|
+
let autoCompile = null;
|
|
1028
|
+
/** The compiled check to run for `def` in this call, or `null` (interpreted). */ function compiledCheckFor(def, env) {
|
|
1029
|
+
if (!anyCompiled) return null;
|
|
1030
|
+
let entry = entries.get(def);
|
|
1031
|
+
if (entry === void 0) {
|
|
1032
|
+
if (autoCompile === null) return null;
|
|
1033
|
+
autoCompile(def);
|
|
1034
|
+
entry = entries.get(def);
|
|
1035
|
+
if (entry === void 0) return null;
|
|
1036
|
+
}
|
|
1037
|
+
return entry.epoch === currentEpoch() ? entry.fn : bind(entry, entry, def, env, false);
|
|
1038
|
+
}
|
|
1039
|
+
/** (Re)binds `entry`'s layout against `node` for the current epoch. */ function bind(binding, entry, node, env, template) {
|
|
1040
|
+
binding.epoch = currentEpoch();
|
|
1041
|
+
binding.fn = null;
|
|
1042
|
+
binding.binding = true;
|
|
1043
|
+
try {
|
|
1044
|
+
const K = bindLayout(entry.layout, node, env, template);
|
|
1045
|
+
if (K !== void 0) binding.fn = entry.factory(K, env);
|
|
1046
|
+
} catch {} finally {
|
|
1047
|
+
binding.binding = false;
|
|
1048
|
+
}
|
|
1049
|
+
return binding.fn;
|
|
1050
|
+
}
|
|
1051
|
+
/**
|
|
1052
|
+
* Verifies the live tree of `node` against `layout` and collects the K slots; `undefined`
|
|
1053
|
+
* when anything differs. A `template` bind (a node referring to the layout's class) leaves
|
|
1054
|
+
* the root's `optional` to the referring site.
|
|
1055
|
+
*/ function bindLayout(layout, node, env, template) {
|
|
1056
|
+
const [defs, root, slotCount] = layout;
|
|
1057
|
+
const bound = [];
|
|
1058
|
+
const K = [];
|
|
1059
|
+
K.length = slotCount;
|
|
1060
|
+
/** An EXT / INTERP site: its check, then whether its type is a literal. */ const bindRuntimeSite = (n, at, ext) => {
|
|
1061
|
+
const type = n.type;
|
|
1062
|
+
if (typeof type !== "object" || type === null || isPhantomType(n)) return false;
|
|
1063
|
+
watch(n);
|
|
1064
|
+
watch(type);
|
|
1065
|
+
K[at] = ext ? externalCheck(n, env) : interpreted(n, env);
|
|
1066
|
+
K[at + 1] = type.value !== void 0;
|
|
1067
|
+
return true;
|
|
1068
|
+
};
|
|
1069
|
+
const bindSite = (site, n) => {
|
|
1070
|
+
const [optional, mask, d, shape, at] = site;
|
|
1071
|
+
if (d === INTERP) return bindRuntimeSite(n, at, false);
|
|
1072
|
+
if (template && site === root ? optional !== 0 : !!n.optional !== (optional === 1)) return false;
|
|
1073
|
+
watch(n);
|
|
1074
|
+
const metadata = n.metadata;
|
|
1075
|
+
if (!(metadata instanceof AnnotationMap)) return false;
|
|
1076
|
+
watch(metadata);
|
|
1077
|
+
let k = at;
|
|
1078
|
+
if (hasRejectSlot(site)) K[k++] = n;
|
|
1079
|
+
if (d === EXT) return readKeys(metadata, siteBits(void 0, optional === 1), env).mask === mask && bindRuntimeSite(n, k, true);
|
|
1080
|
+
const type = n.type;
|
|
1081
|
+
if (bound[d] === void 0) {
|
|
1082
|
+
if (typeof type !== "object" || type === null) return false;
|
|
1083
|
+
bound[d] = type;
|
|
1084
|
+
if (!bindDef(defs[d], type)) return false;
|
|
1085
|
+
} else if (bound[d] !== type) return false;
|
|
1086
|
+
const read = readKeys(metadata, siteBits(type, optional === 1), env, K, k);
|
|
1087
|
+
return read.mask === mask && read.shape === shape;
|
|
1088
|
+
};
|
|
1089
|
+
const bindSites = (sites, nodes) => {
|
|
1090
|
+
if (!Array.isArray(nodes) || nodes.length !== sites.length) return false;
|
|
1091
|
+
for (let i = 0; i < sites.length; i++) if (!bindSite(sites[i], nodes[i])) return false;
|
|
1092
|
+
return true;
|
|
1093
|
+
};
|
|
1094
|
+
const bindDef = (def, type) => {
|
|
1095
|
+
watch(type);
|
|
1096
|
+
switch (def[0]) {
|
|
1097
|
+
case D_FINAL: return type.kind === "" && type.designType === def[1] && (def.length === 2 ? type.value === void 0 : type.value === def[2]);
|
|
1098
|
+
case D_OBJECT: {
|
|
1099
|
+
const props = type.props;
|
|
1100
|
+
if (type.kind !== "object" || !(props instanceof PropsMap) || !Array.isArray(type.propsPatterns) || type.propsPatterns.length > 0) return false;
|
|
1101
|
+
watch(props);
|
|
1102
|
+
const keys = def[1];
|
|
1103
|
+
const sites = def[2];
|
|
1104
|
+
if (props.size !== keys.length) return false;
|
|
1105
|
+
let i = 0;
|
|
1106
|
+
for (const [key, prop] of props) {
|
|
1107
|
+
if (key !== keys[i] || !bindSite(sites[i], prop)) return false;
|
|
1108
|
+
i++;
|
|
1109
|
+
}
|
|
1110
|
+
return true;
|
|
1111
|
+
}
|
|
1112
|
+
case D_OBJECT_PATTERNS: return type.kind === "object" && Array.isArray(type.propsPatterns) && type.propsPatterns.length > 0;
|
|
1113
|
+
case D_ARRAY: return type.kind === "array" && bindSite(def[1], type.of);
|
|
1114
|
+
case D_UNION:
|
|
1115
|
+
case D_INTERSECTION:
|
|
1116
|
+
case D_TUPLE: return type.kind === ITEMS_KIND_NAMES[def[0]] && bindSites(def[1], type.items);
|
|
1117
|
+
case D_UNKNOWN: return type.kind === def[1];
|
|
1118
|
+
default: return false;
|
|
1119
|
+
}
|
|
1120
|
+
};
|
|
1121
|
+
return bindSite(root, node) ? K : void 0;
|
|
1122
|
+
}
|
|
1123
|
+
/** The interpreted pre-check of `node` (its subtree is read live on every call). */ function interpreted(node, env) {
|
|
1124
|
+
const interp = env.interp;
|
|
1125
|
+
return (value, flags, opts) => interp(node, value, flags, opts);
|
|
1126
|
+
}
|
|
1127
|
+
/**
|
|
1128
|
+
* The check of a node referencing another module's type (called for a non-nullish value
|
|
1129
|
+
* when the node is optional; the referring site handles `optional`): the referenced class's
|
|
1130
|
+
* layout bound against the node when it matches, else the interpreted pre-check.
|
|
1131
|
+
*/ function externalCheck(node, env) {
|
|
1132
|
+
const entry = templateOf(node);
|
|
1133
|
+
if (entry !== void 0) {
|
|
1134
|
+
let binding = templates.get(node);
|
|
1135
|
+
if (binding === void 0 || binding.entry !== entry) {
|
|
1136
|
+
binding = {
|
|
1137
|
+
entry,
|
|
1138
|
+
epoch: -1,
|
|
1139
|
+
fn: null,
|
|
1140
|
+
binding: false
|
|
1141
|
+
};
|
|
1142
|
+
templates.set(node, binding);
|
|
1143
|
+
}
|
|
1144
|
+
if (binding.binding) {
|
|
1145
|
+
const pending = binding;
|
|
1146
|
+
const fallback = interpreted(node, env);
|
|
1147
|
+
return (value, flags, opts) => (pending.fn ?? fallback)(value, flags, opts);
|
|
1148
|
+
}
|
|
1149
|
+
const fn = binding.epoch === currentEpoch() ? binding.fn : bind(binding, entry, node, env, true);
|
|
1150
|
+
if (fn !== null) return fn;
|
|
1151
|
+
}
|
|
1152
|
+
return interpreted(node, env);
|
|
1153
|
+
}
|
|
1154
|
+
/**
|
|
1155
|
+
* The registered class whose layout a referring node binds against: the target of its
|
|
1156
|
+
* (unchained) reference, through aliases of other modules' types.
|
|
1157
|
+
*/ function templateOf(node) {
|
|
1158
|
+
let ref = node.ref;
|
|
1159
|
+
for (let hops = 0; hops < 16 && ref !== void 0 && ref.field === ""; hops++) {
|
|
1160
|
+
let target;
|
|
1161
|
+
try {
|
|
1162
|
+
target = ref.type();
|
|
1163
|
+
} catch {
|
|
1164
|
+
return;
|
|
1165
|
+
}
|
|
1166
|
+
const entry = entries.get(target);
|
|
1167
|
+
if (entry === void 0 || entry.layout[1][2] !== EXT) return entry;
|
|
1168
|
+
ref = target.ref;
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
|
|
669
1172
|
//#endregion
|
|
670
1173
|
//#region packages/typescript/src/runtime/validator-fast-check.ts
|
|
671
|
-
function _define_property$
|
|
1174
|
+
function _define_property$7(obj, key, value) {
|
|
672
1175
|
if (key in obj) Object.defineProperty(obj, key, {
|
|
673
1176
|
value,
|
|
674
1177
|
enumerable: true,
|
|
@@ -732,7 +1235,7 @@ const BAIL = 2;
|
|
|
732
1235
|
if (opts.unknownProps === "strip") return fastCheckStripping(def, value, opts);
|
|
733
1236
|
stamp++;
|
|
734
1237
|
try {
|
|
735
|
-
return
|
|
1238
|
+
return check(def, value, opts) === PASS;
|
|
736
1239
|
} catch {
|
|
737
1240
|
return false;
|
|
738
1241
|
}
|
|
@@ -743,7 +1246,7 @@ const BAIL = 2;
|
|
|
743
1246
|
stripping = true;
|
|
744
1247
|
let passed;
|
|
745
1248
|
try {
|
|
746
|
-
passed =
|
|
1249
|
+
passed = check(def, value, opts) === PASS;
|
|
747
1250
|
} catch {
|
|
748
1251
|
passed = false;
|
|
749
1252
|
} finally {
|
|
@@ -756,6 +1259,14 @@ const BAIL = 2;
|
|
|
756
1259
|
}
|
|
757
1260
|
return passed;
|
|
758
1261
|
}
|
|
1262
|
+
/**
|
|
1263
|
+
* The pre-check of the root: the type's build-time compiled check when it has one bound to
|
|
1264
|
+
* the live tree (see compiled-check/registry.ts), else the interpreted one. Both share the
|
|
1265
|
+
* contract above and the strip state.
|
|
1266
|
+
*/ function check(def, value, opts) {
|
|
1267
|
+
const compiled = compiledCheckFor(def, COMPILED_ENV);
|
|
1268
|
+
return compiled === null ? fastCheck(def, value, TOP, opts) : compiled(value, TOP | optionFlags(opts), opts);
|
|
1269
|
+
}
|
|
759
1270
|
const DECIMAL_RE = /^[+-]?\d+(\.\d+)?$/;
|
|
760
1271
|
const regexCache = /* @__PURE__ */ new Map();
|
|
761
1272
|
/** Compiled `@expect.pattern` regex, cached per (pattern, flags). */ function getRegex(pattern, flags) {
|
|
@@ -789,6 +1300,14 @@ function acceptsNull(def) {
|
|
|
789
1300
|
}
|
|
790
1301
|
return false;
|
|
791
1302
|
}
|
|
1303
|
+
/** What compiled checks share with the interpreted pre-check. */ const COMPILED_ENV = {
|
|
1304
|
+
pk: pendingKeys,
|
|
1305
|
+
ho: Object.prototype.hasOwnProperty,
|
|
1306
|
+
dec: DECIMAL_RE,
|
|
1307
|
+
regex: getRegex,
|
|
1308
|
+
interp: fastCheck,
|
|
1309
|
+
rn: rejectsNullAsRequired
|
|
1310
|
+
};
|
|
792
1311
|
function fastCheck(def, value, flags, opts) {
|
|
793
1312
|
if (def.optional && (value === void 0 || value === null)) return value === null && rejectsNullAsRequired(def) ? FAIL : PASS;
|
|
794
1313
|
const type = def.type;
|
|
@@ -984,41 +1503,41 @@ function isFinalOp(op) {
|
|
|
984
1503
|
const plans = /* @__PURE__ */ new WeakMap();
|
|
985
1504
|
/** One shape for every plan, so plan field access stays monomorphic. */ let Plan = class Plan {
|
|
986
1505
|
constructor(def) {
|
|
987
|
-
_define_property$
|
|
988
|
-
/** Call in which the node structure was last verified (`-1`: must rebuild). */ _define_property$
|
|
989
|
-
/** Call in which the annotation values (`m0`–`m4`) were last verified. */ _define_property$
|
|
990
|
-
_define_property$
|
|
991
|
-
_define_property$
|
|
992
|
-
_define_property$
|
|
993
|
-
_define_property$
|
|
994
|
-
_define_property$
|
|
995
|
-
_define_property$
|
|
996
|
-
_define_property$
|
|
997
|
-
/** String plans: compiled `@expect.pattern` regexes by position (checked against the live entry). */ _define_property$
|
|
1506
|
+
_define_property$7(this, "def", void 0);
|
|
1507
|
+
/** Call in which the node structure was last verified (`-1`: must rebuild). */ _define_property$7(this, "stamp", void 0);
|
|
1508
|
+
/** Call in which the annotation values (`m0`–`m4`) were last verified. */ _define_property$7(this, "metaStamp", void 0);
|
|
1509
|
+
_define_property$7(this, "op", void 0);
|
|
1510
|
+
_define_property$7(this, "type", void 0);
|
|
1511
|
+
_define_property$7(this, "optional", void 0);
|
|
1512
|
+
_define_property$7(this, "metadata", void 0);
|
|
1513
|
+
_define_property$7(this, "kind", void 0);
|
|
1514
|
+
_define_property$7(this, "designType", void 0);
|
|
1515
|
+
_define_property$7(this, "literal", void 0);
|
|
1516
|
+
/** String plans: compiled `@expect.pattern` regexes by position (checked against the live entry). */ _define_property$7(this, "regexes", void 0);
|
|
998
1517
|
/**
|
|
999
1518
|
* Annotation keys the node's check reads, and their values (`undefined` when absent):
|
|
1000
1519
|
* string `meta.required, expect.minLength, maxLength, pattern`; number `expect.int, min,
|
|
1001
1520
|
* max, exclusiveMin, exclusiveMax`; boolean `meta.required`; array `expect.minLength, maxLength, array.uniqueItems`.
|
|
1002
|
-
*/ _define_property$
|
|
1003
|
-
_define_property$
|
|
1004
|
-
_define_property$
|
|
1005
|
-
_define_property$
|
|
1006
|
-
_define_property$
|
|
1007
|
-
_define_property$
|
|
1008
|
-
_define_property$
|
|
1009
|
-
_define_property$
|
|
1010
|
-
_define_property$
|
|
1011
|
-
_define_property$
|
|
1012
|
-
_define_property$
|
|
1013
|
-
_define_property$
|
|
1014
|
-
_define_property$
|
|
1015
|
-
/** Call in which `props.get(keys[i]) === nodes[i]` was last verified. */ _define_property$
|
|
1016
|
-
/** Prop plans by key (the entries of `props`, verified per visit via `edges`). */ _define_property$
|
|
1017
|
-
_define_property$
|
|
1018
|
-
_define_property$
|
|
1019
|
-
_define_property$
|
|
1020
|
-
_define_property$
|
|
1021
|
-
_define_property$
|
|
1521
|
+
*/ _define_property$7(this, "k0", void 0);
|
|
1522
|
+
_define_property$7(this, "k1", void 0);
|
|
1523
|
+
_define_property$7(this, "k2", void 0);
|
|
1524
|
+
_define_property$7(this, "k3", void 0);
|
|
1525
|
+
_define_property$7(this, "k4", void 0);
|
|
1526
|
+
_define_property$7(this, "m0", void 0);
|
|
1527
|
+
_define_property$7(this, "m1", void 0);
|
|
1528
|
+
_define_property$7(this, "m2", void 0);
|
|
1529
|
+
_define_property$7(this, "m3", void 0);
|
|
1530
|
+
_define_property$7(this, "m4", void 0);
|
|
1531
|
+
_define_property$7(this, "props", void 0);
|
|
1532
|
+
_define_property$7(this, "propsPatterns", void 0);
|
|
1533
|
+
_define_property$7(this, "keys", void 0);
|
|
1534
|
+
/** Call in which `props.get(keys[i]) === nodes[i]` was last verified. */ _define_property$7(this, "edges", void 0);
|
|
1535
|
+
/** Prop plans by key (the entries of `props`, verified per visit via `edges`). */ _define_property$7(this, "byKey", void 0);
|
|
1536
|
+
_define_property$7(this, "nodes", void 0);
|
|
1537
|
+
_define_property$7(this, "children", void 0);
|
|
1538
|
+
_define_property$7(this, "of", void 0);
|
|
1539
|
+
_define_property$7(this, "ofPlan", void 0);
|
|
1540
|
+
_define_property$7(this, "items", void 0);
|
|
1022
1541
|
this.def = def;
|
|
1023
1542
|
this.stamp = -1;
|
|
1024
1543
|
this.metaStamp = -1;
|
|
@@ -1137,14 +1656,14 @@ function setMetaKeys(plan, k0, k1, k2, k3, k4) {
|
|
|
1137
1656
|
if (type.designType === "phantom") plan.op = OP_PHANTOM;
|
|
1138
1657
|
else if (type.value !== void 0) plan.op = OP_FINAL;
|
|
1139
1658
|
else if (type.designType === "string") {
|
|
1140
|
-
setMetaKeys(plan,
|
|
1659
|
+
setMetaKeys(plan, ...STRING_KEYS);
|
|
1141
1660
|
plan.regexes = [];
|
|
1142
1661
|
plan.op = OP_STRING;
|
|
1143
1662
|
} else if (type.designType === "number") {
|
|
1144
|
-
setMetaKeys(plan,
|
|
1663
|
+
setMetaKeys(plan, ...NUMBER_KEYS);
|
|
1145
1664
|
plan.op = OP_NUMBER;
|
|
1146
1665
|
} else if (type.designType === "boolean") {
|
|
1147
|
-
setMetaKeys(plan,
|
|
1666
|
+
setMetaKeys(plan, ...BOOLEAN_KEYS);
|
|
1148
1667
|
plan.op = OP_BOOLEAN;
|
|
1149
1668
|
} else plan.op = OP_FINAL;
|
|
1150
1669
|
break;
|
|
@@ -1170,7 +1689,7 @@ function setMetaKeys(plan, k0, k1, k2, k3, k4) {
|
|
|
1170
1689
|
break;
|
|
1171
1690
|
}
|
|
1172
1691
|
case "array":
|
|
1173
|
-
setMetaKeys(plan,
|
|
1692
|
+
setMetaKeys(plan, ...ARRAY_KEYS);
|
|
1174
1693
|
plan.of = type.of;
|
|
1175
1694
|
plan.ofPlan = planFor(type.of);
|
|
1176
1695
|
plan.op = OP_ARRAY;
|
|
@@ -1308,7 +1827,7 @@ function setMetaKeys(plan, k0, k1, k2, k3, k4) {
|
|
|
1308
1827
|
|
|
1309
1828
|
//#endregion
|
|
1310
1829
|
//#region packages/typescript/src/runtime/validator.ts
|
|
1311
|
-
function _define_property$
|
|
1830
|
+
function _define_property$6(obj, key, value) {
|
|
1312
1831
|
if (key in obj) Object.defineProperty(obj, key, {
|
|
1313
1832
|
value,
|
|
1314
1833
|
enumerable: true,
|
|
@@ -1807,17 +2326,17 @@ function _define_property$4(obj, key, value) {
|
|
|
1807
2326
|
return true;
|
|
1808
2327
|
}
|
|
1809
2328
|
constructor(def, opts) {
|
|
1810
|
-
_define_property$
|
|
1811
|
-
_define_property$
|
|
1812
|
-
_define_property$
|
|
1813
|
-
_define_property$
|
|
1814
|
-
_define_property$
|
|
1815
|
-
/** Validation errors collected during the last {@link validate} call. */ _define_property$
|
|
1816
|
-
_define_property$
|
|
1817
|
-
_define_property$
|
|
1818
|
-
_define_property$
|
|
1819
|
-
_define_property$
|
|
1820
|
-
_define_property$
|
|
2329
|
+
_define_property$6(this, "def", void 0);
|
|
2330
|
+
_define_property$6(this, "opts", void 0);
|
|
2331
|
+
_define_property$6(this, "hasPlugins", void 0);
|
|
2332
|
+
_define_property$6(this, "hasReplace", void 0);
|
|
2333
|
+
_define_property$6(this, "replaceCache", void 0);
|
|
2334
|
+
/** Validation errors collected during the last {@link validate} call. */ _define_property$6(this, "errors", void 0);
|
|
2335
|
+
_define_property$6(this, "stackErrors", void 0);
|
|
2336
|
+
_define_property$6(this, "pathSegments", void 0);
|
|
2337
|
+
_define_property$6(this, "depth", void 0);
|
|
2338
|
+
_define_property$6(this, "limitExceeded", void 0);
|
|
2339
|
+
_define_property$6(this, "context", void 0);
|
|
1821
2340
|
this.def = def;
|
|
1822
2341
|
this.errors = [];
|
|
1823
2342
|
this.stackErrors = [];
|
|
@@ -1862,7 +2381,7 @@ function _define_property$4(obj, key, value) {
|
|
|
1862
2381
|
}
|
|
1863
2382
|
/** Error thrown by {@link Validator.validate} when validation fails. Contains structured error details. */ var ValidatorError = class extends Error {
|
|
1864
2383
|
constructor(errors) {
|
|
1865
|
-
super(`${errors[0].path ? errors[0].path + ": " : ""}${errors[0].message}`), _define_property$
|
|
2384
|
+
super(`${errors[0].path ? errors[0].path + ": " : ""}${errors[0].message}`), _define_property$6(this, "errors", void 0), _define_property$6(this, "name", void 0), this.errors = errors, this.name = "Validation Error";
|
|
1866
2385
|
}
|
|
1867
2386
|
};
|
|
1868
2387
|
|
|
@@ -1879,7 +2398,12 @@ const COMPLEX_KINDS = new Set([
|
|
|
1879
2398
|
/**
|
|
1880
2399
|
* Creates a minimal annotated type node from a type def and metadata map.
|
|
1881
2400
|
* Centralises the shape so callers never duplicate the boilerplate.
|
|
1882
|
-
|
|
2401
|
+
*
|
|
2402
|
+
* `metadata` is used as-is (the node keeps the caller's map, so later writes through it
|
|
2403
|
+
* apply). When omitted, the node gets a new, tracked metadata map. Compiled validator
|
|
2404
|
+
* checks only bind to nodes whose metadata is tracked; nodes built over a caller-supplied
|
|
2405
|
+
* `Map` are always validated by the interpreted validator.
|
|
2406
|
+
*/ function createAnnotatedTypeNode(type, metadata = new AnnotationMap(), opts) {
|
|
1883
2407
|
return {
|
|
1884
2408
|
__is_atscript_annotated_type: true,
|
|
1885
2409
|
type,
|
|
@@ -1890,6 +2414,26 @@ const COMPLEX_KINDS = new Set([
|
|
|
1890
2414
|
ref: opts?.ref
|
|
1891
2415
|
};
|
|
1892
2416
|
}
|
|
2417
|
+
/** A new tracked metadata map holding `entries` (see {@link createAnnotatedTypeNode}). */ function createMetadataMap(entries) {
|
|
2418
|
+
return new AnnotationMap(entries);
|
|
2419
|
+
}
|
|
2420
|
+
/**
|
|
2421
|
+
* The metadata map `defineAnnotatedType()` installs on `base`: `base.metadata` itself, except
|
|
2422
|
+
* that an absent map or an empty plain `Map` (what a generated class starts with:
|
|
2423
|
+
* `static metadata = new Map()`) is replaced by a new tracked map, so the node can bind to a
|
|
2424
|
+
* compiled validator check. A non-empty map or a `Map` subclass is always kept as-is (its
|
|
2425
|
+
* identity and overrides preserved; such a node stays interpreted).
|
|
2426
|
+
*/ function baseMetadata(metadata) {
|
|
2427
|
+
return !metadata || Object.getPrototypeOf(metadata) === Map.prototype && metadata.size === 0 ? createMetadataMap() : metadata;
|
|
2428
|
+
}
|
|
2429
|
+
/** A new object type def (its props map is tracked, see {@link createAnnotatedTypeNode}). */ function createObjectTypeDef(tags = /* @__PURE__ */ new Set(), propsPatterns = []) {
|
|
2430
|
+
return {
|
|
2431
|
+
kind: "object",
|
|
2432
|
+
props: new PropsMap(),
|
|
2433
|
+
propsPatterns,
|
|
2434
|
+
tags
|
|
2435
|
+
};
|
|
2436
|
+
}
|
|
1893
2437
|
/**
|
|
1894
2438
|
* Type Guard to check if a type is atscript-annotated
|
|
1895
2439
|
*/ function isAnnotatedType(type) {
|
|
@@ -1902,26 +2446,36 @@ const COMPLEX_KINDS = new Set([
|
|
|
1902
2446
|
if (!metadata) return;
|
|
1903
2447
|
if (asArray) if (metadata.has(key)) {
|
|
1904
2448
|
const a = metadata.get(key);
|
|
1905
|
-
if (Array.isArray(a))
|
|
1906
|
-
|
|
2449
|
+
if (Array.isArray(a)) {
|
|
2450
|
+
a.push(value);
|
|
2451
|
+
if (isValidatorKey(key)) touched(metadata);
|
|
2452
|
+
} else metadata.set(key, [a, value]);
|
|
1907
2453
|
} else metadata.set(key, [value]);
|
|
1908
2454
|
else metadata.set(key, value);
|
|
1909
2455
|
}
|
|
2456
|
+
/**
|
|
2457
|
+
* Clones a property's type tree in-place so mutations don't leak to shared refs.
|
|
2458
|
+
* Used by mutating annotate codegen when paths cross ref boundaries.
|
|
2459
|
+
*/ function cloneRefProp(parentType, propName) {
|
|
2460
|
+
if (parentType.kind !== "object") return;
|
|
2461
|
+
const objType = parentType;
|
|
2462
|
+
const existing = objType.props.get(propName);
|
|
2463
|
+
if (!existing) return;
|
|
2464
|
+
objType.props.set(propName, cloneNode(existing, cloneTypeDef(existing.type)));
|
|
2465
|
+
}
|
|
2466
|
+
/** A copy of `node` (own metadata map) over `type`. */ function cloneNode(node, type) {
|
|
2467
|
+
return createAnnotatedTypeNode(type, createMetadataMap(node.metadata), {
|
|
2468
|
+
id: node.id,
|
|
2469
|
+
optional: node.optional,
|
|
2470
|
+
ref: node.ref
|
|
2471
|
+
});
|
|
2472
|
+
}
|
|
1910
2473
|
function cloneTypeDef(type) {
|
|
1911
2474
|
if (type.kind === "object") {
|
|
1912
2475
|
const obj = type;
|
|
1913
|
-
const
|
|
1914
|
-
for (const [k, v] of obj.props) props.set(k,
|
|
1915
|
-
|
|
1916
|
-
optional: v.optional,
|
|
1917
|
-
ref: v.ref
|
|
1918
|
-
}));
|
|
1919
|
-
return {
|
|
1920
|
-
kind: "object",
|
|
1921
|
-
props,
|
|
1922
|
-
propsPatterns: [...obj.propsPatterns],
|
|
1923
|
-
tags: new Set(obj.tags)
|
|
1924
|
-
};
|
|
2476
|
+
const clone = createObjectTypeDef(new Set(obj.tags), [...obj.propsPatterns]);
|
|
2477
|
+
for (const [k, v] of obj.props) clone.props.set(k, cloneNode(v, v.type));
|
|
2478
|
+
return clone;
|
|
1925
2479
|
}
|
|
1926
2480
|
if (type.kind === "array") {
|
|
1927
2481
|
const arr = type;
|
|
@@ -1966,14 +2520,15 @@ function cloneTypeDef(type) {
|
|
|
1966
2520
|
const kind = _kind || "";
|
|
1967
2521
|
const baseProvided = base !== void 0;
|
|
1968
2522
|
const type = base?.type || {};
|
|
2523
|
+
touched(type);
|
|
1969
2524
|
type.kind = kind;
|
|
1970
2525
|
if (COMPLEX_KINDS.has(kind)) type.items = [];
|
|
1971
2526
|
if (kind === "object") {
|
|
1972
|
-
type.props =
|
|
2527
|
+
type.props = new PropsMap();
|
|
1973
2528
|
type.propsPatterns = [];
|
|
1974
2529
|
}
|
|
1975
2530
|
type.tags = /* @__PURE__ */ new Set();
|
|
1976
|
-
const metadata = base?.metadata
|
|
2531
|
+
const metadata = baseMetadata(base?.metadata);
|
|
1977
2532
|
const payload = {
|
|
1978
2533
|
__is_atscript_annotated_type: true,
|
|
1979
2534
|
metadata,
|
|
@@ -1991,18 +2546,22 @@ function cloneTypeDef(type) {
|
|
|
1991
2546
|
},
|
|
1992
2547
|
designType(value) {
|
|
1993
2548
|
this.$def.designType = value;
|
|
2549
|
+
touched(this.$def);
|
|
1994
2550
|
return this;
|
|
1995
2551
|
},
|
|
1996
2552
|
value(value) {
|
|
1997
2553
|
this.$def.value = value;
|
|
2554
|
+
touched(this.$def);
|
|
1998
2555
|
return this;
|
|
1999
2556
|
},
|
|
2000
2557
|
of(value) {
|
|
2001
2558
|
this.$def.of = value;
|
|
2559
|
+
touched(this.$def);
|
|
2002
2560
|
return this;
|
|
2003
2561
|
},
|
|
2004
2562
|
item(value) {
|
|
2005
2563
|
this.$def.items.push(value);
|
|
2564
|
+
touched(this.$def);
|
|
2006
2565
|
return this;
|
|
2007
2566
|
},
|
|
2008
2567
|
prop(name, value) {
|
|
@@ -2014,10 +2573,12 @@ function cloneTypeDef(type) {
|
|
|
2014
2573
|
pattern,
|
|
2015
2574
|
def
|
|
2016
2575
|
});
|
|
2576
|
+
touched(this.$def);
|
|
2017
2577
|
return this;
|
|
2018
2578
|
},
|
|
2019
2579
|
optional(value = true) {
|
|
2020
2580
|
this.$type.optional = value;
|
|
2581
|
+
touched(this.$type);
|
|
2021
2582
|
return this;
|
|
2022
2583
|
},
|
|
2023
2584
|
copyMetadata(fromMetadata, ignore) {
|
|
@@ -2026,6 +2587,7 @@ function cloneTypeDef(type) {
|
|
|
2026
2587
|
},
|
|
2027
2588
|
refTo(type, chain) {
|
|
2028
2589
|
const node = this.$type;
|
|
2590
|
+
touched(node);
|
|
2029
2591
|
const field = chain ? chain.join(".") : "";
|
|
2030
2592
|
if (isAnnotatedType(type)) {
|
|
2031
2593
|
let newBase = type;
|
|
@@ -2283,18 +2845,18 @@ function cloneTypeDef(type) {
|
|
|
2283
2845
|
}
|
|
2284
2846
|
/** `@expect.minLength` / `maxLength` → `minItems` / `maxItems` on an array schema. */ function applyArrayConstraints(schema, meta) {
|
|
2285
2847
|
const minLength = meta.get("expect.minLength");
|
|
2286
|
-
if (minLength !== void 0) schema.minItems =
|
|
2848
|
+
if (minLength !== void 0) schema.minItems = boundOf(minLength, "length");
|
|
2287
2849
|
const maxLength = meta.get("expect.maxLength");
|
|
2288
|
-
if (maxLength !== void 0) schema.maxItems =
|
|
2850
|
+
if (maxLength !== void 0) schema.maxItems = boundOf(maxLength, "length");
|
|
2289
2851
|
}
|
|
2290
2852
|
/** String / number constraints on a scalar schema whose `type` is already set. */ function applyScalarConstraints(schema, meta) {
|
|
2291
2853
|
if (schema.type === "number" && meta.get("expect.int")) schema.type = "integer";
|
|
2292
2854
|
if (schema.type === "string") {
|
|
2293
2855
|
if (meta.get("meta.required")) schema.minLength = 1;
|
|
2294
2856
|
const minLength = meta.get("expect.minLength");
|
|
2295
|
-
if (minLength !== void 0) schema.minLength =
|
|
2857
|
+
if (minLength !== void 0) schema.minLength = boundOf(minLength, "length");
|
|
2296
2858
|
const maxLength = meta.get("expect.maxLength");
|
|
2297
|
-
if (maxLength !== void 0) schema.maxLength =
|
|
2859
|
+
if (maxLength !== void 0) schema.maxLength = boundOf(maxLength, "length");
|
|
2298
2860
|
const patterns = meta.get("expect.pattern");
|
|
2299
2861
|
if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
|
|
2300
2862
|
else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
|
|
@@ -2310,9 +2872,6 @@ function cloneTypeDef(type) {
|
|
|
2310
2872
|
if (exMax !== void 0) schema.exclusiveMaximum = boundOf(exMax, "exclusiveMaxValue");
|
|
2311
2873
|
}
|
|
2312
2874
|
}
|
|
2313
|
-
/** A number-bound annotation's value: the bare number, or its `field` in the object form. */ function boundOf(m, field) {
|
|
2314
|
-
return typeof m === "number" ? m : m[field];
|
|
2315
|
-
}
|
|
2316
2875
|
const LOWER_BOUNDS = new Set([
|
|
2317
2876
|
"minimum",
|
|
2318
2877
|
"exclusiveMinimum",
|
|
@@ -2355,6 +2914,662 @@ const UPPER_BOUNDS = new Set([
|
|
|
2355
2914
|
}
|
|
2356
2915
|
}
|
|
2357
2916
|
|
|
2917
|
+
//#endregion
|
|
2918
|
+
//#region packages/typescript/src/runtime/compiled-check/build-layout.ts
|
|
2919
|
+
/**
|
|
2920
|
+
* Build-time layout derivation (renderer and tests; not part of the runtime bundle).
|
|
2921
|
+
*/
|
|
2922
|
+
/**
|
|
2923
|
+
* Derives the layout of `root` (see layout.ts). Shared / recursive type defs are recorded
|
|
2924
|
+
* once and referenced by index.
|
|
2925
|
+
*/ function buildLayout(root, reader, classify) {
|
|
2926
|
+
const defs = [];
|
|
2927
|
+
const index = /* @__PURE__ */ new Map();
|
|
2928
|
+
let slots = 0;
|
|
2929
|
+
function describeDef(type) {
|
|
2930
|
+
switch (type.kind) {
|
|
2931
|
+
case "": return type.value === void 0 ? [D_FINAL, type.designType] : [
|
|
2932
|
+
D_FINAL,
|
|
2933
|
+
type.designType,
|
|
2934
|
+
type.value
|
|
2935
|
+
];
|
|
2936
|
+
case "object": {
|
|
2937
|
+
if (type.propsPatterns.length > 0) return [D_OBJECT_PATTERNS];
|
|
2938
|
+
const props = [...type.props];
|
|
2939
|
+
return [
|
|
2940
|
+
D_OBJECT,
|
|
2941
|
+
props.map(([key]) => key),
|
|
2942
|
+
props.map(([, prop]) => site(prop))
|
|
2943
|
+
];
|
|
2944
|
+
}
|
|
2945
|
+
case "array": return [D_ARRAY, site(type.of)];
|
|
2946
|
+
case "union":
|
|
2947
|
+
case "intersection":
|
|
2948
|
+
case "tuple": return [ITEMS_KIND_CODES[type.kind], type.items.map(site)];
|
|
2949
|
+
default: return [D_UNKNOWN, String(type.kind)];
|
|
2950
|
+
}
|
|
2951
|
+
}
|
|
2952
|
+
function site(node) {
|
|
2953
|
+
const type = node.type;
|
|
2954
|
+
const marker = classify?.(type);
|
|
2955
|
+
const optional = !marker || marker === "ext" ? node.optional ? 1 : 0 : 0;
|
|
2956
|
+
let d;
|
|
2957
|
+
let keys = {
|
|
2958
|
+
mask: 0,
|
|
2959
|
+
shape: ""
|
|
2960
|
+
};
|
|
2961
|
+
if (marker === "interp") d = INTERP;
|
|
2962
|
+
else if (marker === "ext") {
|
|
2963
|
+
d = EXT;
|
|
2964
|
+
keys = readKeys(node.metadata, siteBits(void 0, optional === 1), reader);
|
|
2965
|
+
} else {
|
|
2966
|
+
d = index.get(type) ?? -1;
|
|
2967
|
+
if (d < 0) {
|
|
2968
|
+
d = defs.length;
|
|
2969
|
+
defs.push([]);
|
|
2970
|
+
index.set(type, d);
|
|
2971
|
+
defs[d] = describeDef(type);
|
|
2972
|
+
}
|
|
2973
|
+
keys = readKeys(node.metadata, siteBits(type, optional === 1), reader);
|
|
2974
|
+
}
|
|
2975
|
+
const result = [
|
|
2976
|
+
optional,
|
|
2977
|
+
keys.mask,
|
|
2978
|
+
d,
|
|
2979
|
+
keys.shape,
|
|
2980
|
+
slots
|
|
2981
|
+
];
|
|
2982
|
+
slots += siteSlotCount(result);
|
|
2983
|
+
return result;
|
|
2984
|
+
}
|
|
2985
|
+
return [
|
|
2986
|
+
defs,
|
|
2987
|
+
site(root),
|
|
2988
|
+
slots
|
|
2989
|
+
];
|
|
2990
|
+
}
|
|
2991
|
+
/** JS source of a layout literal (plain arrays, strings and numbers incl. NaN / ±Infinity / -0). */ function layoutSource(value) {
|
|
2992
|
+
if (Array.isArray(value)) return `[${value.map(layoutSource).join(",")}]`;
|
|
2993
|
+
return literalSource(value);
|
|
2994
|
+
}
|
|
2995
|
+
/** JS source of a primitive literal. */ function literalSource(value) {
|
|
2996
|
+
if (typeof value === "number") return Object.is(value, -0) ? "-0" : String(value);
|
|
2997
|
+
if (typeof value === "string") return JSON.stringify(value).replaceAll("\u2028", "\\u2028").replaceAll("\u2029", "\\u2029");
|
|
2998
|
+
if (value === void 0) return "undefined";
|
|
2999
|
+
return JSON.stringify(value);
|
|
3000
|
+
}
|
|
3001
|
+
|
|
3002
|
+
//#endregion
|
|
3003
|
+
//#region packages/typescript/src/runtime/compiled-check/emit.ts
|
|
3004
|
+
/**
|
|
3005
|
+
* Emits the JS source of a compiled validator check factory from its layout.
|
|
3006
|
+
*
|
|
3007
|
+
* Build-time only (the renderer, and test code): the runtime never generates code. The
|
|
3008
|
+
* emitted factory `(K, H) => check` mirrors the interpreted pre-check
|
|
3009
|
+
* (validator-fast-check.ts) node for node — the same checks with the same PASS (1) /
|
|
3010
|
+
* FAIL (0) / BAIL (2) contract — specialised to the layout: one function per object type
|
|
3011
|
+
* def and per def that closes a reference cycle, everything else inlined.
|
|
3012
|
+
*
|
|
3013
|
+
* Generated code conventions:
|
|
3014
|
+
* - Every function is `(v, f, o)`: the value, the flags (TOP, IN_UNION and option bits —
|
|
3015
|
+
* see `optionFlags`) and the validator options (passed on to interpreted sub-checks).
|
|
3016
|
+
* - A FAIL statement always leaves the current check: `return 0` outside unions; inside a
|
|
3017
|
+
* union (where failures must visit everything the walk would) it records the failure and
|
|
3018
|
+
* jumps past the failed node. `q` (`IN_UNION` unset) picks between the two when only the
|
|
3019
|
+
* runtime flags tell. BAIL is always `return 2`.
|
|
3020
|
+
* - Annotation values are closure constants `k<slot>` read from `K` once per bind.
|
|
3021
|
+
*/ function _define_property$5(obj, key, value) {
|
|
3022
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
3023
|
+
value,
|
|
3024
|
+
enumerable: true,
|
|
3025
|
+
configurable: true,
|
|
3026
|
+
writable: true
|
|
3027
|
+
});
|
|
3028
|
+
else obj[key] = value;
|
|
3029
|
+
return obj;
|
|
3030
|
+
}
|
|
3031
|
+
const ROOT_CTX = {
|
|
3032
|
+
fail: "return 0",
|
|
3033
|
+
inUnionFail: "return 0",
|
|
3034
|
+
union: false,
|
|
3035
|
+
clearTop: false
|
|
3036
|
+
};
|
|
3037
|
+
/** Factory-level helpers (see `TCompiledHelpers`), in declaration order. */ const HELPERS = [
|
|
3038
|
+
["pk", /\bpk\./],
|
|
3039
|
+
["ho", /\bho\.call\(/],
|
|
3040
|
+
["dec", /\bdec\.test\(/],
|
|
3041
|
+
["rn", /\brn\(/]
|
|
3042
|
+
];
|
|
3043
|
+
/** Source of the check factory `(K, H) => check` of `layout`. */ function emitCheckFactory(layout) {
|
|
3044
|
+
return new CheckEmitter(layout).emit();
|
|
3045
|
+
}
|
|
3046
|
+
let CheckEmitter = class CheckEmitter {
|
|
3047
|
+
emit() {
|
|
3048
|
+
const body = [];
|
|
3049
|
+
for (let d = 0; d < this.defs.length; d++) if (this.fn[d]) body.push(...this.defFunction(d));
|
|
3050
|
+
const root = this.layout[1];
|
|
3051
|
+
const rootDef = root[2] >= 0 ? this.defs[root[2]][0] : -1;
|
|
3052
|
+
if (root[0] === 0 && this.fn[root[2]] && (rootDef === D_OBJECT || rootDef === D_OBJECT_PATTERNS || rootDef === D_TUPLE || rootDef === D_INTERSECTION)) body.push(`return d${root[2]}`);
|
|
3053
|
+
else {
|
|
3054
|
+
body.push(...this.fnLines("r", () => [...this.site(root, "v", ROOT_CTX), "return 1"]));
|
|
3055
|
+
body.push("return r");
|
|
3056
|
+
}
|
|
3057
|
+
const code = body.join("\n");
|
|
3058
|
+
const head = [];
|
|
3059
|
+
const helpers = HELPERS.filter(([, use]) => use.test(code)).map(([h]) => `${h} = H.${h}`);
|
|
3060
|
+
if (helpers.length > 0) head.push(`const ${helpers.join(", ")}`);
|
|
3061
|
+
const slots = [...new Set(Array.from(code.matchAll(/\bk(\d+)\b/g), (m) => Number(m[1])))].filter((k) => k < this.layout[2]).sort((a, b) => a - b);
|
|
3062
|
+
for (let i = 0; i < slots.length; i += 16) head.push(`const ${slots.slice(i, i + 16).map((k) => `k${k} = K[${k}]`).join(", ")}`);
|
|
3063
|
+
return [
|
|
3064
|
+
"(K, H) => {",
|
|
3065
|
+
...indent([...head, ...body]),
|
|
3066
|
+
"}"
|
|
3067
|
+
].join("\n");
|
|
3068
|
+
}
|
|
3069
|
+
/**
|
|
3070
|
+
* Object defs get a function each; so does every def a reference cycle passes through
|
|
3071
|
+
* (each back-edge target of a DFS), which keeps inlining finite.
|
|
3072
|
+
*/ functionDefs() {
|
|
3073
|
+
const defs = this.defs;
|
|
3074
|
+
const fn = defs.map((def) => def[0] === D_OBJECT || def[0] === D_OBJECT_PATTERNS);
|
|
3075
|
+
const state = Array.from({ length: defs.length }).fill(0);
|
|
3076
|
+
const dfs = (d) => {
|
|
3077
|
+
state[d] = 1;
|
|
3078
|
+
for (const site of childSites(defs[d])) {
|
|
3079
|
+
const c = site[2];
|
|
3080
|
+
if (c < 0) continue;
|
|
3081
|
+
if (state[c] === 1) fn[c] = true;
|
|
3082
|
+
else if (state[c] === 0) dfs(c);
|
|
3083
|
+
}
|
|
3084
|
+
state[d] = 2;
|
|
3085
|
+
};
|
|
3086
|
+
for (let d = 0; d < defs.length; d++) if (state[d] === 0) dfs(d);
|
|
3087
|
+
return fn;
|
|
3088
|
+
}
|
|
3089
|
+
next(prefix) {
|
|
3090
|
+
return `${prefix}${this.uid++}`;
|
|
3091
|
+
}
|
|
3092
|
+
/** A check function `name(v, f, o)` declaring the `q` / `t` locals its body uses. */ fnLines(name, body) {
|
|
3093
|
+
const lines = body();
|
|
3094
|
+
const code = lines.join("\n");
|
|
3095
|
+
const locals = [];
|
|
3096
|
+
if (code.includes("if (q) return 0")) locals.push(`const q = (f & ${F_IN_UNION}) === 0`);
|
|
3097
|
+
if (/\bt = /.test(code)) locals.push("let t");
|
|
3098
|
+
return [
|
|
3099
|
+
`function ${name}(v, f, o) {`,
|
|
3100
|
+
...indent([...locals, ...lines]),
|
|
3101
|
+
"}"
|
|
3102
|
+
];
|
|
3103
|
+
}
|
|
3104
|
+
defFunction(d) {
|
|
3105
|
+
const def = this.defs[d];
|
|
3106
|
+
return this.fnLines(`d${d}`, () => {
|
|
3107
|
+
switch (def[0]) {
|
|
3108
|
+
case D_OBJECT: return this.objectBody(def);
|
|
3109
|
+
case D_OBJECT_PATTERNS: return [`if (${notObject("v")}) return 0`, "return 2"];
|
|
3110
|
+
case D_ARRAY: return [...this.items(def[1], "v", ROOT_CTX), "return 1"];
|
|
3111
|
+
case D_UNION: return [...this.unionBranches(def[1], "v", false, (item) => [`return ${choose(this.literalExpr(item), "3", "1")}`]), "return 0"];
|
|
3112
|
+
case D_TUPLE: return [...this.tuple(def[1], "v", ROOT_CTX), "return 1"];
|
|
3113
|
+
case D_INTERSECTION: return [...def[1].flatMap((item) => this.site(item, "v", ROOT_CTX)), "return 1"];
|
|
3114
|
+
default: return ["return 2"];
|
|
3115
|
+
}
|
|
3116
|
+
});
|
|
3117
|
+
}
|
|
3118
|
+
/** Mirrors `fastCheckObject` (+ `pendStrips` / `hasUnknownKey`). */ objectBody(def) {
|
|
3119
|
+
const sites = def[2];
|
|
3120
|
+
const props = def[1].map((key, i) => ({
|
|
3121
|
+
key,
|
|
3122
|
+
site: sites[i]
|
|
3123
|
+
})).filter((p) => !this.isPhantom(p.site));
|
|
3124
|
+
const lines = [`if (${notObject("v")}) return 0`, "if (pk.length !== 0) return 2"];
|
|
3125
|
+
if (props.some((p) => p.site[0] === 0 || p.site[2] === INTERP)) {
|
|
3126
|
+
const top = F_PARTIAL_TOP | F_TOP;
|
|
3127
|
+
lines.push(`const sk = (f & ${F_PARTIAL_DEEP}) !== 0 || (f & ${top}) === ${top}`);
|
|
3128
|
+
}
|
|
3129
|
+
if (props.length > 0) lines.push("let r = 1");
|
|
3130
|
+
for (const { key, site } of props) {
|
|
3131
|
+
const p = this.next("P");
|
|
3132
|
+
const x = this.next("x");
|
|
3133
|
+
const access = validIdentifier.test(key) ? `v.${key}` : `v[${literalSource(key)}]`;
|
|
3134
|
+
lines.push(`${p}: {`, ` const ${x} = ${access}`);
|
|
3135
|
+
if (site[0] === 0 || site[2] === INTERP) lines.push(` if (${x} === undefined && sk) break ${p}`);
|
|
3136
|
+
const ctx = recordingCtx(`r = 0; break ${p}`, false, true);
|
|
3137
|
+
lines.push(...indent(this.site(site, x, ctx)), "}");
|
|
3138
|
+
}
|
|
3139
|
+
if (props.length > 0) lines.push("if (r === 0) return 0");
|
|
3140
|
+
const known = props.map((p) => p.key);
|
|
3141
|
+
const onUnknown = `{ if ((f & ${F_ERROR}) !== 0) return 0; if ((f & ${F_TOP}) === 0) return 2; pk.push(k) }`;
|
|
3142
|
+
const scan = known.length === 0 ? [`for (const k in v) if (ho.call(v, k)) ${onUnknown}`] : [
|
|
3143
|
+
"for (const k in v) {",
|
|
3144
|
+
" switch (k) {",
|
|
3145
|
+
...known.map((key) => ` case ${literalSource(key)}:`),
|
|
3146
|
+
" break",
|
|
3147
|
+
" default:",
|
|
3148
|
+
` if (ho.call(v, k)) ${onUnknown}`,
|
|
3149
|
+
" }",
|
|
3150
|
+
"}"
|
|
3151
|
+
];
|
|
3152
|
+
lines.push(`if ((f & ${F_ERROR | F_STRIP}) !== 0) {`, ...indent(scan), "}", "return 1");
|
|
3153
|
+
return lines;
|
|
3154
|
+
}
|
|
3155
|
+
isPhantom(site) {
|
|
3156
|
+
const d = site[2];
|
|
3157
|
+
return d >= 0 && this.defs[d][0] === D_FINAL && this.defs[d][1] === "phantom";
|
|
3158
|
+
}
|
|
3159
|
+
isLiteralDef(d) {
|
|
3160
|
+
return d >= 0 && this.defs[d][0] === D_FINAL && this.defs[d].length > 2;
|
|
3161
|
+
}
|
|
3162
|
+
/** A required `never` union branch: it can never match (skipped). */ isNever(site) {
|
|
3163
|
+
const d = site[2];
|
|
3164
|
+
return site[0] === 0 && d >= 0 && this.defs[d][0] === D_FINAL && this.defs[d][1] === "never" && !this.isLiteralDef(d);
|
|
3165
|
+
}
|
|
3166
|
+
/** Expression telling whether the site's type is a literal (`'true'` / `'false'` when known). */ literalExpr(site) {
|
|
3167
|
+
const [, , d, , slot] = site;
|
|
3168
|
+
if (d >= 0) return this.isLiteralDef(d) ? "true" : "false";
|
|
3169
|
+
return `k${d === INTERP ? slot + 1 : slot + (hasRejectSlot(site) ? 1 : 0) + 1}`;
|
|
3170
|
+
}
|
|
3171
|
+
/** Mirrors `fastCheck` for one node holding `x`. */ site(site, x, ctx) {
|
|
3172
|
+
const [optional, mask, d, shape, slot] = site;
|
|
3173
|
+
if (d === INTERP) return this.call(`k${slot}`, x, ctx);
|
|
3174
|
+
let at = slot;
|
|
3175
|
+
const rej = hasRejectSlot(site) ? at++ : -1;
|
|
3176
|
+
const body = d === EXT ? this.call(`k${at}`, x, ctx) : this.def(d, mask, shape, at, x, ctx);
|
|
3177
|
+
if (!optional) return body;
|
|
3178
|
+
if (rej < 0) return body.length > 0 ? [
|
|
3179
|
+
`if (${x} !== undefined && ${x} !== null) {`,
|
|
3180
|
+
...indent(body),
|
|
3181
|
+
"}"
|
|
3182
|
+
] : [];
|
|
3183
|
+
const lines = [`if (${x} === undefined || ${x} === null) {`, ` if (${x} === null && rn(k${rej})) ${ctx.fail}`];
|
|
3184
|
+
if (body.length > 0) lines.push("} else {", ...indent(body));
|
|
3185
|
+
lines.push("}");
|
|
3186
|
+
return lines;
|
|
3187
|
+
}
|
|
3188
|
+
/** Calls a check function returning 0 / 1 / 2. */ call(fn, x, ctx) {
|
|
3189
|
+
this.calls++;
|
|
3190
|
+
return [`t = ${fn}(${x}, ${flags(ctx)}, o)`, `if (t !== 1) { if (t === 2) return 2; ${unblock(ctx.fail)} }`];
|
|
3191
|
+
}
|
|
3192
|
+
def(d, mask, shape, at, x, ctx) {
|
|
3193
|
+
const def = this.defs[d];
|
|
3194
|
+
const kind = def[0];
|
|
3195
|
+
if (this.fn[d] && kind !== D_ARRAY && kind !== D_UNION) return this.call(`d${d}`, x, ctx);
|
|
3196
|
+
switch (kind) {
|
|
3197
|
+
case D_FINAL: return this.final(def, mask, shape, at, x, ctx.fail);
|
|
3198
|
+
case D_ARRAY: return [
|
|
3199
|
+
`if (!Array.isArray(${x})) ${ctx.fail}`,
|
|
3200
|
+
...this.arrayConstraints(mask, keySlots(mask, shape, at), x, ctx.fail),
|
|
3201
|
+
...this.fn[d] ? this.call(`d${d}`, x, ctx) : this.items(def[1], x, ctx)
|
|
3202
|
+
];
|
|
3203
|
+
case D_UNION: {
|
|
3204
|
+
const constraints = this.unionConstraints(mask, shape, at, x, ctx.fail);
|
|
3205
|
+
if (!this.fn[d]) return this.union(def[1], {
|
|
3206
|
+
mask,
|
|
3207
|
+
shape,
|
|
3208
|
+
at
|
|
3209
|
+
}, constraints, x, ctx);
|
|
3210
|
+
this.calls++;
|
|
3211
|
+
const lines = [
|
|
3212
|
+
`t = d${d}(${x}, ${flags(ctx)}, o)`,
|
|
3213
|
+
"if (t === 2) return 2",
|
|
3214
|
+
`if (t === 0) ${ctx.fail}`
|
|
3215
|
+
];
|
|
3216
|
+
if (constraints.length > 0) lines.push("if (t === 1) {", ...indent(constraints), "}");
|
|
3217
|
+
return lines;
|
|
3218
|
+
}
|
|
3219
|
+
case D_INTERSECTION: return def[1].flatMap((item) => this.site(item, x, ctx));
|
|
3220
|
+
case D_TUPLE: return this.tuple(def[1], x, ctx);
|
|
3221
|
+
default: return ["return 2"];
|
|
3222
|
+
}
|
|
3223
|
+
}
|
|
3224
|
+
/** Mirrors `fastCheckFinal` + the string / number / boolean constraints. */ final(def, mask, shape, at, x, fail) {
|
|
3225
|
+
const designType = def[1];
|
|
3226
|
+
if (designType === "phantom") return [];
|
|
3227
|
+
if (def.length > 2) return [`if (${x} !== ${literalSource(def[2])}) ${fail}`];
|
|
3228
|
+
const ks = keySlots(mask, shape, at);
|
|
3229
|
+
switch (designType) {
|
|
3230
|
+
case "string": return this.stringChecks(mask, shape, ks, x, fail, [`typeof ${x} !== "string"`]);
|
|
3231
|
+
case "number": return this.numberChecks(mask, ks, x, fail, [`typeof ${x} !== "number"`, `!Number.isFinite(${x})`]);
|
|
3232
|
+
case "boolean": {
|
|
3233
|
+
const conds = [`typeof ${x} !== "boolean"`];
|
|
3234
|
+
if (mask & B_REQUIRED) conds.push(`${x} !== true`);
|
|
3235
|
+
return [`if (${conds.join(" || ")}) ${fail}`];
|
|
3236
|
+
}
|
|
3237
|
+
case "any": return [];
|
|
3238
|
+
case "never": return [fail];
|
|
3239
|
+
case "undefined": return [`if (${x} !== undefined) ${fail}`];
|
|
3240
|
+
case "null": return [`if (${x} !== null) ${fail}`];
|
|
3241
|
+
case "decimal": return [`if (typeof ${x} !== "string" || !dec.test(${x})) ${fail}`];
|
|
3242
|
+
default: return ["return 2"];
|
|
3243
|
+
}
|
|
3244
|
+
}
|
|
3245
|
+
stringChecks(mask, shape, ks, x, fail, conds) {
|
|
3246
|
+
if (mask & B_REQUIRED) conds.push(`${x}.trim().length === 0`);
|
|
3247
|
+
if (ks.minLength >= 0) conds.push(`${x}.length < k${ks.minLength}`);
|
|
3248
|
+
if (ks.maxLength >= 0) conds.push(`${x}.length > k${ks.maxLength}`);
|
|
3249
|
+
const lines = conds.length > 0 ? [`if (${conds.join(" || ")}) ${fail}`] : [];
|
|
3250
|
+
let p = 0;
|
|
3251
|
+
for (const c of shape) if (c === "n") lines.push(`if (!k${ks.patterns[p++]}.test(${x})) ${fail}`);
|
|
3252
|
+
else if (c === "s") {
|
|
3253
|
+
lines.push("return 2");
|
|
3254
|
+
break;
|
|
3255
|
+
}
|
|
3256
|
+
return lines;
|
|
3257
|
+
}
|
|
3258
|
+
numberChecks(mask, ks, x, fail, conds) {
|
|
3259
|
+
if (mask & B_INT) conds.push(`${x} % 1 !== 0`);
|
|
3260
|
+
if (ks.min >= 0) conds.push(`${x} < k${ks.min}`);
|
|
3261
|
+
if (ks.max >= 0) conds.push(`${x} > k${ks.max}`);
|
|
3262
|
+
if (ks.exMin >= 0) conds.push(`${x} <= k${ks.exMin}`);
|
|
3263
|
+
if (ks.exMax >= 0) conds.push(`${x} >= k${ks.exMax}`);
|
|
3264
|
+
return conds.length > 0 ? [`if (${conds.join(" || ")}) ${fail}`] : [];
|
|
3265
|
+
}
|
|
3266
|
+
/** Array length constraints; `uniqueItems` (duplicate detection) is left to the walk. */ arrayConstraints(mask, ks, x, fail) {
|
|
3267
|
+
const conds = [];
|
|
3268
|
+
if (ks.minLength >= 0) conds.push(`${x}.length < k${ks.minLength}`);
|
|
3269
|
+
if (ks.maxLength >= 0) conds.push(`${x}.length > k${ks.maxLength}`);
|
|
3270
|
+
const lines = conds.length > 0 ? [`if (${conds.join(" || ")}) ${fail}`] : [];
|
|
3271
|
+
if (mask & B_UNIQUE) lines.push("return 2");
|
|
3272
|
+
return lines;
|
|
3273
|
+
}
|
|
3274
|
+
/** Mirrors `fastCheckUnionConstraints` (the caller skips literal branches). */ unionConstraints(mask, shape, at, x, fail) {
|
|
3275
|
+
const ks = keySlots(mask, shape, at);
|
|
3276
|
+
const branches = [];
|
|
3277
|
+
if (mask & STRING_BITS) branches.push([`typeof ${x} === "string"`, this.stringChecks(mask, shape, ks, x, fail, [])]);
|
|
3278
|
+
if (mask & NUMBER_BITS) branches.push([`typeof ${x} === "number"`, this.numberChecks(mask, ks, x, fail, [])]);
|
|
3279
|
+
if (mask & B_REQUIRED) branches.push([`typeof ${x} === "boolean"`, [`if (${x} !== true) ${fail}`]]);
|
|
3280
|
+
if (mask & ARRAY_BITS) branches.push([`Array.isArray(${x})`, this.arrayConstraints(mask, ks, x, fail)]);
|
|
3281
|
+
const lines = [];
|
|
3282
|
+
branches.forEach(([cond, body], i) => {
|
|
3283
|
+
lines.push(`${i === 0 ? "if" : "} else if"} (${cond}) {`, ...indent(body));
|
|
3284
|
+
});
|
|
3285
|
+
if (lines.length > 0) lines.push("}");
|
|
3286
|
+
return lines;
|
|
3287
|
+
}
|
|
3288
|
+
/**
|
|
3289
|
+
* The union-level constraints after a non-literal branch passed. A required (non-optional)
|
|
3290
|
+
* branch of a known primitive / array type fixes `typeof`: only that part applies.
|
|
3291
|
+
*/ branchConstraints(item, { mask, shape, at }, constraints, x, fail) {
|
|
3292
|
+
const [optional, , d] = item;
|
|
3293
|
+
const def = this.defs[d];
|
|
3294
|
+
if (optional) return constraints;
|
|
3295
|
+
const ks = keySlots(mask, shape, at);
|
|
3296
|
+
if (def[0] === D_ARRAY) return this.arrayConstraints(mask, ks, x, fail);
|
|
3297
|
+
if (def[0] !== D_FINAL) return constraints;
|
|
3298
|
+
switch (def[1]) {
|
|
3299
|
+
case "string": return this.stringChecks(mask, shape, ks, x, fail, []);
|
|
3300
|
+
case "number": return this.numberChecks(mask, ks, x, fail, []);
|
|
3301
|
+
case "boolean": return mask & B_REQUIRED ? [`if (${x} !== true) ${fail}`] : [];
|
|
3302
|
+
case "null":
|
|
3303
|
+
case "undefined": return [];
|
|
3304
|
+
default: return constraints;
|
|
3305
|
+
}
|
|
3306
|
+
}
|
|
3307
|
+
/**
|
|
3308
|
+
* Mirrors the `fastCheck` union case: branches in declared order, each a labelled block a
|
|
3309
|
+
* FAIL breaks out of; `onMatch` ends a passing branch.
|
|
3310
|
+
*/ unionBranches(items, x, clearTop, onMatch) {
|
|
3311
|
+
const lines = [];
|
|
3312
|
+
for (const item of items) {
|
|
3313
|
+
if (this.isNever(item)) continue;
|
|
3314
|
+
const b = this.next("B");
|
|
3315
|
+
const calls = this.calls;
|
|
3316
|
+
const fail = `break ${b}`;
|
|
3317
|
+
const body = this.site(item, x, {
|
|
3318
|
+
fail,
|
|
3319
|
+
inUnionFail: fail,
|
|
3320
|
+
union: true,
|
|
3321
|
+
clearTop
|
|
3322
|
+
});
|
|
3323
|
+
lines.push(`${b}: {`, ...indent([...body, ...onMatch(item)]), "}");
|
|
3324
|
+
if (this.calls !== calls) lines.push("if (pk.length !== 0) return 2");
|
|
3325
|
+
}
|
|
3326
|
+
return lines;
|
|
3327
|
+
}
|
|
3328
|
+
/** An inline union: the first matching branch passes after the union-level constraints. */ union(items, keys, constraints, x, ctx) {
|
|
3329
|
+
const u = this.next("U");
|
|
3330
|
+
const branches = this.unionBranches(items, x, ctx.clearTop, (item) => {
|
|
3331
|
+
const lines = [];
|
|
3332
|
+
const literal = constraints.length > 0 ? this.literalExpr(item) : "true";
|
|
3333
|
+
if (literal === "false") lines.push(...this.branchConstraints(item, keys, constraints, x, ctx.fail));
|
|
3334
|
+
else if (literal !== "true") lines.push(`if (!${literal}) {`, ...indent(constraints), "}");
|
|
3335
|
+
lines.push(`break ${u}`);
|
|
3336
|
+
return lines;
|
|
3337
|
+
});
|
|
3338
|
+
return [
|
|
3339
|
+
`${u}: {`,
|
|
3340
|
+
...indent([...branches, ctx.fail]),
|
|
3341
|
+
"}"
|
|
3342
|
+
];
|
|
3343
|
+
}
|
|
3344
|
+
/** Mirrors `checkItems`: every item is visited inside a union (BAIL beats FAIL). */ items(of, x, ctx) {
|
|
3345
|
+
const y = this.next("y");
|
|
3346
|
+
const a = this.next("a");
|
|
3347
|
+
const body = this.site(of, y, recordingCtx(`${a} = 0; continue`, ctx.union, true));
|
|
3348
|
+
if (body.length === 0) return [`for (const ${y} of ${x}) {}`];
|
|
3349
|
+
return [
|
|
3350
|
+
`let ${a} = 1`,
|
|
3351
|
+
`for (const ${y} of ${x}) {`,
|
|
3352
|
+
...indent(body),
|
|
3353
|
+
"}",
|
|
3354
|
+
`if (${a} === 0) ${ctx.inUnionFail}`
|
|
3355
|
+
];
|
|
3356
|
+
}
|
|
3357
|
+
/** Mirrors the `fastCheck` tuple case: stops at the first non-passing item. */ tuple(items, x, ctx) {
|
|
3358
|
+
const lines = [`if (!Array.isArray(${x}) || ${x}.length !== ${items.length}) ${ctx.fail}`];
|
|
3359
|
+
items.forEach((item, i) => {
|
|
3360
|
+
const e = this.next("e");
|
|
3361
|
+
lines.push(`const ${e} = ${x}[${i}]`, ...this.site(item, e, {
|
|
3362
|
+
...ctx,
|
|
3363
|
+
clearTop: true
|
|
3364
|
+
}));
|
|
3365
|
+
});
|
|
3366
|
+
return lines;
|
|
3367
|
+
}
|
|
3368
|
+
constructor(layout) {
|
|
3369
|
+
_define_property$5(this, "layout", void 0);
|
|
3370
|
+
_define_property$5(this, "defs", void 0);
|
|
3371
|
+
/** Def index → emitted as its own function. */ _define_property$5(this, "fn", void 0);
|
|
3372
|
+
_define_property$5(this, "uid", void 0);
|
|
3373
|
+
/** Calls emitted so far (a union branch without calls cannot record strips). */ _define_property$5(this, "calls", void 0);
|
|
3374
|
+
this.layout = layout;
|
|
3375
|
+
this.uid = 0;
|
|
3376
|
+
this.calls = 0;
|
|
3377
|
+
this.defs = layout[0];
|
|
3378
|
+
this.fn = this.functionDefs();
|
|
3379
|
+
}
|
|
3380
|
+
};
|
|
3381
|
+
/**
|
|
3382
|
+
* Context of a node whose failure is recorded (`record`: e.g. `r = 0; break P1`) when
|
|
3383
|
+
* inside a union and leaves the check otherwise.
|
|
3384
|
+
*/ function recordingCtx(record, union, clearTop) {
|
|
3385
|
+
const inUnionFail = `{ ${record} }`;
|
|
3386
|
+
return {
|
|
3387
|
+
fail: union ? inUnionFail : `{ if (q) return 0; ${record} }`,
|
|
3388
|
+
inUnionFail,
|
|
3389
|
+
union,
|
|
3390
|
+
clearTop
|
|
3391
|
+
};
|
|
3392
|
+
}
|
|
3393
|
+
/** Flags passed to a callee. */ function flags(ctx) {
|
|
3394
|
+
if (ctx.clearTop) return ctx.union ? `(f & ${~F_TOP}) | ${F_IN_UNION}` : `f & ${~F_TOP}`;
|
|
3395
|
+
return ctx.union ? `f | ${F_IN_UNION}` : "f";
|
|
3396
|
+
}
|
|
3397
|
+
/** The statements of a `{ … }` block statement (other statements as they are). */ function unblock(statement) {
|
|
3398
|
+
return statement.startsWith("{ ") && statement.endsWith(" }") ? statement.slice(2, -2) : statement;
|
|
3399
|
+
}
|
|
3400
|
+
/** `cond ? a : b` folded when `cond` is a constant. */ function choose(cond, a, b) {
|
|
3401
|
+
return cond === "true" ? a : cond === "false" ? b : `${cond} ? ${a} : ${b}`;
|
|
3402
|
+
}
|
|
3403
|
+
function notObject(x) {
|
|
3404
|
+
return `typeof ${x} !== "object" || ${x} === null || Array.isArray(${x})`;
|
|
3405
|
+
}
|
|
3406
|
+
function indent(lines, n = 2) {
|
|
3407
|
+
const pad = " ".repeat(n);
|
|
3408
|
+
return lines.map((l) => pad + l);
|
|
3409
|
+
}
|
|
3410
|
+
|
|
3411
|
+
//#endregion
|
|
3412
|
+
//#region packages/typescript/src/codegen/validation-model.ts
|
|
3413
|
+
function _define_property$4(obj, key, value) {
|
|
3414
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
3415
|
+
value,
|
|
3416
|
+
enumerable: true,
|
|
3417
|
+
configurable: true,
|
|
3418
|
+
writable: true
|
|
3419
|
+
});
|
|
3420
|
+
else obj[key] = value;
|
|
3421
|
+
return obj;
|
|
3422
|
+
}
|
|
3423
|
+
/** Kinds of the opaque types standing for another module's types (see {@link classifyType}). */ const EXT_KIND = "#ext";
|
|
3424
|
+
const INTERP_KIND = "#interp";
|
|
3425
|
+
/**
|
|
3426
|
+
* How the layout treats a model type: `'ext'` — another module's type referenced as a
|
|
3427
|
+
* whole (also an alias / prop clone of one: `cloneTypeDef` keeps the kind); `'interp'` — a
|
|
3428
|
+
* chained reference into another module's type, only known at runtime.
|
|
3429
|
+
*/ function classifyType(type) {
|
|
3430
|
+
const kind = type?.kind;
|
|
3431
|
+
return kind === EXT_KIND ? "ext" : kind === INTERP_KIND ? "interp" : void 0;
|
|
3432
|
+
}
|
|
3433
|
+
/**
|
|
3434
|
+
* Build-time model of the runtime type trees a generated `.as.js` module builds, used to
|
|
3435
|
+
* compile validator checks (`compileValidators`).
|
|
3436
|
+
*
|
|
3437
|
+
* The renderer drives it in lockstep with the builder chains it emits, through the real
|
|
3438
|
+
* runtime builder (`defineAnnotatedType`, `annotate`, `cloneRefProp`), so the model has
|
|
3439
|
+
* the same structure, the same type-def sharing (by identity) and the same validator
|
|
3440
|
+
* annotations as the module after its initialisation. Only the annotations the validator
|
|
3441
|
+
* pre-check reads are recorded. Types of other modules are opaque markers: a reference to
|
|
3442
|
+
* one (also an alias or a cloned prop of one) compiles to a cross-module site, bound at
|
|
3443
|
+
* runtime to the referenced class's own compiled check (see compiled-check/registry.ts).
|
|
3444
|
+
*
|
|
3445
|
+
* Anything the model cannot follow marks it broken; nothing is compiled then. A model that
|
|
3446
|
+
* nevertheless diverges from the runtime only costs speed: the runtime bind detects it and
|
|
3447
|
+
* the interpreted pre-check runs.
|
|
3448
|
+
*/ var ValidationModel = class {
|
|
3449
|
+
/** Runs a model step; a throw marks the model broken. */ run(step) {
|
|
3450
|
+
if (this.broken) return;
|
|
3451
|
+
try {
|
|
3452
|
+
return step();
|
|
3453
|
+
} catch {
|
|
3454
|
+
this.broken = true;
|
|
3455
|
+
return;
|
|
3456
|
+
}
|
|
3457
|
+
}
|
|
3458
|
+
/** Declares a generated class (`static type = {}; static metadata = new Map()`). */ declare(name) {
|
|
3459
|
+
this.classes.set(name, {
|
|
3460
|
+
__is_atscript_annotated_type: true,
|
|
3461
|
+
type: {},
|
|
3462
|
+
metadata: /* @__PURE__ */ new Map(),
|
|
3463
|
+
name
|
|
3464
|
+
});
|
|
3465
|
+
}
|
|
3466
|
+
/** The model of a class declared in this module (anything else: the emitted code throws). */ classBase(name) {
|
|
3467
|
+
const cls = this.classes.get(name);
|
|
3468
|
+
if (!cls) throw new Error(`${name} is not a class of this module`);
|
|
3469
|
+
return cls;
|
|
3470
|
+
}
|
|
3471
|
+
/** `$(kind, name?)`. */ handle(kind, name) {
|
|
3472
|
+
return this.run(() => defineAnnotatedType(kind, name === void 0 ? void 0 : this.classBase(name)));
|
|
3473
|
+
}
|
|
3474
|
+
/** `.refTo(Local)` / `.refTo(() => Imported)` (with an optional chain). */ refTo(handle, name, imported, chain) {
|
|
3475
|
+
this.run(() => {
|
|
3476
|
+
if (!handle) throw new Error("no chain");
|
|
3477
|
+
if (imported) {
|
|
3478
|
+
const marker = this.marker(name, chain ? INTERP_KIND : EXT_KIND);
|
|
3479
|
+
handle.refTo(() => marker, chain);
|
|
3480
|
+
} else handle.refTo(this.classBase(name), chain);
|
|
3481
|
+
});
|
|
3482
|
+
}
|
|
3483
|
+
/** The opaque stand-in of another module's type `name`. */ marker(name, kind) {
|
|
3484
|
+
const key = `${name}${kind}`;
|
|
3485
|
+
let marker = this.markers.get(key);
|
|
3486
|
+
if (!marker) {
|
|
3487
|
+
marker = {
|
|
3488
|
+
__is_atscript_annotated_type: true,
|
|
3489
|
+
type: { kind },
|
|
3490
|
+
metadata: /* @__PURE__ */ new Map()
|
|
3491
|
+
};
|
|
3492
|
+
this.markers.set(key, marker);
|
|
3493
|
+
}
|
|
3494
|
+
return marker;
|
|
3495
|
+
}
|
|
3496
|
+
/** `.annotate(key, value, multiple)` on the current chain (validator keys only). */ annotate(key, value, multiple) {
|
|
3497
|
+
if (!isValidatorKey(key)) return;
|
|
3498
|
+
this.run(() => {
|
|
3499
|
+
if (!this.current) throw new Error("no chain");
|
|
3500
|
+
this.current.annotate(key, value, multiple);
|
|
3501
|
+
});
|
|
3502
|
+
}
|
|
3503
|
+
/** The node a mutating-annotate accessor (`Name.type.props.get("a")?…`) points at. */ resolve(name, steps) {
|
|
3504
|
+
let cur = this.classes.get(name);
|
|
3505
|
+
for (const step of steps) {
|
|
3506
|
+
if (cur === void 0 || cur === null) return;
|
|
3507
|
+
if (step === "type") {
|
|
3508
|
+
cur = cur.type;
|
|
3509
|
+
if (classifyType(cur) !== void 0) return;
|
|
3510
|
+
} else if (step[0] === "props") cur = cur.props?.get(step[1]);
|
|
3511
|
+
else cur = cur.items?.[step[1]];
|
|
3512
|
+
}
|
|
3513
|
+
return cur;
|
|
3514
|
+
}
|
|
3515
|
+
/** `$c(<accessor>, "prop")`. */ cloneProp(name, steps, prop) {
|
|
3516
|
+
this.run(() => {
|
|
3517
|
+
const parent = this.resolve(name, steps);
|
|
3518
|
+
if (parent) cloneRefProp(parent, prop);
|
|
3519
|
+
});
|
|
3520
|
+
}
|
|
3521
|
+
/** `<accessor>.metadata.delete(key)`. */ deleteAnnotation(name, steps, key) {
|
|
3522
|
+
this.run(() => {
|
|
3523
|
+
this.resolve(name, steps)?.metadata?.delete(key);
|
|
3524
|
+
});
|
|
3525
|
+
}
|
|
3526
|
+
/** `$a(<accessor>.metadata, key, value, multiple)`. */ mutateAnnotation(name, steps, key, value, multiple) {
|
|
3527
|
+
if (!isValidatorKey(key)) return;
|
|
3528
|
+
this.run(() => {
|
|
3529
|
+
annotate(this.resolve(name, steps)?.metadata, key, value, multiple);
|
|
3530
|
+
});
|
|
3531
|
+
}
|
|
3532
|
+
/**
|
|
3533
|
+
* `$cv(Name, version, layout, factory)` for class `name`, or `undefined` when it cannot be
|
|
3534
|
+
* compiled (its root type is only known at runtime, or the model is broken).
|
|
3535
|
+
*/ registration(name) {
|
|
3536
|
+
if (this.broken) return;
|
|
3537
|
+
const cls = this.classes.get(name);
|
|
3538
|
+
if (!cls) return;
|
|
3539
|
+
let layout;
|
|
3540
|
+
try {
|
|
3541
|
+
layout = buildLayout(cls, { regex: getRegex }, classifyType);
|
|
3542
|
+
} catch {
|
|
3543
|
+
return;
|
|
3544
|
+
}
|
|
3545
|
+
const root = layout[1][2];
|
|
3546
|
+
if (root === INTERP || root >= 0 && layout[0][root][0] === D_UNKNOWN) return;
|
|
3547
|
+
return `$cv(${name}, ${LAYOUT_VERSION}, ${layoutSource(layout)}, ${emitCheckFactory(layout)})`;
|
|
3548
|
+
}
|
|
3549
|
+
constructor() {
|
|
3550
|
+
/** The model could not follow the emitted code: compile nothing. */ _define_property$4(this, "broken", false);
|
|
3551
|
+
/** Handle of the chain being emitted (the target of `.annotate(…)` lines). */ _define_property$4(this, "current", void 0);
|
|
3552
|
+
_define_property$4(this, "classes", /* @__PURE__ */ new Map());
|
|
3553
|
+
_define_property$4(this, "markers", /* @__PURE__ */ new Map());
|
|
3554
|
+
}
|
|
3555
|
+
};
|
|
3556
|
+
/**
|
|
3557
|
+
* Runtime value of an annotation argument the emitted code writes as a raw expression
|
|
3558
|
+
* (`true`, `5`, `-1.5`, …). Anything else is opaque (truthy and not a number): the bind
|
|
3559
|
+
* compares only presence / truthiness and reads the real values at runtime.
|
|
3560
|
+
*/ function rawLiteralValue(text) {
|
|
3561
|
+
switch (text) {
|
|
3562
|
+
case "true": return true;
|
|
3563
|
+
case "false": return false;
|
|
3564
|
+
case "null": return null;
|
|
3565
|
+
case "undefined": return;
|
|
3566
|
+
default: {
|
|
3567
|
+
const n = Number(text);
|
|
3568
|
+
return text.trim() !== "" && !Number.isNaN(n) ? n : { opaque: text };
|
|
3569
|
+
}
|
|
3570
|
+
}
|
|
3571
|
+
}
|
|
3572
|
+
|
|
2358
3573
|
//#endregion
|
|
2359
3574
|
//#region packages/typescript/src/codegen/js-renderer.ts
|
|
2360
3575
|
function _define_property$3(obj, key, value) {
|
|
@@ -2387,6 +3602,17 @@ const QUERY_OP_MAP = {
|
|
|
2387
3602
|
"expect.exclusiveMax": "exclusiveMaxValue"
|
|
2388
3603
|
};
|
|
2389
3604
|
const SYNTH_IMPORTS_MARKER = "/*@@atscript-synth-imports@@*/";
|
|
3605
|
+
/** The runtime value of an annotation argument (see `JsRenderer.computeAnnotationValue`). */ function runtimeArg(type, token) {
|
|
3606
|
+
return type === "string" || type === void 0 && token.type === "text" ? token.text : rawLiteralValue(token.text);
|
|
3607
|
+
}
|
|
3608
|
+
/**
|
|
3609
|
+
* The runtime accessor of a mutating-annotate path: `Name` followed by the steps
|
|
3610
|
+
* (`.type`, `.props.get("a")?`, `.items[0]`).
|
|
3611
|
+
*/ function accessorPath(targetName, steps) {
|
|
3612
|
+
let path = targetName;
|
|
3613
|
+
for (const step of steps) path += step === "type" ? ".type" : step[0] === "props" ? `.props.get("${escapeQuotes(step[1])}")?` : `.items[${step[1]}]`;
|
|
3614
|
+
return path;
|
|
3615
|
+
}
|
|
2390
3616
|
var JsRenderer = class extends BaseRenderer {
|
|
2391
3617
|
/**
|
|
2392
3618
|
* Relative imports get the configured `moduleExtension`; bare specifiers
|
|
@@ -2565,6 +3791,7 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
2565
3791
|
if (jsonSchemaMode === "lazy") imports.push("buildJsonSchema as $$");
|
|
2566
3792
|
if (this.opts?.exampleData) imports.push("createDataFromAnnotatedType as $e");
|
|
2567
3793
|
if (jsonSchemaMode === false) imports.push("throwFeatureDisabled as $d");
|
|
3794
|
+
if (this.model) imports.push("registerCompiledCheck as $cv");
|
|
2568
3795
|
this.writeln(`import { ${imports.join(", ")} } from "@atscript/typescript/utils"`);
|
|
2569
3796
|
this.computeSynthesizedImports();
|
|
2570
3797
|
this.writeln(SYNTH_IMPORTS_MARKER);
|
|
@@ -2612,7 +3839,7 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
2612
3839
|
if ((0, _atscript_core.isInterface)(def)) if (def.hasExtends) def = unwound.doc.resolveInterfaceExtends(def) || def.getDefinition() || def;
|
|
2613
3840
|
else def = def.getDefinition() || def;
|
|
2614
3841
|
this._adHocAnnotations = this.buildAdHocMap([annotateNode]);
|
|
2615
|
-
this.annotateType(def, node.id);
|
|
3842
|
+
this.modelClass(node.id, () => this.annotateType(def, node.id));
|
|
2616
3843
|
this._adHocAnnotations = null;
|
|
2617
3844
|
this.indent();
|
|
2618
3845
|
this.defineMetadataForAnnotateAlias(annotateNode);
|
|
@@ -2626,13 +3853,38 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
2626
3853
|
const resolved = this.doc.resolveInterfaceExtends(node);
|
|
2627
3854
|
if (resolved) def = resolved;
|
|
2628
3855
|
}
|
|
2629
|
-
this.annotateType(def, node.id);
|
|
3856
|
+
this.modelClass(node.id, () => this.annotateType(def, node.id));
|
|
2630
3857
|
this.indent().defineMetadata(node).unindent();
|
|
2631
3858
|
this.writeln();
|
|
2632
3859
|
}
|
|
3860
|
+
this.renderCompiledChecks();
|
|
2633
3861
|
this.writeln("// prettier-ignore-end");
|
|
2634
3862
|
super.post();
|
|
2635
3863
|
}
|
|
3864
|
+
/**
|
|
3865
|
+
* Emits class `name`'s chain; the validation model follows it (its handle stays the current
|
|
3866
|
+
* chain, the target of the class-level annotations emitted next).
|
|
3867
|
+
*/ modelClass(name, emit) {
|
|
3868
|
+
if (this.model) this.model.current = void 0;
|
|
3869
|
+
emit();
|
|
3870
|
+
if (this.model?.current) this.modelClasses.push(name);
|
|
3871
|
+
}
|
|
3872
|
+
/**
|
|
3873
|
+
* `compileValidators`: one `$cv(Class, …)` registration per class, after every chain and
|
|
3874
|
+
* mutating annotate of the module ran (the compiled check binds on first use).
|
|
3875
|
+
*/ renderCompiledChecks() {
|
|
3876
|
+
if (!this.model) return;
|
|
3877
|
+
const registrations = [];
|
|
3878
|
+
for (const name of this.modelClasses) {
|
|
3879
|
+
const registration = this.model.registration(name);
|
|
3880
|
+
if (registration) registrations.push(registration);
|
|
3881
|
+
}
|
|
3882
|
+
if (registrations.length > 0) {
|
|
3883
|
+
this.writeln("// Compiled validator checks");
|
|
3884
|
+
for (const registration of registrations) for (const line of registration.split("\n")) this.writeln(line);
|
|
3885
|
+
this.writeln();
|
|
3886
|
+
}
|
|
3887
|
+
}
|
|
2636
3888
|
renderClassStatics(node) {
|
|
2637
3889
|
this.writeln("static __is_atscript_annotated_type = true");
|
|
2638
3890
|
this.writeln("static type = {}");
|
|
@@ -2663,6 +3915,7 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
2663
3915
|
this.write(exported ? "export " : "");
|
|
2664
3916
|
this.write(`class ${node.id} `);
|
|
2665
3917
|
this.blockln("{}");
|
|
3918
|
+
this.model?.declare(node.id);
|
|
2666
3919
|
this.renderClassStatics(node);
|
|
2667
3920
|
this.popln();
|
|
2668
3921
|
this.postAnnotate.push(node);
|
|
@@ -2712,23 +3965,63 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
2712
3965
|
if (measures.length > 0) this.writeln(`static measures = [${measures.map((m) => `'${escapeQuotes(m)}'`).join(", ")}]`);
|
|
2713
3966
|
}
|
|
2714
3967
|
toAnnotatedType(node) {
|
|
2715
|
-
return this.toAnnotatedHandle(node).$type;
|
|
3968
|
+
return node?.entity === "interface" || node?.entity === "type" ? this.declType(node) : this.toAnnotatedHandle(node).$type;
|
|
3969
|
+
}
|
|
3970
|
+
/**
|
|
3971
|
+
* The build-time runtime type of an interface / type declaration (see `_declTypes`);
|
|
3972
|
+
* `doc` is the document that declares it (where an alias's target name resolves).
|
|
3973
|
+
*/ declType(node, doc = this.doc) {
|
|
3974
|
+
const known = this._declTypes.get(node);
|
|
3975
|
+
if (known) return known;
|
|
3976
|
+
let def = node.getDefinition();
|
|
3977
|
+
if ((0, _atscript_core.isInterface)(node) && node.hasExtends) {
|
|
3978
|
+
const resolved = this.doc.resolveInterfaceExtends(node);
|
|
3979
|
+
if (resolved) def = resolved;
|
|
3980
|
+
}
|
|
3981
|
+
const typeId = this.typeIds.get(node) ?? (node.__typeId !== null && node.__typeId !== void 0 ? node.id : void 0);
|
|
3982
|
+
let handle;
|
|
3983
|
+
if (def && (0, _atscript_core.isRef)(def)) {
|
|
3984
|
+
const ref = def;
|
|
3985
|
+
const decl = doc.unwindType(ref.id, ref.chain)?.def;
|
|
3986
|
+
const target = this.refTarget(ref, decl, doc);
|
|
3987
|
+
handle = target ? defineAnnotatedType().refTo(target) : this.toAnnotatedHandle(decl, true);
|
|
3988
|
+
} else {
|
|
3989
|
+
const base = {
|
|
3990
|
+
__is_atscript_annotated_type: true,
|
|
3991
|
+
type: {},
|
|
3992
|
+
metadata: /* @__PURE__ */ new Map(),
|
|
3993
|
+
id: typeId
|
|
3994
|
+
};
|
|
3995
|
+
this._declTypes.set(node, base);
|
|
3996
|
+
if (def) this._declTypes.set(def, base);
|
|
3997
|
+
handle = this.toAnnotatedHandle(def, true, base);
|
|
3998
|
+
}
|
|
3999
|
+
this._declTypes.set(node, handle.$type);
|
|
4000
|
+
if (typeId) handle.id(typeId);
|
|
4001
|
+
this.applyExpectAnnotations(handle, this.doc.evalAnnotationsForNode(node));
|
|
4002
|
+
return handle.$type;
|
|
2716
4003
|
}
|
|
2717
|
-
|
|
2718
|
-
|
|
4004
|
+
/**
|
|
4005
|
+
* The declaration type a reference points at (see `declType`), or `undefined` when it is
|
|
4006
|
+
* not a declaration (a chained reference into a prop, an unresolved name). A plain
|
|
4007
|
+
* reference to a type alias points at the alias itself (its own type and id, like the
|
|
4008
|
+
* runtime `$().refTo(Alias)`), not at what the alias unwinds to.
|
|
4009
|
+
*/ refTarget(ref, decl, doc = this.doc) {
|
|
4010
|
+
if (!decl) return;
|
|
4011
|
+
const owner = ref.hasChain ? void 0 : this.resolveOwner(doc, ref.id);
|
|
4012
|
+
if (owner?.node?.entity === "type" || owner?.node?.entity === "interface") return this.declType(owner.node, owner.doc);
|
|
4013
|
+
const known = this._declTypes.get(decl);
|
|
4014
|
+
if (known) return known;
|
|
4015
|
+
return (0, _atscript_core.isInterface)(decl) ? this.declType(decl) : void 0;
|
|
4016
|
+
}
|
|
4017
|
+
/**
|
|
4018
|
+
* The build-time runtime type of an expression node; with `base`, built into that object
|
|
4019
|
+
* (a declaration's type, see `declType`).
|
|
4020
|
+
*/ toAnnotatedHandle(node, skipAnnotations = false, base) {
|
|
4021
|
+
if (!node) return defineAnnotatedType(void 0, base);
|
|
2719
4022
|
switch (node.entity) {
|
|
2720
4023
|
case "interface":
|
|
2721
|
-
case "type":
|
|
2722
|
-
let def = node.getDefinition();
|
|
2723
|
-
if ((0, _atscript_core.isInterface)(node) && node.hasExtends) {
|
|
2724
|
-
const resolved = this.doc.resolveInterfaceExtends(node);
|
|
2725
|
-
if (resolved) def = resolved;
|
|
2726
|
-
}
|
|
2727
|
-
const handle = this.toAnnotatedHandle(def, true);
|
|
2728
|
-
const typeId = this.typeIds.get(node) ?? (node.__typeId !== null && node.__typeId !== void 0 ? node.id : void 0);
|
|
2729
|
-
if (typeId) handle.id(typeId);
|
|
2730
|
-
return skipAnnotations ? handle : this.applyExpectAnnotations(handle, this.doc.evalAnnotationsForNode(node));
|
|
2731
|
-
}
|
|
4024
|
+
case "type": return defineAnnotatedType().refTo(this.declType(node));
|
|
2732
4025
|
case "prop": {
|
|
2733
4026
|
const prop = node;
|
|
2734
4027
|
const def = prop.getDefinition();
|
|
@@ -2742,7 +4035,8 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
2742
4035
|
case "ref": {
|
|
2743
4036
|
const ref = node;
|
|
2744
4037
|
const decl = this.doc.unwindType(ref.id, ref.chain)?.def;
|
|
2745
|
-
const
|
|
4038
|
+
const target = this.refTarget(ref, decl);
|
|
4039
|
+
const handle = target ? defineAnnotatedType().refTo(target) : this.toAnnotatedHandle(decl, true);
|
|
2746
4040
|
if (skipAnnotations) return handle;
|
|
2747
4041
|
const own = this.doc.evalAnnotationsForNode(node);
|
|
2748
4042
|
const referred = this.refTargetAnnotations(ref, decl);
|
|
@@ -2750,14 +4044,14 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
2750
4044
|
}
|
|
2751
4045
|
case "primitive": {
|
|
2752
4046
|
const prim = node;
|
|
2753
|
-
const handle = defineAnnotatedType();
|
|
4047
|
+
const handle = defineAnnotatedType(void 0, base);
|
|
2754
4048
|
handle.designType(prim.id === "never" ? "never" : prim.config.type);
|
|
2755
4049
|
if (!skipAnnotations) this.applyExpectAnnotations(handle, this.doc.evalAnnotationsForNode(node));
|
|
2756
4050
|
return handle;
|
|
2757
4051
|
}
|
|
2758
4052
|
case "const": {
|
|
2759
4053
|
const c = node;
|
|
2760
|
-
const handle = defineAnnotatedType();
|
|
4054
|
+
const handle = defineAnnotatedType(void 0, base);
|
|
2761
4055
|
const t = c.token("identifier")?.type;
|
|
2762
4056
|
handle.designType(t === "number" ? "number" : "string");
|
|
2763
4057
|
handle.value(t === "number" ? Number(c.id) : c.id);
|
|
@@ -2765,7 +4059,7 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
2765
4059
|
}
|
|
2766
4060
|
case "structure": {
|
|
2767
4061
|
const struct = node;
|
|
2768
|
-
const handle = defineAnnotatedType("object");
|
|
4062
|
+
const handle = defineAnnotatedType("object", base);
|
|
2769
4063
|
for (const prop of Array.from(struct.props.values())) {
|
|
2770
4064
|
const propHandle = this.toAnnotatedHandle(prop);
|
|
2771
4065
|
const pattern = prop.token("identifier")?.pattern;
|
|
@@ -2776,24 +4070,24 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
2776
4070
|
}
|
|
2777
4071
|
case "group": {
|
|
2778
4072
|
const group = node;
|
|
2779
|
-
const handle = defineAnnotatedType(group.op === "|" ? "union" : "intersection");
|
|
4073
|
+
const handle = defineAnnotatedType(group.op === "|" ? "union" : "intersection", base);
|
|
2780
4074
|
for (const item of group.unwrap()) handle.item(this.toAnnotatedHandle(item).$type);
|
|
2781
4075
|
return skipAnnotations ? handle : this.applyExpectAnnotations(handle, this.doc.evalAnnotationsForNode(node));
|
|
2782
4076
|
}
|
|
2783
4077
|
case "tuple": {
|
|
2784
4078
|
const group = node;
|
|
2785
|
-
const handle = defineAnnotatedType("tuple");
|
|
4079
|
+
const handle = defineAnnotatedType("tuple", base);
|
|
2786
4080
|
for (const item of group.unwrap()) handle.item(this.toAnnotatedHandle(item).$type);
|
|
2787
4081
|
return skipAnnotations ? handle : this.applyExpectAnnotations(handle, this.doc.evalAnnotationsForNode(node));
|
|
2788
4082
|
}
|
|
2789
4083
|
case "array": {
|
|
2790
4084
|
const arr = node;
|
|
2791
|
-
const handle = defineAnnotatedType("array");
|
|
4085
|
+
const handle = defineAnnotatedType("array", base);
|
|
2792
4086
|
handle.of(this.toAnnotatedHandle(arr.getDefinition()).$type);
|
|
2793
4087
|
return skipAnnotations ? handle : this.applyExpectAnnotations(handle, this.doc.evalAnnotationsForNode(node));
|
|
2794
4088
|
}
|
|
2795
4089
|
default: {
|
|
2796
|
-
const handle = defineAnnotatedType();
|
|
4090
|
+
const handle = defineAnnotatedType(void 0, base);
|
|
2797
4091
|
return skipAnnotations ? handle : this.applyExpectAnnotations(handle, this.doc.evalAnnotationsForNode(node));
|
|
2798
4092
|
}
|
|
2799
4093
|
}
|
|
@@ -2865,8 +4159,10 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
2865
4159
|
node: synth.ownerNode
|
|
2866
4160
|
} : this.resolveOwner(this.doc, ref.id);
|
|
2867
4161
|
const refName = synth?.alias ?? ref.id;
|
|
2868
|
-
const
|
|
4162
|
+
const isImported = !!synth || (ownerDecl ? ownerDecl.doc !== this.doc : false);
|
|
4163
|
+
const refExpr = isImported ? `() => ${refName}` : refName;
|
|
2869
4164
|
this.writeln(`$(${name ? `"", ${name}` : ""})`).indent().writeln(`.refTo(${refExpr}${chain})`);
|
|
4165
|
+
this.model?.refTo(this.modelHandle("", name), refName, isImported, ref.hasChain ? ref.chain.map((c) => c.text) : void 0);
|
|
2870
4166
|
if (!ref.hasChain) {
|
|
2871
4167
|
if (ownerDecl?.node) ownerDecl.doc.evalReferredAnnotations(ownerDecl.node)?.forEach((an) => {
|
|
2872
4168
|
this.resolveAnnotationValue(ownerDecl.node, an);
|
|
@@ -2879,29 +4175,63 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
2879
4175
|
this.definePrimitive(node, name);
|
|
2880
4176
|
return this;
|
|
2881
4177
|
case "const":
|
|
4178
|
+
this.modelHandle("", name);
|
|
2882
4179
|
this.writeln(`$(${name ? `"", ${name}` : ""})`).indent().defineConst(node).unindent();
|
|
2883
4180
|
return this;
|
|
2884
4181
|
case "structure":
|
|
4182
|
+
this.modelHandle("object", name);
|
|
2885
4183
|
this.writeln(`$("object"${name ? `, ${name}` : ""})`).indent().defineObject(node).unindent();
|
|
2886
4184
|
return this;
|
|
2887
4185
|
case "group":
|
|
2888
4186
|
kind = node.op === "|" ? "union" : "intersection";
|
|
4187
|
+
this.modelHandle(kind, name);
|
|
2889
4188
|
this.writeln(`$("${kind}"${name ? `, ${name}` : ""})`).indent().defineGroup(node).unindent();
|
|
2890
4189
|
return this;
|
|
2891
4190
|
case "tuple":
|
|
4191
|
+
this.modelHandle("tuple", name);
|
|
2892
4192
|
this.writeln(`$("tuple"${name ? `, ${name}` : ""})`).indent().defineGroup(node).unindent();
|
|
2893
4193
|
return this;
|
|
2894
4194
|
case "array":
|
|
4195
|
+
this.modelHandle("array", name);
|
|
2895
4196
|
this.writeln(`$("array"${name ? `, ${name}` : ""})`).indent().defineArray(node).unindent();
|
|
2896
4197
|
return this;
|
|
2897
4198
|
default: return this;
|
|
2898
4199
|
}
|
|
2899
4200
|
}
|
|
4201
|
+
/** `$(kind, name?)` in the validation model; it becomes the current chain. */ modelHandle(kind, name) {
|
|
4202
|
+
if (!this.model) return;
|
|
4203
|
+
const h = this.model.handle(kind, name);
|
|
4204
|
+
this.model.current = h;
|
|
4205
|
+
return h;
|
|
4206
|
+
}
|
|
4207
|
+
/**
|
|
4208
|
+
* The model handle of a nested chain (`emit` writes it and its annotations): `current`
|
|
4209
|
+
* is cleared first so a chain that emits nothing breaks the model instead of leaving a
|
|
4210
|
+
* stale handle, and restored to the parent chain afterwards.
|
|
4211
|
+
*/ modelChild(emit) {
|
|
4212
|
+
if (!this.model) {
|
|
4213
|
+
emit();
|
|
4214
|
+
return;
|
|
4215
|
+
}
|
|
4216
|
+
const parent = this.model.current;
|
|
4217
|
+
this.model.current = void 0;
|
|
4218
|
+
emit();
|
|
4219
|
+
const child = this.model.current;
|
|
4220
|
+
this.model.current = parent;
|
|
4221
|
+
if (!child) this.model.broken = true;
|
|
4222
|
+
return child;
|
|
4223
|
+
}
|
|
2900
4224
|
defineConst(node) {
|
|
2901
4225
|
const t = node.token("identifier")?.type;
|
|
2902
4226
|
const designType = t === "text" ? "string" : t === "number" ? "number" : "unknown";
|
|
2903
4227
|
this.writeln(`.designType("${escapeQuotes(designType)}")`);
|
|
2904
4228
|
this.writeln(`.value(${t === "text" ? `"${escapeQuotes(node.id)}"` : node.id})`);
|
|
4229
|
+
if (this.model) {
|
|
4230
|
+
const h = this.model.current;
|
|
4231
|
+
this.model.run(() => {
|
|
4232
|
+
h.designType(designType).value(t === "text" ? node.id : rawLiteralValue(node.id));
|
|
4233
|
+
});
|
|
4234
|
+
}
|
|
2905
4235
|
return this;
|
|
2906
4236
|
}
|
|
2907
4237
|
definePrimitive(node, name) {
|
|
@@ -2909,66 +4239,82 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
2909
4239
|
this.writeln(` .tags(${Array.from(node.tags).map((f) => `"${escapeQuotes(f)}"`).join(", ")})`);
|
|
2910
4240
|
return this;
|
|
2911
4241
|
}
|
|
2912
|
-
renderPrimitiveDef(def, name) {
|
|
4242
|
+
/** Emits a primitive's builder chain (and follows it in the validation model). */ renderPrimitiveDef(def, name) {
|
|
2913
4243
|
const d = (t) => [`"${t || ""}"`, name].filter(Boolean).join(", ").replace(/^""$/, "");
|
|
2914
|
-
|
|
2915
|
-
|
|
4244
|
+
const final = (designType) => {
|
|
4245
|
+
this.writeln(`$(${d()}).designType("${designType}")`);
|
|
4246
|
+
const h = this.modelHandle("", name);
|
|
4247
|
+
this.model?.run(() => h.designType(designType));
|
|
4248
|
+
};
|
|
4249
|
+
if (!def) return final("any");
|
|
4250
|
+
if (typeof def === "string") return final(def === "void" ? "undefined" : def);
|
|
2916
4251
|
switch (def.kind) {
|
|
2917
|
-
case "final": return
|
|
4252
|
+
case "final": return final(def.value === "void" ? "undefined" : def.value);
|
|
2918
4253
|
case "union":
|
|
2919
4254
|
case "intersection":
|
|
2920
4255
|
case "tuple":
|
|
2921
4256
|
this.writeln(`$(${d(def.kind)})`);
|
|
4257
|
+
this.modelHandle(def.kind, name);
|
|
2922
4258
|
this.indent();
|
|
2923
4259
|
for (const itemDef of def.items) {
|
|
2924
4260
|
this.write(`.item(`);
|
|
2925
4261
|
this.indent();
|
|
2926
|
-
this.renderPrimitiveDef(itemDef);
|
|
4262
|
+
const child = this.modelChild(() => this.renderPrimitiveDef(itemDef));
|
|
2927
4263
|
this.writeln(".$type");
|
|
2928
4264
|
this.unindent();
|
|
2929
4265
|
this.write(`)`);
|
|
4266
|
+
this.model?.run(() => this.model.current.item(child.$type));
|
|
2930
4267
|
}
|
|
2931
4268
|
this.unindent();
|
|
2932
4269
|
return;
|
|
2933
|
-
case "array":
|
|
4270
|
+
case "array": {
|
|
2934
4271
|
this.writeln(`$(${d("array")})`);
|
|
4272
|
+
this.modelHandle("array", name);
|
|
2935
4273
|
this.indent();
|
|
2936
4274
|
this.write(".of(");
|
|
2937
4275
|
this.indent();
|
|
2938
|
-
this.renderPrimitiveDef(def.of);
|
|
4276
|
+
const child = this.modelChild(() => this.renderPrimitiveDef(def.of));
|
|
2939
4277
|
this.writeln(`.$type`);
|
|
2940
4278
|
this.unindent();
|
|
2941
4279
|
this.writeln(`)`);
|
|
2942
4280
|
this.unindent();
|
|
4281
|
+
this.model?.run(() => this.model.current.of(child.$type));
|
|
2943
4282
|
return;
|
|
2944
|
-
|
|
4283
|
+
}
|
|
4284
|
+
case "object": {
|
|
2945
4285
|
this.writeln(`$(${d("object")})`);
|
|
4286
|
+
this.modelHandle("object", name);
|
|
2946
4287
|
this.indent();
|
|
2947
|
-
|
|
2948
|
-
|
|
2949
|
-
|
|
2950
|
-
|
|
2951
|
-
|
|
2952
|
-
|
|
2953
|
-
|
|
2954
|
-
|
|
2955
|
-
|
|
2956
|
-
|
|
2957
|
-
}
|
|
2958
|
-
for (const [key, propDef] of Object.entries(def.propsPatterns)) {
|
|
4288
|
+
const props = [...Object.entries(def.props).map(([k, p]) => [
|
|
4289
|
+
k,
|
|
4290
|
+
p,
|
|
4291
|
+
false
|
|
4292
|
+
]), ...Object.entries(def.propsPatterns).map(([k, p]) => [
|
|
4293
|
+
k,
|
|
4294
|
+
p,
|
|
4295
|
+
true
|
|
4296
|
+
])];
|
|
4297
|
+
for (const [key, propDef, isPattern] of props) {
|
|
2959
4298
|
const optional = typeof propDef === "object" && propDef.optional;
|
|
2960
|
-
this.writeln(`.propPattern(`);
|
|
4299
|
+
this.writeln(isPattern ? `.propPattern(` : `.prop(`);
|
|
2961
4300
|
this.indent();
|
|
2962
|
-
this.writeln(`${key},`);
|
|
2963
|
-
this.
|
|
2964
|
-
|
|
4301
|
+
this.writeln(isPattern ? `${key},` : `"${escapeQuotes(key)}",`);
|
|
4302
|
+
const child = this.modelChild(() => {
|
|
4303
|
+
this.renderPrimitiveDef(propDef);
|
|
4304
|
+
if (optional) {
|
|
4305
|
+
this.writeln(".optional()");
|
|
4306
|
+
this.model?.run(() => this.model.current.optional());
|
|
4307
|
+
}
|
|
4308
|
+
});
|
|
2965
4309
|
this.writeln(".$type");
|
|
2966
4310
|
this.unindent();
|
|
2967
4311
|
this.write(`)`);
|
|
4312
|
+
this.model?.run(() => isPattern ? this.model.current.propPattern(/./, child.$type) : this.model.current.prop(key, child.$type));
|
|
2968
4313
|
}
|
|
2969
4314
|
this.unindent();
|
|
2970
4315
|
return;
|
|
2971
|
-
|
|
4316
|
+
}
|
|
4317
|
+
default: return final("any");
|
|
2972
4318
|
}
|
|
2973
4319
|
}
|
|
2974
4320
|
defineObject(node) {
|
|
@@ -2986,25 +4332,39 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
2986
4332
|
this.indent();
|
|
2987
4333
|
this.writeln(`"${escapeQuotes(prop.id)}",`);
|
|
2988
4334
|
}
|
|
2989
|
-
this.
|
|
2990
|
-
|
|
4335
|
+
const child = this.modelChild(() => {
|
|
4336
|
+
this.annotateType(prop.getDefinition());
|
|
4337
|
+
this.indent().defineMetadata(prop).unindent();
|
|
4338
|
+
});
|
|
2991
4339
|
if (optional) this.writeln(" .optional()");
|
|
2992
4340
|
this.writeln(" .$type");
|
|
2993
4341
|
this.unindent();
|
|
2994
4342
|
this.write(`)`);
|
|
2995
4343
|
this._propPath.pop();
|
|
4344
|
+
this.model?.run(() => {
|
|
4345
|
+
if (optional) child.optional();
|
|
4346
|
+
if (pattern) this.model.current.propPattern(pattern, child.$type);
|
|
4347
|
+
else this.model.current.prop(prop.id, child.$type);
|
|
4348
|
+
});
|
|
2996
4349
|
}
|
|
2997
4350
|
this.writeln();
|
|
2998
4351
|
return this;
|
|
2999
4352
|
}
|
|
3000
4353
|
defineGroup(node) {
|
|
3001
|
-
const
|
|
3002
|
-
|
|
4354
|
+
for (const item of node.unwrap()) {
|
|
4355
|
+
this.write(".item(").indent();
|
|
4356
|
+
const child = this.modelChild(() => this.annotateType(item).defineInlinePrimitiveMetadata(item));
|
|
4357
|
+
this.write(" .$type").writeln(`)`).unindent();
|
|
4358
|
+
this.model?.run(() => this.model.current.item(child.$type));
|
|
4359
|
+
}
|
|
3003
4360
|
return this;
|
|
3004
4361
|
}
|
|
3005
4362
|
defineArray(node) {
|
|
3006
4363
|
const of = node.getDefinition();
|
|
3007
|
-
this.write(".of(").indent()
|
|
4364
|
+
this.write(".of(").indent();
|
|
4365
|
+
const child = this.modelChild(() => this.annotateType(of).defineInlinePrimitiveMetadata(of));
|
|
4366
|
+
this.write(" .$type").writeln(`)`).unindent();
|
|
4367
|
+
this.model?.run(() => this.model.current.of(child.$type));
|
|
3008
4368
|
return this;
|
|
3009
4369
|
}
|
|
3010
4370
|
/**
|
|
@@ -3075,9 +4435,10 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
3075
4435
|
return this;
|
|
3076
4436
|
}
|
|
3077
4437
|
resolveAnnotationValue(node, an) {
|
|
3078
|
-
const { value, multiple } = this.computeAnnotationValue(node, an);
|
|
4438
|
+
const { value, multiple, runtime } = this.computeAnnotationValue(node, an);
|
|
3079
4439
|
if (multiple) this.writeln(`.annotate("${escapeQuotes(an.name)}", ${value}, true)`);
|
|
3080
4440
|
else this.writeln(`.annotate("${escapeQuotes(an.name)}", ${value})`);
|
|
4441
|
+
this.model?.annotate(an.name, runtime, multiple);
|
|
3081
4442
|
}
|
|
3082
4443
|
emitRefValue(text, origin) {
|
|
3083
4444
|
const dotIdx = text.indexOf(".");
|
|
@@ -3156,33 +4517,50 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
3156
4517
|
default: return token.text;
|
|
3157
4518
|
}
|
|
3158
4519
|
}
|
|
3159
|
-
|
|
4520
|
+
/**
|
|
4521
|
+
* The JS source of an annotation's value, and (for the validation model, which only reads
|
|
4522
|
+
* validator annotations: numbers, strings, flags) the runtime value it evaluates to;
|
|
4523
|
+
* other argument kinds are opaque there.
|
|
4524
|
+
*/ computeAnnotationValue(node, an) {
|
|
3160
4525
|
const origin = this.doc.annotationOrigin(an);
|
|
3161
4526
|
const spec = this.doc.resolveAnnotation(an.name);
|
|
3162
4527
|
let targetValue = "true";
|
|
4528
|
+
let runtime = true;
|
|
3163
4529
|
let multiple = false;
|
|
3164
4530
|
if (spec) {
|
|
3165
4531
|
multiple = spec.config.multiple;
|
|
3166
4532
|
const length = spec.arguments.length;
|
|
3167
|
-
if (length !== 0)
|
|
3168
|
-
|
|
3169
|
-
|
|
3170
|
-
|
|
3171
|
-
|
|
3172
|
-
|
|
4533
|
+
if (length !== 0) {
|
|
4534
|
+
if (Array.isArray(spec.config.argument)) {
|
|
4535
|
+
targetValue = "{ ";
|
|
4536
|
+
const fields = {};
|
|
4537
|
+
let i = 0;
|
|
4538
|
+
for (const aSpec of spec.arguments) {
|
|
4539
|
+
if (an.args[i]) {
|
|
4540
|
+
targetValue += `${wrapProp(aSpec.name)}: ${this.emitArgValue(aSpec, an.args[i], origin)}${i === length - 1 ? "" : ", "} `;
|
|
4541
|
+
fields[aSpec.name] = runtimeArg(aSpec.type, an.args[i]);
|
|
4542
|
+
}
|
|
4543
|
+
i++;
|
|
4544
|
+
}
|
|
4545
|
+
targetValue += "}";
|
|
4546
|
+
runtime = fields;
|
|
4547
|
+
} else if (an.args[0]) {
|
|
4548
|
+
const aSpec = spec.arguments[0];
|
|
4549
|
+
targetValue = this.emitArgValue(aSpec, an.args[0], origin);
|
|
4550
|
+
runtime = runtimeArg(aSpec.type, an.args[0]);
|
|
3173
4551
|
}
|
|
3174
|
-
targetValue += "}";
|
|
3175
|
-
} else {
|
|
3176
|
-
const aSpec = spec.arguments[0];
|
|
3177
|
-
targetValue = an.args[0] ? this.emitArgValue(aSpec, an.args[0], origin) : "true";
|
|
3178
4552
|
}
|
|
3179
4553
|
} else {
|
|
3180
4554
|
multiple = node.countAnnotations(an.name) > 1 || an.args.length > 1;
|
|
3181
|
-
if (an.args.length > 0)
|
|
4555
|
+
if (an.args.length > 0) {
|
|
4556
|
+
targetValue = an.args[0].type === "text" ? `"${escapeQuotes(an.args[0].text)}"` : an.args[0].text;
|
|
4557
|
+
runtime = runtimeArg(void 0, an.args[0]);
|
|
4558
|
+
}
|
|
3182
4559
|
}
|
|
3183
4560
|
return {
|
|
3184
4561
|
value: targetValue,
|
|
3185
|
-
multiple: !!multiple
|
|
4562
|
+
multiple: !!multiple,
|
|
4563
|
+
runtime
|
|
3186
4564
|
};
|
|
3187
4565
|
}
|
|
3188
4566
|
renderMutatingAnnotateNode(node) {
|
|
@@ -3195,7 +4573,7 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
3195
4573
|
const anns = entry.annotations;
|
|
3196
4574
|
if (!anns || anns.length === 0) continue;
|
|
3197
4575
|
const parts = entry.hasChain ? [entry.id, ...entry.chain.map((c) => c.text)] : [entry.id];
|
|
3198
|
-
const { accessors, clones } = this.buildMutatingAccessors(
|
|
4576
|
+
const { accessors, clones } = this.buildMutatingAccessors(targetDef, parts);
|
|
3199
4577
|
allClones.push(...clones);
|
|
3200
4578
|
entryAccessors.push({
|
|
3201
4579
|
entry,
|
|
@@ -3204,29 +4582,36 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
3204
4582
|
}
|
|
3205
4583
|
const cloneKeys = /* @__PURE__ */ new Set();
|
|
3206
4584
|
for (const clone of allClones) {
|
|
3207
|
-
const
|
|
4585
|
+
const parentPath = accessorPath(targetName, clone.parentSteps);
|
|
4586
|
+
const key = `${parentPath}|${clone.propName}`;
|
|
3208
4587
|
if (!cloneKeys.has(key)) {
|
|
3209
4588
|
cloneKeys.add(key);
|
|
3210
|
-
this.writeln(`$c(${
|
|
4589
|
+
this.writeln(`$c(${parentPath}, "${escapeQuotes(clone.propName)}")`);
|
|
4590
|
+
this.model?.cloneProp(targetName, clone.parentSteps, clone.propName);
|
|
3211
4591
|
}
|
|
3212
4592
|
}
|
|
3213
|
-
for (const { entry, accessors } of entryAccessors) for (const accessor of accessors) this.emitMutatingAnnotations(entry, entry.annotations, accessor);
|
|
4593
|
+
for (const { entry, accessors } of entryAccessors) for (const accessor of accessors) this.emitMutatingAnnotations(entry, entry.annotations, accessor, targetName);
|
|
3214
4594
|
const topAnnotations = node.annotations;
|
|
3215
|
-
if (topAnnotations && topAnnotations.length > 0) this.emitMutatingAnnotations(node, topAnnotations, targetName);
|
|
4595
|
+
if (topAnnotations && topAnnotations.length > 0) this.emitMutatingAnnotations(node, topAnnotations, [], targetName);
|
|
3216
4596
|
this.writeln();
|
|
3217
4597
|
}
|
|
3218
|
-
emitMutatingAnnotations(node, annotations,
|
|
4598
|
+
emitMutatingAnnotations(node, annotations, steps, targetName) {
|
|
4599
|
+
const accessor = accessorPath(targetName, steps);
|
|
3219
4600
|
const cleared = /* @__PURE__ */ new Set();
|
|
3220
4601
|
for (const an of annotations) {
|
|
3221
|
-
const { value, multiple } = this.computeAnnotationValue(node, an);
|
|
4602
|
+
const { value, multiple, runtime } = this.computeAnnotationValue(node, an);
|
|
3222
4603
|
if (multiple) {
|
|
3223
4604
|
if (!cleared.has(an.name)) {
|
|
3224
4605
|
const spec = this.doc.resolveAnnotation(an.name);
|
|
3225
|
-
if (!spec || spec.config.mergeStrategy !== "append")
|
|
4606
|
+
if (!spec || spec.config.mergeStrategy !== "append") {
|
|
4607
|
+
this.writeln(`${accessor}.metadata.delete("${escapeQuotes(an.name)}")`);
|
|
4608
|
+
this.model?.deleteAnnotation(targetName, steps, an.name);
|
|
4609
|
+
}
|
|
3226
4610
|
cleared.add(an.name);
|
|
3227
4611
|
}
|
|
3228
4612
|
this.writeln(`$a(${accessor}.metadata, "${escapeQuotes(an.name)}", ${value}, true)`);
|
|
3229
4613
|
} else this.writeln(`$a(${accessor}.metadata, "${escapeQuotes(an.name)}", ${value})`);
|
|
4614
|
+
this.model?.mutateAnnotation(targetName, steps, an.name, runtime, multiple);
|
|
3230
4615
|
}
|
|
3231
4616
|
}
|
|
3232
4617
|
resolveTargetDef(targetName) {
|
|
@@ -3237,51 +4622,48 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
3237
4622
|
return def;
|
|
3238
4623
|
}
|
|
3239
4624
|
/**
|
|
3240
|
-
* Builds the
|
|
4625
|
+
* Builds the accessor paths (as steps, see `accessorPath`) for mutating annotate entries.
|
|
3241
4626
|
* Computes exact paths at compile time by walking the AST,
|
|
3242
4627
|
* so the generated JS accesses props directly without runtime search.
|
|
3243
4628
|
* Returns multiple paths when a property appears in multiple union branches.
|
|
3244
|
-
*/ buildMutatingAccessors(
|
|
4629
|
+
*/ buildMutatingAccessors(targetDef, parts) {
|
|
3245
4630
|
let accessors = [{
|
|
3246
|
-
|
|
4631
|
+
steps: ["type"],
|
|
3247
4632
|
def: targetDef
|
|
3248
4633
|
}];
|
|
3249
4634
|
const clones = [];
|
|
3250
4635
|
for (let i = 0; i < parts.length; i++) {
|
|
4636
|
+
const last = i === parts.length - 1;
|
|
3251
4637
|
const nextAccessors = [];
|
|
3252
|
-
for (const {
|
|
4638
|
+
for (const { steps, def } of accessors) {
|
|
3253
4639
|
const results = this.buildPropPaths(def, parts[i]);
|
|
3254
|
-
|
|
3255
|
-
|
|
3256
|
-
|
|
4640
|
+
const found = results.length > 0 ? results : [{
|
|
4641
|
+
steps: [["props", parts[i]]],
|
|
4642
|
+
propDef: void 0
|
|
4643
|
+
}];
|
|
4644
|
+
for (const result of found) {
|
|
4645
|
+
if (!last && result.propDef && (0, _atscript_core.isRef)(result.propDef)) clones.push({
|
|
4646
|
+
parentSteps: steps,
|
|
3257
4647
|
propName: parts[i]
|
|
3258
4648
|
});
|
|
4649
|
+
const next = [...steps, ...result.steps];
|
|
4650
|
+
if (!last) next.push("type");
|
|
3259
4651
|
nextAccessors.push({
|
|
3260
|
-
|
|
4652
|
+
steps: next,
|
|
3261
4653
|
def: result.propDef
|
|
3262
4654
|
});
|
|
3263
|
-
} else nextAccessors.push({
|
|
3264
|
-
prefix: `${prefix}${result.path}?`,
|
|
3265
|
-
def: result.propDef
|
|
3266
|
-
});
|
|
3267
|
-
else {
|
|
3268
|
-
const suffix = `.props.get("${escapeQuotes(parts[i])}")${i < parts.length - 1 ? "?.type" : "?"}`;
|
|
3269
|
-
nextAccessors.push({
|
|
3270
|
-
prefix: `${prefix}${suffix}`,
|
|
3271
|
-
def: void 0
|
|
3272
|
-
});
|
|
3273
4655
|
}
|
|
3274
4656
|
}
|
|
3275
4657
|
accessors = nextAccessors;
|
|
3276
4658
|
}
|
|
3277
4659
|
return {
|
|
3278
|
-
accessors: accessors.map((a) => a.
|
|
4660
|
+
accessors: accessors.map((a) => a.steps),
|
|
3279
4661
|
clones
|
|
3280
4662
|
};
|
|
3281
4663
|
}
|
|
3282
4664
|
/**
|
|
3283
4665
|
* Finds a property in a type tree at compile time, returning all
|
|
3284
|
-
* matching
|
|
4666
|
+
* matching accessor steps and prop definitions for further chaining.
|
|
3285
4667
|
* Returns multiple results when the same property appears in different union branches.
|
|
3286
4668
|
*/ buildPropPaths(def, propName) {
|
|
3287
4669
|
if (!def) return [];
|
|
@@ -3295,7 +4677,7 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
3295
4677
|
if ((0, _atscript_core.isStructure)(def)) {
|
|
3296
4678
|
const prop = def.props.get(propName);
|
|
3297
4679
|
if (prop) return [{
|
|
3298
|
-
|
|
4680
|
+
steps: [["props", propName]],
|
|
3299
4681
|
propDef: prop.getDefinition()
|
|
3300
4682
|
}];
|
|
3301
4683
|
return [];
|
|
@@ -3304,7 +4686,11 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
3304
4686
|
const items = def.unwrap();
|
|
3305
4687
|
const results = [];
|
|
3306
4688
|
for (let i = 0; i < items.length; i++) for (const result of this.buildPropPaths(items[i], propName)) results.push({
|
|
3307
|
-
|
|
4689
|
+
steps: [
|
|
4690
|
+
["items", i],
|
|
4691
|
+
"type",
|
|
4692
|
+
...result.steps
|
|
4693
|
+
],
|
|
3308
4694
|
propDef: result.propDef
|
|
3309
4695
|
});
|
|
3310
4696
|
return results;
|
|
@@ -3312,13 +4698,19 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
3312
4698
|
return [];
|
|
3313
4699
|
}
|
|
3314
4700
|
constructor(doc, opts) {
|
|
3315
|
-
super(doc), _define_property$3(this, "opts", void 0), _define_property$3(this, "postAnnotate", void 0), _define_property$3(this, "_adHocAnnotations", void 0), _define_property$3(this, "_propPath", void 0), _define_property$3(this, "typeIds", void 0), _define_property$3(this, "_unusedJs", void 0), _define_property$3(this, "_extendsRefs", void 0), _define_property$3(this, "_refSynthInfo", void 0), _define_property$3(this, "_synthImports", void 0), _define_property$3(this, "_synthComputed", void 0), _define_property$3(this, "_synthTaken", void 0), _define_property$3(this, "_synthAllocated", void 0), _define_property$3(this, "_synthPathCache", void 0), _define_property$3(this, "_ownerCache", void 0), this.opts = opts, this.postAnnotate = [], this._adHocAnnotations = null, this._propPath = [], this.typeIds = /* @__PURE__ */ new Map(), this._refSynthInfo = /* @__PURE__ */ new Map(), this._synthImports = /* @__PURE__ */ new Map(), this._synthComputed = false, this._synthAllocated = /* @__PURE__ */ new Map(), this._synthPathCache = /* @__PURE__ */ new Map(), this._ownerCache = /* @__PURE__ */ new Map();
|
|
4701
|
+
super(doc), _define_property$3(this, "opts", void 0), _define_property$3(this, "postAnnotate", void 0), _define_property$3(this, "_adHocAnnotations", void 0), _define_property$3(this, "_propPath", void 0), _define_property$3(this, "typeIds", void 0), _define_property$3(this, "_unusedJs", void 0), _define_property$3(this, "_extendsRefs", void 0), _define_property$3(this, "_refSynthInfo", void 0), _define_property$3(this, "_synthImports", void 0), _define_property$3(this, "_synthComputed", void 0), _define_property$3(this, "_synthTaken", void 0), _define_property$3(this, "_synthAllocated", void 0), _define_property$3(this, "_synthPathCache", void 0), _define_property$3(this, "_ownerCache", void 0), _define_property$3(this, "model", void 0), _define_property$3(this, "modelClasses", void 0), _define_property$3(this, "_declTypes", void 0), this.opts = opts, this.postAnnotate = [], this._adHocAnnotations = null, this._propPath = [], this.typeIds = /* @__PURE__ */ new Map(), this._refSynthInfo = /* @__PURE__ */ new Map(), this._synthImports = /* @__PURE__ */ new Map(), this._synthComputed = false, this._synthAllocated = /* @__PURE__ */ new Map(), this._synthPathCache = /* @__PURE__ */ new Map(), this._ownerCache = /* @__PURE__ */ new Map(), this.modelClasses = [], this._declTypes = /* @__PURE__ */ new Map();
|
|
4702
|
+
if (opts?.compileValidators) this.model = new ValidationModel();
|
|
3316
4703
|
}
|
|
3317
4704
|
};
|
|
3318
4705
|
|
|
3319
4706
|
//#endregion
|
|
3320
4707
|
//#region packages/typescript/src/plugin.ts
|
|
3321
4708
|
/**
|
|
4709
|
+
* Render formats of the JS output that override {@link TTsPluginOptions.compileValidators}
|
|
4710
|
+
* (used by `unplugin-atscript`'s `compileValidators` option).
|
|
4711
|
+
*/ const JS_COMPILED_FORMAT = "js:compiled";
|
|
4712
|
+
const JS_INTERPRETED_FORMAT = "js:interpreted";
|
|
4713
|
+
/**
|
|
3322
4714
|
* Resolves the effective JSON schema mode from plugin options.
|
|
3323
4715
|
*/ function resolveJsonSchemaMode(opts) {
|
|
3324
4716
|
if (opts?.jsonSchema !== void 0) return opts.jsonSchema;
|
|
@@ -3332,10 +4724,16 @@ const tsPlugin = (opts) => {
|
|
|
3332
4724
|
fileName: `${doc.name}.d.ts`,
|
|
3333
4725
|
content: new TypeRenderer(doc, opts).render()
|
|
3334
4726
|
}];
|
|
3335
|
-
if (format === "js")
|
|
3336
|
-
|
|
3337
|
-
|
|
3338
|
-
|
|
4727
|
+
if (format === "js" || format === JS_COMPILED_FORMAT || format === JS_INTERPRETED_FORMAT) {
|
|
4728
|
+
const jsOpts = format === "js" ? opts : {
|
|
4729
|
+
...opts,
|
|
4730
|
+
compileValidators: format === JS_COMPILED_FORMAT
|
|
4731
|
+
};
|
|
4732
|
+
return [{
|
|
4733
|
+
fileName: `${doc.name}.js`,
|
|
4734
|
+
content: new JsRenderer(doc, jsOpts).render()
|
|
4735
|
+
}];
|
|
4736
|
+
}
|
|
3339
4737
|
},
|
|
3340
4738
|
async buildEnd(output, format, repo) {
|
|
3341
4739
|
if (format === "dts" || format === _atscript_core.DEFAULT_FORMAT) {
|