@wener/utils 1.1.31 → 1.1.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/LICENSE.txt +1 -0
- package/dist/cjs/index.cjs +21 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/esm/index.js +21 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/system/index.js +21 -0
- package/dist/system/index.js.map +1 -0
- package/package.json +37 -69
- package/src/asyncs/nextOfAsyncIterator.ts +13 -1
- package/src/asyncs/timeout.ts +15 -12
- package/src/cn/index.ts +1 -0
- package/src/cn/parseChineseNumber.test.ts +24 -0
- package/src/cn/parseChineseNumber.ts +97 -0
- package/src/crypto/getRandomValues.ts +5 -3
- package/src/crypto/hashing.test.ts +1 -1
- package/src/errors/Errors.test.ts +22 -0
- package/src/errors/Errors.ts +1 -4
- package/src/index.ts +2 -0
- package/src/io/Bytes.ts +64 -0
- package/src/isomorphics/structuredClone.ts +0 -3
- package/src/langs/AsyncCloser.ts +45 -0
- package/src/langs/Closer.ts +39 -0
- package/src/server/index.ts +6 -0
- package/src/{servers → server}/polyfill/polyfillWebSocket.ts +5 -0
- package/src/{types/index.d.ts → types.d.ts} +12 -0
- package/tsconfig.json +7 -5
- package/index.ts +0 -1
- package/lib/arrays/MaybeArray.js +0 -41
- package/lib/arrays/MaybeArray.js.map +0 -1
- package/lib/arrays/arrayFromAsync.js +0 -7
- package/lib/arrays/arrayFromAsync.js.map +0 -1
- package/lib/asyncs/AsyncInterval.js +0 -14
- package/lib/asyncs/AsyncInterval.js.map +0 -1
- package/lib/asyncs/MaybePromise.js +0 -3
- package/lib/asyncs/MaybePromise.js.map +0 -1
- package/lib/asyncs/createAsyncIterator.js +0 -41
- package/lib/asyncs/createAsyncIterator.js.map +0 -1
- package/lib/asyncs/createLazyPromise.js +0 -65
- package/lib/asyncs/createLazyPromise.js.map +0 -1
- package/lib/asyncs/firstOfAsyncIterator.js +0 -11
- package/lib/asyncs/firstOfAsyncIterator.js.map +0 -1
- package/lib/asyncs/generatorOfStream.js +0 -16
- package/lib/asyncs/generatorOfStream.js.map +0 -1
- package/lib/asyncs/isIterator.js +0 -5
- package/lib/asyncs/isIterator.js.map +0 -1
- package/lib/asyncs/isPromise.js +0 -5
- package/lib/asyncs/isPromise.js.map +0 -1
- package/lib/asyncs/isThenable.js +0 -6
- package/lib/asyncs/isThenable.js.map +0 -1
- package/lib/asyncs/nextOfAsyncIterator.js +0 -25
- package/lib/asyncs/nextOfAsyncIterator.js.map +0 -1
- package/lib/asyncs/promiseOfCallback.js +0 -17
- package/lib/asyncs/promiseOfCallback.js.map +0 -1
- package/lib/asyncs/sleep.js +0 -3
- package/lib/asyncs/sleep.js.map +0 -1
- package/lib/asyncs/timeout.js +0 -25
- package/lib/asyncs/timeout.js.map +0 -1
- package/lib/browsers/copy.js +0 -53
- package/lib/browsers/copy.js.map +0 -1
- package/lib/browsers/download.js +0 -41
- package/lib/browsers/download.js.map +0 -1
- package/lib/browsers/getFileFromDataTransfer.js +0 -45
- package/lib/browsers/getFileFromDataTransfer.js.map +0 -1
- package/lib/browsers/loaders.js +0 -49
- package/lib/browsers/loaders.js.map +0 -1
- package/lib/crypto/base.js +0 -7
- package/lib/crypto/base.js.map +0 -1
- package/lib/crypto/getNodeCrypto.js +0 -18
- package/lib/crypto/getNodeCrypto.js.map +0 -1
- package/lib/crypto/getRandomValues.js +0 -29
- package/lib/crypto/getRandomValues.js.map +0 -1
- package/lib/crypto/hashing.js +0 -57
- package/lib/crypto/hashing.js.map +0 -1
- package/lib/crypto/md5.bench.js +0 -21
- package/lib/crypto/md5.bench.js.map +0 -1
- package/lib/crypto/md5.d.js +0 -3
- package/lib/crypto/md5.d.js.map +0 -1
- package/lib/crypto/md5.js +0 -121
- package/lib/crypto/md5.js.map +0 -1
- package/lib/crypto/pem/pem.js +0 -35
- package/lib/crypto/pem/pem.js.map +0 -1
- package/lib/crypto/randomUUID.js +0 -16
- package/lib/crypto/randomUUID.js.map +0 -1
- package/lib/crypto/ulid.js +0 -153
- package/lib/crypto/ulid.js.map +0 -1
- package/lib/errors/Errors.js +0 -181
- package/lib/errors/Errors.js.map +0 -1
- package/lib/fetch/createFetchWith.js +0 -23
- package/lib/fetch/createFetchWith.js.map +0 -1
- package/lib/fetch/createFetchWithLogging.js +0 -25
- package/lib/fetch/createFetchWithLogging.js.map +0 -1
- package/lib/fetch/createFetchWithRetry.js +0 -66
- package/lib/fetch/createFetchWithRetry.js.map +0 -1
- package/lib/fetch/dumpRequest.js +0 -45
- package/lib/fetch/dumpRequest.js.map +0 -1
- package/lib/fetch/dumpResponse.js +0 -16
- package/lib/fetch/dumpResponse.js.map +0 -1
- package/lib/fetch/index.js +0 -7
- package/lib/fetch/index.js.map +0 -1
- package/lib/fetch/types.js +0 -3
- package/lib/fetch/types.js.map +0 -1
- package/lib/http/HttpStatus.js +0 -94
- package/lib/http/HttpStatus.js.map +0 -1
- package/lib/i18n/createTranslate.js +0 -54
- package/lib/i18n/createTranslate.js.map +0 -1
- package/lib/index.js +0 -80
- package/lib/index.js.map +0 -1
- package/lib/io/AbstractEncoding.js +0 -7
- package/lib/io/AbstractEncoding.js.map +0 -1
- package/lib/io/ArrayBuffer.test-d.js +0 -7
- package/lib/io/ArrayBuffer.test-d.js.map +0 -1
- package/lib/io/ArrayBuffers.js +0 -210
- package/lib/io/ArrayBuffers.js.map +0 -1
- package/lib/io/Buffer.js +0 -25
- package/lib/io/Buffer.js.map +0 -1
- package/lib/io/base64.js +0 -54
- package/lib/io/base64.js.map +0 -1
- package/lib/io/isBuffer.js +0 -9
- package/lib/io/isBuffer.js.map +0 -1
- package/lib/io/isTransferable.js +0 -30
- package/lib/io/isTransferable.js.map +0 -1
- package/lib/isomorphics/getGlobalThis.js +0 -19
- package/lib/isomorphics/getGlobalThis.js.map +0 -1
- package/lib/isomorphics/structuredClone.js +0 -80
- package/lib/isomorphics/structuredClone.js.map +0 -1
- package/lib/langs/MaybeFunction.js +0 -9
- package/lib/langs/MaybeFunction.js.map +0 -1
- package/lib/langs/classOf.js +0 -5
- package/lib/langs/classOf.js.map +0 -1
- package/lib/langs/deepEqual.js +0 -87
- package/lib/langs/deepEqual.js.map +0 -1
- package/lib/langs/deepFreeze.js +0 -20
- package/lib/langs/deepFreeze.js.map +0 -1
- package/lib/langs/hashCode.ignored.js +0 -112
- package/lib/langs/hashCode.ignored.js.map +0 -1
- package/lib/langs/isClass.js +0 -5
- package/lib/langs/isClass.js.map +0 -1
- package/lib/langs/isDefined.js +0 -5
- package/lib/langs/isDefined.js.map +0 -1
- package/lib/langs/isEmptyObject.js +0 -6
- package/lib/langs/isEmptyObject.js.map +0 -1
- package/lib/langs/isFunction.js +0 -5
- package/lib/langs/isFunction.js.map +0 -1
- package/lib/langs/isPlainObject.js +0 -10
- package/lib/langs/isPlainObject.js.map +0 -1
- package/lib/langs/memoize.js +0 -22
- package/lib/langs/memoize.js.map +0 -1
- package/lib/langs/parseBoolean.js +0 -30
- package/lib/langs/parseBoolean.js.map +0 -1
- package/lib/langs/shallowClone.js +0 -15
- package/lib/langs/shallowClone.js.map +0 -1
- package/lib/langs/shallowEqual.js +0 -24
- package/lib/langs/shallowEqual.js.map +0 -1
- package/lib/libs/ms.js +0 -137
- package/lib/libs/ms.js.map +0 -1
- package/lib/logging/Logger.js +0 -5
- package/lib/logging/Logger.js.map +0 -1
- package/lib/logging/createChildLogger.js +0 -16
- package/lib/logging/createChildLogger.js.map +0 -1
- package/lib/logging/createLogger.js +0 -63
- package/lib/logging/createLogger.js.map +0 -1
- package/lib/logging/createNoopLogger.js +0 -13
- package/lib/logging/createNoopLogger.js.map +0 -1
- package/lib/logging/slog.js +0 -143
- package/lib/logging/slog.js.map +0 -1
- package/lib/maths/clamp.js +0 -21
- package/lib/maths/clamp.js.map +0 -1
- package/lib/maths/random.js +0 -17
- package/lib/maths/random.js.map +0 -1
- package/lib/modules/isModule.js +0 -7
- package/lib/modules/isModule.js.map +0 -1
- package/lib/modules/parseModuleId.js +0 -39
- package/lib/modules/parseModuleId.js.map +0 -1
- package/lib/objects/get.js +0 -16
- package/lib/objects/get.js.map +0 -1
- package/lib/objects/get.test-d.js +0 -33
- package/lib/objects/get.test-d.js.map +0 -1
- package/lib/objects/merge/index.js +0 -3
- package/lib/objects/merge/index.js.map +0 -1
- package/lib/objects/merge/isMergeableObject.js +0 -19
- package/lib/objects/merge/isMergeableObject.js.map +0 -1
- package/lib/objects/merge/merge.js +0 -89
- package/lib/objects/merge/merge.js.map +0 -1
- package/lib/objects/parseObjectPath.js +0 -39
- package/lib/objects/parseObjectPath.js.map +0 -1
- package/lib/objects/set.js +0 -38
- package/lib/objects/set.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/common/encoder.js +0 -100
- package/lib/schema/typebox/gen/codegen/common/encoder.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/common/formatter.js +0 -33
- package/lib/schema/typebox/gen/codegen/common/formatter.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/common/index.js +0 -5
- package/lib/schema/typebox/gen/codegen/common/index.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/common/jsdoc.js +0 -117
- package/lib/schema/typebox/gen/codegen/common/jsdoc.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/expression/compiler.js +0 -367
- package/lib/schema/typebox/gen/codegen/expression/compiler.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/expression/errors.js +0 -259
- package/lib/schema/typebox/gen/codegen/expression/errors.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/expression/evaluator.js +0 -254
- package/lib/schema/typebox/gen/codegen/expression/evaluator.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/expression/expression.js +0 -384
- package/lib/schema/typebox/gen/codegen/expression/expression.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/expression/index.js +0 -6
- package/lib/schema/typebox/gen/codegen/expression/index.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/index.js +0 -5
- package/lib/schema/typebox/gen/codegen/index.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/index.js +0 -16
- package/lib/schema/typebox/gen/codegen/model/index.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js +0 -239
- package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-expression.js +0 -359
- package/lib/schema/typebox/gen/codegen/model/model-to-expression.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js +0 -217
- package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js +0 -253
- package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js +0 -47
- package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js +0 -171
- package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js +0 -33
- package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js +0 -164
- package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js +0 -218
- package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-value.js +0 -43
- package/lib/schema/typebox/gen/codegen/model/model-to-value.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js +0 -206
- package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-yup.js +0 -204
- package/lib/schema/typebox/gen/codegen/model/model-to-yup.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-zod.js +0 -227
- package/lib/schema/typebox/gen/codegen/model/model-to-zod.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model.js +0 -27
- package/lib/schema/typebox/gen/codegen/model/model.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/typescript/index.js +0 -4
- package/lib/schema/typebox/gen/codegen/typescript/index.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js +0 -48
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js +0 -596
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js.map +0 -1
- package/lib/schema/typebox/gen/index.js +0 -3
- package/lib/schema/typebox/gen/index.js.map +0 -1
- package/lib/schema/typebox/index.js +0 -3
- package/lib/schema/typebox/index.js.map +0 -1
- package/lib/schema/typebox/typebox.js +0 -23
- package/lib/schema/typebox/typebox.js.map +0 -1
- package/lib/server.js +0 -7
- package/lib/server.js.map +0 -1
- package/lib/servers/crypto/md5.js +0 -6
- package/lib/servers/crypto/md5.js.map +0 -1
- package/lib/servers/fetch/createFetchWithProxy.js +0 -4
- package/lib/servers/fetch/createFetchWithProxy.js.map +0 -1
- package/lib/servers/fetch/createFetchWithProxyByNodeFetch.js +0 -38
- package/lib/servers/fetch/createFetchWithProxyByNodeFetch.js.map +0 -1
- package/lib/servers/fetch/createFetchWithProxyByUndici.js +0 -64
- package/lib/servers/fetch/createFetchWithProxyByUndici.js.map +0 -1
- package/lib/servers/getPackageDir.js +0 -16
- package/lib/servers/getPackageDir.js.map +0 -1
- package/lib/servers/jsdom.js +0 -3
- package/lib/servers/jsdom.js.map +0 -1
- package/lib/servers/node-fetch.js +0 -4
- package/lib/servers/node-fetch.js.map +0 -1
- package/lib/servers/polyfill/polyfillBrowser.js +0 -17
- package/lib/servers/polyfill/polyfillBrowser.js.map +0 -1
- package/lib/servers/polyfill/polyfillCrypto.js +0 -11
- package/lib/servers/polyfill/polyfillCrypto.js.map +0 -1
- package/lib/servers/polyfill/polyfillFetch.js +0 -25
- package/lib/servers/polyfill/polyfillFetch.js.map +0 -1
- package/lib/servers/polyfill/polyfillJsDom.js +0 -64
- package/lib/servers/polyfill/polyfillJsDom.js.map +0 -1
- package/lib/servers/polyfill/polyfillWebSocket.js +0 -16
- package/lib/servers/polyfill/polyfillWebSocket.js.map +0 -1
- package/lib/servers/ws.js +0 -3
- package/lib/servers/ws.js.map +0 -1
- package/lib/strings/camelCase.js +0 -58
- package/lib/strings/camelCase.js.map +0 -1
- package/lib/strings/formatBytes.js +0 -43
- package/lib/strings/formatBytes.js.map +0 -1
- package/lib/strings/renderTemplate.js +0 -32
- package/lib/strings/renderTemplate.js.map +0 -1
- package/lib/typedoc.js +0 -4
- package/lib/typedoc.js.map +0 -1
- package/lib/types/global.d.js +0 -2
- package/lib/types/global.d.js.map +0 -1
- package/lib/types/index.d.js +0 -6
- package/lib/types/index.d.js.map +0 -1
- package/lib/validations/asserts.js +0 -14
- package/lib/validations/asserts.js.map +0 -1
- package/lib/validations/isUUID.js +0 -5
- package/lib/validations/isUUID.js.map +0 -1
- package/lib/validations/parseTimestamp.js +0 -29
- package/lib/validations/parseTimestamp.js.map +0 -1
- package/schema/typebox/gen/index.ts +0 -1
- package/schema/typebox/index.ts +0 -1
- package/server/jsdom.ts +0 -1
- package/server/node-fetch.ts +0 -1
- package/server/ws.ts +0 -1
- package/server.ts +0 -1
- package/src/server.ts +0 -6
- package/src/types/global.d.ts +0 -11
- package/types.d.ts +0 -1
- /package/src/{servers → server}/crypto/md5.ts +0 -0
- /package/src/{servers → server}/fetch/createFetchWithProxy.ts +0 -0
- /package/src/{servers → server}/fetch/createFetchWithProxyByNodeFetch.ts +0 -0
- /package/src/{servers → server}/fetch/createFetchWithProxyByUndici.ts +0 -0
- /package/src/{servers → server}/getPackageDir.ts +0 -0
- /package/src/{servers → server}/jsdom.ts +0 -0
- /package/src/{servers → server}/node-fetch.ts +0 -0
- /package/src/{servers → server}/polyfill/polyfillBrowser.test.ts +0 -0
- /package/src/{servers → server}/polyfill/polyfillBrowser.ts +0 -0
- /package/src/{servers → server}/polyfill/polyfillCrypto.ts +0 -0
- /package/src/{servers → server}/polyfill/polyfillFetch.ts +0 -0
- /package/src/{servers → server}/polyfill/polyfillJsDom.ts +0 -0
- /package/src/{servers → server}/ws.ts +0 -0
|
@@ -1,596 +0,0 @@
|
|
|
1
|
-
import * as ts from 'typescript';
|
|
2
|
-
import { JsDoc } from '../common/jsdoc';
|
|
3
|
-
export class TypeScriptToTypeBoxError extends Error {
|
|
4
|
-
diagnostics;
|
|
5
|
-
constructor(diagnostics){
|
|
6
|
-
super('');
|
|
7
|
-
this.diagnostics = diagnostics;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
/** Generates TypeBox types from TypeScript code */ export var TypeScriptToTypeBox;
|
|
11
|
-
(function(TypeScriptToTypeBox) {
|
|
12
|
-
// ------------------------------------------------------------------------------------------------------------
|
|
13
|
-
// Transpile Options
|
|
14
|
-
// ------------------------------------------------------------------------------------------------------------
|
|
15
|
-
const transpilerOptions = {
|
|
16
|
-
compilerOptions: {
|
|
17
|
-
strict: true,
|
|
18
|
-
target: ts.ScriptTarget.ES2022
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
// ------------------------------------------------------------------------------------------------------------
|
|
22
|
-
// Transpile States
|
|
23
|
-
// ------------------------------------------------------------------------------------------------------------
|
|
24
|
-
// (auto) tracked on calls to find type name
|
|
25
|
-
const typenames = new Set();
|
|
26
|
-
// (auto) tracked for recursive types and used to associate This type references
|
|
27
|
-
let recursiveDeclaration = null;
|
|
28
|
-
// (auto) tracked for scoped block level definitions and used to prevent `export` emit when not in global scope.
|
|
29
|
-
let blockLevel = 0;
|
|
30
|
-
// (auto) tracked for injecting typebox import statements
|
|
31
|
-
let useImports = false;
|
|
32
|
-
// (auto) tracked for injecting JSON schema optios
|
|
33
|
-
let useOptions = false;
|
|
34
|
-
// (auto) tracked for injecting TSchema import statements
|
|
35
|
-
let useGenerics = false;
|
|
36
|
-
// (auto) tracked for cases where composition requires deep clone
|
|
37
|
-
let useCloneType = false;
|
|
38
|
-
// (option) export override to ensure all schematics
|
|
39
|
-
let useExportsEverything = false;
|
|
40
|
-
// (option) inject identifiers
|
|
41
|
-
let useIdentifiers = false;
|
|
42
|
-
// (option) specifies if typebox imports should be included
|
|
43
|
-
let useTypeBoxImport = true;
|
|
44
|
-
// ------------------------------------------------------------------------------------------------------------
|
|
45
|
-
// AST Query
|
|
46
|
-
// ------------------------------------------------------------------------------------------------------------
|
|
47
|
-
function FindRecursiveParent(decl, node) {
|
|
48
|
-
return ts.isTypeReferenceNode(node) && decl.name.getText() === node.typeName.getText() || node.getChildren().some((node)=>FindRecursiveParent(decl, node));
|
|
49
|
-
}
|
|
50
|
-
function FindRecursiveThis(node) {
|
|
51
|
-
return node.getChildren().some((node)=>ts.isThisTypeNode(node) || FindRecursiveThis(node));
|
|
52
|
-
}
|
|
53
|
-
function FindTypeName(node, name) {
|
|
54
|
-
const found = typenames.has(name) || node.getChildren().some((node)=>{
|
|
55
|
-
return (ts.isInterfaceDeclaration(node) || ts.isTypeAliasDeclaration(node)) && node.name.getText() === name || FindTypeName(node, name);
|
|
56
|
-
});
|
|
57
|
-
if (found) typenames.add(name);
|
|
58
|
-
return found;
|
|
59
|
-
}
|
|
60
|
-
function IsRecursiveType(decl) {
|
|
61
|
-
const check1 = ts.isTypeAliasDeclaration(decl) ? [
|
|
62
|
-
decl.type
|
|
63
|
-
].some((node)=>FindRecursiveParent(decl, node)) : decl.members.some((node)=>FindRecursiveParent(decl, node));
|
|
64
|
-
const check2 = ts.isInterfaceDeclaration(decl) && FindRecursiveThis(decl);
|
|
65
|
-
return check1 || check2;
|
|
66
|
-
}
|
|
67
|
-
function IsReadonlyProperty(node) {
|
|
68
|
-
return node.modifiers !== undefined && node.modifiers.find((modifier)=>modifier.getText() === 'readonly') !== undefined;
|
|
69
|
-
}
|
|
70
|
-
function IsOptionalProperty(node) {
|
|
71
|
-
return node.questionToken !== undefined;
|
|
72
|
-
}
|
|
73
|
-
function IsExport(node) {
|
|
74
|
-
return blockLevel === 0 && (useExportsEverything || node.modifiers !== undefined && node.modifiers.find((modifier)=>modifier.getText() === 'export') !== undefined);
|
|
75
|
-
}
|
|
76
|
-
function IsNamespace(node) {
|
|
77
|
-
return node.flags === ts.NodeFlags.Namespace;
|
|
78
|
-
}
|
|
79
|
-
// ------------------------------------------------------------------------------------------------------------
|
|
80
|
-
// Options
|
|
81
|
-
// ------------------------------------------------------------------------------------------------------------
|
|
82
|
-
function ResolveJsDocComment(node) {
|
|
83
|
-
const content = node.getFullText().trim();
|
|
84
|
-
if (node.kind === ts.SyntaxKind.LiteralType) {
|
|
85
|
-
if (content.startsWith('/**')) {
|
|
86
|
-
// add a new line for JsDoc to parse properly
|
|
87
|
-
return content.slice(0, content.lastIndexOf('*/') + 2).replace(/\*+\/$/, '\n$0');
|
|
88
|
-
} else {
|
|
89
|
-
return '';
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
const indices = [
|
|
93
|
-
content.indexOf('/**'),
|
|
94
|
-
content.indexOf('type'),
|
|
95
|
-
content.indexOf('interface')
|
|
96
|
-
].map((n)=>n === -1 ? Infinity : n);
|
|
97
|
-
if (indices[0] === -1 || indices[1] < indices[0] || indices[2] < indices[0]) return ''; // no comment or declaration before comment
|
|
98
|
-
for(let i = indices[0]; i < content.length; i++){
|
|
99
|
-
if (content[i] === '*' && content[i + 1] === '/') return content.slice(0, i + 2);
|
|
100
|
-
}
|
|
101
|
-
return '';
|
|
102
|
-
}
|
|
103
|
-
function ResolveOptions(node) {
|
|
104
|
-
const content = ResolveJsDocComment(node);
|
|
105
|
-
return JsDoc.Parse(content);
|
|
106
|
-
}
|
|
107
|
-
// ------------------------------------------------------------------------------------------------------------
|
|
108
|
-
// Identifiers
|
|
109
|
-
// ------------------------------------------------------------------------------------------------------------
|
|
110
|
-
function ResolveIdentifier(node) {
|
|
111
|
-
function* resolve(node) {
|
|
112
|
-
if (node.parent) yield* resolve(node.parent);
|
|
113
|
-
if (ts.isModuleDeclaration(node)) yield node.name.getText();
|
|
114
|
-
}
|
|
115
|
-
return [
|
|
116
|
-
...resolve(node),
|
|
117
|
-
node.name.getText()
|
|
118
|
-
].join('.');
|
|
119
|
-
}
|
|
120
|
-
function UnwrapModifier(type) {
|
|
121
|
-
for(let i = 0; i < type.length; i++)if (type[i] === '(') return type.slice(i + 1, type.length - 1);
|
|
122
|
-
return type;
|
|
123
|
-
}
|
|
124
|
-
// Note: This function is only called when 'useIdentifiers' is true. What we're trying to achieve with
|
|
125
|
-
// identifier injection is a referential type model over the default inline model. For the purposes of
|
|
126
|
-
// code generation, we tend to prefer referential types as these can be both inlined or referenced in
|
|
127
|
-
// the codegen target; and where different targets may have different referential requirements. It
|
|
128
|
-
// should be possible to implement a more robust injection mechanism however. For review.
|
|
129
|
-
// prettier-ignore
|
|
130
|
-
function InjectOptions(type, options) {
|
|
131
|
-
if (globalThis.Object.keys(options).length === 0) return type;
|
|
132
|
-
// unwrap for modifiers
|
|
133
|
-
if (type.indexOf('Type.ReadonlyOptional') === 0) return `Type.ReadonlyOptional( ${InjectOptions(UnwrapModifier(type), options)} )`;
|
|
134
|
-
if (type.indexOf('Type.Readonly') === 0) return `Type.Readonly( ${InjectOptions(UnwrapModifier(type), options)} )`;
|
|
135
|
-
if (type.indexOf('Type.Optional') === 0) return `Type.Optional( ${InjectOptions(UnwrapModifier(type), options)} )`;
|
|
136
|
-
const encoded = JSON.stringify(options);
|
|
137
|
-
// indexer type
|
|
138
|
-
if (type.lastIndexOf(']') === type.length - 1) useCloneType = true;
|
|
139
|
-
if (type.lastIndexOf(']') === type.length - 1) return `CloneType(${type}, ${encoded})`;
|
|
140
|
-
// referenced type
|
|
141
|
-
if (type.indexOf('(') === -1) {
|
|
142
|
-
useCloneType = true;
|
|
143
|
-
return `CloneType(${type}, ${encoded})`;
|
|
144
|
-
}
|
|
145
|
-
if (type.lastIndexOf('()') === type.length - 2) return type.slice(0, type.length - 1) + `${encoded})`;
|
|
146
|
-
if (type.lastIndexOf('})') === type.length - 2) return type.slice(0, type.length - 1) + `, ${encoded})`;
|
|
147
|
-
if (type.lastIndexOf('])') === type.length - 2) return type.slice(0, type.length - 1) + `, ${encoded})`;
|
|
148
|
-
if (type.lastIndexOf(')') === type.length - 1) return type.slice(0, type.length - 1) + `, ${encoded})`;
|
|
149
|
-
return type;
|
|
150
|
-
}
|
|
151
|
-
// ------------------------------------------------------------------------------------------------------------
|
|
152
|
-
// Nodes
|
|
153
|
-
// ------------------------------------------------------------------------------------------------------------
|
|
154
|
-
function* SourceFile(node) {
|
|
155
|
-
for (const next of node.getChildren()){
|
|
156
|
-
yield* Visit(next);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
function* PropertySignature(node) {
|
|
160
|
-
const [readonly, optional] = [
|
|
161
|
-
IsReadonlyProperty(node),
|
|
162
|
-
IsOptionalProperty(node)
|
|
163
|
-
];
|
|
164
|
-
const options = ResolveOptions(node);
|
|
165
|
-
const type_0 = Collect(node.type);
|
|
166
|
-
const type_1 = InjectOptions(type_0, options);
|
|
167
|
-
if (readonly && optional) {
|
|
168
|
-
return yield `${node.name.getText()}: Type.ReadonlyOptional(${type_1})`;
|
|
169
|
-
} else if (readonly) {
|
|
170
|
-
return yield `${node.name.getText()}: Type.Readonly(${type_1})`;
|
|
171
|
-
} else if (optional) {
|
|
172
|
-
return yield `${node.name.getText()}: Type.Optional(${type_1})`;
|
|
173
|
-
} else {
|
|
174
|
-
return yield `${node.name.getText()}: ${type_1}`;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
function* ArrayTypeNode(node) {
|
|
178
|
-
const type = Collect(node.elementType);
|
|
179
|
-
yield `Type.Array(${type})`;
|
|
180
|
-
}
|
|
181
|
-
function* Block(node) {
|
|
182
|
-
blockLevel += 1;
|
|
183
|
-
const statments = node.statements.map((statement)=>Collect(statement)).join('\n\n');
|
|
184
|
-
blockLevel -= 1;
|
|
185
|
-
yield `{\n${statments}\n}`;
|
|
186
|
-
}
|
|
187
|
-
function* TupleTypeNode(node) {
|
|
188
|
-
const types = node.elements.map((type)=>Collect(type)).join(',\n');
|
|
189
|
-
yield `Type.Tuple([\n${types}\n])`;
|
|
190
|
-
}
|
|
191
|
-
function* UnionTypeNode(node) {
|
|
192
|
-
const types = node.types.map((type)=>Collect(type)).join(',\n');
|
|
193
|
-
yield `Type.Union([\n${types}\n])`;
|
|
194
|
-
}
|
|
195
|
-
function* MappedTypeNode(node) {
|
|
196
|
-
const K = Collect(node.typeParameter);
|
|
197
|
-
const T = Collect(node.type);
|
|
198
|
-
const C = Collect(node.typeParameter.constraint);
|
|
199
|
-
const readonly = node.readonlyToken !== undefined;
|
|
200
|
-
const optional = node.questionToken !== undefined;
|
|
201
|
-
const readonly_subtractive = readonly && ts.isMinusToken(node.readonlyToken);
|
|
202
|
-
const optional_subtractive = optional && ts.isMinusToken(node.questionToken);
|
|
203
|
-
// prettier-ignore
|
|
204
|
-
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})`;
|
|
205
|
-
}
|
|
206
|
-
function* MethodSignature(node) {
|
|
207
|
-
const parameters = node.parameters.map((parameter)=>parameter.dotDotDotToken !== undefined ? `...Type.Rest(${Collect(parameter)})` : Collect(parameter)).join(', ');
|
|
208
|
-
const returnType = node.type === undefined ? `Type.Unknown()` : Collect(node.type);
|
|
209
|
-
yield `${node.name.getText()}: Type.Function([${parameters}], ${returnType})`;
|
|
210
|
-
}
|
|
211
|
-
// prettier-ignore
|
|
212
|
-
function* TemplateLiteralTypeNode(node) {
|
|
213
|
-
const collect = node.getChildren().map((node)=>Collect(node)).join('');
|
|
214
|
-
yield `Type.TemplateLiteral([${collect.slice(0, collect.length - 2)}])` // can't remove trailing here
|
|
215
|
-
;
|
|
216
|
-
}
|
|
217
|
-
// prettier-ignore
|
|
218
|
-
function* TemplateLiteralTypeSpan(node) {
|
|
219
|
-
const collect = node.getChildren().map((node)=>Collect(node)).join(', ');
|
|
220
|
-
if (collect.length > 0) yield `${collect}`;
|
|
221
|
-
}
|
|
222
|
-
function* TemplateHead(node) {
|
|
223
|
-
if (node.text.length > 0) yield `Type.Literal('${node.text}'), `;
|
|
224
|
-
}
|
|
225
|
-
function* TemplateMiddle(node) {
|
|
226
|
-
if (node.text.length > 0) yield `Type.Literal('${node.text}'), `;
|
|
227
|
-
}
|
|
228
|
-
function* TemplateTail(node) {
|
|
229
|
-
if (node.text.length > 0) yield `Type.Literal('${node.text}'), `;
|
|
230
|
-
}
|
|
231
|
-
function* ThisTypeNode(node) {
|
|
232
|
-
yield `This`;
|
|
233
|
-
}
|
|
234
|
-
function* IntersectionTypeNode(node) {
|
|
235
|
-
const types = node.types.map((type)=>Collect(type)).join(',\n');
|
|
236
|
-
yield `Type.Intersect([\n${types}\n])`;
|
|
237
|
-
}
|
|
238
|
-
function* TypeOperatorNode(node) {
|
|
239
|
-
if (node.operator === ts.SyntaxKind.KeyOfKeyword) {
|
|
240
|
-
const type = Collect(node.type);
|
|
241
|
-
yield `Type.KeyOf(${type})`;
|
|
242
|
-
}
|
|
243
|
-
if (node.operator === ts.SyntaxKind.ReadonlyKeyword) {
|
|
244
|
-
yield `Type.Readonly(${Collect(node.type)})`;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
function* Parameter(node) {
|
|
248
|
-
yield Collect(node.type);
|
|
249
|
-
}
|
|
250
|
-
function* FunctionTypeNode(node) {
|
|
251
|
-
const parameters = node.parameters.map((parameter)=>parameter.dotDotDotToken !== undefined ? `...Type.Rest(${Collect(parameter)})` : Collect(parameter)).join(', ');
|
|
252
|
-
const returns = Collect(node.type);
|
|
253
|
-
yield `Type.Function([${parameters}], ${returns})`;
|
|
254
|
-
}
|
|
255
|
-
function* ConstructorTypeNode(node) {
|
|
256
|
-
const parameters = node.parameters.map((param)=>Collect(param)).join(', ');
|
|
257
|
-
const returns = Collect(node.type);
|
|
258
|
-
yield `Type.Constructor([${parameters}], ${returns})`;
|
|
259
|
-
}
|
|
260
|
-
function* EnumDeclaration(node) {
|
|
261
|
-
useImports = true;
|
|
262
|
-
const exports = IsExport(node) ? 'export ' : '';
|
|
263
|
-
const members = node.members.map((member)=>member.getText()).join(', ');
|
|
264
|
-
const enumType = `${exports}enum Enum${node.name.getText()} { ${members} }`;
|
|
265
|
-
const staticType = `${exports}type ${node.name.getText()} = Static<typeof ${node.name.getText()}>`;
|
|
266
|
-
const type = `${exports}const ${node.name.getText()} = Type.Enum(Enum${node.name.getText()})`;
|
|
267
|
-
yield [
|
|
268
|
-
enumType,
|
|
269
|
-
'',
|
|
270
|
-
staticType,
|
|
271
|
-
type
|
|
272
|
-
].join('\n');
|
|
273
|
-
}
|
|
274
|
-
function PropertiesFromTypeElementArray(members) {
|
|
275
|
-
const properties = members.filter((member)=>!ts.isIndexSignatureDeclaration(member));
|
|
276
|
-
const indexers = members.filter((member)=>ts.isIndexSignatureDeclaration(member));
|
|
277
|
-
const propertyCollect = properties.map((property)=>Collect(property)).join(',\n');
|
|
278
|
-
const indexer = indexers.length > 0 ? Collect(indexers[indexers.length - 1]) : '';
|
|
279
|
-
if (properties.length === 0 && indexer.length > 0) {
|
|
280
|
-
return `{},\n{\nadditionalProperties: ${indexer}\n }`;
|
|
281
|
-
} else if (properties.length > 0 && indexer.length > 0) {
|
|
282
|
-
return `{\n${propertyCollect}\n},\n{\nadditionalProperties: ${indexer}\n }`;
|
|
283
|
-
} else {
|
|
284
|
-
return `{\n${propertyCollect}\n}`;
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
function* TypeLiteralNode(node) {
|
|
288
|
-
const members = PropertiesFromTypeElementArray(node.members);
|
|
289
|
-
yield* `Type.Object(${members})`;
|
|
290
|
-
}
|
|
291
|
-
function* InterfaceDeclaration(node) {
|
|
292
|
-
useImports = true;
|
|
293
|
-
const isRecursiveType = IsRecursiveType(node);
|
|
294
|
-
if (isRecursiveType) recursiveDeclaration = node;
|
|
295
|
-
const heritage = node.heritageClauses !== undefined ? node.heritageClauses.flatMap((node)=>Collect(node)) : [];
|
|
296
|
-
if (node.typeParameters) {
|
|
297
|
-
useGenerics = true;
|
|
298
|
-
const exports = IsExport(node) ? 'export ' : '';
|
|
299
|
-
const identifier = ResolveIdentifier(node);
|
|
300
|
-
const options = useIdentifiers ? {
|
|
301
|
-
...ResolveOptions(node),
|
|
302
|
-
$id: identifier
|
|
303
|
-
} : {
|
|
304
|
-
...ResolveOptions(node)
|
|
305
|
-
};
|
|
306
|
-
const constraints = node.typeParameters.map((param)=>`${Collect(param)} extends TSchema`).join(', ');
|
|
307
|
-
const parameters = node.typeParameters.map((param)=>`${Collect(param)}: ${Collect(param)}`).join(', ');
|
|
308
|
-
const members = PropertiesFromTypeElementArray(node.members);
|
|
309
|
-
// const names = node.typeParameters.map((param) => `${Collect(param)}`).join(', ')
|
|
310
|
-
// const staticDeclaration = `${exports}type ${node.name.getText()}<${constraints}> = Static<ReturnType<typeof ${node.name.getText()}<${names}>>>`
|
|
311
|
-
const rawTypeExpression = IsRecursiveType(node) ? `Type.Recursive(This => Type.Object(${members}))` : `Type.Object(${members})`;
|
|
312
|
-
const typeExpression = heritage.length === 0 ? rawTypeExpression : `Type.Composite([${heritage.join(', ')}, ${rawTypeExpression}])`;
|
|
313
|
-
const type = InjectOptions(typeExpression, options);
|
|
314
|
-
const typeDeclaration = `${exports}const ${node.name.getText()} = <${constraints}>(${parameters}) => ${type}`;
|
|
315
|
-
yield `${typeDeclaration}`;
|
|
316
|
-
} else {
|
|
317
|
-
const exports = IsExport(node) ? 'export ' : '';
|
|
318
|
-
const identifier = ResolveIdentifier(node);
|
|
319
|
-
const options = useIdentifiers ? {
|
|
320
|
-
...ResolveOptions(node),
|
|
321
|
-
$id: identifier
|
|
322
|
-
} : {
|
|
323
|
-
...ResolveOptions(node)
|
|
324
|
-
};
|
|
325
|
-
const members = PropertiesFromTypeElementArray(node.members);
|
|
326
|
-
const staticDeclaration = `${exports}type ${node.name.getText()} = Static<typeof ${node.name.getText()}>`;
|
|
327
|
-
const rawTypeExpression = IsRecursiveType(node) ? `Type.Recursive(This => Type.Object(${members}))` : `Type.Object(${members})`;
|
|
328
|
-
const typeExpression = heritage.length === 0 ? rawTypeExpression : `Type.Composite([${heritage.join(', ')}, ${rawTypeExpression}])`;
|
|
329
|
-
const type = InjectOptions(typeExpression, options);
|
|
330
|
-
const typeDeclaration = `${exports}const ${node.name.getText()} = ${type}`;
|
|
331
|
-
yield `${staticDeclaration}\n${typeDeclaration}`;
|
|
332
|
-
}
|
|
333
|
-
recursiveDeclaration = null;
|
|
334
|
-
}
|
|
335
|
-
function* TypeAliasDeclaration(node) {
|
|
336
|
-
useImports = true;
|
|
337
|
-
const isRecursiveType = IsRecursiveType(node);
|
|
338
|
-
if (isRecursiveType) recursiveDeclaration = node;
|
|
339
|
-
// Generics case
|
|
340
|
-
if (node.typeParameters) {
|
|
341
|
-
useGenerics = true;
|
|
342
|
-
const exports = IsExport(node) ? 'export ' : '';
|
|
343
|
-
const options = useIdentifiers ? {
|
|
344
|
-
$id: ResolveIdentifier(node)
|
|
345
|
-
} : {};
|
|
346
|
-
const constraints = node.typeParameters.map((param)=>`${Collect(param)} extends TSchema`).join(', ');
|
|
347
|
-
const parameters = node.typeParameters.map((param)=>`${Collect(param)}: ${Collect(param)}`).join(', ');
|
|
348
|
-
const type_0 = Collect(node.type);
|
|
349
|
-
const type_1 = isRecursiveType ? `Type.Recursive(This => ${type_0})` : type_0;
|
|
350
|
-
const type_2 = InjectOptions(type_1, options);
|
|
351
|
-
// const names = node.typeParameters.map((param) => Collect(param)).join(', ')
|
|
352
|
-
// const staticDeclaration = `${exports}type ${node.name.getText()}<${constraints}> = Static<ReturnType<typeof ${node.name.getText()}<${names}>>>`
|
|
353
|
-
const typeDeclaration = `${exports}const ${node.name.getText()} = <${constraints}>(${parameters}) => ${type_2}`;
|
|
354
|
-
yield `${typeDeclaration}`;
|
|
355
|
-
} else {
|
|
356
|
-
const exports = IsExport(node) ? 'export ' : '';
|
|
357
|
-
const options = useIdentifiers ? {
|
|
358
|
-
$id: ResolveIdentifier(node),
|
|
359
|
-
...ResolveOptions(node)
|
|
360
|
-
} : {
|
|
361
|
-
...ResolveOptions(node)
|
|
362
|
-
};
|
|
363
|
-
const type_0 = Collect(node.type);
|
|
364
|
-
const type_1 = isRecursiveType ? `Type.Recursive(This => ${type_0})` : type_0;
|
|
365
|
-
const type_2 = InjectOptions(type_1, options);
|
|
366
|
-
const staticDeclaration = `${exports}type ${node.name.getText()} = Static<typeof ${node.name.getText()}>`;
|
|
367
|
-
const typeDeclaration = `${exports}const ${node.name.getText()} = ${type_2}`;
|
|
368
|
-
yield `${staticDeclaration}\n${typeDeclaration}`;
|
|
369
|
-
}
|
|
370
|
-
recursiveDeclaration = null;
|
|
371
|
-
}
|
|
372
|
-
function* HeritageClause(node) {
|
|
373
|
-
// Consideration: This may be better expressed as a heritage type (for review)
|
|
374
|
-
const types = node.types.map((node)=>Collect(node));
|
|
375
|
-
if (types.length === 1) return yield types[0];
|
|
376
|
-
// yield `Type.Composite([${types.join(', ')}])`
|
|
377
|
-
// Note: Heritage clauses are only used in extends cases, and where Composite
|
|
378
|
-
// is the exterior type. These types will be prepended to the Composite array.
|
|
379
|
-
yield types.join(', ');
|
|
380
|
-
}
|
|
381
|
-
function* IndexedAccessType(node) {
|
|
382
|
-
const obj = node.objectType.getText();
|
|
383
|
-
const key = Collect(node.indexType);
|
|
384
|
-
yield `Type.Index(${obj}, ${key})`;
|
|
385
|
-
}
|
|
386
|
-
function* ExpressionWithTypeArguments(node) {
|
|
387
|
-
const name = Collect(node.expression);
|
|
388
|
-
const typeArguments = node.typeArguments === undefined ? [] : node.typeArguments.map((node)=>Collect(node));
|
|
389
|
-
// todo: default type argument (resolve `= number` from `type Foo<T = number>`)
|
|
390
|
-
return yield typeArguments.length === 0 ? `${name}` : `${name}(${typeArguments.join(', ')})`;
|
|
391
|
-
}
|
|
392
|
-
function* TypeParameterDeclaration(node) {
|
|
393
|
-
yield node.name.getText();
|
|
394
|
-
}
|
|
395
|
-
function* ParenthesizedTypeNode(node) {
|
|
396
|
-
yield Collect(node.type);
|
|
397
|
-
}
|
|
398
|
-
function* RestTypeNode(node) {
|
|
399
|
-
yield `...Type.Rest(${node.type.getText()})`;
|
|
400
|
-
}
|
|
401
|
-
function* ConditionalTypeNode(node) {
|
|
402
|
-
const checkType = Collect(node.checkType);
|
|
403
|
-
const extendsType = Collect(node.extendsType);
|
|
404
|
-
const trueType = Collect(node.trueType);
|
|
405
|
-
const falseType = Collect(node.falseType);
|
|
406
|
-
yield `Type.Extends(${checkType}, ${extendsType}, ${trueType}, ${falseType})`;
|
|
407
|
-
}
|
|
408
|
-
function* isIndexSignatureDeclaration(node) {
|
|
409
|
-
// note: we ignore the key and just return the type. this is a mismatch between
|
|
410
|
-
// object and record types. Address in TypeBox by unifying validation paths
|
|
411
|
-
// for objects and record types.
|
|
412
|
-
yield Collect(node.type);
|
|
413
|
-
}
|
|
414
|
-
function* TypeReferenceNode(node) {
|
|
415
|
-
const name = node.typeName.getText();
|
|
416
|
-
const args = node.typeArguments ? `(${node.typeArguments.map((type)=>Collect(type)).join(', ')})` : '';
|
|
417
|
-
// --------------------------------------------------------------
|
|
418
|
-
// Instance Types
|
|
419
|
-
// --------------------------------------------------------------
|
|
420
|
-
if (name === 'Date') return yield `Type.Date()`;
|
|
421
|
-
if (name === 'Uint8Array') return yield `Type.Uint8Array()`;
|
|
422
|
-
if (name === 'String') return yield `Type.String()`;
|
|
423
|
-
if (name === 'Number') return yield `Type.Number()`;
|
|
424
|
-
if (name === 'Boolean') return yield `Type.Boolean()`;
|
|
425
|
-
if (name === 'Function') return yield `Type.Function([], Type.Unknown())`;
|
|
426
|
-
// --------------------------------------------------------------
|
|
427
|
-
// Types
|
|
428
|
-
// --------------------------------------------------------------
|
|
429
|
-
if (name === 'Array') return yield `Type.Array${args}`;
|
|
430
|
-
if (name === 'Record') return yield `Type.Record${args}`;
|
|
431
|
-
if (name === 'Partial') return yield `Type.Partial${args}`;
|
|
432
|
-
if (name === 'Required') return yield `Type.Required${args}`;
|
|
433
|
-
if (name === 'Omit') return yield `Type.Omit${args}`;
|
|
434
|
-
if (name === 'Pick') return yield `Type.Pick${args}`;
|
|
435
|
-
if (name === 'Promise') return yield `Type.Promise${args}`;
|
|
436
|
-
if (name === 'ReturnType') return yield `Type.ReturnType${args}`;
|
|
437
|
-
if (name === 'InstanceType') return yield `Type.InstanceType${args}`;
|
|
438
|
-
if (name === 'Parameters') return yield `Type.Parameters${args}`;
|
|
439
|
-
if (name === 'AsyncIterableIterator') return yield `Type.AsyncIterator${args}`;
|
|
440
|
-
if (name === 'IterableIterator') return yield `Type.Iterator${args}`;
|
|
441
|
-
if (name === 'ConstructorParameters') return yield `Type.ConstructorParameters${args}`;
|
|
442
|
-
if (name === 'Exclude') return yield `Type.Exclude${args}`;
|
|
443
|
-
if (name === 'Extract') return yield `Type.Extract${args}`;
|
|
444
|
-
if (name === 'Awaited') return yield `Type.Awaited${args}`;
|
|
445
|
-
if (name === 'Uppercase') return yield `Type.Uppercase${args}`;
|
|
446
|
-
if (name === 'Lowercase') return yield `Type.Lowercase${args}`;
|
|
447
|
-
if (name === 'Capitalize') return yield `Type.Capitalize${args}`;
|
|
448
|
-
if (name === 'Uncapitalize') return yield `Type.Uncapitalize${args}`;
|
|
449
|
-
if (recursiveDeclaration !== null && FindRecursiveParent(recursiveDeclaration, node)) return yield `This`;
|
|
450
|
-
if (FindTypeName(node.getSourceFile(), name) && args.length === 0 /** non-resolvable */ ) {
|
|
451
|
-
return yield `${name}${args}`;
|
|
452
|
-
}
|
|
453
|
-
if (name in globalThis) return yield `Type.Never()`;
|
|
454
|
-
return yield `${name}${args}`;
|
|
455
|
-
}
|
|
456
|
-
function* LiteralTypeNode(node) {
|
|
457
|
-
const text = node.getText();
|
|
458
|
-
if (text === 'null') return yield `Type.Null()`;
|
|
459
|
-
yield InjectOptions(`Type.Literal(${node.getText()})`, ResolveOptions(node));
|
|
460
|
-
}
|
|
461
|
-
function* ModuleDeclaration(node) {
|
|
462
|
-
const export_specifier = IsExport(node) ? 'export ' : '';
|
|
463
|
-
const module_specifier = IsNamespace(node) ? 'namespace' : 'module';
|
|
464
|
-
yield `${export_specifier}${module_specifier} ${node.name.getText()} {`;
|
|
465
|
-
yield* Visit(node.body);
|
|
466
|
-
yield `}`;
|
|
467
|
-
}
|
|
468
|
-
function* ModuleBlock(node) {
|
|
469
|
-
for (const statement of node.statements){
|
|
470
|
-
yield* Visit(statement);
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
function* FunctionDeclaration(node) {
|
|
474
|
-
// ignore
|
|
475
|
-
}
|
|
476
|
-
function* ClassDeclaration(node) {
|
|
477
|
-
// ignore
|
|
478
|
-
}
|
|
479
|
-
function Collect(node) {
|
|
480
|
-
return `${[
|
|
481
|
-
...Visit(node)
|
|
482
|
-
].join('')}`;
|
|
483
|
-
}
|
|
484
|
-
function* Visit(node) {
|
|
485
|
-
if (node === undefined) return;
|
|
486
|
-
if (ts.isArrayTypeNode(node)) return yield* ArrayTypeNode(node);
|
|
487
|
-
if (ts.isBlock(node)) return yield* Block(node);
|
|
488
|
-
if (ts.isClassDeclaration(node)) return yield* ClassDeclaration(node);
|
|
489
|
-
if (ts.isConditionalTypeNode(node)) return yield* ConditionalTypeNode(node);
|
|
490
|
-
if (ts.isConstructorTypeNode(node)) return yield* ConstructorTypeNode(node);
|
|
491
|
-
if (ts.isEnumDeclaration(node)) return yield* EnumDeclaration(node);
|
|
492
|
-
if (ts.isExpressionWithTypeArguments(node)) return yield* ExpressionWithTypeArguments(node);
|
|
493
|
-
if (ts.isFunctionDeclaration(node)) return yield* FunctionDeclaration(node);
|
|
494
|
-
if (ts.isFunctionTypeNode(node)) return yield* FunctionTypeNode(node);
|
|
495
|
-
if (ts.isHeritageClause(node)) return yield* HeritageClause(node);
|
|
496
|
-
if (ts.isIndexedAccessTypeNode(node)) return yield* IndexedAccessType(node);
|
|
497
|
-
if (ts.isIndexSignatureDeclaration(node)) return yield* isIndexSignatureDeclaration(node);
|
|
498
|
-
if (ts.isInterfaceDeclaration(node)) return yield* InterfaceDeclaration(node);
|
|
499
|
-
if (ts.isLiteralTypeNode(node)) return yield* LiteralTypeNode(node);
|
|
500
|
-
if (ts.isPropertySignature(node)) return yield* PropertySignature(node);
|
|
501
|
-
if (ts.isModuleDeclaration(node)) return yield* ModuleDeclaration(node);
|
|
502
|
-
if (ts.isIdentifier(node)) return yield node.getText();
|
|
503
|
-
if (ts.isIntersectionTypeNode(node)) return yield* IntersectionTypeNode(node);
|
|
504
|
-
if (ts.isUnionTypeNode(node)) return yield* UnionTypeNode(node);
|
|
505
|
-
if (ts.isMappedTypeNode(node)) return yield* MappedTypeNode(node);
|
|
506
|
-
if (ts.isMethodSignature(node)) return yield* MethodSignature(node);
|
|
507
|
-
if (ts.isModuleBlock(node)) return yield* ModuleBlock(node);
|
|
508
|
-
if (ts.isParameter(node)) return yield* Parameter(node);
|
|
509
|
-
if (ts.isParenthesizedTypeNode(node)) return yield* ParenthesizedTypeNode(node);
|
|
510
|
-
if (ts.isRestTypeNode(node)) return yield* RestTypeNode(node);
|
|
511
|
-
if (ts.isTupleTypeNode(node)) return yield* TupleTypeNode(node);
|
|
512
|
-
if (ts.isTemplateLiteralTypeNode(node)) return yield* TemplateLiteralTypeNode(node);
|
|
513
|
-
if (ts.isTemplateLiteralTypeSpan(node)) return yield* TemplateLiteralTypeSpan(node);
|
|
514
|
-
if (ts.isTemplateHead(node)) return yield* TemplateHead(node);
|
|
515
|
-
if (ts.isTemplateMiddle(node)) return yield* TemplateMiddle(node);
|
|
516
|
-
if (ts.isTemplateTail(node)) return yield* TemplateTail(node);
|
|
517
|
-
if (ts.isThisTypeNode(node)) return yield* ThisTypeNode(node);
|
|
518
|
-
if (ts.isTypeAliasDeclaration(node)) return yield* TypeAliasDeclaration(node);
|
|
519
|
-
if (ts.isTypeLiteralNode(node)) return yield* TypeLiteralNode(node);
|
|
520
|
-
if (ts.isTypeOperatorNode(node)) return yield* TypeOperatorNode(node);
|
|
521
|
-
if (ts.isTypeParameterDeclaration(node)) return yield* TypeParameterDeclaration(node);
|
|
522
|
-
if (ts.isTypeReferenceNode(node)) return yield* TypeReferenceNode(node);
|
|
523
|
-
if (ts.isSourceFile(node)) return yield* SourceFile(node);
|
|
524
|
-
if (node.kind === ts.SyntaxKind.ExportKeyword) return yield `export`;
|
|
525
|
-
if (node.kind === ts.SyntaxKind.KeyOfKeyword) return yield `Type.KeyOf()`;
|
|
526
|
-
if (node.kind === ts.SyntaxKind.NumberKeyword) return yield `Type.Number()`;
|
|
527
|
-
if (node.kind === ts.SyntaxKind.BigIntKeyword) return yield `Type.BigInt()`;
|
|
528
|
-
if (node.kind === ts.SyntaxKind.StringKeyword) return yield `Type.String()`;
|
|
529
|
-
if (node.kind === ts.SyntaxKind.SymbolKeyword) return yield `Type.Symbol()`;
|
|
530
|
-
if (node.kind === ts.SyntaxKind.BooleanKeyword) return yield `Type.Boolean()`;
|
|
531
|
-
if (node.kind === ts.SyntaxKind.UndefinedKeyword) return yield `Type.Undefined()`;
|
|
532
|
-
if (node.kind === ts.SyntaxKind.UnknownKeyword) return yield `Type.Unknown()`;
|
|
533
|
-
if (node.kind === ts.SyntaxKind.AnyKeyword) return yield `Type.Any()`;
|
|
534
|
-
if (node.kind === ts.SyntaxKind.NeverKeyword) return yield `Type.Never()`;
|
|
535
|
-
if (node.kind === ts.SyntaxKind.NullKeyword) return yield `Type.Null()`;
|
|
536
|
-
if (node.kind === ts.SyntaxKind.VoidKeyword) return yield `Type.Void()`;
|
|
537
|
-
if (node.kind === ts.SyntaxKind.EndOfFileToken) return;
|
|
538
|
-
if (node.kind === ts.SyntaxKind.SyntaxList) {
|
|
539
|
-
for (const child of node.getChildren()){
|
|
540
|
-
yield* Visit(child);
|
|
541
|
-
}
|
|
542
|
-
return;
|
|
543
|
-
}
|
|
544
|
-
console.warn('Unhandled:', ts.SyntaxKind[node.kind], node.getText());
|
|
545
|
-
}
|
|
546
|
-
function ImportStatement() {
|
|
547
|
-
if (!(useImports && useTypeBoxImport)) return '';
|
|
548
|
-
const set = new Set([
|
|
549
|
-
'Type',
|
|
550
|
-
'Static'
|
|
551
|
-
]);
|
|
552
|
-
if (useGenerics) {
|
|
553
|
-
set.add('TSchema');
|
|
554
|
-
}
|
|
555
|
-
if (useOptions) {
|
|
556
|
-
set.add('SchemaOptions');
|
|
557
|
-
}
|
|
558
|
-
if (useCloneType) {
|
|
559
|
-
set.add('CloneType');
|
|
560
|
-
}
|
|
561
|
-
const imports = [
|
|
562
|
-
...set
|
|
563
|
-
].join(', ');
|
|
564
|
-
return `import { ${imports} } from '@sinclair/typebox'`;
|
|
565
|
-
}
|
|
566
|
-
function Generate(typescriptCode, options) {
|
|
567
|
-
useExportsEverything = options?.useExportEverything ?? false;
|
|
568
|
-
useIdentifiers = options?.useIdentifiers ?? false;
|
|
569
|
-
useTypeBoxImport = options?.useTypeBoxImport ?? true;
|
|
570
|
-
typenames.clear();
|
|
571
|
-
useImports = false;
|
|
572
|
-
useOptions = false;
|
|
573
|
-
useGenerics = false;
|
|
574
|
-
useCloneType = false;
|
|
575
|
-
blockLevel = 0;
|
|
576
|
-
const source = ts.createSourceFile('types.ts', typescriptCode, ts.ScriptTarget.ESNext, true);
|
|
577
|
-
const declarations = [
|
|
578
|
-
...Visit(source)
|
|
579
|
-
].join('\n\n');
|
|
580
|
-
const imports = ImportStatement();
|
|
581
|
-
const typescript = [
|
|
582
|
-
imports,
|
|
583
|
-
'',
|
|
584
|
-
'',
|
|
585
|
-
declarations
|
|
586
|
-
].join('\n');
|
|
587
|
-
const assertion = ts.transpileModule(typescript, transpilerOptions);
|
|
588
|
-
if (assertion.diagnostics && assertion.diagnostics.length > 0) {
|
|
589
|
-
throw new TypeScriptToTypeBoxError(assertion.diagnostics);
|
|
590
|
-
}
|
|
591
|
-
return typescript;
|
|
592
|
-
}
|
|
593
|
-
TypeScriptToTypeBox.Generate = Generate;
|
|
594
|
-
})(TypeScriptToTypeBox || (TypeScriptToTypeBox = {}));
|
|
595
|
-
|
|
596
|
-
//# sourceMappingURL=typescript-to-typebox.js.map
|