@mionjs/run-types 0.8.8 → 0.8.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/.dist/cjs/src/createRunType.cjs +3 -1
  2. package/.dist/cjs/src/createRunType.cjs.map +1 -1
  3. package/.dist/cjs/src/createRunType.d.ts.map +1 -1
  4. package/.dist/cjs/src/jitCompilers/binary/fromBinary.cjs +1 -1
  5. package/.dist/cjs/src/jitCompilers/binary/fromBinary.cjs.map +1 -1
  6. package/.dist/cjs/src/jitCompilers/binary/fromBinary.d.ts.map +1 -1
  7. package/.dist/cjs/src/jitCompilers/binary/toBinary.cjs +1 -1
  8. package/.dist/cjs/src/jitCompilers/binary/toBinary.cjs.map +1 -1
  9. package/.dist/cjs/src/jitCompilers/binary/toBinary.d.ts.map +1 -1
  10. package/.dist/cjs/src/jitCompilers/json/stringifyJson.cjs +1 -1
  11. package/.dist/cjs/src/jitCompilers/json/stringifyJson.cjs.map +1 -1
  12. package/.dist/cjs/src/jitCompilers/json/stringifyJson.d.ts.map +1 -1
  13. package/.dist/cjs/src/mocking/mockType.cjs +46 -1
  14. package/.dist/cjs/src/mocking/mockType.cjs.map +1 -1
  15. package/.dist/cjs/src/nodes/collection/templateLiteral.cjs +73 -0
  16. package/.dist/cjs/src/nodes/collection/templateLiteral.cjs.map +1 -0
  17. package/.dist/cjs/src/nodes/collection/templateLiteral.d.ts +16 -0
  18. package/.dist/cjs/src/nodes/collection/templateLiteral.d.ts.map +1 -0
  19. package/.dist/cjs/src/nodes/member/indexProperty.cjs +69 -22
  20. package/.dist/cjs/src/nodes/member/indexProperty.cjs.map +1 -1
  21. package/.dist/cjs/src/nodes/member/indexProperty.d.ts +1 -0
  22. package/.dist/cjs/src/nodes/member/indexProperty.d.ts.map +1 -1
  23. package/.dist/esm/src/createRunType.d.ts.map +1 -1
  24. package/.dist/esm/src/createRunType.js +3 -1
  25. package/.dist/esm/src/createRunType.js.map +1 -1
  26. package/.dist/esm/src/jitCompilers/binary/fromBinary.d.ts.map +1 -1
  27. package/.dist/esm/src/jitCompilers/binary/fromBinary.js +1 -1
  28. package/.dist/esm/src/jitCompilers/binary/fromBinary.js.map +1 -1
  29. package/.dist/esm/src/jitCompilers/binary/toBinary.d.ts.map +1 -1
  30. package/.dist/esm/src/jitCompilers/binary/toBinary.js +1 -1
  31. package/.dist/esm/src/jitCompilers/binary/toBinary.js.map +1 -1
  32. package/.dist/esm/src/jitCompilers/json/stringifyJson.d.ts.map +1 -1
  33. package/.dist/esm/src/jitCompilers/json/stringifyJson.js +1 -1
  34. package/.dist/esm/src/jitCompilers/json/stringifyJson.js.map +1 -1
  35. package/.dist/esm/src/mocking/mockType.js +47 -2
  36. package/.dist/esm/src/mocking/mockType.js.map +1 -1
  37. package/.dist/esm/src/mocking/mockUtils.js +1 -1
  38. package/.dist/esm/src/nodes/collection/templateLiteral.d.ts +16 -0
  39. package/.dist/esm/src/nodes/collection/templateLiteral.d.ts.map +1 -0
  40. package/.dist/esm/src/nodes/collection/templateLiteral.js +73 -0
  41. package/.dist/esm/src/nodes/collection/templateLiteral.js.map +1 -0
  42. package/.dist/esm/src/nodes/member/indexProperty.d.ts +1 -0
  43. package/.dist/esm/src/nodes/member/indexProperty.d.ts.map +1 -1
  44. package/.dist/esm/src/nodes/member/indexProperty.js +70 -23
  45. package/.dist/esm/src/nodes/member/indexProperty.js.map +1 -1
  46. package/package.json +3 -3
  47. package/src/createRunType.ts +5 -6
  48. package/src/jitCompilers/binary/fromBinary.ts +2 -1
  49. package/src/jitCompilers/binary/toBinary.ts +2 -1
  50. package/src/jitCompilers/json/stringifyJson.ts +2 -1
  51. package/src/jitCompilers/serialization-suite.ts +52 -0
  52. package/src/jitCompilers/xyz-Template/fromXYZ.ts +2 -1
  53. package/src/jitCompilers/xyz-Template/toXYZ.ts +2 -1
  54. package/src/mocking/mockType.ts +56 -1
  55. package/src/nodes/collection/templateLiteral.ts +87 -0
  56. package/src/nodes/member/indexProperty.ts +66 -17
@@ -0,0 +1,16 @@
1
+ import { TypeTemplateLiteral } from '@deepkit/type';
2
+ import { JitFnCompiler, JitErrorsFnCompiler } from '../../lib/jitFnCompiler.ts';
3
+ import { JitCode } from '../../types.ts';
4
+ import { CollectionRunType } from '../../lib/baseRunTypes.ts';
5
+ export declare class TemplateLiteralRunType extends CollectionRunType<TypeTemplateLiteral> {
6
+ private _regexSource;
7
+ getRegexSource(): string;
8
+ private getRegexVar;
9
+ emitIsType(comp: JitFnCompiler): JitCode;
10
+ emitTypeErrors(comp: JitErrorsFnCompiler): JitCode;
11
+ emitPrepareForJson(): JitCode;
12
+ emitRestoreFromJson(): JitCode;
13
+ }
14
+ export declare function buildAnchoredTemplateRegexSource(types: TypeTemplateLiteral['types']): string;
15
+ export declare function spanToRegex(t: TypeTemplateLiteral['types'][number]): string;
16
+ //# sourceMappingURL=templateLiteral.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"templateLiteral.d.ts","sourceRoot":"","sources":["../../../../../src/nodes/collection/templateLiteral.ts"],"names":[],"mappings":"AAOA,OAAO,EAAiB,KAAK,mBAAmB,EAAmB,MAAM,eAAe,CAAC;AACzF,OAAO,KAAK,EAAC,aAAa,EAAE,mBAAmB,EAAC,MAAM,4BAA4B,CAAC;AACnF,OAAO,KAAK,EAAC,OAAO,EAAC,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAC,iBAAiB,EAAC,MAAM,2BAA2B,CAAC;AAS5D,qBAAa,sBAAuB,SAAQ,iBAAiB,CAAC,mBAAmB,CAAC;IAC9E,OAAO,CAAC,YAAY,CAAqB;IAGzC,cAAc,IAAI,MAAM;IAOxB,OAAO,CAAC,WAAW;IAQnB,UAAU,CAAC,IAAI,EAAE,aAAa,GAAG,OAAO;IAIxC,cAAc,CAAC,IAAI,EAAE,mBAAmB,GAAG,OAAO;IAQlD,kBAAkB,IAAI,OAAO;IAI7B,mBAAmB,IAAI,OAAO;CAGjC;AAGD,wBAAgB,gCAAgC,CAAC,KAAK,EAAE,mBAAmB,CAAC,OAAO,CAAC,GAAG,MAAM,CAE5F;AAGD,wBAAgB,WAAW,CAAC,CAAC,EAAE,mBAAmB,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,GAAG,MAAM,CAgB3E"}
@@ -0,0 +1,73 @@
1
+ import { ReflectionKind } from "@deepkit/type";
2
+ import { CollectionRunType } from "../../lib/baseRunTypes.js";
3
+ function __assignType(fn, args) {
4
+ fn.__type = args;
5
+ return fn;
6
+ }
7
+ class TemplateLiteralRunType extends CollectionRunType {
8
+ _regexSource;
9
+ /** Builds the anchored regex source from src.types. Memoized. */
10
+ getRegexSource() {
11
+ if (this._regexSource !== void 0)
12
+ return this._regexSource;
13
+ this._regexSource = buildAnchoredTemplateRegexSource(this.src.types || []);
14
+ return this._regexSource;
15
+ }
16
+ /** Returns a context-bound variable name holding the RegExp; emits the const into the compiler context. */
17
+ getRegexVar(comp) {
18
+ const varName = comp.getLocalVarName("reTL", this);
19
+ if (!comp.hasContextItem(varName)) {
20
+ comp.setContextItem(varName, `const ${varName} = new RegExp(${JSON.stringify(this.getRegexSource())})`);
21
+ }
22
+ return varName;
23
+ }
24
+ emitIsType(comp) {
25
+ const re = this.getRegexVar(comp);
26
+ return { code: `(typeof ${comp.vλl} === 'string' && ${re}.test(${comp.vλl}))`, type: "E" };
27
+ }
28
+ emitTypeErrors(comp) {
29
+ const re = this.getRegexVar(comp);
30
+ return {
31
+ code: `if (typeof ${comp.vλl} !== 'string' || !${re}.test(${comp.vλl})) ${comp.callJitErr(this)}`,
32
+ type: "S"
33
+ };
34
+ }
35
+ /** value is already a JSON-safe string, no transform required */
36
+ emitPrepareForJson() {
37
+ return { code: void 0, type: "S" };
38
+ }
39
+ /** value is already a JSON-safe string, no transform required */
40
+ emitRestoreFromJson() {
41
+ return { code: void 0, type: "S" };
42
+ }
43
+ static __type = ["TypeTemplateLiteral", () => CollectionRunType, "_regexSource", "getRegexSource", "JitFnCompiler", "comp", "getRegexVar", "JitCode", "emitIsType", "JitErrorsFnCompiler", "emitTypeErrors", "emitPrepareForJson", "emitRestoreFromJson", "TemplateLiteralRunType", `P"w!7"P&-J3#;P&0$P"w%2&&0';P"w%2&"w(0)P"w*2&"w(0+P"w(0,P"w(0-5"w!6"w.`];
44
+ }
45
+ function buildAnchoredTemplateRegexSource(types) {
46
+ return `^${types.map(__assignType((t) => spanToRegex(t), ["t", "", 'P"2!"/"'])).join("")}$`;
47
+ }
48
+ buildAnchoredTemplateRegexSource.__type = ["TypeTemplateLiteral", "types", "types", "buildAnchoredTemplateRegexSource", 'P"w!."f2#&/$'];
49
+ function spanToRegex(t) {
50
+ switch (t.kind) {
51
+ case ReflectionKind.literal:
52
+ return escapeForRegex(String(t.literal));
53
+ case ReflectionKind.number:
54
+ return "-?(?:\\d+\\.?\\d*|\\.\\d+)";
55
+ case ReflectionKind.string:
56
+ case ReflectionKind.any:
57
+ case ReflectionKind.infer:
58
+ return "[\\s\\S]*";
59
+ default:
60
+ throw new Error(`Unsupported template literal span kind: ${t.kind}`);
61
+ }
62
+ }
63
+ spanToRegex.__type = ["TypeTemplateLiteral", "types", "t", "spanToRegex", `P"w!."f'f2#&/$`];
64
+ function escapeForRegex(s) {
65
+ return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
66
+ }
67
+ escapeForRegex.__type = ["s", "escapeForRegex", 'P&2!&/"'];
68
+ export {
69
+ TemplateLiteralRunType,
70
+ buildAnchoredTemplateRegexSource,
71
+ spanToRegex
72
+ };
73
+ //# sourceMappingURL=templateLiteral.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"templateLiteral.js","sources":["../../../../../src/nodes/collection/templateLiteral.ts"],"sourcesContent":["/* ########\n * 2025 mion\n * Author: Ma-jerez\n * License: MIT\n * The software is provided \"as is\", without warranty of any kind.\n * ######## */\n\nimport {ReflectionKind, type TypeTemplateLiteral, type TypeLiteral} from '@deepkit/type';\nimport type {JitFnCompiler, JitErrorsFnCompiler} from '../../lib/jitFnCompiler.ts';\nimport type {JitCode} from '../../types.ts';\nimport {CollectionRunType} from '../../lib/baseRunTypes.ts';\n\n/**\n * RunType for TypeScript template literal types, ie: `type T = \\`api/user/${number}\\``.\n * The runtime value is a string. Validation is done by compiling the template literal type\n * to a single anchored regex at JIT-build time and then calling `regex.test(v)` at runtime.\n * Mocking walks the children directly (literal => verbatim, string/any => mockString, number => mockNumber)\n * and concatenates the spans, which guarantees the result satisfies the validation regex.\n */\nexport class TemplateLiteralRunType extends CollectionRunType<TypeTemplateLiteral> {\n private _regexSource: string | undefined;\n\n /** Builds the anchored regex source from src.types. Memoized. */\n getRegexSource(): string {\n if (this._regexSource !== undefined) return this._regexSource;\n this._regexSource = buildAnchoredTemplateRegexSource(this.src.types || []);\n return this._regexSource;\n }\n\n /** Returns a context-bound variable name holding the RegExp; emits the const into the compiler context. */\n private getRegexVar(comp: JitFnCompiler): string {\n const varName = comp.getLocalVarName('reTL', this);\n if (!comp.hasContextItem(varName)) {\n comp.setContextItem(varName, `const ${varName} = new RegExp(${JSON.stringify(this.getRegexSource())})`);\n }\n return varName;\n }\n\n emitIsType(comp: JitFnCompiler): JitCode {\n const re = this.getRegexVar(comp);\n return {code: `(typeof ${comp.vλl} === 'string' && ${re}.test(${comp.vλl}))`, type: 'E'};\n }\n emitTypeErrors(comp: JitErrorsFnCompiler): JitCode {\n const re = this.getRegexVar(comp);\n return {\n code: `if (typeof ${comp.vλl} !== 'string' || !${re}.test(${comp.vλl})) ${comp.callJitErr(this)}`,\n type: 'S',\n };\n }\n /** value is already a JSON-safe string, no transform required */\n emitPrepareForJson(): JitCode {\n return {code: undefined, type: 'S'};\n }\n /** value is already a JSON-safe string, no transform required */\n emitRestoreFromJson(): JitCode {\n return {code: undefined, type: 'S'};\n }\n}\n\n/** Build the full ^...$ regex source for a template literal type's spans */\nexport function buildAnchoredTemplateRegexSource(types: TypeTemplateLiteral['types']): string {\n return `^${types.map((t) => spanToRegex(t)).join('')}$`;\n}\n\n/** Translate a single template-literal span (TypeString | TypeAny | TypeNumber | TypeLiteral | TypeInfer) into regex source */\nexport function spanToRegex(t: TypeTemplateLiteral['types'][number]): string {\n switch (t.kind) {\n case ReflectionKind.literal:\n return escapeForRegex(String((t as TypeLiteral).literal));\n case ReflectionKind.number:\n // matches signed integers and floats (including leading dot like '.5'), mirroring TS's `${number}` semantics\n return '-?(?:\\\\d+\\\\.?\\\\d*|\\\\.\\\\d+)';\n case ReflectionKind.string:\n case ReflectionKind.any:\n case ReflectionKind.infer:\n // `${string}` accepts the empty string, so use * not +\n return '[\\\\s\\\\S]*';\n default:\n // unreachable per deepkit's TypeTemplateLiteral.types definition\n throw new Error(`Unsupported template literal span kind: ${(t as {kind: number}).kind}`);\n }\n}\n\n/** Escape regex metacharacters so a literal substring is matched verbatim */\nfunction escapeForRegex(s: string): string {\n return s.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n}\n"],"names":[],"mappings":";;;;;;AAmBM,MAAO,+BAA+B,kBAAsC;AAAA,EACtE;AAAA;AAAA,EAGR,iBAAc;AACV,QAAI,KAAK,iBAAiB;AAAW,aAAO,KAAK;AACjD,SAAK,eAAe,iCAAiC,KAAK,IAAI,SAAS,EAAE;AACzE,WAAO,KAAK;AAAA,EAChB;AAAA;AAAA,EAGQ,YAAY,MAAmB;AACnC,UAAM,UAAU,KAAK,gBAAgB,QAAQ,IAAI;AACjD,QAAI,CAAC,KAAK,eAAe,OAAO,GAAG;AAC/B,WAAK,eAAe,SAAS,SAAS,OAAO,iBAAiB,KAAK,UAAU,KAAK,eAAA,CAAgB,CAAC,GAAG;AAAA,IAC1G;AACA,WAAO;AAAA,EACX;AAAA,EAEA,WAAW,MAAmB;AAC1B,UAAM,KAAK,KAAK,YAAY,IAAI;AAChC,WAAO,EAAC,MAAM,WAAW,KAAK,GAAG,oBAAoB,EAAE,SAAS,KAAK,GAAG,MAAM,MAAM,IAAA;AAAA,EACxF;AAAA,EACA,eAAe,MAAyB;AACpC,UAAM,KAAK,KAAK,YAAY,IAAI;AAChC,WAAO;AAAA,MACH,MAAM,cAAc,KAAK,GAAG,qBAAqB,EAAE,SAAS,KAAK,GAAG,MAAM,KAAK,WAAW,IAAI,CAAC;AAAA,MAC/F,MAAM;AAAA,IAAA;AAAA,EAEd;AAAA;AAAA,EAEA,qBAAkB;AACd,WAAO,EAAC,MAAM,QAAW,MAAM,IAAA;AAAA,EACnC;AAAA;AAAA,EAEA,sBAAmB;AACf,WAAO,EAAC,MAAM,QAAW,MAAM,IAAA;AAAA,EACnC;AAAA;;AAIE,SAAU,iCAAiC,OAAmC;AAChF,SAAO,IAAI,MAAM,iBAAI,CAAC,MAAM,YAAY,CAAC,GAAC,CAAA,KAAA,IAAA,SAAA,CAAA,CAAA,EAAE,KAAK,EAAE,CAAC;AACxD;;AAGM,SAAU,YAAY,GAAuC;AAC/D,UAAQ,EAAE,MAAA;AAAA,IACN,KAAK,eAAe;AAChB,aAAO,eAAe,OAAQ,EAAkB,OAAO,CAAC;AAAA,IAC5D,KAAK,eAAe;AAEhB,aAAO;AAAA,IACX,KAAK,eAAe;AAAA,IACpB,KAAK,eAAe;AAAA,IACpB,KAAK,eAAe;AAEhB,aAAO;AAAA,IACX;AAEI,YAAM,IAAI,MAAM,2CAA4C,EAAqB,IAAI,EAAE;AAAA,EAAA;AAEnG;;AAGA,SAAS,eAAe,GAAS;AAC7B,SAAO,EAAE,QAAQ,uBAAuB,MAAM;AAClD;;"}
@@ -8,6 +8,7 @@ export declare class IndexSignatureRunType extends MemberRunType<TypeIndexSignat
8
8
  getChildLiteral(comp: JitFnCompiler): string;
9
9
  useArrayAccessor(): true;
10
10
  skipJit(comp: JitFnCompiler): boolean;
11
+ private getKeyPatternVar;
11
12
  emitIsType(comp: JitFnCompiler): JitCode;
12
13
  emitTypeErrors(comp: JitErrorsFnCompiler): JitCode;
13
14
  emitPrepareForJson(comp: JitFnCompiler): JitCode;
@@ -1 +1 @@
1
- {"version":3,"file":"indexProperty.d.ts","sourceRoot":"","sources":["../../../../../src/nodes/member/indexProperty.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,kBAAkB,EAAC,MAAM,eAAe,CAAC;AACjE,OAAO,EAAC,aAAa,EAAC,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAC,KAAK,OAAO,EAAC,MAAM,gBAAgB,CAAC;AAE5C,OAAO,KAAK,EAAC,aAAa,EAAE,mBAAmB,EAAC,MAAM,4BAA4B,CAAC;AAWnF,qBAAa,qBAAsB,SAAQ,aAAa,CAAC,kBAAkB,CAAC;IACxE,UAAU,IAAI,OAAO;IAGrB,eAAe,CAAC,IAAI,EAAE,aAAa,GAAG,MAAM;IAG5C,eAAe,CAAC,IAAI,EAAE,aAAa,GAAG,MAAM;IAG5C,gBAAgB,IAAI,IAAI;IAGxB,OAAO,CAAC,IAAI,EAAE,aAAa,GAAG,OAAO;IASrC,UAAU,CAAC,IAAI,EAAE,aAAa,GAAG,OAAO;IASxC,cAAc,CAAC,IAAI,EAAE,mBAAmB,GAAG,OAAO;IAMlD,kBAAkB,CAAC,IAAI,EAAE,aAAa,GAAG,OAAO;IAWhD,mBAAmB,CAAC,IAAI,EAAE,aAAa,GAAG,OAAO;IAWjD,kBAAkB,CAAC,IAAI,EAAE,aAAa,GAAG,OAAO;IAahD,oBAAoB,CAAC,IAAI,EAAE,mBAAmB,GAAG,OAAO;IAMxD,oBAAoB,CAAC,IAAI,EAAE,aAAa,GAAG,OAAO;IAMlD,0BAA0B,CAAC,IAAI,EAAE,aAAa,GAAG,OAAO;IAMxD,YAAY,CAAC,IAAI,EAAE,aAAa,EAAE,QAAQ,EAAE,OAAO,GAAG,SAAS,GAAG,OAAO;IAWzE,WAAW,CAAC,IAAI,EAAE,aAAa,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM;CAQzD"}
1
+ {"version":3,"file":"indexProperty.d.ts","sourceRoot":"","sources":["../../../../../src/nodes/member/indexProperty.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,KAAK,kBAAkB,EAA2B,MAAM,eAAe,CAAC;AAChG,OAAO,EAAC,aAAa,EAAC,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAC,KAAK,OAAO,EAAC,MAAM,gBAAgB,CAAC;AAE5C,OAAO,KAAK,EAAC,aAAa,EAAE,mBAAmB,EAAC,MAAM,4BAA4B,CAAC;AAYnF,qBAAa,qBAAsB,SAAQ,aAAa,CAAC,kBAAkB,CAAC;IACxE,UAAU,IAAI,OAAO;IAGrB,eAAe,CAAC,IAAI,EAAE,aAAa,GAAG,MAAM;IAG5C,eAAe,CAAC,IAAI,EAAE,aAAa,GAAG,MAAM;IAG5C,gBAAgB,IAAI,IAAI;IAGxB,OAAO,CAAC,IAAI,EAAE,aAAa,GAAG,OAAO;IASrC,OAAO,CAAC,gBAAgB;IAYxB,UAAU,CAAC,IAAI,EAAE,aAAa,GAAG,OAAO;IAcxC,cAAc,CAAC,IAAI,EAAE,mBAAmB,GAAG,OAAO;IAYlD,kBAAkB,CAAC,IAAI,EAAE,aAAa,GAAG,OAAO;IAahD,mBAAmB,CAAC,IAAI,EAAE,aAAa,GAAG,OAAO;IAajD,kBAAkB,CAAC,IAAI,EAAE,aAAa,GAAG,OAAO;IAmBhD,oBAAoB,CAAC,IAAI,EAAE,mBAAmB,GAAG,OAAO;IAWxD,oBAAoB,CAAC,IAAI,EAAE,aAAa,GAAG,OAAO;IAWlD,0BAA0B,CAAC,IAAI,EAAE,aAAa,GAAG,OAAO;IAWxD,YAAY,CAAC,IAAI,EAAE,aAAa,EAAE,QAAQ,EAAE,OAAO,GAAG,SAAS,GAAG,OAAO;IAWzE,WAAW,CAAC,IAAI,EAAE,aAAa,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM;CAQzD"}
@@ -1,7 +1,8 @@
1
- import { ReflectionKind, __ΩTypeIndexSignature as ___TypeIndexSignature } from "@deepkit/type";
1
+ import { ReflectionKind } from "@deepkit/type";
2
2
  import { MemberRunType } from "../../lib/baseRunTypes.js";
3
3
  import { JitFunctions } from "../../constants.functions.js";
4
4
  import { childIsExpression } from "../../lib/utils.js";
5
+ import { buildAnchoredTemplateRegexSource } from "../collection/templateLiteral.js";
5
6
  function __assignType(fn, args) {
6
7
  fn.__type = args;
7
8
  return fn;
@@ -26,23 +27,44 @@ class IndexSignatureRunType extends MemberRunType {
26
27
  }
27
28
  return false;
28
29
  }
30
+ /** if the index key is a template literal type, return the JIT context var holding the compiled key-pattern regex */
31
+ getKeyPatternVar(comp) {
32
+ const idx = this.src.index;
33
+ if (idx?.kind !== ReflectionKind.templateLiteral)
34
+ return void 0;
35
+ const varName = comp.getLocalVarName("reIdx", this);
36
+ if (!comp.hasContextItem(varName)) {
37
+ const src = buildAnchoredTemplateRegexSource(idx.types || []);
38
+ comp.setContextItem(varName, `const ${varName} = new RegExp(${JSON.stringify(src)})`);
39
+ }
40
+ return varName;
41
+ }
29
42
  // #### jit code ####
30
43
  emitIsType(comp) {
31
44
  const child = this.getJitChild(comp);
32
45
  const childJit = comp.compileIsType(child, "E");
33
- if (!childJit?.code)
46
+ const prop = this.getChildVarName(comp);
47
+ const reVar = this.getKeyPatternVar(comp);
48
+ const skipCode = this.getSkipCode(comp, prop);
49
+ const keyCheck = reVar ? `if (!${reVar}.test(${prop})) return false;` : "";
50
+ if (!childJit?.code && !keyCheck)
34
51
  return { code: void 0, type: "E" };
52
+ const valueCheck = childJit?.code ? `if (!(${childJit.code})) return false;` : "";
35
53
  return {
36
- code: `for (const ${this.getChildVarName(comp)} in ${comp.vλl}){if (!(${childJit.code})) return false;} return true;`,
54
+ code: `for (const ${prop} in ${comp.vλl}){${skipCode} ${keyCheck} ${valueCheck}} return true;`,
37
55
  type: "RB"
38
56
  };
39
57
  }
40
58
  emitTypeErrors(comp) {
41
59
  const child = this.getJitChild(comp);
42
60
  const childJit = comp.compileTypeErrors(child, "S");
43
- if (!childJit?.code)
61
+ const prop = this.getChildVarName(comp);
62
+ const reVar = this.getKeyPatternVar(comp);
63
+ const skipCode = this.getSkipCode(comp, prop);
64
+ const keyErr = reVar ? `if (!${reVar}.test(${prop})) {${comp.callJitErrWithPath(this, prop)}; continue;}` : "";
65
+ if (!childJit?.code && !keyErr)
44
66
  return { code: void 0, type: "S" };
45
- return { code: `for (const ${this.getChildVarName(comp)} in ${comp.vλl}) {${childJit.code}}`, type: "S" };
67
+ return { code: `for (const ${prop} in ${comp.vλl}) {${skipCode} ${keyErr} ${childJit?.code || ""}}`, type: "S" };
46
68
  }
47
69
  emitPrepareForJson(comp) {
48
70
  const child = this.getJitChild(comp);
@@ -52,9 +74,11 @@ class IndexSignatureRunType extends MemberRunType {
52
74
  const varName = comp.vλl;
53
75
  const prop = this.getChildVarName(comp);
54
76
  const skipCode = this.getSkipCode(comp, prop);
77
+ const reVar = this.getKeyPatternVar(comp);
78
+ const patternSkip = reVar ? `if (!${reVar}.test(${prop})) continue;` : "";
55
79
  const isExpression = childIsExpression(childJit, child);
56
80
  const code = isExpression ? `${comp.getChildVλl()} = ${childJit.code};` : childJit.code || "";
57
- return { code: `for (const ${prop} in ${varName}){${skipCode} ${code}}`, type: "S" };
81
+ return { code: `for (const ${prop} in ${varName}){${skipCode} ${patternSkip} ${code}}`, type: "S" };
58
82
  }
59
83
  emitRestoreFromJson(comp) {
60
84
  const child = this.getJitChild(comp);
@@ -64,45 +88,68 @@ class IndexSignatureRunType extends MemberRunType {
64
88
  const varName = comp.vλl;
65
89
  const prop = this.getChildVarName(comp);
66
90
  const skipCode = this.getSkipCode(comp, prop);
91
+ const reVar = this.getKeyPatternVar(comp);
92
+ const patternSkip = reVar ? `if (!${reVar}.test(${prop})) continue;` : "";
67
93
  const isExpression = childIsExpression(childJit, child);
68
94
  const code = isExpression ? `${comp.getChildVλl()} = ${childJit.code};` : childJit.code || "";
69
- return { code: `for (const ${prop} in ${varName}){${skipCode} ${code}}`, type: "S" };
95
+ return { code: `for (const ${prop} in ${varName}){${skipCode} ${patternSkip} ${code}}`, type: "S" };
70
96
  }
71
97
  emitHasUnknownKeys(comp) {
72
- if (this.getMemberType().getFamily() === "A")
73
- return { code: void 0, type: "E" };
98
+ const reVar = this.getKeyPatternVar(comp);
74
99
  const child = this.getJitChild(comp);
75
100
  const childJit = comp.compileHasUnknownKeys(child, "E");
76
- if (!childJit?.code)
77
- return { code: "", type: "E" };
101
+ if (this.getMemberType().getFamily() === "A" && !reVar)
102
+ return { code: void 0, type: "E" };
78
103
  const varName = comp.vλl;
79
104
  const prop = this.getChildVarName(comp);
80
- const resultVal = comp.getLocalVarName("res", this);
105
+ const skipCode = this.getSkipCode(comp, prop);
106
+ const patternCheck = reVar ? `if (!${reVar}.test(${prop})) return true;` : "";
107
+ const childCheck = childJit?.code ? `const ${comp.getLocalVarName("res", this)} = ${childJit.code};if (${comp.getLocalVarName("res", this)}) return true;` : "";
108
+ if (!patternCheck && !childCheck)
109
+ return { code: "", type: "E" };
81
110
  return {
82
- code: `for (const ${prop} in ${varName}) {const ${resultVal} = ${childJit.code};if (${resultVal}) return true;}return false;`,
111
+ code: `for (const ${prop} in ${varName}) {${skipCode} ${patternCheck} ${childCheck}}return false;`,
83
112
  type: "RB"
84
113
  };
85
114
  }
86
115
  emitUnknownKeyErrors(comp) {
87
- if (this.getMemberType().getFamily() === "A")
88
- return { code: void 0, type: "S" };
116
+ const reVar = this.getKeyPatternVar(comp);
89
117
  const child = this.getJitChild(comp);
90
118
  const childJit = comp.compileUnknownKeyErrors(child, "S");
91
- return this.traverseCode(comp, childJit);
119
+ if (this.getMemberType().getFamily() === "A" && !reVar)
120
+ return { code: void 0, type: "S" };
121
+ const prop = this.getChildVarName(comp);
122
+ const skipCode = this.getSkipCode(comp, prop);
123
+ const patternErr = reVar ? `if (!${reVar}.test(${prop})) {${comp.callJitErrWithPath("never", prop)}; continue;}` : "";
124
+ if (!patternErr && !childJit?.code)
125
+ return { code: void 0, type: "S" };
126
+ return { code: `for (const ${prop} in ${comp.vλl}) {${skipCode} ${patternErr} ${childJit?.code || ""}}`, type: "S" };
92
127
  }
93
128
  emitStripUnknownKeys(comp) {
94
- if (this.getMemberType().getFamily() === "A")
95
- return { code: void 0, type: "S" };
129
+ const reVar = this.getKeyPatternVar(comp);
96
130
  const child = this.getJitChild(comp);
97
131
  const childJit = comp.compileStripUnknownKeys(child, "S");
98
- return this.traverseCode(comp, childJit);
132
+ if (this.getMemberType().getFamily() === "A" && !reVar)
133
+ return { code: void 0, type: "S" };
134
+ const prop = this.getChildVarName(comp);
135
+ const skipCode = this.getSkipCode(comp, prop);
136
+ const patternStrip = reVar ? `if (!${reVar}.test(${prop})) {delete ${comp.vλl}[${prop}]; continue;}` : "";
137
+ if (!patternStrip && !childJit?.code)
138
+ return { code: void 0, type: "S" };
139
+ return { code: `for (const ${prop} in ${comp.vλl}) {${skipCode} ${patternStrip} ${childJit?.code || ""}}`, type: "S" };
99
140
  }
100
141
  emitUnknownKeysToUndefined(comp) {
101
- if (this.getMemberType().getFamily() === "A")
102
- return { code: void 0, type: "S" };
142
+ const reVar = this.getKeyPatternVar(comp);
103
143
  const child = this.getJitChild(comp);
104
144
  const childJit = comp.compileUnknownKeysToUndefined(child, "S");
105
- return this.traverseCode(comp, childJit);
145
+ if (this.getMemberType().getFamily() === "A" && !reVar)
146
+ return { code: void 0, type: "S" };
147
+ const prop = this.getChildVarName(comp);
148
+ const skipCode = this.getSkipCode(comp, prop);
149
+ const patternUndef = reVar ? `if (!${reVar}.test(${prop})) {${comp.vλl}[${prop}] = undefined; continue;}` : "";
150
+ if (!patternUndef && !childJit?.code)
151
+ return { code: void 0, type: "S" };
152
+ return { code: `for (const ${prop} in ${comp.vλl}) {${skipCode} ${patternUndef} ${childJit?.code || ""}}`, type: "S" };
106
153
  }
107
154
  traverseCode(comp, childJit) {
108
155
  if (!childJit?.code)
@@ -122,7 +169,7 @@ class IndexSignatureRunType extends MemberRunType {
122
169
  const skipNames = namedChildren.length ? namedChildren.map(__assignType((child) => `${child.getChildLiteral(comp)} === ${prop}`, ["child", "", 'P"2!"/"'])).join(" || ") : "";
123
170
  return namedChildren.length ? `if (${skipNames}) continue;` : "";
124
171
  }
125
- static __type = [() => ___TypeIndexSignature, () => MemberRunType, "isOptional", "JitFnCompiler", "comp", "getChildVarName", "getChildLiteral", true, "useArrayAccessor", "skipJit", "JitCode", "emitIsType", "JitErrorsFnCompiler", "emitTypeErrors", "emitPrepareForJson", "emitRestoreFromJson", "emitHasUnknownKeys", "emitUnknownKeyErrors", "emitStripUnknownKeys", "emitUnknownKeysToUndefined", "childJit", "traverseCode", "prop", "getSkipCode", () => ___TypeIndexSignature, "IndexSignatureRunType", `Pn!7"P)0#P"w$2%&0&P"w$2%&0'P.(0)P"w$2%)0*P"w$2%"w+0,P"w-2%"w+0.P"w$2%"w+0/P"w$2%"w+00P"w$2%"w+01P"w-2%"w+02P"w$2%"w+03P"w$2%"w+04P"w$2%P"w+-J25"w+06P"w$2%&27&085n96"w:`];
172
+ static __type = ["TypeIndexSignature", () => MemberRunType, "isOptional", "JitFnCompiler", "comp", "getChildVarName", "getChildLiteral", true, "useArrayAccessor", "skipJit", "getKeyPatternVar", "JitCode", "emitIsType", "JitErrorsFnCompiler", "emitTypeErrors", "emitPrepareForJson", "emitRestoreFromJson", "emitHasUnknownKeys", "emitUnknownKeyErrors", "emitStripUnknownKeys", "emitUnknownKeysToUndefined", "childJit", "traverseCode", "prop", "getSkipCode", "IndexSignatureRunType", `P"w!7"P)0#P"w$2%&0&P"w$2%&0'P.(0)P"w$2%)0*P"w$2%P&-J0+;P"w$2%"w,0-P"w.2%"w,0/P"w$2%"w,00P"w$2%"w,01P"w$2%"w,02P"w.2%"w,03P"w$2%"w,04P"w$2%"w,05P"w$2%P"w,-J26"w,07P"w$2%&28&095"w!6"w:`];
126
173
  }
127
174
  export {
128
175
  IndexSignatureRunType
@@ -1 +1 @@
1
- {"version":3,"file":"indexProperty.js","sources":["../../../../../src/nodes/member/indexProperty.ts"],"sourcesContent":["import {ReflectionKind, TypeIndexSignature} from '@deepkit/type';\nimport {MemberRunType} from '../../lib/baseRunTypes.ts';\nimport {type JitCode} from '../../types.ts';\nimport {JitFunctions} from '../../constants.functions.ts';\nimport type {JitFnCompiler, JitErrorsFnCompiler} from '../../lib/jitFnCompiler.ts';\nimport {InterfaceRunType} from '../collection/interface.ts';\nimport {childIsExpression} from '../../lib/utils.ts';\n\n/* ########\n * 2024 mion\n * Author: Ma-jerez\n * License: MIT\n * The software is provided \"as is\", without warranty of any kind.\n * ######## */\n\nexport class IndexSignatureRunType extends MemberRunType<TypeIndexSignature> {\n isOptional(): boolean {\n return true;\n }\n getChildVarName(comp: JitFnCompiler): string {\n return comp.getLocalVarName('p', this);\n }\n getChildLiteral(comp: JitFnCompiler): string {\n return this.getChildVarName(comp);\n }\n useArrayAccessor(): true {\n return true;\n }\n skipJit(comp: JitFnCompiler): boolean {\n const index = (this.src as TypeIndexSignature).index?.kind;\n if (index === ReflectionKind.symbol) {\n return comp?.fnID !== JitFunctions.toJSCode.id;\n }\n return false;\n }\n\n // #### jit code ####\n emitIsType(comp: JitFnCompiler): JitCode {\n const child = this.getJitChild(comp);\n const childJit = comp.compileIsType(child, 'E');\n if (!childJit?.code) return {code: undefined, type: 'E'};\n return {\n code: `for (const ${this.getChildVarName(comp)} in ${comp.vλl}){if (!(${childJit.code})) return false;} return true;`,\n type: 'RB',\n };\n }\n emitTypeErrors(comp: JitErrorsFnCompiler): JitCode {\n const child = this.getJitChild(comp);\n const childJit = comp.compileTypeErrors(child, 'S');\n if (!childJit?.code) return {code: undefined, type: 'S'};\n return {code: `for (const ${this.getChildVarName(comp)} in ${comp.vλl}) {${childJit.code}}`, type: 'S'};\n }\n emitPrepareForJson(comp: JitFnCompiler): JitCode {\n const child = this.getJitChild(comp);\n const childJit = comp.compilePrepareForJson(child, 'S');\n if (!child || !childJit?.code) return {code: undefined, type: 'S'};\n const varName = comp.vλl;\n const prop = this.getChildVarName(comp);\n const skipCode = this.getSkipCode(comp, prop);\n const isExpression = childIsExpression(childJit, child);\n const code = isExpression ? `${comp.getChildVλl()} = ${childJit.code};` : childJit.code || '';\n return {code: `for (const ${prop} in ${varName}){${skipCode} ${code}}`, type: 'S'};\n }\n emitRestoreFromJson(comp: JitFnCompiler): JitCode {\n const child = this.getJitChild(comp);\n const childJit = comp.compileRestoreFromJson(child, 'S');\n if (!child || !childJit?.code) return {code: undefined, type: 'S'};\n const varName = comp.vλl;\n const prop = this.getChildVarName(comp);\n const skipCode = this.getSkipCode(comp, prop);\n const isExpression = childIsExpression(childJit, child);\n const code = isExpression ? `${comp.getChildVλl()} = ${childJit.code};` : childJit.code || '';\n return {code: `for (const ${prop} in ${varName}){${skipCode} ${code}}`, type: 'S'};\n }\n emitHasUnknownKeys(comp: JitFnCompiler): JitCode {\n if (this.getMemberType().getFamily() === 'A') return {code: undefined, type: 'E'};\n const child = this.getJitChild(comp);\n const childJit = comp.compileHasUnknownKeys(child, 'E');\n if (!childJit?.code) return {code: '', type: 'E'};\n const varName = comp.vλl;\n const prop = this.getChildVarName(comp);\n const resultVal = comp.getLocalVarName('res', this);\n return {\n code: `for (const ${prop} in ${varName}) {const ${resultVal} = ${childJit.code};if (${resultVal}) return true;}return false;`,\n type: 'RB',\n };\n }\n emitUnknownKeyErrors(comp: JitErrorsFnCompiler): JitCode {\n if (this.getMemberType().getFamily() === 'A') return {code: undefined, type: 'S'};\n const child = this.getJitChild(comp);\n const childJit = comp.compileUnknownKeyErrors(child, 'S');\n return this.traverseCode(comp, childJit);\n }\n emitStripUnknownKeys(comp: JitFnCompiler): JitCode {\n if (this.getMemberType().getFamily() === 'A') return {code: undefined, type: 'S'};\n const child = this.getJitChild(comp);\n const childJit = comp.compileStripUnknownKeys(child, 'S');\n return this.traverseCode(comp, childJit);\n }\n emitUnknownKeysToUndefined(comp: JitFnCompiler): JitCode {\n if (this.getMemberType().getFamily() === 'A') return {code: undefined, type: 'S'};\n const child = this.getJitChild(comp);\n const childJit = comp.compileUnknownKeysToUndefined(child, 'S');\n return this.traverseCode(comp, childJit);\n }\n traverseCode(comp: JitFnCompiler, childJit: JitCode | undefined): JitCode {\n if (!childJit?.code) return {code: undefined, type: 'S'};\n const prop = this.getChildVarName(comp);\n return {code: `for (const ${prop} in ${comp.vλl}) {${childJit.code}}`, type: 'S'};\n }\n /**\n * if index property should be skipped then it output some code to skip it,\n * this happen when an object/interface has an index property but also has named properties\n * that might collide with the index property. ie {[key: string]: string, a: string}\n * when executing the logic for the index property we need to skip the named properties.\n */\n getSkipCode(comp: JitFnCompiler, prop: string): string {\n const parent = this.getParent() as InterfaceRunType;\n const namedChildren = parent.getNamedChildren(comp);\n const skipNames = namedChildren.length\n ? namedChildren.map((child) => `${child.getChildLiteral(comp)} === ${prop}`).join(' || ')\n : '';\n return namedChildren.length ? `if (${skipNames}) continue;` : '';\n }\n}\n"],"names":[],"mappings":";;;;;;;;AAeM,MAAO,8BAA8B,cAAiC;AAAA,EACxE,aAAU;AACN,WAAO;AAAA,EACX;AAAA,EACA,gBAAgB,MAAmB;AAC/B,WAAO,KAAK,gBAAgB,KAAK,IAAI;AAAA,EACzC;AAAA,EACA,gBAAgB,MAAmB;AAC/B,WAAO,KAAK,gBAAgB,IAAI;AAAA,EACpC;AAAA,EACA,mBAAgB;AACZ,WAAO;AAAA,EACX;AAAA,EACA,QAAQ,MAAmB;AACvB,UAAM,QAAS,KAAK,IAA2B,OAAO;AACtD,QAAI,UAAU,eAAe,QAAQ;AACjC,aAAO,MAAM,SAAS,aAAa,SAAS;AAAA,IAChD;AACA,WAAO;AAAA,EACX;AAAA;AAAA,EAGA,WAAW,MAAmB;AAC1B,UAAM,QAAQ,KAAK,YAAY,IAAI;AACnC,UAAM,WAAW,KAAK,cAAc,OAAO,GAAG;AAC9C,QAAI,CAAC,UAAU;AAAM,aAAO,EAAC,MAAM,QAAW,MAAM,IAAA;AACpD,WAAO;AAAA,MACH,MAAM,cAAc,KAAK,gBAAgB,IAAI,CAAC,OAAO,KAAK,GAAG,WAAW,SAAS,IAAI;AAAA,MACrF,MAAM;AAAA,IAAA;AAAA,EAE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AK,YAAY,IAAI;AACnC,UAAM,WAAW,KAAK,8BAA8B,OAAO,GAAG;AAC9D,WAAO,KAAK,aAAa,MAAM,QAAQ;AAAA,EAC3C;AAAA,EACA,aAAa,MAAqB,UAA6B;AAC3D,QAAI,CAAC,UAAU;AAAM,aAAO,EAAC,MAAM,QAAW,MAAM,IAAA;AACpD,UAAM,OAAO,KAAK,gBAAgB,IAAI;AACtC,WAAO,EAAC,MAAM,cAAc,IAAI,OAAO,KAAK,GAAG,MAAM,SAAS,IAAI,KAAK,MAAM,IAAA;AAAA,EACjF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,YAAY,MAAqB,MAAY;AACzC,UAAM,SAAS,KAAK,UAAA;AACpB,UAAM,gBAAgB,OAAO,iBAAiB,IAAI;AAClD,UAAM,YAAY,cAAc,SAC1B,cAAc,IAAG,aAAC,CAAC,UAAU,GAAG,MAAM,gBAAgB,IAAI,CAAC,QAAQ,IAAI,IAAE,CAAA,SAAA,IAAA,SAAA,CAAA,CAAA,EAAE,KAAK,MAAM,IACtF;AACN,WAAO,cAAc,SAAS,OAAO,SAAS,gBAAgB;AAAA,EAClE;AAAA;;"}
1
+ {"version":3,"file":"indexProperty.js","sources":["../../../../../src/nodes/member/indexProperty.ts"],"sourcesContent":["import {ReflectionKind, type TypeIndexSignature, type TypeTemplateLiteral} from '@deepkit/type';\nimport {MemberRunType} from '../../lib/baseRunTypes.ts';\nimport {type JitCode} from '../../types.ts';\nimport {JitFunctions} from '../../constants.functions.ts';\nimport type {JitFnCompiler, JitErrorsFnCompiler} from '../../lib/jitFnCompiler.ts';\nimport {InterfaceRunType} from '../collection/interface.ts';\nimport {childIsExpression} from '../../lib/utils.ts';\nimport {buildAnchoredTemplateRegexSource} from '../collection/templateLiteral.ts';\n\n/* ########\n * 2024 mion\n * Author: Ma-jerez\n * License: MIT\n * The software is provided \"as is\", without warranty of any kind.\n * ######## */\n\nexport class IndexSignatureRunType extends MemberRunType<TypeIndexSignature> {\n isOptional(): boolean {\n return true;\n }\n getChildVarName(comp: JitFnCompiler): string {\n return comp.getLocalVarName('p', this);\n }\n getChildLiteral(comp: JitFnCompiler): string {\n return this.getChildVarName(comp);\n }\n useArrayAccessor(): true {\n return true;\n }\n skipJit(comp: JitFnCompiler): boolean {\n const index = (this.src as TypeIndexSignature).index?.kind;\n if (index === ReflectionKind.symbol) {\n return comp?.fnID !== JitFunctions.toJSCode.id;\n }\n return false;\n }\n\n /** if the index key is a template literal type, return the JIT context var holding the compiled key-pattern regex */\n private getKeyPatternVar(comp: JitFnCompiler): string | undefined {\n const idx = this.src.index;\n if (idx?.kind !== ReflectionKind.templateLiteral) return undefined;\n const varName = comp.getLocalVarName('reIdx', this);\n if (!comp.hasContextItem(varName)) {\n const src = buildAnchoredTemplateRegexSource((idx as TypeTemplateLiteral).types || []);\n comp.setContextItem(varName, `const ${varName} = new RegExp(${JSON.stringify(src)})`);\n }\n return varName;\n }\n\n // #### jit code ####\n emitIsType(comp: JitFnCompiler): JitCode {\n const child = this.getJitChild(comp);\n const childJit = comp.compileIsType(child, 'E');\n const prop = this.getChildVarName(comp);\n const reVar = this.getKeyPatternVar(comp);\n const skipCode = this.getSkipCode(comp, prop);\n const keyCheck = reVar ? `if (!${reVar}.test(${prop})) return false;` : '';\n if (!childJit?.code && !keyCheck) return {code: undefined, type: 'E'};\n const valueCheck = childJit?.code ? `if (!(${childJit.code})) return false;` : '';\n return {\n code: `for (const ${prop} in ${comp.vλl}){${skipCode} ${keyCheck} ${valueCheck}} return true;`,\n type: 'RB',\n };\n }\n emitTypeErrors(comp: JitErrorsFnCompiler): JitCode {\n const child = this.getJitChild(comp);\n const childJit = comp.compileTypeErrors(child, 'S');\n const prop = this.getChildVarName(comp);\n const reVar = this.getKeyPatternVar(comp);\n const skipCode = this.getSkipCode(comp, prop);\n // when the key fails the template literal pattern, report it (with the offending key in the path)\n // and skip the value check to avoid compounding errors on values whose key was already invalid\n const keyErr = reVar ? `if (!${reVar}.test(${prop})) {${comp.callJitErrWithPath(this, prop)}; continue;}` : '';\n if (!childJit?.code && !keyErr) return {code: undefined, type: 'S'};\n return {code: `for (const ${prop} in ${comp.vλl}) {${skipCode} ${keyErr} ${childJit?.code || ''}}`, type: 'S'};\n }\n emitPrepareForJson(comp: JitFnCompiler): JitCode {\n const child = this.getJitChild(comp);\n const childJit = comp.compilePrepareForJson(child, 'S');\n if (!child || !childJit?.code) return {code: undefined, type: 'S'};\n const varName = comp.vλl;\n const prop = this.getChildVarName(comp);\n const skipCode = this.getSkipCode(comp, prop);\n const reVar = this.getKeyPatternVar(comp);\n const patternSkip = reVar ? `if (!${reVar}.test(${prop})) continue;` : '';\n const isExpression = childIsExpression(childJit, child);\n const code = isExpression ? `${comp.getChildVλl()} = ${childJit.code};` : childJit.code || '';\n return {code: `for (const ${prop} in ${varName}){${skipCode} ${patternSkip} ${code}}`, type: 'S'};\n }\n emitRestoreFromJson(comp: JitFnCompiler): JitCode {\n const child = this.getJitChild(comp);\n const childJit = comp.compileRestoreFromJson(child, 'S');\n if (!child || !childJit?.code) return {code: undefined, type: 'S'};\n const varName = comp.vλl;\n const prop = this.getChildVarName(comp);\n const skipCode = this.getSkipCode(comp, prop);\n const reVar = this.getKeyPatternVar(comp);\n const patternSkip = reVar ? `if (!${reVar}.test(${prop})) continue;` : '';\n const isExpression = childIsExpression(childJit, child);\n const code = isExpression ? `${comp.getChildVλl()} = ${childJit.code};` : childJit.code || '';\n return {code: `for (const ${prop} in ${varName}){${skipCode} ${patternSkip} ${code}}`, type: 'S'};\n }\n emitHasUnknownKeys(comp: JitFnCompiler): JitCode {\n const reVar = this.getKeyPatternVar(comp);\n const child = this.getJitChild(comp);\n const childJit = comp.compileHasUnknownKeys(child, 'E');\n // when the value is atomic and there's no key pattern, every key is \"known\" -> no check needed\n if (this.getMemberType().getFamily() === 'A' && !reVar) return {code: undefined, type: 'E'};\n const varName = comp.vλl;\n const prop = this.getChildVarName(comp);\n const skipCode = this.getSkipCode(comp, prop);\n const patternCheck = reVar ? `if (!${reVar}.test(${prop})) return true;` : '';\n const childCheck = childJit?.code\n ? `const ${comp.getLocalVarName('res', this)} = ${childJit.code};if (${comp.getLocalVarName('res', this)}) return true;`\n : '';\n if (!patternCheck && !childCheck) return {code: '', type: 'E'};\n return {\n code: `for (const ${prop} in ${varName}) {${skipCode} ${patternCheck} ${childCheck}}return false;`,\n type: 'RB',\n };\n }\n emitUnknownKeyErrors(comp: JitErrorsFnCompiler): JitCode {\n const reVar = this.getKeyPatternVar(comp);\n const child = this.getJitChild(comp);\n const childJit = comp.compileUnknownKeyErrors(child, 'S');\n if (this.getMemberType().getFamily() === 'A' && !reVar) return {code: undefined, type: 'S'};\n const prop = this.getChildVarName(comp);\n const skipCode = this.getSkipCode(comp, prop);\n const patternErr = reVar ? `if (!${reVar}.test(${prop})) {${comp.callJitErrWithPath('never', prop)}; continue;}` : '';\n if (!patternErr && !childJit?.code) return {code: undefined, type: 'S'};\n return {code: `for (const ${prop} in ${comp.vλl}) {${skipCode} ${patternErr} ${childJit?.code || ''}}`, type: 'S'};\n }\n emitStripUnknownKeys(comp: JitFnCompiler): JitCode {\n const reVar = this.getKeyPatternVar(comp);\n const child = this.getJitChild(comp);\n const childJit = comp.compileStripUnknownKeys(child, 'S');\n if (this.getMemberType().getFamily() === 'A' && !reVar) return {code: undefined, type: 'S'};\n const prop = this.getChildVarName(comp);\n const skipCode = this.getSkipCode(comp, prop);\n const patternStrip = reVar ? `if (!${reVar}.test(${prop})) {delete ${comp.vλl}[${prop}]; continue;}` : '';\n if (!patternStrip && !childJit?.code) return {code: undefined, type: 'S'};\n return {code: `for (const ${prop} in ${comp.vλl}) {${skipCode} ${patternStrip} ${childJit?.code || ''}}`, type: 'S'};\n }\n emitUnknownKeysToUndefined(comp: JitFnCompiler): JitCode {\n const reVar = this.getKeyPatternVar(comp);\n const child = this.getJitChild(comp);\n const childJit = comp.compileUnknownKeysToUndefined(child, 'S');\n if (this.getMemberType().getFamily() === 'A' && !reVar) return {code: undefined, type: 'S'};\n const prop = this.getChildVarName(comp);\n const skipCode = this.getSkipCode(comp, prop);\n const patternUndef = reVar ? `if (!${reVar}.test(${prop})) {${comp.vλl}[${prop}] = undefined; continue;}` : '';\n if (!patternUndef && !childJit?.code) return {code: undefined, type: 'S'};\n return {code: `for (const ${prop} in ${comp.vλl}) {${skipCode} ${patternUndef} ${childJit?.code || ''}}`, type: 'S'};\n }\n traverseCode(comp: JitFnCompiler, childJit: JitCode | undefined): JitCode {\n if (!childJit?.code) return {code: undefined, type: 'S'};\n const prop = this.getChildVarName(comp);\n return {code: `for (const ${prop} in ${comp.vλl}) {${childJit.code}}`, type: 'S'};\n }\n /**\n * if index property should be skipped then it output some code to skip it,\n * this happen when an object/interface has an index property but also has named properties\n * that might collide with the index property. ie {[key: string]: string, a: string}\n * when executing the logic for the index property we need to skip the named properties.\n */\n getSkipCode(comp: JitFnCompiler, prop: string): string {\n const parent = this.getParent() as InterfaceRunType;\n const namedChildren = parent.getNamedChildren(comp);\n const skipNames = namedChildren.length\n ? namedChildren.map((child) => `${child.getChildLiteral(comp)} === ${prop}`).join(' || ')\n : '';\n return namedChildren.length ? `if (${skipNames}) continue;` : '';\n }\n}\n"],"names":[],"mappings":";;;;;;;;;AAgBM,MAAO,8BAA8B,cAAiC;AAAA,EACxE,aAAU;AACN,WAAO;AAAA,EACX;AAAA,EACA,gBAAgB,MAAmB;AAC/B,WAAO,KAAK,gBAAgB,KAAK,IAAI;AAAA,EACzC;AAAA,EACA,gBAAgB,MAAmB;AAC/B,WAAO,KAAK,gBAAgB,IAAI;AAAA,EACpC;AAAA,EACA,mBAAgB;AACZ,WAAO;AAAA,EACX;AAAA,EACA,QAAQ,MAAmB;AACvB,UAAM,QAAS,KAAK,IAA2B,OAAO;AACtD,QAAI,UAAU,eAAe,QAAQ;AACjC,aAAO,MAAM,SAAS,aAAa,SAAS;AAAA,IAChD;AACA,WAAO;AAAA,EACX;AAAA;AAAA,EAGQ,iBAAiB,MAAmB;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O,CAAC;AAAO,aAAO,EAAC,MAAM,QAAW,MAAM,IAAA;AACvF,UAAM,OAAO,KAAK,gBAAgB,IAAI;AACtC,UAAM,WAAW,KAAK,YAAY,MAAM,IAAI;AAC5C,UAAM,eAAe,QAAQ,QAAQ,KAAK,SAAS,IAAI,OAAO,KAAK,GAAG,IAAI,IAAI,8BAA8B;AAC5G,QAAI,CAAC,gBAAgB,CAAC,UAAU;AAAM,aAAO,EAAC,MAAM,QAAW,MAAM,IAAA;AACrE,WAAO,EAAC,MAAM,cAAc,IAAI,OAAO,KAAK,GAAG,MAAM,QAAQ,IAAI,YAAY,IAAI,UAAU,QAAQ,EAAE,KAAK,MAAM,IAAA;AAAA,EACpH;AAAA,EACA,aAAa,MAAqB,UAA6B;AAC3D,QAAI,CAAC,UAAU;AAAM,aAAO,EAAC,MAAM,QAAW,MAAM,IAAA;AACpD,UAAM,OAAO,KAAK,gBAAgB,IAAI;AACtC,WAAO,EAAC,MAAM,cAAc,IAAI,OAAO,KAAK,GAAG,MAAM,SAAS,IAAI,KAAK,MAAM,IAAA;AAAA,EACjF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,YAAY,MAAqB,MAAY;AACzC,UAAM,SAAS,KAAK,UAAA;AACpB,UAAM,gBAAgB,OAAO,iBAAiB,IAAI;AAClD,UAAM,YAAY,cAAc,SAC1B,cAAc,IAAG,aAAC,CAAC,UAAU,GAAG,MAAM,gBAAgB,IAAI,CAAC,QAAQ,IAAI,IAAE,CAAA,SAAA,IAAA,SAAA,CAAA,CAAA,EAAE,KAAK,MAAM,IACtF;AACN,WAAO,cAAc,SAAS,OAAO,SAAS,gBAAgB;AAAA,EAClE;AAAA;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mionjs/run-types",
3
- "version": "0.8.8",
3
+ "version": "0.8.10",
4
4
  "type": "module",
5
5
  "description": "Run time functionality directly from typescript types",
6
6
  "keywords": [
@@ -64,10 +64,10 @@
64
64
  "dependencies": {
65
65
  "@deepkit/core": "1.0.19",
66
66
  "@deepkit/type": "1.0.19",
67
- "@mionjs/core": "0.8.8"
67
+ "@mionjs/core": "0.8.10"
68
68
  },
69
69
  "peerDependencies": {
70
70
  "typescript": ">=6.0.0"
71
71
  },
72
- "gitHead": "a92a438011336a078d394b75054422f3a56f8468"
72
+ "gitHead": "3e2b3b633ceb0b054fc1f1aad914d779ddd6c8b0"
73
73
  }
@@ -52,6 +52,7 @@ import {FunctionRunType} from './nodes/function/function.ts';
52
52
  import {PromiseRunType} from './nodes/native/promise.ts';
53
53
  import {ObjectRunType} from './nodes/atomic/object.ts';
54
54
  import {IntersectionRunType} from './nodes/collection/intersection.ts';
55
+ import {TemplateLiteralRunType} from './nodes/collection/templateLiteral.ts';
55
56
  import {ParameterRunType} from './nodes/member/param.ts';
56
57
  import {MethodRunType} from './nodes/member/method.ts';
57
58
  import {RestParamsRunType} from './nodes/member/restParams.ts';
@@ -197,12 +198,10 @@ function createRunType(deepkitType: Mutable<SrcType>): RunType {
197
198
  rt = new SymbolRunType();
198
199
  break;
199
200
  case ReflectionKind.templateLiteral:
200
- // deepkit automatically resolves template literals unions to literals
201
- // this is only called when you define the type of a template literal i.e: type T = `foo${string}`;
202
- // this is not supported at the moment but would be useful for type safe urls etc
203
- throw new Error(
204
- 'Template Literals are resolved by the compiler to Literals ie: const tl = `${string}World`. Template literal types are not supported. ie type TL = `${string}World`'
205
- );
201
+ // deepkit resolves finite template literal unions to literal-string unions before reaching here.
202
+ // this branch handles the open form ie: type T = `api/user/${number}` (children: literal/string/number/any/infer)
203
+ rt = new TemplateLiteralRunType();
204
+ break;
206
205
  case ReflectionKind.undefined:
207
206
  rt = new UndefinedRunType();
208
207
  break;
@@ -84,7 +84,8 @@ export function emitFromBinary(runType: BaseRunType, comp: BinaryCompiler): JitC
84
84
  case ReflectionKind.never:
85
85
  throw new Error('Never type cannot be deserialized from Binary');
86
86
  case ReflectionKind.templateLiteral:
87
- throw new Error('Template literals are not supported in Binary deserialization');
87
+ // runtime value is a plain string
88
+ return {code: `${dεs}.desString()`, type: 'E'};
88
89
  case ReflectionKind.literal: {
89
90
  if (comp.opts.noLiterals) {
90
91
  const lit = (runType as LiteralRunType).src.literal;
@@ -81,7 +81,8 @@ export function emitToBinary(runType: BaseRunType, comp: BinaryCompiler): JitCod
81
81
  case ReflectionKind.never:
82
82
  throw new Error('Never type cannot be serialized to Binary');
83
83
  case ReflectionKind.templateLiteral:
84
- throw new Error('Template literals are not supported in Binary serialization');
84
+ // runtime value is a plain string
85
+ return {code: `${sεr}.serString(${comp.vλl})`, type: 'S'};
85
86
  case ReflectionKind.literal: {
86
87
  if (comp.opts.noLiterals) {
87
88
  const lit = (runType as LiteralRunType).src.literal;
@@ -108,7 +108,8 @@ export function createStringifyCompiler(fnID: Operation) {
108
108
  case ReflectionKind.symbol:
109
109
  return {code: `JSON.stringify('Symbol:' + (${comp.vλl}.description || ''))`, type: 'E'};
110
110
  case ReflectionKind.templateLiteral:
111
- throw new Error('Template Literals are not supported.');
111
+ // runtime value is a plain string
112
+ return {code: `JSON.stringify(${comp.vλl})`, type: 'E'};
112
113
  case ReflectionKind.undefined: {
113
114
  const isRoot = comp.getNestLevel(runType) === 0;
114
115
  if (isRoot) return {code: `undefined`, type: 'E'};
@@ -1592,6 +1592,58 @@ export const SERIALIZATION_SPEC = {
1592
1592
  },
1593
1593
  },
1594
1594
  },
1595
+ TEMPLATE_LITERALS: {
1596
+ url_string: {
1597
+ title: 'template literal as string type, ie: `api/users/${number}`',
1598
+ getTestData: (dataOnly = false) => {
1599
+ type GetUserUrl = `api/users/${number}`;
1600
+ const rt = dataOnly ? (null as any) : runType<GetUserUrl>();
1601
+ const values: GetUserUrl[] = [
1602
+ 'api/users/0',
1603
+ 'api/users/1',
1604
+ 'api/users/42',
1605
+ 'api/users/-7',
1606
+ 'api/users/3.14',
1607
+ `api/users/${Number.MAX_SAFE_INTEGER}`,
1608
+ ];
1609
+ return {rt, values};
1610
+ },
1611
+ },
1612
+ url_in_object: {
1613
+ title: 'template literal as object property type',
1614
+ getTestData: (dataOnly = false) => {
1615
+ type ObjectWithTemplate = {
1616
+ url: `api/user/${number}`;
1617
+ method: string;
1618
+ };
1619
+ const rt = dataOnly ? (null as any) : runType<ObjectWithTemplate>();
1620
+ const values: ObjectWithTemplate[] = [
1621
+ {url: 'api/user/1', method: 'GET'},
1622
+ {url: 'api/user/42', method: 'POST'},
1623
+ {url: 'api/user/-7', method: 'DELETE'},
1624
+ ];
1625
+ return {rt, values};
1626
+ },
1627
+ },
1628
+ url_index_key: {
1629
+ title: 'template literal as index signature key',
1630
+ getTestData: (dataOnly = false) => {
1631
+ type Routes = {[key: `api/${string}`]: number};
1632
+ const rt = dataOnly ? (null as any) : runType<Routes>();
1633
+ const values: Routes[] = [{}, {'api/users': 1, 'api/posts': 2}, {'api/v1/users': 7, 'api/admin': 0}];
1634
+ return {rt, values};
1635
+ },
1636
+ },
1637
+ url_index_key_with_named: {
1638
+ title: 'template literal index key + sibling named property',
1639
+ getTestData: (dataOnly = false) => {
1640
+ type Routes = {meta: string; [key: `api/${string}`]: string | number};
1641
+ const rt = dataOnly ? (null as any) : runType<Routes>();
1642
+ const values: Routes[] = [{meta: 'a'}, {meta: 'b', 'api/users': 1}, {meta: 'c', 'api/users': 1, 'api/posts': 2}];
1643
+ return {rt, values};
1644
+ },
1645
+ },
1646
+ },
1595
1647
  OTHERS: {
1596
1648
  promise_jsonStringify_error: {
1597
1649
  title: 'throw error for Promise types',
@@ -69,7 +69,8 @@ function _compileFromXYZ(runType: BaseRunType, comp: JitXYZCompiler): JitCode {
69
69
  throw new Error('Never type cannot be deserialized from XYZ');
70
70
 
71
71
  case ReflectionKind.templateLiteral:
72
- throw new Error('Template literals are not supported in XYZ deserialization');
72
+ // TODO: deserialize as string
73
+ break;
73
74
 
74
75
  case ReflectionKind.literal:
75
76
  return compileLiteral(runType as LiteralRunType, comp);
@@ -74,7 +74,8 @@ export function _compileToXYZ(runType: BaseRunType, comp: JitXYZCompiler): JitCo
74
74
  throw new Error('Never type cannot be serialized to XYZ');
75
75
 
76
76
  case ReflectionKind.templateLiteral:
77
- throw new Error('Template literals are not supported in XYZ serialization');
77
+ // TODO: serialize as string
78
+ break;
78
79
 
79
80
  case ReflectionKind.literal:
80
81
  return compileLiteral(runType as LiteralRunType, comp);
@@ -269,6 +269,35 @@ function _mockType(runType: BaseRunType, comp: JitFnCompiler, stack: BaseRunType
269
269
  case !!(rt.src.index.kind === ReflectionKind.symbol):
270
270
  propName = Symbol.for(`key${i}`);
271
271
  break;
272
+ case !!(rt.src.index.kind === ReflectionKind.templateLiteral): {
273
+ // walk the template literal spans to produce a key that matches the pattern.
274
+ // retry a few times on collision since narrow patterns (eg `id-${number}`) can repeat.
275
+ const tplTypes = ((rt.src.index as any).types as any[]) || [];
276
+ const buildKey = () =>
277
+ tplTypes
278
+ .map((part: any) => {
279
+ switch (part.kind) {
280
+ case ReflectionKind.literal:
281
+ return String(part.literal);
282
+ case ReflectionKind.number:
283
+ return String(mockNumber(mOps.minNumber, mOps.maxNumber));
284
+ case ReflectionKind.string:
285
+ case ReflectionKind.any:
286
+ case ReflectionKind.infer:
287
+ return mockString(
288
+ mOps.stringLength || random(1, mOps.maxRandomStringLength),
289
+ mOps.stringCharSet || stringCharSet
290
+ );
291
+ default:
292
+ throw new Error(`Unsupported template literal span kind: ${part.kind}`);
293
+ }
294
+ })
295
+ .join('');
296
+ let candidate = buildKey();
297
+ for (let r = 0; r < 5 && candidate in parentObj; r++) candidate = buildKey();
298
+ propName = candidate;
299
+ break;
300
+ }
272
301
  default:
273
302
  throw new Error('Invalid index signature type.');
274
303
  }
@@ -277,8 +306,34 @@ function _mockType(runType: BaseRunType, comp: JitFnCompiler, stack: BaseRunType
277
306
  return parentObj;
278
307
  }
279
308
 
309
+ case ReflectionKind.templateLiteral: {
310
+ // walk the spans and emit a string for each: literal verbatim, string/any/infer => mockString,
311
+ // number => stringified mockNumber. The concatenated value is, by construction,
312
+ // accepted by the validation regex emitted by TemplateLiteralRunType.
313
+ const types = (src as any).types as any[];
314
+ return types
315
+ .map((part: any) => {
316
+ switch (part.kind) {
317
+ case ReflectionKind.literal:
318
+ return String(part.literal);
319
+ case ReflectionKind.number:
320
+ return String(mockNumber(mOps.minNumber, mOps.maxNumber));
321
+ case ReflectionKind.string:
322
+ case ReflectionKind.any:
323
+ case ReflectionKind.infer:
324
+ return mockString(
325
+ mOps.stringLength || random(1, mOps.maxRandomStringLength),
326
+ mOps.stringCharSet || stringCharSet
327
+ );
328
+ default:
329
+ throw new Error(
330
+ `Unsupported template literal span kind: ${part.kind}` + printStackTrace(comp, stack)
331
+ );
332
+ }
333
+ })
334
+ .join('');
335
+ }
280
336
  case ReflectionKind.infer:
281
- case ReflectionKind.templateLiteral:
282
337
  case ReflectionKind.typeParameter:
283
338
  default:
284
339
  throw new Error(`Cant mock Unsupported RunType: ${runType.getTypeName()}` + printStackTrace(comp, stack));