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

Function resolveModuleImportToFile

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

Source from the content-addressed store, hash-verified

4364}
4365
4366function resolveModuleImportToFile(
4367 ref: UnresolvedRef,
4368 imports: ImportMapping[],
4369 context: ResolutionContext
4370): ResolvedRef | null {
4371 if (ref.referenceKind !== 'imports') return null;
4372 if (ref.referenceName.includes('.')) return null;
4373
4374 for (const imp of importsBoundTo(imports, ref.referenceName)) {
4375
4376 let modulePath: string;
4377 if (imp.isNamespace || imp.isDefault) {
4378 // `import * as ns from './x'` (namespace) or `import x from './x'`
4379 // (default) — the dependency is on the MODULE FILE. A default import binds
4380 // a (possibly renamed) local to whatever the module's default export is
4381 // (`import articlesController from './article.controller'` ← `export
4382 // default router`), so the binding name can't be found as a symbol — link
4383 // the file the import resolves to instead. External modules don't resolve
4384 // (no file), so `import React from 'react'` creates no edge.
4385 modulePath = imp.source;
4386 } else if (ref.language === 'python') {
4387 // `from . import certs` — the imported NAME is a submodule of the source,
4388 // unless the package itself binds that name (see pythonPackageBinds).
4389 // As in resolvePythonModuleMember, use the exported name so an alias
4390 // still links to the real module file (#1626).
4391 if (pythonPackageBinds(imp, ref.filePath, context)) continue;
4392 const moduleName = imp.exportedName === '*' ? imp.localName : imp.exportedName;
4393 modulePath = imp.source.endsWith('.')
4394 ? imp.source + moduleName
4395 : imp.source + '.' + moduleName;
4396 } else {
4397 // A named TS/JS import binds a symbol, not a module — leave it alone.
4398 continue;
4399 }
4400
4401 const declaring = declaredModuleFile(modulePath, ref, context);
4402 if (declaring) return { original: ref, targetNodeId: declaring.id, confidence: 0.9, resolvedBy: 'import' };
4403
4404 const resolvedPath = resolveImportPath(modulePath, ref.filePath, ref.language, context);
4405 if (resolvedPath && resolvedPath !== ref.filePath) {
4406 const fileNode = context.getNodesInFile(resolvedPath).find((n) => n.kind === 'file');
4407 if (fileNode) {
4408 return { original: ref, targetNodeId: fileNode.id, confidence: 0.9, resolvedBy: 'import' };
4409 }
4410 }
4411 }
4412 return null;
4413}
4414
4415/**
4416 * 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