tree-sitter.ts 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358
  1. /**
  2. * Tree-sitter Parser Wrapper
  3. *
  4. * Handles parsing source code and extracting structural information.
  5. */
  6. import { Node as SyntaxNode, Tree } from 'web-tree-sitter';
  7. import * as crypto from 'crypto';
  8. import * as path from 'path';
  9. import {
  10. Language,
  11. Node,
  12. Edge,
  13. NodeKind,
  14. ExtractionResult,
  15. ExtractionError,
  16. UnresolvedReference,
  17. } from '../types';
  18. import { getParser, detectLanguage, isLanguageSupported } from './grammars';
  19. /**
  20. * Generate a unique node ID
  21. *
  22. * Uses a 32-character (128-bit) hash to avoid collisions when indexing
  23. * large codebases with many files containing similar symbols.
  24. */
  25. export function generateNodeId(
  26. filePath: string,
  27. kind: NodeKind,
  28. name: string,
  29. line: number
  30. ): string {
  31. const hash = crypto
  32. .createHash('sha256')
  33. .update(`${filePath}:${kind}:${name}:${line}`)
  34. .digest('hex')
  35. .substring(0, 32);
  36. return `${kind}:${hash}`;
  37. }
  38. /**
  39. * Extract text from a syntax node
  40. */
  41. function getNodeText(node: SyntaxNode, source: string): string {
  42. return source.substring(node.startIndex, node.endIndex);
  43. }
  44. /**
  45. * Find a child node by field name
  46. */
  47. function getChildByField(node: SyntaxNode, fieldName: string): SyntaxNode | null {
  48. return node.childForFieldName(fieldName);
  49. }
  50. /**
  51. * Get the docstring/comment preceding a node
  52. */
  53. function getPrecedingDocstring(node: SyntaxNode, source: string): string | undefined {
  54. let sibling = node.previousNamedSibling;
  55. const comments: string[] = [];
  56. while (sibling) {
  57. if (
  58. sibling.type === 'comment' ||
  59. sibling.type === 'line_comment' ||
  60. sibling.type === 'block_comment' ||
  61. sibling.type === 'documentation_comment'
  62. ) {
  63. comments.unshift(getNodeText(sibling, source));
  64. sibling = sibling.previousNamedSibling;
  65. } else {
  66. break;
  67. }
  68. }
  69. if (comments.length === 0) return undefined;
  70. // Clean up comment markers
  71. return comments
  72. .map((c) =>
  73. c
  74. .replace(/^\/\*\*?|\*\/$/g, '')
  75. .replace(/^\/\/\s?/gm, '')
  76. .replace(/^\s*\*\s?/gm, '')
  77. .trim()
  78. )
  79. .join('\n')
  80. .trim();
  81. }
  82. /**
  83. * Language-specific extraction configuration
  84. */
  85. interface LanguageExtractor {
  86. /** Node types that represent functions */
  87. functionTypes: string[];
  88. /** Node types that represent classes */
  89. classTypes: string[];
  90. /** Node types that represent methods */
  91. methodTypes: string[];
  92. /** Node types that represent interfaces/protocols/traits */
  93. interfaceTypes: string[];
  94. /** Node types that represent structs */
  95. structTypes: string[];
  96. /** Node types that represent enums */
  97. enumTypes: string[];
  98. /** Node types that represent type aliases (e.g. `type X = ...`) */
  99. typeAliasTypes: string[];
  100. /** Node types that represent imports */
  101. importTypes: string[];
  102. /** Node types that represent function calls */
  103. callTypes: string[];
  104. /** Node types that represent variable declarations (const, let, var, etc.) */
  105. variableTypes: string[];
  106. /** Field name for identifier/name */
  107. nameField: string;
  108. /** Field name for body */
  109. bodyField: string;
  110. /** Field name for parameters */
  111. paramsField: string;
  112. /** Field name for return type */
  113. returnField?: string;
  114. /** Extract signature from node */
  115. getSignature?: (node: SyntaxNode, source: string) => string | undefined;
  116. /** Extract visibility from node */
  117. getVisibility?: (node: SyntaxNode) => 'public' | 'private' | 'protected' | 'internal' | undefined;
  118. /** Check if node is exported */
  119. isExported?: (node: SyntaxNode, source: string) => boolean;
  120. /** Check if node is async */
  121. isAsync?: (node: SyntaxNode) => boolean;
  122. /** Check if node is static */
  123. isStatic?: (node: SyntaxNode) => boolean;
  124. /** Check if variable declaration is a constant (const vs let/var) */
  125. isConst?: (node: SyntaxNode) => boolean;
  126. }
  127. /**
  128. * Language-specific extractors
  129. */
  130. const EXTRACTORS: Partial<Record<Language, LanguageExtractor>> = {
  131. typescript: {
  132. functionTypes: ['function_declaration', 'arrow_function', 'function_expression'],
  133. classTypes: ['class_declaration'],
  134. methodTypes: ['method_definition', 'public_field_definition'],
  135. interfaceTypes: ['interface_declaration'],
  136. structTypes: [],
  137. enumTypes: ['enum_declaration'],
  138. typeAliasTypes: ['type_alias_declaration'],
  139. importTypes: ['import_statement'],
  140. callTypes: ['call_expression'],
  141. variableTypes: ['lexical_declaration', 'variable_declaration'],
  142. nameField: 'name',
  143. bodyField: 'body',
  144. paramsField: 'parameters',
  145. returnField: 'return_type',
  146. getSignature: (node, source) => {
  147. const params = getChildByField(node, 'parameters');
  148. const returnType = getChildByField(node, 'return_type');
  149. if (!params) return undefined;
  150. let sig = getNodeText(params, source);
  151. if (returnType) {
  152. sig += ': ' + getNodeText(returnType, source).replace(/^:\s*/, '');
  153. }
  154. return sig;
  155. },
  156. getVisibility: (node) => {
  157. for (let i = 0; i < node.childCount; i++) {
  158. const child = node.child(i);
  159. if (child?.type === 'accessibility_modifier') {
  160. const text = child.text;
  161. if (text === 'public') return 'public';
  162. if (text === 'private') return 'private';
  163. if (text === 'protected') return 'protected';
  164. }
  165. }
  166. return undefined;
  167. },
  168. isExported: (node, _source) => {
  169. // Walk the parent chain to find an export_statement ancestor.
  170. // This correctly handles deeply nested nodes like arrow functions
  171. // inside variable declarations: `export const X = () => { ... }`
  172. // where the arrow_function is 3 levels deep under export_statement.
  173. let current = node.parent;
  174. while (current) {
  175. if (current.type === 'export_statement') return true;
  176. current = current.parent;
  177. }
  178. return false;
  179. },
  180. isAsync: (node) => {
  181. for (let i = 0; i < node.childCount; i++) {
  182. const child = node.child(i);
  183. if (child?.type === 'async') return true;
  184. }
  185. return false;
  186. },
  187. isStatic: (node) => {
  188. for (let i = 0; i < node.childCount; i++) {
  189. const child = node.child(i);
  190. if (child?.type === 'static') return true;
  191. }
  192. return false;
  193. },
  194. isConst: (node) => {
  195. // For lexical_declaration, check if it's 'const' or 'let'
  196. // For variable_declaration, it's always 'var'
  197. if (node.type === 'lexical_declaration') {
  198. for (let i = 0; i < node.childCount; i++) {
  199. const child = node.child(i);
  200. if (child?.type === 'const') return true;
  201. }
  202. }
  203. return false;
  204. },
  205. },
  206. javascript: {
  207. functionTypes: ['function_declaration', 'arrow_function', 'function_expression'],
  208. classTypes: ['class_declaration'],
  209. methodTypes: ['method_definition', 'field_definition'],
  210. interfaceTypes: [],
  211. structTypes: [],
  212. enumTypes: [],
  213. typeAliasTypes: [],
  214. importTypes: ['import_statement'],
  215. callTypes: ['call_expression'],
  216. variableTypes: ['lexical_declaration', 'variable_declaration'],
  217. nameField: 'name',
  218. bodyField: 'body',
  219. paramsField: 'parameters',
  220. getSignature: (node, source) => {
  221. const params = getChildByField(node, 'parameters');
  222. return params ? getNodeText(params, source) : undefined;
  223. },
  224. isExported: (node, _source) => {
  225. let current = node.parent;
  226. while (current) {
  227. if (current.type === 'export_statement') return true;
  228. current = current.parent;
  229. }
  230. return false;
  231. },
  232. isAsync: (node) => {
  233. for (let i = 0; i < node.childCount; i++) {
  234. const child = node.child(i);
  235. if (child?.type === 'async') return true;
  236. }
  237. return false;
  238. },
  239. isConst: (node) => {
  240. if (node.type === 'lexical_declaration') {
  241. for (let i = 0; i < node.childCount; i++) {
  242. const child = node.child(i);
  243. if (child?.type === 'const') return true;
  244. }
  245. }
  246. return false;
  247. },
  248. },
  249. python: {
  250. functionTypes: ['function_definition'],
  251. classTypes: ['class_definition'],
  252. methodTypes: ['function_definition'], // Methods are functions inside classes
  253. interfaceTypes: [],
  254. structTypes: [],
  255. enumTypes: [],
  256. typeAliasTypes: [],
  257. importTypes: ['import_statement', 'import_from_statement'],
  258. callTypes: ['call'],
  259. variableTypes: ['assignment'], // Python uses assignment for variable declarations
  260. nameField: 'name',
  261. bodyField: 'body',
  262. paramsField: 'parameters',
  263. returnField: 'return_type',
  264. getSignature: (node, source) => {
  265. const params = getChildByField(node, 'parameters');
  266. const returnType = getChildByField(node, 'return_type');
  267. if (!params) return undefined;
  268. let sig = getNodeText(params, source);
  269. if (returnType) {
  270. sig += ' -> ' + getNodeText(returnType, source);
  271. }
  272. return sig;
  273. },
  274. isAsync: (node) => {
  275. const prev = node.previousSibling;
  276. return prev?.type === 'async';
  277. },
  278. isStatic: (node) => {
  279. // Check for @staticmethod decorator
  280. const prev = node.previousNamedSibling;
  281. if (prev?.type === 'decorator') {
  282. const text = prev.text;
  283. return text.includes('staticmethod');
  284. }
  285. return false;
  286. },
  287. },
  288. go: {
  289. functionTypes: ['function_declaration'],
  290. classTypes: [], // Go doesn't have classes
  291. methodTypes: ['method_declaration'],
  292. interfaceTypes: ['interface_type'],
  293. structTypes: ['struct_type'],
  294. enumTypes: [],
  295. typeAliasTypes: ['type_spec'], // Go type declarations
  296. importTypes: ['import_declaration'],
  297. callTypes: ['call_expression'],
  298. variableTypes: ['var_declaration', 'short_var_declaration', 'const_declaration'],
  299. nameField: 'name',
  300. bodyField: 'body',
  301. paramsField: 'parameters',
  302. returnField: 'result',
  303. getSignature: (node, source) => {
  304. const params = getChildByField(node, 'parameters');
  305. const result = getChildByField(node, 'result');
  306. if (!params) return undefined;
  307. let sig = getNodeText(params, source);
  308. if (result) {
  309. sig += ' ' + getNodeText(result, source);
  310. }
  311. return sig;
  312. },
  313. },
  314. rust: {
  315. functionTypes: ['function_item'],
  316. classTypes: [], // Rust has impl blocks
  317. methodTypes: ['function_item'], // Methods are functions in impl blocks
  318. interfaceTypes: ['trait_item'],
  319. structTypes: ['struct_item'],
  320. enumTypes: ['enum_item'],
  321. typeAliasTypes: ['type_item'], // Rust type aliases
  322. importTypes: ['use_declaration'],
  323. callTypes: ['call_expression'],
  324. variableTypes: ['let_declaration', 'const_item', 'static_item'],
  325. nameField: 'name',
  326. bodyField: 'body',
  327. paramsField: 'parameters',
  328. returnField: 'return_type',
  329. getSignature: (node, source) => {
  330. const params = getChildByField(node, 'parameters');
  331. const returnType = getChildByField(node, 'return_type');
  332. if (!params) return undefined;
  333. let sig = getNodeText(params, source);
  334. if (returnType) {
  335. sig += ' -> ' + getNodeText(returnType, source);
  336. }
  337. return sig;
  338. },
  339. isAsync: (node) => {
  340. for (let i = 0; i < node.childCount; i++) {
  341. const child = node.child(i);
  342. if (child?.type === 'async') return true;
  343. }
  344. return false;
  345. },
  346. getVisibility: (node) => {
  347. for (let i = 0; i < node.childCount; i++) {
  348. const child = node.child(i);
  349. if (child?.type === 'visibility_modifier') {
  350. return child.text.includes('pub') ? 'public' : 'private';
  351. }
  352. }
  353. return 'private'; // Rust defaults to private
  354. },
  355. },
  356. java: {
  357. functionTypes: [],
  358. classTypes: ['class_declaration'],
  359. methodTypes: ['method_declaration', 'constructor_declaration'],
  360. interfaceTypes: ['interface_declaration'],
  361. structTypes: [],
  362. enumTypes: ['enum_declaration'],
  363. typeAliasTypes: [],
  364. importTypes: ['import_declaration'],
  365. callTypes: ['method_invocation'],
  366. variableTypes: ['local_variable_declaration', 'field_declaration'],
  367. nameField: 'name',
  368. bodyField: 'body',
  369. paramsField: 'parameters',
  370. returnField: 'type',
  371. getSignature: (node, source) => {
  372. const params = getChildByField(node, 'parameters');
  373. const returnType = getChildByField(node, 'type');
  374. if (!params) return undefined;
  375. const paramsText = getNodeText(params, source);
  376. return returnType ? getNodeText(returnType, source) + ' ' + paramsText : paramsText;
  377. },
  378. getVisibility: (node) => {
  379. for (let i = 0; i < node.childCount; i++) {
  380. const child = node.child(i);
  381. if (child?.type === 'modifiers') {
  382. const text = child.text;
  383. if (text.includes('public')) return 'public';
  384. if (text.includes('private')) return 'private';
  385. if (text.includes('protected')) return 'protected';
  386. }
  387. }
  388. return undefined;
  389. },
  390. isStatic: (node) => {
  391. for (let i = 0; i < node.childCount; i++) {
  392. const child = node.child(i);
  393. if (child?.type === 'modifiers' && child.text.includes('static')) {
  394. return true;
  395. }
  396. }
  397. return false;
  398. },
  399. },
  400. c: {
  401. functionTypes: ['function_definition'],
  402. classTypes: [],
  403. methodTypes: [],
  404. interfaceTypes: [],
  405. structTypes: ['struct_specifier'],
  406. enumTypes: ['enum_specifier'],
  407. typeAliasTypes: ['type_definition'], // typedef
  408. importTypes: ['preproc_include'],
  409. callTypes: ['call_expression'],
  410. variableTypes: ['declaration'],
  411. nameField: 'declarator',
  412. bodyField: 'body',
  413. paramsField: 'parameters',
  414. },
  415. cpp: {
  416. functionTypes: ['function_definition'],
  417. classTypes: ['class_specifier'],
  418. methodTypes: ['function_definition'],
  419. interfaceTypes: [],
  420. structTypes: ['struct_specifier'],
  421. enumTypes: ['enum_specifier'],
  422. typeAliasTypes: ['type_definition', 'alias_declaration'], // typedef and using
  423. importTypes: ['preproc_include'],
  424. callTypes: ['call_expression'],
  425. variableTypes: ['declaration'],
  426. nameField: 'declarator',
  427. bodyField: 'body',
  428. paramsField: 'parameters',
  429. getVisibility: (node) => {
  430. // Check for access specifier in parent
  431. const parent = node.parent;
  432. if (parent) {
  433. for (let i = 0; i < parent.childCount; i++) {
  434. const child = parent.child(i);
  435. if (child?.type === 'access_specifier') {
  436. const text = child.text;
  437. if (text.includes('public')) return 'public';
  438. if (text.includes('private')) return 'private';
  439. if (text.includes('protected')) return 'protected';
  440. }
  441. }
  442. }
  443. return undefined;
  444. },
  445. },
  446. csharp: {
  447. functionTypes: [],
  448. classTypes: ['class_declaration'],
  449. methodTypes: ['method_declaration', 'constructor_declaration'],
  450. interfaceTypes: ['interface_declaration'],
  451. structTypes: ['struct_declaration'],
  452. enumTypes: ['enum_declaration'],
  453. typeAliasTypes: [],
  454. importTypes: ['using_directive'],
  455. callTypes: ['invocation_expression'],
  456. variableTypes: ['local_declaration_statement', 'field_declaration'],
  457. nameField: 'name',
  458. bodyField: 'body',
  459. paramsField: 'parameter_list',
  460. getVisibility: (node) => {
  461. for (let i = 0; i < node.childCount; i++) {
  462. const child = node.child(i);
  463. if (child?.type === 'modifier') {
  464. const text = child.text;
  465. if (text === 'public') return 'public';
  466. if (text === 'private') return 'private';
  467. if (text === 'protected') return 'protected';
  468. if (text === 'internal') return 'internal';
  469. }
  470. }
  471. return 'private'; // C# defaults to private
  472. },
  473. isStatic: (node) => {
  474. for (let i = 0; i < node.childCount; i++) {
  475. const child = node.child(i);
  476. if (child?.type === 'modifier' && child.text === 'static') {
  477. return true;
  478. }
  479. }
  480. return false;
  481. },
  482. isAsync: (node) => {
  483. for (let i = 0; i < node.childCount; i++) {
  484. const child = node.child(i);
  485. if (child?.type === 'modifier' && child.text === 'async') {
  486. return true;
  487. }
  488. }
  489. return false;
  490. },
  491. },
  492. php: {
  493. functionTypes: ['function_definition'],
  494. classTypes: ['class_declaration'],
  495. methodTypes: ['method_declaration'],
  496. interfaceTypes: ['interface_declaration'],
  497. structTypes: [],
  498. enumTypes: ['enum_declaration'],
  499. typeAliasTypes: [],
  500. importTypes: ['namespace_use_declaration'],
  501. callTypes: ['function_call_expression', 'member_call_expression', 'scoped_call_expression'],
  502. variableTypes: ['property_declaration', 'const_declaration'],
  503. nameField: 'name',
  504. bodyField: 'body',
  505. paramsField: 'parameters',
  506. returnField: 'return_type',
  507. getVisibility: (node) => {
  508. for (let i = 0; i < node.childCount; i++) {
  509. const child = node.child(i);
  510. if (child?.type === 'visibility_modifier') {
  511. const text = child.text;
  512. if (text === 'public') return 'public';
  513. if (text === 'private') return 'private';
  514. if (text === 'protected') return 'protected';
  515. }
  516. }
  517. return 'public'; // PHP defaults to public
  518. },
  519. isStatic: (node) => {
  520. for (let i = 0; i < node.childCount; i++) {
  521. const child = node.child(i);
  522. if (child?.type === 'static_modifier') return true;
  523. }
  524. return false;
  525. },
  526. },
  527. ruby: {
  528. functionTypes: ['method'],
  529. classTypes: ['class'],
  530. methodTypes: ['method', 'singleton_method'],
  531. interfaceTypes: [], // Ruby uses modules
  532. structTypes: [],
  533. enumTypes: [],
  534. typeAliasTypes: [],
  535. importTypes: ['call'], // require/require_relative
  536. callTypes: ['call', 'method_call'],
  537. variableTypes: ['assignment'], // Ruby uses assignment like Python
  538. nameField: 'name',
  539. bodyField: 'body',
  540. paramsField: 'parameters',
  541. getVisibility: (node) => {
  542. // Ruby visibility is based on preceding visibility modifiers
  543. let sibling = node.previousNamedSibling;
  544. while (sibling) {
  545. if (sibling.type === 'call') {
  546. const methodName = getChildByField(sibling, 'method');
  547. if (methodName) {
  548. const text = methodName.text;
  549. if (text === 'private') return 'private';
  550. if (text === 'protected') return 'protected';
  551. if (text === 'public') return 'public';
  552. }
  553. }
  554. sibling = sibling.previousNamedSibling;
  555. }
  556. return 'public';
  557. },
  558. },
  559. swift: {
  560. functionTypes: ['function_declaration'],
  561. classTypes: ['class_declaration'],
  562. methodTypes: ['function_declaration'], // Methods are functions inside classes
  563. interfaceTypes: ['protocol_declaration'],
  564. structTypes: ['struct_declaration'],
  565. enumTypes: ['enum_declaration'],
  566. typeAliasTypes: ['typealias_declaration'],
  567. importTypes: ['import_declaration'],
  568. callTypes: ['call_expression'],
  569. variableTypes: ['property_declaration', 'constant_declaration'],
  570. nameField: 'name',
  571. bodyField: 'body',
  572. paramsField: 'parameter',
  573. returnField: 'return_type',
  574. getSignature: (node, source) => {
  575. // Swift function signature: func name(params) -> ReturnType
  576. const params = getChildByField(node, 'parameter');
  577. const returnType = getChildByField(node, 'return_type');
  578. if (!params) return undefined;
  579. let sig = getNodeText(params, source);
  580. if (returnType) {
  581. sig += ' -> ' + getNodeText(returnType, source);
  582. }
  583. return sig;
  584. },
  585. getVisibility: (node) => {
  586. // Check for visibility modifiers in Swift
  587. for (let i = 0; i < node.childCount; i++) {
  588. const child = node.child(i);
  589. if (child?.type === 'modifiers') {
  590. const text = child.text;
  591. if (text.includes('public')) return 'public';
  592. if (text.includes('private')) return 'private';
  593. if (text.includes('internal')) return 'internal';
  594. if (text.includes('fileprivate')) return 'private';
  595. }
  596. }
  597. return 'internal'; // Swift defaults to internal
  598. },
  599. isStatic: (node) => {
  600. for (let i = 0; i < node.childCount; i++) {
  601. const child = node.child(i);
  602. if (child?.type === 'modifiers') {
  603. if (child.text.includes('static') || child.text.includes('class')) {
  604. return true;
  605. }
  606. }
  607. }
  608. return false;
  609. },
  610. isAsync: (node) => {
  611. for (let i = 0; i < node.childCount; i++) {
  612. const child = node.child(i);
  613. if (child?.type === 'modifiers' && child.text.includes('async')) {
  614. return true;
  615. }
  616. }
  617. return false;
  618. },
  619. },
  620. kotlin: {
  621. functionTypes: ['function_declaration'],
  622. classTypes: ['class_declaration'],
  623. methodTypes: ['function_declaration'], // Methods are functions inside classes
  624. interfaceTypes: ['class_declaration'], // Interfaces use class_declaration with 'interface' modifier
  625. structTypes: [], // Kotlin uses data classes
  626. enumTypes: ['class_declaration'], // Enums use class_declaration with 'enum' modifier
  627. typeAliasTypes: ['type_alias'],
  628. importTypes: ['import_header'],
  629. callTypes: ['call_expression'],
  630. variableTypes: ['property_declaration'],
  631. nameField: 'simple_identifier',
  632. bodyField: 'function_body',
  633. paramsField: 'function_value_parameters',
  634. returnField: 'type',
  635. getSignature: (node, source) => {
  636. // Kotlin function signature: fun name(params): ReturnType
  637. const params = getChildByField(node, 'function_value_parameters');
  638. const returnType = getChildByField(node, 'type');
  639. if (!params) return undefined;
  640. let sig = getNodeText(params, source);
  641. if (returnType) {
  642. sig += ': ' + getNodeText(returnType, source);
  643. }
  644. return sig;
  645. },
  646. getVisibility: (node) => {
  647. // Check for visibility modifiers in Kotlin
  648. for (let i = 0; i < node.childCount; i++) {
  649. const child = node.child(i);
  650. if (child?.type === 'modifiers') {
  651. const text = child.text;
  652. if (text.includes('public')) return 'public';
  653. if (text.includes('private')) return 'private';
  654. if (text.includes('protected')) return 'protected';
  655. if (text.includes('internal')) return 'internal';
  656. }
  657. }
  658. return 'public'; // Kotlin defaults to public
  659. },
  660. isStatic: (_node) => {
  661. // Kotlin doesn't have static, uses companion objects
  662. // Check if inside companion object would require more context
  663. return false;
  664. },
  665. isAsync: (node) => {
  666. // Kotlin uses suspend keyword for coroutines
  667. for (let i = 0; i < node.childCount; i++) {
  668. const child = node.child(i);
  669. if (child?.type === 'modifiers' && child.text.includes('suspend')) {
  670. return true;
  671. }
  672. }
  673. return false;
  674. },
  675. },
  676. dart: {
  677. functionTypes: ['function_signature'],
  678. classTypes: ['class_definition'],
  679. methodTypes: ['method_signature'],
  680. interfaceTypes: [],
  681. structTypes: [],
  682. enumTypes: ['enum_declaration'],
  683. typeAliasTypes: ['type_alias'],
  684. importTypes: ['import_or_export'],
  685. callTypes: [], // Dart calls use identifier+selector, handled via function body traversal
  686. variableTypes: [],
  687. nameField: 'name',
  688. bodyField: 'body', // class_definition uses 'body' field
  689. paramsField: 'formal_parameter_list',
  690. returnField: 'type',
  691. getSignature: (node, source) => {
  692. // For function_signature: extract params + return type
  693. // For method_signature: delegate to inner function_signature
  694. let sig = node;
  695. if (node.type === 'method_signature') {
  696. const inner = node.namedChildren.find((c: SyntaxNode) =>
  697. c.type === 'function_signature' || c.type === 'getter_signature' || c.type === 'setter_signature'
  698. );
  699. if (inner) sig = inner;
  700. }
  701. const params = sig.namedChildren.find((c: SyntaxNode) => c.type === 'formal_parameter_list');
  702. const retType = sig.namedChildren.find((c: SyntaxNode) =>
  703. c.type === 'type_identifier' || c.type === 'void_type'
  704. );
  705. if (!params && !retType) return undefined;
  706. let result = '';
  707. if (retType) result += getNodeText(retType, source) + ' ';
  708. if (params) result += getNodeText(params, source);
  709. return result.trim() || undefined;
  710. },
  711. getVisibility: (node) => {
  712. // Dart convention: _ prefix means private, otherwise public
  713. let nameNode: SyntaxNode | null = null;
  714. if (node.type === 'method_signature') {
  715. const inner = node.namedChildren.find((c: SyntaxNode) =>
  716. c.type === 'function_signature' || c.type === 'getter_signature' || c.type === 'setter_signature'
  717. );
  718. if (inner) nameNode = inner.namedChildren.find((c: SyntaxNode) => c.type === 'identifier') || null;
  719. } else {
  720. nameNode = node.childForFieldName('name');
  721. }
  722. if (nameNode && nameNode.text.startsWith('_')) return 'private';
  723. return 'public';
  724. },
  725. isAsync: (node) => {
  726. // In Dart, 'async' is on the function_body (next sibling), not the signature
  727. const nextSibling = node.nextNamedSibling;
  728. if (nextSibling?.type === 'function_body') {
  729. for (let i = 0; i < nextSibling.childCount; i++) {
  730. const child = nextSibling.child(i);
  731. if (child?.type === 'async') return true;
  732. }
  733. }
  734. return false;
  735. },
  736. isStatic: (node) => {
  737. // For method_signature, check for 'static' child
  738. if (node.type === 'method_signature') {
  739. for (let i = 0; i < node.childCount; i++) {
  740. const child = node.child(i);
  741. if (child?.type === 'static') return true;
  742. }
  743. }
  744. return false;
  745. },
  746. },
  747. pascal: {
  748. functionTypes: ['declProc'],
  749. classTypes: ['declClass'],
  750. methodTypes: ['declProc'],
  751. interfaceTypes: ['declIntf'],
  752. structTypes: [],
  753. enumTypes: ['declEnum'],
  754. typeAliasTypes: ['declType'],
  755. importTypes: ['declUses'],
  756. callTypes: ['exprCall'],
  757. variableTypes: ['declField', 'declConst'],
  758. nameField: 'name',
  759. bodyField: 'body',
  760. paramsField: 'args',
  761. returnField: 'type',
  762. getSignature: (node, source) => {
  763. const args = getChildByField(node, 'args');
  764. const returnType = node.namedChildren.find(
  765. (c: SyntaxNode) => c.type === 'typeref'
  766. );
  767. if (!args && !returnType) return undefined;
  768. let sig = '';
  769. if (args) sig = getNodeText(args, source);
  770. if (returnType) {
  771. sig += ': ' + getNodeText(returnType, source);
  772. }
  773. return sig || undefined;
  774. },
  775. getVisibility: (node) => {
  776. let current = node.parent;
  777. while (current) {
  778. if (current.type === 'declSection') {
  779. for (let i = 0; i < current.childCount; i++) {
  780. const child = current.child(i);
  781. if (child?.type === 'kPublic' || child?.type === 'kPublished')
  782. return 'public';
  783. if (child?.type === 'kPrivate') return 'private';
  784. if (child?.type === 'kProtected') return 'protected';
  785. }
  786. }
  787. current = current.parent;
  788. }
  789. return undefined;
  790. },
  791. isExported: (_node, _source) => {
  792. // In Pascal, symbols declared in the interface section are exported
  793. return false;
  794. },
  795. isStatic: (node) => {
  796. for (let i = 0; i < node.childCount; i++) {
  797. if (node.child(i)?.type === 'kClass') return true;
  798. }
  799. return false;
  800. },
  801. isConst: (node) => {
  802. return node.type === 'declConst';
  803. },
  804. },
  805. };
  806. // TSX and JSX use the same extractors as their base languages
  807. EXTRACTORS.tsx = EXTRACTORS.typescript;
  808. EXTRACTORS.jsx = EXTRACTORS.javascript;
  809. /**
  810. * Extract the name from a node based on language
  811. */
  812. function extractName(node: SyntaxNode, source: string, extractor: LanguageExtractor): string {
  813. // Try field name first
  814. const nameNode = getChildByField(node, extractor.nameField);
  815. if (nameNode) {
  816. // Handle complex declarators (C/C++)
  817. if (nameNode.type === 'function_declarator' || nameNode.type === 'declarator') {
  818. const innerName = getChildByField(nameNode, 'declarator') || nameNode.namedChild(0);
  819. return innerName ? getNodeText(innerName, source) : getNodeText(nameNode, source);
  820. }
  821. return getNodeText(nameNode, source);
  822. }
  823. // For Dart method_signature, look inside inner signature types
  824. if (node.type === 'method_signature') {
  825. for (let i = 0; i < node.namedChildCount; i++) {
  826. const child = node.namedChild(i);
  827. if (child && (
  828. child.type === 'function_signature' ||
  829. child.type === 'getter_signature' ||
  830. child.type === 'setter_signature' ||
  831. child.type === 'constructor_signature' ||
  832. child.type === 'factory_constructor_signature'
  833. )) {
  834. // Find identifier inside the inner signature
  835. for (let j = 0; j < child.namedChildCount; j++) {
  836. const inner = child.namedChild(j);
  837. if (inner?.type === 'identifier') {
  838. return getNodeText(inner, source);
  839. }
  840. }
  841. }
  842. }
  843. }
  844. // Fall back to first identifier child
  845. for (let i = 0; i < node.namedChildCount; i++) {
  846. const child = node.namedChild(i);
  847. if (
  848. child &&
  849. (child.type === 'identifier' ||
  850. child.type === 'type_identifier' ||
  851. child.type === 'simple_identifier' ||
  852. child.type === 'constant')
  853. ) {
  854. return getNodeText(child, source);
  855. }
  856. }
  857. return '<anonymous>';
  858. }
  859. /**
  860. * TreeSitterExtractor - Main extraction class
  861. */
  862. export class TreeSitterExtractor {
  863. private filePath: string;
  864. private language: Language;
  865. private source: string;
  866. private tree: Tree | null = null;
  867. private nodes: Node[] = [];
  868. private edges: Edge[] = [];
  869. private unresolvedReferences: UnresolvedReference[] = [];
  870. private errors: ExtractionError[] = [];
  871. private extractor: LanguageExtractor | null = null;
  872. private nodeStack: string[] = []; // Stack of parent node IDs
  873. private methodIndex: Map<string, string> | null = null; // lookup key → node ID for Pascal defProc lookup
  874. constructor(filePath: string, source: string, language?: Language) {
  875. this.filePath = filePath;
  876. this.source = source;
  877. this.language = language || detectLanguage(filePath);
  878. this.extractor = EXTRACTORS[this.language] || null;
  879. }
  880. /**
  881. * Parse and extract from the source code
  882. */
  883. extract(): ExtractionResult {
  884. const startTime = Date.now();
  885. if (!isLanguageSupported(this.language)) {
  886. return {
  887. nodes: [],
  888. edges: [],
  889. unresolvedReferences: [],
  890. errors: [
  891. {
  892. message: `Unsupported language: ${this.language}`,
  893. severity: 'error',
  894. },
  895. ],
  896. durationMs: Date.now() - startTime,
  897. };
  898. }
  899. const parser = getParser(this.language);
  900. if (!parser) {
  901. return {
  902. nodes: [],
  903. edges: [],
  904. unresolvedReferences: [],
  905. errors: [
  906. {
  907. message: `Failed to get parser for language: ${this.language}`,
  908. severity: 'error',
  909. },
  910. ],
  911. durationMs: Date.now() - startTime,
  912. };
  913. }
  914. try {
  915. this.tree = parser.parse(this.source) ?? null;
  916. if (!this.tree) {
  917. throw new Error('Parser returned null tree');
  918. }
  919. // Create file node representing the source file
  920. const fileNode: Node = {
  921. id: `file:${this.filePath}`,
  922. kind: 'file',
  923. name: path.basename(this.filePath),
  924. qualifiedName: this.filePath,
  925. filePath: this.filePath,
  926. language: this.language,
  927. startLine: 1,
  928. endLine: this.source.split('\n').length,
  929. startColumn: 0,
  930. endColumn: 0,
  931. isExported: false,
  932. updatedAt: Date.now(),
  933. };
  934. this.nodes.push(fileNode);
  935. // Push file node onto stack so top-level declarations get contains edges
  936. this.nodeStack.push(fileNode.id);
  937. this.visitNode(this.tree.rootNode);
  938. this.nodeStack.pop();
  939. } catch (error) {
  940. this.errors.push({
  941. message: `Parse error: ${error instanceof Error ? error.message : String(error)}`,
  942. severity: 'error',
  943. });
  944. }
  945. return {
  946. nodes: this.nodes,
  947. edges: this.edges,
  948. unresolvedReferences: this.unresolvedReferences,
  949. errors: this.errors,
  950. durationMs: Date.now() - startTime,
  951. };
  952. }
  953. /**
  954. * Visit a node and extract information
  955. */
  956. private visitNode(node: SyntaxNode): void {
  957. if (!this.extractor) return;
  958. const nodeType = node.type;
  959. let skipChildren = false;
  960. // Pascal-specific AST handling
  961. if (this.language === 'pascal') {
  962. skipChildren = this.visitPascalNode(node);
  963. if (skipChildren) return;
  964. }
  965. // Check for function declarations
  966. // For Python/Ruby, function_definition inside a class should be treated as method
  967. if (this.extractor.functionTypes.includes(nodeType)) {
  968. if (this.isInsideClassLikeNode() && this.extractor.methodTypes.includes(nodeType)) {
  969. // Inside a class - treat as method
  970. this.extractMethod(node);
  971. skipChildren = true; // extractMethod visits children via visitFunctionBody
  972. } else {
  973. this.extractFunction(node);
  974. skipChildren = true; // extractFunction visits children via visitFunctionBody
  975. }
  976. }
  977. // Check for class declarations
  978. else if (this.extractor.classTypes.includes(nodeType)) {
  979. // Swift uses class_declaration for both classes and structs
  980. // Check for 'struct' child to differentiate
  981. if (this.language === 'swift' && this.hasChildOfType(node, 'struct')) {
  982. this.extractStruct(node);
  983. } else if (this.language === 'swift' && this.hasChildOfType(node, 'enum')) {
  984. this.extractEnum(node);
  985. } else {
  986. this.extractClass(node);
  987. }
  988. skipChildren = true; // extractClass visits body children
  989. }
  990. // Dart-specific: mixin and extension declarations treated as classes
  991. else if (this.language === 'dart' && (nodeType === 'mixin_declaration' || nodeType === 'extension_declaration')) {
  992. this.extractClass(node);
  993. skipChildren = true;
  994. }
  995. // Check for method declarations (only if not already handled by functionTypes)
  996. else if (this.extractor.methodTypes.includes(nodeType)) {
  997. this.extractMethod(node);
  998. skipChildren = true; // extractMethod visits children via visitFunctionBody
  999. }
  1000. // Check for interface/protocol/trait declarations
  1001. else if (this.extractor.interfaceTypes.includes(nodeType)) {
  1002. this.extractInterface(node);
  1003. skipChildren = true; // extractInterface visits body children
  1004. }
  1005. // Check for struct declarations
  1006. else if (this.extractor.structTypes.includes(nodeType)) {
  1007. this.extractStruct(node);
  1008. skipChildren = true; // extractStruct visits body children
  1009. }
  1010. // Check for enum declarations
  1011. else if (this.extractor.enumTypes.includes(nodeType)) {
  1012. this.extractEnum(node);
  1013. skipChildren = true; // extractEnum visits body children
  1014. }
  1015. // Check for type alias declarations (e.g. `type X = ...` in TypeScript)
  1016. else if (this.extractor.typeAliasTypes.includes(nodeType)) {
  1017. this.extractTypeAlias(node);
  1018. }
  1019. // Check for variable declarations (const, let, var, etc.)
  1020. // Only extract top-level variables (not inside functions/methods)
  1021. else if (this.extractor.variableTypes.includes(nodeType) && !this.isInsideClassLikeNode()) {
  1022. this.extractVariable(node);
  1023. skipChildren = true; // extractVariable handles children
  1024. }
  1025. // Check for export statements containing non-function variable declarations
  1026. // e.g. `export const X = create(...)`, `export const X = { ... }`
  1027. else if (nodeType === 'export_statement') {
  1028. this.extractExportedVariables(node);
  1029. // Don't skip children — still need to visit inner nodes (functions, calls, etc.)
  1030. }
  1031. // Check for imports
  1032. else if (this.extractor.importTypes.includes(nodeType)) {
  1033. this.extractImport(node);
  1034. }
  1035. // Check for function calls
  1036. else if (this.extractor.callTypes.includes(nodeType)) {
  1037. this.extractCall(node);
  1038. }
  1039. // Visit children (unless the extract method already visited them)
  1040. if (!skipChildren) {
  1041. for (let i = 0; i < node.namedChildCount; i++) {
  1042. const child = node.namedChild(i);
  1043. if (child) {
  1044. this.visitNode(child);
  1045. }
  1046. }
  1047. }
  1048. }
  1049. /**
  1050. * Create a Node object
  1051. */
  1052. private createNode(
  1053. kind: NodeKind,
  1054. name: string,
  1055. node: SyntaxNode,
  1056. extra?: Partial<Node>
  1057. ): Node | null {
  1058. // Skip nodes with empty/missing names — they are not meaningful symbols
  1059. // and would cause FK violations when edges reference them (see issue #42)
  1060. if (!name) {
  1061. return null;
  1062. }
  1063. const id = generateNodeId(this.filePath, kind, name, node.startPosition.row + 1);
  1064. const newNode: Node = {
  1065. id,
  1066. kind,
  1067. name,
  1068. qualifiedName: this.buildQualifiedName(name),
  1069. filePath: this.filePath,
  1070. language: this.language,
  1071. startLine: node.startPosition.row + 1,
  1072. endLine: node.endPosition.row + 1,
  1073. startColumn: node.startPosition.column,
  1074. endColumn: node.endPosition.column,
  1075. updatedAt: Date.now(),
  1076. ...extra,
  1077. };
  1078. this.nodes.push(newNode);
  1079. // Add containment edge from parent
  1080. if (this.nodeStack.length > 0) {
  1081. const parentId = this.nodeStack[this.nodeStack.length - 1];
  1082. if (parentId) {
  1083. this.edges.push({
  1084. source: parentId,
  1085. target: id,
  1086. kind: 'contains',
  1087. });
  1088. }
  1089. }
  1090. return newNode;
  1091. }
  1092. /**
  1093. * Build qualified name from node stack
  1094. */
  1095. private buildQualifiedName(name: string): string {
  1096. // Get names from the node stack
  1097. const parts: string[] = [this.filePath];
  1098. for (const nodeId of this.nodeStack) {
  1099. const node = this.nodes.find((n) => n.id === nodeId);
  1100. if (node) {
  1101. parts.push(node.name);
  1102. }
  1103. }
  1104. parts.push(name);
  1105. return parts.join('::');
  1106. }
  1107. /**
  1108. * Check if a node has a child of a specific type
  1109. */
  1110. private hasChildOfType(node: SyntaxNode, type: string): boolean {
  1111. for (let i = 0; i < node.childCount; i++) {
  1112. const child = node.child(i);
  1113. if (child?.type === type) {
  1114. return true;
  1115. }
  1116. }
  1117. return false;
  1118. }
  1119. /**
  1120. * Check if the current node stack indicates we are inside a class-like node
  1121. * (class, struct, interface, trait). File nodes do not count as class-like.
  1122. */
  1123. private isInsideClassLikeNode(): boolean {
  1124. if (this.nodeStack.length === 0) return false;
  1125. const parentId = this.nodeStack[this.nodeStack.length - 1];
  1126. if (!parentId) return false;
  1127. const parentNode = this.nodes.find((n) => n.id === parentId);
  1128. if (!parentNode) return false;
  1129. return (
  1130. parentNode.kind === 'class' ||
  1131. parentNode.kind === 'struct' ||
  1132. parentNode.kind === 'interface' ||
  1133. parentNode.kind === 'trait' ||
  1134. parentNode.kind === 'enum'
  1135. );
  1136. }
  1137. /**
  1138. * Extract a function
  1139. */
  1140. private extractFunction(node: SyntaxNode): void {
  1141. if (!this.extractor) return;
  1142. let name = extractName(node, this.source, this.extractor);
  1143. // For arrow functions and function expressions assigned to variables,
  1144. // resolve the name from the parent variable_declarator.
  1145. // e.g. `export const useAuth = () => { ... }` — the arrow_function node
  1146. // has no `name` field; the name lives on the variable_declarator.
  1147. if (
  1148. name === '<anonymous>' &&
  1149. (node.type === 'arrow_function' || node.type === 'function_expression')
  1150. ) {
  1151. const parent = node.parent;
  1152. if (parent?.type === 'variable_declarator') {
  1153. const varName = getChildByField(parent, 'name');
  1154. if (varName) {
  1155. name = getNodeText(varName, this.source);
  1156. }
  1157. }
  1158. }
  1159. if (name === '<anonymous>') return; // Skip anonymous functions
  1160. const docstring = getPrecedingDocstring(node, this.source);
  1161. const signature = this.extractor.getSignature?.(node, this.source);
  1162. const visibility = this.extractor.getVisibility?.(node);
  1163. const isExported = this.extractor.isExported?.(node, this.source);
  1164. const isAsync = this.extractor.isAsync?.(node);
  1165. const isStatic = this.extractor.isStatic?.(node);
  1166. const funcNode = this.createNode('function', name, node, {
  1167. docstring,
  1168. signature,
  1169. visibility,
  1170. isExported,
  1171. isAsync,
  1172. isStatic,
  1173. });
  1174. if (!funcNode) return;
  1175. // Extract type annotations (parameter types and return type)
  1176. this.extractTypeAnnotations(node, funcNode.id);
  1177. // Push to stack and visit body
  1178. this.nodeStack.push(funcNode.id);
  1179. // Dart: function_body is a next sibling of function_signature, not a child
  1180. const body = this.language === 'dart'
  1181. ? node.nextNamedSibling?.type === 'function_body' ? node.nextNamedSibling : null
  1182. : getChildByField(node, this.extractor.bodyField);
  1183. if (body) {
  1184. this.visitFunctionBody(body, funcNode.id);
  1185. }
  1186. this.nodeStack.pop();
  1187. }
  1188. /**
  1189. * Extract a class
  1190. */
  1191. private extractClass(node: SyntaxNode): void {
  1192. if (!this.extractor) return;
  1193. const name = extractName(node, this.source, this.extractor);
  1194. const docstring = getPrecedingDocstring(node, this.source);
  1195. const visibility = this.extractor.getVisibility?.(node);
  1196. const isExported = this.extractor.isExported?.(node, this.source);
  1197. const classNode = this.createNode('class', name, node, {
  1198. docstring,
  1199. visibility,
  1200. isExported,
  1201. });
  1202. if (!classNode) return;
  1203. // Extract extends/implements
  1204. this.extractInheritance(node, classNode.id);
  1205. // Push to stack and visit body
  1206. this.nodeStack.push(classNode.id);
  1207. let body = getChildByField(node, this.extractor.bodyField);
  1208. // Dart: mixin_declaration uses class_body, extension uses extension_body
  1209. if (!body && this.language === 'dart') {
  1210. body = node.namedChildren.find((c: SyntaxNode) =>
  1211. c.type === 'class_body' || c.type === 'extension_body'
  1212. ) || null;
  1213. }
  1214. if (!body) body = node;
  1215. // Visit all children for methods and properties
  1216. for (let i = 0; i < body.namedChildCount; i++) {
  1217. const child = body.namedChild(i);
  1218. if (child) {
  1219. this.visitNode(child);
  1220. }
  1221. }
  1222. this.nodeStack.pop();
  1223. }
  1224. /**
  1225. * Extract a method
  1226. */
  1227. private extractMethod(node: SyntaxNode): void {
  1228. if (!this.extractor) return;
  1229. // For most languages, only extract as method if inside a class-like node
  1230. // But Go methods are top-level with a receiver, so always treat them as methods
  1231. if (!this.isInsideClassLikeNode() && this.language !== 'go') {
  1232. // Not inside a class-like node and not Go, treat as function
  1233. this.extractFunction(node);
  1234. return;
  1235. }
  1236. const name = extractName(node, this.source, this.extractor);
  1237. const docstring = getPrecedingDocstring(node, this.source);
  1238. const signature = this.extractor.getSignature?.(node, this.source);
  1239. const visibility = this.extractor.getVisibility?.(node);
  1240. const isAsync = this.extractor.isAsync?.(node);
  1241. const isStatic = this.extractor.isStatic?.(node);
  1242. const methodNode = this.createNode('method', name, node, {
  1243. docstring,
  1244. signature,
  1245. visibility,
  1246. isAsync,
  1247. isStatic,
  1248. });
  1249. if (!methodNode) return;
  1250. // Extract type annotations (parameter types and return type)
  1251. this.extractTypeAnnotations(node, methodNode.id);
  1252. // Push to stack and visit body
  1253. this.nodeStack.push(methodNode.id);
  1254. // Dart: function_body is a next sibling of method_signature, not a child
  1255. const body = this.language === 'dart'
  1256. ? node.nextNamedSibling?.type === 'function_body' ? node.nextNamedSibling : null
  1257. : getChildByField(node, this.extractor.bodyField);
  1258. if (body) {
  1259. this.visitFunctionBody(body, methodNode.id);
  1260. }
  1261. this.nodeStack.pop();
  1262. }
  1263. /**
  1264. * Extract an interface/protocol/trait
  1265. */
  1266. private extractInterface(node: SyntaxNode): void {
  1267. if (!this.extractor) return;
  1268. const name = extractName(node, this.source, this.extractor);
  1269. const docstring = getPrecedingDocstring(node, this.source);
  1270. const isExported = this.extractor.isExported?.(node, this.source);
  1271. // Determine kind based on language
  1272. let kind: NodeKind = 'interface';
  1273. if (this.language === 'rust') kind = 'trait';
  1274. const interfaceNode = this.createNode(kind, name, node, {
  1275. docstring,
  1276. isExported,
  1277. });
  1278. if (!interfaceNode) return;
  1279. // Extract extends (interface inheritance)
  1280. this.extractInheritance(node, interfaceNode.id);
  1281. }
  1282. /**
  1283. * Extract a struct
  1284. */
  1285. private extractStruct(node: SyntaxNode): void {
  1286. if (!this.extractor) return;
  1287. const name = extractName(node, this.source, this.extractor);
  1288. const docstring = getPrecedingDocstring(node, this.source);
  1289. const visibility = this.extractor.getVisibility?.(node);
  1290. const isExported = this.extractor.isExported?.(node, this.source);
  1291. const structNode = this.createNode('struct', name, node, {
  1292. docstring,
  1293. visibility,
  1294. isExported,
  1295. });
  1296. if (!structNode) return;
  1297. // Push to stack for field extraction
  1298. this.nodeStack.push(structNode.id);
  1299. const body = getChildByField(node, this.extractor.bodyField) || node;
  1300. for (let i = 0; i < body.namedChildCount; i++) {
  1301. const child = body.namedChild(i);
  1302. if (child) {
  1303. this.visitNode(child);
  1304. }
  1305. }
  1306. this.nodeStack.pop();
  1307. }
  1308. /**
  1309. * Extract an enum
  1310. */
  1311. private extractEnum(node: SyntaxNode): void {
  1312. if (!this.extractor) return;
  1313. const name = extractName(node, this.source, this.extractor);
  1314. const docstring = getPrecedingDocstring(node, this.source);
  1315. const visibility = this.extractor.getVisibility?.(node);
  1316. const isExported = this.extractor.isExported?.(node, this.source);
  1317. this.createNode('enum', name, node, {
  1318. docstring,
  1319. visibility,
  1320. isExported,
  1321. });
  1322. }
  1323. /**
  1324. * Extract a variable declaration (const, let, var, etc.)
  1325. *
  1326. * Extracts top-level and module-level variable declarations.
  1327. * Captures the variable name and first 100 chars of initializer in signature for searchability.
  1328. */
  1329. private extractVariable(node: SyntaxNode): void {
  1330. if (!this.extractor) return;
  1331. // Different languages have different variable declaration structures
  1332. // TypeScript/JavaScript: lexical_declaration contains variable_declarator children
  1333. // Python: assignment has left (identifier) and right (value)
  1334. // Go: var_declaration, short_var_declaration, const_declaration
  1335. const isConst = this.extractor.isConst?.(node) ?? false;
  1336. const kind: NodeKind = isConst ? 'constant' : 'variable';
  1337. const docstring = getPrecedingDocstring(node, this.source);
  1338. const isExported = this.extractor.isExported?.(node, this.source) ?? false;
  1339. // Extract variable declarators based on language
  1340. if (this.language === 'typescript' || this.language === 'javascript' ||
  1341. this.language === 'tsx' || this.language === 'jsx') {
  1342. // Handle lexical_declaration and variable_declaration
  1343. // These contain one or more variable_declarator children
  1344. for (let i = 0; i < node.namedChildCount; i++) {
  1345. const child = node.namedChild(i);
  1346. if (child?.type === 'variable_declarator') {
  1347. const nameNode = getChildByField(child, 'name');
  1348. const valueNode = getChildByField(child, 'value');
  1349. if (nameNode) {
  1350. const name = getNodeText(nameNode, this.source);
  1351. // Arrow functions / function expressions: extract as function instead of variable
  1352. if (valueNode && (valueNode.type === 'arrow_function' || valueNode.type === 'function_expression')) {
  1353. this.extractFunction(valueNode);
  1354. continue;
  1355. }
  1356. // Capture first 100 chars of initializer for context (stored in signature for searchability)
  1357. const initValue = valueNode ? getNodeText(valueNode, this.source).slice(0, 100) : undefined;
  1358. const initSignature = initValue ? `= ${initValue}${initValue.length >= 100 ? '...' : ''}` : undefined;
  1359. const varNode = this.createNode(kind, name, child, {
  1360. docstring,
  1361. signature: initSignature,
  1362. isExported,
  1363. });
  1364. // Extract type annotation references (e.g., const x: ITextModel = ...)
  1365. if (varNode) {
  1366. this.extractVariableTypeAnnotation(child, varNode.id);
  1367. }
  1368. }
  1369. }
  1370. }
  1371. } else if (this.language === 'python' || this.language === 'ruby') {
  1372. // Python/Ruby assignment: left = right
  1373. const left = getChildByField(node, 'left') || node.namedChild(0);
  1374. const right = getChildByField(node, 'right') || node.namedChild(1);
  1375. if (left && left.type === 'identifier') {
  1376. const name = getNodeText(left, this.source);
  1377. // Skip if name starts with lowercase and looks like a function call result
  1378. // Python constants are usually UPPER_CASE
  1379. const initValue = right ? getNodeText(right, this.source).slice(0, 100) : undefined;
  1380. const initSignature = initValue ? `= ${initValue}${initValue.length >= 100 ? '...' : ''}` : undefined;
  1381. this.createNode(kind, name, node, {
  1382. docstring,
  1383. signature: initSignature,
  1384. });
  1385. }
  1386. } else if (this.language === 'go') {
  1387. // Go: var_declaration, short_var_declaration, const_declaration
  1388. // These can have multiple identifiers on the left
  1389. const specs = node.namedChildren.filter(c =>
  1390. c.type === 'var_spec' || c.type === 'const_spec'
  1391. );
  1392. for (const spec of specs) {
  1393. const nameNode = spec.namedChild(0);
  1394. if (nameNode && nameNode.type === 'identifier') {
  1395. const name = getNodeText(nameNode, this.source);
  1396. const valueNode = spec.namedChildCount > 1 ? spec.namedChild(spec.namedChildCount - 1) : null;
  1397. const initValue = valueNode ? getNodeText(valueNode, this.source).slice(0, 100) : undefined;
  1398. const initSignature = initValue ? `= ${initValue}${initValue.length >= 100 ? '...' : ''}` : undefined;
  1399. this.createNode(node.type === 'const_declaration' ? 'constant' : 'variable', name, spec, {
  1400. docstring,
  1401. signature: initSignature,
  1402. });
  1403. }
  1404. }
  1405. // Handle short_var_declaration (:=)
  1406. if (node.type === 'short_var_declaration') {
  1407. const left = getChildByField(node, 'left');
  1408. const right = getChildByField(node, 'right');
  1409. if (left) {
  1410. // Can be expression_list with multiple identifiers
  1411. const identifiers = left.type === 'expression_list'
  1412. ? left.namedChildren.filter(c => c.type === 'identifier')
  1413. : [left];
  1414. for (const id of identifiers) {
  1415. const name = getNodeText(id, this.source);
  1416. const initValue = right ? getNodeText(right, this.source).slice(0, 100) : undefined;
  1417. const initSignature = initValue ? `= ${initValue}${initValue.length >= 100 ? '...' : ''}` : undefined;
  1418. this.createNode('variable', name, node, {
  1419. docstring,
  1420. signature: initSignature,
  1421. });
  1422. }
  1423. }
  1424. }
  1425. } else {
  1426. // Generic fallback for other languages
  1427. // Try to find identifier children
  1428. for (let i = 0; i < node.namedChildCount; i++) {
  1429. const child = node.namedChild(i);
  1430. if (child?.type === 'identifier' || child?.type === 'variable_declarator') {
  1431. const name = child.type === 'identifier'
  1432. ? getNodeText(child, this.source)
  1433. : extractName(child, this.source, this.extractor);
  1434. if (name && name !== '<anonymous>') {
  1435. this.createNode(kind, name, child, {
  1436. docstring,
  1437. isExported,
  1438. });
  1439. }
  1440. }
  1441. }
  1442. }
  1443. }
  1444. /**
  1445. * Extract a type alias (e.g. `export type X = ...` in TypeScript)
  1446. */
  1447. private extractTypeAlias(node: SyntaxNode): void {
  1448. if (!this.extractor) return;
  1449. const name = extractName(node, this.source, this.extractor);
  1450. if (name === '<anonymous>') return;
  1451. const docstring = getPrecedingDocstring(node, this.source);
  1452. const isExported = this.extractor.isExported?.(node, this.source);
  1453. const typeAliasNode = this.createNode('type_alias', name, node, {
  1454. docstring,
  1455. isExported,
  1456. });
  1457. // Extract type references from the alias value (e.g., `type X = ITextModel | null`)
  1458. if (typeAliasNode && this.TYPE_ANNOTATION_LANGUAGES.has(this.language)) {
  1459. // The value is everything after the `=`, which is typically the last named child
  1460. // In tree-sitter TS: type_alias_declaration has name + value children
  1461. const value = getChildByField(node, 'value');
  1462. if (value) {
  1463. this.extractTypeRefsFromSubtree(value, typeAliasNode.id);
  1464. }
  1465. }
  1466. }
  1467. /**
  1468. * Extract an exported variable declaration that isn't a function.
  1469. * Handles patterns like:
  1470. * export const X = create(...)
  1471. * export const X = { ... }
  1472. * export const X = [...]
  1473. * export const X = "value"
  1474. *
  1475. * This is called for `export_statement` nodes that contain a
  1476. * `lexical_declaration` with `variable_declarator` children whose
  1477. * values are NOT already handled by functionTypes (arrow_function,
  1478. * function_expression).
  1479. */
  1480. private extractExportedVariables(exportNode: SyntaxNode): void {
  1481. if (!this.extractor) return;
  1482. // Find the lexical_declaration or variable_declaration child
  1483. for (let i = 0; i < exportNode.namedChildCount; i++) {
  1484. const decl = exportNode.namedChild(i);
  1485. if (!decl || (decl.type !== 'lexical_declaration' && decl.type !== 'variable_declaration')) {
  1486. continue;
  1487. }
  1488. // Iterate over each variable_declarator in the declaration
  1489. for (let j = 0; j < decl.namedChildCount; j++) {
  1490. const declarator = decl.namedChild(j);
  1491. if (!declarator || declarator.type !== 'variable_declarator') continue;
  1492. const nameNode = getChildByField(declarator, 'name');
  1493. if (!nameNode) continue;
  1494. const name = getNodeText(nameNode, this.source);
  1495. // Skip if the value is a function type — those are already handled
  1496. // by extractFunction via the functionTypes dispatch
  1497. const value = getChildByField(declarator, 'value');
  1498. if (value) {
  1499. const valueType = value.type;
  1500. if (
  1501. this.extractor.functionTypes.includes(valueType)
  1502. ) {
  1503. continue; // Already handled by extractFunction
  1504. }
  1505. }
  1506. const docstring = getPrecedingDocstring(exportNode, this.source);
  1507. this.createNode('variable', name, declarator, {
  1508. docstring,
  1509. isExported: true,
  1510. });
  1511. }
  1512. }
  1513. }
  1514. /**
  1515. * Extract an import
  1516. *
  1517. * Creates an import node with the full import statement stored in signature for searchability.
  1518. * Also creates unresolved references for resolution purposes.
  1519. */
  1520. private extractImport(node: SyntaxNode): void {
  1521. const importText = getNodeText(node, this.source).trim();
  1522. // Extract module/package name based on language
  1523. let moduleName = '';
  1524. if (this.language === 'typescript' || this.language === 'javascript' ||
  1525. this.language === 'tsx' || this.language === 'jsx') {
  1526. const source = getChildByField(node, 'source');
  1527. if (source) {
  1528. moduleName = getNodeText(source, this.source).replace(/['"]/g, '');
  1529. }
  1530. // Create import node with full statement as signature for searchability
  1531. if (moduleName) {
  1532. this.createNode('import', moduleName, node, {
  1533. signature: importText,
  1534. });
  1535. }
  1536. } else if (this.language === 'python') {
  1537. // Python has two import forms:
  1538. // 1. import_statement: import os, sys
  1539. // 2. import_from_statement: from os import path
  1540. if (node.type === 'import_from_statement') {
  1541. const moduleNode = getChildByField(node, 'module_name');
  1542. if (moduleNode) {
  1543. moduleName = getNodeText(moduleNode, this.source);
  1544. }
  1545. } else {
  1546. // import_statement - may have multiple modules
  1547. // Can be dotted_name (import os) or aliased_import (import numpy as np)
  1548. for (let i = 0; i < node.namedChildCount; i++) {
  1549. const child = node.namedChild(i);
  1550. if (child?.type === 'dotted_name') {
  1551. const name = getNodeText(child, this.source);
  1552. this.createNode('import', name, node, {
  1553. signature: importText,
  1554. });
  1555. } else if (child?.type === 'aliased_import') {
  1556. // Extract the module name from inside aliased_import
  1557. const dottedName = child.namedChildren.find(c => c.type === 'dotted_name');
  1558. if (dottedName) {
  1559. const name = getNodeText(dottedName, this.source);
  1560. this.createNode('import', name, node, {
  1561. signature: importText,
  1562. });
  1563. }
  1564. }
  1565. }
  1566. // Skip creating another node below if we handled import_statement
  1567. if (node.type === 'import_statement') {
  1568. return;
  1569. }
  1570. }
  1571. if (moduleName) {
  1572. this.createNode('import', moduleName, node, {
  1573. signature: importText,
  1574. });
  1575. }
  1576. } else if (this.language === 'go') {
  1577. // Go imports can be single or grouped
  1578. // Single: import "fmt" - uses import_spec directly as child
  1579. // Grouped: import ( "fmt" \n "os" ) - uses import_spec_list containing import_spec children
  1580. // Helper function to extract path from import_spec
  1581. const extractFromSpec = (spec: SyntaxNode): void => {
  1582. const stringLiteral = spec.namedChildren.find(c => c.type === 'interpreted_string_literal');
  1583. if (stringLiteral) {
  1584. const path = getNodeText(stringLiteral, this.source).replace(/['"]/g, '');
  1585. if (path) {
  1586. this.createNode('import', path, spec, {
  1587. signature: getNodeText(spec, this.source).trim(),
  1588. });
  1589. }
  1590. }
  1591. };
  1592. // Find import_spec_list for grouped imports
  1593. const importSpecList = node.namedChildren.find(c => c.type === 'import_spec_list');
  1594. if (importSpecList) {
  1595. // Grouped imports - iterate through import_spec children
  1596. const importSpecs = importSpecList.namedChildren.filter(c => c.type === 'import_spec');
  1597. for (const spec of importSpecs) {
  1598. extractFromSpec(spec);
  1599. }
  1600. } else {
  1601. // Single import: import "fmt" - import_spec is direct child
  1602. const importSpec = node.namedChildren.find(c => c.type === 'import_spec');
  1603. if (importSpec) {
  1604. extractFromSpec(importSpec);
  1605. }
  1606. }
  1607. return; // Go handled completely above
  1608. } else if (this.language === 'rust') {
  1609. // Rust use declarations
  1610. // use std::{ffi::OsStr, io}; -> scoped_use_list with identifier "std"
  1611. // use crate::error::Error; -> scoped_identifier starting with "crate"
  1612. // use super::utils; -> scoped_identifier starting with "super"
  1613. // Helper to get the root crate/module from a scoped path
  1614. const getRootModule = (scopedNode: SyntaxNode): string => {
  1615. // Recursively find the leftmost identifier/crate/super/self
  1616. const firstChild = scopedNode.namedChild(0);
  1617. if (!firstChild) return getNodeText(scopedNode, this.source);
  1618. if (firstChild.type === 'identifier' ||
  1619. firstChild.type === 'crate' ||
  1620. firstChild.type === 'super' ||
  1621. firstChild.type === 'self') {
  1622. return getNodeText(firstChild, this.source);
  1623. } else if (firstChild.type === 'scoped_identifier') {
  1624. return getRootModule(firstChild);
  1625. }
  1626. return getNodeText(firstChild, this.source);
  1627. };
  1628. // Find the use argument (scoped_use_list or scoped_identifier)
  1629. const useArg = node.namedChildren.find(c =>
  1630. c.type === 'scoped_use_list' ||
  1631. c.type === 'scoped_identifier' ||
  1632. c.type === 'use_list' ||
  1633. c.type === 'identifier'
  1634. );
  1635. if (useArg) {
  1636. moduleName = getRootModule(useArg);
  1637. this.createNode('import', moduleName, node, {
  1638. signature: importText,
  1639. });
  1640. }
  1641. return; // Rust handled completely above
  1642. } else if (this.language === 'swift') {
  1643. // Swift imports: import Foundation, @testable import Alamofire
  1644. // AST structure: import_declaration -> identifier -> simple_identifier
  1645. const identifier = node.namedChildren.find(c => c.type === 'identifier');
  1646. if (identifier) {
  1647. moduleName = getNodeText(identifier, this.source);
  1648. this.createNode('import', moduleName, node, {
  1649. signature: importText,
  1650. });
  1651. }
  1652. return; // Swift handled completely above
  1653. } else if (this.language === 'kotlin') {
  1654. // Kotlin imports: import java.io.IOException, import x.y.Z as Alias, import x.y.*
  1655. // AST structure: import_header -> identifier (dotted path)
  1656. const identifier = node.namedChildren.find(c => c.type === 'identifier');
  1657. if (identifier) {
  1658. moduleName = getNodeText(identifier, this.source);
  1659. this.createNode('import', moduleName, node, {
  1660. signature: importText,
  1661. });
  1662. }
  1663. return; // Kotlin handled completely above
  1664. } else if (this.language === 'java') {
  1665. // Java imports: import java.util.List, import static x.Y.method, import x.y.*
  1666. // AST structure: import_declaration -> scoped_identifier (dotted path)
  1667. const scopedId = node.namedChildren.find(c => c.type === 'scoped_identifier');
  1668. if (scopedId) {
  1669. moduleName = getNodeText(scopedId, this.source);
  1670. this.createNode('import', moduleName, node, {
  1671. signature: importText,
  1672. });
  1673. }
  1674. // Create unresolved reference for import resolution before returning
  1675. if (moduleName && this.nodeStack.length > 0) {
  1676. const parentId = this.nodeStack[this.nodeStack.length - 1];
  1677. if (parentId) {
  1678. this.unresolvedReferences.push({
  1679. fromNodeId: parentId,
  1680. referenceName: moduleName,
  1681. referenceKind: 'imports',
  1682. line: node.startPosition.row + 1,
  1683. column: node.startPosition.column,
  1684. });
  1685. }
  1686. }
  1687. return; // Java handled completely above
  1688. } else if (this.language === 'csharp') {
  1689. // C# using directives: using System, using System.Collections.Generic, using static X, using Alias = X
  1690. // AST structure: using_directive -> qualified_name (dotted) or identifier (simple)
  1691. // For alias imports: identifier = qualified_name - we want the qualified_name
  1692. const qualifiedName = node.namedChildren.find(c => c.type === 'qualified_name');
  1693. if (qualifiedName) {
  1694. moduleName = getNodeText(qualifiedName, this.source);
  1695. } else {
  1696. // Simple namespace like "using System;" - get the first identifier
  1697. const identifier = node.namedChildren.find(c => c.type === 'identifier');
  1698. if (identifier) {
  1699. moduleName = getNodeText(identifier, this.source);
  1700. }
  1701. }
  1702. if (moduleName) {
  1703. this.createNode('import', moduleName, node, {
  1704. signature: importText,
  1705. });
  1706. }
  1707. return; // C# handled completely above
  1708. } else if (this.language === 'php') {
  1709. // PHP use declarations: use X\Y\Z, use X as Y, use function X\func, use X\{A, B}
  1710. // AST structure: namespace_use_declaration -> namespace_use_clause -> qualified_name or name
  1711. // Check for grouped imports first: use X\{A, B}
  1712. const namespacePrefix = node.namedChildren.find(c => c.type === 'namespace_name');
  1713. const useGroup = node.namedChildren.find(c => c.type === 'namespace_use_group');
  1714. if (namespacePrefix && useGroup) {
  1715. // Grouped import - create one import per item
  1716. const prefix = getNodeText(namespacePrefix, this.source);
  1717. const useClauses = useGroup.namedChildren.filter((c: SyntaxNode) =>
  1718. c.type === 'namespace_use_group_clause' || c.type === 'namespace_use_clause'
  1719. );
  1720. for (const clause of useClauses) {
  1721. // WASM grammar wraps names in namespace_name; native uses name directly
  1722. const nsName = clause.namedChildren.find((c: SyntaxNode) => c.type === 'namespace_name');
  1723. const name = nsName
  1724. ? nsName.namedChildren.find((c: SyntaxNode) => c.type === 'name')
  1725. : clause.namedChildren.find((c: SyntaxNode) => c.type === 'name');
  1726. if (name) {
  1727. const fullPath = `${prefix}\\${getNodeText(name, this.source)}`;
  1728. this.createNode('import', fullPath, node, {
  1729. signature: importText,
  1730. });
  1731. }
  1732. }
  1733. return;
  1734. }
  1735. // Single import - find namespace_use_clause
  1736. const useClause = node.namedChildren.find(c => c.type === 'namespace_use_clause');
  1737. if (useClause) {
  1738. // Look for qualified_name (full path) or name (simple)
  1739. const qualifiedName = useClause.namedChildren.find((c: SyntaxNode) => c.type === 'qualified_name');
  1740. if (qualifiedName) {
  1741. moduleName = getNodeText(qualifiedName, this.source);
  1742. } else {
  1743. const name = useClause.namedChildren.find((c: SyntaxNode) => c.type === 'name');
  1744. if (name) {
  1745. moduleName = getNodeText(name, this.source);
  1746. }
  1747. }
  1748. }
  1749. if (moduleName) {
  1750. this.createNode('import', moduleName, node, {
  1751. signature: importText,
  1752. });
  1753. }
  1754. return; // PHP handled completely above
  1755. } else if (this.language === 'ruby') {
  1756. // Ruby imports: require 'json', require_relative '../helper'
  1757. // AST structure: call -> identifier (require/require_relative) + argument_list -> string -> string_content
  1758. // Check if this is a require/require_relative call
  1759. const identifier = node.namedChildren.find(c => c.type === 'identifier');
  1760. if (!identifier) return;
  1761. const methodName = getNodeText(identifier, this.source);
  1762. if (methodName !== 'require' && methodName !== 'require_relative') {
  1763. return; // Not an import, skip
  1764. }
  1765. // Find the argument (string)
  1766. const argList = node.namedChildren.find(c => c.type === 'argument_list');
  1767. if (argList) {
  1768. const stringNode = argList.namedChildren.find((c: SyntaxNode) => c.type === 'string');
  1769. if (stringNode) {
  1770. // Get string_content (without quotes)
  1771. const stringContent = stringNode.namedChildren.find((c: SyntaxNode) => c.type === 'string_content');
  1772. if (stringContent) {
  1773. moduleName = getNodeText(stringContent, this.source);
  1774. }
  1775. }
  1776. }
  1777. if (moduleName) {
  1778. this.createNode('import', moduleName, node, {
  1779. signature: importText,
  1780. });
  1781. }
  1782. return; // Ruby handled completely above
  1783. } else if (this.language === 'dart') {
  1784. // Dart imports: import 'dart:async'; import 'package:foo/bar.dart' as bar;
  1785. // AST: import_or_export -> library_import -> import_specification -> configurable_uri -> uri -> string_literal
  1786. const libraryImport = node.namedChildren.find(c => c.type === 'library_import');
  1787. if (libraryImport) {
  1788. const importSpec = libraryImport.namedChildren.find((c: SyntaxNode) => c.type === 'import_specification');
  1789. if (importSpec) {
  1790. const configurableUri = importSpec.namedChildren.find((c: SyntaxNode) => c.type === 'configurable_uri');
  1791. if (configurableUri) {
  1792. const uri = configurableUri.namedChildren.find((c: SyntaxNode) => c.type === 'uri');
  1793. if (uri) {
  1794. const stringLiteral = uri.namedChildren.find((c: SyntaxNode) => c.type === 'string_literal');
  1795. if (stringLiteral) {
  1796. moduleName = getNodeText(stringLiteral, this.source).replace(/['"]/g, '');
  1797. }
  1798. }
  1799. }
  1800. }
  1801. }
  1802. // Also handle exports: export 'src/foo.dart';
  1803. const libraryExport = node.namedChildren.find(c => c.type === 'library_export');
  1804. if (libraryExport) {
  1805. const configurableUri = libraryExport.namedChildren.find((c: SyntaxNode) => c.type === 'configurable_uri');
  1806. if (configurableUri) {
  1807. const uri = configurableUri.namedChildren.find((c: SyntaxNode) => c.type === 'uri');
  1808. if (uri) {
  1809. const stringLiteral = uri.namedChildren.find((c: SyntaxNode) => c.type === 'string_literal');
  1810. if (stringLiteral) {
  1811. moduleName = getNodeText(stringLiteral, this.source).replace(/['"]/g, '');
  1812. }
  1813. }
  1814. }
  1815. }
  1816. if (moduleName) {
  1817. this.createNode('import', moduleName, node, {
  1818. signature: importText,
  1819. });
  1820. }
  1821. return; // Dart handled completely above
  1822. } else if (this.language === 'c' || this.language === 'cpp') {
  1823. // C/C++ includes: #include <iostream>, #include "myheader.h"
  1824. // AST: preproc_include -> system_lib_string (<...>) or string_literal ("...")
  1825. // Check for system include: <path>
  1826. const systemLib = node.namedChildren.find(c => c.type === 'system_lib_string');
  1827. if (systemLib) {
  1828. // Remove angle brackets: <iostream> -> iostream
  1829. moduleName = getNodeText(systemLib, this.source).replace(/^<|>$/g, '');
  1830. } else {
  1831. // Check for local include: "path"
  1832. const stringLiteral = node.namedChildren.find(c => c.type === 'string_literal');
  1833. if (stringLiteral) {
  1834. const stringContent = stringLiteral.namedChildren.find((c: SyntaxNode) => c.type === 'string_content');
  1835. if (stringContent) {
  1836. moduleName = getNodeText(stringContent, this.source);
  1837. }
  1838. }
  1839. }
  1840. if (moduleName) {
  1841. this.createNode('import', moduleName, node, {
  1842. signature: importText,
  1843. });
  1844. }
  1845. return; // C/C++ handled completely above
  1846. } else {
  1847. // Generic extraction for other languages
  1848. moduleName = importText;
  1849. if (moduleName) {
  1850. this.createNode('import', moduleName, node, {
  1851. signature: importText,
  1852. });
  1853. }
  1854. }
  1855. // Keep unresolved reference creation for resolution purposes
  1856. // This is used to resolve imports to their target files/modules
  1857. if (moduleName && this.nodeStack.length > 0) {
  1858. const parentId = this.nodeStack[this.nodeStack.length - 1];
  1859. if (parentId) {
  1860. this.unresolvedReferences.push({
  1861. fromNodeId: parentId,
  1862. referenceName: moduleName,
  1863. referenceKind: 'imports',
  1864. line: node.startPosition.row + 1,
  1865. column: node.startPosition.column,
  1866. });
  1867. }
  1868. }
  1869. }
  1870. /**
  1871. * Extract a function call
  1872. */
  1873. private extractCall(node: SyntaxNode): void {
  1874. if (this.nodeStack.length === 0) return;
  1875. const callerId = this.nodeStack[this.nodeStack.length - 1];
  1876. if (!callerId) return;
  1877. // Get the function/method being called
  1878. let calleeName = '';
  1879. // Java/Kotlin method_invocation has 'object' + 'name' fields instead of 'function'
  1880. const nameField = getChildByField(node, 'name');
  1881. const objectField = getChildByField(node, 'object');
  1882. if (nameField && objectField && node.type === 'method_invocation') {
  1883. // Java-style method call: receiver.method()
  1884. const methodName = getNodeText(nameField, this.source);
  1885. const receiverName = getNodeText(objectField, this.source);
  1886. if (methodName) {
  1887. // Emit receiver.method form for qualified resolution
  1888. calleeName = `${receiverName}.${methodName}`;
  1889. }
  1890. } else {
  1891. const func = getChildByField(node, 'function') || node.namedChild(0);
  1892. if (func) {
  1893. if (func.type === 'member_expression' || func.type === 'attribute') {
  1894. // Method call: obj.method()
  1895. const property = getChildByField(func, 'property') || func.namedChild(1);
  1896. if (property) {
  1897. calleeName = getNodeText(property, this.source);
  1898. }
  1899. } else if (func.type === 'scoped_identifier' || func.type === 'scoped_call_expression') {
  1900. // Scoped call: Module::function()
  1901. calleeName = getNodeText(func, this.source);
  1902. } else {
  1903. calleeName = getNodeText(func, this.source);
  1904. }
  1905. }
  1906. }
  1907. if (calleeName) {
  1908. this.unresolvedReferences.push({
  1909. fromNodeId: callerId,
  1910. referenceName: calleeName,
  1911. referenceKind: 'calls',
  1912. line: node.startPosition.row + 1,
  1913. column: node.startPosition.column,
  1914. });
  1915. }
  1916. }
  1917. /**
  1918. * Visit function body and extract calls
  1919. */
  1920. private visitFunctionBody(body: SyntaxNode, _functionId: string): void {
  1921. if (!this.extractor) return;
  1922. // Recursively find all call expressions
  1923. const visitForCalls = (node: SyntaxNode): void => {
  1924. if (this.extractor!.callTypes.includes(node.type)) {
  1925. this.extractCall(node);
  1926. }
  1927. for (let i = 0; i < node.namedChildCount; i++) {
  1928. const child = node.namedChild(i);
  1929. if (child) {
  1930. visitForCalls(child);
  1931. }
  1932. }
  1933. };
  1934. visitForCalls(body);
  1935. }
  1936. /**
  1937. * Extract inheritance relationships
  1938. */
  1939. private extractInheritance(node: SyntaxNode, classId: string): void {
  1940. // Look for extends/implements clauses
  1941. for (let i = 0; i < node.namedChildCount; i++) {
  1942. const child = node.namedChild(i);
  1943. if (!child) continue;
  1944. if (
  1945. child.type === 'extends_clause' ||
  1946. child.type === 'class_heritage' ||
  1947. child.type === 'superclass' ||
  1948. child.type === 'extends_interfaces' // Java interface extends
  1949. ) {
  1950. // Extract parent class/interface names
  1951. // Java uses type_list wrapper: superclass -> type_identifier, extends_interfaces -> type_list -> type_identifier
  1952. const typeList = child.namedChildren.find((c: SyntaxNode) => c.type === 'type_list');
  1953. const targets = typeList ? typeList.namedChildren : [child.namedChild(0)];
  1954. for (const target of targets) {
  1955. if (target) {
  1956. const name = getNodeText(target, this.source);
  1957. this.unresolvedReferences.push({
  1958. fromNodeId: classId,
  1959. referenceName: name,
  1960. referenceKind: 'extends',
  1961. line: target.startPosition.row + 1,
  1962. column: target.startPosition.column,
  1963. });
  1964. }
  1965. }
  1966. }
  1967. if (
  1968. child.type === 'implements_clause' ||
  1969. child.type === 'class_interface_clause' ||
  1970. child.type === 'super_interfaces' || // Java class implements
  1971. child.type === 'interfaces' // Dart
  1972. ) {
  1973. // Extract implemented interfaces
  1974. // Java uses type_list wrapper: super_interfaces -> type_list -> type_identifier
  1975. const typeList = child.namedChildren.find((c: SyntaxNode) => c.type === 'type_list');
  1976. const targets = typeList ? typeList.namedChildren : child.namedChildren;
  1977. for (const iface of targets) {
  1978. if (iface) {
  1979. const name = getNodeText(iface, this.source);
  1980. this.unresolvedReferences.push({
  1981. fromNodeId: classId,
  1982. referenceName: name,
  1983. referenceKind: 'implements',
  1984. line: iface.startPosition.row + 1,
  1985. column: iface.startPosition.column,
  1986. });
  1987. }
  1988. }
  1989. }
  1990. }
  1991. }
  1992. /**
  1993. * Languages that support type annotations (TypeScript, etc.)
  1994. */
  1995. private readonly TYPE_ANNOTATION_LANGUAGES = new Set([
  1996. 'typescript', 'tsx', 'dart', 'kotlin', 'swift', 'rust', 'go', 'java', 'csharp',
  1997. ]);
  1998. /**
  1999. * Built-in/primitive type names that shouldn't create references
  2000. */
  2001. private readonly BUILTIN_TYPES = new Set([
  2002. 'string', 'number', 'boolean', 'void', 'null', 'undefined', 'never', 'any', 'unknown',
  2003. 'object', 'symbol', 'bigint', 'true', 'false',
  2004. // Rust
  2005. 'str', 'bool', 'i8', 'i16', 'i32', 'i64', 'i128', 'isize',
  2006. 'u8', 'u16', 'u32', 'u64', 'u128', 'usize', 'f32', 'f64', 'char',
  2007. // Java/C#
  2008. 'int', 'long', 'short', 'byte', 'float', 'double', 'char',
  2009. // Go
  2010. 'int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64',
  2011. 'float32', 'float64', 'complex64', 'complex128', 'rune', 'error',
  2012. ]);
  2013. /**
  2014. * Extract type references from type annotations on a function/method/field node.
  2015. * Creates 'references' edges for parameter types, return types, and field types.
  2016. */
  2017. private extractTypeAnnotations(node: SyntaxNode, nodeId: string): void {
  2018. if (!this.extractor) return;
  2019. if (!this.TYPE_ANNOTATION_LANGUAGES.has(this.language)) return;
  2020. // Extract parameter type annotations
  2021. const params = getChildByField(node, this.extractor.paramsField || 'parameters');
  2022. if (params) {
  2023. this.extractTypeRefsFromSubtree(params, nodeId);
  2024. }
  2025. // Extract return type annotation
  2026. const returnType = getChildByField(node, this.extractor.returnField || 'return_type');
  2027. if (returnType) {
  2028. this.extractTypeRefsFromSubtree(returnType, nodeId);
  2029. }
  2030. // Extract direct type annotation (for class fields like `model: ITextModel`)
  2031. const typeAnnotation = node.namedChildren.find(
  2032. (c: SyntaxNode) => c.type === 'type_annotation'
  2033. );
  2034. if (typeAnnotation) {
  2035. this.extractTypeRefsFromSubtree(typeAnnotation, nodeId);
  2036. }
  2037. }
  2038. /**
  2039. * Extract type references from a variable's type annotation.
  2040. */
  2041. private extractVariableTypeAnnotation(node: SyntaxNode, nodeId: string): void {
  2042. if (!this.TYPE_ANNOTATION_LANGUAGES.has(this.language)) return;
  2043. // Find type_annotation child (covers TS `: Type`, Rust `: Type`, etc.)
  2044. const typeAnnotation = node.namedChildren.find(
  2045. (c: SyntaxNode) => c.type === 'type_annotation'
  2046. );
  2047. if (typeAnnotation) {
  2048. this.extractTypeRefsFromSubtree(typeAnnotation, nodeId);
  2049. }
  2050. }
  2051. /**
  2052. * Recursively walk a subtree and extract all type_identifier references.
  2053. * Handles unions, intersections, generics, arrays, etc.
  2054. */
  2055. private extractTypeRefsFromSubtree(node: SyntaxNode, fromNodeId: string): void {
  2056. if (node.type === 'type_identifier') {
  2057. const typeName = getNodeText(node, this.source);
  2058. if (typeName && !this.BUILTIN_TYPES.has(typeName)) {
  2059. this.unresolvedReferences.push({
  2060. fromNodeId,
  2061. referenceName: typeName,
  2062. referenceKind: 'references',
  2063. line: node.startPosition.row + 1,
  2064. column: node.startPosition.column,
  2065. });
  2066. }
  2067. return; // type_identifier is a leaf
  2068. }
  2069. // Recurse into children (handles union_type, intersection_type, generic_type, etc.)
  2070. for (let i = 0; i < node.namedChildCount; i++) {
  2071. const child = node.namedChild(i);
  2072. if (child) {
  2073. this.extractTypeRefsFromSubtree(child, fromNodeId);
  2074. }
  2075. }
  2076. }
  2077. /**
  2078. * Handle Pascal-specific AST structures.
  2079. * Returns true if the node was fully handled and children should be skipped.
  2080. */
  2081. private visitPascalNode(node: SyntaxNode): boolean {
  2082. const nodeType = node.type;
  2083. // Unit/Program/Library → module node
  2084. if (nodeType === 'unit' || nodeType === 'program' || nodeType === 'library') {
  2085. const moduleNameNode = node.namedChildren.find(
  2086. (c: SyntaxNode) => c.type === 'moduleName'
  2087. );
  2088. const name = moduleNameNode ? getNodeText(moduleNameNode, this.source) : '';
  2089. // Fallback to filename without extension if module name is empty
  2090. const moduleName = name || path.basename(this.filePath).replace(/\.[^.]+$/, '');
  2091. this.createNode('module', moduleName, node);
  2092. // Continue visiting children (interface/implementation sections)
  2093. for (let i = 0; i < node.namedChildCount; i++) {
  2094. const child = node.namedChild(i);
  2095. if (child) this.visitNode(child);
  2096. }
  2097. return true;
  2098. }
  2099. // declType wraps declClass/declIntf/declEnum/type-alias
  2100. // The name lives on declType, the inner node determines the kind
  2101. if (nodeType === 'declType') {
  2102. this.extractPascalDeclType(node);
  2103. return true;
  2104. }
  2105. // declUses → import nodes for each unit name
  2106. if (nodeType === 'declUses') {
  2107. this.extractPascalUses(node);
  2108. return true;
  2109. }
  2110. // declConsts → container; visit children for individual declConst
  2111. if (nodeType === 'declConsts') {
  2112. for (let i = 0; i < node.namedChildCount; i++) {
  2113. const child = node.namedChild(i);
  2114. if (child?.type === 'declConst') {
  2115. this.extractPascalConst(child);
  2116. }
  2117. }
  2118. return true;
  2119. }
  2120. // declConst at top level (outside declConsts)
  2121. if (nodeType === 'declConst') {
  2122. this.extractPascalConst(node);
  2123. return true;
  2124. }
  2125. // declTypes → container for type declarations
  2126. if (nodeType === 'declTypes') {
  2127. for (let i = 0; i < node.namedChildCount; i++) {
  2128. const child = node.namedChild(i);
  2129. if (child) this.visitNode(child);
  2130. }
  2131. return true;
  2132. }
  2133. // declVars → container for variable declarations
  2134. if (nodeType === 'declVars') {
  2135. for (let i = 0; i < node.namedChildCount; i++) {
  2136. const child = node.namedChild(i);
  2137. if (child?.type === 'declVar') {
  2138. const nameNode = getChildByField(child, 'name');
  2139. if (nameNode) {
  2140. const name = getNodeText(nameNode, this.source);
  2141. this.createNode('variable', name, child);
  2142. }
  2143. }
  2144. }
  2145. return true;
  2146. }
  2147. // defProc in implementation section → extract calls but don't create duplicate nodes
  2148. if (nodeType === 'defProc') {
  2149. this.extractPascalDefProc(node);
  2150. return true;
  2151. }
  2152. // declProp → property node
  2153. if (nodeType === 'declProp') {
  2154. const nameNode = getChildByField(node, 'name');
  2155. if (nameNode) {
  2156. const name = getNodeText(nameNode, this.source);
  2157. const visibility = this.extractor!.getVisibility?.(node);
  2158. this.createNode('property', name, node, { visibility });
  2159. }
  2160. return true;
  2161. }
  2162. // declField → field node
  2163. if (nodeType === 'declField') {
  2164. const nameNode = getChildByField(node, 'name');
  2165. if (nameNode) {
  2166. const name = getNodeText(nameNode, this.source);
  2167. const visibility = this.extractor!.getVisibility?.(node);
  2168. this.createNode('field', name, node, { visibility });
  2169. }
  2170. return true;
  2171. }
  2172. // declSection → visit children (propagates visibility via getVisibility)
  2173. if (nodeType === 'declSection') {
  2174. for (let i = 0; i < node.namedChildCount; i++) {
  2175. const child = node.namedChild(i);
  2176. if (child) this.visitNode(child);
  2177. }
  2178. return true;
  2179. }
  2180. // exprCall → extract function call reference
  2181. if (nodeType === 'exprCall') {
  2182. this.extractPascalCall(node);
  2183. return true;
  2184. }
  2185. // interface/implementation sections → visit children
  2186. if (nodeType === 'interface' || nodeType === 'implementation') {
  2187. for (let i = 0; i < node.namedChildCount; i++) {
  2188. const child = node.namedChild(i);
  2189. if (child) this.visitNode(child);
  2190. }
  2191. return true;
  2192. }
  2193. // block (begin..end) → visit for calls
  2194. if (nodeType === 'block') {
  2195. this.visitPascalBlock(node);
  2196. return true;
  2197. }
  2198. return false;
  2199. }
  2200. /**
  2201. * Extract a Pascal declType node (class, interface, enum, or type alias)
  2202. */
  2203. private extractPascalDeclType(node: SyntaxNode): void {
  2204. const nameNode = getChildByField(node, 'name');
  2205. if (!nameNode) return;
  2206. const name = getNodeText(nameNode, this.source);
  2207. // Find the inner type declaration
  2208. const declClass = node.namedChildren.find(
  2209. (c: SyntaxNode) => c.type === 'declClass'
  2210. );
  2211. const declIntf = node.namedChildren.find(
  2212. (c: SyntaxNode) => c.type === 'declIntf'
  2213. );
  2214. const typeChild = node.namedChildren.find(
  2215. (c: SyntaxNode) => c.type === 'type'
  2216. );
  2217. if (declClass) {
  2218. const classNode = this.createNode('class', name, node);
  2219. if (classNode) {
  2220. // Extract inheritance from typeref children of declClass
  2221. this.extractPascalInheritance(declClass, classNode.id);
  2222. // Visit class body
  2223. this.nodeStack.push(classNode.id);
  2224. for (let i = 0; i < declClass.namedChildCount; i++) {
  2225. const child = declClass.namedChild(i);
  2226. if (child) this.visitNode(child);
  2227. }
  2228. this.nodeStack.pop();
  2229. }
  2230. } else if (declIntf) {
  2231. const ifaceNode = this.createNode('interface', name, node);
  2232. if (ifaceNode) {
  2233. // Visit interface members
  2234. this.nodeStack.push(ifaceNode.id);
  2235. for (let i = 0; i < declIntf.namedChildCount; i++) {
  2236. const child = declIntf.namedChild(i);
  2237. if (child) this.visitNode(child);
  2238. }
  2239. this.nodeStack.pop();
  2240. }
  2241. } else if (typeChild) {
  2242. // Check if it contains a declEnum
  2243. const declEnum = typeChild.namedChildren.find(
  2244. (c: SyntaxNode) => c.type === 'declEnum'
  2245. );
  2246. if (declEnum) {
  2247. const enumNode = this.createNode('enum', name, node);
  2248. if (enumNode) {
  2249. // Extract enum members
  2250. this.nodeStack.push(enumNode.id);
  2251. for (let i = 0; i < declEnum.namedChildCount; i++) {
  2252. const child = declEnum.namedChild(i);
  2253. if (child?.type === 'declEnumValue') {
  2254. const memberName = getChildByField(child, 'name');
  2255. if (memberName) {
  2256. this.createNode('enum_member', getNodeText(memberName, this.source), child);
  2257. }
  2258. }
  2259. }
  2260. this.nodeStack.pop();
  2261. }
  2262. } else {
  2263. // Simple type alias: type TFoo = string / type TFoo = Integer
  2264. this.createNode('type_alias', name, node);
  2265. }
  2266. } else {
  2267. // Fallback: could be a forward declaration or simple alias
  2268. this.createNode('type_alias', name, node);
  2269. }
  2270. }
  2271. /**
  2272. * Extract Pascal uses clause into individual import nodes
  2273. */
  2274. private extractPascalUses(node: SyntaxNode): void {
  2275. const importText = getNodeText(node, this.source).trim();
  2276. for (let i = 0; i < node.namedChildCount; i++) {
  2277. const child = node.namedChild(i);
  2278. if (child?.type === 'moduleName') {
  2279. const unitName = getNodeText(child, this.source);
  2280. this.createNode('import', unitName, child, {
  2281. signature: importText,
  2282. });
  2283. // Create unresolved reference for resolution
  2284. if (this.nodeStack.length > 0) {
  2285. const parentId = this.nodeStack[this.nodeStack.length - 1];
  2286. if (parentId) {
  2287. this.unresolvedReferences.push({
  2288. fromNodeId: parentId,
  2289. referenceName: unitName,
  2290. referenceKind: 'imports',
  2291. line: child.startPosition.row + 1,
  2292. column: child.startPosition.column,
  2293. });
  2294. }
  2295. }
  2296. }
  2297. }
  2298. }
  2299. /**
  2300. * Extract a Pascal constant declaration
  2301. */
  2302. private extractPascalConst(node: SyntaxNode): void {
  2303. const nameNode = getChildByField(node, 'name');
  2304. if (!nameNode) return;
  2305. const name = getNodeText(nameNode, this.source);
  2306. const defaultValue = node.namedChildren.find(
  2307. (c: SyntaxNode) => c.type === 'defaultValue'
  2308. );
  2309. const sig = defaultValue ? getNodeText(defaultValue, this.source) : undefined;
  2310. this.createNode('constant', name, node, { signature: sig });
  2311. }
  2312. /**
  2313. * Extract Pascal inheritance (extends/implements) from declClass typeref children
  2314. */
  2315. private extractPascalInheritance(declClass: SyntaxNode, classId: string): void {
  2316. const typerefs = declClass.namedChildren.filter(
  2317. (c: SyntaxNode) => c.type === 'typeref'
  2318. );
  2319. for (let i = 0; i < typerefs.length; i++) {
  2320. const ref = typerefs[i]!;
  2321. const name = getNodeText(ref, this.source);
  2322. this.unresolvedReferences.push({
  2323. fromNodeId: classId,
  2324. referenceName: name,
  2325. referenceKind: i === 0 ? 'extends' : 'implements',
  2326. line: ref.startPosition.row + 1,
  2327. column: ref.startPosition.column,
  2328. });
  2329. }
  2330. }
  2331. /**
  2332. * Extract calls and resolve method context from a Pascal defProc (implementation body).
  2333. * Does not create a new node — the declaration was already captured from the interface section.
  2334. */
  2335. private extractPascalDefProc(node: SyntaxNode): void {
  2336. // Find the matching declaration node by name to use as call parent
  2337. const declProc = node.namedChildren.find(
  2338. (c: SyntaxNode) => c.type === 'declProc'
  2339. );
  2340. if (!declProc) return;
  2341. const nameNode = getChildByField(declProc, 'name');
  2342. if (!nameNode) return;
  2343. const fullName = getNodeText(nameNode, this.source).trim();
  2344. // fullName is like "TAuthService.Create"
  2345. const shortName = fullName.includes('.') ? fullName.split('.').pop()! : fullName;
  2346. const fullNameKey = fullName.toLowerCase();
  2347. const shortNameKey = shortName.toLowerCase();
  2348. // Build method index on first use (O(n) once, then O(1) per lookup)
  2349. if (!this.methodIndex) {
  2350. this.methodIndex = new Map();
  2351. for (const n of this.nodes) {
  2352. if (n.kind === 'method' || n.kind === 'function') {
  2353. const nameKey = n.name.toLowerCase();
  2354. // Keep first seen short-name mapping to avoid silently overwriting earlier entries.
  2355. if (!this.methodIndex.has(nameKey)) {
  2356. this.methodIndex.set(nameKey, n.id);
  2357. }
  2358. // For Pascal methods, also index qualified forms (e.g. TAuthService.Create).
  2359. if (n.kind === 'method') {
  2360. const qualifiedParts = n.qualifiedName.split('::').slice(1); // drop file path
  2361. if (qualifiedParts.length >= 2) {
  2362. // Create suffix keys so both "Module.Class.Method" and "Class.Method" can resolve.
  2363. for (let i = 0; i < qualifiedParts.length - 1; i++) {
  2364. const scopedName = qualifiedParts.slice(i).join('.').toLowerCase();
  2365. this.methodIndex.set(scopedName, n.id);
  2366. }
  2367. }
  2368. }
  2369. }
  2370. }
  2371. }
  2372. const parentId =
  2373. this.methodIndex.get(fullNameKey) ||
  2374. this.methodIndex.get(shortNameKey) ||
  2375. this.nodeStack[this.nodeStack.length - 1];
  2376. if (!parentId) return;
  2377. // Visit the block for calls
  2378. const block = node.namedChildren.find(
  2379. (c: SyntaxNode) => c.type === 'block'
  2380. );
  2381. if (block) {
  2382. this.nodeStack.push(parentId);
  2383. this.visitPascalBlock(block);
  2384. this.nodeStack.pop();
  2385. }
  2386. }
  2387. /**
  2388. * Extract function calls from a Pascal expression
  2389. */
  2390. private extractPascalCall(node: SyntaxNode): void {
  2391. if (this.nodeStack.length === 0) return;
  2392. const callerId = this.nodeStack[this.nodeStack.length - 1];
  2393. if (!callerId) return;
  2394. // Get the callee name — first child is typically the identifier or exprDot
  2395. const firstChild = node.namedChild(0);
  2396. if (!firstChild) return;
  2397. let calleeName = '';
  2398. if (firstChild.type === 'exprDot') {
  2399. // Qualified call: Obj.Method(...)
  2400. const identifiers = firstChild.namedChildren.filter(
  2401. (c: SyntaxNode) => c.type === 'identifier'
  2402. );
  2403. if (identifiers.length > 0) {
  2404. calleeName = identifiers.map((id: SyntaxNode) => getNodeText(id, this.source)).join('.');
  2405. }
  2406. } else if (firstChild.type === 'identifier') {
  2407. calleeName = getNodeText(firstChild, this.source);
  2408. }
  2409. if (calleeName) {
  2410. this.unresolvedReferences.push({
  2411. fromNodeId: callerId,
  2412. referenceName: calleeName,
  2413. referenceKind: 'calls',
  2414. line: node.startPosition.row + 1,
  2415. column: node.startPosition.column,
  2416. });
  2417. }
  2418. // Also visit arguments for nested calls
  2419. const args = node.namedChildren.find(
  2420. (c: SyntaxNode) => c.type === 'exprArgs'
  2421. );
  2422. if (args) {
  2423. this.visitPascalBlock(args);
  2424. }
  2425. }
  2426. /**
  2427. * Recursively visit a Pascal block/statement tree for call expressions
  2428. */
  2429. private visitPascalBlock(node: SyntaxNode): void {
  2430. for (let i = 0; i < node.namedChildCount; i++) {
  2431. const child = node.namedChild(i);
  2432. if (!child) continue;
  2433. if (child.type === 'exprCall') {
  2434. this.extractPascalCall(child);
  2435. } else if (child.type === 'exprDot') {
  2436. // Check if exprDot contains an exprCall
  2437. for (let j = 0; j < child.namedChildCount; j++) {
  2438. const grandchild = child.namedChild(j);
  2439. if (grandchild?.type === 'exprCall') {
  2440. this.extractPascalCall(grandchild);
  2441. }
  2442. }
  2443. } else {
  2444. this.visitPascalBlock(child);
  2445. }
  2446. }
  2447. }
  2448. }
  2449. /**
  2450. * LiquidExtractor - Extracts relationships from Liquid template files
  2451. *
  2452. * Liquid is a templating language (used by Shopify, Jekyll, etc.) that doesn't
  2453. * have traditional functions or classes. Instead, we extract:
  2454. * - Section references ({% section 'name' %})
  2455. * - Snippet references ({% render 'name' %} and {% include 'name' %})
  2456. * - Schema blocks ({% schema %}...{% endschema %})
  2457. */
  2458. export class LiquidExtractor {
  2459. private filePath: string;
  2460. private source: string;
  2461. private nodes: Node[] = [];
  2462. private edges: Edge[] = [];
  2463. private unresolvedReferences: UnresolvedReference[] = [];
  2464. private errors: ExtractionError[] = [];
  2465. constructor(filePath: string, source: string) {
  2466. this.filePath = filePath;
  2467. this.source = source;
  2468. }
  2469. /**
  2470. * Extract from Liquid source
  2471. */
  2472. extract(): ExtractionResult {
  2473. const startTime = Date.now();
  2474. try {
  2475. // Create file node
  2476. const fileNode = this.createFileNode();
  2477. // Extract render/include statements (snippet references)
  2478. this.extractSnippetReferences(fileNode.id);
  2479. // Extract section references
  2480. this.extractSectionReferences(fileNode.id);
  2481. // Extract schema block
  2482. this.extractSchema(fileNode.id);
  2483. // Extract assign statements as variables
  2484. this.extractAssignments(fileNode.id);
  2485. } catch (error) {
  2486. this.errors.push({
  2487. message: `Liquid extraction error: ${error instanceof Error ? error.message : String(error)}`,
  2488. severity: 'error',
  2489. });
  2490. }
  2491. return {
  2492. nodes: this.nodes,
  2493. edges: this.edges,
  2494. unresolvedReferences: this.unresolvedReferences,
  2495. errors: this.errors,
  2496. durationMs: Date.now() - startTime,
  2497. };
  2498. }
  2499. /**
  2500. * Create a file node for the Liquid template
  2501. */
  2502. private createFileNode(): Node {
  2503. const lines = this.source.split('\n');
  2504. const id = generateNodeId(this.filePath, 'file', this.filePath, 1);
  2505. const fileNode: Node = {
  2506. id,
  2507. kind: 'file',
  2508. name: this.filePath.split('/').pop() || this.filePath,
  2509. qualifiedName: this.filePath,
  2510. filePath: this.filePath,
  2511. language: 'liquid',
  2512. startLine: 1,
  2513. endLine: lines.length,
  2514. startColumn: 0,
  2515. endColumn: lines[lines.length - 1]?.length || 0,
  2516. updatedAt: Date.now(),
  2517. };
  2518. this.nodes.push(fileNode);
  2519. return fileNode;
  2520. }
  2521. /**
  2522. * Extract {% render 'snippet' %} and {% include 'snippet' %} references
  2523. */
  2524. private extractSnippetReferences(fileNodeId: string): void {
  2525. // Match {% render 'name' %} or {% include 'name' %} with optional parameters
  2526. const renderRegex = /\{%[-]?\s*(render|include)\s+['"]([^'"]+)['"]/g;
  2527. let match;
  2528. while ((match = renderRegex.exec(this.source)) !== null) {
  2529. const [fullMatch, tagType, snippetName] = match;
  2530. const line = this.getLineNumber(match.index);
  2531. // Create an import node for searchability
  2532. const importNodeId = generateNodeId(this.filePath, 'import', snippetName!, line);
  2533. const importNode: Node = {
  2534. id: importNodeId,
  2535. kind: 'import',
  2536. name: snippetName!,
  2537. qualifiedName: `${this.filePath}::import:${snippetName}`,
  2538. filePath: this.filePath,
  2539. language: 'liquid',
  2540. signature: fullMatch,
  2541. startLine: line,
  2542. endLine: line,
  2543. startColumn: match.index - this.getLineStart(line),
  2544. endColumn: match.index - this.getLineStart(line) + fullMatch.length,
  2545. updatedAt: Date.now(),
  2546. };
  2547. this.nodes.push(importNode);
  2548. // Add containment edge from file to import
  2549. this.edges.push({
  2550. source: fileNodeId,
  2551. target: importNodeId,
  2552. kind: 'contains',
  2553. });
  2554. // Create a component node for the snippet reference
  2555. const nodeId = generateNodeId(this.filePath, 'component', `${tagType}:${snippetName}`, line);
  2556. const node: Node = {
  2557. id: nodeId,
  2558. kind: 'component',
  2559. name: snippetName!,
  2560. qualifiedName: `${this.filePath}::${tagType}:${snippetName}`,
  2561. filePath: this.filePath,
  2562. language: 'liquid',
  2563. startLine: line,
  2564. endLine: line,
  2565. startColumn: match.index - this.getLineStart(line),
  2566. endColumn: match.index - this.getLineStart(line) + fullMatch.length,
  2567. updatedAt: Date.now(),
  2568. };
  2569. this.nodes.push(node);
  2570. // Add containment edge from file
  2571. this.edges.push({
  2572. source: fileNodeId,
  2573. target: nodeId,
  2574. kind: 'contains',
  2575. });
  2576. // Add unresolved reference to the snippet file
  2577. this.unresolvedReferences.push({
  2578. fromNodeId: fileNodeId,
  2579. referenceName: `snippets/${snippetName}.liquid`,
  2580. referenceKind: 'references',
  2581. line,
  2582. column: match.index - this.getLineStart(line),
  2583. });
  2584. }
  2585. }
  2586. /**
  2587. * Extract {% section 'name' %} references
  2588. */
  2589. private extractSectionReferences(fileNodeId: string): void {
  2590. // Match {% section 'name' %}
  2591. const sectionRegex = /\{%[-]?\s*section\s+['"]([^'"]+)['"]/g;
  2592. let match;
  2593. while ((match = sectionRegex.exec(this.source)) !== null) {
  2594. const [fullMatch, sectionName] = match;
  2595. const line = this.getLineNumber(match.index);
  2596. // Create an import node for searchability
  2597. const importNodeId = generateNodeId(this.filePath, 'import', sectionName!, line);
  2598. const importNode: Node = {
  2599. id: importNodeId,
  2600. kind: 'import',
  2601. name: sectionName!,
  2602. qualifiedName: `${this.filePath}::import:${sectionName}`,
  2603. filePath: this.filePath,
  2604. language: 'liquid',
  2605. signature: fullMatch,
  2606. startLine: line,
  2607. endLine: line,
  2608. startColumn: match.index - this.getLineStart(line),
  2609. endColumn: match.index - this.getLineStart(line) + fullMatch.length,
  2610. updatedAt: Date.now(),
  2611. };
  2612. this.nodes.push(importNode);
  2613. // Add containment edge from file to import
  2614. this.edges.push({
  2615. source: fileNodeId,
  2616. target: importNodeId,
  2617. kind: 'contains',
  2618. });
  2619. // Create a component node for the section reference
  2620. const nodeId = generateNodeId(this.filePath, 'component', `section:${sectionName}`, line);
  2621. const node: Node = {
  2622. id: nodeId,
  2623. kind: 'component',
  2624. name: sectionName!,
  2625. qualifiedName: `${this.filePath}::section:${sectionName}`,
  2626. filePath: this.filePath,
  2627. language: 'liquid',
  2628. startLine: line,
  2629. endLine: line,
  2630. startColumn: match.index - this.getLineStart(line),
  2631. endColumn: match.index - this.getLineStart(line) + fullMatch.length,
  2632. updatedAt: Date.now(),
  2633. };
  2634. this.nodes.push(node);
  2635. // Add containment edge from file
  2636. this.edges.push({
  2637. source: fileNodeId,
  2638. target: nodeId,
  2639. kind: 'contains',
  2640. });
  2641. // Add unresolved reference to the section file
  2642. this.unresolvedReferences.push({
  2643. fromNodeId: fileNodeId,
  2644. referenceName: `sections/${sectionName}.liquid`,
  2645. referenceKind: 'references',
  2646. line,
  2647. column: match.index - this.getLineStart(line),
  2648. });
  2649. }
  2650. }
  2651. /**
  2652. * Extract {% schema %}...{% endschema %} blocks
  2653. */
  2654. private extractSchema(fileNodeId: string): void {
  2655. // Match {% schema %}...{% endschema %}
  2656. const schemaRegex = /\{%[-]?\s*schema\s*[-]?%\}([\s\S]*?)\{%[-]?\s*endschema\s*[-]?%\}/g;
  2657. let match;
  2658. while ((match = schemaRegex.exec(this.source)) !== null) {
  2659. const [fullMatch, schemaContent] = match;
  2660. const startLine = this.getLineNumber(match.index);
  2661. const endLine = this.getLineNumber(match.index + fullMatch.length);
  2662. // Try to parse the schema JSON to get the name
  2663. let schemaName = 'schema';
  2664. try {
  2665. const schemaJson = JSON.parse(schemaContent!);
  2666. if (schemaJson.name) {
  2667. schemaName = schemaJson.name;
  2668. }
  2669. } catch {
  2670. // Schema isn't valid JSON, use default name
  2671. }
  2672. // Create a node for the schema
  2673. const nodeId = generateNodeId(this.filePath, 'constant', `schema:${schemaName}`, startLine);
  2674. const node: Node = {
  2675. id: nodeId,
  2676. kind: 'constant',
  2677. name: schemaName,
  2678. qualifiedName: `${this.filePath}::schema:${schemaName}`,
  2679. filePath: this.filePath,
  2680. language: 'liquid',
  2681. startLine,
  2682. endLine,
  2683. startColumn: match.index - this.getLineStart(startLine),
  2684. endColumn: 0,
  2685. docstring: schemaContent?.trim().substring(0, 200), // Store first 200 chars as docstring
  2686. updatedAt: Date.now(),
  2687. };
  2688. this.nodes.push(node);
  2689. // Add containment edge from file
  2690. this.edges.push({
  2691. source: fileNodeId,
  2692. target: nodeId,
  2693. kind: 'contains',
  2694. });
  2695. }
  2696. }
  2697. /**
  2698. * Extract {% assign var = value %} statements
  2699. */
  2700. private extractAssignments(fileNodeId: string): void {
  2701. // Match {% assign variable_name = ... %}
  2702. const assignRegex = /\{%[-]?\s*assign\s+(\w+)\s*=/g;
  2703. let match;
  2704. while ((match = assignRegex.exec(this.source)) !== null) {
  2705. const [, variableName] = match;
  2706. const line = this.getLineNumber(match.index);
  2707. // Create a variable node
  2708. const nodeId = generateNodeId(this.filePath, 'variable', variableName!, line);
  2709. const node: Node = {
  2710. id: nodeId,
  2711. kind: 'variable',
  2712. name: variableName!,
  2713. qualifiedName: `${this.filePath}::${variableName}`,
  2714. filePath: this.filePath,
  2715. language: 'liquid',
  2716. startLine: line,
  2717. endLine: line,
  2718. startColumn: match.index - this.getLineStart(line),
  2719. endColumn: match.index - this.getLineStart(line) + match[0].length,
  2720. updatedAt: Date.now(),
  2721. };
  2722. this.nodes.push(node);
  2723. // Add containment edge from file
  2724. this.edges.push({
  2725. source: fileNodeId,
  2726. target: nodeId,
  2727. kind: 'contains',
  2728. });
  2729. }
  2730. }
  2731. /**
  2732. * Get the line number for a character index
  2733. */
  2734. private getLineNumber(index: number): number {
  2735. const substring = this.source.substring(0, index);
  2736. return (substring.match(/\n/g) || []).length + 1;
  2737. }
  2738. /**
  2739. * Get the character index of the start of a line
  2740. */
  2741. private getLineStart(lineNumber: number): number {
  2742. const lines = this.source.split('\n');
  2743. let index = 0;
  2744. for (let i = 0; i < lineNumber - 1 && i < lines.length; i++) {
  2745. index += lines[i]!.length + 1; // +1 for newline
  2746. }
  2747. return index;
  2748. }
  2749. }
  2750. /**
  2751. * SvelteExtractor - Extracts code relationships from Svelte component files
  2752. *
  2753. * Svelte files are multi-language (script + template + style). Rather than
  2754. * parsing the full Svelte grammar, we extract the <script> block content
  2755. * and delegate it to the TypeScript/JavaScript TreeSitterExtractor.
  2756. *
  2757. * Every .svelte file produces a component node (Svelte components are always importable).
  2758. */
  2759. export class SvelteExtractor {
  2760. private filePath: string;
  2761. private source: string;
  2762. private nodes: Node[] = [];
  2763. private edges: Edge[] = [];
  2764. private unresolvedReferences: UnresolvedReference[] = [];
  2765. private errors: ExtractionError[] = [];
  2766. constructor(filePath: string, source: string) {
  2767. this.filePath = filePath;
  2768. this.source = source;
  2769. }
  2770. /**
  2771. * Extract from Svelte source
  2772. */
  2773. extract(): ExtractionResult {
  2774. const startTime = Date.now();
  2775. try {
  2776. // Create component node for the .svelte file itself
  2777. const componentNode = this.createComponentNode();
  2778. // Extract and process script blocks
  2779. const scriptBlocks = this.extractScriptBlocks();
  2780. for (const block of scriptBlocks) {
  2781. this.processScriptBlock(block, componentNode.id);
  2782. }
  2783. } catch (error) {
  2784. this.errors.push({
  2785. message: `Svelte extraction error: ${error instanceof Error ? error.message : String(error)}`,
  2786. severity: 'error',
  2787. });
  2788. }
  2789. return {
  2790. nodes: this.nodes,
  2791. edges: this.edges,
  2792. unresolvedReferences: this.unresolvedReferences,
  2793. errors: this.errors,
  2794. durationMs: Date.now() - startTime,
  2795. };
  2796. }
  2797. /**
  2798. * Create a component node for the .svelte file
  2799. */
  2800. private createComponentNode(): Node {
  2801. const lines = this.source.split('\n');
  2802. const fileName = this.filePath.split(/[/\\]/).pop() || this.filePath;
  2803. const componentName = fileName.replace(/\.svelte$/, '');
  2804. const id = generateNodeId(this.filePath, 'component', componentName, 1);
  2805. const node: Node = {
  2806. id,
  2807. kind: 'component',
  2808. name: componentName,
  2809. qualifiedName: `${this.filePath}::${componentName}`,
  2810. filePath: this.filePath,
  2811. language: 'svelte',
  2812. startLine: 1,
  2813. endLine: lines.length,
  2814. startColumn: 0,
  2815. endColumn: lines[lines.length - 1]?.length || 0,
  2816. isExported: true, // Svelte components are always importable
  2817. updatedAt: Date.now(),
  2818. };
  2819. this.nodes.push(node);
  2820. return node;
  2821. }
  2822. /**
  2823. * Extract <script> blocks from the Svelte source
  2824. */
  2825. private extractScriptBlocks(): Array<{
  2826. content: string;
  2827. startLine: number;
  2828. isModule: boolean;
  2829. isTypeScript: boolean;
  2830. }> {
  2831. const blocks: Array<{
  2832. content: string;
  2833. startLine: number;
  2834. isModule: boolean;
  2835. isTypeScript: boolean;
  2836. }> = [];
  2837. const scriptRegex = /<script(\s[^>]*)?>(?<content>[\s\S]*?)<\/script>/g;
  2838. let match;
  2839. while ((match = scriptRegex.exec(this.source)) !== null) {
  2840. const attrs = match[1] || '';
  2841. const content = match.groups?.content || match[2] || '';
  2842. // Detect TypeScript from lang attribute
  2843. const isTypeScript = /lang\s*=\s*["'](ts|typescript)["']/.test(attrs);
  2844. // Detect module script
  2845. const isModule = /context\s*=\s*["']module["']/.test(attrs);
  2846. // Calculate start line of the script content (line after <script>)
  2847. const beforeScript = this.source.substring(0, match.index);
  2848. const scriptTagLine = (beforeScript.match(/\n/g) || []).length;
  2849. // The content starts on the line after the opening <script> tag
  2850. const openingTag = match[0].substring(0, match[0].indexOf('>') + 1);
  2851. const openingTagLines = (openingTag.match(/\n/g) || []).length;
  2852. const contentStartLine = scriptTagLine + openingTagLines + 1; // 0-indexed line
  2853. blocks.push({
  2854. content,
  2855. startLine: contentStartLine,
  2856. isModule,
  2857. isTypeScript,
  2858. });
  2859. }
  2860. return blocks;
  2861. }
  2862. /**
  2863. * Process a script block by delegating to TreeSitterExtractor
  2864. */
  2865. private processScriptBlock(
  2866. block: { content: string; startLine: number; isModule: boolean; isTypeScript: boolean },
  2867. componentNodeId: string
  2868. ): void {
  2869. const scriptLanguage: Language = block.isTypeScript ? 'typescript' : 'javascript';
  2870. // Check if the script language parser is available
  2871. if (!isLanguageSupported(scriptLanguage)) {
  2872. this.errors.push({
  2873. message: `Parser for ${scriptLanguage} not available, cannot parse Svelte script block`,
  2874. severity: 'warning',
  2875. });
  2876. return;
  2877. }
  2878. // Delegate to TreeSitterExtractor
  2879. const extractor = new TreeSitterExtractor(this.filePath, block.content, scriptLanguage);
  2880. const result = extractor.extract();
  2881. // Offset line numbers from script block back to .svelte file positions
  2882. for (const node of result.nodes) {
  2883. node.startLine += block.startLine;
  2884. node.endLine += block.startLine;
  2885. node.language = 'svelte'; // Mark as svelte, not TS/JS
  2886. this.nodes.push(node);
  2887. // Add containment edge from component to this node
  2888. this.edges.push({
  2889. source: componentNodeId,
  2890. target: node.id,
  2891. kind: 'contains',
  2892. });
  2893. }
  2894. // Offset edges (they reference line numbers)
  2895. for (const edge of result.edges) {
  2896. if (edge.line) {
  2897. edge.line += block.startLine;
  2898. }
  2899. this.edges.push(edge);
  2900. }
  2901. // Offset unresolved references
  2902. for (const ref of result.unresolvedReferences) {
  2903. ref.line += block.startLine;
  2904. ref.filePath = this.filePath;
  2905. ref.language = 'svelte';
  2906. this.unresolvedReferences.push(ref);
  2907. }
  2908. // Carry over errors
  2909. for (const error of result.errors) {
  2910. if (error.line) {
  2911. error.line += block.startLine;
  2912. }
  2913. this.errors.push(error);
  2914. }
  2915. }
  2916. }
  2917. /**
  2918. * Custom extractor for Delphi DFM/FMX form files.
  2919. *
  2920. * DFM/FMX files describe the visual component hierarchy and event handler
  2921. * bindings. They use a simple text format (object/end blocks) that we parse
  2922. * with regex — no tree-sitter grammar exists for this format.
  2923. *
  2924. * Extracted information:
  2925. * - Components as NodeKind `component`
  2926. * - Nesting as EdgeKind `contains`
  2927. * - Event handlers (OnClick = MethodName) as UnresolvedReference → EdgeKind `references`
  2928. */
  2929. export class DfmExtractor {
  2930. private filePath: string;
  2931. private source: string;
  2932. private nodes: Node[] = [];
  2933. private edges: Edge[] = [];
  2934. private unresolvedReferences: UnresolvedReference[] = [];
  2935. private errors: ExtractionError[] = [];
  2936. constructor(filePath: string, source: string) {
  2937. this.filePath = filePath;
  2938. this.source = source;
  2939. }
  2940. /**
  2941. * Extract components and event handler references from DFM/FMX source
  2942. */
  2943. extract(): ExtractionResult {
  2944. const startTime = Date.now();
  2945. try {
  2946. const fileNode = this.createFileNode();
  2947. this.parseComponents(fileNode.id);
  2948. } catch (error) {
  2949. this.errors.push({
  2950. message: `DFM extraction error: ${error instanceof Error ? error.message : String(error)}`,
  2951. severity: 'error',
  2952. });
  2953. }
  2954. return {
  2955. nodes: this.nodes,
  2956. edges: this.edges,
  2957. unresolvedReferences: this.unresolvedReferences,
  2958. errors: this.errors,
  2959. durationMs: Date.now() - startTime,
  2960. };
  2961. }
  2962. /** Create a file node for the DFM form file */
  2963. private createFileNode(): Node {
  2964. const lines = this.source.split('\n');
  2965. const id = generateNodeId(this.filePath, 'file', this.filePath, 1);
  2966. const fileNode: Node = {
  2967. id,
  2968. kind: 'file',
  2969. name: this.filePath.split('/').pop() || this.filePath,
  2970. qualifiedName: this.filePath,
  2971. filePath: this.filePath,
  2972. language: 'pascal',
  2973. startLine: 1,
  2974. endLine: lines.length,
  2975. startColumn: 0,
  2976. endColumn: lines[lines.length - 1]?.length || 0,
  2977. updatedAt: Date.now(),
  2978. };
  2979. this.nodes.push(fileNode);
  2980. return fileNode;
  2981. }
  2982. /** Parse object/end blocks and extract components + event handlers */
  2983. private parseComponents(fileNodeId: string): void {
  2984. const lines = this.source.split('\n');
  2985. const stack: string[] = [fileNodeId];
  2986. const objectPattern = /^\s*(object|inherited|inline)\s+(\w+)\s*:\s*(\w+)/;
  2987. const eventPattern = /^\s*(On\w+)\s*=\s*(\w+)\s*$/;
  2988. const endPattern = /^\s*end\s*$/;
  2989. const multiLineStart = /=\s*\(\s*$/;
  2990. const multiLineItemStart = /=\s*<\s*$/;
  2991. let inMultiLine = false;
  2992. let multiLineEndChar = ')';
  2993. for (let i = 0; i < lines.length; i++) {
  2994. const line = lines[i]!;
  2995. const lineNum = i + 1;
  2996. // Skip multi-line properties
  2997. if (inMultiLine) {
  2998. if (line.trimEnd().endsWith(multiLineEndChar)) inMultiLine = false;
  2999. continue;
  3000. }
  3001. if (multiLineStart.test(line)) {
  3002. inMultiLine = true;
  3003. multiLineEndChar = ')';
  3004. continue;
  3005. }
  3006. if (multiLineItemStart.test(line)) {
  3007. inMultiLine = true;
  3008. multiLineEndChar = '>';
  3009. continue;
  3010. }
  3011. // Component declaration
  3012. const objMatch = line.match(objectPattern);
  3013. if (objMatch) {
  3014. const [, , name, typeName] = objMatch;
  3015. const nodeId = generateNodeId(this.filePath, 'component', name!, lineNum);
  3016. this.nodes.push({
  3017. id: nodeId,
  3018. kind: 'component',
  3019. name: name!,
  3020. qualifiedName: `${this.filePath}#${name}`,
  3021. filePath: this.filePath,
  3022. language: 'pascal',
  3023. startLine: lineNum,
  3024. endLine: lineNum,
  3025. startColumn: 0,
  3026. endColumn: line.length,
  3027. signature: typeName,
  3028. updatedAt: Date.now(),
  3029. });
  3030. this.edges.push({
  3031. source: stack[stack.length - 1]!,
  3032. target: nodeId,
  3033. kind: 'contains',
  3034. });
  3035. stack.push(nodeId);
  3036. continue;
  3037. }
  3038. // Event handler
  3039. const eventMatch = line.match(eventPattern);
  3040. if (eventMatch) {
  3041. const [, , methodName] = eventMatch;
  3042. this.unresolvedReferences.push({
  3043. fromNodeId: stack[stack.length - 1]!,
  3044. referenceName: methodName!,
  3045. referenceKind: 'references',
  3046. line: lineNum,
  3047. column: 0,
  3048. });
  3049. continue;
  3050. }
  3051. // Block end
  3052. if (endPattern.test(line)) {
  3053. if (stack.length > 1) stack.pop();
  3054. }
  3055. }
  3056. }
  3057. }
  3058. /**
  3059. * Extract nodes and edges from source code
  3060. */
  3061. export function extractFromSource(
  3062. filePath: string,
  3063. source: string,
  3064. language?: Language
  3065. ): ExtractionResult {
  3066. const detectedLanguage = language || detectLanguage(filePath);
  3067. const fileExtension = path.extname(filePath).toLowerCase();
  3068. // Use custom extractor for Svelte
  3069. if (detectedLanguage === 'svelte') {
  3070. const extractor = new SvelteExtractor(filePath, source);
  3071. return extractor.extract();
  3072. }
  3073. // Use custom extractor for Liquid
  3074. if (detectedLanguage === 'liquid') {
  3075. const extractor = new LiquidExtractor(filePath, source);
  3076. return extractor.extract();
  3077. }
  3078. // Use custom extractor for DFM/FMX form files
  3079. if (
  3080. detectedLanguage === 'pascal' &&
  3081. (fileExtension === '.dfm' || fileExtension === '.fmx')
  3082. ) {
  3083. const extractor = new DfmExtractor(filePath, source);
  3084. return extractor.extract();
  3085. }
  3086. const extractor = new TreeSitterExtractor(filePath, source, detectedLanguage);
  3087. return extractor.extract();
  3088. }