MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / captureFnRefCandidates

Function captureFnRefCandidates

src/extraction/function-ref.ts:434–537  ·  view source on GitHub ↗
(
  container: SyntaxNode,
  rule: CaptureRule,
  spec: FnRefSpec,
  source: string
)

Source from the content-addressed store, hash-verified

432 * (name, position) pairs of every function-value-shaped expression found.
433 */
434export function captureFnRefCandidates(
435 container: SyntaxNode,
436 rule: CaptureRule,
437 spec: FnRefSpec,
438 source: string
439): FnRefCandidate[] {
440 const valueNodes: SyntaxNode[] = [];
441
442 switch (rule.mode) {
443 case 'args':
444 case 'list': {
445 for (let i = 0; i < container.namedChildCount; i++) {
446 const child = container.namedChild(i);
447 if (child) valueNodes.push(child);
448 }
449 break;
450 }
451 case 'rhs': {
452 const rhs = rule.field
453 ? getChildByField(container, rule.field)
454 : container.namedChild(container.namedChildCount - 1);
455 if (rhs) {
456 // Param-storage skip: `this.status = status` / `o->cb = cb` — when
457 // the assigned member's name EQUALS the RHS identifier, the RHS is a
458 // local/parameter being stored, and the function it holds (if any)
459 // is unknowable statically. A same-named function elsewhere would
460 // resolve to the WRONG target (excalidraw A/B finding), so skip.
461 const lhs =
462 getChildByField(container, 'left') ??
463 getChildByField(container, 'lhs') ??
464 getChildByField(container, 'target') ??
465 (container.namedChildCount >= 2 ? container.namedChild(0) : null);
466 const lhsText = lhs ? getNodeText(lhs, source) : '';
467 const lhsLastName = lhsText.match(/([A-Za-z_$][A-Za-z0-9_$]*)\s*$/)?.[1];
468 const rhsText = getNodeText(rhs, source).trim();
469 if (lhsLastName && lhsLastName === rhsText) break;
470 valueNodes.push(rhs);
471 }
472 break;
473 }
474 case 'value': {
475 let value = rule.field ? getChildByField(container, rule.field) : null;
476 // Keyed containers without a value field (Go keyed_element): the value
477 // is the LAST named child (the first is the key).
478 if (!value && container.namedChildCount > 0) {
479 value = container.namedChild(container.namedChildCount - 1);
480 }
481 if (value) valueNodes.push(value);
482 break;
483 }
484 case 'varinit': {
485 // Destructuring (`const { center } = ellipse`) extracts DATA from the
486 // RHS — never a function alias. Without this skip, a parameter that
487 // shadows a same-named imported function produced a wrong edge.
488 const nameNode =
489 getChildByField(container, 'name') ?? getChildByField(container, 'pattern');
490 if (nameNode && (nameNode.type === 'object_pattern' || nameNode.type === 'array_pattern' ||
491 nameNode.type === 'tuple_pattern' || nameNode.type === 'struct_pattern')) {

Callers 2

maybeCaptureFnRefsMethod · 0.90

Calls 5

getChildByFieldFunction · 0.90
getNodeTextFunction · 0.90
normalizeValueFunction · 0.85
pushMethod · 0.80
hasMethod · 0.80

Tested by

no test coverage detected