resolution.test.ts 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269
  1. /**
  2. * Resolution Module Tests
  3. *
  4. * Tests for Phase 3: Reference Resolution
  5. */
  6. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  7. import * as fs from 'fs';
  8. import * as path from 'path';
  9. import * as os from 'os';
  10. import { CodeGraph } from '../src';
  11. import { Node, UnresolvedReference } from '../src/types';
  12. import { ReferenceResolver, createResolver, ResolutionContext } from '../src/resolution';
  13. import { matchReference } from '../src/resolution/name-matcher';
  14. import { resolveImportPath, extractImportMappings, resolveJvmImport, loadCppIncludeDirs, clearCppIncludeDirCache, isPhpIncludePathRef } from '../src/resolution/import-resolver';
  15. import type { UnresolvedRef } from '../src/resolution/types';
  16. import { detectFrameworks, getAllFrameworkResolvers } from '../src/resolution/frameworks';
  17. import { QueryBuilder } from '../src/db/queries';
  18. import { DatabaseConnection } from '../src/db';
  19. describe('Resolution Module', () => {
  20. let tempDir: string;
  21. let cg: CodeGraph;
  22. beforeEach(() => {
  23. // Create temp directory
  24. tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-resolution-test-'));
  25. });
  26. afterEach(() => {
  27. // Clean up
  28. if (cg) {
  29. cg.destroy();
  30. } else if (fs.existsSync(tempDir)) {
  31. fs.rmSync(tempDir, { recursive: true });
  32. }
  33. });
  34. describe('Name Matcher', () => {
  35. it('should match exact name references', () => {
  36. // Create a mock context
  37. const mockNodes: Node[] = [
  38. {
  39. id: 'func:test.ts:myFunction:10',
  40. kind: 'function',
  41. name: 'myFunction',
  42. qualifiedName: 'test.ts::myFunction',
  43. filePath: 'test.ts',
  44. language: 'typescript',
  45. startLine: 10,
  46. endLine: 20,
  47. startColumn: 0,
  48. endColumn: 0,
  49. updatedAt: Date.now(),
  50. },
  51. ];
  52. const context: ResolutionContext = {
  53. getNodesInFile: () => mockNodes,
  54. getNodesByName: (name) => mockNodes.filter((n) => n.name === name),
  55. getNodesByQualifiedName: () => [],
  56. getNodesByKind: () => [],
  57. fileExists: () => true,
  58. readFile: () => null,
  59. getProjectRoot: () => '/test',
  60. getAllFiles: () => ['test.ts'],
  61. };
  62. const ref = {
  63. fromNodeId: 'caller:main.ts:caller:5',
  64. referenceName: 'myFunction',
  65. referenceKind: 'calls' as const,
  66. line: 5,
  67. column: 10,
  68. filePath: 'main.ts',
  69. language: 'typescript' as const,
  70. };
  71. const result = matchReference(ref, context);
  72. expect(result).not.toBeNull();
  73. expect(result?.targetNodeId).toBe('func:test.ts:myFunction:10');
  74. expect(result?.resolvedBy).toBe('exact-match');
  75. });
  76. it('should prefer same-module candidates over cross-module matches', () => {
  77. // Simulates a Python monorepo where multiple apps define navigate()
  78. const candidateA: Node = {
  79. id: 'func:apps/app_a/src/server.py:navigate:10',
  80. kind: 'function',
  81. name: 'navigate',
  82. qualifiedName: 'apps/app_a/src/server.py::navigate',
  83. filePath: 'apps/app_a/src/server.py',
  84. language: 'python',
  85. startLine: 10,
  86. endLine: 20,
  87. startColumn: 0,
  88. endColumn: 0,
  89. updatedAt: Date.now(),
  90. };
  91. const candidateB: Node = {
  92. id: 'func:apps/app_b/src/server.py:navigate:15',
  93. kind: 'function',
  94. name: 'navigate',
  95. qualifiedName: 'apps/app_b/src/server.py::navigate',
  96. filePath: 'apps/app_b/src/server.py',
  97. language: 'python',
  98. startLine: 15,
  99. endLine: 25,
  100. startColumn: 0,
  101. endColumn: 0,
  102. updatedAt: Date.now(),
  103. };
  104. const context: ResolutionContext = {
  105. getNodesInFile: () => [],
  106. getNodesByName: (name) => name === 'navigate' ? [candidateA, candidateB] : [],
  107. getNodesByQualifiedName: () => [],
  108. getNodesByKind: () => [],
  109. fileExists: () => true,
  110. readFile: () => null,
  111. getProjectRoot: () => '/test',
  112. getAllFiles: () => [],
  113. getNodesByLowerName: () => [],
  114. getImportMappings: () => [],
  115. };
  116. // Reference from app_a should resolve to app_a's navigate, not app_b's
  117. const ref = {
  118. fromNodeId: 'func:apps/app_a/src/handler.py:handler:5',
  119. referenceName: 'navigate',
  120. referenceKind: 'calls' as const,
  121. line: 5,
  122. column: 10,
  123. filePath: 'apps/app_a/src/handler.py',
  124. language: 'python' as const,
  125. };
  126. const result = matchReference(ref, context);
  127. expect(result).not.toBeNull();
  128. expect(result?.targetNodeId).toBe('func:apps/app_a/src/server.py:navigate:10');
  129. expect(result?.resolvedBy).toBe('exact-match');
  130. });
  131. it('should lower confidence for cross-module exact matches', () => {
  132. // Only one candidate but in a completely different module
  133. const candidates: Node[] = [
  134. {
  135. id: 'func:apps/app_b/src/server.py:navigate:10',
  136. kind: 'function',
  137. name: 'navigate',
  138. qualifiedName: 'apps/app_b/src/server.py::navigate',
  139. filePath: 'apps/app_b/src/server.py',
  140. language: 'python',
  141. startLine: 10,
  142. endLine: 20,
  143. startColumn: 0,
  144. endColumn: 0,
  145. updatedAt: Date.now(),
  146. },
  147. {
  148. id: 'func:apps/app_c/src/server.py:navigate:10',
  149. kind: 'function',
  150. name: 'navigate',
  151. qualifiedName: 'apps/app_c/src/server.py::navigate',
  152. filePath: 'apps/app_c/src/server.py',
  153. language: 'python',
  154. startLine: 10,
  155. endLine: 20,
  156. startColumn: 0,
  157. endColumn: 0,
  158. updatedAt: Date.now(),
  159. },
  160. ];
  161. const context: ResolutionContext = {
  162. getNodesInFile: () => [],
  163. getNodesByName: (name) => name === 'navigate' ? candidates : [],
  164. getNodesByQualifiedName: () => [],
  165. getNodesByKind: () => [],
  166. fileExists: () => true,
  167. readFile: () => null,
  168. getProjectRoot: () => '/test',
  169. getAllFiles: () => [],
  170. getNodesByLowerName: () => [],
  171. getImportMappings: () => [],
  172. };
  173. // Reference from app_a — neither candidate is in the same module
  174. const ref = {
  175. fromNodeId: 'func:apps/app_a/src/handler.py:handler:5',
  176. referenceName: 'navigate',
  177. referenceKind: 'calls' as const,
  178. line: 5,
  179. column: 10,
  180. filePath: 'apps/app_a/src/handler.py',
  181. language: 'python' as const,
  182. };
  183. const result = matchReference(ref, context);
  184. // Should still resolve but with low confidence
  185. expect(result).not.toBeNull();
  186. expect(result?.confidence).toBeLessThanOrEqual(0.4);
  187. });
  188. it('should match qualified name references', () => {
  189. const mockClassNode: Node = {
  190. id: 'class:user.ts:User:5',
  191. kind: 'class',
  192. name: 'User',
  193. qualifiedName: 'user.ts::User',
  194. filePath: 'user.ts',
  195. language: 'typescript',
  196. startLine: 5,
  197. endLine: 30,
  198. startColumn: 0,
  199. endColumn: 0,
  200. updatedAt: Date.now(),
  201. };
  202. const mockMethodNode: Node = {
  203. id: 'method:user.ts:User.save:15',
  204. kind: 'method',
  205. name: 'save',
  206. qualifiedName: 'user.ts::User::save',
  207. filePath: 'user.ts',
  208. language: 'typescript',
  209. startLine: 15,
  210. endLine: 25,
  211. startColumn: 0,
  212. endColumn: 0,
  213. updatedAt: Date.now(),
  214. };
  215. const context: ResolutionContext = {
  216. getNodesInFile: (fp) => fp === 'user.ts' ? [mockClassNode, mockMethodNode] : [],
  217. getNodesByName: (name) => {
  218. if (name === 'User') return [mockClassNode];
  219. if (name === 'save') return [mockMethodNode];
  220. return [];
  221. },
  222. getNodesByQualifiedName: (qn) => {
  223. if (qn === 'user.ts::User::save') return [mockMethodNode];
  224. return [];
  225. },
  226. getNodesByKind: () => [],
  227. fileExists: () => true,
  228. readFile: () => null,
  229. getProjectRoot: () => '/test',
  230. getAllFiles: () => ['user.ts'],
  231. };
  232. const ref = {
  233. fromNodeId: 'caller:main.ts:main:5',
  234. referenceName: 'User.save',
  235. referenceKind: 'calls' as const,
  236. line: 5,
  237. column: 10,
  238. filePath: 'main.ts',
  239. language: 'typescript' as const,
  240. };
  241. const result = matchReference(ref, context);
  242. expect(result).not.toBeNull();
  243. expect(result?.targetNodeId).toBe('method:user.ts:User.save:15');
  244. });
  245. });
  246. describe('Import Resolver', () => {
  247. it('should resolve relative import paths', () => {
  248. const context: ResolutionContext = {
  249. getNodesInFile: () => [],
  250. getNodesByName: () => [],
  251. getNodesByQualifiedName: () => [],
  252. getNodesByKind: () => [],
  253. fileExists: (p) => p === 'src/components/utils.ts' || p === 'src/components/utils/index.ts',
  254. readFile: () => null,
  255. getProjectRoot: () => '',
  256. getAllFiles: () => ['src/components/utils.ts', 'src/components/utils/index.ts'],
  257. };
  258. const result = resolveImportPath(
  259. './utils',
  260. 'src/components/Button.ts',
  261. 'typescript',
  262. context
  263. );
  264. expect(result).toBe('src/components/utils.ts');
  265. });
  266. it('should resolve parent directory imports', () => {
  267. const context: ResolutionContext = {
  268. getNodesInFile: () => [],
  269. getNodesByName: () => [],
  270. getNodesByQualifiedName: () => [],
  271. getNodesByKind: () => [],
  272. fileExists: (p) => p === 'src/helpers.ts' || p === 'src/helpers/index.ts',
  273. readFile: () => null,
  274. getProjectRoot: () => '',
  275. getAllFiles: () => ['src/helpers.ts', 'src/helpers/index.ts'],
  276. };
  277. const result = resolveImportPath(
  278. '../helpers',
  279. 'src/components/Button.ts',
  280. 'typescript',
  281. context
  282. );
  283. expect(result).toBe('src/helpers.ts');
  284. });
  285. it('should extract JS/TS import mappings', () => {
  286. const content = `
  287. import { foo } from './foo';
  288. import bar from '../bar';
  289. import * as utils from './utils';
  290. import { baz, qux } from './baz';
  291. `;
  292. const mappings = extractImportMappings(
  293. 'src/index.ts',
  294. content,
  295. 'typescript'
  296. );
  297. expect(mappings.length).toBeGreaterThan(0);
  298. expect(mappings.some((m) => m.localName === 'foo')).toBe(true);
  299. expect(mappings.some((m) => m.localName === 'bar')).toBe(true);
  300. });
  301. it('should extract Python import mappings', () => {
  302. const content = `
  303. from utils import helper
  304. from .models import User
  305. import os
  306. from ..services import auth_service
  307. `;
  308. const mappings = extractImportMappings(
  309. 'src/main.py',
  310. content,
  311. 'python'
  312. );
  313. expect(mappings.length).toBeGreaterThan(0);
  314. expect(mappings.some((m) => m.localName === 'helper')).toBe(true);
  315. expect(mappings.some((m) => m.localName === 'User')).toBe(true);
  316. });
  317. });
  318. describe('JVM FQN Import Resolution', () => {
  319. // Build a ResolutionContext stub whose getNodesByQualifiedName answers
  320. // from a fixed table — the only context method resolveJvmImport touches.
  321. const makeContext = (byQName: Record<string, Node[]>): ResolutionContext => ({
  322. getNodesInFile: () => [],
  323. getNodesByName: () => [],
  324. getNodesByQualifiedName: (q) => byQName[q] ?? [],
  325. getNodesByKind: () => [],
  326. fileExists: () => false,
  327. readFile: () => null,
  328. getProjectRoot: () => '',
  329. getAllFiles: () => [],
  330. });
  331. const node = (id: string, name: string, qualifiedName: string, kind: Node['kind'] = 'class', language: Node['language'] = 'kotlin'): Node => ({
  332. id, kind, name, qualifiedName,
  333. filePath: 'Models.kt', language,
  334. startLine: 1, endLine: 1, startColumn: 0, endColumn: 0,
  335. updatedAt: 0,
  336. });
  337. const importRef = (referenceName: string, language: Node['language'] = 'kotlin'): UnresolvedRef => ({
  338. fromNodeId: 'caller',
  339. referenceName,
  340. referenceKind: 'imports',
  341. line: 1, column: 0,
  342. filePath: 'Caller.kt',
  343. language,
  344. });
  345. it('resolves a Kotlin class import by FQN regardless of filename', () => {
  346. const target = node('n1', 'Bar', 'com.example.foo::Bar');
  347. const ctx = makeContext({ 'com.example.foo::Bar': [target] });
  348. const result = resolveJvmImport(importRef('com.example.foo.Bar'), ctx);
  349. expect(result?.targetNodeId).toBe('n1');
  350. expect(result?.resolvedBy).toBe('import');
  351. });
  352. it('resolves a Kotlin top-level function import by FQN', () => {
  353. const util = node('n2', 'util', 'com.example.foo::util', 'function');
  354. const ctx = makeContext({ 'com.example.foo::util': [util] });
  355. const result = resolveJvmImport(importRef('com.example.foo.util'), ctx);
  356. expect(result?.targetNodeId).toBe('n2');
  357. });
  358. it('resolves a Java import by FQN', () => {
  359. const target = node('n3', 'Bar', 'com.example.foo::Bar', 'class', 'java');
  360. const ctx = makeContext({ 'com.example.foo::Bar': [target] });
  361. const result = resolveJvmImport(importRef('com.example.foo.Bar', 'java'), ctx);
  362. expect(result?.targetNodeId).toBe('n3');
  363. });
  364. it('resolves cross-language: Kotlin importing a Java class', () => {
  365. // The Kotlin file declares `import com.example.JavaBar` — the target is
  366. // a Java class node. JVM interop means the resolver doesn't care about
  367. // the source language of the target, only that the FQN matches.
  368. const target = node('n4', 'JavaBar', 'com.example::JavaBar', 'class', 'java');
  369. const ctx = makeContext({ 'com.example::JavaBar': [target] });
  370. const result = resolveJvmImport(importRef('com.example.JavaBar'), ctx);
  371. expect(result?.targetNodeId).toBe('n4');
  372. });
  373. it('disambiguates a name collision across packages', () => {
  374. // Two classes named `Bar` in different packages. Each import resolves
  375. // to the one whose FQN matches — not to "whichever was found first".
  376. const barA = node('n5a', 'Bar', 'com.example.alpha::Bar');
  377. const barB = node('n5b', 'Bar', 'com.example.beta::Bar');
  378. const ctx = makeContext({
  379. 'com.example.alpha::Bar': [barA],
  380. 'com.example.beta::Bar': [barB],
  381. });
  382. expect(resolveJvmImport(importRef('com.example.alpha.Bar'), ctx)?.targetNodeId).toBe('n5a');
  383. expect(resolveJvmImport(importRef('com.example.beta.Bar'), ctx)?.targetNodeId).toBe('n5b');
  384. });
  385. it('returns null for wildcard imports', () => {
  386. const ctx = makeContext({});
  387. expect(resolveJvmImport(importRef('com.example.foo.*'), ctx)).toBeNull();
  388. });
  389. it('returns null for unqualified names', () => {
  390. // A single-segment name has no package; nothing to look up by FQN.
  391. const ctx = makeContext({ 'Bar': [node('n6', 'Bar', 'Bar')] });
  392. expect(resolveJvmImport(importRef('Bar'), ctx)).toBeNull();
  393. });
  394. it('returns null for non-JVM languages', () => {
  395. const target = node('n7', 'Bar', 'com.example::Bar');
  396. const ctx = makeContext({ 'com.example::Bar': [target] });
  397. expect(resolveJvmImport(importRef('com.example.Bar', 'typescript'), ctx)).toBeNull();
  398. });
  399. it('returns null for non-imports reference kinds', () => {
  400. // The resolver intentionally only acts on `imports` refs; ordinary
  401. // `calls`/`extends` refs fall through to the framework + name-matcher
  402. // strategies.
  403. const target = node('n8', 'Bar', 'com.example::Bar');
  404. const ctx = makeContext({ 'com.example::Bar': [target] });
  405. const ref: UnresolvedRef = {
  406. fromNodeId: 'caller', referenceName: 'com.example.Bar',
  407. referenceKind: 'calls', line: 1, column: 0,
  408. filePath: 'Caller.kt', language: 'kotlin',
  409. };
  410. expect(resolveJvmImport(ref, ctx)).toBeNull();
  411. });
  412. it('returns null when the FQN is not in the index', () => {
  413. const ctx = makeContext({});
  414. expect(resolveJvmImport(importRef('com.example.Unknown'), ctx)).toBeNull();
  415. });
  416. });
  417. describe('Framework Detection', () => {
  418. it('should detect React framework', () => {
  419. const context: ResolutionContext = {
  420. getNodesInFile: () => [],
  421. getNodesByName: () => [],
  422. getNodesByQualifiedName: () => [],
  423. getNodesByKind: () => [],
  424. fileExists: () => false,
  425. readFile: (p) => {
  426. if (p === 'package.json') {
  427. return JSON.stringify({
  428. dependencies: { react: '^18.0.0' },
  429. });
  430. }
  431. return null;
  432. },
  433. getProjectRoot: () => '/test',
  434. getAllFiles: () => ['package.json', 'src/App.tsx'],
  435. };
  436. const frameworks = detectFrameworks(context);
  437. expect(frameworks.some((f) => f.name === 'react')).toBe(true);
  438. });
  439. it('should detect Express framework', () => {
  440. const context: ResolutionContext = {
  441. getNodesInFile: () => [],
  442. getNodesByName: () => [],
  443. getNodesByQualifiedName: () => [],
  444. getNodesByKind: () => [],
  445. fileExists: () => false,
  446. readFile: (p) => {
  447. if (p === 'package.json') {
  448. return JSON.stringify({
  449. dependencies: { express: '^4.18.0' },
  450. });
  451. }
  452. return null;
  453. },
  454. getProjectRoot: () => '/test',
  455. getAllFiles: () => ['package.json', 'src/app.js'],
  456. };
  457. const frameworks = detectFrameworks(context);
  458. expect(frameworks.some((f) => f.name === 'express')).toBe(true);
  459. });
  460. it('should detect Laravel framework', () => {
  461. const context: ResolutionContext = {
  462. getNodesInFile: () => [],
  463. getNodesByName: () => [],
  464. getNodesByQualifiedName: () => [],
  465. getNodesByKind: () => [],
  466. fileExists: (p) => p === 'artisan',
  467. readFile: () => null,
  468. getProjectRoot: () => '/test',
  469. getAllFiles: () => ['artisan', 'app/Http/Kernel.php'],
  470. };
  471. const frameworks = detectFrameworks(context);
  472. expect(frameworks.some((f) => f.name === 'laravel')).toBe(true);
  473. });
  474. it('should return all framework resolvers', () => {
  475. const resolvers = getAllFrameworkResolvers();
  476. expect(resolvers.length).toBeGreaterThan(0);
  477. expect(resolvers.some((r) => r.name === 'react')).toBe(true);
  478. expect(resolvers.some((r) => r.name === 'express')).toBe(true);
  479. expect(resolvers.some((r) => r.name === 'laravel')).toBe(true);
  480. });
  481. });
  482. describe('React Framework Resolver', () => {
  483. it('should resolve React component references', () => {
  484. const mockNodes: Node[] = [
  485. {
  486. id: 'component:src/Button.tsx:Button:5',
  487. kind: 'component',
  488. name: 'Button',
  489. qualifiedName: 'src/Button.tsx::Button',
  490. filePath: 'src/Button.tsx',
  491. language: 'tsx',
  492. startLine: 5,
  493. endLine: 20,
  494. startColumn: 0,
  495. endColumn: 0,
  496. updatedAt: Date.now(),
  497. },
  498. ];
  499. const context: ResolutionContext = {
  500. getNodesInFile: (fp) => (fp === 'src/Button.tsx' ? mockNodes : []),
  501. getNodesByName: () => mockNodes,
  502. getNodesByQualifiedName: () => [],
  503. getNodesByKind: () => [],
  504. fileExists: () => false,
  505. readFile: (p) => {
  506. if (p === 'package.json') {
  507. return JSON.stringify({ dependencies: { react: '^18.0.0' } });
  508. }
  509. return null;
  510. },
  511. getProjectRoot: () => '/test',
  512. getAllFiles: () => ['package.json', 'src/Button.tsx', 'src/App.tsx'],
  513. };
  514. const frameworks = detectFrameworks(context);
  515. const reactResolver = frameworks.find((f) => f.name === 'react');
  516. expect(reactResolver).toBeDefined();
  517. const ref = {
  518. fromNodeId: 'component:src/App.tsx:App:1',
  519. referenceName: 'Button',
  520. referenceKind: 'renders' as const,
  521. line: 10,
  522. column: 5,
  523. filePath: 'src/App.tsx',
  524. language: 'typescript' as const,
  525. };
  526. const result = reactResolver!.resolve(ref, context);
  527. expect(result).not.toBeNull();
  528. expect(result?.targetNodeId).toBe('component:src/Button.tsx:Button:5');
  529. });
  530. it('should resolve custom hook references', () => {
  531. const mockNodes: Node[] = [
  532. {
  533. id: 'hook:src/hooks/useAuth.ts:useAuth:1',
  534. kind: 'function',
  535. name: 'useAuth',
  536. qualifiedName: 'src/hooks/useAuth.ts::useAuth',
  537. filePath: 'src/hooks/useAuth.ts',
  538. language: 'typescript',
  539. startLine: 1,
  540. endLine: 20,
  541. startColumn: 0,
  542. endColumn: 0,
  543. updatedAt: Date.now(),
  544. },
  545. ];
  546. const context: ResolutionContext = {
  547. getNodesInFile: (fp) => (fp.includes('useAuth') ? mockNodes : []),
  548. getNodesByName: () => mockNodes,
  549. getNodesByQualifiedName: () => [],
  550. getNodesByKind: () => [],
  551. fileExists: () => false,
  552. readFile: (p) => {
  553. if (p === 'package.json') {
  554. return JSON.stringify({ dependencies: { react: '^18.0.0' } });
  555. }
  556. return null;
  557. },
  558. getProjectRoot: () => '/test',
  559. getAllFiles: () => ['package.json', 'src/hooks/useAuth.ts'],
  560. };
  561. const frameworks = detectFrameworks(context);
  562. const reactResolver = frameworks.find((f) => f.name === 'react');
  563. const ref = {
  564. fromNodeId: 'component:src/App.tsx:App:1',
  565. referenceName: 'useAuth',
  566. referenceKind: 'calls' as const,
  567. line: 5,
  568. column: 10,
  569. filePath: 'src/App.tsx',
  570. language: 'typescript' as const,
  571. };
  572. const result = reactResolver!.resolve(ref, context);
  573. expect(result).not.toBeNull();
  574. expect(result?.targetNodeId).toBe('hook:src/hooks/useAuth.ts:useAuth:1');
  575. });
  576. });
  577. describe('Integration Tests', () => {
  578. it('should create resolver from CodeGraph instance', async () => {
  579. // Create a simple TypeScript project
  580. fs.writeFileSync(
  581. path.join(tempDir, 'package.json'),
  582. JSON.stringify({ name: 'test', dependencies: { react: '^18.0.0' } })
  583. );
  584. const srcDir = path.join(tempDir, 'src');
  585. fs.mkdirSync(srcDir);
  586. // Create utility file
  587. fs.writeFileSync(
  588. path.join(srcDir, 'utils.ts'),
  589. `export function formatDate(date: Date): string {
  590. return date.toISOString();
  591. }
  592. export function parseDate(str: string): Date {
  593. return new Date(str);
  594. }`
  595. );
  596. // Create main file that uses utils
  597. fs.writeFileSync(
  598. path.join(srcDir, 'main.ts'),
  599. `import { formatDate, parseDate } from './utils';
  600. function processDate(input: string): string {
  601. const date = parseDate(input);
  602. return formatDate(date);
  603. }`
  604. );
  605. // Initialize and index
  606. cg = await CodeGraph.init(tempDir, { index: true });
  607. // Check that resolver detected React framework
  608. const frameworks = cg.getDetectedFrameworks();
  609. expect(frameworks).toContain('react');
  610. // Get stats to verify indexing worked
  611. const stats = cg.getStats();
  612. expect(stats.fileCount).toBe(2);
  613. expect(stats.nodeCount).toBeGreaterThan(0);
  614. });
  615. it('should resolve references after indexing', async () => {
  616. // Create a project with references
  617. const srcDir = path.join(tempDir, 'src');
  618. fs.mkdirSync(srcDir, { recursive: true });
  619. fs.writeFileSync(
  620. path.join(srcDir, 'helper.ts'),
  621. `export function helperFunction(): void {
  622. console.log('helper');
  623. }`
  624. );
  625. fs.writeFileSync(
  626. path.join(srcDir, 'main.ts'),
  627. `import { helperFunction } from './helper';
  628. function main(): void {
  629. helperFunction();
  630. }`
  631. );
  632. cg = await CodeGraph.init(tempDir, { index: true });
  633. // Run reference resolution
  634. const result = cg.resolveReferences();
  635. // Should have attempted resolution
  636. expect(result.stats.total).toBeGreaterThanOrEqual(0);
  637. });
  638. it('promotes calls→instantiates when target resolves to a class (Python)', async () => {
  639. // Python has no `new` keyword — `Foo()` is the standard
  640. // instantiation syntax. Extraction can't tell that apart from
  641. // a function call without symbol info, so it emits a `calls`
  642. // ref. Resolution promotes it to `instantiates` once the
  643. // target is known to be a class.
  644. const srcDir = path.join(tempDir, 'src');
  645. fs.mkdirSync(srcDir, { recursive: true });
  646. fs.writeFileSync(
  647. path.join(srcDir, 'app.py'),
  648. `class UserService:
  649. def __init__(self):
  650. self.db = None
  651. def bootstrap():
  652. return UserService()
  653. `
  654. );
  655. cg = await CodeGraph.init(tempDir, { index: true });
  656. cg.resolveReferences();
  657. const bootstrap = cg
  658. .getNodesByKind('function')
  659. .find((n) => n.name === 'bootstrap');
  660. expect(bootstrap).toBeDefined();
  661. const outgoing = cg.getOutgoingEdges(bootstrap!.id);
  662. const instantiates = outgoing.find((e) => e.kind === 'instantiates');
  663. expect(instantiates).toBeDefined();
  664. // Same edge must NOT also appear as a `calls` edge — promotion
  665. // replaces the kind, doesn't duplicate.
  666. const callsToUserService = outgoing.filter(
  667. (e) => e.kind === 'calls' && e.target === instantiates!.target
  668. );
  669. expect(callsToUserService).toHaveLength(0);
  670. });
  671. it('resolves Go cross-package qualified calls via go.mod module path (#388)', async () => {
  672. // Pre-#388, every `pkga.FuncX(...)` call in a Go monorepo was flagged
  673. // external (isExternalImport returned true for any non-`/internal/`
  674. // import without `.`-prefix) and resolution fell through to name-match
  675. // with path proximity — recall on cross-package callers was ~<1%.
  676. fs.writeFileSync(
  677. path.join(tempDir, 'go.mod'),
  678. 'module github.com/example/myproject\n\ngo 1.21\n'
  679. );
  680. const pkgaDir = path.join(tempDir, 'pkga');
  681. const pkgbDir = path.join(tempDir, 'pkgb');
  682. const pkgcDir = path.join(tempDir, 'pkgc');
  683. fs.mkdirSync(pkgaDir);
  684. fs.mkdirSync(pkgbDir);
  685. fs.mkdirSync(pkgcDir);
  686. // Same-name exported function in two packages — only the imported one
  687. // should resolve. Exercises disambiguation, not just connectivity.
  688. fs.writeFileSync(
  689. path.join(pkgaDir, 'conv.go'),
  690. 'package pkga\nfunc Convert(x int) int { return x * 2 }\n'
  691. );
  692. fs.writeFileSync(
  693. path.join(pkgbDir, 'conv.go'),
  694. 'package pkgb\nfunc Convert(x int) int { return x + 1 }\n'
  695. );
  696. fs.writeFileSync(
  697. path.join(pkgcDir, 'use.go'),
  698. `package pkgc
  699. import "github.com/example/myproject/pkga"
  700. func UsePkga() {
  701. pkga.Convert(5)
  702. }
  703. `
  704. );
  705. cg = await CodeGraph.init(tempDir, { index: true });
  706. const usePkga = cg.getNodesByKind('function').filter((n) => n.name ==='UsePkga')[0];
  707. expect(usePkga).toBeDefined();
  708. const outgoing = cg.getOutgoingEdges(usePkga!.id);
  709. const callEdges = outgoing.filter((e) => e.kind === 'calls');
  710. expect(callEdges).toHaveLength(1);
  711. const target = cg.getNode(callEdges[0]!.target);
  712. expect(target?.name).toBe('Convert');
  713. // Critical: the resolver must pick the imported pkga's Convert,
  714. // not pkgb's. With the broken (pre-fix) resolver this lands on
  715. // whichever Convert happens to be cheaper under path proximity.
  716. expect(target?.filePath.replace(/\\/g, '/')).toBe('pkga/conv.go');
  717. });
  718. it('resolves Go aliased imports across packages (#388)', async () => {
  719. fs.writeFileSync(
  720. path.join(tempDir, 'go.mod'),
  721. 'module github.com/example/myproject\n\ngo 1.21\n'
  722. );
  723. fs.mkdirSync(path.join(tempDir, 'pkgb'));
  724. fs.mkdirSync(path.join(tempDir, 'pkgd'));
  725. fs.writeFileSync(
  726. path.join(tempDir, 'pkgb', 'lib.go'),
  727. 'package pkgb\nfunc Compute(x int) int { return x }\n'
  728. );
  729. fs.writeFileSync(
  730. path.join(tempDir, 'pkgd', 'use.go'),
  731. `package pkgd
  732. import (
  733. "fmt"
  734. alias "github.com/example/myproject/pkgb"
  735. )
  736. func UseAliased() {
  737. fmt.Println("hi")
  738. alias.Compute(3)
  739. }
  740. `
  741. );
  742. cg = await CodeGraph.init(tempDir, { index: true });
  743. const useAliased = cg.getNodesByKind('function').filter((n) => n.name ==='UseAliased')[0];
  744. expect(useAliased).toBeDefined();
  745. const calls = cg.getOutgoingEdges(useAliased!.id).filter((e) => e.kind === 'calls');
  746. // fmt.Println is stdlib — must stay external. alias.Compute must resolve.
  747. expect(calls).toHaveLength(1);
  748. const target = cg.getNode(calls[0]!.target);
  749. expect(target?.name).toBe('Compute');
  750. expect(target?.filePath.replace(/\\/g, '/')).toBe('pkgb/lib.go');
  751. });
  752. it('resolves Python module-attribute calls after `from pkg import module` (#578)', async () => {
  753. // Pre-#578, a `module.func()` call where `module` was bound via
  754. // `from pkg import module` dropped its `calls` edge. The file→file import
  755. // edge resolved (resolveModuleImportToFile falls back to a dotted-module
  756. // file lookup for absolute package paths), but resolvePythonModuleMember
  757. // had no such fallback — resolveImportPath returns null for an absolute
  758. // package path like `pkg.module`, so the member never resolved and
  759. // callers/callees/impact on the target came back empty. Same root-cause
  760. // class as the Go cross-package qualified call (#388).
  761. fs.mkdirSync(path.join(tempDir, 'pkg'));
  762. fs.writeFileSync(path.join(tempDir, 'pkg', '__init__.py'), '');
  763. fs.writeFileSync(
  764. path.join(tempDir, 'pkg', 'module.py'),
  765. 'def func():\n return 1\n'
  766. );
  767. fs.writeFileSync(
  768. path.join(tempDir, 'main.py'),
  769. `from pkg import module
  770. import os
  771. def caller():
  772. return module.func()
  773. def external_caller():
  774. return os.getcwd()
  775. `
  776. );
  777. cg = await CodeGraph.init(tempDir, { index: true });
  778. const caller = cg.getNodesByKind('function').filter((n) => n.name === 'caller')[0];
  779. expect(caller).toBeDefined();
  780. const calls = cg.getOutgoingEdges(caller!.id).filter((e) => e.kind === 'calls');
  781. // module.func() must resolve to the real function in the submodule file.
  782. expect(calls).toHaveLength(1);
  783. const target = cg.getNode(calls[0]!.target);
  784. expect(target?.name).toBe('func');
  785. expect(target?.filePath.replace(/\\/g, '/')).toBe('pkg/module.py');
  786. // The flip side of the fix: an attribute call through a *stdlib* module
  787. // (`os.getcwd()`) must still create no edge — the fallback only matches
  788. // real in-repo module files.
  789. const externalCaller = cg.getNodesByKind('function').filter((n) => n.name === 'external_caller')[0];
  790. expect(externalCaller).toBeDefined();
  791. const externalCalls = cg.getOutgoingEdges(externalCaller!.id).filter((e) => e.kind === 'calls');
  792. expect(externalCalls).toHaveLength(0);
  793. });
  794. it('attaches Go methods to their receiver type across files (#583, cross-file half)', async () => {
  795. // In Go a type's methods are commonly declared in a different file from the
  796. // `type` declaration (`type Box` in box.go, `func (b *Box) Get()` in
  797. // box_methods.go). Extraction only attaches the struct→method `contains`
  798. // edge when the type is in the SAME file (the owner lookup is file-scoped),
  799. // so a cross-file method was orphaned from its struct — breaking member
  800. // outlines and any callers/callees/impact traversal through `contains`. A
  801. // resolution-phase pass now links them within the package (= directory).
  802. fs.writeFileSync(
  803. path.join(tempDir, 'box.go'),
  804. 'package main\n\ntype Box struct{ v int }\n'
  805. );
  806. fs.writeFileSync(
  807. path.join(tempDir, 'box_methods.go'),
  808. 'package main\n\nfunc (b *Box) Get() int { return b.v }\nfunc (b *Box) Set(x int) { b.v = x }\n'
  809. );
  810. // Generic receiver declared cross-file too — exercises #583 half A
  811. // (generic `*Stack[T]` receiver parsing) and half B (cross-file) together.
  812. fs.writeFileSync(
  813. path.join(tempDir, 'stack.go'),
  814. 'package main\n\ntype Stack[T any] struct {\n\titems []T\n}\n'
  815. );
  816. fs.writeFileSync(
  817. path.join(tempDir, 'stack_push.go'),
  818. 'package main\n\nfunc (s *Stack[T]) Push(v T) { s.items = append(s.items, v) }\n'
  819. );
  820. // A same-named type in another package must NOT capture this package's
  821. // methods — the link is scoped to the receiver type's own directory.
  822. fs.mkdirSync(path.join(tempDir, 'other'));
  823. fs.writeFileSync(
  824. path.join(tempDir, 'other', 'box.go'),
  825. 'package other\n\ntype Box struct{ w int }\n'
  826. );
  827. cg = await CodeGraph.init(tempDir, { index: true });
  828. const methodsOf = (typeName: string, file: string): string[] => {
  829. const node = cg
  830. .getNodesByKind('struct')
  831. .find((n) => n.name === typeName && n.filePath.replace(/\\/g, '/') === file);
  832. expect(node, `${typeName} @ ${file}`).toBeDefined();
  833. return cg
  834. .getOutgoingEdges(node!.id)
  835. .filter((e) => e.kind === 'contains')
  836. .map((e) => cg.getNode(e.target))
  837. .filter((n) => !!n && n.kind === 'method')
  838. .map((n) => n!.name)
  839. .sort();
  840. };
  841. // Cross-file (non-generic) methods now attach to their struct.
  842. expect(methodsOf('Box', 'box.go')).toEqual(['Get', 'Set']);
  843. // Generic + cross-file.
  844. expect(methodsOf('Stack', 'stack.go')).toEqual(['Push']);
  845. // Cross-package isolation: other/Box defines no methods of its own.
  846. expect(methodsOf('Box', 'other/box.go')).toEqual([]);
  847. });
  848. it('TS type_alias object-shape members resolve method calls (#359)', async () => {
  849. // Pre-#359, `recorder.stop()` (recorder: RecorderHandle) attached
  850. // to `StdioMcpClient.stop` in a sibling directory via path-proximity
  851. // because the type_alias had no `stop` node — only the unrelated
  852. // class did. Now type_alias produces member nodes (property/method),
  853. // so the camelCase receiver↔type word overlap pulls the call to
  854. // `RecorderHandle::stop` instead of the look-alike class.
  855. fs.mkdirSync(path.join(tempDir, 'voice'));
  856. fs.mkdirSync(path.join(tempDir, 'codegraph'));
  857. fs.writeFileSync(
  858. path.join(tempDir, 'voice', 'recorder.ts'),
  859. `export type RecorderHandle = {
  860. wavPath: string;
  861. stop: () => Promise<{ ok: true }>;
  862. };
  863. `
  864. );
  865. fs.writeFileSync(
  866. path.join(tempDir, 'voice', 'controller.ts'),
  867. `import type { RecorderHandle } from "./recorder";
  868. export async function finaliseRecording(recorder: RecorderHandle) {
  869. return await recorder.stop();
  870. }
  871. `
  872. );
  873. fs.writeFileSync(
  874. path.join(tempDir, 'codegraph', 'stdio-client.ts'),
  875. `export class StdioMcpClient {
  876. private stopped = false;
  877. async stop(): Promise<void> { this.stopped = true; }
  878. }
  879. `
  880. );
  881. cg = await CodeGraph.init(tempDir, { index: true });
  882. const handleStop = cg
  883. .getNodesByKind('method')
  884. .find((n) => n.qualifiedName === 'RecorderHandle::stop');
  885. expect(handleStop).toBeDefined();
  886. const clientStop = cg
  887. .getNodesByKind('method')
  888. .find((n) => n.qualifiedName === 'StdioMcpClient::stop');
  889. expect(clientStop).toBeDefined();
  890. const handleCallers = cg.getIncomingEdges(handleStop!.id).filter((e) => e.kind === 'calls');
  891. const clientCallers = cg.getIncomingEdges(clientStop!.id).filter((e) => e.kind === 'calls');
  892. expect(handleCallers.length).toBeGreaterThanOrEqual(1);
  893. // The class method must have NO callers — voice/'s call must NOT
  894. // mis-attribute. A non-empty list would mean the false-positive
  895. // path is still firing.
  896. expect(clientCallers).toHaveLength(0);
  897. // Function-typed property surfaces as a `method` node, not `property`,
  898. // because `stop()` semantics at the call site are method semantics.
  899. expect(handleStop!.kind).toBe('method');
  900. });
  901. it('Java import disambiguates same-name classes across modules (#314)', async () => {
  902. // Pre-#314 the import resolver had no Java branch at all, so a
  903. // multi-module Maven repo where `dao/converter/FooConverter` and
  904. // `service/converter/FooConverter` both export a `convert` method
  905. // resolved by file-path proximity — picking whichever class was
  906. // closer to the caller, which is wrong any time the caller lives
  907. // in an equidistant cross-cutting module.
  908. const daoDir = path.join(tempDir, 'dao/src/main/java/com/example/dao/converter');
  909. const serviceDir = path.join(tempDir, 'service/src/main/java/com/example/service/converter');
  910. const webDir = path.join(tempDir, 'web/src/main/java/com/example/web');
  911. fs.mkdirSync(daoDir, { recursive: true });
  912. fs.mkdirSync(serviceDir, { recursive: true });
  913. fs.mkdirSync(webDir, { recursive: true });
  914. fs.writeFileSync(
  915. path.join(daoDir, 'FooConverter.java'),
  916. `package com.example.dao.converter;
  917. public class FooConverter { public String convert(String x) { return "dao:" + x; } }
  918. `
  919. );
  920. fs.writeFileSync(
  921. path.join(serviceDir, 'FooConverter.java'),
  922. `package com.example.service.converter;
  923. public class FooConverter { public String convert(String x) { return "svc:" + x; } }
  924. `
  925. );
  926. // The caller imports the SERVICE version — even though dao is
  927. // alphabetically/lexically first in the candidate list, the
  928. // import must trump that order.
  929. fs.writeFileSync(
  930. path.join(webDir, 'Handler.java'),
  931. `package com.example.web;
  932. import com.example.service.converter.FooConverter;
  933. public class Handler {
  934. private FooConverter fooConverter;
  935. public String use() { return fooConverter.convert("input"); }
  936. }
  937. `
  938. );
  939. cg = await CodeGraph.init(tempDir, { index: true });
  940. const use = cg
  941. .getNodesByKind('method')
  942. .find((n) => n.qualifiedName === 'com.example.web::Handler::use');
  943. expect(use).toBeDefined();
  944. const calls = cg.getOutgoingEdges(use!.id).filter((e) => e.kind === 'calls');
  945. expect(calls.length).toBeGreaterThanOrEqual(1);
  946. const target = cg.getNode(calls[0]!.target);
  947. expect(target?.name).toBe('convert');
  948. expect(target?.filePath.replace(/\\/g, '/')).toBe(
  949. 'service/src/main/java/com/example/service/converter/FooConverter.java'
  950. );
  951. });
  952. it('C# extracts references from method/property/field types (#381)', async () => {
  953. // Pre-#381, every C# project produced ZERO `references` edges:
  954. // csharp.ts was missing returnField, and the type-leaf walker
  955. // only recognized TS/Java's `type_identifier` nodes — C# uses
  956. // `identifier`/`predefined_type`/`qualified_name`/`generic_name`.
  957. const srcDir = path.join(tempDir, 'src');
  958. fs.mkdirSync(srcDir, { recursive: true });
  959. fs.writeFileSync(
  960. path.join(srcDir, 'Dtos.cs'),
  961. `namespace MyApp;
  962. public class SessionInfoDto { public string Id { get; set; } = ""; }
  963. public class UserDto { public string Name { get; set; } = ""; }
  964. `
  965. );
  966. fs.writeFileSync(
  967. path.join(srcDir, 'Service.cs'),
  968. `using System.Threading.Tasks;
  969. namespace MyApp;
  970. public class DataExporter
  971. {
  972. public SessionInfoDto Build(UserDto user, SessionInfoDto session) { return session; }
  973. public Task<SessionInfoDto> BuildAsync(UserDto user) { return Task.FromResult(new SessionInfoDto()); }
  974. public SessionInfoDto Latest { get; set; } = new();
  975. private UserDto _cached;
  976. }
  977. `
  978. );
  979. cg = await CodeGraph.init(tempDir, { index: true });
  980. const sessionDto = cg
  981. .getNodesByKind('class')
  982. .find((n) => n.name === 'SessionInfoDto');
  983. const userDto = cg
  984. .getNodesByKind('class')
  985. .find((n) => n.name === 'UserDto');
  986. expect(sessionDto).toBeDefined();
  987. expect(userDto).toBeDefined();
  988. const sessionIncoming = cg
  989. .getIncomingEdges(sessionDto!.id)
  990. .filter((e) => e.kind === 'references');
  991. const userIncoming = cg
  992. .getIncomingEdges(userDto!.id)
  993. .filter((e) => e.kind === 'references');
  994. // SessionInfoDto: Build return, Build param, BuildAsync return (inside Task<>), Latest property.
  995. // UserDto: Build param, BuildAsync param, _cached field.
  996. expect(sessionIncoming.length).toBeGreaterThanOrEqual(4);
  997. expect(userIncoming.length).toBeGreaterThanOrEqual(3);
  998. });
  999. it('C# primary-constructor parameters record their type dependencies (#237)', async () => {
  1000. // C# 12 primary constructors declare a type's injected dependencies inline
  1001. // (`class Svc(IRepo repo, [FromKeyedServices("k")] ICache cache)`). Each
  1002. // ctor parameter's type is recorded as a `references` edge from the class,
  1003. // so a DI-registered contract reached only through a primary ctor is no
  1004. // longer reported as having no dependents.
  1005. fs.mkdirSync(path.join(tempDir, 'src'), { recursive: true });
  1006. fs.writeFileSync(
  1007. path.join(tempDir, 'src', 'Contracts.cs'),
  1008. `namespace App;
  1009. public interface IRepo { }
  1010. public class ICache { }
  1011. `
  1012. );
  1013. fs.writeFileSync(
  1014. path.join(tempDir, 'src', 'OrderService.cs'),
  1015. `namespace App;
  1016. public sealed class OrderService(IRepo repo, [FromKeyedServices("primary")] ICache cache)
  1017. {
  1018. public void Run() { }
  1019. }
  1020. `
  1021. );
  1022. cg = await CodeGraph.init(tempDir, { index: true });
  1023. const svc = cg.getNodesByKind('class').find((n) => n.name === 'OrderService');
  1024. expect(svc).toBeDefined();
  1025. // The class itself must index (it used to vanish under the old grammar).
  1026. const out = cg.getOutgoingEdges(svc!.id).filter((e) => e.kind === 'references');
  1027. const depNames = out.map((e) => cg.getNode(e.target)?.name);
  1028. expect(depNames).toContain('IRepo');
  1029. expect(depNames).toContain('ICache'); // the keyed-DI ([FromKeyedServices]) dependency
  1030. });
  1031. it('Go: leaves stdlib calls (fmt.Println, etc.) external', async () => {
  1032. fs.writeFileSync(
  1033. path.join(tempDir, 'go.mod'),
  1034. 'module github.com/example/myproject\n\ngo 1.21\n'
  1035. );
  1036. fs.writeFileSync(
  1037. path.join(tempDir, 'main.go'),
  1038. `package main
  1039. import "fmt"
  1040. func main() {
  1041. fmt.Println("hi")
  1042. }
  1043. `
  1044. );
  1045. cg = await CodeGraph.init(tempDir, { index: true });
  1046. const mainFn = cg.getNodesByKind('function').filter((n) => n.name ==='main')[0];
  1047. const calls = cg.getOutgoingEdges(mainFn!.id).filter((e) => e.kind === 'calls');
  1048. // No spurious in-project edge — fmt.* must stay unresolved/external.
  1049. expect(calls).toHaveLength(0);
  1050. });
  1051. });
  1052. describe('Name Matcher: kind bias for new ref kinds', () => {
  1053. const baseContext = (candidates: Node[]): ResolutionContext => ({
  1054. getNodesInFile: () => [],
  1055. getNodesByName: (name) => candidates.filter((c) => c.name === name),
  1056. getNodesByQualifiedName: () => [],
  1057. getNodesByKind: () => [],
  1058. fileExists: () => true,
  1059. readFile: () => null,
  1060. getProjectRoot: () => '/test',
  1061. getAllFiles: () => [],
  1062. getNodesByLowerName: () => [],
  1063. getImportMappings: () => [],
  1064. });
  1065. it('prefers a class candidate over a function for `instantiates` refs', () => {
  1066. // A class and a function share a name across the codebase.
  1067. // Without the kind bias, the function (which gets the +25 `calls`
  1068. // bonus historically applied to all candidates of that kind) would
  1069. // win. Now the instantiates branch reverses it.
  1070. const fn: Node = {
  1071. id: 'func:utils.ts:Logger:5', kind: 'function', name: 'Logger',
  1072. qualifiedName: 'utils.ts::Logger', filePath: 'utils.ts', language: 'typescript',
  1073. startLine: 5, endLine: 7, startColumn: 0, endColumn: 0, updatedAt: Date.now(),
  1074. };
  1075. const cls: Node = {
  1076. id: 'class:logger.ts:Logger:10', kind: 'class', name: 'Logger',
  1077. qualifiedName: 'logger.ts::Logger', filePath: 'logger.ts', language: 'typescript',
  1078. startLine: 10, endLine: 30, startColumn: 0, endColumn: 0, updatedAt: Date.now(),
  1079. };
  1080. const ref = {
  1081. fromNodeId: 'func:main.ts:bootstrap:1',
  1082. referenceName: 'Logger',
  1083. referenceKind: 'instantiates' as const,
  1084. line: 5, column: 0, filePath: 'main.ts', language: 'typescript' as const,
  1085. };
  1086. const result = matchReference(ref, baseContext([fn, cls]));
  1087. expect(result?.targetNodeId).toBe('class:logger.ts:Logger:10');
  1088. });
  1089. it('prefers a function candidate over a non-function for `decorates` refs', () => {
  1090. const variable: Node = {
  1091. id: 'var:config.ts:Inject:5', kind: 'variable', name: 'Inject',
  1092. qualifiedName: 'config.ts::Inject', filePath: 'config.ts', language: 'typescript',
  1093. startLine: 5, endLine: 5, startColumn: 0, endColumn: 0, updatedAt: Date.now(),
  1094. };
  1095. const decorator: Node = {
  1096. id: 'func:di.ts:Inject:10', kind: 'function', name: 'Inject',
  1097. qualifiedName: 'di.ts::Inject', filePath: 'di.ts', language: 'typescript',
  1098. startLine: 10, endLine: 20, startColumn: 0, endColumn: 0, updatedAt: Date.now(),
  1099. };
  1100. const ref = {
  1101. fromNodeId: 'class:svc.ts:UserService:1',
  1102. referenceName: 'Inject',
  1103. referenceKind: 'decorates' as const,
  1104. line: 5, column: 0, filePath: 'svc.ts', language: 'typescript' as const,
  1105. };
  1106. const result = matchReference(ref, baseContext([variable, decorator]));
  1107. expect(result?.targetNodeId).toBe('func:di.ts:Inject:10');
  1108. });
  1109. });
  1110. describe('tsconfig path aliases', () => {
  1111. it('resolves an aliased import to the alias-mapped file (not a same-named file elsewhere)', async () => {
  1112. // Two same-named exports in different directories. Without alias
  1113. // resolution, name-matcher would pick whichever it finds first;
  1114. // with alias resolution, the import path uniquely picks one.
  1115. fs.mkdirSync(path.join(tempDir, 'src/utils'), { recursive: true });
  1116. fs.mkdirSync(path.join(tempDir, 'src/legacy'), { recursive: true });
  1117. fs.writeFileSync(
  1118. path.join(tempDir, 'src/utils/format.ts'),
  1119. `export function pickMe(): number { return 1; }\n`
  1120. );
  1121. fs.writeFileSync(
  1122. path.join(tempDir, 'src/legacy/format.ts'),
  1123. `export function pickMe(): number { return 99; }\n`
  1124. );
  1125. fs.writeFileSync(
  1126. path.join(tempDir, 'src/main.ts'),
  1127. `import { pickMe } from '@utils/format';\nexport function go(): number { return pickMe(); }\n`
  1128. );
  1129. fs.writeFileSync(
  1130. path.join(tempDir, 'tsconfig.json'),
  1131. JSON.stringify({
  1132. compilerOptions: {
  1133. baseUrl: './src',
  1134. paths: { '@utils/*': ['utils/*'] },
  1135. },
  1136. })
  1137. );
  1138. cg = await CodeGraph.init(tempDir, { index: true });
  1139. cg.resolveReferences();
  1140. // The two pickMe nodes live in different files. The aliased
  1141. // import should attach the call edge to the @utils-mapped one,
  1142. // not the legacy duplicate.
  1143. const all = cg.getNodesByKind('function').filter((n) => n.name === 'pickMe');
  1144. const utilsNode = all.find((n) => n.filePath === 'src/utils/format.ts');
  1145. const legacyNode = all.find((n) => n.filePath === 'src/legacy/format.ts');
  1146. expect(utilsNode).toBeDefined();
  1147. expect(legacyNode).toBeDefined();
  1148. const utilsCallers = cg.getCallers(utilsNode!.id);
  1149. const legacyCallers = cg.getCallers(legacyNode!.id);
  1150. expect(utilsCallers.length).toBeGreaterThan(0);
  1151. expect(utilsCallers.some((c) => c.node.filePath === 'src/main.ts')).toBe(true);
  1152. // The legacy node should NOT have a caller from src/main.ts —
  1153. // the alias correctly picked the utils version.
  1154. expect(legacyCallers.some((c) => c.node.filePath === 'src/main.ts')).toBe(false);
  1155. });
  1156. it('falls back gracefully when tsconfig is absent', async () => {
  1157. fs.mkdirSync(path.join(tempDir, 'src'), { recursive: true });
  1158. fs.writeFileSync(
  1159. path.join(tempDir, 'src/a.ts'),
  1160. `export function aFn(): void {}\n`
  1161. );
  1162. fs.writeFileSync(
  1163. path.join(tempDir, 'src/b.ts'),
  1164. `import { aFn } from './a';\nexport function bFn(): void { aFn(); }\n`
  1165. );
  1166. cg = await CodeGraph.init(tempDir, { index: true });
  1167. // No tsconfig present — index should still complete and the
  1168. // relative-import-based call edge should be created.
  1169. const aFn = cg.getNodesByKind('function').find((n) => n.name === 'aFn');
  1170. expect(aFn).toBeDefined();
  1171. const callers = cg.getCallers(aFn!.id);
  1172. expect(callers.some((c) => c.node.filePath === 'src/b.ts')).toBe(true);
  1173. });
  1174. });
  1175. describe('re-export chain following', () => {
  1176. it('chases a 3-hop barrel chain (wildcard → named → declaration)', async () => {
  1177. // main.ts → all.ts (wildcard) → index.ts (named) → auth.ts (declaration).
  1178. // Without chain following, `signIn` resolves to nothing because
  1179. // none of the barrel files declare it directly.
  1180. fs.mkdirSync(path.join(tempDir, 'src/services'), { recursive: true });
  1181. fs.writeFileSync(
  1182. path.join(tempDir, 'src/services/auth.ts'),
  1183. `export function signIn(): void {}\n`
  1184. );
  1185. fs.writeFileSync(
  1186. path.join(tempDir, 'src/services/index.ts'),
  1187. `export { signIn } from './auth';\n`
  1188. );
  1189. fs.writeFileSync(
  1190. path.join(tempDir, 'src/all.ts'),
  1191. `export * from './services/index';\n`
  1192. );
  1193. fs.writeFileSync(
  1194. path.join(tempDir, 'src/main.ts'),
  1195. `import { signIn } from './all';\nexport function go(): void { signIn(); }\n`
  1196. );
  1197. cg = await CodeGraph.init(tempDir, { index: true });
  1198. cg.resolveReferences();
  1199. const signInNode = cg
  1200. .getNodesByKind('function')
  1201. .find((n) => n.name === 'signIn' && n.filePath === 'src/services/auth.ts');
  1202. expect(signInNode).toBeDefined();
  1203. const callers = cg.getCallers(signInNode!.id);
  1204. expect(callers.some((c) => c.node.filePath === 'src/main.ts')).toBe(true);
  1205. });
  1206. it('follows a renamed named re-export (export { foo as bar } from ...)', async () => {
  1207. // The chase has to look up `foo` in the upstream module even
  1208. // though the importer asked for `bar` — exercises the rename
  1209. // branch of findExportedSymbol.
  1210. fs.mkdirSync(path.join(tempDir, 'src'), { recursive: true });
  1211. fs.writeFileSync(
  1212. path.join(tempDir, 'src/auth.ts'),
  1213. `export function signIn(): void {}\n`
  1214. );
  1215. fs.writeFileSync(
  1216. path.join(tempDir, 'src/index.ts'),
  1217. `export { signIn as login } from './auth';\n`
  1218. );
  1219. fs.writeFileSync(
  1220. path.join(tempDir, 'src/main.ts'),
  1221. `import { login } from './index';\nexport function go(): void { login(); }\n`
  1222. );
  1223. cg = await CodeGraph.init(tempDir, { index: true });
  1224. cg.resolveReferences();
  1225. const signInNode = cg
  1226. .getNodesByKind('function')
  1227. .find((n) => n.name === 'signIn' && n.filePath === 'src/auth.ts');
  1228. expect(signInNode).toBeDefined();
  1229. const callers = cg.getCallers(signInNode!.id);
  1230. expect(callers.some((c) => c.node.filePath === 'src/main.ts')).toBe(true);
  1231. });
  1232. it('follows a default re-export of a .svelte component (export { default as Foo } from ./RealButton.svelte) (#629)', async () => {
  1233. // The ubiquitous Svelte/React component-barrel form. The leaf is a
  1234. // .svelte component (extracted as kind 'component', the default
  1235. // export). The re-export ALIAS (`Foo`) deliberately differs from the
  1236. // component's real name (`RealButton`) so the name-matcher fallback
  1237. // can't coincidentally connect them — the only path to the edge is
  1238. // the import-chase, which must match a `component` (not just
  1239. // function/class) for the default export. Otherwise the
  1240. // consumer↔component edge is never created and `callers` returns a
  1241. // false 0.
  1242. fs.mkdirSync(path.join(tempDir, 'src/lib'), { recursive: true });
  1243. fs.writeFileSync(
  1244. path.join(tempDir, 'src/lib/RealButton.svelte'),
  1245. `<script lang="ts">\n export let label: string = '';\n</script>\n\n<button>{label}</button>\n`
  1246. );
  1247. fs.writeFileSync(
  1248. path.join(tempDir, 'src/lib/index.ts'),
  1249. `export { default as Foo } from './RealButton.svelte';\n`
  1250. );
  1251. fs.writeFileSync(
  1252. path.join(tempDir, 'src/Bar.svelte'),
  1253. `<script lang="ts">\n import { Foo } from './lib';\n</script>\n\n<Foo />\n`
  1254. );
  1255. cg = await CodeGraph.init(tempDir, { index: true });
  1256. cg.resolveReferences();
  1257. const fooNode = cg
  1258. .getNodesByKind('component')
  1259. .find((n) => n.name === 'RealButton' && n.filePath === 'src/lib/RealButton.svelte');
  1260. expect(fooNode).toBeDefined();
  1261. const callers = cg.getCallers(fooNode!.id);
  1262. expect(callers.some((c) => c.node.filePath === 'src/Bar.svelte')).toBe(true);
  1263. });
  1264. it('resolves a bare directory import (import { x } from "." / "./") to index.ts (#629)', async () => {
  1265. // `import { helper } from '.'` (or './') must map to the
  1266. // directory's index.ts before the re-export chase can run. The
  1267. // barrel renames `realHelper` → `helper` so the name-matcher can't
  1268. // mask a path-resolution failure: only the bare-dir resolution +
  1269. // rename chase can connect the edge.
  1270. fs.mkdirSync(path.join(tempDir, 'src'), { recursive: true });
  1271. fs.writeFileSync(
  1272. path.join(tempDir, 'src/util.ts'),
  1273. `export function realHelper(): void {}\n`
  1274. );
  1275. fs.writeFileSync(
  1276. path.join(tempDir, 'src/index.ts'),
  1277. `export { realHelper as helper } from './util';\n`
  1278. );
  1279. fs.writeFileSync(
  1280. path.join(tempDir, 'src/main.ts'),
  1281. `import { helper } from '.';\nexport function go(): void { helper(); }\n`
  1282. );
  1283. fs.writeFileSync(
  1284. path.join(tempDir, 'src/main2.ts'),
  1285. `import { helper } from './';\nexport function go2(): void { helper(); }\n`
  1286. );
  1287. cg = await CodeGraph.init(tempDir, { index: true });
  1288. cg.resolveReferences();
  1289. const helperNode = cg
  1290. .getNodesByKind('function')
  1291. .find((n) => n.name === 'realHelper' && n.filePath === 'src/util.ts');
  1292. expect(helperNode).toBeDefined();
  1293. const callers = cg.getCallers(helperNode!.id);
  1294. expect(callers.some((c) => c.node.filePath === 'src/main.ts')).toBe(true);
  1295. expect(callers.some((c) => c.node.filePath === 'src/main2.ts')).toBe(true);
  1296. });
  1297. it('resolves a workspace package-subpath barrel (@scope/pkg/sub) to its index (#629)', async () => {
  1298. // bun/npm/pnpm workspace: `@scope/ui/widgets` → the `ui` package's
  1299. // `widgets/` subdir index, which re-exports a .svelte component.
  1300. // Alias `Thing` ≠ component `Widget` defeats the name-matcher, so
  1301. // only workspace-package resolution can connect the edge.
  1302. fs.mkdirSync(path.join(tempDir, 'packages/ui/widgets'), { recursive: true });
  1303. fs.writeFileSync(
  1304. path.join(tempDir, 'package.json'),
  1305. JSON.stringify({ name: 'root', private: true, workspaces: ['packages/*'] }, null, 2)
  1306. );
  1307. fs.writeFileSync(
  1308. path.join(tempDir, 'packages/ui/package.json'),
  1309. JSON.stringify({ name: '@scope/ui', version: '1.0.0' }, null, 2)
  1310. );
  1311. fs.writeFileSync(
  1312. path.join(tempDir, 'packages/ui/widgets/Widget.svelte'),
  1313. `<script lang="ts">\n export let label: string = '';\n</script>\n\n<button>{label}</button>\n`
  1314. );
  1315. fs.writeFileSync(
  1316. path.join(tempDir, 'packages/ui/widgets/index.ts'),
  1317. `export { default as Thing } from './Widget.svelte';\n`
  1318. );
  1319. fs.mkdirSync(path.join(tempDir, 'app'), { recursive: true });
  1320. fs.writeFileSync(
  1321. path.join(tempDir, 'app/App.svelte'),
  1322. `<script lang="ts">\n import { Thing } from '@scope/ui/widgets';\n</script>\n\n<Thing />\n`
  1323. );
  1324. cg = await CodeGraph.init(tempDir, { index: true });
  1325. cg.resolveReferences();
  1326. const buttonNode = cg
  1327. .getNodesByKind('component')
  1328. .find((n) => n.name === 'Widget' && n.filePath === 'packages/ui/widgets/Widget.svelte');
  1329. expect(buttonNode).toBeDefined();
  1330. const callers = cg.getCallers(buttonNode!.id);
  1331. expect(callers.some((c) => c.node.filePath === 'app/App.svelte')).toBe(true);
  1332. });
  1333. it('resolves a barrel import from a Vue SFC <script> block (#629)', async () => {
  1334. // The same import-resolution gaps (no SFC import mappings, no SFC
  1335. // extension list, barrel parsed in the consumer's language) broke
  1336. // Vue SFCs too. Guards the resolver-side generalization to `.vue`.
  1337. // The barrel renames `realRun` → `run` so only the import-chase (not
  1338. // the name-matcher) can connect the call.
  1339. fs.mkdirSync(path.join(tempDir, 'src'), { recursive: true });
  1340. fs.writeFileSync(
  1341. path.join(tempDir, 'src/util.ts'),
  1342. `export function realRun(): void {}\n`
  1343. );
  1344. fs.writeFileSync(
  1345. path.join(tempDir, 'src/index.ts'),
  1346. `export { realRun as run } from './util';\n`
  1347. );
  1348. fs.writeFileSync(
  1349. path.join(tempDir, 'src/App.vue'),
  1350. `<script lang="ts">\nimport { run } from './';\nexport default { mounted() { run(); } };\n</script>\n<template><div/></template>\n`
  1351. );
  1352. cg = await CodeGraph.init(tempDir, { index: true });
  1353. cg.resolveReferences();
  1354. const runNode = cg
  1355. .getNodesByKind('function')
  1356. .find((n) => n.name === 'realRun' && n.filePath === 'src/util.ts');
  1357. expect(runNode).toBeDefined();
  1358. const callers = cg.getCallers(runNode!.id);
  1359. expect(callers.some((c) => c.node.filePath === 'src/App.vue')).toBe(true);
  1360. });
  1361. it('follows a Vue component used in a <template> through a default re-export barrel (#629)', async () => {
  1362. // End-to-end Vue analogue of the Svelte case: the leaf is a `.vue`
  1363. // component re-exported under an alias (`Thing`) that differs from its
  1364. // real name (`Widget`), and the consumer uses it ONLY in markup
  1365. // (`<Thing />`). Requires both the new template-tag extraction AND the
  1366. // barrel default-export chase to connect the edge.
  1367. fs.mkdirSync(path.join(tempDir, 'src/lib'), { recursive: true });
  1368. fs.writeFileSync(
  1369. path.join(tempDir, 'src/lib/Widget.vue'),
  1370. `<script setup lang="ts">\ndefineProps<{ label?: string }>();\n</script>\n<template><button>x</button></template>\n`
  1371. );
  1372. fs.writeFileSync(
  1373. path.join(tempDir, 'src/lib/index.ts'),
  1374. `export { default as Thing } from './Widget.vue';\n`
  1375. );
  1376. fs.writeFileSync(
  1377. path.join(tempDir, 'src/App.vue'),
  1378. `<script setup lang="ts">\nimport { Thing } from './lib';\n</script>\n<template>\n <Thing />\n</template>\n`
  1379. );
  1380. cg = await CodeGraph.init(tempDir, { index: true });
  1381. cg.resolveReferences();
  1382. const widgetNode = cg
  1383. .getNodesByKind('component')
  1384. .find((n) => n.name === 'Widget' && n.filePath === 'src/lib/Widget.vue');
  1385. expect(widgetNode).toBeDefined();
  1386. const callers = cg.getCallers(widgetNode!.id);
  1387. expect(callers.some((c) => c.node.filePath === 'src/App.vue')).toBe(true);
  1388. });
  1389. });
  1390. describe('C/C++ Import Resolution', () => {
  1391. afterEach(() => {
  1392. clearCppIncludeDirCache();
  1393. });
  1394. it('should resolve C include to header in same directory', () => {
  1395. const context: ResolutionContext = {
  1396. getNodesInFile: () => [],
  1397. getNodesByName: () => [],
  1398. getNodesByQualifiedName: () => [],
  1399. getNodesByKind: () => [],
  1400. fileExists: (p) => p === 'utils.h',
  1401. readFile: () => null,
  1402. getProjectRoot: () => '',
  1403. getAllFiles: () => ['utils.h', 'main.c'],
  1404. };
  1405. const result = resolveImportPath(
  1406. 'utils.h',
  1407. 'main.c',
  1408. 'c',
  1409. context
  1410. );
  1411. expect(result).toBe('utils.h');
  1412. });
  1413. it('should resolve C++ include with .hpp extension', () => {
  1414. const context: ResolutionContext = {
  1415. getNodesInFile: () => [],
  1416. getNodesByName: () => [],
  1417. getNodesByQualifiedName: () => [],
  1418. getNodesByKind: () => [],
  1419. fileExists: (p) => p === 'include/myclass.hpp',
  1420. readFile: () => null,
  1421. getProjectRoot: () => '',
  1422. getAllFiles: () => ['include/myclass.hpp', 'src/main.cpp'],
  1423. getCppIncludeDirs: () => ['include'],
  1424. };
  1425. const result = resolveImportPath(
  1426. 'myclass.hpp',
  1427. 'src/main.cpp',
  1428. 'cpp',
  1429. context
  1430. );
  1431. expect(result).toBe('include/myclass.hpp');
  1432. });
  1433. it('should resolve include with subdirectory path', () => {
  1434. const context: ResolutionContext = {
  1435. getNodesInFile: () => [],
  1436. getNodesByName: () => [],
  1437. getNodesByQualifiedName: () => [],
  1438. getNodesByKind: () => [],
  1439. fileExists: (p) => p === 'utils/helpers.h',
  1440. readFile: () => null,
  1441. getProjectRoot: () => '',
  1442. getAllFiles: () => ['utils/helpers.h', 'main.c'],
  1443. };
  1444. const result = resolveImportPath(
  1445. 'utils/helpers.h',
  1446. 'main.c',
  1447. 'c',
  1448. context
  1449. );
  1450. expect(result).toBe('utils/helpers.h');
  1451. });
  1452. it('should resolve include via include directories', () => {
  1453. const context: ResolutionContext = {
  1454. getNodesInFile: () => [],
  1455. getNodesByName: () => [],
  1456. getNodesByQualifiedName: () => [],
  1457. getNodesByKind: () => [],
  1458. fileExists: (p) => p === 'include/myheader.h',
  1459. readFile: () => null,
  1460. getProjectRoot: () => '',
  1461. getAllFiles: () => ['include/myheader.h', 'src/main.cpp'],
  1462. getCppIncludeDirs: () => ['include'],
  1463. };
  1464. const result = resolveImportPath(
  1465. 'myheader.h',
  1466. 'src/main.cpp',
  1467. 'cpp',
  1468. context
  1469. );
  1470. expect(result).toBe('include/myheader.h');
  1471. });
  1472. it('should resolve include trying multiple extensions', () => {
  1473. const context: ResolutionContext = {
  1474. getNodesInFile: () => [],
  1475. getNodesByName: () => [],
  1476. getNodesByQualifiedName: () => [],
  1477. getNodesByKind: () => [],
  1478. // myclass.h does not exist, but myclass.hpp does
  1479. fileExists: (p) => p === 'include/myclass.hpp',
  1480. readFile: () => null,
  1481. getProjectRoot: () => '',
  1482. getAllFiles: () => ['include/myclass.hpp', 'src/main.cpp'],
  1483. getCppIncludeDirs: () => ['include'],
  1484. };
  1485. const result = resolveImportPath(
  1486. 'myclass',
  1487. 'src/main.cpp',
  1488. 'cpp',
  1489. context
  1490. );
  1491. expect(result).toBe('include/myclass.hpp');
  1492. });
  1493. it('should return null for system headers', () => {
  1494. const context: ResolutionContext = {
  1495. getNodesInFile: () => [],
  1496. getNodesByName: () => [],
  1497. getNodesByQualifiedName: () => [],
  1498. getNodesByKind: () => [],
  1499. fileExists: () => true,
  1500. readFile: () => null,
  1501. getProjectRoot: () => '',
  1502. getAllFiles: () => [],
  1503. };
  1504. // C standard library header
  1505. expect(resolveImportPath('stdio.h', 'main.c', 'c', context)).toBeNull();
  1506. // C++ standard library header
  1507. expect(resolveImportPath('vector', 'main.cpp', 'cpp', context)).toBeNull();
  1508. // C++ C-wrapper header
  1509. expect(resolveImportPath('cstdio', 'main.cpp', 'cpp', context)).toBeNull();
  1510. });
  1511. it('should return null for single-component third-party paths that cannot be resolved', () => {
  1512. const context: ResolutionContext = {
  1513. getNodesInFile: () => [],
  1514. getNodesByName: () => [],
  1515. getNodesByQualifiedName: () => [],
  1516. getNodesByKind: () => [],
  1517. fileExists: () => false,
  1518. readFile: () => null,
  1519. getProjectRoot: () => '',
  1520. getAllFiles: () => [],
  1521. getCppIncludeDirs: () => [],
  1522. };
  1523. // Third-party bare header without path — not resolvable, returns null
  1524. const result = resolveImportPath(
  1525. 'openssl/ssl.h',
  1526. 'main.cpp',
  1527. 'cpp',
  1528. context
  1529. );
  1530. expect(result).toBeNull();
  1531. });
  1532. it('should not filter project headers with path separators', () => {
  1533. const context: ResolutionContext = {
  1534. getNodesInFile: () => [],
  1535. getNodesByName: () => [],
  1536. getNodesByQualifiedName: () => [],
  1537. getNodesByKind: () => [],
  1538. fileExists: (p) => p === 'mylib/utils.h',
  1539. readFile: () => null,
  1540. getProjectRoot: () => '',
  1541. getAllFiles: () => ['mylib/utils.h'],
  1542. };
  1543. // Path with separator should NOT be filtered as external
  1544. const result = resolveImportPath(
  1545. 'mylib/utils.h',
  1546. 'main.c',
  1547. 'c',
  1548. context
  1549. );
  1550. expect(result).toBe('mylib/utils.h');
  1551. });
  1552. it('should extract C/C++ import mappings from #include directives', () => {
  1553. const code = `#include <iostream>
  1554. #include "myheader.h"
  1555. #include "utils/helpers.hpp"`;
  1556. const mappings = extractImportMappings('main.cpp', code, 'cpp');
  1557. expect(mappings.length).toBe(3);
  1558. expect(mappings[0]).toEqual({
  1559. localName: 'iostream',
  1560. exportedName: '*',
  1561. source: 'iostream',
  1562. isDefault: false,
  1563. isNamespace: true,
  1564. });
  1565. expect(mappings[1]).toEqual({
  1566. localName: 'myheader',
  1567. exportedName: '*',
  1568. source: 'myheader.h',
  1569. isDefault: false,
  1570. isNamespace: true,
  1571. });
  1572. expect(mappings[2]).toEqual({
  1573. localName: 'helpers',
  1574. exportedName: '*',
  1575. source: 'utils/helpers.hpp',
  1576. isDefault: false,
  1577. isNamespace: true,
  1578. });
  1579. });
  1580. it('should discover include directories from compile_commands.json', () => {
  1581. // Create a temp project with compile_commands.json
  1582. const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-cpp-test-'));
  1583. try {
  1584. const compileDb = [
  1585. {
  1586. directory: tempProject,
  1587. command: 'g++ -Iinclude -Isrc/lib -isystem /usr/include -c src/main.cpp',
  1588. file: 'src/main.cpp',
  1589. },
  1590. ];
  1591. fs.writeFileSync(
  1592. path.join(tempProject, 'compile_commands.json'),
  1593. JSON.stringify(compileDb)
  1594. );
  1595. // Create the include dirs so they exist
  1596. fs.mkdirSync(path.join(tempProject, 'include'), { recursive: true });
  1597. fs.mkdirSync(path.join(tempProject, 'src', 'lib'), { recursive: true });
  1598. clearCppIncludeDirCache();
  1599. const dirs = loadCppIncludeDirs(tempProject);
  1600. // Should find include and src/lib (relative to project root)
  1601. // /usr/include is absolute and outside project, should be excluded
  1602. expect(dirs).toContain('include');
  1603. expect(dirs).toContain('src/lib');
  1604. expect(dirs.some(d => d.includes('usr'))).toBe(false);
  1605. } finally {
  1606. fs.rmSync(tempProject, { recursive: true });
  1607. }
  1608. });
  1609. it('should fall back to heuristic include dirs when no compile_commands.json', () => {
  1610. const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-cpp-test-'));
  1611. try {
  1612. // Create include/ and src/ directories with headers
  1613. fs.mkdirSync(path.join(tempProject, 'include'), { recursive: true });
  1614. fs.writeFileSync(path.join(tempProject, 'include', 'types.h'), '');
  1615. fs.mkdirSync(path.join(tempProject, 'src'), { recursive: true });
  1616. fs.writeFileSync(path.join(tempProject, 'src', 'main.cpp'), '');
  1617. // Create a directory without headers — should not be included
  1618. fs.mkdirSync(path.join(tempProject, 'docs'), { recursive: true });
  1619. clearCppIncludeDirCache();
  1620. const dirs = loadCppIncludeDirs(tempProject);
  1621. expect(dirs).toContain('include');
  1622. expect(dirs).toContain('src');
  1623. expect(dirs).not.toContain('docs');
  1624. } finally {
  1625. fs.rmSync(tempProject, { recursive: true });
  1626. }
  1627. });
  1628. // Documents the cross-language `.h` behavior. Objective-C and C++ share
  1629. // the `.h` extension, so in a mixed iOS-style project an Obj-C header
  1630. // dir gets claimed as a C/C++ include dir too. That's intentional — a
  1631. // C++ file legitimately can `#include "Foo.h"` against an Obj-C header
  1632. // (Obj-C++ / .mm callers), and false-positive inclusion is far cheaper
  1633. // than missing real resolutions. The test pins this so a later
  1634. // "exclude objc dirs" refactor breaks loudly and reviewers see the
  1635. // trade-off explicitly.
  1636. it('heuristic claims any top-level dir containing .h files, including Obj-C', () => {
  1637. const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-cpp-test-'));
  1638. try {
  1639. // C++ side: an `cppmod` dir with a .hpp (C++-only extension)
  1640. fs.mkdirSync(path.join(tempProject, 'cppmod'), { recursive: true });
  1641. fs.writeFileSync(path.join(tempProject, 'cppmod', 'shared.hpp'), '');
  1642. // Obj-C side: an `iosmod` dir with .h + .m (no .cpp/.hpp).
  1643. fs.mkdirSync(path.join(tempProject, 'iosmod'), { recursive: true });
  1644. fs.writeFileSync(path.join(tempProject, 'iosmod', 'View.h'), '');
  1645. fs.writeFileSync(path.join(tempProject, 'iosmod', 'View.m'), '');
  1646. clearCppIncludeDirCache();
  1647. const dirs = loadCppIncludeDirs(tempProject);
  1648. // Both included — Obj-C dirs are intentionally allowed.
  1649. expect(dirs).toContain('cppmod');
  1650. expect(dirs).toContain('iosmod');
  1651. } finally {
  1652. fs.rmSync(tempProject, { recursive: true });
  1653. }
  1654. });
  1655. // End-to-end: ensure `#include "X.h"` produces a file→file `imports` edge
  1656. // in the actual indexing pipeline (not just a phantom file→import-node
  1657. // edge). This pins the include-dir resolution path so the headline PR
  1658. // feature can't silently regress to a no-op in the indexing flow.
  1659. it('connects #include to the real header file via include-dir scan (end-to-end)', async () => {
  1660. const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-cpp-e2e-'));
  1661. try {
  1662. fs.mkdirSync(path.join(tempProject, 'include'), { recursive: true });
  1663. fs.mkdirSync(path.join(tempProject, 'src'), { recursive: true });
  1664. fs.writeFileSync(
  1665. path.join(tempProject, 'include', 'utils.h'),
  1666. `#ifndef UTILS_H\n#define UTILS_H\nint add(int, int);\n#endif\n`
  1667. );
  1668. fs.writeFileSync(
  1669. path.join(tempProject, 'src', 'main.cpp'),
  1670. `#include "utils.h"\n#include <vector>\nint main(){ return add(1,2); }\n`
  1671. );
  1672. clearCppIncludeDirCache();
  1673. cg = await CodeGraph.init(tempProject, { index: true });
  1674. // Sanity: file nodes exist for the header and the cpp.
  1675. const allFiles = cg.getStats();
  1676. expect(allFiles.fileCount).toBe(2);
  1677. // The `#include "utils.h"` edge should target the real
  1678. // `include/utils.h` file node — not a floating `import` node
  1679. // living inside main.cpp.
  1680. const db = DatabaseConnection.open(path.join(tempProject, '.codegraph', 'codegraph.db'));
  1681. const rows = db.getDb().prepare(`
  1682. select dst.kind as dstKind, dst.file_path as dstPath
  1683. from edges e
  1684. join nodes src on e.source = src.id
  1685. join nodes dst on e.target = dst.id
  1686. where e.kind = 'imports'
  1687. and src.kind = 'file'
  1688. and src.file_path = 'src/main.cpp'
  1689. `).all() as Array<{ dstKind: string; dstPath: string }>;
  1690. const resolvedToHeader = rows.find(
  1691. (r) => r.dstKind === 'file' && r.dstPath === 'include/utils.h'
  1692. );
  1693. expect(resolvedToHeader, 'main.cpp → include/utils.h imports edge missing').toBeDefined();
  1694. // `<vector>` should NOT produce a file edge — it's a stdlib header.
  1695. const stdlibFile = rows.find(
  1696. (r) => r.dstKind === 'file' && r.dstPath && r.dstPath.endsWith('vector')
  1697. );
  1698. expect(stdlibFile).toBeUndefined();
  1699. } finally {
  1700. fs.rmSync(tempProject, { recursive: true, force: true });
  1701. }
  1702. });
  1703. });
  1704. describe('PHP Include Resolution', () => {
  1705. it('isPhpIncludePathRef distinguishes include paths from namespace use (#660)', () => {
  1706. const mk = (name: string, over: Partial<UnresolvedRef> = {}): UnresolvedRef => ({
  1707. fromNodeId: 'f', referenceName: name, referenceKind: 'imports',
  1708. line: 1, column: 0, filePath: 'x.php', language: 'php', ...over,
  1709. });
  1710. // include paths: contain a slash or a file extension
  1711. expect(isPhpIncludePathRef(mk('lib.php'))).toBe(true);
  1712. expect(isPhpIncludePathRef(mk('inc/db.php'))).toBe(true);
  1713. expect(isPhpIncludePathRef(mk('../config.php'))).toBe(true);
  1714. // namespace use symbols: a bare class (Closure) or FQN — never a path,
  1715. // so they must NOT be treated as includes (would mis-connect to a
  1716. // same-named Closure.php / Bar.php file).
  1717. expect(isPhpIncludePathRef(mk('Closure'))).toBe(false);
  1718. expect(isPhpIncludePathRef(mk('PDO'))).toBe(false);
  1719. expect(isPhpIncludePathRef(mk('App\\Foo\\Bar'))).toBe(false);
  1720. // scoped to PHP imports only
  1721. expect(isPhpIncludePathRef(mk('lib.php', { language: 'c' }))).toBe(false);
  1722. expect(isPhpIncludePathRef(mk('lib.php', { referenceKind: 'calls' }))).toBe(false);
  1723. });
  1724. it('resolves require_once to a file→file imports edge (#660)', async () => {
  1725. const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-php-e2e-'));
  1726. try {
  1727. fs.mkdirSync(path.join(tempProject, 'src'), { recursive: true });
  1728. fs.writeFileSync(
  1729. path.join(tempProject, 'src', 'lib.php'),
  1730. `<?php\nfunction greet() { return "hi"; }\n`
  1731. );
  1732. fs.writeFileSync(
  1733. path.join(tempProject, 'src', 'page.php'),
  1734. `<?php\nrequire_once("lib.php");\necho greet();\n`
  1735. );
  1736. cg = await CodeGraph.init(tempProject, { index: true });
  1737. // reporter's repro: page.php's `require_once("lib.php")` must resolve
  1738. // to the real src/lib.php file node — a file→file `imports` edge, so
  1739. // callers(lib.php) now includes page.php.
  1740. const db = DatabaseConnection.open(path.join(tempProject, '.codegraph', 'codegraph.db'));
  1741. const rows = db.getDb().prepare(`
  1742. select dst.kind as dstKind, dst.file_path as dstPath
  1743. from edges e
  1744. join nodes src on e.source = src.id
  1745. join nodes dst on e.target = dst.id
  1746. where e.kind = 'imports'
  1747. and src.kind = 'file'
  1748. and src.file_path = 'src/page.php'
  1749. `).all() as Array<{ dstKind: string; dstPath: string }>;
  1750. const resolved = rows.find(
  1751. (r) => r.dstKind === 'file' && r.dstPath === 'src/lib.php'
  1752. );
  1753. expect(resolved, 'page.php → src/lib.php imports edge missing').toBeDefined();
  1754. } finally {
  1755. fs.rmSync(tempProject, { recursive: true, force: true });
  1756. }
  1757. });
  1758. it('resolves a subdirectory include path to the correct file (#660)', async () => {
  1759. const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-php-subdir-'));
  1760. try {
  1761. fs.mkdirSync(path.join(tempProject, 'inc'), { recursive: true });
  1762. fs.writeFileSync(
  1763. path.join(tempProject, 'inc', 'db.php'),
  1764. `<?php\nfunction query() { return 1; }\n`
  1765. );
  1766. fs.writeFileSync(
  1767. path.join(tempProject, 'index.php'),
  1768. `<?php\nrequire "inc/db.php";\nquery();\n`
  1769. );
  1770. cg = await CodeGraph.init(tempProject, { index: true });
  1771. const db = DatabaseConnection.open(path.join(tempProject, '.codegraph', 'codegraph.db'));
  1772. const rows = db.getDb().prepare(`
  1773. select dst.kind as dstKind, dst.file_path as dstPath
  1774. from edges e
  1775. join nodes src on e.source = src.id
  1776. join nodes dst on e.target = dst.id
  1777. where e.kind = 'imports'
  1778. and src.kind = 'file'
  1779. and src.file_path = 'index.php'
  1780. `).all() as Array<{ dstKind: string; dstPath: string }>;
  1781. expect(
  1782. rows.find((r) => r.dstKind === 'file' && r.dstPath === 'inc/db.php'),
  1783. 'index.php → inc/db.php imports edge missing'
  1784. ).toBeDefined();
  1785. } finally {
  1786. fs.rmSync(tempProject, { recursive: true, force: true });
  1787. }
  1788. });
  1789. it('does not mis-connect an unresolvable include to a same-named file elsewhere (#660)', async () => {
  1790. const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-php-misresolve-'));
  1791. try {
  1792. // app/page.php's `require "inc/db.php"` resolves relative to app/, where
  1793. // inc/db.php does NOT exist. A same-named lib/inc/db.php exists elsewhere
  1794. // but is unrelated — no edge should be created (a wrong edge is worse
  1795. // than a missing one).
  1796. fs.mkdirSync(path.join(tempProject, 'app'), { recursive: true });
  1797. fs.mkdirSync(path.join(tempProject, 'lib', 'inc'), { recursive: true });
  1798. fs.writeFileSync(
  1799. path.join(tempProject, 'lib', 'inc', 'db.php'),
  1800. `<?php\nfunction unrelated() {}\n`
  1801. );
  1802. fs.writeFileSync(
  1803. path.join(tempProject, 'app', 'page.php'),
  1804. `<?php\nrequire "inc/db.php";\n`
  1805. );
  1806. cg = await CodeGraph.init(tempProject, { index: true });
  1807. const db = DatabaseConnection.open(path.join(tempProject, '.codegraph', 'codegraph.db'));
  1808. const rows = db.getDb().prepare(`
  1809. select dst.kind as dstKind, dst.file_path as dstPath
  1810. from edges e
  1811. join nodes src on e.source = src.id
  1812. join nodes dst on e.target = dst.id
  1813. where e.kind = 'imports'
  1814. and src.kind = 'file'
  1815. and src.file_path = 'app/page.php'
  1816. `).all() as Array<{ dstKind: string; dstPath: string }>;
  1817. expect(
  1818. rows.find((r) => r.dstKind === 'file' && r.dstPath === 'lib/inc/db.php'),
  1819. 'app/page.php must NOT mis-connect to unrelated lib/inc/db.php'
  1820. ).toBeUndefined();
  1821. } finally {
  1822. fs.rmSync(tempProject, { recursive: true, force: true });
  1823. }
  1824. });
  1825. });
  1826. describe('C++ chained-call receiver resolution (#645)', () => {
  1827. async function indexCpp(files: Record<string, string>): Promise<void> {
  1828. for (const [name, content] of Object.entries(files)) {
  1829. fs.writeFileSync(path.join(tempDir, name), content);
  1830. }
  1831. cg = await CodeGraph.init(tempDir, { index: true });
  1832. }
  1833. function callerNamesOf(qualifiedName: string): string[] {
  1834. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  1835. if (!target) return [];
  1836. const names = cg
  1837. .getIncomingEdges(target.id)
  1838. .filter((e) => e.kind === 'calls')
  1839. .map((e) => cg.getNode(e.source)?.name)
  1840. .filter((n): n is string => !!n);
  1841. return [...new Set(names)].sort();
  1842. }
  1843. it('resolves singleton chains and auto locals to the right class, never the first-sorted one', async () => {
  1844. // Two classes share writeLog; Logger sorts first so it wins any name-only
  1845. // tie. All three call forms target Metrics.
  1846. await indexCpp({
  1847. 'logger.hpp': `#pragma once
  1848. #include <string>
  1849. class Logger { public: static Logger& instance(); void writeLog(const std::string&); };
  1850. class Metrics { public: static Metrics& instance(); void writeLog(const std::string&); };
  1851. `,
  1852. 'impl.cpp': `#include "logger.hpp"
  1853. Logger& Logger::instance() { static Logger l; return l; }
  1854. Metrics& Metrics::instance() { static Metrics m; return m; }
  1855. void Logger::writeLog(const std::string&) {}
  1856. void Metrics::writeLog(const std::string&) {}
  1857. `,
  1858. 'app.cpp': `#include "logger.hpp"
  1859. void a() { Metrics::instance().writeLog("x"); } // chained singleton
  1860. void b() { auto& m = Metrics::instance(); m.writeLog("x"); } // stored in auto
  1861. void c() { Metrics& m = Metrics::instance(); m.writeLog("x"); } // explicit type
  1862. `,
  1863. });
  1864. expect(callerNamesOf('Metrics::writeLog')).toEqual(['a', 'b', 'c']);
  1865. expect(callerNamesOf('Logger::writeLog')).toEqual([]);
  1866. });
  1867. it('resolves factories, free-function factories, and member chains via the inner call return type', async () => {
  1868. await indexCpp({
  1869. 'types.hpp': `#pragma once
  1870. #include <memory>
  1871. struct Widget { void draw(); };
  1872. struct Session { void run(); };
  1873. struct View { void render(); };
  1874. class WidgetFactory { public: static Widget create(); };
  1875. class Manager { public: View view(); };
  1876. Session* openSession();
  1877. // Decoy that sorts first and has all three methods — must never win.
  1878. struct Aaa { void draw(); void run(); void render(); };
  1879. `,
  1880. 'impl.cpp': `#include "types.hpp"
  1881. void Widget::draw() {}
  1882. void Session::run() {}
  1883. void View::render() {}
  1884. void Aaa::draw() {}
  1885. void Aaa::run() {}
  1886. void Aaa::render() {}
  1887. Widget WidgetFactory::create() { return Widget(); }
  1888. View Manager::view() { return View(); }
  1889. Session* openSession() { return nullptr; }
  1890. `,
  1891. 'app.cpp': `#include "types.hpp"
  1892. void factory() { WidgetFactory::create().draw(); } // -> Widget::draw
  1893. void freefunc() { openSession()->run(); } // -> Session::run
  1894. void member() { Manager mgr; mgr.view().render(); } // -> View::render
  1895. void makeUnique() { auto w = std::make_unique<Widget>(); w->draw(); } // -> Widget::draw
  1896. `,
  1897. });
  1898. expect(callerNamesOf('Widget::draw')).toEqual(['factory', 'makeUnique']);
  1899. expect(callerNamesOf('Session::run')).toEqual(['freefunc']);
  1900. expect(callerNamesOf('View::render')).toEqual(['member']);
  1901. // The first-sorted decoy never captures any of them.
  1902. expect(callerNamesOf('Aaa::draw')).toEqual([]);
  1903. expect(callerNamesOf('Aaa::run')).toEqual([]);
  1904. expect(callerNamesOf('Aaa::render')).toEqual([]);
  1905. });
  1906. it('creates NO edge when the inferred type lacks the method (silent miss, not a wrong edge)', async () => {
  1907. await indexCpp({
  1908. 'types.hpp': `#pragma once
  1909. struct Widget { void draw(); };
  1910. struct Other { void onlyOther(); };
  1911. class WidgetFactory { public: static Widget create(); };
  1912. `,
  1913. 'impl.cpp': `#include "types.hpp"
  1914. void Widget::draw() {}
  1915. void Other::onlyOther() {}
  1916. Widget WidgetFactory::create() { return Widget(); }
  1917. `,
  1918. 'app.cpp': `#include "types.hpp"
  1919. // Widget has no onlyOther() — must produce NO edge, never a wrong one to Other.
  1920. void wrong() { WidgetFactory::create().onlyOther(); }
  1921. `,
  1922. });
  1923. expect(callerNamesOf('Other::onlyOther')).toEqual([]);
  1924. });
  1925. });
  1926. describe('PHP chained static-factory call resolution (#608)', () => {
  1927. function callerNamesOf(qualifiedName: string): string[] {
  1928. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  1929. if (!target) return [];
  1930. const names = cg
  1931. .getIncomingEdges(target.id)
  1932. .filter((e) => e.kind === 'calls')
  1933. .map((e) => cg.getNode(e.source)?.name)
  1934. .filter((n): n is string => !!n);
  1935. return [...new Set(names)].sort();
  1936. }
  1937. it('resolves Cls::for($x)->method() via the factory\'s `: self` return (#608)', async () => {
  1938. fs.writeFileSync(
  1939. path.join(tempDir, 'ApiClient.php'),
  1940. `<?php\nclass ApiClient {\n public static function for(string $c): self { return new self; }\n public function createOrder(array $p): array { return []; }\n}\n`
  1941. );
  1942. fs.writeFileSync(
  1943. path.join(tempDir, 'DispatchOrder.php'),
  1944. `<?php\nclass DispatchOrder {\n public function handle(): void {\n ApiClient::for('cred')->createOrder([]);\n }\n}\n`
  1945. );
  1946. cg = await CodeGraph.init(tempDir, { index: true });
  1947. // The chained call's edge attaches to the factory result's method.
  1948. expect(callerNamesOf('ApiClient::createOrder')).toContain('handle');
  1949. });
  1950. it('creates NO edge when the factory result lacks the method (#608)', async () => {
  1951. fs.writeFileSync(
  1952. path.join(tempDir, 'lib.php'),
  1953. `<?php\nclass ApiClient { public static function for(string $c): self { return new self; } }\nclass Other { public function onlyOther(): void {} }\nclass Caller { public function go(): void { ApiClient::for('x')->onlyOther(); } }\n`
  1954. );
  1955. cg = await CodeGraph.init(tempDir, { index: true });
  1956. // ApiClient has no onlyOther — must not mis-attach to the same-named Other::onlyOther.
  1957. expect(callerNamesOf('Other::onlyOther')).toEqual([]);
  1958. });
  1959. });
  1960. describe('Java chained static-factory call resolution (#645/#608 mechanism)', () => {
  1961. function callerNamesOf(qualifiedName: string): string[] {
  1962. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  1963. if (!target) return [];
  1964. const names = cg
  1965. .getIncomingEdges(target.id)
  1966. .filter((e) => e.kind === 'calls')
  1967. .map((e) => cg.getNode(e.source)?.name)
  1968. .filter((n): n is string => !!n);
  1969. return [...new Set(names)].sort();
  1970. }
  1971. it('resolves Foo.getInstance().bar() via the factory return type, never a same-named decoy', async () => {
  1972. // Aaa sorts first and has a same-named bar() — it must never win the chain.
  1973. fs.writeFileSync(
  1974. path.join(tempDir, 'Main.java'),
  1975. `class Aaa { void bar() {} }
  1976. class Foo {
  1977. static Foo getInstance() { return new Foo(); }
  1978. void bar() {}
  1979. }
  1980. class Caller {
  1981. void run() { Foo.getInstance().bar(); }
  1982. }
  1983. `
  1984. );
  1985. cg = await CodeGraph.init(tempDir, { index: true });
  1986. expect(callerNamesOf('Foo::bar')).toEqual(['run']);
  1987. expect(callerNamesOf('Aaa::bar')).toEqual([]);
  1988. });
  1989. it('resolves a factory chain that passes arguments — Foo.create(cfg).build()', async () => {
  1990. // The factory call carries an argument; the extractor must normalize the
  1991. // receiver to empty parens (`Foo.create().build`) so the chain still splits.
  1992. fs.writeFileSync(
  1993. path.join(tempDir, 'Main.java'),
  1994. `class Config {}
  1995. class Foo {
  1996. static Foo create(Config c) { return new Foo(); }
  1997. void build() {}
  1998. }
  1999. class Caller {
  2000. void run() { Foo.create(new Config()).build(); }
  2001. }
  2002. `
  2003. );
  2004. cg = await CodeGraph.init(tempDir, { index: true });
  2005. expect(callerNamesOf('Foo::build')).toEqual(['run']);
  2006. });
  2007. it('creates NO edge when the factory return type lacks the method (silent miss, not a wrong edge)', async () => {
  2008. fs.writeFileSync(
  2009. path.join(tempDir, 'Main.java'),
  2010. `class Foo {
  2011. static Foo getInstance() { return new Foo(); }
  2012. }
  2013. class Other { void onlyOther() {} }
  2014. class Caller {
  2015. void run() { Foo.getInstance().onlyOther(); }
  2016. }
  2017. `
  2018. );
  2019. cg = await CodeGraph.init(tempDir, { index: true });
  2020. // Foo has no onlyOther() — must not mis-attach to the same-named Other::onlyOther.
  2021. expect(callerNamesOf('Other::onlyOther')).toEqual([]);
  2022. });
  2023. });
  2024. describe('Kotlin chained companion-factory call resolution (#645/#608 mechanism)', () => {
  2025. function callerNamesOf(qualifiedName: string): string[] {
  2026. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  2027. if (!target) return [];
  2028. const names = cg
  2029. .getIncomingEdges(target.id)
  2030. .filter((e) => e.kind === 'calls')
  2031. .map((e) => cg.getNode(e.source)?.name)
  2032. .filter((n): n is string => !!n);
  2033. return [...new Set(names)].sort();
  2034. }
  2035. it('resolves Foo.getInstance().bar() via the companion return type, never a same-named decoy', async () => {
  2036. // Aaa sorts first and has a same-named bar() — without the chain fix Kotlin
  2037. // dropped the receiver to a bare `bar` and attached to Aaa (a wrong edge).
  2038. fs.writeFileSync(
  2039. path.join(tempDir, 'Main.kt'),
  2040. `class Aaa { fun bar() {} }
  2041. class Foo {
  2042. companion object {
  2043. fun getInstance(): Foo = Foo()
  2044. }
  2045. fun bar() {}
  2046. }
  2047. class Caller {
  2048. fun run() { Foo.getInstance().bar() }
  2049. }
  2050. `
  2051. );
  2052. cg = await CodeGraph.init(tempDir, { index: true });
  2053. expect(callerNamesOf('Foo::bar')).toEqual(['run']);
  2054. expect(callerNamesOf('Aaa::bar')).toEqual([]);
  2055. });
  2056. it('resolves a companion factory chain that passes arguments — Foo.create(cfg).build()', async () => {
  2057. fs.writeFileSync(
  2058. path.join(tempDir, 'Main.kt'),
  2059. `class Config
  2060. class Foo {
  2061. companion object {
  2062. fun create(c: Config): Foo = Foo()
  2063. }
  2064. fun build() {}
  2065. }
  2066. class Caller {
  2067. fun run() { Foo.create(Config()).build() }
  2068. }
  2069. `
  2070. );
  2071. cg = await CodeGraph.init(tempDir, { index: true });
  2072. expect(callerNamesOf('Foo::build')).toEqual(['run']);
  2073. });
  2074. it('creates NO edge when the companion return type lacks the method (silent miss, not a wrong edge)', async () => {
  2075. fs.writeFileSync(
  2076. path.join(tempDir, 'Main.kt'),
  2077. `class Foo {
  2078. companion object {
  2079. fun getInstance(): Foo = Foo()
  2080. }
  2081. }
  2082. class Other { fun onlyOther() {} }
  2083. class Caller {
  2084. fun run() { Foo.getInstance().onlyOther() }
  2085. }
  2086. `
  2087. );
  2088. cg = await CodeGraph.init(tempDir, { index: true });
  2089. // Foo has no onlyOther() — must not mis-attach to the same-named Other::onlyOther.
  2090. expect(callerNamesOf('Other::onlyOther')).toEqual([]);
  2091. });
  2092. });
  2093. describe('C# chained static-factory call resolution (#645/#608 mechanism)', () => {
  2094. function callerNamesOf(qualifiedName: string): string[] {
  2095. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  2096. if (!target) return [];
  2097. const names = cg
  2098. .getIncomingEdges(target.id)
  2099. .filter((e) => e.kind === 'calls')
  2100. .map((e) => cg.getNode(e.source)?.name)
  2101. .filter((n): n is string => !!n);
  2102. return [...new Set(names)].sort();
  2103. }
  2104. it('resolves Foo.Create().Bar() via the factory return type, never a same-named decoy', async () => {
  2105. // Aaa sorts first and has a same-named Bar() — it must never win the chain.
  2106. fs.writeFileSync(
  2107. path.join(tempDir, 'Main.cs'),
  2108. `class Aaa { void Bar() {} }
  2109. class Foo {
  2110. static Foo Create() { return new Foo(); }
  2111. void Bar() {}
  2112. }
  2113. class Caller {
  2114. void Run() { Foo.Create().Bar(); }
  2115. }
  2116. `
  2117. );
  2118. cg = await CodeGraph.init(tempDir, { index: true });
  2119. expect(callerNamesOf('Foo::Bar')).toEqual(['Run']);
  2120. expect(callerNamesOf('Aaa::Bar')).toEqual([]);
  2121. });
  2122. it('resolves a factory chain that passes arguments — Foo.Make(cfg).Build()', async () => {
  2123. fs.writeFileSync(
  2124. path.join(tempDir, 'Main.cs'),
  2125. `class Config {}
  2126. class Foo {
  2127. static Foo Make(Config c) { return new Foo(); }
  2128. void Build() {}
  2129. }
  2130. class Caller {
  2131. void Run() { Foo.Make(new Config()).Build(); }
  2132. }
  2133. `
  2134. );
  2135. cg = await CodeGraph.init(tempDir, { index: true });
  2136. expect(callerNamesOf('Foo::Build')).toEqual(['Run']);
  2137. });
  2138. it('creates NO edge when the factory return type lacks the method (silent miss, not a wrong edge)', async () => {
  2139. fs.writeFileSync(
  2140. path.join(tempDir, 'Main.cs'),
  2141. `class Foo {
  2142. static Foo Create() { return new Foo(); }
  2143. }
  2144. class Other { void OnlyOther() {} }
  2145. class Caller {
  2146. void Run() { Foo.Create().OnlyOther(); }
  2147. }
  2148. `
  2149. );
  2150. cg = await CodeGraph.init(tempDir, { index: true });
  2151. // Foo has no OnlyOther() — must not mis-attach to the same-named Other::OnlyOther.
  2152. expect(callerNamesOf('Other::OnlyOther')).toEqual([]);
  2153. });
  2154. });
  2155. describe('Swift chained static-factory call resolution (#645/#608 mechanism)', () => {
  2156. function callerNamesOf(qualifiedName: string): string[] {
  2157. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  2158. if (!target) return [];
  2159. const names = cg
  2160. .getIncomingEdges(target.id)
  2161. .filter((e) => e.kind === 'calls')
  2162. .map((e) => cg.getNode(e.source)?.name)
  2163. .filter((n): n is string => !!n);
  2164. return [...new Set(names)].sort();
  2165. }
  2166. it('resolves Foo.make().draw() via the factory return type, never a same-named decoy', async () => {
  2167. // Aaa sorts first and has a same-named draw() — without the fix Swift dropped
  2168. // the receiver to a bare `draw` and attached to Aaa (a wrong edge).
  2169. fs.writeFileSync(
  2170. path.join(tempDir, 'Main.swift'),
  2171. `class Aaa { func draw() {} }
  2172. class Foo {
  2173. static func make() -> Foo { return Foo() }
  2174. func draw() {}
  2175. }
  2176. func runCaller() { Foo.make().draw() }
  2177. `
  2178. );
  2179. cg = await CodeGraph.init(tempDir, { index: true });
  2180. expect(callerNamesOf('Foo::draw')).toEqual(['runCaller']);
  2181. expect(callerNamesOf('Aaa::draw')).toEqual([]);
  2182. });
  2183. it('resolves a constructor chain Foo().draw() and an args factory chain Foo.build(c).render()', async () => {
  2184. fs.writeFileSync(
  2185. path.join(tempDir, 'Main.swift'),
  2186. `class Config {}
  2187. class Foo {
  2188. static func build(_ c: Config) -> Foo { return Foo() }
  2189. func draw() {}
  2190. func render() {}
  2191. }
  2192. func runCaller() {
  2193. Foo().draw()
  2194. Foo.build(Config()).render()
  2195. }
  2196. `
  2197. );
  2198. cg = await CodeGraph.init(tempDir, { index: true });
  2199. expect(callerNamesOf('Foo::draw')).toEqual(['runCaller']);
  2200. expect(callerNamesOf('Foo::render')).toEqual(['runCaller']);
  2201. });
  2202. it('creates NO edge when the factory return type lacks the method (silent miss, not a wrong edge)', async () => {
  2203. fs.writeFileSync(
  2204. path.join(tempDir, 'Main.swift'),
  2205. `class Foo {
  2206. static func make() -> Foo { return Foo() }
  2207. }
  2208. class Other { func onlyOther() {} }
  2209. func runCaller() { Foo.make().onlyOther() }
  2210. `
  2211. );
  2212. cg = await CodeGraph.init(tempDir, { index: true });
  2213. // Foo has no onlyOther() — must not mis-attach to the same-named Other::onlyOther.
  2214. expect(callerNamesOf('Other::onlyOther')).toEqual([]);
  2215. });
  2216. });
  2217. describe('Chained call resolves a method on a supertype (conformance, #750)', () => {
  2218. function callerNamesOf(qualifiedName: string): string[] {
  2219. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  2220. if (!target) return [];
  2221. const names = cg
  2222. .getIncomingEdges(target.id)
  2223. .filter((e) => e.kind === 'calls')
  2224. .map((e) => cg.getNode(e.source)?.name)
  2225. .filter((n): n is string => !!n);
  2226. return [...new Set(names)].sort();
  2227. }
  2228. it('resolves a chained method defined only on a SUPERCLASS the return type extends', async () => {
  2229. // draw() lives on Base; Widget (the factory's return type) has no draw() of
  2230. // its own. Decoy.draw must never win. Needs the conformance second pass.
  2231. fs.writeFileSync(
  2232. path.join(tempDir, 'Main.java'),
  2233. `class Base { void draw() {} }
  2234. class Widget extends Base {}
  2235. class Decoy { void draw() {} }
  2236. class Factory { static Widget create() { return new Widget(); } }
  2237. class Caller {
  2238. void run() { Factory.create().draw(); }
  2239. }
  2240. `
  2241. );
  2242. cg = await CodeGraph.init(tempDir, { index: true });
  2243. expect(callerNamesOf('Base::draw')).toEqual(['run']);
  2244. expect(callerNamesOf('Decoy::draw')).toEqual([]);
  2245. });
  2246. it('resolves a chained method defined on an INTERFACE the return type implements (default method)', async () => {
  2247. fs.writeFileSync(
  2248. path.join(tempDir, 'Main.java'),
  2249. `interface Drawable { default void draw() {} }
  2250. class Widget implements Drawable {}
  2251. class Decoy { void draw() {} }
  2252. class Factory { static Widget create() { return new Widget(); } }
  2253. class Caller {
  2254. void run() { Factory.create().draw(); }
  2255. }
  2256. `
  2257. );
  2258. cg = await CodeGraph.init(tempDir, { index: true });
  2259. expect(callerNamesOf('Drawable::draw')).toEqual(['run']);
  2260. expect(callerNamesOf('Decoy::draw')).toEqual([]);
  2261. });
  2262. it('still creates NO edge when no supertype has the method (safety preserved)', async () => {
  2263. fs.writeFileSync(
  2264. path.join(tempDir, 'Main.java'),
  2265. `class Base {}
  2266. class Widget extends Base {}
  2267. class Other { void onlyOther() {} }
  2268. class Factory { static Widget create() { return new Widget(); } }
  2269. class Caller {
  2270. void run() { Factory.create().onlyOther(); }
  2271. }
  2272. `
  2273. );
  2274. cg = await CodeGraph.init(tempDir, { index: true });
  2275. // Neither Widget nor Base has onlyOther() — must not attach to Other::onlyOther.
  2276. expect(callerNamesOf('Other::onlyOther')).toEqual([]);
  2277. });
  2278. });
  2279. describe('Rust chained associated-function call resolution (#645/#608 mechanism)', () => {
  2280. function callerNamesOf(qualifiedName: string): string[] {
  2281. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  2282. if (!target) return [];
  2283. const names = cg
  2284. .getIncomingEdges(target.id)
  2285. .filter((e) => e.kind === 'calls')
  2286. .map((e) => cg.getNode(e.source)?.name)
  2287. .filter((n): n is string => !!n);
  2288. return [...new Set(names)].sort();
  2289. }
  2290. it('resolves Foo::new().bar() (and a Self return) via the associated fn, never a same-named decoy', async () => {
  2291. fs.writeFileSync(
  2292. path.join(tempDir, 'main.rs'),
  2293. `struct Aaa { _x: i32 }
  2294. impl Aaa { fn bar(&self) {} }
  2295. struct Foo { _x: i32 }
  2296. impl Foo {
  2297. fn new() -> Foo { Foo { _x: 0 } }
  2298. fn make() -> Self { Foo { _x: 0 } }
  2299. fn bar(&self) {}
  2300. }
  2301. fn caller() {
  2302. Foo::new().bar();
  2303. Foo::make().bar();
  2304. }
  2305. `
  2306. );
  2307. cg = await CodeGraph.init(tempDir, { index: true });
  2308. expect(callerNamesOf('Foo::bar')).toEqual(['caller']);
  2309. expect(callerNamesOf('Aaa::bar')).toEqual([]);
  2310. });
  2311. it('resolves a chain that passes arguments — Foo::with(c).build()', async () => {
  2312. fs.writeFileSync(
  2313. path.join(tempDir, 'main.rs'),
  2314. `struct Config;
  2315. struct Foo { _x: i32 }
  2316. impl Foo {
  2317. fn with(c: Config) -> Foo { Foo { _x: 0 } }
  2318. fn build(&self) {}
  2319. }
  2320. fn caller() { Foo::with(Config).build(); }
  2321. `
  2322. );
  2323. cg = await CodeGraph.init(tempDir, { index: true });
  2324. expect(callerNamesOf('Foo::build')).toEqual(['caller']);
  2325. });
  2326. it('resolves a chained method from a trait the type implements (default method, via conformance)', async () => {
  2327. fs.writeFileSync(
  2328. path.join(tempDir, 'main.rs'),
  2329. `struct Foo { _x: i32 }
  2330. impl Foo { fn new() -> Foo { Foo { _x: 0 } } }
  2331. struct Decoy { _x: i32 }
  2332. impl Decoy { fn draw(&self) {} }
  2333. trait Drawable { fn draw(&self) {} }
  2334. impl Drawable for Foo {}
  2335. fn caller() { Foo::new().draw(); }
  2336. `
  2337. );
  2338. cg = await CodeGraph.init(tempDir, { index: true });
  2339. expect(callerNamesOf('Drawable::draw')).toEqual(['caller']);
  2340. expect(callerNamesOf('Decoy::draw')).toEqual([]);
  2341. });
  2342. it('creates NO edge when neither the type nor a supertype has the method (silent miss)', async () => {
  2343. fs.writeFileSync(
  2344. path.join(tempDir, 'main.rs'),
  2345. `struct Foo { _x: i32 }
  2346. impl Foo { fn new() -> Foo { Foo { _x: 0 } } }
  2347. struct Other { _x: i32 }
  2348. impl Other { fn only_other(&self) {} }
  2349. fn caller() { Foo::new().only_other(); }
  2350. `
  2351. );
  2352. cg = await CodeGraph.init(tempDir, { index: true });
  2353. // Foo has no only_other() — must not mis-attach to the same-named Other::only_other.
  2354. expect(callerNamesOf('Other::only_other')).toEqual([]);
  2355. });
  2356. });
  2357. describe('Go chained factory-function call resolution (#645/#608 mechanism)', () => {
  2358. function callerNamesOf(qualifiedName: string): string[] {
  2359. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  2360. if (!target) return [];
  2361. const names = cg
  2362. .getIncomingEdges(target.id)
  2363. .filter((e) => e.kind === 'calls')
  2364. .map((e) => cg.getNode(e.source)?.name)
  2365. .filter((n): n is string => !!n);
  2366. return [...new Set(names)].sort();
  2367. }
  2368. it('resolves New().Bar() via the factory return type (pointer), never a same-named decoy', async () => {
  2369. fs.writeFileSync(
  2370. path.join(tempDir, 'main.go'),
  2371. `package main
  2372. type Aaa struct{}
  2373. func (a *Aaa) Bar() {}
  2374. type Foo struct{}
  2375. func New() *Foo { return &Foo{} }
  2376. func (f *Foo) Bar() {}
  2377. func caller() { New().Bar() }
  2378. `
  2379. );
  2380. cg = await CodeGraph.init(tempDir, { index: true });
  2381. expect(callerNamesOf('Foo::Bar')).toEqual(['caller']);
  2382. expect(callerNamesOf('Aaa::Bar')).toEqual([]);
  2383. });
  2384. it('resolves an args chain and a multi-return factory — With(c).Build(), (*Foo, error)', async () => {
  2385. fs.writeFileSync(
  2386. path.join(tempDir, 'main.go'),
  2387. `package main
  2388. type Config struct{}
  2389. type Foo struct{}
  2390. func With(c Config) (*Foo, error) { return &Foo{}, nil }
  2391. func (f *Foo) Build() {}
  2392. func caller() { With(Config{}).Build() }
  2393. `
  2394. );
  2395. cg = await CodeGraph.init(tempDir, { index: true });
  2396. expect(callerNamesOf('Foo::Build')).toEqual(['caller']);
  2397. });
  2398. it('resolves a method provided by an embedded struct (via conformance)', async () => {
  2399. fs.writeFileSync(
  2400. path.join(tempDir, 'main.go'),
  2401. `package main
  2402. type Base struct{}
  2403. func (b *Base) Embedded() {}
  2404. type Decoy struct{}
  2405. func (d *Decoy) Embedded() {}
  2406. type Widget struct{ Base }
  2407. func NewWidget() *Widget { return &Widget{} }
  2408. func caller() { NewWidget().Embedded() }
  2409. `
  2410. );
  2411. cg = await CodeGraph.init(tempDir, { index: true });
  2412. expect(callerNamesOf('Base::Embedded')).toEqual(['caller']);
  2413. expect(callerNamesOf('Decoy::Embedded')).toEqual([]);
  2414. });
  2415. it('creates NO edge when neither the type nor an embedded type has the method (silent miss)', async () => {
  2416. fs.writeFileSync(
  2417. path.join(tempDir, 'main.go'),
  2418. `package main
  2419. type Foo struct{}
  2420. func New() *Foo { return &Foo{} }
  2421. type Other struct{}
  2422. func (o *Other) OnlyOther() {}
  2423. func caller() { New().OnlyOther() }
  2424. `
  2425. );
  2426. cg = await CodeGraph.init(tempDir, { index: true });
  2427. // Foo has no OnlyOther() — must not mis-attach to the same-named Other::OnlyOther.
  2428. expect(callerNamesOf('Other::OnlyOther')).toEqual([]);
  2429. });
  2430. it('falls back to bare-name resolution for a VARIABLE-inner chain without exploding the graph', async () => {
  2431. // `engine` is a package-level VARIABLE holding a func value, not a factory
  2432. // FUNCTION — so its return type can't be recovered and the chain falls back
  2433. // to bare-name resolution of the method (restoring the pre-re-encoding edge).
  2434. // Regression for the runaway this fallback originally caused: it resolved
  2435. // with a mutated `original.referenceName` (the bare `ServeHTTP`, not the
  2436. // stored `engine().ServeHTTP`), so the batched resolver's keyed delete
  2437. // no-oped, the offset-0 batch never drained, and edges inserted forever
  2438. // (5M edges / 1.4 GB on a 99-file repo). The fallback now ties the match to
  2439. // the original ref, and a non-progress guard backstops the loop.
  2440. fs.writeFileSync(
  2441. path.join(tempDir, 'main.go'),
  2442. `package main
  2443. type Server struct{}
  2444. func (s *Server) ServeHTTP() {}
  2445. var engine = func() *Server { return &Server{} }
  2446. func caller() { engine().ServeHTTP() }
  2447. `
  2448. );
  2449. cg = await CodeGraph.init(tempDir, { index: true });
  2450. // Recall: the variable-inner chain still finds the method by bare name.
  2451. expect(callerNamesOf('Server::ServeHTTP')).toEqual(['caller']);
  2452. // No runaway: a single call site yields a single edge, not millions.
  2453. const target = cg
  2454. .getNodesByKind('method')
  2455. .find((n) => n.qualifiedName === 'Server::ServeHTTP')!;
  2456. const rawCalls = cg
  2457. .getIncomingEdges(target.id)
  2458. .filter((e) => e.kind === 'calls');
  2459. expect(rawCalls.length).toBeLessThan(5);
  2460. });
  2461. });
  2462. describe('Scala chained static-factory call resolution (#645/#608 mechanism)', () => {
  2463. function callerNamesOf(qualifiedName: string): string[] {
  2464. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  2465. if (!target) return [];
  2466. const names = cg
  2467. .getIncomingEdges(target.id)
  2468. .filter((e) => e.kind === 'calls')
  2469. .map((e) => cg.getNode(e.source)?.name)
  2470. .filter((n): n is string => !!n);
  2471. return [...new Set(names)].sort();
  2472. }
  2473. it('resolves a companion-factory chain Foo.create().doIt() to the return type, never a same-named decoy', async () => {
  2474. fs.writeFileSync(
  2475. path.join(tempDir, 'Main.scala'),
  2476. `object Foo {
  2477. def create(): Bar = new Bar()
  2478. }
  2479. class Bar {
  2480. def doIt(): Unit = {}
  2481. }
  2482. class Decoy {
  2483. def doIt(): Unit = {}
  2484. }
  2485. object Main {
  2486. def run(): Unit = { Foo.create().doIt() }
  2487. }
  2488. `
  2489. );
  2490. cg = await CodeGraph.init(tempDir, { index: true });
  2491. expect(callerNamesOf('Bar::doIt')).toEqual(['run']);
  2492. expect(callerNamesOf('Decoy::doIt')).toEqual([]);
  2493. });
  2494. it('resolves a case-class apply construction Point(x).dist() on the constructed class', async () => {
  2495. fs.writeFileSync(
  2496. path.join(tempDir, 'Main.scala'),
  2497. `class Point(x: Int) {
  2498. def dist(): Int = x
  2499. }
  2500. class Other {
  2501. def dist(): Int = 0
  2502. }
  2503. object Main {
  2504. def run(): Unit = { Point(3).dist() }
  2505. }
  2506. `
  2507. );
  2508. cg = await CodeGraph.init(tempDir, { index: true });
  2509. expect(callerNamesOf('Point::dist')).toEqual(['run']);
  2510. expect(callerNamesOf('Other::dist')).toEqual([]);
  2511. });
  2512. it('resolves a chained method provided by a trait the return type extends (via conformance)', async () => {
  2513. fs.writeFileSync(
  2514. path.join(tempDir, 'Main.scala'),
  2515. `trait Base {
  2516. def shared(): Unit = {}
  2517. }
  2518. class Widget extends Base
  2519. class Decoy {
  2520. def shared(): Unit = {}
  2521. }
  2522. object Factory {
  2523. def make(): Widget = new Widget()
  2524. }
  2525. object Main {
  2526. def run(): Unit = { Factory.make().shared() }
  2527. }
  2528. `
  2529. );
  2530. cg = await CodeGraph.init(tempDir, { index: true });
  2531. expect(callerNamesOf('Base::shared')).toEqual(['run']);
  2532. expect(callerNamesOf('Decoy::shared')).toEqual([]);
  2533. });
  2534. it('creates NO edge when neither the factory return type nor a supertype has the method (silent miss)', async () => {
  2535. fs.writeFileSync(
  2536. path.join(tempDir, 'Main.scala'),
  2537. `object Foo {
  2538. def create(): Bar = new Bar()
  2539. }
  2540. class Bar {
  2541. }
  2542. class Other {
  2543. def onlyOther(): Unit = {}
  2544. }
  2545. object Main {
  2546. def run(): Unit = { Foo.create().onlyOther() }
  2547. }
  2548. `
  2549. );
  2550. cg = await CodeGraph.init(tempDir, { index: true });
  2551. // Bar has no onlyOther() — must not mis-attach to the same-named Other::onlyOther.
  2552. expect(callerNamesOf('Other::onlyOther')).toEqual([]);
  2553. });
  2554. });
  2555. describe('Dart chained static-factory / factory-constructor call resolution (#645/#608 mechanism)', () => {
  2556. function callerNamesOf(qualifiedName: string): string[] {
  2557. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  2558. if (!target) return [];
  2559. const names = cg
  2560. .getIncomingEdges(target.id)
  2561. .filter((e) => e.kind === 'calls')
  2562. .map((e) => cg.getNode(e.source)?.name)
  2563. .filter((n): n is string => !!n);
  2564. return [...new Set(names)].sort();
  2565. }
  2566. it('resolves a static-factory chain Foo.makeBar().doIt() to the return type, never a same-named decoy', async () => {
  2567. fs.writeFileSync(
  2568. path.join(tempDir, 'main.dart'),
  2569. `class Foo {
  2570. static Bar makeBar() => Bar();
  2571. }
  2572. class Bar {
  2573. void doIt() {}
  2574. }
  2575. class Decoy {
  2576. void doIt() {}
  2577. }
  2578. void run() {
  2579. Foo.makeBar().doIt();
  2580. }
  2581. `
  2582. );
  2583. cg = await CodeGraph.init(tempDir, { index: true });
  2584. expect(callerNamesOf('Bar::doIt')).toEqual(['run']);
  2585. expect(callerNamesOf('Decoy::doIt')).toEqual([]);
  2586. });
  2587. it('resolves a named factory-constructor chain Foo.create().ship() on the constructed class', async () => {
  2588. fs.writeFileSync(
  2589. path.join(tempDir, 'main.dart'),
  2590. `class Foo {
  2591. Foo._();
  2592. factory Foo.create() => Foo._();
  2593. void ship() {}
  2594. }
  2595. class Decoy {
  2596. void ship() {}
  2597. }
  2598. void run() {
  2599. Foo.create().ship();
  2600. }
  2601. `
  2602. );
  2603. cg = await CodeGraph.init(tempDir, { index: true });
  2604. // The factory constructor `Foo.create` is now a node whose return type is Foo,
  2605. // so `ship` resolves on Foo, not the same-named Decoy.
  2606. expect(callerNamesOf('Foo::ship')).toEqual(['run']);
  2607. expect(callerNamesOf('Decoy::ship')).toEqual([]);
  2608. });
  2609. it('resolves a constructor-receiver chain Bar().doIt() on the constructed class', async () => {
  2610. fs.writeFileSync(
  2611. path.join(tempDir, 'main.dart'),
  2612. `class Bar {
  2613. void doIt() {}
  2614. }
  2615. class Decoy {
  2616. void doIt() {}
  2617. }
  2618. void run() {
  2619. Bar().doIt();
  2620. }
  2621. `
  2622. );
  2623. cg = await CodeGraph.init(tempDir, { index: true });
  2624. expect(callerNamesOf('Bar::doIt')).toEqual(['run']);
  2625. expect(callerNamesOf('Decoy::doIt')).toEqual([]);
  2626. });
  2627. it('resolves a chained method inherited from a superclass the return type extends (via conformance)', async () => {
  2628. fs.writeFileSync(
  2629. path.join(tempDir, 'main.dart'),
  2630. `class Base {
  2631. void render() {}
  2632. }
  2633. class Widget extends Base {
  2634. static Widget make() => Widget();
  2635. }
  2636. class Decoy {
  2637. void render() {}
  2638. }
  2639. void run() {
  2640. Widget.make().render();
  2641. }
  2642. `
  2643. );
  2644. cg = await CodeGraph.init(tempDir, { index: true });
  2645. expect(callerNamesOf('Base::render')).toEqual(['run']);
  2646. expect(callerNamesOf('Decoy::render')).toEqual([]);
  2647. });
  2648. it('creates NO edge when neither the factory return type nor a supertype has the method (silent miss)', async () => {
  2649. fs.writeFileSync(
  2650. path.join(tempDir, 'main.dart'),
  2651. `class Foo {
  2652. static Bar makeBar() => Bar();
  2653. }
  2654. class Bar {
  2655. }
  2656. class Other {
  2657. void onlyOther() {}
  2658. }
  2659. void run() {
  2660. Foo.makeBar().onlyOther();
  2661. }
  2662. `
  2663. );
  2664. cg = await CodeGraph.init(tempDir, { index: true });
  2665. // Bar has no onlyOther() — must not mis-attach to the same-named Other::onlyOther.
  2666. expect(callerNamesOf('Other::onlyOther')).toEqual([]);
  2667. });
  2668. it('still extracts a method tree-sitter misparses as a constructor (@override + record return)', async () => {
  2669. // tree-sitter-dart misparses `@override (A, B) reduce()` — the annotation
  2670. // swallows the record return type, so `reduce()` looks like a single-
  2671. // identifier constructor_signature. It must NOT be skipped as an unnamed
  2672. // ctor (its name doesn't match the class); its body call must attribute to
  2673. // `reduce`, not the class.
  2674. fs.writeFileSync(
  2675. path.join(tempDir, 'main.dart'),
  2676. `class Base {}
  2677. class Action extends Base {
  2678. Action({required int x});
  2679. @override
  2680. (int, String) reduce() {
  2681. return (compute(), "y");
  2682. }
  2683. int compute() => 1;
  2684. }
  2685. `
  2686. );
  2687. cg = await CodeGraph.init(tempDir, { index: true });
  2688. // reduce must be a node and its body call must resolve to Action::compute.
  2689. expect(callerNamesOf('Action::compute')).toEqual(['reduce']);
  2690. });
  2691. it('keeps plain construction Foo() as instantiation, not a Foo::Foo method call', async () => {
  2692. // The unnamed constructor is intentionally NOT extracted as a `Foo::Foo`
  2693. // method, so `Foo(...)` resolves to the class (an `instantiates` edge),
  2694. // never hijacked into a call to a phantom constructor method.
  2695. fs.writeFileSync(
  2696. path.join(tempDir, 'main.dart'),
  2697. `class Widget {
  2698. final int x;
  2699. Widget(this.x);
  2700. }
  2701. void run() {
  2702. Widget(3);
  2703. }
  2704. `
  2705. );
  2706. cg = await CodeGraph.init(tempDir, { index: true });
  2707. // No Foo::Foo phantom method node.
  2708. expect(cg.getNodesByKind('method').some((n) => n.qualifiedName === 'Widget::Widget')).toBe(false);
  2709. // The construction resolves to the class as an `instantiates` edge.
  2710. const widget = cg.getNodesByKind('class').find((n) => n.name === 'Widget')!;
  2711. const incoming = cg.getIncomingEdges(widget.id);
  2712. expect(incoming.some((e) => e.kind === 'instantiates')).toBe(true);
  2713. });
  2714. });
  2715. describe('Objective-C chained message-send call resolution (#645/#608 mechanism)', () => {
  2716. function callerNamesOf(qualifiedName: string): string[] {
  2717. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  2718. if (!target) return [];
  2719. const names = cg
  2720. .getIncomingEdges(target.id)
  2721. .filter((e) => e.kind === 'calls')
  2722. .map((e) => cg.getNode(e.source)?.name)
  2723. .filter((n): n is string => !!n);
  2724. return [...new Set(names)].sort();
  2725. }
  2726. it('resolves a chained message send [[Foo create] doIt] via the return type, never a same-named decoy', async () => {
  2727. fs.writeFileSync(
  2728. path.join(tempDir, 'main.m'),
  2729. `@interface Bar : NSObject
  2730. - (void)doIt;
  2731. @end
  2732. @implementation Bar
  2733. - (void)doIt {}
  2734. @end
  2735. @interface Decoy : NSObject
  2736. - (void)doIt;
  2737. @end
  2738. @implementation Decoy
  2739. - (void)doIt {}
  2740. @end
  2741. @interface Foo : NSObject
  2742. + (Bar *)create;
  2743. @end
  2744. @implementation Foo
  2745. + (Bar *)create { return nil; }
  2746. - (void)run { [[Foo create] doIt]; }
  2747. @end
  2748. `
  2749. );
  2750. cg = await CodeGraph.init(tempDir, { index: true });
  2751. expect(callerNamesOf('Bar::doIt')).toEqual(['run']);
  2752. expect(callerNamesOf('Decoy::doIt')).toEqual([]);
  2753. });
  2754. it('resolves a chained message whose method is inherited from a superclass (via conformance)', async () => {
  2755. fs.writeFileSync(
  2756. path.join(tempDir, 'main.m'),
  2757. `@interface Base : NSObject
  2758. - (void)render;
  2759. @end
  2760. @implementation Base
  2761. - (void)render {}
  2762. @end
  2763. @interface Widget : Base
  2764. @end
  2765. @implementation Widget
  2766. @end
  2767. @interface Decoy : NSObject
  2768. - (void)render;
  2769. @end
  2770. @implementation Decoy
  2771. - (void)render {}
  2772. @end
  2773. @interface Factory : NSObject
  2774. + (Widget *)make;
  2775. @end
  2776. @implementation Factory
  2777. + (Widget *)make { return nil; }
  2778. - (void)run { [[Factory make] render]; }
  2779. @end
  2780. `
  2781. );
  2782. cg = await CodeGraph.init(tempDir, { index: true });
  2783. expect(callerNamesOf('Base::render')).toEqual(['run']);
  2784. expect(callerNamesOf('Decoy::render')).toEqual([]);
  2785. });
  2786. it('creates NO edge when the factory return type lacks the method (silent miss)', async () => {
  2787. fs.writeFileSync(
  2788. path.join(tempDir, 'main.m'),
  2789. `@interface Bar : NSObject
  2790. @end
  2791. @implementation Bar
  2792. @end
  2793. @interface Other : NSObject
  2794. - (void)onlyOther;
  2795. @end
  2796. @implementation Other
  2797. - (void)onlyOther {}
  2798. @end
  2799. @interface Foo : NSObject
  2800. + (Bar *)create;
  2801. @end
  2802. @implementation Foo
  2803. + (Bar *)create { return nil; }
  2804. - (void)run { [[Foo create] onlyOther]; }
  2805. @end
  2806. `
  2807. );
  2808. cg = await CodeGraph.init(tempDir, { index: true });
  2809. // Bar has no onlyOther — must not mis-attach to the same-named Other::onlyOther.
  2810. expect(callerNamesOf('Other::onlyOther')).toEqual([]);
  2811. });
  2812. it('resolves a singleton chain [[Cache shared] clearAll] whose factory returns nonnull instancetype', async () => {
  2813. // The factory returns `nonnull instancetype` — the nullability qualifier must
  2814. // be skipped (not captured AS the type), and an instancetype class-message
  2815. // factory returns the receiver class, so clearAll resolves on Cache, never a
  2816. // same-named decoy. (Regression for both: the captured-`nonnull` bug and the
  2817. // ubiquitous `[[X alloc] init]` / singleton pattern.)
  2818. fs.writeFileSync(
  2819. path.join(tempDir, 'main.m'),
  2820. `@interface Cache : NSObject
  2821. + (nonnull instancetype)shared;
  2822. - (void)clearAll;
  2823. @end
  2824. @implementation Cache
  2825. + (nonnull instancetype)shared { return nil; }
  2826. - (void)clearAll {}
  2827. @end
  2828. @interface Decoy : NSObject
  2829. - (void)clearAll;
  2830. @end
  2831. @implementation Decoy
  2832. - (void)clearAll {}
  2833. @end
  2834. @interface Caller : NSObject
  2835. - (void)run;
  2836. @end
  2837. @implementation Caller
  2838. - (void)run { [[Cache shared] clearAll]; }
  2839. @end
  2840. `
  2841. );
  2842. cg = await CodeGraph.init(tempDir, { index: true });
  2843. expect(callerNamesOf('Cache::clearAll')).toEqual(['run']);
  2844. expect(callerNamesOf('Decoy::clearAll')).toEqual([]);
  2845. });
  2846. });
  2847. describe('Pascal/Delphi chained static-factory call resolution (#645/#608 mechanism)', () => {
  2848. function callerNamesOf(qualifiedName: string): string[] {
  2849. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  2850. if (!target) return [];
  2851. const names = cg
  2852. .getIncomingEdges(target.id)
  2853. .filter((e) => e.kind === 'calls')
  2854. .map((e) => cg.getNode(e.source)?.name)
  2855. .filter((n): n is string => !!n);
  2856. return [...new Set(names)].sort();
  2857. }
  2858. function isCalled(qn: string): boolean {
  2859. const t = cg.getNodesByKind('method').find((n) => n.qualifiedName === qn);
  2860. return !!t && cg.getIncomingEdges(t.id).some((e) => e.kind === 'calls');
  2861. }
  2862. it('resolves a chained factory call TFoo.GetInstance().DoIt() via the return type, never a same-named decoy', async () => {
  2863. fs.writeFileSync(
  2864. path.join(tempDir, 'main.pas'),
  2865. `unit Main;
  2866. interface
  2867. type
  2868. TBar = class
  2869. procedure DoIt;
  2870. end;
  2871. TDecoy = class
  2872. procedure DoIt;
  2873. end;
  2874. TFoo = class
  2875. class function GetInstance: TBar;
  2876. end;
  2877. implementation
  2878. procedure TBar.DoIt; begin end;
  2879. procedure TDecoy.DoIt; begin end;
  2880. class function TFoo.GetInstance: TBar; begin Result := nil; end;
  2881. procedure Run;
  2882. begin
  2883. TFoo.GetInstance().DoIt();
  2884. end;
  2885. end.
  2886. `
  2887. );
  2888. cg = await CodeGraph.init(tempDir, { index: true });
  2889. expect(isCalled('TBar::DoIt')).toBe(true);
  2890. expect(isCalled('TDecoy::DoIt')).toBe(false);
  2891. });
  2892. it('resolves a constructor chain TFoo.Create().Configure() on the constructed class', async () => {
  2893. fs.writeFileSync(
  2894. path.join(tempDir, 'main.pas'),
  2895. `unit Main;
  2896. interface
  2897. type
  2898. TFoo = class
  2899. constructor Create;
  2900. procedure Configure;
  2901. end;
  2902. TDecoy = class
  2903. procedure Configure;
  2904. end;
  2905. implementation
  2906. constructor TFoo.Create; begin end;
  2907. procedure TFoo.Configure; begin end;
  2908. procedure TDecoy.Configure; begin end;
  2909. procedure Run;
  2910. begin
  2911. TFoo.Create().Configure();
  2912. end;
  2913. end.
  2914. `
  2915. );
  2916. cg = await CodeGraph.init(tempDir, { index: true });
  2917. // A constructor returns its own class (no `: TBar` annotation), so Configure
  2918. // resolves on TFoo, not the same-named decoy.
  2919. expect(isCalled('TFoo::Configure')).toBe(true);
  2920. expect(isCalled('TDecoy::Configure')).toBe(false);
  2921. });
  2922. it('resolves a typecast chain TFoo(x).DoIt() on the cast type', async () => {
  2923. fs.writeFileSync(
  2924. path.join(tempDir, 'main.pas'),
  2925. `unit Main;
  2926. interface
  2927. type
  2928. TFoo = class
  2929. procedure DoIt;
  2930. end;
  2931. TDecoy = class
  2932. procedure DoIt;
  2933. end;
  2934. implementation
  2935. procedure TFoo.DoIt; begin end;
  2936. procedure TDecoy.DoIt; begin end;
  2937. procedure Run(obj: TObject);
  2938. begin
  2939. TFoo(obj).DoIt();
  2940. end;
  2941. end.
  2942. `
  2943. );
  2944. cg = await CodeGraph.init(tempDir, { index: true });
  2945. expect(isCalled('TFoo::DoIt')).toBe(true);
  2946. expect(isCalled('TDecoy::DoIt')).toBe(false);
  2947. });
  2948. it('creates NO edge when the factory return type lacks the method (silent miss)', async () => {
  2949. fs.writeFileSync(
  2950. path.join(tempDir, 'main.pas'),
  2951. `unit Main;
  2952. interface
  2953. type
  2954. TBar = class
  2955. end;
  2956. TOther = class
  2957. procedure OnlyOther;
  2958. end;
  2959. TFoo = class
  2960. class function GetInstance: TBar;
  2961. end;
  2962. implementation
  2963. procedure TOther.OnlyOther; begin end;
  2964. class function TFoo.GetInstance: TBar; begin Result := nil; end;
  2965. procedure Run;
  2966. begin
  2967. TFoo.GetInstance().OnlyOther();
  2968. end;
  2969. end.
  2970. `
  2971. );
  2972. cg = await CodeGraph.init(tempDir, { index: true });
  2973. // TBar has no OnlyOther — must not mis-attach to the same-named TOther::OnlyOther.
  2974. expect(isCalled('TOther::OnlyOther')).toBe(false);
  2975. });
  2976. });
  2977. });