tree-sitter.ts 65 KB

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