tree-sitter.ts 81 KB

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