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

Function resolveModuleImportToFile

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

Source from the content-addressed store, hash-verified

4116}
4117
4118function resolveModuleImportToFile(
4119 ref: UnresolvedRef,
4120 imports: ImportMapping[],
4121 context: ResolutionContext
4122): ResolvedRef | null {
4123 if (ref.referenceKind !== 'imports') return null;
4124 if (ref.referenceName.includes('.')) return null;
4125
4126 for (const imp of imports) {
4127 if (imp.localName !== ref.referenceName) continue;
4128
4129 let modulePath: string;
4130 if (imp.isNamespace || imp.isDefault) {
4131 // `import * as ns from './x'` (namespace) or `import x from './x'`
4132 // (default) — the dependency is on the MODULE FILE. A default import binds
4133 // a (possibly renamed) local to whatever the module's default export is
4134 // (`import articlesController from './article.controller'` ← `export
4135 // default router`), so the binding name can't be found as a symbol — link
4136 // the file the import resolves to instead. External modules don't resolve
4137 // (no file), so `import React from 'react'` creates no edge.
4138 modulePath = imp.source;
4139 } else if (ref.language === 'python') {
4140 // `from . import certs` — the imported NAME is a submodule of the source,
4141 // unless the package itself binds that name (see pythonPackageBinds).
4142 // As in resolvePythonModuleMember, use the exported name so an alias
4143 // still links to the real module file (#1626).
4144 if (pythonPackageBinds(imp, ref.filePath, context)) continue;
4145 const moduleName = imp.exportedName === '*' ? imp.localName : imp.exportedName;
4146 modulePath = imp.source.endsWith('.')
4147 ? imp.source + moduleName
4148 : imp.source + '.' + moduleName;
4149 } else {
4150 // A named TS/JS import binds a symbol, not a module — leave it alone.
4151 continue;
4152 }
4153
4154 const declaring = declaredModuleFile(modulePath, ref, context);
4155 if (declaring) return { original: ref, targetNodeId: declaring.id, confidence: 0.9, resolvedBy: 'import' };
4156
4157 const resolvedPath = resolveImportPath(modulePath, ref.filePath, ref.language, context);
4158 if (resolvedPath && resolvedPath !== ref.filePath) {
4159 const fileNode = context.getNodesInFile(resolvedPath).find((n) => n.kind === 'file');
4160 if (fileNode) {
4161 return { original: ref, targetNodeId: fileNode.id, confidence: 0.9, resolvedBy: 'import' };
4162 }
4163 }
4164 }
4165 return null;
4166}
4167
4168/**
4169 * 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