tree-sitter.ts 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767
  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. // Get names from the node stack
  343. const parts: string[] = [this.filePath];
  344. for (const nodeId of this.nodeStack) {
  345. const node = this.nodes.find((n) => n.id === nodeId);
  346. if (node) {
  347. parts.push(node.name);
  348. }
  349. }
  350. parts.push(name);
  351. return parts.join('::');
  352. }
  353. /**
  354. * Check if the current node stack indicates we are inside a class-like node
  355. * (class, struct, interface, trait). File nodes do not count as class-like.
  356. */
  357. private isInsideClassLikeNode(): boolean {
  358. if (this.nodeStack.length === 0) return false;
  359. const parentId = this.nodeStack[this.nodeStack.length - 1];
  360. if (!parentId) return false;
  361. const parentNode = this.nodes.find((n) => n.id === parentId);
  362. if (!parentNode) return false;
  363. return (
  364. parentNode.kind === 'class' ||
  365. parentNode.kind === 'struct' ||
  366. parentNode.kind === 'interface' ||
  367. parentNode.kind === 'trait' ||
  368. parentNode.kind === 'enum'
  369. );
  370. }
  371. /**
  372. * Extract a function
  373. */
  374. private extractFunction(node: SyntaxNode): void {
  375. if (!this.extractor) return;
  376. let name = extractName(node, this.source, this.extractor);
  377. // For arrow functions and function expressions assigned to variables,
  378. // resolve the name from the parent variable_declarator.
  379. // e.g. `export const useAuth = () => { ... }` — the arrow_function node
  380. // has no `name` field; the name lives on the variable_declarator.
  381. if (
  382. name === '<anonymous>' &&
  383. (node.type === 'arrow_function' || node.type === 'function_expression')
  384. ) {
  385. const parent = node.parent;
  386. if (parent?.type === 'variable_declarator') {
  387. const varName = getChildByField(parent, 'name');
  388. if (varName) {
  389. name = getNodeText(varName, this.source);
  390. }
  391. }
  392. }
  393. if (name === '<anonymous>') return; // Skip anonymous functions
  394. const docstring = getPrecedingDocstring(node, this.source);
  395. const signature = this.extractor.getSignature?.(node, this.source);
  396. const visibility = this.extractor.getVisibility?.(node);
  397. const isExported = this.extractor.isExported?.(node, this.source);
  398. const isAsync = this.extractor.isAsync?.(node);
  399. const isStatic = this.extractor.isStatic?.(node);
  400. const funcNode = this.createNode('function', name, node, {
  401. docstring,
  402. signature,
  403. visibility,
  404. isExported,
  405. isAsync,
  406. isStatic,
  407. });
  408. if (!funcNode) return;
  409. // Extract type annotations (parameter types and return type)
  410. this.extractTypeAnnotations(node, funcNode.id);
  411. // Push to stack and visit body
  412. this.nodeStack.push(funcNode.id);
  413. const body = this.extractor.resolveBody?.(node, this.extractor.bodyField)
  414. ?? getChildByField(node, this.extractor.bodyField);
  415. if (body) {
  416. this.visitFunctionBody(body, funcNode.id);
  417. }
  418. this.nodeStack.pop();
  419. }
  420. /**
  421. * Extract a class
  422. */
  423. private extractClass(node: SyntaxNode): void {
  424. if (!this.extractor) return;
  425. const name = extractName(node, this.source, this.extractor);
  426. const docstring = getPrecedingDocstring(node, this.source);
  427. const visibility = this.extractor.getVisibility?.(node);
  428. const isExported = this.extractor.isExported?.(node, this.source);
  429. const classNode = this.createNode('class', name, node, {
  430. docstring,
  431. visibility,
  432. isExported,
  433. });
  434. if (!classNode) return;
  435. // Extract extends/implements
  436. this.extractInheritance(node, classNode.id);
  437. // Push to stack and visit body
  438. this.nodeStack.push(classNode.id);
  439. let body = this.extractor.resolveBody?.(node, this.extractor.bodyField)
  440. ?? getChildByField(node, this.extractor.bodyField);
  441. if (!body) body = node;
  442. // Visit all children for methods and properties
  443. for (let i = 0; i < body.namedChildCount; i++) {
  444. const child = body.namedChild(i);
  445. if (child) {
  446. this.visitNode(child);
  447. }
  448. }
  449. this.nodeStack.pop();
  450. }
  451. /**
  452. * Extract a method
  453. */
  454. private extractMethod(node: SyntaxNode): void {
  455. if (!this.extractor) return;
  456. // For most languages, only extract as method if inside a class-like node
  457. // Languages with methodsAreTopLevel (e.g. Go) always treat them as methods
  458. if (!this.isInsideClassLikeNode() && !this.extractor.methodsAreTopLevel) {
  459. // Not inside a class-like node and not Go, treat as function
  460. this.extractFunction(node);
  461. return;
  462. }
  463. const name = extractName(node, this.source, this.extractor);
  464. const docstring = getPrecedingDocstring(node, this.source);
  465. const signature = this.extractor.getSignature?.(node, this.source);
  466. const visibility = this.extractor.getVisibility?.(node);
  467. const isAsync = this.extractor.isAsync?.(node);
  468. const isStatic = this.extractor.isStatic?.(node);
  469. // For languages with receiver types (Go), include receiver in qualified name
  470. // so FTS can match "scrapeLoop.run" → qualified_name "...::scrapeLoop::run"
  471. const receiverType = this.extractor.getReceiverType?.(node, this.source);
  472. const extraProps: Partial<Node> = {
  473. docstring,
  474. signature,
  475. visibility,
  476. isAsync,
  477. isStatic,
  478. };
  479. if (receiverType) {
  480. extraProps.qualifiedName = `${this.filePath}::${receiverType}::${name}`;
  481. }
  482. const methodNode = this.createNode('method', name, node, extraProps);
  483. if (!methodNode) return;
  484. // Extract type annotations (parameter types and return type)
  485. this.extractTypeAnnotations(node, methodNode.id);
  486. // Push to stack and visit body
  487. this.nodeStack.push(methodNode.id);
  488. const body = this.extractor.resolveBody?.(node, this.extractor.bodyField)
  489. ?? getChildByField(node, this.extractor.bodyField);
  490. if (body) {
  491. this.visitFunctionBody(body, methodNode.id);
  492. }
  493. this.nodeStack.pop();
  494. }
  495. /**
  496. * Extract an interface/protocol/trait
  497. */
  498. private extractInterface(node: SyntaxNode): void {
  499. if (!this.extractor) return;
  500. const name = extractName(node, this.source, this.extractor);
  501. const docstring = getPrecedingDocstring(node, this.source);
  502. const isExported = this.extractor.isExported?.(node, this.source);
  503. const kind: NodeKind = this.extractor.interfaceKind ?? 'interface';
  504. const interfaceNode = this.createNode(kind, name, node, {
  505. docstring,
  506. isExported,
  507. });
  508. if (!interfaceNode) return;
  509. // Extract extends (interface inheritance)
  510. this.extractInheritance(node, interfaceNode.id);
  511. }
  512. /**
  513. * Extract a struct
  514. */
  515. private extractStruct(node: SyntaxNode): void {
  516. if (!this.extractor) return;
  517. const name = extractName(node, this.source, this.extractor);
  518. const docstring = getPrecedingDocstring(node, this.source);
  519. const visibility = this.extractor.getVisibility?.(node);
  520. const isExported = this.extractor.isExported?.(node, this.source);
  521. const structNode = this.createNode('struct', name, node, {
  522. docstring,
  523. visibility,
  524. isExported,
  525. });
  526. if (!structNode) return;
  527. // Push to stack for field extraction
  528. this.nodeStack.push(structNode.id);
  529. const body = getChildByField(node, this.extractor.bodyField) || node;
  530. for (let i = 0; i < body.namedChildCount; i++) {
  531. const child = body.namedChild(i);
  532. if (child) {
  533. this.visitNode(child);
  534. }
  535. }
  536. this.nodeStack.pop();
  537. }
  538. /**
  539. * Extract an enum
  540. */
  541. private extractEnum(node: SyntaxNode): void {
  542. if (!this.extractor) return;
  543. const name = extractName(node, this.source, this.extractor);
  544. const docstring = getPrecedingDocstring(node, this.source);
  545. const visibility = this.extractor.getVisibility?.(node);
  546. const isExported = this.extractor.isExported?.(node, this.source);
  547. const enumNode = this.createNode('enum', name, node, {
  548. docstring,
  549. visibility,
  550. isExported,
  551. });
  552. if (!enumNode) return;
  553. // Extract inheritance (e.g. Swift: enum AFError: Error)
  554. this.extractInheritance(node, enumNode.id);
  555. // Push to stack and visit body children (enum members, nested types, methods)
  556. this.nodeStack.push(enumNode.id);
  557. const body = this.extractor.resolveBody?.(node, this.extractor.bodyField)
  558. ?? getChildByField(node, this.extractor.bodyField)
  559. ?? node;
  560. const memberTypes = this.extractor.enumMemberTypes;
  561. for (let i = 0; i < body.namedChildCount; i++) {
  562. const child = body.namedChild(i);
  563. if (!child) continue;
  564. if (memberTypes?.includes(child.type)) {
  565. this.extractEnumMembers(child);
  566. } else {
  567. this.visitNode(child);
  568. }
  569. }
  570. this.nodeStack.pop();
  571. }
  572. /**
  573. * Extract enum member names from an enum member node.
  574. * Handles multi-case declarations (Swift: `case put, delete`) and single-case patterns.
  575. */
  576. private extractEnumMembers(node: SyntaxNode): void {
  577. // Try field-based name first (e.g. Rust enum_variant has a 'name' field)
  578. const nameNode = getChildByField(node, 'name');
  579. if (nameNode) {
  580. this.createNode('enum_member', getNodeText(nameNode, this.source), node);
  581. return;
  582. }
  583. // Check for identifier-like children (Swift: simple_identifier, TS: property_identifier)
  584. let found = false;
  585. for (let i = 0; i < node.namedChildCount; i++) {
  586. const child = node.namedChild(i);
  587. if (child && (child.type === 'simple_identifier' || child.type === 'identifier' || child.type === 'property_identifier')) {
  588. this.createNode('enum_member', getNodeText(child, this.source), child);
  589. found = true;
  590. }
  591. }
  592. // If the node itself IS the identifier (e.g. TS property_identifier directly in enum body)
  593. if (!found && node.namedChildCount === 0) {
  594. this.createNode('enum_member', getNodeText(node, this.source), node);
  595. }
  596. }
  597. /**
  598. * Extract a class field declaration (e.g. Java field_declaration, C# field_declaration).
  599. * Extracts each declarator as a 'field' kind node inside the owning class.
  600. */
  601. private extractField(node: SyntaxNode): void {
  602. if (!this.extractor) return;
  603. const docstring = getPrecedingDocstring(node, this.source);
  604. const visibility = this.extractor.getVisibility?.(node);
  605. const isStatic = this.extractor.isStatic?.(node) ?? false;
  606. // Java field_declaration: "private final String name = value;"
  607. // Children include modifiers, type, variable_declarator(s)
  608. const declarators = node.namedChildren.filter(
  609. c => c.type === 'variable_declarator'
  610. );
  611. if (declarators.length > 0) {
  612. // Get field type from the type child
  613. const typeNode = node.namedChildren.find(
  614. c => c.type !== 'modifiers' && c.type !== 'variable_declarator'
  615. && c.type !== 'marker_annotation' && c.type !== 'annotation'
  616. );
  617. const typeText = typeNode ? getNodeText(typeNode, this.source) : undefined;
  618. for (const decl of declarators) {
  619. const nameNode = getChildByField(decl, 'name');
  620. if (!nameNode) continue;
  621. const name = getNodeText(nameNode, this.source);
  622. const signature = typeText ? `${typeText} ${name}` : name;
  623. this.createNode('field', name, decl, {
  624. docstring,
  625. signature,
  626. visibility,
  627. isStatic,
  628. });
  629. }
  630. } else {
  631. // Fallback: try to find an identifier child directly
  632. const nameNode = getChildByField(node, 'name')
  633. || node.namedChildren.find(c => c.type === 'identifier');
  634. if (nameNode) {
  635. const name = getNodeText(nameNode, this.source);
  636. this.createNode('field', name, node, {
  637. docstring,
  638. visibility,
  639. isStatic,
  640. });
  641. }
  642. }
  643. }
  644. /**
  645. * Extract a variable declaration (const, let, var, etc.)
  646. *
  647. * Extracts top-level and module-level variable declarations.
  648. * Captures the variable name and first 100 chars of initializer in signature for searchability.
  649. */
  650. private extractVariable(node: SyntaxNode): void {
  651. if (!this.extractor) return;
  652. // Different languages have different variable declaration structures
  653. // TypeScript/JavaScript: lexical_declaration contains variable_declarator children
  654. // Python: assignment has left (identifier) and right (value)
  655. // Go: var_declaration, short_var_declaration, const_declaration
  656. const isConst = this.extractor.isConst?.(node) ?? false;
  657. const kind: NodeKind = isConst ? 'constant' : 'variable';
  658. const docstring = getPrecedingDocstring(node, this.source);
  659. const isExported = this.extractor.isExported?.(node, this.source) ?? false;
  660. // Extract variable declarators based on language
  661. if (this.language === 'typescript' || this.language === 'javascript' ||
  662. this.language === 'tsx' || this.language === 'jsx') {
  663. // Handle lexical_declaration and variable_declaration
  664. // These contain one or more variable_declarator children
  665. for (let i = 0; i < node.namedChildCount; i++) {
  666. const child = node.namedChild(i);
  667. if (child?.type === 'variable_declarator') {
  668. const nameNode = getChildByField(child, 'name');
  669. const valueNode = getChildByField(child, 'value');
  670. if (nameNode) {
  671. const name = getNodeText(nameNode, this.source);
  672. // Arrow functions / function expressions: extract as function instead of variable
  673. if (valueNode && (valueNode.type === 'arrow_function' || valueNode.type === 'function_expression')) {
  674. this.extractFunction(valueNode);
  675. continue;
  676. }
  677. // Capture first 100 chars of initializer for context (stored in signature for searchability)
  678. const initValue = valueNode ? getNodeText(valueNode, this.source).slice(0, 100) : undefined;
  679. const initSignature = initValue ? `= ${initValue}${initValue.length >= 100 ? '...' : ''}` : undefined;
  680. const varNode = this.createNode(kind, name, child, {
  681. docstring,
  682. signature: initSignature,
  683. isExported,
  684. });
  685. // Extract type annotation references (e.g., const x: ITextModel = ...)
  686. if (varNode) {
  687. this.extractVariableTypeAnnotation(child, varNode.id);
  688. }
  689. }
  690. }
  691. }
  692. } else if (this.language === 'python' || this.language === 'ruby') {
  693. // Python/Ruby assignment: left = right
  694. const left = getChildByField(node, 'left') || node.namedChild(0);
  695. const right = getChildByField(node, 'right') || node.namedChild(1);
  696. if (left && left.type === 'identifier') {
  697. const name = getNodeText(left, this.source);
  698. // Skip if name starts with lowercase and looks like a function call result
  699. // Python constants are usually UPPER_CASE
  700. const initValue = right ? getNodeText(right, this.source).slice(0, 100) : undefined;
  701. const initSignature = initValue ? `= ${initValue}${initValue.length >= 100 ? '...' : ''}` : undefined;
  702. this.createNode(kind, name, node, {
  703. docstring,
  704. signature: initSignature,
  705. });
  706. }
  707. } else if (this.language === 'go') {
  708. // Go: var_declaration, short_var_declaration, const_declaration
  709. // These can have multiple identifiers on the left
  710. const specs = node.namedChildren.filter(c =>
  711. c.type === 'var_spec' || c.type === 'const_spec'
  712. );
  713. for (const spec of specs) {
  714. const nameNode = spec.namedChild(0);
  715. if (nameNode && nameNode.type === 'identifier') {
  716. const name = getNodeText(nameNode, this.source);
  717. const valueNode = spec.namedChildCount > 1 ? spec.namedChild(spec.namedChildCount - 1) : null;
  718. const initValue = valueNode ? getNodeText(valueNode, this.source).slice(0, 100) : undefined;
  719. const initSignature = initValue ? `= ${initValue}${initValue.length >= 100 ? '...' : ''}` : undefined;
  720. this.createNode(node.type === 'const_declaration' ? 'constant' : 'variable', name, spec, {
  721. docstring,
  722. signature: initSignature,
  723. });
  724. }
  725. }
  726. // Handle short_var_declaration (:=)
  727. if (node.type === 'short_var_declaration') {
  728. const left = getChildByField(node, 'left');
  729. const right = getChildByField(node, 'right');
  730. if (left) {
  731. // Can be expression_list with multiple identifiers
  732. const identifiers = left.type === 'expression_list'
  733. ? left.namedChildren.filter(c => c.type === 'identifier')
  734. : [left];
  735. for (const id of identifiers) {
  736. const name = getNodeText(id, this.source);
  737. const initValue = right ? getNodeText(right, this.source).slice(0, 100) : undefined;
  738. const initSignature = initValue ? `= ${initValue}${initValue.length >= 100 ? '...' : ''}` : undefined;
  739. this.createNode('variable', name, node, {
  740. docstring,
  741. signature: initSignature,
  742. });
  743. }
  744. }
  745. }
  746. } else {
  747. // Generic fallback for other languages
  748. // Try to find identifier children
  749. for (let i = 0; i < node.namedChildCount; i++) {
  750. const child = node.namedChild(i);
  751. if (child?.type === 'identifier' || child?.type === 'variable_declarator') {
  752. const name = child.type === 'identifier'
  753. ? getNodeText(child, this.source)
  754. : extractName(child, this.source, this.extractor);
  755. if (name && name !== '<anonymous>') {
  756. this.createNode(kind, name, child, {
  757. docstring,
  758. isExported,
  759. });
  760. }
  761. }
  762. }
  763. }
  764. }
  765. /**
  766. * Extract a type alias (e.g. `export type X = ...` in TypeScript).
  767. * For languages like Go, resolveTypeAliasKind detects when the type_spec
  768. * wraps a struct or interface definition and creates the correct node kind.
  769. * Returns true if children should be skipped (struct/interface handled body visiting).
  770. */
  771. private extractTypeAlias(node: SyntaxNode): boolean {
  772. if (!this.extractor) return false;
  773. const name = extractName(node, this.source, this.extractor);
  774. if (name === '<anonymous>') return false;
  775. const docstring = getPrecedingDocstring(node, this.source);
  776. const isExported = this.extractor.isExported?.(node, this.source);
  777. // Check if this type alias is actually a struct or interface definition
  778. // (e.g. Go: `type Foo struct { ... }` is a type_spec wrapping struct_type)
  779. const resolvedKind = this.extractor.resolveTypeAliasKind?.(node, this.source);
  780. if (resolvedKind === 'struct') {
  781. const structNode = this.createNode('struct', name, node, { docstring, isExported });
  782. if (!structNode) return true;
  783. // Visit body children for field extraction
  784. this.nodeStack.push(structNode.id);
  785. const typeChild = getChildByField(node, 'type');
  786. if (typeChild) {
  787. const body = getChildByField(typeChild, this.extractor.bodyField) || typeChild;
  788. for (let i = 0; i < body.namedChildCount; i++) {
  789. const child = body.namedChild(i);
  790. if (child) this.visitNode(child);
  791. }
  792. }
  793. this.nodeStack.pop();
  794. return true;
  795. }
  796. if (resolvedKind === 'interface') {
  797. const kind: NodeKind = this.extractor.interfaceKind ?? 'interface';
  798. const interfaceNode = this.createNode(kind, name, node, { docstring, isExported });
  799. if (!interfaceNode) return true;
  800. // Extract interface inheritance from the inner type node
  801. const typeChild = getChildByField(node, 'type');
  802. if (typeChild) this.extractInheritance(typeChild, interfaceNode.id);
  803. return true;
  804. }
  805. const typeAliasNode = this.createNode('type_alias', name, node, {
  806. docstring,
  807. isExported,
  808. });
  809. // Extract type references from the alias value (e.g., `type X = ITextModel | null`)
  810. if (typeAliasNode && this.TYPE_ANNOTATION_LANGUAGES.has(this.language)) {
  811. // The value is everything after the `=`, which is typically the last named child
  812. // In tree-sitter TS: type_alias_declaration has name + value children
  813. const value = getChildByField(node, 'value');
  814. if (value) {
  815. this.extractTypeRefsFromSubtree(value, typeAliasNode.id);
  816. }
  817. }
  818. return false;
  819. }
  820. /**
  821. * Extract an exported variable declaration that isn't a function.
  822. * Handles patterns like:
  823. * export const X = create(...)
  824. * export const X = { ... }
  825. * export const X = [...]
  826. * export const X = "value"
  827. *
  828. * This is called for `export_statement` nodes that contain a
  829. * `lexical_declaration` with `variable_declarator` children whose
  830. * values are NOT already handled by functionTypes (arrow_function,
  831. * function_expression).
  832. */
  833. private extractExportedVariables(exportNode: SyntaxNode): void {
  834. if (!this.extractor) return;
  835. // Find the lexical_declaration or variable_declaration child
  836. for (let i = 0; i < exportNode.namedChildCount; i++) {
  837. const decl = exportNode.namedChild(i);
  838. if (!decl || (decl.type !== 'lexical_declaration' && decl.type !== 'variable_declaration')) {
  839. continue;
  840. }
  841. // Iterate over each variable_declarator in the declaration
  842. for (let j = 0; j < decl.namedChildCount; j++) {
  843. const declarator = decl.namedChild(j);
  844. if (!declarator || declarator.type !== 'variable_declarator') continue;
  845. const nameNode = getChildByField(declarator, 'name');
  846. if (!nameNode) continue;
  847. const name = getNodeText(nameNode, this.source);
  848. // Skip if the value is a function type — those are already handled
  849. // by extractFunction via the functionTypes dispatch
  850. const value = getChildByField(declarator, 'value');
  851. if (value) {
  852. const valueType = value.type;
  853. if (
  854. this.extractor.functionTypes.includes(valueType)
  855. ) {
  856. continue; // Already handled by extractFunction
  857. }
  858. }
  859. const docstring = getPrecedingDocstring(exportNode, this.source);
  860. this.createNode('variable', name, declarator, {
  861. docstring,
  862. isExported: true,
  863. });
  864. }
  865. }
  866. }
  867. /**
  868. * Extract an import
  869. *
  870. * Creates an import node with the full import statement stored in signature for searchability.
  871. * Also creates unresolved references for resolution purposes.
  872. */
  873. private extractImport(node: SyntaxNode): void {
  874. if (!this.extractor) return;
  875. const importText = getNodeText(node, this.source).trim();
  876. // Try language-specific hook first
  877. if (this.extractor.extractImport) {
  878. const info = this.extractor.extractImport(node, this.source);
  879. if (info) {
  880. this.createNode('import', info.moduleName, node, {
  881. signature: info.signature,
  882. });
  883. // Create unresolved reference unless the hook handled it
  884. if (!info.handledRefs && info.moduleName && this.nodeStack.length > 0) {
  885. const parentId = this.nodeStack[this.nodeStack.length - 1];
  886. if (parentId) {
  887. this.unresolvedReferences.push({
  888. fromNodeId: parentId,
  889. referenceName: info.moduleName,
  890. referenceKind: 'imports',
  891. line: node.startPosition.row + 1,
  892. column: node.startPosition.column,
  893. });
  894. }
  895. }
  896. return;
  897. }
  898. // Hook returned null — fall through to multi-import inline handlers only
  899. // (hook returning null means "I didn't handle this" for multi-import cases,
  900. // NOT "use generic fallback" — the hook already declined)
  901. }
  902. // Multi-import cases that create multiple nodes (can't be expressed with single-return hook)
  903. // Python import_statement: import os, sys (creates one import per module)
  904. if (this.language === 'python' && node.type === 'import_statement') {
  905. for (let i = 0; i < node.namedChildCount; i++) {
  906. const child = node.namedChild(i);
  907. if (child?.type === 'dotted_name') {
  908. this.createNode('import', getNodeText(child, this.source), node, {
  909. signature: importText,
  910. });
  911. } else if (child?.type === 'aliased_import') {
  912. const dottedName = child.namedChildren.find(c => c.type === 'dotted_name');
  913. if (dottedName) {
  914. this.createNode('import', getNodeText(dottedName, this.source), node, {
  915. signature: importText,
  916. });
  917. }
  918. }
  919. }
  920. return;
  921. }
  922. // Go imports: single or grouped (creates one import per spec)
  923. if (this.language === 'go') {
  924. const extractFromSpec = (spec: SyntaxNode): void => {
  925. const stringLiteral = spec.namedChildren.find(c => c.type === 'interpreted_string_literal');
  926. if (stringLiteral) {
  927. const importPath = getNodeText(stringLiteral, this.source).replace(/['"]/g, '');
  928. if (importPath) {
  929. this.createNode('import', importPath, spec, {
  930. signature: getNodeText(spec, this.source).trim(),
  931. });
  932. }
  933. }
  934. };
  935. const importSpecList = node.namedChildren.find(c => c.type === 'import_spec_list');
  936. if (importSpecList) {
  937. for (const spec of importSpecList.namedChildren.filter(c => c.type === 'import_spec')) {
  938. extractFromSpec(spec);
  939. }
  940. } else {
  941. const importSpec = node.namedChildren.find(c => c.type === 'import_spec');
  942. if (importSpec) {
  943. extractFromSpec(importSpec);
  944. }
  945. }
  946. return;
  947. }
  948. // PHP grouped imports: use X\{A, B} (creates one import per item)
  949. if (this.language === 'php') {
  950. const namespacePrefix = node.namedChildren.find(c => c.type === 'namespace_name');
  951. const useGroup = node.namedChildren.find(c => c.type === 'namespace_use_group');
  952. if (namespacePrefix && useGroup) {
  953. const prefix = getNodeText(namespacePrefix, this.source);
  954. const useClauses = useGroup.namedChildren.filter((c: SyntaxNode) =>
  955. c.type === 'namespace_use_group_clause' || c.type === 'namespace_use_clause'
  956. );
  957. for (const clause of useClauses) {
  958. const nsName = clause.namedChildren.find((c: SyntaxNode) => c.type === 'namespace_name');
  959. const name = nsName
  960. ? nsName.namedChildren.find((c: SyntaxNode) => c.type === 'name')
  961. : clause.namedChildren.find((c: SyntaxNode) => c.type === 'name');
  962. if (name) {
  963. const fullPath = `${prefix}\\${getNodeText(name, this.source)}`;
  964. this.createNode('import', fullPath, node, {
  965. signature: importText,
  966. });
  967. }
  968. }
  969. return;
  970. }
  971. }
  972. // If a hook exists but returned null, it intentionally declined this node — don't create fallback
  973. if (this.extractor.extractImport) return;
  974. // Generic fallback for languages without hooks
  975. this.createNode('import', importText, node, {
  976. signature: importText,
  977. });
  978. }
  979. /**
  980. * Extract a function call
  981. */
  982. private extractCall(node: SyntaxNode): void {
  983. if (this.nodeStack.length === 0) return;
  984. const callerId = this.nodeStack[this.nodeStack.length - 1];
  985. if (!callerId) return;
  986. // Get the function/method being called
  987. let calleeName = '';
  988. // Java/Kotlin method_invocation has 'object' + 'name' fields instead of 'function'
  989. const nameField = getChildByField(node, 'name');
  990. const objectField = getChildByField(node, 'object');
  991. if (nameField && objectField && node.type === 'method_invocation') {
  992. // Java-style method call: receiver.method()
  993. const methodName = getNodeText(nameField, this.source);
  994. const receiverName = getNodeText(objectField, this.source);
  995. if (methodName) {
  996. // Emit receiver.method form for qualified resolution
  997. calleeName = `${receiverName}.${methodName}`;
  998. }
  999. } else {
  1000. const func = getChildByField(node, 'function') || node.namedChild(0);
  1001. if (func) {
  1002. if (func.type === 'member_expression' || func.type === 'attribute') {
  1003. // Method call: obj.method()
  1004. const property = getChildByField(func, 'property') || func.namedChild(1);
  1005. if (property) {
  1006. calleeName = getNodeText(property, this.source);
  1007. }
  1008. } else if (func.type === 'scoped_identifier' || func.type === 'scoped_call_expression') {
  1009. // Scoped call: Module::function()
  1010. calleeName = getNodeText(func, this.source);
  1011. } else {
  1012. calleeName = getNodeText(func, this.source);
  1013. }
  1014. }
  1015. }
  1016. if (calleeName) {
  1017. this.unresolvedReferences.push({
  1018. fromNodeId: callerId,
  1019. referenceName: calleeName,
  1020. referenceKind: 'calls',
  1021. line: node.startPosition.row + 1,
  1022. column: node.startPosition.column,
  1023. });
  1024. }
  1025. }
  1026. /**
  1027. * Visit function body and extract calls
  1028. */
  1029. private visitFunctionBody(body: SyntaxNode, _functionId: string): void {
  1030. if (!this.extractor) return;
  1031. // Recursively find all call expressions
  1032. const visitForCalls = (node: SyntaxNode): void => {
  1033. if (this.extractor!.callTypes.includes(node.type)) {
  1034. this.extractCall(node);
  1035. }
  1036. for (let i = 0; i < node.namedChildCount; i++) {
  1037. const child = node.namedChild(i);
  1038. if (child) {
  1039. visitForCalls(child);
  1040. }
  1041. }
  1042. };
  1043. visitForCalls(body);
  1044. }
  1045. /**
  1046. * Extract inheritance relationships
  1047. */
  1048. private extractInheritance(node: SyntaxNode, classId: string): void {
  1049. // Look for extends/implements clauses
  1050. for (let i = 0; i < node.namedChildCount; i++) {
  1051. const child = node.namedChild(i);
  1052. if (!child) continue;
  1053. if (
  1054. child.type === 'extends_clause' ||
  1055. child.type === 'class_heritage' ||
  1056. child.type === 'superclass' ||
  1057. child.type === 'extends_interfaces' // Java interface extends
  1058. ) {
  1059. // Extract parent class/interface names
  1060. // Java uses type_list wrapper: superclass -> type_identifier, extends_interfaces -> type_list -> type_identifier
  1061. const typeList = child.namedChildren.find((c: SyntaxNode) => c.type === 'type_list');
  1062. const targets = typeList ? typeList.namedChildren : [child.namedChild(0)];
  1063. for (const target of targets) {
  1064. if (target) {
  1065. const name = getNodeText(target, this.source);
  1066. this.unresolvedReferences.push({
  1067. fromNodeId: classId,
  1068. referenceName: name,
  1069. referenceKind: 'extends',
  1070. line: target.startPosition.row + 1,
  1071. column: target.startPosition.column,
  1072. });
  1073. }
  1074. }
  1075. }
  1076. if (
  1077. child.type === 'implements_clause' ||
  1078. child.type === 'class_interface_clause' ||
  1079. child.type === 'super_interfaces' || // Java class implements
  1080. child.type === 'interfaces' // Dart
  1081. ) {
  1082. // Extract implemented interfaces
  1083. // Java uses type_list wrapper: super_interfaces -> type_list -> type_identifier
  1084. const typeList = child.namedChildren.find((c: SyntaxNode) => c.type === 'type_list');
  1085. const targets = typeList ? typeList.namedChildren : child.namedChildren;
  1086. for (const iface of targets) {
  1087. if (iface) {
  1088. const name = getNodeText(iface, this.source);
  1089. this.unresolvedReferences.push({
  1090. fromNodeId: classId,
  1091. referenceName: name,
  1092. referenceKind: 'implements',
  1093. line: iface.startPosition.row + 1,
  1094. column: iface.startPosition.column,
  1095. });
  1096. }
  1097. }
  1098. }
  1099. }
  1100. }
  1101. /**
  1102. * Languages that support type annotations (TypeScript, etc.)
  1103. */
  1104. private readonly TYPE_ANNOTATION_LANGUAGES = new Set([
  1105. 'typescript', 'tsx', 'dart', 'kotlin', 'swift', 'rust', 'go', 'java', 'csharp',
  1106. ]);
  1107. /**
  1108. * Built-in/primitive type names that shouldn't create references
  1109. */
  1110. private readonly BUILTIN_TYPES = new Set([
  1111. 'string', 'number', 'boolean', 'void', 'null', 'undefined', 'never', 'any', 'unknown',
  1112. 'object', 'symbol', 'bigint', 'true', 'false',
  1113. // Rust
  1114. 'str', 'bool', 'i8', 'i16', 'i32', 'i64', 'i128', 'isize',
  1115. 'u8', 'u16', 'u32', 'u64', 'u128', 'usize', 'f32', 'f64', 'char',
  1116. // Java/C#
  1117. 'int', 'long', 'short', 'byte', 'float', 'double', 'char',
  1118. // Go
  1119. 'int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64',
  1120. 'float32', 'float64', 'complex64', 'complex128', 'rune', 'error',
  1121. ]);
  1122. /**
  1123. * Extract type references from type annotations on a function/method/field node.
  1124. * Creates 'references' edges for parameter types, return types, and field types.
  1125. */
  1126. private extractTypeAnnotations(node: SyntaxNode, nodeId: string): void {
  1127. if (!this.extractor) return;
  1128. if (!this.TYPE_ANNOTATION_LANGUAGES.has(this.language)) return;
  1129. // Extract parameter type annotations
  1130. const params = getChildByField(node, this.extractor.paramsField || 'parameters');
  1131. if (params) {
  1132. this.extractTypeRefsFromSubtree(params, nodeId);
  1133. }
  1134. // Extract return type annotation
  1135. const returnType = getChildByField(node, this.extractor.returnField || 'return_type');
  1136. if (returnType) {
  1137. this.extractTypeRefsFromSubtree(returnType, nodeId);
  1138. }
  1139. // Extract direct type annotation (for class fields like `model: ITextModel`)
  1140. const typeAnnotation = node.namedChildren.find(
  1141. (c: SyntaxNode) => c.type === 'type_annotation'
  1142. );
  1143. if (typeAnnotation) {
  1144. this.extractTypeRefsFromSubtree(typeAnnotation, nodeId);
  1145. }
  1146. }
  1147. /**
  1148. * Extract type references from a variable's type annotation.
  1149. */
  1150. private extractVariableTypeAnnotation(node: SyntaxNode, nodeId: string): void {
  1151. if (!this.TYPE_ANNOTATION_LANGUAGES.has(this.language)) return;
  1152. // Find type_annotation child (covers TS `: Type`, Rust `: Type`, etc.)
  1153. const typeAnnotation = node.namedChildren.find(
  1154. (c: SyntaxNode) => c.type === 'type_annotation'
  1155. );
  1156. if (typeAnnotation) {
  1157. this.extractTypeRefsFromSubtree(typeAnnotation, nodeId);
  1158. }
  1159. }
  1160. /**
  1161. * Recursively walk a subtree and extract all type_identifier references.
  1162. * Handles unions, intersections, generics, arrays, etc.
  1163. */
  1164. private extractTypeRefsFromSubtree(node: SyntaxNode, fromNodeId: string): void {
  1165. if (node.type === 'type_identifier') {
  1166. const typeName = getNodeText(node, this.source);
  1167. if (typeName && !this.BUILTIN_TYPES.has(typeName)) {
  1168. this.unresolvedReferences.push({
  1169. fromNodeId,
  1170. referenceName: typeName,
  1171. referenceKind: 'references',
  1172. line: node.startPosition.row + 1,
  1173. column: node.startPosition.column,
  1174. });
  1175. }
  1176. return; // type_identifier is a leaf
  1177. }
  1178. // Recurse into children (handles union_type, intersection_type, generic_type, etc.)
  1179. for (let i = 0; i < node.namedChildCount; i++) {
  1180. const child = node.namedChild(i);
  1181. if (child) {
  1182. this.extractTypeRefsFromSubtree(child, fromNodeId);
  1183. }
  1184. }
  1185. }
  1186. /**
  1187. * Handle Pascal-specific AST structures.
  1188. * Returns true if the node was fully handled and children should be skipped.
  1189. */
  1190. private visitPascalNode(node: SyntaxNode): boolean {
  1191. const nodeType = node.type;
  1192. // Unit/Program/Library → module node
  1193. if (nodeType === 'unit' || nodeType === 'program' || nodeType === 'library') {
  1194. const moduleNameNode = node.namedChildren.find(
  1195. (c: SyntaxNode) => c.type === 'moduleName'
  1196. );
  1197. const name = moduleNameNode ? getNodeText(moduleNameNode, this.source) : '';
  1198. // Fallback to filename without extension if module name is empty
  1199. const moduleName = name || path.basename(this.filePath).replace(/\.[^.]+$/, '');
  1200. this.createNode('module', moduleName, node);
  1201. // Continue visiting children (interface/implementation sections)
  1202. for (let i = 0; i < node.namedChildCount; i++) {
  1203. const child = node.namedChild(i);
  1204. if (child) this.visitNode(child);
  1205. }
  1206. return true;
  1207. }
  1208. // declType wraps declClass/declIntf/declEnum/type-alias
  1209. // The name lives on declType, the inner node determines the kind
  1210. if (nodeType === 'declType') {
  1211. this.extractPascalDeclType(node);
  1212. return true;
  1213. }
  1214. // declUses → import nodes for each unit name
  1215. if (nodeType === 'declUses') {
  1216. this.extractPascalUses(node);
  1217. return true;
  1218. }
  1219. // declConsts → container; visit children for individual declConst
  1220. if (nodeType === 'declConsts') {
  1221. for (let i = 0; i < node.namedChildCount; i++) {
  1222. const child = node.namedChild(i);
  1223. if (child?.type === 'declConst') {
  1224. this.extractPascalConst(child);
  1225. }
  1226. }
  1227. return true;
  1228. }
  1229. // declConst at top level (outside declConsts)
  1230. if (nodeType === 'declConst') {
  1231. this.extractPascalConst(node);
  1232. return true;
  1233. }
  1234. // declTypes → container for type declarations
  1235. if (nodeType === 'declTypes') {
  1236. for (let i = 0; i < node.namedChildCount; i++) {
  1237. const child = node.namedChild(i);
  1238. if (child) this.visitNode(child);
  1239. }
  1240. return true;
  1241. }
  1242. // declVars → container for variable declarations
  1243. if (nodeType === 'declVars') {
  1244. for (let i = 0; i < node.namedChildCount; i++) {
  1245. const child = node.namedChild(i);
  1246. if (child?.type === 'declVar') {
  1247. const nameNode = getChildByField(child, 'name');
  1248. if (nameNode) {
  1249. const name = getNodeText(nameNode, this.source);
  1250. this.createNode('variable', name, child);
  1251. }
  1252. }
  1253. }
  1254. return true;
  1255. }
  1256. // defProc in implementation section → extract calls but don't create duplicate nodes
  1257. if (nodeType === 'defProc') {
  1258. this.extractPascalDefProc(node);
  1259. return true;
  1260. }
  1261. // declProp → property node
  1262. if (nodeType === 'declProp') {
  1263. const nameNode = getChildByField(node, 'name');
  1264. if (nameNode) {
  1265. const name = getNodeText(nameNode, this.source);
  1266. const visibility = this.extractor!.getVisibility?.(node);
  1267. this.createNode('property', name, node, { visibility });
  1268. }
  1269. return true;
  1270. }
  1271. // declField → field node
  1272. if (nodeType === 'declField') {
  1273. const nameNode = getChildByField(node, 'name');
  1274. if (nameNode) {
  1275. const name = getNodeText(nameNode, this.source);
  1276. const visibility = this.extractor!.getVisibility?.(node);
  1277. this.createNode('field', name, node, { visibility });
  1278. }
  1279. return true;
  1280. }
  1281. // declSection → visit children (propagates visibility via getVisibility)
  1282. if (nodeType === 'declSection') {
  1283. for (let i = 0; i < node.namedChildCount; i++) {
  1284. const child = node.namedChild(i);
  1285. if (child) this.visitNode(child);
  1286. }
  1287. return true;
  1288. }
  1289. // exprCall → extract function call reference
  1290. if (nodeType === 'exprCall') {
  1291. this.extractPascalCall(node);
  1292. return true;
  1293. }
  1294. // interface/implementation sections → visit children
  1295. if (nodeType === 'interface' || nodeType === 'implementation') {
  1296. for (let i = 0; i < node.namedChildCount; i++) {
  1297. const child = node.namedChild(i);
  1298. if (child) this.visitNode(child);
  1299. }
  1300. return true;
  1301. }
  1302. // block (begin..end) → visit for calls
  1303. if (nodeType === 'block') {
  1304. this.visitPascalBlock(node);
  1305. return true;
  1306. }
  1307. return false;
  1308. }
  1309. /**
  1310. * Extract a Pascal declType node (class, interface, enum, or type alias)
  1311. */
  1312. private extractPascalDeclType(node: SyntaxNode): void {
  1313. const nameNode = getChildByField(node, 'name');
  1314. if (!nameNode) return;
  1315. const name = getNodeText(nameNode, this.source);
  1316. // Find the inner type declaration
  1317. const declClass = node.namedChildren.find(
  1318. (c: SyntaxNode) => c.type === 'declClass'
  1319. );
  1320. const declIntf = node.namedChildren.find(
  1321. (c: SyntaxNode) => c.type === 'declIntf'
  1322. );
  1323. const typeChild = node.namedChildren.find(
  1324. (c: SyntaxNode) => c.type === 'type'
  1325. );
  1326. if (declClass) {
  1327. const classNode = this.createNode('class', name, node);
  1328. if (classNode) {
  1329. // Extract inheritance from typeref children of declClass
  1330. this.extractPascalInheritance(declClass, classNode.id);
  1331. // Visit class body
  1332. this.nodeStack.push(classNode.id);
  1333. for (let i = 0; i < declClass.namedChildCount; i++) {
  1334. const child = declClass.namedChild(i);
  1335. if (child) this.visitNode(child);
  1336. }
  1337. this.nodeStack.pop();
  1338. }
  1339. } else if (declIntf) {
  1340. const ifaceNode = this.createNode('interface', name, node);
  1341. if (ifaceNode) {
  1342. // Visit interface members
  1343. this.nodeStack.push(ifaceNode.id);
  1344. for (let i = 0; i < declIntf.namedChildCount; i++) {
  1345. const child = declIntf.namedChild(i);
  1346. if (child) this.visitNode(child);
  1347. }
  1348. this.nodeStack.pop();
  1349. }
  1350. } else if (typeChild) {
  1351. // Check if it contains a declEnum
  1352. const declEnum = typeChild.namedChildren.find(
  1353. (c: SyntaxNode) => c.type === 'declEnum'
  1354. );
  1355. if (declEnum) {
  1356. const enumNode = this.createNode('enum', name, node);
  1357. if (enumNode) {
  1358. // Extract enum members
  1359. this.nodeStack.push(enumNode.id);
  1360. for (let i = 0; i < declEnum.namedChildCount; i++) {
  1361. const child = declEnum.namedChild(i);
  1362. if (child?.type === 'declEnumValue') {
  1363. const memberName = getChildByField(child, 'name');
  1364. if (memberName) {
  1365. this.createNode('enum_member', getNodeText(memberName, this.source), child);
  1366. }
  1367. }
  1368. }
  1369. this.nodeStack.pop();
  1370. }
  1371. } else {
  1372. // Simple type alias: type TFoo = string / type TFoo = Integer
  1373. this.createNode('type_alias', name, node);
  1374. }
  1375. } else {
  1376. // Fallback: could be a forward declaration or simple alias
  1377. this.createNode('type_alias', name, node);
  1378. }
  1379. }
  1380. /**
  1381. * Extract Pascal uses clause into individual import nodes
  1382. */
  1383. private extractPascalUses(node: SyntaxNode): void {
  1384. const importText = getNodeText(node, this.source).trim();
  1385. for (let i = 0; i < node.namedChildCount; i++) {
  1386. const child = node.namedChild(i);
  1387. if (child?.type === 'moduleName') {
  1388. const unitName = getNodeText(child, this.source);
  1389. this.createNode('import', unitName, child, {
  1390. signature: importText,
  1391. });
  1392. // Create unresolved reference for resolution
  1393. if (this.nodeStack.length > 0) {
  1394. const parentId = this.nodeStack[this.nodeStack.length - 1];
  1395. if (parentId) {
  1396. this.unresolvedReferences.push({
  1397. fromNodeId: parentId,
  1398. referenceName: unitName,
  1399. referenceKind: 'imports',
  1400. line: child.startPosition.row + 1,
  1401. column: child.startPosition.column,
  1402. });
  1403. }
  1404. }
  1405. }
  1406. }
  1407. }
  1408. /**
  1409. * Extract a Pascal constant declaration
  1410. */
  1411. private extractPascalConst(node: SyntaxNode): void {
  1412. const nameNode = getChildByField(node, 'name');
  1413. if (!nameNode) return;
  1414. const name = getNodeText(nameNode, this.source);
  1415. const defaultValue = node.namedChildren.find(
  1416. (c: SyntaxNode) => c.type === 'defaultValue'
  1417. );
  1418. const sig = defaultValue ? getNodeText(defaultValue, this.source) : undefined;
  1419. this.createNode('constant', name, node, { signature: sig });
  1420. }
  1421. /**
  1422. * Extract Pascal inheritance (extends/implements) from declClass typeref children
  1423. */
  1424. private extractPascalInheritance(declClass: SyntaxNode, classId: string): void {
  1425. const typerefs = declClass.namedChildren.filter(
  1426. (c: SyntaxNode) => c.type === 'typeref'
  1427. );
  1428. for (let i = 0; i < typerefs.length; i++) {
  1429. const ref = typerefs[i]!;
  1430. const name = getNodeText(ref, this.source);
  1431. this.unresolvedReferences.push({
  1432. fromNodeId: classId,
  1433. referenceName: name,
  1434. referenceKind: i === 0 ? 'extends' : 'implements',
  1435. line: ref.startPosition.row + 1,
  1436. column: ref.startPosition.column,
  1437. });
  1438. }
  1439. }
  1440. /**
  1441. * Extract calls and resolve method context from a Pascal defProc (implementation body).
  1442. * Does not create a new node — the declaration was already captured from the interface section.
  1443. */
  1444. private extractPascalDefProc(node: SyntaxNode): void {
  1445. // Find the matching declaration node by name to use as call parent
  1446. const declProc = node.namedChildren.find(
  1447. (c: SyntaxNode) => c.type === 'declProc'
  1448. );
  1449. if (!declProc) return;
  1450. const nameNode = getChildByField(declProc, 'name');
  1451. if (!nameNode) return;
  1452. const fullName = getNodeText(nameNode, this.source).trim();
  1453. // fullName is like "TAuthService.Create"
  1454. const shortName = fullName.includes('.') ? fullName.split('.').pop()! : fullName;
  1455. const fullNameKey = fullName.toLowerCase();
  1456. const shortNameKey = shortName.toLowerCase();
  1457. // Build method index on first use (O(n) once, then O(1) per lookup)
  1458. if (!this.methodIndex) {
  1459. this.methodIndex = new Map();
  1460. for (const n of this.nodes) {
  1461. if (n.kind === 'method' || n.kind === 'function') {
  1462. const nameKey = n.name.toLowerCase();
  1463. // Keep first seen short-name mapping to avoid silently overwriting earlier entries.
  1464. if (!this.methodIndex.has(nameKey)) {
  1465. this.methodIndex.set(nameKey, n.id);
  1466. }
  1467. // For Pascal methods, also index qualified forms (e.g. TAuthService.Create).
  1468. if (n.kind === 'method') {
  1469. const qualifiedParts = n.qualifiedName.split('::').slice(1); // drop file path
  1470. if (qualifiedParts.length >= 2) {
  1471. // Create suffix keys so both "Module.Class.Method" and "Class.Method" can resolve.
  1472. for (let i = 0; i < qualifiedParts.length - 1; i++) {
  1473. const scopedName = qualifiedParts.slice(i).join('.').toLowerCase();
  1474. this.methodIndex.set(scopedName, n.id);
  1475. }
  1476. }
  1477. }
  1478. }
  1479. }
  1480. }
  1481. const parentId =
  1482. this.methodIndex.get(fullNameKey) ||
  1483. this.methodIndex.get(shortNameKey) ||
  1484. this.nodeStack[this.nodeStack.length - 1];
  1485. if (!parentId) return;
  1486. // Visit the block for calls
  1487. const block = node.namedChildren.find(
  1488. (c: SyntaxNode) => c.type === 'block'
  1489. );
  1490. if (block) {
  1491. this.nodeStack.push(parentId);
  1492. this.visitPascalBlock(block);
  1493. this.nodeStack.pop();
  1494. }
  1495. }
  1496. /**
  1497. * Extract function calls from a Pascal expression
  1498. */
  1499. private extractPascalCall(node: SyntaxNode): void {
  1500. if (this.nodeStack.length === 0) return;
  1501. const callerId = this.nodeStack[this.nodeStack.length - 1];
  1502. if (!callerId) return;
  1503. // Get the callee name — first child is typically the identifier or exprDot
  1504. const firstChild = node.namedChild(0);
  1505. if (!firstChild) return;
  1506. let calleeName = '';
  1507. if (firstChild.type === 'exprDot') {
  1508. // Qualified call: Obj.Method(...)
  1509. const identifiers = firstChild.namedChildren.filter(
  1510. (c: SyntaxNode) => c.type === 'identifier'
  1511. );
  1512. if (identifiers.length > 0) {
  1513. calleeName = identifiers.map((id: SyntaxNode) => getNodeText(id, this.source)).join('.');
  1514. }
  1515. } else if (firstChild.type === 'identifier') {
  1516. calleeName = getNodeText(firstChild, this.source);
  1517. }
  1518. if (calleeName) {
  1519. this.unresolvedReferences.push({
  1520. fromNodeId: callerId,
  1521. referenceName: calleeName,
  1522. referenceKind: 'calls',
  1523. line: node.startPosition.row + 1,
  1524. column: node.startPosition.column,
  1525. });
  1526. }
  1527. // Also visit arguments for nested calls
  1528. const args = node.namedChildren.find(
  1529. (c: SyntaxNode) => c.type === 'exprArgs'
  1530. );
  1531. if (args) {
  1532. this.visitPascalBlock(args);
  1533. }
  1534. }
  1535. /**
  1536. * Recursively visit a Pascal block/statement tree for call expressions
  1537. */
  1538. private visitPascalBlock(node: SyntaxNode): void {
  1539. for (let i = 0; i < node.namedChildCount; i++) {
  1540. const child = node.namedChild(i);
  1541. if (!child) continue;
  1542. if (child.type === 'exprCall') {
  1543. this.extractPascalCall(child);
  1544. } else if (child.type === 'exprDot') {
  1545. // Check if exprDot contains an exprCall
  1546. for (let j = 0; j < child.namedChildCount; j++) {
  1547. const grandchild = child.namedChild(j);
  1548. if (grandchild?.type === 'exprCall') {
  1549. this.extractPascalCall(grandchild);
  1550. }
  1551. }
  1552. } else {
  1553. this.visitPascalBlock(child);
  1554. }
  1555. }
  1556. }
  1557. }
  1558. /**
  1559. * Extract nodes and edges from source code
  1560. */
  1561. export function extractFromSource(
  1562. filePath: string,
  1563. source: string,
  1564. language?: Language
  1565. ): ExtractionResult {
  1566. const detectedLanguage = language || detectLanguage(filePath);
  1567. const fileExtension = path.extname(filePath).toLowerCase();
  1568. // Use custom extractor for Svelte
  1569. if (detectedLanguage === 'svelte') {
  1570. const extractor = new SvelteExtractor(filePath, source);
  1571. return extractor.extract();
  1572. }
  1573. // Use custom extractor for Liquid
  1574. if (detectedLanguage === 'liquid') {
  1575. const extractor = new LiquidExtractor(filePath, source);
  1576. return extractor.extract();
  1577. }
  1578. // Use custom extractor for DFM/FMX form files
  1579. if (
  1580. detectedLanguage === 'pascal' &&
  1581. (fileExtension === '.dfm' || fileExtension === '.fmx')
  1582. ) {
  1583. const extractor = new DfmExtractor(filePath, source);
  1584. return extractor.extract();
  1585. }
  1586. const extractor = new TreeSitterExtractor(filePath, source, detectedLanguage);
  1587. return extractor.extract();
  1588. }