import-resolver.ts 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896
  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. * Is this reference a PHP include/require PATH (vs a namespace `use` symbol)?
  490. *
  491. * include/require emit a file path ("lib.php", "inc/db.php", "../x.php"),
  492. * whereas namespace use is an FQN (App\Foo\Bar) or a bare class symbol
  493. * (Closure). PHP identifiers contain neither '/' nor '.', so a slash or dot
  494. * marks a path-shaped include. Such references resolve to files only — never
  495. * to a same-named symbol — so callers must not fall back to the name-matcher.
  496. */
  497. export function isPhpIncludePathRef(ref: UnresolvedRef): boolean {
  498. return (
  499. ref.language === 'php' &&
  500. ref.referenceKind === 'imports' &&
  501. (ref.referenceName.includes('/') || ref.referenceName.includes('.'))
  502. );
  503. }
  504. /**
  505. * Resolve a PHP include/require path to a project-relative file path.
  506. *
  507. * PHP resolves includes relative to the including file's directory (the
  508. * common case for procedural codebases); php.ini `include_path` is not
  509. * modeled. Callers pass an already-extracted static literal path.
  510. */
  511. function resolvePhpIncludePath(
  512. includePath: string,
  513. fromFile: string,
  514. context: ResolutionContext
  515. ): string | null {
  516. const projectRoot = context.getProjectRoot();
  517. const fromDir = path.dirname(path.join(projectRoot, fromFile));
  518. const basePath = path.resolve(fromDir, includePath);
  519. const relativePath = path.relative(projectRoot, basePath).replace(/\\/g, '/');
  520. if (context.fileExists(relativePath)) return relativePath;
  521. // The literal may omit the .php extension (e.g. include "config").
  522. for (const ext of EXTENSION_RESOLUTION.php ?? []) {
  523. if (context.fileExists(relativePath + ext)) return relativePath + ext;
  524. }
  525. return null;
  526. }
  527. /**
  528. * Extract import mappings from a file
  529. */
  530. export function extractImportMappings(
  531. _filePath: string,
  532. content: string,
  533. language: Language
  534. ): ImportMapping[] {
  535. const mappings: ImportMapping[] = [];
  536. if (language === 'typescript' || language === 'javascript' || language === 'tsx' || language === 'jsx') {
  537. mappings.push(...extractJSImports(content));
  538. } else if (language === 'svelte' || language === 'vue') {
  539. // Svelte/Vue single-file components import via plain ES6 inside their
  540. // `<script>` block. Without this, a `.svelte`/`.vue` consumer produces
  541. // zero import mappings, so `resolveViaImport` can't run and a barrel
  542. // import (`import { Foo } from './lib'`) falls back to name-matching —
  543. // which silently fails whenever the re-export alias differs from the
  544. // component's real name, yielding a false 0 callers (#629). The ES6
  545. // import regex only matches `import … from '…'`, so running it over the
  546. // whole SFC (markup + styles included) is safe.
  547. mappings.push(...extractJSImports(content));
  548. } else if (language === 'python') {
  549. mappings.push(...extractPythonImports(content));
  550. } else if (language === 'go') {
  551. mappings.push(...extractGoImports(content));
  552. } else if (language === 'java' || language === 'kotlin') {
  553. mappings.push(...extractJavaImports(content));
  554. } else if (language === 'php') {
  555. mappings.push(...extractPHPImports(content));
  556. } else if (language === 'c' || language === 'cpp') {
  557. mappings.push(...extractCppImports(content));
  558. }
  559. return mappings;
  560. }
  561. /**
  562. * Extract JS/TS import mappings
  563. */
  564. function extractJSImports(content: string): ImportMapping[] {
  565. const mappings: ImportMapping[] = [];
  566. // ES6 imports
  567. const importRegex = /import\s+(?:(\w+)\s*,?\s*)?(?:\{([^}]+)\})?\s*(?:(\*)\s+as\s+(\w+))?\s*from\s*['"]([^'"]+)['"]/g;
  568. let match;
  569. while ((match = importRegex.exec(content)) !== null) {
  570. const [, defaultImport, namedImports, star, namespaceAlias, source] = match;
  571. // Default import
  572. if (defaultImport) {
  573. mappings.push({
  574. localName: defaultImport,
  575. exportedName: 'default',
  576. source: source!,
  577. isDefault: true,
  578. isNamespace: false,
  579. });
  580. }
  581. // Named imports
  582. if (namedImports) {
  583. const names = namedImports.split(',').map((s) => s.trim());
  584. for (const name of names) {
  585. const aliasMatch = name.match(/(\w+)\s+as\s+(\w+)/);
  586. if (aliasMatch) {
  587. mappings.push({
  588. localName: aliasMatch[2]!,
  589. exportedName: aliasMatch[1]!,
  590. source: source!,
  591. isDefault: false,
  592. isNamespace: false,
  593. });
  594. } else if (name) {
  595. mappings.push({
  596. localName: name,
  597. exportedName: name,
  598. source: source!,
  599. isDefault: false,
  600. isNamespace: false,
  601. });
  602. }
  603. }
  604. }
  605. // Namespace import
  606. if (star && namespaceAlias) {
  607. mappings.push({
  608. localName: namespaceAlias,
  609. exportedName: '*',
  610. source: source!,
  611. isDefault: false,
  612. isNamespace: true,
  613. });
  614. }
  615. }
  616. // Require statements
  617. const requireRegex = /(?:const|let|var)\s+(?:(\w+)|{([^}]+)})\s*=\s*require\(['"]([^'"]+)['"]\)/g;
  618. while ((match = requireRegex.exec(content)) !== null) {
  619. const [, defaultName, destructured, source] = match;
  620. if (defaultName) {
  621. mappings.push({
  622. localName: defaultName,
  623. exportedName: 'default',
  624. source: source!,
  625. isDefault: true,
  626. isNamespace: false,
  627. });
  628. }
  629. if (destructured) {
  630. const names = destructured.split(',').map((s) => s.trim());
  631. for (const name of names) {
  632. const aliasMatch = name.match(/(\w+)\s*:\s*(\w+)/);
  633. if (aliasMatch) {
  634. mappings.push({
  635. localName: aliasMatch[2]!,
  636. exportedName: aliasMatch[1]!,
  637. source: source!,
  638. isDefault: false,
  639. isNamespace: false,
  640. });
  641. } else if (name) {
  642. mappings.push({
  643. localName: name,
  644. exportedName: name,
  645. source: source!,
  646. isDefault: false,
  647. isNamespace: false,
  648. });
  649. }
  650. }
  651. }
  652. }
  653. return mappings;
  654. }
  655. /**
  656. * Extract Python import mappings
  657. */
  658. function extractPythonImports(content: string): ImportMapping[] {
  659. const mappings: ImportMapping[] = [];
  660. // from X import Y
  661. const fromImportRegex = /from\s+([\w.]+)\s+import\s+([^#\n]+)/g;
  662. let match;
  663. while ((match = fromImportRegex.exec(content)) !== null) {
  664. const [, source, imports] = match;
  665. const names = imports!.split(',').map((s) => s.trim());
  666. for (const name of names) {
  667. const aliasMatch = name.match(/(\w+)\s+as\s+(\w+)/);
  668. if (aliasMatch) {
  669. mappings.push({
  670. localName: aliasMatch[2]!,
  671. exportedName: aliasMatch[1]!,
  672. source: source!,
  673. isDefault: false,
  674. isNamespace: false,
  675. });
  676. } else if (name && name !== '*') {
  677. mappings.push({
  678. localName: name,
  679. exportedName: name,
  680. source: source!,
  681. isDefault: false,
  682. isNamespace: false,
  683. });
  684. }
  685. }
  686. }
  687. // import X
  688. const importRegex = /^import\s+([\w.]+)(?:\s+as\s+(\w+))?/gm;
  689. while ((match = importRegex.exec(content)) !== null) {
  690. const [, source, alias] = match;
  691. const localName = alias || source!.split('.').pop()!;
  692. mappings.push({
  693. localName,
  694. exportedName: '*',
  695. source: source!,
  696. isDefault: false,
  697. isNamespace: true,
  698. });
  699. }
  700. return mappings;
  701. }
  702. /**
  703. * Extract Go import mappings
  704. */
  705. function extractGoImports(content: string): ImportMapping[] {
  706. const mappings: ImportMapping[] = [];
  707. // import "path" or import alias "path"
  708. const singleImportRegex = /import\s+(?:(\w+)\s+)?["']([^"']+)["']/g;
  709. let match;
  710. while ((match = singleImportRegex.exec(content)) !== null) {
  711. const [, alias, source] = match;
  712. const packageName = source!.split('/').pop()!;
  713. mappings.push({
  714. localName: alias || packageName,
  715. exportedName: '*',
  716. source: source!,
  717. isDefault: false,
  718. isNamespace: true,
  719. });
  720. }
  721. // import ( ... ) block
  722. const blockImportRegex = /import\s*\(\s*([^)]+)\s*\)/gs;
  723. while ((match = blockImportRegex.exec(content)) !== null) {
  724. const block = match[1]!;
  725. const lineRegex = /(?:(\w+)\s+)?["']([^"']+)["']/g;
  726. let lineMatch;
  727. while ((lineMatch = lineRegex.exec(block)) !== null) {
  728. const [, alias, source] = lineMatch;
  729. const packageName = source!.split('/').pop()!;
  730. mappings.push({
  731. localName: alias || packageName,
  732. exportedName: '*',
  733. source: source!,
  734. isDefault: false,
  735. isNamespace: true,
  736. });
  737. }
  738. }
  739. return mappings;
  740. }
  741. /**
  742. * Extract Java / Kotlin import mappings.
  743. *
  744. * Java/Kotlin imports carry the full qualified name of the imported
  745. * symbol — `import com.example.dao.converter.FooConverter;` — which is
  746. * exactly the disambiguation signal we need when two packages both
  747. * declare a `FooConverter`. Pre-#314 the resolver had no Java branch
  748. * here at all, so this mapping was empty and cross-module name
  749. * collisions were resolved by file-path proximity (often wrongly).
  750. *
  751. * `import static com.example.Foo.bar;` is parsed as a local-name `bar`
  752. * pointing at FQN `com.example.Foo.bar` so static-method call sites
  753. * (`bar(...)`) can resolve through the same import lookup.
  754. */
  755. function extractJavaImports(content: string): ImportMapping[] {
  756. const mappings: ImportMapping[] = [];
  757. // Strip line and block comments so `// import foo;` doesn't false-match.
  758. const stripped = content
  759. .replace(/\/\*[\s\S]*?\*\//g, '')
  760. .replace(/\/\/[^\n]*/g, '');
  761. // `import [static] <fqn>[.*];`
  762. const re = /^\s*import\s+(static\s+)?([\w.]+(?:\.\*)?)\s*;/gm;
  763. let match: RegExpExecArray | null;
  764. while ((match = re.exec(stripped)) !== null) {
  765. const fqn = match[2]!;
  766. // `import com.example.*;` — wildcard. We can't materialize a single
  767. // local name; skip and let name-matching handle members reachable
  768. // through the wildcard. (Future enhancement: enumerate package files.)
  769. if (fqn.endsWith('.*')) continue;
  770. const parts = fqn.split('.');
  771. const localName = parts[parts.length - 1];
  772. if (!localName) continue;
  773. mappings.push({
  774. localName,
  775. exportedName: localName,
  776. source: fqn,
  777. isDefault: false,
  778. isNamespace: false,
  779. });
  780. }
  781. return mappings;
  782. }
  783. /**
  784. * Extract PHP import mappings (use statements)
  785. */
  786. function extractPHPImports(content: string): ImportMapping[] {
  787. const mappings: ImportMapping[] = [];
  788. // use Namespace\Class; or use Namespace\Class as Alias;
  789. const useRegex = /use\s+([\w\\]+)(?:\s+as\s+(\w+))?;/g;
  790. let match;
  791. while ((match = useRegex.exec(content)) !== null) {
  792. const [, fullPath, alias] = match;
  793. const className = fullPath!.split('\\').pop()!;
  794. mappings.push({
  795. localName: alias || className,
  796. exportedName: className,
  797. source: fullPath!,
  798. isDefault: false,
  799. isNamespace: false,
  800. });
  801. }
  802. return mappings;
  803. }
  804. /**
  805. * Extract C/C++ import mappings from #include directives.
  806. *
  807. * #include brings all symbols from the included header into scope
  808. * (namespace import), so each mapping uses isNamespace: true and
  809. * exportedName: '*'. The localName is set to the header's basename
  810. * without extension so that symbol references like `MyClass` can
  811. * match against any include that might provide it.
  812. */
  813. function extractCppImports(content: string): ImportMapping[] {
  814. const mappings: ImportMapping[] = [];
  815. // Match both #include <...> and #include "..."
  816. const includeRegex = /^\s*#\s*include\s+[<"]([^>"]+)[>"]/gm;
  817. let match;
  818. while ((match = includeRegex.exec(content)) !== null) {
  819. const modulePath = match[1]!;
  820. // Basename without extension for localName matching
  821. const basename = modulePath.split('/').pop()!.replace(/\.(h|hpp|hxx|hh|inl|ipp|cxx|cc|cpp)$/,'');
  822. mappings.push({
  823. localName: basename || modulePath,
  824. exportedName: '*',
  825. source: modulePath,
  826. isDefault: false,
  827. isNamespace: true,
  828. });
  829. }
  830. return mappings;
  831. }
  832. // Cache import mappings per file to avoid re-reading and re-parsing
  833. const importMappingCache = new Map<string, ImportMapping[]>();
  834. /**
  835. * Clear the import mapping cache (call between indexing runs)
  836. */
  837. export function clearImportMappingCache(): void {
  838. importMappingCache.clear();
  839. cppIncludeDirCache.clear();
  840. }
  841. /**
  842. * Strip JS line + block comments from `content` while preserving
  843. * string literals (so `"//"` inside a string stays intact). Used by
  844. * {@link extractReExports} so commented-out export-from statements
  845. * don't generate phantom re-export edges.
  846. *
  847. * Scanner is deliberately small: it only tracks the three contexts
  848. * relevant for JS/TS — single-quote string, double-quote string, and
  849. * template literal. Comment recognition is the JS spec subset, no
  850. * regex-literal awareness (which is fine for our use case: we don't
  851. * apply this to function bodies, only to top-level files).
  852. */
  853. function stripJsComments(content: string): string {
  854. let out = '';
  855. let i = 0;
  856. let str: '"' | "'" | '`' | null = null;
  857. while (i < content.length) {
  858. const ch = content[i]!;
  859. if (str !== null) {
  860. out += ch;
  861. if (ch === '\\' && i + 1 < content.length) {
  862. out += content[i + 1]!;
  863. i += 2;
  864. continue;
  865. }
  866. if (ch === str) str = null;
  867. i++;
  868. continue;
  869. }
  870. if (ch === '"' || ch === "'" || ch === '`') {
  871. str = ch;
  872. out += ch;
  873. i++;
  874. continue;
  875. }
  876. if (ch === '/' && content[i + 1] === '/') {
  877. while (i < content.length && content[i] !== '\n') i++;
  878. continue;
  879. }
  880. if (ch === '/' && content[i + 1] === '*') {
  881. i += 2;
  882. while (i < content.length && !(content[i] === '*' && content[i + 1] === '/')) i++;
  883. i += 2;
  884. continue;
  885. }
  886. out += ch;
  887. i++;
  888. }
  889. return out;
  890. }
  891. /**
  892. * Extract JS/TS re-export declarations from `content`.
  893. *
  894. * Recognised forms:
  895. * export { foo } from './a';
  896. * export { foo as bar } from './a';
  897. * export * from './a';
  898. * export * as ns from './a'; (treated as wildcard for chasing)
  899. * export { default as Foo } from './a';
  900. *
  901. * The walker intentionally stays regex-based — the import-resolver
  902. * elsewhere in this file already chooses regex over a fresh
  903. * tree-sitter pass, and this function shares that trade-off. Errors
  904. * fall through silently; resolution simply skips the broken file.
  905. */
  906. export function extractReExports(content: string, language: Language): ReExport[] {
  907. if (
  908. language !== 'typescript' &&
  909. language !== 'javascript' &&
  910. language !== 'tsx' &&
  911. language !== 'jsx'
  912. ) {
  913. return [];
  914. }
  915. const out: ReExport[] = [];
  916. // Pre-strip block comments + line comments so a commented-out
  917. // `// export { x } from '...'` doesn't produce a phantom edge.
  918. // (Template literals are still a possible source of false positives;
  919. // a project that builds export statements as runtime strings is
  920. // out of scope.)
  921. const cleaned = stripJsComments(content);
  922. // Wildcard: `export * from '...'` or `export * as ns from '...'`
  923. const wildcardRe = /export\s*\*(?:\s+as\s+\w+)?\s*from\s*['"]([^'"]+)['"]/g;
  924. let m: RegExpExecArray | null;
  925. while ((m = wildcardRe.exec(cleaned)) !== null) {
  926. out.push({ kind: 'wildcard', source: m[1]! });
  927. }
  928. // Named: `export { a, b as c } from '...'`
  929. const namedRe = /export\s*\{([^}]+)\}\s*from\s*['"]([^'"]+)['"]/g;
  930. while ((m = namedRe.exec(cleaned)) !== null) {
  931. const inner = m[1]!;
  932. const source = m[2]!;
  933. for (const raw of inner.split(',')) {
  934. const item = raw.trim();
  935. if (!item) continue;
  936. const aliasMatch = item.match(/^(\w+)\s+as\s+(\w+)$/);
  937. if (aliasMatch) {
  938. out.push({
  939. kind: 'named',
  940. exportedName: aliasMatch[2]!,
  941. originalName: aliasMatch[1]!,
  942. source,
  943. });
  944. } else if (/^\w+$/.test(item)) {
  945. out.push({
  946. kind: 'named',
  947. exportedName: item,
  948. originalName: item,
  949. source,
  950. });
  951. }
  952. }
  953. }
  954. return out;
  955. }
  956. /**
  957. * Resolve a reference using import mappings
  958. */
  959. /**
  960. * JVM (Java / Kotlin) imports use fully-qualified names (`import
  961. * com.example.foo.Bar`) decoupled from filenames, so the JS/Python
  962. * style filesystem path lookup misses them whenever the file isn't
  963. * named after its primary symbol (Kotlin `Utils.kt` exporting `Bar`,
  964. * top-level fns, extension fns). Resolve them through the
  965. * `qualifiedName` index instead — populated by the package_header /
  966. * package_declaration namespace wrappers in the extractor.
  967. */
  968. export function resolveJvmImport(
  969. ref: UnresolvedRef,
  970. context: ResolutionContext
  971. ): ResolvedRef | null {
  972. if (ref.referenceKind !== 'imports') return null;
  973. if (ref.language !== 'java' && ref.language !== 'kotlin') return null;
  974. const fqn = ref.referenceName;
  975. const lastDot = fqn.lastIndexOf('.');
  976. if (lastDot <= 0) return null;
  977. const pkg = fqn.substring(0, lastDot);
  978. const sym = fqn.substring(lastDot + 1);
  979. // Wildcard imports (`com.example.*`) deliberately punt to name-matcher.
  980. if (sym === '*') return null;
  981. const candidates = context.getNodesByQualifiedName(`${pkg}::${sym}`);
  982. if (candidates.length === 0) return null;
  983. // Kotlin Multiplatform: an `expect` declaration and its `actual`s share one
  984. // FQN across source sets (commonMain / androidMain / appleMain). Taking the
  985. // first candidate let a single platform `actual` absorb every common-side
  986. // import, so the `expect` (the canonical API a commonMain file imports)
  987. // looked unused. Prefer the candidate CLOSEST to the importing file by
  988. // directory proximity — a commonMain import resolves to the commonMain
  989. // declaration — with the `expect` side as a tiebreak.
  990. const best = candidates.length === 1 ? candidates[0]! : pickClosestJvmCandidate(candidates, ref.filePath);
  991. return {
  992. original: ref,
  993. targetNodeId: best.id,
  994. confidence: 0.95,
  995. resolvedBy: 'import',
  996. };
  997. }
  998. /**
  999. * Pick the same-FQN candidate closest to `fromPath` by shared directory
  1000. * prefix, preferring an `expect` declaration on a tie. Used to keep a Kotlin
  1001. * Multiplatform `expect`/`actual` import resolving within the importer's own
  1002. * source set instead of an arbitrary platform `actual`.
  1003. */
  1004. function pickClosestJvmCandidate(candidates: Node[], fromPath: string): Node {
  1005. const fromDirs = fromPath.split('/').slice(0, -1);
  1006. const sharedPrefix = (p: string): number => {
  1007. const d = p.split('/').slice(0, -1);
  1008. let shared = 0;
  1009. for (let i = 0; i < Math.min(fromDirs.length, d.length); i++) {
  1010. if (fromDirs[i] === d[i]) shared++;
  1011. else break;
  1012. }
  1013. return shared;
  1014. };
  1015. const isExpect = (n: Node): boolean => Array.isArray(n.decorators) && n.decorators.includes('expect');
  1016. let best = candidates[0]!;
  1017. let bestProx = sharedPrefix(best.filePath);
  1018. for (let i = 1; i < candidates.length; i++) {
  1019. const c = candidates[i]!;
  1020. const prox = sharedPrefix(c.filePath);
  1021. if (prox > bestProx || (prox === bestProx && isExpect(c) && !isExpect(best))) {
  1022. best = c;
  1023. bestProx = prox;
  1024. }
  1025. }
  1026. return best;
  1027. }
  1028. export function resolveViaImport(
  1029. ref: UnresolvedRef,
  1030. context: ResolutionContext
  1031. ): ResolvedRef | null {
  1032. // C/C++ #include references — resolve directly to the included file
  1033. // (file→file edge), bypassing symbol lookup. The extractor emits these
  1034. // with `referenceKind: 'imports'` and `referenceName: <include path>`
  1035. // (e.g. "uint256.h" or "common/args.h"). Without this branch the
  1036. // include-dir scan path inside resolveImportPath never produces an
  1037. // edge — resolveViaImport's symbol lookup below would search the
  1038. // resolved file for a symbol named like the file extension and fail.
  1039. if ((ref.language === 'c' || ref.language === 'cpp') && ref.referenceKind === 'imports') {
  1040. // C/C++ quoted includes (`#include "X.h"`) resolve relative to the
  1041. // INCLUDING file's own directory first (the C standard's quoted-include
  1042. // search order). Prefer a same-directory header over an -I directory or a
  1043. // same-named header on another platform (windows/code/RNCAsyncStorage.h vs
  1044. // apple/.../RNCAsyncStorage.h) — the include-dir heuristic below would
  1045. // otherwise pick an arbitrary same-named header, leaving the real local one
  1046. // with no dependents.
  1047. const slash = ref.filePath.lastIndexOf('/');
  1048. const fromDir = slash >= 0 ? ref.filePath.slice(0, slash) : '';
  1049. const siblingPath = path.posix.normalize(fromDir ? `${fromDir}/${ref.referenceName}` : ref.referenceName);
  1050. const siblingBase = siblingPath.split('/').pop()!;
  1051. const sibling = context
  1052. .getNodesByName(siblingBase)
  1053. .find((n) => n.kind === 'file' && n.filePath === siblingPath);
  1054. if (sibling) {
  1055. return { original: ref, targetNodeId: sibling.id, confidence: 0.92, resolvedBy: 'import' };
  1056. }
  1057. const resolvedPath = resolveImportPath(ref.referenceName, ref.filePath, ref.language, context);
  1058. if (!resolvedPath) return null;
  1059. const basename = resolvedPath.split('/').pop()!;
  1060. const fileNodes = context.getNodesByName(basename).filter((n) => n.kind === 'file');
  1061. const fileNode = fileNodes.find((n) => n.filePath === resolvedPath);
  1062. if (fileNode) {
  1063. return {
  1064. original: ref,
  1065. targetNodeId: fileNode.id,
  1066. confidence: 0.9,
  1067. resolvedBy: 'import',
  1068. };
  1069. }
  1070. return null;
  1071. }
  1072. // PHP include/require — resolve the static string path to a file→file
  1073. // edge, mirroring the C/C++ branch above. Distinguish include PATHS from
  1074. // namespace `use` symbols by shape: an include path contains a slash or a
  1075. // file extension ("lib.php", "inc/db.php", "../x.php"), whereas a namespace
  1076. // use is an FQN (App\Foo\Bar) or a bare class symbol (Closure) — PHP
  1077. // identifiers contain neither '/' nor '.'. Only path-shaped references are
  1078. // includes; symbol references fall through to the namespace resolution.
  1079. if (isPhpIncludePathRef(ref)) {
  1080. const resolvedPath = resolvePhpIncludePath(ref.referenceName, ref.filePath, context);
  1081. if (resolvedPath) {
  1082. const basename = resolvedPath.split('/').pop()!;
  1083. const fileNode = context
  1084. .getNodesByName(basename)
  1085. .find((n) => n.kind === 'file' && n.filePath === resolvedPath);
  1086. if (fileNode) {
  1087. return {
  1088. original: ref,
  1089. targetNodeId: fileNode.id,
  1090. confidence: 0.9,
  1091. resolvedBy: 'import',
  1092. };
  1093. }
  1094. }
  1095. // A path-shaped include that doesn't resolve to a known project file is a
  1096. // dead end. Return unresolved rather than falling through to the symbol
  1097. // name-matcher, which would mis-connect e.g. "inc/db.php" to an unrelated
  1098. // db.php elsewhere in the tree — a wrong edge is worse than a missing one.
  1099. return null;
  1100. }
  1101. // Use cached import mappings (avoids re-reading and re-parsing per ref)
  1102. const imports = context.getImportMappings(ref.filePath, ref.language);
  1103. if (imports.length === 0 && !context.readFile(ref.filePath)) {
  1104. return null;
  1105. }
  1106. // Go cross-package calls: `pkga.FuncX(...)` extracts to referenceName
  1107. // `pkga.FuncX` and the import `github.com/example/myproject/pkga`
  1108. // maps to a *package directory* containing one or more .go files.
  1109. // The generic file-based lookup below can't follow that — issue #388.
  1110. if (ref.language === 'go') {
  1111. const goResult = resolveGoCrossPackageReference(ref, imports, context);
  1112. if (goResult) return goResult;
  1113. }
  1114. // Java / Kotlin: imports are FQNs (`import com.example.Foo;`) — no
  1115. // resolvable file path the JS/TS-style chain below could follow. Look
  1116. // up the symbol by name and filter to the candidate whose file path
  1117. // matches the imported FQN. This is the disambiguation signal that
  1118. // breaks the same-name class collision the path-proximity matcher
  1119. // can't resolve (issue #314).
  1120. if (ref.language === 'java' || ref.language === 'kotlin') {
  1121. const javaResult = resolveJavaImportedReference(ref, imports, context);
  1122. if (javaResult) return javaResult;
  1123. }
  1124. // Python qualified access through an imported MODULE: `certs.where()` after
  1125. // `from . import certs`, `mod.func()` after `import mod`. The receiver names a
  1126. // submodule (a file), not a symbol, so the generic symbol lookup below would
  1127. // search the *package* for `certs` instead of looking inside the module.
  1128. if (ref.language === 'python') {
  1129. const pyResult = resolvePythonModuleMember(ref, imports, context);
  1130. if (pyResult) return pyResult;
  1131. // Absolute dotted module import: `import conduit.apps.articles.signals`
  1132. // (the standard Django AppConfig.ready() signal-registration pattern, and
  1133. // any side-effect `import pkg.mod`). Map the dotted path to its file.
  1134. const pyModResult = resolvePythonAbsoluteModule(ref, context);
  1135. if (pyModResult) return pyModResult;
  1136. }
  1137. // Rust qualified path: resolve the module prefix of `crate::m::Item` /
  1138. // `self::sub::Item` / `super::m::func` to a file, then find the leaf symbol in
  1139. // it. Disambiguates common-name `pub use self::read::read` re-exports that
  1140. // name-matching would land on the wrong same-named symbol.
  1141. if (ref.language === 'rust' && ref.referenceName.includes('::')) {
  1142. const rustResult = resolveRustPathReference(ref, context);
  1143. if (rustResult) return rustResult;
  1144. }
  1145. // Lua / Luau `require(...)`: a dotted module path (`a.b.c` from
  1146. // `require("a.b.c")`) or an instance-path leaf (`Signal` from
  1147. // `require(script.Parent.Signal)`) — map it to a module file. There's no static
  1148. // import statement, so the generic path-matcher can't bridge the dot↔slash /
  1149. // leaf↔basename gap; resolve it explicitly to the module file.
  1150. if ((ref.language === 'lua' || ref.language === 'luau') && ref.referenceKind === 'imports') {
  1151. const luaResult = resolveLuaRequire(ref, context);
  1152. if (luaResult) return luaResult;
  1153. }
  1154. // Whole-module / namespace imports → link the importing file to the module
  1155. // file. Python `from . import certs` / `import mod`, and TS/JS `import * as ns
  1156. // from './x'` (so a namespace touched only via a value-member read still
  1157. // records the dependency). A named TS/JS import returns null here and falls
  1158. // through to symbol resolution below.
  1159. if (
  1160. ref.language === 'python' ||
  1161. ref.language === 'typescript' ||
  1162. ref.language === 'tsx' ||
  1163. ref.language === 'javascript' ||
  1164. ref.language === 'jsx'
  1165. ) {
  1166. const moduleFile = resolveModuleImportToFile(ref, imports, context);
  1167. if (moduleFile) return moduleFile;
  1168. }
  1169. // Check if the reference name matches any import
  1170. for (const imp of imports) {
  1171. if (imp.localName === ref.referenceName || ref.referenceName.startsWith(imp.localName + '.')) {
  1172. // Resolve the import path
  1173. const resolvedPath = resolveImportPath(
  1174. imp.source,
  1175. ref.filePath,
  1176. ref.language,
  1177. context
  1178. );
  1179. if (resolvedPath) {
  1180. const exportedName = imp.isDefault ? 'default' : imp.exportedName;
  1181. const memberName = imp.isNamespace
  1182. ? ref.referenceName.replace(imp.localName + '.', '')
  1183. : null;
  1184. const targetNode = findExportedSymbol(
  1185. resolvedPath,
  1186. { isDefault: imp.isDefault, isNamespace: imp.isNamespace, exportedName, memberName },
  1187. ref.language,
  1188. context,
  1189. new Set()
  1190. );
  1191. if (targetNode) {
  1192. return {
  1193. original: ref,
  1194. targetNodeId: targetNode.id,
  1195. confidence: 0.9,
  1196. resolvedBy: 'import',
  1197. };
  1198. }
  1199. }
  1200. }
  1201. }
  1202. return null;
  1203. }
  1204. /**
  1205. * Resolve a Python qualified reference whose receiver is an imported MODULE:
  1206. * `certs.where()` after `from . import certs`, `mod.func()` after `import mod`
  1207. * or `from pkg import mod`. The receiver names a submodule (a file), not a
  1208. * symbol, so the generic symbol lookup in `resolveViaImport` can't follow it —
  1209. * it would search the *package* for `certs`/`mod` instead of looking inside the
  1210. * module. This is the Python half of the cross-package qualified-call problem
  1211. * (cf. `resolveGoCrossPackageReference` for Go's `pkg.Func`, issue #388).
  1212. *
  1213. * Builds the module's dotted import path from the binding — `from . import
  1214. * certs` → `.certs`; `from pkg import mod` → `pkg.mod`; `import mod` → `mod` —
  1215. * resolves it to the module file, and finds the member defined there. Returns
  1216. * null when no module file exists at that path, so attribute access on an
  1217. * imported *value* (`helper.attr` where `helper` is a function) falls through
  1218. * to the other strategies untouched.
  1219. */
  1220. function resolvePythonModuleMember(
  1221. ref: UnresolvedRef,
  1222. imports: ImportMapping[],
  1223. context: ResolutionContext
  1224. ): ResolvedRef | null {
  1225. const dotIdx = ref.referenceName.indexOf('.');
  1226. if (dotIdx <= 0) return null;
  1227. const receiver = ref.referenceName.substring(0, dotIdx);
  1228. // The immediate member of the module (first segment after the receiver).
  1229. const member = ref.referenceName.substring(dotIdx + 1).split('.')[0];
  1230. if (!member) return null;
  1231. for (const imp of imports) {
  1232. if (imp.localName !== receiver) continue;
  1233. // `import mod` / `import numpy as np` bind the module at `source` itself;
  1234. // `from . import certs` / `from pkg import mod` bind a SUBMODULE whose
  1235. // dotted path is the source joined with the imported name.
  1236. const modulePath = imp.isNamespace
  1237. ? imp.source
  1238. : imp.source.endsWith('.')
  1239. ? imp.source + imp.localName
  1240. : imp.source + '.' + imp.localName;
  1241. // resolveImportPath only maps RELATIVE dotted paths (`.mod`, `..pkg.mod`); an
  1242. // ABSOLUTE package path (`pkg.module` from `from pkg import module`, or a bare
  1243. // `import pkg.mod`) resolves to null there, so fall back to the dotted-module
  1244. // file lookup — the same asymmetry resolveModuleImportToFile already handles
  1245. // for the file→file import edge. Without this, a `module.func()` call after
  1246. // `from pkg import module` dropped its `calls` edge even though the import
  1247. // edge resolved (#578).
  1248. let resolvedPath = resolveImportPath(modulePath, ref.filePath, ref.language, context);
  1249. if (!resolvedPath) {
  1250. resolvedPath = findPythonModuleFile(modulePath, context, ref.filePath)?.filePath ?? null;
  1251. }
  1252. if (!resolvedPath || resolvedPath === ref.filePath) continue;
  1253. // Find the member as a top-level definition in the module file. Exclude
  1254. // `method` so `mod.foo` never lands on a same-named class method.
  1255. const target = context.getNodesInFile(resolvedPath).find(
  1256. (n) =>
  1257. n.name === member &&
  1258. (n.kind === 'function' ||
  1259. n.kind === 'class' ||
  1260. n.kind === 'variable' ||
  1261. n.kind === 'constant')
  1262. );
  1263. if (target) {
  1264. return { original: ref, targetNodeId: target.id, confidence: 0.85, resolvedBy: 'import' };
  1265. }
  1266. }
  1267. return null;
  1268. }
  1269. /**
  1270. * Resolve a whole-MODULE import to that module's file (a file→file dependency).
  1271. * The imported name is a module, not a symbol, so there's nothing to resolve to
  1272. * — but importing a module IS a dependency on it. Covers:
  1273. * - Python submodule imports — `from . import certs`, `from pkg import sub`;
  1274. * - namespace imports — Python `import mod` / `import numpy as np`, and
  1275. * TS/JS `import * as ns from './x'`.
  1276. *
  1277. * It is also the robust backstop for {@link resolvePythonModuleMember} and for
  1278. * TS namespace usage: it records the dependency even when the used member is
  1279. * re-exported elsewhere (requests' `certs.where`, re-exported from `certifi`),
  1280. * the usage is module-level code that isn't extracted as a call, or a TS
  1281. * namespace is touched only via a value-member read (`ns.SOME_CONST`).
  1282. *
  1283. * Only fires for dot-free `imports`-kind refs whose module path resolves to a
  1284. * real file. A NAMED TS/JS import (`import { widget }`) is not a module, so it
  1285. * returns null and normal symbol resolution handles it.
  1286. */
  1287. /**
  1288. * Resolve a Lua/Luau `require(...)` to its module file. The reference name is
  1289. * either a dotted module path (`telescope.config` → `telescope/config.lua`) or a
  1290. * Roblox instance-path leaf (`Signal` from `require(script.Parent.Signal)` →
  1291. * `Signal.luau`). We try `<path>.lua|.luau` and `<path>/init.lua|.luau`, matched
  1292. * by path suffix (the module root — `lua/`, `src/`, … — is project-specific).
  1293. * Among suffix matches, the one sharing the longest directory prefix with the
  1294. * requiring file wins (instance-path requires resolve within the same package).
  1295. */
  1296. function resolveLuaRequire(ref: UnresolvedRef, context: ResolutionContext): ResolvedRef | null {
  1297. const name = ref.referenceName;
  1298. if (!name) return null;
  1299. const base = name.includes('.') ? name.replace(/\./g, '/') : name;
  1300. const suffixes = [`${base}.lua`, `${base}.luau`, `${base}/init.lua`, `${base}/init.luau`];
  1301. const files = context.getAllFiles();
  1302. const shared = (a: string, b: string): number => {
  1303. let i = 0;
  1304. while (i < a.length && i < b.length && a[i] === b[i]) i++;
  1305. return i;
  1306. };
  1307. for (const suffix of suffixes) {
  1308. const matches = files.filter((f) => f === suffix || f.endsWith('/' + suffix));
  1309. if (matches.length === 0) continue;
  1310. matches.sort((x, y) => shared(y, ref.filePath) - shared(x, ref.filePath));
  1311. const best = matches[0]!;
  1312. if (best === ref.filePath) continue;
  1313. const fileNode = context.getNodesInFile(best).find((n) => n.kind === 'file');
  1314. if (fileNode) {
  1315. // Confidence ≥ 0.9 so this deterministic path/suffix match wins over
  1316. // name-matching, which otherwise resolves the require to the import node
  1317. // itself (a same-name self-match).
  1318. return { original: ref, targetNodeId: fileNode.id, confidence: 0.9, resolvedBy: 'import' };
  1319. }
  1320. }
  1321. return null;
  1322. }
  1323. function resolveModuleImportToFile(
  1324. ref: UnresolvedRef,
  1325. imports: ImportMapping[],
  1326. context: ResolutionContext
  1327. ): ResolvedRef | null {
  1328. if (ref.referenceKind !== 'imports') return null;
  1329. if (ref.referenceName.includes('.')) return null;
  1330. for (const imp of imports) {
  1331. if (imp.localName !== ref.referenceName) continue;
  1332. let modulePath: string;
  1333. if (imp.isNamespace || imp.isDefault) {
  1334. // `import * as ns from './x'` (namespace) or `import x from './x'`
  1335. // (default) — the dependency is on the MODULE FILE. A default import binds
  1336. // a (possibly renamed) local to whatever the module's default export is
  1337. // (`import articlesController from './article.controller'` ← `export
  1338. // default router`), so the binding name can't be found as a symbol — link
  1339. // the file the import resolves to instead. External modules don't resolve
  1340. // (no file), so `import React from 'react'` creates no edge.
  1341. modulePath = imp.source;
  1342. } else if (ref.language === 'python') {
  1343. // `from . import certs` — the imported NAME is a submodule of the source.
  1344. modulePath = imp.source.endsWith('.')
  1345. ? imp.source + imp.localName
  1346. : imp.source + '.' + imp.localName;
  1347. } else {
  1348. // A named TS/JS import binds a symbol, not a module — leave it alone.
  1349. continue;
  1350. }
  1351. const resolvedPath = resolveImportPath(modulePath, ref.filePath, ref.language, context);
  1352. if (resolvedPath && resolvedPath !== ref.filePath) {
  1353. const fileNode = context.getNodesInFile(resolvedPath).find((n) => n.kind === 'file');
  1354. if (fileNode) {
  1355. return { original: ref, targetNodeId: fileNode.id, confidence: 0.9, resolvedBy: 'import' };
  1356. }
  1357. }
  1358. // Python absolute `from a.b import submodule` (a FastAPI router aggregator's
  1359. // `from app.api.routes import authentication`): resolveImportPath only maps
  1360. // RELATIVE dotted paths to a file, so resolve the absolute dotted module
  1361. // directly to its file node.
  1362. if (ref.language === 'python') {
  1363. const modFile = findPythonModuleFile(modulePath, context, ref.filePath);
  1364. if (modFile) {
  1365. return { original: ref, targetNodeId: modFile.id, confidence: 0.9, resolvedBy: 'import' };
  1366. }
  1367. }
  1368. }
  1369. return null;
  1370. }
  1371. /**
  1372. * Find the file node for a Python dotted module path `a.b.c` — a module file
  1373. * ending in `a/b/c.py`, or a package `a/b/c/__init__.py` (suffix-matched, so a
  1374. * package rooted under `src/` etc. still resolves). Returns null for
  1375. * stdlib/external modules (no matching repo file node), so `import os` creates
  1376. * no edge. Shared by absolute `import a.b.c` and absolute `from a.b import c`
  1377. * (where `c` is a submodule) resolution.
  1378. */
  1379. function findPythonModuleFile(
  1380. mod: string,
  1381. context: ResolutionContext,
  1382. excludeFilePath: string
  1383. ): Node | null {
  1384. if (!mod || mod.startsWith('.')) return null; // relative imports handled elsewhere
  1385. const rel = mod.replace(/\./g, '/');
  1386. const lastSeg = mod.split('.').pop()!;
  1387. const endsWith = (p: string, want: string): boolean => p === want || p.endsWith('/' + want);
  1388. const moduleFile = context
  1389. .getNodesByName(`${lastSeg}.py`)
  1390. .find((n) => n.kind === 'file' && n.filePath !== excludeFilePath && endsWith(n.filePath, `${rel}.py`));
  1391. if (moduleFile) return moduleFile;
  1392. const pkgFile = context
  1393. .getNodesByName('__init__.py')
  1394. .find((n) => n.kind === 'file' && n.filePath !== excludeFilePath && endsWith(n.filePath, `${rel}/__init__.py`));
  1395. return pkgFile ?? null;
  1396. }
  1397. /**
  1398. * Resolve a Python ABSOLUTE dotted module import (`import a.b.c`) to its file —
  1399. * the Django `AppConfig.ready(): import myapp.signals` pattern and any
  1400. * side-effect module import.
  1401. */
  1402. function resolvePythonAbsoluteModule(
  1403. ref: UnresolvedRef,
  1404. context: ResolutionContext
  1405. ): ResolvedRef | null {
  1406. if (ref.referenceKind !== 'imports') return null;
  1407. // Only a DOTTED `import a.b.c` ref carries its full module path. A bare leaf
  1408. // (`from app.api.routes import authentication`) is ambiguous on its own — three
  1409. // `authentication.py` files may exist — so leave it to resolveModuleImportToFile,
  1410. // which uses the import's source (`app.api.routes`) to build the full path.
  1411. if (!ref.referenceName.includes('.')) return null;
  1412. const hit = findPythonModuleFile(ref.referenceName, context, ref.filePath);
  1413. return hit ? { original: ref, targetNodeId: hit.id, confidence: 0.9, resolvedBy: 'import' } : null;
  1414. }
  1415. /**
  1416. * Resolve a Rust qualified reference `A::B::C` by mapping the MODULE prefix
  1417. * (`A::B`) to a file and finding the leaf symbol (`C`) in it. This is the Rust
  1418. * analog of {@link resolvePythonModuleMember} / {@link resolveGoCrossPackageReference}
  1419. * and the precise answer to common-name re-exports (`pub use self::read::read`)
  1420. * that name-matching can't disambiguate. Returns null when the prefix isn't a
  1421. * real module path (e.g. `Widget::new` — `Widget` is a struct, not a module),
  1422. * so associated-function calls and enum-variant paths fall through untouched.
  1423. */
  1424. function resolveRustPathReference(
  1425. ref: UnresolvedRef,
  1426. context: ResolutionContext
  1427. ): ResolvedRef | null {
  1428. const segments = ref.referenceName.split('::').filter((s) => s.length > 0);
  1429. if (segments.length < 2) return null;
  1430. const leaf = segments[segments.length - 1]!;
  1431. const modSegs = segments.slice(0, -1);
  1432. const file = resolveRustModuleFile(modSegs, ref.filePath, context);
  1433. if (!file || file === ref.filePath) return null;
  1434. const target = context.getNodesInFile(file).find(
  1435. (n) =>
  1436. n.name === leaf &&
  1437. (n.kind === 'function' ||
  1438. n.kind === 'struct' ||
  1439. n.kind === 'enum' ||
  1440. n.kind === 'trait' ||
  1441. n.kind === 'type_alias' ||
  1442. n.kind === 'constant' ||
  1443. n.kind === 'method' ||
  1444. n.kind === 'class' ||
  1445. n.kind === 'interface')
  1446. );
  1447. if (target) {
  1448. return { original: ref, targetNodeId: target.id, confidence: 0.9, resolvedBy: 'import' };
  1449. }
  1450. return null;
  1451. }
  1452. /** The crate-root directory (holds `lib.rs`/`main.rs`), walking up from a file. */
  1453. function rustCrateRootDir(fromFileAbs: string, context: ResolutionContext): string | null {
  1454. const projectRoot = context.getProjectRoot();
  1455. const toRel = (p: string) => path.relative(projectRoot, p).replace(/\\/g, '/');
  1456. let dir = path.dirname(fromFileAbs);
  1457. for (let i = 0; i < 64; i++) {
  1458. if (context.fileExists(toRel(path.join(dir, 'lib.rs'))) ||
  1459. context.fileExists(toRel(path.join(dir, 'main.rs')))) {
  1460. return dir;
  1461. }
  1462. const parent = path.dirname(dir);
  1463. if (parent === dir) return null;
  1464. dir = parent;
  1465. }
  1466. return null;
  1467. }
  1468. /** Directory under which the current file's module declares its SUBMODULES. */
  1469. function rustSelfModuleDir(fromFileAbs: string): string {
  1470. const base = path.basename(fromFileAbs);
  1471. const dir = path.dirname(fromFileAbs);
  1472. // mod.rs / lib.rs / main.rs own their directory; `foo.rs`'s submodules live in `foo/`.
  1473. if (base === 'mod.rs' || base === 'lib.rs' || base === 'main.rs') return dir;
  1474. return path.join(dir, base.replace(/\.rs$/, ''));
  1475. }
  1476. /**
  1477. * Resolve a Rust module path (segments WITHOUT the leaf symbol) to the file of
  1478. * the last module segment — `crate::a::b` → `<crate>/a/b.rs` (or `.../b/mod.rs`).
  1479. * Anchors on `crate` / `self` / `super`; a bare path is tried crate-relative.
  1480. */
  1481. function resolveRustModuleFile(
  1482. segments: string[],
  1483. fromFile: string,
  1484. context: ResolutionContext
  1485. ): string | null {
  1486. if (segments.length === 0) return null;
  1487. const projectRoot = context.getProjectRoot();
  1488. const fromAbs = path.join(projectRoot, fromFile);
  1489. const toRel = (p: string) => path.relative(projectRoot, p).replace(/\\/g, '/');
  1490. // Walk a sequence of module segments down from `startDir`, mapping each to a
  1491. // `<seg>.rs` or `<seg>/mod.rs` file. Returns the leaf module's file, or null
  1492. // if `startDir` is null or any segment has no file on disk.
  1493. const resolveUnder = (startDir: string | null, rest: string[]): string | null => {
  1494. if (!startDir) return null;
  1495. let dir = startDir;
  1496. let targetFile: string | null = null;
  1497. for (const seg of rest) {
  1498. if (seg === 'self' || seg === 'crate' || seg === 'super') continue;
  1499. const asFile = toRel(path.join(dir, seg + '.rs'));
  1500. const asMod = toRel(path.join(dir, seg, 'mod.rs'));
  1501. if (context.fileExists(asFile)) targetFile = asFile;
  1502. else if (context.fileExists(asMod)) targetFile = asMod;
  1503. else return null;
  1504. dir = path.join(dir, seg);
  1505. }
  1506. return targetFile;
  1507. };
  1508. const first = segments[0]!;
  1509. if (first === 'crate') {
  1510. return resolveUnder(rustCrateRootDir(fromAbs, context), segments.slice(1));
  1511. }
  1512. if (first === 'self') {
  1513. return resolveUnder(rustSelfModuleDir(fromAbs), segments.slice(1));
  1514. }
  1515. if (first === 'super') {
  1516. let supers = 0;
  1517. while (segments[supers] === 'super') supers++;
  1518. let dir: string | null = rustSelfModuleDir(fromAbs);
  1519. for (let s = 0; s < supers && dir; s++) dir = path.dirname(dir);
  1520. return resolveUnder(dir, segments.slice(supers));
  1521. }
  1522. // Bare path. In expression position (`submodule::item()` — the router-assembly
  1523. // and general cross-module-call pattern) the prefix is a SUBMODULE of the
  1524. // current module, i.e. 2018 `self::`-relative — so try self-relative FIRST.
  1525. // Fall back to crate-relative for 2015-edition / crate-root items. External
  1526. // crate paths (`serde::de::Error`) miss both and fall through to name-matching.
  1527. return (
  1528. resolveUnder(rustSelfModuleDir(fromAbs), segments) ??
  1529. resolveUnder(rustCrateRootDir(fromAbs, context), segments)
  1530. );
  1531. }
  1532. /**
  1533. * Resolve a Java/Kotlin reference whose receiver is the simple name of
  1534. * an imported FQN: `Foo.bar(...)` where `import com.example.Foo;`. The
  1535. * imported FQN converts to a file-path suffix (`com/example/Foo.java`
  1536. * or `.kt`) which uniquely identifies the right symbol when multiple
  1537. * classes share the same simple name.
  1538. *
  1539. * Also handles bare references to the imported class itself
  1540. * (`new Foo()` extraction emits `Foo` as a `references`/`instantiates`
  1541. * ref) and `import static <Foo>.bar` style imports of a single member.
  1542. */
  1543. function resolveJavaImportedReference(
  1544. ref: UnresolvedRef,
  1545. imports: ImportMapping[],
  1546. context: ResolutionContext
  1547. ): ResolvedRef | null {
  1548. if (imports.length === 0) return null;
  1549. const ext = ref.language === 'kotlin' ? '.kt' : '.java';
  1550. for (const imp of imports) {
  1551. const matchesBare = imp.localName === ref.referenceName;
  1552. const matchesQualified = ref.referenceName.startsWith(imp.localName + '.');
  1553. if (!matchesBare && !matchesQualified) continue;
  1554. // Convert FQN to a file-path suffix. `com.example.Foo` ->
  1555. // `com/example/Foo.java` (or `.kt`). The actual file may live
  1556. // under any source root (`src/main/java/`, `src/`, etc.), so match
  1557. // by suffix rather than exact path.
  1558. const fqnPath = imp.source.replace(/\./g, '/') + ext;
  1559. // Which symbol name to look up: the class itself, or a member.
  1560. const memberName = matchesBare
  1561. ? imp.localName
  1562. : ref.referenceName.substring(imp.localName.length + 1);
  1563. const candidates = context.getNodesByName(memberName);
  1564. for (const node of candidates) {
  1565. if (node.language !== ref.language) continue;
  1566. const fp = node.filePath.replace(/\\/g, '/');
  1567. if (fp.endsWith(fqnPath) || fp.endsWith('/' + fqnPath)) {
  1568. return {
  1569. original: ref,
  1570. targetNodeId: node.id,
  1571. confidence: 0.9,
  1572. resolvedBy: 'import',
  1573. };
  1574. }
  1575. }
  1576. // `import static com.example.Foo.bar;` — the FQN's tail is the
  1577. // member name, the part before is the owner class. Look up the
  1578. // member named `<imp.localName>` (e.g. `bar`) and prefer the
  1579. // candidate whose file matches the parent FQN's path.
  1580. if (matchesBare) {
  1581. const dot = imp.source.lastIndexOf('.');
  1582. if (dot > 0) {
  1583. const ownerFqn = imp.source.substring(0, dot);
  1584. const ownerPath = ownerFqn.replace(/\./g, '/') + ext;
  1585. for (const node of candidates) {
  1586. if (node.language !== ref.language) continue;
  1587. const fp = node.filePath.replace(/\\/g, '/');
  1588. if (fp.endsWith(ownerPath) || fp.endsWith('/' + ownerPath)) {
  1589. return {
  1590. original: ref,
  1591. targetNodeId: node.id,
  1592. confidence: 0.9,
  1593. resolvedBy: 'import',
  1594. };
  1595. }
  1596. }
  1597. }
  1598. }
  1599. }
  1600. return null;
  1601. }
  1602. /**
  1603. * Resolve a Go cross-package qualified reference (`pkga.FuncX`) by matching
  1604. * the package alias against an in-module import, stripping the module prefix
  1605. * to a project-relative directory, and locating the exported symbol in any
  1606. * `.go` file under that directory. Returns `null` for stdlib / third-party
  1607. * imports (no `go.mod`-relative match) so the rest of `resolveViaImport`
  1608. * can still try the file-based path.
  1609. */
  1610. function resolveGoCrossPackageReference(
  1611. ref: UnresolvedRef,
  1612. imports: ImportMapping[],
  1613. context: ResolutionContext
  1614. ): ResolvedRef | null {
  1615. const mod = context.getGoModule?.();
  1616. if (!mod) return null;
  1617. // Qualified call: receiver before `.`, member after. A bare reference
  1618. // (no dot) is a same-file/in-package call — handled elsewhere.
  1619. const dotIdx = ref.referenceName.indexOf('.');
  1620. if (dotIdx <= 0) return null;
  1621. const receiver = ref.referenceName.substring(0, dotIdx);
  1622. const memberName = ref.referenceName.substring(dotIdx + 1);
  1623. if (!memberName) return null;
  1624. for (const imp of imports) {
  1625. if (imp.localName !== receiver) continue;
  1626. // Only in-module imports map to a known directory.
  1627. if (imp.source !== mod.modulePath && !imp.source.startsWith(mod.modulePath + '/')) {
  1628. continue;
  1629. }
  1630. const pkgDir = imp.source === mod.modulePath
  1631. ? ''
  1632. : imp.source.substring(mod.modulePath.length + 1);
  1633. // Look up the member by name and pick the candidate whose file lives
  1634. // directly in the package directory. Match the immediate parent dir
  1635. // exactly so a call to `pkga.FuncX` doesn't accidentally land on a
  1636. // `FuncX` declared in `pkga/subpkg/`.
  1637. const candidates = context.getNodesByName(memberName);
  1638. for (const node of candidates) {
  1639. if (node.language !== 'go') continue;
  1640. if (!node.isExported) continue;
  1641. const fp = node.filePath.replace(/\\/g, '/');
  1642. const lastSlash = fp.lastIndexOf('/');
  1643. const fileDir = lastSlash >= 0 ? fp.substring(0, lastSlash) : '';
  1644. if (fileDir === pkgDir) {
  1645. return {
  1646. original: ref,
  1647. targetNodeId: node.id,
  1648. confidence: 0.9,
  1649. resolvedBy: 'import',
  1650. };
  1651. }
  1652. }
  1653. }
  1654. return null;
  1655. }
  1656. /** Recursive depth cap for re-export chain following. Real codebases
  1657. * rarely chain barrels more than 2–3 deep; 8 is a generous safety
  1658. * net that still bounds worst-case work. */
  1659. const REEXPORT_MAX_DEPTH = 8;
  1660. /**
  1661. * Find an exported symbol in `filePath`, following `export { x } from
  1662. * './other'` and `export * from './other'` chains until the original
  1663. * declaration is reached. Cycle-safe via the `visited` set.
  1664. *
  1665. * Without this, every barrel-style import (`import { Foo } from
  1666. * './index'` where `index.ts` only re-exports) used to resolve to
  1667. * nothing — the existing code only looked for declarations IN the
  1668. * resolved file, not declarations the file forwarded.
  1669. */
  1670. function findExportedSymbol(
  1671. filePath: string,
  1672. want: {
  1673. isDefault: boolean;
  1674. isNamespace: boolean;
  1675. exportedName: string;
  1676. memberName: string | null;
  1677. },
  1678. language: Language,
  1679. context: ResolutionContext,
  1680. visited: Set<string>,
  1681. depth = 0
  1682. ): Node | undefined {
  1683. if (depth > REEXPORT_MAX_DEPTH) return undefined;
  1684. if (visited.has(filePath)) return undefined;
  1685. visited.add(filePath);
  1686. const nodesInFile = context.getNodesInFile(filePath);
  1687. // 1. Direct hit: the symbol is declared in this file.
  1688. if (want.isDefault) {
  1689. // Svelte/Vue single-file components ARE the module's default export,
  1690. // but are extracted as kind 'component' (not function/class). Prefer
  1691. // the component node; fall back to an exported function/class for the
  1692. // `.ts`/`.tsx` `export default fn`/`class` case. Without the component
  1693. // branch, an `export { default as X } from './X.svelte'` barrel never
  1694. // resolves and the component shows a false 0 callers (#629).
  1695. const direct =
  1696. nodesInFile.find((n) => n.isExported && n.kind === 'component') ??
  1697. nodesInFile.find(
  1698. (n) => n.isExported && (n.kind === 'function' || n.kind === 'class')
  1699. );
  1700. if (direct) return direct;
  1701. } else if (want.isNamespace && want.memberName) {
  1702. const direct = nodesInFile.find(
  1703. (n) => n.name === want.memberName && n.isExported
  1704. );
  1705. if (direct) return direct;
  1706. } else {
  1707. const direct = nodesInFile.find(
  1708. (n) => n.name === want.exportedName && n.isExported
  1709. );
  1710. if (direct) return direct;
  1711. }
  1712. // 2. Re-export hit: the file forwards the symbol to another module.
  1713. const reExports = context.getReExports?.(filePath, language) ?? [];
  1714. if (reExports.length === 0) return undefined;
  1715. // Look for explicit `export { want } from './other'` (with optional rename).
  1716. const targetName = want.isDefault ? 'default' : want.exportedName;
  1717. for (const rex of reExports) {
  1718. if (rex.kind === 'named' && rex.exportedName === targetName) {
  1719. const next = resolveImportPath(rex.source, filePath, language, context);
  1720. if (!next) continue;
  1721. // After rename: `export { foo as bar } from './x'` — to chase
  1722. // `bar`, we look for `foo` in `./x`.
  1723. const chained = findExportedSymbol(
  1724. next,
  1725. {
  1726. isDefault: rex.originalName === 'default',
  1727. isNamespace: false,
  1728. exportedName: rex.originalName,
  1729. memberName: null,
  1730. },
  1731. language,
  1732. context,
  1733. visited,
  1734. depth + 1
  1735. );
  1736. if (chained) return chained;
  1737. }
  1738. }
  1739. // 3. Wildcard re-export: `export * from './other'` — try every
  1740. // forwarding source. This is the barrel-of-barrels case.
  1741. for (const rex of reExports) {
  1742. if (rex.kind === 'wildcard') {
  1743. const next = resolveImportPath(rex.source, filePath, language, context);
  1744. if (!next) continue;
  1745. const chained = findExportedSymbol(next, want, language, context, visited, depth + 1);
  1746. if (chained) return chained;
  1747. }
  1748. }
  1749. return undefined;
  1750. }