tree-sitter.ts 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307
  1. /**
  2. * Tree-sitter Parser Wrapper
  3. *
  4. * Handles parsing source code and extracting structural information.
  5. */
  6. import { SyntaxNode, Tree } from 'tree-sitter';
  7. import * as crypto from 'crypto';
  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 { captureException } from '../sentry';
  19. /**
  20. * Generate a unique node ID
  21. *
  22. * Uses a 32-character (128-bit) hash to avoid collisions when indexing
  23. * large codebases with many files containing similar symbols.
  24. */
  25. export function generateNodeId(
  26. filePath: string,
  27. kind: NodeKind,
  28. name: string,
  29. line: number
  30. ): string {
  31. const hash = crypto
  32. .createHash('sha256')
  33. .update(`${filePath}:${kind}:${name}:${line}`)
  34. .digest('hex')
  35. .substring(0, 32);
  36. return `${kind}:${hash}`;
  37. }
  38. /**
  39. * Extract text from a syntax node
  40. */
  41. function getNodeText(node: SyntaxNode, source: string): string {
  42. return source.substring(node.startIndex, node.endIndex);
  43. }
  44. /**
  45. * Find a child node by field name
  46. */
  47. function getChildByField(node: SyntaxNode, fieldName: string): SyntaxNode | null {
  48. return node.childForFieldName(fieldName);
  49. }
  50. /**
  51. * Get the docstring/comment preceding a node
  52. */
  53. function getPrecedingDocstring(node: SyntaxNode, source: string): string | undefined {
  54. let sibling = node.previousNamedSibling;
  55. const comments: string[] = [];
  56. while (sibling) {
  57. if (
  58. sibling.type === 'comment' ||
  59. sibling.type === 'line_comment' ||
  60. sibling.type === 'block_comment' ||
  61. sibling.type === 'documentation_comment'
  62. ) {
  63. comments.unshift(getNodeText(sibling, source));
  64. sibling = sibling.previousNamedSibling;
  65. } else {
  66. break;
  67. }
  68. }
  69. if (comments.length === 0) return undefined;
  70. // Clean up comment markers
  71. return comments
  72. .map((c) =>
  73. c
  74. .replace(/^\/\*\*?|\*\/$/g, '')
  75. .replace(/^\/\/\s?/gm, '')
  76. .replace(/^\s*\*\s?/gm, '')
  77. .trim()
  78. )
  79. .join('\n')
  80. .trim();
  81. }
  82. /**
  83. * Language-specific extraction configuration
  84. */
  85. interface LanguageExtractor {
  86. /** Node types that represent functions */
  87. functionTypes: string[];
  88. /** Node types that represent classes */
  89. classTypes: string[];
  90. /** Node types that represent methods */
  91. methodTypes: string[];
  92. /** Node types that represent interfaces/protocols/traits */
  93. interfaceTypes: string[];
  94. /** Node types that represent structs */
  95. structTypes: string[];
  96. /** Node types that represent enums */
  97. enumTypes: string[];
  98. /** Node types that represent type aliases (e.g. `type X = ...`) */
  99. typeAliasTypes: string[];
  100. /** Node types that represent imports */
  101. importTypes: string[];
  102. /** Node types that represent function calls */
  103. callTypes: string[];
  104. /** Node types that represent variable declarations (const, let, var, etc.) */
  105. variableTypes: string[];
  106. /** Field name for identifier/name */
  107. nameField: string;
  108. /** Field name for body */
  109. bodyField: string;
  110. /** Field name for parameters */
  111. paramsField: string;
  112. /** Field name for return type */
  113. returnField?: string;
  114. /** Extract signature from node */
  115. getSignature?: (node: SyntaxNode, source: string) => string | undefined;
  116. /** Extract visibility from node */
  117. getVisibility?: (node: SyntaxNode) => 'public' | 'private' | 'protected' | 'internal' | undefined;
  118. /** Check if node is exported */
  119. isExported?: (node: SyntaxNode, source: string) => boolean;
  120. /** Check if node is async */
  121. isAsync?: (node: SyntaxNode) => boolean;
  122. /** Check if node is static */
  123. isStatic?: (node: SyntaxNode) => boolean;
  124. /** Check if variable declaration is a constant (const vs let/var) */
  125. isConst?: (node: SyntaxNode) => boolean;
  126. }
  127. /**
  128. * Language-specific extractors
  129. */
  130. const EXTRACTORS: Partial<Record<Language, LanguageExtractor>> = {
  131. typescript: {
  132. functionTypes: ['function_declaration', 'arrow_function', 'function_expression'],
  133. classTypes: ['class_declaration'],
  134. methodTypes: ['method_definition', 'public_field_definition'],
  135. interfaceTypes: ['interface_declaration'],
  136. structTypes: [],
  137. enumTypes: ['enum_declaration'],
  138. typeAliasTypes: ['type_alias_declaration'],
  139. importTypes: ['import_statement'],
  140. callTypes: ['call_expression'],
  141. variableTypes: ['lexical_declaration', 'variable_declaration'],
  142. nameField: 'name',
  143. bodyField: 'body',
  144. paramsField: 'parameters',
  145. returnField: 'return_type',
  146. getSignature: (node, source) => {
  147. const params = getChildByField(node, 'parameters');
  148. const returnType = getChildByField(node, 'return_type');
  149. if (!params) return undefined;
  150. let sig = getNodeText(params, source);
  151. if (returnType) {
  152. sig += ': ' + getNodeText(returnType, source).replace(/^:\s*/, '');
  153. }
  154. return sig;
  155. },
  156. getVisibility: (node) => {
  157. for (let i = 0; i < node.childCount; i++) {
  158. const child = node.child(i);
  159. if (child?.type === 'accessibility_modifier') {
  160. const text = child.text;
  161. if (text === 'public') return 'public';
  162. if (text === 'private') return 'private';
  163. if (text === 'protected') return 'protected';
  164. }
  165. }
  166. return undefined;
  167. },
  168. isExported: (node, _source) => {
  169. // Walk the parent chain to find an export_statement ancestor.
  170. // This correctly handles deeply nested nodes like arrow functions
  171. // inside variable declarations: `export const X = () => { ... }`
  172. // where the arrow_function is 3 levels deep under export_statement.
  173. let current = node.parent;
  174. while (current) {
  175. if (current.type === 'export_statement') return true;
  176. current = current.parent;
  177. }
  178. return false;
  179. },
  180. isAsync: (node) => {
  181. for (let i = 0; i < node.childCount; i++) {
  182. const child = node.child(i);
  183. if (child?.type === 'async') return true;
  184. }
  185. return false;
  186. },
  187. isStatic: (node) => {
  188. for (let i = 0; i < node.childCount; i++) {
  189. const child = node.child(i);
  190. if (child?.type === 'static') return true;
  191. }
  192. return false;
  193. },
  194. isConst: (node) => {
  195. // For lexical_declaration, check if it's 'const' or 'let'
  196. // For variable_declaration, it's always 'var'
  197. if (node.type === 'lexical_declaration') {
  198. for (let i = 0; i < node.childCount; i++) {
  199. const child = node.child(i);
  200. if (child?.type === 'const') return true;
  201. }
  202. }
  203. return false;
  204. },
  205. },
  206. javascript: {
  207. functionTypes: ['function_declaration', 'arrow_function', 'function_expression'],
  208. classTypes: ['class_declaration'],
  209. methodTypes: ['method_definition', 'field_definition'],
  210. interfaceTypes: [],
  211. structTypes: [],
  212. enumTypes: [],
  213. typeAliasTypes: [],
  214. importTypes: ['import_statement'],
  215. callTypes: ['call_expression'],
  216. variableTypes: ['lexical_declaration', 'variable_declaration'],
  217. nameField: 'name',
  218. bodyField: 'body',
  219. paramsField: 'parameters',
  220. getSignature: (node, source) => {
  221. const params = getChildByField(node, 'parameters');
  222. return params ? getNodeText(params, source) : undefined;
  223. },
  224. isExported: (node, _source) => {
  225. let current = node.parent;
  226. while (current) {
  227. if (current.type === 'export_statement') return true;
  228. current = current.parent;
  229. }
  230. return false;
  231. },
  232. isAsync: (node) => {
  233. for (let i = 0; i < node.childCount; i++) {
  234. const child = node.child(i);
  235. if (child?.type === 'async') return true;
  236. }
  237. return false;
  238. },
  239. isConst: (node) => {
  240. if (node.type === 'lexical_declaration') {
  241. for (let i = 0; i < node.childCount; i++) {
  242. const child = node.child(i);
  243. if (child?.type === 'const') return true;
  244. }
  245. }
  246. return false;
  247. },
  248. },
  249. python: {
  250. functionTypes: ['function_definition'],
  251. classTypes: ['class_definition'],
  252. methodTypes: ['function_definition'], // Methods are functions inside classes
  253. interfaceTypes: [],
  254. structTypes: [],
  255. enumTypes: [],
  256. typeAliasTypes: [],
  257. importTypes: ['import_statement', 'import_from_statement'],
  258. callTypes: ['call'],
  259. variableTypes: ['assignment'], // Python uses assignment for variable declarations
  260. nameField: 'name',
  261. bodyField: 'body',
  262. paramsField: 'parameters',
  263. returnField: 'return_type',
  264. getSignature: (node, source) => {
  265. const params = getChildByField(node, 'parameters');
  266. const returnType = getChildByField(node, 'return_type');
  267. if (!params) return undefined;
  268. let sig = getNodeText(params, source);
  269. if (returnType) {
  270. sig += ' -> ' + getNodeText(returnType, source);
  271. }
  272. return sig;
  273. },
  274. isAsync: (node) => {
  275. const prev = node.previousSibling;
  276. return prev?.type === 'async';
  277. },
  278. isStatic: (node) => {
  279. // Check for @staticmethod decorator
  280. const prev = node.previousNamedSibling;
  281. if (prev?.type === 'decorator') {
  282. const text = prev.text;
  283. return text.includes('staticmethod');
  284. }
  285. return false;
  286. },
  287. },
  288. go: {
  289. functionTypes: ['function_declaration'],
  290. classTypes: [], // Go doesn't have classes
  291. methodTypes: ['method_declaration'],
  292. interfaceTypes: ['interface_type'],
  293. structTypes: ['struct_type'],
  294. enumTypes: [],
  295. typeAliasTypes: ['type_spec'], // Go type declarations
  296. importTypes: ['import_declaration'],
  297. callTypes: ['call_expression'],
  298. variableTypes: ['var_declaration', 'short_var_declaration', 'const_declaration'],
  299. nameField: 'name',
  300. bodyField: 'body',
  301. paramsField: 'parameters',
  302. returnField: 'result',
  303. getSignature: (node, source) => {
  304. const params = getChildByField(node, 'parameters');
  305. const result = getChildByField(node, 'result');
  306. if (!params) return undefined;
  307. let sig = getNodeText(params, source);
  308. if (result) {
  309. sig += ' ' + getNodeText(result, source);
  310. }
  311. return sig;
  312. },
  313. },
  314. rust: {
  315. functionTypes: ['function_item'],
  316. classTypes: [], // Rust has impl blocks
  317. methodTypes: ['function_item'], // Methods are functions in impl blocks
  318. interfaceTypes: ['trait_item'],
  319. structTypes: ['struct_item'],
  320. enumTypes: ['enum_item'],
  321. typeAliasTypes: ['type_item'], // Rust type aliases
  322. importTypes: ['use_declaration'],
  323. callTypes: ['call_expression'],
  324. variableTypes: ['let_declaration', 'const_item', 'static_item'],
  325. nameField: 'name',
  326. bodyField: 'body',
  327. paramsField: 'parameters',
  328. returnField: 'return_type',
  329. getSignature: (node, source) => {
  330. const params = getChildByField(node, 'parameters');
  331. const returnType = getChildByField(node, 'return_type');
  332. if (!params) return undefined;
  333. let sig = getNodeText(params, source);
  334. if (returnType) {
  335. sig += ' -> ' + getNodeText(returnType, source);
  336. }
  337. return sig;
  338. },
  339. isAsync: (node) => {
  340. for (let i = 0; i < node.childCount; i++) {
  341. const child = node.child(i);
  342. if (child?.type === 'async') return true;
  343. }
  344. return false;
  345. },
  346. getVisibility: (node) => {
  347. for (let i = 0; i < node.childCount; i++) {
  348. const child = node.child(i);
  349. if (child?.type === 'visibility_modifier') {
  350. return child.text.includes('pub') ? 'public' : 'private';
  351. }
  352. }
  353. return 'private'; // Rust defaults to private
  354. },
  355. },
  356. java: {
  357. functionTypes: [],
  358. classTypes: ['class_declaration'],
  359. methodTypes: ['method_declaration', 'constructor_declaration'],
  360. interfaceTypes: ['interface_declaration'],
  361. structTypes: [],
  362. enumTypes: ['enum_declaration'],
  363. typeAliasTypes: [],
  364. importTypes: ['import_declaration'],
  365. callTypes: ['method_invocation'],
  366. variableTypes: ['local_variable_declaration', 'field_declaration'],
  367. nameField: 'name',
  368. bodyField: 'body',
  369. paramsField: 'parameters',
  370. returnField: 'type',
  371. getSignature: (node, source) => {
  372. const params = getChildByField(node, 'parameters');
  373. const returnType = getChildByField(node, 'type');
  374. if (!params) return undefined;
  375. const paramsText = getNodeText(params, source);
  376. return returnType ? getNodeText(returnType, source) + ' ' + paramsText : paramsText;
  377. },
  378. getVisibility: (node) => {
  379. for (let i = 0; i < node.childCount; i++) {
  380. const child = node.child(i);
  381. if (child?.type === 'modifiers') {
  382. const text = child.text;
  383. if (text.includes('public')) return 'public';
  384. if (text.includes('private')) return 'private';
  385. if (text.includes('protected')) return 'protected';
  386. }
  387. }
  388. return undefined;
  389. },
  390. isStatic: (node) => {
  391. for (let i = 0; i < node.childCount; i++) {
  392. const child = node.child(i);
  393. if (child?.type === 'modifiers' && child.text.includes('static')) {
  394. return true;
  395. }
  396. }
  397. return false;
  398. },
  399. },
  400. c: {
  401. functionTypes: ['function_definition'],
  402. classTypes: [],
  403. methodTypes: [],
  404. interfaceTypes: [],
  405. structTypes: ['struct_specifier'],
  406. enumTypes: ['enum_specifier'],
  407. typeAliasTypes: ['type_definition'], // typedef
  408. importTypes: ['preproc_include'],
  409. callTypes: ['call_expression'],
  410. variableTypes: ['declaration'],
  411. nameField: 'declarator',
  412. bodyField: 'body',
  413. paramsField: 'parameters',
  414. },
  415. cpp: {
  416. functionTypes: ['function_definition'],
  417. classTypes: ['class_specifier'],
  418. methodTypes: ['function_definition'],
  419. interfaceTypes: [],
  420. structTypes: ['struct_specifier'],
  421. enumTypes: ['enum_specifier'],
  422. typeAliasTypes: ['type_definition', 'alias_declaration'], // typedef and using
  423. importTypes: ['preproc_include'],
  424. callTypes: ['call_expression'],
  425. variableTypes: ['declaration'],
  426. nameField: 'declarator',
  427. bodyField: 'body',
  428. paramsField: 'parameters',
  429. getVisibility: (node) => {
  430. // Check for access specifier in parent
  431. const parent = node.parent;
  432. if (parent) {
  433. for (let i = 0; i < parent.childCount; i++) {
  434. const child = parent.child(i);
  435. if (child?.type === 'access_specifier') {
  436. const text = child.text;
  437. if (text.includes('public')) return 'public';
  438. if (text.includes('private')) return 'private';
  439. if (text.includes('protected')) return 'protected';
  440. }
  441. }
  442. }
  443. return undefined;
  444. },
  445. },
  446. csharp: {
  447. functionTypes: [],
  448. classTypes: ['class_declaration'],
  449. methodTypes: ['method_declaration', 'constructor_declaration'],
  450. interfaceTypes: ['interface_declaration'],
  451. structTypes: ['struct_declaration'],
  452. enumTypes: ['enum_declaration'],
  453. typeAliasTypes: [],
  454. importTypes: ['using_directive'],
  455. callTypes: ['invocation_expression'],
  456. variableTypes: ['local_declaration_statement', 'field_declaration'],
  457. nameField: 'name',
  458. bodyField: 'body',
  459. paramsField: 'parameter_list',
  460. getVisibility: (node) => {
  461. for (let i = 0; i < node.childCount; i++) {
  462. const child = node.child(i);
  463. if (child?.type === 'modifier') {
  464. const text = child.text;
  465. if (text === 'public') return 'public';
  466. if (text === 'private') return 'private';
  467. if (text === 'protected') return 'protected';
  468. if (text === 'internal') return 'internal';
  469. }
  470. }
  471. return 'private'; // C# defaults to private
  472. },
  473. isStatic: (node) => {
  474. for (let i = 0; i < node.childCount; i++) {
  475. const child = node.child(i);
  476. if (child?.type === 'modifier' && child.text === 'static') {
  477. return true;
  478. }
  479. }
  480. return false;
  481. },
  482. isAsync: (node) => {
  483. for (let i = 0; i < node.childCount; i++) {
  484. const child = node.child(i);
  485. if (child?.type === 'modifier' && child.text === 'async') {
  486. return true;
  487. }
  488. }
  489. return false;
  490. },
  491. },
  492. php: {
  493. functionTypes: ['function_definition'],
  494. classTypes: ['class_declaration'],
  495. methodTypes: ['method_declaration'],
  496. interfaceTypes: ['interface_declaration'],
  497. structTypes: [],
  498. enumTypes: ['enum_declaration'],
  499. typeAliasTypes: [],
  500. importTypes: ['namespace_use_declaration'],
  501. callTypes: ['function_call_expression', 'member_call_expression', 'scoped_call_expression'],
  502. variableTypes: ['property_declaration', 'const_declaration'],
  503. nameField: 'name',
  504. bodyField: 'body',
  505. paramsField: 'parameters',
  506. returnField: 'return_type',
  507. getVisibility: (node) => {
  508. for (let i = 0; i < node.childCount; i++) {
  509. const child = node.child(i);
  510. if (child?.type === 'visibility_modifier') {
  511. const text = child.text;
  512. if (text === 'public') return 'public';
  513. if (text === 'private') return 'private';
  514. if (text === 'protected') return 'protected';
  515. }
  516. }
  517. return 'public'; // PHP defaults to public
  518. },
  519. isStatic: (node) => {
  520. for (let i = 0; i < node.childCount; i++) {
  521. const child = node.child(i);
  522. if (child?.type === 'static_modifier') return true;
  523. }
  524. return false;
  525. },
  526. },
  527. ruby: {
  528. functionTypes: ['method'],
  529. classTypes: ['class'],
  530. methodTypes: ['method', 'singleton_method'],
  531. interfaceTypes: [], // Ruby uses modules
  532. structTypes: [],
  533. enumTypes: [],
  534. typeAliasTypes: [],
  535. importTypes: ['call'], // require/require_relative
  536. callTypes: ['call', 'method_call'],
  537. variableTypes: ['assignment'], // Ruby uses assignment like Python
  538. nameField: 'name',
  539. bodyField: 'body',
  540. paramsField: 'parameters',
  541. getVisibility: (node) => {
  542. // Ruby visibility is based on preceding visibility modifiers
  543. let sibling = node.previousNamedSibling;
  544. while (sibling) {
  545. if (sibling.type === 'call') {
  546. const methodName = getChildByField(sibling, 'method');
  547. if (methodName) {
  548. const text = methodName.text;
  549. if (text === 'private') return 'private';
  550. if (text === 'protected') return 'protected';
  551. if (text === 'public') return 'public';
  552. }
  553. }
  554. sibling = sibling.previousNamedSibling;
  555. }
  556. return 'public';
  557. },
  558. },
  559. swift: {
  560. functionTypes: ['function_declaration'],
  561. classTypes: ['class_declaration'],
  562. methodTypes: ['function_declaration'], // Methods are functions inside classes
  563. interfaceTypes: ['protocol_declaration'],
  564. structTypes: ['struct_declaration'],
  565. enumTypes: ['enum_declaration'],
  566. typeAliasTypes: ['typealias_declaration'],
  567. importTypes: ['import_declaration'],
  568. callTypes: ['call_expression'],
  569. variableTypes: ['property_declaration', 'constant_declaration'],
  570. nameField: 'name',
  571. bodyField: 'body',
  572. paramsField: 'parameter',
  573. returnField: 'return_type',
  574. getSignature: (node, source) => {
  575. // Swift function signature: func name(params) -> ReturnType
  576. const params = getChildByField(node, 'parameter');
  577. const returnType = getChildByField(node, 'return_type');
  578. if (!params) return undefined;
  579. let sig = getNodeText(params, source);
  580. if (returnType) {
  581. sig += ' -> ' + getNodeText(returnType, source);
  582. }
  583. return sig;
  584. },
  585. getVisibility: (node) => {
  586. // Check for visibility modifiers in Swift
  587. for (let i = 0; i < node.childCount; i++) {
  588. const child = node.child(i);
  589. if (child?.type === 'modifiers') {
  590. const text = child.text;
  591. if (text.includes('public')) return 'public';
  592. if (text.includes('private')) return 'private';
  593. if (text.includes('internal')) return 'internal';
  594. if (text.includes('fileprivate')) return 'private';
  595. }
  596. }
  597. return 'internal'; // Swift defaults to internal
  598. },
  599. isStatic: (node) => {
  600. for (let i = 0; i < node.childCount; i++) {
  601. const child = node.child(i);
  602. if (child?.type === 'modifiers') {
  603. if (child.text.includes('static') || child.text.includes('class')) {
  604. return true;
  605. }
  606. }
  607. }
  608. return false;
  609. },
  610. isAsync: (node) => {
  611. for (let i = 0; i < node.childCount; i++) {
  612. const child = node.child(i);
  613. if (child?.type === 'modifiers' && child.text.includes('async')) {
  614. return true;
  615. }
  616. }
  617. return false;
  618. },
  619. },
  620. kotlin: {
  621. functionTypes: ['function_declaration'],
  622. classTypes: ['class_declaration'],
  623. methodTypes: ['function_declaration'], // Methods are functions inside classes
  624. interfaceTypes: ['class_declaration'], // Interfaces use class_declaration with 'interface' modifier
  625. structTypes: [], // Kotlin uses data classes
  626. enumTypes: ['class_declaration'], // Enums use class_declaration with 'enum' modifier
  627. typeAliasTypes: ['type_alias'],
  628. importTypes: ['import_header'],
  629. callTypes: ['call_expression'],
  630. variableTypes: ['property_declaration'],
  631. nameField: 'simple_identifier',
  632. bodyField: 'function_body',
  633. paramsField: 'function_value_parameters',
  634. returnField: 'type',
  635. getSignature: (node, source) => {
  636. // Kotlin function signature: fun name(params): ReturnType
  637. const params = getChildByField(node, 'function_value_parameters');
  638. const returnType = getChildByField(node, 'type');
  639. if (!params) return undefined;
  640. let sig = getNodeText(params, source);
  641. if (returnType) {
  642. sig += ': ' + getNodeText(returnType, source);
  643. }
  644. return sig;
  645. },
  646. getVisibility: (node) => {
  647. // Check for visibility modifiers in Kotlin
  648. for (let i = 0; i < node.childCount; i++) {
  649. const child = node.child(i);
  650. if (child?.type === 'modifiers') {
  651. const text = child.text;
  652. if (text.includes('public')) return 'public';
  653. if (text.includes('private')) return 'private';
  654. if (text.includes('protected')) return 'protected';
  655. if (text.includes('internal')) return 'internal';
  656. }
  657. }
  658. return 'public'; // Kotlin defaults to public
  659. },
  660. isStatic: (_node) => {
  661. // Kotlin doesn't have static, uses companion objects
  662. // Check if inside companion object would require more context
  663. return false;
  664. },
  665. isAsync: (node) => {
  666. // Kotlin uses suspend keyword for coroutines
  667. for (let i = 0; i < node.childCount; i++) {
  668. const child = node.child(i);
  669. if (child?.type === 'modifiers' && child.text.includes('suspend')) {
  670. return true;
  671. }
  672. }
  673. return false;
  674. },
  675. },
  676. dart: {
  677. functionTypes: ['function_signature'],
  678. classTypes: ['class_definition'],
  679. methodTypes: ['method_signature'],
  680. interfaceTypes: [],
  681. structTypes: [],
  682. enumTypes: ['enum_declaration'],
  683. typeAliasTypes: ['type_alias'],
  684. importTypes: ['import_or_export'],
  685. callTypes: [], // Dart calls use identifier+selector, handled via function body traversal
  686. variableTypes: [],
  687. nameField: 'name',
  688. bodyField: 'body', // class_definition uses 'body' field
  689. paramsField: 'formal_parameter_list',
  690. returnField: 'type',
  691. getSignature: (node, source) => {
  692. // For function_signature: extract params + return type
  693. // For method_signature: delegate to inner function_signature
  694. let sig = node;
  695. if (node.type === 'method_signature') {
  696. const inner = node.namedChildren.find((c: SyntaxNode) =>
  697. c.type === 'function_signature' || c.type === 'getter_signature' || c.type === 'setter_signature'
  698. );
  699. if (inner) sig = inner;
  700. }
  701. const params = sig.namedChildren.find((c: SyntaxNode) => c.type === 'formal_parameter_list');
  702. const retType = sig.namedChildren.find((c: SyntaxNode) =>
  703. c.type === 'type_identifier' || c.type === 'void_type'
  704. );
  705. if (!params && !retType) return undefined;
  706. let result = '';
  707. if (retType) result += getNodeText(retType, source) + ' ';
  708. if (params) result += getNodeText(params, source);
  709. return result.trim() || undefined;
  710. },
  711. getVisibility: (node) => {
  712. // Dart convention: _ prefix means private, otherwise public
  713. let nameNode: SyntaxNode | null = null;
  714. if (node.type === 'method_signature') {
  715. const inner = node.namedChildren.find((c: SyntaxNode) =>
  716. c.type === 'function_signature' || c.type === 'getter_signature' || c.type === 'setter_signature'
  717. );
  718. if (inner) nameNode = inner.namedChildren.find((c: SyntaxNode) => c.type === 'identifier') || null;
  719. } else {
  720. nameNode = node.childForFieldName('name');
  721. }
  722. if (nameNode && nameNode.text.startsWith('_')) return 'private';
  723. return 'public';
  724. },
  725. isAsync: (node) => {
  726. // In Dart, 'async' is on the function_body (next sibling), not the signature
  727. const nextSibling = node.nextNamedSibling;
  728. if (nextSibling?.type === 'function_body') {
  729. for (let i = 0; i < nextSibling.childCount; i++) {
  730. const child = nextSibling.child(i);
  731. if (child?.type === 'async') return true;
  732. }
  733. }
  734. return false;
  735. },
  736. isStatic: (node) => {
  737. // For method_signature, check for 'static' child
  738. if (node.type === 'method_signature') {
  739. for (let i = 0; i < node.childCount; i++) {
  740. const child = node.child(i);
  741. if (child?.type === 'static') return true;
  742. }
  743. }
  744. return false;
  745. },
  746. },
  747. };
  748. // TSX and JSX use the same extractors as their base languages
  749. EXTRACTORS.tsx = EXTRACTORS.typescript;
  750. EXTRACTORS.jsx = EXTRACTORS.javascript;
  751. /**
  752. * Extract the name from a node based on language
  753. */
  754. function extractName(node: SyntaxNode, source: string, extractor: LanguageExtractor): string {
  755. // Try field name first
  756. const nameNode = getChildByField(node, extractor.nameField);
  757. if (nameNode) {
  758. // Handle complex declarators (C/C++)
  759. if (nameNode.type === 'function_declarator' || nameNode.type === 'declarator') {
  760. const innerName = getChildByField(nameNode, 'declarator') || nameNode.namedChild(0);
  761. return innerName ? getNodeText(innerName, source) : getNodeText(nameNode, source);
  762. }
  763. return getNodeText(nameNode, source);
  764. }
  765. // For Dart method_signature, look inside inner signature types
  766. if (node.type === 'method_signature') {
  767. for (let i = 0; i < node.namedChildCount; i++) {
  768. const child = node.namedChild(i);
  769. if (child && (
  770. child.type === 'function_signature' ||
  771. child.type === 'getter_signature' ||
  772. child.type === 'setter_signature' ||
  773. child.type === 'constructor_signature' ||
  774. child.type === 'factory_constructor_signature'
  775. )) {
  776. // Find identifier inside the inner signature
  777. for (let j = 0; j < child.namedChildCount; j++) {
  778. const inner = child.namedChild(j);
  779. if (inner?.type === 'identifier') {
  780. return getNodeText(inner, source);
  781. }
  782. }
  783. }
  784. }
  785. }
  786. // Fall back to first identifier child
  787. for (let i = 0; i < node.namedChildCount; i++) {
  788. const child = node.namedChild(i);
  789. if (
  790. child &&
  791. (child.type === 'identifier' ||
  792. child.type === 'type_identifier' ||
  793. child.type === 'simple_identifier' ||
  794. child.type === 'constant')
  795. ) {
  796. return getNodeText(child, source);
  797. }
  798. }
  799. return '<anonymous>';
  800. }
  801. /**
  802. * TreeSitterExtractor - Main extraction class
  803. */
  804. export class TreeSitterExtractor {
  805. private filePath: string;
  806. private language: Language;
  807. private source: string;
  808. private tree: Tree | null = null;
  809. private nodes: Node[] = [];
  810. private edges: Edge[] = [];
  811. private unresolvedReferences: UnresolvedReference[] = [];
  812. private errors: ExtractionError[] = [];
  813. private extractor: LanguageExtractor | null = null;
  814. private nodeStack: string[] = []; // Stack of parent node IDs
  815. constructor(filePath: string, source: string, language?: Language) {
  816. this.filePath = filePath;
  817. this.source = source;
  818. this.language = language || detectLanguage(filePath);
  819. this.extractor = EXTRACTORS[this.language] || null;
  820. }
  821. /**
  822. * Parse and extract from the source code
  823. */
  824. extract(): ExtractionResult {
  825. const startTime = Date.now();
  826. if (!isLanguageSupported(this.language)) {
  827. return {
  828. nodes: [],
  829. edges: [],
  830. unresolvedReferences: [],
  831. errors: [
  832. {
  833. message: `Unsupported language: ${this.language}`,
  834. severity: 'error',
  835. },
  836. ],
  837. durationMs: Date.now() - startTime,
  838. };
  839. }
  840. const parser = getParser(this.language);
  841. if (!parser) {
  842. return {
  843. nodes: [],
  844. edges: [],
  845. unresolvedReferences: [],
  846. errors: [
  847. {
  848. message: `Failed to get parser for language: ${this.language}`,
  849. severity: 'error',
  850. },
  851. ],
  852. durationMs: Date.now() - startTime,
  853. };
  854. }
  855. try {
  856. this.tree = parser.parse(this.source);
  857. this.visitNode(this.tree.rootNode);
  858. } catch (error) {
  859. captureException(error, { operation: 'tree-sitter-parse', filePath: this.filePath, language: this.language });
  860. this.errors.push({
  861. message: `Parse error: ${error instanceof Error ? error.message : String(error)}`,
  862. severity: 'error',
  863. });
  864. }
  865. return {
  866. nodes: this.nodes,
  867. edges: this.edges,
  868. unresolvedReferences: this.unresolvedReferences,
  869. errors: this.errors,
  870. durationMs: Date.now() - startTime,
  871. };
  872. }
  873. /**
  874. * Visit a node and extract information
  875. */
  876. private visitNode(node: SyntaxNode): void {
  877. if (!this.extractor) return;
  878. const nodeType = node.type;
  879. let skipChildren = false;
  880. // Check for function declarations
  881. // For Python/Ruby, function_definition inside a class should be treated as method
  882. if (this.extractor.functionTypes.includes(nodeType)) {
  883. if (this.nodeStack.length > 0 && this.extractor.methodTypes.includes(nodeType)) {
  884. // Inside a class - treat as method
  885. this.extractMethod(node);
  886. skipChildren = true; // extractMethod visits children via visitFunctionBody
  887. } else {
  888. this.extractFunction(node);
  889. skipChildren = true; // extractFunction visits children via visitFunctionBody
  890. }
  891. }
  892. // Check for class declarations
  893. else if (this.extractor.classTypes.includes(nodeType)) {
  894. // Swift uses class_declaration for both classes and structs
  895. // Check for 'struct' child to differentiate
  896. if (this.language === 'swift' && this.hasChildOfType(node, 'struct')) {
  897. this.extractStruct(node);
  898. } else if (this.language === 'swift' && this.hasChildOfType(node, 'enum')) {
  899. this.extractEnum(node);
  900. } else {
  901. this.extractClass(node);
  902. }
  903. skipChildren = true; // extractClass visits body children
  904. }
  905. // Dart-specific: mixin and extension declarations treated as classes
  906. else if (this.language === 'dart' && (nodeType === 'mixin_declaration' || nodeType === 'extension_declaration')) {
  907. this.extractClass(node);
  908. skipChildren = true;
  909. }
  910. // Check for method declarations (only if not already handled by functionTypes)
  911. else if (this.extractor.methodTypes.includes(nodeType)) {
  912. this.extractMethod(node);
  913. skipChildren = true; // extractMethod visits children via visitFunctionBody
  914. }
  915. // Check for interface/protocol/trait declarations
  916. else if (this.extractor.interfaceTypes.includes(nodeType)) {
  917. this.extractInterface(node);
  918. skipChildren = true; // extractInterface visits body children
  919. }
  920. // Check for struct declarations
  921. else if (this.extractor.structTypes.includes(nodeType)) {
  922. this.extractStruct(node);
  923. skipChildren = true; // extractStruct visits body children
  924. }
  925. // Check for enum declarations
  926. else if (this.extractor.enumTypes.includes(nodeType)) {
  927. this.extractEnum(node);
  928. skipChildren = true; // extractEnum visits body children
  929. }
  930. // Check for type alias declarations (e.g. `type X = ...` in TypeScript)
  931. else if (this.extractor.typeAliasTypes.includes(nodeType)) {
  932. this.extractTypeAlias(node);
  933. }
  934. // Check for variable declarations (const, let, var, etc.)
  935. // Only extract top-level variables (not inside functions/methods)
  936. else if (this.extractor.variableTypes.includes(nodeType) && this.nodeStack.length === 0) {
  937. this.extractVariable(node);
  938. skipChildren = true; // extractVariable handles children
  939. }
  940. // Check for export statements containing non-function variable declarations
  941. // e.g. `export const X = create(...)`, `export const X = { ... }`
  942. else if (nodeType === 'export_statement') {
  943. this.extractExportedVariables(node);
  944. // Don't skip children — still need to visit inner nodes (functions, calls, etc.)
  945. }
  946. // Check for imports
  947. else if (this.extractor.importTypes.includes(nodeType)) {
  948. this.extractImport(node);
  949. }
  950. // Check for function calls
  951. else if (this.extractor.callTypes.includes(nodeType)) {
  952. this.extractCall(node);
  953. }
  954. // Visit children (unless the extract method already visited them)
  955. if (!skipChildren) {
  956. for (let i = 0; i < node.namedChildCount; i++) {
  957. const child = node.namedChild(i);
  958. if (child) {
  959. this.visitNode(child);
  960. }
  961. }
  962. }
  963. }
  964. /**
  965. * Create a Node object
  966. */
  967. private createNode(
  968. kind: NodeKind,
  969. name: string,
  970. node: SyntaxNode,
  971. extra?: Partial<Node>
  972. ): Node {
  973. const id = generateNodeId(this.filePath, kind, name, node.startPosition.row + 1);
  974. const newNode: Node = {
  975. id,
  976. kind,
  977. name,
  978. qualifiedName: this.buildQualifiedName(name),
  979. filePath: this.filePath,
  980. language: this.language,
  981. startLine: node.startPosition.row + 1,
  982. endLine: node.endPosition.row + 1,
  983. startColumn: node.startPosition.column,
  984. endColumn: node.endPosition.column,
  985. updatedAt: Date.now(),
  986. ...extra,
  987. };
  988. this.nodes.push(newNode);
  989. // Add containment edge from parent
  990. if (this.nodeStack.length > 0) {
  991. const parentId = this.nodeStack[this.nodeStack.length - 1];
  992. if (parentId) {
  993. this.edges.push({
  994. source: parentId,
  995. target: id,
  996. kind: 'contains',
  997. });
  998. }
  999. }
  1000. return newNode;
  1001. }
  1002. /**
  1003. * Build qualified name from node stack
  1004. */
  1005. private buildQualifiedName(name: string): string {
  1006. // Get names from the node stack
  1007. const parts: string[] = [this.filePath];
  1008. for (const nodeId of this.nodeStack) {
  1009. const node = this.nodes.find((n) => n.id === nodeId);
  1010. if (node) {
  1011. parts.push(node.name);
  1012. }
  1013. }
  1014. parts.push(name);
  1015. return parts.join('::');
  1016. }
  1017. /**
  1018. * Check if a node has a child of a specific type
  1019. */
  1020. private hasChildOfType(node: SyntaxNode, type: string): boolean {
  1021. for (let i = 0; i < node.childCount; i++) {
  1022. const child = node.child(i);
  1023. if (child?.type === type) {
  1024. return true;
  1025. }
  1026. }
  1027. return false;
  1028. }
  1029. /**
  1030. * Extract a function
  1031. */
  1032. private extractFunction(node: SyntaxNode): void {
  1033. if (!this.extractor) return;
  1034. let name = extractName(node, this.source, this.extractor);
  1035. // For arrow functions and function expressions assigned to variables,
  1036. // resolve the name from the parent variable_declarator.
  1037. // e.g. `export const useAuth = () => { ... }` — the arrow_function node
  1038. // has no `name` field; the name lives on the variable_declarator.
  1039. if (
  1040. name === '<anonymous>' &&
  1041. (node.type === 'arrow_function' || node.type === 'function_expression')
  1042. ) {
  1043. const parent = node.parent;
  1044. if (parent?.type === 'variable_declarator') {
  1045. const varName = getChildByField(parent, 'name');
  1046. if (varName) {
  1047. name = getNodeText(varName, this.source);
  1048. }
  1049. }
  1050. }
  1051. if (name === '<anonymous>') return; // Skip anonymous functions
  1052. const docstring = getPrecedingDocstring(node, this.source);
  1053. const signature = this.extractor.getSignature?.(node, this.source);
  1054. const visibility = this.extractor.getVisibility?.(node);
  1055. const isExported = this.extractor.isExported?.(node, this.source);
  1056. const isAsync = this.extractor.isAsync?.(node);
  1057. const isStatic = this.extractor.isStatic?.(node);
  1058. const funcNode = this.createNode('function', name, node, {
  1059. docstring,
  1060. signature,
  1061. visibility,
  1062. isExported,
  1063. isAsync,
  1064. isStatic,
  1065. });
  1066. // Push to stack and visit body
  1067. this.nodeStack.push(funcNode.id);
  1068. // Dart: function_body is a next sibling of function_signature, not a child
  1069. const body = this.language === 'dart'
  1070. ? node.nextNamedSibling?.type === 'function_body' ? node.nextNamedSibling : null
  1071. : getChildByField(node, this.extractor.bodyField);
  1072. if (body) {
  1073. this.visitFunctionBody(body, funcNode.id);
  1074. }
  1075. this.nodeStack.pop();
  1076. }
  1077. /**
  1078. * Extract a class
  1079. */
  1080. private extractClass(node: SyntaxNode): void {
  1081. if (!this.extractor) return;
  1082. const name = extractName(node, this.source, this.extractor);
  1083. const docstring = getPrecedingDocstring(node, this.source);
  1084. const visibility = this.extractor.getVisibility?.(node);
  1085. const isExported = this.extractor.isExported?.(node, this.source);
  1086. const classNode = this.createNode('class', name, node, {
  1087. docstring,
  1088. visibility,
  1089. isExported,
  1090. });
  1091. // Extract extends/implements
  1092. this.extractInheritance(node, classNode.id);
  1093. // Push to stack and visit body
  1094. this.nodeStack.push(classNode.id);
  1095. let body = getChildByField(node, this.extractor.bodyField);
  1096. // Dart: mixin_declaration uses class_body, extension uses extension_body
  1097. if (!body && this.language === 'dart') {
  1098. body = node.namedChildren.find((c: SyntaxNode) =>
  1099. c.type === 'class_body' || c.type === 'extension_body'
  1100. ) || null;
  1101. }
  1102. if (!body) body = node;
  1103. // Visit all children for methods and properties
  1104. for (let i = 0; i < body.namedChildCount; i++) {
  1105. const child = body.namedChild(i);
  1106. if (child) {
  1107. this.visitNode(child);
  1108. }
  1109. }
  1110. this.nodeStack.pop();
  1111. }
  1112. /**
  1113. * Extract a method
  1114. */
  1115. private extractMethod(node: SyntaxNode): void {
  1116. if (!this.extractor) return;
  1117. // For most languages, only extract as method if inside a class
  1118. // But Go methods are top-level with a receiver, so always treat them as methods
  1119. if (this.nodeStack.length === 0 && this.language !== 'go') {
  1120. // Top-level and not Go, treat as function
  1121. this.extractFunction(node);
  1122. return;
  1123. }
  1124. const name = extractName(node, this.source, this.extractor);
  1125. const docstring = getPrecedingDocstring(node, this.source);
  1126. const signature = this.extractor.getSignature?.(node, this.source);
  1127. const visibility = this.extractor.getVisibility?.(node);
  1128. const isAsync = this.extractor.isAsync?.(node);
  1129. const isStatic = this.extractor.isStatic?.(node);
  1130. const methodNode = this.createNode('method', name, node, {
  1131. docstring,
  1132. signature,
  1133. visibility,
  1134. isAsync,
  1135. isStatic,
  1136. });
  1137. // Push to stack and visit body
  1138. this.nodeStack.push(methodNode.id);
  1139. // Dart: function_body is a next sibling of method_signature, not a child
  1140. const body = this.language === 'dart'
  1141. ? node.nextNamedSibling?.type === 'function_body' ? node.nextNamedSibling : null
  1142. : getChildByField(node, this.extractor.bodyField);
  1143. if (body) {
  1144. this.visitFunctionBody(body, methodNode.id);
  1145. }
  1146. this.nodeStack.pop();
  1147. }
  1148. /**
  1149. * Extract an interface/protocol/trait
  1150. */
  1151. private extractInterface(node: SyntaxNode): void {
  1152. if (!this.extractor) return;
  1153. const name = extractName(node, this.source, this.extractor);
  1154. const docstring = getPrecedingDocstring(node, this.source);
  1155. const isExported = this.extractor.isExported?.(node, this.source);
  1156. // Determine kind based on language
  1157. let kind: NodeKind = 'interface';
  1158. if (this.language === 'rust') kind = 'trait';
  1159. this.createNode(kind, name, node, {
  1160. docstring,
  1161. isExported,
  1162. });
  1163. }
  1164. /**
  1165. * Extract a struct
  1166. */
  1167. private extractStruct(node: SyntaxNode): void {
  1168. if (!this.extractor) return;
  1169. const name = extractName(node, this.source, this.extractor);
  1170. const docstring = getPrecedingDocstring(node, this.source);
  1171. const visibility = this.extractor.getVisibility?.(node);
  1172. const isExported = this.extractor.isExported?.(node, this.source);
  1173. const structNode = this.createNode('struct', name, node, {
  1174. docstring,
  1175. visibility,
  1176. isExported,
  1177. });
  1178. // Push to stack for field extraction
  1179. this.nodeStack.push(structNode.id);
  1180. const body = getChildByField(node, this.extractor.bodyField) || node;
  1181. for (let i = 0; i < body.namedChildCount; i++) {
  1182. const child = body.namedChild(i);
  1183. if (child) {
  1184. this.visitNode(child);
  1185. }
  1186. }
  1187. this.nodeStack.pop();
  1188. }
  1189. /**
  1190. * Extract an enum
  1191. */
  1192. private extractEnum(node: SyntaxNode): void {
  1193. if (!this.extractor) return;
  1194. const name = extractName(node, this.source, this.extractor);
  1195. const docstring = getPrecedingDocstring(node, this.source);
  1196. const visibility = this.extractor.getVisibility?.(node);
  1197. const isExported = this.extractor.isExported?.(node, this.source);
  1198. this.createNode('enum', name, node, {
  1199. docstring,
  1200. visibility,
  1201. isExported,
  1202. });
  1203. }
  1204. /**
  1205. * Extract a variable declaration (const, let, var, etc.)
  1206. *
  1207. * Extracts top-level and module-level variable declarations.
  1208. * Captures the variable name and first 100 chars of initializer in signature for searchability.
  1209. */
  1210. private extractVariable(node: SyntaxNode): void {
  1211. if (!this.extractor) return;
  1212. // Different languages have different variable declaration structures
  1213. // TypeScript/JavaScript: lexical_declaration contains variable_declarator children
  1214. // Python: assignment has left (identifier) and right (value)
  1215. // Go: var_declaration, short_var_declaration, const_declaration
  1216. const isConst = this.extractor.isConst?.(node) ?? false;
  1217. const kind: NodeKind = isConst ? 'constant' : 'variable';
  1218. const docstring = getPrecedingDocstring(node, this.source);
  1219. const isExported = this.extractor.isExported?.(node, this.source) ?? false;
  1220. // Extract variable declarators based on language
  1221. if (this.language === 'typescript' || this.language === 'javascript' ||
  1222. this.language === 'tsx' || this.language === 'jsx') {
  1223. // Handle lexical_declaration and variable_declaration
  1224. // These contain one or more variable_declarator children
  1225. for (let i = 0; i < node.namedChildCount; i++) {
  1226. const child = node.namedChild(i);
  1227. if (child?.type === 'variable_declarator') {
  1228. const nameNode = getChildByField(child, 'name');
  1229. const valueNode = getChildByField(child, 'value');
  1230. if (nameNode) {
  1231. const name = getNodeText(nameNode, this.source);
  1232. // Skip if it looks like a function (arrow function or function expression)
  1233. if (valueNode && (valueNode.type === 'arrow_function' || valueNode.type === 'function_expression')) {
  1234. continue; // Already handled by function extraction
  1235. }
  1236. // Capture first 100 chars of initializer for context (stored in signature for searchability)
  1237. const initValue = valueNode ? getNodeText(valueNode, this.source).slice(0, 100) : undefined;
  1238. const initSignature = initValue ? `= ${initValue}${initValue.length >= 100 ? '...' : ''}` : undefined;
  1239. this.createNode(kind, name, child, {
  1240. docstring,
  1241. signature: initSignature,
  1242. isExported,
  1243. });
  1244. }
  1245. }
  1246. }
  1247. } else if (this.language === 'python' || this.language === 'ruby') {
  1248. // Python/Ruby assignment: left = right
  1249. const left = getChildByField(node, 'left') || node.namedChild(0);
  1250. const right = getChildByField(node, 'right') || node.namedChild(1);
  1251. if (left && left.type === 'identifier') {
  1252. const name = getNodeText(left, this.source);
  1253. // Skip if name starts with lowercase and looks like a function call result
  1254. // Python constants are usually UPPER_CASE
  1255. const initValue = right ? getNodeText(right, this.source).slice(0, 100) : undefined;
  1256. const initSignature = initValue ? `= ${initValue}${initValue.length >= 100 ? '...' : ''}` : undefined;
  1257. this.createNode(kind, name, node, {
  1258. docstring,
  1259. signature: initSignature,
  1260. });
  1261. }
  1262. } else if (this.language === 'go') {
  1263. // Go: var_declaration, short_var_declaration, const_declaration
  1264. // These can have multiple identifiers on the left
  1265. const specs = node.namedChildren.filter(c =>
  1266. c.type === 'var_spec' || c.type === 'const_spec'
  1267. );
  1268. for (const spec of specs) {
  1269. const nameNode = spec.namedChild(0);
  1270. if (nameNode && nameNode.type === 'identifier') {
  1271. const name = getNodeText(nameNode, this.source);
  1272. const valueNode = spec.namedChildCount > 1 ? spec.namedChild(spec.namedChildCount - 1) : null;
  1273. const initValue = valueNode ? getNodeText(valueNode, this.source).slice(0, 100) : undefined;
  1274. const initSignature = initValue ? `= ${initValue}${initValue.length >= 100 ? '...' : ''}` : undefined;
  1275. this.createNode(node.type === 'const_declaration' ? 'constant' : 'variable', name, spec, {
  1276. docstring,
  1277. signature: initSignature,
  1278. });
  1279. }
  1280. }
  1281. // Handle short_var_declaration (:=)
  1282. if (node.type === 'short_var_declaration') {
  1283. const left = getChildByField(node, 'left');
  1284. const right = getChildByField(node, 'right');
  1285. if (left) {
  1286. // Can be expression_list with multiple identifiers
  1287. const identifiers = left.type === 'expression_list'
  1288. ? left.namedChildren.filter(c => c.type === 'identifier')
  1289. : [left];
  1290. for (const id of identifiers) {
  1291. const name = getNodeText(id, this.source);
  1292. const initValue = right ? getNodeText(right, this.source).slice(0, 100) : undefined;
  1293. const initSignature = initValue ? `= ${initValue}${initValue.length >= 100 ? '...' : ''}` : undefined;
  1294. this.createNode('variable', name, node, {
  1295. docstring,
  1296. signature: initSignature,
  1297. });
  1298. }
  1299. }
  1300. }
  1301. } else {
  1302. // Generic fallback for other languages
  1303. // Try to find identifier children
  1304. for (let i = 0; i < node.namedChildCount; i++) {
  1305. const child = node.namedChild(i);
  1306. if (child?.type === 'identifier' || child?.type === 'variable_declarator') {
  1307. const name = child.type === 'identifier'
  1308. ? getNodeText(child, this.source)
  1309. : extractName(child, this.source, this.extractor);
  1310. if (name && name !== '<anonymous>') {
  1311. this.createNode(kind, name, child, {
  1312. docstring,
  1313. isExported,
  1314. });
  1315. }
  1316. }
  1317. }
  1318. }
  1319. }
  1320. /**
  1321. * Extract a type alias (e.g. `export type X = ...` in TypeScript)
  1322. */
  1323. private extractTypeAlias(node: SyntaxNode): void {
  1324. if (!this.extractor) return;
  1325. const name = extractName(node, this.source, this.extractor);
  1326. if (name === '<anonymous>') return;
  1327. const docstring = getPrecedingDocstring(node, this.source);
  1328. const isExported = this.extractor.isExported?.(node, this.source);
  1329. this.createNode('type_alias', name, node, {
  1330. docstring,
  1331. isExported,
  1332. });
  1333. }
  1334. /**
  1335. * Extract an exported variable declaration that isn't a function.
  1336. * Handles patterns like:
  1337. * export const X = create(...)
  1338. * export const X = { ... }
  1339. * export const X = [...]
  1340. * export const X = "value"
  1341. *
  1342. * This is called for `export_statement` nodes that contain a
  1343. * `lexical_declaration` with `variable_declarator` children whose
  1344. * values are NOT already handled by functionTypes (arrow_function,
  1345. * function_expression).
  1346. */
  1347. private extractExportedVariables(exportNode: SyntaxNode): void {
  1348. if (!this.extractor) return;
  1349. // Find the lexical_declaration or variable_declaration child
  1350. for (let i = 0; i < exportNode.namedChildCount; i++) {
  1351. const decl = exportNode.namedChild(i);
  1352. if (!decl || (decl.type !== 'lexical_declaration' && decl.type !== 'variable_declaration')) {
  1353. continue;
  1354. }
  1355. // Iterate over each variable_declarator in the declaration
  1356. for (let j = 0; j < decl.namedChildCount; j++) {
  1357. const declarator = decl.namedChild(j);
  1358. if (!declarator || declarator.type !== 'variable_declarator') continue;
  1359. const nameNode = getChildByField(declarator, 'name');
  1360. if (!nameNode) continue;
  1361. const name = getNodeText(nameNode, this.source);
  1362. // Skip if the value is a function type — those are already handled
  1363. // by extractFunction via the functionTypes dispatch
  1364. const value = getChildByField(declarator, 'value');
  1365. if (value) {
  1366. const valueType = value.type;
  1367. if (
  1368. this.extractor.functionTypes.includes(valueType)
  1369. ) {
  1370. continue; // Already handled by extractFunction
  1371. }
  1372. }
  1373. const docstring = getPrecedingDocstring(exportNode, this.source);
  1374. this.createNode('variable', name, declarator, {
  1375. docstring,
  1376. isExported: true,
  1377. });
  1378. }
  1379. }
  1380. }
  1381. /**
  1382. * Extract an import
  1383. *
  1384. * Creates an import node with the full import statement stored in signature for searchability.
  1385. * Also creates unresolved references for resolution purposes.
  1386. */
  1387. private extractImport(node: SyntaxNode): void {
  1388. const importText = getNodeText(node, this.source).trim();
  1389. // Extract module/package name based on language
  1390. let moduleName = '';
  1391. if (this.language === 'typescript' || this.language === 'javascript' ||
  1392. this.language === 'tsx' || this.language === 'jsx') {
  1393. const source = getChildByField(node, 'source');
  1394. if (source) {
  1395. moduleName = getNodeText(source, this.source).replace(/['"]/g, '');
  1396. }
  1397. // Create import node with full statement as signature for searchability
  1398. if (moduleName) {
  1399. this.createNode('import', moduleName, node, {
  1400. signature: importText,
  1401. });
  1402. }
  1403. } else if (this.language === 'python') {
  1404. // Python has two import forms:
  1405. // 1. import_statement: import os, sys
  1406. // 2. import_from_statement: from os import path
  1407. if (node.type === 'import_from_statement') {
  1408. const moduleNode = getChildByField(node, 'module_name');
  1409. if (moduleNode) {
  1410. moduleName = getNodeText(moduleNode, this.source);
  1411. }
  1412. } else {
  1413. // import_statement - may have multiple modules
  1414. // Can be dotted_name (import os) or aliased_import (import numpy as np)
  1415. for (let i = 0; i < node.namedChildCount; i++) {
  1416. const child = node.namedChild(i);
  1417. if (child?.type === 'dotted_name') {
  1418. const name = getNodeText(child, this.source);
  1419. this.createNode('import', name, node, {
  1420. signature: importText,
  1421. });
  1422. } else if (child?.type === 'aliased_import') {
  1423. // Extract the module name from inside aliased_import
  1424. const dottedName = child.namedChildren.find(c => c.type === 'dotted_name');
  1425. if (dottedName) {
  1426. const name = getNodeText(dottedName, this.source);
  1427. this.createNode('import', name, node, {
  1428. signature: importText,
  1429. });
  1430. }
  1431. }
  1432. }
  1433. // Skip creating another node below if we handled import_statement
  1434. if (node.type === 'import_statement') {
  1435. return;
  1436. }
  1437. }
  1438. if (moduleName) {
  1439. this.createNode('import', moduleName, node, {
  1440. signature: importText,
  1441. });
  1442. }
  1443. } else if (this.language === 'go') {
  1444. // Go imports can be single or grouped
  1445. // Single: import "fmt" - uses import_spec directly as child
  1446. // Grouped: import ( "fmt" \n "os" ) - uses import_spec_list containing import_spec children
  1447. // Helper function to extract path from import_spec
  1448. const extractFromSpec = (spec: SyntaxNode): void => {
  1449. const stringLiteral = spec.namedChildren.find(c => c.type === 'interpreted_string_literal');
  1450. if (stringLiteral) {
  1451. const path = getNodeText(stringLiteral, this.source).replace(/['"]/g, '');
  1452. if (path) {
  1453. this.createNode('import', path, spec, {
  1454. signature: getNodeText(spec, this.source).trim(),
  1455. });
  1456. }
  1457. }
  1458. };
  1459. // Find import_spec_list for grouped imports
  1460. const importSpecList = node.namedChildren.find(c => c.type === 'import_spec_list');
  1461. if (importSpecList) {
  1462. // Grouped imports - iterate through import_spec children
  1463. const importSpecs = importSpecList.namedChildren.filter(c => c.type === 'import_spec');
  1464. for (const spec of importSpecs) {
  1465. extractFromSpec(spec);
  1466. }
  1467. } else {
  1468. // Single import: import "fmt" - import_spec is direct child
  1469. const importSpec = node.namedChildren.find(c => c.type === 'import_spec');
  1470. if (importSpec) {
  1471. extractFromSpec(importSpec);
  1472. }
  1473. }
  1474. return; // Go handled completely above
  1475. } else if (this.language === 'rust') {
  1476. // Rust use declarations
  1477. // use std::{ffi::OsStr, io}; -> scoped_use_list with identifier "std"
  1478. // use crate::error::Error; -> scoped_identifier starting with "crate"
  1479. // use super::utils; -> scoped_identifier starting with "super"
  1480. // Helper to get the root crate/module from a scoped path
  1481. const getRootModule = (scopedNode: SyntaxNode): string => {
  1482. // Recursively find the leftmost identifier/crate/super/self
  1483. const firstChild = scopedNode.namedChild(0);
  1484. if (!firstChild) return getNodeText(scopedNode, this.source);
  1485. if (firstChild.type === 'identifier' ||
  1486. firstChild.type === 'crate' ||
  1487. firstChild.type === 'super' ||
  1488. firstChild.type === 'self') {
  1489. return getNodeText(firstChild, this.source);
  1490. } else if (firstChild.type === 'scoped_identifier') {
  1491. return getRootModule(firstChild);
  1492. }
  1493. return getNodeText(firstChild, this.source);
  1494. };
  1495. // Find the use argument (scoped_use_list or scoped_identifier)
  1496. const useArg = node.namedChildren.find(c =>
  1497. c.type === 'scoped_use_list' ||
  1498. c.type === 'scoped_identifier' ||
  1499. c.type === 'use_list' ||
  1500. c.type === 'identifier'
  1501. );
  1502. if (useArg) {
  1503. moduleName = getRootModule(useArg);
  1504. this.createNode('import', moduleName, node, {
  1505. signature: importText,
  1506. });
  1507. }
  1508. return; // Rust handled completely above
  1509. } else if (this.language === 'swift') {
  1510. // Swift imports: import Foundation, @testable import Alamofire
  1511. // AST structure: import_declaration -> identifier -> simple_identifier
  1512. const identifier = node.namedChildren.find(c => c.type === 'identifier');
  1513. if (identifier) {
  1514. moduleName = getNodeText(identifier, this.source);
  1515. this.createNode('import', moduleName, node, {
  1516. signature: importText,
  1517. });
  1518. }
  1519. return; // Swift handled completely above
  1520. } else if (this.language === 'kotlin') {
  1521. // Kotlin imports: import java.io.IOException, import x.y.Z as Alias, import x.y.*
  1522. // AST structure: import_header -> identifier (dotted path)
  1523. const identifier = node.namedChildren.find(c => c.type === 'identifier');
  1524. if (identifier) {
  1525. moduleName = getNodeText(identifier, this.source);
  1526. this.createNode('import', moduleName, node, {
  1527. signature: importText,
  1528. });
  1529. }
  1530. return; // Kotlin handled completely above
  1531. } else if (this.language === 'java') {
  1532. // Java imports: import java.util.List, import static x.Y.method, import x.y.*
  1533. // AST structure: import_declaration -> scoped_identifier (dotted path)
  1534. const scopedId = node.namedChildren.find(c => c.type === 'scoped_identifier');
  1535. if (scopedId) {
  1536. moduleName = getNodeText(scopedId, this.source);
  1537. this.createNode('import', moduleName, node, {
  1538. signature: importText,
  1539. });
  1540. }
  1541. return; // Java handled completely above
  1542. } else if (this.language === 'csharp') {
  1543. // C# using directives: using System, using System.Collections.Generic, using static X, using Alias = X
  1544. // AST structure: using_directive -> qualified_name (dotted) or identifier (simple)
  1545. // For alias imports: identifier = qualified_name - we want the qualified_name
  1546. const qualifiedName = node.namedChildren.find(c => c.type === 'qualified_name');
  1547. if (qualifiedName) {
  1548. moduleName = getNodeText(qualifiedName, this.source);
  1549. } else {
  1550. // Simple namespace like "using System;" - get the first identifier
  1551. const identifier = node.namedChildren.find(c => c.type === 'identifier');
  1552. if (identifier) {
  1553. moduleName = getNodeText(identifier, this.source);
  1554. }
  1555. }
  1556. if (moduleName) {
  1557. this.createNode('import', moduleName, node, {
  1558. signature: importText,
  1559. });
  1560. }
  1561. return; // C# handled completely above
  1562. } else if (this.language === 'php') {
  1563. // PHP use declarations: use X\Y\Z, use X as Y, use function X\func, use X\{A, B}
  1564. // AST structure: namespace_use_declaration -> namespace_use_clause -> qualified_name or name
  1565. // Check for grouped imports first: use X\{A, B}
  1566. const namespacePrefix = node.namedChildren.find(c => c.type === 'namespace_name');
  1567. const useGroup = node.namedChildren.find(c => c.type === 'namespace_use_group');
  1568. if (namespacePrefix && useGroup) {
  1569. // Grouped import - create one import per item
  1570. const prefix = getNodeText(namespacePrefix, this.source);
  1571. const useClauses = useGroup.namedChildren.filter((c: SyntaxNode) => c.type === 'namespace_use_clause');
  1572. for (const clause of useClauses) {
  1573. const name = clause.namedChildren.find((c: SyntaxNode) => c.type === 'name');
  1574. if (name) {
  1575. const fullPath = `${prefix}\\${getNodeText(name, this.source)}`;
  1576. this.createNode('import', fullPath, node, {
  1577. signature: importText,
  1578. });
  1579. }
  1580. }
  1581. return;
  1582. }
  1583. // Single import - find namespace_use_clause
  1584. const useClause = node.namedChildren.find(c => c.type === 'namespace_use_clause');
  1585. if (useClause) {
  1586. // Look for qualified_name (full path) or name (simple)
  1587. const qualifiedName = useClause.namedChildren.find((c: SyntaxNode) => c.type === 'qualified_name');
  1588. if (qualifiedName) {
  1589. moduleName = getNodeText(qualifiedName, this.source);
  1590. } else {
  1591. const name = useClause.namedChildren.find((c: SyntaxNode) => c.type === 'name');
  1592. if (name) {
  1593. moduleName = getNodeText(name, this.source);
  1594. }
  1595. }
  1596. }
  1597. if (moduleName) {
  1598. this.createNode('import', moduleName, node, {
  1599. signature: importText,
  1600. });
  1601. }
  1602. return; // PHP handled completely above
  1603. } else if (this.language === 'ruby') {
  1604. // Ruby imports: require 'json', require_relative '../helper'
  1605. // AST structure: call -> identifier (require/require_relative) + argument_list -> string -> string_content
  1606. // Check if this is a require/require_relative call
  1607. const identifier = node.namedChildren.find(c => c.type === 'identifier');
  1608. if (!identifier) return;
  1609. const methodName = getNodeText(identifier, this.source);
  1610. if (methodName !== 'require' && methodName !== 'require_relative') {
  1611. return; // Not an import, skip
  1612. }
  1613. // Find the argument (string)
  1614. const argList = node.namedChildren.find(c => c.type === 'argument_list');
  1615. if (argList) {
  1616. const stringNode = argList.namedChildren.find((c: SyntaxNode) => c.type === 'string');
  1617. if (stringNode) {
  1618. // Get string_content (without quotes)
  1619. const stringContent = stringNode.namedChildren.find((c: SyntaxNode) => c.type === 'string_content');
  1620. if (stringContent) {
  1621. moduleName = getNodeText(stringContent, this.source);
  1622. }
  1623. }
  1624. }
  1625. if (moduleName) {
  1626. this.createNode('import', moduleName, node, {
  1627. signature: importText,
  1628. });
  1629. }
  1630. return; // Ruby handled completely above
  1631. } else if (this.language === 'dart') {
  1632. // Dart imports: import 'dart:async'; import 'package:foo/bar.dart' as bar;
  1633. // AST: import_or_export -> library_import -> import_specification -> configurable_uri -> uri -> string_literal
  1634. const libraryImport = node.namedChildren.find(c => c.type === 'library_import');
  1635. if (libraryImport) {
  1636. const importSpec = libraryImport.namedChildren.find((c: SyntaxNode) => c.type === 'import_specification');
  1637. if (importSpec) {
  1638. const configurableUri = importSpec.namedChildren.find((c: SyntaxNode) => c.type === 'configurable_uri');
  1639. if (configurableUri) {
  1640. const uri = configurableUri.namedChildren.find((c: SyntaxNode) => c.type === 'uri');
  1641. if (uri) {
  1642. const stringLiteral = uri.namedChildren.find((c: SyntaxNode) => c.type === 'string_literal');
  1643. if (stringLiteral) {
  1644. moduleName = getNodeText(stringLiteral, this.source).replace(/['"]/g, '');
  1645. }
  1646. }
  1647. }
  1648. }
  1649. }
  1650. // Also handle exports: export 'src/foo.dart';
  1651. const libraryExport = node.namedChildren.find(c => c.type === 'library_export');
  1652. if (libraryExport) {
  1653. const configurableUri = libraryExport.namedChildren.find((c: SyntaxNode) => c.type === 'configurable_uri');
  1654. if (configurableUri) {
  1655. const uri = configurableUri.namedChildren.find((c: SyntaxNode) => c.type === 'uri');
  1656. if (uri) {
  1657. const stringLiteral = uri.namedChildren.find((c: SyntaxNode) => c.type === 'string_literal');
  1658. if (stringLiteral) {
  1659. moduleName = getNodeText(stringLiteral, this.source).replace(/['"]/g, '');
  1660. }
  1661. }
  1662. }
  1663. }
  1664. if (moduleName) {
  1665. this.createNode('import', moduleName, node, {
  1666. signature: importText,
  1667. });
  1668. }
  1669. return; // Dart handled completely above
  1670. } else if (this.language === 'c' || this.language === 'cpp') {
  1671. // C/C++ includes: #include <iostream>, #include "myheader.h"
  1672. // AST: preproc_include -> system_lib_string (<...>) or string_literal ("...")
  1673. // Check for system include: <path>
  1674. const systemLib = node.namedChildren.find(c => c.type === 'system_lib_string');
  1675. if (systemLib) {
  1676. // Remove angle brackets: <iostream> -> iostream
  1677. moduleName = getNodeText(systemLib, this.source).replace(/^<|>$/g, '');
  1678. } else {
  1679. // Check for local include: "path"
  1680. const stringLiteral = node.namedChildren.find(c => c.type === 'string_literal');
  1681. if (stringLiteral) {
  1682. const stringContent = stringLiteral.namedChildren.find((c: SyntaxNode) => c.type === 'string_content');
  1683. if (stringContent) {
  1684. moduleName = getNodeText(stringContent, this.source);
  1685. }
  1686. }
  1687. }
  1688. if (moduleName) {
  1689. this.createNode('import', moduleName, node, {
  1690. signature: importText,
  1691. });
  1692. }
  1693. return; // C/C++ handled completely above
  1694. } else {
  1695. // Generic extraction for other languages
  1696. moduleName = importText;
  1697. if (moduleName) {
  1698. this.createNode('import', moduleName, node, {
  1699. signature: importText,
  1700. });
  1701. }
  1702. }
  1703. // Keep unresolved reference creation for resolution purposes
  1704. // This is used to resolve imports to their target files/modules
  1705. if (moduleName && this.nodeStack.length > 0) {
  1706. const parentId = this.nodeStack[this.nodeStack.length - 1];
  1707. if (parentId) {
  1708. this.unresolvedReferences.push({
  1709. fromNodeId: parentId,
  1710. referenceName: moduleName,
  1711. referenceKind: 'imports',
  1712. line: node.startPosition.row + 1,
  1713. column: node.startPosition.column,
  1714. });
  1715. }
  1716. }
  1717. }
  1718. /**
  1719. * Extract a function call
  1720. */
  1721. private extractCall(node: SyntaxNode): void {
  1722. if (this.nodeStack.length === 0) return;
  1723. const callerId = this.nodeStack[this.nodeStack.length - 1];
  1724. if (!callerId) return;
  1725. // Get the function/method being called
  1726. let calleeName = '';
  1727. const func = getChildByField(node, 'function') || node.namedChild(0);
  1728. if (func) {
  1729. if (func.type === 'member_expression' || func.type === 'attribute') {
  1730. // Method call: obj.method()
  1731. const property = getChildByField(func, 'property') || func.namedChild(1);
  1732. if (property) {
  1733. calleeName = getNodeText(property, this.source);
  1734. }
  1735. } else if (func.type === 'scoped_identifier' || func.type === 'scoped_call_expression') {
  1736. // Scoped call: Module::function()
  1737. calleeName = getNodeText(func, this.source);
  1738. } else {
  1739. calleeName = getNodeText(func, this.source);
  1740. }
  1741. }
  1742. if (calleeName) {
  1743. this.unresolvedReferences.push({
  1744. fromNodeId: callerId,
  1745. referenceName: calleeName,
  1746. referenceKind: 'calls',
  1747. line: node.startPosition.row + 1,
  1748. column: node.startPosition.column,
  1749. });
  1750. }
  1751. }
  1752. /**
  1753. * Visit function body and extract calls
  1754. */
  1755. private visitFunctionBody(body: SyntaxNode, _functionId: string): void {
  1756. if (!this.extractor) return;
  1757. // Recursively find all call expressions
  1758. const visitForCalls = (node: SyntaxNode): void => {
  1759. if (this.extractor!.callTypes.includes(node.type)) {
  1760. this.extractCall(node);
  1761. }
  1762. for (let i = 0; i < node.namedChildCount; i++) {
  1763. const child = node.namedChild(i);
  1764. if (child) {
  1765. visitForCalls(child);
  1766. }
  1767. }
  1768. };
  1769. visitForCalls(body);
  1770. }
  1771. /**
  1772. * Extract inheritance relationships
  1773. */
  1774. private extractInheritance(node: SyntaxNode, classId: string): void {
  1775. // Look for extends/implements clauses
  1776. for (let i = 0; i < node.namedChildCount; i++) {
  1777. const child = node.namedChild(i);
  1778. if (!child) continue;
  1779. if (
  1780. child.type === 'extends_clause' ||
  1781. child.type === 'class_heritage' ||
  1782. child.type === 'superclass'
  1783. ) {
  1784. // Extract parent class name
  1785. const superclass = child.namedChild(0);
  1786. if (superclass) {
  1787. const name = getNodeText(superclass, this.source);
  1788. this.unresolvedReferences.push({
  1789. fromNodeId: classId,
  1790. referenceName: name,
  1791. referenceKind: 'extends',
  1792. line: child.startPosition.row + 1,
  1793. column: child.startPosition.column,
  1794. });
  1795. }
  1796. }
  1797. if (
  1798. child.type === 'implements_clause' ||
  1799. child.type === 'class_interface_clause' ||
  1800. child.type === 'interfaces' // Dart
  1801. ) {
  1802. // Extract implemented interfaces
  1803. for (let j = 0; j < child.namedChildCount; j++) {
  1804. const iface = child.namedChild(j);
  1805. if (iface) {
  1806. const name = getNodeText(iface, this.source);
  1807. this.unresolvedReferences.push({
  1808. fromNodeId: classId,
  1809. referenceName: name,
  1810. referenceKind: 'implements',
  1811. line: iface.startPosition.row + 1,
  1812. column: iface.startPosition.column,
  1813. });
  1814. }
  1815. }
  1816. }
  1817. }
  1818. }
  1819. }
  1820. /**
  1821. * LiquidExtractor - Extracts relationships from Liquid template files
  1822. *
  1823. * Liquid is a templating language (used by Shopify, Jekyll, etc.) that doesn't
  1824. * have traditional functions or classes. Instead, we extract:
  1825. * - Section references ({% section 'name' %})
  1826. * - Snippet references ({% render 'name' %} and {% include 'name' %})
  1827. * - Schema blocks ({% schema %}...{% endschema %})
  1828. */
  1829. export class LiquidExtractor {
  1830. private filePath: string;
  1831. private source: string;
  1832. private nodes: Node[] = [];
  1833. private edges: Edge[] = [];
  1834. private unresolvedReferences: UnresolvedReference[] = [];
  1835. private errors: ExtractionError[] = [];
  1836. constructor(filePath: string, source: string) {
  1837. this.filePath = filePath;
  1838. this.source = source;
  1839. }
  1840. /**
  1841. * Extract from Liquid source
  1842. */
  1843. extract(): ExtractionResult {
  1844. const startTime = Date.now();
  1845. try {
  1846. // Create file node
  1847. const fileNode = this.createFileNode();
  1848. // Extract render/include statements (snippet references)
  1849. this.extractSnippetReferences(fileNode.id);
  1850. // Extract section references
  1851. this.extractSectionReferences(fileNode.id);
  1852. // Extract schema block
  1853. this.extractSchema(fileNode.id);
  1854. // Extract assign statements as variables
  1855. this.extractAssignments(fileNode.id);
  1856. } catch (error) {
  1857. captureException(error, { operation: 'liquid-extraction', filePath: this.filePath });
  1858. this.errors.push({
  1859. message: `Liquid extraction error: ${error instanceof Error ? error.message : String(error)}`,
  1860. severity: 'error',
  1861. });
  1862. }
  1863. return {
  1864. nodes: this.nodes,
  1865. edges: this.edges,
  1866. unresolvedReferences: this.unresolvedReferences,
  1867. errors: this.errors,
  1868. durationMs: Date.now() - startTime,
  1869. };
  1870. }
  1871. /**
  1872. * Create a file node for the Liquid template
  1873. */
  1874. private createFileNode(): Node {
  1875. const lines = this.source.split('\n');
  1876. const id = generateNodeId(this.filePath, 'file', this.filePath, 1);
  1877. const fileNode: Node = {
  1878. id,
  1879. kind: 'file',
  1880. name: this.filePath.split('/').pop() || this.filePath,
  1881. qualifiedName: this.filePath,
  1882. filePath: this.filePath,
  1883. language: 'liquid',
  1884. startLine: 1,
  1885. endLine: lines.length,
  1886. startColumn: 0,
  1887. endColumn: lines[lines.length - 1]?.length || 0,
  1888. updatedAt: Date.now(),
  1889. };
  1890. this.nodes.push(fileNode);
  1891. return fileNode;
  1892. }
  1893. /**
  1894. * Extract {% render 'snippet' %} and {% include 'snippet' %} references
  1895. */
  1896. private extractSnippetReferences(fileNodeId: string): void {
  1897. // Match {% render 'name' %} or {% include 'name' %} with optional parameters
  1898. const renderRegex = /\{%[-]?\s*(render|include)\s+['"]([^'"]+)['"]/g;
  1899. let match;
  1900. while ((match = renderRegex.exec(this.source)) !== null) {
  1901. const [fullMatch, tagType, snippetName] = match;
  1902. const line = this.getLineNumber(match.index);
  1903. // Create an import node for searchability
  1904. const importNodeId = generateNodeId(this.filePath, 'import', snippetName!, line);
  1905. const importNode: Node = {
  1906. id: importNodeId,
  1907. kind: 'import',
  1908. name: snippetName!,
  1909. qualifiedName: `${this.filePath}::import:${snippetName}`,
  1910. filePath: this.filePath,
  1911. language: 'liquid',
  1912. signature: fullMatch,
  1913. startLine: line,
  1914. endLine: line,
  1915. startColumn: match.index - this.getLineStart(line),
  1916. endColumn: match.index - this.getLineStart(line) + fullMatch.length,
  1917. updatedAt: Date.now(),
  1918. };
  1919. this.nodes.push(importNode);
  1920. // Add containment edge from file to import
  1921. this.edges.push({
  1922. source: fileNodeId,
  1923. target: importNodeId,
  1924. kind: 'contains',
  1925. });
  1926. // Create a component node for the snippet reference
  1927. const nodeId = generateNodeId(this.filePath, 'component', `${tagType}:${snippetName}`, line);
  1928. const node: Node = {
  1929. id: nodeId,
  1930. kind: 'component',
  1931. name: snippetName!,
  1932. qualifiedName: `${this.filePath}::${tagType}:${snippetName}`,
  1933. filePath: this.filePath,
  1934. language: 'liquid',
  1935. startLine: line,
  1936. endLine: line,
  1937. startColumn: match.index - this.getLineStart(line),
  1938. endColumn: match.index - this.getLineStart(line) + fullMatch.length,
  1939. updatedAt: Date.now(),
  1940. };
  1941. this.nodes.push(node);
  1942. // Add containment edge from file
  1943. this.edges.push({
  1944. source: fileNodeId,
  1945. target: nodeId,
  1946. kind: 'contains',
  1947. });
  1948. // Add unresolved reference to the snippet file
  1949. this.unresolvedReferences.push({
  1950. fromNodeId: fileNodeId,
  1951. referenceName: `snippets/${snippetName}.liquid`,
  1952. referenceKind: 'references',
  1953. line,
  1954. column: match.index - this.getLineStart(line),
  1955. });
  1956. }
  1957. }
  1958. /**
  1959. * Extract {% section 'name' %} references
  1960. */
  1961. private extractSectionReferences(fileNodeId: string): void {
  1962. // Match {% section 'name' %}
  1963. const sectionRegex = /\{%[-]?\s*section\s+['"]([^'"]+)['"]/g;
  1964. let match;
  1965. while ((match = sectionRegex.exec(this.source)) !== null) {
  1966. const [fullMatch, sectionName] = match;
  1967. const line = this.getLineNumber(match.index);
  1968. // Create an import node for searchability
  1969. const importNodeId = generateNodeId(this.filePath, 'import', sectionName!, line);
  1970. const importNode: Node = {
  1971. id: importNodeId,
  1972. kind: 'import',
  1973. name: sectionName!,
  1974. qualifiedName: `${this.filePath}::import:${sectionName}`,
  1975. filePath: this.filePath,
  1976. language: 'liquid',
  1977. signature: fullMatch,
  1978. startLine: line,
  1979. endLine: line,
  1980. startColumn: match.index - this.getLineStart(line),
  1981. endColumn: match.index - this.getLineStart(line) + fullMatch.length,
  1982. updatedAt: Date.now(),
  1983. };
  1984. this.nodes.push(importNode);
  1985. // Add containment edge from file to import
  1986. this.edges.push({
  1987. source: fileNodeId,
  1988. target: importNodeId,
  1989. kind: 'contains',
  1990. });
  1991. // Create a component node for the section reference
  1992. const nodeId = generateNodeId(this.filePath, 'component', `section:${sectionName}`, line);
  1993. const node: Node = {
  1994. id: nodeId,
  1995. kind: 'component',
  1996. name: sectionName!,
  1997. qualifiedName: `${this.filePath}::section:${sectionName}`,
  1998. filePath: this.filePath,
  1999. language: 'liquid',
  2000. startLine: line,
  2001. endLine: line,
  2002. startColumn: match.index - this.getLineStart(line),
  2003. endColumn: match.index - this.getLineStart(line) + fullMatch.length,
  2004. updatedAt: Date.now(),
  2005. };
  2006. this.nodes.push(node);
  2007. // Add containment edge from file
  2008. this.edges.push({
  2009. source: fileNodeId,
  2010. target: nodeId,
  2011. kind: 'contains',
  2012. });
  2013. // Add unresolved reference to the section file
  2014. this.unresolvedReferences.push({
  2015. fromNodeId: fileNodeId,
  2016. referenceName: `sections/${sectionName}.liquid`,
  2017. referenceKind: 'references',
  2018. line,
  2019. column: match.index - this.getLineStart(line),
  2020. });
  2021. }
  2022. }
  2023. /**
  2024. * Extract {% schema %}...{% endschema %} blocks
  2025. */
  2026. private extractSchema(fileNodeId: string): void {
  2027. // Match {% schema %}...{% endschema %}
  2028. const schemaRegex = /\{%[-]?\s*schema\s*[-]?%\}([\s\S]*?)\{%[-]?\s*endschema\s*[-]?%\}/g;
  2029. let match;
  2030. while ((match = schemaRegex.exec(this.source)) !== null) {
  2031. const [fullMatch, schemaContent] = match;
  2032. const startLine = this.getLineNumber(match.index);
  2033. const endLine = this.getLineNumber(match.index + fullMatch.length);
  2034. // Try to parse the schema JSON to get the name
  2035. let schemaName = 'schema';
  2036. try {
  2037. const schemaJson = JSON.parse(schemaContent!);
  2038. if (schemaJson.name) {
  2039. schemaName = schemaJson.name;
  2040. }
  2041. } catch {
  2042. // Schema isn't valid JSON, use default name
  2043. }
  2044. // Create a node for the schema
  2045. const nodeId = generateNodeId(this.filePath, 'constant', `schema:${schemaName}`, startLine);
  2046. const node: Node = {
  2047. id: nodeId,
  2048. kind: 'constant',
  2049. name: schemaName,
  2050. qualifiedName: `${this.filePath}::schema:${schemaName}`,
  2051. filePath: this.filePath,
  2052. language: 'liquid',
  2053. startLine,
  2054. endLine,
  2055. startColumn: match.index - this.getLineStart(startLine),
  2056. endColumn: 0,
  2057. docstring: schemaContent?.trim().substring(0, 200), // Store first 200 chars as docstring
  2058. updatedAt: Date.now(),
  2059. };
  2060. this.nodes.push(node);
  2061. // Add containment edge from file
  2062. this.edges.push({
  2063. source: fileNodeId,
  2064. target: nodeId,
  2065. kind: 'contains',
  2066. });
  2067. }
  2068. }
  2069. /**
  2070. * Extract {% assign var = value %} statements
  2071. */
  2072. private extractAssignments(fileNodeId: string): void {
  2073. // Match {% assign variable_name = ... %}
  2074. const assignRegex = /\{%[-]?\s*assign\s+(\w+)\s*=/g;
  2075. let match;
  2076. while ((match = assignRegex.exec(this.source)) !== null) {
  2077. const [, variableName] = match;
  2078. const line = this.getLineNumber(match.index);
  2079. // Create a variable node
  2080. const nodeId = generateNodeId(this.filePath, 'variable', variableName!, line);
  2081. const node: Node = {
  2082. id: nodeId,
  2083. kind: 'variable',
  2084. name: variableName!,
  2085. qualifiedName: `${this.filePath}::${variableName}`,
  2086. filePath: this.filePath,
  2087. language: 'liquid',
  2088. startLine: line,
  2089. endLine: line,
  2090. startColumn: match.index - this.getLineStart(line),
  2091. endColumn: match.index - this.getLineStart(line) + match[0].length,
  2092. updatedAt: Date.now(),
  2093. };
  2094. this.nodes.push(node);
  2095. // Add containment edge from file
  2096. this.edges.push({
  2097. source: fileNodeId,
  2098. target: nodeId,
  2099. kind: 'contains',
  2100. });
  2101. }
  2102. }
  2103. /**
  2104. * Get the line number for a character index
  2105. */
  2106. private getLineNumber(index: number): number {
  2107. const substring = this.source.substring(0, index);
  2108. return (substring.match(/\n/g) || []).length + 1;
  2109. }
  2110. /**
  2111. * Get the character index of the start of a line
  2112. */
  2113. private getLineStart(lineNumber: number): number {
  2114. const lines = this.source.split('\n');
  2115. let index = 0;
  2116. for (let i = 0; i < lineNumber - 1 && i < lines.length; i++) {
  2117. index += lines[i]!.length + 1; // +1 for newline
  2118. }
  2119. return index;
  2120. }
  2121. }
  2122. /**
  2123. * Extract nodes and edges from source code
  2124. */
  2125. export function extractFromSource(
  2126. filePath: string,
  2127. source: string,
  2128. language?: Language
  2129. ): ExtractionResult {
  2130. const detectedLanguage = language || detectLanguage(filePath);
  2131. // Use custom extractor for Liquid
  2132. if (detectedLanguage === 'liquid') {
  2133. const extractor = new LiquidExtractor(filePath, source);
  2134. return extractor.extract();
  2135. }
  2136. const extractor = new TreeSitterExtractor(filePath, source, detectedLanguage);
  2137. return extractor.extract();
  2138. }