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

Function normalizeValue

src/extraction/function-ref.ts:558–629  ·  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

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

Callers 1

captureFnRefCandidatesFunction · 0.85

Calls 6

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

Tested by

no test coverage detected