tree-sitter.ts 96 KB

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