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

Function resolveModuleImportToFile

src/resolution/import-resolver.ts:3917–3965  ·  view source on GitHub ↗
(
  ref: UnresolvedRef,
  imports: ImportMapping[],
  context: ResolutionContext
)

Source from the content-addressed store, hash-verified

3915}
3916
3917function resolveModuleImportToFile(
3918 ref: UnresolvedRef,
3919 imports: ImportMapping[],
3920 context: ResolutionContext
3921): ResolvedRef | null {
3922 if (ref.referenceKind !== 'imports') return null;
3923 if (ref.referenceName.includes('.')) return null;
3924
3925 for (const imp of imports) {
3926 if (imp.localName !== ref.referenceName) continue;
3927
3928 let modulePath: string;
3929 if (imp.isNamespace || imp.isDefault) {
3930 // `import * as ns from './x'` (namespace) or `import x from './x'`
3931 // (default) — the dependency is on the MODULE FILE. A default import binds
3932 // a (possibly renamed) local to whatever the module's default export is
3933 // (`import articlesController from './article.controller'` ← `export
3934 // default router`), so the binding name can't be found as a symbol — link
3935 // the file the import resolves to instead. External modules don't resolve
3936 // (no file), so `import React from 'react'` creates no edge.
3937 modulePath = imp.source;
3938 } else if (ref.language === 'python') {
3939 // `from . import certs` — the imported NAME is a submodule of the source,
3940 // unless the package itself binds that name (see pythonPackageBinds).
3941 // As in resolvePythonModuleMember, use the exported name so an alias
3942 // still links to the real module file (#1626).
3943 if (pythonPackageBinds(imp, ref.filePath, context)) continue;
3944 const moduleName = imp.exportedName === '*' ? imp.localName : imp.exportedName;
3945 modulePath = imp.source.endsWith('.')
3946 ? imp.source + moduleName
3947 : imp.source + '.' + moduleName;
3948 } else {
3949 // A named TS/JS import binds a symbol, not a module — leave it alone.
3950 continue;
3951 }
3952
3953 const declaring = declaredModuleFile(modulePath, ref, context);
3954 if (declaring) return { original: ref, targetNodeId: declaring.id, confidence: 0.9, resolvedBy: 'import' };
3955
3956 const resolvedPath = resolveImportPath(modulePath, ref.filePath, ref.language, context);
3957 if (resolvedPath && resolvedPath !== ref.filePath) {
3958 const fileNode = context.getNodesInFile(resolvedPath).find((n) => n.kind === 'file');
3959 if (fileNode) {
3960 return { original: ref, targetNodeId: fileNode.id, confidence: 0.9, resolvedBy: 'import' };
3961 }
3962 }
3963 }
3964 return null;
3965}
3966
3967/**
3968 * Resolve a Python dotted module import (`import a.b.c`, `from .a.b import x`) to its file —

Callers 1

resolveViaImportFunction · 0.85

Calls 4

pythonPackageBindsFunction · 0.85
declaredModuleFileFunction · 0.85
resolveImportPathFunction · 0.85
getNodesInFileMethod · 0.65

Tested by

no test coverage detected