|
| 1 | +import { ASTTraversal } from '@daxserver/validation-schema-codegen/traverse/ast-traversal' |
| 2 | +import { InterfaceDeclaration, TypeAliasDeclaration } from 'ts-morph' |
| 3 | + |
| 4 | +/** |
| 5 | + * Dependency analyzer for determining processing order |
| 6 | + */ |
| 7 | +export class DependencyAnalyzer { |
| 8 | + /** |
| 9 | + * Extract interface names referenced by a type alias |
| 10 | + */ |
| 11 | + extractInterfaceReferences( |
| 12 | + typeAlias: TypeAliasDeclaration, |
| 13 | + interfaces: Map<string, InterfaceDeclaration>, |
| 14 | + ): string[] { |
| 15 | + return ASTTraversal.extractInterfaceReferences(typeAlias, interfaces) |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Extract type alias names referenced by an interface |
| 20 | + */ |
| 21 | + extractTypeAliasReferences( |
| 22 | + interfaceDecl: InterfaceDeclaration, |
| 23 | + typeAliases: Map<string, TypeAliasDeclaration>, |
| 24 | + ): string[] { |
| 25 | + return ASTTraversal.extractTypeAliasReferences(interfaceDecl, typeAliases) |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Check if any type aliases reference interfaces |
| 30 | + */ |
| 31 | + hasInterfaceReferences( |
| 32 | + typeAliases: TypeAliasDeclaration[], |
| 33 | + interfaces: InterfaceDeclaration[], |
| 34 | + ): boolean { |
| 35 | + const interfaceMap = new Map<string, InterfaceDeclaration>() |
| 36 | + for (const iface of interfaces) { |
| 37 | + interfaceMap.set(iface.getName(), iface) |
| 38 | + } |
| 39 | + |
| 40 | + for (const typeAlias of typeAliases) { |
| 41 | + const references = this.extractInterfaceReferences(typeAlias, interfaceMap) |
| 42 | + if (references.length > 0) { |
| 43 | + return true |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return false |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Get type aliases that reference interfaces, ordered by their dependencies |
| 52 | + */ |
| 53 | + getTypeAliasesReferencingInterfaces( |
| 54 | + typeAliases: TypeAliasDeclaration[], |
| 55 | + interfaces: InterfaceDeclaration[], |
| 56 | + ): { typeAlias: TypeAliasDeclaration; referencedInterfaces: string[] }[] { |
| 57 | + const interfaceMap = new Map<string, InterfaceDeclaration>() |
| 58 | + for (const iface of interfaces) { |
| 59 | + interfaceMap.set(iface.getName(), iface) |
| 60 | + } |
| 61 | + |
| 62 | + const result: { typeAlias: TypeAliasDeclaration; referencedInterfaces: string[] }[] = [] |
| 63 | + |
| 64 | + for (const typeAlias of typeAliases) { |
| 65 | + const references = this.extractInterfaceReferences(typeAlias, interfaceMap) |
| 66 | + if (references.length > 0) { |
| 67 | + result.push({ |
| 68 | + typeAlias, |
| 69 | + referencedInterfaces: references, |
| 70 | + }) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return result |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Determine the correct processing order for interfaces and type aliases |
| 79 | + * Returns an object indicating which should be processed first |
| 80 | + */ |
| 81 | + analyzeProcessingOrder( |
| 82 | + typeAliases: TypeAliasDeclaration[], |
| 83 | + interfaces: InterfaceDeclaration[], |
| 84 | + ): { |
| 85 | + processInterfacesFirst: boolean |
| 86 | + typeAliasesDependingOnInterfaces: string[] |
| 87 | + interfacesDependingOnTypeAliases: string[] |
| 88 | + } { |
| 89 | + const typeAliasMap = new Map<string, TypeAliasDeclaration>() |
| 90 | + const interfaceMap = new Map<string, InterfaceDeclaration>() |
| 91 | + |
| 92 | + for (const typeAlias of typeAliases) { |
| 93 | + typeAliasMap.set(typeAlias.getName(), typeAlias) |
| 94 | + } |
| 95 | + |
| 96 | + for (const interfaceDecl of interfaces) { |
| 97 | + interfaceMap.set(interfaceDecl.getName(), interfaceDecl) |
| 98 | + } |
| 99 | + |
| 100 | + const typeAliasesDependingOnInterfaces: string[] = [] |
| 101 | + const interfacesDependingOnTypeAliases: string[] = [] |
| 102 | + |
| 103 | + // Check type aliases that depend on interfaces |
| 104 | + for (const typeAlias of typeAliases) { |
| 105 | + const interfaceRefs = this.extractInterfaceReferences(typeAlias, interfaceMap) |
| 106 | + if (interfaceRefs.length > 0) { |
| 107 | + typeAliasesDependingOnInterfaces.push(typeAlias.getName()) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // Check interfaces that depend on type aliases |
| 112 | + for (const interfaceDecl of interfaces) { |
| 113 | + const typeAliasRefs = this.extractTypeAliasReferences(interfaceDecl, typeAliasMap) |
| 114 | + if (typeAliasRefs.length > 0) { |
| 115 | + interfacesDependingOnTypeAliases.push(interfaceDecl.getName()) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // Determine processing order: |
| 120 | + // If interfaces depend on type aliases, process type aliases first |
| 121 | + // If only type aliases depend on interfaces, process interfaces first |
| 122 | + // If both have dependencies, process type aliases that interfaces depend on first, |
| 123 | + // then interfaces, then type aliases that depend on interfaces |
| 124 | + const processInterfacesFirst = |
| 125 | + interfacesDependingOnTypeAliases.length === 0 && typeAliasesDependingOnInterfaces.length > 0 |
| 126 | + |
| 127 | + return { |
| 128 | + processInterfacesFirst, |
| 129 | + typeAliasesDependingOnInterfaces, |
| 130 | + interfacesDependingOnTypeAliases, |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Clear internal caches |
| 136 | + */ |
| 137 | + clearCache(): void { |
| 138 | + ASTTraversal.clearCache() |
| 139 | + } |
| 140 | +} |
0 commit comments