@runtyped/type-compiler 1.0.20-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +4 -0
  3. package/compiler-debug.ts +38 -0
  4. package/deepkit-compiler-debug.js +5 -0
  5. package/deepkit-type-install.js +5 -0
  6. package/dist/cjs/compiler-debug.d.ts +1 -0
  7. package/dist/cjs/compiler-debug.js +28 -0
  8. package/dist/cjs/compiler-debug.js.map +1 -0
  9. package/dist/cjs/index.d.ts +7 -0
  10. package/dist/cjs/index.js +36 -0
  11. package/dist/cjs/index.js.map +1 -0
  12. package/dist/cjs/install-transformer.d.ts +7 -0
  13. package/dist/cjs/install-transformer.js +77 -0
  14. package/dist/cjs/install-transformer.js.map +1 -0
  15. package/dist/cjs/src/compiler.d.ts +313 -0
  16. package/dist/cjs/src/compiler.js +2506 -0
  17. package/dist/cjs/src/compiler.js.map +1 -0
  18. package/dist/cjs/src/config.d.ts +83 -0
  19. package/dist/cjs/src/config.js +248 -0
  20. package/dist/cjs/src/config.js.map +1 -0
  21. package/dist/cjs/src/debug.d.ts +9 -0
  22. package/dist/cjs/src/debug.js +26 -0
  23. package/dist/cjs/src/debug.js.map +1 -0
  24. package/dist/cjs/src/loader.d.ts +18 -0
  25. package/dist/cjs/src/loader.js +55 -0
  26. package/dist/cjs/src/loader.js.map +1 -0
  27. package/dist/cjs/src/plugin.d.ts +14 -0
  28. package/dist/cjs/src/plugin.js +46 -0
  29. package/dist/cjs/src/plugin.js.map +1 -0
  30. package/dist/cjs/src/reflection-ast.d.ts +46 -0
  31. package/dist/cjs/src/reflection-ast.js +335 -0
  32. package/dist/cjs/src/reflection-ast.js.map +1 -0
  33. package/dist/cjs/src/resolver.d.ts +27 -0
  34. package/dist/cjs/src/resolver.js +88 -0
  35. package/dist/cjs/src/resolver.js.map +1 -0
  36. package/dist/cjs/src/ts-types.d.ts +17 -0
  37. package/dist/cjs/src/ts-types.js +6 -0
  38. package/dist/cjs/src/ts-types.js.map +1 -0
  39. package/dist/esm/compiler-debug.d.ts +1 -0
  40. package/dist/esm/compiler-debug.js +26 -0
  41. package/dist/esm/compiler-debug.js.map +1 -0
  42. package/dist/esm/index.d.ts +7 -0
  43. package/dist/esm/index.js +19 -0
  44. package/dist/esm/index.js.map +1 -0
  45. package/dist/esm/install-transformer.d.ts +7 -0
  46. package/dist/esm/install-transformer.js +75 -0
  47. package/dist/esm/install-transformer.js.map +1 -0
  48. package/dist/esm/src/compiler.d.ts +313 -0
  49. package/dist/esm/src/compiler.js +2463 -0
  50. package/dist/esm/src/compiler.js.map +1 -0
  51. package/dist/esm/src/config.d.ts +83 -0
  52. package/dist/esm/src/config.js +238 -0
  53. package/dist/esm/src/config.js.map +1 -0
  54. package/dist/esm/src/debug.d.ts +9 -0
  55. package/dist/esm/src/debug.js +21 -0
  56. package/dist/esm/src/debug.js.map +1 -0
  57. package/dist/esm/src/loader.d.ts +18 -0
  58. package/dist/esm/src/loader.js +48 -0
  59. package/dist/esm/src/loader.js.map +1 -0
  60. package/dist/esm/src/plugin.d.ts +14 -0
  61. package/dist/esm/src/plugin.js +40 -0
  62. package/dist/esm/src/plugin.js.map +1 -0
  63. package/dist/esm/src/reflection-ast.d.ts +46 -0
  64. package/dist/esm/src/reflection-ast.js +284 -0
  65. package/dist/esm/src/reflection-ast.js.map +1 -0
  66. package/dist/esm/src/resolver.d.ts +27 -0
  67. package/dist/esm/src/resolver.js +80 -0
  68. package/dist/esm/src/resolver.js.map +1 -0
  69. package/dist/esm/src/ts-types.d.ts +17 -0
  70. package/dist/esm/src/ts-types.js +5 -0
  71. package/dist/esm/src/ts-types.js.map +1 -0
  72. package/index.ts +22 -0
  73. package/install-transformer.ts +84 -0
  74. package/package.json +59 -0
  75. package/src/compiler.ts +3003 -0
  76. package/src/config.ts +331 -0
  77. package/src/debug.ts +22 -0
  78. package/src/loader.ts +58 -0
  79. package/src/plugin.ts +58 -0
  80. package/src/reflection-ast.ts +335 -0
  81. package/src/resolver.ts +113 -0
  82. package/src/ts-types.ts +28 -0
  83. package/tsconfig.esm.json +16 -0
  84. package/tsconfig.json +27 -0
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) up to 2026 Marc J. Schmidt <marc@marcjschmidt.de>
4
+ Copyright (c) 2026 - present Jacopo Scazzosi <jacopo@scazzosi.com>
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,4 @@
1
+ # @runtyped/type-compiler
2
+
3
+ The compiler/transformer companion to [@runtyped/type](https://npm.im/@runtyped/type).
4
+ Check the documentation at [https://github.com/runtyped/runtyped](https://github.com/runtyped/runtyped).
@@ -0,0 +1,38 @@
1
+ import { deepkitType } from './src/plugin.js';
2
+ import { readFileSync } from 'fs';
3
+
4
+ interface Arguments {
5
+ file: string;
6
+ config?: string;
7
+ }
8
+
9
+ function parseArguments(args: string[]): Arguments {
10
+ const result: Arguments = {
11
+ file: '',
12
+ };
13
+
14
+ for (let i = 0; i < args.length; i++) {
15
+ const arg = args[i];
16
+ if (arg === '--config') {
17
+ result.config = args[i + 1];
18
+ i++;
19
+ } else {
20
+ result.file = arg;
21
+ }
22
+ }
23
+
24
+ return result;
25
+ }
26
+
27
+ const args = parseArguments(process.argv.slice(2));
28
+
29
+
30
+
31
+ const transformer = deepkitType({
32
+ tsConfig: args.config,
33
+ });
34
+
35
+ const code = readFileSync(args.file, 'utf8');
36
+ const transformed = transformer(code, args.file);
37
+
38
+ console.log(transformed?.code);
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env node
2
+
3
+ try {
4
+ require('./dist/cjs/compiler-debug.js');
5
+ } catch (error) {}
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env node
2
+
3
+ try {
4
+ require('./dist/cjs/install-transformer.js');
5
+ } catch (error) {}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const plugin_js_1 = require("./src/plugin.js");
4
+ const fs_1 = require("fs");
5
+ function parseArguments(args) {
6
+ const result = {
7
+ file: '',
8
+ };
9
+ for (let i = 0; i < args.length; i++) {
10
+ const arg = args[i];
11
+ if (arg === '--config') {
12
+ result.config = args[i + 1];
13
+ i++;
14
+ }
15
+ else {
16
+ result.file = arg;
17
+ }
18
+ }
19
+ return result;
20
+ }
21
+ const args = parseArguments(process.argv.slice(2));
22
+ const transformer = (0, plugin_js_1.deepkitType)({
23
+ tsConfig: args.config,
24
+ });
25
+ const code = (0, fs_1.readFileSync)(args.file, 'utf8');
26
+ const transformed = transformer(code, args.file);
27
+ console.log(transformed?.code);
28
+ //# sourceMappingURL=compiler-debug.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compiler-debug.js","sourceRoot":"","sources":["../../compiler-debug.ts"],"names":[],"mappings":";;AAAA,+CAA8C;AAC9C,2BAAkC;AAOlC,SAAS,cAAc,CAAC,IAAc;IAClC,MAAM,MAAM,GAAc;QACtB,IAAI,EAAE,EAAE;KACX,CAAC;IAEF,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACnC,MAAM,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;QACpB,IAAI,GAAG,KAAK,UAAU,EAAE,CAAC;YACrB,MAAM,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;YAC5B,CAAC,EAAE,CAAC;QACR,CAAC;aAAM,CAAC;YACJ,MAAM,CAAC,IAAI,GAAG,GAAG,CAAC;QACtB,CAAC;IACL,CAAC;IAED,OAAO,MAAM,CAAC;AAClB,CAAC;AAED,MAAM,IAAI,GAAG,cAAc,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AAInD,MAAM,WAAW,GAAG,IAAA,uBAAW,EAAC;IAC5B,QAAQ,EAAE,IAAI,CAAC,MAAM;CACxB,CAAC,CAAC;AAEH,MAAM,IAAI,GAAG,IAAA,iBAAY,EAAC,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;AAC7C,MAAM,WAAW,GAAG,WAAW,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;AAEjD,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC"}
@@ -0,0 +1,7 @@
1
+ import type { Program } from 'typescript';
2
+ export * from './src/compiler.js';
3
+ export * from './src/loader.js';
4
+ export default function myTransformerPlugin(program: Program, opts: {}): {
5
+ before: import("typescript").CustomTransformerFactory;
6
+ afterDeclarations: import("typescript").CustomTransformerFactory;
7
+ };
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ /*
3
+ * Runtyped Framework
4
+ * Copyright (C) 2021 Deepkit UG, Marc J. Schmidt
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the MIT License.
8
+ *
9
+ * You should have received a copy of the MIT License along with this program.
10
+ */
11
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
12
+ if (k2 === undefined) k2 = k;
13
+ var desc = Object.getOwnPropertyDescriptor(m, k);
14
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
15
+ desc = { enumerable: true, get: function() { return m[k]; } };
16
+ }
17
+ Object.defineProperty(o, k2, desc);
18
+ }) : (function(o, m, k, k2) {
19
+ if (k2 === undefined) k2 = k;
20
+ o[k2] = m[k];
21
+ }));
22
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
23
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.default = myTransformerPlugin;
27
+ const compiler_js_1 = require("./src/compiler.js");
28
+ __exportStar(require("./src/compiler.js"), exports);
29
+ __exportStar(require("./src/loader.js"), exports);
30
+ function myTransformerPlugin(program, opts) {
31
+ return {
32
+ before: compiler_js_1.transformer,
33
+ afterDeclarations: compiler_js_1.declarationTransformer,
34
+ };
35
+ }
36
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../index.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;;;;;;;;;;;;;;;AAQH,sCAKC;AAXD,mDAAwE;AAGxE,oDAAkC;AAClC,kDAAgC;AAEhC,SAAwB,mBAAmB,CAAC,OAAgB,EAAE,IAAQ;IAClE,OAAO;QACH,MAAM,EAAE,yBAAW;QACnB,iBAAiB,EAAE,oCAAsB;KAC5C,CAAC;AACN,CAAC"}
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * This script installs the deepkit/type transformer (that extracts automatically types and adds the correct @t decorator) to the typescript node_modules.
4
+ *
5
+ * The critical section that needs adjustment is in the `function getScriptTransformers` in `node_modules/typescript/lib/tsc.js`.
6
+ */
7
+ export {};
@@ -0,0 +1,77 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ /**
4
+ * This script installs the deepkit/type transformer (that extracts automatically types and adds the correct @t decorator) to the typescript node_modules.
5
+ *
6
+ * The critical section that needs adjustment is in the `function getScriptTransformers` in `node_modules/typescript/lib/tsc.js`.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ const path_1 = require("path");
10
+ const fs_1 = require("fs");
11
+ let to = process.argv[2] || process.cwd();
12
+ function getPatchId(id) {
13
+ return 'deepkit_type_patch_' + id;
14
+ }
15
+ function getCode(deepkitDistPath, varName, id) {
16
+ return `
17
+ //${getPatchId(id)}
18
+ try {
19
+ var typeTransformer;
20
+ try {
21
+ typeTransformer = require('@runtyped/type-compiler');
22
+ } catch (error) {
23
+ typeTransformer = require(${JSON.stringify(deepkitDistPath)});
24
+ }
25
+ if (typeTransformer) {
26
+ if (!customTransformers) ${varName} = {};
27
+ if (!${varName}.before) ${varName}.before = [];
28
+ let alreadyPatched = false;
29
+ for (let fn of ${varName}.before) {
30
+ if (fn && fn.name === 'deepkitTransformer') alreadyPatched = true;
31
+ }
32
+ if (!alreadyPatched) {
33
+ if (!${varName}.before.includes(typeTransformer.transformer)) ${varName}.before.push(typeTransformer.transformer);
34
+
35
+ if (!${varName}.afterDeclarations) ${varName}.afterDeclarations = [];
36
+ if (!${varName}.afterDeclarations.includes(typeTransformer.declarationTransformer)) {
37
+ ${varName}.afterDeclarations.push(typeTransformer.declarationTransformer);
38
+ }
39
+ }
40
+ }
41
+ } catch (e) {
42
+ }
43
+ //${getPatchId(id)}-end
44
+ `;
45
+ }
46
+ function isPatched(code, id) {
47
+ return code.includes(getPatchId(id));
48
+ }
49
+ function patchGetTransformers(deepkitDistPath, code) {
50
+ const id = 'patchGetTransformers';
51
+ if (isPatched(code, id))
52
+ return '';
53
+ const find = /function getTransformers\([^)]+\)\s*{/;
54
+ if (!code.match(find))
55
+ return '';
56
+ code = code.replace(find, function (a) {
57
+ return a + '\n ' + getCode(deepkitDistPath, 'customTransformers', id);
58
+ });
59
+ return code;
60
+ }
61
+ if (to + '/dist/cjs' === __dirname) {
62
+ to = (0, path_1.join)(to, '..'); //we exclude type-compiler/node_modules
63
+ }
64
+ const typeScriptPath = (0, path_1.dirname)(require.resolve('typescript', { paths: [to] }));
65
+ const deepkitDistPath = (0, path_1.relative)(typeScriptPath, __dirname);
66
+ const paths = ['tsc.js', '_tsc.js', 'typescript.js'];
67
+ for (const fileName of paths) {
68
+ const file = (0, path_1.join)(typeScriptPath, fileName);
69
+ if (!(0, fs_1.existsSync)(file))
70
+ continue;
71
+ const content = patchGetTransformers(deepkitDistPath, (0, fs_1.readFileSync)(file, 'utf8'));
72
+ if (!content)
73
+ continue;
74
+ (0, fs_1.writeFileSync)(file, content);
75
+ console.log('Deepkit Type: Injected TypeScript transformer at', file);
76
+ }
77
+ //# sourceMappingURL=install-transformer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"install-transformer.js","sourceRoot":"","sources":["../../install-transformer.ts"],"names":[],"mappings":";;AAEA;;;;GAIG;;AAEH,+BAA+C;AAC/C,2BAA6D;AAE7D,IAAI,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,OAAO,CAAC,GAAG,EAAE,CAAC;AAE1C,SAAS,UAAU,CAAC,EAAU;IAC1B,OAAO,qBAAqB,GAAG,EAAE,CAAC;AACtC,CAAC;AAED,SAAS,OAAO,CAAC,eAAuB,EAAE,OAAe,EAAE,EAAU;IACjE,OAAO;YACC,UAAU,CAAC,EAAE,CAAC;;;;;;4CAMkB,IAAI,CAAC,SAAS,CAAC,eAAe,CAAC;;;2CAGhC,OAAO;uBAC3B,OAAO,YAAY,OAAO;;iCAEhB,OAAO;;;;2BAIb,OAAO,kDAAkD,OAAO;;2BAEhE,OAAO,uBAAuB,OAAO;2BACrC,OAAO;0BACR,OAAO;;;;;;YAMrB,UAAU,CAAC,EAAE,CAAC;KACrB,CAAC;AACN,CAAC;AAED,SAAS,SAAS,CAAC,IAAY,EAAE,EAAU;IACvC,OAAO,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,CAAC;AACzC,CAAC;AAED,SAAS,oBAAoB,CAAC,eAAuB,EAAE,IAAY;IAC/D,MAAM,EAAE,GAAG,sBAAsB,CAAC;IAClC,IAAI,SAAS,CAAC,IAAI,EAAE,EAAE,CAAC;QAAE,OAAO,EAAE,CAAC;IAEnC,MAAM,IAAI,GAAG,uCAAuC,CAAC;IACrD,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;QAAE,OAAO,EAAE,CAAC;IAEjC,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,UAAU,CAAC;QACjC,OAAO,CAAC,GAAG,QAAQ,GAAG,OAAO,CAAC,eAAe,EAAE,oBAAoB,EAAE,EAAE,CAAC,CAAC;IAC7E,CAAC,CAAC,CAAC;IAEH,OAAO,IAAI,CAAC;AAChB,CAAC;AAED,IAAI,EAAE,GAAG,WAAW,KAAK,SAAS,EAAE,CAAC;IACjC,EAAE,GAAG,IAAA,WAAI,EAAC,EAAE,EAAE,IAAI,CAAC,CAAC,CAAC,uCAAuC;AAChE,CAAC;AAED,MAAM,cAAc,GAAG,IAAA,cAAO,EAAC,OAAO,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;AAC/E,MAAM,eAAe,GAAG,IAAA,eAAQ,EAAC,cAAc,EAAE,SAAS,CAAC,CAAC;AAE5D,MAAM,KAAK,GAAG,CAAC,QAAQ,EAAE,SAAS,EAAE,eAAe,CAAC,CAAC;AAErD,KAAK,MAAM,QAAQ,IAAI,KAAK,EAAE,CAAC;IAC3B,MAAM,IAAI,GAAG,IAAA,WAAI,EAAC,cAAc,EAAE,QAAQ,CAAC,CAAC;IAC5C,IAAI,CAAC,IAAA,eAAU,EAAC,IAAI,CAAC;QAAE,SAAS;IAChC,MAAM,OAAO,GAAG,oBAAoB,CAAC,eAAe,EAAE,IAAA,iBAAY,EAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC;IAClF,IAAI,CAAC,OAAO;QAAE,SAAS;IACvB,IAAA,kBAAa,EAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IAC7B,OAAO,CAAC,GAAG,CAAC,kDAAkD,EAAE,IAAI,CAAC,CAAC;AAC1E,CAAC"}
@@ -0,0 +1,313 @@
1
+ import ts, { ArrowFunction, Bundle, ClassDeclaration, ClassExpression, CompilerHost, CompilerOptions, ConstructorDeclaration, CustomTransformer, CustomTransformerFactory, Declaration, EntityName, ExportDeclaration, Expression, ExpressionWithTypeArguments, FunctionDeclaration, FunctionExpression, Identifier, ImportDeclaration, JSDocImportTag, MethodDeclaration, ModuleDeclaration, ModuleExportName, Node, NodeArray, NodeFactory, ParseConfigHost, PropertyAccessExpression, QualifiedName, Statement, TransformationContext, TypeAliasDeclaration, TypeChecker, TypeParameterDeclaration, TypeReferenceNode } from 'typescript';
2
+ import { NodeConverter, PackExpression } from './reflection-ast.js';
3
+ import { SourceFile } from './ts-types.js';
4
+ import { ReflectionOp } from '@runtyped/type-spec';
5
+ import { Resolver } from './resolver.js';
6
+ import { ConfigResolver, MatchResult, ReflectionConfig, ReflectionConfigCache } from './config.js';
7
+ export declare function encodeOps(ops: ReflectionOp[]): string;
8
+ export declare const packSizeByte: number;
9
+ /**
10
+ * It can't be more ops than this given number
11
+ */
12
+ export declare const packSize: number;
13
+ export declare function debugPackStruct(sourceFile: SourceFile, forType: Node, pack: {
14
+ ops: ReflectionOp[];
15
+ stack: PackExpression[];
16
+ }): void;
17
+ interface Frame {
18
+ variables: {
19
+ name: string;
20
+ index: number;
21
+ }[];
22
+ opIndex: number;
23
+ conditional?: true;
24
+ previous?: Frame;
25
+ }
26
+ type StackEntry = Expression | string | number | boolean;
27
+ declare class CompilerProgram {
28
+ forNode: Node;
29
+ sourceFile?: SourceFile | undefined;
30
+ protected ops: ReflectionOp[];
31
+ protected stack: StackEntry[];
32
+ protected mainOffset: number;
33
+ protected stackPosition: number;
34
+ protected frame: Frame;
35
+ protected activeCoRoutines: {
36
+ ops: ReflectionOp[];
37
+ }[];
38
+ protected coRoutines: {
39
+ ops: ReflectionOp[];
40
+ }[];
41
+ constructor(forNode: Node, sourceFile?: SourceFile | undefined);
42
+ buildPackStruct(): {
43
+ ops: ReflectionOp[];
44
+ stack: StackEntry[];
45
+ };
46
+ isEmpty(): boolean;
47
+ pushConditionalFrame(): void;
48
+ pushStack(item: StackEntry): number;
49
+ pushCoRoutine(): void;
50
+ popCoRoutine(): number;
51
+ pushOp(...ops: ReflectionOp[]): void;
52
+ pushOpAtFrame(frame: Frame, ...ops: ReflectionOp[]): void;
53
+ /**
54
+ * Returns the index of the `entry` in the stack, if already exists. If not, add it, and return that new index.
55
+ */
56
+ findOrAddStackEntry(entry: any): number;
57
+ /**
58
+ * To make room for a stack entry expected on the stack as input for example.
59
+ */
60
+ increaseStackPosition(): number;
61
+ protected resolveFunctionParameters: Map<ts.Node, number>;
62
+ resolveFunctionParametersIncrease(fn: Node): void;
63
+ resolveFunctionParametersDecrease(fn: Node): void;
64
+ isResolveFunctionParameters(fn: Node): boolean;
65
+ /**
66
+ *
67
+ * Each pushFrame() call needs a popFrame() call.
68
+ */
69
+ pushFrame(implicit?: boolean): Frame;
70
+ findConditionalFrame(): Frame | undefined;
71
+ /**
72
+ * Remove stack without doing it as OP in the processor. Some other command calls popFrame() already, which makes popFrameImplicit() an implicit popFrame.
73
+ * e.g. union, class, etc. all call popFrame(). the current CompilerProgram needs to be aware of that, which this function is for.
74
+ */
75
+ popFrameImplicit(): void;
76
+ moveFrame(): void;
77
+ pushVariable(name: string, frame?: Frame): number;
78
+ pushTemplateParameter(name: string, withDefault?: boolean): number;
79
+ findVariable(name: string, frame?: Frame): {
80
+ frameOffset: number;
81
+ stackIndex: number;
82
+ } | undefined;
83
+ }
84
+ export declare class Cache {
85
+ resolver: ReflectionConfigCache;
86
+ sourceFiles: {
87
+ [fileName: string]: SourceFile;
88
+ };
89
+ globalSourceFiles?: SourceFile[];
90
+ /**
91
+ * Signals the cache to check if it needs to be cleared.
92
+ */
93
+ tick(): void;
94
+ }
95
+ /**
96
+ * Read the TypeScript AST and generate pack struct (instructions + pre-defined stack).
97
+ *
98
+ * This transformer extracts type and add the encoded (so its small and low overhead) at classes and functions as property.
99
+ *
100
+ * runtyped/type can then extract and decode them on-demand.
101
+ */
102
+ export declare class ReflectionTransformer implements CustomTransformer {
103
+ protected context: TransformationContext;
104
+ protected cache: Cache;
105
+ sourceFile: SourceFile;
106
+ protected f: NodeFactory;
107
+ protected embedAssignType: boolean;
108
+ /**
109
+ * Types added to this map will get a type program directly under it.
110
+ * This is for types used in the very same file.
111
+ */
112
+ protected compileDeclarations: Map<ts.EnumDeclaration | ts.InterfaceDeclaration | ts.TypeAliasDeclaration, {
113
+ name: EntityName;
114
+ sourceFile: SourceFile;
115
+ compiled?: Statement[];
116
+ }>;
117
+ /**
118
+ * Types added to this map will get a type program at the top root level of the program.
119
+ * This is for imported types, which need to be inlined into the current file, as we do not emit type imports (TS will omit them).
120
+ */
121
+ protected embedDeclarations: Map<ts.Node, {
122
+ name: EntityName;
123
+ sourceFile?: SourceFile;
124
+ }>;
125
+ /**
126
+ * When a node was embedded or compiled (from the maps above), we store it here to know to not add it again.
127
+ */
128
+ protected compiledDeclarations: Set<ts.Node>;
129
+ protected addImports: {
130
+ importDeclaration: ImportDeclaration | JSDocImportTag;
131
+ identifier: Identifier;
132
+ }[];
133
+ protected additionalImports: Map<ts.ImportDeclaration | ts.JSDocImportTag, ts.Statement>;
134
+ protected nodeConverter: NodeConverter;
135
+ protected typeChecker?: TypeChecker;
136
+ protected resolver: Resolver;
137
+ protected host: CompilerHost;
138
+ protected overriddenHost: boolean;
139
+ protected overriddenConfigResolver?: ConfigResolver;
140
+ protected compilerOptions: CompilerOptions;
141
+ /**
142
+ * When a deep call expression was found a script-wide variable is necessary
143
+ * as temporary storage.
144
+ */
145
+ protected tempResultIdentifier?: Identifier;
146
+ protected parseConfigHost: ParseConfigHost;
147
+ protected intrinsicMetaDeclaration: TypeAliasDeclaration;
148
+ constructor(context: TransformationContext, cache?: Cache);
149
+ forHost(host: CompilerHost): this;
150
+ withReflection(config: ReflectionConfig): this;
151
+ transformBundle(node: Bundle): Bundle;
152
+ getTempResultIdentifier(): Identifier;
153
+ protected getConfigResolver(sourceFile: {
154
+ fileName: string;
155
+ }): ConfigResolver;
156
+ protected getReflectionConfig(sourceFile: {
157
+ fileName: string;
158
+ }): MatchResult;
159
+ protected isWithReflection(sourceFile: SourceFile | undefined, node: Node & {
160
+ __deepkitConfig?: ReflectionConfig;
161
+ }): boolean;
162
+ transformSourceFile(sourceFile: SourceFile): SourceFile;
163
+ attachAdditionalStatements(statements: NodeArray<Statement> | Statement[]): Statement[];
164
+ protected getModuleType(): 'cjs' | 'esm';
165
+ protected getArrowFunctionΩPropertyAccessIdentifier(node: ArrowFunction): Identifier | undefined;
166
+ protected injectResetΩ<T extends FunctionDeclaration | FunctionExpression | MethodDeclaration | ConstructorDeclaration | ArrowFunction>(node: T): T;
167
+ protected createProgramVarFromNode(node: Node, name: EntityName, sourceFile?: SourceFile): Statement[];
168
+ protected extractPackStructOfExpression(node: Expression, program: CompilerProgram): void;
169
+ protected extractPackStructOfType(node: Node | Declaration | ClassDeclaration | ClassExpression, program: CompilerProgram): void;
170
+ protected knownClasses: {
171
+ [name: string]: ReflectionOp;
172
+ };
173
+ protected getGlobalLibs(): SourceFile[];
174
+ /**
175
+ * This is a custom resolver based on populated `locals` from the binder. It uses a custom resolution algorithm since
176
+ * we have no access to the binder/TypeChecker directly and instantiating a TypeChecker per file/transformer is incredible slow.
177
+ */
178
+ protected resolveDeclaration(typeName: EntityName): {
179
+ declaration: Node;
180
+ importDeclaration?: ImportDeclaration | JSDocImportTag;
181
+ typeOnly?: boolean;
182
+ } | void;
183
+ protected getDeclarationVariableName(typeName: EntityName): Identifier;
184
+ /**
185
+ * The semantic of isExcluded is different from checking if the fileName is part
186
+ * of reflection config option. isExcluded checks if the file should be excluded
187
+ * via the exclude option. mainly used to exclude globals and external libraries.
188
+ */
189
+ protected isExcluded(fileName: string): boolean;
190
+ protected extractPackStructOfTypeReference(type: Identifier | TypeReferenceNode | ExpressionWithTypeArguments, program: CompilerProgram): void;
191
+ /**
192
+ * Returns the class declaration, function/arrow declaration, or block where type was used.
193
+ */
194
+ protected getTypeUser(type: Node): Node;
195
+ /**
196
+ * With this function we want to check if `type` is used in the signature itself from the parent of `declaration`.
197
+ * If so, we do not try to infer the type from runtime values.
198
+ *
199
+ * Examples where we do not infer from runtime, `type` being `T` and `declaration` being `<T>` (return false):
200
+ *
201
+ * ```typescript
202
+ * class User<T> {
203
+ * config: T;
204
+ * }
205
+ *
206
+ * class User<T> {
207
+ * constructor(public config: T) {}
208
+ * }
209
+ *
210
+ * function do<T>(item: T): void {}
211
+ * function do<T>(item: T): T {}
212
+ * ```
213
+ *
214
+ * Examples where we infer from runtime (return true):
215
+ *
216
+ * ```typescript
217
+ * function do<T>(item: T) {
218
+ * return typeOf<T>; //<-- because of that
219
+ * }
220
+ *
221
+ * function do<T>(item: T) {
222
+ * class A {
223
+ * config: T; //<-- because of that
224
+ * }
225
+ * return A;
226
+ * }
227
+ *
228
+ * function do<T>(item: T) {
229
+ * class A {
230
+ * doIt() {
231
+ * class B {
232
+ * config: T; //<-- because of that
233
+ * }
234
+ * return B;
235
+ * }
236
+ * }
237
+ * return A;
238
+ * }
239
+ *
240
+ * function do<T>(item: T) {
241
+ * class A {
242
+ * doIt(): T { //<-- because of that
243
+ * }
244
+ * }
245
+ * return A;
246
+ * }
247
+ * ```
248
+ */
249
+ protected needsToBeInferred(declaration: TypeParameterDeclaration, type: Identifier | TypeReferenceNode | ExpressionWithTypeArguments): boolean;
250
+ protected resolveTypeOnlyImport(entityName: EntityName, program: CompilerProgram): void;
251
+ protected resolveTypeName(typeName: string, program: CompilerProgram): void;
252
+ protected resolveTypeParameter(declaration: TypeParameterDeclaration, type: Identifier | TypeReferenceNode | ExpressionWithTypeArguments, program: CompilerProgram): void;
253
+ protected createAccessorForEntityName(e: QualifiedName): PropertyAccessExpression;
254
+ protected findDeclarationInFile(sourceFile: SourceFile | ModuleDeclaration, declarationName: string): Declaration | undefined;
255
+ protected resolveImportSpecifier(_declarationName: string | ModuleExportName, importOrExport: ExportDeclaration | ImportDeclaration | JSDocImportTag, sourceFile: SourceFile): Declaration | undefined;
256
+ protected followExport(declarationName: string, statement: ExportDeclaration, sourceFile: SourceFile): Declaration | undefined;
257
+ protected getTypeOfType(type: Node | Declaration): Expression | undefined;
258
+ protected packOpsAndStack(program: CompilerProgram): ts.Expression | undefined;
259
+ /**
260
+ * Note: We have to duplicate the expressions as it can be that incoming expression are from another file and contain wrong pos/end properties,
261
+ * so the code generation is then broken when we simply reuse them. Wrong code like ``User.__type = [.toEqual({`` is then generated.
262
+ * This function is probably not complete, but we add new copies when required.
263
+ */
264
+ protected valueToExpression(value: undefined | PackExpression | PackExpression[]): Expression;
265
+ /**
266
+ * A class is decorated with type information by adding a static variable.
267
+ *
268
+ * class Model {
269
+ * static __types = pack(ReflectionOp.string); //<-- encoded type information
270
+ * title: string;
271
+ * }
272
+ */
273
+ protected decorateClass(sourceFile: SourceFile, node: ClassDeclaration | ClassExpression): Node;
274
+ /**
275
+ * const fn = function() {}
276
+ *
277
+ * => const fn = __assignType(function() {}, [34])
278
+ */
279
+ protected decorateFunctionExpression(expression: FunctionExpression): ts.FunctionExpression | ts.CallExpression;
280
+ /**
281
+ * function name() {}
282
+ *
283
+ * => function name() {}; name.__type = 34;
284
+ */
285
+ protected decorateFunctionDeclaration(declaration: FunctionDeclaration): ts.ExportAssignment | ts.FunctionDeclaration | ts.Statement[] | undefined;
286
+ /**
287
+ * const fn = () => {}
288
+ * => const fn = __assignType(() => {}, [34])
289
+ */
290
+ protected decorateArrowFunction(expression: ArrowFunction): ts.ArrowFunction | ts.CallExpression;
291
+ /**
292
+ * Object.assign(fn, {__type: []}) is much slower than a custom implementation like
293
+ *
294
+ * assignType(fn, [])
295
+ *
296
+ * where we embed assignType() at the beginning of the type.
297
+ */
298
+ protected wrapWithAssignType(fn: Expression, type: Expression): ts.CallExpression;
299
+ /**
300
+ * Checks if reflection was disabled/enabled in file via JSDoc attribute for a particular
301
+ * Node, e.g `@reflection no`. If nothing is found, "reflection" config option needs to be used.
302
+ */
303
+ protected getExplicitReflectionMode(sourceFile: SourceFile | undefined, node: Node): boolean | undefined;
304
+ }
305
+ export declare class DeclarationTransformer extends ReflectionTransformer {
306
+ protected addExports: {
307
+ identifier: string;
308
+ }[];
309
+ transformSourceFile(sourceFile: SourceFile): SourceFile;
310
+ }
311
+ export declare const transformer: CustomTransformerFactory;
312
+ export declare const declarationTransformer: CustomTransformerFactory;
313
+ export {};