@wener/utils 1.1.26 → 1.1.27
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/README.md +4 -0
- package/dist/cjs/createFetchWith-qhRObsE4.js +2 -0
- package/dist/cjs/createFetchWith-qhRObsE4.js.map +1 -0
- package/dist/cjs/{getGlobalThis-304f74e0.js → getGlobalThis-GhffAgiG.js} +1 -1
- package/dist/cjs/{getGlobalThis-304f74e0.js.map → getGlobalThis-GhffAgiG.js.map} +1 -1
- package/dist/cjs/index.cjs +11 -11
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/schema/typebox/gen/index.cjs +118 -0
- package/dist/cjs/schema/typebox/gen/index.cjs.map +1 -0
- package/dist/cjs/schema/typebox/index.cjs +2 -0
- package/dist/cjs/schema/typebox/index.cjs.map +1 -0
- package/dist/cjs/server.cjs +1 -1
- package/dist/cjs/server.cjs.map +1 -1
- package/dist/cjs/servers/jsdom.cjs +1 -1
- package/dist/cjs/servers/jsdom.cjs.map +1 -1
- package/dist/cjs/servers/node-fetch.cjs +1 -1
- package/dist/cjs/servers/ws.cjs +1 -1
- package/dist/esm/createFetchWith-1q1S-Xro.js +2 -0
- package/dist/esm/createFetchWith-1q1S-Xro.js.map +1 -0
- package/dist/esm/{getGlobalThis-b7ad0cf9.js → getGlobalThis-A8F-nU7J.js} +1 -1
- package/dist/esm/{getGlobalThis-b7ad0cf9.js.map → getGlobalThis-A8F-nU7J.js.map} +1 -1
- package/dist/esm/index.js +11 -11
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/schema/typebox/gen.js +118 -0
- package/dist/esm/schema/typebox/gen.js.map +1 -0
- package/dist/esm/schema/typebox.js +2 -0
- package/dist/esm/schema/typebox.js.map +1 -0
- package/dist/esm/server.js +1 -1
- package/dist/esm/server.js.map +1 -1
- package/dist/esm/servers/jsdom.js +1 -1
- package/dist/esm/servers/jsdom.js.map +1 -1
- package/dist/esm/servers/node-fetch.js +1 -1
- package/dist/esm/servers/ws.js +1 -1
- package/dist/system/createFetchWith-TjUle-dI.js +2 -0
- package/dist/system/createFetchWith-TjUle-dI.js.map +1 -0
- package/dist/system/{getGlobalThis-8951eb0e.js → getGlobalThis-X5ifUXDI.js} +1 -1
- package/dist/system/{getGlobalThis-8951eb0e.js.map → getGlobalThis-X5ifUXDI.js.map} +1 -1
- package/dist/system/index.js +11 -11
- package/dist/system/index.js.map +1 -1
- package/dist/system/schema/typebox/gen.js +118 -0
- package/dist/system/schema/typebox/gen.js.map +1 -0
- package/dist/system/schema/typebox.js +2 -0
- package/dist/system/schema/typebox.js.map +1 -0
- package/dist/system/server.js +1 -1
- package/dist/system/server.js.map +1 -1
- package/dist/system/servers/jsdom.js +1 -1
- package/dist/system/servers/jsdom.js.map +1 -1
- package/dist/system/servers/node-fetch.js +1 -1
- package/dist/system/servers/ws.js +1 -1
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -1
- package/lib/langs/deepFreeze.js +16 -0
- package/lib/langs/deepFreeze.js.map +1 -0
- package/lib/maths/clamp.js +21 -0
- package/lib/maths/clamp.js.map +1 -0
- package/lib/objects/get.js.map +1 -1
- package/lib/objects/set.js.map +1 -1
- package/lib/schema/typebox/gen/codegen/common/encoder.js +74 -0
- package/lib/schema/typebox/gen/codegen/common/encoder.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/common/formatter.js +10 -0
- package/lib/schema/typebox/gen/codegen/common/formatter.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/common/jsdoc.js +68 -0
- package/lib/schema/typebox/gen/codegen/common/jsdoc.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/expression/compiler.js +325 -0
- package/lib/schema/typebox/gen/codegen/expression/compiler.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/expression/errors.js +237 -0
- package/lib/schema/typebox/gen/codegen/expression/errors.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/expression/evaluator.js +241 -0
- package/lib/schema/typebox/gen/codegen/expression/evaluator.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/expression/expression.js +200 -0
- package/lib/schema/typebox/gen/codegen/expression/expression.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/index.js +19 -0
- package/lib/schema/typebox/gen/codegen/index.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js +263 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-expression.js +363 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-expression.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js +245 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js +316 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js +20 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js +171 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js +10 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js +198 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js +252 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-value.js +19 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-value.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js +247 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-yup.js +249 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-yup.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-zod.js +272 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-zod.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js +45 -0
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js +620 -0
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js.map +1 -0
- package/lib/schema/typebox/gen/index.js +3 -0
- package/lib/schema/typebox/gen/index.js.map +1 -0
- package/lib/schema/typebox/index.js +3 -0
- package/lib/schema/typebox/index.js.map +1 -0
- package/lib/schema/typebox/typebox.js +34 -0
- package/lib/schema/typebox/typebox.js.map +1 -0
- package/lib/servers/fetch/createFetchWithProxyByUndici.js +15 -0
- package/lib/servers/fetch/createFetchWithProxyByUndici.js.map +1 -1
- package/package.json +21 -6
- package/src/index.ts +3 -3
- package/src/langs/deepFreeze.ts +19 -0
- package/src/langs/hashCode.ignored.ts +125 -0
- package/src/libs/README.md +2 -0
- package/src/logging/slog.ts +121 -0
- package/src/maths/clamp.test.ts +19 -0
- package/src/maths/clamp.ts +23 -1
- package/src/objects/get.ts +14 -14
- package/src/objects/set.ts +5 -5
- package/src/schema/typebox/gen/codegen/common/encoder.ts +99 -0
- package/src/schema/typebox/gen/codegen/common/formatter.ts +31 -0
- package/src/schema/typebox/gen/codegen/common/index.ts +29 -0
- package/src/schema/typebox/gen/codegen/common/jsdoc.ts +93 -0
- package/src/schema/typebox/gen/codegen/expression/compiler.ts +377 -0
- package/src/schema/typebox/gen/codegen/expression/errors.ts +302 -0
- package/src/schema/typebox/gen/codegen/expression/evaluator.ts +268 -0
- package/src/schema/typebox/gen/codegen/expression/expression.ts +538 -0
- package/src/schema/typebox/gen/codegen/expression/index.ts +32 -0
- package/src/schema/typebox/gen/codegen/index.ts +29 -0
- package/src/schema/typebox/gen/codegen/model/index.ts +40 -0
- package/src/schema/typebox/gen/codegen/model/model-to-arktype.ts +266 -0
- package/src/schema/typebox/gen/codegen/model/model-to-expression.ts +378 -0
- package/src/schema/typebox/gen/codegen/model/model-to-grpc.ts +244 -0
- package/src/schema/typebox/gen/codegen/model/model-to-io-ts.ts +294 -0
- package/src/schema/typebox/gen/codegen/model/model-to-javascript.ts +42 -0
- package/src/schema/typebox/gen/codegen/model/model-to-json-schema.ts +166 -0
- package/src/schema/typebox/gen/codegen/model/model-to-typebox.ts +32 -0
- package/src/schema/typebox/gen/codegen/model/model-to-typescript.ts +189 -0
- package/src/schema/typebox/gen/codegen/model/model-to-valibot.ts +236 -0
- package/src/schema/typebox/gen/codegen/model/model-to-value.ts +42 -0
- package/src/schema/typebox/gen/codegen/model/model-to-yrel.ts +232 -0
- package/src/schema/typebox/gen/codegen/model/model-to-yup.ts +226 -0
- package/src/schema/typebox/gen/codegen/model/model-to-zod.ts +251 -0
- package/src/schema/typebox/gen/codegen/model/model.ts +33 -0
- package/src/schema/typebox/gen/codegen/typescript/index.ts +28 -0
- package/src/schema/typebox/gen/codegen/typescript/typescript-to-model.ts +61 -0
- package/src/schema/typebox/gen/codegen/typescript/typescript-to-typebox.ts +647 -0
- package/src/schema/typebox/gen/gen.test.ts +12 -0
- package/src/schema/typebox/gen/index.ts +1 -0
- package/src/schema/typebox/index.ts +1 -0
- package/src/schema/typebox/typebox.ts +34 -0
- package/src/servers/fetch/createFetchWithProxyByUndici.ts +16 -1
- package/src/types/index.d.ts +1 -0
- package/src/langs/hashCode.ts +0 -37
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typescript-to-model.js","sources":["../../../../../../src/schema/typebox/gen/codegen/typescript/typescript-to-model.ts"],"sourcesContent":["/*--------------------------------------------------------------------------\n\n@sinclair/typebox-codegen\n\nThe MIT License (MIT)\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n\n---------------------------------------------------------------------------*/\nimport { Type, Kind, CloneType, type TSchema } from '@sinclair/typebox';\nimport * as ts from 'typescript';\nimport { TypeBoxModel } from '../model/model';\nimport { TypeScriptToTypeBox } from './typescript-to-typebox';\n\nexport namespace TypeScriptToModel {\n const compilerOptions: ts.CompilerOptions = {\n module: ts.ModuleKind.CommonJS, // used for exports\n target: ts.ScriptTarget.ES2015, // evaluation target\n };\n export function Exports(code: string): Map<string, TSchema | Function> {\n const exports = {};\n const evaluate = new Function('exports', 'Type', 'Kind', 'CloneType', code);\n evaluate(exports, Type, Kind, CloneType);\n return new Map(globalThis.Object.entries(exports));\n }\n export function Types(exports: Map<string, TSchema | Function>): TSchema[] {\n const types: TSchema[] = [];\n for (const [key, schema] of exports) {\n if (typeof schema === 'function') continue;\n types.push({ ...schema, $id: key });\n }\n return types;\n }\n export function Generate(typescriptCode: string): TypeBoxModel {\n const typescript = TypeScriptToTypeBox.Generate(typescriptCode, {\n useExportEverything: false,\n useTypeBoxImport: false,\n useIdentifiers: true,\n });\n const javascript = ts.transpileModule(typescript, { compilerOptions });\n const exports = Exports(javascript.outputText);\n const types = Types(exports);\n return { exports, types };\n }\n}\n"],"names":["TypeScriptToModel"],"mappings":";;;;AA8BiB,IAAA,kBAAA;AAAA,CAAV,CAAUA,kBAAV,KAAA;AACL,EAAA,MAAM,eAAsC,GAAA;AAAA,IAC1C,MAAA,EAAQ,GAAG,UAAW,CAAA,QAAA;AAAA;AAAA,IACtB,MAAA,EAAQ,GAAG,YAAa,CAAA,MAAA;AAAA;AAAA,GAC1B,CAAA;AACO,EAAA,SAAS,QAAQ,IAA+C,EAAA;AACrE,IAAA,MAAM,UAAU,EAAC,CAAA;AACjB,IAAA,MAAM,WAAW,IAAI,QAAA,CAAS,WAAW,MAAQ,EAAA,MAAA,EAAQ,aAAa,IAAI,CAAA,CAAA;AAC1E,IAAS,QAAA,CAAA,OAAA,EAAS,IAAM,EAAA,IAAA,EAAM,SAAS,CAAA,CAAA;AACvC,IAAA,OAAO,IAAI,GAAI,CAAA,UAAA,CAAW,MAAO,CAAA,OAAA,CAAQ,OAAO,CAAC,CAAA,CAAA;AAAA,GACnD;AALO,EAAAA,kBAAS,CAAA,OAAA,GAAA,OAAA,CAAA;AAMT,EAAA,SAAS,MAAM,OAAqD,EAAA;AACzE,IAAA,MAAM,QAAmB,EAAC,CAAA;AAC1B,IAAA,KAAA,MAAW,CAAC,GAAA,EAAK,MAAM,CAAA,IAAK,OAAS,EAAA;AACnC,MAAA,IAAI,OAAO,MAAW,KAAA,UAAA;AAAY,QAAA,SAAA;AAClC,MAAA,KAAA,CAAM,KAAK,EAAE,GAAG,MAAQ,EAAA,GAAA,EAAK,KAAK,CAAA,CAAA;AAAA,KACpC;AACA,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AAPO,EAAAA,kBAAS,CAAA,KAAA,GAAA,KAAA,CAAA;AAQT,EAAA,SAAS,SAAS,cAAsC,EAAA;AAC7D,IAAM,MAAA,UAAA,GAAa,mBAAoB,CAAA,QAAA,CAAS,cAAgB,EAAA;AAAA,MAC9D,mBAAqB,EAAA,KAAA;AAAA,MACrB,gBAAkB,EAAA,KAAA;AAAA,MAClB,cAAgB,EAAA,IAAA;AAAA,KACjB,CAAA,CAAA;AACD,IAAA,MAAM,aAAa,EAAG,CAAA,eAAA,CAAgB,UAAY,EAAA,EAAE,iBAAiB,CAAA,CAAA;AACrE,IAAM,MAAA,OAAA,GAAU,OAAQ,CAAA,UAAA,CAAW,UAAU,CAAA,CAAA;AAC7C,IAAM,MAAA,KAAA,GAAQ,MAAM,OAAO,CAAA,CAAA;AAC3B,IAAO,OAAA,EAAE,SAAS,KAAM,EAAA,CAAA;AAAA,GAC1B;AAVO,EAAAA,kBAAS,CAAA,QAAA,GAAA,QAAA,CAAA;AAAA,CAnBD,EAAA,iBAAA,KAAA,iBAAA,GAAA,EAAA,CAAA,CAAA;;;;"}
|
|
@@ -0,0 +1,620 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
import { JsDoc } from '../common/jsdoc.js';
|
|
3
|
+
|
|
4
|
+
class TypeScriptToTypeBoxError extends Error {
|
|
5
|
+
constructor(diagnostics) {
|
|
6
|
+
super("");
|
|
7
|
+
this.diagnostics = diagnostics;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
var TypeScriptToTypeBox;
|
|
11
|
+
((TypeScriptToTypeBox2) => {
|
|
12
|
+
const transpilerOptions = {
|
|
13
|
+
compilerOptions: {
|
|
14
|
+
strict: true,
|
|
15
|
+
target: ts.ScriptTarget.ES2022
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
const typenames = /* @__PURE__ */ new Set();
|
|
19
|
+
let recursiveDeclaration = null;
|
|
20
|
+
let blockLevel = 0;
|
|
21
|
+
let useImports = false;
|
|
22
|
+
let useOptions = false;
|
|
23
|
+
let useGenerics = false;
|
|
24
|
+
let useCloneType = false;
|
|
25
|
+
let useExportsEverything = false;
|
|
26
|
+
let useIdentifiers = false;
|
|
27
|
+
let useTypeBoxImport = true;
|
|
28
|
+
function FindRecursiveParent(decl, node) {
|
|
29
|
+
return ts.isTypeReferenceNode(node) && decl.name.getText() === node.typeName.getText() || node.getChildren().some((node2) => FindRecursiveParent(decl, node2));
|
|
30
|
+
}
|
|
31
|
+
function FindRecursiveThis(node) {
|
|
32
|
+
return node.getChildren().some((node2) => ts.isThisTypeNode(node2) || FindRecursiveThis(node2));
|
|
33
|
+
}
|
|
34
|
+
function FindTypeName(node, name) {
|
|
35
|
+
const found = typenames.has(name) || node.getChildren().some((node2) => {
|
|
36
|
+
return (ts.isInterfaceDeclaration(node2) || ts.isTypeAliasDeclaration(node2)) && node2.name.getText() === name || FindTypeName(node2, name);
|
|
37
|
+
});
|
|
38
|
+
if (found)
|
|
39
|
+
typenames.add(name);
|
|
40
|
+
return found;
|
|
41
|
+
}
|
|
42
|
+
function IsRecursiveType(decl) {
|
|
43
|
+
const check1 = ts.isTypeAliasDeclaration(decl) ? [decl.type].some((node) => FindRecursiveParent(decl, node)) : decl.members.some((node) => FindRecursiveParent(decl, node));
|
|
44
|
+
const check2 = ts.isInterfaceDeclaration(decl) && FindRecursiveThis(decl);
|
|
45
|
+
return check1 || check2;
|
|
46
|
+
}
|
|
47
|
+
function IsReadonlyProperty(node) {
|
|
48
|
+
return node.modifiers !== void 0 && node.modifiers.find((modifier) => modifier.getText() === "readonly") !== void 0;
|
|
49
|
+
}
|
|
50
|
+
function IsOptionalProperty(node) {
|
|
51
|
+
return node.questionToken !== void 0;
|
|
52
|
+
}
|
|
53
|
+
function IsExport(node) {
|
|
54
|
+
return blockLevel === 0 && (useExportsEverything || node.modifiers !== void 0 && node.modifiers.find((modifier) => modifier.getText() === "export") !== void 0);
|
|
55
|
+
}
|
|
56
|
+
function IsNamespace(node) {
|
|
57
|
+
return node.flags === ts.NodeFlags.Namespace;
|
|
58
|
+
}
|
|
59
|
+
function ResolveJsDocComment(node) {
|
|
60
|
+
const content = node.getFullText().trim();
|
|
61
|
+
if (node.kind === ts.SyntaxKind.LiteralType) {
|
|
62
|
+
if (content.startsWith("/**")) {
|
|
63
|
+
return content.slice(0, content.lastIndexOf("*/") + 2).replace(/\*+\/$/, "\n$0");
|
|
64
|
+
} else {
|
|
65
|
+
return "";
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const indices = [content.indexOf("/**"), content.indexOf("type"), content.indexOf("interface")].map(
|
|
69
|
+
(n) => n === -1 ? Infinity : n
|
|
70
|
+
);
|
|
71
|
+
if (indices[0] === -1 || indices[1] < indices[0] || indices[2] < indices[0])
|
|
72
|
+
return "";
|
|
73
|
+
for (let i = indices[0]; i < content.length; i++) {
|
|
74
|
+
if (content[i] === "*" && content[i + 1] === "/")
|
|
75
|
+
return content.slice(0, i + 2);
|
|
76
|
+
}
|
|
77
|
+
return "";
|
|
78
|
+
}
|
|
79
|
+
function ResolveOptions(node) {
|
|
80
|
+
const content = ResolveJsDocComment(node);
|
|
81
|
+
return JsDoc.Parse(content);
|
|
82
|
+
}
|
|
83
|
+
function ResolveIdentifier(node) {
|
|
84
|
+
function* resolve(node2) {
|
|
85
|
+
if (node2.parent)
|
|
86
|
+
yield* resolve(node2.parent);
|
|
87
|
+
if (ts.isModuleDeclaration(node2))
|
|
88
|
+
yield node2.name.getText();
|
|
89
|
+
}
|
|
90
|
+
return [...resolve(node), node.name.getText()].join(".");
|
|
91
|
+
}
|
|
92
|
+
function UnwrapModifier(type) {
|
|
93
|
+
for (let i = 0; i < type.length; i++)
|
|
94
|
+
if (type[i] === "(")
|
|
95
|
+
return type.slice(i + 1, type.length - 1);
|
|
96
|
+
return type;
|
|
97
|
+
}
|
|
98
|
+
function InjectOptions(type, options) {
|
|
99
|
+
if (globalThis.Object.keys(options).length === 0)
|
|
100
|
+
return type;
|
|
101
|
+
if (type.indexOf("Type.ReadonlyOptional") === 0)
|
|
102
|
+
return `Type.ReadonlyOptional( ${InjectOptions(UnwrapModifier(type), options)} )`;
|
|
103
|
+
if (type.indexOf("Type.Readonly") === 0)
|
|
104
|
+
return `Type.Readonly( ${InjectOptions(UnwrapModifier(type), options)} )`;
|
|
105
|
+
if (type.indexOf("Type.Optional") === 0)
|
|
106
|
+
return `Type.Optional( ${InjectOptions(UnwrapModifier(type), options)} )`;
|
|
107
|
+
const encoded = JSON.stringify(options);
|
|
108
|
+
if (type.lastIndexOf("]") === type.length - 1)
|
|
109
|
+
useCloneType = true;
|
|
110
|
+
if (type.lastIndexOf("]") === type.length - 1)
|
|
111
|
+
return `CloneType(${type}, ${encoded})`;
|
|
112
|
+
if (type.indexOf("(") === -1) {
|
|
113
|
+
useCloneType = true;
|
|
114
|
+
return `CloneType(${type}, ${encoded})`;
|
|
115
|
+
}
|
|
116
|
+
if (type.lastIndexOf("()") === type.length - 2)
|
|
117
|
+
return type.slice(0, type.length - 1) + `${encoded})`;
|
|
118
|
+
if (type.lastIndexOf("})") === type.length - 2)
|
|
119
|
+
return type.slice(0, type.length - 1) + `, ${encoded})`;
|
|
120
|
+
if (type.lastIndexOf("])") === type.length - 2)
|
|
121
|
+
return type.slice(0, type.length - 1) + `, ${encoded})`;
|
|
122
|
+
if (type.lastIndexOf(")") === type.length - 1)
|
|
123
|
+
return type.slice(0, type.length - 1) + `, ${encoded})`;
|
|
124
|
+
return type;
|
|
125
|
+
}
|
|
126
|
+
function* SourceFile(node) {
|
|
127
|
+
for (const next of node.getChildren()) {
|
|
128
|
+
yield* Visit(next);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function* PropertySignature(node) {
|
|
132
|
+
const [readonly, optional] = [IsReadonlyProperty(node), IsOptionalProperty(node)];
|
|
133
|
+
const options = ResolveOptions(node);
|
|
134
|
+
const type_0 = Collect(node.type);
|
|
135
|
+
const type_1 = InjectOptions(type_0, options);
|
|
136
|
+
if (readonly && optional) {
|
|
137
|
+
return yield `${node.name.getText()}: Type.ReadonlyOptional(${type_1})`;
|
|
138
|
+
} else if (readonly) {
|
|
139
|
+
return yield `${node.name.getText()}: Type.Readonly(${type_1})`;
|
|
140
|
+
} else if (optional) {
|
|
141
|
+
return yield `${node.name.getText()}: Type.Optional(${type_1})`;
|
|
142
|
+
} else {
|
|
143
|
+
return yield `${node.name.getText()}: ${type_1}`;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function* ArrayTypeNode(node) {
|
|
147
|
+
const type = Collect(node.elementType);
|
|
148
|
+
yield `Type.Array(${type})`;
|
|
149
|
+
}
|
|
150
|
+
function* Block(node) {
|
|
151
|
+
blockLevel += 1;
|
|
152
|
+
const statments = node.statements.map((statement) => Collect(statement)).join("\n\n");
|
|
153
|
+
blockLevel -= 1;
|
|
154
|
+
yield `{
|
|
155
|
+
${statments}
|
|
156
|
+
}`;
|
|
157
|
+
}
|
|
158
|
+
function* TupleTypeNode(node) {
|
|
159
|
+
const types = node.elements.map((type) => Collect(type)).join(",\n");
|
|
160
|
+
yield `Type.Tuple([
|
|
161
|
+
${types}
|
|
162
|
+
])`;
|
|
163
|
+
}
|
|
164
|
+
function* UnionTypeNode(node) {
|
|
165
|
+
const types = node.types.map((type) => Collect(type)).join(",\n");
|
|
166
|
+
yield `Type.Union([
|
|
167
|
+
${types}
|
|
168
|
+
])`;
|
|
169
|
+
}
|
|
170
|
+
function* MappedTypeNode(node) {
|
|
171
|
+
const K = Collect(node.typeParameter);
|
|
172
|
+
const T = Collect(node.type);
|
|
173
|
+
const C = Collect(node.typeParameter.constraint);
|
|
174
|
+
const readonly = node.readonlyToken !== void 0;
|
|
175
|
+
const optional = node.questionToken !== void 0;
|
|
176
|
+
const readonly_subtractive = readonly && ts.isMinusToken(node.readonlyToken);
|
|
177
|
+
const optional_subtractive = optional && ts.isMinusToken(node.questionToken);
|
|
178
|
+
return yield readonly && optional ? readonly_subtractive && optional_subtractive ? `Type.Mapped(${C}, ${K} => Type.Readonly(Type.Optional(${T}, false), false))` : readonly_subtractive ? `Type.Mapped(${C}, ${K} => Type.Readonly(Type.Optional(${T}), false))` : optional_subtractive ? `Type.Mapped(${C}, ${K} => Type.Readonly(Type.Optional(${T}, false)))` : `Type.Mapped(${C}, ${K} => Type.Readonly(Type.Optional(${T})))` : readonly ? readonly_subtractive ? `Type.Mapped(${C}, ${K} => Type.Readonly(${T}, false))` : `Type.Mapped(${C}, ${K} => Type.Readonly(${T}))` : optional ? optional_subtractive ? `Type.Mapped(${C}, ${K} => Type.Optional(${T}, false))` : `Type.Mapped(${C}, ${K} => Type.Optional(${T}))` : `Type.Mapped(${C}, ${K} => ${T})`;
|
|
179
|
+
}
|
|
180
|
+
function* MethodSignature(node) {
|
|
181
|
+
const parameters = node.parameters.map(
|
|
182
|
+
(parameter) => parameter.dotDotDotToken !== void 0 ? `...Type.Rest(${Collect(parameter)})` : Collect(parameter)
|
|
183
|
+
).join(", ");
|
|
184
|
+
const returnType = node.type === void 0 ? `Type.Unknown()` : Collect(node.type);
|
|
185
|
+
yield `${node.name.getText()}: Type.Function([${parameters}], ${returnType})`;
|
|
186
|
+
}
|
|
187
|
+
function* TemplateLiteralTypeNode(node) {
|
|
188
|
+
const collect = node.getChildren().map((node2) => Collect(node2)).join("");
|
|
189
|
+
yield `Type.TemplateLiteral([${collect.slice(0, collect.length - 2)}])`;
|
|
190
|
+
}
|
|
191
|
+
function* TemplateLiteralTypeSpan(node) {
|
|
192
|
+
const collect = node.getChildren().map((node2) => Collect(node2)).join(", ");
|
|
193
|
+
if (collect.length > 0)
|
|
194
|
+
yield `${collect}`;
|
|
195
|
+
}
|
|
196
|
+
function* TemplateHead(node) {
|
|
197
|
+
if (node.text.length > 0)
|
|
198
|
+
yield `Type.Literal('${node.text}'), `;
|
|
199
|
+
}
|
|
200
|
+
function* TemplateMiddle(node) {
|
|
201
|
+
if (node.text.length > 0)
|
|
202
|
+
yield `Type.Literal('${node.text}'), `;
|
|
203
|
+
}
|
|
204
|
+
function* TemplateTail(node) {
|
|
205
|
+
if (node.text.length > 0)
|
|
206
|
+
yield `Type.Literal('${node.text}'), `;
|
|
207
|
+
}
|
|
208
|
+
function* ThisTypeNode(node) {
|
|
209
|
+
yield `This`;
|
|
210
|
+
}
|
|
211
|
+
function* IntersectionTypeNode(node) {
|
|
212
|
+
const types = node.types.map((type) => Collect(type)).join(",\n");
|
|
213
|
+
yield `Type.Intersect([
|
|
214
|
+
${types}
|
|
215
|
+
])`;
|
|
216
|
+
}
|
|
217
|
+
function* TypeOperatorNode(node) {
|
|
218
|
+
if (node.operator === ts.SyntaxKind.KeyOfKeyword) {
|
|
219
|
+
const type = Collect(node.type);
|
|
220
|
+
yield `Type.KeyOf(${type})`;
|
|
221
|
+
}
|
|
222
|
+
if (node.operator === ts.SyntaxKind.ReadonlyKeyword) {
|
|
223
|
+
yield `Type.Readonly(${Collect(node.type)})`;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
function* Parameter(node) {
|
|
227
|
+
yield Collect(node.type);
|
|
228
|
+
}
|
|
229
|
+
function* FunctionTypeNode(node) {
|
|
230
|
+
const parameters = node.parameters.map(
|
|
231
|
+
(parameter) => parameter.dotDotDotToken !== void 0 ? `...Type.Rest(${Collect(parameter)})` : Collect(parameter)
|
|
232
|
+
).join(", ");
|
|
233
|
+
const returns = Collect(node.type);
|
|
234
|
+
yield `Type.Function([${parameters}], ${returns})`;
|
|
235
|
+
}
|
|
236
|
+
function* ConstructorTypeNode(node) {
|
|
237
|
+
const parameters = node.parameters.map((param) => Collect(param)).join(", ");
|
|
238
|
+
const returns = Collect(node.type);
|
|
239
|
+
yield `Type.Constructor([${parameters}], ${returns})`;
|
|
240
|
+
}
|
|
241
|
+
function* EnumDeclaration(node) {
|
|
242
|
+
useImports = true;
|
|
243
|
+
const exports = IsExport(node) ? "export " : "";
|
|
244
|
+
const members = node.members.map((member) => member.getText()).join(", ");
|
|
245
|
+
const enumType = `${exports}enum Enum${node.name.getText()} { ${members} }`;
|
|
246
|
+
const staticType = `${exports}type ${node.name.getText()} = Static<typeof ${node.name.getText()}>`;
|
|
247
|
+
const type = `${exports}const ${node.name.getText()} = Type.Enum(Enum${node.name.getText()})`;
|
|
248
|
+
yield [enumType, "", staticType, type].join("\n");
|
|
249
|
+
}
|
|
250
|
+
function PropertiesFromTypeElementArray(members) {
|
|
251
|
+
const properties = members.filter((member) => !ts.isIndexSignatureDeclaration(member));
|
|
252
|
+
const indexers = members.filter((member) => ts.isIndexSignatureDeclaration(member));
|
|
253
|
+
const propertyCollect = properties.map((property) => Collect(property)).join(",\n");
|
|
254
|
+
const indexer = indexers.length > 0 ? Collect(indexers[indexers.length - 1]) : "";
|
|
255
|
+
if (properties.length === 0 && indexer.length > 0) {
|
|
256
|
+
return `{},
|
|
257
|
+
{
|
|
258
|
+
additionalProperties: ${indexer}
|
|
259
|
+
}`;
|
|
260
|
+
} else if (properties.length > 0 && indexer.length > 0) {
|
|
261
|
+
return `{
|
|
262
|
+
${propertyCollect}
|
|
263
|
+
},
|
|
264
|
+
{
|
|
265
|
+
additionalProperties: ${indexer}
|
|
266
|
+
}`;
|
|
267
|
+
} else {
|
|
268
|
+
return `{
|
|
269
|
+
${propertyCollect}
|
|
270
|
+
}`;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
function* TypeLiteralNode(node) {
|
|
274
|
+
const members = PropertiesFromTypeElementArray(node.members);
|
|
275
|
+
yield* `Type.Object(${members})`;
|
|
276
|
+
}
|
|
277
|
+
function* InterfaceDeclaration(node) {
|
|
278
|
+
useImports = true;
|
|
279
|
+
const isRecursiveType = IsRecursiveType(node);
|
|
280
|
+
if (isRecursiveType)
|
|
281
|
+
recursiveDeclaration = node;
|
|
282
|
+
const heritage = node.heritageClauses !== void 0 ? node.heritageClauses.flatMap((node2) => Collect(node2)) : [];
|
|
283
|
+
if (node.typeParameters) {
|
|
284
|
+
useGenerics = true;
|
|
285
|
+
const exports = IsExport(node) ? "export " : "";
|
|
286
|
+
const identifier = ResolveIdentifier(node);
|
|
287
|
+
const options = useIdentifiers ? { ...ResolveOptions(node), $id: identifier } : { ...ResolveOptions(node) };
|
|
288
|
+
const constraints = node.typeParameters.map((param) => `${Collect(param)} extends TSchema`).join(", ");
|
|
289
|
+
const parameters = node.typeParameters.map((param) => `${Collect(param)}: ${Collect(param)}`).join(", ");
|
|
290
|
+
const members = PropertiesFromTypeElementArray(node.members);
|
|
291
|
+
const rawTypeExpression = IsRecursiveType(node) ? `Type.Recursive(This => Type.Object(${members}))` : `Type.Object(${members})`;
|
|
292
|
+
const typeExpression = heritage.length === 0 ? rawTypeExpression : `Type.Composite([${heritage.join(", ")}, ${rawTypeExpression}])`;
|
|
293
|
+
const type = InjectOptions(typeExpression, options);
|
|
294
|
+
const typeDeclaration = `${exports}const ${node.name.getText()} = <${constraints}>(${parameters}) => ${type}`;
|
|
295
|
+
yield `${typeDeclaration}`;
|
|
296
|
+
} else {
|
|
297
|
+
const exports = IsExport(node) ? "export " : "";
|
|
298
|
+
const identifier = ResolveIdentifier(node);
|
|
299
|
+
const options = useIdentifiers ? { ...ResolveOptions(node), $id: identifier } : { ...ResolveOptions(node) };
|
|
300
|
+
const members = PropertiesFromTypeElementArray(node.members);
|
|
301
|
+
const staticDeclaration = `${exports}type ${node.name.getText()} = Static<typeof ${node.name.getText()}>`;
|
|
302
|
+
const rawTypeExpression = IsRecursiveType(node) ? `Type.Recursive(This => Type.Object(${members}))` : `Type.Object(${members})`;
|
|
303
|
+
const typeExpression = heritage.length === 0 ? rawTypeExpression : `Type.Composite([${heritage.join(", ")}, ${rawTypeExpression}])`;
|
|
304
|
+
const type = InjectOptions(typeExpression, options);
|
|
305
|
+
const typeDeclaration = `${exports}const ${node.name.getText()} = ${type}`;
|
|
306
|
+
yield `${staticDeclaration}
|
|
307
|
+
${typeDeclaration}`;
|
|
308
|
+
}
|
|
309
|
+
recursiveDeclaration = null;
|
|
310
|
+
}
|
|
311
|
+
function* TypeAliasDeclaration(node) {
|
|
312
|
+
useImports = true;
|
|
313
|
+
const isRecursiveType = IsRecursiveType(node);
|
|
314
|
+
if (isRecursiveType)
|
|
315
|
+
recursiveDeclaration = node;
|
|
316
|
+
if (node.typeParameters) {
|
|
317
|
+
useGenerics = true;
|
|
318
|
+
const exports = IsExport(node) ? "export " : "";
|
|
319
|
+
const options = useIdentifiers ? { $id: ResolveIdentifier(node) } : {};
|
|
320
|
+
const constraints = node.typeParameters.map((param) => `${Collect(param)} extends TSchema`).join(", ");
|
|
321
|
+
const parameters = node.typeParameters.map((param) => `${Collect(param)}: ${Collect(param)}`).join(", ");
|
|
322
|
+
const type_0 = Collect(node.type);
|
|
323
|
+
const type_1 = isRecursiveType ? `Type.Recursive(This => ${type_0})` : type_0;
|
|
324
|
+
const type_2 = InjectOptions(type_1, options);
|
|
325
|
+
const typeDeclaration = `${exports}const ${node.name.getText()} = <${constraints}>(${parameters}) => ${type_2}`;
|
|
326
|
+
yield `${typeDeclaration}`;
|
|
327
|
+
} else {
|
|
328
|
+
const exports = IsExport(node) ? "export " : "";
|
|
329
|
+
const options = useIdentifiers ? { $id: ResolveIdentifier(node), ...ResolveOptions(node) } : { ...ResolveOptions(node) };
|
|
330
|
+
const type_0 = Collect(node.type);
|
|
331
|
+
const type_1 = isRecursiveType ? `Type.Recursive(This => ${type_0})` : type_0;
|
|
332
|
+
const type_2 = InjectOptions(type_1, options);
|
|
333
|
+
const staticDeclaration = `${exports}type ${node.name.getText()} = Static<typeof ${node.name.getText()}>`;
|
|
334
|
+
const typeDeclaration = `${exports}const ${node.name.getText()} = ${type_2}`;
|
|
335
|
+
yield `${staticDeclaration}
|
|
336
|
+
${typeDeclaration}`;
|
|
337
|
+
}
|
|
338
|
+
recursiveDeclaration = null;
|
|
339
|
+
}
|
|
340
|
+
function* HeritageClause(node) {
|
|
341
|
+
const types = node.types.map((node2) => Collect(node2));
|
|
342
|
+
if (types.length === 1)
|
|
343
|
+
return yield types[0];
|
|
344
|
+
yield types.join(", ");
|
|
345
|
+
}
|
|
346
|
+
function* IndexedAccessType(node) {
|
|
347
|
+
const obj = node.objectType.getText();
|
|
348
|
+
const key = Collect(node.indexType);
|
|
349
|
+
yield `Type.Index(${obj}, ${key})`;
|
|
350
|
+
}
|
|
351
|
+
function* ExpressionWithTypeArguments(node) {
|
|
352
|
+
const name = Collect(node.expression);
|
|
353
|
+
const typeArguments = node.typeArguments === void 0 ? [] : node.typeArguments.map((node2) => Collect(node2));
|
|
354
|
+
return yield typeArguments.length === 0 ? `${name}` : `${name}(${typeArguments.join(", ")})`;
|
|
355
|
+
}
|
|
356
|
+
function* TypeParameterDeclaration(node) {
|
|
357
|
+
yield node.name.getText();
|
|
358
|
+
}
|
|
359
|
+
function* ParenthesizedTypeNode(node) {
|
|
360
|
+
yield Collect(node.type);
|
|
361
|
+
}
|
|
362
|
+
function* RestTypeNode(node) {
|
|
363
|
+
yield `...Type.Rest(${node.type.getText()})`;
|
|
364
|
+
}
|
|
365
|
+
function* ConditionalTypeNode(node) {
|
|
366
|
+
const checkType = Collect(node.checkType);
|
|
367
|
+
const extendsType = Collect(node.extendsType);
|
|
368
|
+
const trueType = Collect(node.trueType);
|
|
369
|
+
const falseType = Collect(node.falseType);
|
|
370
|
+
yield `Type.Extends(${checkType}, ${extendsType}, ${trueType}, ${falseType})`;
|
|
371
|
+
}
|
|
372
|
+
function* isIndexSignatureDeclaration(node) {
|
|
373
|
+
yield Collect(node.type);
|
|
374
|
+
}
|
|
375
|
+
function* TypeReferenceNode(node) {
|
|
376
|
+
const name = node.typeName.getText();
|
|
377
|
+
const args = node.typeArguments ? `(${node.typeArguments.map((type) => Collect(type)).join(", ")})` : "";
|
|
378
|
+
if (name === "Date")
|
|
379
|
+
return yield `Type.Date()`;
|
|
380
|
+
if (name === "Uint8Array")
|
|
381
|
+
return yield `Type.Uint8Array()`;
|
|
382
|
+
if (name === "String")
|
|
383
|
+
return yield `Type.String()`;
|
|
384
|
+
if (name === "Number")
|
|
385
|
+
return yield `Type.Number()`;
|
|
386
|
+
if (name === "Boolean")
|
|
387
|
+
return yield `Type.Boolean()`;
|
|
388
|
+
if (name === "Function")
|
|
389
|
+
return yield `Type.Function([], Type.Unknown())`;
|
|
390
|
+
if (name === "Array")
|
|
391
|
+
return yield `Type.Array${args}`;
|
|
392
|
+
if (name === "Record")
|
|
393
|
+
return yield `Type.Record${args}`;
|
|
394
|
+
if (name === "Partial")
|
|
395
|
+
return yield `Type.Partial${args}`;
|
|
396
|
+
if (name === "Required")
|
|
397
|
+
return yield `Type.Required${args}`;
|
|
398
|
+
if (name === "Omit")
|
|
399
|
+
return yield `Type.Omit${args}`;
|
|
400
|
+
if (name === "Pick")
|
|
401
|
+
return yield `Type.Pick${args}`;
|
|
402
|
+
if (name === "Promise")
|
|
403
|
+
return yield `Type.Promise${args}`;
|
|
404
|
+
if (name === "ReturnType")
|
|
405
|
+
return yield `Type.ReturnType${args}`;
|
|
406
|
+
if (name === "InstanceType")
|
|
407
|
+
return yield `Type.InstanceType${args}`;
|
|
408
|
+
if (name === "Parameters")
|
|
409
|
+
return yield `Type.Parameters${args}`;
|
|
410
|
+
if (name === "AsyncIterableIterator")
|
|
411
|
+
return yield `Type.AsyncIterator${args}`;
|
|
412
|
+
if (name === "IterableIterator")
|
|
413
|
+
return yield `Type.Iterator${args}`;
|
|
414
|
+
if (name === "ConstructorParameters")
|
|
415
|
+
return yield `Type.ConstructorParameters${args}`;
|
|
416
|
+
if (name === "Exclude")
|
|
417
|
+
return yield `Type.Exclude${args}`;
|
|
418
|
+
if (name === "Extract")
|
|
419
|
+
return yield `Type.Extract${args}`;
|
|
420
|
+
if (name === "Awaited")
|
|
421
|
+
return yield `Type.Awaited${args}`;
|
|
422
|
+
if (name === "Uppercase")
|
|
423
|
+
return yield `Type.Uppercase${args}`;
|
|
424
|
+
if (name === "Lowercase")
|
|
425
|
+
return yield `Type.Lowercase${args}`;
|
|
426
|
+
if (name === "Capitalize")
|
|
427
|
+
return yield `Type.Capitalize${args}`;
|
|
428
|
+
if (name === "Uncapitalize")
|
|
429
|
+
return yield `Type.Uncapitalize${args}`;
|
|
430
|
+
if (recursiveDeclaration !== null && FindRecursiveParent(recursiveDeclaration, node))
|
|
431
|
+
return yield `This`;
|
|
432
|
+
if (FindTypeName(node.getSourceFile(), name) && args.length === 0) {
|
|
433
|
+
return yield `${name}${args}`;
|
|
434
|
+
}
|
|
435
|
+
if (name in globalThis)
|
|
436
|
+
return yield `Type.Never()`;
|
|
437
|
+
return yield `${name}${args}`;
|
|
438
|
+
}
|
|
439
|
+
function* LiteralTypeNode(node) {
|
|
440
|
+
const text = node.getText();
|
|
441
|
+
if (text === "null")
|
|
442
|
+
return yield `Type.Null()`;
|
|
443
|
+
yield InjectOptions(`Type.Literal(${node.getText()})`, ResolveOptions(node));
|
|
444
|
+
}
|
|
445
|
+
function* ModuleDeclaration(node) {
|
|
446
|
+
const export_specifier = IsExport(node) ? "export " : "";
|
|
447
|
+
const module_specifier = IsNamespace(node) ? "namespace" : "module";
|
|
448
|
+
yield `${export_specifier}${module_specifier} ${node.name.getText()} {`;
|
|
449
|
+
yield* Visit(node.body);
|
|
450
|
+
yield `}`;
|
|
451
|
+
}
|
|
452
|
+
function* ModuleBlock(node) {
|
|
453
|
+
for (const statement of node.statements) {
|
|
454
|
+
yield* Visit(statement);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
function* FunctionDeclaration(node) {
|
|
458
|
+
}
|
|
459
|
+
function* ClassDeclaration(node) {
|
|
460
|
+
}
|
|
461
|
+
function Collect(node) {
|
|
462
|
+
return `${[...Visit(node)].join("")}`;
|
|
463
|
+
}
|
|
464
|
+
function* Visit(node) {
|
|
465
|
+
if (node === void 0)
|
|
466
|
+
return;
|
|
467
|
+
if (ts.isArrayTypeNode(node))
|
|
468
|
+
return yield* ArrayTypeNode(node);
|
|
469
|
+
if (ts.isBlock(node))
|
|
470
|
+
return yield* Block(node);
|
|
471
|
+
if (ts.isClassDeclaration(node))
|
|
472
|
+
return yield* ClassDeclaration();
|
|
473
|
+
if (ts.isConditionalTypeNode(node))
|
|
474
|
+
return yield* ConditionalTypeNode(node);
|
|
475
|
+
if (ts.isConstructorTypeNode(node))
|
|
476
|
+
return yield* ConstructorTypeNode(node);
|
|
477
|
+
if (ts.isEnumDeclaration(node))
|
|
478
|
+
return yield* EnumDeclaration(node);
|
|
479
|
+
if (ts.isExpressionWithTypeArguments(node))
|
|
480
|
+
return yield* ExpressionWithTypeArguments(node);
|
|
481
|
+
if (ts.isFunctionDeclaration(node))
|
|
482
|
+
return yield* FunctionDeclaration();
|
|
483
|
+
if (ts.isFunctionTypeNode(node))
|
|
484
|
+
return yield* FunctionTypeNode(node);
|
|
485
|
+
if (ts.isHeritageClause(node))
|
|
486
|
+
return yield* HeritageClause(node);
|
|
487
|
+
if (ts.isIndexedAccessTypeNode(node))
|
|
488
|
+
return yield* IndexedAccessType(node);
|
|
489
|
+
if (ts.isIndexSignatureDeclaration(node))
|
|
490
|
+
return yield* isIndexSignatureDeclaration(node);
|
|
491
|
+
if (ts.isInterfaceDeclaration(node))
|
|
492
|
+
return yield* InterfaceDeclaration(node);
|
|
493
|
+
if (ts.isLiteralTypeNode(node))
|
|
494
|
+
return yield* LiteralTypeNode(node);
|
|
495
|
+
if (ts.isPropertySignature(node))
|
|
496
|
+
return yield* PropertySignature(node);
|
|
497
|
+
if (ts.isModuleDeclaration(node))
|
|
498
|
+
return yield* ModuleDeclaration(node);
|
|
499
|
+
if (ts.isIdentifier(node))
|
|
500
|
+
return yield node.getText();
|
|
501
|
+
if (ts.isIntersectionTypeNode(node))
|
|
502
|
+
return yield* IntersectionTypeNode(node);
|
|
503
|
+
if (ts.isUnionTypeNode(node))
|
|
504
|
+
return yield* UnionTypeNode(node);
|
|
505
|
+
if (ts.isMappedTypeNode(node))
|
|
506
|
+
return yield* MappedTypeNode(node);
|
|
507
|
+
if (ts.isMethodSignature(node))
|
|
508
|
+
return yield* MethodSignature(node);
|
|
509
|
+
if (ts.isModuleBlock(node))
|
|
510
|
+
return yield* ModuleBlock(node);
|
|
511
|
+
if (ts.isParameter(node))
|
|
512
|
+
return yield* Parameter(node);
|
|
513
|
+
if (ts.isParenthesizedTypeNode(node))
|
|
514
|
+
return yield* ParenthesizedTypeNode(node);
|
|
515
|
+
if (ts.isRestTypeNode(node))
|
|
516
|
+
return yield* RestTypeNode(node);
|
|
517
|
+
if (ts.isTupleTypeNode(node))
|
|
518
|
+
return yield* TupleTypeNode(node);
|
|
519
|
+
if (ts.isTemplateLiteralTypeNode(node))
|
|
520
|
+
return yield* TemplateLiteralTypeNode(node);
|
|
521
|
+
if (ts.isTemplateLiteralTypeSpan(node))
|
|
522
|
+
return yield* TemplateLiteralTypeSpan(node);
|
|
523
|
+
if (ts.isTemplateHead(node))
|
|
524
|
+
return yield* TemplateHead(node);
|
|
525
|
+
if (ts.isTemplateMiddle(node))
|
|
526
|
+
return yield* TemplateMiddle(node);
|
|
527
|
+
if (ts.isTemplateTail(node))
|
|
528
|
+
return yield* TemplateTail(node);
|
|
529
|
+
if (ts.isThisTypeNode(node))
|
|
530
|
+
return yield* ThisTypeNode();
|
|
531
|
+
if (ts.isTypeAliasDeclaration(node))
|
|
532
|
+
return yield* TypeAliasDeclaration(node);
|
|
533
|
+
if (ts.isTypeLiteralNode(node))
|
|
534
|
+
return yield* TypeLiteralNode(node);
|
|
535
|
+
if (ts.isTypeOperatorNode(node))
|
|
536
|
+
return yield* TypeOperatorNode(node);
|
|
537
|
+
if (ts.isTypeParameterDeclaration(node))
|
|
538
|
+
return yield* TypeParameterDeclaration(node);
|
|
539
|
+
if (ts.isTypeReferenceNode(node))
|
|
540
|
+
return yield* TypeReferenceNode(node);
|
|
541
|
+
if (ts.isSourceFile(node))
|
|
542
|
+
return yield* SourceFile(node);
|
|
543
|
+
if (node.kind === ts.SyntaxKind.ExportKeyword)
|
|
544
|
+
return yield `export`;
|
|
545
|
+
if (node.kind === ts.SyntaxKind.KeyOfKeyword)
|
|
546
|
+
return yield `Type.KeyOf()`;
|
|
547
|
+
if (node.kind === ts.SyntaxKind.NumberKeyword)
|
|
548
|
+
return yield `Type.Number()`;
|
|
549
|
+
if (node.kind === ts.SyntaxKind.BigIntKeyword)
|
|
550
|
+
return yield `Type.BigInt()`;
|
|
551
|
+
if (node.kind === ts.SyntaxKind.StringKeyword)
|
|
552
|
+
return yield `Type.String()`;
|
|
553
|
+
if (node.kind === ts.SyntaxKind.SymbolKeyword)
|
|
554
|
+
return yield `Type.Symbol()`;
|
|
555
|
+
if (node.kind === ts.SyntaxKind.BooleanKeyword)
|
|
556
|
+
return yield `Type.Boolean()`;
|
|
557
|
+
if (node.kind === ts.SyntaxKind.UndefinedKeyword)
|
|
558
|
+
return yield `Type.Undefined()`;
|
|
559
|
+
if (node.kind === ts.SyntaxKind.UnknownKeyword)
|
|
560
|
+
return yield `Type.Unknown()`;
|
|
561
|
+
if (node.kind === ts.SyntaxKind.AnyKeyword)
|
|
562
|
+
return yield `Type.Any()`;
|
|
563
|
+
if (node.kind === ts.SyntaxKind.NeverKeyword)
|
|
564
|
+
return yield `Type.Never()`;
|
|
565
|
+
if (node.kind === ts.SyntaxKind.NullKeyword)
|
|
566
|
+
return yield `Type.Null()`;
|
|
567
|
+
if (node.kind === ts.SyntaxKind.VoidKeyword)
|
|
568
|
+
return yield `Type.Void()`;
|
|
569
|
+
if (node.kind === ts.SyntaxKind.EndOfFileToken)
|
|
570
|
+
return;
|
|
571
|
+
if (node.kind === ts.SyntaxKind.SyntaxList) {
|
|
572
|
+
for (const child of node.getChildren()) {
|
|
573
|
+
yield* Visit(child);
|
|
574
|
+
}
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
console.warn("Unhandled:", ts.SyntaxKind[node.kind], node.getText());
|
|
578
|
+
}
|
|
579
|
+
function ImportStatement() {
|
|
580
|
+
if (!(useImports && useTypeBoxImport))
|
|
581
|
+
return "";
|
|
582
|
+
const set = /* @__PURE__ */ new Set(["Type", "Static"]);
|
|
583
|
+
if (useGenerics) {
|
|
584
|
+
set.add("TSchema");
|
|
585
|
+
}
|
|
586
|
+
if (useOptions) {
|
|
587
|
+
set.add("SchemaOptions");
|
|
588
|
+
}
|
|
589
|
+
if (useCloneType) {
|
|
590
|
+
set.add("CloneType");
|
|
591
|
+
}
|
|
592
|
+
const imports = [...set].join(", ");
|
|
593
|
+
return `import { ${imports} } from '@sinclair/typebox'`;
|
|
594
|
+
}
|
|
595
|
+
function Generate(typescriptCode, options) {
|
|
596
|
+
var _a, _b, _c;
|
|
597
|
+
useExportsEverything = (_a = options == null ? void 0 : options.useExportEverything) != null ? _a : false;
|
|
598
|
+
useIdentifiers = (_b = options == null ? void 0 : options.useIdentifiers) != null ? _b : false;
|
|
599
|
+
useTypeBoxImport = (_c = options == null ? void 0 : options.useTypeBoxImport) != null ? _c : true;
|
|
600
|
+
typenames.clear();
|
|
601
|
+
useImports = false;
|
|
602
|
+
useOptions = false;
|
|
603
|
+
useGenerics = false;
|
|
604
|
+
useCloneType = false;
|
|
605
|
+
blockLevel = 0;
|
|
606
|
+
const source = ts.createSourceFile("types.ts", typescriptCode, ts.ScriptTarget.ESNext, true);
|
|
607
|
+
const declarations = [...Visit(source)].join("\n\n");
|
|
608
|
+
const imports = ImportStatement();
|
|
609
|
+
const typescript = [imports, "", "", declarations].join("\n");
|
|
610
|
+
const assertion = ts.transpileModule(typescript, transpilerOptions);
|
|
611
|
+
if (assertion.diagnostics && assertion.diagnostics.length > 0) {
|
|
612
|
+
throw new TypeScriptToTypeBoxError(assertion.diagnostics);
|
|
613
|
+
}
|
|
614
|
+
return typescript;
|
|
615
|
+
}
|
|
616
|
+
TypeScriptToTypeBox2.Generate = Generate;
|
|
617
|
+
})(TypeScriptToTypeBox || (TypeScriptToTypeBox = {}));
|
|
618
|
+
|
|
619
|
+
export { TypeScriptToTypeBox, TypeScriptToTypeBoxError };
|
|
620
|
+
//# sourceMappingURL=typescript-to-typebox.js.map
|