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

Function normalizeValue

src/extraction/function-ref.ts:551–623  ·  view source on GitHub ↗

* Normalize one value expression to zero or more function names. Recursion is * bounded (wrapper layers only); anything that isn't a recognized * function-value shape yields [].

(
  node: SyntaxNode,
  spec: FnRefSpec,
  source: string,
  depth: number
)

Source from the content-addressed store, hash-verified

549 * function-value shape yields [].
550 */
551function normalizeValue(
552 node: SyntaxNode,
553 spec: FnRefSpec,
554 source: string,
555 depth: number
556): NormalizedRef[] {
557 if (depth > 4) return [];
558 const type = node.type;
559
560 // Bare identifier
561 if (spec.idTypes.has(type)) {
562 return [{ name: getNodeText(node, source), node }];
563 }
564
565 // Transparent layers (argument, value_argument, literal_element,
566 // expression_list, block_argument). expression_list fans out (Go `a, b = f, g`).
567 const layerField = spec.layers?.get(type);
568 if (spec.layers?.has(type)) {
569 // Labeled-argument param-forward skip (Swift/Kotlin): `value: value` /
570 // `delay: delay` — when the label EQUALS the value identifier, the value
571 // is a forwarded local/parameter, not a function reference (Alamofire
572 // A/B finding; same rationale as the `this.x = x` assignment skip).
573 if (type === 'value_argument') {
574 const label = getChildByField(node, 'name');
575 const value = getChildByField(node, 'value') ?? node.namedChild(node.namedChildCount - 1);
576 if (
577 label &&
578 value &&
579 getNodeText(label, source).trim() === getNodeText(value, source).trim()
580 ) {
581 return [];
582 }
583 }
584 if (layerField) {
585 const inner = getChildByField(node, layerField);
586 return inner ? normalizeValue(inner, spec, source, depth + 1) : [];
587 }
588 const results: NormalizedRef[] = [];
589 for (let i = 0; i < node.namedChildCount; i++) {
590 const child = node.namedChild(i);
591 if (child) results.push(...normalizeValue(child, spec, source, depth + 1));
592 }
593 return results;
594 }
595
596 // Unary wrappers: &fn / @Fn / `fn _`
597 const unwrapField = spec.unwrap?.get(type);
598 if (spec.unwrap?.has(type)) {
599 // C-family `pointer_expression` covers BOTH `&x` (address-of — a function
600 // value) and `*x` (dereference — a data read, never a function value).
601 // Only `&` qualifies; without this, fmt's `*begin` reads resolved to its
602 // free `begin()` functions.
603 if (type === 'pointer_expression' && node.child(0)?.type !== '&') return [];
604 const inner = unwrapField ? getChildByField(node, unwrapField) : node.namedChild(0);
605 if (!inner) return [];
606 // C++ `&Widget::on_click` — keep the QUALIFIED name. Resolution scopes the
607 // method to that class (more precise than a bare-name match, and exempt
608 // from the cpp bare-ids-are-free-functions rule since `&Cls::m` is an

Callers 1

captureFnRefCandidatesFunction · 0.85

Calls 6

getNodeTextFunction · 0.90
getChildByFieldFunction · 0.90
normalizeSpecialFunction · 0.85
hasMethod · 0.80
pushMethod · 0.80
getMethod · 0.65

Tested by

no test coverage detected