types.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * Reference Resolution Types
  3. *
  4. * Types for the reference resolution system.
  5. */
  6. import { EdgeKind, Language, Node } from '../types';
  7. /**
  8. * An unresolved reference from extraction
  9. */
  10. export interface UnresolvedRef {
  11. /** ID of the source node containing the reference */
  12. fromNodeId: string;
  13. /** The name being referenced */
  14. referenceName: string;
  15. /** Type of reference */
  16. referenceKind: EdgeKind;
  17. /** Line where reference occurs */
  18. line: number;
  19. /** Column where reference occurs */
  20. column: number;
  21. /** File path where reference occurs */
  22. filePath: string;
  23. /** Language of the source file */
  24. language: Language;
  25. /** Possible qualified names it might resolve to */
  26. candidates?: string[];
  27. }
  28. /**
  29. * A resolved reference
  30. */
  31. export interface ResolvedRef {
  32. /** Original unresolved reference */
  33. original: UnresolvedRef;
  34. /** ID of the target node */
  35. targetNodeId: string;
  36. /** Confidence score (0-1) */
  37. confidence: number;
  38. /** How it was resolved */
  39. resolvedBy: 'exact-match' | 'import' | 'qualified-name' | 'framework' | 'fuzzy' | 'instance-method' | 'file-path';
  40. }
  41. /**
  42. * Result of resolution attempt
  43. */
  44. export interface ResolutionResult {
  45. /** Successfully resolved references */
  46. resolved: ResolvedRef[];
  47. /** References that couldn't be resolved */
  48. unresolved: UnresolvedRef[];
  49. /** Statistics */
  50. stats: {
  51. total: number;
  52. resolved: number;
  53. unresolved: number;
  54. byMethod: Record<string, number>;
  55. };
  56. }
  57. /**
  58. * Context for resolution - provides access to the graph
  59. */
  60. export interface ResolutionContext {
  61. /** Get all nodes in a file */
  62. getNodesInFile(filePath: string): Node[];
  63. /** Get all nodes by name */
  64. getNodesByName(name: string): Node[];
  65. /** Get all nodes by qualified name */
  66. getNodesByQualifiedName(qualifiedName: string): Node[];
  67. /** Get all nodes of a kind */
  68. getNodesByKind(kind: Node['kind']): Node[];
  69. /** Check if a file exists */
  70. fileExists(filePath: string): boolean;
  71. /** Read file content */
  72. readFile(filePath: string): string | null;
  73. /** Get project root */
  74. getProjectRoot(): string;
  75. /** Get all files */
  76. getAllFiles(): string[];
  77. /** Get nodes by lowercase name (O(1) lookup for fuzzy matching) */
  78. getNodesByLowerName(lowerName: string): Node[];
  79. /** Get cached import mappings for a file */
  80. getImportMappings(filePath: string, language: Language): ImportMapping[];
  81. /**
  82. * Project import-path aliases (tsconfig/jsconfig `paths`). Returns
  83. * `null` when the project doesn't define any. Cached per resolver
  84. * instance — safe to call from any resolver code path. Optional so
  85. * existing test fixtures and external context implementations
  86. * compile without modification; production resolver implements it.
  87. */
  88. getProjectAliases?(): import('./path-aliases').AliasMap | null;
  89. /**
  90. * Go module info from `go.mod` at the project root. Returns `null`
  91. * when the project has no `go.mod` (non-Go projects, pre-modules
  92. * Go code, or projects whose modules live in subdirectories). Used
  93. * by the Go branch of import resolution to distinguish in-module
  94. * cross-package imports from third-party packages.
  95. */
  96. getGoModule?(): import('./go-module').GoModule | null;
  97. /**
  98. * Re-exports declared by a file (`export { x } from './other'`,
  99. * `export * from './other'`). Empty array when the file has none.
  100. * Optional so older callers compile; the import resolver follows
  101. * re-export chains when this is provided.
  102. */
  103. getReExports?(filePath: string, language: Language): ReExport[];
  104. /**
  105. * List immediate subdirectories of `relativePath` (relative to the
  106. * project root). Returns an empty array when the path doesn't exist
  107. * or isn't a directory. Used by framework resolvers that need to
  108. * walk build-system metadata (e.g. Cargo workspace globs). Optional
  109. * so external context implementations and test fixtures compile
  110. * without modification.
  111. */
  112. listDirectories?(relativePath: string): string[];
  113. /**
  114. * C/C++ include search directories (relative to project root),
  115. * extracted from compile_commands.json or discovered by heuristic.
  116. * Used by resolveCppIncludePath to search -I directories when
  117. * relative resolution fails. Optional so existing callers compile.
  118. */
  119. getCppIncludeDirs?(): string[];
  120. }
  121. /**
  122. * Result of framework-specific file extraction.
  123. */
  124. export interface FrameworkExtractionResult {
  125. /** Framework-specific nodes (e.g. routes) */
  126. nodes: Node[];
  127. /** Framework-specific unresolved references (e.g. route -> handler) */
  128. references: UnresolvedRef[];
  129. }
  130. /**
  131. * Framework-specific resolver
  132. */
  133. export interface FrameworkResolver {
  134. /** Framework name */
  135. name: string;
  136. /** Languages this framework applies to. If omitted, applies to all languages. */
  137. languages?: Language[];
  138. /** Detect if project uses this framework (project-level, called once at startup) */
  139. detect(context: ResolutionContext): boolean;
  140. /** Resolve a reference using framework-specific patterns */
  141. resolve(ref: UnresolvedRef, context: ResolutionContext): ResolvedRef | null;
  142. /**
  143. * Opt a reference NAME through the resolver's name-exists pre-filter, even when
  144. * no node is named that. Needed for dynamic dispatch where the call target is
  145. * an attribute/descriptor, not a declared symbol (e.g. Django's
  146. * `self._iterable_class(...)`, React effect callbacks). Returning true lets the
  147. * ref reach `resolve()` instead of being dropped for having no name match.
  148. */
  149. claimsReference?(name: string): boolean;
  150. /**
  151. * Extract framework-specific nodes and references from a file.
  152. *
  153. * Returns route nodes, middleware nodes, etc., plus unresolved references
  154. * that link those nodes to handlers (view classes, controller methods,
  155. * included modules). Unresolved references flow into the normal resolution
  156. * pipeline; the framework's own `resolve()` is one of the strategies tried.
  157. */
  158. extract?(filePath: string, content: string): FrameworkExtractionResult;
  159. /**
  160. * Cross-file finalization pass, called once after all per-file extraction
  161. * completes (and again on every incremental sync). Used by frameworks where
  162. * a symbol's final representation depends on a sibling file the per-file
  163. * `extract()` never saw — e.g. NestJS's `RouterModule.register([...])`
  164. * sets route prefixes for controllers declared elsewhere.
  165. *
  166. * Implementations return route/etc. nodes with mutated fields (typically
  167. * `name`); the orchestrator persists each via `updateNode`. The node `id`
  168. * MUST be preserved so existing edges (route → handler, etc.) stay intact;
  169. * `qualifiedName` SHOULD be preserved so the pass stays idempotent — a
  170. * second run can recover the original in-file form from `qualifiedName`.
  171. */
  172. postExtract?(context: ResolutionContext): Node[];
  173. }
  174. /**
  175. * Import mapping from a file
  176. */
  177. export interface ImportMapping {
  178. /** Local name used in the file */
  179. localName: string;
  180. /** Original exported name (may differ due to aliasing) */
  181. exportedName: string;
  182. /** Source module/path */
  183. source: string;
  184. /** Whether it's a default import */
  185. isDefault: boolean;
  186. /** Whether it's a namespace import (import * as X) */
  187. isNamespace: boolean;
  188. /** Resolved file path (if local) */
  189. resolvedPath?: string;
  190. }
  191. /**
  192. * Re-export from a file: `export { x } from './other'` or
  193. * `export * from './other'`. Used by the resolver to chase
  194. * symbols through barrel files.
  195. */
  196. export type ReExport =
  197. | {
  198. kind: 'named';
  199. /** Name as exported by THIS file. */
  200. exportedName: string;
  201. /** Name in the upstream module (differs when renamed: `as`). */
  202. originalName: string;
  203. /** Module specifier of the upstream module. */
  204. source: string;
  205. }
  206. | {
  207. kind: 'wildcard';
  208. /** Module specifier of the upstream module. */
  209. source: string;
  210. };