@twin.org/tools-core 0.0.3-next.3 → 0.0.3-next.31
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 +2 -2
- package/dist/es/index.js +21 -11
- package/dist/es/index.js.map +1 -1
- package/dist/es/models/ITypeScriptToSchemaContext.js +2 -0
- package/dist/es/models/ITypeScriptToSchemaContext.js.map +1 -0
- package/dist/es/models/ITypeScriptToSchemaDiagnostics.js +4 -0
- package/dist/es/models/ITypeScriptToSchemaDiagnostics.js.map +1 -0
- package/dist/es/models/ITypeScriptToSchemaOptions.js +4 -0
- package/dist/es/models/ITypeScriptToSchemaOptions.js.map +1 -0
- package/dist/es/models/embeddedSchemaMode.js +17 -0
- package/dist/es/models/embeddedSchemaMode.js.map +1 -0
- package/dist/es/utils/constants.js +43 -0
- package/dist/es/utils/constants.js.map +1 -0
- package/dist/es/utils/diagnosticReporter.js +32 -0
- package/dist/es/utils/diagnosticReporter.js.map +1 -0
- package/dist/es/utils/disallowedTypeGuard.js +151 -0
- package/dist/es/utils/disallowedTypeGuard.js.map +1 -0
- package/dist/es/utils/enum.js +152 -0
- package/dist/es/utils/enum.js.map +1 -0
- package/dist/es/utils/fileUtils.js +132 -0
- package/dist/es/utils/fileUtils.js.map +1 -0
- package/dist/es/utils/importTypeQuerySchemaResolver.js +363 -0
- package/dist/es/utils/importTypeQuerySchemaResolver.js.map +1 -0
- package/dist/es/utils/indexSignaturePatternResolver.js +94 -0
- package/dist/es/utils/indexSignaturePatternResolver.js.map +1 -0
- package/dist/es/utils/intersectionSchemaMerger.js +85 -0
- package/dist/es/utils/intersectionSchemaMerger.js.map +1 -0
- package/dist/es/utils/jsDoc.js +138 -0
- package/dist/es/utils/jsDoc.js.map +1 -0
- package/dist/es/utils/jsonSchemaBuilder.js +3415 -0
- package/dist/es/utils/jsonSchemaBuilder.js.map +1 -0
- package/dist/es/utils/mappedTypeSchemaResolver.js +265 -0
- package/dist/es/utils/mappedTypeSchemaResolver.js.map +1 -0
- package/dist/es/utils/objectTransformer.js +161 -0
- package/dist/es/utils/objectTransformer.js.map +1 -0
- package/dist/es/utils/regEx.js +128 -0
- package/dist/es/utils/regEx.js.map +1 -0
- package/dist/es/utils/resolver.js +177 -0
- package/dist/es/utils/resolver.js.map +1 -0
- package/dist/es/utils/templateLiteralPatternBuilder.js +94 -0
- package/dist/es/utils/templateLiteralPatternBuilder.js.map +1 -0
- package/dist/es/utils/typeScriptToSchema.js +319 -0
- package/dist/es/utils/typeScriptToSchema.js.map +1 -0
- package/dist/es/utils/utilityTypeSchemaMapper.js +475 -0
- package/dist/es/utils/utilityTypeSchemaMapper.js.map +1 -0
- package/dist/types/index.d.ts +21 -11
- package/dist/types/models/ITypeScriptToSchemaContext.d.ts +71 -0
- package/dist/types/models/ITypeScriptToSchemaDiagnostics.d.ts +31 -0
- package/dist/types/models/ITypeScriptToSchemaOptions.d.ts +22 -0
- package/dist/types/models/embeddedSchemaMode.d.ts +17 -0
- package/dist/types/utils/constants.d.ts +13 -0
- package/dist/types/utils/diagnosticReporter.d.ts +17 -0
- package/dist/types/utils/disallowedTypeGuard.d.ts +16 -0
- package/dist/types/utils/enum.d.ts +42 -0
- package/dist/types/utils/fileUtils.d.ts +66 -0
- package/dist/types/utils/importTypeQuerySchemaResolver.d.ts +87 -0
- package/dist/types/utils/indexSignaturePatternResolver.d.ts +21 -0
- package/dist/types/utils/intersectionSchemaMerger.d.ts +16 -0
- package/dist/types/utils/jsDoc.d.ts +53 -0
- package/dist/types/utils/jsonSchemaBuilder.d.ts +671 -0
- package/dist/types/utils/mappedTypeSchemaResolver.d.ts +81 -0
- package/dist/types/utils/objectTransformer.d.ts +33 -0
- package/dist/types/utils/regEx.d.ts +24 -0
- package/dist/types/utils/resolver.d.ts +22 -0
- package/dist/types/utils/templateLiteralPatternBuilder.d.ts +12 -0
- package/dist/types/utils/typeScriptToSchema.d.ts +92 -0
- package/dist/types/utils/utilityTypeSchemaMapper.d.ts +141 -0
- package/docs/changelog.md +365 -34
- package/docs/examples.md +87 -1
- package/docs/reference/classes/Constants.md +29 -0
- package/docs/reference/classes/DiagnosticReporter.md +49 -0
- package/docs/reference/classes/DisallowedTypeGuard.md +35 -0
- package/docs/reference/classes/Enum.md +93 -0
- package/docs/reference/classes/FileUtils.md +237 -0
- package/docs/reference/classes/ImportTypeQuerySchemaResolver.md +109 -0
- package/docs/reference/classes/IndexSignaturePatternResolver.md +69 -0
- package/docs/reference/classes/IntersectionSchemaMerger.md +48 -0
- package/docs/reference/classes/JsDoc.md +169 -0
- package/docs/reference/classes/JsonSchemaBuilder.md +2578 -0
- package/docs/reference/classes/MappedTypeSchemaResolver.md +275 -0
- package/docs/reference/classes/ObjectTransformer.md +119 -0
- package/docs/reference/classes/RegEx.md +99 -0
- package/docs/reference/classes/Resolver.md +52 -0
- package/docs/reference/classes/TemplateLiteralPatternBuilder.md +35 -0
- package/docs/reference/classes/TypeScriptToSchema.md +91 -0
- package/docs/reference/classes/UtilityTypeSchemaMapper.md +343 -0
- package/docs/reference/index.md +25 -11
- package/docs/reference/interfaces/ITypeScriptToSchemaContext.md +125 -0
- package/docs/reference/interfaces/ITypeScriptToSchemaDiagnostics.md +55 -0
- package/docs/reference/interfaces/ITypeScriptToSchemaOptions.md +44 -0
- package/docs/reference/type-aliases/EmbeddedSchemaMode.md +5 -0
- package/docs/reference/variables/EmbeddedSchemaMode.md +19 -0
- package/locales/en.json +32 -1
- package/package.json +6 -5
- package/dist/es/models/IJsonSchema.js +0 -2
- package/dist/es/models/IJsonSchema.js.map +0 -1
- package/dist/es/models/IOpenApi.js +0 -2
- package/dist/es/models/IOpenApi.js.map +0 -1
- package/dist/es/models/IOpenApiExample.js +0 -4
- package/dist/es/models/IOpenApiExample.js.map +0 -1
- package/dist/es/models/IOpenApiHeader.js +0 -4
- package/dist/es/models/IOpenApiHeader.js.map +0 -1
- package/dist/es/models/IOpenApiPathMethod.js +0 -2
- package/dist/es/models/IOpenApiPathMethod.js.map +0 -1
- package/dist/es/models/IOpenApiResponse.js +0 -2
- package/dist/es/models/IOpenApiResponse.js.map +0 -1
- package/dist/es/models/IOpenApiSecurityScheme.js +0 -4
- package/dist/es/models/IOpenApiSecurityScheme.js.map +0 -1
- package/dist/es/models/IPackageJson.js +0 -4
- package/dist/es/models/IPackageJson.js.map +0 -1
- package/dist/es/models/jsonTypeName.js +0 -2
- package/dist/es/models/jsonTypeName.js.map +0 -1
- package/dist/es/utils/jsonSchemaHelper.js +0 -258
- package/dist/es/utils/jsonSchemaHelper.js.map +0 -1
- package/dist/es/utils/openApiHelper.js +0 -12
- package/dist/es/utils/openApiHelper.js.map +0 -1
- package/dist/types/models/IJsonSchema.d.ts +0 -5
- package/dist/types/models/IOpenApi.d.ts +0 -54
- package/dist/types/models/IOpenApiExample.d.ts +0 -13
- package/dist/types/models/IOpenApiHeader.d.ts +0 -19
- package/dist/types/models/IOpenApiPathMethod.d.ts +0 -65
- package/dist/types/models/IOpenApiResponse.d.ts +0 -32
- package/dist/types/models/IOpenApiSecurityScheme.d.ts +0 -25
- package/dist/types/models/IPackageJson.d.ts +0 -15
- package/dist/types/models/jsonTypeName.d.ts +0 -5
- package/dist/types/utils/jsonSchemaHelper.d.ts +0 -78
- package/dist/types/utils/openApiHelper.d.ts +0 -9
- package/docs/reference/classes/JsonSchemaHelper.md +0 -233
- package/docs/reference/classes/OpenApiHelper.md +0 -21
- package/docs/reference/interfaces/IOpenApi.md +0 -103
- package/docs/reference/interfaces/IOpenApiExample.md +0 -19
- package/docs/reference/interfaces/IOpenApiHeader.md +0 -31
- package/docs/reference/interfaces/IOpenApiPathMethod.md +0 -119
- package/docs/reference/interfaces/IOpenApiResponse.md +0 -35
- package/docs/reference/interfaces/IOpenApiSecurityScheme.md +0 -43
- package/docs/reference/interfaces/IPackageJson.md +0 -23
- package/docs/reference/type-aliases/IJsonSchema.md +0 -5
- package/docs/reference/type-aliases/JsonTypeName.md +0 -5
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { IJsonSchema } from "@twin.org/tools-models";
|
|
2
|
+
import type * as ts from "typescript";
|
|
3
|
+
import type { EmbeddedSchemaMode } from "./embeddedSchemaMode.js";
|
|
4
|
+
import type { ITypeScriptToSchemaOptions } from "./ITypeScriptToSchemaOptions.js";
|
|
5
|
+
/**
|
|
6
|
+
* Context for TypeScript to JSON schema generation.
|
|
7
|
+
*/
|
|
8
|
+
export interface ITypeScriptToSchemaContext {
|
|
9
|
+
/**
|
|
10
|
+
* The namespace for generated schema ids.
|
|
11
|
+
*/
|
|
12
|
+
namespace: string;
|
|
13
|
+
/**
|
|
14
|
+
* The package name for generated schema ids.
|
|
15
|
+
*/
|
|
16
|
+
packageName: string;
|
|
17
|
+
/**
|
|
18
|
+
* The schema cache indexed by package and title.
|
|
19
|
+
*/
|
|
20
|
+
schemas: {
|
|
21
|
+
[id: string]: {
|
|
22
|
+
[id: string]: IJsonSchema;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* The currently active source file being mapped.
|
|
27
|
+
*/
|
|
28
|
+
activeSourceFile?: ts.SourceFile;
|
|
29
|
+
/**
|
|
30
|
+
* The currently active enclosing object (interface/type) being mapped.
|
|
31
|
+
*/
|
|
32
|
+
activeEnclosingObjectName?: string;
|
|
33
|
+
/**
|
|
34
|
+
* The first disallowed type encountered while mapping the active enclosing object.
|
|
35
|
+
*/
|
|
36
|
+
activeDisallowedType?: {
|
|
37
|
+
disallowedTypeName: string;
|
|
38
|
+
propertyName: string;
|
|
39
|
+
enclosingObjectName: string;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Type names currently being resolved to avoid recursive local-type expansion loops.
|
|
43
|
+
*/
|
|
44
|
+
resolvingTypeNames?: Set<string>;
|
|
45
|
+
/**
|
|
46
|
+
* Utility type names currently being processed to avoid infinite recursion cycles
|
|
47
|
+
* when encountering complex combinations of indexed access types, keyof, and utility types.
|
|
48
|
+
*/
|
|
49
|
+
resolvingUtilityTypes?: Set<string>;
|
|
50
|
+
/**
|
|
51
|
+
* Imported utility base schemas currently being resolved to avoid recursive re-entry
|
|
52
|
+
* while parsing module graphs for utility type application.
|
|
53
|
+
*/
|
|
54
|
+
resolvingImportedObjectSchemas?: Set<string>;
|
|
55
|
+
/**
|
|
56
|
+
* Generic type parameter bindings active for the current mapping scope.
|
|
57
|
+
*/
|
|
58
|
+
typeParameterBindings?: {
|
|
59
|
+
[id: string]: ts.TypeNode | null;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Schema ids marked with @json-schema embedded and their embedding mode.
|
|
63
|
+
*/
|
|
64
|
+
embeddedSchemaModes?: {
|
|
65
|
+
[id: string]: EmbeddedSchemaMode;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Optional schema generation options.
|
|
69
|
+
*/
|
|
70
|
+
options?: ITypeScriptToSchemaOptions;
|
|
71
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Diagnostic payload for non-fatal schema generation issues.
|
|
3
|
+
*/
|
|
4
|
+
export interface ITypeScriptToSchemaDiagnostics {
|
|
5
|
+
/**
|
|
6
|
+
* Stable diagnostic code identifying the issue type.
|
|
7
|
+
*/
|
|
8
|
+
code: string;
|
|
9
|
+
/**
|
|
10
|
+
* Additional structured metadata related to the diagnostic.
|
|
11
|
+
*/
|
|
12
|
+
properties?: {
|
|
13
|
+
[key: string]: unknown;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Schema path where the issue was detected.
|
|
17
|
+
*/
|
|
18
|
+
path: string;
|
|
19
|
+
/**
|
|
20
|
+
* Source file where the diagnostic originated, if available.
|
|
21
|
+
*/
|
|
22
|
+
fileName?: string;
|
|
23
|
+
/**
|
|
24
|
+
* One-based source line number, when available.
|
|
25
|
+
*/
|
|
26
|
+
line?: number;
|
|
27
|
+
/**
|
|
28
|
+
* One-based source column number, when available.
|
|
29
|
+
*/
|
|
30
|
+
column?: number;
|
|
31
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ITypeScriptToSchemaDiagnostics } from "./ITypeScriptToSchemaDiagnostics.js";
|
|
2
|
+
/**
|
|
3
|
+
* Options for TypeScript to JSON schema generation.
|
|
4
|
+
*/
|
|
5
|
+
export interface ITypeScriptToSchemaOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Mapping of package ids, type ids, wildcard patterns, or regex patterns to schema id prefixes
|
|
8
|
+
* or replacement templates for referenced schemas.
|
|
9
|
+
*/
|
|
10
|
+
externalReferences?: {
|
|
11
|
+
[id: string]: string;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Optional diagnostic callback for non-fatal generation issues.
|
|
15
|
+
* @param diagnostic The diagnostic details for the generation issue.
|
|
16
|
+
*/
|
|
17
|
+
onDiagnostic?: (diagnostic: ITypeScriptToSchemaDiagnostics) => void;
|
|
18
|
+
/**
|
|
19
|
+
* Package names where diagnostics should be suppressed, e.g. jose.
|
|
20
|
+
*/
|
|
21
|
+
suppressPackageWarnings?: string[];
|
|
22
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The available embedded schema modes.
|
|
3
|
+
*/
|
|
4
|
+
export declare const EmbeddedSchemaMode: {
|
|
5
|
+
/**
|
|
6
|
+
* Referenced schemas are added to the "defs" section of the root schema.
|
|
7
|
+
*/
|
|
8
|
+
readonly Defs: "defs";
|
|
9
|
+
/**
|
|
10
|
+
* Referenced schemas are inlined into the referencing schema.
|
|
11
|
+
*/
|
|
12
|
+
readonly Inline: "inline";
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* The embedded schema mode value type.
|
|
16
|
+
*/
|
|
17
|
+
export type EmbeddedSchemaMode = (typeof EmbeddedSchemaMode)[keyof typeof EmbeddedSchemaMode];
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared constants for TypeScript to JSON schema generation.
|
|
3
|
+
*/
|
|
4
|
+
export declare class Constants {
|
|
5
|
+
/**
|
|
6
|
+
* Utility type names currently unsupported and mapped to open schemas with diagnostics.
|
|
7
|
+
*/
|
|
8
|
+
static readonly UNSUPPORTED_UTILITY_TYPE_NAMES: string[];
|
|
9
|
+
/**
|
|
10
|
+
* Native typed-array and binary type names mapped to JSON number arrays.
|
|
11
|
+
*/
|
|
12
|
+
static readonly ARRAY_NUMBER_TYPE_NAMES: string[];
|
|
13
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type * as ts from "typescript";
|
|
2
|
+
import type { ITypeScriptToSchemaContext } from "../models/ITypeScriptToSchemaContext.js";
|
|
3
|
+
/**
|
|
4
|
+
* Emits non-fatal diagnostics during schema mapping.
|
|
5
|
+
*/
|
|
6
|
+
export declare class DiagnosticReporter {
|
|
7
|
+
/**
|
|
8
|
+
* Emit an optional non-fatal schema generation diagnostic.
|
|
9
|
+
* @param context The generation context.
|
|
10
|
+
* @param node The related AST node.
|
|
11
|
+
* @param code The diagnostic code.
|
|
12
|
+
* @param properties The values to substitute into the localised message.
|
|
13
|
+
*/
|
|
14
|
+
static report(context: ITypeScriptToSchemaContext, node: ts.Node, code: string, properties?: {
|
|
15
|
+
[key: string]: unknown;
|
|
16
|
+
}): void;
|
|
17
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
/**
|
|
3
|
+
* Validates whether a type node is allowed for schema generation.
|
|
4
|
+
*/
|
|
5
|
+
export declare class DisallowedTypeGuard {
|
|
6
|
+
/**
|
|
7
|
+
* Type names that are not supported for JSON schema generation.
|
|
8
|
+
*/
|
|
9
|
+
private static readonly _DISALLOWED_TYPE_NAMES;
|
|
10
|
+
/**
|
|
11
|
+
* Resolve a disallowed type name from a type node.
|
|
12
|
+
* @param typeNode The type node to inspect.
|
|
13
|
+
* @returns The disallowed type name, if found.
|
|
14
|
+
*/
|
|
15
|
+
static getDisallowedTypeName(typeNode: ts.TypeNode): string | undefined;
|
|
16
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
/**
|
|
3
|
+
* Utility methods for extracting enum values from TypeScript AST nodes.
|
|
4
|
+
*
|
|
5
|
+
* Handles three distinct patterns used to define enumerable sets of values:
|
|
6
|
+
* - Native enum declarations: `enum Color { Red = "red" }`
|
|
7
|
+
* - Const-object-with-matching-type patterns: `const Foo = { A: "a" } as const` paired with a type alias named identically to the const
|
|
8
|
+
*/
|
|
9
|
+
export declare class Enum {
|
|
10
|
+
/**
|
|
11
|
+
* Extract enum values from a const-and-type pair where a const object and a type alias share the
|
|
12
|
+
* same name. The const object is located first because its members may carry JSDoc descriptions
|
|
13
|
+
* that would be lost if the type alias were processed in isolation.
|
|
14
|
+
* @param name The name to search for.
|
|
15
|
+
* @param sourceFile The source file to search.
|
|
16
|
+
* @returns The extracted enum entries or undefined when the pattern is not present.
|
|
17
|
+
*/
|
|
18
|
+
static extractEnumValuesFromConstAndType(name: string, sourceFile: ts.SourceFile): {
|
|
19
|
+
value: string | number;
|
|
20
|
+
description?: string;
|
|
21
|
+
}[] | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Extract enum entries from a const object literal.
|
|
24
|
+
* Only properties whose initializer is a string or numeric literal are included.
|
|
25
|
+
* @param objLiteral The object literal expression.
|
|
26
|
+
* @returns The extracted enum entries.
|
|
27
|
+
*/
|
|
28
|
+
static extractEnumEntriesFromConstObject(objLiteral: ts.ObjectLiteralExpression): {
|
|
29
|
+
value: string | number;
|
|
30
|
+
description?: string;
|
|
31
|
+
}[];
|
|
32
|
+
/**
|
|
33
|
+
* Extract enum values from a native TypeScript enum declaration.
|
|
34
|
+
* Numeric members without an explicit initializer are auto-incremented from the previous value.
|
|
35
|
+
* @param declaration The enum declaration.
|
|
36
|
+
* @returns The extracted enum entries.
|
|
37
|
+
*/
|
|
38
|
+
static extractEnumValuesFromEnumDeclaration(declaration: ts.EnumDeclaration): {
|
|
39
|
+
value: string | number;
|
|
40
|
+
description?: string;
|
|
41
|
+
}[];
|
|
42
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility helpers for TypeScript file and directory paths.
|
|
3
|
+
*/
|
|
4
|
+
export declare class FileUtils {
|
|
5
|
+
/**
|
|
6
|
+
* Does the file exist.
|
|
7
|
+
* @param filePath The file path.
|
|
8
|
+
* @returns True if the file exists.
|
|
9
|
+
*/
|
|
10
|
+
static fileExists(filePath: string): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Read the file.
|
|
13
|
+
* @param filePath The file path.
|
|
14
|
+
* @returns The file contents.
|
|
15
|
+
*/
|
|
16
|
+
static readFile(filePath: string): string;
|
|
17
|
+
/**
|
|
18
|
+
* Resolve a path.
|
|
19
|
+
* @param filePath The file path.
|
|
20
|
+
* @returns The resolved path.
|
|
21
|
+
*/
|
|
22
|
+
static resolvePath(filePath: string): string;
|
|
23
|
+
/**
|
|
24
|
+
* Get the current working directory.
|
|
25
|
+
* @returns The current working directory.
|
|
26
|
+
*/
|
|
27
|
+
static getCurrentWorkingDirectory(): string;
|
|
28
|
+
/**
|
|
29
|
+
* Normalize path separators for consistent comparisons.
|
|
30
|
+
* @param filePath The file path.
|
|
31
|
+
* @returns The normalized file path.
|
|
32
|
+
*/
|
|
33
|
+
static normalizeFilePath(filePath: string): string;
|
|
34
|
+
/**
|
|
35
|
+
* Get the directory portion of a file path.
|
|
36
|
+
* @param filePath The file path.
|
|
37
|
+
* @returns The directory path.
|
|
38
|
+
*/
|
|
39
|
+
static getDirectoryPath(filePath: string): string;
|
|
40
|
+
/**
|
|
41
|
+
* Resolve a relative path against a base directory.
|
|
42
|
+
* @param baseDirectory The base directory.
|
|
43
|
+
* @param relativePath The relative path.
|
|
44
|
+
* @returns The resolved path.
|
|
45
|
+
*/
|
|
46
|
+
static resolveRelativePath(baseDirectory: string, relativePath: string): string;
|
|
47
|
+
/**
|
|
48
|
+
* Resolve a local import specifier to a TypeScript source file path.
|
|
49
|
+
* @param sourceFilePath The importing source file path.
|
|
50
|
+
* @param importPath The import specifier.
|
|
51
|
+
* @returns The resolved source file path.
|
|
52
|
+
*/
|
|
53
|
+
static resolveImportSourceFilePath(sourceFilePath: string, importPath: string): string | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Determine if the provided path includes glob pattern tokens.
|
|
56
|
+
* @param sourceFileOrGlob The direct source file path or glob pattern.
|
|
57
|
+
* @returns True if the value is a glob pattern.
|
|
58
|
+
*/
|
|
59
|
+
static isGlobPattern(sourceFileOrGlob: string): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Resolve source files from a direct path or a glob pattern.
|
|
62
|
+
* @param sourceFileOrGlob The direct source file path or glob pattern.
|
|
63
|
+
* @returns The resolved source file paths.
|
|
64
|
+
*/
|
|
65
|
+
static resolveSourceFiles(sourceFileOrGlob: string): string[];
|
|
66
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { IJsonSchema } from "@twin.org/tools-models";
|
|
2
|
+
import * as ts from "typescript";
|
|
3
|
+
import type { ITypeScriptToSchemaContext } from "../models/ITypeScriptToSchemaContext.js";
|
|
4
|
+
/**
|
|
5
|
+
* Static helpers for import type and type query schema resolution.
|
|
6
|
+
*/
|
|
7
|
+
export declare class ImportTypeQuerySchemaResolver {
|
|
8
|
+
/**
|
|
9
|
+
* Map import type nodes (e.g. import("pkg").Type) to schema references.
|
|
10
|
+
* @param context The generation context.
|
|
11
|
+
* @param typeNode The import type node.
|
|
12
|
+
* @returns The mapped schema.
|
|
13
|
+
*/
|
|
14
|
+
static mapImportTypeNodeToSchema(context: ITypeScriptToSchemaContext, typeNode: ts.ImportTypeNode): IJsonSchema | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* Map a type query node (typeof expr) to schema by resolving the referenced variable.
|
|
17
|
+
* @param context The generation context.
|
|
18
|
+
* @param typeNode The type query node.
|
|
19
|
+
* @returns The mapped schema.
|
|
20
|
+
*/
|
|
21
|
+
static mapTypeQueryNodeToSchema(context: ITypeScriptToSchemaContext, typeNode: ts.TypeQueryNode): IJsonSchema;
|
|
22
|
+
/**
|
|
23
|
+
* Resolve import-type references to local or external schema ids.
|
|
24
|
+
* @param context The generation context.
|
|
25
|
+
* @param moduleSpecifier The import module specifier.
|
|
26
|
+
* @param typeName The imported type name.
|
|
27
|
+
* @param title The stripped schema title.
|
|
28
|
+
* @returns The resolved schema id.
|
|
29
|
+
*/
|
|
30
|
+
static resolveImportTypeReferenceSchemaId(context: ITypeScriptToSchemaContext, moduleSpecifier: string, typeName: string, title: string): string | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Resolve a property value from a const object declaration in an imported source file.
|
|
33
|
+
* @param context The generation context.
|
|
34
|
+
* @param objectName The imported object symbol name.
|
|
35
|
+
* @param propertyName The property name to resolve from the object.
|
|
36
|
+
* @returns The resolved literal property value.
|
|
37
|
+
*/
|
|
38
|
+
private static resolveConstObjectProperty;
|
|
39
|
+
/**
|
|
40
|
+
* Find a variable declaration by traversing import and export chains.
|
|
41
|
+
* @param sourceFilePath The source file path to inspect.
|
|
42
|
+
* @param variableName The variable name to find.
|
|
43
|
+
* @param visitedFiles The set of visited files to prevent recursion cycles.
|
|
44
|
+
* @returns The matched variable declaration.
|
|
45
|
+
*/
|
|
46
|
+
private static findVariableDeclarationInModuleGraph;
|
|
47
|
+
/**
|
|
48
|
+
* Find an imported symbol reference by local identifier name.
|
|
49
|
+
* @param sourceFile The active source file.
|
|
50
|
+
* @param localName The local identifier name.
|
|
51
|
+
* @returns The imported symbol reference.
|
|
52
|
+
*/
|
|
53
|
+
private static findImportedValueReference;
|
|
54
|
+
/**
|
|
55
|
+
* Resolve an import declaration module specifier to a source file.
|
|
56
|
+
* @param containingSourceFilePath The path of the file containing the import declaration.
|
|
57
|
+
* @param moduleSpecifier The module specifier to resolve.
|
|
58
|
+
* @returns The resolved source file path.
|
|
59
|
+
*/
|
|
60
|
+
private static resolveImportDeclarationSourceFile;
|
|
61
|
+
/**
|
|
62
|
+
* Extract a const-object property value from a declaration initializer.
|
|
63
|
+
* @param objectDeclaration The variable declaration containing the object initializer.
|
|
64
|
+
* @param propertyName The property name to resolve.
|
|
65
|
+
* @returns The extracted literal property value.
|
|
66
|
+
*/
|
|
67
|
+
private static extractConstObjectPropertyFromDeclarationInitializer;
|
|
68
|
+
/**
|
|
69
|
+
* Extract a const-object property value from a declaration type annotation.
|
|
70
|
+
* @param declarationTypeNode The declaration type node to inspect.
|
|
71
|
+
* @param propertyName The property name to resolve.
|
|
72
|
+
* @returns The extracted literal property value.
|
|
73
|
+
*/
|
|
74
|
+
private static extractConstObjectPropertyFromDeclarationType;
|
|
75
|
+
/**
|
|
76
|
+
* Extract a literal value from a type node when possible.
|
|
77
|
+
* @param typeNode The type node to inspect.
|
|
78
|
+
* @returns The extracted literal value.
|
|
79
|
+
*/
|
|
80
|
+
private static extractLiteralValueFromTypeNode;
|
|
81
|
+
/**
|
|
82
|
+
* Extract a referenced type name from an import type qualifier.
|
|
83
|
+
* @param qualifier The import type qualifier.
|
|
84
|
+
* @returns The extracted type name.
|
|
85
|
+
*/
|
|
86
|
+
private static extractImportTypeName;
|
|
87
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
import type { ITypeScriptToSchemaContext } from "../models/ITypeScriptToSchemaContext.js";
|
|
3
|
+
/**
|
|
4
|
+
* Resolves regex patterns for index signature key types.
|
|
5
|
+
*/
|
|
6
|
+
export declare class IndexSignaturePatternResolver {
|
|
7
|
+
/**
|
|
8
|
+
* Determine whether an index signature can be represented as JSON object additionalProperties.
|
|
9
|
+
* @param member The index signature declaration.
|
|
10
|
+
* @returns True if supported.
|
|
11
|
+
*/
|
|
12
|
+
static isSupportedIndexSignature(member: ts.IndexSignatureDeclaration): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Extract a regex pattern for an index signature key type when representable.
|
|
15
|
+
* @param context The generation context.
|
|
16
|
+
* @param member The index signature declaration.
|
|
17
|
+
* @param getTypeParameterBinding Callback for resolving generic bindings.
|
|
18
|
+
* @returns The property-name pattern, if derivable.
|
|
19
|
+
*/
|
|
20
|
+
static extractIndexSignaturePattern(context: ITypeScriptToSchemaContext, member: ts.IndexSignatureDeclaration, getTypeParameterBinding: (context: ITypeScriptToSchemaContext, typeName: string) => ts.TypeNode | null | undefined): string | undefined;
|
|
21
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { IJsonSchema } from "@twin.org/tools-models";
|
|
2
|
+
import type { ITypeScriptToSchemaContext } from "../models/ITypeScriptToSchemaContext.js";
|
|
3
|
+
/**
|
|
4
|
+
* Merges compatible object intersections into a single JSON schema object.
|
|
5
|
+
*/
|
|
6
|
+
export declare class IntersectionSchemaMerger {
|
|
7
|
+
/**
|
|
8
|
+
* Merge simple object intersection parts into a single object schema.
|
|
9
|
+
* Supports local/known object refs by expanding them before merge.
|
|
10
|
+
* @param context The generation context.
|
|
11
|
+
* @param schemas The mapped intersection schemas.
|
|
12
|
+
* @param toInlineUtilityObjectSchema Callback for converting referenced schemas to inline forms.
|
|
13
|
+
* @returns The merged schema, or undefined when merge is not safe.
|
|
14
|
+
*/
|
|
15
|
+
static mergeIntersectionObjectSchemas(context: ITypeScriptToSchemaContext, schemas: IJsonSchema[], toInlineUtilityObjectSchema: (schema: IJsonSchema) => IJsonSchema): IJsonSchema | undefined;
|
|
16
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
/**
|
|
3
|
+
* General-purpose utility methods for working with JsDoc.
|
|
4
|
+
*/
|
|
5
|
+
export declare class JsDoc {
|
|
6
|
+
/**
|
|
7
|
+
* Extract the JSDoc description comment for an AST node.
|
|
8
|
+
* Only top-level JSDoc block comments are considered; inline tags are ignored.
|
|
9
|
+
* @param node The node to inspect.
|
|
10
|
+
* @returns The trimmed description text, or undefined when absent.
|
|
11
|
+
*/
|
|
12
|
+
static getNodeJsDocDescription(node: ts.Node): string | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* Convert a JSDoc tag's comment portion to a plain text string.
|
|
15
|
+
* JSDoc tag comments may be plain strings or arrays of link/text parts.
|
|
16
|
+
* @param jsDocTag The JSDoc tag.
|
|
17
|
+
* @returns The comment text, or undefined when absent.
|
|
18
|
+
*/
|
|
19
|
+
static getJSDocTagCommentText(jsDocTag: ts.JSDocTag): string | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Read all custom JSDoc tags matching a tag name from a node and convert them to key/value pairs.
|
|
22
|
+
* Each matching tag's comment must be in the form "key: value"; entries that do not follow this
|
|
23
|
+
* convention are silently skipped.
|
|
24
|
+
* @param node The node to inspect.
|
|
25
|
+
* @param tagName The tag name to filter by (e.g., 'json-schema').
|
|
26
|
+
* @returns The extracted key/value pairs.
|
|
27
|
+
*/
|
|
28
|
+
static getNodeTags(node: ts.Node, tagName: string): {
|
|
29
|
+
[id: string]: string;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Read the plain comment text for the first matching JSDoc tag on a node.
|
|
33
|
+
* @param node The node to inspect.
|
|
34
|
+
* @param tagName The tag name to filter by (e.g., 'default').
|
|
35
|
+
* @returns The trimmed comment text, or undefined when absent.
|
|
36
|
+
*/
|
|
37
|
+
static getNodeTagComment(node: ts.Node, tagName: string): string | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* Read the plain comment text for all matching JSDoc tags on a node.
|
|
40
|
+
* @param node The node to inspect.
|
|
41
|
+
* @param tagName The tag name to filter by (e.g., 'example').
|
|
42
|
+
* @returns An array of trimmed comment texts for all matching tags.
|
|
43
|
+
*/
|
|
44
|
+
static getNodeTagComments(node: ts.Node, tagName: string): string[];
|
|
45
|
+
/**
|
|
46
|
+
* Parse a custom JSDoc tag value into JSON-compatible data.
|
|
47
|
+
* Values that begin with a JSON token character (open brace, open bracket, double quote, true, false, null) or look like a
|
|
48
|
+
* number are parsed with JSON.parse. All other values are returned as plain strings.
|
|
49
|
+
* @param value The raw value text.
|
|
50
|
+
* @returns The parsed value.
|
|
51
|
+
*/
|
|
52
|
+
static parseTagValue(value: string): unknown;
|
|
53
|
+
}
|