| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981 |
- /**
- * Extraction Tests
- *
- * Tests for the tree-sitter extraction system.
- */
- import { describe, it, expect, beforeAll, beforeEach, afterEach } from 'vitest';
- import * as fs from 'fs';
- import * as path from 'path';
- import * as os from 'os';
- import { CodeGraph } from '../src';
- import { extractFromSource, scanDirectory } from '../src/extraction';
- import { detectLanguage, isLanguageSupported, getSupportedLanguages, initGrammars, loadAllGrammars, isSourceFile } from '../src/extraction/grammars';
- import { normalizePath } from '../src/utils';
- beforeAll(async () => {
- await initGrammars();
- await loadAllGrammars();
- });
- // Create a temporary directory for each test
- function createTempDir(): string {
- return fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-test-'));
- }
- // Clean up temporary directory
- function cleanupTempDir(dir: string): void {
- if (fs.existsSync(dir)) {
- fs.rmSync(dir, { recursive: true, force: true });
- }
- }
- describe('Language Detection', () => {
- it('should detect TypeScript files', () => {
- expect(detectLanguage('src/index.ts')).toBe('typescript');
- expect(detectLanguage('components/Button.tsx')).toBe('tsx');
- });
- it('should detect JavaScript files', () => {
- expect(detectLanguage('index.js')).toBe('javascript');
- expect(detectLanguage('App.jsx')).toBe('jsx');
- expect(detectLanguage('config.mjs')).toBe('javascript');
- });
- it('should detect Python files', () => {
- expect(detectLanguage('main.py')).toBe('python');
- });
- it('should detect Go files', () => {
- expect(detectLanguage('main.go')).toBe('go');
- });
- it('should detect Rust files', () => {
- expect(detectLanguage('lib.rs')).toBe('rust');
- });
- it('should detect Java files', () => {
- expect(detectLanguage('Main.java')).toBe('java');
- });
- it('should detect C files', () => {
- expect(detectLanguage('main.c')).toBe('c');
- expect(detectLanguage('utils.h')).toBe('c');
- });
- it('should detect C++ files', () => {
- expect(detectLanguage('main.cpp')).toBe('cpp');
- expect(detectLanguage('class.hpp')).toBe('cpp');
- });
- it('should detect C# files', () => {
- expect(detectLanguage('Program.cs')).toBe('csharp');
- });
- it('should detect PHP files', () => {
- expect(detectLanguage('index.php')).toBe('php');
- });
- it('should detect Ruby files', () => {
- expect(detectLanguage('app.rb')).toBe('ruby');
- });
- it('should detect Swift files', () => {
- expect(detectLanguage('ViewController.swift')).toBe('swift');
- });
- it('should detect Kotlin files', () => {
- expect(detectLanguage('MainActivity.kt')).toBe('kotlin');
- expect(detectLanguage('build.gradle.kts')).toBe('kotlin');
- });
- it('should detect Dart files', () => {
- expect(detectLanguage('main.dart')).toBe('dart');
- });
- it('should detect Objective-C files', () => {
- expect(detectLanguage('AppDelegate.m')).toBe('objc');
- expect(detectLanguage('ViewController.mm')).toBe('objc');
- const objcHeader = '@interface Foo : NSObject\n@end\n';
- expect(detectLanguage('Foo.h', objcHeader)).toBe('objc');
- expect(detectLanguage('stdio.h', '#ifndef STDIO_H\nvoid printf();\n#endif\n')).toBe('c');
- });
- it('should return unknown for unsupported extensions', () => {
- expect(detectLanguage('styles.css')).toBe('unknown');
- expect(detectLanguage('data.json')).toBe('unknown');
- });
- });
- describe('Language Support', () => {
- it('should report supported languages', () => {
- expect(isLanguageSupported('typescript')).toBe(true);
- expect(isLanguageSupported('python')).toBe(true);
- expect(isLanguageSupported('go')).toBe(true);
- expect(isLanguageSupported('unknown')).toBe(false);
- });
- it('should list all supported languages', () => {
- const languages = getSupportedLanguages();
- expect(languages).toContain('typescript');
- expect(languages).toContain('javascript');
- expect(languages).toContain('python');
- expect(languages).toContain('go');
- expect(languages).toContain('rust');
- expect(languages).toContain('java');
- expect(languages).toContain('csharp');
- expect(languages).toContain('php');
- expect(languages).toContain('ruby');
- expect(languages).toContain('swift');
- expect(languages).toContain('kotlin');
- expect(languages).toContain('dart');
- });
- });
- describe('TypeScript Extraction', () => {
- it('should extract function declarations', () => {
- const code = `
- export function processPayment(amount: number): Promise<Receipt> {
- return stripe.charge(amount);
- }
- `;
- const result = extractFromSource('payment.ts', code);
- // File node + function node
- const fileNode = result.nodes.find((n) => n.kind === 'file');
- expect(fileNode).toBeDefined();
- expect(fileNode?.name).toBe('payment.ts');
- const funcNode = result.nodes.find((n) => n.kind === 'function');
- expect(funcNode).toMatchObject({
- kind: 'function',
- name: 'processPayment',
- language: 'typescript',
- isExported: true,
- });
- expect(funcNode?.signature).toContain('amount: number');
- });
- it('should extract class declarations', () => {
- const code = `
- export class PaymentService {
- private stripe: StripeClient;
- constructor(apiKey: string) {
- this.stripe = new StripeClient(apiKey);
- }
- async charge(amount: number): Promise<Receipt> {
- return this.stripe.charge(amount);
- }
- }
- `;
- const result = extractFromSource('service.ts', code);
- const classNode = result.nodes.find((n) => n.kind === 'class');
- const methodNodes = result.nodes.filter((n) => n.kind === 'method');
- expect(classNode).toBeDefined();
- expect(classNode?.name).toBe('PaymentService');
- expect(classNode?.isExported).toBe(true);
- expect(methodNodes.length).toBeGreaterThanOrEqual(1);
- const chargeMethod = methodNodes.find((m) => m.name === 'charge');
- expect(chargeMethod).toBeDefined();
- });
- it('should extract interfaces', () => {
- const code = `
- export interface User {
- id: string;
- name: string;
- email: string;
- }
- `;
- const result = extractFromSource('types.ts', code);
- const fileNode = result.nodes.find((n) => n.kind === 'file');
- expect(fileNode).toBeDefined();
- const ifaceNode = result.nodes.find((n) => n.kind === 'interface');
- expect(ifaceNode).toMatchObject({
- kind: 'interface',
- name: 'User',
- isExported: true,
- });
- });
- it('should extract type references from interface property signatures', () => {
- const code = `
- import type { IPage } from '../PromoterList';
- import type { IOrderField } from '../types';
- interface Hprops {
- value?: Partial<IPage> & Partial<IOrderField>;
- }
- `;
- const result = extractFromSource('HeaderFilter.ts', code);
- const refs = result.unresolvedReferences.filter((r) => r.referenceKind === 'references');
- expect(refs.some((r) => r.referenceName === 'IPage')).toBe(true);
- expect(refs.some((r) => r.referenceName === 'IOrderField')).toBe(true);
- });
- it('should extract type references from interface method signatures', () => {
- const code = `
- import type { IPage } from '../PromoterList';
- import type { IOrderField } from '../types';
- interface MethodForm {
- fetchPage(arg: IPage): IOrderField;
- }
- `;
- const result = extractFromSource('MethodForm.ts', code);
- const refs = result.unresolvedReferences.filter((r) => r.referenceKind === 'references');
- expect(refs.some((r) => r.referenceName === 'IPage')).toBe(true);
- expect(refs.some((r) => r.referenceName === 'IOrderField')).toBe(true);
- });
- it('extracts type references from in-body local variable annotations', () => {
- // A function that uses a type ONLY in its body — `const items: Foo[] = []` —
- // still depends on Foo. The body walker used to capture calls but never type
- // annotations, so impact / `affected` missed the dependency. Must cover
- // function, class-method, and object-literal-method bodies — and must NOT
- // turn the locals themselves into graph nodes (that would explode the graph).
- const code = `
- import { Foo } from './types';
- export function build(): void {
- const items: Foo[] = [];
- void items;
- }
- export class K {
- run(): void {
- const a: Foo = { x: 1 };
- void a;
- }
- }
- export const handler = {
- handle(): void {
- const b: Foo = { x: 1 };
- void b;
- },
- };
- `;
- const result = extractFromSource('inbody.ts', code);
- const fooRefs = result.unresolvedReferences.filter(
- (r) => r.referenceKind === 'references' && r.referenceName === 'Foo'
- );
- // One per body scope: build(), K.run(), handler.handle().
- expect(fooRefs.length).toBeGreaterThanOrEqual(3);
- // Each reference is attributed to its enclosing function/method node — never
- // to a local-variable node, because locals are intentionally not extracted.
- const byId = new Map(result.nodes.map((n) => [n.id, n]));
- for (const ref of fooRefs) {
- const owner = byId.get(ref.fromNodeId);
- expect(owner).toBeDefined();
- expect(['function', 'method']).toContain(owner!.kind);
- }
- // The locals (items/a/b) must not leak in as symbols.
- expect(result.nodes.some((n) => ['items', 'a', 'b'].includes(n.name))).toBe(false);
- });
- it('should track function calls', () => {
- const code = `
- function main() {
- const result = processData();
- console.log(result);
- }
- `;
- const result = extractFromSource('main.ts', code);
- expect(result.unresolvedReferences.length).toBeGreaterThan(0);
- const calls = result.unresolvedReferences.filter((r) => r.referenceKind === 'calls');
- expect(calls.some((c) => c.referenceName === 'processData')).toBe(true);
- });
- });
- describe('Arrow Function Export Extraction', () => {
- it('should extract exported arrow functions assigned to const', () => {
- const code = `
- export const useAuth = (): AuthContextValue => {
- return useContext(AuthContext);
- };
- `;
- const result = extractFromSource('hooks.ts', code);
- const funcNode = result.nodes.find((n) => n.kind === 'function' && n.name === 'useAuth');
- expect(funcNode).toBeDefined();
- expect(funcNode).toMatchObject({
- kind: 'function',
- name: 'useAuth',
- isExported: true,
- });
- });
- it('should extract exported function expressions assigned to const', () => {
- const code = `
- export const processData = function(input: string): string {
- return input.trim();
- };
- `;
- const result = extractFromSource('utils.ts', code);
- const funcNode = result.nodes.find((n) => n.kind === 'function' && n.name === 'processData');
- expect(funcNode).toBeDefined();
- expect(funcNode).toMatchObject({
- kind: 'function',
- name: 'processData',
- isExported: true,
- });
- });
- it('should not extract non-exported arrow functions as exported', () => {
- const code = `
- const internalHelper = () => {
- return 42;
- };
- `;
- const result = extractFromSource('internal.ts', code);
- const helperNode = result.nodes.find((n) => n.name === 'internalHelper');
- expect(helperNode).toBeDefined();
- expect(helperNode?.isExported).toBeFalsy();
- });
- it('should still skip truly anonymous arrow functions', () => {
- const code = `
- const items = [1, 2, 3].map((x) => x * 2);
- `;
- const result = extractFromSource('anon.ts', code);
- // The inline arrow function passed to .map() has no variable_declarator parent
- // and should remain anonymous (skipped)
- const anonFunctions = result.nodes.filter(
- (n) => n.kind === 'function' && n.name === '<anonymous>'
- );
- expect(anonFunctions).toHaveLength(0);
- });
- it('should extract multiple exported arrow functions from the same file', () => {
- const code = `
- export const add = (a: number, b: number): number => a + b;
- export const subtract = (a: number, b: number): number => a - b;
- const internal = () => 'not exported';
- `;
- const result = extractFromSource('math.ts', code);
- const exported = result.nodes.filter((n) => n.kind === 'function' && n.isExported);
- expect(exported).toHaveLength(2);
- expect(exported.map((n) => n.name).sort()).toEqual(['add', 'subtract']);
- const internalNode = result.nodes.find((n) => n.name === 'internal');
- expect(internalNode).toBeDefined();
- expect(internalNode?.isExported).toBeFalsy();
- });
- it('should extract arrow functions in JavaScript files', () => {
- const code = `
- export const fetchData = async () => {
- const response = await fetch('/api/data');
- return response.json();
- };
- `;
- const result = extractFromSource('api.js', code);
- const funcNode = result.nodes.find((n) => n.kind === 'function' && n.name === 'fetchData');
- expect(funcNode).toBeDefined();
- expect(funcNode).toMatchObject({
- kind: 'function',
- name: 'fetchData',
- isExported: true,
- });
- });
- });
- describe('Type Alias Extraction', () => {
- it('should extract exported type aliases in TypeScript', () => {
- const code = `
- export type AuthContextValue = {
- user: User | null;
- login: () => void;
- logout: () => void;
- };
- `;
- const result = extractFromSource('types.ts', code);
- const typeNode = result.nodes.find((n) => n.kind === 'type_alias');
- expect(typeNode).toMatchObject({
- kind: 'type_alias',
- name: 'AuthContextValue',
- isExported: true,
- });
- });
- it('should extract non-exported type aliases', () => {
- const code = `
- type InternalState = {
- loading: boolean;
- error: string | null;
- };
- `;
- const result = extractFromSource('internal.ts', code);
- const typeNode = result.nodes.find((n) => n.kind === 'type_alias');
- expect(typeNode).toMatchObject({
- kind: 'type_alias',
- name: 'InternalState',
- isExported: false,
- });
- });
- it('should extract multiple type aliases from the same file', () => {
- const code = `
- export type UnitSystem = 'metric' | 'imperial';
- export type DateFormat = 'ISO' | 'US' | 'EU';
- type Internal = string;
- `;
- const result = extractFromSource('config.ts', code);
- const typeAliases = result.nodes.filter((n) => n.kind === 'type_alias');
- expect(typeAliases).toHaveLength(3);
- const exported = typeAliases.filter((n) => n.isExported);
- expect(exported).toHaveLength(2);
- expect(exported.map((n) => n.name).sort()).toEqual(['DateFormat', 'UnitSystem']);
- });
- });
- describe('Exported Variable Extraction', () => {
- it('should extract exported const with call expression (Zustand store)', () => {
- const code = `
- export const useUIStore = create<UIState>((set) => ({
- isOpen: false,
- toggle: () => set((s) => ({ isOpen: !s.isOpen })),
- }));
- `;
- const result = extractFromSource('store.ts', code);
- const varNode = result.nodes.find((n) => n.kind === 'constant' && n.name === 'useUIStore');
- expect(varNode).toBeDefined();
- expect(varNode?.isExported).toBe(true);
- });
- it('should extract exported const with object literal', () => {
- const code = `
- export const config = {
- apiUrl: 'https://api.example.com',
- timeout: 5000,
- };
- `;
- const result = extractFromSource('config.ts', code);
- const varNode = result.nodes.find((n) => n.kind === 'constant' && n.name === 'config');
- expect(varNode).toBeDefined();
- expect(varNode?.isExported).toBe(true);
- });
- it('should extract exported const with array literal', () => {
- const code = `
- export const SCREEN_NAMES = ['home', 'settings', 'profile'] as const;
- `;
- const result = extractFromSource('constants.ts', code);
- const varNode = result.nodes.find((n) => n.kind === 'constant' && n.name === 'SCREEN_NAMES');
- expect(varNode).toBeDefined();
- expect(varNode?.isExported).toBe(true);
- });
- it('should extract exported const with primitive value', () => {
- const code = `
- export const MAX_RETRIES = 3;
- export const API_VERSION = "v2";
- `;
- const result = extractFromSource('constants.ts', code);
- const variables = result.nodes.filter((n) => n.kind === 'constant');
- expect(variables).toHaveLength(2);
- expect(variables.map((n) => n.name).sort()).toEqual(['API_VERSION', 'MAX_RETRIES']);
- });
- it('should NOT duplicate arrow functions as both function and variable', () => {
- const code = `
- export const useAuth = () => {
- return useContext(AuthContext);
- };
- `;
- const result = extractFromSource('hooks.ts', code);
- // Should be extracted as function (from arrow function handler), NOT as variable
- const funcNodes = result.nodes.filter((n) => n.kind === 'function' && n.name === 'useAuth');
- const varNodes = result.nodes.filter((n) => n.kind === 'variable' && n.name === 'useAuth');
- expect(funcNodes).toHaveLength(1);
- expect(varNodes).toHaveLength(0);
- });
- it('should extract non-exported const as non-exported variable', () => {
- const code = `
- const internalConfig = {
- debug: true,
- };
- `;
- const result = extractFromSource('internal.ts', code);
- // Non-exported const at file level should be extracted as a constant (not exported)
- const varNodes = result.nodes.filter((n) => (n.kind === 'variable' || n.kind === 'constant') && n.name === 'internalConfig');
- expect(varNodes).toHaveLength(1);
- expect(varNodes[0]?.isExported).toBeFalsy();
- });
- it('should extract Zod schema exports', () => {
- const code = `
- export const userSchema = z.object({
- id: z.string(),
- name: z.string(),
- email: z.string().email(),
- });
- `;
- const result = extractFromSource('schemas.ts', code);
- const varNode = result.nodes.find((n) => n.kind === 'constant' && n.name === 'userSchema');
- expect(varNode).toBeDefined();
- expect(varNode?.isExported).toBe(true);
- });
- it('should extract XState machine exports', () => {
- const code = `
- export const authMachine = createMachine({
- id: "auth",
- initial: "idle",
- states: {
- idle: {},
- authenticated: {},
- },
- });
- `;
- const result = extractFromSource('machine.ts', code);
- const varNode = result.nodes.find((n) => n.kind === 'constant' && n.name === 'authMachine');
- expect(varNode).toBeDefined();
- expect(varNode?.isExported).toBe(true);
- });
- it('should extract calls from a top-level variable initializer (issue #425)', () => {
- const code = `
- import { getTokenMp } from './api/upload';
- const token = getTokenMp();
- `;
- const result = extractFromSource('app.ts', code);
- const call = result.unresolvedReferences.find(
- (ref) => ref.referenceKind === 'calls' && ref.referenceName === 'getTokenMp'
- );
- expect(call).toBeDefined();
- });
- });
- describe('File Node Extraction', () => {
- it('should create a file-kind node for each parsed file', () => {
- const code = `
- export function greet(name: string): string {
- return "Hello " + name;
- }
- `;
- const result = extractFromSource('greeter.ts', code);
- const fileNode = result.nodes.find((n) => n.kind === 'file');
- expect(fileNode).toBeDefined();
- expect(fileNode?.name).toBe('greeter.ts');
- expect(fileNode?.filePath).toBe('greeter.ts');
- expect(fileNode?.language).toBe('typescript');
- expect(fileNode?.startLine).toBe(1);
- });
- it('should create file nodes for Python files', () => {
- const code = `
- def main():
- pass
- `;
- const result = extractFromSource('main.py', code);
- const fileNode = result.nodes.find((n) => n.kind === 'file');
- expect(fileNode).toBeDefined();
- expect(fileNode?.name).toBe('main.py');
- expect(fileNode?.language).toBe('python');
- });
- it('should create containment edges from file node to top-level declarations', () => {
- const code = `
- export function foo() {}
- export function bar() {}
- `;
- const result = extractFromSource('fns.ts', code);
- const fileNode = result.nodes.find((n) => n.kind === 'file');
- expect(fileNode).toBeDefined();
- // There should be contains edges from the file node to each function
- const containsEdges = result.edges.filter(
- (e) => e.source === fileNode?.id && e.kind === 'contains'
- );
- expect(containsEdges.length).toBeGreaterThanOrEqual(2);
- });
- });
- describe('Python Extraction', () => {
- it('should extract function definitions', () => {
- const code = `
- def calculate_total(items: list, tax_rate: float) -> float:
- """Calculate total with tax."""
- subtotal = sum(item.price for item in items)
- return subtotal * (1 + tax_rate)
- `;
- const result = extractFromSource('calc.py', code);
- const fileNode = result.nodes.find((n) => n.kind === 'file');
- expect(fileNode).toBeDefined();
- const funcNode = result.nodes.find((n) => n.kind === 'function');
- expect(funcNode).toMatchObject({
- kind: 'function',
- name: 'calculate_total',
- language: 'python',
- });
- });
- it('should extract class definitions', () => {
- const code = `
- class UserService:
- """Service for managing users."""
- def __init__(self, db):
- self.db = db
- def get_user(self, user_id: str) -> User:
- return self.db.find_user(user_id)
- `;
- const result = extractFromSource('service.py', code);
- const classNode = result.nodes.find((n) => n.kind === 'class');
- expect(classNode).toBeDefined();
- expect(classNode?.name).toBe('UserService');
- });
- });
- describe('Go Extraction', () => {
- it('should extract function declarations', () => {
- const code = `
- package main
- func ProcessOrder(order Order) (Receipt, error) {
- // Process the order
- return Receipt{}, nil
- }
- `;
- const result = extractFromSource('main.go', code);
- const funcNode = result.nodes.find((n) => n.kind === 'function');
- expect(funcNode).toBeDefined();
- expect(funcNode?.name).toBe('ProcessOrder');
- });
- it('should extract method declarations', () => {
- const code = `
- package main
- type Service struct {
- db *Database
- }
- func (s *Service) GetUser(id string) (*User, error) {
- return s.db.FindUser(id)
- }
- `;
- const result = extractFromSource('service.go', code);
- const methodNode = result.nodes.find((n) => n.kind === 'method');
- expect(methodNode).toBeDefined();
- expect(methodNode?.name).toBe('GetUser');
- });
- });
- describe('Rust Extraction', () => {
- it('should extract function declarations', () => {
- const code = `
- pub fn process_data(input: &str) -> Result<Output, Error> {
- // Process data
- Ok(Output::new())
- }
- `;
- const result = extractFromSource('lib.rs', code);
- const funcNode = result.nodes.find((n) => n.kind === 'function');
- expect(funcNode).toBeDefined();
- expect(funcNode?.name).toBe('process_data');
- expect(funcNode?.visibility).toBe('public');
- });
- it('should extract struct declarations', () => {
- const code = `
- pub struct User {
- pub id: String,
- pub name: String,
- email: String,
- }
- `;
- const result = extractFromSource('models.rs', code);
- const structNode = result.nodes.find((n) => n.kind === 'struct');
- expect(structNode).toBeDefined();
- expect(structNode?.name).toBe('User');
- });
- it('should extract trait declarations', () => {
- const code = `
- pub trait Repository {
- fn find(&self, id: &str) -> Option<Entity>;
- fn save(&mut self, entity: Entity) -> Result<(), Error>;
- }
- `;
- const result = extractFromSource('traits.rs', code);
- const traitNode = result.nodes.find((n) => n.kind === 'trait');
- expect(traitNode).toBeDefined();
- expect(traitNode?.name).toBe('Repository');
- });
- it('should extract impl Trait for Type as implements edges', () => {
- const code = `
- pub struct MyCache {}
- pub trait Cache {
- fn get(&self, key: &str) -> Option<String>;
- }
- impl Cache for MyCache {
- fn get(&self, key: &str) -> Option<String> {
- None
- }
- }
- `;
- const result = extractFromSource('cache.rs', code);
- // Should have an unresolved reference for implements
- const implRef = result.unresolvedReferences.find(
- (r) => r.referenceKind === 'implements' && r.referenceName === 'Cache'
- );
- expect(implRef).toBeDefined();
- // The struct MyCache should be the source
- const myCacheNode = result.nodes.find((n) => n.name === 'MyCache' && n.kind === 'struct');
- expect(myCacheNode).toBeDefined();
- expect(implRef?.fromNodeId).toBe(myCacheNode?.id);
- });
- it('should extract trait supertraits as extends references', () => {
- const code = `
- pub trait Display {}
- pub trait Error: Display {
- fn description(&self) -> &str;
- }
- `;
- const result = extractFromSource('error.rs', code);
- const extendsRef = result.unresolvedReferences.find(
- (r) => r.referenceKind === 'extends' && r.referenceName === 'Display'
- );
- expect(extendsRef).toBeDefined();
- const errorTrait = result.nodes.find((n) => n.name === 'Error' && n.kind === 'trait');
- expect(errorTrait).toBeDefined();
- expect(extendsRef?.fromNodeId).toBe(errorTrait?.id);
- });
- it('should not create implements edges for plain impl blocks', () => {
- const code = `
- pub struct Counter {
- count: u32,
- }
- impl Counter {
- pub fn new() -> Counter {
- Counter { count: 0 }
- }
- pub fn increment(&mut self) {
- self.count += 1;
- }
- }
- `;
- const result = extractFromSource('counter.rs', code);
- // Should have no implements references (no trait involved)
- const implRefs = result.unresolvedReferences.filter(
- (r) => r.referenceKind === 'implements'
- );
- expect(implRefs).toHaveLength(0);
- });
- });
- describe('Java Extraction', () => {
- it('should extract class declarations', () => {
- const code = `
- public class UserService {
- private final UserRepository repository;
- public UserService(UserRepository repository) {
- this.repository = repository;
- }
- public User getUser(String id) {
- return repository.findById(id);
- }
- }
- `;
- const result = extractFromSource('UserService.java', code);
- const classNode = result.nodes.find((n) => n.kind === 'class');
- expect(classNode).toBeDefined();
- expect(classNode?.name).toBe('UserService');
- expect(classNode?.visibility).toBe('public');
- });
- it('should extract method declarations', () => {
- const code = `
- public class Calculator {
- public static int add(int a, int b) {
- return a + b;
- }
- }
- `;
- const result = extractFromSource('Calculator.java', code);
- const methodNode = result.nodes.find((n) => n.kind === 'method' && n.name === 'add');
- expect(methodNode).toBeDefined();
- expect(methodNode?.isStatic).toBe(true);
- });
- it('wraps top-level declarations in a namespace from package_declaration', () => {
- const code = `
- package com.example.foo;
- public class Bar {
- public String greet() { return "hi"; }
- }
- `;
- const result = extractFromSource('Bar.java', code);
- const ns = result.nodes.find((n) => n.kind === 'namespace');
- expect(ns?.name).toBe('com.example.foo');
- const cls = result.nodes.find((n) => n.kind === 'class' && n.name === 'Bar');
- expect(cls?.qualifiedName).toBe('com.example.foo::Bar');
- const greet = result.nodes.find((n) => n.kind === 'method' && n.name === 'greet');
- expect(greet?.qualifiedName).toBe('com.example.foo::Bar::greet');
- });
- it('does not wrap when no package is declared', () => {
- const code = `
- public class Bar {
- public String greet() { return "hi"; }
- }
- `;
- const result = extractFromSource('Bar.java', code);
- expect(result.nodes.find((n) => n.kind === 'namespace')).toBeUndefined();
- const cls = result.nodes.find((n) => n.kind === 'class' && n.name === 'Bar');
- expect(cls?.qualifiedName).toBe('Bar');
- });
- it('extracts anonymous-class overrides from `new T() { ... }`', () => {
- // The pattern that breaks the trace through `strategy.foo()` in
- // libraries like guava's Splitter: the lambda-returned anonymous
- // class overrides abstract methods on the base, but without
- // extracting those overrides the interface→impl synthesizer has
- // nothing to bridge.
- const code = `
- package com.example;
- abstract class Base {
- abstract int compute(int x);
- }
- public class Factory {
- public Base make() {
- return new Base() {
- @Override
- int compute(int x) { return x + 1; }
- };
- }
- }
- `;
- const result = extractFromSource('Factory.java', code);
- const anon = result.nodes.find((n) => n.kind === 'class' && /Base\$anon@/.test(n.name));
- expect(anon, 'anonymous Base subclass should be extracted as a class').toBeDefined();
- const compute = result.nodes.find(
- (n) => n.kind === 'method' && n.name === 'compute' && n.qualifiedName.includes('$anon@')
- );
- expect(compute, 'override method should be a method on the anon class').toBeDefined();
- expect(compute!.qualifiedName).toContain('Factory::make::<Base$anon@');
- expect(compute!.qualifiedName.endsWith('::compute')).toBe(true);
- // Anon class must extend Base so Phase 5.5 (interface-impl) can bridge.
- const extendsRef = result.unresolvedReferences.find(
- (r) => r.referenceKind === 'extends' && r.referenceName === 'Base' && r.fromNodeId === anon!.id
- );
- expect(extendsRef, 'anon class should carry an `extends Base` reference').toBeDefined();
- // The enclosing `make` method still emits an instantiates edge to Base —
- // anon extraction must not swallow that signal.
- const instantiatesRef = result.unresolvedReferences.find(
- (r) => r.referenceKind === 'instantiates' && r.referenceName === 'Base'
- );
- expect(instantiatesRef, 'enclosing method should still instantiate Base').toBeDefined();
- });
- it('extracts anonymous-class overrides inside a lambda body', () => {
- // The exact guava pattern: a lambda is passed to a constructor, and the
- // lambda body returns `new T() { @Override ... }`. The anon class must
- // still surface even though it sits inside a lambda_expression node.
- const code = `
- package com.example;
- interface Strategy {
- java.util.Iterator<String> iterator(String s);
- }
- abstract class BaseIter implements java.util.Iterator<String> {
- abstract int separatorStart(int start);
- }
- public class Splitter {
- private final Strategy strategy;
- public Splitter(Strategy s) { this.strategy = s; }
- public static Splitter on(char c) {
- return new Splitter((seq) ->
- new BaseIter() {
- @Override
- int separatorStart(int start) { return start + 1; }
- @Override public boolean hasNext() { return false; }
- @Override public String next() { return null; }
- });
- }
- }
- `;
- const result = extractFromSource('Splitter.java', code);
- const anon = result.nodes.find((n) => n.kind === 'class' && /BaseIter\$anon@/.test(n.name));
- expect(anon, 'anon BaseIter inside the lambda body should be extracted').toBeDefined();
- const sepStart = result.nodes.find(
- (n) =>
- n.kind === 'method' &&
- n.name === 'separatorStart' &&
- n.qualifiedName.includes('$anon@')
- );
- expect(sepStart, 'override inside the lambda-returned anon class should be a method node').toBeDefined();
- });
- });
- describe('C# Extraction', () => {
- it('should extract class declarations', () => {
- const code = `
- public class OrderService
- {
- private readonly IOrderRepository _repository;
- public OrderService(IOrderRepository repository)
- {
- _repository = repository;
- }
- public async Task<Order> GetOrderAsync(string id)
- {
- return await _repository.FindByIdAsync(id);
- }
- }
- `;
- const result = extractFromSource('OrderService.cs', code);
- const classNode = result.nodes.find((n) => n.kind === 'class');
- expect(classNode).toBeDefined();
- expect(classNode?.name).toBe('OrderService');
- expect(classNode?.visibility).toBe('public');
- });
- });
- describe('PHP Extraction', () => {
- it('should extract class declarations', () => {
- const code = `<?php
- class UserController
- {
- private UserService $userService;
- public function __construct(UserService $userService)
- {
- $this->userService = $userService;
- }
- public function show(string $id): User
- {
- return $this->userService->find($id);
- }
- }
- `;
- const result = extractFromSource('UserController.php', code);
- const classNode = result.nodes.find((n) => n.kind === 'class');
- expect(classNode).toBeDefined();
- expect(classNode?.name).toBe('UserController');
- });
- it('should extract class inheritance (extends) and interface implementation', () => {
- const code = `<?php
- class ChildController extends BaseController implements Serializable, JsonSerializable
- {
- public function serialize(): string
- {
- return json_encode($this);
- }
- }
- `;
- const result = extractFromSource('ChildController.php', code);
- const classNode = result.nodes.find((n) => n.kind === 'class');
- expect(classNode).toBeDefined();
- expect(classNode?.name).toBe('ChildController');
- const extendsRef = result.unresolvedReferences.find(
- (r) => r.referenceKind === 'extends'
- );
- expect(extendsRef).toBeDefined();
- expect(extendsRef?.referenceName).toBe('BaseController');
- const implementsRefs = result.unresolvedReferences.filter(
- (r) => r.referenceKind === 'implements'
- );
- expect(implementsRefs.length).toBe(2);
- expect(implementsRefs.map((r) => r.referenceName)).toContain('Serializable');
- expect(implementsRefs.map((r) => r.referenceName)).toContain('JsonSerializable');
- });
- });
- describe('Swift Extraction', () => {
- it('should extract class declarations', () => {
- const code = `
- public class NetworkManager {
- private let session: URLSession
- public init(session: URLSession = .shared) {
- self.session = session
- }
- public func fetchData(from url: URL) async throws -> Data {
- let (data, _) = try await session.data(from: url)
- return data
- }
- }
- `;
- const result = extractFromSource('NetworkManager.swift', code);
- const classNode = result.nodes.find((n) => n.kind === 'class');
- expect(classNode).toBeDefined();
- expect(classNode?.name).toBe('NetworkManager');
- });
- it('should extract function declarations', () => {
- const code = `
- func calculateSum(_ numbers: [Int]) -> Int {
- return numbers.reduce(0, +)
- }
- public func formatCurrency(amount: Double) -> String {
- return String(format: "$%.2f", amount)
- }
- `;
- const result = extractFromSource('utils.swift', code);
- const functions = result.nodes.filter((n) => n.kind === 'function');
- expect(functions.length).toBeGreaterThanOrEqual(1);
- });
- it('should extract struct declarations', () => {
- const code = `
- public struct User {
- let id: UUID
- var name: String
- var email: String
- func displayName() -> String {
- return name
- }
- }
- `;
- const result = extractFromSource('User.swift', code);
- const structNode = result.nodes.find((n) => n.kind === 'struct');
- expect(structNode).toBeDefined();
- expect(structNode?.name).toBe('User');
- });
- it('should extract protocol declarations', () => {
- const code = `
- public protocol Repository {
- associatedtype Entity
- func find(id: String) async throws -> Entity?
- func save(_ entity: Entity) async throws
- }
- `;
- const result = extractFromSource('Repository.swift', code);
- const protocolNode = result.nodes.find((n) => n.kind === 'interface');
- expect(protocolNode).toBeDefined();
- expect(protocolNode?.name).toBe('Repository');
- });
- it('should extract class inheritance and protocol conformance', () => {
- const code = `
- class DataRequest: Request {
- func validate() {}
- }
- class UploadRequest: DataRequest, Sendable {
- func upload() {}
- }
- enum AFError: Error {
- case invalidURL
- }
- struct HTTPMethod: RawRepresentable {
- let rawValue: String
- }
- protocol UploadConvertible: URLRequestConvertible {
- func asURLRequest() throws -> URLRequest
- }
- `;
- const result = extractFromSource('Inheritance.swift', code);
- const extendsRefs = result.unresolvedReferences.filter(
- (r) => r.referenceKind === 'extends'
- );
- // DataRequest extends Request
- expect(extendsRefs.find((r) => r.referenceName === 'Request')).toBeDefined();
- // UploadRequest extends DataRequest and Sendable
- expect(extendsRefs.find((r) => r.referenceName === 'DataRequest')).toBeDefined();
- expect(extendsRefs.find((r) => r.referenceName === 'Sendable')).toBeDefined();
- // AFError extends Error
- expect(extendsRefs.find((r) => r.referenceName === 'Error')).toBeDefined();
- // HTTPMethod extends RawRepresentable
- expect(extendsRefs.find((r) => r.referenceName === 'RawRepresentable')).toBeDefined();
- // UploadConvertible extends URLRequestConvertible
- expect(extendsRefs.find((r) => r.referenceName === 'URLRequestConvertible')).toBeDefined();
- });
- });
- describe('Kotlin Extraction', () => {
- it('should extract class declarations', () => {
- const code = `
- class UserRepository(private val database: Database) {
- fun findById(id: String): User? {
- return database.query("SELECT * FROM users WHERE id = ?", id)
- }
- suspend fun save(user: User) {
- database.insert(user)
- }
- }
- `;
- const result = extractFromSource('UserRepository.kt', code);
- const classNode = result.nodes.find((n) => n.kind === 'class');
- expect(classNode).toBeDefined();
- expect(classNode?.name).toBe('UserRepository');
- });
- it('should extract function declarations', () => {
- const code = `
- fun calculateTotal(items: List<Item>): Double {
- return items.sumOf { it.price }
- }
- suspend fun fetchUserData(userId: String): User {
- return api.getUser(userId)
- }
- `;
- const result = extractFromSource('utils.kt', code);
- const functions = result.nodes.filter((n) => n.kind === 'function');
- expect(functions.length).toBeGreaterThanOrEqual(1);
- });
- it('should detect suspend functions as async', () => {
- const code = `
- suspend fun loadData(): List<String> {
- delay(1000)
- return listOf("a", "b", "c")
- }
- `;
- const result = extractFromSource('loader.kt', code);
- const funcNode = result.nodes.find((n) => n.kind === 'function');
- expect(funcNode).toBeDefined();
- expect(funcNode?.isAsync).toBe(true);
- });
- it('should extract fun interface declarations', () => {
- const code = `
- fun interface OnObjectRetainedListener {
- fun onObjectRetained()
- }
- `;
- const result = extractFromSource('listener.kt', code);
- const ifaceNode = result.nodes.find((n) => n.kind === 'interface');
- expect(ifaceNode).toBeDefined();
- expect(ifaceNode?.name).toBe('OnObjectRetainedListener');
- const methodNode = result.nodes.find((n) => n.kind === 'method');
- expect(methodNode).toBeDefined();
- expect(methodNode?.name).toBe('onObjectRetained');
- expect(methodNode?.qualifiedName).toBe('OnObjectRetainedListener::onObjectRetained');
- });
- it('should extract complex fun interface with nested classes', () => {
- const code = `
- fun interface EventListener {
- fun onEvent(event: Event)
- sealed class Event {
- class DumpingHeap : Event()
- }
- }
- `;
- const result = extractFromSource('events.kt', code);
- const ifaceNode = result.nodes.find((n) => n.kind === 'interface');
- expect(ifaceNode).toBeDefined();
- expect(ifaceNode?.name).toBe('EventListener');
- // Nested sealed class should still be extracted (as sibling due to grammar limitations)
- const eventClass = result.nodes.find((n) => n.kind === 'class' && n.name === 'Event');
- expect(eventClass).toBeDefined();
- const dumpingHeap = result.nodes.find((n) => n.kind === 'class' && n.name === 'DumpingHeap');
- expect(dumpingHeap).toBeDefined();
- });
- it('should not affect regular function declarations', () => {
- const code = `
- fun interface MyCallback {
- fun invoke(value: Int)
- }
- fun regularFunction(): String {
- return "hello"
- }
- `;
- const result = extractFromSource('mixed.kt', code);
- const ifaceNode = result.nodes.find((n) => n.kind === 'interface');
- expect(ifaceNode).toBeDefined();
- expect(ifaceNode?.name).toBe('MyCallback');
- const funcNode = result.nodes.find((n) => n.kind === 'function');
- expect(funcNode).toBeDefined();
- expect(funcNode?.name).toBe('regularFunction');
- });
- it('should extract fun interface with annotation on method (Pattern 2b)', () => {
- // When the SAM method has annotations like @Throws, tree-sitter produces a different
- // misparse: function_declaration > ERROR("interface Name {") instead of
- // function_declaration > user_type("interface"). This is the OkHttp Interceptor pattern.
- const code = `
- import java.io.IOException
- fun interface Interceptor {
- @Throws(IOException::class)
- fun intercept(chain: Chain): Response
- }
- `;
- const result = extractFromSource('interceptor.kt', code);
- const ifaceNode = result.nodes.find((n) => n.kind === 'interface');
- expect(ifaceNode).toBeDefined();
- expect(ifaceNode?.name).toBe('Interceptor');
- });
- it('should extract methods from interface with nested fun interface', () => {
- // When an interface contains a nested `fun interface`, tree-sitter misparsed
- // the parent body as ERROR. Methods inside should still be extracted.
- const code = `
- interface WebSocket {
- fun request(): Request
- fun send(text: String): Boolean
- fun cancel()
- fun interface Factory {
- fun newWebSocket(request: Request): WebSocket
- }
- }
- `;
- const result = extractFromSource('websocket.kt', code);
- const wsIface = result.nodes.find((n) => n.kind === 'interface' && n.name === 'WebSocket');
- expect(wsIface).toBeDefined();
- const methods = result.nodes.filter((n) => n.kind === 'method' && n.qualifiedName?.startsWith('WebSocket::'));
- const methodNames = methods.map((m) => m.name);
- expect(methodNames).toContain('request');
- expect(methodNames).toContain('send');
- expect(methodNames).toContain('cancel');
- });
- it('wraps top-level declarations in a namespace from package_header', () => {
- const code = `
- package com.example.foo
- class Bar {
- fun greet(): String = "hi"
- }
- fun util(): Int = 42
- `;
- const result = extractFromSource('Bar.kt', code);
- const ns = result.nodes.find((n) => n.kind === 'namespace');
- expect(ns?.name).toBe('com.example.foo');
- const cls = result.nodes.find((n) => n.kind === 'class' && n.name === 'Bar');
- expect(cls?.qualifiedName).toBe('com.example.foo::Bar');
- const greet = result.nodes.find((n) => n.kind === 'method' && n.name === 'greet');
- expect(greet?.qualifiedName).toBe('com.example.foo::Bar::greet');
- const util = result.nodes.find((n) => n.kind === 'function' && n.name === 'util');
- expect(util?.qualifiedName).toBe('com.example.foo::util');
- });
- it('handles a single-segment package', () => {
- const code = `
- package foo
- class Bar
- `;
- const result = extractFromSource('Bar.kt', code);
- const cls = result.nodes.find((n) => n.kind === 'class' && n.name === 'Bar');
- expect(cls?.qualifiedName).toBe('foo::Bar');
- });
- it('does not wrap when no package is declared', () => {
- const code = `
- class Bar {
- fun greet() = "hi"
- }
- `;
- const result = extractFromSource('Bar.kt', code);
- expect(result.nodes.find((n) => n.kind === 'namespace')).toBeUndefined();
- const cls = result.nodes.find((n) => n.kind === 'class' && n.name === 'Bar');
- expect(cls?.qualifiedName).toBe('Bar');
- });
- });
- describe('Dart Extraction', () => {
- it('should extract class declarations', () => {
- const code = `
- class UserService {
- final Database _db;
- Future<User> findById(String id) async {
- return await _db.query(id);
- }
- void _privateMethod() {}
- }
- `;
- const result = extractFromSource('service.dart', code);
- const classNode = result.nodes.find((n) => n.kind === 'class');
- expect(classNode).toBeDefined();
- expect(classNode?.name).toBe('UserService');
- expect(classNode?.visibility).toBe('public');
- const methodNodes = result.nodes.filter((n) => n.kind === 'method');
- expect(methodNodes.length).toBeGreaterThanOrEqual(2);
- const findById = methodNodes.find((m) => m.name === 'findById');
- expect(findById).toBeDefined();
- expect(findById?.isAsync).toBe(true);
- const privateMethod = methodNodes.find((m) => m.name === '_privateMethod');
- expect(privateMethod).toBeDefined();
- expect(privateMethod?.visibility).toBe('private');
- // Dart models a method body as a SIBLING of the signature, so the method
- // node must be extended to span its body (not just the signature line) —
- // required for body-level analysis (callees, the callback synthesizer).
- expect(findById!.endLine).toBeGreaterThan(findById!.startLine);
- });
- it('should extract top-level function declarations', () => {
- const code = `
- void topLevelFunction(String name) {
- print(name);
- }
- `;
- const result = extractFromSource('utils.dart', code);
- const funcNode = result.nodes.find((n) => n.kind === 'function');
- expect(funcNode).toBeDefined();
- expect(funcNode?.name).toBe('topLevelFunction');
- expect(funcNode?.language).toBe('dart');
- });
- it('should extract enum declarations', () => {
- const code = `
- enum Status { active, inactive, pending }
- `;
- const result = extractFromSource('models.dart', code);
- const enumNode = result.nodes.find((n) => n.kind === 'enum');
- expect(enumNode).toBeDefined();
- expect(enumNode?.name).toBe('Status');
- });
- it('should extract mixin declarations', () => {
- const code = `
- mixin LoggerMixin {
- void log(String message) {}
- }
- `;
- const result = extractFromSource('mixins.dart', code);
- const classNode = result.nodes.find((n) => n.kind === 'class');
- expect(classNode).toBeDefined();
- expect(classNode?.name).toBe('LoggerMixin');
- const methodNode = result.nodes.find((n) => n.kind === 'method');
- expect(methodNode).toBeDefined();
- expect(methodNode?.name).toBe('log');
- });
- it('should extract extension declarations', () => {
- const code = `
- extension StringExt on String {
- bool get isBlank => trim().isEmpty;
- }
- `;
- const result = extractFromSource('extensions.dart', code);
- const classNode = result.nodes.find((n) => n.kind === 'class');
- expect(classNode).toBeDefined();
- expect(classNode?.name).toBe('StringExt');
- });
- it('should detect static methods', () => {
- const code = `
- class Utils {
- static void doWork() {}
- }
- `;
- const result = extractFromSource('utils.dart', code);
- const methodNode = result.nodes.find((n) => n.kind === 'method');
- expect(methodNode).toBeDefined();
- expect(methodNode?.name).toBe('doWork');
- expect(methodNode?.isStatic).toBe(true);
- });
- it('should detect async functions', () => {
- const code = `
- Future<String> fetchData() async {
- return await http.get('/data');
- }
- `;
- const result = extractFromSource('api.dart', code);
- const funcNode = result.nodes.find((n) => n.kind === 'function');
- expect(funcNode).toBeDefined();
- expect(funcNode?.name).toBe('fetchData');
- expect(funcNode?.isAsync).toBe(true);
- });
- it('should detect private visibility via underscore convention', () => {
- const code = `
- void _privateHelper() {}
- void publicFunction() {}
- `;
- const result = extractFromSource('helpers.dart', code);
- const functions = result.nodes.filter((n) => n.kind === 'function');
- const privateFunc = functions.find((f) => f.name === '_privateHelper');
- const publicFunc = functions.find((f) => f.name === 'publicFunction');
- expect(privateFunc?.visibility).toBe('private');
- expect(publicFunc?.visibility).toBe('public');
- });
- });
- describe('Import Extraction', () => {
- describe('TypeScript/JavaScript imports', () => {
- it('should extract default imports', () => {
- const code = `import React from 'react';`;
- const result = extractFromSource('app.tsx', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('react');
- expect(importNode?.signature).toBe("import React from 'react';");
- });
- it('should extract named imports', () => {
- const code = `import { Bug, Database } from '@phosphor-icons/react';`;
- const result = extractFromSource('icons.tsx', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('@phosphor-icons/react');
- expect(importNode?.signature).toContain('Bug');
- expect(importNode?.signature).toContain('Database');
- });
- it('should extract namespace imports', () => {
- const code = `import * as Icons from '@phosphor-icons/react';`;
- const result = extractFromSource('icons.tsx', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('@phosphor-icons/react');
- expect(importNode?.signature).toContain('* as Icons');
- });
- it('should extract side-effect imports', () => {
- const code = `import './styles.css';`;
- const result = extractFromSource('app.tsx', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('./styles.css');
- });
- it('should extract mixed imports (default + named)', () => {
- const code = `import React, { useState, useEffect } from 'react';`;
- const result = extractFromSource('app.tsx', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('react');
- expect(importNode?.signature).toContain('React');
- expect(importNode?.signature).toContain('useState');
- expect(importNode?.signature).toContain('useEffect');
- });
- it('should extract multiple import statements', () => {
- const code = `
- import React from 'react';
- import { Button } from './components';
- import './styles.css';
- `;
- const result = extractFromSource('app.tsx', code);
- const importNodes = result.nodes.filter((n) => n.kind === 'import');
- expect(importNodes.length).toBe(3);
- const names = importNodes.map((n) => n.name);
- expect(names).toContain('react');
- expect(names).toContain('./components');
- expect(names).toContain('./styles.css');
- });
- it('should extract type imports', () => {
- const code = `import type { FC, ReactNode } from 'react';`;
- const result = extractFromSource('types.ts', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('react');
- expect(importNode?.signature).toContain('type');
- expect(importNode?.signature).toContain('FC');
- });
- it('should extract aliased named imports', () => {
- const code = `import { useState as useStateAlias } from 'react';`;
- const result = extractFromSource('hooks.ts', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('react');
- expect(importNode?.signature).toContain('useState');
- expect(importNode?.signature).toContain('useStateAlias');
- });
- it('should extract relative path imports', () => {
- const code = `import { helper } from '../utils/helper';`;
- const result = extractFromSource('components/Button.tsx', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('../utils/helper');
- expect(importNode?.signature).toContain('helper');
- });
- });
- describe('Python imports', () => {
- it('should extract simple import statement', () => {
- const code = `import json`;
- const result = extractFromSource('utils.py', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('json');
- });
- it('should extract from import statement', () => {
- const code = `from os import path`;
- const result = extractFromSource('utils.py', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('os');
- expect(importNode?.signature).toContain('path');
- });
- it('should extract multiple imports from same module', () => {
- const code = `from typing import List, Dict, Optional`;
- const result = extractFromSource('types.py', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('typing');
- expect(importNode?.signature).toContain('List');
- expect(importNode?.signature).toContain('Dict');
- });
- it('should extract multiple import statements', () => {
- const code = `
- import os
- import sys
- `;
- const result = extractFromSource('main.py', code);
- const importNodes = result.nodes.filter((n) => n.kind === 'import');
- expect(importNodes.length).toBe(2);
- const names = importNodes.map((n) => n.name);
- expect(names).toContain('os');
- expect(names).toContain('sys');
- });
- it('should extract aliased import', () => {
- const code = `import numpy as np`;
- const result = extractFromSource('data.py', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('numpy');
- expect(importNode?.signature).toContain('as np');
- });
- it('should extract relative import', () => {
- const code = `from .utils import helper`;
- const result = extractFromSource('module.py', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('.utils');
- expect(importNode?.signature).toContain('helper');
- });
- it('should extract wildcard import', () => {
- const code = `from typing import *`;
- const result = extractFromSource('types.py', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('typing');
- expect(importNode?.signature).toContain('*');
- });
- });
- describe('Rust imports', () => {
- it('should extract simple use declaration', () => {
- const code = `use std::io;`;
- const result = extractFromSource('main.rs', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('std');
- expect(importNode?.signature).toBe('use std::io;');
- });
- it('should extract scoped use list', () => {
- const code = `use std::{ffi::OsStr, io, path::Path};`;
- const result = extractFromSource('main.rs', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('std');
- expect(importNode?.signature).toContain('ffi::OsStr');
- expect(importNode?.signature).toContain('path::Path');
- });
- it('should extract crate imports', () => {
- const code = `use crate::error::Error;`;
- const result = extractFromSource('lib.rs', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('crate');
- });
- it('should extract super imports', () => {
- const code = `use super::utils;`;
- const result = extractFromSource('submod.rs', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('super');
- });
- it('should extract external crate imports', () => {
- const code = `use serde::{Serialize, Deserialize};`;
- const result = extractFromSource('types.rs', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('serde');
- expect(importNode?.signature).toContain('Serialize');
- expect(importNode?.signature).toContain('Deserialize');
- });
- });
- describe('Go imports', () => {
- it('should extract single import', () => {
- const code = `
- package main
- import "fmt"
- `;
- const result = extractFromSource('main.go', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('fmt');
- });
- it('should extract grouped imports', () => {
- const code = `
- package main
- import (
- "fmt"
- "os"
- "encoding/json"
- )
- `;
- const result = extractFromSource('main.go', code);
- const importNodes = result.nodes.filter((n) => n.kind === 'import');
- expect(importNodes.length).toBe(3);
- const names = importNodes.map((n) => n.name);
- expect(names).toContain('fmt');
- expect(names).toContain('os');
- expect(names).toContain('encoding/json');
- });
- it('should extract aliased import', () => {
- const code = `
- package main
- import f "fmt"
- `;
- const result = extractFromSource('main.go', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('fmt');
- expect(importNode?.signature).toContain('f');
- });
- it('should extract dot import', () => {
- const code = `
- package main
- import . "math"
- `;
- const result = extractFromSource('main.go', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('math');
- expect(importNode?.signature).toContain('.');
- });
- it('should extract blank import', () => {
- const code = `
- package main
- import _ "github.com/go-sql-driver/mysql"
- `;
- const result = extractFromSource('main.go', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('github.com/go-sql-driver/mysql');
- expect(importNode?.signature).toContain('_');
- });
- });
- describe('Swift imports', () => {
- it('should extract simple import', () => {
- const code = `import Foundation`;
- const result = extractFromSource('main.swift', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('Foundation');
- expect(importNode?.signature).toBe('import Foundation');
- });
- it('should extract @testable import', () => {
- const code = `@testable import Alamofire`;
- const result = extractFromSource('Tests.swift', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('Alamofire');
- expect(importNode?.signature).toContain('@testable');
- });
- it('should extract @preconcurrency import', () => {
- const code = `@preconcurrency import Security`;
- const result = extractFromSource('Auth.swift', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('Security');
- });
- it('should extract multiple imports', () => {
- const code = `
- import Foundation
- import UIKit
- import Alamofire
- `;
- const result = extractFromSource('App.swift', code);
- const importNodes = result.nodes.filter((n) => n.kind === 'import');
- expect(importNodes.length).toBe(3);
- const names = importNodes.map((n) => n.name);
- expect(names).toContain('Foundation');
- expect(names).toContain('UIKit');
- expect(names).toContain('Alamofire');
- });
- });
- describe('Kotlin imports', () => {
- it('should extract simple import', () => {
- const code = `import java.io.IOException`;
- const result = extractFromSource('Main.kt', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('java.io.IOException');
- expect(importNode?.signature).toBe('import java.io.IOException');
- });
- it('should extract aliased import', () => {
- const code = `import okhttp3.Request.Builder as RequestBuilder`;
- const result = extractFromSource('Utils.kt', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('okhttp3.Request.Builder');
- expect(importNode?.signature).toContain('as RequestBuilder');
- });
- it('should extract wildcard import', () => {
- const code = `import java.util.concurrent.TimeUnit.*`;
- const result = extractFromSource('Time.kt', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('java.util.concurrent.TimeUnit');
- expect(importNode?.signature).toContain('.*');
- });
- it('should extract multiple imports', () => {
- const code = `
- import java.io.IOException
- import kotlin.test.assertFailsWith
- import okhttp3.OkHttpClient
- `;
- const result = extractFromSource('Test.kt', code);
- const importNodes = result.nodes.filter((n) => n.kind === 'import');
- expect(importNodes.length).toBe(3);
- const names = importNodes.map((n) => n.name);
- expect(names).toContain('java.io.IOException');
- expect(names).toContain('kotlin.test.assertFailsWith');
- expect(names).toContain('okhttp3.OkHttpClient');
- });
- });
- describe('Java imports', () => {
- it('should extract simple import', () => {
- const code = `import java.util.List;`;
- const result = extractFromSource('Main.java', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('java.util.List');
- expect(importNode?.signature).toBe('import java.util.List;');
- });
- it('should extract static import', () => {
- const code = `import static java.util.Collections.emptyList;`;
- const result = extractFromSource('Utils.java', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('java.util.Collections.emptyList');
- expect(importNode?.signature).toContain('static');
- });
- it('should extract wildcard import', () => {
- const code = `import java.util.*;`;
- const result = extractFromSource('App.java', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('java.util');
- expect(importNode?.signature).toContain('.*');
- });
- it('should extract nested class import', () => {
- const code = `import java.util.Map.Entry;`;
- const result = extractFromSource('MapUtil.java', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('java.util.Map.Entry');
- });
- it('should extract multiple imports', () => {
- const code = `
- import java.util.List;
- import java.util.Map;
- import java.io.IOException;
- `;
- const result = extractFromSource('Service.java', code);
- const importNodes = result.nodes.filter((n) => n.kind === 'import');
- expect(importNodes.length).toBe(3);
- const names = importNodes.map((n) => n.name);
- expect(names).toContain('java.util.List');
- expect(names).toContain('java.util.Map');
- expect(names).toContain('java.io.IOException');
- });
- });
- describe('C# imports', () => {
- it('should extract simple using', () => {
- const code = `using System;`;
- const result = extractFromSource('Program.cs', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('System');
- expect(importNode?.signature).toBe('using System;');
- });
- it('should extract qualified using', () => {
- const code = `using System.Collections.Generic;`;
- const result = extractFromSource('Utils.cs', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('System.Collections.Generic');
- });
- it('should extract static using', () => {
- const code = `using static System.Console;`;
- const result = extractFromSource('App.cs', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('System.Console');
- expect(importNode?.signature).toContain('static');
- });
- it('should extract alias using', () => {
- const code = `using MyList = System.Collections.Generic.List<int>;`;
- const result = extractFromSource('Types.cs', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('System.Collections.Generic.List<int>');
- expect(importNode?.signature).toContain('MyList =');
- });
- it('should extract multiple usings', () => {
- const code = `
- using System;
- using System.Threading.Tasks;
- using Microsoft.Extensions.DependencyInjection;
- `;
- const result = extractFromSource('Service.cs', code);
- const importNodes = result.nodes.filter((n) => n.kind === 'import');
- expect(importNodes.length).toBe(3);
- const names = importNodes.map((n) => n.name);
- expect(names).toContain('System');
- expect(names).toContain('System.Threading.Tasks');
- expect(names).toContain('Microsoft.Extensions.DependencyInjection');
- });
- });
- describe('PHP imports', () => {
- it('should extract simple use', () => {
- const code = `<?php use PHPUnit\\Framework\\TestCase;`;
- const result = extractFromSource('Test.php', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('PHPUnit\\Framework\\TestCase');
- });
- it('should extract aliased use', () => {
- const code = `<?php use Mockery as m;`;
- const result = extractFromSource('Test.php', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('Mockery');
- expect(importNode?.signature).toContain('as m');
- });
- it('should extract function use', () => {
- const code = `<?php use function Illuminate\\Support\\env;`;
- const result = extractFromSource('helpers.php', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('Illuminate\\Support\\env');
- expect(importNode?.signature).toContain('function');
- });
- it('should extract grouped use', () => {
- const code = `<?php use Illuminate\\Database\\{Model, Builder};`;
- const result = extractFromSource('Models.php', code);
- const importNodes = result.nodes.filter((n) => n.kind === 'import');
- expect(importNodes.length).toBe(2);
- const names = importNodes.map((n) => n.name);
- expect(names).toContain('Illuminate\\Database\\Model');
- expect(names).toContain('Illuminate\\Database\\Builder');
- });
- it('should extract multiple uses', () => {
- const code = `<?php
- use Illuminate\\Support\\Collection;
- use Illuminate\\Support\\Str;
- use Closure;
- `;
- const result = extractFromSource('Service.php', code);
- const importNodes = result.nodes.filter((n) => n.kind === 'import');
- expect(importNodes.length).toBe(3);
- const names = importNodes.map((n) => n.name);
- expect(names).toContain('Illuminate\\Support\\Collection');
- expect(names).toContain('Illuminate\\Support\\Str');
- expect(names).toContain('Closure');
- });
- });
- describe('Ruby imports', () => {
- it('should extract require', () => {
- const code = `require 'json'`;
- const result = extractFromSource('app.rb', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('json');
- expect(importNode?.signature).toBe("require 'json'");
- });
- it('should extract require with path', () => {
- const code = `require 'active_support/core_ext/string'`;
- const result = extractFromSource('config.rb', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('active_support/core_ext/string');
- });
- it('should extract require_relative', () => {
- const code = `require_relative '../test_helper'`;
- const result = extractFromSource('test/my_test.rb', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('../test_helper');
- expect(importNode?.signature).toContain('require_relative');
- });
- it('should not extract non-require calls', () => {
- const code = `puts 'hello'`;
- const result = extractFromSource('app.rb', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeUndefined();
- });
- it('should extract multiple requires', () => {
- const code = `
- require 'json'
- require 'yaml'
- require_relative 'helper'
- `;
- const result = extractFromSource('lib.rb', code);
- const importNodes = result.nodes.filter((n) => n.kind === 'import');
- expect(importNodes.length).toBe(3);
- const names = importNodes.map((n) => n.name);
- expect(names).toContain('json');
- expect(names).toContain('yaml');
- expect(names).toContain('helper');
- });
- });
- describe('Ruby modules', () => {
- it('should extract module as module node with containment', () => {
- const code = `
- module CachedCounting
- def self.disable
- @enabled = false
- end
- def perform_increment!(key, count)
- write_cache!(key, count)
- end
- end
- `;
- const result = extractFromSource('concerns/cached_counting.rb', code);
- const moduleNode = result.nodes.find((n) => n.kind === 'module' && n.name === 'CachedCounting');
- expect(moduleNode).toBeDefined();
- expect(moduleNode?.qualifiedName).toBe('CachedCounting');
- // Methods inside module should have module-qualified names
- const disableMethod = result.nodes.find((n) => n.name === 'disable' && n.kind === 'method');
- expect(disableMethod).toBeDefined();
- expect(disableMethod?.qualifiedName).toBe('CachedCounting::disable');
- const incrementMethod = result.nodes.find((n) => n.name === 'perform_increment!' && n.kind === 'method');
- expect(incrementMethod).toBeDefined();
- expect(incrementMethod?.qualifiedName).toBe('CachedCounting::perform_increment!');
- // Containment edge from module to methods
- const containsEdges = result.edges.filter((e) => e.source === moduleNode?.id && e.kind === 'contains');
- expect(containsEdges.length).toBeGreaterThanOrEqual(2);
- });
- it('should handle nested modules with classes', () => {
- const code = `
- module Discourse
- module Auth
- class AuthProvider
- def authenticate(params)
- validate(params)
- end
- end
- end
- end
- `;
- const result = extractFromSource('lib/auth.rb', code);
- const discourseModule = result.nodes.find((n) => n.kind === 'module' && n.name === 'Discourse');
- expect(discourseModule).toBeDefined();
- const authModule = result.nodes.find((n) => n.kind === 'module' && n.name === 'Auth');
- expect(authModule).toBeDefined();
- expect(authModule?.qualifiedName).toBe('Discourse::Auth');
- const authProvider = result.nodes.find((n) => n.kind === 'class' && n.name === 'AuthProvider');
- expect(authProvider).toBeDefined();
- expect(authProvider?.qualifiedName).toBe('Discourse::Auth::AuthProvider');
- const authMethod = result.nodes.find((n) => n.name === 'authenticate');
- expect(authMethod).toBeDefined();
- expect(authMethod?.qualifiedName).toBe('Discourse::Auth::AuthProvider::authenticate');
- });
- });
- describe('C/C++ imports', () => {
- it('should extract system include', () => {
- const code = `#include <iostream>`;
- const result = extractFromSource('main.cpp', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('iostream');
- expect(importNode?.signature).toBe('#include <iostream>');
- });
- it('should extract system include with path', () => {
- const code = `#include <nlohmann/json.hpp>`;
- const result = extractFromSource('app.cpp', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('nlohmann/json.hpp');
- });
- it('should extract local include', () => {
- const code = `#include "myheader.h"`;
- const result = extractFromSource('main.cpp', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('myheader.h');
- });
- it('should extract C header', () => {
- const code = `#include <stdio.h>`;
- const result = extractFromSource('main.c', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('stdio.h');
- });
- it('should extract multiple includes', () => {
- const code = `
- #include <iostream>
- #include <vector>
- #include "config.h"
- `;
- const result = extractFromSource('app.cpp', code);
- const importNodes = result.nodes.filter((n) => n.kind === 'import');
- expect(importNodes.length).toBe(3);
- const names = importNodes.map((n) => n.name);
- expect(names).toContain('iostream');
- expect(names).toContain('vector');
- expect(names).toContain('config.h');
- });
- it('should create unresolved references for local includes', () => {
- const code = `#include "myheader.h"`;
- const result = extractFromSource('main.cpp', code);
- const importRef = result.unresolvedReferences.find(
- (r) => r.referenceKind === 'imports' && r.referenceName === 'myheader.h'
- );
- expect(importRef).toBeDefined();
- expect(importRef?.line).toBe(1);
- });
- it('should create unresolved references for system includes', () => {
- const code = `#include <iostream>`;
- const result = extractFromSource('main.cpp', code);
- const importRef = result.unresolvedReferences.find(
- (r) => r.referenceKind === 'imports' && r.referenceName === 'iostream'
- );
- expect(importRef).toBeDefined();
- });
- });
- describe('Dart imports', () => {
- it('should extract dart: import', () => {
- const code = `import 'dart:async';`;
- const result = extractFromSource('main.dart', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('dart:async');
- expect(importNode?.signature).toBe("import 'dart:async';");
- });
- it('should extract package import', () => {
- const code = `import 'package:flutter/material.dart';`;
- const result = extractFromSource('app.dart', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('package:flutter/material.dart');
- });
- it('should extract aliased import', () => {
- const code = `import 'package:http/http.dart' as http;`;
- const result = extractFromSource('api.dart', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('package:http/http.dart');
- expect(importNode?.signature).toContain('as http');
- });
- it('should extract multiple imports', () => {
- const code = `
- import 'dart:async';
- import 'dart:convert';
- import 'package:flutter/material.dart';
- `;
- const result = extractFromSource('main.dart', code);
- const importNodes = result.nodes.filter((n) => n.kind === 'import');
- expect(importNodes.length).toBe(3);
- const names = importNodes.map((n) => n.name);
- expect(names).toContain('dart:async');
- expect(names).toContain('dart:convert');
- expect(names).toContain('package:flutter/material.dart');
- });
- it('should extract relative import', () => {
- const code = `import '../utils/helpers.dart';`;
- const result = extractFromSource('lib/main.dart', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('../utils/helpers.dart');
- });
- });
- describe('Liquid imports', () => {
- it('should extract render tag', () => {
- const code = `{% render 'loading-spinner' %}`;
- const result = extractFromSource('template.liquid', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('loading-spinner');
- expect(importNode?.signature).toContain('render');
- });
- it('should extract section tag', () => {
- const code = `{% section 'header' %}`;
- const result = extractFromSource('layout/theme.liquid', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('header');
- expect(importNode?.signature).toContain('section');
- });
- it('should extract include tag', () => {
- const code = `{% include 'icon-cart' %}`;
- const result = extractFromSource('snippets/header.liquid', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('icon-cart');
- expect(importNode?.signature).toContain('include');
- });
- it('should extract render with whitespace control', () => {
- const code = `{%- render 'price' -%}`;
- const result = extractFromSource('snippets/product.liquid', code);
- const importNode = result.nodes.find((n) => n.kind === 'import');
- expect(importNode).toBeDefined();
- expect(importNode?.name).toBe('price');
- });
- it('should extract multiple imports', () => {
- const code = `
- {% section 'header' %}
- {% render 'loading-spinner' %}
- {% render 'cart-drawer' %}
- `;
- const result = extractFromSource('layout/theme.liquid', code);
- const importNodes = result.nodes.filter((n) => n.kind === 'import');
- expect(importNodes.length).toBe(3);
- const names = importNodes.map((n) => n.name);
- expect(names).toContain('header');
- expect(names).toContain('loading-spinner');
- expect(names).toContain('cart-drawer');
- });
- });
- });
- // =============================================================================
- // Pascal / Delphi Extraction
- // =============================================================================
- describe('Pascal / Delphi Extraction', () => {
- describe('Language detection', () => {
- it('should detect Pascal files', () => {
- expect(detectLanguage('UAuth.pas')).toBe('pascal');
- expect(detectLanguage('App.dpr')).toBe('pascal');
- expect(detectLanguage('Package.dpk')).toBe('pascal');
- expect(detectLanguage('App.lpr')).toBe('pascal');
- expect(detectLanguage('MainForm.dfm')).toBe('pascal');
- expect(detectLanguage('MainForm.fmx')).toBe('pascal');
- });
- it('should report Pascal as supported', () => {
- expect(isLanguageSupported('pascal')).toBe(true);
- expect(getSupportedLanguages()).toContain('pascal');
- });
- });
- describe('Unit extraction', () => {
- it('should extract unit as module', () => {
- const code = `unit MyUnit;\ninterface\nimplementation\nend.`;
- const result = extractFromSource('MyUnit.pas', code);
- const moduleNode = result.nodes.find((n) => n.kind === 'module');
- expect(moduleNode).toBeDefined();
- expect(moduleNode?.name).toBe('MyUnit');
- expect(moduleNode?.language).toBe('pascal');
- });
- it('should extract program as module', () => {
- const code = `program MyApp;\nbegin\nend.`;
- const result = extractFromSource('MyApp.dpr', code);
- const moduleNode = result.nodes.find((n) => n.kind === 'module');
- expect(moduleNode).toBeDefined();
- expect(moduleNode?.name).toBe('MyApp');
- });
- it('should fallback to filename when module name is empty', () => {
- // Some .dpr templates use "program;" without a name
- const code = `program;\nuses SysUtils;\nbegin\nend.`;
- const result = extractFromSource('Console.dpr', code);
- const moduleNode = result.nodes.find((n) => n.kind === 'module');
- expect(moduleNode).toBeDefined();
- expect(moduleNode?.name).toBe('Console');
- });
- });
- describe('Uses clause (imports)', () => {
- it('should extract uses as individual imports', () => {
- const code = `unit Test;\ninterface\nuses\n System.SysUtils,\n System.Classes;\nimplementation\nend.`;
- const result = extractFromSource('Test.pas', code);
- const imports = result.nodes.filter((n) => n.kind === 'import');
- expect(imports.length).toBe(2);
- expect(imports.map((n) => n.name)).toContain('System.SysUtils');
- expect(imports.map((n) => n.name)).toContain('System.Classes');
- });
- it('should create unresolved references for imports', () => {
- const code = `unit Test;\ninterface\nuses\n UAuth;\nimplementation\nend.`;
- const result = extractFromSource('Test.pas', code);
- const importRef = result.unresolvedReferences.find(
- (r) => r.referenceKind === 'imports'
- );
- expect(importRef).toBeDefined();
- expect(importRef?.referenceName).toBe('UAuth');
- });
- });
- describe('Class extraction', () => {
- it('should extract class declarations', () => {
- const code = `unit Test;\ninterface\ntype\n TMyClass = class\n public\n procedure DoSomething;\n end;\nimplementation\nend.`;
- const result = extractFromSource('Test.pas', code);
- const classNode = result.nodes.find((n) => n.kind === 'class');
- expect(classNode).toBeDefined();
- expect(classNode?.name).toBe('TMyClass');
- });
- it('should extract class with inheritance', () => {
- const code = `unit Test;\ninterface\ntype\n TChild = class(TParent)\n end;\nimplementation\nend.`;
- const result = extractFromSource('Test.pas', code);
- const extendsRef = result.unresolvedReferences.find(
- (r) => r.referenceKind === 'extends'
- );
- expect(extendsRef).toBeDefined();
- expect(extendsRef?.referenceName).toBe('TParent');
- });
- it('should extract class with interface implementation', () => {
- const code = `unit Test;\ninterface\ntype\n TService = class(TInterfacedObject, ILogger)\n end;\nimplementation\nend.`;
- const result = extractFromSource('Test.pas', code);
- const extendsRef = result.unresolvedReferences.find(
- (r) => r.referenceKind === 'extends'
- );
- const implementsRef = result.unresolvedReferences.find(
- (r) => r.referenceKind === 'implements'
- );
- expect(extendsRef?.referenceName).toBe('TInterfacedObject');
- expect(implementsRef?.referenceName).toBe('ILogger');
- });
- });
- describe('Record extraction', () => {
- it('should extract records as class nodes', () => {
- const code = `unit Test;\ninterface\ntype\n TPoint = record\n X: Double;\n Y: Double;\n end;\nimplementation\nend.`;
- const result = extractFromSource('Test.pas', code);
- const classNode = result.nodes.find((n) => n.kind === 'class');
- expect(classNode).toBeDefined();
- expect(classNode?.name).toBe('TPoint');
- const fields = result.nodes.filter((n) => n.kind === 'field');
- expect(fields.length).toBe(2);
- expect(fields.map((f) => f.name)).toContain('X');
- expect(fields.map((f) => f.name)).toContain('Y');
- });
- });
- describe('Interface extraction', () => {
- it('should extract interface declarations', () => {
- const code = `unit Test;\ninterface\ntype\n ILogger = interface\n procedure Log(const AMsg: string);\n end;\nimplementation\nend.`;
- const result = extractFromSource('Test.pas', code);
- const ifaceNode = result.nodes.find((n) => n.kind === 'interface');
- expect(ifaceNode).toBeDefined();
- expect(ifaceNode?.name).toBe('ILogger');
- });
- });
- describe('Method extraction', () => {
- it('should extract methods with visibility', () => {
- const code = `unit Test;\ninterface\ntype\n TMyClass = class\n private\n FValue: Integer;\n public\n constructor Create;\n function GetValue: Integer;\n end;\nimplementation\nend.`;
- const result = extractFromSource('Test.pas', code);
- const methods = result.nodes.filter((n) => n.kind === 'method');
- expect(methods.length).toBe(2);
- const createMethod = methods.find((m) => m.name === 'Create');
- expect(createMethod?.visibility).toBe('public');
- const getValue = methods.find((m) => m.name === 'GetValue');
- expect(getValue?.visibility).toBe('public');
- const fields = result.nodes.filter((n) => n.kind === 'field');
- const fValue = fields.find((f) => f.name === 'FValue');
- expect(fValue?.visibility).toBe('private');
- });
- it('should detect static methods (class methods)', () => {
- const code = `unit Test;\ninterface\ntype\n THelper = class\n public\n class function Create: THelper; static;\n end;\nimplementation\nend.`;
- const result = extractFromSource('Test.pas', code);
- const methods = result.nodes.filter((n) => n.kind === 'method');
- const staticMethod = methods.find((m) => m.name === 'Create');
- expect(staticMethod?.isStatic).toBe(true);
- });
- });
- describe('Enum extraction', () => {
- it('should extract enums with members', () => {
- const code = `unit Test;\ninterface\ntype\n TColor = (clRed, clGreen, clBlue);\nimplementation\nend.`;
- const result = extractFromSource('Test.pas', code);
- const enumNode = result.nodes.find((n) => n.kind === 'enum');
- expect(enumNode).toBeDefined();
- expect(enumNode?.name).toBe('TColor');
- const members = result.nodes.filter((n) => n.kind === 'enum_member');
- expect(members.length).toBe(3);
- expect(members.map((m) => m.name)).toEqual(['clRed', 'clGreen', 'clBlue']);
- });
- });
- describe('Property extraction', () => {
- it('should extract properties', () => {
- const code = `unit Test;\ninterface\ntype\n TObj = class\n public\n property Name: string read FName write FName;\n end;\nimplementation\nend.`;
- const result = extractFromSource('Test.pas', code);
- const propNode = result.nodes.find((n) => n.kind === 'property');
- expect(propNode).toBeDefined();
- expect(propNode?.name).toBe('Name');
- expect(propNode?.visibility).toBe('public');
- });
- });
- describe('Constant extraction', () => {
- it('should extract constants', () => {
- const code = `unit Test;\ninterface\nconst\n MAX_RETRIES = 3;\n APP_NAME = 'MyApp';\nimplementation\nend.`;
- const result = extractFromSource('Test.pas', code);
- const constants = result.nodes.filter((n) => n.kind === 'constant');
- expect(constants.length).toBe(2);
- expect(constants.map((c) => c.name)).toContain('MAX_RETRIES');
- expect(constants.map((c) => c.name)).toContain('APP_NAME');
- });
- });
- describe('Type alias extraction', () => {
- it('should extract type aliases', () => {
- const code = `unit Test;\ninterface\ntype\n TUserName = string;\nimplementation\nend.`;
- const result = extractFromSource('Test.pas', code);
- const aliasNode = result.nodes.find((n) => n.kind === 'type_alias');
- expect(aliasNode).toBeDefined();
- expect(aliasNode?.name).toBe('TUserName');
- });
- });
- describe('Call extraction', () => {
- it('should extract calls from implementation bodies', () => {
- const code = `unit Test;\ninterface\ntype\n TObj = class\n public\n procedure DoWork;\n end;\nimplementation\nprocedure TObj.DoWork;\nbegin\n WriteLn('hello');\nend;\nend.`;
- const result = extractFromSource('Test.pas', code);
- const callRef = result.unresolvedReferences.find(
- (r) => r.referenceKind === 'calls'
- );
- expect(callRef).toBeDefined();
- expect(callRef?.referenceName).toBe('WriteLn');
- });
- });
- describe('Containment edges', () => {
- it('should create contains edges for class members', () => {
- const code = `unit Test;\ninterface\ntype\n TObj = class\n public\n procedure Foo;\n end;\nimplementation\nend.`;
- const result = extractFromSource('Test.pas', code);
- const classNode = result.nodes.find((n) => n.kind === 'class');
- const methodNode = result.nodes.find((n) => n.kind === 'method');
- expect(classNode).toBeDefined();
- expect(methodNode).toBeDefined();
- const containsEdge = result.edges.find(
- (e) => e.source === classNode?.id && e.target === methodNode?.id && e.kind === 'contains'
- );
- expect(containsEdge).toBeDefined();
- });
- });
- describe('Full fixture: UAuth.pas', () => {
- const code = `unit UAuth;
- interface
- uses
- System.SysUtils,
- System.Classes;
- type
- ITokenValidator = interface
- ['{11111111-1111-1111-1111-111111111111}']
- function Validate(const AToken: string): Boolean;
- end;
- TAuthService = class(TInterfacedObject, ITokenValidator)
- private
- FToken: string;
- FLoginCount: Integer;
- procedure IncLoginCount;
- protected
- function GetToken: string;
- public
- constructor Create;
- destructor Destroy; override;
- function Validate(const AToken: string): Boolean;
- function Login(const AUser, APass: string): string;
- property Token: string read GetToken;
- property LoginCount: Integer read FLoginCount;
- end;
- implementation
- constructor TAuthService.Create;
- begin
- inherited Create;
- FToken := '';
- FLoginCount := 0;
- end;
- destructor TAuthService.Destroy;
- begin
- FToken := '';
- inherited Destroy;
- end;
- procedure TAuthService.IncLoginCount;
- begin
- Inc(FLoginCount);
- end;
- function TAuthService.GetToken: string;
- begin
- Result := FToken;
- end;
- function TAuthService.Validate(const AToken: string): Boolean;
- begin
- Result := AToken <> '';
- end;
- function TAuthService.Login(const AUser, APass: string): string;
- begin
- IncLoginCount;
- if Validate(AUser + ':' + APass) then
- begin
- FToken := AUser;
- Result := 'ok';
- end
- else
- Result := '';
- end;
- end.`;
- it('should extract all expected nodes', () => {
- const result = extractFromSource('UAuth.pas', code);
- expect(result.errors).toHaveLength(0);
- // Module
- const moduleNode = result.nodes.find((n) => n.kind === 'module');
- expect(moduleNode?.name).toBe('UAuth');
- // Imports
- const imports = result.nodes.filter((n) => n.kind === 'import');
- expect(imports.length).toBe(2);
- // Interface
- const ifaceNode = result.nodes.find((n) => n.kind === 'interface');
- expect(ifaceNode?.name).toBe('ITokenValidator');
- // Class
- const classNode = result.nodes.find((n) => n.kind === 'class');
- expect(classNode?.name).toBe('TAuthService');
- // Methods
- const methods = result.nodes.filter((n) => n.kind === 'method');
- expect(methods.length).toBeGreaterThanOrEqual(6);
- expect(methods.map((m) => m.name)).toContain('Create');
- expect(methods.map((m) => m.name)).toContain('Destroy');
- expect(methods.map((m) => m.name)).toContain('Login');
- // Fields
- const fields = result.nodes.filter((n) => n.kind === 'field');
- expect(fields.length).toBe(2);
- expect(fields.every((f) => f.visibility === 'private')).toBe(true);
- // Properties
- const props = result.nodes.filter((n) => n.kind === 'property');
- expect(props.length).toBe(2);
- expect(props.map((p) => p.name)).toContain('Token');
- expect(props.map((p) => p.name)).toContain('LoginCount');
- });
- it('should extract inheritance and interface implementation', () => {
- const result = extractFromSource('UAuth.pas', code);
- const extendsRef = result.unresolvedReferences.find(
- (r) => r.referenceKind === 'extends'
- );
- expect(extendsRef?.referenceName).toBe('TInterfacedObject');
- const implementsRef = result.unresolvedReferences.find(
- (r) => r.referenceKind === 'implements'
- );
- expect(implementsRef?.referenceName).toBe('ITokenValidator');
- });
- it('should extract calls from implementation', () => {
- const result = extractFromSource('UAuth.pas', code);
- const callRefs = result.unresolvedReferences.filter(
- (r) => r.referenceKind === 'calls'
- );
- expect(callRefs.map((r) => r.referenceName)).toContain('Inc');
- expect(callRefs.map((r) => r.referenceName)).toContain('Validate');
- });
- });
- describe('Full fixture: UTypes.pas', () => {
- const code = `unit UTypes;
- interface
- uses
- System.SysUtils;
- const
- C_MAX_RETRIES = 3;
- C_DEFAULT_NAME = 'Guest';
- type
- TUserRole = (urAdmin, urEditor, urViewer);
- TPoint2D = record
- X: Double;
- Y: Double;
- end;
- TUserName = string;
- TUserInfo = class
- public
- type
- TAddress = record
- Street: string;
- City: string;
- Zip: string;
- end;
- private
- FName: TUserName;
- FRole: TUserRole;
- FAddress: TAddress;
- public
- constructor Create(const AName: TUserName; ARole: TUserRole);
- function GetDisplayName: string;
- class function CreateAdmin(const AName: TUserName): TUserInfo; static;
- property Name: TUserName read FName write FName;
- property Role: TUserRole read FRole;
- property Address: TAddress read FAddress write FAddress;
- end;
- implementation
- constructor TUserInfo.Create(const AName: TUserName; ARole: TUserRole);
- begin
- FName := AName;
- FRole := ARole;
- end;
- function TUserInfo.GetDisplayName: string;
- begin
- if FRole = urAdmin then
- Result := '[Admin] ' + FName
- else
- Result := FName;
- end;
- class function TUserInfo.CreateAdmin(const AName: TUserName): TUserInfo;
- begin
- Result := TUserInfo.Create(AName, urAdmin);
- end;
- end.`;
- it('should extract enums with members', () => {
- const result = extractFromSource('UTypes.pas', code);
- const enumNode = result.nodes.find((n) => n.kind === 'enum');
- expect(enumNode?.name).toBe('TUserRole');
- const members = result.nodes.filter((n) => n.kind === 'enum_member');
- expect(members.length).toBe(3);
- expect(members.map((m) => m.name)).toEqual(['urAdmin', 'urEditor', 'urViewer']);
- });
- it('should extract constants', () => {
- const result = extractFromSource('UTypes.pas', code);
- const constants = result.nodes.filter((n) => n.kind === 'constant');
- expect(constants.length).toBe(2);
- expect(constants.map((c) => c.name)).toContain('C_MAX_RETRIES');
- expect(constants.map((c) => c.name)).toContain('C_DEFAULT_NAME');
- });
- it('should extract type aliases', () => {
- const result = extractFromSource('UTypes.pas', code);
- const aliases = result.nodes.filter((n) => n.kind === 'type_alias');
- expect(aliases.map((a) => a.name)).toContain('TUserName');
- });
- it('should extract records as classes with fields', () => {
- const result = extractFromSource('UTypes.pas', code);
- const classes = result.nodes.filter((n) => n.kind === 'class');
- expect(classes.map((c) => c.name)).toContain('TPoint2D');
- // TPoint2D fields
- const fields = result.nodes.filter((n) => n.kind === 'field');
- expect(fields.map((f) => f.name)).toContain('X');
- expect(fields.map((f) => f.name)).toContain('Y');
- });
- it('should extract static class methods', () => {
- const result = extractFromSource('UTypes.pas', code);
- const methods = result.nodes.filter((n) => n.kind === 'method');
- const staticMethod = methods.find((m) => m.name === 'CreateAdmin');
- expect(staticMethod).toBeDefined();
- expect(staticMethod?.isStatic).toBe(true);
- });
- it('should extract nested types', () => {
- const result = extractFromSource('UTypes.pas', code);
- const classes = result.nodes.filter((n) => n.kind === 'class');
- expect(classes.map((c) => c.name)).toContain('TAddress');
- });
- });
- });
- // =============================================================================
- // DFM/FMX Extraction
- // =============================================================================
- describe('DFM/FMX Extraction', () => {
- it('should extract components from DFM', () => {
- const code = `object Form1: TForm1
- Left = 0
- Top = 0
- Caption = 'My Form'
- object Button1: TButton
- Left = 10
- Top = 10
- Caption = 'Click Me'
- end
- end`;
- const result = extractFromSource('Form1.dfm', code);
- const components = result.nodes.filter((n) => n.kind === 'component');
- expect(components.length).toBe(2);
- expect(components.map((c) => c.name)).toContain('Form1');
- expect(components.map((c) => c.name)).toContain('Button1');
- const button = components.find((c) => c.name === 'Button1');
- expect(button?.signature).toBe('TButton');
- });
- it('should extract nested component hierarchy', () => {
- const code = `object Form1: TForm1
- object Panel1: TPanel
- object Label1: TLabel
- Caption = 'Hello'
- end
- end
- end`;
- const result = extractFromSource('Form1.dfm', code);
- const components = result.nodes.filter((n) => n.kind === 'component');
- expect(components.length).toBe(3);
- // Check nesting: Panel1 contains Label1
- const panel = components.find((c) => c.name === 'Panel1');
- const label = components.find((c) => c.name === 'Label1');
- const containsEdge = result.edges.find(
- (e) => e.source === panel?.id && e.target === label?.id && e.kind === 'contains'
- );
- expect(containsEdge).toBeDefined();
- });
- it('should extract event handler references', () => {
- const code = `object Form1: TForm1
- OnCreate = FormCreate
- OnDestroy = FormDestroy
- object Button1: TButton
- OnClick = Button1Click
- end
- end`;
- const result = extractFromSource('Form1.dfm', code);
- const refs = result.unresolvedReferences;
- expect(refs.length).toBe(3);
- expect(refs.map((r) => r.referenceName)).toContain('FormCreate');
- expect(refs.map((r) => r.referenceName)).toContain('FormDestroy');
- expect(refs.map((r) => r.referenceName)).toContain('Button1Click');
- expect(refs.every((r) => r.referenceKind === 'references')).toBe(true);
- });
- it('should handle multi-line properties', () => {
- const code = `object Form1: TForm1
- SQL.Strings = (
- 'SELECT * FROM users'
- 'WHERE active = 1')
- object Button1: TButton
- OnClick = Button1Click
- end
- end`;
- const result = extractFromSource('Form1.dfm', code);
- const components = result.nodes.filter((n) => n.kind === 'component');
- expect(components.length).toBe(2);
- const refs = result.unresolvedReferences;
- expect(refs.length).toBe(1);
- expect(refs[0]?.referenceName).toBe('Button1Click');
- });
- it('should handle inherited keyword', () => {
- const code = `inherited Form1: TForm1
- Caption = 'Inherited Form'
- object Button1: TButton
- OnClick = Button1Click
- end
- end`;
- const result = extractFromSource('Form1.dfm', code);
- const components = result.nodes.filter((n) => n.kind === 'component');
- expect(components.length).toBe(2);
- expect(components.map((c) => c.name)).toContain('Form1');
- });
- it('should handle item collection properties', () => {
- const code = `object Form1: TForm1
- object StatusBar1: TStatusBar
- Panels = <
- item
- Width = 200
- end
- item
- Width = 200
- end>
- end
- end`;
- const result = extractFromSource('Form1.dfm', code);
- const components = result.nodes.filter((n) => n.kind === 'component');
- expect(components.length).toBe(2);
- });
- describe('Full fixture: MainForm.dfm', () => {
- const code = `object frmMain: TfrmMain
- Left = 0
- Top = 0
- Caption = 'CodeGraph DFM Fixture'
- ClientHeight = 480
- ClientWidth = 640
- OnCreate = FormCreate
- OnDestroy = FormDestroy
- object pnlTop: TPanel
- Left = 0
- Top = 0
- Width = 640
- Height = 50
- object lblTitle: TLabel
- Left = 16
- Top = 16
- Caption = 'Authentication Service'
- end
- object btnLogin: TButton
- Left = 540
- Top = 12
- OnClick = btnLoginClick
- end
- end
- object pnlContent: TPanel
- Left = 0
- Top = 50
- object edtUsername: TEdit
- Left = 16
- Top = 16
- OnChange = edtUsernameChange
- end
- object edtPassword: TEdit
- Left = 16
- Top = 48
- OnKeyPress = edtPasswordKeyPress
- end
- object mmoLog: TMemo
- Left = 16
- Top = 88
- end
- end
- object pnlStatus: TStatusBar
- Left = 0
- Top = 440
- Panels = <
- item
- Width = 200
- end
- item
- Width = 200
- end>
- end
- end`;
- it('should extract all components', () => {
- const result = extractFromSource('MainForm.dfm', code);
- const components = result.nodes.filter((n) => n.kind === 'component');
- expect(components.length).toBe(9);
- expect(components.map((c) => c.name)).toEqual(
- expect.arrayContaining([
- 'frmMain', 'pnlTop', 'lblTitle', 'btnLogin',
- 'pnlContent', 'edtUsername', 'edtPassword', 'mmoLog', 'pnlStatus',
- ])
- );
- });
- it('should extract all event handlers', () => {
- const result = extractFromSource('MainForm.dfm', code);
- const refs = result.unresolvedReferences;
- expect(refs.length).toBe(5);
- expect(refs.map((r) => r.referenceName)).toEqual(
- expect.arrayContaining([
- 'FormCreate', 'FormDestroy', 'btnLoginClick',
- 'edtUsernameChange', 'edtPasswordKeyPress',
- ])
- );
- });
- });
- });
- describe('Kotlin Multiplatform expect/actual', () => {
- let tempDir: string;
- let cg: CodeGraph;
- beforeEach(() => {
- tempDir = createTempDir();
- });
- afterEach(() => {
- if (cg) cg.close();
- if (fs.existsSync(tempDir)) fs.rmSync(tempDir, { recursive: true, force: true });
- });
- it('links expect declarations to platform actual implementations and surfaces them in impact', async () => {
- const common = path.join(tempDir, 'src', 'commonMain');
- const jvm = path.join(tempDir, 'src', 'jvmMain');
- fs.mkdirSync(common, { recursive: true });
- fs.mkdirSync(jvm, { recursive: true });
- // common source set: expect declarations + a caller that uses them
- fs.writeFileSync(
- path.join(common, 'SystemProps.kt'),
- `package demo.internal
- expect fun systemProp(name: String): String?
- expect class Platform {
- fun describe(): String
- }
- `
- );
- fs.writeFileSync(
- path.join(common, 'Caller.kt'),
- `package demo
- import demo.internal.systemProp
- import demo.internal.Platform
- fun useIt(): String {
- val v = systemProp("os.name")
- return Platform().describe() + v
- }
- `
- );
- // jvm source set: actual implementations
- fs.writeFileSync(
- path.join(jvm, 'SystemProps.kt'),
- `package demo.internal
- actual fun systemProp(name: String): String? = System.getProperty(name)
- actual class Platform {
- actual fun describe(): String = "JVM"
- }
- `
- );
- cg = CodeGraph.initSync(tempDir);
- await cg.indexAll();
- cg.resolveReferences();
- // The expect/actual markers are captured onto the node's decorators.
- const fns = cg.getNodesByKind('function');
- const actualFn = fns.find(
- (n) => n.name === 'systemProp' && n.decorators?.includes('actual')
- );
- const expectFn = fns.find(
- (n) => n.name === 'systemProp' && n.decorators?.includes('expect')
- );
- expect(actualFn).toBeDefined();
- expect(expectFn).toBeDefined();
- expect(actualFn!.filePath).not.toBe(expectFn!.filePath);
- // Editing the JVM actual must surface the common expect AND its caller —
- // before the expect/actual bridge the actual had zero dependents.
- const impact = cg.getImpactRadius(actualFn!.id, 3);
- const impacted = [...impact.nodes.values()].map((n) => n.name);
- expect(impacted).toContain('systemProp'); // the common expect
- expect(impacted).toContain('useIt'); // the caller, reached transitively
- // The bridging edge is a heuristic `calls` edge tagged by the synthesizer.
- const bridge = impact.edges.find(
- (e) =>
- e.target === actualFn!.id &&
- e.provenance === 'heuristic' &&
- (e.metadata as { synthesizedBy?: string } | undefined)?.synthesizedBy ===
- 'kotlin-expect-actual'
- );
- expect(bridge).toBeDefined();
- expect(bridge!.source).toBe(expectFn!.id);
- });
- it('links an expect class to an actual typealias (different node kinds)', async () => {
- const common = path.join(tempDir, 'src', 'commonMain');
- const jvm = path.join(tempDir, 'src', 'jvmMain');
- fs.mkdirSync(common, { recursive: true });
- fs.mkdirSync(jvm, { recursive: true });
- fs.writeFileSync(
- path.join(common, 'Lock.kt'),
- `package demo
- expect class Lock {
- fun acquire()
- }
- `
- );
- fs.writeFileSync(
- path.join(jvm, 'Lock.kt'),
- `package demo
- actual typealias Lock = java.util.concurrent.locks.ReentrantLock
- `
- );
- cg = CodeGraph.initSync(tempDir);
- await cg.indexAll();
- cg.resolveReferences();
- const aliasNode = cg
- .getNodesByKind('type_alias')
- .find((n) => n.name === 'Lock' && n.decorators?.includes('actual'));
- expect(aliasNode).toBeDefined();
- // The actual typealias is now a cross-file dependency target (linked from
- // the expect class), so it participates in impact rather than being orphaned.
- const impact = cg.getImpactRadius(aliasNode!.id, 3);
- const bridge = impact.edges.find(
- (e) =>
- e.target === aliasNode!.id &&
- (e.metadata as { synthesizedBy?: string } | undefined)?.synthesizedBy ===
- 'kotlin-expect-actual'
- );
- expect(bridge).toBeDefined();
- });
- });
- describe('Scala cross-file dependencies', () => {
- let tempDir: string;
- let cg: CodeGraph;
- beforeEach(() => {
- tempDir = createTempDir();
- });
- afterEach(() => {
- if (cg) cg.close();
- if (fs.existsSync(tempDir)) fs.rmSync(tempDir, { recursive: true, force: true });
- });
- it('links parameterized supertypes, type annotations, and implicit params across files', async () => {
- const src = path.join(tempDir, 'src', 'main', 'scala', 'demo');
- fs.mkdirSync(src, { recursive: true });
- fs.writeFileSync(
- path.join(src, 'Semigroup.scala'),
- `package demo
- trait Semigroup[A] {
- def combine(x: A, y: A): A
- }
- `
- );
- fs.writeFileSync(
- path.join(src, 'Monoid.scala'),
- `package demo
- trait Monoid[A] extends Semigroup[A] {
- def empty: A
- }
- `
- );
- fs.writeFileSync(
- path.join(src, 'Instances.scala'),
- `package demo
- object Instances {
- implicit val intMonoid: Monoid[Int] = new Monoid[Int] {
- def empty: Int = 0
- def combine(x: Int, y: Int): Int = x + y
- }
- }
- `
- );
- fs.writeFileSync(
- path.join(src, 'Folding.scala'),
- `package demo
- object Folding {
- def fold[A](xs: List[A])(implicit M: Monoid[A]): A =
- xs.foldLeft(M.empty)(M.combine)
- }
- `
- );
- cg = CodeGraph.initSync(tempDir);
- await cg.indexAll();
- cg.resolveReferences();
- const monoid = cg.getNodesByKind('trait').find((n) => n.name === 'Monoid');
- const semigroup = cg.getNodesByKind('trait').find((n) => n.name === 'Semigroup');
- expect(monoid).toBeDefined();
- expect(semigroup).toBeDefined();
- expect(monoid!.filePath).not.toBe(semigroup!.filePath);
- // Parameterized supertype `extends Semigroup[A]` must create an extends edge —
- // the whole point of the fix (the `[A]` used to defeat name matching).
- const semaImpact = cg.getImpactRadius(semigroup!.id, 3);
- expect([...semaImpact.nodes.values()].map((n) => n.name)).toContain('Monoid');
- // Editing Monoid surfaces the cross-file users: the instance val typed
- // `Monoid[Int]` and the method taking it as an implicit (curried) param.
- const impacted = [...cg.getImpactRadius(monoid!.id, 3).nodes.values()].map((n) => n.name);
- expect(impacted).toContain('intMonoid'); // field type annotation
- expect(impacted).toContain('fold'); // trailing implicit parameter list
- });
- });
- describe('PHP namespace + import resolution', () => {
- let tempDir: string;
- let cg: CodeGraph;
- beforeEach(() => {
- tempDir = createTempDir();
- });
- afterEach(() => {
- if (cg) cg.close();
- if (fs.existsSync(tempDir)) fs.rmSync(tempDir, { recursive: true, force: true });
- });
- it('resolves `use` imports to the namespace-qualified definition and type-hints across files', async () => {
- const src = path.join(tempDir, 'src');
- // Two interfaces with the SAME simple name in different namespaces — the
- // exact ambiguity (Laravel has 7+ `Factory`) that bare-name matching can't
- // resolve. The namespace qualifies them; the `use` import disambiguates.
- fs.mkdirSync(path.join(src, 'Cache'), { recursive: true });
- fs.mkdirSync(path.join(src, 'Mail'), { recursive: true });
- fs.mkdirSync(path.join(src, 'App'), { recursive: true });
- fs.writeFileSync(
- path.join(src, 'Cache', 'Factory.php'),
- `<?php
- namespace Contracts\\Cache;
- interface Factory {
- public function store(): object;
- }
- `
- );
- fs.writeFileSync(
- path.join(src, 'Mail', 'Factory.php'),
- `<?php
- namespace Contracts\\Mail;
- interface Factory {
- public function mailer(): object;
- }
- `
- );
- fs.writeFileSync(
- path.join(src, 'App', 'Service.php'),
- `<?php
- namespace App;
- use Contracts\\Cache\\Factory;
- class Service {
- public function make(): Factory {
- return resolve(Factory::class);
- }
- }
- `
- );
- cg = CodeGraph.initSync(tempDir);
- await cg.indexAll();
- cg.resolveReferences();
- // The PHP namespace is captured into the qualified name, so the two
- // same-named interfaces are distinguishable.
- const cacheFactory = cg
- .getNodesByKind('interface')
- .find((n) => n.qualifiedName === 'Contracts\\Cache::Factory');
- const mailFactory = cg
- .getNodesByKind('interface')
- .find((n) => n.qualifiedName === 'Contracts\\Mail::Factory');
- expect(cacheFactory).toBeDefined();
- expect(mailFactory).toBeDefined();
- // Service `use`s Contracts\Cache\Factory, so editing THAT interface reaches
- // Service.php — and editing the same-named Contracts\Mail\Factory must NOT
- // (the import resolved to the right namespace, not an arbitrary `Factory`).
- const serviceFile = 'src/App/Service.php';
- const cacheReaches = [...cg.getImpactRadius(cacheFactory!.id, 3).nodes.values()].some(
- (n) => (n.filePath ?? '').endsWith(serviceFile)
- );
- const mailReaches = [...cg.getImpactRadius(mailFactory!.id, 3).nodes.values()].some(
- (n) => (n.filePath ?? '').endsWith(serviceFile)
- );
- expect(cacheReaches).toBe(true);
- expect(mailReaches).toBe(false);
- });
- });
- describe('Ruby mixins (include/extend/prepend)', () => {
- let tempDir: string;
- let cg: CodeGraph;
- beforeEach(() => {
- tempDir = createTempDir();
- });
- afterEach(() => {
- if (cg) cg.close();
- if (fs.existsSync(tempDir)) fs.rmSync(tempDir, { recursive: true, force: true });
- });
- it('links include/extend/prepend to the mixed-in module across files', async () => {
- const lib = path.join(tempDir, 'lib');
- fs.mkdirSync(lib, { recursive: true });
- fs.writeFileSync(
- path.join(lib, 'concerns.rb'),
- `module Trackable
- def track; end
- end
- module Cacheable
- def cache; end
- end
- module Loggable
- def log; end
- end
- `
- );
- fs.writeFileSync(
- path.join(lib, 'model.rb'),
- `class Model
- include Trackable
- prepend Cacheable
- extend Loggable
- end
- `
- );
- cg = CodeGraph.initSync(tempDir);
- await cg.indexAll();
- cg.resolveReferences();
- const model = cg.getNodesByKind('class').find((n) => n.name === 'Model');
- expect(model).toBeDefined();
- // All three mixin forms create an `implements` edge Model → module, so
- // editing a concern surfaces every class that mixes it in (across files).
- for (const moduleName of ['Trackable', 'Cacheable', 'Loggable']) {
- const mod = cg.getNodesByKind('module').find((n) => n.name === moduleName);
- expect(mod, moduleName).toBeDefined();
- const impacted = [...cg.getImpactRadius(mod!.id, 3).nodes.values()].map((n) => n.name);
- expect(impacted, `${moduleName} should be depended on by Model`).toContain('Model');
- }
- });
- it('resolves require / require_relative to the required file', async () => {
- const lib = path.join(tempDir, 'lib');
- fs.mkdirSync(path.join(lib, 'app'), { recursive: true });
- // A leaf file whose class is referenced only dynamically — so without
- // require resolution it would look like nothing depends on it.
- fs.writeFileSync(
- path.join(lib, 'app', 'fetcher.rb'),
- `module App
- class Fetcher
- def fetch; end
- end
- end
- `
- );
- // Pulled in by a load-path `require` …
- fs.writeFileSync(
- path.join(lib, 'app', 'worker.rb'),
- `require "app/fetcher"
- module App
- class Worker; end
- end
- `
- );
- // … and a sibling pulled in by `require_relative`.
- fs.writeFileSync(
- path.join(lib, 'app', 'boot.rb'),
- `require_relative "fetcher"
- `
- );
- cg = CodeGraph.initSync(tempDir);
- await cg.indexAll();
- cg.resolveReferences();
- // The require edges target fetcher.rb's FILE node. Editing it should reach
- // BOTH the load-path requirer (worker.rb) and the require_relative one
- // (boot.rb) — without require resolution its file would have no dependents.
- const fetcher = cg.getNodesByKind('file').find((n) => n.filePath.endsWith('app/fetcher.rb'));
- expect(fetcher, 'fetcher.rb indexed').toBeDefined();
- const reached = [...cg.getImpactRadius(fetcher!.id, 2).nodes.values()].map((n) => n.filePath ?? '');
- expect(reached.some((p) => p.endsWith('app/worker.rb'))).toBe(true);
- expect(reached.some((p) => p.endsWith('app/boot.rb'))).toBe(true);
- });
- });
- describe('C++ free-function name extraction', () => {
- let tempDir: string;
- let cg: CodeGraph;
- beforeEach(() => {
- tempDir = createTempDir();
- });
- afterEach(() => {
- if (cg) cg.close();
- if (fs.existsSync(tempDir)) fs.rmSync(tempDir, { recursive: true, force: true });
- });
- it('names a free function correctly when it has qualified-type params or a trailing return type', async () => {
- const src = path.join(tempDir, 'src');
- fs.mkdirSync(src, { recursive: true });
- // TableFileName has a `const std::string&` parameter; BuildName uses an
- // `auto … -> std::string` trailing return type. Both used to be named
- // `string` (picked up from the parameter / return type), so callers never
- // resolved and the defining file looked like nothing depended on it.
- fs.writeFileSync(
- path.join(src, 'names.cc'),
- `#include <string>
- std::string TableFileName(const std::string& dbname, int number) {
- return dbname;
- }
- auto BuildName(const std::string& a) -> std::string {
- return a;
- }
- `
- );
- fs.writeFileSync(
- path.join(src, 'user.cc'),
- `#include <string>
- std::string use() {
- return TableFileName("db", 1) + BuildName("x");
- }
- `
- );
- cg = CodeGraph.initSync(tempDir);
- await cg.indexAll();
- cg.resolveReferences();
- // The functions are extracted under their real names, not `string`.
- const fns = cg.getNodesByKind('function');
- const tableFn = fns.find((n) => n.name === 'TableFileName');
- const buildFn = fns.find((n) => n.name === 'BuildName');
- expect(tableFn, 'TableFileName extracted (not "string")').toBeDefined();
- expect(buildFn, 'BuildName extracted (not "string")').toBeDefined();
- // And the cross-file calls resolve to them, so editing names.cc surfaces user.cc.
- for (const fn of [tableFn!, buildFn!]) {
- const reached = [...cg.getImpactRadius(fn.id, 3).nodes.values()].map((n) => n.filePath ?? '');
- expect(reached.some((p) => p.endsWith('user.cc')), `${fn.name} should be called from user.cc`).toBe(true);
- }
- });
- });
- describe('Dart mixins and type references', () => {
- let tempDir: string;
- let cg: CodeGraph;
- beforeEach(() => {
- tempDir = createTempDir();
- });
- afterEach(() => {
- if (cg) cg.close();
- if (fs.existsSync(tempDir)) fs.rmSync(tempDir, { recursive: true, force: true });
- });
- it('links `with` mixins and method parameter/return types across files', async () => {
- const lib = path.join(tempDir, 'lib');
- fs.mkdirSync(lib, { recursive: true });
- fs.writeFileSync(
- path.join(lib, 'models.dart'),
- `class User {
- final String name;
- User(this.name);
- }
- mixin Loggable {
- void log() {}
- }
- abstract class Repository {
- User find(int id);
- }
- `
- );
- fs.writeFileSync(
- path.join(lib, 'service.dart'),
- `import 'models.dart';
- class UserService extends Repository with Loggable {
- @override
- User find(int id) => User('x');
- List<User> all() => [];
- }
- `
- );
- cg = CodeGraph.initSync(tempDir);
- await cg.indexAll();
- cg.resolveReferences();
- const inModels = (name: string) =>
- cg.getNodesByKind('class').concat(cg.getNodesByKind('module'))
- .find((n) => n.name === name && n.filePath.endsWith('models.dart'));
- // The `with Loggable` mixin records a dependency — editing the mixin surfaces
- // the class that mixes it in (across files). Loggable is a `mixin`, indexed
- // as a class-like node.
- const loggable = cg.getNodesByKind('class').find((n) => n.name === 'Loggable')
- ?? cg.getNodesByKind('module').find((n) => n.name === 'Loggable');
- expect(loggable, 'Loggable mixin indexed').toBeDefined();
- const mixinUsers = [...cg.getImpactRadius(loggable!.id, 3).nodes.values()].map((n) => n.name);
- expect(mixinUsers).toContain('UserService');
- // `User` is used only as a method parameter/return type in service.dart —
- // editing it must still surface service.dart via the type references.
- const user = inModels('User') ?? cg.getNodesByKind('class').find((n) => n.name === 'User');
- expect(user, 'User indexed').toBeDefined();
- const userDeps = [...cg.getImpactRadius(user!.id, 3).nodes.values()].map((n) => n.filePath ?? '');
- expect(userDeps.some((p) => p.endsWith('service.dart'))).toBe(true);
- });
- });
- describe('Static-member / value-read references', () => {
- let tempDir: string;
- let cg: CodeGraph;
- beforeEach(() => {
- tempDir = createTempDir();
- });
- afterEach(() => {
- if (cg) cg.close();
- if (fs.existsSync(tempDir)) fs.rmSync(tempDir, { recursive: true, force: true });
- });
- it('links a type referenced only via a static field / enum value (and ignores lowercase receivers)', async () => {
- fs.writeFileSync(
- path.join(tempDir, 'JsonScope.java'),
- `class JsonScope {
- static final int EMPTY_DOCUMENT = 1;
- }
- `
- );
- fs.writeFileSync(
- path.join(tempDir, 'Reader.java'),
- `class Reader {
- private int helper;
- int peek() {
- return JsonScope.EMPTY_DOCUMENT;
- }
- int noop() {
- return this.helper;
- }
- }
- `
- );
- cg = CodeGraph.initSync(tempDir);
- await cg.indexAll();
- cg.resolveReferences();
- // JsonScope is used ONLY as `JsonScope.EMPTY_DOCUMENT` (a static-field value
- // read — never constructed or called), so before the static-member pass it
- // had no dependents. Editing it now surfaces Reader.java.
- const scope = cg.getNodesByKind('class').find((n) => n.name === 'JsonScope');
- expect(scope, 'JsonScope indexed').toBeDefined();
- const reached = [...cg.getImpactRadius(scope!.id, 3).nodes.values()].map((n) => n.filePath ?? '');
- expect(reached.some((p) => p.endsWith('Reader.java'))).toBe(true);
- // A lowercase receiver (`this.helper`) must NOT be emitted as a type ref —
- // only Capitalized receivers (types) are. No node named `this`/`helper`
- // should appear as a reference target from peek/noop beyond JsonScope.
- const refTargets = cg
- .getNodesByKind('class')
- .filter((n) => n.name === 'this' || n.name === 'helper');
- expect(refTargets.length).toBe(0);
- });
- it('does not link a static-member read across language families (coincidental name)', async () => {
- // A native (Kotlin) `Build.VERSION` reads the Android system class — it must
- // NOT link to a coincidentally same-named TS class (the cross-language false
- // positive that name-matching produces; `references` edges are language-local).
- fs.writeFileSync(
- path.join(tempDir, 'Build.ts'),
- `export class Build {\n static version = 1;\n}\n`
- );
- fs.writeFileSync(
- path.join(tempDir, 'Device.kt'),
- `package app\nclass Device {\n fun sdk(): Int = Build.VERSION\n}\n`
- );
- cg = CodeGraph.initSync(tempDir);
- await cg.indexAll();
- cg.resolveReferences();
- const tsBuild = cg.getNodesByKind('class').find((n) => n.name === 'Build' && n.filePath.endsWith('Build.ts'));
- expect(tsBuild).toBeDefined();
- // The Kotlin file is `app/Device.kt`; the TS Build must have NO dependent there.
- const deps = [...cg.getImpactRadius(tsBuild!.id, 2).nodes.values()].map((n) => n.filePath ?? '');
- expect(deps.some((p) => p.endsWith('Device.kt'))).toBe(false);
- });
- });
- describe('Cross-language type/import gate (RN name collisions)', () => {
- let tempDir: string;
- let cg: CodeGraph;
- beforeEach(() => {
- tempDir = createTempDir();
- });
- afterEach(() => {
- if (cg) cg.close();
- if (fs.existsSync(tempDir)) fs.rmSync(tempDir, { recursive: true, force: true });
- });
- it('a TS PascalCase type ref lands on the TS type, never a same-named native class', async () => {
- // react-native-async-storage's example app has a TS `type TestRunner` AND a
- // Kotlin `class TestRunner`. The React PascalCase resolver name-matched the
- // Kotlin `class` (its COMPONENT_KINDS includes `class`) with no language
- // check at confidence 0.8, outranking the (cross-language-penalized 0.5)
- // TS name-match — so a TS ref to `TestRunner` crossed web→jvm. The ref here
- // is intentionally NOT imported: a clean relative import would mask the bug
- // by resolving via the import map before the framework strategy can win.
- fs.writeFileSync(
- path.join(tempDir, 'package.json'),
- JSON.stringify({ dependencies: { 'react-native': '*' } })
- );
- fs.writeFileSync(
- path.join(tempDir, 'useTests.ts'),
- `export type TestRunner = { run: () => void };\n`
- );
- fs.writeFileSync(
- path.join(tempDir, 'basic.tsx'),
- `export function useBasicTest(r: TestRunner): TestRunner {\n return r;\n}\n`
- );
- fs.writeFileSync(
- path.join(tempDir, 'TestUtils.kt'),
- `package app\nclass TestRunner {\n fun run() {}\n}\n`
- );
- cg = CodeGraph.initSync(tempDir);
- await cg.indexAll();
- cg.resolveReferences();
- const ktRunner = cg
- .getNodesByKind('class')
- .find((n) => n.name === 'TestRunner' && n.filePath.endsWith('TestUtils.kt'));
- expect(ktRunner, 'Kotlin TestRunner class').toBeDefined();
- const ktDeps = [...cg.getImpactRadius(ktRunner!.id, 2).nodes.values()].map((n) => n.filePath ?? '');
- expect(ktDeps.some((p) => p.endsWith('basic.tsx')), 'Kotlin class has NO TS dependent').toBe(false);
- const tsRunner = cg.getNodesByKind('type_alias').find((n) => n.name === 'TestRunner');
- expect(tsRunner, 'TS TestRunner type_alias').toBeDefined();
- const tsDeps = [...cg.getImpactRadius(tsRunner!.id, 2).nodes.values()].map((n) => n.filePath ?? '');
- expect(tsDeps.some((p) => p.endsWith('basic.tsx')), 'TS type captured the ref (re-pointed)').toBe(true);
- });
- it('gates a cross-family import name collision but keeps same-family imports', async () => {
- // A TS `import { Widget }` that only matches a Swift `class Widget` must not
- // create a web→apple dependency — but a sibling TS module imported by
- // another TS file (same family) must still resolve (no over-gating).
- fs.writeFileSync(path.join(tempDir, 'Widget.swift'), `class Widget {\n func render() {}\n}\n`);
- fs.writeFileSync(
- path.join(tempDir, 'widget.ts'),
- `import { Widget } from './native';\nexport function mount(w: Widget) {}\n`
- );
- fs.writeFileSync(path.join(tempDir, 'util.ts'), `export class Helper {}\n`);
- fs.writeFileSync(
- path.join(tempDir, 'app.ts'),
- `import { Helper } from './util';\nexport const h = new Helper();\n`
- );
- cg = CodeGraph.initSync(tempDir);
- await cg.indexAll();
- cg.resolveReferences();
- const swiftWidget = cg
- .getNodesByKind('class')
- .find((n) => n.name === 'Widget' && n.filePath.endsWith('.swift'));
- expect(swiftWidget, 'Swift Widget class').toBeDefined();
- const wDeps = [...cg.getImpactRadius(swiftWidget!.id, 2).nodes.values()].map((n) => n.filePath ?? '');
- expect(wDeps.some((p) => p.endsWith('widget.ts')), 'Swift class has NO TS dependent').toBe(false);
- // Same-family control — the TS Helper must still see its TS dependent.
- const helper = cg.getNodesByKind('class').find((n) => n.name === 'Helper');
- expect(helper, 'TS Helper class').toBeDefined();
- const hDeps = [...cg.getImpactRadius(helper!.id, 2).nodes.values()].map((n) => n.filePath ?? '');
- expect(hDeps.some((p) => p.endsWith('app.ts')), 'same-family TS import preserved').toBe(true);
- });
- });
- describe('Python absolute module import resolution', () => {
- let tempDir: string;
- let cg: CodeGraph;
- beforeEach(() => {
- tempDir = createTempDir();
- });
- afterEach(() => {
- if (cg) cg.close();
- if (fs.existsSync(tempDir)) fs.rmSync(tempDir, { recursive: true, force: true });
- });
- it('links a bare `import pkg.module` of an internal module to its file', async () => {
- // `import conduit.apps.signals` (a Django-style side-effect import, and any
- // dotted absolute module import) had no edge to the module file — only
- // `from x import y` was linked — so a module imported by its dotted path
- // looked like nothing depended on it.
- fs.mkdirSync(path.join(tempDir, 'conduit/apps'), { recursive: true });
- fs.writeFileSync(path.join(tempDir, 'conduit/__init__.py'), '');
- fs.writeFileSync(path.join(tempDir, 'conduit/apps/__init__.py'), '');
- fs.writeFileSync(path.join(tempDir, 'conduit/apps/signals.py'), `def handler():\n pass\n`);
- fs.writeFileSync(
- path.join(tempDir, 'conduit/apps/app.py'),
- `import conduit.apps.signals\nimport os\n\nVALUE = 1\n`
- );
- cg = CodeGraph.initSync(tempDir);
- await cg.indexAll();
- cg.resolveReferences();
- const signals = cg.getNodesByKind('file').find((n) => n.filePath.endsWith('conduit/apps/signals.py'));
- expect(signals, 'signals.py indexed').toBeDefined();
- const deps = [...cg.getImpactRadius(signals!.id, 2).nodes.values()].map((n) => n.filePath ?? '');
- expect(deps.some((p) => p.endsWith('app.py')), 'importer depends on the module').toBe(true);
- // `import os` (stdlib) must NOT fabricate an edge — no os.py file in the repo.
- const osNode = cg.getNodesByKind('file').find((n) => n.filePath.endsWith('/os.py'));
- expect(osNode, 'no stdlib os.py node').toBeUndefined();
- });
- it('Django include() links the root URLconf to the included app urls module', async () => {
- // `url(r'^api/', include('app.urls'))` should record a dependency from the
- // root urlconf onto the included app's `urls.py` — so editing an app's routes
- // surfaces the project urlconf that mounts them.
- fs.mkdirSync(path.join(tempDir, 'app'), { recursive: true });
- fs.writeFileSync(path.join(tempDir, 'requirements.txt'), `django==4.0\n`);
- fs.writeFileSync(path.join(tempDir, 'app/__init__.py'), '');
- fs.writeFileSync(path.join(tempDir, 'app/views.py'), `def home(request):\n return None\n`);
- fs.writeFileSync(
- path.join(tempDir, 'app/urls.py'),
- `from django.conf.urls import url\nfrom . import views\nurlpatterns = [url(r'^$', views.home)]\n`
- );
- fs.writeFileSync(
- path.join(tempDir, 'urls.py'),
- `from django.conf.urls import include, url\nurlpatterns = [url(r'^app/', include('app.urls'))]\n`
- );
- cg = CodeGraph.initSync(tempDir);
- await cg.indexAll();
- cg.resolveReferences();
- const appUrls = cg.getNodesByKind('file').find((n) => n.filePath.endsWith('app/urls.py'));
- expect(appUrls, 'app/urls.py indexed').toBeDefined();
- const deps = [...cg.getImpactRadius(appUrls!.id, 2).nodes.values()].map((n) => n.filePath ?? '');
- expect(deps.some((p) => p.endsWith('urls.py') && !p.endsWith('app/urls.py')), 'root urlconf depends on the included app urls').toBe(true);
- });
- it('resolves `from pkg import submodule` to the submodule under that package, not a same-named one', async () => {
- // FastAPI router-aggregator pattern: `from app.api.routes import authentication`
- // with same-named modules in sibling packages must resolve via the import's
- // SOURCE (the package), not a coincidental same-basename file elsewhere.
- fs.mkdirSync(path.join(tempDir, 'app/api/routes'), { recursive: true });
- fs.mkdirSync(path.join(tempDir, 'app/api/dependencies'), { recursive: true });
- for (const p of ['app/__init__.py', 'app/api/__init__.py', 'app/api/routes/__init__.py', 'app/api/dependencies/__init__.py']) {
- fs.writeFileSync(path.join(tempDir, p), '');
- }
- fs.writeFileSync(path.join(tempDir, 'app/api/routes/authentication.py'), `def login():\n pass\n`);
- fs.writeFileSync(path.join(tempDir, 'app/api/dependencies/authentication.py'), `def get_user():\n pass\n`);
- fs.writeFileSync(
- path.join(tempDir, 'app/api/routes/api.py'),
- `from app.api.routes import authentication\n\nROUTER = authentication\n`
- );
- cg = CodeGraph.initSync(tempDir);
- await cg.indexAll();
- cg.resolveReferences();
- const routesAuth = cg.getNodesByKind('file').find((n) => n.filePath.endsWith('routes/authentication.py'));
- const depsAuth = cg.getNodesByKind('file').find((n) => n.filePath.endsWith('dependencies/authentication.py'));
- expect(routesAuth && depsAuth).toBeTruthy();
- const routesDeps = [...cg.getImpactRadius(routesAuth!.id, 2).nodes.values()].map((n) => n.filePath ?? '');
- const depsDeps = [...cg.getImpactRadius(depsAuth!.id, 2).nodes.values()].map((n) => n.filePath ?? '');
- expect(routesDeps.some((p) => p.endsWith('routes/api.py')), 'submodule under the imported package is the dependent').toBe(true);
- expect(depsDeps.some((p) => p.endsWith('routes/api.py')), 'same-named module in a sibling package is NOT').toBe(false);
- });
- });
- describe('Razor / Blazor markup extraction', () => {
- let tempDir: string;
- let cg: CodeGraph;
- beforeEach(() => {
- tempDir = createTempDir();
- });
- afterEach(() => {
- if (cg) cg.close();
- if (fs.existsSync(tempDir)) fs.rmSync(tempDir, { recursive: true, force: true });
- });
- it('links @model and Blazor component tags to their C# types; ignores HTML elements', async () => {
- fs.mkdirSync(path.join(tempDir, 'Views'), { recursive: true });
- fs.writeFileSync(
- path.join(tempDir, 'LoginViewModel.cs'),
- `namespace App { public class LoginViewModel { public string Email { get; set; } } }`
- );
- fs.writeFileSync(
- path.join(tempDir, 'ToastComponent.cs'),
- `namespace App { public class ToastComponent { } }`
- );
- fs.writeFileSync(
- path.join(tempDir, 'Views/Login.cshtml'),
- `@model LoginViewModel\n<div class="form">\n <input asp-for="Email" />\n</div>\n`
- );
- fs.writeFileSync(
- path.join(tempDir, 'Index.razor'),
- `<div>\n <ToastComponent />\n</div>\n`
- );
- cg = CodeGraph.initSync(tempDir);
- await cg.indexAll();
- cg.resolveReferences();
- // `@model LoginViewModel` → the view-model class.
- const vm = cg.getNodesByKind('class').find((n) => n.name === 'LoginViewModel');
- expect(vm, 'LoginViewModel class').toBeDefined();
- const vmDeps = [...cg.getImpactRadius(vm!.id, 2).nodes.values()].map((n) => n.filePath ?? '');
- expect(vmDeps.some((p) => p.endsWith('Login.cshtml')), '@model links the view').toBe(true);
- // `<ToastComponent />` → the component class.
- const toast = cg.getNodesByKind('class').find((n) => n.name === 'ToastComponent');
- expect(toast, 'ToastComponent class').toBeDefined();
- const toastDeps = [...cg.getImpactRadius(toast!.id, 2).nodes.values()].map((n) => n.filePath ?? '');
- expect(toastDeps.some((p) => p.endsWith('Index.razor')), 'Blazor tag links the component').toBe(true);
- // HTML elements (`<div>`, `<input>`) must NOT become component references.
- const htmlNodes = cg.getNodesByKind('class').filter((n) => n.name === 'div' || n.name === 'input');
- expect(htmlNodes.length, 'no node for HTML elements').toBe(0);
- });
- });
- describe('Default import resolution (renamed default export)', () => {
- let tempDir: string;
- let cg: CodeGraph;
- beforeEach(() => {
- tempDir = createTempDir();
- });
- afterEach(() => {
- if (cg) cg.close();
- if (fs.existsSync(tempDir)) fs.rmSync(tempDir, { recursive: true, force: true });
- });
- it('links a renamed default import to the module file', async () => {
- // Express route aggregator: `import articlesController from './controller'`
- // where the module does `export default router`. The renamed local can't be
- // found as a symbol, so the controller file had no dependent — the dependency
- // is on the module file regardless of the default export's name.
- fs.mkdirSync(path.join(tempDir, 'app'), { recursive: true });
- fs.writeFileSync(path.join(tempDir, 'app/controller.ts'), `const router = { get() {} };\nexport default router;\n`);
- fs.writeFileSync(path.join(tempDir, 'app/routes.ts'), `import myController from './controller';\nexport const api = myController;\n`);
- cg = CodeGraph.initSync(tempDir);
- await cg.indexAll();
- cg.resolveReferences();
- const controller = cg.getNodesByKind('file').find((n) => n.filePath.endsWith('app/controller.ts'));
- expect(controller, 'controller.ts indexed').toBeDefined();
- const deps = [...cg.getImpactRadius(controller!.id, 2).nodes.values()].map((n) => n.filePath ?? '');
- expect(deps.some((p) => p.endsWith('routes.ts')), 'importer depends on the default-exporting module').toBe(true);
- });
- });
- describe('Chained method-call resolution (C# extension methods)', () => {
- let tempDir: string;
- let cg: CodeGraph;
- beforeEach(() => {
- tempDir = createTempDir();
- });
- afterEach(() => {
- if (cg) cg.close();
- if (fs.existsSync(tempDir)) fs.rmSync(tempDir, { recursive: true, force: true });
- });
- it('resolves a chained extension-method call (a.b.Method()) to its definition', async () => {
- // ASP.NET DI registration: `builder.Services.AddCoreServices(...)` calls a
- // static extension method elsewhere. A multi-dot receiver chain matched no
- // method-call pattern before, so the extension method had no caller.
- fs.mkdirSync(path.join(tempDir, 'cfg'), { recursive: true });
- fs.writeFileSync(
- path.join(tempDir, 'cfg/Ext.cs'),
- `namespace App {\n public static class Ext {\n public static object AddCoreServices(this object services, int x) { return services; }\n }\n}\n`
- );
- fs.writeFileSync(
- path.join(tempDir, 'Program.cs'),
- `namespace App {\n public class Program {\n public void Run(object builder) {\n builder.Services.AddCoreServices(1);\n }\n }\n}\n`
- );
- cg = CodeGraph.initSync(tempDir);
- await cg.indexAll();
- cg.resolveReferences();
- const ext = cg
- .getNodesByKind('method')
- .find((n) => n.name === 'AddCoreServices')
- ?? cg.getNodesByKind('function').find((n) => n.name === 'AddCoreServices');
- expect(ext, 'AddCoreServices defined').toBeDefined();
- const callers = [...cg.getImpactRadius(ext!.id, 2).nodes.values()].map((n) => n.filePath ?? '');
- expect(callers.some((p) => p.endsWith('Program.cs')), 'chained extension call resolves to its definition').toBe(true);
- });
- });
- describe('Same-directory include + KMP import resolution', () => {
- let tempDir: string;
- let cg: CodeGraph;
- beforeEach(() => {
- tempDir = createTempDir();
- });
- afterEach(() => {
- if (cg) cg.close();
- if (fs.existsSync(tempDir)) fs.rmSync(tempDir, { recursive: true, force: true });
- });
- it('a C/C++ #include resolves to the same-directory header, not a same-named one elsewhere', async () => {
- // A multi-platform native module has a header of the same basename per
- // platform. `windows/Provider.cpp`'s `#include "Storage.h"` means its OWN
- // sibling header — not `apple/Storage.h` (which sorts first and so was
- // picked arbitrarily before, leaving the real local header with 0 deps).
- fs.mkdirSync(path.join(tempDir, 'apple'), { recursive: true });
- fs.mkdirSync(path.join(tempDir, 'windows'), { recursive: true });
- fs.writeFileSync(path.join(tempDir, 'apple', 'Storage.h'), `#pragma once\nstruct Storage { int n; };\n`);
- fs.writeFileSync(path.join(tempDir, 'windows', 'Storage.h'), `#pragma once\nstruct Storage { int n; };\n`);
- fs.writeFileSync(
- path.join(tempDir, 'windows', 'Provider.cpp'),
- `#include "Storage.h"\nint use() { Storage s; return s.n; }\n`
- );
- cg = CodeGraph.initSync(tempDir);
- await cg.indexAll();
- cg.resolveReferences();
- const winHeader = cg.getNodesByKind('file').find((n) => n.filePath.endsWith('windows/Storage.h'));
- const appleHeader = cg.getNodesByKind('file').find((n) => n.filePath.endsWith('apple/Storage.h'));
- expect(winHeader, 'windows/Storage.h indexed').toBeDefined();
- expect(appleHeader, 'apple/Storage.h indexed').toBeDefined();
- const winDeps = [...cg.getImpactRadius(winHeader!.id, 2).nodes.values()].map((n) => n.filePath ?? '');
- const appleDeps = [...cg.getImpactRadius(appleHeader!.id, 2).nodes.values()].map((n) => n.filePath ?? '');
- expect(winDeps.some((p) => p.endsWith('Provider.cpp')), 'same-dir header gets the includer').toBe(true);
- expect(appleDeps.some((p) => p.endsWith('Provider.cpp')), 'other-platform header does NOT').toBe(false);
- });
- it('a Kotlin Multiplatform commonMain import resolves to the expect, not a platform actual', async () => {
- const common = path.join(tempDir, 'src/commonMain/kotlin/app');
- const android = path.join(tempDir, 'src/androidMain/kotlin/app');
- fs.mkdirSync(common, { recursive: true });
- fs.mkdirSync(android, { recursive: true });
- fs.writeFileSync(path.join(common, 'Platform.kt'), `package app\nexpect class PlatformContext\n`);
- fs.writeFileSync(path.join(android, 'Platform.android.kt'), `package app\nactual class PlatformContext\n`);
- fs.writeFileSync(
- path.join(common, 'Db.kt'),
- `package app\nimport app.PlatformContext\nclass Db {\n fun open(ctx: PlatformContext) {}\n}\n`
- );
- cg = CodeGraph.initSync(tempDir);
- await cg.indexAll();
- cg.resolveReferences();
- const expectCtx = cg
- .getNodesByKind('class')
- .find((n) => n.name === 'PlatformContext' && n.filePath.endsWith('commonMain/kotlin/app/Platform.kt'));
- expect(expectCtx, 'commonMain expect PlatformContext').toBeDefined();
- const deps = [...cg.getImpactRadius(expectCtx!.id, 2).nodes.values()].map((n) => n.filePath ?? '');
- expect(deps.some((p) => p.endsWith('Db.kt')), 'commonMain import lands on the expect, not the actual').toBe(true);
- });
- });
- describe('Objective-C messages, class receivers, and #import', () => {
- let tempDir: string;
- let cg: CodeGraph;
- beforeEach(() => {
- tempDir = createTempDir();
- });
- afterEach(() => {
- if (cg) cg.close();
- if (fs.existsSync(tempDir)) fs.rmSync(tempDir, { recursive: true, force: true });
- });
- it('resolves single-arg selectors, class-message receivers, and #import headers', async () => {
- fs.writeFileSync(
- path.join(tempDir, 'SDImageCache.h'),
- `#import <Foundation/Foundation.h>
- @interface SDImageCache : NSObject
- + (instancetype)sharedCache;
- + (void)storeImage:(NSString *)key;
- @end
- `
- );
- fs.writeFileSync(
- path.join(tempDir, 'SDImageCache.m'),
- `#import "SDImageCache.h"
- @implementation SDImageCache
- + (instancetype)sharedCache { return nil; }
- + (void)storeImage:(NSString *)key { }
- @end
- `
- );
- fs.writeFileSync(
- path.join(tempDir, 'SDManager.m'),
- `#import "SDImageCache.h"
- @interface SDManager : NSObject
- @end
- @implementation SDManager
- - (void)run {
- [SDImageCache sharedCache];
- [SDImageCache storeImage:@"k"];
- }
- @end
- `
- );
- cg = CodeGraph.initSync(tempDir);
- await cg.indexAll();
- cg.resolveReferences();
- // 1. The single-argument selector `[SDImageCache storeImage:@"k"]` resolves
- // to the `storeImage:` method — named WITH its colon both at the call site
- // and the definition (before the fix the call site dropped the colon).
- const storeImage = cg.getNodesByKind('method').find((n) => n.name === 'storeImage:');
- expect(storeImage, 'storeImage: method').toBeDefined();
- const storeCallers = [...cg.getImpactRadius(storeImage!.id, 2).nodes.values()].map((n) => n.filePath ?? '');
- expect(storeCallers.some((p) => p.endsWith('SDManager.m'))).toBe(true);
- // 2. The class-message receiver `[SDImageCache sharedCache]` references the
- // SDImageCache class (whose @interface lives in the header).
- const cache = cg.getNodesByKind('class').find((n) => n.name === 'SDImageCache');
- expect(cache, 'SDImageCache class').toBeDefined();
- const classDeps = [...cg.getImpactRadius(cache!.id, 2).nodes.values()].map((n) => n.filePath ?? '');
- expect(classDeps.some((p) => p.endsWith('SDManager.m'))).toBe(true);
- // 3. `#import "SDImageCache.h"` resolves to the header FILE — editing it
- // surfaces both importers.
- const header = cg.getNodesByKind('file').find((n) => n.filePath.endsWith('SDImageCache.h'));
- expect(header, 'SDImageCache.h indexed').toBeDefined();
- const importers = [...cg.getImpactRadius(header!.id, 2).nodes.values()].map((n) => n.filePath ?? '');
- expect(importers.some((p) => p.endsWith('SDManager.m'))).toBe(true);
- });
- });
- describe('Full Indexing', () => {
- let tempDir: string;
- beforeEach(() => {
- tempDir = createTempDir();
- });
- afterEach(() => {
- cleanupTempDir(tempDir);
- });
- it('should index a TypeScript file', async () => {
- // Create test file
- const srcDir = path.join(tempDir, 'src');
- fs.mkdirSync(srcDir);
- fs.writeFileSync(
- path.join(srcDir, 'utils.ts'),
- `
- export function add(a: number, b: number): number {
- return a + b;
- }
- export function multiply(a: number, b: number): number {
- return a * b;
- }
- `
- );
- // Initialize and index
- const cg = CodeGraph.initSync(tempDir);
- const result = await cg.indexAll();
- expect(result.success).toBe(true);
- expect(result.filesIndexed).toBe(1);
- expect(result.nodesCreated).toBeGreaterThanOrEqual(2);
- // Check nodes were stored
- const nodes = cg.getNodesInFile('src/utils.ts');
- expect(nodes.length).toBeGreaterThanOrEqual(2);
- const addFunc = nodes.find((n) => n.name === 'add');
- expect(addFunc).toBeDefined();
- expect(addFunc?.kind).toBe('function');
- cg.close();
- });
- it('should index multiple files', async () => {
- // Create test files
- const srcDir = path.join(tempDir, 'src');
- fs.mkdirSync(srcDir);
- fs.writeFileSync(
- path.join(srcDir, 'math.ts'),
- `export function add(a: number, b: number) { return a + b; }`
- );
- fs.writeFileSync(
- path.join(srcDir, 'string.ts'),
- `export function capitalize(s: string) { return s.toUpperCase(); }`
- );
- // Initialize and index
- const cg = CodeGraph.initSync(tempDir);
- const result = await cg.indexAll();
- expect(result.success).toBe(true);
- expect(result.filesIndexed).toBe(2);
- const files = cg.getFiles();
- expect(files.length).toBe(2);
- cg.close();
- });
- it('should track file hashes for incremental updates', async () => {
- // Create initial file
- const srcDir = path.join(tempDir, 'src');
- fs.mkdirSync(srcDir);
- fs.writeFileSync(path.join(srcDir, 'main.ts'), `export const x = 1;`);
- // Initialize and index
- const cg = CodeGraph.initSync(tempDir);
- await cg.indexAll();
- // Check file is tracked
- const file = cg.getFile('src/main.ts');
- expect(file).toBeDefined();
- expect(file?.contentHash).toBeDefined();
- // Modify file
- fs.writeFileSync(path.join(srcDir, 'main.ts'), `export const x = 2;`);
- // Check for changes
- const changes = cg.getChangedFiles();
- expect(changes.modified).toContain('src/main.ts');
- cg.close();
- });
- it('should sync and detect changes', async () => {
- // Create initial file
- const srcDir = path.join(tempDir, 'src');
- fs.mkdirSync(srcDir);
- fs.writeFileSync(
- path.join(srcDir, 'main.ts'),
- `export function original() { return 1; }`
- );
- // Initialize and index
- const cg = CodeGraph.initSync(tempDir);
- await cg.indexAll();
- const initialNodes = cg.getNodesInFile('src/main.ts');
- expect(initialNodes.some((n) => n.name === 'original')).toBe(true);
- // Modify file
- fs.writeFileSync(
- path.join(srcDir, 'main.ts'),
- `export function updated() { return 2; }`
- );
- // Sync
- const syncResult = await cg.sync();
- expect(syncResult.filesModified).toBe(1);
- // Check nodes were updated
- const updatedNodes = cg.getNodesInFile('src/main.ts');
- expect(updatedNodes.some((n) => n.name === 'updated')).toBe(true);
- expect(updatedNodes.some((n) => n.name === 'original')).toBe(false);
- cg.close();
- });
- it('should count file-level tracked YAML files as indexed', async () => {
- fs.writeFileSync(path.join(tempDir, 'app.yaml'), 'name: test\n');
- fs.writeFileSync(path.join(tempDir, 'routes.yml'), 'route: value\n');
- const cg = CodeGraph.initSync(tempDir);
- const result = await cg.indexAll();
- expect(result.success).toBe(true);
- expect(result.filesIndexed).toBe(2);
- expect(result.filesSkipped).toBe(0);
- expect(cg.getFiles().map((f) => f.path).sort()).toEqual(['app.yaml', 'routes.yml']);
- cg.close();
- });
- it('should count file-level tracked YAML/Twig files as indexed in indexFiles()', async () => {
- fs.writeFileSync(path.join(tempDir, 'app.yaml'), 'name: test\n');
- fs.writeFileSync(path.join(tempDir, 'view.twig'), '{{ title }}\n');
- const cg = CodeGraph.initSync(tempDir);
- const result = await cg.indexFiles(['app.yaml', 'view.twig']);
- expect(result.success).toBe(true);
- expect(result.filesIndexed).toBe(2);
- expect(result.filesSkipped).toBe(0);
- const tracked = cg.getFiles().map((f) => `${f.path}:${f.language}`).sort();
- expect(tracked).toEqual(['app.yaml:yaml', 'view.twig:twig']);
- cg.close();
- });
- it('should count file-level tracked .properties files as indexed', async () => {
- fs.writeFileSync(path.join(tempDir, 'application.properties'), 'server.port=8080\n');
- fs.writeFileSync(path.join(tempDir, 'log.properties'), 'log.level=INFO\n');
- const cg = CodeGraph.initSync(tempDir);
- const result = await cg.indexAll();
- expect(result.success).toBe(true);
- expect(result.filesIndexed).toBe(2);
- expect(result.filesSkipped).toBe(0);
- cg.close();
- });
- it('should count the full file-level tracked class (yaml/twig/properties) in indexFiles()', async () => {
- fs.writeFileSync(path.join(tempDir, 'app.yaml'), 'name: test\n');
- fs.writeFileSync(path.join(tempDir, 'view.twig'), '{{ title }}\n');
- fs.writeFileSync(path.join(tempDir, 'application.properties'), 'server.port=8080\n');
- const cg = CodeGraph.initSync(tempDir);
- const result = await cg.indexFiles(['app.yaml', 'view.twig', 'application.properties']);
- expect(result.success).toBe(true);
- expect(result.filesIndexed).toBe(3);
- expect(result.filesSkipped).toBe(0);
- const tracked = cg.getFiles().map((f) => `${f.path}:${f.language}`).sort();
- expect(tracked).toEqual(['app.yaml:yaml', 'application.properties:properties', 'view.twig:twig']);
- cg.close();
- });
- });
- describe('Path Normalization', () => {
- it('should convert backslashes to forward slashes', () => {
- expect(normalizePath('gui\\node_modules\\foo')).toBe('gui/node_modules/foo');
- expect(normalizePath('src\\components\\Button.tsx')).toBe('src/components/Button.tsx');
- });
- it('should leave forward-slash paths unchanged', () => {
- expect(normalizePath('src/components/Button.tsx')).toBe('src/components/Button.tsx');
- });
- it('should handle empty string', () => {
- expect(normalizePath('')).toBe('');
- });
- });
- describe('Directory Exclusion', () => {
- let tempDir: string;
- beforeEach(() => {
- tempDir = createTempDir();
- });
- afterEach(() => {
- cleanupTempDir(tempDir);
- });
- it('should exclude directories listed in .gitignore', () => {
- // Create structure: src/index.ts + node_modules/pkg/index.js, gitignore node_modules
- const srcDir = path.join(tempDir, 'src');
- const nmDir = path.join(tempDir, 'node_modules', 'pkg');
- fs.mkdirSync(srcDir, { recursive: true });
- fs.mkdirSync(nmDir, { recursive: true });
- fs.writeFileSync(path.join(srcDir, 'index.ts'), 'export const x = 1;');
- fs.writeFileSync(path.join(nmDir, 'index.js'), 'module.exports = {};');
- fs.writeFileSync(path.join(tempDir, '.gitignore'), 'node_modules/\n');
- const files = scanDirectory(tempDir);
- expect(files).toContain('src/index.ts');
- expect(files.every((f) => !f.includes('node_modules'))).toBe(true);
- });
- it('should exclude nested node_modules via a root .gitignore', () => {
- // A trailing-slash pattern with no leading slash matches at any depth.
- const srcDir = path.join(tempDir, 'packages', 'app', 'src');
- const nmDir = path.join(tempDir, 'packages', 'app', 'node_modules', 'pkg');
- fs.mkdirSync(srcDir, { recursive: true });
- fs.mkdirSync(nmDir, { recursive: true });
- fs.writeFileSync(path.join(srcDir, 'index.ts'), 'export const x = 1;');
- fs.writeFileSync(path.join(nmDir, 'index.js'), 'module.exports = {};');
- fs.writeFileSync(path.join(tempDir, '.gitignore'), 'node_modules/\n');
- const files = scanDirectory(tempDir);
- expect(files).toContain('packages/app/src/index.ts');
- expect(files.every((f) => !f.includes('node_modules'))).toBe(true);
- });
- it('should apply a nested .gitignore only to its own subtree', () => {
- const appSrc = path.join(tempDir, 'app', 'src');
- fs.mkdirSync(appSrc, { recursive: true });
- fs.writeFileSync(path.join(appSrc, 'keep.ts'), 'export const a = 1;');
- fs.writeFileSync(path.join(appSrc, 'skip.ts'), 'export const b = 2;');
- fs.writeFileSync(path.join(tempDir, 'app', '.gitignore'), 'src/skip.ts\n');
- // A sibling with the same name outside app/ must NOT be ignored.
- const otherDir = path.join(tempDir, 'other', 'src');
- fs.mkdirSync(otherDir, { recursive: true });
- fs.writeFileSync(path.join(otherDir, 'skip.ts'), 'export const c = 3;');
- const files = scanDirectory(tempDir);
- expect(files).toContain('app/src/keep.ts');
- expect(files).not.toContain('app/src/skip.ts');
- expect(files).toContain('other/src/skip.ts');
- });
- it('should always skip .git directories', () => {
- const srcDir = path.join(tempDir, 'src');
- const gitDir = path.join(tempDir, '.git', 'objects');
- fs.mkdirSync(srcDir, { recursive: true });
- fs.mkdirSync(gitDir, { recursive: true });
- fs.writeFileSync(path.join(srcDir, 'index.ts'), 'export const x = 1;');
- fs.writeFileSync(path.join(gitDir, 'pack.ts'), 'export const y = 2;');
- const files = scanDirectory(tempDir);
- expect(files).toContain('src/index.ts');
- expect(files.every((f) => !f.includes('.git'))).toBe(true);
- });
- it('should return forward-slash paths on all platforms', () => {
- const srcDir = path.join(tempDir, 'src', 'components');
- fs.mkdirSync(srcDir, { recursive: true });
- fs.writeFileSync(path.join(srcDir, 'Button.tsx'), 'export function Button() {}');
- const files = scanDirectory(tempDir);
- expect(files.length).toBe(1);
- expect(files[0]).toBe('src/components/Button.tsx');
- expect(files[0]).not.toContain('\\');
- });
- });
- describe('Git Submodules', () => {
- let tempDir: string;
- beforeEach(() => {
- tempDir = createTempDir();
- });
- afterEach(() => {
- cleanupTempDir(tempDir);
- });
- it('should index files inside git submodules (issue #147)', async () => {
- const { execFileSync } = await import('child_process');
- const git = (cwd: string, ...args: string[]) =>
- execFileSync('git', args, { cwd, stdio: 'pipe' });
- // Build a separate "library" repo to use as a submodule source.
- const libDir = path.join(tempDir, '_lib');
- fs.mkdirSync(libDir, { recursive: true });
- git(libDir, 'init', '-q');
- git(libDir, 'config', 'user.email', 'test@test.com');
- git(libDir, 'config', 'user.name', 'Test');
- fs.writeFileSync(path.join(libDir, 'lib.ts'), 'export const fromSubmodule = 1;');
- git(libDir, 'add', '-A');
- git(libDir, 'commit', '-q', '-m', 'lib init');
- // Build the main repo and add the lib repo as a submodule.
- const mainDir = path.join(tempDir, 'main');
- fs.mkdirSync(mainDir, { recursive: true });
- git(mainDir, 'init', '-q');
- git(mainDir, 'config', 'user.email', 'test@test.com');
- git(mainDir, 'config', 'user.name', 'Test');
- fs.writeFileSync(path.join(mainDir, 'app.ts'), 'export const app = 1;');
- git(mainDir, 'add', '-A');
- git(mainDir, 'commit', '-q', '-m', 'app init');
- // protocol.file.allow=always is required to add a local-path submodule on
- // recent git versions (CVE-2022-39253 mitigation).
- execFileSync(
- 'git',
- ['-c', 'protocol.file.allow=always', 'submodule', 'add', '-q', libDir, 'libs/lib'],
- { cwd: mainDir, stdio: 'pipe' }
- );
- git(mainDir, 'commit', '-q', '-m', 'add submodule');
- const files = scanDirectory(mainDir);
- expect(files).toContain('app.ts');
- expect(files).toContain('libs/lib/lib.ts');
- });
- });
- describe('Nested non-submodule git repos', () => {
- let tempDir: string;
- beforeEach(() => {
- tempDir = createTempDir();
- });
- afterEach(() => {
- cleanupTempDir(tempDir);
- });
- it('should index files in embedded git repos run from a git super-repo (issue #193)', async () => {
- const { execFileSync } = await import('child_process');
- const git = (cwd: string, ...args: string[]) =>
- execFileSync('git', args, { cwd, stdio: 'pipe' });
- // Top-level workspace is itself a git repo, holding no source directly —
- // the CMake "super-repo" layout from the issue.
- const root = path.join(tempDir, 'root');
- fs.mkdirSync(path.join(root, 'coding'), { recursive: true });
- git(root, 'init', '-q');
- git(root, 'config', 'user.email', 'test@test.com');
- git(root, 'config', 'user.name', 'Test');
- fs.writeFileSync(path.join(root, 'CMakeLists.txt'), 'cmake_minimum_required(VERSION 3.10)\n');
- // Two independent clones living inside the workspace (NOT submodules):
- // one with committed source, one with only untracked source.
- const sub1 = path.join(root, 'sub_repo1', 'src');
- fs.mkdirSync(sub1, { recursive: true });
- git(path.join(root, 'sub_repo1'), 'init', '-q');
- git(path.join(root, 'sub_repo1'), 'config', 'user.email', 'test@test.com');
- git(path.join(root, 'sub_repo1'), 'config', 'user.name', 'Test');
- fs.writeFileSync(path.join(sub1, 'one.ts'), 'export const one = 1;');
- git(path.join(root, 'sub_repo1'), 'add', '-A');
- git(path.join(root, 'sub_repo1'), 'commit', '-q', '-m', 'sub1 init');
- const sub2 = path.join(root, 'sub_repo2', 'src');
- fs.mkdirSync(sub2, { recursive: true });
- git(path.join(root, 'sub_repo2'), 'init', '-q');
- fs.writeFileSync(path.join(sub2, 'two.ts'), 'export const two = 2;');
- const files = scanDirectory(root);
- // Both committed and untracked source from the nested repos must be found.
- expect(files).toContain('sub_repo1/src/one.ts');
- expect(files).toContain('sub_repo2/src/two.ts');
- });
- it('should respect each embedded repo\'s own .gitignore', async () => {
- const { execFileSync } = await import('child_process');
- const git = (cwd: string, ...args: string[]) =>
- execFileSync('git', args, { cwd, stdio: 'pipe' });
- const root = path.join(tempDir, 'root');
- fs.mkdirSync(root, { recursive: true });
- git(root, 'init', '-q');
- const sub = path.join(root, 'sub_repo', 'src');
- fs.mkdirSync(sub, { recursive: true });
- git(path.join(root, 'sub_repo'), 'init', '-q');
- fs.writeFileSync(path.join(root, 'sub_repo', '.gitignore'), 'src/generated.ts\n');
- fs.writeFileSync(path.join(sub, 'real.ts'), 'export const real = 1;');
- fs.writeFileSync(path.join(sub, 'generated.ts'), 'export const generated = 1;');
- const files = scanDirectory(root);
- expect(files).toContain('sub_repo/src/real.ts');
- expect(files).not.toContain('sub_repo/src/generated.ts');
- });
- });
- // =============================================================================
- // Scala
- // =============================================================================
- describe('Scala Extraction', () => {
- describe('Language detection', () => {
- it('should detect Scala files', () => {
- expect(detectLanguage('Main.scala')).toBe('scala');
- expect(detectLanguage('script.sc')).toBe('scala');
- expect(detectLanguage('src/UserService.scala')).toBe('scala');
- });
- it('should report Scala as supported', () => {
- expect(isLanguageSupported('scala')).toBe(true);
- expect(getSupportedLanguages()).toContain('scala');
- });
- });
- describe('Class extraction', () => {
- it('should extract class definitions', () => {
- const code = `
- class UserService(private val repo: UserRepository) {
- def findUser(id: String): Option[String] = Some(id)
- }
- `;
- const result = extractFromSource('UserService.scala', code);
- const cls = result.nodes.find((n) => n.kind === 'class' && n.name === 'UserService');
- expect(cls).toBeDefined();
- expect(cls?.language).toBe('scala');
- });
- it('should extract object definitions as class kind', () => {
- const code = `
- object DatabaseConfig {
- val url = "jdbc:postgresql://localhost/mydb"
- }
- `;
- const result = extractFromSource('Config.scala', code);
- const obj = result.nodes.find((n) => n.kind === 'class' && n.name === 'DatabaseConfig');
- expect(obj).toBeDefined();
- });
- it('should extract trait definitions as trait kind', () => {
- const code = `
- trait Repository[A] {
- def findById(id: String): Option[A]
- def save(entity: A): Unit
- }
- `;
- const result = extractFromSource('Repository.scala', code);
- const trait_ = result.nodes.find((n) => n.kind === 'trait' && n.name === 'Repository');
- expect(trait_).toBeDefined();
- });
- });
- describe('Method and function extraction', () => {
- it('should extract method definitions inside a class', () => {
- const code = `
- class Calculator {
- def add(a: Int, b: Int): Int = a + b
- def divide(a: Double, b: Double): Double = a / b
- }
- `;
- const result = extractFromSource('Calculator.scala', code);
- const methods = result.nodes.filter((n) => n.kind === 'method');
- expect(methods.find((m) => m.name === 'add')).toBeDefined();
- expect(methods.find((m) => m.name === 'divide')).toBeDefined();
- });
- it('should extract method signatures', () => {
- const code = `
- class Greeter {
- def greet(name: String): String = s"Hello, \${name}!"
- }
- `;
- const result = extractFromSource('Greeter.scala', code);
- const method = result.nodes.find((n) => n.name === 'greet');
- expect(method?.signature).toContain('name: String');
- expect(method?.signature).toContain('String');
- });
- it('should extract top-level function definitions as functions', () => {
- const code = `
- def factorial(n: Int): Int = if (n <= 1) 1 else n * factorial(n - 1)
- def greet(name: String): String = s"Hello, \${name}!"
- `;
- const result = extractFromSource('utils.scala', code);
- const fns = result.nodes.filter((n) => n.kind === 'function');
- expect(fns.find((f) => f.name === 'factorial')).toBeDefined();
- expect(fns.find((f) => f.name === 'greet')).toBeDefined();
- });
- });
- describe('Val and var extraction', () => {
- it('should extract val inside a class as field', () => {
- const code = `
- class Config {
- val timeout: Int = 30
- val host: String = "localhost"
- }
- `;
- const result = extractFromSource('Config.scala', code);
- const fields = result.nodes.filter((n) => n.kind === 'field');
- expect(fields.find((f) => f.name === 'timeout')).toBeDefined();
- expect(fields.find((f) => f.name === 'host')).toBeDefined();
- });
- it('should extract var inside a class as field', () => {
- const code = `
- class Counter {
- var count: Int = 0
- }
- `;
- const result = extractFromSource('Counter.scala', code);
- const field = result.nodes.find((n) => n.kind === 'field' && n.name === 'count');
- expect(field).toBeDefined();
- });
- it('should extract top-level val as constant', () => {
- const code = `
- val MaxConnections: Int = 100
- val DefaultTimeout = 30
- `;
- const result = extractFromSource('constants.scala', code);
- const consts = result.nodes.filter((n) => n.kind === 'constant');
- expect(consts.find((c) => c.name === 'MaxConnections')).toBeDefined();
- });
- it('should extract top-level var as variable', () => {
- const code = `
- var retries: Int = 3
- `;
- const result = extractFromSource('state.scala', code);
- const v = result.nodes.find((n) => n.kind === 'variable' && n.name === 'retries');
- expect(v).toBeDefined();
- });
- it('should include type in val/var signature', () => {
- const code = `
- class Service {
- val timeout: Int = 30
- }
- `;
- const result = extractFromSource('Service.scala', code);
- const field = result.nodes.find((n) => n.name === 'timeout');
- expect(field?.signature).toContain('timeout');
- expect(field?.signature).toContain('Int');
- });
- });
- describe('Enum extraction', () => {
- it('should extract enum definitions', () => {
- const code = `
- enum Color:
- case Red
- case Green
- case Blue
- `;
- const result = extractFromSource('Color.scala', code);
- const enumNode = result.nodes.find((n) => n.kind === 'enum' && n.name === 'Color');
- expect(enumNode).toBeDefined();
- });
- it('should extract enum cases as enum_member', () => {
- const code = `
- enum Direction:
- case North
- case South
- case East
- case West
- `;
- const result = extractFromSource('Direction.scala', code);
- const members = result.nodes.filter((n) => n.kind === 'enum_member');
- expect(members.find((m) => m.name === 'North')).toBeDefined();
- expect(members.find((m) => m.name === 'South')).toBeDefined();
- expect(members.length).toBeGreaterThanOrEqual(4);
- });
- });
- describe('Type alias extraction', () => {
- it('should extract type aliases', () => {
- const code = `
- type UserId = String
- type UserMap = Map[String, String]
- `;
- const result = extractFromSource('types.scala', code);
- const aliases = result.nodes.filter((n) => n.kind === 'type_alias');
- expect(aliases.find((a) => a.name === 'UserId')).toBeDefined();
- expect(aliases.find((a) => a.name === 'UserMap')).toBeDefined();
- });
- });
- describe('Import extraction', () => {
- it('should extract import declarations', () => {
- const code = `
- import scala.collection.mutable.ListBuffer
- import scala.concurrent.Future
- `;
- const result = extractFromSource('imports.scala', code);
- const imports = result.nodes.filter((n) => n.kind === 'import');
- expect(imports.length).toBeGreaterThanOrEqual(2);
- });
- });
- describe('Visibility modifiers', () => {
- it('should extract private visibility', () => {
- const code = `
- class Service {
- private val secret: String = "abc"
- private def helper(): Unit = {}
- }
- `;
- const result = extractFromSource('Service.scala', code);
- const secretField = result.nodes.find((n) => n.name === 'secret');
- expect(secretField?.visibility).toBe('private');
- const helperMethod = result.nodes.find((n) => n.name === 'helper');
- expect(helperMethod?.visibility).toBe('private');
- });
- it('should extract protected visibility', () => {
- const code = `
- class Base {
- protected def helperMethod(): Unit = {}
- }
- `;
- const result = extractFromSource('Base.scala', code);
- const method = result.nodes.find((n) => n.name === 'helperMethod');
- expect(method?.visibility).toBe('protected');
- });
- it('should default to public visibility', () => {
- const code = `
- class Greeter {
- def hello(): Unit = {}
- }
- `;
- const result = extractFromSource('Greeter.scala', code);
- const method = result.nodes.find((n) => n.name === 'hello');
- expect(method?.visibility).toBe('public');
- });
- });
- describe('Inheritance', () => {
- it('should extract extends relationships', () => {
- const code = `
- class AdminUser extends User {
- def adminAction(): Unit = {}
- }
- `;
- const result = extractFromSource('AdminUser.scala', code);
- const extendsRefs = result.unresolvedReferences.filter((r) => r.referenceKind === 'extends');
- expect(extendsRefs.find((r) => r.referenceName === 'User')).toBeDefined();
- });
- });
- describe('Call extraction', () => {
- it('should extract function call expressions', () => {
- const code = `
- def processData(): Unit = {
- val result = computeResult()
- println(result)
- }
- `;
- const result = extractFromSource('processor.scala', code);
- const calls = result.unresolvedReferences.filter((r) => r.referenceKind === 'calls');
- expect(calls.length).toBeGreaterThan(0);
- });
- });
- });
- describe('Vue Extraction', () => {
- it('should detect Vue files', () => {
- expect(detectLanguage('App.vue')).toBe('vue');
- expect(detectLanguage('components/Button.vue')).toBe('vue');
- expect(isLanguageSupported('vue')).toBe(true);
- });
- it('should extract component node from a Vue SFC', () => {
- const code = `<template>
- <div>{{ message }}</div>
- </template>
- <script>
- export default {
- data() {
- return { message: 'Hello' };
- }
- }
- </script>
- `;
- const result = extractFromSource('HelloWorld.vue', code);
- const componentNode = result.nodes.find((n) => n.kind === 'component');
- expect(componentNode).toBeDefined();
- expect(componentNode?.name).toBe('HelloWorld');
- expect(componentNode?.language).toBe('vue');
- expect(componentNode?.isExported).toBe(true);
- });
- it('should extract functions from <script> block', () => {
- const code = `<template>
- <button @click="handleClick">Click</button>
- </template>
- <script>
- function handleClick() {
- console.log('clicked');
- }
- const count = 0;
- </script>
- `;
- const result = extractFromSource('Button.vue', code);
- const componentNode = result.nodes.find((n) => n.kind === 'component');
- expect(componentNode).toBeDefined();
- expect(componentNode?.name).toBe('Button');
- const funcNode = result.nodes.find((n) => n.kind === 'function' && n.name === 'handleClick');
- expect(funcNode).toBeDefined();
- expect(funcNode?.language).toBe('vue');
- });
- it('should extract from <script setup lang="ts"> block', () => {
- const code = `<template>
- <div>{{ count }}</div>
- </template>
- <script setup lang="ts">
- import { ref } from 'vue';
- const count = ref(0);
- function increment(): void {
- count.value++;
- }
- </script>
- `;
- const result = extractFromSource('Counter.vue', code);
- const componentNode = result.nodes.find((n) => n.kind === 'component');
- expect(componentNode).toBeDefined();
- expect(componentNode?.name).toBe('Counter');
- const funcNode = result.nodes.find((n) => n.kind === 'function' && n.name === 'increment');
- expect(funcNode).toBeDefined();
- expect(funcNode?.language).toBe('vue');
- // All nodes should be marked as vue language
- for (const node of result.nodes) {
- expect(node.language).toBe('vue');
- }
- });
- it('should extract calls from top-level <script setup> initializers', () => {
- const code = `<template>
- <div>{{ token }}</div>
- </template>
- <script setup lang="ts">
- import { getTokenMp } from './api/upload';
- const token = getTokenMp();
- </script>
- `;
- const result = extractFromSource('Issue425Setup.vue', code);
- const call = result.unresolvedReferences.find(
- (ref) => ref.referenceKind === 'calls' && ref.referenceName === 'getTokenMp'
- );
- expect(call).toBeDefined();
- });
- it('should extract calls from Vue Options API object methods', () => {
- const code = `<template>
- <button @click="save">Save</button>
- </template>
- <script>
- import { getTokenMp } from './api/upload';
- export default {
- methods: {
- save() {
- return getTokenMp();
- }
- },
- setup() {
- return getTokenMp();
- }
- }
- </script>
- `;
- const result = extractFromSource('Issue425Options.vue', code);
- const calls = result.unresolvedReferences.filter(
- (ref) => ref.referenceKind === 'calls' && ref.referenceName === 'getTokenMp'
- );
- expect(calls).toHaveLength(2);
- });
- it('should extract component usages from the Vue template (PascalCase + kebab, skipping built-ins) (#629)', () => {
- const code = `<template>
- <div class="wrap">
- <UserCard :user="u" />
- <my-button>Click</my-button>
- <Transition><span>x</span></Transition>
- </div>
- </template>
- <script setup lang="ts">
- import UserCard from './UserCard.vue';
- import MyButton from './MyButton.vue';
- </script>
- `;
- const result = extractFromSource('Host.vue', code);
- const refs = result.unresolvedReferences
- .filter((r) => r.referenceKind === 'references')
- .map((r) => r.referenceName);
- expect(refs).toContain('UserCard'); // PascalCase tag
- expect(refs).toContain('MyButton'); // kebab <my-button> → MyButton
- expect(refs).not.toContain('Transition'); // Vue built-in skipped
- expect(refs).not.toContain('Div'); // native HTML element skipped
- expect(refs).not.toContain('Span');
- });
- it('should extract from both <script> and <script setup> blocks', () => {
- const code = `<template>
- <div>{{ msg }}</div>
- </template>
- <script>
- export default {
- name: 'DualScript'
- }
- </script>
- <script setup>
- const msg = 'hello';
- function greet() {
- return msg;
- }
- </script>
- `;
- const result = extractFromSource('DualScript.vue', code);
- const componentNode = result.nodes.find((n) => n.kind === 'component');
- expect(componentNode).toBeDefined();
- const greetFunc = result.nodes.find((n) => n.kind === 'function' && n.name === 'greet');
- expect(greetFunc).toBeDefined();
- });
- it('should create component node for template-only Vue file', () => {
- const code = `<template>
- <div>Static content</div>
- </template>
- `;
- const result = extractFromSource('Static.vue', code);
- const componentNode = result.nodes.find((n) => n.kind === 'component');
- expect(componentNode).toBeDefined();
- expect(componentNode?.name).toBe('Static');
- expect(componentNode?.language).toBe('vue');
- // Only the component node should exist (no script nodes)
- expect(result.nodes.length).toBe(1);
- });
- it('should create containment edges from component to script nodes', () => {
- const code = `<template>
- <div>{{ value }}</div>
- </template>
- <script setup lang="ts">
- const value = 42;
- </script>
- `;
- const result = extractFromSource('Contained.vue', code);
- const componentNode = result.nodes.find((n) => n.kind === 'component');
- expect(componentNode).toBeDefined();
- // Should have containment edges from component to child nodes
- const containEdges = result.edges.filter(
- (e) => e.source === componentNode!.id && e.kind === 'contains'
- );
- expect(containEdges.length).toBeGreaterThan(0);
- });
- });
- describe('Instantiates + Decorates edge extraction', () => {
- it('emits an instantiates ref for `new Foo()`', () => {
- const code = `
- class Foo {}
- function bootstrap() { return new Foo(); }
- `;
- const result = extractFromSource('app.ts', code);
- const ref = result.unresolvedReferences.find(
- (r) => r.referenceKind === 'instantiates' && r.referenceName === 'Foo'
- );
- expect(ref).toBeDefined();
- });
- it('strips type-argument suffix from generic constructors', () => {
- const code = `
- class Container<T> { constructor(_: T) {} }
- function go() { return new Container<string>('x'); }
- `;
- const result = extractFromSource('app.ts', code);
- const ref = result.unresolvedReferences.find(
- (r) => r.referenceKind === 'instantiates'
- );
- expect(ref).toBeDefined();
- // Container<string> must be normalised to "Container" — otherwise
- // resolution can never match the class node.
- expect(ref!.referenceName).toBe('Container');
- });
- it('keeps trailing identifier from qualified `new ns.Foo()`', () => {
- const code = `
- const ns = { Foo: class {} };
- function go() { return new ns.Foo(); }
- `;
- const result = extractFromSource('app.ts', code);
- const ref = result.unresolvedReferences.find(
- (r) => r.referenceKind === 'instantiates'
- );
- // We can't always resolve which Foo, but the name should be the
- // simple identifier so name-matching has a chance.
- expect(ref?.referenceName).toBe('Foo');
- });
- it('emits a decorates ref for `@Foo class X {}`', () => {
- const code = `
- function Foo(_arg: string) { return (cls: any) => cls; }
- @Foo('x')
- class X {}
- `;
- const result = extractFromSource('app.ts', code);
- const decorClass = result.unresolvedReferences.find(
- (r) => r.referenceKind === 'decorates' && r.referenceName === 'Foo'
- );
- expect(decorClass).toBeDefined();
- });
- it('does NOT attribute a prior class\'s decorator to the next class', () => {
- // Regression: the sibling-walk must stop at the first non-
- // decorator separator. `@A class Foo {} @B class Bar {}` must
- // produce `decorates(Foo, A)` and `decorates(Bar, B)` — never
- // `decorates(Bar, A)`.
- const code = `
- function A(cls: any) { return cls; }
- function B(cls: any) { return cls; }
- @A
- class Foo {}
- @B
- class Bar {}
- `;
- const result = extractFromSource('app.ts', code);
- const decoratesEdges = result.unresolvedReferences.filter(
- (r) => r.referenceKind === 'decorates'
- );
- // Exactly one decorates ref per decorated class, no cross-attribution.
- const fromBar = decoratesEdges.filter((r) =>
- result.nodes.find((n) => n.id === r.fromNodeId && n.name === 'Bar')
- );
- expect(fromBar.length).toBe(1);
- expect(fromBar[0]!.referenceName).toBe('B');
- });
- it('emits a decorates ref for `@Foo method() {}`', () => {
- const code = `
- function Get(p: string) { return (t: any, k: string) => t; }
- class Svc {
- @Get('/x') method() { return 1; }
- }
- `;
- const result = extractFromSource('app.ts', code);
- const decorMethod = result.unresolvedReferences.find(
- (r) => r.referenceKind === 'decorates' && r.referenceName === 'Get'
- );
- expect(decorMethod).toBeDefined();
- // The decorated symbol must be `method`, not the constructor or class.
- const decoratedNode = result.nodes.find((n) => n.id === decorMethod!.fromNodeId);
- expect(decoratedNode?.name).toBe('method');
- });
- });
- // =============================================================================
- // Lua
- // =============================================================================
- describe('Lua Extraction', () => {
- describe('Language detection', () => {
- it('should detect Lua files', () => {
- expect(detectLanguage('init.lua')).toBe('lua');
- expect(detectLanguage('src/util.lua')).toBe('lua');
- });
- it('should report Lua as supported', () => {
- expect(isLanguageSupported('lua')).toBe(true);
- expect(getSupportedLanguages()).toContain('lua');
- });
- });
- describe('Function extraction', () => {
- it('should extract global and local functions', () => {
- const code = `
- function configure(opts) return opts end
- local function helper(x) return x * 2 end
- `;
- const result = extractFromSource('init.lua', code);
- const funcs = result.nodes.filter((n) => n.kind === 'function').map((n) => n.name);
- expect(funcs).toContain('configure');
- expect(funcs).toContain('helper');
- const configure = result.nodes.find((n) => n.name === 'configure');
- expect(configure?.language).toBe('lua');
- expect(configure?.signature).toBe('(opts)');
- });
- it('should split table/method functions into a receiver and method name', () => {
- const code = `
- function M.connect(host, port) return host end
- function M:send(data) return self end
- `;
- const result = extractFromSource('init.lua', code);
- const methods = result.nodes.filter((n) => n.kind === 'method');
- const connect = methods.find((m) => m.name === 'connect');
- expect(connect?.qualifiedName).toBe('M::connect');
- const send = methods.find((m) => m.name === 'send');
- expect(send?.qualifiedName).toBe('M::send');
- });
- });
- describe('Variable extraction', () => {
- it('should extract local variable declarations', () => {
- const code = `
- local M = {}
- local count = 0
- `;
- const result = extractFromSource('mod.lua', code);
- const vars = result.nodes.filter((n) => n.kind === 'variable').map((n) => n.name);
- expect(vars).toContain('M');
- expect(vars).toContain('count');
- });
- });
- describe('Import extraction (require)', () => {
- it('should extract require() in local declarations and bare calls', () => {
- const code = `
- local socket = require("socket")
- local http = require "resty.http"
- require("side.effect")
- `;
- const result = extractFromSource('net.lua', code);
- const imports = result.nodes.filter((n) => n.kind === 'import').map((n) => n.name);
- expect(imports).toContain('socket');
- expect(imports).toContain('resty.http');
- expect(imports).toContain('side.effect');
- const ref = result.unresolvedReferences.find(
- (r) => r.referenceKind === 'imports' && r.referenceName === 'socket'
- );
- expect(ref).toBeDefined();
- });
- // Regression: the tree-sitter-wasms Lua grammar (ABI 13) corrupts the shared
- // WASM heap under web-tree-sitter 0.25, dropping nested calls/imports on every
- // parse after the first. We vendor the ABI-15 grammar instead — this guards it
- // by extracting several sources in sequence and asserting the LAST still works.
- it('should keep extracting require across many sequential parses', () => {
- let last;
- for (let i = 0; i < 8; i++) {
- last = extractFromSource(`f${i}.lua`, `local m = require("module.${i}")\nreturn m\n`);
- }
- const imports = last!.nodes.filter((n) => n.kind === 'import').map((n) => n.name);
- expect(imports).toContain('module.7');
- });
- });
- describe('Call extraction', () => {
- it('should record intra-file calls as resolvable references', () => {
- const code = `
- local function helper(x) return x end
- local function run(y) return helper(y) end
- `;
- const result = extractFromSource('calls.lua', code);
- const call = result.unresolvedReferences.find(
- (r) => r.referenceKind === 'calls' && r.referenceName === 'helper'
- );
- expect(call).toBeDefined();
- });
- });
- });
- // =============================================================================
- // Luau (typed superset of Lua — https://luau.org)
- // =============================================================================
- describe('Luau Extraction', () => {
- describe('Language detection', () => {
- it('should detect Luau files', () => {
- expect(detectLanguage('init.luau')).toBe('luau');
- expect(detectLanguage('src/Client.luau')).toBe('luau');
- });
- it('should report Luau as supported', () => {
- expect(isLanguageSupported('luau')).toBe(true);
- expect(getSupportedLanguages()).toContain('luau');
- });
- });
- describe('Type aliases', () => {
- it('should extract `type` and `export type` definitions', () => {
- const code = `
- export type Vector = { x: number, y: number }
- type Handler = (msg: string) -> boolean
- `;
- const result = extractFromSource('types.luau', code);
- const aliases = result.nodes.filter((n) => n.kind === 'type_alias');
- const vector = aliases.find((a) => a.name === 'Vector');
- expect(vector).toBeDefined();
- expect(vector?.isExported).toBe(true);
- const handler = aliases.find((a) => a.name === 'Handler');
- expect(handler).toBeDefined();
- expect(handler?.isExported).toBe(false);
- });
- });
- describe('Typed functions and methods', () => {
- it('should capture typed signatures and split methods by receiver', () => {
- const code = `
- function configure(opts: { debug: boolean }): boolean
- return opts.debug
- end
- function Client:fetch(path: string): Response
- return path
- end
- `;
- const result = extractFromSource('client.luau', code);
- const configure = result.nodes.find((n) => n.kind === 'function' && n.name === 'configure');
- expect(configure?.language).toBe('luau');
- expect(configure?.signature).toBe('(opts: { debug: boolean }): boolean');
- const fetch = result.nodes.find((n) => n.kind === 'method' && n.name === 'fetch');
- expect(fetch?.qualifiedName).toBe('Client::fetch');
- });
- });
- describe('Imports and variables', () => {
- it('should extract string and Roblox instance-path require imports', () => {
- const code = `
- local http = require("http")
- local Signal = require(script.Parent.Signal)
- local count = 0
- `;
- const result = extractFromSource('mod.luau', code);
- const imports = result.nodes.filter((n) => n.kind === 'import').map((n) => n.name);
- expect(imports).toContain('http'); // string require
- expect(imports).toContain('Signal'); // Roblox instance-path require
- const vars = result.nodes.filter((n) => n.kind === 'variable').map((n) => n.name);
- expect(vars).toContain('count');
- });
- });
- });
- // =============================================================================
- // Objective-C
- // =============================================================================
- describe('Objective-C Extraction', () => {
- const sample = `
- #import <Foundation/Foundation.h>
- #import "MyClass.h"
- @interface MyClass : NSObject <NSCopying>
- @property (nonatomic, copy) NSString *name;
- - (void)greet;
- - (void)doThing:(id)x with:(id)y;
- + (instancetype)shared;
- @end
- @implementation MyClass
- - (void)greet {
- NSLog(@"Hello");
- [self doWork];
- }
- - (void)doThing:(id)x with:(id)y {
- [self notify:x];
- }
- + (instancetype)shared {
- return [[MyClass alloc] init];
- }
- @end
- void helperFunction(int count) {
- MyClass *obj = [MyClass shared];
- [obj greet];
- }
- `;
- it('should extract classes, methods, functions, and imports', () => {
- const result = extractFromSource('App.m', sample);
- const classes = result.nodes.filter((n) => n.kind === 'class');
- expect(classes.filter((c) => c.name === 'MyClass')).toHaveLength(1);
- const methods = result.nodes.filter((n) => n.kind === 'method');
- expect(methods.map((m) => m.name).sort()).toEqual(['doThing:with:', 'greet', 'shared']);
- const shared = methods.find((m) => m.name === 'shared');
- expect(shared?.isStatic).toBe(true);
- const properties = result.nodes.filter((n) => n.kind === 'property');
- expect(properties.some((p) => p.name === 'name')).toBe(true);
- const functions = result.nodes.filter((n) => n.kind === 'function');
- expect(functions.some((f) => f.name === 'helperFunction')).toBe(true);
- const imports = result.nodes.filter((n) => n.kind === 'import').map((n) => n.name);
- expect(imports).toContain('Foundation/Foundation.h');
- expect(imports).toContain('MyClass.h');
- });
- it('should record inheritance and protocol conformance', () => {
- const result = extractFromSource('App.m', sample);
- const extendsRefs = result.unresolvedReferences.filter((r) => r.referenceKind === 'extends');
- const implementsRefs = result.unresolvedReferences.filter((r) => r.referenceKind === 'implements');
- expect(extendsRefs.map((r) => r.referenceName)).toContain('NSObject');
- expect(implementsRefs.map((r) => r.referenceName)).toContain('NSCopying');
- });
- it('should record message sends and C calls', () => {
- const result = extractFromSource('App.m', sample);
- const calls = result.unresolvedReferences
- .filter((r) => r.referenceKind === 'calls')
- .map((r) => r.referenceName);
- expect(calls).toEqual(expect.arrayContaining(['NSLog', 'doWork', 'MyClass.shared', 'obj.greet']));
- });
- it('should reconstruct multi-keyword selectors at the call site so they resolve to the method definition', () => {
- // Regression for the gap discovered post-#165: message_expression's
- // multi-keyword form `[obj a:1 b:2]` was only emitting the first keyword,
- // so calls never resolved to multi-part method definitions like
- // `GET:parameters:headers:progress:success:failure:`. The call-site name
- // must match the method-definition name with full keywords + trailing colons.
- const code = `
- @implementation Caller
- - (void)demo {
- NSMutableDictionary *d = [NSMutableDictionary new];
- [d setObject:@"v" forKey:@"k"];
- [d setObject:@"v2" forKey:@"k2" withRetry:@YES];
- [self touchesBegan:nil withEvent:nil];
- }
- @end
- `;
- const result = extractFromSource('Caller.m', code);
- const calls = result.unresolvedReferences
- .filter((r) => r.referenceKind === 'calls')
- .map((r) => r.referenceName);
- expect(calls).toEqual(
- expect.arrayContaining([
- 'd.setObject:forKey:',
- 'd.setObject:forKey:withRetry:',
- 'touchesBegan:withEvent:',
- ])
- );
- });
- it('should not classify pure C headers with @end in comments as objc', () => {
- const cHeader = '/* @end of file */\n#ifndef STDIO_H\nvoid printf(const char *);\n#endif\n';
- expect(detectLanguage('stdio.h', cHeader)).toBe('c');
- });
- it('should extract protocol declarations', () => {
- const code = `
- @protocol DataSource <NSObject>
- - (NSInteger)numberOfItems;
- @end
- `;
- const result = extractFromSource('DataSource.h', code);
- const protocol = result.nodes.find((n) => n.kind === 'protocol' && n.name === 'DataSource');
- expect(protocol).toBeDefined();
- });
- it('should report Objective-C as supported', () => {
- expect(isLanguageSupported('objc')).toBe(true);
- expect(getSupportedLanguages()).toContain('objc');
- });
- });
- describe('Regression: issue-specific extraction fixes', () => {
- it('indexes inner functions of an anonymous AMD/CommonJS module wrapper (#528)', () => {
- const code = `
- define(['dep'], function (dep) {
- function innerHelper(x) { return x + 1; }
- function compute(y) { return innerHelper(y); }
- return { compute: compute };
- });
- `;
- const result = extractFromSource('amd-module.js', code);
- const fns = result.nodes.filter((n) => n.kind === 'function').map((n) => n.name);
- expect(fns).toContain('innerHelper');
- expect(fns).toContain('compute');
- });
- it('attaches Go methods on generic receivers to their type (#583)', () => {
- const code = `
- package main
- type Stack[T any] struct { items []T }
- func (s *Stack[T]) Push(v T) { s.items = append(s.items, v) }
- func (s Stack[T]) Len() int { return len(s.items) }
- `;
- const result = extractFromSource('stack.go', code);
- const methods = result.nodes.filter((n) => n.kind === 'method');
- expect(methods.find((m) => m.name === 'Push')?.qualifiedName).toBe('Stack::Push');
- expect(methods.find((m) => m.name === 'Len')?.qualifiedName).toBe('Stack::Len');
- });
- it('indexes new module extensions: .mts/.cts (TS) and .xsjs/.xsjslib (JS) (#366, #556)', () => {
- expect(isSourceFile('mod.mts')).toBe(true);
- expect(isSourceFile('mod.cts')).toBe(true);
- expect(isSourceFile('service.xsjs')).toBe(true);
- expect(isSourceFile('lib.xsjslib')).toBe(true);
- expect(detectLanguage('mod.mts')).toBe('typescript');
- expect(detectLanguage('service.xsjs')).toBe('javascript');
- // End-to-end: a .mts file is parsed as TS, a .xsjs file as JS.
- const ts = extractFromSource('mod.mts', 'export function hello(): number { return 1; }');
- expect(ts.nodes.find((n) => n.name === 'hello' && n.kind === 'function')).toBeDefined();
- const js = extractFromSource('service.xsjs', 'function handleRequest() { return 1; }');
- expect(js.nodes.find((n) => n.name === 'handleRequest' && n.kind === 'function')).toBeDefined();
- });
- });
- describe('Import / re-export dependency linking (blast-radius recall)', () => {
- // An import IS a dependency, but extraction only emits references for calls,
- // instantiations, type annotations, and inheritance — so a symbol imported and
- // then merely re-exported, placed in a registry array, passed as an argument,
- // or used in JSX produced no cross-file edge, leaving the providing file with a
- // false "0 dependents". These tests pin the import/re-export binding linking.
- it('emits an imports reference per named, aliased, and default import binding', () => {
- const code = `
- import { widget, helper as h } from './foo';
- import Thing from './thing';
- import * as NS from './ns';
- export const registry = [widget];
- `;
- const result = extractFromSource('bar.ts', code);
- const names = result.unresolvedReferences
- .filter((r) => r.referenceKind === 'imports')
- .map((r) => r.referenceName);
- expect(names).toContain('widget'); // named import → local name
- expect(names).toContain('h'); // aliased import → local alias
- expect(names).toContain('Thing'); // default import
- expect(names).toContain('NS'); // namespace import → linked to the module file as a dependency
- });
- it('emits an imports reference per re-exported binding', () => {
- const result = extractFromSource('barrel.ts', `export { alpha, beta as b } from './source';`);
- const names = result.unresolvedReferences
- .filter((r) => r.referenceKind === 'imports')
- .map((r) => r.referenceName);
- // Re-export links the SOURCE-side name, not the local alias.
- expect(names).toContain('alpha');
- expect(names).toContain('beta');
- });
- it('a value imported/re-exported but never called still makes the importer a dependent', async () => {
- const dir = createTempDir();
- try {
- fs.mkdirSync(path.join(dir, 'src'), { recursive: true });
- fs.writeFileSync(
- path.join(dir, 'src', 'foo.ts'),
- `export const widget = { n: 1 };\nexport function helper(): void {}\n`
- );
- // bar uses widget ONLY in an array and re-exports helper — neither is
- // called/typed, so before import-linking bar had no edge to foo at all.
- fs.writeFileSync(
- path.join(dir, 'src', 'bar.ts'),
- `import { widget } from './foo';\nexport { helper } from './foo';\nexport const registry = [widget];\n`
- );
- const cg = CodeGraph.initSync(dir, { config: { include: ['src/**/*.ts'], exclude: [] } });
- await cg.indexAll();
- cg.resolveReferences();
- expect(cg.getFileDependents('src/foo.ts')).toContain('src/bar.ts');
- cg.destroy();
- } finally {
- cleanupTempDir(dir);
- }
- });
- it('a namespace import touched only via a value-member read still links the module file', async () => {
- const dir = createTempDir();
- try {
- fs.mkdirSync(path.join(dir, 'src'), { recursive: true });
- fs.writeFileSync(path.join(dir, 'src', 'foo.ts'), `export const SOME_CONST = 42;\n`);
- // `foo` is imported as a namespace and used ONLY via a value-member read
- // (no call, no type) — `foo.helper()` would link on its own, but a bare
- // `foo.SOME_CONST` would not, so the module-import backstop must link it.
- fs.writeFileSync(path.join(dir, 'src', 'bar.ts'), `import * as foo from './foo';\nexport const x = foo.SOME_CONST;\n`);
- const cg = CodeGraph.initSync(dir, { config: { include: ['src/**/*.ts'], exclude: [] } });
- await cg.indexAll();
- cg.resolveReferences();
- expect(cg.getFileDependents('src/foo.ts')).toContain('src/bar.ts');
- cg.destroy();
- } finally {
- cleanupTempDir(dir);
- }
- });
- });
- describe('Python import dependency linking (blast-radius recall)', () => {
- // Same recall gap as TS: Python only linked called/instantiated imports, so a
- // name brought in with `from module import X` and then merely stored, used as
- // a decorator/argument, or re-exported through an `__init__.py` produced no
- // cross-file edge — the providing module showed a false "0 dependents".
- it('emits an imports reference per name in a `from module import ...` (incl. value/aliased)', () => {
- const code = [
- 'from foo import helper, widget',
- 'from foo import Thing as T',
- 'from . import sibling',
- 'from bar import *',
- ].join('\n');
- const names = extractFromSource('mod.py', code)
- .unresolvedReferences.filter((r) => r.referenceKind === 'imports')
- .map((r) => r.referenceName);
- expect(names).toContain('helper');
- expect(names).toContain('widget'); // value import
- expect(names).toContain('T'); // aliased import → local name
- expect(names).toContain('sibling'); // `from . import <name>`
- expect(names).not.toContain('*'); // wildcard import has no names
- });
- it('a Python value imported but never called still makes the importer a dependent', async () => {
- const dir = createTempDir();
- try {
- fs.mkdirSync(path.join(dir, 'pkg'), { recursive: true });
- fs.writeFileSync(path.join(dir, 'pkg', 'foo.py'), `widget = {"n": 1}\ndef helper():\n return 1\n`);
- // bar imports widget+helper but only stores widget in a list — nothing is
- // called, so before import-linking bar had no edge to foo.
- fs.writeFileSync(path.join(dir, 'pkg', 'bar.py'), `from foo import widget, helper\nregistry = [widget]\n`);
- const cg = CodeGraph.initSync(dir, { config: { include: ['pkg/**/*.py'], exclude: [] } });
- await cg.indexAll();
- cg.resolveReferences();
- expect(cg.getFileDependents('pkg/foo.py')).toContain('pkg/bar.py');
- cg.destroy();
- } finally {
- cleanupTempDir(dir);
- }
- });
- it('resolves `from . import submodule` + `submodule.func()` to the submodule', async () => {
- const dir = createTempDir();
- try {
- fs.mkdirSync(path.join(dir, 'pkg'), { recursive: true });
- fs.writeFileSync(path.join(dir, 'pkg', '__init__.py'), '');
- fs.writeFileSync(path.join(dir, 'pkg', 'certs.py'), `def where():\n return "/ca.pem"\n`);
- // certs is an imported MODULE (a file), and certs.where() is a qualified
- // call through it — the receiver isn't a symbol, so plain name-matching
- // can't link it. Also exercises the Python relative-dot path fix (`.certs`).
- fs.writeFileSync(path.join(dir, 'pkg', 'utils.py'), `from . import certs\ndef go():\n return certs.where()\n`);
- const cg = CodeGraph.initSync(dir, { config: { include: ['pkg/**/*.py'], exclude: [] } });
- await cg.indexAll();
- cg.resolveReferences();
- expect(cg.getFileDependents('pkg/certs.py')).toContain('pkg/utils.py');
- cg.destroy();
- } finally {
- cleanupTempDir(dir);
- }
- });
- it('a module import is a dependency even when the used member is re-exported elsewhere', async () => {
- const dir = createTempDir();
- try {
- fs.mkdirSync(path.join(dir, 'pkg'), { recursive: true });
- fs.writeFileSync(path.join(dir, 'pkg', '__init__.py'), '');
- // `where` is NOT defined in certs.py (re-exported from a 3rd-party pkg), so
- // member resolution can't find it — the module-import backstop must still
- // record utils -> certs. (Mirrors requests' real `certs.where`.)
- fs.writeFileSync(path.join(dir, 'pkg', 'certs.py'), `from external_ca import where\n`);
- fs.writeFileSync(path.join(dir, 'pkg', 'utils.py'), `from . import certs\nCA = certs.where()\n`);
- const cg = CodeGraph.initSync(dir, { config: { include: ['pkg/**/*.py'], exclude: [] } });
- await cg.indexAll();
- cg.resolveReferences();
- expect(cg.getFileDependents('pkg/certs.py')).toContain('pkg/utils.py');
- cg.destroy();
- } finally {
- cleanupTempDir(dir);
- }
- });
- });
- describe('Go cross-package composite literals (blast-radius recall)', () => {
- // Go function calls and type references across packages already resolved, but
- // struct composite literals — `render.XML{...}` / `pkga.Widget{...}` — were not
- // extracted at all, so a package whose types are only INSTANTIATED elsewhere
- // (gin's render/binding implementations) showed 0 dependents.
- it('links a cross-package struct composite literal to the defining package', async () => {
- const dir = createTempDir();
- try {
- fs.writeFileSync(path.join(dir, 'go.mod'), 'module example.com/proj\n\ngo 1.21\n');
- fs.mkdirSync(path.join(dir, 'render'), { recursive: true });
- fs.writeFileSync(path.join(dir, 'render', 'xml.go'), `package render\n\ntype XML struct { Data any }\n`);
- fs.writeFileSync(path.join(dir, 'app.go'), `package main\n\nimport "example.com/proj/render"\n\nfunc handle() any { return render.XML{} }\n`);
- const cg = CodeGraph.initSync(dir, { config: { include: ['**/*.go'], exclude: [] } });
- await cg.indexAll();
- cg.resolveReferences();
- expect(cg.getFileDependents('render/xml.go')).toContain('app.go');
- cg.destroy();
- } finally {
- cleanupTempDir(dir);
- }
- });
- it('links a composite literal in a package-level var registry', async () => {
- const dir = createTempDir();
- try {
- fs.writeFileSync(path.join(dir, 'go.mod'), 'module example.com/proj\n\ngo 1.21\n');
- fs.mkdirSync(path.join(dir, 'render'), { recursive: true });
- fs.writeFileSync(path.join(dir, 'render', 'xml.go'), `package render\n\ntype XML struct {}\nfunc (XML) Render() {}\n`);
- // The implementation is registered only in a top-level `var registry = {...}`
- // map literal — the body walker doesn't cover top-level declarations, so this
- // exercises the var-initializer walking added for Go.
- fs.writeFileSync(path.join(dir, 'reg.go'), `package main\n\nimport "example.com/proj/render"\n\ntype R interface { Render() }\n\nvar registry = map[string]R{ "xml": render.XML{} }\n`);
- const cg = CodeGraph.initSync(dir, { config: { include: ['**/*.go'], exclude: [] } });
- await cg.indexAll();
- cg.resolveReferences();
- expect(cg.getFileDependents('render/xml.go')).toContain('reg.go');
- cg.destroy();
- } finally {
- cleanupTempDir(dir);
- }
- });
- it('links a parenthesized pointer type conversion `(*T)(x)` to the type', async () => {
- const dir = createTempDir();
- try {
- fs.writeFileSync(path.join(dir, 'go.mod'), 'module example.com/proj\n\ngo 1.21\n');
- fs.writeFileSync(path.join(dir, 'types.go'), `package main\n\ntype Wrapped struct { N int }\n`);
- // `(*Wrapped)(x)` parses as a call whose callee is the parenthesized type
- // `(*Wrapped)` — without normalization it dropped on the floor.
- fs.writeFileSync(path.join(dir, 'use.go'), `package main\n\nfunc run(x *int) { _ = (*Wrapped)(x) }\n`);
- const cg = CodeGraph.initSync(dir, { config: { include: ['**/*.go'], exclude: [] } });
- await cg.indexAll();
- cg.resolveReferences();
- expect(cg.getFileDependents('types.go')).toContain('use.go');
- cg.destroy();
- } finally {
- cleanupTempDir(dir);
- }
- });
- it('links an implementation reached only through a Go interface (implicit satisfaction, #584)', async () => {
- const dir = createTempDir();
- try {
- fs.writeFileSync(path.join(dir, 'go.mod'), 'module example.com/proj\n\ngo 1.21\n');
- fs.mkdirSync(path.join(dir, 'codec'), { recursive: true });
- fs.writeFileSync(path.join(dir, 'codec', 'api.go'), `package codec\n\ntype Core interface {\n\tMarshal(v any) ([]byte, error)\n}\n\nvar API Core\n`);
- // jsonApi satisfies Core structurally (no `implements` keyword) and is
- // reached ONLY through the interface (API.Marshal). Without implicit
- // interface satisfaction + dispatch, json.go shows 0 dependents.
- fs.writeFileSync(path.join(dir, 'codec', 'json.go'), `package codec\n\ntype jsonApi struct{}\n\nfunc (j jsonApi) Marshal(v any) ([]byte, error) { return nil, nil }\n\nfunc init() { API = jsonApi{} }\n`);
- const cg = CodeGraph.initSync(dir, { config: { include: ['**/*.go'], exclude: [] } });
- await cg.indexAll();
- cg.resolveReferences();
- expect(cg.getFileDependents('codec/json.go')).toContain('codec/api.go');
- cg.destroy();
- } finally {
- cleanupTempDir(dir);
- }
- });
- });
- describe('C# records (blast-radius recall)', () => {
- // Records are ubiquitous in modern C# (DTOs, value objects, CQRS messages),
- // but `record` / `record struct` declarations weren't extracted as types — so
- // every reference, generic-type-argument, and `new` of a record dropped on the
- // floor and the defining file showed 0 dependents. (#237)
- it('extracts a record as a graph node (record class + record struct)', () => {
- const r = extractFromSource('r.cs', `namespace P;\npublic record Box(int N);\npublic record struct Pt(int X);\n`);
- expect(r.nodes.find((n) => n.name === 'Box' && (n.kind === 'class' || n.kind === 'struct'))).toBeDefined();
- expect(r.nodes.find((n) => n.name === 'Pt' && (n.kind === 'class' || n.kind === 'struct'))).toBeDefined();
- });
- it('resolves references / instantiations of a record across files', async () => {
- const dir = createTempDir();
- try {
- fs.writeFileSync(path.join(dir, 'types.cs'), `namespace P;\npublic record Box(int N);\n`);
- // Box is used as a generic type argument and instantiated — both require
- // Box to be a node to resolve.
- fs.writeFileSync(
- path.join(dir, 'use.cs'),
- `using System.Collections.Generic;\nnamespace P;\npublic class User {\n public IEnumerable<Box> Boxes { get; }\n public Box Make() => new Box(1);\n}\n`
- );
- const cg = CodeGraph.initSync(dir, { config: { include: ['**/*.cs'], exclude: [] } });
- await cg.indexAll();
- cg.resolveReferences();
- expect(cg.getFileDependents('types.cs')).toContain('use.cs');
- cg.destroy();
- } finally {
- cleanupTempDir(dir);
- }
- });
- });
- describe('Rust cross-module recall', () => {
- function rustProject(files: Record<string, string>): string {
- const dir = createTempDir();
- fs.writeFileSync(path.join(dir, 'Cargo.toml'), '[package]\nname = "proj"\nversion = "0.1.0"\nedition = "2021"\n');
- fs.mkdirSync(path.join(dir, 'src'), { recursive: true });
- for (const [rel, content] of Object.entries(files)) {
- const full = path.join(dir, 'src', rel);
- fs.mkdirSync(path.dirname(full), { recursive: true });
- fs.writeFileSync(full, content);
- }
- return dir;
- }
- it('extracts a struct literal `Foo { .. }` as an instantiation across modules', async () => {
- const dir = rustProject({
- 'lib.rs': 'pub mod types;\npub mod consumer;\n',
- 'types.rs': 'pub struct Widget { pub n: i32 }\n',
- 'consumer.rs': 'use crate::types::Widget;\npub fn build() -> Widget { Widget { n: 1 } }\n',
- });
- try {
- const cg = CodeGraph.initSync(dir, { config: { include: ['src/**/*.rs'], exclude: [] } });
- await cg.indexAll();
- cg.resolveReferences();
- expect(cg.getFileDependents('src/types.rs')).toContain('src/consumer.rs');
- cg.destroy();
- } finally { cleanupTempDir(dir); }
- });
- it('extracts trait method declarations and bridges trait dispatch to the impl', async () => {
- const dir = rustProject({
- 'lib.rs': 'pub mod types;\npub mod consumer;\n',
- 'types.rs': 'pub trait Render { fn render(&self) -> i32; }\n',
- // Mine implements Render structurally; reached via &dyn Render dispatch.
- 'consumer.rs': 'use crate::types::Render;\npub struct Mine { pub x: i32 }\nimpl Render for Mine { fn render(&self) -> i32 { self.x } }\n',
- });
- try {
- const cg = CodeGraph.initSync(dir, { config: { include: ['src/**/*.rs'], exclude: [] } });
- await cg.indexAll();
- cg.resolveReferences();
- // implements edge (Mine -> Render) makes types.rs a dependent of consumer.rs's struct.
- expect(cg.getFileDependents('src/types.rs')).toContain('src/consumer.rs');
- cg.destroy();
- } finally { cleanupTempDir(dir); }
- });
- it('links `pub use` re-export hubs to the modules they re-export', async () => {
- const dir = rustProject({
- 'lib.rs': 'pub mod api;\n',
- 'api/mod.rs': 'mod widget;\npub use self::widget::Widget;\n',
- 'api/widget.rs': 'pub struct Widget { pub n: i32 }\n',
- });
- try {
- const cg = CodeGraph.initSync(dir, { config: { include: ['src/**/*.rs'], exclude: [] } });
- await cg.indexAll();
- cg.resolveReferences();
- // The re-export hub depends on the module it re-exports from.
- expect(cg.getFileDependents('src/api/widget.rs')).toContain('src/api/mod.rs');
- cg.destroy();
- } finally { cleanupTempDir(dir); }
- });
- it('resolves a qualified path to the correct module when the leaf name collides', async () => {
- const dir = rustProject({
- 'lib.rs': 'pub mod fast;\npub mod slow;\npub mod hub;\n',
- 'fast.rs': 'pub fn read() -> i32 { 1 }\n',
- 'slow.rs': 'pub fn read() -> i32 { 2 }\n',
- // `read` exists in BOTH fast.rs and slow.rs — module-path resolution must
- // send this re-export to fast.rs specifically, not name-match either.
- 'hub.rs': 'pub use crate::fast::read;\n',
- });
- try {
- const cg = CodeGraph.initSync(dir, { config: { include: ['src/**/*.rs'], exclude: [] } });
- await cg.indexAll();
- cg.resolveReferences();
- expect(cg.getFileDependents('src/fast.rs')).toContain('src/hub.rs');
- expect(cg.getFileDependents('src/slow.rs')).not.toContain('src/hub.rs');
- cg.destroy();
- } finally { cleanupTempDir(dir); }
- });
- });
- describe('Java annotations (blast-radius recall)', () => {
- it('indexes @interface definitions and links @Annotation usages to them', async () => {
- const dir = createTempDir();
- try {
- fs.mkdirSync(path.join(dir, 'p'), { recursive: true });
- // The annotation DEFINITION must be a node, and the @MyAnno usages (which
- // live inside a `modifiers` node on the class/field/method) must extract.
- fs.writeFileSync(path.join(dir, 'p', 'MyAnno.java'), `package p;\npublic @interface MyAnno { String value() default ""; }\n`);
- fs.writeFileSync(
- path.join(dir, 'p', 'User.java'),
- `package p;\n@MyAnno("c")\npublic class User {\n @MyAnno("f") int field;\n @MyAnno("m") void go() {}\n}\n`
- );
- const cg = CodeGraph.initSync(dir, { config: { include: ['**/*.java'], exclude: [] } });
- await cg.indexAll();
- cg.resolveReferences();
- expect(cg.getFileDependents('p/MyAnno.java')).toContain('p/User.java');
- cg.destroy();
- } finally { cleanupTempDir(dir); }
- });
- });
- describe('Swift property wrappers / attributes (blast-radius recall)', () => {
- it('links a @propertyWrapper usage to the wrapper type', async () => {
- const dir = createTempDir();
- try {
- fs.mkdirSync(path.join(dir, 'Sources', 'M'), { recursive: true });
- fs.writeFileSync(path.join(dir, 'Sources', 'M', 'Wrap.swift'), `@propertyWrapper\npublic struct Argument<T> { public var wrappedValue: T }\n`);
- // `@Argument` is a Swift attribute on a stored property — it lives in the
- // property's `modifiers` and Swift doesn't extract instance properties as
- // their own nodes, so without the fix the wrapper type has no users.
- fs.writeFileSync(path.join(dir, 'Sources', 'M', 'Cmd.swift'), `public struct MyCommand {\n @Argument var name: String\n @Argument var count: Int\n}\n`);
- const cg = CodeGraph.initSync(dir, { config: { include: ['Sources/**/*.swift'], exclude: [] } });
- await cg.indexAll();
- cg.resolveReferences();
- expect(cg.getFileDependents('Sources/M/Wrap.swift')).toContain('Sources/M/Cmd.swift');
- cg.destroy();
- } finally { cleanupTempDir(dir); }
- });
- });
|