resolution.test.ts 122 KB

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