@player-lang/functional-dsl-generator 0.0.2-next.0
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/cjs/index.cjs +2146 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/index.legacy-esm.js +2075 -0
- package/dist/index.mjs +2075 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +38 -0
- package/src/__tests__/__snapshots__/generator.test.ts.snap +886 -0
- package/src/__tests__/builder-class-generator.test.ts +627 -0
- package/src/__tests__/cli.test.ts +685 -0
- package/src/__tests__/default-value-generator.test.ts +365 -0
- package/src/__tests__/generator.test.ts +2860 -0
- package/src/__tests__/import-generator.test.ts +444 -0
- package/src/__tests__/path-utils.test.ts +174 -0
- package/src/__tests__/type-collector.test.ts +674 -0
- package/src/__tests__/type-transformer.test.ts +934 -0
- package/src/__tests__/utils.test.ts +597 -0
- package/src/builder-class-generator.ts +254 -0
- package/src/cli.ts +285 -0
- package/src/default-value-generator.ts +307 -0
- package/src/generator.ts +257 -0
- package/src/import-generator.ts +331 -0
- package/src/index.ts +38 -0
- package/src/path-utils.ts +155 -0
- package/src/ts-morph-type-finder.ts +319 -0
- package/src/type-categorizer.ts +131 -0
- package/src/type-collector.ts +296 -0
- package/src/type-resolver.ts +266 -0
- package/src/type-transformer.ts +487 -0
- package/src/utils.ts +762 -0
- package/types/builder-class-generator.d.ts +56 -0
- package/types/cli.d.ts +6 -0
- package/types/default-value-generator.d.ts +74 -0
- package/types/generator.d.ts +102 -0
- package/types/import-generator.d.ts +77 -0
- package/types/index.d.ts +12 -0
- package/types/path-utils.d.ts +65 -0
- package/types/ts-morph-type-finder.d.ts +73 -0
- package/types/type-categorizer.d.ts +46 -0
- package/types/type-collector.d.ts +62 -0
- package/types/type-resolver.d.ts +49 -0
- package/types/type-transformer.d.ts +74 -0
- package/types/utils.d.ts +205 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { ObjectType } from "@xlr-lib/xlr";
|
|
2
|
+
import { type TypeRegistry } from "./utils";
|
|
3
|
+
import type { TypeTransformer } from "./type-transformer";
|
|
4
|
+
/**
|
|
5
|
+
* Information about a builder class to generate.
|
|
6
|
+
*/
|
|
7
|
+
export interface BuilderInfo {
|
|
8
|
+
/** Original type name from XLR */
|
|
9
|
+
name: string;
|
|
10
|
+
/** Generated class name (e.g., "TextAssetBuilder") */
|
|
11
|
+
className: string;
|
|
12
|
+
/** Factory function name (e.g., "text") */
|
|
13
|
+
factoryName: string;
|
|
14
|
+
/** The XLR ObjectType being generated */
|
|
15
|
+
objectType: ObjectType;
|
|
16
|
+
/** Asset type string if this is an Asset (e.g., "text") */
|
|
17
|
+
assetType?: string;
|
|
18
|
+
/** Generic parameters declaration if type is generic */
|
|
19
|
+
genericParams?: string;
|
|
20
|
+
/** Whether this type extends Asset */
|
|
21
|
+
isAsset: boolean;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Generates builder class code from BuilderInfo.
|
|
25
|
+
*/
|
|
26
|
+
export declare class BuilderClassGenerator {
|
|
27
|
+
private readonly typeTransformer;
|
|
28
|
+
private readonly defaultValueGenerator;
|
|
29
|
+
private readonly typeRegistry;
|
|
30
|
+
/** Track array properties for __arrayProperties__ */
|
|
31
|
+
private arrayProperties;
|
|
32
|
+
/** Track asset wrapper paths for __assetWrapperPaths__ */
|
|
33
|
+
private assetWrapperPaths;
|
|
34
|
+
constructor(typeTransformer: TypeTransformer, typeRegistry?: TypeRegistry);
|
|
35
|
+
/**
|
|
36
|
+
* Generate the builder class code.
|
|
37
|
+
*/
|
|
38
|
+
generateBuilderClass(info: BuilderInfo): string;
|
|
39
|
+
/**
|
|
40
|
+
* Generate interface methods for the builder.
|
|
41
|
+
*/
|
|
42
|
+
private generateInterfaceMethods;
|
|
43
|
+
/**
|
|
44
|
+
* Collect properties that need methods generated.
|
|
45
|
+
*/
|
|
46
|
+
private collectPropertiesForMethods;
|
|
47
|
+
/**
|
|
48
|
+
* Generate class methods for the builder.
|
|
49
|
+
*/
|
|
50
|
+
private generateClassMethods;
|
|
51
|
+
/**
|
|
52
|
+
* Generate default values object using DefaultValueGenerator.
|
|
53
|
+
*/
|
|
54
|
+
private generateDefaults;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=builder-class-generator.d.ts.map
|
package/types/cli.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { ObjectType } from "@xlr-lib/xlr";
|
|
2
|
+
/**
|
|
3
|
+
* Configuration for default value generation
|
|
4
|
+
*/
|
|
5
|
+
export interface DefaultValueConfig {
|
|
6
|
+
/**
|
|
7
|
+
* Maximum depth for recursive object defaults.
|
|
8
|
+
* Prevents infinite recursion for deeply nested or circular types.
|
|
9
|
+
*
|
|
10
|
+
* The depth counter starts at 0 for the root object and increments
|
|
11
|
+
* for each nested object level. When depth equals maxDepth, nested
|
|
12
|
+
* objects are returned as empty `{}` instead of recursing further.
|
|
13
|
+
*
|
|
14
|
+
* Example with maxDepth=3:
|
|
15
|
+
* - Root object (depth 0): full defaults generated
|
|
16
|
+
* - level1.nested (depth 1): full defaults generated
|
|
17
|
+
* - level1.nested.child (depth 2): full defaults generated
|
|
18
|
+
* - level1.nested.child.deep (depth 3): returns {} (depth limit reached)
|
|
19
|
+
*
|
|
20
|
+
* Default: 3
|
|
21
|
+
*/
|
|
22
|
+
maxDepth?: number;
|
|
23
|
+
/**
|
|
24
|
+
* Type names to skip (user must provide these values).
|
|
25
|
+
* Typically includes "Asset" and "AssetWrapper".
|
|
26
|
+
*/
|
|
27
|
+
skipTypes?: Set<string>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Generates smart default values for builder classes.
|
|
31
|
+
*
|
|
32
|
+
* This generator creates sensible defaults for required fields:
|
|
33
|
+
* - String → ""
|
|
34
|
+
* - Number → 0
|
|
35
|
+
* - Boolean → false
|
|
36
|
+
* - Array → []
|
|
37
|
+
* - Object → {} or recursive defaults for required properties
|
|
38
|
+
* - Expression/Binding → ""
|
|
39
|
+
* - Union types → uses the first non-null/undefined variant
|
|
40
|
+
* - AssetWrapper → SKIPPED (user must provide)
|
|
41
|
+
*/
|
|
42
|
+
export declare class DefaultValueGenerator {
|
|
43
|
+
private readonly config;
|
|
44
|
+
constructor(config?: DefaultValueConfig);
|
|
45
|
+
/**
|
|
46
|
+
* Generate default values for an ObjectType.
|
|
47
|
+
*
|
|
48
|
+
* @param objectType - The XLR ObjectType to generate defaults for
|
|
49
|
+
* @param assetType - Optional asset type string for Asset types
|
|
50
|
+
* @returns Record of property names to default values
|
|
51
|
+
*/
|
|
52
|
+
generateDefaults(objectType: ObjectType, assetType?: string): Record<string, unknown>;
|
|
53
|
+
/**
|
|
54
|
+
* Get the default value for a specific type node.
|
|
55
|
+
*
|
|
56
|
+
* @param node - The type node
|
|
57
|
+
* @param context - Generation context for tracking depth
|
|
58
|
+
* @returns The default value, or undefined if the type should be skipped
|
|
59
|
+
*/
|
|
60
|
+
private getDefaultForType;
|
|
61
|
+
/**
|
|
62
|
+
* Get default for a union type by picking the first non-null/undefined variant.
|
|
63
|
+
*/
|
|
64
|
+
private getDefaultForUnion;
|
|
65
|
+
/**
|
|
66
|
+
* Get default for an intersection type by attempting to merge.
|
|
67
|
+
*/
|
|
68
|
+
private getDefaultForIntersection;
|
|
69
|
+
/**
|
|
70
|
+
* Get default for an object type by processing required properties.
|
|
71
|
+
*/
|
|
72
|
+
private getDefaultForObject;
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=default-value-generator.d.ts.map
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { ObjectType, NamedType } from "@xlr-lib/xlr";
|
|
2
|
+
import { type TypeRegistry } from "./utils";
|
|
3
|
+
import { type TypeScriptContext, type UnexportedTypeInfo } from "./type-resolver";
|
|
4
|
+
export type { TypeScriptContext, UnexportedTypeInfo } from "./type-resolver";
|
|
5
|
+
export type { BuilderInfo } from "./builder-class-generator";
|
|
6
|
+
/**
|
|
7
|
+
* Configuration for the generator
|
|
8
|
+
*/
|
|
9
|
+
export interface GeneratorConfig {
|
|
10
|
+
/** Import path for functional utilities (default: "@player-lang/functional-dsl") */
|
|
11
|
+
functionalImportPath?: string;
|
|
12
|
+
/** Import path for player-ui types (default: "@player-ui/types") */
|
|
13
|
+
typesImportPath?: string;
|
|
14
|
+
/**
|
|
15
|
+
* TypeScript context for automatic import resolution.
|
|
16
|
+
* When provided, the generator will automatically resolve import paths
|
|
17
|
+
* using TypeScript's module resolution.
|
|
18
|
+
*/
|
|
19
|
+
tsContext?: TypeScriptContext;
|
|
20
|
+
/** Function to generate the type import path for a given type name */
|
|
21
|
+
typeImportPathGenerator?: (typeName: string) => string;
|
|
22
|
+
/**
|
|
23
|
+
* Set of type names that are defined in the same source file as the main type.
|
|
24
|
+
* Types not in this set will be imported from their own source files using typeImportPathGenerator.
|
|
25
|
+
* When tsContext is provided, this is computed automatically from the source file.
|
|
26
|
+
*/
|
|
27
|
+
sameFileTypes?: Set<string>;
|
|
28
|
+
/**
|
|
29
|
+
* Explicitly maps type names to their package names for external imports.
|
|
30
|
+
* Types in this map will be imported from the specified package (e.g., "@player-lang/types").
|
|
31
|
+
* This takes precedence over typeImportPathGenerator for the specified types.
|
|
32
|
+
*/
|
|
33
|
+
externalTypes?: Map<string, string>;
|
|
34
|
+
/**
|
|
35
|
+
* Type registry for resolving nested interface references.
|
|
36
|
+
* Maps type names to their XLR ObjectType definitions.
|
|
37
|
+
* Used to find AssetWrapper paths in nested interfaces.
|
|
38
|
+
*/
|
|
39
|
+
typeRegistry?: TypeRegistry;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Result of builder generation including warnings
|
|
43
|
+
*/
|
|
44
|
+
export interface GeneratorResult {
|
|
45
|
+
/** Generated TypeScript code */
|
|
46
|
+
code: string;
|
|
47
|
+
/** Types that need to be exported in their source files */
|
|
48
|
+
unexportedTypes: UnexportedTypeInfo[];
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Generates functional builder TypeScript code from XLR types.
|
|
52
|
+
* This class orchestrates the type collection, transformation, and code generation.
|
|
53
|
+
*/
|
|
54
|
+
export declare class FunctionalBuilderGenerator {
|
|
55
|
+
private readonly namedType;
|
|
56
|
+
private readonly config;
|
|
57
|
+
/** Import generator handles import tracking and generation */
|
|
58
|
+
private readonly importGenerator;
|
|
59
|
+
/** Type transformer handles XLR to TypeScript type conversion */
|
|
60
|
+
private readonly typeTransformer;
|
|
61
|
+
/** Type collector handles collecting type references */
|
|
62
|
+
private readonly typeCollector;
|
|
63
|
+
/** Builder class generator handles class code generation */
|
|
64
|
+
private readonly builderClassGenerator;
|
|
65
|
+
/** Map short type names to their full qualified names */
|
|
66
|
+
private readonly namespaceMemberMap;
|
|
67
|
+
constructor(namedType: NamedType<ObjectType>, config?: GeneratorConfig);
|
|
68
|
+
/**
|
|
69
|
+
* Get list of types that exist but need to be exported.
|
|
70
|
+
* Call this after generate() to get warnings for the user.
|
|
71
|
+
*/
|
|
72
|
+
getUnexportedTypes(): UnexportedTypeInfo[];
|
|
73
|
+
/**
|
|
74
|
+
* Get list of types that couldn't be resolved at all.
|
|
75
|
+
* These types are used in the generated code but won't be imported,
|
|
76
|
+
* causing type errors. Often these are namespaced types (e.g., Validation.CrossfieldReference).
|
|
77
|
+
*/
|
|
78
|
+
getUnresolvedTypes(): string[];
|
|
79
|
+
/**
|
|
80
|
+
* Generate the builder code
|
|
81
|
+
*/
|
|
82
|
+
generate(): string;
|
|
83
|
+
private createBuilderInfo;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Generate functional builder code from a NamedType<ObjectType>
|
|
87
|
+
* @param namedType - The XLR NamedType to generate a builder for
|
|
88
|
+
* @param config - Optional generator configuration
|
|
89
|
+
* @returns Generated TypeScript code for the functional builder
|
|
90
|
+
*/
|
|
91
|
+
export declare function generateFunctionalBuilder(namedType: NamedType<ObjectType>, config?: GeneratorConfig): string;
|
|
92
|
+
/**
|
|
93
|
+
* Generate functional builder code with warnings about unexported types.
|
|
94
|
+
* Use this when you want to get detailed information about types that need
|
|
95
|
+
* to be exported in their source files.
|
|
96
|
+
*
|
|
97
|
+
* @param namedType - The XLR NamedType to generate a builder for
|
|
98
|
+
* @param config - Optional generator configuration
|
|
99
|
+
* @returns Generated code and list of types that need to be exported
|
|
100
|
+
*/
|
|
101
|
+
export declare function generateFunctionalBuilderWithWarnings(namedType: NamedType<ObjectType>, config?: GeneratorConfig): GeneratorResult;
|
|
102
|
+
//# sourceMappingURL=generator.d.ts.map
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { RefType } from "@xlr-lib/xlr";
|
|
2
|
+
import type { TypeScriptContext, UnexportedTypeInfo } from "./type-resolver";
|
|
3
|
+
import { type TypeRegistry } from "./utils";
|
|
4
|
+
import type { TypeTracker } from "./type-collector";
|
|
5
|
+
import type { TypeTransformContext } from "./type-transformer";
|
|
6
|
+
/**
|
|
7
|
+
* Configuration for import generation.
|
|
8
|
+
*/
|
|
9
|
+
export interface ImportGeneratorConfig {
|
|
10
|
+
/** Import path for functional utilities (default: "@player-lang/functional-dsl") */
|
|
11
|
+
functionalImportPath?: string;
|
|
12
|
+
/** Import path for player-ui types (default: "@player-ui/types") */
|
|
13
|
+
typesImportPath?: string;
|
|
14
|
+
/** TypeScript context for automatic import resolution */
|
|
15
|
+
tsContext?: TypeScriptContext;
|
|
16
|
+
/** Function to generate the type import path for a given type name */
|
|
17
|
+
typeImportPathGenerator?: (typeName: string) => string;
|
|
18
|
+
/** Types defined in the same source file as the main type */
|
|
19
|
+
sameFileTypes?: Set<string>;
|
|
20
|
+
/** External type mappings (type name -> package name) */
|
|
21
|
+
externalTypes?: Map<string, string>;
|
|
22
|
+
/** Type registry for resolving types that extend AssetWrapper */
|
|
23
|
+
typeRegistry?: TypeRegistry;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Generates import statements and tracks type references.
|
|
27
|
+
* Implements both TypeTracker and TypeTransformContext interfaces.
|
|
28
|
+
*/
|
|
29
|
+
export declare class ImportGenerator implements TypeTracker, TypeTransformContext {
|
|
30
|
+
private readonly config;
|
|
31
|
+
/** Track all type references that need to be imported, grouped by source file */
|
|
32
|
+
private referencedTypesBySource;
|
|
33
|
+
/** Track types that should be imported from the main type's source file */
|
|
34
|
+
private referencedTypes;
|
|
35
|
+
/** Track whether Asset type is needed for imports */
|
|
36
|
+
private needsAssetImport;
|
|
37
|
+
/** Track generic parameter symbols (e.g., T, U) that should not be imported */
|
|
38
|
+
private genericParamSymbols;
|
|
39
|
+
/** TypeScript resolver for automatic import path resolution */
|
|
40
|
+
private readonly tsResolver?;
|
|
41
|
+
/** Track types that couldn't be resolved - will cause errors if used */
|
|
42
|
+
private unresolvedTypes;
|
|
43
|
+
/** Track namespaces that need to be imported (e.g., "Validation" from @player-ui/types) */
|
|
44
|
+
private namespaceImports;
|
|
45
|
+
/** Map short type names to their full qualified names (e.g., "CrossfieldReference" -> "Validation.CrossfieldReference") */
|
|
46
|
+
private namespaceMemberMap;
|
|
47
|
+
/** Effective sameFileTypes - computed from tsContext or provided directly */
|
|
48
|
+
private readonly effectiveSameFileTypes?;
|
|
49
|
+
constructor(config?: ImportGeneratorConfig);
|
|
50
|
+
setNeedsAssetImport(value: boolean): void;
|
|
51
|
+
getNeedsAssetImport(): boolean;
|
|
52
|
+
getNamespaceMemberMap(): Map<string, string>;
|
|
53
|
+
getGenericParamSymbols(): Set<string>;
|
|
54
|
+
getAssetWrapperExtendsRef(typeName: string): RefType | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* Get list of types that exist but need to be exported.
|
|
57
|
+
*/
|
|
58
|
+
getUnexportedTypes(): UnexportedTypeInfo[];
|
|
59
|
+
/**
|
|
60
|
+
* Get list of types that couldn't be resolved at all.
|
|
61
|
+
*/
|
|
62
|
+
getUnresolvedTypes(): string[];
|
|
63
|
+
/**
|
|
64
|
+
* Track a referenced type for import generation.
|
|
65
|
+
*/
|
|
66
|
+
trackReferencedType(typeName: string): void;
|
|
67
|
+
/**
|
|
68
|
+
* Track a namespace that needs to be imported.
|
|
69
|
+
*/
|
|
70
|
+
trackNamespaceImport(namespaceName: string): void;
|
|
71
|
+
/**
|
|
72
|
+
* Generate import statements.
|
|
73
|
+
*/
|
|
74
|
+
generateImports(mainTypeName: string): string;
|
|
75
|
+
private getTypeFileName;
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=import-generator.d.ts.map
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @player-lang/functional-dsl-generator
|
|
3
|
+
*
|
|
4
|
+
* Generates functional builders from XLR types for Player-UI assets.
|
|
5
|
+
*/
|
|
6
|
+
export { generateFunctionalBuilder, generateFunctionalBuilderWithWarnings, type GeneratorConfig, type BuilderInfo, type GeneratorResult, type UnexportedTypeInfo, type TypeScriptContext, } from "./generator";
|
|
7
|
+
export * from "./utils";
|
|
8
|
+
export { TsMorphTypeDefinitionFinder, type UnexportedTypeLocation, } from "./ts-morph-type-finder";
|
|
9
|
+
export { createTypeScriptResolver, isBuiltInDeclarationPath, isDeclarationExported, type TypeResolutionResult, } from "./type-resolver";
|
|
10
|
+
export { isNodeModulesPath, extractPackageNameFromPath, createRelativeImportPath, resolveRelativeImportPath, } from "./path-utils";
|
|
11
|
+
export { categorizeTypes, groupExternalTypesByPackage, type TypeCategories, type CategorizerOptions, } from "./type-categorizer";
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Determines if a file path appears to be from a package (node_modules or pnpm store).
|
|
3
|
+
*
|
|
4
|
+
* @param filePath - The file path to analyze
|
|
5
|
+
* @returns True if the path looks like it belongs to a package
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* isNodeModulesPath('/project/node_modules/react/index.d.ts') // true
|
|
10
|
+
* isNodeModulesPath('/project/src/components/Button.ts') // false
|
|
11
|
+
* isNodeModulesPath('/project/node_modules/.pnpm/react@18.0.0/...') // true
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare function isNodeModulesPath(filePath: string): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Extracts the package name from a node_modules path.
|
|
17
|
+
* Handles npm, pnpm store, scoped packages, and various structures.
|
|
18
|
+
*
|
|
19
|
+
* @param filePath - The file path to extract package name from
|
|
20
|
+
* @returns The package name (e.g., 'lodash', '@player-lang/types') or null
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* // Standard npm package
|
|
25
|
+
* extractPackageNameFromPath('/project/node_modules/lodash/index.d.ts')
|
|
26
|
+
* // Returns: 'lodash'
|
|
27
|
+
*
|
|
28
|
+
* // Scoped package
|
|
29
|
+
* extractPackageNameFromPath('/project/node_modules/@player-lang/types/index.d.ts')
|
|
30
|
+
* // Returns: '@player-lang/types'
|
|
31
|
+
*
|
|
32
|
+
* // pnpm store
|
|
33
|
+
* extractPackageNameFromPath('/project/node_modules/.pnpm/@player-lang+types@1.0.0/node_modules/@player-lang/types/index.d.ts')
|
|
34
|
+
* // Returns: '@player-lang/types'
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare function extractPackageNameFromPath(filePath: string): string | null;
|
|
38
|
+
/**
|
|
39
|
+
* Creates a relative import path from one file to another.
|
|
40
|
+
* Converts TypeScript extensions to JavaScript for runtime imports.
|
|
41
|
+
*
|
|
42
|
+
* @param fromFile - The absolute path of the importing file
|
|
43
|
+
* @param toFile - The absolute path of the file being imported
|
|
44
|
+
* @returns Relative import path with .js extension
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* createRelativeImportPath('/project/src/types/foo.ts', '/project/src/types/bar.ts')
|
|
49
|
+
* // Returns: './bar.js'
|
|
50
|
+
*
|
|
51
|
+
* createRelativeImportPath('/project/src/builders/foo.ts', '/project/src/types/bar.ts')
|
|
52
|
+
* // Returns: '../types/bar.js'
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export declare function createRelativeImportPath(fromFile: string, toFile: string): string;
|
|
56
|
+
/**
|
|
57
|
+
* Resolves a relative import path to an absolute file path.
|
|
58
|
+
* Handles .js to .ts conversion for TypeScript resolution.
|
|
59
|
+
*
|
|
60
|
+
* @param fromFile - The absolute path of the file containing the import
|
|
61
|
+
* @param importSpecifier - The import specifier (e.g., './types', '../utils.js')
|
|
62
|
+
* @returns Absolute path with .ts extension
|
|
63
|
+
*/
|
|
64
|
+
export declare function resolveRelativeImportPath(fromFile: string, importSpecifier: string): string;
|
|
65
|
+
//# sourceMappingURL=path-utils.d.ts.map
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Information about an unexported type that was found
|
|
3
|
+
*/
|
|
4
|
+
export interface UnexportedTypeLocation {
|
|
5
|
+
/** The name of the type */
|
|
6
|
+
typeName: string;
|
|
7
|
+
/** The file where the type is declared (but not exported) */
|
|
8
|
+
filePath: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Finds type definitions by searching through TypeScript source files using ts-morph.
|
|
12
|
+
* Handles interfaces, type aliases, classes, and re-exported types.
|
|
13
|
+
* Supports both local files and types from node_modules.
|
|
14
|
+
*
|
|
15
|
+
* Note: This is the ts-morph based implementation. For the TypeScript API based
|
|
16
|
+
* implementation, see TypeScriptTypeDefinitionFinder in type-resolver.ts.
|
|
17
|
+
*/
|
|
18
|
+
export declare class TsMorphTypeDefinitionFinder {
|
|
19
|
+
private project;
|
|
20
|
+
private readonly typeLocationCache;
|
|
21
|
+
private readonly unexportedTypes;
|
|
22
|
+
/**
|
|
23
|
+
* Finds the source file for a type by searching the codebase.
|
|
24
|
+
* Recursively follows imports to find where a type is actually defined.
|
|
25
|
+
* Supports both relative imports and node_modules packages.
|
|
26
|
+
*
|
|
27
|
+
* @param typeName - The name of the type to find
|
|
28
|
+
* @param startingFile - The file to start searching from
|
|
29
|
+
* @returns The path to the file containing the type definition, or null if not found
|
|
30
|
+
*/
|
|
31
|
+
findTypeSourceFile(typeName: string, startingFile: string): string | null;
|
|
32
|
+
/**
|
|
33
|
+
* Recursively searches for a type definition through imports.
|
|
34
|
+
* Handles both relative imports and node_modules packages.
|
|
35
|
+
*/
|
|
36
|
+
private searchForType;
|
|
37
|
+
/**
|
|
38
|
+
* Checks if an import declaration imports a specific type.
|
|
39
|
+
* Handles named imports, default imports, and namespace imports.
|
|
40
|
+
*/
|
|
41
|
+
private getImportedTypeFromDeclaration;
|
|
42
|
+
/**
|
|
43
|
+
* Checks if a source file defines and exports a specific type.
|
|
44
|
+
* Handles interfaces, type aliases, classes, and re-exported types.
|
|
45
|
+
* Only returns true if the type is publicly exported.
|
|
46
|
+
*/
|
|
47
|
+
private fileDefinesType;
|
|
48
|
+
/**
|
|
49
|
+
* Resolves a relative import path to an actual file path.
|
|
50
|
+
* Handles TypeScript extensions and index files.
|
|
51
|
+
*/
|
|
52
|
+
private resolveImportPath;
|
|
53
|
+
/**
|
|
54
|
+
* Get all types that were found to exist but are not exported.
|
|
55
|
+
* Call this after searching to get the list of types that need to be exported.
|
|
56
|
+
*/
|
|
57
|
+
getUnexportedTypes(): UnexportedTypeLocation[];
|
|
58
|
+
/**
|
|
59
|
+
* Check if a file declares a type (regardless of export status).
|
|
60
|
+
* Used internally to track unexported types.
|
|
61
|
+
*/
|
|
62
|
+
private fileHasTypeDeclaration;
|
|
63
|
+
/**
|
|
64
|
+
* Track an unexported type for later reporting.
|
|
65
|
+
*/
|
|
66
|
+
private trackUnexportedType;
|
|
67
|
+
/**
|
|
68
|
+
* Disposes of internal resources and clears caches.
|
|
69
|
+
* Should be called when the finder is no longer needed.
|
|
70
|
+
*/
|
|
71
|
+
dispose(): void;
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=ts-morph-type-finder.d.ts.map
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { TsMorphTypeDefinitionFinder } from "./ts-morph-type-finder";
|
|
2
|
+
/**
|
|
3
|
+
* Categorized types for import generation.
|
|
4
|
+
* Types are split into three categories based on where they should be imported from.
|
|
5
|
+
*/
|
|
6
|
+
export interface TypeCategories {
|
|
7
|
+
/** Types defined in the same file as the main type (import from main source) */
|
|
8
|
+
localTypes: Set<string>;
|
|
9
|
+
/** Types from other local files, grouped by relative import path */
|
|
10
|
+
relativeImports: Map<string, Set<string>>;
|
|
11
|
+
/** Types from external packages (node_modules), mapped to package name */
|
|
12
|
+
externalTypes: Map<string, string>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Options for type categorization.
|
|
16
|
+
*/
|
|
17
|
+
export interface CategorizerOptions {
|
|
18
|
+
/** The main source file being generated */
|
|
19
|
+
mainSourceFile: string;
|
|
20
|
+
/** Types known to be in the main source file (optional optimization) */
|
|
21
|
+
sameFileTypes?: Set<string>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Categorizes referenced types into local, relative, and external imports.
|
|
25
|
+
*
|
|
26
|
+
* Uses TsMorphTypeDefinitionFinder to resolve where each type is defined, then
|
|
27
|
+
* categorizes based on the resolved path:
|
|
28
|
+
* - Same file as main → localTypes
|
|
29
|
+
* - Different local file → relativeImports
|
|
30
|
+
* - node_modules → externalTypes
|
|
31
|
+
*
|
|
32
|
+
* @param referencedTypes - Set of type names that need to be imported
|
|
33
|
+
* @param finder - TsMorphTypeDefinitionFinder instance for resolving type locations
|
|
34
|
+
* @param options - Categorization options including main source file
|
|
35
|
+
* @returns Categorized types for import generation
|
|
36
|
+
*/
|
|
37
|
+
export declare function categorizeTypes(referencedTypes: Set<string>, finder: TsMorphTypeDefinitionFinder, options: CategorizerOptions): TypeCategories;
|
|
38
|
+
/**
|
|
39
|
+
* Groups external types by their package name for import generation.
|
|
40
|
+
* This allows generating single imports per package with multiple types.
|
|
41
|
+
*
|
|
42
|
+
* @param externalTypes - Map of typeName → packageName
|
|
43
|
+
* @returns Map of packageName → Set of typeNames
|
|
44
|
+
*/
|
|
45
|
+
export declare function groupExternalTypesByPackage(externalTypes: Map<string, string>): Map<string, Set<string>>;
|
|
46
|
+
//# sourceMappingURL=type-categorizer.d.ts.map
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { NodeType, ObjectType, NamedType } from "@xlr-lib/xlr";
|
|
2
|
+
import { type TypeRegistry } from "./utils";
|
|
3
|
+
/**
|
|
4
|
+
* Interface for tracking type references.
|
|
5
|
+
* Implemented by the import generator to track types that need to be imported.
|
|
6
|
+
*/
|
|
7
|
+
export interface TypeTracker {
|
|
8
|
+
trackReferencedType(typeName: string): void;
|
|
9
|
+
trackNamespaceImport(namespaceName: string): void;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Collects type references from XLR types for import generation.
|
|
13
|
+
*/
|
|
14
|
+
export declare class TypeCollector {
|
|
15
|
+
private readonly typeTracker;
|
|
16
|
+
private readonly genericParamSymbols;
|
|
17
|
+
private readonly mainTypeName;
|
|
18
|
+
private readonly namespaceMemberMap;
|
|
19
|
+
private readonly typeRegistry?;
|
|
20
|
+
constructor(typeTracker: TypeTracker, genericParamSymbols: Set<string>, mainTypeName: string, namespaceMemberMap: Map<string, string>, typeRegistry?: TypeRegistry);
|
|
21
|
+
/**
|
|
22
|
+
* Collect generic parameter symbols (e.g., T, U) from the type definition.
|
|
23
|
+
* These should not be imported as they are type parameters, not concrete types.
|
|
24
|
+
*
|
|
25
|
+
* Also handles the case where a non-generic type extends a generic base without
|
|
26
|
+
* passing type arguments. In that scenario, XLR copies properties from the base
|
|
27
|
+
* (including references to the base's generic params like `AnyAsset`) but does
|
|
28
|
+
* NOT propagate `genericTokens` to the child type. We scan the type registry for
|
|
29
|
+
* generic parameter symbols that should be excluded from imports.
|
|
30
|
+
*/
|
|
31
|
+
collectGenericParamSymbols(namedType: NamedType<ObjectType>): void;
|
|
32
|
+
/**
|
|
33
|
+
* Collect type references from generic parameter constraints and defaults.
|
|
34
|
+
* This ensures types used in generics like "T extends Foo = Bar<X>" have
|
|
35
|
+
* Foo, Bar, and X added to referencedTypes for import generation.
|
|
36
|
+
*/
|
|
37
|
+
collectTypesFromGenericTokens(namedType: NamedType<ObjectType>): void;
|
|
38
|
+
/**
|
|
39
|
+
* Collect all referenced types from an object type for imports.
|
|
40
|
+
*/
|
|
41
|
+
collectReferencedTypes(objType: ObjectType): void;
|
|
42
|
+
/**
|
|
43
|
+
* Collect referenced types from a node type.
|
|
44
|
+
*/
|
|
45
|
+
collectReferencedTypesFromNode(node: NodeType): void;
|
|
46
|
+
/**
|
|
47
|
+
* Recursively collect type references from any NodeType.
|
|
48
|
+
* This handles refs, arrays, unions, intersections, and objects.
|
|
49
|
+
*/
|
|
50
|
+
private collectTypeReferencesFromNode;
|
|
51
|
+
/**
|
|
52
|
+
* Check if a type name appears to be a generic type parameter of the referenced type.
|
|
53
|
+
* This detects cases like ref="Bar<AnyAsset>" where "AnyAsset" is Bar's type parameter,
|
|
54
|
+
* not a concrete type to import.
|
|
55
|
+
*
|
|
56
|
+
* @param argName - The name of the type argument being checked
|
|
57
|
+
* @param parentRef - The parent ref string that contains the generic usage
|
|
58
|
+
* @returns true if argName appears to be a type parameter in parentRef
|
|
59
|
+
*/
|
|
60
|
+
private isTypeParamOfRef;
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=type-collector.d.ts.map
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
/**
|
|
3
|
+
* TypeScript context for automatic import resolution.
|
|
4
|
+
* When provided, the generator uses TypeScript's module resolution to determine
|
|
5
|
+
* where types should be imported from.
|
|
6
|
+
*/
|
|
7
|
+
export interface TypeScriptContext {
|
|
8
|
+
/** The TypeScript program */
|
|
9
|
+
program: ts.Program;
|
|
10
|
+
/** The source file containing the type being generated */
|
|
11
|
+
sourceFile: ts.SourceFile;
|
|
12
|
+
/** The output directory where generated files will be written */
|
|
13
|
+
outputDir: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Information about an unexported type that needs to be exported
|
|
17
|
+
*/
|
|
18
|
+
export interface UnexportedTypeInfo {
|
|
19
|
+
/** The type name that needs to be exported */
|
|
20
|
+
typeName: string;
|
|
21
|
+
/** The file path where the type is declared */
|
|
22
|
+
filePath: string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Result from type resolution.
|
|
26
|
+
* - sameFile: type is defined in the same file as the main type
|
|
27
|
+
* - notFound: type couldn't be resolved anywhere
|
|
28
|
+
* - string: import path for the type (package name or relative path)
|
|
29
|
+
*/
|
|
30
|
+
export type TypeResolutionResult = "sameFile" | "notFound" | string;
|
|
31
|
+
/**
|
|
32
|
+
* Check if a file path is a TypeScript lib/built-in declaration file.
|
|
33
|
+
* These are files like lib.dom.d.ts that contain built-in type definitions.
|
|
34
|
+
*/
|
|
35
|
+
export declare function isBuiltInDeclarationPath(filePath: string): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Check if a declaration node is exported.
|
|
38
|
+
*/
|
|
39
|
+
export declare function isDeclarationExported(node: ts.Declaration, typescript: typeof ts): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Creates an import resolver using TypeScript's type checker and
|
|
42
|
+
* TsMorphTypeDefinitionFinder for tracing types through imports.
|
|
43
|
+
* This handles cases where types come through extends, Pick, re-exports, etc.
|
|
44
|
+
*/
|
|
45
|
+
export declare function createTypeScriptResolver(tsContext: TypeScriptContext): {
|
|
46
|
+
resolveTypePath: (typeName: string) => TypeResolutionResult;
|
|
47
|
+
getUnexportedTypes: () => UnexportedTypeInfo[];
|
|
48
|
+
};
|
|
49
|
+
//# sourceMappingURL=type-resolver.d.ts.map
|