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

Function resolveModuleImportToFile

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

Source from the content-addressed store, hash-verified

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

Callers 1

resolveViaImportFunction · 0.85

Calls 5

importsBoundToFunction · 0.90
pythonPackageBindsFunction · 0.85
declaredModuleFileFunction · 0.85
resolveImportPathFunction · 0.85
getNodesInFileMethod · 0.65

Tested by

no test coverage detected