tree-sitter.ts 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548
  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 path from 'path';
  8. import {
  9. Language,
  10. Node,
  11. Edge,
  12. NodeKind,
  13. ExtractionResult,
  14. ExtractionError,
  15. UnresolvedReference,
  16. } from '../types';
  17. import { getParser, detectLanguage, isLanguageSupported } from './grammars';
  18. import { generateNodeId, getNodeText, getChildByField, getPrecedingDocstring } from './tree-sitter-helpers';
  19. import type { LanguageExtractor, ExtractorContext } from './tree-sitter-types';
  20. import { EXTRACTORS } from './languages';
  21. import { LiquidExtractor } from './liquid-extractor';
  22. import { SvelteExtractor } from './svelte-extractor';
  23. import { DfmExtractor } from './dfm-extractor';
  24. import { VueExtractor } from './vue-extractor';
  25. import {
  26. getAllFrameworkResolvers,
  27. getApplicableFrameworks,
  28. } from '../resolution/frameworks';
  29. // Re-export for backward compatibility
  30. export { generateNodeId } from './tree-sitter-helpers';
  31. /**
  32. * Extract the name from a node based on language
  33. */
  34. function extractName(node: SyntaxNode, source: string, extractor: LanguageExtractor): string {
  35. // Try field name first
  36. const nameNode = getChildByField(node, extractor.nameField);
  37. if (nameNode) {
  38. // Unwrap pointer_declarator(s) for C/C++ pointer return types
  39. let resolved = nameNode;
  40. while (resolved.type === 'pointer_declarator') {
  41. const inner = getChildByField(resolved, 'declarator') || resolved.namedChild(0);
  42. if (!inner) break;
  43. resolved = inner;
  44. }
  45. // Handle complex declarators (C/C++)
  46. if (resolved.type === 'function_declarator' || resolved.type === 'declarator') {
  47. const innerName = getChildByField(resolved, 'declarator') || resolved.namedChild(0);
  48. return innerName ? getNodeText(innerName, source) : getNodeText(resolved, source);
  49. }
  50. return getNodeText(resolved, source);
  51. }
  52. // For Dart method_signature, look inside inner signature types
  53. if (node.type === 'method_signature') {
  54. for (let i = 0; i < node.namedChildCount; i++) {
  55. const child = node.namedChild(i);
  56. if (child && (
  57. child.type === 'function_signature' ||
  58. child.type === 'getter_signature' ||
  59. child.type === 'setter_signature' ||
  60. child.type === 'constructor_signature' ||
  61. child.type === 'factory_constructor_signature'
  62. )) {
  63. // Find identifier inside the inner signature
  64. for (let j = 0; j < child.namedChildCount; j++) {
  65. const inner = child.namedChild(j);
  66. if (inner?.type === 'identifier') {
  67. return getNodeText(inner, source);
  68. }
  69. }
  70. }
  71. }
  72. }
  73. // Arrow/function expressions get their name from the parent variable_declarator,
  74. // not from identifiers in their body. Without this, single-expression arrow
  75. // functions like `const fn = () => someIdentifier` get named "someIdentifier"
  76. // instead of "fn", because the fallback below finds the body identifier.
  77. if (node.type === 'arrow_function' || node.type === 'function_expression') {
  78. return '<anonymous>';
  79. }
  80. // Fall back to first identifier child
  81. for (let i = 0; i < node.namedChildCount; i++) {
  82. const child = node.namedChild(i);
  83. if (
  84. child &&
  85. (child.type === 'identifier' ||
  86. child.type === 'type_identifier' ||
  87. child.type === 'simple_identifier' ||
  88. child.type === 'constant')
  89. ) {
  90. return getNodeText(child, source);
  91. }
  92. }
  93. return '<anonymous>';
  94. }
  95. /**
  96. * Tree-sitter node kinds that represent constructor invocations
  97. * (`new Foo()` and friends). Used by extractInstantiation to emit
  98. * an `instantiates` reference targeting the class name.
  99. */
  100. const INSTANTIATION_KINDS: ReadonlySet<string> = new Set([
  101. 'new_expression', // typescript / javascript / tsx / jsx
  102. 'object_creation_expression', // java / c#
  103. 'instance_creation_expression', // some grammars
  104. ]);
  105. /**
  106. * TreeSitterExtractor - Main extraction class
  107. */
  108. export class TreeSitterExtractor {
  109. private filePath: string;
  110. private language: Language;
  111. private source: string;
  112. private tree: Tree | null = null;
  113. private nodes: Node[] = [];
  114. private edges: Edge[] = [];
  115. private unresolvedReferences: UnresolvedReference[] = [];
  116. private errors: ExtractionError[] = [];
  117. private extractor: LanguageExtractor | null = null;
  118. private nodeStack: string[] = []; // Stack of parent node IDs
  119. private methodIndex: Map<string, string> | null = null; // lookup key → node ID for Pascal defProc lookup
  120. constructor(filePath: string, source: string, language?: Language) {
  121. this.filePath = filePath;
  122. this.source = source;
  123. this.language = language || detectLanguage(filePath, source);
  124. this.extractor = EXTRACTORS[this.language] || null;
  125. }
  126. /**
  127. * Parse and extract from the source code
  128. */
  129. extract(): ExtractionResult {
  130. const startTime = Date.now();
  131. if (!isLanguageSupported(this.language)) {
  132. return {
  133. nodes: [],
  134. edges: [],
  135. unresolvedReferences: [],
  136. errors: [
  137. {
  138. message: `Unsupported language: ${this.language}`,
  139. filePath: this.filePath,
  140. severity: 'error',
  141. code: 'unsupported_language',
  142. },
  143. ],
  144. durationMs: Date.now() - startTime,
  145. };
  146. }
  147. const parser = getParser(this.language);
  148. if (!parser) {
  149. return {
  150. nodes: [],
  151. edges: [],
  152. unresolvedReferences: [],
  153. errors: [
  154. {
  155. message: `Failed to get parser for language: ${this.language}`,
  156. filePath: this.filePath,
  157. severity: 'error',
  158. code: 'parser_error',
  159. },
  160. ],
  161. durationMs: Date.now() - startTime,
  162. };
  163. }
  164. try {
  165. this.tree = parser.parse(this.source) ?? null;
  166. if (!this.tree) {
  167. throw new Error('Parser returned null tree');
  168. }
  169. // Create file node representing the source file
  170. const fileNode: Node = {
  171. id: `file:${this.filePath}`,
  172. kind: 'file',
  173. name: path.basename(this.filePath),
  174. qualifiedName: this.filePath,
  175. filePath: this.filePath,
  176. language: this.language,
  177. startLine: 1,
  178. endLine: this.source.split('\n').length,
  179. startColumn: 0,
  180. endColumn: 0,
  181. isExported: false,
  182. updatedAt: Date.now(),
  183. };
  184. this.nodes.push(fileNode);
  185. // Push file node onto stack so top-level declarations get contains edges
  186. this.nodeStack.push(fileNode.id);
  187. this.visitNode(this.tree.rootNode);
  188. this.nodeStack.pop();
  189. } catch (error) {
  190. const msg = error instanceof Error ? error.message : String(error);
  191. // WASM memory errors leave the module in a corrupted state — all subsequent
  192. // parses would also fail. Re-throw so the worker can detect and crash,
  193. // forcing a clean restart with a fresh heap.
  194. if (msg.includes('memory access out of bounds') || msg.includes('out of memory')) {
  195. throw error;
  196. }
  197. this.errors.push({
  198. message: `Parse error: ${msg}`,
  199. filePath: this.filePath,
  200. severity: 'error',
  201. code: 'parse_error',
  202. });
  203. } finally {
  204. // Free tree-sitter WASM memory immediately — trees hold native heap memory
  205. // invisible to V8's GC that accumulates across thousands of files.
  206. if (this.tree) {
  207. this.tree.delete();
  208. this.tree = null;
  209. }
  210. // Release source string to reduce GC pressure
  211. this.source = '';
  212. }
  213. return {
  214. nodes: this.nodes,
  215. edges: this.edges,
  216. unresolvedReferences: this.unresolvedReferences,
  217. errors: this.errors,
  218. durationMs: Date.now() - startTime,
  219. };
  220. }
  221. /**
  222. * Visit a node and extract information
  223. */
  224. private visitNode(node: SyntaxNode): void {
  225. if (!this.extractor) return;
  226. const nodeType = node.type;
  227. let skipChildren = false;
  228. // Language-specific custom visitor hook
  229. if (this.extractor.visitNode) {
  230. const ctx = this.makeExtractorContext();
  231. const handled = this.extractor.visitNode(node, ctx);
  232. if (handled) return;
  233. }
  234. // Pascal-specific AST handling
  235. if (this.language === 'pascal') {
  236. skipChildren = this.visitPascalNode(node);
  237. if (skipChildren) return;
  238. }
  239. // Check for function declarations
  240. // For Python/Ruby, function_definition inside a class should be treated as method
  241. if (this.extractor.functionTypes.includes(nodeType)) {
  242. if (this.isInsideClassLikeNode() && this.extractor.methodTypes.includes(nodeType)) {
  243. // Inside a class - treat as method
  244. this.extractMethod(node);
  245. skipChildren = true; // extractMethod visits children via visitFunctionBody
  246. } else {
  247. this.extractFunction(node);
  248. skipChildren = true; // extractFunction visits children via visitFunctionBody
  249. }
  250. }
  251. // Check for class declarations
  252. else if (this.extractor.classTypes.includes(nodeType)) {
  253. // Some languages reuse class_declaration for structs/enums (e.g. Swift)
  254. const classification = this.extractor.classifyClassNode?.(node) ?? 'class';
  255. if (classification === 'struct') {
  256. this.extractStruct(node);
  257. } else if (classification === 'enum') {
  258. this.extractEnum(node);
  259. } else if (classification === 'interface') {
  260. this.extractInterface(node);
  261. } else if (classification === 'trait') {
  262. this.extractClass(node, 'trait');
  263. } else {
  264. this.extractClass(node);
  265. }
  266. skipChildren = true; // extractClass visits body children
  267. }
  268. // Extra class node types (e.g. Dart mixin_declaration, extension_declaration)
  269. else if (this.extractor.extraClassNodeTypes?.includes(nodeType)) {
  270. this.extractClass(node);
  271. skipChildren = true;
  272. }
  273. // Check for method declarations (only if not already handled by functionTypes)
  274. else if (this.extractor.methodTypes.includes(nodeType)) {
  275. this.extractMethod(node);
  276. skipChildren = true; // extractMethod visits children via visitFunctionBody
  277. }
  278. // Check for interface/protocol/trait declarations
  279. else if (this.extractor.interfaceTypes.includes(nodeType)) {
  280. this.extractInterface(node);
  281. skipChildren = true; // extractInterface visits body children
  282. }
  283. // Check for struct declarations
  284. else if (this.extractor.structTypes.includes(nodeType)) {
  285. this.extractStruct(node);
  286. skipChildren = true; // extractStruct visits body children
  287. }
  288. // Check for enum declarations
  289. else if (this.extractor.enumTypes.includes(nodeType)) {
  290. this.extractEnum(node);
  291. skipChildren = true; // extractEnum visits body children
  292. }
  293. // Check for type alias declarations (e.g. `type X = ...` in TypeScript)
  294. // For Go, type_spec wraps struct/interface definitions — resolveTypeAliasKind
  295. // detects these and extractTypeAlias creates the correct node kind.
  296. else if (this.extractor.typeAliasTypes.includes(nodeType)) {
  297. skipChildren = this.extractTypeAlias(node);
  298. }
  299. // Check for class properties (e.g. C# property_declaration)
  300. else if (this.extractor.propertyTypes?.includes(nodeType) && this.isInsideClassLikeNode()) {
  301. this.extractProperty(node);
  302. skipChildren = true;
  303. }
  304. // Check for class fields (e.g. Java field_declaration, C# field_declaration)
  305. else if (this.extractor.fieldTypes?.includes(nodeType) && this.isInsideClassLikeNode()) {
  306. this.extractField(node);
  307. skipChildren = true;
  308. }
  309. // Check for variable declarations (const, let, var, etc.)
  310. // Only extract top-level variables (not inside functions/methods)
  311. else if (this.extractor.variableTypes.includes(nodeType) && !this.isInsideClassLikeNode()) {
  312. this.extractVariable(node);
  313. skipChildren = true; // extractVariable handles children
  314. }
  315. // `export_statement` itself is not extracted — the walker descends
  316. // into children, where the inner declaration (lexical_declaration,
  317. // function_declaration, class_declaration, etc.) is dispatched to
  318. // its own extractor. `isExported` walks the parent chain, so the
  319. // exported flag is preserved automatically.
  320. //
  321. // Calling extractExportedVariables here AND descending caused every
  322. // `export const X = ...` to produce two nodes for the same symbol —
  323. // one kind:'variable' from extractExportedVariables and one
  324. // kind:'constant' from extractVariable. The dedicated dispatch is
  325. // the correct one (it picks kind from isConst, captures the
  326. // initializer signature, and walks type annotations); the
  327. // export-statement helper was redundant.
  328. // Check for imports
  329. else if (this.extractor.importTypes.includes(nodeType)) {
  330. this.extractImport(node);
  331. }
  332. // Check for function calls
  333. else if (this.extractor.callTypes.includes(nodeType)) {
  334. this.extractCall(node);
  335. }
  336. // `new Foo(...)` / `Foo::new(...)` / object_creation_expression —
  337. // produce an `instantiates` reference. Children still walked so
  338. // nested calls inside the constructor args (`new Foo(bar())`) get
  339. // their own `calls` refs.
  340. else if (INSTANTIATION_KINDS.has(nodeType)) {
  341. this.extractInstantiation(node);
  342. }
  343. // (Decorator handling lives inside the symbol-creating extractors
  344. // — extractClass / extractFunction / extractProperty — because the
  345. // decorator node sits BEFORE the symbol in the AST and the walker
  346. // would otherwise see the wrong nodeStack head.)
  347. // Rust: `impl Trait for Type { ... }` — creates implements edge from Type to Trait
  348. else if (nodeType === 'impl_item') {
  349. this.extractRustImplItem(node);
  350. }
  351. // Visit children (unless the extract method already visited them)
  352. if (!skipChildren) {
  353. for (let i = 0; i < node.namedChildCount; i++) {
  354. const child = node.namedChild(i);
  355. if (child) {
  356. this.visitNode(child);
  357. }
  358. }
  359. }
  360. }
  361. /**
  362. * Create a Node object
  363. */
  364. private createNode(
  365. kind: NodeKind,
  366. name: string,
  367. node: SyntaxNode,
  368. extra?: Partial<Node>
  369. ): Node | null {
  370. // Skip nodes with empty/missing names — they are not meaningful symbols
  371. // and would cause FK violations when edges reference them (see issue #42)
  372. if (!name) {
  373. return null;
  374. }
  375. const id = generateNodeId(this.filePath, kind, name, node.startPosition.row + 1);
  376. const newNode: Node = {
  377. id,
  378. kind,
  379. name,
  380. qualifiedName: this.buildQualifiedName(name),
  381. filePath: this.filePath,
  382. language: this.language,
  383. startLine: node.startPosition.row + 1,
  384. endLine: node.endPosition.row + 1,
  385. startColumn: node.startPosition.column,
  386. endColumn: node.endPosition.column,
  387. updatedAt: Date.now(),
  388. ...extra,
  389. };
  390. this.nodes.push(newNode);
  391. // Add containment edge from parent
  392. if (this.nodeStack.length > 0) {
  393. const parentId = this.nodeStack[this.nodeStack.length - 1];
  394. if (parentId) {
  395. this.edges.push({
  396. source: parentId,
  397. target: id,
  398. kind: 'contains',
  399. });
  400. }
  401. }
  402. return newNode;
  403. }
  404. /**
  405. * Find first named child whose type is in the given list.
  406. * Used to locate inner type nodes (e.g. enum_specifier inside a typedef).
  407. */
  408. private findChildByTypes(node: SyntaxNode, types: string[]): SyntaxNode | null {
  409. for (let i = 0; i < node.namedChildCount; i++) {
  410. const child = node.namedChild(i);
  411. if (child && types.includes(child.type)) return child;
  412. }
  413. return null;
  414. }
  415. /**
  416. * Build qualified name from node stack
  417. */
  418. private buildQualifiedName(name: string): string {
  419. // Build a qualified name from the semantic hierarchy only (no file path).
  420. // The file path is stored separately in filePath and pollutes FTS if included here.
  421. const parts: string[] = [];
  422. for (const nodeId of this.nodeStack) {
  423. const node = this.nodes.find((n) => n.id === nodeId);
  424. if (node && node.kind !== 'file') {
  425. parts.push(node.name);
  426. }
  427. }
  428. parts.push(name);
  429. return parts.join('::');
  430. }
  431. /**
  432. * Build an ExtractorContext for passing to language-specific visitNode hooks.
  433. */
  434. private makeExtractorContext(): ExtractorContext {
  435. // eslint-disable-next-line @typescript-eslint/no-this-alias
  436. const self = this;
  437. return {
  438. createNode: (kind, name, node, extra) => self.createNode(kind, name, node, extra),
  439. visitNode: (node) => self.visitNode(node),
  440. visitFunctionBody: (body, functionId) => self.visitFunctionBody(body, functionId),
  441. addUnresolvedReference: (ref) => self.unresolvedReferences.push(ref),
  442. pushScope: (nodeId) => self.nodeStack.push(nodeId),
  443. popScope: () => self.nodeStack.pop(),
  444. get filePath() { return self.filePath; },
  445. get source() { return self.source; },
  446. get nodeStack() { return self.nodeStack; },
  447. get nodes() { return self.nodes; },
  448. };
  449. }
  450. /**
  451. * Check if the current node stack indicates we are inside a class-like node
  452. * (class, struct, interface, trait). File nodes do not count as class-like.
  453. */
  454. private isInsideClassLikeNode(): boolean {
  455. if (this.nodeStack.length === 0) return false;
  456. const parentId = this.nodeStack[this.nodeStack.length - 1];
  457. if (!parentId) return false;
  458. const parentNode = this.nodes.find((n) => n.id === parentId);
  459. if (!parentNode) return false;
  460. return (
  461. parentNode.kind === 'class' ||
  462. parentNode.kind === 'struct' ||
  463. parentNode.kind === 'interface' ||
  464. parentNode.kind === 'trait' ||
  465. parentNode.kind === 'enum' ||
  466. parentNode.kind === 'module'
  467. );
  468. }
  469. /**
  470. * Extract a function
  471. */
  472. private extractFunction(node: SyntaxNode): void {
  473. if (!this.extractor) return;
  474. // If the language provides getReceiverType and this function has a receiver
  475. // (e.g., Rust function_item inside an impl block), extract as method instead
  476. if (this.extractor.getReceiverType?.(node, this.source)) {
  477. this.extractMethod(node);
  478. return;
  479. }
  480. let name = extractName(node, this.source, this.extractor);
  481. // For arrow functions and function expressions assigned to variables,
  482. // resolve the name from the parent variable_declarator.
  483. // e.g. `export const useAuth = () => { ... }` — the arrow_function node
  484. // has no `name` field; the name lives on the variable_declarator.
  485. if (
  486. name === '<anonymous>' &&
  487. (node.type === 'arrow_function' || node.type === 'function_expression')
  488. ) {
  489. const parent = node.parent;
  490. if (parent?.type === 'variable_declarator') {
  491. const varName = getChildByField(parent, 'name');
  492. if (varName) {
  493. name = getNodeText(varName, this.source);
  494. }
  495. }
  496. }
  497. if (name === '<anonymous>') return; // Skip anonymous functions
  498. // Check for misparse artifacts (e.g. C++ macros causing "namespace detail" functions)
  499. // Skip the node but still visit the body for calls and structural nodes
  500. if (this.extractor.isMisparsedFunction?.(name, node)) {
  501. const body = this.extractor.resolveBody?.(node, this.extractor.bodyField)
  502. ?? getChildByField(node, this.extractor.bodyField);
  503. if (body) {
  504. this.visitFunctionBody(body, '');
  505. }
  506. return;
  507. }
  508. const docstring = getPrecedingDocstring(node, this.source);
  509. const signature = this.extractor.getSignature?.(node, this.source);
  510. const visibility = this.extractor.getVisibility?.(node);
  511. const isExported = this.extractor.isExported?.(node, this.source);
  512. const isAsync = this.extractor.isAsync?.(node);
  513. const isStatic = this.extractor.isStatic?.(node);
  514. const funcNode = this.createNode('function', name, node, {
  515. docstring,
  516. signature,
  517. visibility,
  518. isExported,
  519. isAsync,
  520. isStatic,
  521. });
  522. if (!funcNode) return;
  523. // Extract type annotations (parameter types and return type)
  524. this.extractTypeAnnotations(node, funcNode.id);
  525. // Extract decorators applied to the function (rare in JS/TS but
  526. // present in Python `@decorator def f():` and Java/Kotlin
  527. // annotations on free functions).
  528. this.extractDecoratorsFor(node, funcNode.id);
  529. // Push to stack and visit body
  530. this.nodeStack.push(funcNode.id);
  531. const body = this.extractor.resolveBody?.(node, this.extractor.bodyField)
  532. ?? getChildByField(node, this.extractor.bodyField);
  533. if (body) {
  534. this.visitFunctionBody(body, funcNode.id);
  535. }
  536. this.nodeStack.pop();
  537. }
  538. /**
  539. * Extract a class
  540. */
  541. private extractClass(node: SyntaxNode, kind: NodeKind = 'class'): void {
  542. if (!this.extractor) return;
  543. const name = extractName(node, this.source, this.extractor);
  544. const docstring = getPrecedingDocstring(node, this.source);
  545. const visibility = this.extractor.getVisibility?.(node);
  546. const isExported = this.extractor.isExported?.(node, this.source);
  547. const classNode = this.createNode(kind, name, node, {
  548. docstring,
  549. visibility,
  550. isExported,
  551. });
  552. if (!classNode) return;
  553. // Extract extends/implements
  554. this.extractInheritance(node, classNode.id);
  555. // Extract decorators applied to the class (`@Foo class X {}`).
  556. this.extractDecoratorsFor(node, classNode.id);
  557. // Push to stack and visit body
  558. this.nodeStack.push(classNode.id);
  559. let body = this.extractor.resolveBody?.(node, this.extractor.bodyField)
  560. ?? getChildByField(node, this.extractor.bodyField);
  561. if (!body) body = node;
  562. // Visit all children for methods and properties
  563. for (let i = 0; i < body.namedChildCount; i++) {
  564. const child = body.namedChild(i);
  565. if (child) {
  566. this.visitNode(child);
  567. }
  568. }
  569. this.nodeStack.pop();
  570. }
  571. /**
  572. * Extract a method
  573. */
  574. private extractMethod(node: SyntaxNode): void {
  575. if (!this.extractor) return;
  576. // For languages with receiver types (Go, Rust), include receiver in qualified name
  577. // so FTS can match "scrapeLoop.run" → qualified_name "...::scrapeLoop::run"
  578. const receiverType = this.extractor.getReceiverType?.(node, this.source);
  579. // For most languages, only extract as method if inside a class-like node
  580. // Languages with methodsAreTopLevel (e.g. Go) always treat them as methods
  581. // Languages with getReceiverType (e.g. Rust) extract as method when receiver is found
  582. if (!this.isInsideClassLikeNode() && !this.extractor.methodsAreTopLevel && !receiverType) {
  583. // Skip method_definition nodes inside object literals (getters/setters/methods
  584. // in inline objects). These are ephemeral and create noise (e.g., Svelte context
  585. // objects: `ctx.set({ get view() { ... } })`).
  586. if (node.parent?.type === 'object' || node.parent?.type === 'object_expression') {
  587. return;
  588. }
  589. // Not inside a class-like node and no receiver type, treat as function
  590. this.extractFunction(node);
  591. return;
  592. }
  593. const name = extractName(node, this.source, this.extractor);
  594. // Check for misparse artifacts (e.g. C++ "switch" inside macro-confused class body)
  595. if (this.extractor.isMisparsedFunction?.(name, node)) {
  596. const body = this.extractor.resolveBody?.(node, this.extractor.bodyField)
  597. ?? getChildByField(node, this.extractor.bodyField);
  598. if (body) {
  599. this.visitFunctionBody(body, '');
  600. }
  601. return;
  602. }
  603. const docstring = getPrecedingDocstring(node, this.source);
  604. const signature = this.extractor.getSignature?.(node, this.source);
  605. const visibility = this.extractor.getVisibility?.(node);
  606. const isAsync = this.extractor.isAsync?.(node);
  607. const isStatic = this.extractor.isStatic?.(node);
  608. const extraProps: Partial<Node> = {
  609. docstring,
  610. signature,
  611. visibility,
  612. isAsync,
  613. isStatic,
  614. };
  615. if (receiverType) {
  616. extraProps.qualifiedName = `${receiverType}::${name}`;
  617. }
  618. const methodNode = this.createNode('method', name, node, extraProps);
  619. if (!methodNode) return;
  620. // For methods with a receiver type but no class-like parent on the stack
  621. // (e.g., Rust impl blocks), add a contains edge from the owning struct/trait
  622. if (receiverType && !this.isInsideClassLikeNode()) {
  623. const ownerNode = this.nodes.find(
  624. (n) =>
  625. n.name === receiverType &&
  626. n.filePath === this.filePath &&
  627. (n.kind === 'struct' || n.kind === 'class' || n.kind === 'enum' || n.kind === 'trait')
  628. );
  629. if (ownerNode) {
  630. this.edges.push({
  631. source: ownerNode.id,
  632. target: methodNode.id,
  633. kind: 'contains',
  634. });
  635. }
  636. }
  637. // Extract type annotations (parameter types and return type)
  638. this.extractTypeAnnotations(node, methodNode.id);
  639. // Extract decorators (`@Get('/list') list() {}`).
  640. this.extractDecoratorsFor(node, methodNode.id);
  641. // Push to stack and visit body
  642. this.nodeStack.push(methodNode.id);
  643. const body = this.extractor.resolveBody?.(node, this.extractor.bodyField)
  644. ?? getChildByField(node, this.extractor.bodyField);
  645. if (body) {
  646. this.visitFunctionBody(body, methodNode.id);
  647. }
  648. this.nodeStack.pop();
  649. }
  650. /**
  651. * Extract an interface/protocol/trait
  652. */
  653. private extractInterface(node: SyntaxNode): void {
  654. if (!this.extractor) return;
  655. const name = extractName(node, this.source, this.extractor);
  656. const docstring = getPrecedingDocstring(node, this.source);
  657. const isExported = this.extractor.isExported?.(node, this.source);
  658. const kind: NodeKind = this.extractor.interfaceKind ?? 'interface';
  659. const interfaceNode = this.createNode(kind, name, node, {
  660. docstring,
  661. isExported,
  662. });
  663. if (!interfaceNode) return;
  664. // Extract extends (interface inheritance)
  665. this.extractInheritance(node, interfaceNode.id);
  666. // Visit body children for interface methods and nested types
  667. this.nodeStack.push(interfaceNode.id);
  668. let body = this.extractor.resolveBody?.(node, this.extractor.bodyField)
  669. ?? getChildByField(node, this.extractor.bodyField);
  670. if (!body) body = node;
  671. for (let i = 0; i < body.namedChildCount; i++) {
  672. const child = body.namedChild(i);
  673. if (child) {
  674. this.visitNode(child);
  675. }
  676. }
  677. this.nodeStack.pop();
  678. }
  679. /**
  680. * Extract a struct
  681. */
  682. private extractStruct(node: SyntaxNode): void {
  683. if (!this.extractor) return;
  684. // Skip forward declarations and type references (no body = not a definition)
  685. const body = getChildByField(node, this.extractor.bodyField);
  686. if (!body) return;
  687. const name = extractName(node, this.source, this.extractor);
  688. const docstring = getPrecedingDocstring(node, this.source);
  689. const visibility = this.extractor.getVisibility?.(node);
  690. const isExported = this.extractor.isExported?.(node, this.source);
  691. const structNode = this.createNode('struct', name, node, {
  692. docstring,
  693. visibility,
  694. isExported,
  695. });
  696. if (!structNode) return;
  697. // Extract inheritance (e.g. Swift: struct HTTPMethod: RawRepresentable)
  698. this.extractInheritance(node, structNode.id);
  699. // Push to stack for field extraction
  700. this.nodeStack.push(structNode.id);
  701. for (let i = 0; i < body.namedChildCount; i++) {
  702. const child = body.namedChild(i);
  703. if (child) {
  704. this.visitNode(child);
  705. }
  706. }
  707. this.nodeStack.pop();
  708. }
  709. /**
  710. * Extract an enum
  711. */
  712. private extractEnum(node: SyntaxNode): void {
  713. if (!this.extractor) return;
  714. // Skip forward declarations and type references (no body = not a definition)
  715. const body = this.extractor.resolveBody?.(node, this.extractor.bodyField)
  716. ?? getChildByField(node, this.extractor.bodyField);
  717. if (!body) return;
  718. const name = extractName(node, this.source, this.extractor);
  719. const docstring = getPrecedingDocstring(node, this.source);
  720. const visibility = this.extractor.getVisibility?.(node);
  721. const isExported = this.extractor.isExported?.(node, this.source);
  722. const enumNode = this.createNode('enum', name, node, {
  723. docstring,
  724. visibility,
  725. isExported,
  726. });
  727. if (!enumNode) return;
  728. // Extract inheritance (e.g. Swift: enum AFError: Error)
  729. this.extractInheritance(node, enumNode.id);
  730. // Push to stack and visit body children (enum members, nested types, methods)
  731. this.nodeStack.push(enumNode.id);
  732. const memberTypes = this.extractor.enumMemberTypes;
  733. for (let i = 0; i < body.namedChildCount; i++) {
  734. const child = body.namedChild(i);
  735. if (!child) continue;
  736. if (memberTypes?.includes(child.type)) {
  737. this.extractEnumMembers(child);
  738. } else {
  739. this.visitNode(child);
  740. }
  741. }
  742. this.nodeStack.pop();
  743. }
  744. /**
  745. * Extract enum member names from an enum member node.
  746. * Handles multi-case declarations (Swift: `case put, delete`) and single-case patterns.
  747. */
  748. private extractEnumMembers(node: SyntaxNode): void {
  749. // Try field-based name first (e.g. Rust enum_variant has a 'name' field)
  750. const nameNode = getChildByField(node, 'name');
  751. if (nameNode) {
  752. this.createNode('enum_member', getNodeText(nameNode, this.source), node);
  753. return;
  754. }
  755. // Check for identifier-like children (Swift: simple_identifier, TS: property_identifier)
  756. let found = false;
  757. for (let i = 0; i < node.namedChildCount; i++) {
  758. const child = node.namedChild(i);
  759. if (child && (child.type === 'simple_identifier' || child.type === 'identifier' || child.type === 'property_identifier')) {
  760. this.createNode('enum_member', getNodeText(child, this.source), child);
  761. found = true;
  762. }
  763. }
  764. // If the node itself IS the identifier (e.g. TS property_identifier directly in enum body)
  765. if (!found && node.namedChildCount === 0) {
  766. this.createNode('enum_member', getNodeText(node, this.source), node);
  767. }
  768. }
  769. /**
  770. * Extract a class property declaration (e.g. C# `public string Name { get; set; }`).
  771. * Extracts as 'property' kind node inside the owning class.
  772. */
  773. private extractProperty(node: SyntaxNode): void {
  774. if (!this.extractor) return;
  775. const docstring = getPrecedingDocstring(node, this.source);
  776. const visibility = this.extractor.getVisibility?.(node);
  777. const isStatic = this.extractor.isStatic?.(node) ?? false;
  778. // Property name is a direct identifier child
  779. const nameNode = getChildByField(node, 'name')
  780. || node.namedChildren.find(c => c.type === 'identifier');
  781. if (!nameNode) return;
  782. const name = getNodeText(nameNode, this.source);
  783. // Get property type from the type child (first named child that isn't modifier or identifier)
  784. const typeNode = node.namedChildren.find(
  785. c => c.type !== 'modifier' && c.type !== 'modifiers'
  786. && c.type !== 'identifier' && c.type !== 'accessor_list'
  787. && c.type !== 'accessors' && c.type !== 'equals_value_clause'
  788. );
  789. const typeText = typeNode ? getNodeText(typeNode, this.source) : undefined;
  790. const signature = typeText ? `${typeText} ${name}` : name;
  791. const propNode = this.createNode('property', name, node, {
  792. docstring,
  793. signature,
  794. visibility,
  795. isStatic,
  796. });
  797. // `@Inject() private svc: Foo` and similar — capture the
  798. // decorator->target relationship for class properties too.
  799. if (propNode) {
  800. this.extractDecoratorsFor(node, propNode.id);
  801. }
  802. }
  803. /**
  804. * Extract a class field declaration (e.g. Java field_declaration, C# field_declaration).
  805. * Extracts each declarator as a 'field' kind node inside the owning class.
  806. */
  807. private extractField(node: SyntaxNode): void {
  808. if (!this.extractor) return;
  809. const docstring = getPrecedingDocstring(node, this.source);
  810. const visibility = this.extractor.getVisibility?.(node);
  811. const isStatic = this.extractor.isStatic?.(node) ?? false;
  812. // Java field_declaration: "private final String name = value;" → variable_declarator(s) are direct children
  813. // C# field_declaration: wraps in variable_declaration → variable_declarator(s)
  814. let declarators = node.namedChildren.filter(
  815. c => c.type === 'variable_declarator'
  816. );
  817. // C#: look inside variable_declaration wrapper
  818. if (declarators.length === 0) {
  819. const varDecl = node.namedChildren.find(c => c.type === 'variable_declaration');
  820. if (varDecl) {
  821. declarators = varDecl.namedChildren.filter(c => c.type === 'variable_declarator');
  822. }
  823. }
  824. // PHP property_declaration: property_element → variable_name → name
  825. if (declarators.length === 0) {
  826. const propElements = node.namedChildren.filter(c => c.type === 'property_element');
  827. if (propElements.length > 0) {
  828. // Get type annotation if present (e.g. "string", "int", "?Foo")
  829. const typeNode = node.namedChildren.find(
  830. c => c.type !== 'visibility_modifier' && c.type !== 'static_modifier'
  831. && c.type !== 'readonly_modifier' && c.type !== 'property_element'
  832. && c.type !== 'var_modifier'
  833. );
  834. const typeText = typeNode ? getNodeText(typeNode, this.source) : undefined;
  835. for (const elem of propElements) {
  836. const varName = elem.namedChildren.find(c => c.type === 'variable_name');
  837. const nameNode = varName?.namedChildren.find(c => c.type === 'name');
  838. if (!nameNode) continue;
  839. const name = getNodeText(nameNode, this.source);
  840. const signature = typeText ? `${typeText} $${name}` : `$${name}`;
  841. this.createNode('field', name, elem, {
  842. docstring,
  843. signature,
  844. visibility,
  845. isStatic,
  846. });
  847. }
  848. return;
  849. }
  850. }
  851. if (declarators.length > 0) {
  852. // Get field type from the type child
  853. // Java: type is a direct child of field_declaration
  854. // C#: type is inside variable_declaration wrapper
  855. const varDecl = node.namedChildren.find(c => c.type === 'variable_declaration');
  856. const typeSearchNode = varDecl ?? node;
  857. const typeNode = typeSearchNode.namedChildren.find(
  858. c => c.type !== 'modifiers' && c.type !== 'modifier' && c.type !== 'variable_declarator'
  859. && c.type !== 'variable_declaration' && c.type !== 'marker_annotation' && c.type !== 'annotation'
  860. );
  861. const typeText = typeNode ? getNodeText(typeNode, this.source) : undefined;
  862. for (const decl of declarators) {
  863. const nameNode = getChildByField(decl, 'name')
  864. || decl.namedChildren.find(c => c.type === 'identifier');
  865. if (!nameNode) continue;
  866. const name = getNodeText(nameNode, this.source);
  867. const signature = typeText ? `${typeText} ${name}` : name;
  868. const fieldNode = this.createNode('field', name, decl, {
  869. docstring,
  870. signature,
  871. visibility,
  872. isStatic,
  873. });
  874. // Java/Kotlin annotations / TS field decorators sit on the
  875. // outer field_declaration, not on the individual declarator.
  876. if (fieldNode) this.extractDecoratorsFor(node, fieldNode.id);
  877. }
  878. } else {
  879. // Fallback: try to find an identifier child directly
  880. const nameNode = getChildByField(node, 'name')
  881. || node.namedChildren.find(c => c.type === 'identifier');
  882. if (nameNode) {
  883. const name = getNodeText(nameNode, this.source);
  884. this.createNode('field', name, node, {
  885. docstring,
  886. visibility,
  887. isStatic,
  888. });
  889. }
  890. }
  891. }
  892. /**
  893. * Extract a variable declaration (const, let, var, etc.)
  894. *
  895. * Extracts top-level and module-level variable declarations.
  896. * Captures the variable name and first 100 chars of initializer in signature for searchability.
  897. */
  898. private extractVariable(node: SyntaxNode): void {
  899. if (!this.extractor) return;
  900. // Different languages have different variable declaration structures
  901. // TypeScript/JavaScript: lexical_declaration contains variable_declarator children
  902. // Python: assignment has left (identifier) and right (value)
  903. // Go: var_declaration, short_var_declaration, const_declaration
  904. const isConst = this.extractor.isConst?.(node) ?? false;
  905. const kind: NodeKind = isConst ? 'constant' : 'variable';
  906. const docstring = getPrecedingDocstring(node, this.source);
  907. const isExported = this.extractor.isExported?.(node, this.source) ?? false;
  908. // Extract variable declarators based on language
  909. if (this.language === 'typescript' || this.language === 'javascript' ||
  910. this.language === 'tsx' || this.language === 'jsx') {
  911. // Handle lexical_declaration and variable_declaration
  912. // These contain one or more variable_declarator children
  913. for (let i = 0; i < node.namedChildCount; i++) {
  914. const child = node.namedChild(i);
  915. if (child?.type === 'variable_declarator') {
  916. const nameNode = getChildByField(child, 'name');
  917. const valueNode = getChildByField(child, 'value');
  918. if (nameNode) {
  919. // Skip destructured patterns (e.g., `let { x, y } = $props()` in Svelte)
  920. // These produce ugly multi-line names like "{ class: className }"
  921. if (nameNode.type === 'object_pattern' || nameNode.type === 'array_pattern') {
  922. continue;
  923. }
  924. const name = getNodeText(nameNode, this.source);
  925. // Arrow functions / function expressions: extract as function instead of variable
  926. if (valueNode && (valueNode.type === 'arrow_function' || valueNode.type === 'function_expression')) {
  927. this.extractFunction(valueNode);
  928. continue;
  929. }
  930. // Capture first 100 chars of initializer for context (stored in signature for searchability)
  931. const initValue = valueNode ? getNodeText(valueNode, this.source).slice(0, 100) : undefined;
  932. const initSignature = initValue ? `= ${initValue}${initValue.length >= 100 ? '...' : ''}` : undefined;
  933. const varNode = this.createNode(kind, name, child, {
  934. docstring,
  935. signature: initSignature,
  936. isExported,
  937. });
  938. // Extract type annotation references (e.g., const x: ITextModel = ...)
  939. if (varNode) {
  940. this.extractVariableTypeAnnotation(child, varNode.id);
  941. }
  942. }
  943. }
  944. }
  945. } else if (this.language === 'python' || this.language === 'ruby') {
  946. // Python/Ruby assignment: left = right
  947. const left = getChildByField(node, 'left') || node.namedChild(0);
  948. const right = getChildByField(node, 'right') || node.namedChild(1);
  949. if (left && left.type === 'identifier') {
  950. const name = getNodeText(left, this.source);
  951. // Skip if name starts with lowercase and looks like a function call result
  952. // Python constants are usually UPPER_CASE
  953. const initValue = right ? getNodeText(right, this.source).slice(0, 100) : undefined;
  954. const initSignature = initValue ? `= ${initValue}${initValue.length >= 100 ? '...' : ''}` : undefined;
  955. this.createNode(kind, name, node, {
  956. docstring,
  957. signature: initSignature,
  958. });
  959. }
  960. } else if (this.language === 'go') {
  961. // Go: var_declaration, short_var_declaration, const_declaration
  962. // These can have multiple identifiers on the left
  963. const specs = node.namedChildren.filter(c =>
  964. c.type === 'var_spec' || c.type === 'const_spec'
  965. );
  966. for (const spec of specs) {
  967. const nameNode = spec.namedChild(0);
  968. if (nameNode && nameNode.type === 'identifier') {
  969. const name = getNodeText(nameNode, this.source);
  970. const valueNode = spec.namedChildCount > 1 ? spec.namedChild(spec.namedChildCount - 1) : null;
  971. const initValue = valueNode ? getNodeText(valueNode, this.source).slice(0, 100) : undefined;
  972. const initSignature = initValue ? `= ${initValue}${initValue.length >= 100 ? '...' : ''}` : undefined;
  973. this.createNode(node.type === 'const_declaration' ? 'constant' : 'variable', name, spec, {
  974. docstring,
  975. signature: initSignature,
  976. });
  977. }
  978. }
  979. // Handle short_var_declaration (:=)
  980. if (node.type === 'short_var_declaration') {
  981. const left = getChildByField(node, 'left');
  982. const right = getChildByField(node, 'right');
  983. if (left) {
  984. // Can be expression_list with multiple identifiers
  985. const identifiers = left.type === 'expression_list'
  986. ? left.namedChildren.filter(c => c.type === 'identifier')
  987. : [left];
  988. for (const id of identifiers) {
  989. const name = getNodeText(id, this.source);
  990. const initValue = right ? getNodeText(right, this.source).slice(0, 100) : undefined;
  991. const initSignature = initValue ? `= ${initValue}${initValue.length >= 100 ? '...' : ''}` : undefined;
  992. this.createNode('variable', name, node, {
  993. docstring,
  994. signature: initSignature,
  995. });
  996. }
  997. }
  998. }
  999. } else {
  1000. // Generic fallback for other languages
  1001. // Try to find identifier children
  1002. for (let i = 0; i < node.namedChildCount; i++) {
  1003. const child = node.namedChild(i);
  1004. if (child?.type === 'identifier' || child?.type === 'variable_declarator') {
  1005. const name = child.type === 'identifier'
  1006. ? getNodeText(child, this.source)
  1007. : extractName(child, this.source, this.extractor);
  1008. if (name && name !== '<anonymous>') {
  1009. this.createNode(kind, name, child, {
  1010. docstring,
  1011. isExported,
  1012. });
  1013. }
  1014. }
  1015. }
  1016. }
  1017. }
  1018. /**
  1019. * Extract a type alias (e.g. `export type X = ...` in TypeScript).
  1020. * For languages like Go, resolveTypeAliasKind detects when the type_spec
  1021. * wraps a struct or interface definition and creates the correct node kind.
  1022. * Returns true if children should be skipped (struct/interface handled body visiting).
  1023. */
  1024. private extractTypeAlias(node: SyntaxNode): boolean {
  1025. if (!this.extractor) return false;
  1026. const name = extractName(node, this.source, this.extractor);
  1027. if (name === '<anonymous>') return false;
  1028. const docstring = getPrecedingDocstring(node, this.source);
  1029. const isExported = this.extractor.isExported?.(node, this.source);
  1030. // Check if this type alias is actually a struct or interface definition
  1031. // (e.g. Go: `type Foo struct { ... }` is a type_spec wrapping struct_type)
  1032. const resolvedKind = this.extractor.resolveTypeAliasKind?.(node, this.source);
  1033. if (resolvedKind === 'struct') {
  1034. const structNode = this.createNode('struct', name, node, { docstring, isExported });
  1035. if (!structNode) return true;
  1036. // Visit body children for field extraction
  1037. this.nodeStack.push(structNode.id);
  1038. // Try Go-style 'type' field first, then find inner struct child (C typedef struct)
  1039. const typeChild = getChildByField(node, 'type')
  1040. || this.findChildByTypes(node, this.extractor.structTypes);
  1041. if (typeChild) {
  1042. // Extract struct embedding (e.g. Go: `type DB struct { *Head; Queryable }`)
  1043. this.extractInheritance(typeChild, structNode.id);
  1044. const body = getChildByField(typeChild, this.extractor.bodyField) || typeChild;
  1045. for (let i = 0; i < body.namedChildCount; i++) {
  1046. const child = body.namedChild(i);
  1047. if (child) this.visitNode(child);
  1048. }
  1049. }
  1050. this.nodeStack.pop();
  1051. return true;
  1052. }
  1053. if (resolvedKind === 'enum') {
  1054. const enumNode = this.createNode('enum', name, node, { docstring, isExported });
  1055. if (!enumNode) return true;
  1056. this.nodeStack.push(enumNode.id);
  1057. // Find the inner enum type child (e.g. C: typedef enum { ... } name)
  1058. const innerEnum = this.findChildByTypes(node, this.extractor.enumTypes);
  1059. if (innerEnum) {
  1060. this.extractInheritance(innerEnum, enumNode.id);
  1061. const body = this.extractor.resolveBody?.(innerEnum, this.extractor.bodyField)
  1062. ?? getChildByField(innerEnum, this.extractor.bodyField);
  1063. if (body) {
  1064. const memberTypes = this.extractor.enumMemberTypes;
  1065. for (let i = 0; i < body.namedChildCount; i++) {
  1066. const child = body.namedChild(i);
  1067. if (!child) continue;
  1068. if (memberTypes?.includes(child.type)) {
  1069. this.extractEnumMembers(child);
  1070. } else {
  1071. this.visitNode(child);
  1072. }
  1073. }
  1074. }
  1075. }
  1076. this.nodeStack.pop();
  1077. return true;
  1078. }
  1079. if (resolvedKind === 'interface') {
  1080. const kind: NodeKind = this.extractor.interfaceKind ?? 'interface';
  1081. const interfaceNode = this.createNode(kind, name, node, { docstring, isExported });
  1082. if (!interfaceNode) return true;
  1083. // Extract interface inheritance from the inner type node
  1084. const typeChild = getChildByField(node, 'type');
  1085. if (typeChild) this.extractInheritance(typeChild, interfaceNode.id);
  1086. return true;
  1087. }
  1088. const typeAliasNode = this.createNode('type_alias', name, node, {
  1089. docstring,
  1090. isExported,
  1091. });
  1092. // Extract type references from the alias value (e.g., `type X = ITextModel | null`)
  1093. if (typeAliasNode && this.TYPE_ANNOTATION_LANGUAGES.has(this.language)) {
  1094. // The value is everything after the `=`, which is typically the last named child
  1095. // In tree-sitter TS: type_alias_declaration has name + value children
  1096. const value = getChildByField(node, 'value');
  1097. if (value) {
  1098. this.extractTypeRefsFromSubtree(value, typeAliasNode.id);
  1099. }
  1100. }
  1101. return false;
  1102. }
  1103. // extractExportedVariables removed — the walker now descends into
  1104. // export_statement children and the inner declaration's dedicated
  1105. // extractor (extractVariable, extractFunction, extractClass, etc.)
  1106. // handles the symbol with isExported=true via parent-walk in the
  1107. // language extractor's isExported predicate.
  1108. /**
  1109. * Extract an import
  1110. *
  1111. * Creates an import node with the full import statement stored in signature for searchability.
  1112. * Also creates unresolved references for resolution purposes.
  1113. */
  1114. private extractImport(node: SyntaxNode): void {
  1115. if (!this.extractor) return;
  1116. const importText = getNodeText(node, this.source).trim();
  1117. // Try language-specific hook first
  1118. if (this.extractor.extractImport) {
  1119. const info = this.extractor.extractImport(node, this.source);
  1120. if (info) {
  1121. this.createNode('import', info.moduleName, node, {
  1122. signature: info.signature,
  1123. });
  1124. // Create unresolved reference unless the hook handled it
  1125. if (!info.handledRefs && info.moduleName && this.nodeStack.length > 0) {
  1126. const parentId = this.nodeStack[this.nodeStack.length - 1];
  1127. if (parentId) {
  1128. this.unresolvedReferences.push({
  1129. fromNodeId: parentId,
  1130. referenceName: info.moduleName,
  1131. referenceKind: 'imports',
  1132. line: node.startPosition.row + 1,
  1133. column: node.startPosition.column,
  1134. });
  1135. }
  1136. }
  1137. return;
  1138. }
  1139. // Hook returned null — fall through to multi-import inline handlers only
  1140. // (hook returning null means "I didn't handle this" for multi-import cases,
  1141. // NOT "use generic fallback" — the hook already declined)
  1142. }
  1143. // Multi-import cases that create multiple nodes (can't be expressed with single-return hook)
  1144. // Python import_statement: import os, sys (creates one import per module)
  1145. if (this.language === 'python' && node.type === 'import_statement') {
  1146. for (let i = 0; i < node.namedChildCount; i++) {
  1147. const child = node.namedChild(i);
  1148. if (child?.type === 'dotted_name') {
  1149. this.createNode('import', getNodeText(child, this.source), node, {
  1150. signature: importText,
  1151. });
  1152. } else if (child?.type === 'aliased_import') {
  1153. const dottedName = child.namedChildren.find(c => c.type === 'dotted_name');
  1154. if (dottedName) {
  1155. this.createNode('import', getNodeText(dottedName, this.source), node, {
  1156. signature: importText,
  1157. });
  1158. }
  1159. }
  1160. }
  1161. return;
  1162. }
  1163. // Go imports: single or grouped (creates one import per spec)
  1164. if (this.language === 'go') {
  1165. const parentId = this.nodeStack.length > 0 ? this.nodeStack[this.nodeStack.length - 1] : null;
  1166. const extractFromSpec = (spec: SyntaxNode): void => {
  1167. const stringLiteral = spec.namedChildren.find(c => c.type === 'interpreted_string_literal');
  1168. if (stringLiteral) {
  1169. const importPath = getNodeText(stringLiteral, this.source).replace(/['"]/g, '');
  1170. if (importPath) {
  1171. this.createNode('import', importPath, spec, {
  1172. signature: getNodeText(spec, this.source).trim(),
  1173. });
  1174. // Create unresolved reference so the resolver can create imports edges
  1175. if (parentId) {
  1176. this.unresolvedReferences.push({
  1177. fromNodeId: parentId,
  1178. referenceName: importPath,
  1179. referenceKind: 'imports',
  1180. line: spec.startPosition.row + 1,
  1181. column: spec.startPosition.column,
  1182. });
  1183. }
  1184. }
  1185. }
  1186. };
  1187. const importSpecList = node.namedChildren.find(c => c.type === 'import_spec_list');
  1188. if (importSpecList) {
  1189. for (const spec of importSpecList.namedChildren.filter(c => c.type === 'import_spec')) {
  1190. extractFromSpec(spec);
  1191. }
  1192. } else {
  1193. const importSpec = node.namedChildren.find(c => c.type === 'import_spec');
  1194. if (importSpec) {
  1195. extractFromSpec(importSpec);
  1196. }
  1197. }
  1198. return;
  1199. }
  1200. // PHP grouped imports: use X\{A, B} (creates one import per item)
  1201. if (this.language === 'php') {
  1202. const namespacePrefix = node.namedChildren.find(c => c.type === 'namespace_name');
  1203. const useGroup = node.namedChildren.find(c => c.type === 'namespace_use_group');
  1204. if (namespacePrefix && useGroup) {
  1205. const prefix = getNodeText(namespacePrefix, this.source);
  1206. const useClauses = useGroup.namedChildren.filter((c: SyntaxNode) =>
  1207. c.type === 'namespace_use_group_clause' || c.type === 'namespace_use_clause'
  1208. );
  1209. for (const clause of useClauses) {
  1210. const nsName = clause.namedChildren.find((c: SyntaxNode) => c.type === 'namespace_name');
  1211. const name = nsName
  1212. ? nsName.namedChildren.find((c: SyntaxNode) => c.type === 'name')
  1213. : clause.namedChildren.find((c: SyntaxNode) => c.type === 'name');
  1214. if (name) {
  1215. const fullPath = `${prefix}\\${getNodeText(name, this.source)}`;
  1216. this.createNode('import', fullPath, node, {
  1217. signature: importText,
  1218. });
  1219. }
  1220. }
  1221. return;
  1222. }
  1223. }
  1224. // If a hook exists but returned null, it intentionally declined this node — don't create fallback
  1225. if (this.extractor.extractImport) return;
  1226. // Generic fallback for languages without hooks
  1227. this.createNode('import', importText, node, {
  1228. signature: importText,
  1229. });
  1230. }
  1231. /**
  1232. * Extract a function call
  1233. */
  1234. private extractCall(node: SyntaxNode): void {
  1235. if (this.nodeStack.length === 0) return;
  1236. const callerId = this.nodeStack[this.nodeStack.length - 1];
  1237. if (!callerId) return;
  1238. // Get the function/method being called
  1239. let calleeName = '';
  1240. // Java/Kotlin method_invocation has 'object' + 'name' fields instead of 'function'
  1241. // PHP member_call_expression has 'object' + 'name', scoped_call_expression has 'scope' + 'name'
  1242. const nameField = getChildByField(node, 'name');
  1243. const objectField = getChildByField(node, 'object') || getChildByField(node, 'scope');
  1244. if (nameField && objectField && (node.type === 'method_invocation' || node.type === 'member_call_expression' || node.type === 'scoped_call_expression')) {
  1245. // Method call with explicit receiver: receiver.method() / $receiver->method() / ClassName::method()
  1246. const methodName = getNodeText(nameField, this.source);
  1247. let receiverName = getNodeText(objectField, this.source);
  1248. // Strip PHP $ prefix from variable names
  1249. receiverName = receiverName.replace(/^\$/, '');
  1250. if (methodName) {
  1251. // Skip self/this/parent/static receivers — they don't aid resolution
  1252. const SKIP_RECEIVERS = new Set(['self', 'this', 'cls', 'super', 'parent', 'static']);
  1253. if (SKIP_RECEIVERS.has(receiverName)) {
  1254. calleeName = methodName;
  1255. } else {
  1256. calleeName = `${receiverName}.${methodName}`;
  1257. }
  1258. }
  1259. } else {
  1260. const func = getChildByField(node, 'function') || node.namedChild(0);
  1261. if (func) {
  1262. if (func.type === 'member_expression' || func.type === 'attribute' || func.type === 'selector_expression' || func.type === 'navigation_expression') {
  1263. // Method call: obj.method() or obj.field.method()
  1264. // Go uses selector_expression with 'field', JS/TS uses member_expression with 'property'
  1265. // Kotlin uses navigation_expression with navigation_suffix > simple_identifier
  1266. let property = getChildByField(func, 'property') || getChildByField(func, 'field');
  1267. if (!property) {
  1268. const child1 = func.namedChild(1);
  1269. // Kotlin: navigation_suffix wraps the method name — extract simple_identifier from it
  1270. if (child1?.type === 'navigation_suffix') {
  1271. property = child1.namedChildren.find((c: SyntaxNode) => c.type === 'simple_identifier') ?? child1;
  1272. } else {
  1273. property = child1;
  1274. }
  1275. }
  1276. if (property) {
  1277. const methodName = getNodeText(property, this.source);
  1278. // Include receiver name for qualified resolution (e.g., console.print → "console.print")
  1279. // This helps the resolver distinguish method calls from bare function calls
  1280. // (e.g., Python's console.print() vs builtin print())
  1281. // Skip self/this/cls as they don't aid resolution
  1282. const receiver = getChildByField(func, 'object') || getChildByField(func, 'operand') || func.namedChild(0);
  1283. const SKIP_RECEIVERS = new Set(['self', 'this', 'cls', 'super']);
  1284. if (receiver && (receiver.type === 'identifier' || receiver.type === 'simple_identifier')) {
  1285. const receiverName = getNodeText(receiver, this.source);
  1286. if (!SKIP_RECEIVERS.has(receiverName)) {
  1287. calleeName = `${receiverName}.${methodName}`;
  1288. } else {
  1289. calleeName = methodName;
  1290. }
  1291. } else {
  1292. calleeName = methodName;
  1293. }
  1294. }
  1295. } else if (func.type === 'scoped_identifier' || func.type === 'scoped_call_expression') {
  1296. // Scoped call: Module::function()
  1297. calleeName = getNodeText(func, this.source);
  1298. } else {
  1299. calleeName = getNodeText(func, this.source);
  1300. }
  1301. }
  1302. }
  1303. if (calleeName) {
  1304. this.unresolvedReferences.push({
  1305. fromNodeId: callerId,
  1306. referenceName: calleeName,
  1307. referenceKind: 'calls',
  1308. line: node.startPosition.row + 1,
  1309. column: node.startPosition.column,
  1310. });
  1311. }
  1312. }
  1313. /**
  1314. * `new Foo(...)` / `Foo::new(...)` / object_creation_expression —
  1315. * emit an `instantiates` reference to the class name. The resolver
  1316. * then links it to the class node, producing the `instantiates`
  1317. * edge that powers "what creates instances of X" queries.
  1318. *
  1319. * Children are still walked so nested calls inside the constructor
  1320. * arguments (`new Foo(bar())`) get their own `calls` references.
  1321. */
  1322. private extractInstantiation(node: SyntaxNode): void {
  1323. if (this.nodeStack.length === 0) return;
  1324. const fromId = this.nodeStack[this.nodeStack.length - 1];
  1325. if (!fromId) return;
  1326. // The class name is in the `constructor`/`type`/first-named-child
  1327. // depending on grammar.
  1328. const ctor =
  1329. getChildByField(node, 'constructor') ||
  1330. getChildByField(node, 'type') ||
  1331. getChildByField(node, 'name') ||
  1332. node.namedChild(0);
  1333. if (!ctor) return;
  1334. let className = getNodeText(ctor, this.source);
  1335. // Strip type-argument suffix first: `new Map<K, V>()` would
  1336. // otherwise produce className 'Map<K, V>' (the constructor
  1337. // field is a `generic_type` node) and resolution would fail
  1338. // because no class is named with the angle-bracket suffix.
  1339. const ltIdx = className.indexOf('<');
  1340. if (ltIdx > 0) className = className.slice(0, ltIdx);
  1341. // For namespaced/qualified constructors (`new ns.Foo()`,
  1342. // `new ns::Foo()`) keep the trailing identifier — that's what
  1343. // matches a class node in the index.
  1344. const lastDot = Math.max(
  1345. className.lastIndexOf('.'),
  1346. className.lastIndexOf('::')
  1347. );
  1348. if (lastDot >= 0) className = className.slice(lastDot + 1).replace(/^[:.]/, '');
  1349. className = className.trim();
  1350. if (className) {
  1351. this.unresolvedReferences.push({
  1352. fromNodeId: fromId,
  1353. referenceName: className,
  1354. referenceKind: 'instantiates',
  1355. line: node.startPosition.row + 1,
  1356. column: node.startPosition.column,
  1357. });
  1358. }
  1359. }
  1360. /**
  1361. * Scan `declNode` and its preceding siblings (within the parent's
  1362. * named children) for decorator nodes, emitting a `decorates`
  1363. * reference from `decoratedId` to each decorator's function name.
  1364. *
  1365. * Why preceding siblings: in TypeScript, `@Foo class Bar {}` parses
  1366. * as an `export_statement` (or top-level wrapper) with the
  1367. * `decorator` as a child *before* the `class_declaration` — so the
  1368. * decorator isn't a child of the class itself. For methods/
  1369. * properties, the decorator IS a direct child of the declaration,
  1370. * so we also scan declNode.namedChildren.
  1371. *
  1372. * Idempotent across grammars: if neither location yields decorators
  1373. * (most non-decorator-using languages), the function is a no-op.
  1374. */
  1375. private extractDecoratorsFor(declNode: SyntaxNode, decoratedId: string): void {
  1376. const consider = (n: SyntaxNode | null): void => {
  1377. if (!n) return;
  1378. // `marker_annotation` is Java's grammar for arg-less annotations
  1379. // (`@Override`, `@Deprecated`); without including it, every
  1380. // such Java annotation would be silently skipped.
  1381. if (
  1382. n.type !== 'decorator' &&
  1383. n.type !== 'annotation' &&
  1384. n.type !== 'marker_annotation'
  1385. ) {
  1386. return;
  1387. }
  1388. // Find the leading identifier: skip the `@` punct, unwrap
  1389. // a call_expression if the decorator is invoked with args.
  1390. let target: SyntaxNode | null = null;
  1391. for (let i = 0; i < n.namedChildCount; i++) {
  1392. const child = n.namedChild(i);
  1393. if (!child) continue;
  1394. if (child.type === 'call_expression') {
  1395. const fn = getChildByField(child, 'function') ?? child.namedChild(0);
  1396. if (fn) target = fn;
  1397. if (target) break;
  1398. }
  1399. if (
  1400. child.type === 'identifier' ||
  1401. child.type === 'member_expression' ||
  1402. child.type === 'scoped_identifier' ||
  1403. child.type === 'navigation_expression'
  1404. ) {
  1405. target = child;
  1406. break;
  1407. }
  1408. }
  1409. if (!target) return;
  1410. let name = getNodeText(target, this.source);
  1411. const lastDot = Math.max(name.lastIndexOf('.'), name.lastIndexOf('::'));
  1412. if (lastDot >= 0) name = name.slice(lastDot + 1).replace(/^[:.]/, '');
  1413. if (!name) return;
  1414. this.unresolvedReferences.push({
  1415. fromNodeId: decoratedId,
  1416. referenceName: name,
  1417. referenceKind: 'decorates',
  1418. line: n.startPosition.row + 1,
  1419. column: n.startPosition.column,
  1420. });
  1421. };
  1422. // 1. Decorators that are direct children of the declaration
  1423. // (method/property style, also some grammars for class).
  1424. for (let i = 0; i < declNode.namedChildCount; i++) {
  1425. consider(declNode.namedChild(i));
  1426. }
  1427. // 2. Decorators that are PRECEDING siblings of the declaration
  1428. // inside the parent's children (TypeScript class style).
  1429. // Walk BACKWARDS from the declaration and stop at the first
  1430. // non-decorator sibling — without that stop, decorators
  1431. // belonging to an EARLIER unrelated declaration leak in
  1432. // (e.g. `@A class Foo {} @B class Bar {}` would otherwise
  1433. // attribute @A to Bar).
  1434. //
  1435. // Note on identity: tree-sitter web bindings return fresh JS
  1436. // wrapper objects from `parent`/`namedChild` navigation, so
  1437. // `sibling === declNode` is unreliable — `startIndex` does
  1438. // the matching instead.
  1439. const parent = declNode.parent;
  1440. if (parent) {
  1441. const declStart = declNode.startIndex;
  1442. let declIdx = -1;
  1443. for (let i = 0; i < parent.namedChildCount; i++) {
  1444. const sibling = parent.namedChild(i);
  1445. if (sibling && sibling.startIndex === declStart) {
  1446. declIdx = i;
  1447. break;
  1448. }
  1449. }
  1450. if (declIdx > 0) {
  1451. for (let j = declIdx - 1; j >= 0; j--) {
  1452. const sibling = parent.namedChild(j);
  1453. if (!sibling) continue;
  1454. if (sibling.type !== 'decorator' && sibling.type !== 'annotation' && sibling.type !== 'marker_annotation') {
  1455. break; // non-decorator separator → stop consuming
  1456. }
  1457. consider(sibling);
  1458. }
  1459. }
  1460. }
  1461. }
  1462. /**
  1463. * Visit function body and extract calls (and structural nodes).
  1464. *
  1465. * In addition to call expressions, this also detects class/struct/enum
  1466. * definitions inside function bodies. This handles two cases:
  1467. * 1. Local class/struct/enum definitions (valid in C++, Java, etc.)
  1468. * 2. C++ macro misparsing — macros like NLOHMANN_JSON_NAMESPACE_BEGIN cause
  1469. * tree-sitter to interpret the namespace block as a function_definition,
  1470. * hiding real class/struct/enum nodes inside the "function body".
  1471. */
  1472. private visitFunctionBody(body: SyntaxNode, _functionId: string): void {
  1473. if (!this.extractor) return;
  1474. const visitForCallsAndStructure = (node: SyntaxNode): void => {
  1475. const nodeType = node.type;
  1476. if (this.extractor!.callTypes.includes(nodeType)) {
  1477. this.extractCall(node);
  1478. } else if (INSTANTIATION_KINDS.has(nodeType)) {
  1479. // `new Foo()` inside a function body — emit an `instantiates`
  1480. // reference. Without this branch the body walker only knew
  1481. // about `call_expression`, so constructor invocations
  1482. // produced no graph edges at all.
  1483. this.extractInstantiation(node);
  1484. } else if (this.extractor!.extractBareCall) {
  1485. const calleeName = this.extractor!.extractBareCall(node, this.source);
  1486. if (calleeName && this.nodeStack.length > 0) {
  1487. const callerId = this.nodeStack[this.nodeStack.length - 1];
  1488. if (callerId) {
  1489. this.unresolvedReferences.push({
  1490. fromNodeId: callerId,
  1491. referenceName: calleeName,
  1492. referenceKind: 'calls',
  1493. line: node.startPosition.row + 1,
  1494. column: node.startPosition.column,
  1495. });
  1496. }
  1497. }
  1498. }
  1499. // Extract structural nodes found inside function bodies.
  1500. // Each extract method visits its own children, so we return after extracting.
  1501. if (this.extractor!.classTypes.includes(nodeType)) {
  1502. const classification = this.extractor!.classifyClassNode?.(node) ?? 'class';
  1503. if (classification === 'struct') this.extractStruct(node);
  1504. else if (classification === 'enum') this.extractEnum(node);
  1505. else if (classification === 'interface') this.extractInterface(node);
  1506. else if (classification === 'trait') this.extractClass(node, 'trait');
  1507. else this.extractClass(node);
  1508. return;
  1509. }
  1510. if (this.extractor!.structTypes.includes(nodeType)) {
  1511. this.extractStruct(node);
  1512. return;
  1513. }
  1514. if (this.extractor!.enumTypes.includes(nodeType)) {
  1515. this.extractEnum(node);
  1516. return;
  1517. }
  1518. if (this.extractor!.interfaceTypes.includes(nodeType)) {
  1519. this.extractInterface(node);
  1520. return;
  1521. }
  1522. for (let i = 0; i < node.namedChildCount; i++) {
  1523. const child = node.namedChild(i);
  1524. if (child) {
  1525. visitForCallsAndStructure(child);
  1526. }
  1527. }
  1528. };
  1529. visitForCallsAndStructure(body);
  1530. }
  1531. /**
  1532. * Extract inheritance relationships
  1533. */
  1534. private extractInheritance(node: SyntaxNode, classId: string): void {
  1535. // Look for extends/implements clauses
  1536. for (let i = 0; i < node.namedChildCount; i++) {
  1537. const child = node.namedChild(i);
  1538. if (!child) continue;
  1539. if (
  1540. child.type === 'extends_clause' ||
  1541. child.type === 'superclass' ||
  1542. child.type === 'base_clause' || // PHP class extends
  1543. child.type === 'extends_interfaces' // Java interface extends
  1544. ) {
  1545. // Extract parent class/interface names
  1546. // Java uses type_list wrapper: superclass -> type_identifier, extends_interfaces -> type_list -> type_identifier
  1547. const typeList = child.namedChildren.find((c: SyntaxNode) => c.type === 'type_list');
  1548. const targets = typeList ? typeList.namedChildren : [child.namedChild(0)];
  1549. for (const target of targets) {
  1550. if (target) {
  1551. const name = getNodeText(target, this.source);
  1552. this.unresolvedReferences.push({
  1553. fromNodeId: classId,
  1554. referenceName: name,
  1555. referenceKind: 'extends',
  1556. line: target.startPosition.row + 1,
  1557. column: target.startPosition.column,
  1558. });
  1559. }
  1560. }
  1561. }
  1562. if (
  1563. child.type === 'implements_clause' ||
  1564. child.type === 'class_interface_clause' ||
  1565. child.type === 'super_interfaces' || // Java class implements
  1566. child.type === 'interfaces' // Dart
  1567. ) {
  1568. // Extract implemented interfaces
  1569. // Java uses type_list wrapper: super_interfaces -> type_list -> type_identifier
  1570. const typeList = child.namedChildren.find((c: SyntaxNode) => c.type === 'type_list');
  1571. const targets = typeList ? typeList.namedChildren : child.namedChildren;
  1572. for (const iface of targets) {
  1573. if (iface) {
  1574. const name = getNodeText(iface, this.source);
  1575. this.unresolvedReferences.push({
  1576. fromNodeId: classId,
  1577. referenceName: name,
  1578. referenceKind: 'implements',
  1579. line: iface.startPosition.row + 1,
  1580. column: iface.startPosition.column,
  1581. });
  1582. }
  1583. }
  1584. }
  1585. // Python superclass list: `class Flask(Scaffold, Mixin):`
  1586. // argument_list contains identifier children for each parent class
  1587. if (child.type === 'argument_list' && node.type === 'class_definition') {
  1588. for (const arg of child.namedChildren) {
  1589. if (arg.type === 'identifier' || arg.type === 'attribute') {
  1590. const name = getNodeText(arg, this.source);
  1591. this.unresolvedReferences.push({
  1592. fromNodeId: classId,
  1593. referenceName: name,
  1594. referenceKind: 'extends',
  1595. line: arg.startPosition.row + 1,
  1596. column: arg.startPosition.column,
  1597. });
  1598. }
  1599. }
  1600. }
  1601. // Go interface embedding: `type Querier interface { LabelQuerier; ... }`
  1602. // constraint_elem wraps the embedded interface type identifier
  1603. if (child.type === 'constraint_elem') {
  1604. const typeId = child.namedChildren.find((c: SyntaxNode) => c.type === 'type_identifier');
  1605. if (typeId) {
  1606. const name = getNodeText(typeId, this.source);
  1607. this.unresolvedReferences.push({
  1608. fromNodeId: classId,
  1609. referenceName: name,
  1610. referenceKind: 'extends',
  1611. line: typeId.startPosition.row + 1,
  1612. column: typeId.startPosition.column,
  1613. });
  1614. }
  1615. }
  1616. // Go struct embedding: field_declaration without field_identifier
  1617. // e.g. `type DB struct { *Head; Queryable }` — no field name means embedded type
  1618. if (child.type === 'field_declaration') {
  1619. const hasFieldIdentifier = child.namedChildren.some((c: SyntaxNode) => c.type === 'field_identifier');
  1620. if (!hasFieldIdentifier) {
  1621. const typeId = child.namedChildren.find((c: SyntaxNode) => c.type === 'type_identifier');
  1622. if (typeId) {
  1623. const name = getNodeText(typeId, this.source);
  1624. this.unresolvedReferences.push({
  1625. fromNodeId: classId,
  1626. referenceName: name,
  1627. referenceKind: 'extends',
  1628. line: typeId.startPosition.row + 1,
  1629. column: typeId.startPosition.column,
  1630. });
  1631. }
  1632. }
  1633. }
  1634. // Rust trait supertraits: `trait SubTrait: SuperTrait + Display { ... }`
  1635. // trait_bounds contains type_identifier, generic_type, or higher_ranked_trait_bound children
  1636. if (child.type === 'trait_bounds') {
  1637. for (const bound of child.namedChildren) {
  1638. let typeName: string | undefined;
  1639. let posNode: SyntaxNode | undefined;
  1640. if (bound.type === 'type_identifier') {
  1641. typeName = getNodeText(bound, this.source);
  1642. posNode = bound;
  1643. } else if (bound.type === 'generic_type') {
  1644. // e.g. `Deserialize<'de>`
  1645. const inner = bound.namedChildren.find((c: SyntaxNode) => c.type === 'type_identifier');
  1646. if (inner) { typeName = getNodeText(inner, this.source); posNode = inner; }
  1647. } else if (bound.type === 'higher_ranked_trait_bound') {
  1648. // e.g. `for<'de> Deserialize<'de>`
  1649. const generic = bound.namedChildren.find((c: SyntaxNode) => c.type === 'generic_type');
  1650. const typeId = generic?.namedChildren.find((c: SyntaxNode) => c.type === 'type_identifier')
  1651. ?? bound.namedChildren.find((c: SyntaxNode) => c.type === 'type_identifier');
  1652. if (typeId) { typeName = getNodeText(typeId, this.source); posNode = typeId; }
  1653. }
  1654. if (typeName && posNode) {
  1655. this.unresolvedReferences.push({
  1656. fromNodeId: classId,
  1657. referenceName: typeName,
  1658. referenceKind: 'extends',
  1659. line: posNode.startPosition.row + 1,
  1660. column: posNode.startPosition.column,
  1661. });
  1662. }
  1663. }
  1664. }
  1665. // C#: `class Movie : BaseItem, IPlugin` → base_list with identifier children
  1666. // base_list combines both base class and interfaces in a single colon-separated list.
  1667. // We emit all as 'extends' since the syntax doesn't distinguish them.
  1668. if (child.type === 'base_list') {
  1669. for (const baseType of child.namedChildren) {
  1670. if (baseType) {
  1671. // For generic base types like `ClientBase<T>`, extract just the type name
  1672. const name = baseType.type === 'generic_name'
  1673. ? getNodeText(baseType.namedChildren.find((c: SyntaxNode) => c.type === 'identifier') ?? baseType, this.source)
  1674. : getNodeText(baseType, this.source);
  1675. this.unresolvedReferences.push({
  1676. fromNodeId: classId,
  1677. referenceName: name,
  1678. referenceKind: 'extends',
  1679. line: baseType.startPosition.row + 1,
  1680. column: baseType.startPosition.column,
  1681. });
  1682. }
  1683. }
  1684. }
  1685. // Kotlin: `class Foo : Bar, Baz` → delegation_specifier > user_type > type_identifier
  1686. // Also handles `class Foo : Bar()` → delegation_specifier > constructor_invocation > user_type
  1687. if (child.type === 'delegation_specifier') {
  1688. const userType = child.namedChildren.find((c: SyntaxNode) => c.type === 'user_type');
  1689. const constructorInvocation = child.namedChildren.find((c: SyntaxNode) => c.type === 'constructor_invocation');
  1690. const target = userType ?? constructorInvocation;
  1691. if (target) {
  1692. const typeId = target.type === 'user_type'
  1693. ? target.namedChildren.find((c: SyntaxNode) => c.type === 'type_identifier') ?? target
  1694. : target.namedChildren.find((c: SyntaxNode) => c.type === 'user_type')?.namedChildren.find((c: SyntaxNode) => c.type === 'type_identifier')
  1695. ?? target.namedChildren.find((c: SyntaxNode) => c.type === 'user_type') ?? target;
  1696. const name = getNodeText(typeId, this.source);
  1697. this.unresolvedReferences.push({
  1698. fromNodeId: classId,
  1699. referenceName: name,
  1700. referenceKind: 'extends',
  1701. line: typeId.startPosition.row + 1,
  1702. column: typeId.startPosition.column,
  1703. });
  1704. }
  1705. }
  1706. // Swift: inheritance_specifier > user_type > type_identifier
  1707. // Used for class inheritance, protocol conformance, and protocol inheritance
  1708. if (child.type === 'inheritance_specifier') {
  1709. const userType = child.namedChildren.find((c: SyntaxNode) => c.type === 'user_type');
  1710. const typeId = userType?.namedChildren.find((c: SyntaxNode) => c.type === 'type_identifier');
  1711. if (typeId) {
  1712. const name = getNodeText(typeId, this.source);
  1713. this.unresolvedReferences.push({
  1714. fromNodeId: classId,
  1715. referenceName: name,
  1716. referenceKind: 'extends',
  1717. line: typeId.startPosition.row + 1,
  1718. column: typeId.startPosition.column,
  1719. });
  1720. }
  1721. }
  1722. // JavaScript class_heritage has bare identifier without extends_clause wrapper
  1723. // e.g. `class Foo extends Bar {}` → class_heritage → identifier("Bar")
  1724. if (
  1725. (child.type === 'identifier' || child.type === 'type_identifier') &&
  1726. node.type === 'class_heritage'
  1727. ) {
  1728. const name = getNodeText(child, this.source);
  1729. this.unresolvedReferences.push({
  1730. fromNodeId: classId,
  1731. referenceName: name,
  1732. referenceKind: 'extends',
  1733. line: child.startPosition.row + 1,
  1734. column: child.startPosition.column,
  1735. });
  1736. }
  1737. // Recurse into container nodes (e.g. field_declaration_list in Go structs,
  1738. // class_heritage in TypeScript which wraps extends_clause/implements_clause)
  1739. if (child.type === 'field_declaration_list' || child.type === 'class_heritage') {
  1740. this.extractInheritance(child, classId);
  1741. }
  1742. }
  1743. }
  1744. /**
  1745. * Rust `impl Trait for Type` — creates an implements edge from Type to Trait.
  1746. * For plain `impl Type { ... }` (no trait), no inheritance edge is needed.
  1747. */
  1748. private extractRustImplItem(node: SyntaxNode): void {
  1749. // Check if this is `impl Trait for Type` by looking for a `for` keyword
  1750. const hasFor = node.children.some(
  1751. (c: SyntaxNode) => c.type === 'for' && !c.isNamed
  1752. );
  1753. if (!hasFor) return;
  1754. // In `impl Trait for Type`, the type_identifiers are:
  1755. // first = Trait name, last = implementing Type name
  1756. // Also handle generic types like `impl<T> Trait for MyStruct<T>`
  1757. const typeIdents = node.namedChildren.filter(
  1758. (c: SyntaxNode) => c.type === 'type_identifier' || c.type === 'generic_type' || c.type === 'scoped_type_identifier'
  1759. );
  1760. if (typeIdents.length < 2) return;
  1761. const traitNode = typeIdents[0]!;
  1762. const typeNode = typeIdents[typeIdents.length - 1]!;
  1763. // Get the trait name (handle scoped paths like std::fmt::Display)
  1764. const traitName = traitNode.type === 'scoped_type_identifier'
  1765. ? this.source.substring(traitNode.startIndex, traitNode.endIndex)
  1766. : getNodeText(traitNode, this.source);
  1767. // Get the implementing type name (extract inner type_identifier for generics)
  1768. let typeName: string;
  1769. if (typeNode.type === 'generic_type') {
  1770. const inner = typeNode.namedChildren.find(
  1771. (c: SyntaxNode) => c.type === 'type_identifier'
  1772. );
  1773. typeName = inner ? getNodeText(inner, this.source) : getNodeText(typeNode, this.source);
  1774. } else {
  1775. typeName = getNodeText(typeNode, this.source);
  1776. }
  1777. // Find the struct/type node for the implementing type
  1778. const typeNodeId = this.findNodeByName(typeName);
  1779. if (typeNodeId) {
  1780. this.unresolvedReferences.push({
  1781. fromNodeId: typeNodeId,
  1782. referenceName: traitName,
  1783. referenceKind: 'implements',
  1784. line: traitNode.startPosition.row + 1,
  1785. column: traitNode.startPosition.column,
  1786. });
  1787. }
  1788. }
  1789. /**
  1790. * Find a previously-extracted node by name (used for back-references like impl blocks)
  1791. */
  1792. private findNodeByName(name: string): string | undefined {
  1793. for (const node of this.nodes) {
  1794. if (node.name === name && (node.kind === 'struct' || node.kind === 'enum' || node.kind === 'class')) {
  1795. return node.id;
  1796. }
  1797. }
  1798. return undefined;
  1799. }
  1800. /**
  1801. * Languages that support type annotations (TypeScript, etc.)
  1802. */
  1803. private readonly TYPE_ANNOTATION_LANGUAGES = new Set([
  1804. 'typescript', 'tsx', 'dart', 'kotlin', 'swift', 'rust', 'go', 'java', 'csharp',
  1805. ]);
  1806. /**
  1807. * Built-in/primitive type names that shouldn't create references
  1808. */
  1809. private readonly BUILTIN_TYPES = new Set([
  1810. 'string', 'number', 'boolean', 'void', 'null', 'undefined', 'never', 'any', 'unknown',
  1811. 'object', 'symbol', 'bigint', 'true', 'false',
  1812. // Rust
  1813. 'str', 'bool', 'i8', 'i16', 'i32', 'i64', 'i128', 'isize',
  1814. 'u8', 'u16', 'u32', 'u64', 'u128', 'usize', 'f32', 'f64', 'char',
  1815. // Java/C#
  1816. 'int', 'long', 'short', 'byte', 'float', 'double', 'char',
  1817. // Go
  1818. 'int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64',
  1819. 'float32', 'float64', 'complex64', 'complex128', 'rune', 'error',
  1820. ]);
  1821. /**
  1822. * Extract type references from type annotations on a function/method/field node.
  1823. * Creates 'references' edges for parameter types, return types, and field types.
  1824. */
  1825. private extractTypeAnnotations(node: SyntaxNode, nodeId: string): void {
  1826. if (!this.extractor) return;
  1827. if (!this.TYPE_ANNOTATION_LANGUAGES.has(this.language)) return;
  1828. // Extract parameter type annotations
  1829. const params = getChildByField(node, this.extractor.paramsField || 'parameters');
  1830. if (params) {
  1831. this.extractTypeRefsFromSubtree(params, nodeId);
  1832. }
  1833. // Extract return type annotation
  1834. const returnType = getChildByField(node, this.extractor.returnField || 'return_type');
  1835. if (returnType) {
  1836. this.extractTypeRefsFromSubtree(returnType, nodeId);
  1837. }
  1838. // Extract direct type annotation (for class fields like `model: ITextModel`)
  1839. const typeAnnotation = node.namedChildren.find(
  1840. (c: SyntaxNode) => c.type === 'type_annotation'
  1841. );
  1842. if (typeAnnotation) {
  1843. this.extractTypeRefsFromSubtree(typeAnnotation, nodeId);
  1844. }
  1845. }
  1846. /**
  1847. * Extract type references from a variable's type annotation.
  1848. */
  1849. private extractVariableTypeAnnotation(node: SyntaxNode, nodeId: string): void {
  1850. if (!this.TYPE_ANNOTATION_LANGUAGES.has(this.language)) return;
  1851. // Find type_annotation child (covers TS `: Type`, Rust `: Type`, etc.)
  1852. const typeAnnotation = node.namedChildren.find(
  1853. (c: SyntaxNode) => c.type === 'type_annotation'
  1854. );
  1855. if (typeAnnotation) {
  1856. this.extractTypeRefsFromSubtree(typeAnnotation, nodeId);
  1857. }
  1858. }
  1859. /**
  1860. * Recursively walk a subtree and extract all type_identifier references.
  1861. * Handles unions, intersections, generics, arrays, etc.
  1862. */
  1863. private extractTypeRefsFromSubtree(node: SyntaxNode, fromNodeId: string): void {
  1864. if (node.type === 'type_identifier') {
  1865. const typeName = getNodeText(node, this.source);
  1866. if (typeName && !this.BUILTIN_TYPES.has(typeName)) {
  1867. this.unresolvedReferences.push({
  1868. fromNodeId,
  1869. referenceName: typeName,
  1870. referenceKind: 'references',
  1871. line: node.startPosition.row + 1,
  1872. column: node.startPosition.column,
  1873. });
  1874. }
  1875. return; // type_identifier is a leaf
  1876. }
  1877. // Recurse into children (handles union_type, intersection_type, generic_type, etc.)
  1878. for (let i = 0; i < node.namedChildCount; i++) {
  1879. const child = node.namedChild(i);
  1880. if (child) {
  1881. this.extractTypeRefsFromSubtree(child, fromNodeId);
  1882. }
  1883. }
  1884. }
  1885. /**
  1886. * Handle Pascal-specific AST structures.
  1887. * Returns true if the node was fully handled and children should be skipped.
  1888. */
  1889. private visitPascalNode(node: SyntaxNode): boolean {
  1890. const nodeType = node.type;
  1891. // Unit/Program/Library → module node
  1892. if (nodeType === 'unit' || nodeType === 'program' || nodeType === 'library') {
  1893. const moduleNameNode = node.namedChildren.find(
  1894. (c: SyntaxNode) => c.type === 'moduleName'
  1895. );
  1896. const name = moduleNameNode ? getNodeText(moduleNameNode, this.source) : '';
  1897. // Fallback to filename without extension if module name is empty
  1898. const moduleName = name || path.basename(this.filePath).replace(/\.[^.]+$/, '');
  1899. this.createNode('module', moduleName, node);
  1900. // Continue visiting children (interface/implementation sections)
  1901. for (let i = 0; i < node.namedChildCount; i++) {
  1902. const child = node.namedChild(i);
  1903. if (child) this.visitNode(child);
  1904. }
  1905. return true;
  1906. }
  1907. // declType wraps declClass/declIntf/declEnum/type-alias
  1908. // The name lives on declType, the inner node determines the kind
  1909. if (nodeType === 'declType') {
  1910. this.extractPascalDeclType(node);
  1911. return true;
  1912. }
  1913. // declUses → import nodes for each unit name
  1914. if (nodeType === 'declUses') {
  1915. this.extractPascalUses(node);
  1916. return true;
  1917. }
  1918. // declConsts → container; visit children for individual declConst
  1919. if (nodeType === 'declConsts') {
  1920. for (let i = 0; i < node.namedChildCount; i++) {
  1921. const child = node.namedChild(i);
  1922. if (child?.type === 'declConst') {
  1923. this.extractPascalConst(child);
  1924. }
  1925. }
  1926. return true;
  1927. }
  1928. // declConst at top level (outside declConsts)
  1929. if (nodeType === 'declConst') {
  1930. this.extractPascalConst(node);
  1931. return true;
  1932. }
  1933. // declTypes → container for type declarations
  1934. if (nodeType === 'declTypes') {
  1935. for (let i = 0; i < node.namedChildCount; i++) {
  1936. const child = node.namedChild(i);
  1937. if (child) this.visitNode(child);
  1938. }
  1939. return true;
  1940. }
  1941. // declVars → container for variable declarations
  1942. if (nodeType === 'declVars') {
  1943. for (let i = 0; i < node.namedChildCount; i++) {
  1944. const child = node.namedChild(i);
  1945. if (child?.type === 'declVar') {
  1946. const nameNode = getChildByField(child, 'name');
  1947. if (nameNode) {
  1948. const name = getNodeText(nameNode, this.source);
  1949. this.createNode('variable', name, child);
  1950. }
  1951. }
  1952. }
  1953. return true;
  1954. }
  1955. // defProc in implementation section → extract calls but don't create duplicate nodes
  1956. if (nodeType === 'defProc') {
  1957. this.extractPascalDefProc(node);
  1958. return true;
  1959. }
  1960. // declProp → property node
  1961. if (nodeType === 'declProp') {
  1962. const nameNode = getChildByField(node, 'name');
  1963. if (nameNode) {
  1964. const name = getNodeText(nameNode, this.source);
  1965. const visibility = this.extractor!.getVisibility?.(node);
  1966. this.createNode('property', name, node, { visibility });
  1967. }
  1968. return true;
  1969. }
  1970. // declField → field node
  1971. if (nodeType === 'declField') {
  1972. const nameNode = getChildByField(node, 'name');
  1973. if (nameNode) {
  1974. const name = getNodeText(nameNode, this.source);
  1975. const visibility = this.extractor!.getVisibility?.(node);
  1976. this.createNode('field', name, node, { visibility });
  1977. }
  1978. return true;
  1979. }
  1980. // declSection → visit children (propagates visibility via getVisibility)
  1981. if (nodeType === 'declSection') {
  1982. for (let i = 0; i < node.namedChildCount; i++) {
  1983. const child = node.namedChild(i);
  1984. if (child) this.visitNode(child);
  1985. }
  1986. return true;
  1987. }
  1988. // exprCall → extract function call reference
  1989. if (nodeType === 'exprCall') {
  1990. this.extractPascalCall(node);
  1991. return true;
  1992. }
  1993. // interface/implementation sections → visit children
  1994. if (nodeType === 'interface' || nodeType === 'implementation') {
  1995. for (let i = 0; i < node.namedChildCount; i++) {
  1996. const child = node.namedChild(i);
  1997. if (child) this.visitNode(child);
  1998. }
  1999. return true;
  2000. }
  2001. // block (begin..end) → visit for calls
  2002. if (nodeType === 'block') {
  2003. this.visitPascalBlock(node);
  2004. return true;
  2005. }
  2006. return false;
  2007. }
  2008. /**
  2009. * Extract a Pascal declType node (class, interface, enum, or type alias)
  2010. */
  2011. private extractPascalDeclType(node: SyntaxNode): void {
  2012. const nameNode = getChildByField(node, 'name');
  2013. if (!nameNode) return;
  2014. const name = getNodeText(nameNode, this.source);
  2015. // Find the inner type declaration
  2016. const declClass = node.namedChildren.find(
  2017. (c: SyntaxNode) => c.type === 'declClass'
  2018. );
  2019. const declIntf = node.namedChildren.find(
  2020. (c: SyntaxNode) => c.type === 'declIntf'
  2021. );
  2022. const typeChild = node.namedChildren.find(
  2023. (c: SyntaxNode) => c.type === 'type'
  2024. );
  2025. if (declClass) {
  2026. const classNode = this.createNode('class', name, node);
  2027. if (classNode) {
  2028. // Extract inheritance from typeref children of declClass
  2029. this.extractPascalInheritance(declClass, classNode.id);
  2030. // Visit class body
  2031. this.nodeStack.push(classNode.id);
  2032. for (let i = 0; i < declClass.namedChildCount; i++) {
  2033. const child = declClass.namedChild(i);
  2034. if (child) this.visitNode(child);
  2035. }
  2036. this.nodeStack.pop();
  2037. }
  2038. } else if (declIntf) {
  2039. const ifaceNode = this.createNode('interface', name, node);
  2040. if (ifaceNode) {
  2041. // Visit interface members
  2042. this.nodeStack.push(ifaceNode.id);
  2043. for (let i = 0; i < declIntf.namedChildCount; i++) {
  2044. const child = declIntf.namedChild(i);
  2045. if (child) this.visitNode(child);
  2046. }
  2047. this.nodeStack.pop();
  2048. }
  2049. } else if (typeChild) {
  2050. // Check if it contains a declEnum
  2051. const declEnum = typeChild.namedChildren.find(
  2052. (c: SyntaxNode) => c.type === 'declEnum'
  2053. );
  2054. if (declEnum) {
  2055. const enumNode = this.createNode('enum', name, node);
  2056. if (enumNode) {
  2057. // Extract enum members
  2058. this.nodeStack.push(enumNode.id);
  2059. for (let i = 0; i < declEnum.namedChildCount; i++) {
  2060. const child = declEnum.namedChild(i);
  2061. if (child?.type === 'declEnumValue') {
  2062. const memberName = getChildByField(child, 'name');
  2063. if (memberName) {
  2064. this.createNode('enum_member', getNodeText(memberName, this.source), child);
  2065. }
  2066. }
  2067. }
  2068. this.nodeStack.pop();
  2069. }
  2070. } else {
  2071. // Simple type alias: type TFoo = string / type TFoo = Integer
  2072. this.createNode('type_alias', name, node);
  2073. }
  2074. } else {
  2075. // Fallback: could be a forward declaration or simple alias
  2076. this.createNode('type_alias', name, node);
  2077. }
  2078. }
  2079. /**
  2080. * Extract Pascal uses clause into individual import nodes
  2081. */
  2082. private extractPascalUses(node: SyntaxNode): void {
  2083. const importText = getNodeText(node, this.source).trim();
  2084. for (let i = 0; i < node.namedChildCount; i++) {
  2085. const child = node.namedChild(i);
  2086. if (child?.type === 'moduleName') {
  2087. const unitName = getNodeText(child, this.source);
  2088. this.createNode('import', unitName, child, {
  2089. signature: importText,
  2090. });
  2091. // Create unresolved reference for resolution
  2092. if (this.nodeStack.length > 0) {
  2093. const parentId = this.nodeStack[this.nodeStack.length - 1];
  2094. if (parentId) {
  2095. this.unresolvedReferences.push({
  2096. fromNodeId: parentId,
  2097. referenceName: unitName,
  2098. referenceKind: 'imports',
  2099. line: child.startPosition.row + 1,
  2100. column: child.startPosition.column,
  2101. });
  2102. }
  2103. }
  2104. }
  2105. }
  2106. }
  2107. /**
  2108. * Extract a Pascal constant declaration
  2109. */
  2110. private extractPascalConst(node: SyntaxNode): void {
  2111. const nameNode = getChildByField(node, 'name');
  2112. if (!nameNode) return;
  2113. const name = getNodeText(nameNode, this.source);
  2114. const defaultValue = node.namedChildren.find(
  2115. (c: SyntaxNode) => c.type === 'defaultValue'
  2116. );
  2117. const sig = defaultValue ? getNodeText(defaultValue, this.source) : undefined;
  2118. this.createNode('constant', name, node, { signature: sig });
  2119. }
  2120. /**
  2121. * Extract Pascal inheritance (extends/implements) from declClass typeref children
  2122. */
  2123. private extractPascalInheritance(declClass: SyntaxNode, classId: string): void {
  2124. const typerefs = declClass.namedChildren.filter(
  2125. (c: SyntaxNode) => c.type === 'typeref'
  2126. );
  2127. for (let i = 0; i < typerefs.length; i++) {
  2128. const ref = typerefs[i]!;
  2129. const name = getNodeText(ref, this.source);
  2130. this.unresolvedReferences.push({
  2131. fromNodeId: classId,
  2132. referenceName: name,
  2133. referenceKind: i === 0 ? 'extends' : 'implements',
  2134. line: ref.startPosition.row + 1,
  2135. column: ref.startPosition.column,
  2136. });
  2137. }
  2138. }
  2139. /**
  2140. * Extract calls and resolve method context from a Pascal defProc (implementation body).
  2141. * Does not create a new node — the declaration was already captured from the interface section.
  2142. */
  2143. private extractPascalDefProc(node: SyntaxNode): void {
  2144. // Find the matching declaration node by name to use as call parent
  2145. const declProc = node.namedChildren.find(
  2146. (c: SyntaxNode) => c.type === 'declProc'
  2147. );
  2148. if (!declProc) return;
  2149. const nameNode = getChildByField(declProc, 'name');
  2150. if (!nameNode) return;
  2151. const fullName = getNodeText(nameNode, this.source).trim();
  2152. // fullName is like "TAuthService.Create"
  2153. const shortName = fullName.includes('.') ? fullName.split('.').pop()! : fullName;
  2154. const fullNameKey = fullName.toLowerCase();
  2155. const shortNameKey = shortName.toLowerCase();
  2156. // Build method index on first use (O(n) once, then O(1) per lookup)
  2157. if (!this.methodIndex) {
  2158. this.methodIndex = new Map();
  2159. for (const n of this.nodes) {
  2160. if (n.kind === 'method' || n.kind === 'function') {
  2161. const nameKey = n.name.toLowerCase();
  2162. // Keep first seen short-name mapping to avoid silently overwriting earlier entries.
  2163. if (!this.methodIndex.has(nameKey)) {
  2164. this.methodIndex.set(nameKey, n.id);
  2165. }
  2166. // For Pascal methods, also index qualified forms (e.g. TAuthService.Create).
  2167. if (n.kind === 'method') {
  2168. const qualifiedParts = n.qualifiedName.split('::');
  2169. if (qualifiedParts.length >= 2) {
  2170. // Create suffix keys so both "Module.Class.Method" and "Class.Method" can resolve.
  2171. for (let i = 0; i < qualifiedParts.length - 1; i++) {
  2172. const scopedName = qualifiedParts.slice(i).join('.').toLowerCase();
  2173. this.methodIndex.set(scopedName, n.id);
  2174. }
  2175. }
  2176. }
  2177. }
  2178. }
  2179. }
  2180. const parentId =
  2181. this.methodIndex.get(fullNameKey) ||
  2182. this.methodIndex.get(shortNameKey) ||
  2183. this.nodeStack[this.nodeStack.length - 1];
  2184. if (!parentId) return;
  2185. // Visit the block for calls
  2186. const block = node.namedChildren.find(
  2187. (c: SyntaxNode) => c.type === 'block'
  2188. );
  2189. if (block) {
  2190. this.nodeStack.push(parentId);
  2191. this.visitPascalBlock(block);
  2192. this.nodeStack.pop();
  2193. }
  2194. }
  2195. /**
  2196. * Extract function calls from a Pascal expression
  2197. */
  2198. private extractPascalCall(node: SyntaxNode): void {
  2199. if (this.nodeStack.length === 0) return;
  2200. const callerId = this.nodeStack[this.nodeStack.length - 1];
  2201. if (!callerId) return;
  2202. // Get the callee name — first child is typically the identifier or exprDot
  2203. const firstChild = node.namedChild(0);
  2204. if (!firstChild) return;
  2205. let calleeName = '';
  2206. if (firstChild.type === 'exprDot') {
  2207. // Qualified call: Obj.Method(...)
  2208. const identifiers = firstChild.namedChildren.filter(
  2209. (c: SyntaxNode) => c.type === 'identifier'
  2210. );
  2211. if (identifiers.length > 0) {
  2212. calleeName = identifiers.map((id: SyntaxNode) => getNodeText(id, this.source)).join('.');
  2213. }
  2214. } else if (firstChild.type === 'identifier') {
  2215. calleeName = getNodeText(firstChild, this.source);
  2216. }
  2217. if (calleeName) {
  2218. this.unresolvedReferences.push({
  2219. fromNodeId: callerId,
  2220. referenceName: calleeName,
  2221. referenceKind: 'calls',
  2222. line: node.startPosition.row + 1,
  2223. column: node.startPosition.column,
  2224. });
  2225. }
  2226. // Also visit arguments for nested calls
  2227. const args = node.namedChildren.find(
  2228. (c: SyntaxNode) => c.type === 'exprArgs'
  2229. );
  2230. if (args) {
  2231. this.visitPascalBlock(args);
  2232. }
  2233. }
  2234. /**
  2235. * Recursively visit a Pascal block/statement tree for call expressions
  2236. */
  2237. private visitPascalBlock(node: SyntaxNode): void {
  2238. for (let i = 0; i < node.namedChildCount; i++) {
  2239. const child = node.namedChild(i);
  2240. if (!child) continue;
  2241. if (child.type === 'exprCall') {
  2242. this.extractPascalCall(child);
  2243. } else if (child.type === 'exprDot') {
  2244. // Check if exprDot contains an exprCall
  2245. for (let j = 0; j < child.namedChildCount; j++) {
  2246. const grandchild = child.namedChild(j);
  2247. if (grandchild?.type === 'exprCall') {
  2248. this.extractPascalCall(grandchild);
  2249. }
  2250. }
  2251. } else {
  2252. this.visitPascalBlock(child);
  2253. }
  2254. }
  2255. }
  2256. }
  2257. /**
  2258. * Extract nodes and edges from source code.
  2259. *
  2260. * If `frameworkNames` is provided, framework-specific extractors matching
  2261. * those names and the file's language are run after the tree-sitter pass.
  2262. * Their nodes/references/errors are merged into the returned result.
  2263. */
  2264. export function extractFromSource(
  2265. filePath: string,
  2266. source: string,
  2267. language?: Language,
  2268. frameworkNames?: string[]
  2269. ): ExtractionResult {
  2270. const detectedLanguage = language || detectLanguage(filePath, source);
  2271. const fileExtension = path.extname(filePath).toLowerCase();
  2272. let result: ExtractionResult;
  2273. // Use custom extractor for Svelte
  2274. if (detectedLanguage === 'svelte') {
  2275. const extractor = new SvelteExtractor(filePath, source);
  2276. result = extractor.extract();
  2277. } else if (detectedLanguage === 'vue') {
  2278. // Use custom extractor for Vue
  2279. const extractor = new VueExtractor(filePath, source);
  2280. result = extractor.extract();
  2281. } else if (detectedLanguage === 'liquid') {
  2282. // Use custom extractor for Liquid
  2283. const extractor = new LiquidExtractor(filePath, source);
  2284. result = extractor.extract();
  2285. } else if (
  2286. detectedLanguage === 'pascal' &&
  2287. (fileExtension === '.dfm' || fileExtension === '.fmx')
  2288. ) {
  2289. // Use custom extractor for DFM/FMX form files
  2290. const extractor = new DfmExtractor(filePath, source);
  2291. result = extractor.extract();
  2292. } else {
  2293. const extractor = new TreeSitterExtractor(filePath, source, detectedLanguage);
  2294. result = extractor.extract();
  2295. }
  2296. // Framework-specific extraction (routes, middleware, etc.)
  2297. if (frameworkNames && frameworkNames.length > 0) {
  2298. const allResolvers = getAllFrameworkResolvers();
  2299. const applicable = getApplicableFrameworks(
  2300. allResolvers.filter((r) => frameworkNames.includes(r.name)),
  2301. detectedLanguage
  2302. );
  2303. for (const fw of applicable) {
  2304. if (!fw.extract) continue;
  2305. try {
  2306. const fwResult = fw.extract(filePath, source);
  2307. result.nodes.push(...fwResult.nodes);
  2308. result.unresolvedReferences.push(...fwResult.references);
  2309. } catch (err) {
  2310. result.errors.push({
  2311. message: `Framework extractor '${fw.name}' failed: ${
  2312. err instanceof Error ? err.message : String(err)
  2313. }`,
  2314. filePath,
  2315. severity: 'warning',
  2316. });
  2317. }
  2318. }
  2319. }
  2320. return result;
  2321. }