gitnexus 1.4.6 → 1.4.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/README.md +22 -1
  2. package/dist/cli/ai-context.d.ts +1 -1
  3. package/dist/cli/ai-context.js +1 -1
  4. package/dist/cli/analyze.d.ts +2 -0
  5. package/dist/cli/analyze.js +54 -21
  6. package/dist/cli/index.js +2 -1
  7. package/dist/cli/setup.js +78 -1
  8. package/dist/config/supported-languages.d.ts +30 -0
  9. package/dist/config/supported-languages.js +30 -0
  10. package/dist/core/embeddings/embedder.d.ts +6 -1
  11. package/dist/core/embeddings/embedder.js +65 -5
  12. package/dist/core/embeddings/embedding-pipeline.js +11 -9
  13. package/dist/core/embeddings/http-client.d.ts +31 -0
  14. package/dist/core/embeddings/http-client.js +179 -0
  15. package/dist/core/embeddings/index.d.ts +1 -0
  16. package/dist/core/embeddings/index.js +1 -0
  17. package/dist/core/embeddings/types.d.ts +1 -1
  18. package/dist/core/graph/types.d.ts +4 -3
  19. package/dist/core/ingestion/ast-helpers.d.ts +80 -0
  20. package/dist/core/ingestion/ast-helpers.js +738 -0
  21. package/dist/core/ingestion/call-analysis.d.ts +73 -0
  22. package/dist/core/ingestion/call-analysis.js +490 -0
  23. package/dist/core/ingestion/call-processor.d.ts +55 -2
  24. package/dist/core/ingestion/call-processor.js +673 -108
  25. package/dist/core/ingestion/call-routing.d.ts +23 -2
  26. package/dist/core/ingestion/call-routing.js +21 -0
  27. package/dist/core/ingestion/entry-point-scoring.js +36 -26
  28. package/dist/core/ingestion/framework-detection.d.ts +10 -2
  29. package/dist/core/ingestion/framework-detection.js +49 -12
  30. package/dist/core/ingestion/heritage-processor.js +47 -49
  31. package/dist/core/ingestion/import-processor.d.ts +1 -1
  32. package/dist/core/ingestion/import-processor.js +103 -194
  33. package/dist/core/ingestion/import-resolution.d.ts +101 -0
  34. package/dist/core/ingestion/import-resolution.js +251 -0
  35. package/dist/core/ingestion/language-config.d.ts +3 -0
  36. package/dist/core/ingestion/language-config.js +13 -0
  37. package/dist/core/ingestion/markdown-processor.d.ts +17 -0
  38. package/dist/core/ingestion/markdown-processor.js +124 -0
  39. package/dist/core/ingestion/mro-processor.js +8 -3
  40. package/dist/core/ingestion/named-binding-extraction.d.ts +9 -43
  41. package/dist/core/ingestion/named-binding-extraction.js +89 -79
  42. package/dist/core/ingestion/parsing-processor.d.ts +3 -2
  43. package/dist/core/ingestion/parsing-processor.js +27 -60
  44. package/dist/core/ingestion/pipeline.d.ts +10 -0
  45. package/dist/core/ingestion/pipeline.js +425 -4
  46. package/dist/core/ingestion/resolution-context.d.ts +5 -0
  47. package/dist/core/ingestion/resolution-context.js +7 -4
  48. package/dist/core/ingestion/resolvers/index.d.ts +1 -1
  49. package/dist/core/ingestion/resolvers/index.js +1 -1
  50. package/dist/core/ingestion/resolvers/jvm.d.ts +2 -1
  51. package/dist/core/ingestion/resolvers/jvm.js +25 -9
  52. package/dist/core/ingestion/resolvers/php.d.ts +14 -0
  53. package/dist/core/ingestion/resolvers/php.js +43 -3
  54. package/dist/core/ingestion/resolvers/utils.d.ts +5 -0
  55. package/dist/core/ingestion/resolvers/utils.js +16 -0
  56. package/dist/core/ingestion/symbol-table.d.ts +29 -3
  57. package/dist/core/ingestion/symbol-table.js +42 -9
  58. package/dist/core/ingestion/tree-sitter-queries.d.ts +12 -12
  59. package/dist/core/ingestion/tree-sitter-queries.js +243 -2
  60. package/dist/core/ingestion/type-env.d.ts +28 -1
  61. package/dist/core/ingestion/type-env.js +451 -72
  62. package/dist/core/ingestion/type-extractors/c-cpp.d.ts +5 -0
  63. package/dist/core/ingestion/type-extractors/c-cpp.js +146 -2
  64. package/dist/core/ingestion/type-extractors/csharp.js +189 -16
  65. package/dist/core/ingestion/type-extractors/go.js +45 -0
  66. package/dist/core/ingestion/type-extractors/index.d.ts +1 -1
  67. package/dist/core/ingestion/type-extractors/index.js +1 -1
  68. package/dist/core/ingestion/type-extractors/jvm.js +244 -69
  69. package/dist/core/ingestion/type-extractors/php.js +31 -4
  70. package/dist/core/ingestion/type-extractors/python.js +89 -17
  71. package/dist/core/ingestion/type-extractors/ruby.js +17 -2
  72. package/dist/core/ingestion/type-extractors/rust.js +72 -4
  73. package/dist/core/ingestion/type-extractors/shared.d.ts +12 -2
  74. package/dist/core/ingestion/type-extractors/shared.js +115 -13
  75. package/dist/core/ingestion/type-extractors/swift.js +7 -6
  76. package/dist/core/ingestion/type-extractors/types.d.ts +54 -11
  77. package/dist/core/ingestion/type-extractors/typescript.js +171 -9
  78. package/dist/core/ingestion/utils.d.ts +2 -95
  79. package/dist/core/ingestion/utils.js +3 -892
  80. package/dist/core/ingestion/workers/parse-worker.d.ts +36 -11
  81. package/dist/core/ingestion/workers/parse-worker.js +116 -95
  82. package/dist/core/lbug/csv-generator.js +18 -1
  83. package/dist/core/lbug/lbug-adapter.d.ts +12 -0
  84. package/dist/core/lbug/lbug-adapter.js +71 -4
  85. package/dist/core/lbug/schema.d.ts +6 -4
  86. package/dist/core/lbug/schema.js +27 -3
  87. package/dist/mcp/core/embedder.js +11 -3
  88. package/dist/mcp/core/lbug-adapter.d.ts +22 -0
  89. package/dist/mcp/core/lbug-adapter.js +178 -23
  90. package/dist/mcp/local/local-backend.d.ts +22 -0
  91. package/dist/mcp/local/local-backend.js +136 -32
  92. package/dist/mcp/resources.js +13 -0
  93. package/dist/mcp/server.js +26 -4
  94. package/dist/mcp/tools.js +17 -7
  95. package/dist/server/api.d.ts +19 -1
  96. package/dist/server/api.js +66 -6
  97. package/dist/storage/git.d.ts +12 -0
  98. package/dist/storage/git.js +21 -0
  99. package/package.json +12 -4
@@ -0,0 +1,738 @@
1
+ import { SupportedLanguages } from '../../config/supported-languages.js';
2
+ import { generateId } from '../../lib/utils.js';
3
+ import { extractSimpleTypeName } from './type-extractors/shared.js';
4
+ /**
5
+ * Ordered list of definition capture keys for tree-sitter query matches.
6
+ * Used to extract the definition node from a capture map.
7
+ */
8
+ export const DEFINITION_CAPTURE_KEYS = [
9
+ 'definition.function',
10
+ 'definition.class',
11
+ 'definition.interface',
12
+ 'definition.method',
13
+ 'definition.struct',
14
+ 'definition.enum',
15
+ 'definition.namespace',
16
+ 'definition.module',
17
+ 'definition.trait',
18
+ 'definition.impl',
19
+ 'definition.type',
20
+ 'definition.const',
21
+ 'definition.static',
22
+ 'definition.typedef',
23
+ 'definition.macro',
24
+ 'definition.union',
25
+ 'definition.property',
26
+ 'definition.record',
27
+ 'definition.delegate',
28
+ 'definition.annotation',
29
+ 'definition.constructor',
30
+ 'definition.template',
31
+ ];
32
+ /** Extract the definition node from a tree-sitter query capture map. */
33
+ export const getDefinitionNodeFromCaptures = (captureMap) => {
34
+ for (const key of DEFINITION_CAPTURE_KEYS) {
35
+ if (captureMap[key])
36
+ return captureMap[key];
37
+ }
38
+ return null;
39
+ };
40
+ /**
41
+ * Node types that represent function/method definitions across languages.
42
+ * Used to find the enclosing function for a call site.
43
+ */
44
+ export const FUNCTION_NODE_TYPES = new Set([
45
+ // TypeScript/JavaScript
46
+ 'function_declaration',
47
+ 'arrow_function',
48
+ 'function_expression',
49
+ 'method_definition',
50
+ 'generator_function_declaration',
51
+ // Python
52
+ 'function_definition',
53
+ // Common async variants
54
+ 'async_function_declaration',
55
+ 'async_arrow_function',
56
+ // Java
57
+ 'method_declaration',
58
+ 'constructor_declaration',
59
+ // C/C++
60
+ // 'function_definition' already included above
61
+ // Go
62
+ // 'method_declaration' already included from Java
63
+ // C#
64
+ 'local_function_statement',
65
+ // Rust
66
+ 'function_item',
67
+ 'impl_item', // Methods inside impl blocks
68
+ // PHP
69
+ 'anonymous_function',
70
+ // Kotlin
71
+ 'lambda_literal',
72
+ // Swift
73
+ 'init_declaration',
74
+ 'deinit_declaration',
75
+ // Ruby
76
+ 'method', // def foo
77
+ 'singleton_method', // def self.foo
78
+ ]);
79
+ /**
80
+ * Node types for standard function declarations that need C/C++ declarator handling.
81
+ * Used by extractFunctionName to determine how to extract the function name.
82
+ */
83
+ export const FUNCTION_DECLARATION_TYPES = new Set([
84
+ 'function_declaration',
85
+ 'function_definition',
86
+ 'async_function_declaration',
87
+ 'generator_function_declaration',
88
+ 'function_item',
89
+ ]);
90
+ /** AST node types that represent a class-like container (for HAS_METHOD edge extraction) */
91
+ export const CLASS_CONTAINER_TYPES = new Set([
92
+ 'class_declaration', 'abstract_class_declaration',
93
+ 'interface_declaration', 'struct_declaration', 'record_declaration',
94
+ 'class_specifier', 'struct_specifier',
95
+ 'impl_item', 'trait_item', 'struct_item', 'enum_item',
96
+ 'class_definition',
97
+ 'trait_declaration',
98
+ 'protocol_declaration',
99
+ // Ruby
100
+ 'class',
101
+ 'module',
102
+ // Kotlin
103
+ 'object_declaration',
104
+ 'companion_object',
105
+ ]);
106
+ export const CONTAINER_TYPE_TO_LABEL = {
107
+ class_declaration: 'Class',
108
+ abstract_class_declaration: 'Class',
109
+ interface_declaration: 'Interface',
110
+ struct_declaration: 'Struct',
111
+ struct_specifier: 'Struct',
112
+ class_specifier: 'Class',
113
+ class_definition: 'Class',
114
+ impl_item: 'Impl',
115
+ trait_item: 'Trait',
116
+ struct_item: 'Struct',
117
+ enum_item: 'Enum',
118
+ trait_declaration: 'Trait',
119
+ record_declaration: 'Record',
120
+ protocol_declaration: 'Interface',
121
+ class: 'Class',
122
+ module: 'Module',
123
+ object_declaration: 'Class',
124
+ companion_object: 'Class',
125
+ };
126
+ /** Check if a Kotlin function_declaration capture is inside a class_body (i.e., a method).
127
+ * Kotlin grammar uses function_declaration for both top-level functions and class methods.
128
+ * Returns true when the captured definition node has a class_body ancestor. */
129
+ export function isKotlinClassMethod(captureNode) {
130
+ let ancestor = captureNode?.parent;
131
+ while (ancestor) {
132
+ if (ancestor.type === 'class_body')
133
+ return true;
134
+ ancestor = ancestor.parent;
135
+ }
136
+ return false;
137
+ }
138
+ /**
139
+ * C/C++: check if a Function capture is inside a class/struct body.
140
+ * If true, the function is already captured by @definition.method and should be skipped
141
+ * to prevent double-indexing in globalIndex.
142
+ */
143
+ export function isCppDuplicateClassFunction(functionNode, nodeLabel, language) {
144
+ if (nodeLabel !== 'Function')
145
+ return false;
146
+ if (language !== SupportedLanguages.CPlusPlus && language !== SupportedLanguages.C)
147
+ return false;
148
+ let ancestor = functionNode?.parent;
149
+ while (ancestor) {
150
+ if (ancestor.type === 'class_specifier' || ancestor.type === 'struct_specifier')
151
+ return true;
152
+ ancestor = ancestor.parent;
153
+ }
154
+ return false;
155
+ }
156
+ /**
157
+ * Determine the graph node label from a tree-sitter capture map.
158
+ * Handles language-specific reclassification (C/C++ duplicate skipping, Kotlin Method promotion).
159
+ * Returns null if the capture should be skipped (import, call, C/C++ duplicate, missing name).
160
+ */
161
+ export function getLabelFromCaptures(captureMap, language) {
162
+ if (captureMap['import'] || captureMap['call'])
163
+ return null;
164
+ if (!captureMap['name'] && !captureMap['definition.constructor'])
165
+ return null;
166
+ if (captureMap['definition.function']) {
167
+ if (isCppDuplicateClassFunction(captureMap['definition.function'], 'Function', language))
168
+ return null;
169
+ if (language === SupportedLanguages.Kotlin && isKotlinClassMethod(captureMap['definition.function']))
170
+ return 'Method';
171
+ return 'Function';
172
+ }
173
+ if (captureMap['definition.class'])
174
+ return 'Class';
175
+ if (captureMap['definition.interface'])
176
+ return 'Interface';
177
+ if (captureMap['definition.method'])
178
+ return 'Method';
179
+ if (captureMap['definition.struct'])
180
+ return 'Struct';
181
+ if (captureMap['definition.enum'])
182
+ return 'Enum';
183
+ if (captureMap['definition.namespace'])
184
+ return 'Namespace';
185
+ if (captureMap['definition.module'])
186
+ return 'Module';
187
+ if (captureMap['definition.trait'])
188
+ return 'Trait';
189
+ if (captureMap['definition.impl'])
190
+ return 'Impl';
191
+ if (captureMap['definition.type'])
192
+ return 'TypeAlias';
193
+ if (captureMap['definition.const'])
194
+ return 'Const';
195
+ if (captureMap['definition.static'])
196
+ return 'Static';
197
+ if (captureMap['definition.typedef'])
198
+ return 'Typedef';
199
+ if (captureMap['definition.macro'])
200
+ return 'Macro';
201
+ if (captureMap['definition.union'])
202
+ return 'Union';
203
+ if (captureMap['definition.property'])
204
+ return 'Property';
205
+ if (captureMap['definition.record'])
206
+ return 'Record';
207
+ if (captureMap['definition.delegate'])
208
+ return 'Delegate';
209
+ if (captureMap['definition.annotation'])
210
+ return 'Annotation';
211
+ if (captureMap['definition.constructor'])
212
+ return 'Constructor';
213
+ if (captureMap['definition.template'])
214
+ return 'Template';
215
+ return 'CodeElement';
216
+ }
217
+ /** Walk up AST to find enclosing class/struct/interface/impl, return its generateId or null.
218
+ * For Go method_declaration nodes, extracts receiver type (e.g. `func (u *User) Save()` → User struct). */
219
+ export const findEnclosingClassId = (node, filePath) => {
220
+ let current = node.parent;
221
+ while (current) {
222
+ // Go: method_declaration has a receiver parameter with the struct type
223
+ if (current.type === 'method_declaration') {
224
+ const receiver = current.childForFieldName?.('receiver');
225
+ if (receiver) {
226
+ // receiver is a parameter_list: (u *User) or (u User)
227
+ const paramDecl = receiver.namedChildren?.find?.((c) => c.type === 'parameter_declaration');
228
+ if (paramDecl) {
229
+ const typeNode = paramDecl.childForFieldName?.('type');
230
+ if (typeNode) {
231
+ // Unwrap pointer_type (*User → User)
232
+ const inner = typeNode.type === 'pointer_type' ? typeNode.firstNamedChild : typeNode;
233
+ if (inner && (inner.type === 'type_identifier' || inner.type === 'identifier')) {
234
+ return generateId('Struct', `${filePath}:${inner.text}`);
235
+ }
236
+ }
237
+ }
238
+ }
239
+ }
240
+ // Go: type_declaration wrapping a struct_type (type User struct { ... })
241
+ // field_declaration → field_declaration_list → struct_type → type_spec → type_declaration
242
+ if (current.type === 'type_declaration') {
243
+ const typeSpec = current.children?.find((c) => c.type === 'type_spec');
244
+ if (typeSpec) {
245
+ const typeBody = typeSpec.childForFieldName?.('type');
246
+ if (typeBody?.type === 'struct_type' || typeBody?.type === 'interface_type') {
247
+ const nameNode = typeSpec.childForFieldName?.('name');
248
+ if (nameNode) {
249
+ const label = typeBody.type === 'struct_type' ? 'Struct' : 'Interface';
250
+ return generateId(label, `${filePath}:${nameNode.text}`);
251
+ }
252
+ }
253
+ }
254
+ }
255
+ if (CLASS_CONTAINER_TYPES.has(current.type)) {
256
+ // Rust impl_item: for `impl Trait for Struct {}`, pick the type after `for`
257
+ if (current.type === 'impl_item') {
258
+ const children = current.children ?? [];
259
+ const forIdx = children.findIndex((c) => c.text === 'for');
260
+ if (forIdx !== -1) {
261
+ const nameNode = children.slice(forIdx + 1).find((c) => c.type === 'type_identifier' || c.type === 'identifier');
262
+ if (nameNode) {
263
+ return generateId('Impl', `${filePath}:${nameNode.text}`);
264
+ }
265
+ }
266
+ // Fall through: plain `impl Struct {}` — use first type_identifier below
267
+ }
268
+ const nameNode = current.childForFieldName?.('name')
269
+ ?? current.children?.find((c) => c.type === 'type_identifier' || c.type === 'identifier' || c.type === 'name' || c.type === 'constant');
270
+ if (nameNode) {
271
+ const label = CONTAINER_TYPE_TO_LABEL[current.type] || 'Class';
272
+ return generateId(label, `${filePath}:${nameNode.text}`);
273
+ }
274
+ }
275
+ current = current.parent;
276
+ }
277
+ return null;
278
+ };
279
+ /**
280
+ * Find a child of `childType` within a sibling node of `siblingType`.
281
+ * Used for Kotlin AST traversal where visibility_modifier lives inside a modifiers sibling.
282
+ */
283
+ export const findSiblingChild = (parent, siblingType, childType) => {
284
+ for (let i = 0; i < parent.childCount; i++) {
285
+ const sibling = parent.child(i);
286
+ if (sibling?.type === siblingType) {
287
+ for (let j = 0; j < sibling.childCount; j++) {
288
+ const child = sibling.child(j);
289
+ if (child?.type === childType)
290
+ return child;
291
+ }
292
+ }
293
+ }
294
+ return null;
295
+ };
296
+ /**
297
+ * Extract function name and label from a function_definition or similar AST node.
298
+ * Handles C/C++ qualified_identifier (ClassName::MethodName) and other language patterns.
299
+ */
300
+ export const extractFunctionName = (node) => {
301
+ let funcName = null;
302
+ let label = 'Function';
303
+ // Swift init/deinit
304
+ if (node.type === 'init_declaration' || node.type === 'deinit_declaration') {
305
+ return {
306
+ funcName: node.type === 'init_declaration' ? 'init' : 'deinit',
307
+ label: 'Constructor',
308
+ };
309
+ }
310
+ if (FUNCTION_DECLARATION_TYPES.has(node.type)) {
311
+ // C/C++: function_definition -> [pointer_declarator ->] function_declarator -> qualified_identifier/identifier
312
+ // Unwrap pointer_declarator / reference_declarator wrappers to reach function_declarator
313
+ let declarator = node.childForFieldName?.('declarator');
314
+ if (!declarator) {
315
+ for (let i = 0; i < node.childCount; i++) {
316
+ const c = node.child(i);
317
+ if (c?.type === 'function_declarator') {
318
+ declarator = c;
319
+ break;
320
+ }
321
+ }
322
+ }
323
+ while (declarator && (declarator.type === 'pointer_declarator' || declarator.type === 'reference_declarator')) {
324
+ let nextDeclarator = declarator.childForFieldName?.('declarator');
325
+ if (!nextDeclarator) {
326
+ for (let i = 0; i < declarator.childCount; i++) {
327
+ const c = declarator.child(i);
328
+ if (c?.type === 'function_declarator' || c?.type === 'pointer_declarator' || c?.type === 'reference_declarator') {
329
+ nextDeclarator = c;
330
+ break;
331
+ }
332
+ }
333
+ }
334
+ declarator = nextDeclarator;
335
+ }
336
+ if (declarator) {
337
+ let innerDeclarator = declarator.childForFieldName?.('declarator');
338
+ if (!innerDeclarator) {
339
+ for (let i = 0; i < declarator.childCount; i++) {
340
+ const c = declarator.child(i);
341
+ if (c?.type === 'qualified_identifier' || c?.type === 'identifier'
342
+ || c?.type === 'field_identifier' || c?.type === 'parenthesized_declarator') {
343
+ innerDeclarator = c;
344
+ break;
345
+ }
346
+ }
347
+ }
348
+ if (innerDeclarator?.type === 'qualified_identifier') {
349
+ let nameNode = innerDeclarator.childForFieldName?.('name');
350
+ if (!nameNode) {
351
+ for (let i = 0; i < innerDeclarator.childCount; i++) {
352
+ const c = innerDeclarator.child(i);
353
+ if (c?.type === 'identifier') {
354
+ nameNode = c;
355
+ break;
356
+ }
357
+ }
358
+ }
359
+ if (nameNode?.text) {
360
+ funcName = nameNode.text;
361
+ label = 'Method';
362
+ }
363
+ }
364
+ else if (innerDeclarator?.type === 'identifier' || innerDeclarator?.type === 'field_identifier') {
365
+ // field_identifier is used for method names inside C++ class bodies
366
+ funcName = innerDeclarator.text;
367
+ if (innerDeclarator.type === 'field_identifier')
368
+ label = 'Method';
369
+ }
370
+ else if (innerDeclarator?.type === 'parenthesized_declarator') {
371
+ let nestedId = null;
372
+ for (let i = 0; i < innerDeclarator.childCount; i++) {
373
+ const c = innerDeclarator.child(i);
374
+ if (c?.type === 'qualified_identifier' || c?.type === 'identifier') {
375
+ nestedId = c;
376
+ break;
377
+ }
378
+ }
379
+ if (nestedId?.type === 'qualified_identifier') {
380
+ let nameNode = nestedId.childForFieldName?.('name');
381
+ if (!nameNode) {
382
+ for (let i = 0; i < nestedId.childCount; i++) {
383
+ const c = nestedId.child(i);
384
+ if (c?.type === 'identifier') {
385
+ nameNode = c;
386
+ break;
387
+ }
388
+ }
389
+ }
390
+ if (nameNode?.text) {
391
+ funcName = nameNode.text;
392
+ label = 'Method';
393
+ }
394
+ }
395
+ else if (nestedId?.type === 'identifier') {
396
+ funcName = nestedId.text;
397
+ }
398
+ }
399
+ }
400
+ // Fallback for other languages (Kotlin uses simple_identifier, Swift uses simple_identifier)
401
+ if (!funcName) {
402
+ let nameNode = node.childForFieldName?.('name');
403
+ if (!nameNode) {
404
+ for (let i = 0; i < node.childCount; i++) {
405
+ const c = node.child(i);
406
+ if (c?.type === 'identifier' || c?.type === 'property_identifier' || c?.type === 'simple_identifier') {
407
+ nameNode = c;
408
+ break;
409
+ }
410
+ }
411
+ }
412
+ funcName = nameNode?.text;
413
+ // Kotlin: function_declaration inside a class_body is a method, not a top-level function.
414
+ // Must match the label assigned in parse-worker.ts for consistent generateId() output.
415
+ if (funcName && node.type === 'function_declaration' && isKotlinClassMethod(node)) {
416
+ label = 'Method';
417
+ }
418
+ }
419
+ }
420
+ else if (node.type === 'impl_item') {
421
+ let funcItem = null;
422
+ for (let i = 0; i < node.childCount; i++) {
423
+ const c = node.child(i);
424
+ if (c?.type === 'function_item') {
425
+ funcItem = c;
426
+ break;
427
+ }
428
+ }
429
+ if (funcItem) {
430
+ let nameNode = funcItem.childForFieldName?.('name');
431
+ if (!nameNode) {
432
+ for (let i = 0; i < funcItem.childCount; i++) {
433
+ const c = funcItem.child(i);
434
+ if (c?.type === 'identifier') {
435
+ nameNode = c;
436
+ break;
437
+ }
438
+ }
439
+ }
440
+ funcName = nameNode?.text;
441
+ label = 'Method';
442
+ }
443
+ }
444
+ else if (node.type === 'method_definition') {
445
+ let nameNode = node.childForFieldName?.('name');
446
+ if (!nameNode) {
447
+ for (let i = 0; i < node.childCount; i++) {
448
+ const c = node.child(i);
449
+ if (c?.type === 'property_identifier') {
450
+ nameNode = c;
451
+ break;
452
+ }
453
+ }
454
+ }
455
+ funcName = nameNode?.text;
456
+ label = 'Method';
457
+ }
458
+ else if (node.type === 'method_declaration' || node.type === 'constructor_declaration') {
459
+ let nameNode = node.childForFieldName?.('name');
460
+ if (!nameNode) {
461
+ for (let i = 0; i < node.childCount; i++) {
462
+ const c = node.child(i);
463
+ if (c?.type === 'identifier') {
464
+ nameNode = c;
465
+ break;
466
+ }
467
+ }
468
+ }
469
+ funcName = nameNode?.text;
470
+ label = 'Method';
471
+ }
472
+ else if (node.type === 'arrow_function' || node.type === 'function_expression') {
473
+ const parent = node.parent;
474
+ if (parent?.type === 'variable_declarator') {
475
+ let nameNode = parent.childForFieldName?.('name');
476
+ if (!nameNode) {
477
+ for (let i = 0; i < parent.childCount; i++) {
478
+ const c = parent.child(i);
479
+ if (c?.type === 'identifier') {
480
+ nameNode = c;
481
+ break;
482
+ }
483
+ }
484
+ }
485
+ funcName = nameNode?.text;
486
+ }
487
+ }
488
+ else if (node.type === 'method' || node.type === 'singleton_method') {
489
+ let nameNode = node.childForFieldName?.('name');
490
+ if (!nameNode) {
491
+ for (let i = 0; i < node.childCount; i++) {
492
+ const c = node.child(i);
493
+ if (c?.type === 'identifier') {
494
+ nameNode = c;
495
+ break;
496
+ }
497
+ }
498
+ }
499
+ funcName = nameNode?.text;
500
+ label = 'Method';
501
+ }
502
+ return { funcName, label };
503
+ };
504
+ /** Argument list node types shared between extractMethodSignature and countCallArguments. */
505
+ export const CALL_ARGUMENT_LIST_TYPES = new Set([
506
+ 'arguments',
507
+ 'argument_list',
508
+ 'value_arguments',
509
+ ]);
510
+ /**
511
+ * Extract parameter count and return type text from an AST method/function node.
512
+ * Works across languages by looking for common AST patterns.
513
+ */
514
+ export const extractMethodSignature = (node) => {
515
+ let parameterCount = 0;
516
+ let requiredCount = 0;
517
+ let returnType;
518
+ let isVariadic = false;
519
+ const paramTypes = [];
520
+ if (!node)
521
+ return { parameterCount, requiredParameterCount: undefined, parameterTypes: undefined, returnType };
522
+ const paramListTypes = new Set([
523
+ 'formal_parameters', 'parameters', 'parameter_list',
524
+ 'function_parameters', 'method_parameters', 'function_value_parameters',
525
+ ]);
526
+ // Node types that indicate variadic/rest parameters
527
+ const VARIADIC_PARAM_TYPES = new Set([
528
+ 'variadic_parameter_declaration', // Go: ...string
529
+ 'variadic_parameter', // Rust: extern "C" fn(...)
530
+ 'spread_parameter', // Java: Object... args
531
+ 'list_splat_pattern', // Python: *args
532
+ 'dictionary_splat_pattern', // Python: **kwargs
533
+ ]);
534
+ /** AST node types that represent parameters with default values. */
535
+ const OPTIONAL_PARAM_TYPES = new Set([
536
+ 'optional_parameter', // TypeScript, Ruby: (x?: number), (x: number = 5), def f(x = 5)
537
+ 'default_parameter', // Python: def f(x=5)
538
+ 'typed_default_parameter', // Python: def f(x: int = 5)
539
+ 'optional_parameter_declaration', // C++: void f(int x = 5)
540
+ ]);
541
+ /** Check if a parameter node has a default value (handles Kotlin, C#, Swift, PHP
542
+ * where defaults are expressed as child nodes rather than distinct node types). */
543
+ const hasDefaultValue = (paramNode) => {
544
+ if (OPTIONAL_PARAM_TYPES.has(paramNode.type))
545
+ return true;
546
+ // C#, Swift, PHP: check for '=' token or equals_value_clause child
547
+ for (let i = 0; i < paramNode.childCount; i++) {
548
+ const c = paramNode.child(i);
549
+ if (!c)
550
+ continue;
551
+ if (c.type === '=' || c.type === 'equals_value_clause')
552
+ return true;
553
+ }
554
+ // Kotlin: default values are siblings of the parameter node, not children.
555
+ // The AST is: parameter, =, <literal> — all at function_value_parameters level.
556
+ // Check if the immediately following sibling is '=' (default value separator).
557
+ const sib = paramNode.nextSibling;
558
+ if (sib && sib.type === '=')
559
+ return true;
560
+ return false;
561
+ };
562
+ const findParameterList = (current) => {
563
+ for (const child of current.children) {
564
+ if (paramListTypes.has(child.type))
565
+ return child;
566
+ }
567
+ for (const child of current.children) {
568
+ const nested = findParameterList(child);
569
+ if (nested)
570
+ return nested;
571
+ }
572
+ return null;
573
+ };
574
+ const parameterList = (paramListTypes.has(node.type) ? node // node itself IS the parameter list (e.g. C# primary constructors)
575
+ : node.childForFieldName?.('parameters')
576
+ ?? findParameterList(node));
577
+ if (parameterList && paramListTypes.has(parameterList.type)) {
578
+ for (const param of parameterList.namedChildren) {
579
+ if (param.type === 'comment')
580
+ continue;
581
+ if (param.text === 'self' || param.text === '&self' || param.text === '&mut self' ||
582
+ param.type === 'self_parameter') {
583
+ continue;
584
+ }
585
+ // Kotlin: default values are siblings of the parameter node inside
586
+ // function_value_parameters, so they appear as named children (e.g.
587
+ // string_literal, integer_literal, boolean_literal, call_expression).
588
+ // Skip any named child that isn't a parameter-like or modifier node.
589
+ if (param.type.endsWith('_literal') || param.type === 'call_expression'
590
+ || param.type === 'navigation_expression' || param.type === 'prefix_expression'
591
+ || param.type === 'parenthesized_expression') {
592
+ continue;
593
+ }
594
+ // Check for variadic parameter types
595
+ if (VARIADIC_PARAM_TYPES.has(param.type)) {
596
+ isVariadic = true;
597
+ continue;
598
+ }
599
+ // TypeScript/JavaScript: rest parameter — required_parameter containing rest_pattern
600
+ if (param.type === 'required_parameter' || param.type === 'optional_parameter') {
601
+ for (const child of param.children) {
602
+ if (child.type === 'rest_pattern') {
603
+ isVariadic = true;
604
+ break;
605
+ }
606
+ }
607
+ if (isVariadic)
608
+ continue;
609
+ }
610
+ // Kotlin: vararg modifier on a regular parameter
611
+ if (param.type === 'parameter' || param.type === 'formal_parameter') {
612
+ const prev = param.previousSibling;
613
+ if (prev?.type === 'parameter_modifiers' && prev.text.includes('vararg')) {
614
+ isVariadic = true;
615
+ }
616
+ }
617
+ // Extract parameter type name for overload disambiguation.
618
+ // Works for Java (formal_parameter), Kotlin (parameter), C# (parameter),
619
+ // C++ (parameter_declaration). Uses childForFieldName('type') which is the
620
+ // standard tree-sitter field for typed parameters across these languages.
621
+ // Kotlin uses positional children instead of 'type' field — fall back to
622
+ // searching for user_type/nullable_type/predefined_type children.
623
+ const paramTypeNode = param.childForFieldName('type');
624
+ if (paramTypeNode) {
625
+ const typeName = extractSimpleTypeName(paramTypeNode);
626
+ paramTypes.push(typeName ?? 'unknown');
627
+ }
628
+ else {
629
+ // Kotlin: parameter → [simple_identifier, user_type|nullable_type]
630
+ let found = false;
631
+ for (const child of param.namedChildren) {
632
+ if (child.type === 'user_type' || child.type === 'nullable_type'
633
+ || child.type === 'type_identifier' || child.type === 'predefined_type') {
634
+ const typeName = extractSimpleTypeName(child);
635
+ paramTypes.push(typeName ?? 'unknown');
636
+ found = true;
637
+ break;
638
+ }
639
+ }
640
+ if (!found)
641
+ paramTypes.push('unknown');
642
+ }
643
+ if (!hasDefaultValue(param))
644
+ requiredCount++;
645
+ parameterCount++;
646
+ }
647
+ // C/C++: bare `...` token in parameter list (not a named child — check all children)
648
+ if (!isVariadic) {
649
+ for (const child of parameterList.children) {
650
+ if (!child.isNamed && child.text === '...') {
651
+ isVariadic = true;
652
+ break;
653
+ }
654
+ }
655
+ }
656
+ }
657
+ // Return type extraction — language-specific field names
658
+ // Go: 'result' field is either a type_identifier or parameter_list (multi-return)
659
+ const goResult = node.childForFieldName?.('result');
660
+ if (goResult) {
661
+ if (goResult.type === 'parameter_list') {
662
+ // Multi-return: extract first parameter's type only (e.g. (*User, error) → *User)
663
+ const firstParam = goResult.firstNamedChild;
664
+ if (firstParam?.type === 'parameter_declaration') {
665
+ const typeNode = firstParam.childForFieldName('type');
666
+ if (typeNode)
667
+ returnType = typeNode.text;
668
+ }
669
+ else if (firstParam) {
670
+ // Unnamed return types: (string, error) — first child is a bare type node
671
+ returnType = firstParam.text;
672
+ }
673
+ }
674
+ else {
675
+ returnType = goResult.text;
676
+ }
677
+ }
678
+ // Rust: 'return_type' field — the value IS the type node (e.g. primitive_type, type_identifier).
679
+ // Skip if the node is a type_annotation (TS/Python), which is handled by the generic loop below.
680
+ if (!returnType) {
681
+ const rustReturn = node.childForFieldName?.('return_type');
682
+ if (rustReturn && rustReturn.type !== 'type_annotation') {
683
+ returnType = rustReturn.text;
684
+ }
685
+ }
686
+ // C/C++: 'type' field on function_definition
687
+ if (!returnType) {
688
+ const cppType = node.childForFieldName?.('type');
689
+ if (cppType && cppType.text !== 'void') {
690
+ returnType = cppType.text;
691
+ }
692
+ }
693
+ // C#: 'returns' field on method_declaration
694
+ if (!returnType) {
695
+ const csReturn = node.childForFieldName?.('returns');
696
+ if (csReturn && csReturn.text !== 'void') {
697
+ returnType = csReturn.text;
698
+ }
699
+ }
700
+ // TS/Rust/Python/C#/Kotlin: type_annotation or return_type child
701
+ if (!returnType) {
702
+ for (const child of node.children) {
703
+ if (child.type === 'type_annotation' || child.type === 'return_type') {
704
+ const typeNode = child.children.find((c) => c.isNamed);
705
+ if (typeNode)
706
+ returnType = typeNode.text;
707
+ }
708
+ }
709
+ }
710
+ // Kotlin: fun getUser(): User — return type is a bare user_type child of
711
+ // function_declaration. The Kotlin grammar does NOT wrap it in type_annotation
712
+ // or return_type; it appears as a direct child after function_value_parameters.
713
+ // Note: Kotlin uses function_value_parameters (not a field), so we find it by type.
714
+ if (!returnType) {
715
+ let paramsEnd = -1;
716
+ for (let i = 0; i < node.childCount; i++) {
717
+ const child = node.child(i);
718
+ if (!child)
719
+ continue;
720
+ if (child.type === 'function_value_parameters' || child.type === 'value_parameters') {
721
+ paramsEnd = child.endIndex;
722
+ }
723
+ if (paramsEnd >= 0 && child.type === 'user_type' && child.startIndex > paramsEnd) {
724
+ returnType = child.text;
725
+ break;
726
+ }
727
+ }
728
+ }
729
+ if (isVariadic)
730
+ parameterCount = undefined;
731
+ // Only include parameterTypes when at least one type was successfully extracted.
732
+ // Use undefined (not []) to avoid empty array allocations for untyped parameters.
733
+ const hasTypes = paramTypes.length > 0 && paramTypes.some(t => t !== 'unknown');
734
+ // Only set requiredParameterCount when it differs from total — saves memory on the common case.
735
+ const requiredParameterCount = (!isVariadic && requiredCount < (parameterCount ?? 0))
736
+ ? requiredCount : undefined;
737
+ return { parameterCount, requiredParameterCount, parameterTypes: hasTypes ? paramTypes : undefined, returnType };
738
+ };