tree-sitter.ts 83 KB

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