import-resolver.ts 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815
  1. /**
  2. * Import Resolver
  3. *
  4. * Resolves import paths to actual files and symbols.
  5. */
  6. import * as fs from 'fs';
  7. import * as path from 'path';
  8. import { Language, Node } from '../types';
  9. import { UnresolvedRef, ResolvedRef, ResolutionContext, ImportMapping, ReExport } from './types';
  10. import { applyAliases } from './path-aliases';
  11. import { resolveWorkspaceImport } from './workspace-packages';
  12. /**
  13. * Extension resolution order by language
  14. */
  15. const EXTENSION_RESOLUTION: Record<string, string[]> = {
  16. typescript: ['.ts', '.tsx', '.d.ts', '.js', '.jsx', '/index.ts', '/index.tsx', '/index.js'],
  17. javascript: ['.js', '.jsx', '.mjs', '.cjs', '/index.js', '/index.jsx'],
  18. tsx: ['.tsx', '.ts', '.d.ts', '.js', '.jsx', '/index.tsx', '/index.ts', '/index.js'],
  19. jsx: ['.jsx', '.js', '/index.jsx', '/index.js'],
  20. // SFC consumers import plain TS/JS, sibling components, and barrels
  21. // (`./lib` → `./lib/index.ts`). Without a list, relative imports from a
  22. // `.svelte`/`.vue` file resolve to nothing, so barrel callers vanish (#629).
  23. svelte: ['.ts', '.js', '.svelte', '.tsx', '.jsx', '/index.ts', '/index.js', '/index.svelte'],
  24. vue: ['.ts', '.js', '.vue', '.tsx', '.jsx', '/index.ts', '/index.js', '/index.vue'],
  25. python: ['.py', '/__init__.py'],
  26. go: ['.go'],
  27. rust: ['.rs', '/mod.rs'],
  28. java: ['.java'],
  29. c: ['.h', '.c'],
  30. cpp: ['.h', '.hpp', '.hxx', '.cpp', '.cc', '.cxx'],
  31. csharp: ['.cs'],
  32. php: ['.php'],
  33. ruby: ['.rb'],
  34. objc: ['.h', '.m', '.mm'],
  35. };
  36. /**
  37. * Resolve an import path to an actual file
  38. */
  39. export function resolveImportPath(
  40. importPath: string,
  41. fromFile: string,
  42. language: Language,
  43. context: ResolutionContext
  44. ): string | null {
  45. // Skip external/npm packages — but pass the context so the
  46. // bare-specifier heuristic can consult the project's tsconfig
  47. // alias map first (custom prefixes like `@components/*` would
  48. // otherwise be misclassified as npm).
  49. if (isExternalImport(importPath, language, context)) {
  50. return null;
  51. }
  52. const projectRoot = context.getProjectRoot();
  53. const fromDir = path.dirname(path.join(projectRoot, fromFile));
  54. // Handle relative imports
  55. if (importPath.startsWith('.')) {
  56. return resolveRelativeImport(importPath, fromDir, language, context);
  57. }
  58. // Handle absolute/aliased imports (like @/ or src/)
  59. const aliased = resolveAliasedImport(importPath, projectRoot, language, context);
  60. if (aliased) return aliased;
  61. // C/C++ include directory search: when neither relative nor aliased
  62. // resolution found a match, search -I directories from
  63. // compile_commands.json or heuristic probing.
  64. if (language === 'c' || language === 'cpp') {
  65. return resolveCppIncludePath(importPath, language, context);
  66. }
  67. return null;
  68. }
  69. /**
  70. * C and C++ standard library header names (without delimiters).
  71. * Used by isExternalImport to filter system includes from resolution.
  72. */
  73. const C_CPP_STDLIB_HEADERS = new Set([
  74. // C standard library headers
  75. 'assert.h', 'complex.h', 'ctype.h', 'errno.h', 'fenv.h', 'float.h',
  76. 'inttypes.h', 'iso646.h', 'limits.h', 'locale.h', 'math.h', 'setjmp.h',
  77. 'signal.h', 'stdalign.h', 'stdarg.h', 'stdatomic.h', 'stdbool.h',
  78. 'stddef.h', 'stdint.h', 'stdio.h', 'stdlib.h', 'stdnoreturn.h',
  79. 'string.h', 'tgmath.h', 'threads.h', 'time.h', 'uchar.h', 'wchar.h',
  80. 'wctype.h',
  81. // C++ C-library wrappers (cname form)
  82. 'cassert', 'ccomplex', 'cctype', 'cerrno', 'cfenv', 'cfloat',
  83. 'cinttypes', 'ciso646', 'climits', 'clocale', 'cmath', 'csetjmp',
  84. 'csignal', 'cstdalign', 'cstdarg', 'cstdbool', 'cstddef', 'cstdint',
  85. 'cstdio', 'cstdlib', 'cstring', 'ctgmath', 'ctime', 'cuchar',
  86. 'cwchar', 'cwctype',
  87. // C++ STL headers
  88. 'algorithm', 'any', 'array', 'atomic', 'barrier', 'bit', 'bitset',
  89. 'charconv', 'chrono', 'codecvt', 'compare', 'complex', 'concepts',
  90. 'condition_variable', 'coroutine', 'deque', 'exception', 'execution',
  91. 'expected', 'filesystem', 'format', 'forward_list', 'fstream',
  92. 'functional', 'future', 'generator', 'initializer_list', 'iomanip',
  93. 'ios', 'iosfwd', 'iostream', 'istream', 'iterator', 'latch',
  94. 'limits', 'list', 'locale', 'map', 'mdspan', 'memory', 'memory_resource',
  95. 'mutex', 'new', 'numbers', 'numeric', 'optional', 'ostream', 'print',
  96. 'queue', 'random', 'ranges', 'ratio', 'regex', 'scoped_allocator',
  97. 'semaphore', 'set', 'shared_mutex', 'source_location', 'span',
  98. 'spanstream', 'sstream', 'stack', 'stacktrace', 'stdexcept',
  99. 'stdfloat', 'stop_token', 'streambuf', 'string', 'string_view',
  100. 'strstream', 'syncstream', 'system_error', 'thread', 'tuple',
  101. 'type_traits', 'typeindex', 'typeinfo', 'unordered_map',
  102. 'unordered_set', 'utility', 'valarray', 'variant', 'vector',
  103. 'version',
  104. ]);
  105. /**
  106. * Check if an import is external (npm package, etc.)
  107. *
  108. * `context` is consulted for project-defined path aliases
  109. * (tsconfig/jsconfig `paths`). Without that check, custom prefixes
  110. * like `@components/*` would fail the bare-specifier heuristic and
  111. * be classified as external before alias resolution can run.
  112. */
  113. function isExternalImport(
  114. importPath: string,
  115. language: Language,
  116. context?: ResolutionContext
  117. ): boolean {
  118. // Relative imports are not external
  119. if (importPath.startsWith('.')) {
  120. return false;
  121. }
  122. // Workspace-member imports (`@scope/ui`, `@scope/ui/widgets`) are LOCAL to
  123. // a monorepo even though they look like bare npm specifiers. Consult the
  124. // workspace map first so they aren't misclassified as external (#629). The
  125. // map is null for single-package repos, so this is a no-op there.
  126. const workspaces = context?.getWorkspacePackages?.();
  127. if (workspaces && resolveWorkspaceImport(importPath, workspaces)) {
  128. return false;
  129. }
  130. // Common external patterns
  131. if (language === 'typescript' || language === 'javascript' || language === 'tsx' || language === 'jsx') {
  132. // Node built-ins
  133. if (['fs', 'path', 'os', 'crypto', 'http', 'https', 'url', 'util', 'events', 'stream', 'child_process', 'buffer'].includes(importPath)) {
  134. return true;
  135. }
  136. // Project-defined alias prefix? Treat as local.
  137. const aliases = context?.getProjectAliases?.();
  138. if (aliases) {
  139. for (const pat of aliases.patterns) {
  140. if (importPath.startsWith(pat.prefix)) return false;
  141. }
  142. }
  143. // Scoped packages or bare specifiers that don't start with aliases
  144. if (!importPath.startsWith('@/') && !importPath.startsWith('~/') && !importPath.startsWith('src/')) {
  145. // Likely an npm package
  146. return true;
  147. }
  148. }
  149. if (language === 'python') {
  150. // Standard library modules
  151. const stdLibs = ['os', 'sys', 'json', 're', 'math', 'datetime', 'collections', 'typing', 'pathlib', 'logging'];
  152. if (stdLibs.includes(importPath.split('.')[0]!)) {
  153. return true;
  154. }
  155. }
  156. if (language === 'go') {
  157. // Relative imports (rare in idiomatic Go but the grammar allows them).
  158. if (importPath.startsWith('.')) {
  159. return false;
  160. }
  161. // In-module imports look like `<module-path>/sub/pkg` — local to
  162. // this project. Without the module-path check we'd flag every
  163. // cross-package call in a Go monorepo as external (issue #388).
  164. const mod = context?.getGoModule?.();
  165. if (mod && (importPath === mod.modulePath || importPath.startsWith(mod.modulePath + '/'))) {
  166. return false;
  167. }
  168. // `internal/` packages stay local even when go.mod is missing —
  169. // preserves the pre-#388 escape hatch for repos without a parsed module path.
  170. if (importPath.includes('/internal/')) {
  171. return false;
  172. }
  173. // Anything else is the Go standard library or a third-party module.
  174. return true;
  175. }
  176. if (language === 'c' || language === 'cpp') {
  177. // C/C++ standard library headers — both C-style (<stdio.h>) and
  178. // C++-style (<cstdio>, <vector>) forms. Checked against the import
  179. // path (which the extractor strips of <> or "" delimiters).
  180. if (C_CPP_STDLIB_HEADERS.has(importPath)) return true;
  181. // C++ headers without .h extension (e.g. "vector", "string")
  182. const withoutExt = importPath.replace(/\.h$/, '');
  183. if (C_CPP_STDLIB_HEADERS.has(withoutExt)) return true;
  184. }
  185. return false;
  186. }
  187. /**
  188. * Resolve a relative import
  189. */
  190. function resolveRelativeImport(
  191. importPath: string,
  192. fromDir: string,
  193. language: Language,
  194. context: ResolutionContext
  195. ): string | null {
  196. const projectRoot = context.getProjectRoot();
  197. const extensions = EXTENSION_RESOLUTION[language] || [];
  198. // Python dotted-relative imports (`from .certs import x`, `from ..pkg.mod
  199. // import y`): leading dots are PACKAGE levels (1 = current package), and the
  200. // remainder is a dotted submodule path. `path.resolve(dir, '.certs')` would
  201. // treat `.certs` as a literal hidden filename, so translate the Python form
  202. // to a real filesystem-relative path before resolving.
  203. if (language === 'python' && importPath.startsWith('.')) {
  204. const dots = importPath.length - importPath.replace(/^\.+/, '').length;
  205. const up = '../'.repeat(Math.max(0, dots - 1)); // 1 dot = current dir
  206. const rest = importPath.slice(dots).replace(/\./g, '/'); // 'sub.mod' -> 'sub/mod'
  207. const pyBase = path.resolve(fromDir, up + rest);
  208. const pyRel = path.relative(projectRoot, pyBase).replace(/\\/g, '/');
  209. for (const ext of extensions) {
  210. if (context.fileExists(pyRel + ext)) return pyRel + ext;
  211. }
  212. if (pyRel && context.fileExists(pyRel)) return pyRel;
  213. return null;
  214. }
  215. // Try the path as-is first
  216. const basePath = path.resolve(fromDir, importPath);
  217. const relativePath = path.relative(projectRoot, basePath).replace(/\\/g, '/');
  218. // Try each extension
  219. for (const ext of extensions) {
  220. const candidatePath = relativePath + ext;
  221. if (context.fileExists(candidatePath)) {
  222. return candidatePath;
  223. }
  224. }
  225. // Try without extension (might already have one)
  226. if (context.fileExists(relativePath)) {
  227. return relativePath;
  228. }
  229. return null;
  230. }
  231. /**
  232. * Resolve an aliased/absolute import.
  233. *
  234. * Tries, in order:
  235. * 1. Project-defined `compilerOptions.paths` (tsconfig/jsconfig).
  236. * Each pattern can have multiple replacements; tried in tsconfig
  237. * priority order with extension permutations.
  238. * 2. The legacy hard-coded fallback list (`@/`, `~/`, `src/`, ...)
  239. * for projects that have aliases but no tsconfig paths block.
  240. * 3. Direct path lookup (with extensions).
  241. */
  242. function resolveAliasedImport(
  243. importPath: string,
  244. projectRoot: string,
  245. language: Language,
  246. context: ResolutionContext
  247. ): string | null {
  248. const extensions = EXTENSION_RESOLUTION[language] || [];
  249. const tryWithExt = (basePath: string): string | null => {
  250. for (const ext of extensions) {
  251. const candidate = basePath + ext;
  252. if (context.fileExists(candidate)) return candidate;
  253. }
  254. if (context.fileExists(basePath)) return basePath;
  255. return null;
  256. };
  257. // 1. Project tsconfig/jsconfig paths.
  258. const aliasMap = context.getProjectAliases?.();
  259. if (aliasMap) {
  260. const candidates = applyAliases(importPath, aliasMap, projectRoot);
  261. for (const c of candidates) {
  262. const hit = tryWithExt(c);
  263. if (hit) return hit;
  264. }
  265. }
  266. // 1.5 Workspace packages (`@scope/ui/widgets` → `packages/ui/widgets`).
  267. // Resolves a monorepo member import to the member's directory; the
  268. // extension/index permutations below then find its barrel (#629).
  269. const workspaces = context.getWorkspacePackages?.();
  270. if (workspaces) {
  271. const base = resolveWorkspaceImport(importPath, workspaces);
  272. if (base) {
  273. const hit = tryWithExt(base);
  274. if (hit) return hit;
  275. }
  276. }
  277. // 2. Hard-coded fallback list. Kept for projects that use these
  278. // conventional aliases without declaring them in tsconfig.
  279. const fallbackAliases: Record<string, string> = {
  280. '@/': 'src/',
  281. '~/': 'src/',
  282. '@src/': 'src/',
  283. 'src/': 'src/',
  284. '@app/': 'app/',
  285. 'app/': 'app/',
  286. };
  287. for (const [alias, replacement] of Object.entries(fallbackAliases)) {
  288. if (importPath.startsWith(alias)) {
  289. const hit = tryWithExt(importPath.replace(alias, replacement));
  290. if (hit) return hit;
  291. }
  292. }
  293. // 3. Direct path.
  294. return tryWithExt(importPath);
  295. }
  296. /**
  297. * C/C++ include directory cache (keyed by project root).
  298. * Loaded once per resolver instance, shared across calls.
  299. */
  300. const cppIncludeDirCache = new Map<string, string[]>();
  301. /**
  302. * Clear the C/C++ include directory cache (call between indexing runs)
  303. */
  304. export function clearCppIncludeDirCache(): void {
  305. cppIncludeDirCache.clear();
  306. }
  307. /**
  308. * Discover C/C++ include search directories for a project.
  309. *
  310. * Strategy:
  311. * 1. Look for compile_commands.json (Clang compilation database) in the
  312. * project root and common build subdirectories. Parse -I and -isystem
  313. * flags from compiler commands.
  314. * 2. If no compilation database is found, probe for common convention
  315. * directories (include/, src/, lib/, api/) and top-level directories
  316. * containing .h/.hpp files.
  317. *
  318. * Returns paths relative to projectRoot.
  319. */
  320. export function loadCppIncludeDirs(projectRoot: string): string[] {
  321. const cached = cppIncludeDirCache.get(projectRoot);
  322. if (cached !== undefined) return cached;
  323. const dirs = loadCppIncludeDirsFromCompileDB(projectRoot)
  324. || loadCppIncludeDirsHeuristic(projectRoot);
  325. cppIncludeDirCache.set(projectRoot, dirs);
  326. return dirs;
  327. }
  328. /**
  329. * Try to load include directories from compile_commands.json.
  330. * Returns null if no compilation database is found (so the heuristic
  331. * fallback can run). Returns an array (possibly empty) otherwise.
  332. */
  333. function loadCppIncludeDirsFromCompileDB(projectRoot: string): string[] | null {
  334. const candidates = [
  335. path.join(projectRoot, 'compile_commands.json'),
  336. path.join(projectRoot, 'build', 'compile_commands.json'),
  337. path.join(projectRoot, 'cmake-build-debug', 'compile_commands.json'),
  338. path.join(projectRoot, 'cmake-build-release', 'compile_commands.json'),
  339. path.join(projectRoot, 'out', 'compile_commands.json'),
  340. ];
  341. let dbPath: string | undefined;
  342. for (const c of candidates) {
  343. try {
  344. if (fs.existsSync(c)) {
  345. dbPath = c;
  346. break;
  347. }
  348. } catch {
  349. // ignore
  350. }
  351. }
  352. if (!dbPath) return null;
  353. try {
  354. const content = fs.readFileSync(dbPath, 'utf-8');
  355. const entries = JSON.parse(content) as Array<{
  356. directory: string;
  357. command?: string;
  358. arguments?: string[];
  359. }>;
  360. if (!Array.isArray(entries)) return null;
  361. const dirSet = new Set<string>();
  362. for (const entry of entries) {
  363. const dir = entry.directory || projectRoot;
  364. const args = entry.arguments || (entry.command ? shlexSplit(entry.command) : []);
  365. for (let i = 0; i < args.length; i++) {
  366. const arg = args[i]!;
  367. let includeDir: string | undefined;
  368. // -I<dir> (no space)
  369. if (arg.startsWith('-I') && arg.length > 2) {
  370. includeDir = arg.substring(2);
  371. }
  372. // -isystem <dir> (space-separated)
  373. else if ((arg === '-isystem' || arg === '-I') && i + 1 < args.length) {
  374. includeDir = args[i + 1];
  375. i++; // skip next arg
  376. }
  377. if (includeDir) {
  378. // Normalize: resolve relative to the compilation directory
  379. const absPath = path.isAbsolute(includeDir)
  380. ? includeDir
  381. : path.resolve(dir, includeDir);
  382. const relPath = path.relative(projectRoot, absPath).replace(/\\/g, '/');
  383. // Skip system directories and paths outside the project
  384. // (relative paths starting with .. or absolute paths like
  385. // /usr/include or C:\usr on Windows)
  386. if (!relPath.startsWith('..') && relPath.length > 0 && !path.isAbsolute(relPath)) {
  387. dirSet.add(relPath);
  388. }
  389. }
  390. }
  391. }
  392. return Array.from(dirSet);
  393. } catch {
  394. return null;
  395. }
  396. }
  397. /**
  398. * Minimal shlex-style split for compiler command strings.
  399. * Handles double-quoted and single-quoted arguments.
  400. */
  401. function shlexSplit(cmd: string): string[] {
  402. const result: string[] = [];
  403. let i = 0;
  404. while (i < cmd.length) {
  405. // Skip whitespace
  406. while (i < cmd.length && /\s/.test(cmd[i]!)) i++;
  407. if (i >= cmd.length) break;
  408. const ch = cmd[i]!;
  409. if (ch === '"') {
  410. i++;
  411. let arg = '';
  412. while (i < cmd.length && cmd[i] !== '"') {
  413. if (cmd[i] === '\\' && i + 1 < cmd.length) { i++; arg += cmd[i]; }
  414. else { arg += cmd[i]; }
  415. i++;
  416. }
  417. i++; // closing quote
  418. result.push(arg);
  419. } else if (ch === "'") {
  420. i++;
  421. let arg = '';
  422. while (i < cmd.length && cmd[i] !== "'") { arg += cmd[i]; i++; }
  423. i++; // closing quote
  424. result.push(arg);
  425. } else {
  426. let arg = '';
  427. while (i < cmd.length && !/\s/.test(cmd[i]!)) { arg += cmd[i]; i++; }
  428. result.push(arg);
  429. }
  430. }
  431. return result;
  432. }
  433. /**
  434. * Heuristic include directory discovery when no compile_commands.json exists.
  435. * Checks common convention directories and scans top-level dirs for headers.
  436. */
  437. function loadCppIncludeDirsHeuristic(projectRoot: string): string[] {
  438. const dirs: string[] = [];
  439. const conventionDirs = ['include', 'src', 'lib', 'api', 'inc'];
  440. try {
  441. const entries = fs.readdirSync(projectRoot, { withFileTypes: true });
  442. for (const entry of entries) {
  443. if (!entry.isDirectory()) continue;
  444. const name = entry.name;
  445. // Convention directories
  446. if (conventionDirs.includes(name.toLowerCase())) {
  447. dirs.push(name);
  448. continue;
  449. }
  450. // Any top-level directory containing .h or .hpp files
  451. try {
  452. const subFiles = fs.readdirSync(path.join(projectRoot, name));
  453. if (subFiles.some(f => /\.(h|hpp|hxx|hh)$/i.test(f))) {
  454. dirs.push(name);
  455. }
  456. } catch {
  457. // ignore permission errors
  458. }
  459. }
  460. } catch {
  461. // ignore
  462. }
  463. return dirs;
  464. }
  465. /**
  466. * Resolve a C/C++ include path by searching include directories.
  467. * Called as a fallback after relative and aliased resolution fail.
  468. */
  469. function resolveCppIncludePath(
  470. importPath: string,
  471. language: Language,
  472. context: ResolutionContext
  473. ): string | null {
  474. const includeDirs = context.getCppIncludeDirs?.() ?? [];
  475. const extensions = EXTENSION_RESOLUTION[language] ?? [];
  476. for (const dir of includeDirs) {
  477. const normalizedDir = dir.replace(/\\/g, '/');
  478. for (const ext of extensions) {
  479. const candidate = normalizedDir + '/' + importPath + ext;
  480. if (context.fileExists(candidate)) return candidate;
  481. }
  482. // Try as-is (already has extension)
  483. const candidate = normalizedDir + '/' + importPath;
  484. if (context.fileExists(candidate)) return candidate;
  485. }
  486. return null;
  487. }
  488. /**
  489. * Extract import mappings from a file
  490. */
  491. export function extractImportMappings(
  492. _filePath: string,
  493. content: string,
  494. language: Language
  495. ): ImportMapping[] {
  496. const mappings: ImportMapping[] = [];
  497. if (language === 'typescript' || language === 'javascript' || language === 'tsx' || language === 'jsx') {
  498. mappings.push(...extractJSImports(content));
  499. } else if (language === 'svelte' || language === 'vue') {
  500. // Svelte/Vue single-file components import via plain ES6 inside their
  501. // `<script>` block. Without this, a `.svelte`/`.vue` consumer produces
  502. // zero import mappings, so `resolveViaImport` can't run and a barrel
  503. // import (`import { Foo } from './lib'`) falls back to name-matching —
  504. // which silently fails whenever the re-export alias differs from the
  505. // component's real name, yielding a false 0 callers (#629). The ES6
  506. // import regex only matches `import … from '…'`, so running it over the
  507. // whole SFC (markup + styles included) is safe.
  508. mappings.push(...extractJSImports(content));
  509. } else if (language === 'python') {
  510. mappings.push(...extractPythonImports(content));
  511. } else if (language === 'go') {
  512. mappings.push(...extractGoImports(content));
  513. } else if (language === 'java' || language === 'kotlin') {
  514. mappings.push(...extractJavaImports(content));
  515. } else if (language === 'php') {
  516. mappings.push(...extractPHPImports(content));
  517. } else if (language === 'c' || language === 'cpp') {
  518. mappings.push(...extractCppImports(content));
  519. }
  520. return mappings;
  521. }
  522. /**
  523. * Extract JS/TS import mappings
  524. */
  525. function extractJSImports(content: string): ImportMapping[] {
  526. const mappings: ImportMapping[] = [];
  527. // ES6 imports
  528. const importRegex = /import\s+(?:(\w+)\s*,?\s*)?(?:\{([^}]+)\})?\s*(?:(\*)\s+as\s+(\w+))?\s*from\s*['"]([^'"]+)['"]/g;
  529. let match;
  530. while ((match = importRegex.exec(content)) !== null) {
  531. const [, defaultImport, namedImports, star, namespaceAlias, source] = match;
  532. // Default import
  533. if (defaultImport) {
  534. mappings.push({
  535. localName: defaultImport,
  536. exportedName: 'default',
  537. source: source!,
  538. isDefault: true,
  539. isNamespace: false,
  540. });
  541. }
  542. // Named imports
  543. if (namedImports) {
  544. const names = namedImports.split(',').map((s) => s.trim());
  545. for (const name of names) {
  546. const aliasMatch = name.match(/(\w+)\s+as\s+(\w+)/);
  547. if (aliasMatch) {
  548. mappings.push({
  549. localName: aliasMatch[2]!,
  550. exportedName: aliasMatch[1]!,
  551. source: source!,
  552. isDefault: false,
  553. isNamespace: false,
  554. });
  555. } else if (name) {
  556. mappings.push({
  557. localName: name,
  558. exportedName: name,
  559. source: source!,
  560. isDefault: false,
  561. isNamespace: false,
  562. });
  563. }
  564. }
  565. }
  566. // Namespace import
  567. if (star && namespaceAlias) {
  568. mappings.push({
  569. localName: namespaceAlias,
  570. exportedName: '*',
  571. source: source!,
  572. isDefault: false,
  573. isNamespace: true,
  574. });
  575. }
  576. }
  577. // Require statements
  578. const requireRegex = /(?:const|let|var)\s+(?:(\w+)|{([^}]+)})\s*=\s*require\(['"]([^'"]+)['"]\)/g;
  579. while ((match = requireRegex.exec(content)) !== null) {
  580. const [, defaultName, destructured, source] = match;
  581. if (defaultName) {
  582. mappings.push({
  583. localName: defaultName,
  584. exportedName: 'default',
  585. source: source!,
  586. isDefault: true,
  587. isNamespace: false,
  588. });
  589. }
  590. if (destructured) {
  591. const names = destructured.split(',').map((s) => s.trim());
  592. for (const name of names) {
  593. const aliasMatch = name.match(/(\w+)\s*:\s*(\w+)/);
  594. if (aliasMatch) {
  595. mappings.push({
  596. localName: aliasMatch[2]!,
  597. exportedName: aliasMatch[1]!,
  598. source: source!,
  599. isDefault: false,
  600. isNamespace: false,
  601. });
  602. } else if (name) {
  603. mappings.push({
  604. localName: name,
  605. exportedName: name,
  606. source: source!,
  607. isDefault: false,
  608. isNamespace: false,
  609. });
  610. }
  611. }
  612. }
  613. }
  614. return mappings;
  615. }
  616. /**
  617. * Extract Python import mappings
  618. */
  619. function extractPythonImports(content: string): ImportMapping[] {
  620. const mappings: ImportMapping[] = [];
  621. // from X import Y
  622. const fromImportRegex = /from\s+([\w.]+)\s+import\s+([^#\n]+)/g;
  623. let match;
  624. while ((match = fromImportRegex.exec(content)) !== null) {
  625. const [, source, imports] = match;
  626. const names = imports!.split(',').map((s) => s.trim());
  627. for (const name of names) {
  628. const aliasMatch = name.match(/(\w+)\s+as\s+(\w+)/);
  629. if (aliasMatch) {
  630. mappings.push({
  631. localName: aliasMatch[2]!,
  632. exportedName: aliasMatch[1]!,
  633. source: source!,
  634. isDefault: false,
  635. isNamespace: false,
  636. });
  637. } else if (name && name !== '*') {
  638. mappings.push({
  639. localName: name,
  640. exportedName: name,
  641. source: source!,
  642. isDefault: false,
  643. isNamespace: false,
  644. });
  645. }
  646. }
  647. }
  648. // import X
  649. const importRegex = /^import\s+([\w.]+)(?:\s+as\s+(\w+))?/gm;
  650. while ((match = importRegex.exec(content)) !== null) {
  651. const [, source, alias] = match;
  652. const localName = alias || source!.split('.').pop()!;
  653. mappings.push({
  654. localName,
  655. exportedName: '*',
  656. source: source!,
  657. isDefault: false,
  658. isNamespace: true,
  659. });
  660. }
  661. return mappings;
  662. }
  663. /**
  664. * Extract Go import mappings
  665. */
  666. function extractGoImports(content: string): ImportMapping[] {
  667. const mappings: ImportMapping[] = [];
  668. // import "path" or import alias "path"
  669. const singleImportRegex = /import\s+(?:(\w+)\s+)?["']([^"']+)["']/g;
  670. let match;
  671. while ((match = singleImportRegex.exec(content)) !== null) {
  672. const [, alias, source] = match;
  673. const packageName = source!.split('/').pop()!;
  674. mappings.push({
  675. localName: alias || packageName,
  676. exportedName: '*',
  677. source: source!,
  678. isDefault: false,
  679. isNamespace: true,
  680. });
  681. }
  682. // import ( ... ) block
  683. const blockImportRegex = /import\s*\(\s*([^)]+)\s*\)/gs;
  684. while ((match = blockImportRegex.exec(content)) !== null) {
  685. const block = match[1]!;
  686. const lineRegex = /(?:(\w+)\s+)?["']([^"']+)["']/g;
  687. let lineMatch;
  688. while ((lineMatch = lineRegex.exec(block)) !== null) {
  689. const [, alias, source] = lineMatch;
  690. const packageName = source!.split('/').pop()!;
  691. mappings.push({
  692. localName: alias || packageName,
  693. exportedName: '*',
  694. source: source!,
  695. isDefault: false,
  696. isNamespace: true,
  697. });
  698. }
  699. }
  700. return mappings;
  701. }
  702. /**
  703. * Extract Java / Kotlin import mappings.
  704. *
  705. * Java/Kotlin imports carry the full qualified name of the imported
  706. * symbol — `import com.example.dao.converter.FooConverter;` — which is
  707. * exactly the disambiguation signal we need when two packages both
  708. * declare a `FooConverter`. Pre-#314 the resolver had no Java branch
  709. * here at all, so this mapping was empty and cross-module name
  710. * collisions were resolved by file-path proximity (often wrongly).
  711. *
  712. * `import static com.example.Foo.bar;` is parsed as a local-name `bar`
  713. * pointing at FQN `com.example.Foo.bar` so static-method call sites
  714. * (`bar(...)`) can resolve through the same import lookup.
  715. */
  716. function extractJavaImports(content: string): ImportMapping[] {
  717. const mappings: ImportMapping[] = [];
  718. // Strip line and block comments so `// import foo;` doesn't false-match.
  719. const stripped = content
  720. .replace(/\/\*[\s\S]*?\*\//g, '')
  721. .replace(/\/\/[^\n]*/g, '');
  722. // `import [static] <fqn>[.*];`
  723. const re = /^\s*import\s+(static\s+)?([\w.]+(?:\.\*)?)\s*;/gm;
  724. let match: RegExpExecArray | null;
  725. while ((match = re.exec(stripped)) !== null) {
  726. const fqn = match[2]!;
  727. // `import com.example.*;` — wildcard. We can't materialize a single
  728. // local name; skip and let name-matching handle members reachable
  729. // through the wildcard. (Future enhancement: enumerate package files.)
  730. if (fqn.endsWith('.*')) continue;
  731. const parts = fqn.split('.');
  732. const localName = parts[parts.length - 1];
  733. if (!localName) continue;
  734. mappings.push({
  735. localName,
  736. exportedName: localName,
  737. source: fqn,
  738. isDefault: false,
  739. isNamespace: false,
  740. });
  741. }
  742. return mappings;
  743. }
  744. /**
  745. * Extract PHP import mappings (use statements)
  746. */
  747. function extractPHPImports(content: string): ImportMapping[] {
  748. const mappings: ImportMapping[] = [];
  749. // use Namespace\Class; or use Namespace\Class as Alias;
  750. const useRegex = /use\s+([\w\\]+)(?:\s+as\s+(\w+))?;/g;
  751. let match;
  752. while ((match = useRegex.exec(content)) !== null) {
  753. const [, fullPath, alias] = match;
  754. const className = fullPath!.split('\\').pop()!;
  755. mappings.push({
  756. localName: alias || className,
  757. exportedName: className,
  758. source: fullPath!,
  759. isDefault: false,
  760. isNamespace: false,
  761. });
  762. }
  763. return mappings;
  764. }
  765. /**
  766. * Extract C/C++ import mappings from #include directives.
  767. *
  768. * #include brings all symbols from the included header into scope
  769. * (namespace import), so each mapping uses isNamespace: true and
  770. * exportedName: '*'. The localName is set to the header's basename
  771. * without extension so that symbol references like `MyClass` can
  772. * match against any include that might provide it.
  773. */
  774. function extractCppImports(content: string): ImportMapping[] {
  775. const mappings: ImportMapping[] = [];
  776. // Match both #include <...> and #include "..."
  777. const includeRegex = /^\s*#\s*include\s+[<"]([^>"]+)[>"]/gm;
  778. let match;
  779. while ((match = includeRegex.exec(content)) !== null) {
  780. const modulePath = match[1]!;
  781. // Basename without extension for localName matching
  782. const basename = modulePath.split('/').pop()!.replace(/\.(h|hpp|hxx|hh|inl|ipp|cxx|cc|cpp)$/,'');
  783. mappings.push({
  784. localName: basename || modulePath,
  785. exportedName: '*',
  786. source: modulePath,
  787. isDefault: false,
  788. isNamespace: true,
  789. });
  790. }
  791. return mappings;
  792. }
  793. // Cache import mappings per file to avoid re-reading and re-parsing
  794. const importMappingCache = new Map<string, ImportMapping[]>();
  795. /**
  796. * Clear the import mapping cache (call between indexing runs)
  797. */
  798. export function clearImportMappingCache(): void {
  799. importMappingCache.clear();
  800. cppIncludeDirCache.clear();
  801. }
  802. /**
  803. * Strip JS line + block comments from `content` while preserving
  804. * string literals (so `"//"` inside a string stays intact). Used by
  805. * {@link extractReExports} so commented-out export-from statements
  806. * don't generate phantom re-export edges.
  807. *
  808. * Scanner is deliberately small: it only tracks the three contexts
  809. * relevant for JS/TS — single-quote string, double-quote string, and
  810. * template literal. Comment recognition is the JS spec subset, no
  811. * regex-literal awareness (which is fine for our use case: we don't
  812. * apply this to function bodies, only to top-level files).
  813. */
  814. function stripJsComments(content: string): string {
  815. let out = '';
  816. let i = 0;
  817. let str: '"' | "'" | '`' | null = null;
  818. while (i < content.length) {
  819. const ch = content[i]!;
  820. if (str !== null) {
  821. out += ch;
  822. if (ch === '\\' && i + 1 < content.length) {
  823. out += content[i + 1]!;
  824. i += 2;
  825. continue;
  826. }
  827. if (ch === str) str = null;
  828. i++;
  829. continue;
  830. }
  831. if (ch === '"' || ch === "'" || ch === '`') {
  832. str = ch;
  833. out += ch;
  834. i++;
  835. continue;
  836. }
  837. if (ch === '/' && content[i + 1] === '/') {
  838. while (i < content.length && content[i] !== '\n') i++;
  839. continue;
  840. }
  841. if (ch === '/' && content[i + 1] === '*') {
  842. i += 2;
  843. while (i < content.length && !(content[i] === '*' && content[i + 1] === '/')) i++;
  844. i += 2;
  845. continue;
  846. }
  847. out += ch;
  848. i++;
  849. }
  850. return out;
  851. }
  852. /**
  853. * Extract JS/TS re-export declarations from `content`.
  854. *
  855. * Recognised forms:
  856. * export { foo } from './a';
  857. * export { foo as bar } from './a';
  858. * export * from './a';
  859. * export * as ns from './a'; (treated as wildcard for chasing)
  860. * export { default as Foo } from './a';
  861. *
  862. * The walker intentionally stays regex-based — the import-resolver
  863. * elsewhere in this file already chooses regex over a fresh
  864. * tree-sitter pass, and this function shares that trade-off. Errors
  865. * fall through silently; resolution simply skips the broken file.
  866. */
  867. export function extractReExports(content: string, language: Language): ReExport[] {
  868. if (
  869. language !== 'typescript' &&
  870. language !== 'javascript' &&
  871. language !== 'tsx' &&
  872. language !== 'jsx'
  873. ) {
  874. return [];
  875. }
  876. const out: ReExport[] = [];
  877. // Pre-strip block comments + line comments so a commented-out
  878. // `// export { x } from '...'` doesn't produce a phantom edge.
  879. // (Template literals are still a possible source of false positives;
  880. // a project that builds export statements as runtime strings is
  881. // out of scope.)
  882. const cleaned = stripJsComments(content);
  883. // Wildcard: `export * from '...'` or `export * as ns from '...'`
  884. const wildcardRe = /export\s*\*(?:\s+as\s+\w+)?\s*from\s*['"]([^'"]+)['"]/g;
  885. let m: RegExpExecArray | null;
  886. while ((m = wildcardRe.exec(cleaned)) !== null) {
  887. out.push({ kind: 'wildcard', source: m[1]! });
  888. }
  889. // Named: `export { a, b as c } from '...'`
  890. const namedRe = /export\s*\{([^}]+)\}\s*from\s*['"]([^'"]+)['"]/g;
  891. while ((m = namedRe.exec(cleaned)) !== null) {
  892. const inner = m[1]!;
  893. const source = m[2]!;
  894. for (const raw of inner.split(',')) {
  895. const item = raw.trim();
  896. if (!item) continue;
  897. const aliasMatch = item.match(/^(\w+)\s+as\s+(\w+)$/);
  898. if (aliasMatch) {
  899. out.push({
  900. kind: 'named',
  901. exportedName: aliasMatch[2]!,
  902. originalName: aliasMatch[1]!,
  903. source,
  904. });
  905. } else if (/^\w+$/.test(item)) {
  906. out.push({
  907. kind: 'named',
  908. exportedName: item,
  909. originalName: item,
  910. source,
  911. });
  912. }
  913. }
  914. }
  915. return out;
  916. }
  917. /**
  918. * Resolve a reference using import mappings
  919. */
  920. /**
  921. * JVM (Java / Kotlin) imports use fully-qualified names (`import
  922. * com.example.foo.Bar`) decoupled from filenames, so the JS/Python
  923. * style filesystem path lookup misses them whenever the file isn't
  924. * named after its primary symbol (Kotlin `Utils.kt` exporting `Bar`,
  925. * top-level fns, extension fns). Resolve them through the
  926. * `qualifiedName` index instead — populated by the package_header /
  927. * package_declaration namespace wrappers in the extractor.
  928. */
  929. export function resolveJvmImport(
  930. ref: UnresolvedRef,
  931. context: ResolutionContext
  932. ): ResolvedRef | null {
  933. if (ref.referenceKind !== 'imports') return null;
  934. if (ref.language !== 'java' && ref.language !== 'kotlin') return null;
  935. const fqn = ref.referenceName;
  936. const lastDot = fqn.lastIndexOf('.');
  937. if (lastDot <= 0) return null;
  938. const pkg = fqn.substring(0, lastDot);
  939. const sym = fqn.substring(lastDot + 1);
  940. // Wildcard imports (`com.example.*`) deliberately punt to name-matcher.
  941. if (sym === '*') return null;
  942. const candidates = context.getNodesByQualifiedName(`${pkg}::${sym}`);
  943. if (candidates.length === 0) return null;
  944. // Kotlin Multiplatform: an `expect` declaration and its `actual`s share one
  945. // FQN across source sets (commonMain / androidMain / appleMain). Taking the
  946. // first candidate let a single platform `actual` absorb every common-side
  947. // import, so the `expect` (the canonical API a commonMain file imports)
  948. // looked unused. Prefer the candidate CLOSEST to the importing file by
  949. // directory proximity — a commonMain import resolves to the commonMain
  950. // declaration — with the `expect` side as a tiebreak.
  951. const best = candidates.length === 1 ? candidates[0]! : pickClosestJvmCandidate(candidates, ref.filePath);
  952. return {
  953. original: ref,
  954. targetNodeId: best.id,
  955. confidence: 0.95,
  956. resolvedBy: 'import',
  957. };
  958. }
  959. /**
  960. * Pick the same-FQN candidate closest to `fromPath` by shared directory
  961. * prefix, preferring an `expect` declaration on a tie. Used to keep a Kotlin
  962. * Multiplatform `expect`/`actual` import resolving within the importer's own
  963. * source set instead of an arbitrary platform `actual`.
  964. */
  965. function pickClosestJvmCandidate(candidates: Node[], fromPath: string): Node {
  966. const fromDirs = fromPath.split('/').slice(0, -1);
  967. const sharedPrefix = (p: string): number => {
  968. const d = p.split('/').slice(0, -1);
  969. let shared = 0;
  970. for (let i = 0; i < Math.min(fromDirs.length, d.length); i++) {
  971. if (fromDirs[i] === d[i]) shared++;
  972. else break;
  973. }
  974. return shared;
  975. };
  976. const isExpect = (n: Node): boolean => Array.isArray(n.decorators) && n.decorators.includes('expect');
  977. let best = candidates[0]!;
  978. let bestProx = sharedPrefix(best.filePath);
  979. for (let i = 1; i < candidates.length; i++) {
  980. const c = candidates[i]!;
  981. const prox = sharedPrefix(c.filePath);
  982. if (prox > bestProx || (prox === bestProx && isExpect(c) && !isExpect(best))) {
  983. best = c;
  984. bestProx = prox;
  985. }
  986. }
  987. return best;
  988. }
  989. export function resolveViaImport(
  990. ref: UnresolvedRef,
  991. context: ResolutionContext
  992. ): ResolvedRef | null {
  993. // C/C++ #include references — resolve directly to the included file
  994. // (file→file edge), bypassing symbol lookup. The extractor emits these
  995. // with `referenceKind: 'imports'` and `referenceName: <include path>`
  996. // (e.g. "uint256.h" or "common/args.h"). Without this branch the
  997. // include-dir scan path inside resolveImportPath never produces an
  998. // edge — resolveViaImport's symbol lookup below would search the
  999. // resolved file for a symbol named like the file extension and fail.
  1000. if ((ref.language === 'c' || ref.language === 'cpp') && ref.referenceKind === 'imports') {
  1001. // C/C++ quoted includes (`#include "X.h"`) resolve relative to the
  1002. // INCLUDING file's own directory first (the C standard's quoted-include
  1003. // search order). Prefer a same-directory header over an -I directory or a
  1004. // same-named header on another platform (windows/code/RNCAsyncStorage.h vs
  1005. // apple/.../RNCAsyncStorage.h) — the include-dir heuristic below would
  1006. // otherwise pick an arbitrary same-named header, leaving the real local one
  1007. // with no dependents.
  1008. const slash = ref.filePath.lastIndexOf('/');
  1009. const fromDir = slash >= 0 ? ref.filePath.slice(0, slash) : '';
  1010. const siblingPath = path.posix.normalize(fromDir ? `${fromDir}/${ref.referenceName}` : ref.referenceName);
  1011. const siblingBase = siblingPath.split('/').pop()!;
  1012. const sibling = context
  1013. .getNodesByName(siblingBase)
  1014. .find((n) => n.kind === 'file' && n.filePath === siblingPath);
  1015. if (sibling) {
  1016. return { original: ref, targetNodeId: sibling.id, confidence: 0.92, resolvedBy: 'import' };
  1017. }
  1018. const resolvedPath = resolveImportPath(ref.referenceName, ref.filePath, ref.language, context);
  1019. if (!resolvedPath) return null;
  1020. const basename = resolvedPath.split('/').pop()!;
  1021. const fileNodes = context.getNodesByName(basename).filter((n) => n.kind === 'file');
  1022. const fileNode = fileNodes.find((n) => n.filePath === resolvedPath);
  1023. if (fileNode) {
  1024. return {
  1025. original: ref,
  1026. targetNodeId: fileNode.id,
  1027. confidence: 0.9,
  1028. resolvedBy: 'import',
  1029. };
  1030. }
  1031. return null;
  1032. }
  1033. // Use cached import mappings (avoids re-reading and re-parsing per ref)
  1034. const imports = context.getImportMappings(ref.filePath, ref.language);
  1035. if (imports.length === 0 && !context.readFile(ref.filePath)) {
  1036. return null;
  1037. }
  1038. // Go cross-package calls: `pkga.FuncX(...)` extracts to referenceName
  1039. // `pkga.FuncX` and the import `github.com/example/myproject/pkga`
  1040. // maps to a *package directory* containing one or more .go files.
  1041. // The generic file-based lookup below can't follow that — issue #388.
  1042. if (ref.language === 'go') {
  1043. const goResult = resolveGoCrossPackageReference(ref, imports, context);
  1044. if (goResult) return goResult;
  1045. }
  1046. // Java / Kotlin: imports are FQNs (`import com.example.Foo;`) — no
  1047. // resolvable file path the JS/TS-style chain below could follow. Look
  1048. // up the symbol by name and filter to the candidate whose file path
  1049. // matches the imported FQN. This is the disambiguation signal that
  1050. // breaks the same-name class collision the path-proximity matcher
  1051. // can't resolve (issue #314).
  1052. if (ref.language === 'java' || ref.language === 'kotlin') {
  1053. const javaResult = resolveJavaImportedReference(ref, imports, context);
  1054. if (javaResult) return javaResult;
  1055. }
  1056. // Python qualified access through an imported MODULE: `certs.where()` after
  1057. // `from . import certs`, `mod.func()` after `import mod`. The receiver names a
  1058. // submodule (a file), not a symbol, so the generic symbol lookup below would
  1059. // search the *package* for `certs` instead of looking inside the module.
  1060. if (ref.language === 'python') {
  1061. const pyResult = resolvePythonModuleMember(ref, imports, context);
  1062. if (pyResult) return pyResult;
  1063. // Absolute dotted module import: `import conduit.apps.articles.signals`
  1064. // (the standard Django AppConfig.ready() signal-registration pattern, and
  1065. // any side-effect `import pkg.mod`). Map the dotted path to its file.
  1066. const pyModResult = resolvePythonAbsoluteModule(ref, context);
  1067. if (pyModResult) return pyModResult;
  1068. }
  1069. // Rust qualified path: resolve the module prefix of `crate::m::Item` /
  1070. // `self::sub::Item` / `super::m::func` to a file, then find the leaf symbol in
  1071. // it. Disambiguates common-name `pub use self::read::read` re-exports that
  1072. // name-matching would land on the wrong same-named symbol.
  1073. if (ref.language === 'rust' && ref.referenceName.includes('::')) {
  1074. const rustResult = resolveRustPathReference(ref, context);
  1075. if (rustResult) return rustResult;
  1076. }
  1077. // Lua / Luau `require(...)`: a dotted module path (`a.b.c` from
  1078. // `require("a.b.c")`) or an instance-path leaf (`Signal` from
  1079. // `require(script.Parent.Signal)`) — map it to a module file. There's no static
  1080. // import statement, so the generic path-matcher can't bridge the dot↔slash /
  1081. // leaf↔basename gap; resolve it explicitly to the module file.
  1082. if ((ref.language === 'lua' || ref.language === 'luau') && ref.referenceKind === 'imports') {
  1083. const luaResult = resolveLuaRequire(ref, context);
  1084. if (luaResult) return luaResult;
  1085. }
  1086. // Whole-module / namespace imports → link the importing file to the module
  1087. // file. Python `from . import certs` / `import mod`, and TS/JS `import * as ns
  1088. // from './x'` (so a namespace touched only via a value-member read still
  1089. // records the dependency). A named TS/JS import returns null here and falls
  1090. // through to symbol resolution below.
  1091. if (
  1092. ref.language === 'python' ||
  1093. ref.language === 'typescript' ||
  1094. ref.language === 'tsx' ||
  1095. ref.language === 'javascript' ||
  1096. ref.language === 'jsx'
  1097. ) {
  1098. const moduleFile = resolveModuleImportToFile(ref, imports, context);
  1099. if (moduleFile) return moduleFile;
  1100. }
  1101. // Check if the reference name matches any import
  1102. for (const imp of imports) {
  1103. if (imp.localName === ref.referenceName || ref.referenceName.startsWith(imp.localName + '.')) {
  1104. // Resolve the import path
  1105. const resolvedPath = resolveImportPath(
  1106. imp.source,
  1107. ref.filePath,
  1108. ref.language,
  1109. context
  1110. );
  1111. if (resolvedPath) {
  1112. const exportedName = imp.isDefault ? 'default' : imp.exportedName;
  1113. const memberName = imp.isNamespace
  1114. ? ref.referenceName.replace(imp.localName + '.', '')
  1115. : null;
  1116. const targetNode = findExportedSymbol(
  1117. resolvedPath,
  1118. { isDefault: imp.isDefault, isNamespace: imp.isNamespace, exportedName, memberName },
  1119. ref.language,
  1120. context,
  1121. new Set()
  1122. );
  1123. if (targetNode) {
  1124. return {
  1125. original: ref,
  1126. targetNodeId: targetNode.id,
  1127. confidence: 0.9,
  1128. resolvedBy: 'import',
  1129. };
  1130. }
  1131. }
  1132. }
  1133. }
  1134. return null;
  1135. }
  1136. /**
  1137. * Resolve a Python qualified reference whose receiver is an imported MODULE:
  1138. * `certs.where()` after `from . import certs`, `mod.func()` after `import mod`
  1139. * or `from pkg import mod`. The receiver names a submodule (a file), not a
  1140. * symbol, so the generic symbol lookup in `resolveViaImport` can't follow it —
  1141. * it would search the *package* for `certs`/`mod` instead of looking inside the
  1142. * module. This is the Python half of the cross-package qualified-call problem
  1143. * (cf. `resolveGoCrossPackageReference` for Go's `pkg.Func`, issue #388).
  1144. *
  1145. * Builds the module's dotted import path from the binding — `from . import
  1146. * certs` → `.certs`; `from pkg import mod` → `pkg.mod`; `import mod` → `mod` —
  1147. * resolves it to the module file, and finds the member defined there. Returns
  1148. * null when no module file exists at that path, so attribute access on an
  1149. * imported *value* (`helper.attr` where `helper` is a function) falls through
  1150. * to the other strategies untouched.
  1151. */
  1152. function resolvePythonModuleMember(
  1153. ref: UnresolvedRef,
  1154. imports: ImportMapping[],
  1155. context: ResolutionContext
  1156. ): ResolvedRef | null {
  1157. const dotIdx = ref.referenceName.indexOf('.');
  1158. if (dotIdx <= 0) return null;
  1159. const receiver = ref.referenceName.substring(0, dotIdx);
  1160. // The immediate member of the module (first segment after the receiver).
  1161. const member = ref.referenceName.substring(dotIdx + 1).split('.')[0];
  1162. if (!member) return null;
  1163. for (const imp of imports) {
  1164. if (imp.localName !== receiver) continue;
  1165. // `import mod` / `import numpy as np` bind the module at `source` itself;
  1166. // `from . import certs` / `from pkg import mod` bind a SUBMODULE whose
  1167. // dotted path is the source joined with the imported name.
  1168. const modulePath = imp.isNamespace
  1169. ? imp.source
  1170. : imp.source.endsWith('.')
  1171. ? imp.source + imp.localName
  1172. : imp.source + '.' + imp.localName;
  1173. const resolvedPath = resolveImportPath(modulePath, ref.filePath, ref.language, context);
  1174. if (!resolvedPath || resolvedPath === ref.filePath) continue;
  1175. // Find the member as a top-level definition in the module file. Exclude
  1176. // `method` so `mod.foo` never lands on a same-named class method.
  1177. const target = context.getNodesInFile(resolvedPath).find(
  1178. (n) =>
  1179. n.name === member &&
  1180. (n.kind === 'function' ||
  1181. n.kind === 'class' ||
  1182. n.kind === 'variable' ||
  1183. n.kind === 'constant')
  1184. );
  1185. if (target) {
  1186. return { original: ref, targetNodeId: target.id, confidence: 0.85, resolvedBy: 'import' };
  1187. }
  1188. }
  1189. return null;
  1190. }
  1191. /**
  1192. * Resolve a whole-MODULE import to that module's file (a file→file dependency).
  1193. * The imported name is a module, not a symbol, so there's nothing to resolve to
  1194. * — but importing a module IS a dependency on it. Covers:
  1195. * - Python submodule imports — `from . import certs`, `from pkg import sub`;
  1196. * - namespace imports — Python `import mod` / `import numpy as np`, and
  1197. * TS/JS `import * as ns from './x'`.
  1198. *
  1199. * It is also the robust backstop for {@link resolvePythonModuleMember} and for
  1200. * TS namespace usage: it records the dependency even when the used member is
  1201. * re-exported elsewhere (requests' `certs.where`, re-exported from `certifi`),
  1202. * the usage is module-level code that isn't extracted as a call, or a TS
  1203. * namespace is touched only via a value-member read (`ns.SOME_CONST`).
  1204. *
  1205. * Only fires for dot-free `imports`-kind refs whose module path resolves to a
  1206. * real file. A NAMED TS/JS import (`import { widget }`) is not a module, so it
  1207. * returns null and normal symbol resolution handles it.
  1208. */
  1209. /**
  1210. * Resolve a Lua/Luau `require(...)` to its module file. The reference name is
  1211. * either a dotted module path (`telescope.config` → `telescope/config.lua`) or a
  1212. * Roblox instance-path leaf (`Signal` from `require(script.Parent.Signal)` →
  1213. * `Signal.luau`). We try `<path>.lua|.luau` and `<path>/init.lua|.luau`, matched
  1214. * by path suffix (the module root — `lua/`, `src/`, … — is project-specific).
  1215. * Among suffix matches, the one sharing the longest directory prefix with the
  1216. * requiring file wins (instance-path requires resolve within the same package).
  1217. */
  1218. function resolveLuaRequire(ref: UnresolvedRef, context: ResolutionContext): ResolvedRef | null {
  1219. const name = ref.referenceName;
  1220. if (!name) return null;
  1221. const base = name.includes('.') ? name.replace(/\./g, '/') : name;
  1222. const suffixes = [`${base}.lua`, `${base}.luau`, `${base}/init.lua`, `${base}/init.luau`];
  1223. const files = context.getAllFiles();
  1224. const shared = (a: string, b: string): number => {
  1225. let i = 0;
  1226. while (i < a.length && i < b.length && a[i] === b[i]) i++;
  1227. return i;
  1228. };
  1229. for (const suffix of suffixes) {
  1230. const matches = files.filter((f) => f === suffix || f.endsWith('/' + suffix));
  1231. if (matches.length === 0) continue;
  1232. matches.sort((x, y) => shared(y, ref.filePath) - shared(x, ref.filePath));
  1233. const best = matches[0]!;
  1234. if (best === ref.filePath) continue;
  1235. const fileNode = context.getNodesInFile(best).find((n) => n.kind === 'file');
  1236. if (fileNode) {
  1237. // Confidence ≥ 0.9 so this deterministic path/suffix match wins over
  1238. // name-matching, which otherwise resolves the require to the import node
  1239. // itself (a same-name self-match).
  1240. return { original: ref, targetNodeId: fileNode.id, confidence: 0.9, resolvedBy: 'import' };
  1241. }
  1242. }
  1243. return null;
  1244. }
  1245. function resolveModuleImportToFile(
  1246. ref: UnresolvedRef,
  1247. imports: ImportMapping[],
  1248. context: ResolutionContext
  1249. ): ResolvedRef | null {
  1250. if (ref.referenceKind !== 'imports') return null;
  1251. if (ref.referenceName.includes('.')) return null;
  1252. for (const imp of imports) {
  1253. if (imp.localName !== ref.referenceName) continue;
  1254. let modulePath: string;
  1255. if (imp.isNamespace || imp.isDefault) {
  1256. // `import * as ns from './x'` (namespace) or `import x from './x'`
  1257. // (default) — the dependency is on the MODULE FILE. A default import binds
  1258. // a (possibly renamed) local to whatever the module's default export is
  1259. // (`import articlesController from './article.controller'` ← `export
  1260. // default router`), so the binding name can't be found as a symbol — link
  1261. // the file the import resolves to instead. External modules don't resolve
  1262. // (no file), so `import React from 'react'` creates no edge.
  1263. modulePath = imp.source;
  1264. } else if (ref.language === 'python') {
  1265. // `from . import certs` — the imported NAME is a submodule of the source.
  1266. modulePath = imp.source.endsWith('.')
  1267. ? imp.source + imp.localName
  1268. : imp.source + '.' + imp.localName;
  1269. } else {
  1270. // A named TS/JS import binds a symbol, not a module — leave it alone.
  1271. continue;
  1272. }
  1273. const resolvedPath = resolveImportPath(modulePath, ref.filePath, ref.language, context);
  1274. if (resolvedPath && resolvedPath !== ref.filePath) {
  1275. const fileNode = context.getNodesInFile(resolvedPath).find((n) => n.kind === 'file');
  1276. if (fileNode) {
  1277. return { original: ref, targetNodeId: fileNode.id, confidence: 0.9, resolvedBy: 'import' };
  1278. }
  1279. }
  1280. // Python absolute `from a.b import submodule` (a FastAPI router aggregator's
  1281. // `from app.api.routes import authentication`): resolveImportPath only maps
  1282. // RELATIVE dotted paths to a file, so resolve the absolute dotted module
  1283. // directly to its file node.
  1284. if (ref.language === 'python') {
  1285. const modFile = findPythonModuleFile(modulePath, context, ref.filePath);
  1286. if (modFile) {
  1287. return { original: ref, targetNodeId: modFile.id, confidence: 0.9, resolvedBy: 'import' };
  1288. }
  1289. }
  1290. }
  1291. return null;
  1292. }
  1293. /**
  1294. * Find the file node for a Python dotted module path `a.b.c` — a module file
  1295. * ending in `a/b/c.py`, or a package `a/b/c/__init__.py` (suffix-matched, so a
  1296. * package rooted under `src/` etc. still resolves). Returns null for
  1297. * stdlib/external modules (no matching repo file node), so `import os` creates
  1298. * no edge. Shared by absolute `import a.b.c` and absolute `from a.b import c`
  1299. * (where `c` is a submodule) resolution.
  1300. */
  1301. function findPythonModuleFile(
  1302. mod: string,
  1303. context: ResolutionContext,
  1304. excludeFilePath: string
  1305. ): Node | null {
  1306. if (!mod || mod.startsWith('.')) return null; // relative imports handled elsewhere
  1307. const rel = mod.replace(/\./g, '/');
  1308. const lastSeg = mod.split('.').pop()!;
  1309. const endsWith = (p: string, want: string): boolean => p === want || p.endsWith('/' + want);
  1310. const moduleFile = context
  1311. .getNodesByName(`${lastSeg}.py`)
  1312. .find((n) => n.kind === 'file' && n.filePath !== excludeFilePath && endsWith(n.filePath, `${rel}.py`));
  1313. if (moduleFile) return moduleFile;
  1314. const pkgFile = context
  1315. .getNodesByName('__init__.py')
  1316. .find((n) => n.kind === 'file' && n.filePath !== excludeFilePath && endsWith(n.filePath, `${rel}/__init__.py`));
  1317. return pkgFile ?? null;
  1318. }
  1319. /**
  1320. * Resolve a Python ABSOLUTE dotted module import (`import a.b.c`) to its file —
  1321. * the Django `AppConfig.ready(): import myapp.signals` pattern and any
  1322. * side-effect module import.
  1323. */
  1324. function resolvePythonAbsoluteModule(
  1325. ref: UnresolvedRef,
  1326. context: ResolutionContext
  1327. ): ResolvedRef | null {
  1328. if (ref.referenceKind !== 'imports') return null;
  1329. // Only a DOTTED `import a.b.c` ref carries its full module path. A bare leaf
  1330. // (`from app.api.routes import authentication`) is ambiguous on its own — three
  1331. // `authentication.py` files may exist — so leave it to resolveModuleImportToFile,
  1332. // which uses the import's source (`app.api.routes`) to build the full path.
  1333. if (!ref.referenceName.includes('.')) return null;
  1334. const hit = findPythonModuleFile(ref.referenceName, context, ref.filePath);
  1335. return hit ? { original: ref, targetNodeId: hit.id, confidence: 0.9, resolvedBy: 'import' } : null;
  1336. }
  1337. /**
  1338. * Resolve a Rust qualified reference `A::B::C` by mapping the MODULE prefix
  1339. * (`A::B`) to a file and finding the leaf symbol (`C`) in it. This is the Rust
  1340. * analog of {@link resolvePythonModuleMember} / {@link resolveGoCrossPackageReference}
  1341. * and the precise answer to common-name re-exports (`pub use self::read::read`)
  1342. * that name-matching can't disambiguate. Returns null when the prefix isn't a
  1343. * real module path (e.g. `Widget::new` — `Widget` is a struct, not a module),
  1344. * so associated-function calls and enum-variant paths fall through untouched.
  1345. */
  1346. function resolveRustPathReference(
  1347. ref: UnresolvedRef,
  1348. context: ResolutionContext
  1349. ): ResolvedRef | null {
  1350. const segments = ref.referenceName.split('::').filter((s) => s.length > 0);
  1351. if (segments.length < 2) return null;
  1352. const leaf = segments[segments.length - 1]!;
  1353. const modSegs = segments.slice(0, -1);
  1354. const file = resolveRustModuleFile(modSegs, ref.filePath, context);
  1355. if (!file || file === ref.filePath) return null;
  1356. const target = context.getNodesInFile(file).find(
  1357. (n) =>
  1358. n.name === leaf &&
  1359. (n.kind === 'function' ||
  1360. n.kind === 'struct' ||
  1361. n.kind === 'enum' ||
  1362. n.kind === 'trait' ||
  1363. n.kind === 'type_alias' ||
  1364. n.kind === 'constant' ||
  1365. n.kind === 'method' ||
  1366. n.kind === 'class' ||
  1367. n.kind === 'interface')
  1368. );
  1369. if (target) {
  1370. return { original: ref, targetNodeId: target.id, confidence: 0.9, resolvedBy: 'import' };
  1371. }
  1372. return null;
  1373. }
  1374. /** The crate-root directory (holds `lib.rs`/`main.rs`), walking up from a file. */
  1375. function rustCrateRootDir(fromFileAbs: string, context: ResolutionContext): string | null {
  1376. const projectRoot = context.getProjectRoot();
  1377. const toRel = (p: string) => path.relative(projectRoot, p).replace(/\\/g, '/');
  1378. let dir = path.dirname(fromFileAbs);
  1379. for (let i = 0; i < 64; i++) {
  1380. if (context.fileExists(toRel(path.join(dir, 'lib.rs'))) ||
  1381. context.fileExists(toRel(path.join(dir, 'main.rs')))) {
  1382. return dir;
  1383. }
  1384. const parent = path.dirname(dir);
  1385. if (parent === dir) return null;
  1386. dir = parent;
  1387. }
  1388. return null;
  1389. }
  1390. /** Directory under which the current file's module declares its SUBMODULES. */
  1391. function rustSelfModuleDir(fromFileAbs: string): string {
  1392. const base = path.basename(fromFileAbs);
  1393. const dir = path.dirname(fromFileAbs);
  1394. // mod.rs / lib.rs / main.rs own their directory; `foo.rs`'s submodules live in `foo/`.
  1395. if (base === 'mod.rs' || base === 'lib.rs' || base === 'main.rs') return dir;
  1396. return path.join(dir, base.replace(/\.rs$/, ''));
  1397. }
  1398. /**
  1399. * Resolve a Rust module path (segments WITHOUT the leaf symbol) to the file of
  1400. * the last module segment — `crate::a::b` → `<crate>/a/b.rs` (or `.../b/mod.rs`).
  1401. * Anchors on `crate` / `self` / `super`; a bare path is tried crate-relative.
  1402. */
  1403. function resolveRustModuleFile(
  1404. segments: string[],
  1405. fromFile: string,
  1406. context: ResolutionContext
  1407. ): string | null {
  1408. if (segments.length === 0) return null;
  1409. const projectRoot = context.getProjectRoot();
  1410. const fromAbs = path.join(projectRoot, fromFile);
  1411. const toRel = (p: string) => path.relative(projectRoot, p).replace(/\\/g, '/');
  1412. // Walk a sequence of module segments down from `startDir`, mapping each to a
  1413. // `<seg>.rs` or `<seg>/mod.rs` file. Returns the leaf module's file, or null
  1414. // if `startDir` is null or any segment has no file on disk.
  1415. const resolveUnder = (startDir: string | null, rest: string[]): string | null => {
  1416. if (!startDir) return null;
  1417. let dir = startDir;
  1418. let targetFile: string | null = null;
  1419. for (const seg of rest) {
  1420. if (seg === 'self' || seg === 'crate' || seg === 'super') continue;
  1421. const asFile = toRel(path.join(dir, seg + '.rs'));
  1422. const asMod = toRel(path.join(dir, seg, 'mod.rs'));
  1423. if (context.fileExists(asFile)) targetFile = asFile;
  1424. else if (context.fileExists(asMod)) targetFile = asMod;
  1425. else return null;
  1426. dir = path.join(dir, seg);
  1427. }
  1428. return targetFile;
  1429. };
  1430. const first = segments[0]!;
  1431. if (first === 'crate') {
  1432. return resolveUnder(rustCrateRootDir(fromAbs, context), segments.slice(1));
  1433. }
  1434. if (first === 'self') {
  1435. return resolveUnder(rustSelfModuleDir(fromAbs), segments.slice(1));
  1436. }
  1437. if (first === 'super') {
  1438. let supers = 0;
  1439. while (segments[supers] === 'super') supers++;
  1440. let dir: string | null = rustSelfModuleDir(fromAbs);
  1441. for (let s = 0; s < supers && dir; s++) dir = path.dirname(dir);
  1442. return resolveUnder(dir, segments.slice(supers));
  1443. }
  1444. // Bare path. In expression position (`submodule::item()` — the router-assembly
  1445. // and general cross-module-call pattern) the prefix is a SUBMODULE of the
  1446. // current module, i.e. 2018 `self::`-relative — so try self-relative FIRST.
  1447. // Fall back to crate-relative for 2015-edition / crate-root items. External
  1448. // crate paths (`serde::de::Error`) miss both and fall through to name-matching.
  1449. return (
  1450. resolveUnder(rustSelfModuleDir(fromAbs), segments) ??
  1451. resolveUnder(rustCrateRootDir(fromAbs, context), segments)
  1452. );
  1453. }
  1454. /**
  1455. * Resolve a Java/Kotlin reference whose receiver is the simple name of
  1456. * an imported FQN: `Foo.bar(...)` where `import com.example.Foo;`. The
  1457. * imported FQN converts to a file-path suffix (`com/example/Foo.java`
  1458. * or `.kt`) which uniquely identifies the right symbol when multiple
  1459. * classes share the same simple name.
  1460. *
  1461. * Also handles bare references to the imported class itself
  1462. * (`new Foo()` extraction emits `Foo` as a `references`/`instantiates`
  1463. * ref) and `import static <Foo>.bar` style imports of a single member.
  1464. */
  1465. function resolveJavaImportedReference(
  1466. ref: UnresolvedRef,
  1467. imports: ImportMapping[],
  1468. context: ResolutionContext
  1469. ): ResolvedRef | null {
  1470. if (imports.length === 0) return null;
  1471. const ext = ref.language === 'kotlin' ? '.kt' : '.java';
  1472. for (const imp of imports) {
  1473. const matchesBare = imp.localName === ref.referenceName;
  1474. const matchesQualified = ref.referenceName.startsWith(imp.localName + '.');
  1475. if (!matchesBare && !matchesQualified) continue;
  1476. // Convert FQN to a file-path suffix. `com.example.Foo` ->
  1477. // `com/example/Foo.java` (or `.kt`). The actual file may live
  1478. // under any source root (`src/main/java/`, `src/`, etc.), so match
  1479. // by suffix rather than exact path.
  1480. const fqnPath = imp.source.replace(/\./g, '/') + ext;
  1481. // Which symbol name to look up: the class itself, or a member.
  1482. const memberName = matchesBare
  1483. ? imp.localName
  1484. : ref.referenceName.substring(imp.localName.length + 1);
  1485. const candidates = context.getNodesByName(memberName);
  1486. for (const node of candidates) {
  1487. if (node.language !== ref.language) continue;
  1488. const fp = node.filePath.replace(/\\/g, '/');
  1489. if (fp.endsWith(fqnPath) || fp.endsWith('/' + fqnPath)) {
  1490. return {
  1491. original: ref,
  1492. targetNodeId: node.id,
  1493. confidence: 0.9,
  1494. resolvedBy: 'import',
  1495. };
  1496. }
  1497. }
  1498. // `import static com.example.Foo.bar;` — the FQN's tail is the
  1499. // member name, the part before is the owner class. Look up the
  1500. // member named `<imp.localName>` (e.g. `bar`) and prefer the
  1501. // candidate whose file matches the parent FQN's path.
  1502. if (matchesBare) {
  1503. const dot = imp.source.lastIndexOf('.');
  1504. if (dot > 0) {
  1505. const ownerFqn = imp.source.substring(0, dot);
  1506. const ownerPath = ownerFqn.replace(/\./g, '/') + ext;
  1507. for (const node of candidates) {
  1508. if (node.language !== ref.language) continue;
  1509. const fp = node.filePath.replace(/\\/g, '/');
  1510. if (fp.endsWith(ownerPath) || fp.endsWith('/' + ownerPath)) {
  1511. return {
  1512. original: ref,
  1513. targetNodeId: node.id,
  1514. confidence: 0.9,
  1515. resolvedBy: 'import',
  1516. };
  1517. }
  1518. }
  1519. }
  1520. }
  1521. }
  1522. return null;
  1523. }
  1524. /**
  1525. * Resolve a Go cross-package qualified reference (`pkga.FuncX`) by matching
  1526. * the package alias against an in-module import, stripping the module prefix
  1527. * to a project-relative directory, and locating the exported symbol in any
  1528. * `.go` file under that directory. Returns `null` for stdlib / third-party
  1529. * imports (no `go.mod`-relative match) so the rest of `resolveViaImport`
  1530. * can still try the file-based path.
  1531. */
  1532. function resolveGoCrossPackageReference(
  1533. ref: UnresolvedRef,
  1534. imports: ImportMapping[],
  1535. context: ResolutionContext
  1536. ): ResolvedRef | null {
  1537. const mod = context.getGoModule?.();
  1538. if (!mod) return null;
  1539. // Qualified call: receiver before `.`, member after. A bare reference
  1540. // (no dot) is a same-file/in-package call — handled elsewhere.
  1541. const dotIdx = ref.referenceName.indexOf('.');
  1542. if (dotIdx <= 0) return null;
  1543. const receiver = ref.referenceName.substring(0, dotIdx);
  1544. const memberName = ref.referenceName.substring(dotIdx + 1);
  1545. if (!memberName) return null;
  1546. for (const imp of imports) {
  1547. if (imp.localName !== receiver) continue;
  1548. // Only in-module imports map to a known directory.
  1549. if (imp.source !== mod.modulePath && !imp.source.startsWith(mod.modulePath + '/')) {
  1550. continue;
  1551. }
  1552. const pkgDir = imp.source === mod.modulePath
  1553. ? ''
  1554. : imp.source.substring(mod.modulePath.length + 1);
  1555. // Look up the member by name and pick the candidate whose file lives
  1556. // directly in the package directory. Match the immediate parent dir
  1557. // exactly so a call to `pkga.FuncX` doesn't accidentally land on a
  1558. // `FuncX` declared in `pkga/subpkg/`.
  1559. const candidates = context.getNodesByName(memberName);
  1560. for (const node of candidates) {
  1561. if (node.language !== 'go') continue;
  1562. if (!node.isExported) continue;
  1563. const fp = node.filePath.replace(/\\/g, '/');
  1564. const lastSlash = fp.lastIndexOf('/');
  1565. const fileDir = lastSlash >= 0 ? fp.substring(0, lastSlash) : '';
  1566. if (fileDir === pkgDir) {
  1567. return {
  1568. original: ref,
  1569. targetNodeId: node.id,
  1570. confidence: 0.9,
  1571. resolvedBy: 'import',
  1572. };
  1573. }
  1574. }
  1575. }
  1576. return null;
  1577. }
  1578. /** Recursive depth cap for re-export chain following. Real codebases
  1579. * rarely chain barrels more than 2–3 deep; 8 is a generous safety
  1580. * net that still bounds worst-case work. */
  1581. const REEXPORT_MAX_DEPTH = 8;
  1582. /**
  1583. * Find an exported symbol in `filePath`, following `export { x } from
  1584. * './other'` and `export * from './other'` chains until the original
  1585. * declaration is reached. Cycle-safe via the `visited` set.
  1586. *
  1587. * Without this, every barrel-style import (`import { Foo } from
  1588. * './index'` where `index.ts` only re-exports) used to resolve to
  1589. * nothing — the existing code only looked for declarations IN the
  1590. * resolved file, not declarations the file forwarded.
  1591. */
  1592. function findExportedSymbol(
  1593. filePath: string,
  1594. want: {
  1595. isDefault: boolean;
  1596. isNamespace: boolean;
  1597. exportedName: string;
  1598. memberName: string | null;
  1599. },
  1600. language: Language,
  1601. context: ResolutionContext,
  1602. visited: Set<string>,
  1603. depth = 0
  1604. ): Node | undefined {
  1605. if (depth > REEXPORT_MAX_DEPTH) return undefined;
  1606. if (visited.has(filePath)) return undefined;
  1607. visited.add(filePath);
  1608. const nodesInFile = context.getNodesInFile(filePath);
  1609. // 1. Direct hit: the symbol is declared in this file.
  1610. if (want.isDefault) {
  1611. // Svelte/Vue single-file components ARE the module's default export,
  1612. // but are extracted as kind 'component' (not function/class). Prefer
  1613. // the component node; fall back to an exported function/class for the
  1614. // `.ts`/`.tsx` `export default fn`/`class` case. Without the component
  1615. // branch, an `export { default as X } from './X.svelte'` barrel never
  1616. // resolves and the component shows a false 0 callers (#629).
  1617. const direct =
  1618. nodesInFile.find((n) => n.isExported && n.kind === 'component') ??
  1619. nodesInFile.find(
  1620. (n) => n.isExported && (n.kind === 'function' || n.kind === 'class')
  1621. );
  1622. if (direct) return direct;
  1623. } else if (want.isNamespace && want.memberName) {
  1624. const direct = nodesInFile.find(
  1625. (n) => n.name === want.memberName && n.isExported
  1626. );
  1627. if (direct) return direct;
  1628. } else {
  1629. const direct = nodesInFile.find(
  1630. (n) => n.name === want.exportedName && n.isExported
  1631. );
  1632. if (direct) return direct;
  1633. }
  1634. // 2. Re-export hit: the file forwards the symbol to another module.
  1635. const reExports = context.getReExports?.(filePath, language) ?? [];
  1636. if (reExports.length === 0) return undefined;
  1637. // Look for explicit `export { want } from './other'` (with optional rename).
  1638. const targetName = want.isDefault ? 'default' : want.exportedName;
  1639. for (const rex of reExports) {
  1640. if (rex.kind === 'named' && rex.exportedName === targetName) {
  1641. const next = resolveImportPath(rex.source, filePath, language, context);
  1642. if (!next) continue;
  1643. // After rename: `export { foo as bar } from './x'` — to chase
  1644. // `bar`, we look for `foo` in `./x`.
  1645. const chained = findExportedSymbol(
  1646. next,
  1647. {
  1648. isDefault: rex.originalName === 'default',
  1649. isNamespace: false,
  1650. exportedName: rex.originalName,
  1651. memberName: null,
  1652. },
  1653. language,
  1654. context,
  1655. visited,
  1656. depth + 1
  1657. );
  1658. if (chained) return chained;
  1659. }
  1660. }
  1661. // 3. Wildcard re-export: `export * from './other'` — try every
  1662. // forwarding source. This is the barrel-of-barrels case.
  1663. for (const rex of reExports) {
  1664. if (rex.kind === 'wildcard') {
  1665. const next = resolveImportPath(rex.source, filePath, language, context);
  1666. if (!next) continue;
  1667. const chained = findExportedSymbol(next, want, language, context, visited, depth + 1);
  1668. if (chained) return chained;
  1669. }
  1670. }
  1671. return undefined;
  1672. }