resolution.test.ts 167 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312
  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, resolveMethodOnType, matchByQualifiedName, preferCallSiteFile, matchMethodCall } 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('Ubiquitous-name ceiling (#999)', () => {
  247. // A vendored theme/SDK re-declares the same method name across thousands of
  248. // files (Metronic's `init`/`update`/… on every widget). The fuzzy strategies
  249. // used to score every same-named candidate per ref — O(K) per ref, O(K²)
  250. // total — which pinned a core for 15-28 min at "Resolving refs … 94%". Above
  251. // the ceiling they must DECLINE instead, since no proximity/word-overlap
  252. // score can pick the one true target among thousands anyway.
  253. const CEILING = 500;
  254. // A spy context: counts how many nodes the strategy actually inspects, so we
  255. // can assert the cap short-circuits BEFORE the O(K) scoring (not just that it
  256. // returns null).
  257. const makeManyMethods = (n: number, name: string): Node[] =>
  258. Array.from({ length: n }, (_, i) => ({
  259. id: `method:widget${i}.js:Widget${i}.${name}:1`,
  260. kind: 'method' as const,
  261. name,
  262. qualifiedName: `widget${i}.js::Widget${i}::${name}`,
  263. filePath: `static/theme/widget${i}.js`,
  264. language: 'javascript' as const,
  265. startLine: 1,
  266. endLine: 5,
  267. startColumn: 0,
  268. endColumn: 0,
  269. updatedAt: Date.now(),
  270. }));
  271. const spyContext = (nodes: Node[]): { ctx: ResolutionContext; lookups: () => number } => {
  272. let scanned = 0;
  273. const ctx: ResolutionContext = {
  274. getNodesInFile: () => [],
  275. getNodesByName: (name) => {
  276. const hit = nodes.filter((n) => n.name === name);
  277. scanned += hit.length;
  278. return hit;
  279. },
  280. getNodesByQualifiedName: () => [],
  281. getNodesByKind: () => [],
  282. fileExists: () => true,
  283. readFile: () => null,
  284. getProjectRoot: () => '/test',
  285. getAllFiles: () => [],
  286. getNodesByLowerName: () => [],
  287. getImportMappings: () => [],
  288. };
  289. return { ctx, lookups: () => scanned };
  290. };
  291. it('declines a method call (`obj.init`) above the ceiling instead of scoring K candidates', () => {
  292. const { ctx } = spyContext(makeManyMethods(CEILING + 1, 'init'));
  293. const ref = {
  294. fromNodeId: 'method:caller.js:caller:1',
  295. referenceName: 'widget.init',
  296. referenceKind: 'calls' as const,
  297. line: 2,
  298. column: 4,
  299. filePath: 'static/theme/caller.js',
  300. language: 'javascript' as const,
  301. };
  302. expect(matchReference(ref, ctx)).toBeNull();
  303. });
  304. it('declines a bare exact-name ref above the ceiling', () => {
  305. const { ctx } = spyContext(makeManyMethods(CEILING + 1, 'render'));
  306. const ref = {
  307. fromNodeId: 'method:caller.js:caller:1',
  308. referenceName: 'render',
  309. referenceKind: 'calls' as const,
  310. line: 2,
  311. column: 4,
  312. filePath: 'static/theme/caller.js',
  313. language: 'javascript' as const,
  314. };
  315. expect(matchReference(ref, ctx)).toBeNull();
  316. });
  317. it('still resolves a SAME-FILE definition when one exists (precise path unaffected)', () => {
  318. // Strategy 1 (class-name) and same-file matching are precise — a ubiquitous
  319. // name with an unambiguous local target still resolves.
  320. const nodes = makeManyMethods(CEILING + 1, 'init');
  321. const local: Node = {
  322. id: 'class:static/theme/caller.js:Widgetly:1',
  323. kind: 'class',
  324. name: 'Widgetly',
  325. qualifiedName: 'static/theme/caller.js::Widgetly',
  326. filePath: 'static/theme/caller.js',
  327. language: 'javascript',
  328. startLine: 1, endLine: 9, startColumn: 0, endColumn: 0, updatedAt: Date.now(),
  329. };
  330. const localMethod: Node = {
  331. id: 'method:static/theme/caller.js:Widgetly.init:2',
  332. kind: 'method',
  333. name: 'init',
  334. qualifiedName: 'static/theme/caller.js::Widgetly::init',
  335. filePath: 'static/theme/caller.js',
  336. language: 'javascript',
  337. startLine: 2, endLine: 4, startColumn: 0, endColumn: 0, updatedAt: Date.now(),
  338. };
  339. const all = [...nodes, local, localMethod];
  340. const ctx: ResolutionContext = {
  341. getNodesInFile: (fp) => all.filter((n) => n.filePath === fp),
  342. getNodesByName: (name) => all.filter((n) => n.name === name),
  343. getNodesByQualifiedName: () => [],
  344. getNodesByKind: () => [],
  345. fileExists: () => true,
  346. readFile: () => null,
  347. getProjectRoot: () => '/test',
  348. getAllFiles: () => [],
  349. getNodesByLowerName: () => [],
  350. getImportMappings: () => [],
  351. };
  352. // `Widgetly.init` names the class explicitly → Strategy 1 resolves it.
  353. const ref = {
  354. fromNodeId: 'method:static/theme/caller.js:caller:6',
  355. referenceName: 'Widgetly.init',
  356. referenceKind: 'calls' as const,
  357. line: 6,
  358. column: 4,
  359. filePath: 'static/theme/caller.js',
  360. language: 'javascript' as const,
  361. };
  362. const result = matchReference(ref, ctx);
  363. expect(result?.targetNodeId).toBe('method:static/theme/caller.js:Widgetly.init:2');
  364. });
  365. it('still scores normally JUST below the ceiling (no behavior change for normal repos)', () => {
  366. // Real repos top out near ~40 same-named methods; this proves a sub-ceiling
  367. // collision still resolves via proximity, so the cap is invisible to them.
  368. const nodes = makeManyMethods(CEILING - 1, 'update');
  369. // Make ONE candidate share the caller's directory so proximity picks it.
  370. nodes[0] = {
  371. ...nodes[0]!,
  372. id: 'method:static/theme/app/Widget0.update:1',
  373. qualifiedName: 'static/theme/app/widget.js::Widget0::update',
  374. filePath: 'static/theme/app/widget.js',
  375. };
  376. const ctx: ResolutionContext = {
  377. getNodesInFile: () => [],
  378. getNodesByName: (name) => nodes.filter((n) => n.name === name),
  379. getNodesByQualifiedName: () => [],
  380. getNodesByKind: () => [],
  381. fileExists: () => true,
  382. readFile: () => null,
  383. getProjectRoot: () => '/test',
  384. getAllFiles: () => [],
  385. getNodesByLowerName: () => [],
  386. getImportMappings: () => [],
  387. };
  388. const ref = {
  389. fromNodeId: 'method:static/theme/app/caller.js:caller:1',
  390. referenceName: 'update',
  391. referenceKind: 'calls' as const,
  392. line: 2,
  393. column: 4,
  394. filePath: 'static/theme/app/caller.js',
  395. language: 'javascript' as const,
  396. };
  397. // Below the ceiling the fuzzy path runs and resolves SOMETHING (not capped).
  398. expect(matchReference(ref, ctx)).not.toBeNull();
  399. });
  400. });
  401. describe('Import Resolver', () => {
  402. it('should resolve relative import paths', () => {
  403. const context: ResolutionContext = {
  404. getNodesInFile: () => [],
  405. getNodesByName: () => [],
  406. getNodesByQualifiedName: () => [],
  407. getNodesByKind: () => [],
  408. fileExists: (p) => p === 'src/components/utils.ts' || p === 'src/components/utils/index.ts',
  409. readFile: () => null,
  410. getProjectRoot: () => '',
  411. getAllFiles: () => ['src/components/utils.ts', 'src/components/utils/index.ts'],
  412. };
  413. const result = resolveImportPath(
  414. './utils',
  415. 'src/components/Button.ts',
  416. 'typescript',
  417. context
  418. );
  419. expect(result).toBe('src/components/utils.ts');
  420. });
  421. it('should resolve parent directory imports', () => {
  422. const context: ResolutionContext = {
  423. getNodesInFile: () => [],
  424. getNodesByName: () => [],
  425. getNodesByQualifiedName: () => [],
  426. getNodesByKind: () => [],
  427. fileExists: (p) => p === 'src/helpers.ts' || p === 'src/helpers/index.ts',
  428. readFile: () => null,
  429. getProjectRoot: () => '',
  430. getAllFiles: () => ['src/helpers.ts', 'src/helpers/index.ts'],
  431. };
  432. const result = resolveImportPath(
  433. '../helpers',
  434. 'src/components/Button.ts',
  435. 'typescript',
  436. context
  437. );
  438. expect(result).toBe('src/helpers.ts');
  439. });
  440. it('should extract JS/TS import mappings', () => {
  441. const content = `
  442. import { foo } from './foo';
  443. import bar from '../bar';
  444. import * as utils from './utils';
  445. import { baz, qux } from './baz';
  446. `;
  447. const mappings = extractImportMappings(
  448. 'src/index.ts',
  449. content,
  450. 'typescript'
  451. );
  452. expect(mappings.length).toBeGreaterThan(0);
  453. expect(mappings.some((m) => m.localName === 'foo')).toBe(true);
  454. expect(mappings.some((m) => m.localName === 'bar')).toBe(true);
  455. });
  456. it('should extract Python import mappings', () => {
  457. const content = `
  458. from utils import helper
  459. from .models import User
  460. import os
  461. from ..services import auth_service
  462. `;
  463. const mappings = extractImportMappings(
  464. 'src/main.py',
  465. content,
  466. 'python'
  467. );
  468. expect(mappings.length).toBeGreaterThan(0);
  469. expect(mappings.some((m) => m.localName === 'helper')).toBe(true);
  470. expect(mappings.some((m) => m.localName === 'User')).toBe(true);
  471. });
  472. });
  473. describe('JVM FQN Import Resolution', () => {
  474. // Build a ResolutionContext stub whose getNodesByQualifiedName answers
  475. // from a fixed table — the only context method resolveJvmImport touches.
  476. const makeContext = (byQName: Record<string, Node[]>): ResolutionContext => ({
  477. getNodesInFile: () => [],
  478. getNodesByName: () => [],
  479. getNodesByQualifiedName: (q) => byQName[q] ?? [],
  480. getNodesByKind: () => [],
  481. fileExists: () => false,
  482. readFile: () => null,
  483. getProjectRoot: () => '',
  484. getAllFiles: () => [],
  485. });
  486. const node = (id: string, name: string, qualifiedName: string, kind: Node['kind'] = 'class', language: Node['language'] = 'kotlin'): Node => ({
  487. id, kind, name, qualifiedName,
  488. filePath: 'Models.kt', language,
  489. startLine: 1, endLine: 1, startColumn: 0, endColumn: 0,
  490. updatedAt: 0,
  491. });
  492. const importRef = (referenceName: string, language: Node['language'] = 'kotlin'): UnresolvedRef => ({
  493. fromNodeId: 'caller',
  494. referenceName,
  495. referenceKind: 'imports',
  496. line: 1, column: 0,
  497. filePath: 'Caller.kt',
  498. language,
  499. });
  500. it('resolves a Kotlin class import by FQN regardless of filename', () => {
  501. const target = node('n1', 'Bar', 'com.example.foo::Bar');
  502. const ctx = makeContext({ 'com.example.foo::Bar': [target] });
  503. const result = resolveJvmImport(importRef('com.example.foo.Bar'), ctx);
  504. expect(result?.targetNodeId).toBe('n1');
  505. expect(result?.resolvedBy).toBe('import');
  506. });
  507. it('resolves a Kotlin top-level function import by FQN', () => {
  508. const util = node('n2', 'util', 'com.example.foo::util', 'function');
  509. const ctx = makeContext({ 'com.example.foo::util': [util] });
  510. const result = resolveJvmImport(importRef('com.example.foo.util'), ctx);
  511. expect(result?.targetNodeId).toBe('n2');
  512. });
  513. it('resolves a Java import by FQN', () => {
  514. const target = node('n3', 'Bar', 'com.example.foo::Bar', 'class', 'java');
  515. const ctx = makeContext({ 'com.example.foo::Bar': [target] });
  516. const result = resolveJvmImport(importRef('com.example.foo.Bar', 'java'), ctx);
  517. expect(result?.targetNodeId).toBe('n3');
  518. });
  519. it('resolves cross-language: Kotlin importing a Java class', () => {
  520. // The Kotlin file declares `import com.example.JavaBar` — the target is
  521. // a Java class node. JVM interop means the resolver doesn't care about
  522. // the source language of the target, only that the FQN matches.
  523. const target = node('n4', 'JavaBar', 'com.example::JavaBar', 'class', 'java');
  524. const ctx = makeContext({ 'com.example::JavaBar': [target] });
  525. const result = resolveJvmImport(importRef('com.example.JavaBar'), ctx);
  526. expect(result?.targetNodeId).toBe('n4');
  527. });
  528. it('disambiguates a name collision across packages', () => {
  529. // Two classes named `Bar` in different packages. Each import resolves
  530. // to the one whose FQN matches — not to "whichever was found first".
  531. const barA = node('n5a', 'Bar', 'com.example.alpha::Bar');
  532. const barB = node('n5b', 'Bar', 'com.example.beta::Bar');
  533. const ctx = makeContext({
  534. 'com.example.alpha::Bar': [barA],
  535. 'com.example.beta::Bar': [barB],
  536. });
  537. expect(resolveJvmImport(importRef('com.example.alpha.Bar'), ctx)?.targetNodeId).toBe('n5a');
  538. expect(resolveJvmImport(importRef('com.example.beta.Bar'), ctx)?.targetNodeId).toBe('n5b');
  539. });
  540. it('returns null for wildcard imports', () => {
  541. const ctx = makeContext({});
  542. expect(resolveJvmImport(importRef('com.example.foo.*'), ctx)).toBeNull();
  543. });
  544. it('returns null for unqualified names', () => {
  545. // A single-segment name has no package; nothing to look up by FQN.
  546. const ctx = makeContext({ 'Bar': [node('n6', 'Bar', 'Bar')] });
  547. expect(resolveJvmImport(importRef('Bar'), ctx)).toBeNull();
  548. });
  549. it('returns null for non-JVM languages', () => {
  550. const target = node('n7', 'Bar', 'com.example::Bar');
  551. const ctx = makeContext({ 'com.example::Bar': [target] });
  552. expect(resolveJvmImport(importRef('com.example.Bar', 'typescript'), ctx)).toBeNull();
  553. });
  554. it('returns null for non-imports reference kinds', () => {
  555. // The resolver intentionally only acts on `imports` refs; ordinary
  556. // `calls`/`extends` refs fall through to the framework + name-matcher
  557. // strategies.
  558. const target = node('n8', 'Bar', 'com.example::Bar');
  559. const ctx = makeContext({ 'com.example::Bar': [target] });
  560. const ref: UnresolvedRef = {
  561. fromNodeId: 'caller', referenceName: 'com.example.Bar',
  562. referenceKind: 'calls', line: 1, column: 0,
  563. filePath: 'Caller.kt', language: 'kotlin',
  564. };
  565. expect(resolveJvmImport(ref, ctx)).toBeNull();
  566. });
  567. it('returns null when the FQN is not in the index', () => {
  568. const ctx = makeContext({});
  569. expect(resolveJvmImport(importRef('com.example.Unknown'), ctx)).toBeNull();
  570. });
  571. });
  572. describe('Framework Detection', () => {
  573. it('should detect React framework', () => {
  574. const context: ResolutionContext = {
  575. getNodesInFile: () => [],
  576. getNodesByName: () => [],
  577. getNodesByQualifiedName: () => [],
  578. getNodesByKind: () => [],
  579. fileExists: () => false,
  580. readFile: (p) => {
  581. if (p === 'package.json') {
  582. return JSON.stringify({
  583. dependencies: { react: '^18.0.0' },
  584. });
  585. }
  586. return null;
  587. },
  588. getProjectRoot: () => '/test',
  589. getAllFiles: () => ['package.json', 'src/App.tsx'],
  590. };
  591. const frameworks = detectFrameworks(context);
  592. expect(frameworks.some((f) => f.name === 'react')).toBe(true);
  593. });
  594. it('should detect Express framework', () => {
  595. const context: ResolutionContext = {
  596. getNodesInFile: () => [],
  597. getNodesByName: () => [],
  598. getNodesByQualifiedName: () => [],
  599. getNodesByKind: () => [],
  600. fileExists: () => false,
  601. readFile: (p) => {
  602. if (p === 'package.json') {
  603. return JSON.stringify({
  604. dependencies: { express: '^4.18.0' },
  605. });
  606. }
  607. return null;
  608. },
  609. getProjectRoot: () => '/test',
  610. getAllFiles: () => ['package.json', 'src/app.js'],
  611. };
  612. const frameworks = detectFrameworks(context);
  613. expect(frameworks.some((f) => f.name === 'express')).toBe(true);
  614. });
  615. it('should detect Laravel framework', () => {
  616. const context: ResolutionContext = {
  617. getNodesInFile: () => [],
  618. getNodesByName: () => [],
  619. getNodesByQualifiedName: () => [],
  620. getNodesByKind: () => [],
  621. fileExists: (p) => p === 'artisan',
  622. readFile: () => null,
  623. getProjectRoot: () => '/test',
  624. getAllFiles: () => ['artisan', 'app/Http/Kernel.php'],
  625. };
  626. const frameworks = detectFrameworks(context);
  627. expect(frameworks.some((f) => f.name === 'laravel')).toBe(true);
  628. });
  629. it('should return all framework resolvers', () => {
  630. const resolvers = getAllFrameworkResolvers();
  631. expect(resolvers.length).toBeGreaterThan(0);
  632. expect(resolvers.some((r) => r.name === 'react')).toBe(true);
  633. expect(resolvers.some((r) => r.name === 'express')).toBe(true);
  634. expect(resolvers.some((r) => r.name === 'laravel')).toBe(true);
  635. });
  636. });
  637. describe('React Framework Resolver', () => {
  638. it('should resolve React component references', () => {
  639. const mockNodes: Node[] = [
  640. {
  641. id: 'component:src/Button.tsx:Button:5',
  642. kind: 'component',
  643. name: 'Button',
  644. qualifiedName: 'src/Button.tsx::Button',
  645. filePath: 'src/Button.tsx',
  646. language: 'tsx',
  647. startLine: 5,
  648. endLine: 20,
  649. startColumn: 0,
  650. endColumn: 0,
  651. updatedAt: Date.now(),
  652. },
  653. ];
  654. const context: ResolutionContext = {
  655. getNodesInFile: (fp) => (fp === 'src/Button.tsx' ? mockNodes : []),
  656. getNodesByName: () => mockNodes,
  657. getNodesByQualifiedName: () => [],
  658. getNodesByKind: () => [],
  659. fileExists: () => false,
  660. readFile: (p) => {
  661. if (p === 'package.json') {
  662. return JSON.stringify({ dependencies: { react: '^18.0.0' } });
  663. }
  664. return null;
  665. },
  666. getProjectRoot: () => '/test',
  667. getAllFiles: () => ['package.json', 'src/Button.tsx', 'src/App.tsx'],
  668. };
  669. const frameworks = detectFrameworks(context);
  670. const reactResolver = frameworks.find((f) => f.name === 'react');
  671. expect(reactResolver).toBeDefined();
  672. const ref = {
  673. fromNodeId: 'component:src/App.tsx:App:1',
  674. referenceName: 'Button',
  675. referenceKind: 'renders' as const,
  676. line: 10,
  677. column: 5,
  678. filePath: 'src/App.tsx',
  679. // Refs extracted from .tsx files carry language 'tsx' — component
  680. // resolution is gated to JSX-capable refs (#764: PascalCase TYPE refs
  681. // from plain .ts files were resolving to arbitrary same-named classes).
  682. language: 'tsx' as const,
  683. };
  684. const result = reactResolver!.resolve(ref, context);
  685. expect(result).not.toBeNull();
  686. expect(result?.targetNodeId).toBe('component:src/Button.tsx:Button:5');
  687. // The same PascalCase name referenced from a plain .ts file is a TYPE
  688. // reference, not a component usage — component resolution must decline
  689. // and leave it to proximity-aware name matching (#764: a .ts GraphQL
  690. // types file's own `Account` alias was losing to an arbitrary same-named
  691. // class in another monorepo package).
  692. const tsRef = { ...ref, filePath: 'src/models.ts', language: 'typescript' as const };
  693. expect(reactResolver!.resolve(tsRef, context)).toBeNull();
  694. });
  695. it('should resolve custom hook references', () => {
  696. const mockNodes: Node[] = [
  697. {
  698. id: 'hook:src/hooks/useAuth.ts:useAuth:1',
  699. kind: 'function',
  700. name: 'useAuth',
  701. qualifiedName: 'src/hooks/useAuth.ts::useAuth',
  702. filePath: 'src/hooks/useAuth.ts',
  703. language: 'typescript',
  704. startLine: 1,
  705. endLine: 20,
  706. startColumn: 0,
  707. endColumn: 0,
  708. updatedAt: Date.now(),
  709. },
  710. ];
  711. const context: ResolutionContext = {
  712. getNodesInFile: (fp) => (fp.includes('useAuth') ? mockNodes : []),
  713. getNodesByName: () => mockNodes,
  714. getNodesByQualifiedName: () => [],
  715. getNodesByKind: () => [],
  716. fileExists: () => false,
  717. readFile: (p) => {
  718. if (p === 'package.json') {
  719. return JSON.stringify({ dependencies: { react: '^18.0.0' } });
  720. }
  721. return null;
  722. },
  723. getProjectRoot: () => '/test',
  724. getAllFiles: () => ['package.json', 'src/hooks/useAuth.ts'],
  725. };
  726. const frameworks = detectFrameworks(context);
  727. const reactResolver = frameworks.find((f) => f.name === 'react');
  728. const ref = {
  729. fromNodeId: 'component:src/App.tsx:App:1',
  730. referenceName: 'useAuth',
  731. referenceKind: 'calls' as const,
  732. line: 5,
  733. column: 10,
  734. filePath: 'src/App.tsx',
  735. language: 'typescript' as const,
  736. };
  737. const result = reactResolver!.resolve(ref, context);
  738. expect(result).not.toBeNull();
  739. expect(result?.targetNodeId).toBe('hook:src/hooks/useAuth.ts:useAuth:1');
  740. });
  741. });
  742. describe('Integration Tests', () => {
  743. it('should create resolver from CodeGraph instance', async () => {
  744. // Create a simple TypeScript project
  745. fs.writeFileSync(
  746. path.join(tempDir, 'package.json'),
  747. JSON.stringify({ name: 'test', dependencies: { react: '^18.0.0' } })
  748. );
  749. const srcDir = path.join(tempDir, 'src');
  750. fs.mkdirSync(srcDir);
  751. // Create utility file
  752. fs.writeFileSync(
  753. path.join(srcDir, 'utils.ts'),
  754. `export function formatDate(date: Date): string {
  755. return date.toISOString();
  756. }
  757. export function parseDate(str: string): Date {
  758. return new Date(str);
  759. }`
  760. );
  761. // Create main file that uses utils
  762. fs.writeFileSync(
  763. path.join(srcDir, 'main.ts'),
  764. `import { formatDate, parseDate } from './utils';
  765. function processDate(input: string): string {
  766. const date = parseDate(input);
  767. return formatDate(date);
  768. }`
  769. );
  770. // Initialize and index
  771. cg = await CodeGraph.init(tempDir, { index: true });
  772. // Check that resolver detected React framework
  773. const frameworks = cg.getDetectedFrameworks();
  774. expect(frameworks).toContain('react');
  775. // Get stats to verify indexing worked
  776. const stats = cg.getStats();
  777. expect(stats.fileCount).toBe(2);
  778. expect(stats.nodeCount).toBeGreaterThan(0);
  779. });
  780. it('should resolve references after indexing', async () => {
  781. // Create a project with references
  782. const srcDir = path.join(tempDir, 'src');
  783. fs.mkdirSync(srcDir, { recursive: true });
  784. fs.writeFileSync(
  785. path.join(srcDir, 'helper.ts'),
  786. `export function helperFunction(): void {
  787. console.log('helper');
  788. }`
  789. );
  790. fs.writeFileSync(
  791. path.join(srcDir, 'main.ts'),
  792. `import { helperFunction } from './helper';
  793. function main(): void {
  794. helperFunction();
  795. }`
  796. );
  797. cg = await CodeGraph.init(tempDir, { index: true });
  798. // Run reference resolution
  799. const result = cg.resolveReferences();
  800. // Should have attempted resolution
  801. expect(result.stats.total).toBeGreaterThanOrEqual(0);
  802. });
  803. it('promotes calls→instantiates when target resolves to a class (Python)', async () => {
  804. // Python has no `new` keyword — `Foo()` is the standard
  805. // instantiation syntax. Extraction can't tell that apart from
  806. // a function call without symbol info, so it emits a `calls`
  807. // ref. Resolution promotes it to `instantiates` once the
  808. // target is known to be a class.
  809. const srcDir = path.join(tempDir, 'src');
  810. fs.mkdirSync(srcDir, { recursive: true });
  811. fs.writeFileSync(
  812. path.join(srcDir, 'app.py'),
  813. `class UserService:
  814. def __init__(self):
  815. self.db = None
  816. def bootstrap():
  817. return UserService()
  818. `
  819. );
  820. cg = await CodeGraph.init(tempDir, { index: true });
  821. cg.resolveReferences();
  822. const bootstrap = cg
  823. .getNodesByKind('function')
  824. .find((n) => n.name === 'bootstrap');
  825. expect(bootstrap).toBeDefined();
  826. const outgoing = cg.getOutgoingEdges(bootstrap!.id);
  827. const instantiates = outgoing.find((e) => e.kind === 'instantiates');
  828. expect(instantiates).toBeDefined();
  829. // Same edge must NOT also appear as a `calls` edge — promotion
  830. // replaces the kind, doesn't duplicate.
  831. const callsToUserService = outgoing.filter(
  832. (e) => e.kind === 'calls' && e.target === instantiates!.target
  833. );
  834. expect(callsToUserService).toHaveLength(0);
  835. });
  836. it('records instantiates for C++ stack/brace construction, targeting the class (#1035)', async () => {
  837. // `Calculator calc(0)` (direct-init) and `Widget w{1, 2}` (brace-init)
  838. // carry the constructor args directly on the declarator — there's no
  839. // call/new node — so they recorded no `instantiates` edge, while heap
  840. // `new Calculator(0)` did. Both stack forms now do.
  841. fs.writeFileSync(
  842. path.join(tempDir, 'm.cpp'),
  843. `class Calculator { public: Calculator(int seed) {} int add(int a, int b){ return a+b; } };
  844. class Widget { public: Widget(int a, int b) {} };
  845. int runStack(int a, int b) { Calculator calc(0); return calc.add(a, b); }
  846. int runBrace() { Widget w{1, 2}; return 0; }
  847. int runHeap(int a, int b) { Calculator* c = new Calculator(0); return c->add(a, b); }
  848. void noise() { int x(5); int y{6}; Calculator deferred; }
  849. `
  850. );
  851. cg = await CodeGraph.init(tempDir, { index: true });
  852. const fn = (name: string) => cg.getNodesByKind('function').find((n) => n.name === name)!;
  853. const instTargets = (name: string) =>
  854. cg
  855. .getOutgoingEdges(fn(name).id)
  856. .filter((e) => e.kind === 'instantiates')
  857. .map((e) => cg.getNode(e.target)!);
  858. // Direct-init (the issue) and brace-init both instantiate, targeting the
  859. // CLASS node — not the same-named constructor method.
  860. const stack = instTargets('runStack');
  861. expect(stack.map((n) => `${n.kind}:${n.name}`)).toContain('class:Calculator');
  862. expect(instTargets('runBrace').map((n) => `${n.kind}:${n.name}`)).toContain('class:Widget');
  863. // Heap still works (regression guard).
  864. expect(instTargets('runHeap').map((n) => `${n.kind}:${n.name}`)).toContain('class:Calculator');
  865. // Primitives (`int x(0)`/`int y{6}`) and bare default construction
  866. // (`Calculator deferred;`) must NOT mint an instantiates edge.
  867. expect(instTargets('noise')).toHaveLength(0);
  868. });
  869. it('resolves a cross-file static method call to the method, not the class (#825)', async () => {
  870. // `Foo.bar()` where `Foo` is an imported class must link to the static
  871. // method `Foo::bar`, NOT to the class `Foo`. Previously the import
  872. // resolver dropped the `.bar` member and resolved to `Foo`, which the
  873. // calls→instantiates promotion then turned into `run instantiates Foo`,
  874. // leaving the static method with zero callers and a hollow impact radius.
  875. fs.writeFileSync(
  876. path.join(tempDir, 'helpers.ts'),
  877. `export class Foo {\n static bar(x: number) { return x + 1; }\n}\n`
  878. );
  879. fs.writeFileSync(
  880. path.join(tempDir, 'caller.ts'),
  881. `import { Foo } from './helpers';\nexport function run() { return Foo.bar(41); }\n`
  882. );
  883. cg = await CodeGraph.init(tempDir, { index: true });
  884. cg.resolveReferences();
  885. const bar = cg.getNodesByKind('method').find((n) => n.name === 'bar');
  886. const foo = cg.getNodesByKind('class').find((n) => n.name === 'Foo');
  887. const run = cg.getNodesByKind('function').find((n) => n.name === 'run');
  888. expect(bar).toBeDefined();
  889. expect(foo).toBeDefined();
  890. expect(run).toBeDefined();
  891. // `run` is reported as a caller of the static method `Foo.bar`.
  892. const barCallers = cg.getCallers(bar!.id).map((c) => c.node.name);
  893. expect(barCallers).toContain('run');
  894. // And the call is NOT mis-promoted to `run instantiates Foo`.
  895. const outgoing = cg.getOutgoingEdges(run!.id);
  896. expect(
  897. outgoing.filter((e) => e.kind === 'instantiates' && e.target === foo!.id)
  898. ).toHaveLength(0);
  899. // The real edge is a `calls` edge to the method.
  900. expect(
  901. outgoing.some((e) => e.kind === 'calls' && e.target === bar!.id)
  902. ).toBe(true);
  903. });
  904. it('resolves Go cross-package qualified calls via go.mod module path (#388)', async () => {
  905. // Pre-#388, every `pkga.FuncX(...)` call in a Go monorepo was flagged
  906. // external (isExternalImport returned true for any non-`/internal/`
  907. // import without `.`-prefix) and resolution fell through to name-match
  908. // with path proximity — recall on cross-package callers was ~<1%.
  909. fs.writeFileSync(
  910. path.join(tempDir, 'go.mod'),
  911. 'module github.com/example/myproject\n\ngo 1.21\n'
  912. );
  913. const pkgaDir = path.join(tempDir, 'pkga');
  914. const pkgbDir = path.join(tempDir, 'pkgb');
  915. const pkgcDir = path.join(tempDir, 'pkgc');
  916. fs.mkdirSync(pkgaDir);
  917. fs.mkdirSync(pkgbDir);
  918. fs.mkdirSync(pkgcDir);
  919. // Same-name exported function in two packages — only the imported one
  920. // should resolve. Exercises disambiguation, not just connectivity.
  921. fs.writeFileSync(
  922. path.join(pkgaDir, 'conv.go'),
  923. 'package pkga\nfunc Convert(x int) int { return x * 2 }\n'
  924. );
  925. fs.writeFileSync(
  926. path.join(pkgbDir, 'conv.go'),
  927. 'package pkgb\nfunc Convert(x int) int { return x + 1 }\n'
  928. );
  929. fs.writeFileSync(
  930. path.join(pkgcDir, 'use.go'),
  931. `package pkgc
  932. import "github.com/example/myproject/pkga"
  933. func UsePkga() {
  934. pkga.Convert(5)
  935. }
  936. `
  937. );
  938. cg = await CodeGraph.init(tempDir, { index: true });
  939. const usePkga = cg.getNodesByKind('function').filter((n) => n.name ==='UsePkga')[0];
  940. expect(usePkga).toBeDefined();
  941. const outgoing = cg.getOutgoingEdges(usePkga!.id);
  942. const callEdges = outgoing.filter((e) => e.kind === 'calls');
  943. expect(callEdges).toHaveLength(1);
  944. const target = cg.getNode(callEdges[0]!.target);
  945. expect(target?.name).toBe('Convert');
  946. // Critical: the resolver must pick the imported pkga's Convert,
  947. // not pkgb's. With the broken (pre-fix) resolver this lands on
  948. // whichever Convert happens to be cheaper under path proximity.
  949. expect(target?.filePath.replace(/\\/g, '/')).toBe('pkga/conv.go');
  950. });
  951. it('resolves Go aliased imports across packages (#388)', async () => {
  952. fs.writeFileSync(
  953. path.join(tempDir, 'go.mod'),
  954. 'module github.com/example/myproject\n\ngo 1.21\n'
  955. );
  956. fs.mkdirSync(path.join(tempDir, 'pkgb'));
  957. fs.mkdirSync(path.join(tempDir, 'pkgd'));
  958. fs.writeFileSync(
  959. path.join(tempDir, 'pkgb', 'lib.go'),
  960. 'package pkgb\nfunc Compute(x int) int { return x }\n'
  961. );
  962. fs.writeFileSync(
  963. path.join(tempDir, 'pkgd', 'use.go'),
  964. `package pkgd
  965. import (
  966. "fmt"
  967. alias "github.com/example/myproject/pkgb"
  968. )
  969. func UseAliased() {
  970. fmt.Println("hi")
  971. alias.Compute(3)
  972. }
  973. `
  974. );
  975. cg = await CodeGraph.init(tempDir, { index: true });
  976. const useAliased = cg.getNodesByKind('function').filter((n) => n.name ==='UseAliased')[0];
  977. expect(useAliased).toBeDefined();
  978. const calls = cg.getOutgoingEdges(useAliased!.id).filter((e) => e.kind === 'calls');
  979. // fmt.Println is stdlib — must stay external. alias.Compute must resolve.
  980. expect(calls).toHaveLength(1);
  981. const target = cg.getNode(calls[0]!.target);
  982. expect(target?.name).toBe('Compute');
  983. expect(target?.filePath.replace(/\\/g, '/')).toBe('pkgb/lib.go');
  984. });
  985. it('resolves Python module-attribute calls after `from pkg import module` (#578)', async () => {
  986. // Pre-#578, a `module.func()` call where `module` was bound via
  987. // `from pkg import module` dropped its `calls` edge. The file→file import
  988. // edge resolved (resolveModuleImportToFile falls back to a dotted-module
  989. // file lookup for absolute package paths), but resolvePythonModuleMember
  990. // had no such fallback — resolveImportPath returns null for an absolute
  991. // package path like `pkg.module`, so the member never resolved and
  992. // callers/callees/impact on the target came back empty. Same root-cause
  993. // class as the Go cross-package qualified call (#388).
  994. fs.mkdirSync(path.join(tempDir, 'pkg'));
  995. fs.writeFileSync(path.join(tempDir, 'pkg', '__init__.py'), '');
  996. fs.writeFileSync(
  997. path.join(tempDir, 'pkg', 'module.py'),
  998. 'def func():\n return 1\n'
  999. );
  1000. fs.writeFileSync(
  1001. path.join(tempDir, 'main.py'),
  1002. `from pkg import module
  1003. import os
  1004. def caller():
  1005. return module.func()
  1006. def external_caller():
  1007. return os.getcwd()
  1008. `
  1009. );
  1010. cg = await CodeGraph.init(tempDir, { index: true });
  1011. const caller = cg.getNodesByKind('function').filter((n) => n.name === 'caller')[0];
  1012. expect(caller).toBeDefined();
  1013. const calls = cg.getOutgoingEdges(caller!.id).filter((e) => e.kind === 'calls');
  1014. // module.func() must resolve to the real function in the submodule file.
  1015. expect(calls).toHaveLength(1);
  1016. const target = cg.getNode(calls[0]!.target);
  1017. expect(target?.name).toBe('func');
  1018. expect(target?.filePath.replace(/\\/g, '/')).toBe('pkg/module.py');
  1019. // The flip side of the fix: an attribute call through a *stdlib* module
  1020. // (`os.getcwd()`) must still create no edge — the fallback only matches
  1021. // real in-repo module files.
  1022. const externalCaller = cg.getNodesByKind('function').filter((n) => n.name === 'external_caller')[0];
  1023. expect(externalCaller).toBeDefined();
  1024. const externalCalls = cg.getOutgoingEdges(externalCaller!.id).filter((e) => e.kind === 'calls');
  1025. expect(externalCalls).toHaveLength(0);
  1026. });
  1027. it('attaches Go methods to their receiver type across files (#583, cross-file half)', async () => {
  1028. // In Go a type's methods are commonly declared in a different file from the
  1029. // `type` declaration (`type Box` in box.go, `func (b *Box) Get()` in
  1030. // box_methods.go). Extraction only attaches the struct→method `contains`
  1031. // edge when the type is in the SAME file (the owner lookup is file-scoped),
  1032. // so a cross-file method was orphaned from its struct — breaking member
  1033. // outlines and any callers/callees/impact traversal through `contains`. A
  1034. // resolution-phase pass now links them within the package (= directory).
  1035. fs.writeFileSync(
  1036. path.join(tempDir, 'box.go'),
  1037. 'package main\n\ntype Box struct{ v int }\n'
  1038. );
  1039. fs.writeFileSync(
  1040. path.join(tempDir, 'box_methods.go'),
  1041. 'package main\n\nfunc (b *Box) Get() int { return b.v }\nfunc (b *Box) Set(x int) { b.v = x }\n'
  1042. );
  1043. // Generic receiver declared cross-file too — exercises #583 half A
  1044. // (generic `*Stack[T]` receiver parsing) and half B (cross-file) together.
  1045. fs.writeFileSync(
  1046. path.join(tempDir, 'stack.go'),
  1047. 'package main\n\ntype Stack[T any] struct {\n\titems []T\n}\n'
  1048. );
  1049. fs.writeFileSync(
  1050. path.join(tempDir, 'stack_push.go'),
  1051. 'package main\n\nfunc (s *Stack[T]) Push(v T) { s.items = append(s.items, v) }\n'
  1052. );
  1053. // A same-named type in another package must NOT capture this package's
  1054. // methods — the link is scoped to the receiver type's own directory.
  1055. fs.mkdirSync(path.join(tempDir, 'other'));
  1056. fs.writeFileSync(
  1057. path.join(tempDir, 'other', 'box.go'),
  1058. 'package other\n\ntype Box struct{ w int }\n'
  1059. );
  1060. cg = await CodeGraph.init(tempDir, { index: true });
  1061. const methodsOf = (typeName: string, file: string): string[] => {
  1062. const node = cg
  1063. .getNodesByKind('struct')
  1064. .find((n) => n.name === typeName && n.filePath.replace(/\\/g, '/') === file);
  1065. expect(node, `${typeName} @ ${file}`).toBeDefined();
  1066. return cg
  1067. .getOutgoingEdges(node!.id)
  1068. .filter((e) => e.kind === 'contains')
  1069. .map((e) => cg.getNode(e.target))
  1070. .filter((n) => !!n && n.kind === 'method')
  1071. .map((n) => n!.name)
  1072. .sort();
  1073. };
  1074. // Cross-file (non-generic) methods now attach to their struct.
  1075. expect(methodsOf('Box', 'box.go')).toEqual(['Get', 'Set']);
  1076. // Generic + cross-file.
  1077. expect(methodsOf('Stack', 'stack.go')).toEqual(['Push']);
  1078. // Cross-package isolation: other/Box defines no methods of its own.
  1079. expect(methodsOf('Box', 'other/box.go')).toEqual([]);
  1080. });
  1081. it('TS type_alias object-shape members resolve method calls (#359)', async () => {
  1082. // Pre-#359, `recorder.stop()` (recorder: RecorderHandle) attached
  1083. // to `StdioMcpClient.stop` in a sibling directory via path-proximity
  1084. // because the type_alias had no `stop` node — only the unrelated
  1085. // class did. Now type_alias produces member nodes (property/method),
  1086. // so the camelCase receiver↔type word overlap pulls the call to
  1087. // `RecorderHandle::stop` instead of the look-alike class.
  1088. fs.mkdirSync(path.join(tempDir, 'voice'));
  1089. fs.mkdirSync(path.join(tempDir, 'codegraph'));
  1090. fs.writeFileSync(
  1091. path.join(tempDir, 'voice', 'recorder.ts'),
  1092. `export type RecorderHandle = {
  1093. wavPath: string;
  1094. stop: () => Promise<{ ok: true }>;
  1095. };
  1096. `
  1097. );
  1098. fs.writeFileSync(
  1099. path.join(tempDir, 'voice', 'controller.ts'),
  1100. `import type { RecorderHandle } from "./recorder";
  1101. export async function finaliseRecording(recorder: RecorderHandle) {
  1102. return await recorder.stop();
  1103. }
  1104. `
  1105. );
  1106. fs.writeFileSync(
  1107. path.join(tempDir, 'codegraph', 'stdio-client.ts'),
  1108. `export class StdioMcpClient {
  1109. private stopped = false;
  1110. async stop(): Promise<void> { this.stopped = true; }
  1111. }
  1112. `
  1113. );
  1114. cg = await CodeGraph.init(tempDir, { index: true });
  1115. const handleStop = cg
  1116. .getNodesByKind('method')
  1117. .find((n) => n.qualifiedName === 'RecorderHandle::stop');
  1118. expect(handleStop).toBeDefined();
  1119. const clientStop = cg
  1120. .getNodesByKind('method')
  1121. .find((n) => n.qualifiedName === 'StdioMcpClient::stop');
  1122. expect(clientStop).toBeDefined();
  1123. const handleCallers = cg.getIncomingEdges(handleStop!.id).filter((e) => e.kind === 'calls');
  1124. const clientCallers = cg.getIncomingEdges(clientStop!.id).filter((e) => e.kind === 'calls');
  1125. expect(handleCallers.length).toBeGreaterThanOrEqual(1);
  1126. // The class method must have NO callers — voice/'s call must NOT
  1127. // mis-attribute. A non-empty list would mean the false-positive
  1128. // path is still firing.
  1129. expect(clientCallers).toHaveLength(0);
  1130. // Function-typed property surfaces as a `method` node, not `property`,
  1131. // because `stop()` semantics at the call site are method semantics.
  1132. expect(handleStop!.kind).toBe('method');
  1133. });
  1134. it('Java import disambiguates same-name classes across modules (#314)', async () => {
  1135. // Pre-#314 the import resolver had no Java branch at all, so a
  1136. // multi-module Maven repo where `dao/converter/FooConverter` and
  1137. // `service/converter/FooConverter` both export a `convert` method
  1138. // resolved by file-path proximity — picking whichever class was
  1139. // closer to the caller, which is wrong any time the caller lives
  1140. // in an equidistant cross-cutting module.
  1141. const daoDir = path.join(tempDir, 'dao/src/main/java/com/example/dao/converter');
  1142. const serviceDir = path.join(tempDir, 'service/src/main/java/com/example/service/converter');
  1143. const webDir = path.join(tempDir, 'web/src/main/java/com/example/web');
  1144. fs.mkdirSync(daoDir, { recursive: true });
  1145. fs.mkdirSync(serviceDir, { recursive: true });
  1146. fs.mkdirSync(webDir, { recursive: true });
  1147. fs.writeFileSync(
  1148. path.join(daoDir, 'FooConverter.java'),
  1149. `package com.example.dao.converter;
  1150. public class FooConverter { public String convert(String x) { return "dao:" + x; } }
  1151. `
  1152. );
  1153. fs.writeFileSync(
  1154. path.join(serviceDir, 'FooConverter.java'),
  1155. `package com.example.service.converter;
  1156. public class FooConverter { public String convert(String x) { return "svc:" + x; } }
  1157. `
  1158. );
  1159. // The caller imports the SERVICE version — even though dao is
  1160. // alphabetically/lexically first in the candidate list, the
  1161. // import must trump that order.
  1162. fs.writeFileSync(
  1163. path.join(webDir, 'Handler.java'),
  1164. `package com.example.web;
  1165. import com.example.service.converter.FooConverter;
  1166. public class Handler {
  1167. private FooConverter fooConverter;
  1168. public String use() { return fooConverter.convert("input"); }
  1169. }
  1170. `
  1171. );
  1172. cg = await CodeGraph.init(tempDir, { index: true });
  1173. const use = cg
  1174. .getNodesByKind('method')
  1175. .find((n) => n.qualifiedName === 'com.example.web::Handler::use');
  1176. expect(use).toBeDefined();
  1177. const calls = cg.getOutgoingEdges(use!.id).filter((e) => e.kind === 'calls');
  1178. expect(calls.length).toBeGreaterThanOrEqual(1);
  1179. const target = cg.getNode(calls[0]!.target);
  1180. expect(target?.name).toBe('convert');
  1181. expect(target?.filePath.replace(/\\/g, '/')).toBe(
  1182. 'service/src/main/java/com/example/service/converter/FooConverter.java'
  1183. );
  1184. });
  1185. it('C# extracts references from method/property/field types (#381)', async () => {
  1186. // Pre-#381, every C# project produced ZERO `references` edges:
  1187. // csharp.ts was missing returnField, and the type-leaf walker
  1188. // only recognized TS/Java's `type_identifier` nodes — C# uses
  1189. // `identifier`/`predefined_type`/`qualified_name`/`generic_name`.
  1190. const srcDir = path.join(tempDir, 'src');
  1191. fs.mkdirSync(srcDir, { recursive: true });
  1192. fs.writeFileSync(
  1193. path.join(srcDir, 'Dtos.cs'),
  1194. `namespace MyApp;
  1195. public class SessionInfoDto { public string Id { get; set; } = ""; }
  1196. public class UserDto { public string Name { get; set; } = ""; }
  1197. `
  1198. );
  1199. fs.writeFileSync(
  1200. path.join(srcDir, 'Service.cs'),
  1201. `using System.Threading.Tasks;
  1202. namespace MyApp;
  1203. public class DataExporter
  1204. {
  1205. public SessionInfoDto Build(UserDto user, SessionInfoDto session) { return session; }
  1206. public Task<SessionInfoDto> BuildAsync(UserDto user) { return Task.FromResult(new SessionInfoDto()); }
  1207. public SessionInfoDto Latest { get; set; } = new();
  1208. private UserDto _cached;
  1209. }
  1210. `
  1211. );
  1212. cg = await CodeGraph.init(tempDir, { index: true });
  1213. const sessionDto = cg
  1214. .getNodesByKind('class')
  1215. .find((n) => n.name === 'SessionInfoDto');
  1216. const userDto = cg
  1217. .getNodesByKind('class')
  1218. .find((n) => n.name === 'UserDto');
  1219. expect(sessionDto).toBeDefined();
  1220. expect(userDto).toBeDefined();
  1221. const sessionIncoming = cg
  1222. .getIncomingEdges(sessionDto!.id)
  1223. .filter((e) => e.kind === 'references');
  1224. const userIncoming = cg
  1225. .getIncomingEdges(userDto!.id)
  1226. .filter((e) => e.kind === 'references');
  1227. // SessionInfoDto: Build return, Build param, BuildAsync return (inside Task<>), Latest property.
  1228. // UserDto: Build param, BuildAsync param, _cached field.
  1229. expect(sessionIncoming.length).toBeGreaterThanOrEqual(4);
  1230. expect(userIncoming.length).toBeGreaterThanOrEqual(3);
  1231. });
  1232. it('C# primary-constructor parameters record their type dependencies (#237)', async () => {
  1233. // C# 12 primary constructors declare a type's injected dependencies inline
  1234. // (`class Svc(IRepo repo, [FromKeyedServices("k")] ICache cache)`). Each
  1235. // ctor parameter's type is recorded as a `references` edge from the class,
  1236. // so a DI-registered contract reached only through a primary ctor is no
  1237. // longer reported as having no dependents.
  1238. fs.mkdirSync(path.join(tempDir, 'src'), { recursive: true });
  1239. fs.writeFileSync(
  1240. path.join(tempDir, 'src', 'Contracts.cs'),
  1241. `namespace App;
  1242. public interface IRepo { }
  1243. public class ICache { }
  1244. `
  1245. );
  1246. fs.writeFileSync(
  1247. path.join(tempDir, 'src', 'OrderService.cs'),
  1248. `namespace App;
  1249. public sealed class OrderService(IRepo repo, [FromKeyedServices("primary")] ICache cache)
  1250. {
  1251. public void Run() { }
  1252. }
  1253. `
  1254. );
  1255. cg = await CodeGraph.init(tempDir, { index: true });
  1256. const svc = cg.getNodesByKind('class').find((n) => n.name === 'OrderService');
  1257. expect(svc).toBeDefined();
  1258. // The class itself must index (it used to vanish under the old grammar).
  1259. const out = cg.getOutgoingEdges(svc!.id).filter((e) => e.kind === 'references');
  1260. const depNames = out.map((e) => cg.getNode(e.target)?.name);
  1261. expect(depNames).toContain('IRepo');
  1262. expect(depNames).toContain('ICache'); // the keyed-DI ([FromKeyedServices]) dependency
  1263. });
  1264. it('Go: leaves stdlib calls (fmt.Println, etc.) external', async () => {
  1265. fs.writeFileSync(
  1266. path.join(tempDir, 'go.mod'),
  1267. 'module github.com/example/myproject\n\ngo 1.21\n'
  1268. );
  1269. fs.writeFileSync(
  1270. path.join(tempDir, 'main.go'),
  1271. `package main
  1272. import "fmt"
  1273. func main() {
  1274. fmt.Println("hi")
  1275. }
  1276. `
  1277. );
  1278. cg = await CodeGraph.init(tempDir, { index: true });
  1279. const mainFn = cg.getNodesByKind('function').filter((n) => n.name ==='main')[0];
  1280. const calls = cg.getOutgoingEdges(mainFn!.id).filter((e) => e.kind === 'calls');
  1281. // No spurious in-project edge — fmt.* must stay unresolved/external.
  1282. expect(calls).toHaveLength(0);
  1283. });
  1284. });
  1285. describe('Same-name method disambiguation (#1079)', () => {
  1286. // resolveMethodOnType picks among several methods that share a
  1287. // `Type::method` qualifiedName. The precedence is:
  1288. // 1. preferredFqn (Java/Kotlin import — target is intentionally in
  1289. // ANOTHER file, #314),
  1290. // 2. the call site's OWN file (language-agnostic, #1079),
  1291. // 3. matches[0] (first-indexed) as a last resort.
  1292. const methodNode = (
  1293. id: string,
  1294. filePath: string,
  1295. language: Node['language'] = 'cpp',
  1296. qualifiedName = 'Logger::log',
  1297. name = 'log',
  1298. ): Node => ({
  1299. id, kind: 'method', name, qualifiedName, filePath, language,
  1300. startLine: 1, endLine: 1, startColumn: 0, endColumn: 0, updatedAt: 0,
  1301. });
  1302. const callRef = (filePath: string, language: Node['language'] = 'cpp'): UnresolvedRef => ({
  1303. fromNodeId: 'caller', referenceName: 'lg.log', referenceKind: 'calls',
  1304. line: 2, column: 0, filePath, language,
  1305. });
  1306. const ctxFor = (candidates: Node[]): ResolutionContext => ({
  1307. getNodesInFile: () => [],
  1308. getNodesByName: (name) => candidates.filter((c) => c.name === name),
  1309. getNodesByQualifiedName: () => [],
  1310. getNodesByKind: () => [],
  1311. fileExists: () => false,
  1312. readFile: () => null,
  1313. getProjectRoot: () => '',
  1314. getAllFiles: () => [],
  1315. });
  1316. it('prefers the definition in the call site\'s own file (#1079)', () => {
  1317. // matches[0] is the a/ definition; the call comes from b/, so it must
  1318. // resolve to b/ — not collapse onto the first-indexed match.
  1319. const logA = methodNode('m:a', 'a/svc.cpp');
  1320. const logB = methodNode('m:b', 'b/svc.cpp');
  1321. const result = resolveMethodOnType(
  1322. 'Logger', 'log', callRef('b/svc.cpp'), ctxFor([logA, logB]), 0.9, 'instance-method',
  1323. );
  1324. expect(result?.targetNodeId).toBe('m:b');
  1325. });
  1326. it('lets an import FQN pin a cross-file target over the same-file preference (#314)', () => {
  1327. // Java: two `Bar::doIt` in different packages. The import FQN pins the
  1328. // alpha package; even though the call site lives in beta's file, the FQN
  1329. // must win — the same-file preference runs only AFTER preferredFqn.
  1330. const alpha = methodNode('m:alpha', 'com/example/alpha/Bar.java', 'java', 'Bar::doIt', 'doIt');
  1331. const beta = methodNode('m:beta', 'com/example/beta/Bar.java', 'java', 'Bar::doIt', 'doIt');
  1332. const result = resolveMethodOnType(
  1333. 'Bar', 'doIt', callRef('com/example/beta/Bar.java', 'java'),
  1334. ctxFor([alpha, beta]), 0.9, 'instance-method', 'com.example.alpha.Bar',
  1335. );
  1336. expect(result?.targetNodeId).toBe('m:alpha');
  1337. });
  1338. it('falls back to the first match when nothing disambiguates', () => {
  1339. // Call site is a third file: no FQN, no same-file candidate → matches[0].
  1340. const logA = methodNode('m:a', 'a/svc.cpp');
  1341. const logB = methodNode('m:b', 'b/svc.cpp');
  1342. const result = resolveMethodOnType(
  1343. 'Logger', 'log', callRef('c/other.cpp'), ctxFor([logA, logB]), 0.9, 'instance-method',
  1344. );
  1345. expect(result?.targetNodeId).toBe('m:a');
  1346. });
  1347. it('resolves C++ calls end-to-end to same-named classes in different files (#1079)', async () => {
  1348. // The exact repro from the issue: two files, each with its own
  1349. // `Logger::log`. Before the fix both callers pointed at the first def.
  1350. fs.mkdirSync(path.join(tempDir, 'a'), { recursive: true });
  1351. fs.mkdirSync(path.join(tempDir, 'b'), { recursive: true });
  1352. fs.writeFileSync(
  1353. path.join(tempDir, 'a', 'svc.cpp'),
  1354. `class Logger { public: void log() { int a = 1; } };\nvoid useA() { Logger lg; lg.log(); }\n`,
  1355. );
  1356. fs.writeFileSync(
  1357. path.join(tempDir, 'b', 'svc.cpp'),
  1358. `class Logger { public: void log() { int b = 2; } };\nvoid useB() { Logger lg; lg.log(); }\n`,
  1359. );
  1360. cg = await CodeGraph.init(tempDir, { index: true });
  1361. cg.resolveReferences();
  1362. const logInDir = (dir: string) =>
  1363. cg.getNodesByKind('method').find(
  1364. (n) => n.name === 'log' && n.filePath.replace(/\\/g, '/').endsWith(`${dir}/svc.cpp`),
  1365. )!;
  1366. const callTargets = (fnName: string) =>
  1367. cg
  1368. .getOutgoingEdges(cg.getNodesByKind('function').find((n) => n.name === fnName)!.id)
  1369. .filter((e) => e.kind === 'calls')
  1370. .map((e) => e.target);
  1371. const logA = logInDir('a');
  1372. const logB = logInDir('b');
  1373. expect(logA).toBeDefined();
  1374. expect(logB).toBeDefined();
  1375. expect(logA.id).not.toBe(logB.id);
  1376. // Each caller resolves to the Logger::log in its OWN file.
  1377. expect(callTargets('useA')).toContain(logA.id);
  1378. expect(callTargets('useB')).toContain(logB.id);
  1379. });
  1380. it('preferCallSiteFile puts same-file candidates first and is otherwise a no-op', () => {
  1381. const a = methodNode('m:a', 'a/svc.cpp');
  1382. const b = methodNode('m:b', 'b/svc.cpp');
  1383. // Same-file first; the rest keep their original order (stable).
  1384. expect(preferCallSiteFile([a, b], 'b/svc.cpp').map((n) => n.id)).toEqual(['m:b', 'm:a']);
  1385. expect(preferCallSiteFile([a, b], 'a/svc.cpp').map((n) => n.id)).toEqual(['m:a', 'm:b']);
  1386. // No same-file match → unchanged; <2 candidates → returned as-is.
  1387. expect(preferCallSiteFile([a, b], 'c/other.cpp').map((n) => n.id)).toEqual(['m:a', 'm:b']);
  1388. expect(preferCallSiteFile([a], 'z/none.cpp')).toHaveLength(1);
  1389. });
  1390. it('matchByQualifiedName prefers the same-file target when a qualified name is ambiguous (#1079)', () => {
  1391. // Two `Logger::log` definitions; an explicit `Logger::log()` call from b/
  1392. // must resolve to b/'s definition, not the first-indexed one.
  1393. const a = methodNode('m:a', 'a/svc.cpp');
  1394. const b = methodNode('m:b', 'b/svc.cpp');
  1395. const ctx: ResolutionContext = {
  1396. getNodesInFile: () => [],
  1397. getNodesByName: (name) => [a, b].filter((n) => n.name === name),
  1398. getNodesByQualifiedName: (q) => (q === 'Logger::log' ? [a, b] : []),
  1399. getNodesByKind: () => [],
  1400. fileExists: () => false,
  1401. readFile: () => null,
  1402. getProjectRoot: () => '',
  1403. getAllFiles: () => [],
  1404. };
  1405. const ref: UnresolvedRef = {
  1406. fromNodeId: 'caller', referenceName: 'Logger::log', referenceKind: 'calls',
  1407. line: 2, column: 0, filePath: 'b/svc.cpp', language: 'cpp',
  1408. };
  1409. expect(matchByQualifiedName(ref, ctx)?.targetNodeId).toBe('m:b');
  1410. });
  1411. it('resolves a static/class-receiver call to the class in the caller\'s file (#1079)', async () => {
  1412. // `Logger.log()` — the receiver is the class NAME, so this routes through
  1413. // the class-name-receiver strategy (not the C++ instance path). It was
  1414. // file-blind across languages; verified here on TypeScript.
  1415. fs.mkdirSync(path.join(tempDir, 'a'), { recursive: true });
  1416. fs.mkdirSync(path.join(tempDir, 'b'), { recursive: true });
  1417. fs.writeFileSync(
  1418. path.join(tempDir, 'a', 'svc.ts'),
  1419. `class Logger { static log() { return 1; } }\nexport function useA() { return Logger.log(); }\n`,
  1420. );
  1421. fs.writeFileSync(
  1422. path.join(tempDir, 'b', 'svc.ts'),
  1423. `class Logger { static log() { return 2; } }\nexport function useB() { return Logger.log(); }\n`,
  1424. );
  1425. cg = await CodeGraph.init(tempDir, { index: true });
  1426. cg.resolveReferences();
  1427. const logInDir = (dir: string) =>
  1428. cg.getNodesByKind('method').find(
  1429. (n) => n.name === 'log' && n.filePath.replace(/\\/g, '/').endsWith(`${dir}/svc.ts`),
  1430. )!;
  1431. const callTargets = (fnName: string) =>
  1432. cg
  1433. .getOutgoingEdges(cg.getNodesByKind('function').find((n) => n.name === fnName)!.id)
  1434. .filter((e) => e.kind === 'calls')
  1435. .map((e) => e.target);
  1436. const logA = logInDir('a');
  1437. const logB = logInDir('b');
  1438. expect(logA?.id).not.toBe(logB?.id);
  1439. expect(callTargets('useA')).toContain(logA.id);
  1440. expect(callTargets('useB')).toContain(logB.id);
  1441. });
  1442. it('resolves an explicitly-qualified call to the definition in the caller\'s file (#1079)', async () => {
  1443. // `Logger::log()` with two `Logger::log` definitions routes through the
  1444. // qualified-name strategy, whose partial match previously picked the first.
  1445. fs.mkdirSync(path.join(tempDir, 'a'), { recursive: true });
  1446. fs.mkdirSync(path.join(tempDir, 'b'), { recursive: true });
  1447. fs.writeFileSync(
  1448. path.join(tempDir, 'a', 'svc.cpp'),
  1449. `class Logger { public: static void log() { int a = 1; } };\nvoid useA() { Logger::log(); }\n`,
  1450. );
  1451. fs.writeFileSync(
  1452. path.join(tempDir, 'b', 'svc.cpp'),
  1453. `class Logger { public: static void log() { int b = 2; } };\nvoid useB() { Logger::log(); }\n`,
  1454. );
  1455. cg = await CodeGraph.init(tempDir, { index: true });
  1456. cg.resolveReferences();
  1457. const logInDir = (dir: string) =>
  1458. cg.getNodesByKind('method').find(
  1459. (n) => n.name === 'log' && n.filePath.replace(/\\/g, '/').endsWith(`${dir}/svc.cpp`),
  1460. )!;
  1461. const callTargets = (fnName: string) =>
  1462. cg
  1463. .getOutgoingEdges(cg.getNodesByKind('function').find((n) => n.name === fnName)!.id)
  1464. .filter((e) => e.kind === 'calls')
  1465. .map((e) => e.target);
  1466. const logA = logInDir('a');
  1467. const logB = logInDir('b');
  1468. expect(logA?.id).not.toBe(logB?.id);
  1469. expect(callTargets('useA')).toContain(logA.id);
  1470. expect(callTargets('useB')).toContain(logB.id);
  1471. });
  1472. });
  1473. describe('Watchdog-safe resolution on collision-heavy repos (#1122)', () => {
  1474. // On a large Java-style repo, per-ref resolution cost is unbounded in the
  1475. // worst case (a colliding method name whose candidate set misses the LRU
  1476. // re-fetches tens of thousands of rows, and receiver inference re-splits
  1477. // the whole source file). v1.2.0 yielded only every 500 refs, so a dense
  1478. // pocket multiplied that cost past the #850 watchdog window and a VALID
  1479. // `init` was SIGKILLed at "Resolving refs". These pin the three guards:
  1480. // per-ref yield checkpoints, the (type, method) match memo, and the
  1481. // per-file lines cache with its generated/minified-line skip.
  1482. const methodNode = (
  1483. id: string,
  1484. filePath: string,
  1485. qualifiedName: string,
  1486. name: string,
  1487. language: Node['language'] = 'typescript',
  1488. kind: Node['kind'] = 'method',
  1489. ): Node => ({
  1490. id, kind, name, qualifiedName, filePath, language,
  1491. startLine: 1, endLine: 1, startColumn: 0, endColumn: 0, updatedAt: 0,
  1492. });
  1493. it('resolveMethodOnType consults the method-match memo and still disambiguates per call site', () => {
  1494. const logA = methodNode('m:a', 'a/svc.ts', 'Logger::log', 'log');
  1495. const logB = methodNode('m:b', 'b/svc.ts', 'Logger::log', 'log');
  1496. const shared = [logA, logB]; // one cached array served to every caller
  1497. let memoCalls = 0;
  1498. let rawNameLookups = 0;
  1499. const ctx: ResolutionContext = {
  1500. getNodesInFile: () => [],
  1501. getNodesByName: () => { rawNameLookups++; return shared; },
  1502. getMethodMatches: () => { memoCalls++; return shared; },
  1503. getNodesByQualifiedName: () => [],
  1504. getNodesByKind: () => [],
  1505. fileExists: () => false,
  1506. readFile: () => null,
  1507. getProjectRoot: () => '',
  1508. getAllFiles: () => [],
  1509. };
  1510. const refFrom = (filePath: string): UnresolvedRef => ({
  1511. fromNodeId: 'caller', referenceName: 'lg.log', referenceKind: 'calls',
  1512. line: 2, column: 0, filePath, language: 'typescript',
  1513. });
  1514. // Both call sites read the SAME memoized array, yet each still resolves
  1515. // to its own file — per-ref disambiguation runs after the memo (#1079).
  1516. const fromA = resolveMethodOnType('Logger', 'log', refFrom('a/svc.ts'), ctx, 0.9, 'instance-method');
  1517. const fromB = resolveMethodOnType('Logger', 'log', refFrom('b/svc.ts'), ctx, 0.9, 'instance-method');
  1518. expect(fromA?.targetNodeId).toBe('m:a');
  1519. expect(fromB?.targetNodeId).toBe('m:b');
  1520. expect(memoCalls).toBe(2);
  1521. expect(rawNameLookups).toBe(0); // memo bypasses the unbounded name fetch
  1522. });
  1523. it('the production resolver context memoizes method matches per (language, type, method)', async () => {
  1524. fs.writeFileSync(
  1525. path.join(tempDir, 'svc.ts'),
  1526. `class Logger { log() { return 1; } }\nexport function use() { const lg = new Logger(); return lg.log(); }\n`,
  1527. );
  1528. cg = await CodeGraph.init(tempDir, { index: true });
  1529. const resolver = (cg as unknown as { resolver: ReferenceResolver }).resolver;
  1530. const ctx = (resolver as unknown as { context: ResolutionContext }).context;
  1531. const first = ctx.getMethodMatches!('Logger', 'log', 'typescript');
  1532. const second = ctx.getMethodMatches!('Logger', 'log', 'typescript');
  1533. expect(first.map((n) => n.qualifiedName)).toEqual(['Logger::log']);
  1534. // Same array instance = served from the memo, not recomputed.
  1535. expect(second).toBe(first);
  1536. resolver.clearCaches();
  1537. const afterClear = ctx.getMethodMatches!('Logger', 'log', 'typescript');
  1538. expect(afterClear).not.toBe(first);
  1539. expect(afterClear.map((n) => n.qualifiedName)).toEqual(['Logger::log']);
  1540. });
  1541. it('resolveBatchYielding offers a yield checkpoint for every ref', async () => {
  1542. fs.writeFileSync(
  1543. path.join(tempDir, 'a.ts'),
  1544. `export function fnA() { return 1; }\nexport function fnB() { return fnA(); }\nexport function fnC() { return fnB(); }\n`,
  1545. );
  1546. fs.writeFileSync(
  1547. path.join(tempDir, 'b.ts'),
  1548. `import { fnA } from './a';\nexport function fnD() { return fnA(); }\n`,
  1549. );
  1550. cg = await CodeGraph.init(tempDir, { index: true });
  1551. const resolver = (cg as unknown as { resolver: ReferenceResolver }).resolver;
  1552. // `init({ index: true })` already ran resolution, so feed the batch
  1553. // directly — resolveBatchYielding takes it as an argument; whether each
  1554. // ref resolves is irrelevant to the checkpoint contract.
  1555. const refs: UnresolvedReference[] = ['fnA', 'fnB', 'nosuchFn', 'fnA', 'alsoMissing'].map((name, i) => ({
  1556. fromNodeId: `caller-${i}`,
  1557. referenceName: name,
  1558. referenceKind: 'calls',
  1559. line: i + 1,
  1560. column: 0,
  1561. filePath: 'a.ts',
  1562. language: 'typescript',
  1563. }));
  1564. let checkpoints = 0;
  1565. const countingYield = async () => { checkpoints++; };
  1566. const result = await (resolver as unknown as {
  1567. resolveBatchYielding(batch: UnresolvedReference[], maybeYield: () => Promise<void>): Promise<{ stats: { total: number } }>;
  1568. }).resolveBatchYielding(refs, countingYield);
  1569. // One checkpoint per ref: a pocket of pathologically slow refs can never
  1570. // run more than ONE ref past the yield budget before the heartbeat gets
  1571. // a window — the #1122 kill required 500.
  1572. expect(checkpoints).toBe(refs.length);
  1573. expect(result.stats.total).toBe(refs.length);
  1574. });
  1575. it('receiver inference reads lines through getFileLines when the context provides it', () => {
  1576. const loggerClass = methodNode('c:logger', 'svc.ts', 'Logger', 'Logger', 'typescript', 'class');
  1577. const logMethod = methodNode('m:log', 'svc.ts', 'Logger::log', 'log');
  1578. const otherLog = methodNode('m:other', 'other.ts', 'Other::log', 'log');
  1579. const byName: Record<string, Node[]> = {
  1580. Logger: [loggerClass],
  1581. log: [logMethod, otherLog], // ambiguous bare name → only inference can resolve
  1582. };
  1583. const lines = ['const lg = new Logger();', 'lg.log();'];
  1584. const ctx: ResolutionContext = {
  1585. getNodesInFile: () => [],
  1586. getNodesByName: (name) => byName[name] ?? [],
  1587. getNodesByQualifiedName: () => [],
  1588. getNodesByKind: () => [],
  1589. fileExists: () => false,
  1590. // Reading the raw source must not be needed when lines are provided.
  1591. readFile: () => { throw new Error('readFile must not be called when getFileLines exists'); },
  1592. getFileLines: () => lines,
  1593. getProjectRoot: () => '',
  1594. getAllFiles: () => [],
  1595. };
  1596. const ref: UnresolvedRef = {
  1597. fromNodeId: 'caller', referenceName: 'lg.log', referenceKind: 'calls',
  1598. line: 2, column: 0, filePath: 'svc.ts', language: 'typescript',
  1599. };
  1600. expect(matchMethodCall(ref, ctx)?.targetNodeId).toBe('m:log');
  1601. });
  1602. it('receiver inference skips generated/minified lines instead of regex-scanning them', () => {
  1603. const loggerClass = methodNode('c:logger', 'svc.ts', 'Logger', 'Logger', 'typescript', 'class');
  1604. const logMethod = methodNode('m:log', 'svc.ts', 'Logger::log', 'log');
  1605. const otherLog = methodNode('m:other', 'other.ts', 'Other::log', 'log');
  1606. const byName: Record<string, Node[]> = {
  1607. Logger: [loggerClass],
  1608. log: [logMethod, otherLog],
  1609. };
  1610. const ctxWithLines = (lines: string[]): ResolutionContext => ({
  1611. getNodesInFile: () => [],
  1612. getNodesByName: (name) => byName[name] ?? [],
  1613. getNodesByQualifiedName: () => [],
  1614. getNodesByKind: () => [],
  1615. fileExists: () => false,
  1616. readFile: () => null,
  1617. getFileLines: () => lines,
  1618. getProjectRoot: () => '',
  1619. getAllFiles: () => [],
  1620. });
  1621. const ref: UnresolvedRef = {
  1622. fromNodeId: 'caller', referenceName: 'lg.log', referenceKind: 'calls',
  1623. line: 1, column: 0, filePath: 'svc.ts', language: 'typescript',
  1624. };
  1625. // Control: the declaration on a normal-length line resolves.
  1626. const normal = matchMethodCall(ref, ctxWithLines(['const lg = new Logger(); lg.log();']));
  1627. expect(normal?.targetNodeId).toBe('m:log');
  1628. // The same declaration buried in a >10K-char generated/minified line is
  1629. // skipped — no resolution, and no per-ref regex pass over the huge line.
  1630. const minified = 'var pad="' + 'x'.repeat(10_000) + '";const lg = new Logger(); lg.log();';
  1631. expect(matchMethodCall(ref, ctxWithLines([minified]))).toBeNull();
  1632. });
  1633. });
  1634. describe('Local-variable receiver-type inference (#1108)', () => {
  1635. // `lg.log()` where `lg` is a local whose type is inferred from its
  1636. // declaration/initializer. Before this, only C++ resolved these; every
  1637. // other language produced no method edge. Each case is one file with a
  1638. // single Logger + a caller using a local-variable receiver — a correct
  1639. // resolution makes the caller a caller of `log`.
  1640. const cases: Array<{ lang: string; file: string; src: string }> = [
  1641. { lang: 'TypeScript (= new T)', file: 'svc.ts',
  1642. src: `class Logger { log() { return 1; } }\nexport function use() { const lg = new Logger(); return lg.log(); }\n` },
  1643. { lang: 'JavaScript (= new T)', file: 'svc.js',
  1644. src: `class Logger { log() { return 1; } }\nexport function use() { const lg = new Logger(); return lg.log(); }\n` },
  1645. { lang: 'Python (= T())', file: 'svc.py',
  1646. src: `class Logger:\n def log(self):\n return 1\ndef use():\n lg = Logger()\n return lg.log()\n` },
  1647. { lang: 'Java (T x = new T)', file: 'Svc.java',
  1648. src: `class Logger { void log() { int a = 1; } }\nclass Use { void run() { Logger lg = new Logger(); lg.log(); } }\n` },
  1649. { lang: 'C# (var x = new T)', file: 'Svc.cs',
  1650. src: `class Logger { void Log() { int a = 1; } }\nclass Use { void Run() { var lg = new Logger(); lg.Log(); } }\n` },
  1651. { lang: 'Kotlin (val x = T())', file: 'Svc.kt',
  1652. src: `class Logger { fun log(): Int { return 1 } }\nfun use(): Int { val lg = Logger(); return lg.log() }\n` },
  1653. { lang: 'Swift (let x = T())', file: 'svc.swift',
  1654. src: `class Logger { func log() -> Int { return 1 } }\nfunc use() -> Int { let lg = Logger(); return lg.log() }\n` },
  1655. { lang: 'Go (x := T{})', file: 'svc.go',
  1656. src: `package a\ntype Logger struct{}\nfunc (l Logger) Log() int { return 1 }\nfunc Use() int { lg := Logger{}; return lg.Log() }\n` },
  1657. { lang: 'Rust (let x = T{})', file: 'svc.rs',
  1658. src: `pub struct Logger { n: i32 }\nimpl Logger { pub fn log(&self) -> i32 { self.n } }\npub fn use_it() -> i32 { let lg = Logger { n: 1 }; lg.log() }\n` },
  1659. { lang: 'Dart (var x = T())', file: 'svc.dart',
  1660. src: `class Logger { int log() { return 1; } }\nint use() { var lg = Logger(); return lg.log(); }\n` },
  1661. { lang: 'PHP ($x = new T)', file: 'svc.php',
  1662. src: `<?php\nclass Logger { function log() { return 1; } }\nfunction useIt() { $lg = new Logger(); return $lg->log(); }\n` },
  1663. { lang: 'Scala (val x = new T)', file: 'Svc.scala',
  1664. src: `class Logger { def log(): Int = 1 }\nobject A { def use(): Int = { val lg = new Logger(); lg.log() } }\n` },
  1665. { lang: 'Ruby (x = T.new)', file: 'svc.rb',
  1666. src: `class Logger\n def log\n 1\n end\nend\ndef use\n lg = Logger.new\n lg.log\nend\n` },
  1667. { lang: 'Lua (x = T.new(); x:log())', file: 'svc.lua',
  1668. src: `local Logger = {}\nLogger.__index = Logger\nfunction Logger.new() return setmetatable({}, Logger) end\nfunction Logger:log() return 1 end\nlocal function use() local lg = Logger.new(); return lg:log() end\nreturn use\n` },
  1669. { lang: 'Luau (x = T.new(); x:log())', file: 'svc.luau',
  1670. src: `local Logger = {}\nLogger.__index = Logger\nfunction Logger.new() return setmetatable({}, Logger) end\nfunction Logger:log(): number return 1 end\nlocal function use(): number local lg = Logger.new(); return lg:log() end\nreturn use\n` },
  1671. { lang: 'R (x <- T$new(); x$log())', file: 'svc.R',
  1672. src: `Logger <- R6::R6Class("Logger", public = list(log = function() 1))\nuse <- function() { lg <- Logger$new(); lg$log() }\n` },
  1673. { lang: 'Pascal (var x: T; x.Method)', file: 'svc.pas',
  1674. src: `unit A;\ninterface\ntype TLogger = class function Log: Integer; end;\nimplementation\nfunction TLogger.Log: Integer; begin Result := 1; end;\nprocedure Use;\nvar lg: TLogger;\nbegin\n lg := TLogger.Create;\n lg.Log;\nend;\nend.\n` },
  1675. ];
  1676. for (const c of cases) {
  1677. it(`resolves a local-variable method call — ${c.lang}`, async () => {
  1678. fs.writeFileSync(path.join(tempDir, c.file), c.src);
  1679. cg = await CodeGraph.init(tempDir, { index: true });
  1680. cg.resolveReferences();
  1681. const logMethod = cg
  1682. .getNodesByKind('method')
  1683. .find((n) => n.name.toLowerCase() === 'log');
  1684. expect(logMethod, `${c.lang}: log method should be indexed`).toBeDefined();
  1685. // The enclosing caller resolves through the local variable to `log`.
  1686. const callers = cg.getCallers(logMethod!.id).map((x) => x.node.name);
  1687. expect(
  1688. callers.length,
  1689. `${c.lang}: log should have a caller (got [${callers.join(', ')}])`,
  1690. ).toBeGreaterThan(0);
  1691. });
  1692. }
  1693. it('Ruby: builds receiver.method and keeps Foo.new as an instantiation', async () => {
  1694. // The Ruby extractor previously took the receiver as the callee and
  1695. // dropped the method name (`lg.log()` -> a call to `lg`). Now it builds
  1696. // `lg.log`, while `Logger.new` must still record an instantiation.
  1697. fs.writeFileSync(
  1698. path.join(tempDir, 'svc.rb'),
  1699. `class Logger\n def log\n 1\n end\nend\ndef run\n lg = Logger.new\n lg.log\nend\n`,
  1700. );
  1701. cg = await CodeGraph.init(tempDir, { index: true });
  1702. cg.resolveReferences();
  1703. const run = cg.getNodesByKind('function').find((n) => n.name === 'run')!;
  1704. const logMethod = cg.getNodesByKind('method').find((n) => n.name === 'log')!;
  1705. const logger = cg.getNodesByKind('class').find((n) => n.name === 'Logger')!;
  1706. const out = cg.getOutgoingEdges(run.id);
  1707. // lg.log resolved to the method (the receiver-type inference kicked in).
  1708. expect(out.some((e) => e.kind === 'calls' && e.target === logMethod.id)).toBe(true);
  1709. // Logger.new is still an instantiation of the class.
  1710. expect(out.some((e) => e.kind === 'instantiates' && e.target === logger.id)).toBe(true);
  1711. });
  1712. it('TypeScript: infers a typed-parameter receiver, disambiguating same-named methods (#1125)', async () => {
  1713. // A typed function parameter used as a receiver — `function use(lg: Logger)`
  1714. // — never matched the old TS/JS pattern (it required a const|let|var
  1715. // prefix), so `lg.log()` fell through to no edge once a second class shared
  1716. // the method name. Two ambiguous classes are load-bearing here: a
  1717. // single-class version resolves via a same-name fallback even without
  1718. // inference, so only the collision proves type inference actually fired.
  1719. fs.writeFileSync(
  1720. path.join(tempDir, 'svc.ts'),
  1721. `class Logger { log() { return 1; } }\n` +
  1722. `class Other { log() { return 2; } }\n` +
  1723. `export function use(lg: Logger) { return lg.log(); }\n` +
  1724. `export function useOther(o: Other) { return o.log(); }\n`,
  1725. );
  1726. cg = await CodeGraph.init(tempDir, { index: true });
  1727. cg.resolveReferences();
  1728. const classes = cg.getNodesByKind('class');
  1729. const logger = classes.find((n) => n.name === 'Logger')!;
  1730. const other = classes.find((n) => n.name === 'Other')!;
  1731. const logs = cg.getNodesByKind('method').filter((n) => n.name === 'log');
  1732. expect(logs.length, 'both log methods should be indexed').toBe(2);
  1733. // Associate each same-named `log` with its class by line containment.
  1734. const inClass = (m: (typeof logs)[number], c: typeof logger) =>
  1735. m.startLine >= c.startLine && m.startLine <= (c.endLine ?? c.startLine);
  1736. const loggerLog = logs.find((m) => inClass(m, logger))!;
  1737. const otherLog = logs.find((m) => inClass(m, other))!;
  1738. expect(loggerLog, "Logger's log").toBeDefined();
  1739. expect(otherLog, "Other's log").toBeDefined();
  1740. const loggerCallers = cg.getCallers(loggerLog.id).map((x) => x.node.name);
  1741. const otherCallers = cg.getCallers(otherLog.id).map((x) => x.node.name);
  1742. // Each typed-param call routes to its OWN class's method, not the other's.
  1743. expect(loggerCallers).toContain('use');
  1744. expect(loggerCallers).not.toContain('useOther');
  1745. expect(otherCallers).toContain('useOther');
  1746. expect(otherCallers).not.toContain('use');
  1747. });
  1748. // The same typed-parameter gap existed in every language whose pattern set
  1749. // only matched keyword-anchored locals (let/var/:=/= new), not the bare
  1750. // parameter form — Rust, Go, Dart, PHP (#1125). Each case: two classes
  1751. // sharing a method name + two functions taking one as a typed param; a
  1752. // correct fix routes each call to its OWN type's method (the collision is
  1753. // load-bearing — a single class resolves via the same-name fallback either
  1754. // way). Method↔type association is by qualifiedName, robust where the method
  1755. // lives outside the type's line range (Rust `impl`, Go method decl).
  1756. const typedParamCases: Array<{
  1757. lang: string; file: string; method: string; callerA: string; callerB: string; src: string;
  1758. }> = [
  1759. { lang: 'Rust (fn f(x: &T))', file: 'svc.rs', method: 'log', callerA: 'use_it', callerB: 'use_other',
  1760. src: `pub struct Logger { n: i32 }\nimpl Logger { pub fn log(&self) -> i32 { self.n } }\npub struct Other { n: i32 }\nimpl Other { pub fn log(&self) -> i32 { self.n } }\npub fn use_it(lg: &Logger) -> i32 { lg.log() }\npub fn use_other(o: &Other) -> i32 { o.log() }\n` },
  1761. { lang: 'Go (func f(x T))', file: 'svc.go', method: 'Log', callerA: 'UseIt', callerB: 'UseOther',
  1762. src: `package a\ntype Logger struct{}\nfunc (l Logger) Log() int { return 1 }\ntype Other struct{}\nfunc (o Other) Log() int { return 2 }\nfunc UseIt(lg Logger) int { return lg.Log() }\nfunc UseOther(o Other) int { return o.Log() }\n` },
  1763. { lang: 'Dart (T f(U x))', file: 'svc.dart', method: 'log', callerA: 'useIt', callerB: 'useOther',
  1764. src: `class Logger { int log() { return 1; } }\nclass Other { int log() { return 2; } }\nint useIt(Logger lg) { return lg.log(); }\nint useOther(Other o) { return o.log(); }\n` },
  1765. { lang: 'PHP (f(T $x))', file: 'svc.php', method: 'log', callerA: 'useIt', callerB: 'useOther',
  1766. src: `<?php\nclass Logger { function log() { return 1; } }\nclass Other { function log() { return 2; } }\nfunction useIt(Logger $lg) { return $lg->log(); }\nfunction useOther(Other $o) { return $o->log(); }\n` },
  1767. ];
  1768. for (const c of typedParamCases) {
  1769. it(`infers a typed-parameter receiver, disambiguating same-named methods — ${c.lang} (#1125)`, async () => {
  1770. fs.writeFileSync(path.join(tempDir, c.file), c.src);
  1771. cg = await CodeGraph.init(tempDir, { index: true });
  1772. cg.resolveReferences();
  1773. const methods = cg.getNodesByKind('method').filter((n) => n.name === c.method);
  1774. expect(methods.length, `${c.lang}: both ${c.method} methods indexed`).toBe(2);
  1775. const loggerLog = methods.find((m) => /Logger/.test(m.qualifiedName ?? ''));
  1776. const otherLog = methods.find((m) => /Other/.test(m.qualifiedName ?? ''));
  1777. expect(loggerLog, `${c.lang}: Logger's ${c.method}`).toBeDefined();
  1778. expect(otherLog, `${c.lang}: Other's ${c.method}`).toBeDefined();
  1779. const loggerCallers = cg.getCallers(loggerLog!.id).map((x) => x.node.name);
  1780. const otherCallers = cg.getCallers(otherLog!.id).map((x) => x.node.name);
  1781. expect(loggerCallers, `${c.lang}: Logger callers`).toContain(c.callerA);
  1782. expect(loggerCallers, `${c.lang}: Logger callers`).not.toContain(c.callerB);
  1783. expect(otherCallers, `${c.lang}: Other callers`).toContain(c.callerB);
  1784. expect(otherCallers, `${c.lang}: Other callers`).not.toContain(c.callerA);
  1785. });
  1786. }
  1787. // Lua/Luau: a PascalCase method call (`lg:Log()`, the Roblox convention)
  1788. // is the identical `receiver:Name` shape as a Luau type annotation, so it
  1789. // self-matched the annotation pattern on the call's own line and inferred
  1790. // "type = Log" (#1124). Two things are load-bearing in these fixtures:
  1791. // the declaration sits on an EARLIER line than the call (on one line,
  1792. // pattern order resolves it — the `.new` pattern wins first), and TWO
  1793. // classes share the method name (a single class resolves via the
  1794. // same-name fallback even when inference misfires). Luau's `useLogger`
  1795. // takes a typed param instead of calling `.new()`, pinning that the
  1796. // gated pattern still matches a genuine annotation.
  1797. const pascalMethodCases: Array<{ lang: string; file: string; src: string }> = [
  1798. { lang: 'Lua', file: 'svc.lua',
  1799. src: `local Logger = {}\nLogger.__index = Logger\nfunction Logger.new() return setmetatable({}, Logger) end\nfunction Logger:Log() return 1 end\n\nlocal Other = {}\nOther.__index = Other\nfunction Other.new() return setmetatable({}, Other) end\nfunction Other:Log() return 2 end\n\nlocal function useLogger()\n\tlocal lg = Logger.new()\n\treturn lg:Log()\nend\n\nlocal function useOther()\n\tlocal o = Other.new()\n\treturn o:Log()\nend\n\nreturn useLogger, useOther\n` },
  1800. { lang: 'Luau', file: 'svc.luau',
  1801. src: `local Logger = {}\nLogger.__index = Logger\nfunction Logger.new() return setmetatable({}, Logger) end\nfunction Logger:Log(): number return 1 end\n\nlocal Other = {}\nOther.__index = Other\nfunction Other.new() return setmetatable({}, Other) end\nfunction Other:Log(): number return 2 end\n\nlocal function useLogger(lg: Logger): number\n\treturn lg:Log()\nend\n\nlocal function useOther(): number\n\tlocal o = Other.new()\n\treturn o:Log()\nend\n\nreturn useLogger, useOther\n` },
  1802. ];
  1803. for (const c of pascalMethodCases) {
  1804. it(`resolves a PascalCase method call without self-matching the annotation pattern — ${c.lang} (#1124)`, async () => {
  1805. fs.writeFileSync(path.join(tempDir, c.file), c.src);
  1806. cg = await CodeGraph.init(tempDir, { index: true });
  1807. cg.resolveReferences();
  1808. const methods = cg.getNodesByKind('method').filter((n) => n.name === 'Log');
  1809. expect(methods.length, `${c.lang}: both Log methods indexed`).toBe(2);
  1810. const loggerLog = methods.find((m) => /Logger/.test(m.qualifiedName ?? ''));
  1811. const otherLog = methods.find((m) => /Other/.test(m.qualifiedName ?? ''));
  1812. expect(loggerLog, `${c.lang}: Logger's Log`).toBeDefined();
  1813. expect(otherLog, `${c.lang}: Other's Log`).toBeDefined();
  1814. const loggerCallers = cg.getCallers(loggerLog!.id).map((x) => x.node.name);
  1815. const otherCallers = cg.getCallers(otherLog!.id).map((x) => x.node.name);
  1816. expect(loggerCallers, `${c.lang}: Logger callers`).toContain('useLogger');
  1817. expect(loggerCallers, `${c.lang}: Logger callers`).not.toContain('useOther');
  1818. expect(otherCallers, `${c.lang}: Other callers`).toContain('useOther');
  1819. expect(otherCallers, `${c.lang}: Other callers`).not.toContain('useLogger');
  1820. });
  1821. }
  1822. });
  1823. describe('Name Matcher: kind bias for new ref kinds', () => {
  1824. const baseContext = (candidates: Node[]): ResolutionContext => ({
  1825. getNodesInFile: () => [],
  1826. getNodesByName: (name) => candidates.filter((c) => c.name === name),
  1827. getNodesByQualifiedName: () => [],
  1828. getNodesByKind: () => [],
  1829. fileExists: () => true,
  1830. readFile: () => null,
  1831. getProjectRoot: () => '/test',
  1832. getAllFiles: () => [],
  1833. getNodesByLowerName: () => [],
  1834. getImportMappings: () => [],
  1835. });
  1836. it('prefers a class candidate over a function for `instantiates` refs', () => {
  1837. // A class and a function share a name across the codebase.
  1838. // Without the kind bias, the function (which gets the +25 `calls`
  1839. // bonus historically applied to all candidates of that kind) would
  1840. // win. Now the instantiates branch reverses it.
  1841. const fn: Node = {
  1842. id: 'func:utils.ts:Logger:5', kind: 'function', name: 'Logger',
  1843. qualifiedName: 'utils.ts::Logger', filePath: 'utils.ts', language: 'typescript',
  1844. startLine: 5, endLine: 7, startColumn: 0, endColumn: 0, updatedAt: Date.now(),
  1845. };
  1846. const cls: Node = {
  1847. id: 'class:logger.ts:Logger:10', kind: 'class', name: 'Logger',
  1848. qualifiedName: 'logger.ts::Logger', filePath: 'logger.ts', language: 'typescript',
  1849. startLine: 10, endLine: 30, startColumn: 0, endColumn: 0, updatedAt: Date.now(),
  1850. };
  1851. const ref = {
  1852. fromNodeId: 'func:main.ts:bootstrap:1',
  1853. referenceName: 'Logger',
  1854. referenceKind: 'instantiates' as const,
  1855. line: 5, column: 0, filePath: 'main.ts', language: 'typescript' as const,
  1856. };
  1857. const result = matchReference(ref, baseContext([fn, cls]));
  1858. expect(result?.targetNodeId).toBe('class:logger.ts:Logger:10');
  1859. });
  1860. it('prefers a function candidate over a non-function for `decorates` refs', () => {
  1861. const variable: Node = {
  1862. id: 'var:config.ts:Inject:5', kind: 'variable', name: 'Inject',
  1863. qualifiedName: 'config.ts::Inject', filePath: 'config.ts', language: 'typescript',
  1864. startLine: 5, endLine: 5, startColumn: 0, endColumn: 0, updatedAt: Date.now(),
  1865. };
  1866. const decorator: Node = {
  1867. id: 'func:di.ts:Inject:10', kind: 'function', name: 'Inject',
  1868. qualifiedName: 'di.ts::Inject', filePath: 'di.ts', language: 'typescript',
  1869. startLine: 10, endLine: 20, startColumn: 0, endColumn: 0, updatedAt: Date.now(),
  1870. };
  1871. const ref = {
  1872. fromNodeId: 'class:svc.ts:UserService:1',
  1873. referenceName: 'Inject',
  1874. referenceKind: 'decorates' as const,
  1875. line: 5, column: 0, filePath: 'svc.ts', language: 'typescript' as const,
  1876. };
  1877. const result = matchReference(ref, baseContext([variable, decorator]));
  1878. expect(result?.targetNodeId).toBe('func:di.ts:Inject:10');
  1879. });
  1880. });
  1881. describe('tsconfig path aliases', () => {
  1882. it('resolves an aliased import to the alias-mapped file (not a same-named file elsewhere)', async () => {
  1883. // Two same-named exports in different directories. Without alias
  1884. // resolution, name-matcher would pick whichever it finds first;
  1885. // with alias resolution, the import path uniquely picks one.
  1886. fs.mkdirSync(path.join(tempDir, 'src/utils'), { recursive: true });
  1887. fs.mkdirSync(path.join(tempDir, 'src/legacy'), { recursive: true });
  1888. fs.writeFileSync(
  1889. path.join(tempDir, 'src/utils/format.ts'),
  1890. `export function pickMe(): number { return 1; }\n`
  1891. );
  1892. fs.writeFileSync(
  1893. path.join(tempDir, 'src/legacy/format.ts'),
  1894. `export function pickMe(): number { return 99; }\n`
  1895. );
  1896. fs.writeFileSync(
  1897. path.join(tempDir, 'src/main.ts'),
  1898. `import { pickMe } from '@utils/format';\nexport function go(): number { return pickMe(); }\n`
  1899. );
  1900. fs.writeFileSync(
  1901. path.join(tempDir, 'tsconfig.json'),
  1902. JSON.stringify({
  1903. compilerOptions: {
  1904. baseUrl: './src',
  1905. paths: { '@utils/*': ['utils/*'] },
  1906. },
  1907. })
  1908. );
  1909. cg = await CodeGraph.init(tempDir, { index: true });
  1910. cg.resolveReferences();
  1911. // The two pickMe nodes live in different files. The aliased
  1912. // import should attach the call edge to the @utils-mapped one,
  1913. // not the legacy duplicate.
  1914. const all = cg.getNodesByKind('function').filter((n) => n.name === 'pickMe');
  1915. const utilsNode = all.find((n) => n.filePath === 'src/utils/format.ts');
  1916. const legacyNode = all.find((n) => n.filePath === 'src/legacy/format.ts');
  1917. expect(utilsNode).toBeDefined();
  1918. expect(legacyNode).toBeDefined();
  1919. const utilsCallers = cg.getCallers(utilsNode!.id);
  1920. const legacyCallers = cg.getCallers(legacyNode!.id);
  1921. expect(utilsCallers.length).toBeGreaterThan(0);
  1922. expect(utilsCallers.some((c) => c.node.filePath === 'src/main.ts')).toBe(true);
  1923. // The legacy node should NOT have a caller from src/main.ts —
  1924. // the alias correctly picked the utils version.
  1925. expect(legacyCallers.some((c) => c.node.filePath === 'src/main.ts')).toBe(false);
  1926. });
  1927. it('falls back gracefully when tsconfig is absent', async () => {
  1928. fs.mkdirSync(path.join(tempDir, 'src'), { recursive: true });
  1929. fs.writeFileSync(
  1930. path.join(tempDir, 'src/a.ts'),
  1931. `export function aFn(): void {}\n`
  1932. );
  1933. fs.writeFileSync(
  1934. path.join(tempDir, 'src/b.ts'),
  1935. `import { aFn } from './a';\nexport function bFn(): void { aFn(); }\n`
  1936. );
  1937. cg = await CodeGraph.init(tempDir, { index: true });
  1938. // No tsconfig present — index should still complete and the
  1939. // relative-import-based call edge should be created.
  1940. const aFn = cg.getNodesByKind('function').find((n) => n.name === 'aFn');
  1941. expect(aFn).toBeDefined();
  1942. const callers = cg.getCallers(aFn!.id);
  1943. expect(callers.some((c) => c.node.filePath === 'src/b.ts')).toBe(true);
  1944. });
  1945. });
  1946. describe('re-export chain following', () => {
  1947. it('chases a 3-hop barrel chain (wildcard → named → declaration)', async () => {
  1948. // main.ts → all.ts (wildcard) → index.ts (named) → auth.ts (declaration).
  1949. // Without chain following, `signIn` resolves to nothing because
  1950. // none of the barrel files declare it directly.
  1951. fs.mkdirSync(path.join(tempDir, 'src/services'), { recursive: true });
  1952. fs.writeFileSync(
  1953. path.join(tempDir, 'src/services/auth.ts'),
  1954. `export function signIn(): void {}\n`
  1955. );
  1956. fs.writeFileSync(
  1957. path.join(tempDir, 'src/services/index.ts'),
  1958. `export { signIn } from './auth';\n`
  1959. );
  1960. fs.writeFileSync(
  1961. path.join(tempDir, 'src/all.ts'),
  1962. `export * from './services/index';\n`
  1963. );
  1964. fs.writeFileSync(
  1965. path.join(tempDir, 'src/main.ts'),
  1966. `import { signIn } from './all';\nexport function go(): void { signIn(); }\n`
  1967. );
  1968. cg = await CodeGraph.init(tempDir, { index: true });
  1969. cg.resolveReferences();
  1970. const signInNode = cg
  1971. .getNodesByKind('function')
  1972. .find((n) => n.name === 'signIn' && n.filePath === 'src/services/auth.ts');
  1973. expect(signInNode).toBeDefined();
  1974. const callers = cg.getCallers(signInNode!.id);
  1975. expect(callers.some((c) => c.node.filePath === 'src/main.ts')).toBe(true);
  1976. });
  1977. it('follows a renamed named re-export (export { foo as bar } from ...)', async () => {
  1978. // The chase has to look up `foo` in the upstream module even
  1979. // though the importer asked for `bar` — exercises the rename
  1980. // branch of findExportedSymbol.
  1981. fs.mkdirSync(path.join(tempDir, 'src'), { recursive: true });
  1982. fs.writeFileSync(
  1983. path.join(tempDir, 'src/auth.ts'),
  1984. `export function signIn(): void {}\n`
  1985. );
  1986. fs.writeFileSync(
  1987. path.join(tempDir, 'src/index.ts'),
  1988. `export { signIn as login } from './auth';\n`
  1989. );
  1990. fs.writeFileSync(
  1991. path.join(tempDir, 'src/main.ts'),
  1992. `import { login } from './index';\nexport function go(): void { login(); }\n`
  1993. );
  1994. cg = await CodeGraph.init(tempDir, { index: true });
  1995. cg.resolveReferences();
  1996. const signInNode = cg
  1997. .getNodesByKind('function')
  1998. .find((n) => n.name === 'signIn' && n.filePath === 'src/auth.ts');
  1999. expect(signInNode).toBeDefined();
  2000. const callers = cg.getCallers(signInNode!.id);
  2001. expect(callers.some((c) => c.node.filePath === 'src/main.ts')).toBe(true);
  2002. });
  2003. it('follows a default re-export of a .svelte component (export { default as Foo } from ./RealButton.svelte) (#629)', async () => {
  2004. // The ubiquitous Svelte/React component-barrel form. The leaf is a
  2005. // .svelte component (extracted as kind 'component', the default
  2006. // export). The re-export ALIAS (`Foo`) deliberately differs from the
  2007. // component's real name (`RealButton`) so the name-matcher fallback
  2008. // can't coincidentally connect them — the only path to the edge is
  2009. // the import-chase, which must match a `component` (not just
  2010. // function/class) for the default export. Otherwise the
  2011. // consumer↔component edge is never created and `callers` returns a
  2012. // false 0.
  2013. fs.mkdirSync(path.join(tempDir, 'src/lib'), { recursive: true });
  2014. fs.writeFileSync(
  2015. path.join(tempDir, 'src/lib/RealButton.svelte'),
  2016. `<script lang="ts">\n export let label: string = '';\n</script>\n\n<button>{label}</button>\n`
  2017. );
  2018. fs.writeFileSync(
  2019. path.join(tempDir, 'src/lib/index.ts'),
  2020. `export { default as Foo } from './RealButton.svelte';\n`
  2021. );
  2022. fs.writeFileSync(
  2023. path.join(tempDir, 'src/Bar.svelte'),
  2024. `<script lang="ts">\n import { Foo } from './lib';\n</script>\n\n<Foo />\n`
  2025. );
  2026. cg = await CodeGraph.init(tempDir, { index: true });
  2027. cg.resolveReferences();
  2028. const fooNode = cg
  2029. .getNodesByKind('component')
  2030. .find((n) => n.name === 'RealButton' && n.filePath === 'src/lib/RealButton.svelte');
  2031. expect(fooNode).toBeDefined();
  2032. const callers = cg.getCallers(fooNode!.id);
  2033. expect(callers.some((c) => c.node.filePath === 'src/Bar.svelte')).toBe(true);
  2034. });
  2035. it('links an .astro page to the component and TS util it uses (#768)', async () => {
  2036. // The canonical Astro shape: a page imports a layout/component in
  2037. // frontmatter and uses it as a template tag; the component's template
  2038. // calls an imported .ts util. Both hops must produce graph edges or
  2039. // an Astro project is invisible to callers/impact.
  2040. fs.mkdirSync(path.join(tempDir, 'src/components'), { recursive: true });
  2041. fs.mkdirSync(path.join(tempDir, 'src/utils'), { recursive: true });
  2042. fs.mkdirSync(path.join(tempDir, 'src/pages'), { recursive: true });
  2043. fs.writeFileSync(
  2044. path.join(tempDir, 'src/utils/format.ts'),
  2045. `export function formatDate(d: Date): string { return d.toISOString(); }\n`
  2046. );
  2047. fs.writeFileSync(
  2048. path.join(tempDir, 'src/components/PostCard.astro'),
  2049. `---\nimport { formatDate } from '../utils/format';\nconst { date } = Astro.props;\n---\n<time>{formatDate(date)}</time>\n`
  2050. );
  2051. fs.writeFileSync(
  2052. path.join(tempDir, 'src/pages/index.astro'),
  2053. `---\nimport PostCard from '../components/PostCard.astro';\n---\n<PostCard date={new Date()} />\n`
  2054. );
  2055. cg = await CodeGraph.init(tempDir, { index: true });
  2056. cg.resolveReferences();
  2057. // Hop 1: page → component (template tag through the frontmatter import)
  2058. const cardNode = cg
  2059. .getNodesByKind('component')
  2060. .find((n) => n.name === 'PostCard' && n.filePath === 'src/components/PostCard.astro');
  2061. expect(cardNode).toBeDefined();
  2062. const cardCallers = cg.getCallers(cardNode!.id);
  2063. expect(cardCallers.some((c) => c.node.filePath === 'src/pages/index.astro')).toBe(true);
  2064. // Hop 2: component template call → .ts util
  2065. const fmtNode = cg
  2066. .getNodesByKind('function')
  2067. .find((n) => n.name === 'formatDate' && n.filePath === 'src/utils/format.ts');
  2068. expect(fmtNode).toBeDefined();
  2069. const fmtCallers = cg.getCallers(fmtNode!.id);
  2070. expect(fmtCallers.some((c) => c.node.filePath === 'src/components/PostCard.astro')).toBe(true);
  2071. });
  2072. it('resolves a bare directory import (import { x } from "." / "./") to index.ts (#629)', async () => {
  2073. // `import { helper } from '.'` (or './') must map to the
  2074. // directory's index.ts before the re-export chase can run. The
  2075. // barrel renames `realHelper` → `helper` so the name-matcher can't
  2076. // mask a path-resolution failure: only the bare-dir resolution +
  2077. // rename chase can connect the edge.
  2078. fs.mkdirSync(path.join(tempDir, 'src'), { recursive: true });
  2079. fs.writeFileSync(
  2080. path.join(tempDir, 'src/util.ts'),
  2081. `export function realHelper(): void {}\n`
  2082. );
  2083. fs.writeFileSync(
  2084. path.join(tempDir, 'src/index.ts'),
  2085. `export { realHelper as helper } from './util';\n`
  2086. );
  2087. fs.writeFileSync(
  2088. path.join(tempDir, 'src/main.ts'),
  2089. `import { helper } from '.';\nexport function go(): void { helper(); }\n`
  2090. );
  2091. fs.writeFileSync(
  2092. path.join(tempDir, 'src/main2.ts'),
  2093. `import { helper } from './';\nexport function go2(): void { helper(); }\n`
  2094. );
  2095. cg = await CodeGraph.init(tempDir, { index: true });
  2096. cg.resolveReferences();
  2097. const helperNode = cg
  2098. .getNodesByKind('function')
  2099. .find((n) => n.name === 'realHelper' && n.filePath === 'src/util.ts');
  2100. expect(helperNode).toBeDefined();
  2101. const callers = cg.getCallers(helperNode!.id);
  2102. expect(callers.some((c) => c.node.filePath === 'src/main.ts')).toBe(true);
  2103. expect(callers.some((c) => c.node.filePath === 'src/main2.ts')).toBe(true);
  2104. });
  2105. it('resolves a workspace package-subpath barrel (@scope/pkg/sub) to its index (#629)', async () => {
  2106. // bun/npm/pnpm workspace: `@scope/ui/widgets` → the `ui` package's
  2107. // `widgets/` subdir index, which re-exports a .svelte component.
  2108. // Alias `Thing` ≠ component `Widget` defeats the name-matcher, so
  2109. // only workspace-package resolution can connect the edge.
  2110. fs.mkdirSync(path.join(tempDir, 'packages/ui/widgets'), { recursive: true });
  2111. fs.writeFileSync(
  2112. path.join(tempDir, 'package.json'),
  2113. JSON.stringify({ name: 'root', private: true, workspaces: ['packages/*'] }, null, 2)
  2114. );
  2115. fs.writeFileSync(
  2116. path.join(tempDir, 'packages/ui/package.json'),
  2117. JSON.stringify({ name: '@scope/ui', version: '1.0.0' }, null, 2)
  2118. );
  2119. fs.writeFileSync(
  2120. path.join(tempDir, 'packages/ui/widgets/Widget.svelte'),
  2121. `<script lang="ts">\n export let label: string = '';\n</script>\n\n<button>{label}</button>\n`
  2122. );
  2123. fs.writeFileSync(
  2124. path.join(tempDir, 'packages/ui/widgets/index.ts'),
  2125. `export { default as Thing } from './Widget.svelte';\n`
  2126. );
  2127. fs.mkdirSync(path.join(tempDir, 'app'), { recursive: true });
  2128. fs.writeFileSync(
  2129. path.join(tempDir, 'app/App.svelte'),
  2130. `<script lang="ts">\n import { Thing } from '@scope/ui/widgets';\n</script>\n\n<Thing />\n`
  2131. );
  2132. cg = await CodeGraph.init(tempDir, { index: true });
  2133. cg.resolveReferences();
  2134. const buttonNode = cg
  2135. .getNodesByKind('component')
  2136. .find((n) => n.name === 'Widget' && n.filePath === 'packages/ui/widgets/Widget.svelte');
  2137. expect(buttonNode).toBeDefined();
  2138. const callers = cg.getCallers(buttonNode!.id);
  2139. expect(callers.some((c) => c.node.filePath === 'app/App.svelte')).toBe(true);
  2140. });
  2141. it('resolves a barrel import from a Vue SFC <script> block (#629)', async () => {
  2142. // The same import-resolution gaps (no SFC import mappings, no SFC
  2143. // extension list, barrel parsed in the consumer's language) broke
  2144. // Vue SFCs too. Guards the resolver-side generalization to `.vue`.
  2145. // The barrel renames `realRun` → `run` so only the import-chase (not
  2146. // the name-matcher) can connect the call.
  2147. fs.mkdirSync(path.join(tempDir, 'src'), { recursive: true });
  2148. fs.writeFileSync(
  2149. path.join(tempDir, 'src/util.ts'),
  2150. `export function realRun(): void {}\n`
  2151. );
  2152. fs.writeFileSync(
  2153. path.join(tempDir, 'src/index.ts'),
  2154. `export { realRun as run } from './util';\n`
  2155. );
  2156. fs.writeFileSync(
  2157. path.join(tempDir, 'src/App.vue'),
  2158. `<script lang="ts">\nimport { run } from './';\nexport default { mounted() { run(); } };\n</script>\n<template><div/></template>\n`
  2159. );
  2160. cg = await CodeGraph.init(tempDir, { index: true });
  2161. cg.resolveReferences();
  2162. const runNode = cg
  2163. .getNodesByKind('function')
  2164. .find((n) => n.name === 'realRun' && n.filePath === 'src/util.ts');
  2165. expect(runNode).toBeDefined();
  2166. const callers = cg.getCallers(runNode!.id);
  2167. expect(callers.some((c) => c.node.filePath === 'src/App.vue')).toBe(true);
  2168. });
  2169. it('follows a Vue component used in a <template> through a default re-export barrel (#629)', async () => {
  2170. // End-to-end Vue analogue of the Svelte case: the leaf is a `.vue`
  2171. // component re-exported under an alias (`Thing`) that differs from its
  2172. // real name (`Widget`), and the consumer uses it ONLY in markup
  2173. // (`<Thing />`). Requires both the new template-tag extraction AND the
  2174. // barrel default-export chase to connect the edge.
  2175. fs.mkdirSync(path.join(tempDir, 'src/lib'), { recursive: true });
  2176. fs.writeFileSync(
  2177. path.join(tempDir, 'src/lib/Widget.vue'),
  2178. `<script setup lang="ts">\ndefineProps<{ label?: string }>();\n</script>\n<template><button>x</button></template>\n`
  2179. );
  2180. fs.writeFileSync(
  2181. path.join(tempDir, 'src/lib/index.ts'),
  2182. `export { default as Thing } from './Widget.vue';\n`
  2183. );
  2184. fs.writeFileSync(
  2185. path.join(tempDir, 'src/App.vue'),
  2186. `<script setup lang="ts">\nimport { Thing } from './lib';\n</script>\n<template>\n <Thing />\n</template>\n`
  2187. );
  2188. cg = await CodeGraph.init(tempDir, { index: true });
  2189. cg.resolveReferences();
  2190. const widgetNode = cg
  2191. .getNodesByKind('component')
  2192. .find((n) => n.name === 'Widget' && n.filePath === 'src/lib/Widget.vue');
  2193. expect(widgetNode).toBeDefined();
  2194. const callers = cg.getCallers(widgetNode!.id);
  2195. expect(callers.some((c) => c.node.filePath === 'src/App.vue')).toBe(true);
  2196. });
  2197. });
  2198. describe('C/C++ Import Resolution', () => {
  2199. afterEach(() => {
  2200. clearCppIncludeDirCache();
  2201. });
  2202. it('should resolve C include to header in same directory', () => {
  2203. const context: ResolutionContext = {
  2204. getNodesInFile: () => [],
  2205. getNodesByName: () => [],
  2206. getNodesByQualifiedName: () => [],
  2207. getNodesByKind: () => [],
  2208. fileExists: (p) => p === 'utils.h',
  2209. readFile: () => null,
  2210. getProjectRoot: () => '',
  2211. getAllFiles: () => ['utils.h', 'main.c'],
  2212. };
  2213. const result = resolveImportPath(
  2214. 'utils.h',
  2215. 'main.c',
  2216. 'c',
  2217. context
  2218. );
  2219. expect(result).toBe('utils.h');
  2220. });
  2221. it('should resolve C++ include with .hpp extension', () => {
  2222. const context: ResolutionContext = {
  2223. getNodesInFile: () => [],
  2224. getNodesByName: () => [],
  2225. getNodesByQualifiedName: () => [],
  2226. getNodesByKind: () => [],
  2227. fileExists: (p) => p === 'include/myclass.hpp',
  2228. readFile: () => null,
  2229. getProjectRoot: () => '',
  2230. getAllFiles: () => ['include/myclass.hpp', 'src/main.cpp'],
  2231. getCppIncludeDirs: () => ['include'],
  2232. };
  2233. const result = resolveImportPath(
  2234. 'myclass.hpp',
  2235. 'src/main.cpp',
  2236. 'cpp',
  2237. context
  2238. );
  2239. expect(result).toBe('include/myclass.hpp');
  2240. });
  2241. it('should resolve include with subdirectory path', () => {
  2242. const context: ResolutionContext = {
  2243. getNodesInFile: () => [],
  2244. getNodesByName: () => [],
  2245. getNodesByQualifiedName: () => [],
  2246. getNodesByKind: () => [],
  2247. fileExists: (p) => p === 'utils/helpers.h',
  2248. readFile: () => null,
  2249. getProjectRoot: () => '',
  2250. getAllFiles: () => ['utils/helpers.h', 'main.c'],
  2251. };
  2252. const result = resolveImportPath(
  2253. 'utils/helpers.h',
  2254. 'main.c',
  2255. 'c',
  2256. context
  2257. );
  2258. expect(result).toBe('utils/helpers.h');
  2259. });
  2260. it('should resolve include via include directories', () => {
  2261. const context: ResolutionContext = {
  2262. getNodesInFile: () => [],
  2263. getNodesByName: () => [],
  2264. getNodesByQualifiedName: () => [],
  2265. getNodesByKind: () => [],
  2266. fileExists: (p) => p === 'include/myheader.h',
  2267. readFile: () => null,
  2268. getProjectRoot: () => '',
  2269. getAllFiles: () => ['include/myheader.h', 'src/main.cpp'],
  2270. getCppIncludeDirs: () => ['include'],
  2271. };
  2272. const result = resolveImportPath(
  2273. 'myheader.h',
  2274. 'src/main.cpp',
  2275. 'cpp',
  2276. context
  2277. );
  2278. expect(result).toBe('include/myheader.h');
  2279. });
  2280. it('should resolve include trying multiple extensions', () => {
  2281. const context: ResolutionContext = {
  2282. getNodesInFile: () => [],
  2283. getNodesByName: () => [],
  2284. getNodesByQualifiedName: () => [],
  2285. getNodesByKind: () => [],
  2286. // myclass.h does not exist, but myclass.hpp does
  2287. fileExists: (p) => p === 'include/myclass.hpp',
  2288. readFile: () => null,
  2289. getProjectRoot: () => '',
  2290. getAllFiles: () => ['include/myclass.hpp', 'src/main.cpp'],
  2291. getCppIncludeDirs: () => ['include'],
  2292. };
  2293. const result = resolveImportPath(
  2294. 'myclass',
  2295. 'src/main.cpp',
  2296. 'cpp',
  2297. context
  2298. );
  2299. expect(result).toBe('include/myclass.hpp');
  2300. });
  2301. it('should return null for system headers', () => {
  2302. const context: ResolutionContext = {
  2303. getNodesInFile: () => [],
  2304. getNodesByName: () => [],
  2305. getNodesByQualifiedName: () => [],
  2306. getNodesByKind: () => [],
  2307. fileExists: () => true,
  2308. readFile: () => null,
  2309. getProjectRoot: () => '',
  2310. getAllFiles: () => [],
  2311. };
  2312. // C standard library header
  2313. expect(resolveImportPath('stdio.h', 'main.c', 'c', context)).toBeNull();
  2314. // C++ standard library header
  2315. expect(resolveImportPath('vector', 'main.cpp', 'cpp', context)).toBeNull();
  2316. // C++ C-wrapper header
  2317. expect(resolveImportPath('cstdio', 'main.cpp', 'cpp', context)).toBeNull();
  2318. });
  2319. it('should return null for single-component third-party paths that cannot be resolved', () => {
  2320. const context: ResolutionContext = {
  2321. getNodesInFile: () => [],
  2322. getNodesByName: () => [],
  2323. getNodesByQualifiedName: () => [],
  2324. getNodesByKind: () => [],
  2325. fileExists: () => false,
  2326. readFile: () => null,
  2327. getProjectRoot: () => '',
  2328. getAllFiles: () => [],
  2329. getCppIncludeDirs: () => [],
  2330. };
  2331. // Third-party bare header without path — not resolvable, returns null
  2332. const result = resolveImportPath(
  2333. 'openssl/ssl.h',
  2334. 'main.cpp',
  2335. 'cpp',
  2336. context
  2337. );
  2338. expect(result).toBeNull();
  2339. });
  2340. it('should not filter project headers with path separators', () => {
  2341. const context: ResolutionContext = {
  2342. getNodesInFile: () => [],
  2343. getNodesByName: () => [],
  2344. getNodesByQualifiedName: () => [],
  2345. getNodesByKind: () => [],
  2346. fileExists: (p) => p === 'mylib/utils.h',
  2347. readFile: () => null,
  2348. getProjectRoot: () => '',
  2349. getAllFiles: () => ['mylib/utils.h'],
  2350. };
  2351. // Path with separator should NOT be filtered as external
  2352. const result = resolveImportPath(
  2353. 'mylib/utils.h',
  2354. 'main.c',
  2355. 'c',
  2356. context
  2357. );
  2358. expect(result).toBe('mylib/utils.h');
  2359. });
  2360. it('should extract C/C++ import mappings from #include directives', () => {
  2361. const code = `#include <iostream>
  2362. #include "myheader.h"
  2363. #include "utils/helpers.hpp"`;
  2364. const mappings = extractImportMappings('main.cpp', code, 'cpp');
  2365. expect(mappings.length).toBe(3);
  2366. expect(mappings[0]).toEqual({
  2367. localName: 'iostream',
  2368. exportedName: '*',
  2369. source: 'iostream',
  2370. isDefault: false,
  2371. isNamespace: true,
  2372. });
  2373. expect(mappings[1]).toEqual({
  2374. localName: 'myheader',
  2375. exportedName: '*',
  2376. source: 'myheader.h',
  2377. isDefault: false,
  2378. isNamespace: true,
  2379. });
  2380. expect(mappings[2]).toEqual({
  2381. localName: 'helpers',
  2382. exportedName: '*',
  2383. source: 'utils/helpers.hpp',
  2384. isDefault: false,
  2385. isNamespace: true,
  2386. });
  2387. });
  2388. it('should discover include directories from compile_commands.json', () => {
  2389. // Create a temp project with compile_commands.json
  2390. const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-cpp-test-'));
  2391. try {
  2392. const compileDb = [
  2393. {
  2394. directory: tempProject,
  2395. command: 'g++ -Iinclude -Isrc/lib -isystem /usr/include -c src/main.cpp',
  2396. file: 'src/main.cpp',
  2397. },
  2398. ];
  2399. fs.writeFileSync(
  2400. path.join(tempProject, 'compile_commands.json'),
  2401. JSON.stringify(compileDb)
  2402. );
  2403. // Create the include dirs so they exist
  2404. fs.mkdirSync(path.join(tempProject, 'include'), { recursive: true });
  2405. fs.mkdirSync(path.join(tempProject, 'src', 'lib'), { recursive: true });
  2406. clearCppIncludeDirCache();
  2407. const dirs = loadCppIncludeDirs(tempProject);
  2408. // Should find include and src/lib (relative to project root)
  2409. // /usr/include is absolute and outside project, should be excluded
  2410. expect(dirs).toContain('include');
  2411. expect(dirs).toContain('src/lib');
  2412. expect(dirs.some(d => d.includes('usr'))).toBe(false);
  2413. } finally {
  2414. fs.rmSync(tempProject, { recursive: true });
  2415. }
  2416. });
  2417. it('should fall back to heuristic include dirs when no compile_commands.json', () => {
  2418. const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-cpp-test-'));
  2419. try {
  2420. // Create include/ and src/ directories with headers
  2421. fs.mkdirSync(path.join(tempProject, 'include'), { recursive: true });
  2422. fs.writeFileSync(path.join(tempProject, 'include', 'types.h'), '');
  2423. fs.mkdirSync(path.join(tempProject, 'src'), { recursive: true });
  2424. fs.writeFileSync(path.join(tempProject, 'src', 'main.cpp'), '');
  2425. // Create a directory without headers — should not be included
  2426. fs.mkdirSync(path.join(tempProject, 'docs'), { recursive: true });
  2427. clearCppIncludeDirCache();
  2428. const dirs = loadCppIncludeDirs(tempProject);
  2429. expect(dirs).toContain('include');
  2430. expect(dirs).toContain('src');
  2431. expect(dirs).not.toContain('docs');
  2432. } finally {
  2433. fs.rmSync(tempProject, { recursive: true });
  2434. }
  2435. });
  2436. // Documents the cross-language `.h` behavior. Objective-C and C++ share
  2437. // the `.h` extension, so in a mixed iOS-style project an Obj-C header
  2438. // dir gets claimed as a C/C++ include dir too. That's intentional — a
  2439. // C++ file legitimately can `#include "Foo.h"` against an Obj-C header
  2440. // (Obj-C++ / .mm callers), and false-positive inclusion is far cheaper
  2441. // than missing real resolutions. The test pins this so a later
  2442. // "exclude objc dirs" refactor breaks loudly and reviewers see the
  2443. // trade-off explicitly.
  2444. it('heuristic claims any top-level dir containing .h files, including Obj-C', () => {
  2445. const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-cpp-test-'));
  2446. try {
  2447. // C++ side: an `cppmod` dir with a .hpp (C++-only extension)
  2448. fs.mkdirSync(path.join(tempProject, 'cppmod'), { recursive: true });
  2449. fs.writeFileSync(path.join(tempProject, 'cppmod', 'shared.hpp'), '');
  2450. // Obj-C side: an `iosmod` dir with .h + .m (no .cpp/.hpp).
  2451. fs.mkdirSync(path.join(tempProject, 'iosmod'), { recursive: true });
  2452. fs.writeFileSync(path.join(tempProject, 'iosmod', 'View.h'), '');
  2453. fs.writeFileSync(path.join(tempProject, 'iosmod', 'View.m'), '');
  2454. clearCppIncludeDirCache();
  2455. const dirs = loadCppIncludeDirs(tempProject);
  2456. // Both included — Obj-C dirs are intentionally allowed.
  2457. expect(dirs).toContain('cppmod');
  2458. expect(dirs).toContain('iosmod');
  2459. } finally {
  2460. fs.rmSync(tempProject, { recursive: true });
  2461. }
  2462. });
  2463. // End-to-end: ensure `#include "X.h"` produces a file→file `imports` edge
  2464. // in the actual indexing pipeline (not just a phantom file→import-node
  2465. // edge). This pins the include-dir resolution path so the headline PR
  2466. // feature can't silently regress to a no-op in the indexing flow.
  2467. it('connects #include to the real header file via include-dir scan (end-to-end)', async () => {
  2468. const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-cpp-e2e-'));
  2469. try {
  2470. fs.mkdirSync(path.join(tempProject, 'include'), { recursive: true });
  2471. fs.mkdirSync(path.join(tempProject, 'src'), { recursive: true });
  2472. fs.writeFileSync(
  2473. path.join(tempProject, 'include', 'utils.h'),
  2474. `#ifndef UTILS_H\n#define UTILS_H\nint add(int, int);\n#endif\n`
  2475. );
  2476. fs.writeFileSync(
  2477. path.join(tempProject, 'src', 'main.cpp'),
  2478. `#include "utils.h"\n#include <vector>\nint main(){ return add(1,2); }\n`
  2479. );
  2480. clearCppIncludeDirCache();
  2481. cg = await CodeGraph.init(tempProject, { index: true });
  2482. // Sanity: file nodes exist for the header and the cpp.
  2483. const allFiles = cg.getStats();
  2484. expect(allFiles.fileCount).toBe(2);
  2485. // The `#include "utils.h"` edge should target the real
  2486. // `include/utils.h` file node — not a floating `import` node
  2487. // living inside main.cpp.
  2488. const db = DatabaseConnection.open(path.join(tempProject, '.codegraph', 'codegraph.db'));
  2489. const rows = db.getDb().prepare(`
  2490. select dst.kind as dstKind, dst.file_path as dstPath
  2491. from edges e
  2492. join nodes src on e.source = src.id
  2493. join nodes dst on e.target = dst.id
  2494. where e.kind = 'imports'
  2495. and src.kind = 'file'
  2496. and src.file_path = 'src/main.cpp'
  2497. `).all() as Array<{ dstKind: string; dstPath: string }>;
  2498. const resolvedToHeader = rows.find(
  2499. (r) => r.dstKind === 'file' && r.dstPath === 'include/utils.h'
  2500. );
  2501. expect(resolvedToHeader, 'main.cpp → include/utils.h imports edge missing').toBeDefined();
  2502. // `<vector>` should NOT produce a file edge — it's a stdlib header.
  2503. const stdlibFile = rows.find(
  2504. (r) => r.dstKind === 'file' && r.dstPath && r.dstPath.endsWith('vector')
  2505. );
  2506. expect(stdlibFile).toBeUndefined();
  2507. } finally {
  2508. fs.rmSync(tempProject, { recursive: true, force: true });
  2509. }
  2510. });
  2511. });
  2512. describe('C++ templated base-class inheritance (#1043)', () => {
  2513. // A class deriving from a TEMPLATE — `class D : public Base<int>` (or a CRTP
  2514. // `class W : public CRTPBase<W>`, or a qualified `class Q : public ns::Tpl<int>`)
  2515. // recorded its base as the full instantiation text (`Base<int>`), which never
  2516. // name-matched the template, indexed as the bare node `Base`. The `<…>` args
  2517. // are now stripped so the `extends` edge resolves end-to-end.
  2518. it('resolves an extends edge to a templated base (plain, CRTP, struct, multi-base)', async () => {
  2519. fs.writeFileSync(
  2520. path.join(tempDir, 'lib.hpp'),
  2521. `#pragma once
  2522. template<typename T> class Base { public: void foo(); };
  2523. template<typename Derived> class CRTPBase {};
  2524. class Plain {};
  2525. class Widget : public Base<int> {}; // plain template base
  2526. class App : public CRTPBase<App> {}; // CRTP (curiously-recurring)
  2527. struct Node : public Base<double> {}; // struct inheriting a template
  2528. class Both : public Base<char>, public Plain {}; // templated + plain in one clause
  2529. `
  2530. );
  2531. cg = await CodeGraph.init(tempDir, { index: true });
  2532. const db = DatabaseConnection.open(path.join(tempDir, '.codegraph', 'codegraph.db'));
  2533. const edges = db
  2534. .getDb()
  2535. .prepare(
  2536. `select src.name as fromName, dst.name as toName
  2537. from edges e
  2538. join nodes src on e.source = src.id
  2539. join nodes dst on e.target = dst.id
  2540. where e.kind = 'extends'`
  2541. )
  2542. .all() as Array<{ fromName: string; toName: string }>;
  2543. const has = (from: string, to: string) =>
  2544. edges.some((r) => r.fromName === from && r.toName === to);
  2545. // Every templated base now resolves to the bare template node.
  2546. expect(has('Widget', 'Base'), 'Widget : Base<int>').toBe(true);
  2547. expect(has('App', 'CRTPBase'), 'App : CRTPBase<App> (CRTP)').toBe(true);
  2548. expect(has('Node', 'Base'), 'struct Node : Base<double>').toBe(true);
  2549. // A mixed clause resolves BOTH the templated and the plain base.
  2550. expect(has('Both', 'Base'), 'Both : Base<char>').toBe(true);
  2551. expect(has('Both', 'Plain'), 'Both : Plain (non-templated, regression guard)').toBe(true);
  2552. });
  2553. });
  2554. describe('PHP Include Resolution', () => {
  2555. it('isPhpIncludePathRef distinguishes include paths from namespace use (#660)', () => {
  2556. const mk = (name: string, over: Partial<UnresolvedRef> = {}): UnresolvedRef => ({
  2557. fromNodeId: 'f', referenceName: name, referenceKind: 'imports',
  2558. line: 1, column: 0, filePath: 'x.php', language: 'php', ...over,
  2559. });
  2560. // include paths: contain a slash or a file extension
  2561. expect(isPhpIncludePathRef(mk('lib.php'))).toBe(true);
  2562. expect(isPhpIncludePathRef(mk('inc/db.php'))).toBe(true);
  2563. expect(isPhpIncludePathRef(mk('../config.php'))).toBe(true);
  2564. // namespace use symbols: a bare class (Closure) or FQN — never a path,
  2565. // so they must NOT be treated as includes (would mis-connect to a
  2566. // same-named Closure.php / Bar.php file).
  2567. expect(isPhpIncludePathRef(mk('Closure'))).toBe(false);
  2568. expect(isPhpIncludePathRef(mk('PDO'))).toBe(false);
  2569. expect(isPhpIncludePathRef(mk('App\\Foo\\Bar'))).toBe(false);
  2570. // scoped to PHP imports only
  2571. expect(isPhpIncludePathRef(mk('lib.php', { language: 'c' }))).toBe(false);
  2572. expect(isPhpIncludePathRef(mk('lib.php', { referenceKind: 'calls' }))).toBe(false);
  2573. });
  2574. it('resolves require_once to a file→file imports edge (#660)', async () => {
  2575. const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-php-e2e-'));
  2576. try {
  2577. fs.mkdirSync(path.join(tempProject, 'src'), { recursive: true });
  2578. fs.writeFileSync(
  2579. path.join(tempProject, 'src', 'lib.php'),
  2580. `<?php\nfunction greet() { return "hi"; }\n`
  2581. );
  2582. fs.writeFileSync(
  2583. path.join(tempProject, 'src', 'page.php'),
  2584. `<?php\nrequire_once("lib.php");\necho greet();\n`
  2585. );
  2586. cg = await CodeGraph.init(tempProject, { index: true });
  2587. // reporter's repro: page.php's `require_once("lib.php")` must resolve
  2588. // to the real src/lib.php file node — a file→file `imports` edge, so
  2589. // callers(lib.php) now includes page.php.
  2590. const db = DatabaseConnection.open(path.join(tempProject, '.codegraph', 'codegraph.db'));
  2591. const rows = db.getDb().prepare(`
  2592. select dst.kind as dstKind, dst.file_path as dstPath
  2593. from edges e
  2594. join nodes src on e.source = src.id
  2595. join nodes dst on e.target = dst.id
  2596. where e.kind = 'imports'
  2597. and src.kind = 'file'
  2598. and src.file_path = 'src/page.php'
  2599. `).all() as Array<{ dstKind: string; dstPath: string }>;
  2600. const resolved = rows.find(
  2601. (r) => r.dstKind === 'file' && r.dstPath === 'src/lib.php'
  2602. );
  2603. expect(resolved, 'page.php → src/lib.php imports edge missing').toBeDefined();
  2604. } finally {
  2605. fs.rmSync(tempProject, { recursive: true, force: true });
  2606. }
  2607. });
  2608. it('resolves a subdirectory include path to the correct file (#660)', async () => {
  2609. const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-php-subdir-'));
  2610. try {
  2611. fs.mkdirSync(path.join(tempProject, 'inc'), { recursive: true });
  2612. fs.writeFileSync(
  2613. path.join(tempProject, 'inc', 'db.php'),
  2614. `<?php\nfunction query() { return 1; }\n`
  2615. );
  2616. fs.writeFileSync(
  2617. path.join(tempProject, 'index.php'),
  2618. `<?php\nrequire "inc/db.php";\nquery();\n`
  2619. );
  2620. cg = await CodeGraph.init(tempProject, { index: true });
  2621. const db = DatabaseConnection.open(path.join(tempProject, '.codegraph', 'codegraph.db'));
  2622. const rows = db.getDb().prepare(`
  2623. select dst.kind as dstKind, dst.file_path as dstPath
  2624. from edges e
  2625. join nodes src on e.source = src.id
  2626. join nodes dst on e.target = dst.id
  2627. where e.kind = 'imports'
  2628. and src.kind = 'file'
  2629. and src.file_path = 'index.php'
  2630. `).all() as Array<{ dstKind: string; dstPath: string }>;
  2631. expect(
  2632. rows.find((r) => r.dstKind === 'file' && r.dstPath === 'inc/db.php'),
  2633. 'index.php → inc/db.php imports edge missing'
  2634. ).toBeDefined();
  2635. } finally {
  2636. fs.rmSync(tempProject, { recursive: true, force: true });
  2637. }
  2638. });
  2639. it('does not mis-connect an unresolvable include to a same-named file elsewhere (#660)', async () => {
  2640. const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-php-misresolve-'));
  2641. try {
  2642. // app/page.php's `require "inc/db.php"` resolves relative to app/, where
  2643. // inc/db.php does NOT exist. A same-named lib/inc/db.php exists elsewhere
  2644. // but is unrelated — no edge should be created (a wrong edge is worse
  2645. // than a missing one).
  2646. fs.mkdirSync(path.join(tempProject, 'app'), { recursive: true });
  2647. fs.mkdirSync(path.join(tempProject, 'lib', 'inc'), { recursive: true });
  2648. fs.writeFileSync(
  2649. path.join(tempProject, 'lib', 'inc', 'db.php'),
  2650. `<?php\nfunction unrelated() {}\n`
  2651. );
  2652. fs.writeFileSync(
  2653. path.join(tempProject, 'app', 'page.php'),
  2654. `<?php\nrequire "inc/db.php";\n`
  2655. );
  2656. cg = await CodeGraph.init(tempProject, { index: true });
  2657. const db = DatabaseConnection.open(path.join(tempProject, '.codegraph', 'codegraph.db'));
  2658. const rows = db.getDb().prepare(`
  2659. select dst.kind as dstKind, dst.file_path as dstPath
  2660. from edges e
  2661. join nodes src on e.source = src.id
  2662. join nodes dst on e.target = dst.id
  2663. where e.kind = 'imports'
  2664. and src.kind = 'file'
  2665. and src.file_path = 'app/page.php'
  2666. `).all() as Array<{ dstKind: string; dstPath: string }>;
  2667. expect(
  2668. rows.find((r) => r.dstKind === 'file' && r.dstPath === 'lib/inc/db.php'),
  2669. 'app/page.php must NOT mis-connect to unrelated lib/inc/db.php'
  2670. ).toBeUndefined();
  2671. } finally {
  2672. fs.rmSync(tempProject, { recursive: true, force: true });
  2673. }
  2674. });
  2675. });
  2676. describe('C++ chained-call receiver resolution (#645)', () => {
  2677. async function indexCpp(files: Record<string, string>): Promise<void> {
  2678. for (const [name, content] of Object.entries(files)) {
  2679. fs.writeFileSync(path.join(tempDir, name), content);
  2680. }
  2681. cg = await CodeGraph.init(tempDir, { index: true });
  2682. }
  2683. function callerNamesOf(qualifiedName: string): string[] {
  2684. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  2685. if (!target) return [];
  2686. const names = cg
  2687. .getIncomingEdges(target.id)
  2688. .filter((e) => e.kind === 'calls')
  2689. .map((e) => cg.getNode(e.source)?.name)
  2690. .filter((n): n is string => !!n);
  2691. return [...new Set(names)].sort();
  2692. }
  2693. it('resolves singleton chains and auto locals to the right class, never the first-sorted one', async () => {
  2694. // Two classes share writeLog; Logger sorts first so it wins any name-only
  2695. // tie. All three call forms target Metrics.
  2696. await indexCpp({
  2697. 'logger.hpp': `#pragma once
  2698. #include <string>
  2699. class Logger { public: static Logger& instance(); void writeLog(const std::string&); };
  2700. class Metrics { public: static Metrics& instance(); void writeLog(const std::string&); };
  2701. `,
  2702. 'impl.cpp': `#include "logger.hpp"
  2703. Logger& Logger::instance() { static Logger l; return l; }
  2704. Metrics& Metrics::instance() { static Metrics m; return m; }
  2705. void Logger::writeLog(const std::string&) {}
  2706. void Metrics::writeLog(const std::string&) {}
  2707. `,
  2708. 'app.cpp': `#include "logger.hpp"
  2709. void a() { Metrics::instance().writeLog("x"); } // chained singleton
  2710. void b() { auto& m = Metrics::instance(); m.writeLog("x"); } // stored in auto
  2711. void c() { Metrics& m = Metrics::instance(); m.writeLog("x"); } // explicit type
  2712. `,
  2713. });
  2714. expect(callerNamesOf('Metrics::writeLog')).toEqual(['a', 'b', 'c']);
  2715. expect(callerNamesOf('Logger::writeLog')).toEqual([]);
  2716. });
  2717. it('resolves factories, free-function factories, and member chains via the inner call return type', async () => {
  2718. await indexCpp({
  2719. 'types.hpp': `#pragma once
  2720. #include <memory>
  2721. struct Widget { void draw(); };
  2722. struct Session { void run(); };
  2723. struct View { void render(); };
  2724. class WidgetFactory { public: static Widget create(); };
  2725. class Manager { public: View view(); };
  2726. Session* openSession();
  2727. // Decoy that sorts first and has all three methods — must never win.
  2728. struct Aaa { void draw(); void run(); void render(); };
  2729. `,
  2730. 'impl.cpp': `#include "types.hpp"
  2731. void Widget::draw() {}
  2732. void Session::run() {}
  2733. void View::render() {}
  2734. void Aaa::draw() {}
  2735. void Aaa::run() {}
  2736. void Aaa::render() {}
  2737. Widget WidgetFactory::create() { return Widget(); }
  2738. View Manager::view() { return View(); }
  2739. Session* openSession() { return nullptr; }
  2740. `,
  2741. 'app.cpp': `#include "types.hpp"
  2742. void factory() { WidgetFactory::create().draw(); } // -> Widget::draw
  2743. void freefunc() { openSession()->run(); } // -> Session::run
  2744. void member() { Manager mgr; mgr.view().render(); } // -> View::render
  2745. void makeUnique() { auto w = std::make_unique<Widget>(); w->draw(); } // -> Widget::draw
  2746. `,
  2747. });
  2748. expect(callerNamesOf('Widget::draw')).toEqual(['factory', 'makeUnique']);
  2749. expect(callerNamesOf('Session::run')).toEqual(['freefunc']);
  2750. expect(callerNamesOf('View::render')).toEqual(['member']);
  2751. // The first-sorted decoy never captures any of them.
  2752. expect(callerNamesOf('Aaa::draw')).toEqual([]);
  2753. expect(callerNamesOf('Aaa::run')).toEqual([]);
  2754. expect(callerNamesOf('Aaa::render')).toEqual([]);
  2755. });
  2756. it('creates NO edge when the inferred type lacks the method (silent miss, not a wrong edge)', async () => {
  2757. await indexCpp({
  2758. 'types.hpp': `#pragma once
  2759. struct Widget { void draw(); };
  2760. struct Other { void onlyOther(); };
  2761. class WidgetFactory { public: static Widget create(); };
  2762. `,
  2763. 'impl.cpp': `#include "types.hpp"
  2764. void Widget::draw() {}
  2765. void Other::onlyOther() {}
  2766. Widget WidgetFactory::create() { return Widget(); }
  2767. `,
  2768. 'app.cpp': `#include "types.hpp"
  2769. // Widget has no onlyOther() — must produce NO edge, never a wrong one to Other.
  2770. void wrong() { WidgetFactory::create().onlyOther(); }
  2771. `,
  2772. });
  2773. expect(callerNamesOf('Other::onlyOther')).toEqual([]);
  2774. });
  2775. });
  2776. describe('PHP chained static-factory call resolution (#608)', () => {
  2777. function callerNamesOf(qualifiedName: string): string[] {
  2778. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  2779. if (!target) return [];
  2780. const names = cg
  2781. .getIncomingEdges(target.id)
  2782. .filter((e) => e.kind === 'calls')
  2783. .map((e) => cg.getNode(e.source)?.name)
  2784. .filter((n): n is string => !!n);
  2785. return [...new Set(names)].sort();
  2786. }
  2787. it('resolves Cls::for($x)->method() via the factory\'s `: self` return (#608)', async () => {
  2788. fs.writeFileSync(
  2789. path.join(tempDir, 'ApiClient.php'),
  2790. `<?php\nclass ApiClient {\n public static function for(string $c): self { return new self; }\n public function createOrder(array $p): array { return []; }\n}\n`
  2791. );
  2792. fs.writeFileSync(
  2793. path.join(tempDir, 'DispatchOrder.php'),
  2794. `<?php\nclass DispatchOrder {\n public function handle(): void {\n ApiClient::for('cred')->createOrder([]);\n }\n}\n`
  2795. );
  2796. cg = await CodeGraph.init(tempDir, { index: true });
  2797. // The chained call's edge attaches to the factory result's method.
  2798. expect(callerNamesOf('ApiClient::createOrder')).toContain('handle');
  2799. });
  2800. it('creates NO edge when the factory result lacks the method (#608)', async () => {
  2801. fs.writeFileSync(
  2802. path.join(tempDir, 'lib.php'),
  2803. `<?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`
  2804. );
  2805. cg = await CodeGraph.init(tempDir, { index: true });
  2806. // ApiClient has no onlyOther — must not mis-attach to the same-named Other::onlyOther.
  2807. expect(callerNamesOf('Other::onlyOther')).toEqual([]);
  2808. });
  2809. });
  2810. describe('Java chained static-factory call resolution (#645/#608 mechanism)', () => {
  2811. function callerNamesOf(qualifiedName: string): string[] {
  2812. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  2813. if (!target) return [];
  2814. const names = cg
  2815. .getIncomingEdges(target.id)
  2816. .filter((e) => e.kind === 'calls')
  2817. .map((e) => cg.getNode(e.source)?.name)
  2818. .filter((n): n is string => !!n);
  2819. return [...new Set(names)].sort();
  2820. }
  2821. it('resolves Foo.getInstance().bar() via the factory return type, never a same-named decoy', async () => {
  2822. // Aaa sorts first and has a same-named bar() — it must never win the chain.
  2823. fs.writeFileSync(
  2824. path.join(tempDir, 'Main.java'),
  2825. `class Aaa { void bar() {} }
  2826. class Foo {
  2827. static Foo getInstance() { return new Foo(); }
  2828. void bar() {}
  2829. }
  2830. class Caller {
  2831. void run() { Foo.getInstance().bar(); }
  2832. }
  2833. `
  2834. );
  2835. cg = await CodeGraph.init(tempDir, { index: true });
  2836. expect(callerNamesOf('Foo::bar')).toEqual(['run']);
  2837. expect(callerNamesOf('Aaa::bar')).toEqual([]);
  2838. });
  2839. it('resolves a factory chain that passes arguments — Foo.create(cfg).build()', async () => {
  2840. // The factory call carries an argument; the extractor must normalize the
  2841. // receiver to empty parens (`Foo.create().build`) so the chain still splits.
  2842. fs.writeFileSync(
  2843. path.join(tempDir, 'Main.java'),
  2844. `class Config {}
  2845. class Foo {
  2846. static Foo create(Config c) { return new Foo(); }
  2847. void build() {}
  2848. }
  2849. class Caller {
  2850. void run() { Foo.create(new Config()).build(); }
  2851. }
  2852. `
  2853. );
  2854. cg = await CodeGraph.init(tempDir, { index: true });
  2855. expect(callerNamesOf('Foo::build')).toEqual(['run']);
  2856. });
  2857. it('creates NO edge when the factory return type lacks the method (silent miss, not a wrong edge)', async () => {
  2858. fs.writeFileSync(
  2859. path.join(tempDir, 'Main.java'),
  2860. `class Foo {
  2861. static Foo getInstance() { return new Foo(); }
  2862. }
  2863. class Other { void onlyOther() {} }
  2864. class Caller {
  2865. void run() { Foo.getInstance().onlyOther(); }
  2866. }
  2867. `
  2868. );
  2869. cg = await CodeGraph.init(tempDir, { index: true });
  2870. // Foo has no onlyOther() — must not mis-attach to the same-named Other::onlyOther.
  2871. expect(callerNamesOf('Other::onlyOther')).toEqual([]);
  2872. });
  2873. });
  2874. describe('Kotlin chained companion-factory call resolution (#645/#608 mechanism)', () => {
  2875. function callerNamesOf(qualifiedName: string): string[] {
  2876. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  2877. if (!target) return [];
  2878. const names = cg
  2879. .getIncomingEdges(target.id)
  2880. .filter((e) => e.kind === 'calls')
  2881. .map((e) => cg.getNode(e.source)?.name)
  2882. .filter((n): n is string => !!n);
  2883. return [...new Set(names)].sort();
  2884. }
  2885. it('resolves Foo.getInstance().bar() via the companion return type, never a same-named decoy', async () => {
  2886. // Aaa sorts first and has a same-named bar() — without the chain fix Kotlin
  2887. // dropped the receiver to a bare `bar` and attached to Aaa (a wrong edge).
  2888. fs.writeFileSync(
  2889. path.join(tempDir, 'Main.kt'),
  2890. `class Aaa { fun bar() {} }
  2891. class Foo {
  2892. companion object {
  2893. fun getInstance(): Foo = Foo()
  2894. }
  2895. fun bar() {}
  2896. }
  2897. class Caller {
  2898. fun run() { Foo.getInstance().bar() }
  2899. }
  2900. `
  2901. );
  2902. cg = await CodeGraph.init(tempDir, { index: true });
  2903. expect(callerNamesOf('Foo::bar')).toEqual(['run']);
  2904. expect(callerNamesOf('Aaa::bar')).toEqual([]);
  2905. });
  2906. it('resolves a companion factory chain that passes arguments — Foo.create(cfg).build()', async () => {
  2907. fs.writeFileSync(
  2908. path.join(tempDir, 'Main.kt'),
  2909. `class Config
  2910. class Foo {
  2911. companion object {
  2912. fun create(c: Config): Foo = Foo()
  2913. }
  2914. fun build() {}
  2915. }
  2916. class Caller {
  2917. fun run() { Foo.create(Config()).build() }
  2918. }
  2919. `
  2920. );
  2921. cg = await CodeGraph.init(tempDir, { index: true });
  2922. expect(callerNamesOf('Foo::build')).toEqual(['run']);
  2923. });
  2924. it('creates NO edge when the companion return type lacks the method (silent miss, not a wrong edge)', async () => {
  2925. fs.writeFileSync(
  2926. path.join(tempDir, 'Main.kt'),
  2927. `class Foo {
  2928. companion object {
  2929. fun getInstance(): Foo = Foo()
  2930. }
  2931. }
  2932. class Other { fun onlyOther() {} }
  2933. class Caller {
  2934. fun run() { Foo.getInstance().onlyOther() }
  2935. }
  2936. `
  2937. );
  2938. cg = await CodeGraph.init(tempDir, { index: true });
  2939. // Foo has no onlyOther() — must not mis-attach to the same-named Other::onlyOther.
  2940. expect(callerNamesOf('Other::onlyOther')).toEqual([]);
  2941. });
  2942. });
  2943. describe('C# chained static-factory call resolution (#645/#608 mechanism)', () => {
  2944. function callerNamesOf(qualifiedName: string): string[] {
  2945. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  2946. if (!target) return [];
  2947. const names = cg
  2948. .getIncomingEdges(target.id)
  2949. .filter((e) => e.kind === 'calls')
  2950. .map((e) => cg.getNode(e.source)?.name)
  2951. .filter((n): n is string => !!n);
  2952. return [...new Set(names)].sort();
  2953. }
  2954. it('resolves Foo.Create().Bar() via the factory return type, never a same-named decoy', async () => {
  2955. // Aaa sorts first and has a same-named Bar() — it must never win the chain.
  2956. fs.writeFileSync(
  2957. path.join(tempDir, 'Main.cs'),
  2958. `class Aaa { void Bar() {} }
  2959. class Foo {
  2960. static Foo Create() { return new Foo(); }
  2961. void Bar() {}
  2962. }
  2963. class Caller {
  2964. void Run() { Foo.Create().Bar(); }
  2965. }
  2966. `
  2967. );
  2968. cg = await CodeGraph.init(tempDir, { index: true });
  2969. expect(callerNamesOf('Foo::Bar')).toEqual(['Run']);
  2970. expect(callerNamesOf('Aaa::Bar')).toEqual([]);
  2971. });
  2972. it('resolves a factory chain that passes arguments — Foo.Make(cfg).Build()', async () => {
  2973. fs.writeFileSync(
  2974. path.join(tempDir, 'Main.cs'),
  2975. `class Config {}
  2976. class Foo {
  2977. static Foo Make(Config c) { return new Foo(); }
  2978. void Build() {}
  2979. }
  2980. class Caller {
  2981. void Run() { Foo.Make(new Config()).Build(); }
  2982. }
  2983. `
  2984. );
  2985. cg = await CodeGraph.init(tempDir, { index: true });
  2986. expect(callerNamesOf('Foo::Build')).toEqual(['Run']);
  2987. });
  2988. it('creates NO edge when the factory return type lacks the method (silent miss, not a wrong edge)', async () => {
  2989. fs.writeFileSync(
  2990. path.join(tempDir, 'Main.cs'),
  2991. `class Foo {
  2992. static Foo Create() { return new Foo(); }
  2993. }
  2994. class Other { void OnlyOther() {} }
  2995. class Caller {
  2996. void Run() { Foo.Create().OnlyOther(); }
  2997. }
  2998. `
  2999. );
  3000. cg = await CodeGraph.init(tempDir, { index: true });
  3001. // Foo has no OnlyOther() — must not mis-attach to the same-named Other::OnlyOther.
  3002. expect(callerNamesOf('Other::OnlyOther')).toEqual([]);
  3003. });
  3004. });
  3005. describe('Swift chained static-factory call resolution (#645/#608 mechanism)', () => {
  3006. function callerNamesOf(qualifiedName: string): string[] {
  3007. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  3008. if (!target) return [];
  3009. const names = cg
  3010. .getIncomingEdges(target.id)
  3011. .filter((e) => e.kind === 'calls')
  3012. .map((e) => cg.getNode(e.source)?.name)
  3013. .filter((n): n is string => !!n);
  3014. return [...new Set(names)].sort();
  3015. }
  3016. it('resolves Foo.make().draw() via the factory return type, never a same-named decoy', async () => {
  3017. // Aaa sorts first and has a same-named draw() — without the fix Swift dropped
  3018. // the receiver to a bare `draw` and attached to Aaa (a wrong edge).
  3019. fs.writeFileSync(
  3020. path.join(tempDir, 'Main.swift'),
  3021. `class Aaa { func draw() {} }
  3022. class Foo {
  3023. static func make() -> Foo { return Foo() }
  3024. func draw() {}
  3025. }
  3026. func runCaller() { Foo.make().draw() }
  3027. `
  3028. );
  3029. cg = await CodeGraph.init(tempDir, { index: true });
  3030. expect(callerNamesOf('Foo::draw')).toEqual(['runCaller']);
  3031. expect(callerNamesOf('Aaa::draw')).toEqual([]);
  3032. });
  3033. it('resolves a constructor chain Foo().draw() and an args factory chain Foo.build(c).render()', async () => {
  3034. fs.writeFileSync(
  3035. path.join(tempDir, 'Main.swift'),
  3036. `class Config {}
  3037. class Foo {
  3038. static func build(_ c: Config) -> Foo { return Foo() }
  3039. func draw() {}
  3040. func render() {}
  3041. }
  3042. func runCaller() {
  3043. Foo().draw()
  3044. Foo.build(Config()).render()
  3045. }
  3046. `
  3047. );
  3048. cg = await CodeGraph.init(tempDir, { index: true });
  3049. expect(callerNamesOf('Foo::draw')).toEqual(['runCaller']);
  3050. expect(callerNamesOf('Foo::render')).toEqual(['runCaller']);
  3051. });
  3052. it('creates NO edge when the factory return type lacks the method (silent miss, not a wrong edge)', async () => {
  3053. fs.writeFileSync(
  3054. path.join(tempDir, 'Main.swift'),
  3055. `class Foo {
  3056. static func make() -> Foo { return Foo() }
  3057. }
  3058. class Other { func onlyOther() {} }
  3059. func runCaller() { Foo.make().onlyOther() }
  3060. `
  3061. );
  3062. cg = await CodeGraph.init(tempDir, { index: true });
  3063. // Foo has no onlyOther() — must not mis-attach to the same-named Other::onlyOther.
  3064. expect(callerNamesOf('Other::onlyOther')).toEqual([]);
  3065. });
  3066. });
  3067. describe('Chained call resolves a method on a supertype (conformance, #750)', () => {
  3068. function callerNamesOf(qualifiedName: string): string[] {
  3069. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  3070. if (!target) return [];
  3071. const names = cg
  3072. .getIncomingEdges(target.id)
  3073. .filter((e) => e.kind === 'calls')
  3074. .map((e) => cg.getNode(e.source)?.name)
  3075. .filter((n): n is string => !!n);
  3076. return [...new Set(names)].sort();
  3077. }
  3078. it('resolves a chained method defined only on a SUPERCLASS the return type extends', async () => {
  3079. // draw() lives on Base; Widget (the factory's return type) has no draw() of
  3080. // its own. Decoy.draw must never win. Needs the conformance second pass.
  3081. fs.writeFileSync(
  3082. path.join(tempDir, 'Main.java'),
  3083. `class Base { void draw() {} }
  3084. class Widget extends Base {}
  3085. class Decoy { void draw() {} }
  3086. class Factory { static Widget create() { return new Widget(); } }
  3087. class Caller {
  3088. void run() { Factory.create().draw(); }
  3089. }
  3090. `
  3091. );
  3092. cg = await CodeGraph.init(tempDir, { index: true });
  3093. expect(callerNamesOf('Base::draw')).toEqual(['run']);
  3094. expect(callerNamesOf('Decoy::draw')).toEqual([]);
  3095. });
  3096. it('resolves a chained method defined on an INTERFACE the return type implements (default method)', async () => {
  3097. fs.writeFileSync(
  3098. path.join(tempDir, 'Main.java'),
  3099. `interface Drawable { default void draw() {} }
  3100. class Widget implements Drawable {}
  3101. class Decoy { void draw() {} }
  3102. class Factory { static Widget create() { return new Widget(); } }
  3103. class Caller {
  3104. void run() { Factory.create().draw(); }
  3105. }
  3106. `
  3107. );
  3108. cg = await CodeGraph.init(tempDir, { index: true });
  3109. expect(callerNamesOf('Drawable::draw')).toEqual(['run']);
  3110. expect(callerNamesOf('Decoy::draw')).toEqual([]);
  3111. });
  3112. it('still creates NO edge when no supertype has the method (safety preserved)', async () => {
  3113. fs.writeFileSync(
  3114. path.join(tempDir, 'Main.java'),
  3115. `class Base {}
  3116. class Widget extends Base {}
  3117. class Other { void onlyOther() {} }
  3118. class Factory { static Widget create() { return new Widget(); } }
  3119. class Caller {
  3120. void run() { Factory.create().onlyOther(); }
  3121. }
  3122. `
  3123. );
  3124. cg = await CodeGraph.init(tempDir, { index: true });
  3125. // Neither Widget nor Base has onlyOther() — must not attach to Other::onlyOther.
  3126. expect(callerNamesOf('Other::onlyOther')).toEqual([]);
  3127. });
  3128. });
  3129. describe('Rust chained associated-function call resolution (#645/#608 mechanism)', () => {
  3130. function callerNamesOf(qualifiedName: string): string[] {
  3131. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  3132. if (!target) return [];
  3133. const names = cg
  3134. .getIncomingEdges(target.id)
  3135. .filter((e) => e.kind === 'calls')
  3136. .map((e) => cg.getNode(e.source)?.name)
  3137. .filter((n): n is string => !!n);
  3138. return [...new Set(names)].sort();
  3139. }
  3140. it('resolves Foo::new().bar() (and a Self return) via the associated fn, never a same-named decoy', async () => {
  3141. fs.writeFileSync(
  3142. path.join(tempDir, 'main.rs'),
  3143. `struct Aaa { _x: i32 }
  3144. impl Aaa { fn bar(&self) {} }
  3145. struct Foo { _x: i32 }
  3146. impl Foo {
  3147. fn new() -> Foo { Foo { _x: 0 } }
  3148. fn make() -> Self { Foo { _x: 0 } }
  3149. fn bar(&self) {}
  3150. }
  3151. fn caller() {
  3152. Foo::new().bar();
  3153. Foo::make().bar();
  3154. }
  3155. `
  3156. );
  3157. cg = await CodeGraph.init(tempDir, { index: true });
  3158. expect(callerNamesOf('Foo::bar')).toEqual(['caller']);
  3159. expect(callerNamesOf('Aaa::bar')).toEqual([]);
  3160. });
  3161. it('resolves a chain that passes arguments — Foo::with(c).build()', async () => {
  3162. fs.writeFileSync(
  3163. path.join(tempDir, 'main.rs'),
  3164. `struct Config;
  3165. struct Foo { _x: i32 }
  3166. impl Foo {
  3167. fn with(c: Config) -> Foo { Foo { _x: 0 } }
  3168. fn build(&self) {}
  3169. }
  3170. fn caller() { Foo::with(Config).build(); }
  3171. `
  3172. );
  3173. cg = await CodeGraph.init(tempDir, { index: true });
  3174. expect(callerNamesOf('Foo::build')).toEqual(['caller']);
  3175. });
  3176. it('resolves a chained method from a trait the type implements (default method, via conformance)', async () => {
  3177. fs.writeFileSync(
  3178. path.join(tempDir, 'main.rs'),
  3179. `struct Foo { _x: i32 }
  3180. impl Foo { fn new() -> Foo { Foo { _x: 0 } } }
  3181. struct Decoy { _x: i32 }
  3182. impl Decoy { fn draw(&self) {} }
  3183. trait Drawable { fn draw(&self) {} }
  3184. impl Drawable for Foo {}
  3185. fn caller() { Foo::new().draw(); }
  3186. `
  3187. );
  3188. cg = await CodeGraph.init(tempDir, { index: true });
  3189. expect(callerNamesOf('Drawable::draw')).toEqual(['caller']);
  3190. expect(callerNamesOf('Decoy::draw')).toEqual([]);
  3191. });
  3192. it('creates NO edge when neither the type nor a supertype has the method (silent miss)', async () => {
  3193. fs.writeFileSync(
  3194. path.join(tempDir, 'main.rs'),
  3195. `struct Foo { _x: i32 }
  3196. impl Foo { fn new() -> Foo { Foo { _x: 0 } } }
  3197. struct Other { _x: i32 }
  3198. impl Other { fn only_other(&self) {} }
  3199. fn caller() { Foo::new().only_other(); }
  3200. `
  3201. );
  3202. cg = await CodeGraph.init(tempDir, { index: true });
  3203. // Foo has no only_other() — must not mis-attach to the same-named Other::only_other.
  3204. expect(callerNamesOf('Other::only_other')).toEqual([]);
  3205. });
  3206. });
  3207. describe('Go chained factory-function call resolution (#645/#608 mechanism)', () => {
  3208. function callerNamesOf(qualifiedName: string): string[] {
  3209. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  3210. if (!target) return [];
  3211. const names = cg
  3212. .getIncomingEdges(target.id)
  3213. .filter((e) => e.kind === 'calls')
  3214. .map((e) => cg.getNode(e.source)?.name)
  3215. .filter((n): n is string => !!n);
  3216. return [...new Set(names)].sort();
  3217. }
  3218. it('resolves New().Bar() via the factory return type (pointer), never a same-named decoy', async () => {
  3219. fs.writeFileSync(
  3220. path.join(tempDir, 'main.go'),
  3221. `package main
  3222. type Aaa struct{}
  3223. func (a *Aaa) Bar() {}
  3224. type Foo struct{}
  3225. func New() *Foo { return &Foo{} }
  3226. func (f *Foo) Bar() {}
  3227. func caller() { New().Bar() }
  3228. `
  3229. );
  3230. cg = await CodeGraph.init(tempDir, { index: true });
  3231. expect(callerNamesOf('Foo::Bar')).toEqual(['caller']);
  3232. expect(callerNamesOf('Aaa::Bar')).toEqual([]);
  3233. });
  3234. it('resolves an args chain and a multi-return factory — With(c).Build(), (*Foo, error)', async () => {
  3235. fs.writeFileSync(
  3236. path.join(tempDir, 'main.go'),
  3237. `package main
  3238. type Config struct{}
  3239. type Foo struct{}
  3240. func With(c Config) (*Foo, error) { return &Foo{}, nil }
  3241. func (f *Foo) Build() {}
  3242. func caller() { With(Config{}).Build() }
  3243. `
  3244. );
  3245. cg = await CodeGraph.init(tempDir, { index: true });
  3246. expect(callerNamesOf('Foo::Build')).toEqual(['caller']);
  3247. });
  3248. it('resolves a method provided by an embedded struct (via conformance)', async () => {
  3249. fs.writeFileSync(
  3250. path.join(tempDir, 'main.go'),
  3251. `package main
  3252. type Base struct{}
  3253. func (b *Base) Embedded() {}
  3254. type Decoy struct{}
  3255. func (d *Decoy) Embedded() {}
  3256. type Widget struct{ Base }
  3257. func NewWidget() *Widget { return &Widget{} }
  3258. func caller() { NewWidget().Embedded() }
  3259. `
  3260. );
  3261. cg = await CodeGraph.init(tempDir, { index: true });
  3262. expect(callerNamesOf('Base::Embedded')).toEqual(['caller']);
  3263. expect(callerNamesOf('Decoy::Embedded')).toEqual([]);
  3264. });
  3265. it('creates NO edge when neither the type nor an embedded type has the method (silent miss)', async () => {
  3266. fs.writeFileSync(
  3267. path.join(tempDir, 'main.go'),
  3268. `package main
  3269. type Foo struct{}
  3270. func New() *Foo { return &Foo{} }
  3271. type Other struct{}
  3272. func (o *Other) OnlyOther() {}
  3273. func caller() { New().OnlyOther() }
  3274. `
  3275. );
  3276. cg = await CodeGraph.init(tempDir, { index: true });
  3277. // Foo has no OnlyOther() — must not mis-attach to the same-named Other::OnlyOther.
  3278. expect(callerNamesOf('Other::OnlyOther')).toEqual([]);
  3279. });
  3280. it('falls back to bare-name resolution for a VARIABLE-inner chain without exploding the graph', async () => {
  3281. // `engine` is a package-level VARIABLE holding a func value, not a factory
  3282. // FUNCTION — so its return type can't be recovered and the chain falls back
  3283. // to bare-name resolution of the method (restoring the pre-re-encoding edge).
  3284. // Regression for the runaway this fallback originally caused: it resolved
  3285. // with a mutated `original.referenceName` (the bare `ServeHTTP`, not the
  3286. // stored `engine().ServeHTTP`), so the batched resolver's keyed delete
  3287. // no-oped, the offset-0 batch never drained, and edges inserted forever
  3288. // (5M edges / 1.4 GB on a 99-file repo). The fallback now ties the match to
  3289. // the original ref, and a non-progress guard backstops the loop.
  3290. fs.writeFileSync(
  3291. path.join(tempDir, 'main.go'),
  3292. `package main
  3293. type Server struct{}
  3294. func (s *Server) ServeHTTP() {}
  3295. var engine = func() *Server { return &Server{} }
  3296. func caller() { engine().ServeHTTP() }
  3297. `
  3298. );
  3299. cg = await CodeGraph.init(tempDir, { index: true });
  3300. // Recall: the variable-inner chain still finds the method by bare name.
  3301. expect(callerNamesOf('Server::ServeHTTP')).toEqual(['caller']);
  3302. // No runaway: a single call site yields a single edge, not millions.
  3303. const target = cg
  3304. .getNodesByKind('method')
  3305. .find((n) => n.qualifiedName === 'Server::ServeHTTP')!;
  3306. const rawCalls = cg
  3307. .getIncomingEdges(target.id)
  3308. .filter((e) => e.kind === 'calls');
  3309. expect(rawCalls.length).toBeLessThan(5);
  3310. });
  3311. });
  3312. describe('Scala chained static-factory call resolution (#645/#608 mechanism)', () => {
  3313. function callerNamesOf(qualifiedName: string): string[] {
  3314. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  3315. if (!target) return [];
  3316. const names = cg
  3317. .getIncomingEdges(target.id)
  3318. .filter((e) => e.kind === 'calls')
  3319. .map((e) => cg.getNode(e.source)?.name)
  3320. .filter((n): n is string => !!n);
  3321. return [...new Set(names)].sort();
  3322. }
  3323. it('resolves a companion-factory chain Foo.create().doIt() to the return type, never a same-named decoy', async () => {
  3324. fs.writeFileSync(
  3325. path.join(tempDir, 'Main.scala'),
  3326. `object Foo {
  3327. def create(): Bar = new Bar()
  3328. }
  3329. class Bar {
  3330. def doIt(): Unit = {}
  3331. }
  3332. class Decoy {
  3333. def doIt(): Unit = {}
  3334. }
  3335. object Main {
  3336. def run(): Unit = { Foo.create().doIt() }
  3337. }
  3338. `
  3339. );
  3340. cg = await CodeGraph.init(tempDir, { index: true });
  3341. expect(callerNamesOf('Bar::doIt')).toEqual(['run']);
  3342. expect(callerNamesOf('Decoy::doIt')).toEqual([]);
  3343. });
  3344. it('resolves a case-class apply construction Point(x).dist() on the constructed class', async () => {
  3345. fs.writeFileSync(
  3346. path.join(tempDir, 'Main.scala'),
  3347. `class Point(x: Int) {
  3348. def dist(): Int = x
  3349. }
  3350. class Other {
  3351. def dist(): Int = 0
  3352. }
  3353. object Main {
  3354. def run(): Unit = { Point(3).dist() }
  3355. }
  3356. `
  3357. );
  3358. cg = await CodeGraph.init(tempDir, { index: true });
  3359. expect(callerNamesOf('Point::dist')).toEqual(['run']);
  3360. expect(callerNamesOf('Other::dist')).toEqual([]);
  3361. });
  3362. it('resolves a chained method provided by a trait the return type extends (via conformance)', async () => {
  3363. fs.writeFileSync(
  3364. path.join(tempDir, 'Main.scala'),
  3365. `trait Base {
  3366. def shared(): Unit = {}
  3367. }
  3368. class Widget extends Base
  3369. class Decoy {
  3370. def shared(): Unit = {}
  3371. }
  3372. object Factory {
  3373. def make(): Widget = new Widget()
  3374. }
  3375. object Main {
  3376. def run(): Unit = { Factory.make().shared() }
  3377. }
  3378. `
  3379. );
  3380. cg = await CodeGraph.init(tempDir, { index: true });
  3381. expect(callerNamesOf('Base::shared')).toEqual(['run']);
  3382. expect(callerNamesOf('Decoy::shared')).toEqual([]);
  3383. });
  3384. it('creates NO edge when neither the factory return type nor a supertype has the method (silent miss)', async () => {
  3385. fs.writeFileSync(
  3386. path.join(tempDir, 'Main.scala'),
  3387. `object Foo {
  3388. def create(): Bar = new Bar()
  3389. }
  3390. class Bar {
  3391. }
  3392. class Other {
  3393. def onlyOther(): Unit = {}
  3394. }
  3395. object Main {
  3396. def run(): Unit = { Foo.create().onlyOther() }
  3397. }
  3398. `
  3399. );
  3400. cg = await CodeGraph.init(tempDir, { index: true });
  3401. // Bar has no onlyOther() — must not mis-attach to the same-named Other::onlyOther.
  3402. expect(callerNamesOf('Other::onlyOther')).toEqual([]);
  3403. });
  3404. });
  3405. describe('Dart chained static-factory / factory-constructor call resolution (#645/#608 mechanism)', () => {
  3406. function callerNamesOf(qualifiedName: string): string[] {
  3407. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  3408. if (!target) return [];
  3409. const names = cg
  3410. .getIncomingEdges(target.id)
  3411. .filter((e) => e.kind === 'calls')
  3412. .map((e) => cg.getNode(e.source)?.name)
  3413. .filter((n): n is string => !!n);
  3414. return [...new Set(names)].sort();
  3415. }
  3416. it('resolves a static-factory chain Foo.makeBar().doIt() to the return type, never a same-named decoy', async () => {
  3417. fs.writeFileSync(
  3418. path.join(tempDir, 'main.dart'),
  3419. `class Foo {
  3420. static Bar makeBar() => Bar();
  3421. }
  3422. class Bar {
  3423. void doIt() {}
  3424. }
  3425. class Decoy {
  3426. void doIt() {}
  3427. }
  3428. void run() {
  3429. Foo.makeBar().doIt();
  3430. }
  3431. `
  3432. );
  3433. cg = await CodeGraph.init(tempDir, { index: true });
  3434. expect(callerNamesOf('Bar::doIt')).toEqual(['run']);
  3435. expect(callerNamesOf('Decoy::doIt')).toEqual([]);
  3436. });
  3437. it('resolves a named factory-constructor chain Foo.create().ship() on the constructed class', async () => {
  3438. fs.writeFileSync(
  3439. path.join(tempDir, 'main.dart'),
  3440. `class Foo {
  3441. Foo._();
  3442. factory Foo.create() => Foo._();
  3443. void ship() {}
  3444. }
  3445. class Decoy {
  3446. void ship() {}
  3447. }
  3448. void run() {
  3449. Foo.create().ship();
  3450. }
  3451. `
  3452. );
  3453. cg = await CodeGraph.init(tempDir, { index: true });
  3454. // The factory constructor `Foo.create` is now a node whose return type is Foo,
  3455. // so `ship` resolves on Foo, not the same-named Decoy.
  3456. expect(callerNamesOf('Foo::ship')).toEqual(['run']);
  3457. expect(callerNamesOf('Decoy::ship')).toEqual([]);
  3458. });
  3459. it('resolves a constructor-receiver chain Bar().doIt() on the constructed class', async () => {
  3460. fs.writeFileSync(
  3461. path.join(tempDir, 'main.dart'),
  3462. `class Bar {
  3463. void doIt() {}
  3464. }
  3465. class Decoy {
  3466. void doIt() {}
  3467. }
  3468. void run() {
  3469. Bar().doIt();
  3470. }
  3471. `
  3472. );
  3473. cg = await CodeGraph.init(tempDir, { index: true });
  3474. expect(callerNamesOf('Bar::doIt')).toEqual(['run']);
  3475. expect(callerNamesOf('Decoy::doIt')).toEqual([]);
  3476. });
  3477. it('resolves a chained method inherited from a superclass the return type extends (via conformance)', async () => {
  3478. fs.writeFileSync(
  3479. path.join(tempDir, 'main.dart'),
  3480. `class Base {
  3481. void render() {}
  3482. }
  3483. class Widget extends Base {
  3484. static Widget make() => Widget();
  3485. }
  3486. class Decoy {
  3487. void render() {}
  3488. }
  3489. void run() {
  3490. Widget.make().render();
  3491. }
  3492. `
  3493. );
  3494. cg = await CodeGraph.init(tempDir, { index: true });
  3495. expect(callerNamesOf('Base::render')).toEqual(['run']);
  3496. expect(callerNamesOf('Decoy::render')).toEqual([]);
  3497. });
  3498. it('creates NO edge when neither the factory return type nor a supertype has the method (silent miss)', async () => {
  3499. fs.writeFileSync(
  3500. path.join(tempDir, 'main.dart'),
  3501. `class Foo {
  3502. static Bar makeBar() => Bar();
  3503. }
  3504. class Bar {
  3505. }
  3506. class Other {
  3507. void onlyOther() {}
  3508. }
  3509. void run() {
  3510. Foo.makeBar().onlyOther();
  3511. }
  3512. `
  3513. );
  3514. cg = await CodeGraph.init(tempDir, { index: true });
  3515. // Bar has no onlyOther() — must not mis-attach to the same-named Other::onlyOther.
  3516. expect(callerNamesOf('Other::onlyOther')).toEqual([]);
  3517. });
  3518. it('still extracts a method tree-sitter misparses as a constructor (@override + record return)', async () => {
  3519. // tree-sitter-dart misparses `@override (A, B) reduce()` — the annotation
  3520. // swallows the record return type, so `reduce()` looks like a single-
  3521. // identifier constructor_signature. It must NOT be skipped as an unnamed
  3522. // ctor (its name doesn't match the class); its body call must attribute to
  3523. // `reduce`, not the class.
  3524. fs.writeFileSync(
  3525. path.join(tempDir, 'main.dart'),
  3526. `class Base {}
  3527. class Action extends Base {
  3528. Action({required int x});
  3529. @override
  3530. (int, String) reduce() {
  3531. return (compute(), "y");
  3532. }
  3533. int compute() => 1;
  3534. }
  3535. `
  3536. );
  3537. cg = await CodeGraph.init(tempDir, { index: true });
  3538. // reduce must be a node and its body call must resolve to Action::compute.
  3539. expect(callerNamesOf('Action::compute')).toEqual(['reduce']);
  3540. });
  3541. it('keeps plain construction Foo() as instantiation, not a Foo::Foo method call', async () => {
  3542. // The unnamed constructor is intentionally NOT extracted as a `Foo::Foo`
  3543. // method, so `Foo(...)` resolves to the class (an `instantiates` edge),
  3544. // never hijacked into a call to a phantom constructor method.
  3545. fs.writeFileSync(
  3546. path.join(tempDir, 'main.dart'),
  3547. `class Widget {
  3548. final int x;
  3549. Widget(this.x);
  3550. }
  3551. void run() {
  3552. Widget(3);
  3553. }
  3554. `
  3555. );
  3556. cg = await CodeGraph.init(tempDir, { index: true });
  3557. // No Foo::Foo phantom method node.
  3558. expect(cg.getNodesByKind('method').some((n) => n.qualifiedName === 'Widget::Widget')).toBe(false);
  3559. // The construction resolves to the class as an `instantiates` edge.
  3560. const widget = cg.getNodesByKind('class').find((n) => n.name === 'Widget')!;
  3561. const incoming = cg.getIncomingEdges(widget.id);
  3562. expect(incoming.some((e) => e.kind === 'instantiates')).toBe(true);
  3563. });
  3564. });
  3565. describe('Objective-C chained message-send call resolution (#645/#608 mechanism)', () => {
  3566. function callerNamesOf(qualifiedName: string): string[] {
  3567. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  3568. if (!target) return [];
  3569. const names = cg
  3570. .getIncomingEdges(target.id)
  3571. .filter((e) => e.kind === 'calls')
  3572. .map((e) => cg.getNode(e.source)?.name)
  3573. .filter((n): n is string => !!n);
  3574. return [...new Set(names)].sort();
  3575. }
  3576. it('resolves a chained message send [[Foo create] doIt] via the return type, never a same-named decoy', async () => {
  3577. fs.writeFileSync(
  3578. path.join(tempDir, 'main.m'),
  3579. `@interface Bar : NSObject
  3580. - (void)doIt;
  3581. @end
  3582. @implementation Bar
  3583. - (void)doIt {}
  3584. @end
  3585. @interface Decoy : NSObject
  3586. - (void)doIt;
  3587. @end
  3588. @implementation Decoy
  3589. - (void)doIt {}
  3590. @end
  3591. @interface Foo : NSObject
  3592. + (Bar *)create;
  3593. @end
  3594. @implementation Foo
  3595. + (Bar *)create { return nil; }
  3596. - (void)run { [[Foo create] doIt]; }
  3597. @end
  3598. `
  3599. );
  3600. cg = await CodeGraph.init(tempDir, { index: true });
  3601. expect(callerNamesOf('Bar::doIt')).toEqual(['run']);
  3602. expect(callerNamesOf('Decoy::doIt')).toEqual([]);
  3603. });
  3604. it('resolves a chained message whose method is inherited from a superclass (via conformance)', async () => {
  3605. fs.writeFileSync(
  3606. path.join(tempDir, 'main.m'),
  3607. `@interface Base : NSObject
  3608. - (void)render;
  3609. @end
  3610. @implementation Base
  3611. - (void)render {}
  3612. @end
  3613. @interface Widget : Base
  3614. @end
  3615. @implementation Widget
  3616. @end
  3617. @interface Decoy : NSObject
  3618. - (void)render;
  3619. @end
  3620. @implementation Decoy
  3621. - (void)render {}
  3622. @end
  3623. @interface Factory : NSObject
  3624. + (Widget *)make;
  3625. @end
  3626. @implementation Factory
  3627. + (Widget *)make { return nil; }
  3628. - (void)run { [[Factory make] render]; }
  3629. @end
  3630. `
  3631. );
  3632. cg = await CodeGraph.init(tempDir, { index: true });
  3633. expect(callerNamesOf('Base::render')).toEqual(['run']);
  3634. expect(callerNamesOf('Decoy::render')).toEqual([]);
  3635. });
  3636. it('creates NO edge when the factory return type lacks the method (silent miss)', async () => {
  3637. fs.writeFileSync(
  3638. path.join(tempDir, 'main.m'),
  3639. `@interface Bar : NSObject
  3640. @end
  3641. @implementation Bar
  3642. @end
  3643. @interface Other : NSObject
  3644. - (void)onlyOther;
  3645. @end
  3646. @implementation Other
  3647. - (void)onlyOther {}
  3648. @end
  3649. @interface Foo : NSObject
  3650. + (Bar *)create;
  3651. @end
  3652. @implementation Foo
  3653. + (Bar *)create { return nil; }
  3654. - (void)run { [[Foo create] onlyOther]; }
  3655. @end
  3656. `
  3657. );
  3658. cg = await CodeGraph.init(tempDir, { index: true });
  3659. // Bar has no onlyOther — must not mis-attach to the same-named Other::onlyOther.
  3660. expect(callerNamesOf('Other::onlyOther')).toEqual([]);
  3661. });
  3662. it('resolves a singleton chain [[Cache shared] clearAll] whose factory returns nonnull instancetype', async () => {
  3663. // The factory returns `nonnull instancetype` — the nullability qualifier must
  3664. // be skipped (not captured AS the type), and an instancetype class-message
  3665. // factory returns the receiver class, so clearAll resolves on Cache, never a
  3666. // same-named decoy. (Regression for both: the captured-`nonnull` bug and the
  3667. // ubiquitous `[[X alloc] init]` / singleton pattern.)
  3668. fs.writeFileSync(
  3669. path.join(tempDir, 'main.m'),
  3670. `@interface Cache : NSObject
  3671. + (nonnull instancetype)shared;
  3672. - (void)clearAll;
  3673. @end
  3674. @implementation Cache
  3675. + (nonnull instancetype)shared { return nil; }
  3676. - (void)clearAll {}
  3677. @end
  3678. @interface Decoy : NSObject
  3679. - (void)clearAll;
  3680. @end
  3681. @implementation Decoy
  3682. - (void)clearAll {}
  3683. @end
  3684. @interface Caller : NSObject
  3685. - (void)run;
  3686. @end
  3687. @implementation Caller
  3688. - (void)run { [[Cache shared] clearAll]; }
  3689. @end
  3690. `
  3691. );
  3692. cg = await CodeGraph.init(tempDir, { index: true });
  3693. expect(callerNamesOf('Cache::clearAll')).toEqual(['run']);
  3694. expect(callerNamesOf('Decoy::clearAll')).toEqual([]);
  3695. });
  3696. });
  3697. describe('Pascal/Delphi chained static-factory call resolution (#645/#608 mechanism)', () => {
  3698. function callerNamesOf(qualifiedName: string): string[] {
  3699. const target = cg.getNodesByKind('method').find((n) => n.qualifiedName === qualifiedName);
  3700. if (!target) return [];
  3701. const names = cg
  3702. .getIncomingEdges(target.id)
  3703. .filter((e) => e.kind === 'calls')
  3704. .map((e) => cg.getNode(e.source)?.name)
  3705. .filter((n): n is string => !!n);
  3706. return [...new Set(names)].sort();
  3707. }
  3708. function isCalled(qn: string): boolean {
  3709. const t = cg.getNodesByKind('method').find((n) => n.qualifiedName === qn);
  3710. return !!t && cg.getIncomingEdges(t.id).some((e) => e.kind === 'calls');
  3711. }
  3712. it('resolves a chained factory call TFoo.GetInstance().DoIt() via the return type, never a same-named decoy', async () => {
  3713. fs.writeFileSync(
  3714. path.join(tempDir, 'main.pas'),
  3715. `unit Main;
  3716. interface
  3717. type
  3718. TBar = class
  3719. procedure DoIt;
  3720. end;
  3721. TDecoy = class
  3722. procedure DoIt;
  3723. end;
  3724. TFoo = class
  3725. class function GetInstance: TBar;
  3726. end;
  3727. implementation
  3728. procedure TBar.DoIt; begin end;
  3729. procedure TDecoy.DoIt; begin end;
  3730. class function TFoo.GetInstance: TBar; begin Result := nil; end;
  3731. procedure Run;
  3732. begin
  3733. TFoo.GetInstance().DoIt();
  3734. end;
  3735. end.
  3736. `
  3737. );
  3738. cg = await CodeGraph.init(tempDir, { index: true });
  3739. expect(isCalled('TBar::DoIt')).toBe(true);
  3740. expect(isCalled('TDecoy::DoIt')).toBe(false);
  3741. });
  3742. it('resolves a constructor chain TFoo.Create().Configure() on the constructed class', async () => {
  3743. fs.writeFileSync(
  3744. path.join(tempDir, 'main.pas'),
  3745. `unit Main;
  3746. interface
  3747. type
  3748. TFoo = class
  3749. constructor Create;
  3750. procedure Configure;
  3751. end;
  3752. TDecoy = class
  3753. procedure Configure;
  3754. end;
  3755. implementation
  3756. constructor TFoo.Create; begin end;
  3757. procedure TFoo.Configure; begin end;
  3758. procedure TDecoy.Configure; begin end;
  3759. procedure Run;
  3760. begin
  3761. TFoo.Create().Configure();
  3762. end;
  3763. end.
  3764. `
  3765. );
  3766. cg = await CodeGraph.init(tempDir, { index: true });
  3767. // A constructor returns its own class (no `: TBar` annotation), so Configure
  3768. // resolves on TFoo, not the same-named decoy.
  3769. expect(isCalled('TFoo::Configure')).toBe(true);
  3770. expect(isCalled('TDecoy::Configure')).toBe(false);
  3771. });
  3772. it('resolves a typecast chain TFoo(x).DoIt() on the cast type', async () => {
  3773. fs.writeFileSync(
  3774. path.join(tempDir, 'main.pas'),
  3775. `unit Main;
  3776. interface
  3777. type
  3778. TFoo = class
  3779. procedure DoIt;
  3780. end;
  3781. TDecoy = class
  3782. procedure DoIt;
  3783. end;
  3784. implementation
  3785. procedure TFoo.DoIt; begin end;
  3786. procedure TDecoy.DoIt; begin end;
  3787. procedure Run(obj: TObject);
  3788. begin
  3789. TFoo(obj).DoIt();
  3790. end;
  3791. end.
  3792. `
  3793. );
  3794. cg = await CodeGraph.init(tempDir, { index: true });
  3795. expect(isCalled('TFoo::DoIt')).toBe(true);
  3796. expect(isCalled('TDecoy::DoIt')).toBe(false);
  3797. });
  3798. it('creates NO edge when the factory return type lacks the method (silent miss)', async () => {
  3799. fs.writeFileSync(
  3800. path.join(tempDir, 'main.pas'),
  3801. `unit Main;
  3802. interface
  3803. type
  3804. TBar = class
  3805. end;
  3806. TOther = class
  3807. procedure OnlyOther;
  3808. end;
  3809. TFoo = class
  3810. class function GetInstance: TBar;
  3811. end;
  3812. implementation
  3813. procedure TOther.OnlyOther; begin end;
  3814. class function TFoo.GetInstance: TBar; begin Result := nil; end;
  3815. procedure Run;
  3816. begin
  3817. TFoo.GetInstance().OnlyOther();
  3818. end;
  3819. end.
  3820. `
  3821. );
  3822. cg = await CodeGraph.init(tempDir, { index: true });
  3823. // TBar has no OnlyOther — must not mis-attach to the same-named TOther::OnlyOther.
  3824. expect(isCalled('TOther::OnlyOther')).toBe(false);
  3825. });
  3826. it('extracts paren-less method calls (Pascal lets a no-arg method drop its parens)', async () => {
  3827. fs.writeFileSync(
  3828. path.join(tempDir, 'main.pas'),
  3829. `unit Main;
  3830. interface
  3831. type
  3832. TFoo = class
  3833. procedure DoThing;
  3834. procedure Reset;
  3835. end;
  3836. implementation
  3837. procedure TFoo.DoThing; begin end;
  3838. procedure TFoo.Reset; begin end;
  3839. procedure Run(f: TFoo);
  3840. begin
  3841. f.DoThing;
  3842. f.Reset;
  3843. end;
  3844. end.
  3845. `
  3846. );
  3847. cg = await CodeGraph.init(tempDir, { index: true });
  3848. expect(isCalled('TFoo::DoThing')).toBe(true);
  3849. expect(isCalled('TFoo::Reset')).toBe(true);
  3850. });
  3851. it('resolves a PAREN-LESS chained factory call TFoo.GetInstance.DoIt via the return type', async () => {
  3852. fs.writeFileSync(
  3853. path.join(tempDir, 'main.pas'),
  3854. `unit Main;
  3855. interface
  3856. type
  3857. TBar = class
  3858. procedure DoIt;
  3859. end;
  3860. TDecoy = class
  3861. procedure DoIt;
  3862. end;
  3863. TFoo = class
  3864. class function GetInstance: TBar;
  3865. end;
  3866. implementation
  3867. procedure TBar.DoIt; begin end;
  3868. procedure TDecoy.DoIt; begin end;
  3869. class function TFoo.GetInstance: TBar; begin Result := nil; end;
  3870. procedure Run;
  3871. begin
  3872. TFoo.GetInstance.DoIt;
  3873. end;
  3874. end.
  3875. `
  3876. );
  3877. cg = await CodeGraph.init(tempDir, { index: true });
  3878. expect(isCalled('TBar::DoIt')).toBe(true);
  3879. expect(isCalled('TDecoy::DoIt')).toBe(false);
  3880. });
  3881. it('does NOT turn a property write/read into a call edge (only statement-level dots are calls)', async () => {
  3882. fs.writeFileSync(
  3883. path.join(tempDir, 'main.pas'),
  3884. `unit Main;
  3885. interface
  3886. type
  3887. TFoo = class
  3888. function GetValue: Integer;
  3889. procedure SetValue(v: Integer);
  3890. property Value: Integer read GetValue write SetValue;
  3891. end;
  3892. implementation
  3893. function TFoo.GetValue: Integer; begin Result := 0; end;
  3894. procedure TFoo.SetValue(v: Integer); begin end;
  3895. procedure Run(f: TFoo);
  3896. var x: Integer;
  3897. begin
  3898. f.Value := 5;
  3899. x := f.Value;
  3900. end;
  3901. end.
  3902. `
  3903. );
  3904. cg = await CodeGraph.init(tempDir, { index: true });
  3905. // A property read/write is a bare dot in assignment position, not a statement,
  3906. // so it must not be mis-extracted as a call to the property's getter/setter.
  3907. expect(isCalled('TFoo::GetValue')).toBe(false);
  3908. expect(isCalled('TFoo::SetValue')).toBe(false);
  3909. });
  3910. it('attributes an implementation-only free procedure\'s calls to the procedure, not the file', async () => {
  3911. fs.writeFileSync(
  3912. path.join(tempDir, 'main.pas'),
  3913. `unit Main;
  3914. interface
  3915. type
  3916. TTgt = class
  3917. procedure Hit;
  3918. end;
  3919. TFoo = class
  3920. procedure DoStuff;
  3921. end;
  3922. implementation
  3923. procedure TTgt.Hit; begin end;
  3924. procedure TFoo.DoStuff; var t: TTgt; begin t.Hit; end;
  3925. procedure Helper; var t: TTgt; begin t.Hit; end;
  3926. `
  3927. );
  3928. cg = await CodeGraph.init(tempDir, { index: true });
  3929. // `Helper` is implementation-only (no interface decl, not a method), but its
  3930. // body's call must attribute to `Helper`, not the file/module — alongside the
  3931. // method `DoStuff`.
  3932. expect(callerNamesOf('TTgt::Hit')).toEqual(['DoStuff', 'Helper']);
  3933. });
  3934. });
  3935. });