import-resolver.ts 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  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. /**
  12. * Extension resolution order by language
  13. */
  14. const EXTENSION_RESOLUTION: Record<string, string[]> = {
  15. typescript: ['.ts', '.tsx', '.d.ts', '.js', '.jsx', '/index.ts', '/index.tsx', '/index.js'],
  16. javascript: ['.js', '.jsx', '.mjs', '.cjs', '/index.js', '/index.jsx'],
  17. tsx: ['.tsx', '.ts', '.d.ts', '.js', '.jsx', '/index.tsx', '/index.ts', '/index.js'],
  18. jsx: ['.jsx', '.js', '/index.jsx', '/index.js'],
  19. python: ['.py', '/__init__.py'],
  20. go: ['.go'],
  21. rust: ['.rs', '/mod.rs'],
  22. java: ['.java'],
  23. c: ['.h', '.c'],
  24. cpp: ['.h', '.hpp', '.hxx', '.cpp', '.cc', '.cxx'],
  25. csharp: ['.cs'],
  26. php: ['.php'],
  27. ruby: ['.rb'],
  28. objc: ['.h', '.m', '.mm'],
  29. };
  30. /**
  31. * Resolve an import path to an actual file
  32. */
  33. export function resolveImportPath(
  34. importPath: string,
  35. fromFile: string,
  36. language: Language,
  37. context: ResolutionContext
  38. ): string | null {
  39. // Skip external/npm packages — but pass the context so the
  40. // bare-specifier heuristic can consult the project's tsconfig
  41. // alias map first (custom prefixes like `@components/*` would
  42. // otherwise be misclassified as npm).
  43. if (isExternalImport(importPath, language, context)) {
  44. return null;
  45. }
  46. const projectRoot = context.getProjectRoot();
  47. const fromDir = path.dirname(path.join(projectRoot, fromFile));
  48. // Handle relative imports
  49. if (importPath.startsWith('.')) {
  50. return resolveRelativeImport(importPath, fromDir, language, context);
  51. }
  52. // Handle absolute/aliased imports (like @/ or src/)
  53. const aliased = resolveAliasedImport(importPath, projectRoot, language, context);
  54. if (aliased) return aliased;
  55. // C/C++ include directory search: when neither relative nor aliased
  56. // resolution found a match, search -I directories from
  57. // compile_commands.json or heuristic probing.
  58. if (language === 'c' || language === 'cpp') {
  59. return resolveCppIncludePath(importPath, language, context);
  60. }
  61. return null;
  62. }
  63. /**
  64. * C and C++ standard library header names (without delimiters).
  65. * Used by isExternalImport to filter system includes from resolution.
  66. */
  67. const C_CPP_STDLIB_HEADERS = new Set([
  68. // C standard library headers
  69. 'assert.h', 'complex.h', 'ctype.h', 'errno.h', 'fenv.h', 'float.h',
  70. 'inttypes.h', 'iso646.h', 'limits.h', 'locale.h', 'math.h', 'setjmp.h',
  71. 'signal.h', 'stdalign.h', 'stdarg.h', 'stdatomic.h', 'stdbool.h',
  72. 'stddef.h', 'stdint.h', 'stdio.h', 'stdlib.h', 'stdnoreturn.h',
  73. 'string.h', 'tgmath.h', 'threads.h', 'time.h', 'uchar.h', 'wchar.h',
  74. 'wctype.h',
  75. // C++ C-library wrappers (cname form)
  76. 'cassert', 'ccomplex', 'cctype', 'cerrno', 'cfenv', 'cfloat',
  77. 'cinttypes', 'ciso646', 'climits', 'clocale', 'cmath', 'csetjmp',
  78. 'csignal', 'cstdalign', 'cstdarg', 'cstdbool', 'cstddef', 'cstdint',
  79. 'cstdio', 'cstdlib', 'cstring', 'ctgmath', 'ctime', 'cuchar',
  80. 'cwchar', 'cwctype',
  81. // C++ STL headers
  82. 'algorithm', 'any', 'array', 'atomic', 'barrier', 'bit', 'bitset',
  83. 'charconv', 'chrono', 'codecvt', 'compare', 'complex', 'concepts',
  84. 'condition_variable', 'coroutine', 'deque', 'exception', 'execution',
  85. 'expected', 'filesystem', 'format', 'forward_list', 'fstream',
  86. 'functional', 'future', 'generator', 'initializer_list', 'iomanip',
  87. 'ios', 'iosfwd', 'iostream', 'istream', 'iterator', 'latch',
  88. 'limits', 'list', 'locale', 'map', 'mdspan', 'memory', 'memory_resource',
  89. 'mutex', 'new', 'numbers', 'numeric', 'optional', 'ostream', 'print',
  90. 'queue', 'random', 'ranges', 'ratio', 'regex', 'scoped_allocator',
  91. 'semaphore', 'set', 'shared_mutex', 'source_location', 'span',
  92. 'spanstream', 'sstream', 'stack', 'stacktrace', 'stdexcept',
  93. 'stdfloat', 'stop_token', 'streambuf', 'string', 'string_view',
  94. 'strstream', 'syncstream', 'system_error', 'thread', 'tuple',
  95. 'type_traits', 'typeindex', 'typeinfo', 'unordered_map',
  96. 'unordered_set', 'utility', 'valarray', 'variant', 'vector',
  97. 'version',
  98. ]);
  99. /**
  100. * Check if an import is external (npm package, etc.)
  101. *
  102. * `context` is consulted for project-defined path aliases
  103. * (tsconfig/jsconfig `paths`). Without that check, custom prefixes
  104. * like `@components/*` would fail the bare-specifier heuristic and
  105. * be classified as external before alias resolution can run.
  106. */
  107. function isExternalImport(
  108. importPath: string,
  109. language: Language,
  110. context?: ResolutionContext
  111. ): boolean {
  112. // Relative imports are not external
  113. if (importPath.startsWith('.')) {
  114. return false;
  115. }
  116. // Common external patterns
  117. if (language === 'typescript' || language === 'javascript' || language === 'tsx' || language === 'jsx') {
  118. // Node built-ins
  119. if (['fs', 'path', 'os', 'crypto', 'http', 'https', 'url', 'util', 'events', 'stream', 'child_process', 'buffer'].includes(importPath)) {
  120. return true;
  121. }
  122. // Project-defined alias prefix? Treat as local.
  123. const aliases = context?.getProjectAliases?.();
  124. if (aliases) {
  125. for (const pat of aliases.patterns) {
  126. if (importPath.startsWith(pat.prefix)) return false;
  127. }
  128. }
  129. // Scoped packages or bare specifiers that don't start with aliases
  130. if (!importPath.startsWith('@/') && !importPath.startsWith('~/') && !importPath.startsWith('src/')) {
  131. // Likely an npm package
  132. return true;
  133. }
  134. }
  135. if (language === 'python') {
  136. // Standard library modules
  137. const stdLibs = ['os', 'sys', 'json', 're', 'math', 'datetime', 'collections', 'typing', 'pathlib', 'logging'];
  138. if (stdLibs.includes(importPath.split('.')[0]!)) {
  139. return true;
  140. }
  141. }
  142. if (language === 'go') {
  143. // Relative imports (rare in idiomatic Go but the grammar allows them).
  144. if (importPath.startsWith('.')) {
  145. return false;
  146. }
  147. // In-module imports look like `<module-path>/sub/pkg` — local to
  148. // this project. Without the module-path check we'd flag every
  149. // cross-package call in a Go monorepo as external (issue #388).
  150. const mod = context?.getGoModule?.();
  151. if (mod && (importPath === mod.modulePath || importPath.startsWith(mod.modulePath + '/'))) {
  152. return false;
  153. }
  154. // `internal/` packages stay local even when go.mod is missing —
  155. // preserves the pre-#388 escape hatch for repos without a parsed module path.
  156. if (importPath.includes('/internal/')) {
  157. return false;
  158. }
  159. // Anything else is the Go standard library or a third-party module.
  160. return true;
  161. }
  162. if (language === 'c' || language === 'cpp') {
  163. // C/C++ standard library headers — both C-style (<stdio.h>) and
  164. // C++-style (<cstdio>, <vector>) forms. Checked against the import
  165. // path (which the extractor strips of <> or "" delimiters).
  166. if (C_CPP_STDLIB_HEADERS.has(importPath)) return true;
  167. // C++ headers without .h extension (e.g. "vector", "string")
  168. const withoutExt = importPath.replace(/\.h$/, '');
  169. if (C_CPP_STDLIB_HEADERS.has(withoutExt)) return true;
  170. }
  171. return false;
  172. }
  173. /**
  174. * Resolve a relative import
  175. */
  176. function resolveRelativeImport(
  177. importPath: string,
  178. fromDir: string,
  179. language: Language,
  180. context: ResolutionContext
  181. ): string | null {
  182. const projectRoot = context.getProjectRoot();
  183. const extensions = EXTENSION_RESOLUTION[language] || [];
  184. // Try the path as-is first
  185. const basePath = path.resolve(fromDir, importPath);
  186. const relativePath = path.relative(projectRoot, basePath).replace(/\\/g, '/');
  187. // Try each extension
  188. for (const ext of extensions) {
  189. const candidatePath = relativePath + ext;
  190. if (context.fileExists(candidatePath)) {
  191. return candidatePath;
  192. }
  193. }
  194. // Try without extension (might already have one)
  195. if (context.fileExists(relativePath)) {
  196. return relativePath;
  197. }
  198. return null;
  199. }
  200. /**
  201. * Resolve an aliased/absolute import.
  202. *
  203. * Tries, in order:
  204. * 1. Project-defined `compilerOptions.paths` (tsconfig/jsconfig).
  205. * Each pattern can have multiple replacements; tried in tsconfig
  206. * priority order with extension permutations.
  207. * 2. The legacy hard-coded fallback list (`@/`, `~/`, `src/`, ...)
  208. * for projects that have aliases but no tsconfig paths block.
  209. * 3. Direct path lookup (with extensions).
  210. */
  211. function resolveAliasedImport(
  212. importPath: string,
  213. projectRoot: string,
  214. language: Language,
  215. context: ResolutionContext
  216. ): string | null {
  217. const extensions = EXTENSION_RESOLUTION[language] || [];
  218. const tryWithExt = (basePath: string): string | null => {
  219. for (const ext of extensions) {
  220. const candidate = basePath + ext;
  221. if (context.fileExists(candidate)) return candidate;
  222. }
  223. if (context.fileExists(basePath)) return basePath;
  224. return null;
  225. };
  226. // 1. Project tsconfig/jsconfig paths.
  227. const aliasMap = context.getProjectAliases?.();
  228. if (aliasMap) {
  229. const candidates = applyAliases(importPath, aliasMap, projectRoot);
  230. for (const c of candidates) {
  231. const hit = tryWithExt(c);
  232. if (hit) return hit;
  233. }
  234. }
  235. // 2. Hard-coded fallback list. Kept for projects that use these
  236. // conventional aliases without declaring them in tsconfig.
  237. const fallbackAliases: Record<string, string> = {
  238. '@/': 'src/',
  239. '~/': 'src/',
  240. '@src/': 'src/',
  241. 'src/': 'src/',
  242. '@app/': 'app/',
  243. 'app/': 'app/',
  244. };
  245. for (const [alias, replacement] of Object.entries(fallbackAliases)) {
  246. if (importPath.startsWith(alias)) {
  247. const hit = tryWithExt(importPath.replace(alias, replacement));
  248. if (hit) return hit;
  249. }
  250. }
  251. // 3. Direct path.
  252. return tryWithExt(importPath);
  253. }
  254. /**
  255. * C/C++ include directory cache (keyed by project root).
  256. * Loaded once per resolver instance, shared across calls.
  257. */
  258. const cppIncludeDirCache = new Map<string, string[]>();
  259. /**
  260. * Clear the C/C++ include directory cache (call between indexing runs)
  261. */
  262. export function clearCppIncludeDirCache(): void {
  263. cppIncludeDirCache.clear();
  264. }
  265. /**
  266. * Discover C/C++ include search directories for a project.
  267. *
  268. * Strategy:
  269. * 1. Look for compile_commands.json (Clang compilation database) in the
  270. * project root and common build subdirectories. Parse -I and -isystem
  271. * flags from compiler commands.
  272. * 2. If no compilation database is found, probe for common convention
  273. * directories (include/, src/, lib/, api/) and top-level directories
  274. * containing .h/.hpp files.
  275. *
  276. * Returns paths relative to projectRoot.
  277. */
  278. export function loadCppIncludeDirs(projectRoot: string): string[] {
  279. const cached = cppIncludeDirCache.get(projectRoot);
  280. if (cached !== undefined) return cached;
  281. const dirs = loadCppIncludeDirsFromCompileDB(projectRoot)
  282. || loadCppIncludeDirsHeuristic(projectRoot);
  283. cppIncludeDirCache.set(projectRoot, dirs);
  284. return dirs;
  285. }
  286. /**
  287. * Try to load include directories from compile_commands.json.
  288. * Returns null if no compilation database is found (so the heuristic
  289. * fallback can run). Returns an array (possibly empty) otherwise.
  290. */
  291. function loadCppIncludeDirsFromCompileDB(projectRoot: string): string[] | null {
  292. const candidates = [
  293. path.join(projectRoot, 'compile_commands.json'),
  294. path.join(projectRoot, 'build', 'compile_commands.json'),
  295. path.join(projectRoot, 'cmake-build-debug', 'compile_commands.json'),
  296. path.join(projectRoot, 'cmake-build-release', 'compile_commands.json'),
  297. path.join(projectRoot, 'out', 'compile_commands.json'),
  298. ];
  299. let dbPath: string | undefined;
  300. for (const c of candidates) {
  301. try {
  302. if (fs.existsSync(c)) {
  303. dbPath = c;
  304. break;
  305. }
  306. } catch {
  307. // ignore
  308. }
  309. }
  310. if (!dbPath) return null;
  311. try {
  312. const content = fs.readFileSync(dbPath, 'utf-8');
  313. const entries = JSON.parse(content) as Array<{
  314. directory: string;
  315. command?: string;
  316. arguments?: string[];
  317. }>;
  318. if (!Array.isArray(entries)) return null;
  319. const dirSet = new Set<string>();
  320. for (const entry of entries) {
  321. const dir = entry.directory || projectRoot;
  322. const args = entry.arguments || (entry.command ? shlexSplit(entry.command) : []);
  323. for (let i = 0; i < args.length; i++) {
  324. const arg = args[i]!;
  325. let includeDir: string | undefined;
  326. // -I<dir> (no space)
  327. if (arg.startsWith('-I') && arg.length > 2) {
  328. includeDir = arg.substring(2);
  329. }
  330. // -isystem <dir> (space-separated)
  331. else if ((arg === '-isystem' || arg === '-I') && i + 1 < args.length) {
  332. includeDir = args[i + 1];
  333. i++; // skip next arg
  334. }
  335. if (includeDir) {
  336. // Normalize: resolve relative to the compilation directory
  337. const absPath = path.isAbsolute(includeDir)
  338. ? includeDir
  339. : path.resolve(dir, includeDir);
  340. const relPath = path.relative(projectRoot, absPath).replace(/\\/g, '/');
  341. // Skip system directories and paths outside the project
  342. // (relative paths starting with .. or absolute paths like
  343. // /usr/include or C:\usr on Windows)
  344. if (!relPath.startsWith('..') && relPath.length > 0 && !path.isAbsolute(relPath)) {
  345. dirSet.add(relPath);
  346. }
  347. }
  348. }
  349. }
  350. return Array.from(dirSet);
  351. } catch {
  352. return null;
  353. }
  354. }
  355. /**
  356. * Minimal shlex-style split for compiler command strings.
  357. * Handles double-quoted and single-quoted arguments.
  358. */
  359. function shlexSplit(cmd: string): string[] {
  360. const result: string[] = [];
  361. let i = 0;
  362. while (i < cmd.length) {
  363. // Skip whitespace
  364. while (i < cmd.length && /\s/.test(cmd[i]!)) i++;
  365. if (i >= cmd.length) break;
  366. const ch = cmd[i]!;
  367. if (ch === '"') {
  368. i++;
  369. let arg = '';
  370. while (i < cmd.length && cmd[i] !== '"') {
  371. if (cmd[i] === '\\' && i + 1 < cmd.length) { i++; arg += cmd[i]; }
  372. else { arg += cmd[i]; }
  373. i++;
  374. }
  375. i++; // closing quote
  376. result.push(arg);
  377. } else if (ch === "'") {
  378. i++;
  379. let arg = '';
  380. while (i < cmd.length && cmd[i] !== "'") { arg += cmd[i]; i++; }
  381. i++; // closing quote
  382. result.push(arg);
  383. } else {
  384. let arg = '';
  385. while (i < cmd.length && !/\s/.test(cmd[i]!)) { arg += cmd[i]; i++; }
  386. result.push(arg);
  387. }
  388. }
  389. return result;
  390. }
  391. /**
  392. * Heuristic include directory discovery when no compile_commands.json exists.
  393. * Checks common convention directories and scans top-level dirs for headers.
  394. */
  395. function loadCppIncludeDirsHeuristic(projectRoot: string): string[] {
  396. const dirs: string[] = [];
  397. const conventionDirs = ['include', 'src', 'lib', 'api', 'inc'];
  398. try {
  399. const entries = fs.readdirSync(projectRoot, { withFileTypes: true });
  400. for (const entry of entries) {
  401. if (!entry.isDirectory()) continue;
  402. const name = entry.name;
  403. // Convention directories
  404. if (conventionDirs.includes(name.toLowerCase())) {
  405. dirs.push(name);
  406. continue;
  407. }
  408. // Any top-level directory containing .h or .hpp files
  409. try {
  410. const subFiles = fs.readdirSync(path.join(projectRoot, name));
  411. if (subFiles.some(f => /\.(h|hpp|hxx|hh)$/i.test(f))) {
  412. dirs.push(name);
  413. }
  414. } catch {
  415. // ignore permission errors
  416. }
  417. }
  418. } catch {
  419. // ignore
  420. }
  421. return dirs;
  422. }
  423. /**
  424. * Resolve a C/C++ include path by searching include directories.
  425. * Called as a fallback after relative and aliased resolution fail.
  426. */
  427. function resolveCppIncludePath(
  428. importPath: string,
  429. language: Language,
  430. context: ResolutionContext
  431. ): string | null {
  432. const includeDirs = context.getCppIncludeDirs?.() ?? [];
  433. const extensions = EXTENSION_RESOLUTION[language] ?? [];
  434. for (const dir of includeDirs) {
  435. const normalizedDir = dir.replace(/\\/g, '/');
  436. for (const ext of extensions) {
  437. const candidate = normalizedDir + '/' + importPath + ext;
  438. if (context.fileExists(candidate)) return candidate;
  439. }
  440. // Try as-is (already has extension)
  441. const candidate = normalizedDir + '/' + importPath;
  442. if (context.fileExists(candidate)) return candidate;
  443. }
  444. return null;
  445. }
  446. /**
  447. * Extract import mappings from a file
  448. */
  449. export function extractImportMappings(
  450. _filePath: string,
  451. content: string,
  452. language: Language
  453. ): ImportMapping[] {
  454. const mappings: ImportMapping[] = [];
  455. if (language === 'typescript' || language === 'javascript' || language === 'tsx' || language === 'jsx') {
  456. mappings.push(...extractJSImports(content));
  457. } else if (language === 'python') {
  458. mappings.push(...extractPythonImports(content));
  459. } else if (language === 'go') {
  460. mappings.push(...extractGoImports(content));
  461. } else if (language === 'java' || language === 'kotlin') {
  462. mappings.push(...extractJavaImports(content));
  463. } else if (language === 'php') {
  464. mappings.push(...extractPHPImports(content));
  465. } else if (language === 'c' || language === 'cpp') {
  466. mappings.push(...extractCppImports(content));
  467. }
  468. return mappings;
  469. }
  470. /**
  471. * Extract JS/TS import mappings
  472. */
  473. function extractJSImports(content: string): ImportMapping[] {
  474. const mappings: ImportMapping[] = [];
  475. // ES6 imports
  476. const importRegex = /import\s+(?:(\w+)\s*,?\s*)?(?:\{([^}]+)\})?\s*(?:(\*)\s+as\s+(\w+))?\s*from\s*['"]([^'"]+)['"]/g;
  477. let match;
  478. while ((match = importRegex.exec(content)) !== null) {
  479. const [, defaultImport, namedImports, star, namespaceAlias, source] = match;
  480. // Default import
  481. if (defaultImport) {
  482. mappings.push({
  483. localName: defaultImport,
  484. exportedName: 'default',
  485. source: source!,
  486. isDefault: true,
  487. isNamespace: false,
  488. });
  489. }
  490. // Named imports
  491. if (namedImports) {
  492. const names = namedImports.split(',').map((s) => s.trim());
  493. for (const name of names) {
  494. const aliasMatch = name.match(/(\w+)\s+as\s+(\w+)/);
  495. if (aliasMatch) {
  496. mappings.push({
  497. localName: aliasMatch[2]!,
  498. exportedName: aliasMatch[1]!,
  499. source: source!,
  500. isDefault: false,
  501. isNamespace: false,
  502. });
  503. } else if (name) {
  504. mappings.push({
  505. localName: name,
  506. exportedName: name,
  507. source: source!,
  508. isDefault: false,
  509. isNamespace: false,
  510. });
  511. }
  512. }
  513. }
  514. // Namespace import
  515. if (star && namespaceAlias) {
  516. mappings.push({
  517. localName: namespaceAlias,
  518. exportedName: '*',
  519. source: source!,
  520. isDefault: false,
  521. isNamespace: true,
  522. });
  523. }
  524. }
  525. // Require statements
  526. const requireRegex = /(?:const|let|var)\s+(?:(\w+)|{([^}]+)})\s*=\s*require\(['"]([^'"]+)['"]\)/g;
  527. while ((match = requireRegex.exec(content)) !== null) {
  528. const [, defaultName, destructured, source] = match;
  529. if (defaultName) {
  530. mappings.push({
  531. localName: defaultName,
  532. exportedName: 'default',
  533. source: source!,
  534. isDefault: true,
  535. isNamespace: false,
  536. });
  537. }
  538. if (destructured) {
  539. const names = destructured.split(',').map((s) => s.trim());
  540. for (const name of names) {
  541. const aliasMatch = name.match(/(\w+)\s*:\s*(\w+)/);
  542. if (aliasMatch) {
  543. mappings.push({
  544. localName: aliasMatch[2]!,
  545. exportedName: aliasMatch[1]!,
  546. source: source!,
  547. isDefault: false,
  548. isNamespace: false,
  549. });
  550. } else if (name) {
  551. mappings.push({
  552. localName: name,
  553. exportedName: name,
  554. source: source!,
  555. isDefault: false,
  556. isNamespace: false,
  557. });
  558. }
  559. }
  560. }
  561. }
  562. return mappings;
  563. }
  564. /**
  565. * Extract Python import mappings
  566. */
  567. function extractPythonImports(content: string): ImportMapping[] {
  568. const mappings: ImportMapping[] = [];
  569. // from X import Y
  570. const fromImportRegex = /from\s+([\w.]+)\s+import\s+([^#\n]+)/g;
  571. let match;
  572. while ((match = fromImportRegex.exec(content)) !== null) {
  573. const [, source, imports] = match;
  574. const names = imports!.split(',').map((s) => s.trim());
  575. for (const name of names) {
  576. const aliasMatch = name.match(/(\w+)\s+as\s+(\w+)/);
  577. if (aliasMatch) {
  578. mappings.push({
  579. localName: aliasMatch[2]!,
  580. exportedName: aliasMatch[1]!,
  581. source: source!,
  582. isDefault: false,
  583. isNamespace: false,
  584. });
  585. } else if (name && name !== '*') {
  586. mappings.push({
  587. localName: name,
  588. exportedName: name,
  589. source: source!,
  590. isDefault: false,
  591. isNamespace: false,
  592. });
  593. }
  594. }
  595. }
  596. // import X
  597. const importRegex = /^import\s+([\w.]+)(?:\s+as\s+(\w+))?/gm;
  598. while ((match = importRegex.exec(content)) !== null) {
  599. const [, source, alias] = match;
  600. const localName = alias || source!.split('.').pop()!;
  601. mappings.push({
  602. localName,
  603. exportedName: '*',
  604. source: source!,
  605. isDefault: false,
  606. isNamespace: true,
  607. });
  608. }
  609. return mappings;
  610. }
  611. /**
  612. * Extract Go import mappings
  613. */
  614. function extractGoImports(content: string): ImportMapping[] {
  615. const mappings: ImportMapping[] = [];
  616. // import "path" or import alias "path"
  617. const singleImportRegex = /import\s+(?:(\w+)\s+)?["']([^"']+)["']/g;
  618. let match;
  619. while ((match = singleImportRegex.exec(content)) !== null) {
  620. const [, alias, source] = match;
  621. const packageName = source!.split('/').pop()!;
  622. mappings.push({
  623. localName: alias || packageName,
  624. exportedName: '*',
  625. source: source!,
  626. isDefault: false,
  627. isNamespace: true,
  628. });
  629. }
  630. // import ( ... ) block
  631. const blockImportRegex = /import\s*\(\s*([^)]+)\s*\)/gs;
  632. while ((match = blockImportRegex.exec(content)) !== null) {
  633. const block = match[1]!;
  634. const lineRegex = /(?:(\w+)\s+)?["']([^"']+)["']/g;
  635. let lineMatch;
  636. while ((lineMatch = lineRegex.exec(block)) !== null) {
  637. const [, alias, source] = lineMatch;
  638. const packageName = source!.split('/').pop()!;
  639. mappings.push({
  640. localName: alias || packageName,
  641. exportedName: '*',
  642. source: source!,
  643. isDefault: false,
  644. isNamespace: true,
  645. });
  646. }
  647. }
  648. return mappings;
  649. }
  650. /**
  651. * Extract Java / Kotlin import mappings.
  652. *
  653. * Java/Kotlin imports carry the full qualified name of the imported
  654. * symbol — `import com.example.dao.converter.FooConverter;` — which is
  655. * exactly the disambiguation signal we need when two packages both
  656. * declare a `FooConverter`. Pre-#314 the resolver had no Java branch
  657. * here at all, so this mapping was empty and cross-module name
  658. * collisions were resolved by file-path proximity (often wrongly).
  659. *
  660. * `import static com.example.Foo.bar;` is parsed as a local-name `bar`
  661. * pointing at FQN `com.example.Foo.bar` so static-method call sites
  662. * (`bar(...)`) can resolve through the same import lookup.
  663. */
  664. function extractJavaImports(content: string): ImportMapping[] {
  665. const mappings: ImportMapping[] = [];
  666. // Strip line and block comments so `// import foo;` doesn't false-match.
  667. const stripped = content
  668. .replace(/\/\*[\s\S]*?\*\//g, '')
  669. .replace(/\/\/[^\n]*/g, '');
  670. // `import [static] <fqn>[.*];`
  671. const re = /^\s*import\s+(static\s+)?([\w.]+(?:\.\*)?)\s*;/gm;
  672. let match: RegExpExecArray | null;
  673. while ((match = re.exec(stripped)) !== null) {
  674. const fqn = match[2]!;
  675. // `import com.example.*;` — wildcard. We can't materialize a single
  676. // local name; skip and let name-matching handle members reachable
  677. // through the wildcard. (Future enhancement: enumerate package files.)
  678. if (fqn.endsWith('.*')) continue;
  679. const parts = fqn.split('.');
  680. const localName = parts[parts.length - 1];
  681. if (!localName) continue;
  682. mappings.push({
  683. localName,
  684. exportedName: localName,
  685. source: fqn,
  686. isDefault: false,
  687. isNamespace: false,
  688. });
  689. }
  690. return mappings;
  691. }
  692. /**
  693. * Extract PHP import mappings (use statements)
  694. */
  695. function extractPHPImports(content: string): ImportMapping[] {
  696. const mappings: ImportMapping[] = [];
  697. // use Namespace\Class; or use Namespace\Class as Alias;
  698. const useRegex = /use\s+([\w\\]+)(?:\s+as\s+(\w+))?;/g;
  699. let match;
  700. while ((match = useRegex.exec(content)) !== null) {
  701. const [, fullPath, alias] = match;
  702. const className = fullPath!.split('\\').pop()!;
  703. mappings.push({
  704. localName: alias || className,
  705. exportedName: className,
  706. source: fullPath!,
  707. isDefault: false,
  708. isNamespace: false,
  709. });
  710. }
  711. return mappings;
  712. }
  713. /**
  714. * Extract C/C++ import mappings from #include directives.
  715. *
  716. * #include brings all symbols from the included header into scope
  717. * (namespace import), so each mapping uses isNamespace: true and
  718. * exportedName: '*'. The localName is set to the header's basename
  719. * without extension so that symbol references like `MyClass` can
  720. * match against any include that might provide it.
  721. */
  722. function extractCppImports(content: string): ImportMapping[] {
  723. const mappings: ImportMapping[] = [];
  724. // Match both #include <...> and #include "..."
  725. const includeRegex = /^\s*#\s*include\s+[<"]([^>"]+)[>"]/gm;
  726. let match;
  727. while ((match = includeRegex.exec(content)) !== null) {
  728. const modulePath = match[1]!;
  729. // Basename without extension for localName matching
  730. const basename = modulePath.split('/').pop()!.replace(/\.(h|hpp|hxx|hh|inl|ipp|cxx|cc|cpp)$/,'');
  731. mappings.push({
  732. localName: basename || modulePath,
  733. exportedName: '*',
  734. source: modulePath,
  735. isDefault: false,
  736. isNamespace: true,
  737. });
  738. }
  739. return mappings;
  740. }
  741. // Cache import mappings per file to avoid re-reading and re-parsing
  742. const importMappingCache = new Map<string, ImportMapping[]>();
  743. /**
  744. * Clear the import mapping cache (call between indexing runs)
  745. */
  746. export function clearImportMappingCache(): void {
  747. importMappingCache.clear();
  748. cppIncludeDirCache.clear();
  749. }
  750. /**
  751. * Strip JS line + block comments from `content` while preserving
  752. * string literals (so `"//"` inside a string stays intact). Used by
  753. * {@link extractReExports} so commented-out export-from statements
  754. * don't generate phantom re-export edges.
  755. *
  756. * Scanner is deliberately small: it only tracks the three contexts
  757. * relevant for JS/TS — single-quote string, double-quote string, and
  758. * template literal. Comment recognition is the JS spec subset, no
  759. * regex-literal awareness (which is fine for our use case: we don't
  760. * apply this to function bodies, only to top-level files).
  761. */
  762. function stripJsComments(content: string): string {
  763. let out = '';
  764. let i = 0;
  765. let str: '"' | "'" | '`' | null = null;
  766. while (i < content.length) {
  767. const ch = content[i]!;
  768. if (str !== null) {
  769. out += ch;
  770. if (ch === '\\' && i + 1 < content.length) {
  771. out += content[i + 1]!;
  772. i += 2;
  773. continue;
  774. }
  775. if (ch === str) str = null;
  776. i++;
  777. continue;
  778. }
  779. if (ch === '"' || ch === "'" || ch === '`') {
  780. str = ch;
  781. out += ch;
  782. i++;
  783. continue;
  784. }
  785. if (ch === '/' && content[i + 1] === '/') {
  786. while (i < content.length && content[i] !== '\n') i++;
  787. continue;
  788. }
  789. if (ch === '/' && content[i + 1] === '*') {
  790. i += 2;
  791. while (i < content.length && !(content[i] === '*' && content[i + 1] === '/')) i++;
  792. i += 2;
  793. continue;
  794. }
  795. out += ch;
  796. i++;
  797. }
  798. return out;
  799. }
  800. /**
  801. * Extract JS/TS re-export declarations from `content`.
  802. *
  803. * Recognised forms:
  804. * export { foo } from './a';
  805. * export { foo as bar } from './a';
  806. * export * from './a';
  807. * export * as ns from './a'; (treated as wildcard for chasing)
  808. * export { default as Foo } from './a';
  809. *
  810. * The walker intentionally stays regex-based — the import-resolver
  811. * elsewhere in this file already chooses regex over a fresh
  812. * tree-sitter pass, and this function shares that trade-off. Errors
  813. * fall through silently; resolution simply skips the broken file.
  814. */
  815. export function extractReExports(content: string, language: Language): ReExport[] {
  816. if (
  817. language !== 'typescript' &&
  818. language !== 'javascript' &&
  819. language !== 'tsx' &&
  820. language !== 'jsx'
  821. ) {
  822. return [];
  823. }
  824. const out: ReExport[] = [];
  825. // Pre-strip block comments + line comments so a commented-out
  826. // `// export { x } from '...'` doesn't produce a phantom edge.
  827. // (Template literals are still a possible source of false positives;
  828. // a project that builds export statements as runtime strings is
  829. // out of scope.)
  830. const cleaned = stripJsComments(content);
  831. // Wildcard: `export * from '...'` or `export * as ns from '...'`
  832. const wildcardRe = /export\s*\*(?:\s+as\s+\w+)?\s*from\s*['"]([^'"]+)['"]/g;
  833. let m: RegExpExecArray | null;
  834. while ((m = wildcardRe.exec(cleaned)) !== null) {
  835. out.push({ kind: 'wildcard', source: m[1]! });
  836. }
  837. // Named: `export { a, b as c } from '...'`
  838. const namedRe = /export\s*\{([^}]+)\}\s*from\s*['"]([^'"]+)['"]/g;
  839. while ((m = namedRe.exec(cleaned)) !== null) {
  840. const inner = m[1]!;
  841. const source = m[2]!;
  842. for (const raw of inner.split(',')) {
  843. const item = raw.trim();
  844. if (!item) continue;
  845. const aliasMatch = item.match(/^(\w+)\s+as\s+(\w+)$/);
  846. if (aliasMatch) {
  847. out.push({
  848. kind: 'named',
  849. exportedName: aliasMatch[2]!,
  850. originalName: aliasMatch[1]!,
  851. source,
  852. });
  853. } else if (/^\w+$/.test(item)) {
  854. out.push({
  855. kind: 'named',
  856. exportedName: item,
  857. originalName: item,
  858. source,
  859. });
  860. }
  861. }
  862. }
  863. return out;
  864. }
  865. /**
  866. * Resolve a reference using import mappings
  867. */
  868. /**
  869. * JVM (Java / Kotlin) imports use fully-qualified names (`import
  870. * com.example.foo.Bar`) decoupled from filenames, so the JS/Python
  871. * style filesystem path lookup misses them whenever the file isn't
  872. * named after its primary symbol (Kotlin `Utils.kt` exporting `Bar`,
  873. * top-level fns, extension fns). Resolve them through the
  874. * `qualifiedName` index instead — populated by the package_header /
  875. * package_declaration namespace wrappers in the extractor.
  876. */
  877. export function resolveJvmImport(
  878. ref: UnresolvedRef,
  879. context: ResolutionContext
  880. ): ResolvedRef | null {
  881. if (ref.referenceKind !== 'imports') return null;
  882. if (ref.language !== 'java' && ref.language !== 'kotlin') return null;
  883. const fqn = ref.referenceName;
  884. const lastDot = fqn.lastIndexOf('.');
  885. if (lastDot <= 0) return null;
  886. const pkg = fqn.substring(0, lastDot);
  887. const sym = fqn.substring(lastDot + 1);
  888. // Wildcard imports (`com.example.*`) deliberately punt to name-matcher.
  889. if (sym === '*') return null;
  890. const candidates = context.getNodesByQualifiedName(`${pkg}::${sym}`);
  891. if (candidates.length === 0) return null;
  892. return {
  893. original: ref,
  894. targetNodeId: candidates[0]!.id,
  895. confidence: 0.95,
  896. resolvedBy: 'import',
  897. };
  898. }
  899. export function resolveViaImport(
  900. ref: UnresolvedRef,
  901. context: ResolutionContext
  902. ): ResolvedRef | null {
  903. // C/C++ #include references — resolve directly to the included file
  904. // (file→file edge), bypassing symbol lookup. The extractor emits these
  905. // with `referenceKind: 'imports'` and `referenceName: <include path>`
  906. // (e.g. "uint256.h" or "common/args.h"). Without this branch the
  907. // include-dir scan path inside resolveImportPath never produces an
  908. // edge — resolveViaImport's symbol lookup below would search the
  909. // resolved file for a symbol named like the file extension and fail.
  910. if ((ref.language === 'c' || ref.language === 'cpp') && ref.referenceKind === 'imports') {
  911. const resolvedPath = resolveImportPath(ref.referenceName, ref.filePath, ref.language, context);
  912. if (!resolvedPath) return null;
  913. const basename = resolvedPath.split('/').pop()!;
  914. const fileNodes = context.getNodesByName(basename).filter((n) => n.kind === 'file');
  915. const fileNode = fileNodes.find((n) => n.filePath === resolvedPath);
  916. if (fileNode) {
  917. return {
  918. original: ref,
  919. targetNodeId: fileNode.id,
  920. confidence: 0.9,
  921. resolvedBy: 'import',
  922. };
  923. }
  924. return null;
  925. }
  926. // Use cached import mappings (avoids re-reading and re-parsing per ref)
  927. const imports = context.getImportMappings(ref.filePath, ref.language);
  928. if (imports.length === 0 && !context.readFile(ref.filePath)) {
  929. return null;
  930. }
  931. // Go cross-package calls: `pkga.FuncX(...)` extracts to referenceName
  932. // `pkga.FuncX` and the import `github.com/example/myproject/pkga`
  933. // maps to a *package directory* containing one or more .go files.
  934. // The generic file-based lookup below can't follow that — issue #388.
  935. if (ref.language === 'go') {
  936. const goResult = resolveGoCrossPackageReference(ref, imports, context);
  937. if (goResult) return goResult;
  938. }
  939. // Java / Kotlin: imports are FQNs (`import com.example.Foo;`) — no
  940. // resolvable file path the JS/TS-style chain below could follow. Look
  941. // up the symbol by name and filter to the candidate whose file path
  942. // matches the imported FQN. This is the disambiguation signal that
  943. // breaks the same-name class collision the path-proximity matcher
  944. // can't resolve (issue #314).
  945. if (ref.language === 'java' || ref.language === 'kotlin') {
  946. const javaResult = resolveJavaImportedReference(ref, imports, context);
  947. if (javaResult) return javaResult;
  948. }
  949. // Check if the reference name matches any import
  950. for (const imp of imports) {
  951. if (imp.localName === ref.referenceName || ref.referenceName.startsWith(imp.localName + '.')) {
  952. // Resolve the import path
  953. const resolvedPath = resolveImportPath(
  954. imp.source,
  955. ref.filePath,
  956. ref.language,
  957. context
  958. );
  959. if (resolvedPath) {
  960. const exportedName = imp.isDefault ? 'default' : imp.exportedName;
  961. const memberName = imp.isNamespace
  962. ? ref.referenceName.replace(imp.localName + '.', '')
  963. : null;
  964. const targetNode = findExportedSymbol(
  965. resolvedPath,
  966. { isDefault: imp.isDefault, isNamespace: imp.isNamespace, exportedName, memberName },
  967. ref.language,
  968. context,
  969. new Set()
  970. );
  971. if (targetNode) {
  972. return {
  973. original: ref,
  974. targetNodeId: targetNode.id,
  975. confidence: 0.9,
  976. resolvedBy: 'import',
  977. };
  978. }
  979. }
  980. }
  981. }
  982. return null;
  983. }
  984. /**
  985. * Resolve a Java/Kotlin reference whose receiver is the simple name of
  986. * an imported FQN: `Foo.bar(...)` where `import com.example.Foo;`. The
  987. * imported FQN converts to a file-path suffix (`com/example/Foo.java`
  988. * or `.kt`) which uniquely identifies the right symbol when multiple
  989. * classes share the same simple name.
  990. *
  991. * Also handles bare references to the imported class itself
  992. * (`new Foo()` extraction emits `Foo` as a `references`/`instantiates`
  993. * ref) and `import static <Foo>.bar` style imports of a single member.
  994. */
  995. function resolveJavaImportedReference(
  996. ref: UnresolvedRef,
  997. imports: ImportMapping[],
  998. context: ResolutionContext
  999. ): ResolvedRef | null {
  1000. if (imports.length === 0) return null;
  1001. const ext = ref.language === 'kotlin' ? '.kt' : '.java';
  1002. for (const imp of imports) {
  1003. const matchesBare = imp.localName === ref.referenceName;
  1004. const matchesQualified = ref.referenceName.startsWith(imp.localName + '.');
  1005. if (!matchesBare && !matchesQualified) continue;
  1006. // Convert FQN to a file-path suffix. `com.example.Foo` ->
  1007. // `com/example/Foo.java` (or `.kt`). The actual file may live
  1008. // under any source root (`src/main/java/`, `src/`, etc.), so match
  1009. // by suffix rather than exact path.
  1010. const fqnPath = imp.source.replace(/\./g, '/') + ext;
  1011. // Which symbol name to look up: the class itself, or a member.
  1012. const memberName = matchesBare
  1013. ? imp.localName
  1014. : ref.referenceName.substring(imp.localName.length + 1);
  1015. const candidates = context.getNodesByName(memberName);
  1016. for (const node of candidates) {
  1017. if (node.language !== ref.language) continue;
  1018. const fp = node.filePath.replace(/\\/g, '/');
  1019. if (fp.endsWith(fqnPath) || fp.endsWith('/' + fqnPath)) {
  1020. return {
  1021. original: ref,
  1022. targetNodeId: node.id,
  1023. confidence: 0.9,
  1024. resolvedBy: 'import',
  1025. };
  1026. }
  1027. }
  1028. // `import static com.example.Foo.bar;` — the FQN's tail is the
  1029. // member name, the part before is the owner class. Look up the
  1030. // member named `<imp.localName>` (e.g. `bar`) and prefer the
  1031. // candidate whose file matches the parent FQN's path.
  1032. if (matchesBare) {
  1033. const dot = imp.source.lastIndexOf('.');
  1034. if (dot > 0) {
  1035. const ownerFqn = imp.source.substring(0, dot);
  1036. const ownerPath = ownerFqn.replace(/\./g, '/') + ext;
  1037. for (const node of candidates) {
  1038. if (node.language !== ref.language) continue;
  1039. const fp = node.filePath.replace(/\\/g, '/');
  1040. if (fp.endsWith(ownerPath) || fp.endsWith('/' + ownerPath)) {
  1041. return {
  1042. original: ref,
  1043. targetNodeId: node.id,
  1044. confidence: 0.9,
  1045. resolvedBy: 'import',
  1046. };
  1047. }
  1048. }
  1049. }
  1050. }
  1051. }
  1052. return null;
  1053. }
  1054. /**
  1055. * Resolve a Go cross-package qualified reference (`pkga.FuncX`) by matching
  1056. * the package alias against an in-module import, stripping the module prefix
  1057. * to a project-relative directory, and locating the exported symbol in any
  1058. * `.go` file under that directory. Returns `null` for stdlib / third-party
  1059. * imports (no `go.mod`-relative match) so the rest of `resolveViaImport`
  1060. * can still try the file-based path.
  1061. */
  1062. function resolveGoCrossPackageReference(
  1063. ref: UnresolvedRef,
  1064. imports: ImportMapping[],
  1065. context: ResolutionContext
  1066. ): ResolvedRef | null {
  1067. const mod = context.getGoModule?.();
  1068. if (!mod) return null;
  1069. // Qualified call: receiver before `.`, member after. A bare reference
  1070. // (no dot) is a same-file/in-package call — handled elsewhere.
  1071. const dotIdx = ref.referenceName.indexOf('.');
  1072. if (dotIdx <= 0) return null;
  1073. const receiver = ref.referenceName.substring(0, dotIdx);
  1074. const memberName = ref.referenceName.substring(dotIdx + 1);
  1075. if (!memberName) return null;
  1076. for (const imp of imports) {
  1077. if (imp.localName !== receiver) continue;
  1078. // Only in-module imports map to a known directory.
  1079. if (imp.source !== mod.modulePath && !imp.source.startsWith(mod.modulePath + '/')) {
  1080. continue;
  1081. }
  1082. const pkgDir = imp.source === mod.modulePath
  1083. ? ''
  1084. : imp.source.substring(mod.modulePath.length + 1);
  1085. // Look up the member by name and pick the candidate whose file lives
  1086. // directly in the package directory. Match the immediate parent dir
  1087. // exactly so a call to `pkga.FuncX` doesn't accidentally land on a
  1088. // `FuncX` declared in `pkga/subpkg/`.
  1089. const candidates = context.getNodesByName(memberName);
  1090. for (const node of candidates) {
  1091. if (node.language !== 'go') continue;
  1092. if (!node.isExported) continue;
  1093. const fp = node.filePath.replace(/\\/g, '/');
  1094. const lastSlash = fp.lastIndexOf('/');
  1095. const fileDir = lastSlash >= 0 ? fp.substring(0, lastSlash) : '';
  1096. if (fileDir === pkgDir) {
  1097. return {
  1098. original: ref,
  1099. targetNodeId: node.id,
  1100. confidence: 0.9,
  1101. resolvedBy: 'import',
  1102. };
  1103. }
  1104. }
  1105. }
  1106. return null;
  1107. }
  1108. /** Recursive depth cap for re-export chain following. Real codebases
  1109. * rarely chain barrels more than 2–3 deep; 8 is a generous safety
  1110. * net that still bounds worst-case work. */
  1111. const REEXPORT_MAX_DEPTH = 8;
  1112. /**
  1113. * Find an exported symbol in `filePath`, following `export { x } from
  1114. * './other'` and `export * from './other'` chains until the original
  1115. * declaration is reached. Cycle-safe via the `visited` set.
  1116. *
  1117. * Without this, every barrel-style import (`import { Foo } from
  1118. * './index'` where `index.ts` only re-exports) used to resolve to
  1119. * nothing — the existing code only looked for declarations IN the
  1120. * resolved file, not declarations the file forwarded.
  1121. */
  1122. function findExportedSymbol(
  1123. filePath: string,
  1124. want: {
  1125. isDefault: boolean;
  1126. isNamespace: boolean;
  1127. exportedName: string;
  1128. memberName: string | null;
  1129. },
  1130. language: Language,
  1131. context: ResolutionContext,
  1132. visited: Set<string>,
  1133. depth = 0
  1134. ): Node | undefined {
  1135. if (depth > REEXPORT_MAX_DEPTH) return undefined;
  1136. if (visited.has(filePath)) return undefined;
  1137. visited.add(filePath);
  1138. const nodesInFile = context.getNodesInFile(filePath);
  1139. // 1. Direct hit: the symbol is declared in this file.
  1140. if (want.isDefault) {
  1141. const direct = nodesInFile.find(
  1142. (n) => n.isExported && (n.kind === 'function' || n.kind === 'class')
  1143. );
  1144. if (direct) return direct;
  1145. } else if (want.isNamespace && want.memberName) {
  1146. const direct = nodesInFile.find(
  1147. (n) => n.name === want.memberName && n.isExported
  1148. );
  1149. if (direct) return direct;
  1150. } else {
  1151. const direct = nodesInFile.find(
  1152. (n) => n.name === want.exportedName && n.isExported
  1153. );
  1154. if (direct) return direct;
  1155. }
  1156. // 2. Re-export hit: the file forwards the symbol to another module.
  1157. const reExports = context.getReExports?.(filePath, language) ?? [];
  1158. if (reExports.length === 0) return undefined;
  1159. // Look for explicit `export { want } from './other'` (with optional rename).
  1160. const targetName = want.isDefault ? 'default' : want.exportedName;
  1161. for (const rex of reExports) {
  1162. if (rex.kind === 'named' && rex.exportedName === targetName) {
  1163. const next = resolveImportPath(rex.source, filePath, language, context);
  1164. if (!next) continue;
  1165. // After rename: `export { foo as bar } from './x'` — to chase
  1166. // `bar`, we look for `foo` in `./x`.
  1167. const chained = findExportedSymbol(
  1168. next,
  1169. {
  1170. isDefault: rex.originalName === 'default',
  1171. isNamespace: false,
  1172. exportedName: rex.originalName,
  1173. memberName: null,
  1174. },
  1175. language,
  1176. context,
  1177. visited,
  1178. depth + 1
  1179. );
  1180. if (chained) return chained;
  1181. }
  1182. }
  1183. // 3. Wildcard re-export: `export * from './other'` — try every
  1184. // forwarding source. This is the barrel-of-barrels case.
  1185. for (const rex of reExports) {
  1186. if (rex.kind === 'wildcard') {
  1187. const next = resolveImportPath(rex.source, filePath, language, context);
  1188. if (!next) continue;
  1189. const chained = findExportedSymbol(next, want, language, context, visited, depth + 1);
  1190. if (chained) return chained;
  1191. }
  1192. }
  1193. return undefined;
  1194. }