@atscript/typescript 0.1.100 → 0.1.102

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 CHANGED
@@ -666,6 +666,191 @@ function renderPrimitiveTypeDef(def) {
666
666
  }
667
667
  }
668
668
 
669
+ //#endregion
670
+ //#region packages/typescript/src/runtime/validator-fast-check.ts
671
+ /**
672
+ * Allocation-free boolean pre-check used by {@link Validator.validate} before the
673
+ * error-collecting walk.
674
+ *
675
+ * It reads the type tree and metadata LIVE on every call (nothing derived from the
676
+ * type is cached), so `annotate()` / `metadata.set()` / prop mutations made after a
677
+ * validator was created are always honoured — the same as the full walk.
678
+ *
679
+ * Result contract (relative to the full walk over the same node and value):
680
+ * - `PASS` — the walk returns `true`, collects no errors and has no side effects.
681
+ * - `FAIL` — the walk returns `false`; every node the walk would visit was visited
682
+ * here too (so a walk that would throw makes this throw first) and the walk has
683
+ * no side effects for this node.
684
+ * - `BAIL` — not decidable here; the caller must run the full walk.
685
+ *
686
+ * Union branches are tried in declared order, continuing past `FAIL` and stopping
687
+ * at `BAIL`, which keeps union results identical to the walk.
688
+ */ const FAIL = 0;
689
+ const PASS = 1;
690
+ const BAIL = 2;
691
+ /** Node is at walk depth 0 (only object props / array & tuple items increase depth). */ const TOP = 1;
692
+ /** Node is inside a union branch — failures must be exhaustive (see contract). */ const IN_UNION = 2;
693
+ /**
694
+ * `true` only when the full walk over `value` is guaranteed to pass with no errors
695
+ * and no side effects. Any other outcome (including a throw) means "run the walk".
696
+ */ function fastCheckPasses(def, value, opts) {
697
+ try {
698
+ return fastCheck(def, value, TOP, opts) === PASS;
699
+ } catch {
700
+ return false;
701
+ }
702
+ }
703
+ const DECIMAL_RE = /^[+-]?\d+(\.\d+)?$/;
704
+ const regexCache = /* @__PURE__ */ new Map();
705
+ /** Compiled `@expect.pattern` regex, cached per (pattern, flags). */ function getRegex(pattern, flags) {
706
+ let byFlags = regexCache.get(pattern);
707
+ if (!byFlags) {
708
+ byFlags = /* @__PURE__ */ new Map();
709
+ regexCache.set(pattern, byFlags);
710
+ }
711
+ const flagsKey = flags || "";
712
+ let regex = byFlags.get(flagsKey);
713
+ if (!regex) {
714
+ regex = new RegExp(pattern, flags);
715
+ byFlags.set(flagsKey, regex);
716
+ }
717
+ return regex;
718
+ }
719
+ function fastCheck(def, value, flags, opts) {
720
+ if (def.optional && (value === void 0 || value === null)) return PASS;
721
+ const type = def.type;
722
+ switch (type.kind) {
723
+ case "": return fastCheckFinal(def, value);
724
+ case "object": return fastCheckObject(def, value, flags, opts);
725
+ case "array": return fastCheckArray(def, value, flags, opts);
726
+ case "union": {
727
+ const branchFlags = flags | IN_UNION;
728
+ for (const item of type.items) {
729
+ const result = fastCheck(item, value, branchFlags, opts);
730
+ if (result !== FAIL) return result;
731
+ }
732
+ return FAIL;
733
+ }
734
+ case "intersection":
735
+ for (const item of type.items) {
736
+ const result = fastCheck(item, value, flags, opts);
737
+ if (result !== PASS) return result;
738
+ }
739
+ return PASS;
740
+ case "tuple": {
741
+ const items = type.items;
742
+ if (!Array.isArray(value) || value.length !== items.length) return FAIL;
743
+ const itemFlags = flags & IN_UNION;
744
+ for (let i = 0; i < items.length; i++) {
745
+ const result = fastCheck(items[i], value[i], itemFlags, opts);
746
+ if (result !== PASS) return result;
747
+ }
748
+ return PASS;
749
+ }
750
+ default: return BAIL;
751
+ }
752
+ }
753
+ function fastCheckFinal(def, value) {
754
+ const type = def.type;
755
+ const designType = type.designType;
756
+ if (designType === "phantom") return PASS;
757
+ if (type.value !== void 0) return value === type.value ? PASS : FAIL;
758
+ switch (designType) {
759
+ case "string": return typeof value === "string" ? fastCheckString(def, value) : FAIL;
760
+ case "number": return typeof value === "number" && Number.isFinite(value) ? fastCheckNumber(def, value) : FAIL;
761
+ case "boolean":
762
+ if (typeof value !== "boolean") return FAIL;
763
+ if (def.metadata.size === 0) return PASS;
764
+ return def.metadata.get("meta.required") && value !== true ? FAIL : PASS;
765
+ case "any": return PASS;
766
+ case "never": return FAIL;
767
+ case "undefined": return value === void 0 ? PASS : FAIL;
768
+ case "null": return value === null ? PASS : FAIL;
769
+ case "decimal": return typeof value === "string" && DECIMAL_RE.test(value) ? PASS : FAIL;
770
+ default: return BAIL;
771
+ }
772
+ }
773
+ function fastCheckString(def, value) {
774
+ const metadata = def.metadata;
775
+ if (metadata.size === 0) return PASS;
776
+ if (metadata.get("meta.required") && value.trim().length === 0) return FAIL;
777
+ const minLength = metadata.get("expect.minLength");
778
+ if (minLength !== void 0 && value.length < (typeof minLength === "number" ? minLength : minLength.length)) return FAIL;
779
+ const maxLength = metadata.get("expect.maxLength");
780
+ if (maxLength !== void 0 && value.length > (typeof maxLength === "number" ? maxLength : maxLength.length)) return FAIL;
781
+ const patterns = metadata.get("expect.pattern");
782
+ if (patterns) for (const { pattern, flags } of patterns) {
783
+ if (!pattern) continue;
784
+ const regex = getRegex(pattern, flags);
785
+ if (regex.global || regex.sticky) return BAIL;
786
+ if (!regex.test(value)) return FAIL;
787
+ }
788
+ return PASS;
789
+ }
790
+ function fastCheckNumber(def, value) {
791
+ const metadata = def.metadata;
792
+ if (metadata.size === 0) return PASS;
793
+ if (metadata.get("expect.int") && value % 1 !== 0) return FAIL;
794
+ const min = metadata.get("expect.min");
795
+ if (min !== void 0 && value < (typeof min === "number" ? min : min.minValue)) return FAIL;
796
+ const max = metadata.get("expect.max");
797
+ if (max !== void 0 && value > (typeof max === "number" ? max : max.maxValue)) return FAIL;
798
+ return PASS;
799
+ }
800
+ function fastCheckObject(def, value, flags, opts) {
801
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return FAIL;
802
+ const type = def.type;
803
+ if (type.propsPatterns.length > 0) return BAIL;
804
+ const partial = opts.partial;
805
+ const skipUndefined = partial === "deep" || partial === true && (flags & TOP) !== 0;
806
+ const inUnion = (flags & IN_UNION) !== 0;
807
+ const propFlags = flags & IN_UNION;
808
+ let result = PASS;
809
+ for (const [key, item] of type.props) {
810
+ if (isPhantomType(item)) continue;
811
+ const propValue = value[key];
812
+ if (propValue === void 0 && skipUndefined) continue;
813
+ const propResult = fastCheck(item, propValue, propFlags, opts);
814
+ if (propResult !== PASS) {
815
+ if (propResult === BAIL || !inUnion) return propResult;
816
+ result = FAIL;
817
+ }
818
+ }
819
+ if (result !== PASS) return result;
820
+ const unknownProps = opts.unknownProps;
821
+ if (unknownProps === "error" || unknownProps === "strip") {
822
+ const props = type.props;
823
+ for (const key of Object.keys(value)) {
824
+ const known = props.get(key);
825
+ if (known === void 0 || isPhantomType(known)) return unknownProps === "error" ? FAIL : BAIL;
826
+ }
827
+ }
828
+ return PASS;
829
+ }
830
+ function fastCheckArray(def, value, flags, opts) {
831
+ if (!Array.isArray(value)) return FAIL;
832
+ const metadata = def.metadata;
833
+ if (metadata.size > 0) {
834
+ const minLength = metadata.get("expect.minLength");
835
+ if (minLength !== void 0 && value.length < (typeof minLength === "number" ? minLength : minLength.length)) return FAIL;
836
+ const maxLength = metadata.get("expect.maxLength");
837
+ if (maxLength !== void 0 && value.length > (typeof maxLength === "number" ? maxLength : maxLength.length)) return FAIL;
838
+ if (metadata.get("expect.array.uniqueItems")) return BAIL;
839
+ }
840
+ const of = def.type.of;
841
+ const inUnion = (flags & IN_UNION) !== 0;
842
+ const itemFlags = flags & IN_UNION;
843
+ let result = PASS;
844
+ for (const item of value) {
845
+ const itemResult = fastCheck(of, item, itemFlags, opts);
846
+ if (itemResult !== PASS) {
847
+ if (itemResult === BAIL || !inUnion) return itemResult;
848
+ result = FAIL;
849
+ }
850
+ }
851
+ return result;
852
+ }
853
+
669
854
  //#endregion
670
855
  //#region packages/typescript/src/runtime/validator.ts
671
856
  function _define_property$4(obj, key, value) {
@@ -678,7 +863,6 @@ function _define_property$4(obj, key, value) {
678
863
  else obj[key] = value;
679
864
  return obj;
680
865
  }
681
- const regexCache = /* @__PURE__ */ new Map();
682
866
  /**
683
867
  * Validates values against an {@link TAtscriptAnnotatedType} definition.
684
868
  *
@@ -699,7 +883,7 @@ const regexCache = /* @__PURE__ */ new Map();
699
883
  *
700
884
  * @typeParam T - The annotated type definition.
701
885
  * @typeParam DataType - The TypeScript type that `validate` narrows to (auto-inferred).
702
- */ var Validator = class {
886
+ */ var Validator = class Validator {
703
887
  buildPath() {
704
888
  if (this.depth <= 0) return "";
705
889
  let path = this.pathSegments[0];
@@ -753,6 +937,7 @@ const regexCache = /* @__PURE__ */ new Map();
753
937
  this.stackErrors.length = 0;
754
938
  this.depth = 0;
755
939
  this.limitExceeded = false;
940
+ if (this.shortcuts && !this.opts.skipList && typeof this.opts.partial !== "function" && fastCheckPasses(this.def, value, this.opts)) return true;
756
941
  this.context = context;
757
942
  const passed = this.validateSafe(this.def, value);
758
943
  this.context = void 0;
@@ -762,6 +947,14 @@ const regexCache = /* @__PURE__ */ new Map();
762
947
  }
763
948
  return true;
764
949
  }
950
+ /**
951
+ * Plain `Validator` without plugins / `replace`: eligible for the allocation-free
952
+ * shortcuts. Subclasses may override the protected `validate*` hooks, so they
953
+ * always take the full walk. (A getter, not a constructor-set field: validators are
954
+ * often built per call, so construction stays as cheap as before.)
955
+ */ get shortcuts() {
956
+ return this.constructor === Validator && !this.hasPlugins && !this.hasReplace;
957
+ }
765
958
  validateSafe(def, value) {
766
959
  if (this.limitExceeded) return false;
767
960
  if (this.hasReplace) {
@@ -797,6 +990,11 @@ const regexCache = /* @__PURE__ */ new Map();
797
990
  }
798
991
  validateUnion(def, value) {
799
992
  const items = def.type.items;
993
+ if (this.shortcuts) for (const item of items) {
994
+ const type = item.type;
995
+ if (item.optional || type.kind !== "" || type.value === void 0 || type.designType === "phantom") break;
996
+ if (type.value === value) return true;
997
+ }
800
998
  let details;
801
999
  for (const item of items) {
802
1000
  this.stackErrors.push(null);
@@ -840,7 +1038,7 @@ const regexCache = /* @__PURE__ */ new Map();
840
1038
  return false;
841
1039
  }
842
1040
  const minLength = def.metadata.get("expect.minLength");
843
- if (minLength) {
1041
+ if (minLength !== void 0) {
844
1042
  const length = typeof minLength === "number" ? minLength : minLength.length;
845
1043
  if (value.length < length) {
846
1044
  const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} items, got ${value.length} items`;
@@ -849,7 +1047,7 @@ const regexCache = /* @__PURE__ */ new Map();
849
1047
  }
850
1048
  }
851
1049
  const maxLength = def.metadata.get("expect.maxLength");
852
- if (maxLength) {
1050
+ if (maxLength !== void 0) {
853
1051
  const length = typeof maxLength === "number" ? maxLength : maxLength.length;
854
1052
  if (value.length > length) {
855
1053
  const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} items, got ${value.length} items`;
@@ -1021,7 +1219,7 @@ const regexCache = /* @__PURE__ */ new Map();
1021
1219
  this.error(`Expected string (decimal), got ${typeOfValue}`);
1022
1220
  return false;
1023
1221
  }
1024
- if (!/^[+-]?\d+(\.\d+)?$/.test(value)) {
1222
+ if (!DECIMAL_RE.test(value)) {
1025
1223
  this.error(`Invalid decimal format: ${JSON.stringify(value)}`);
1026
1224
  return false;
1027
1225
  }
@@ -1040,7 +1238,7 @@ const regexCache = /* @__PURE__ */ new Map();
1040
1238
  }
1041
1239
  }
1042
1240
  const minLength = def.metadata.get("expect.minLength");
1043
- if (minLength) {
1241
+ if (minLength !== void 0) {
1044
1242
  const length = typeof minLength === "number" ? minLength : minLength.length;
1045
1243
  if (value.length < length) {
1046
1244
  const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} characters, got ${value.length} characters`;
@@ -1049,7 +1247,7 @@ const regexCache = /* @__PURE__ */ new Map();
1049
1247
  }
1050
1248
  }
1051
1249
  const maxLength = def.metadata.get("expect.maxLength");
1052
- if (maxLength) {
1250
+ if (maxLength !== void 0) {
1053
1251
  const length = typeof maxLength === "number" ? maxLength : maxLength.length;
1054
1252
  if (value.length > length) {
1055
1253
  const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} characters, got ${value.length} characters`;
@@ -1060,13 +1258,7 @@ const regexCache = /* @__PURE__ */ new Map();
1060
1258
  const patterns = def.metadata.get("expect.pattern");
1061
1259
  for (const { pattern, flags, message } of patterns || []) {
1062
1260
  if (!pattern) continue;
1063
- const cacheKey = `${pattern}//${flags || ""}`;
1064
- let regex = regexCache.get(cacheKey);
1065
- if (!regex) {
1066
- regex = new RegExp(pattern, flags);
1067
- regexCache.set(cacheKey, regex);
1068
- }
1069
- if (!regex.test(value)) {
1261
+ if (!getRegex(pattern, flags).test(value)) {
1070
1262
  this.error(message || `Value is expected to match pattern "${pattern}"`);
1071
1263
  return false;
1072
1264
  }
@@ -1082,7 +1274,7 @@ const regexCache = /* @__PURE__ */ new Map();
1082
1274
  return false;
1083
1275
  }
1084
1276
  const min = def.metadata.get("expect.min");
1085
- if (min) {
1277
+ if (min !== void 0) {
1086
1278
  const minValue = typeof min === "number" ? min : min.minValue;
1087
1279
  if (value < minValue) {
1088
1280
  const message = typeof min === "object" && min.message ? min.message : `Expected minimum ${minValue}, got ${value}`;
@@ -1091,7 +1283,7 @@ const regexCache = /* @__PURE__ */ new Map();
1091
1283
  }
1092
1284
  }
1093
1285
  const max = def.metadata.get("expect.max");
1094
- if (max) {
1286
+ if (max !== void 0) {
1095
1287
  const maxValue = typeof max === "number" ? max : max.maxValue;
1096
1288
  if (value > maxValue) {
1097
1289
  const message = typeof max === "object" && max.message ? max.message : `Expected maximum ${maxValue}, got ${value}`;
@@ -1512,9 +1704,9 @@ function cloneTypeDef(type) {
1512
1704
  items: build(d.type.of)
1513
1705
  };
1514
1706
  const minLength = meta.get("expect.minLength");
1515
- if (minLength) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
1707
+ if (minLength !== void 0) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
1516
1708
  const maxLength = meta.get("expect.maxLength");
1517
- if (maxLength) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
1709
+ if (maxLength !== void 0) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
1518
1710
  return schema;
1519
1711
  },
1520
1712
  union(d) {
@@ -1557,18 +1749,18 @@ function cloneTypeDef(type) {
1557
1749
  if (schema.type === "string") {
1558
1750
  if (meta.get("meta.required")) schema.minLength = 1;
1559
1751
  const minLength = meta.get("expect.minLength");
1560
- if (minLength) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
1752
+ if (minLength !== void 0) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
1561
1753
  const maxLength = meta.get("expect.maxLength");
1562
- if (maxLength) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
1754
+ if (maxLength !== void 0) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
1563
1755
  const patterns = meta.get("expect.pattern");
1564
1756
  if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
1565
1757
  else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
1566
1758
  }
1567
1759
  if (schema.type === "number" || schema.type === "integer") {
1568
1760
  const min = meta.get("expect.min");
1569
- if (min) schema.minimum = typeof min === "number" ? min : min.minValue;
1761
+ if (min !== void 0) schema.minimum = typeof min === "number" ? min : min.minValue;
1570
1762
  const max = meta.get("expect.max");
1571
- if (max) schema.maximum = typeof max === "number" ? max : max.maxValue;
1763
+ if (max !== void 0) schema.maximum = typeof max === "number" ? max : max.maxValue;
1572
1764
  }
1573
1765
  return schema;
1574
1766
  }
package/dist/index.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
2
- const require_plugin = require('./plugin-Cu6Nez-H.cjs');
2
+ const require_plugin = require('./plugin-DORafTJq.cjs');
3
3
 
4
4
  exports.default = require_plugin.tsPlugin;
5
5
  exports.tsPlugin = require_plugin.tsPlugin;
package/dist/index.mjs CHANGED
@@ -1,3 +1,3 @@
1
- import { t as tsPlugin } from "./plugin-DG3jlbSN.mjs";
1
+ import { t as tsPlugin } from "./plugin-CECXHL6M.mjs";
2
2
 
3
3
  export { tsPlugin as default, tsPlugin };