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

Function resolveStaticMember

src/resolution/import-resolver.ts:4723–4757  ·  view source on GitHub ↗
(
  container: Node,
  ref: UnresolvedRef,
  localName: string,
  context: ResolutionContext
)

Source from the content-addressed store, hash-verified

4721}
4722
4723function resolveStaticMember(
4724 container: Node,
4725 ref: UnresolvedRef,
4726 localName: string,
4727 context: ResolutionContext
4728): Node | undefined {
4729 // A JS constructor function's static methods (`Model.insertMany = function
4730 // () {…}`, #2966) are reached the same way; what it nests is not.
4731 const constructorFn = container.kind === 'function' && JS_FAMILY_FILE.test(container.filePath);
4732 if (!STATIC_MEMBER_CONTAINERS.has(container.kind) && !constructorFn) return undefined;
4733 // First segment after the receiver: `Foo.bar.baz` → `bar`.
4734 const [member, ...rest] = ref.referenceName.slice(localName.length + 1).split('.');
4735 if (!member) return undefined;
4736
4737 const candidates = context
4738 .getNodesByQualifiedName(`${container.qualifiedName}::${member}`)
4739 .filter((n) => n.filePath === container.filePath && (!constructorFn || (n.kind === 'method' && n.isStatic === true)));
4740 if (candidates.length === 0) return undefined;
4741
4742 // A Python reference keeps its whole chain, and it reads the chain's last
4743 // link: `Foo.bar.fset()` is no call of `bar`, nor `Foo.Inner.objects.create()`
4744 // a new `Inner`, but `Foo.Inner.run()` is `Inner`'s `run`.
4745 if (ref.language === 'python' && rest.length > 0) {
4746 const next = candidates.find((n) => STATIC_MEMBER_CONTAINERS.has(n.kind));
4747 return next && resolveStaticMember(next, { ...ref, referenceName: `${member}.${rest.join('.')}` }, member, context);
4748 }
4749
4750 // When the reference is a call, prefer a callable member if several nodes
4751 // share the qualifiedName (e.g. a static property and a method).
4752 if (ref.referenceKind === 'calls') {
4753 const callable = candidates.find((n) => n.kind === 'method' || n.kind === 'function');
4754 if (callable) return callable;
4755 }
4756 return candidates[0];
4757}
4758
4759/**
4760 * Is `name`, as used in `ref`'s file, bound by an import whose module lives

Callers 1

resolveViaImportFunction · 0.85

Calls 3

hasMethod · 0.80
joinMethod · 0.45

Tested by

no test coverage detected