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

Function resolveModuleImportToFile

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

Source from the content-addressed store, hash-verified

1703}
1704
1705function resolveModuleImportToFile(
1706 ref: UnresolvedRef,
1707 imports: ImportMapping[],
1708 context: ResolutionContext
1709): ResolvedRef | null {
1710 if (ref.referenceKind !== 'imports') return null;
1711 if (ref.referenceName.includes('.')) return null;
1712
1713 for (const imp of imports) {
1714 if (imp.localName !== ref.referenceName) continue;
1715
1716 let modulePath: string;
1717 if (imp.isNamespace || imp.isDefault) {
1718 // `import * as ns from './x'` (namespace) or `import x from './x'`
1719 // (default) — the dependency is on the MODULE FILE. A default import binds
1720 // a (possibly renamed) local to whatever the module's default export is
1721 // (`import articlesController from './article.controller'` ← `export
1722 // default router`), so the binding name can't be found as a symbol — link
1723 // the file the import resolves to instead. External modules don't resolve
1724 // (no file), so `import React from 'react'` creates no edge.
1725 modulePath = imp.source;
1726 } else if (ref.language === 'python') {
1727 // `from . import certs` — the imported NAME is a submodule of the source.
1728 modulePath = imp.source.endsWith('.')
1729 ? imp.source + imp.localName
1730 : imp.source + '.' + imp.localName;
1731 } else {
1732 // A named TS/JS import binds a symbol, not a module — leave it alone.
1733 continue;
1734 }
1735
1736 const resolvedPath = resolveImportPath(modulePath, ref.filePath, ref.language, context);
1737 if (resolvedPath && resolvedPath !== ref.filePath) {
1738 const fileNode = context.getNodesInFile(resolvedPath).find((n) => n.kind === 'file');
1739 if (fileNode) {
1740 return { original: ref, targetNodeId: fileNode.id, confidence: 0.9, resolvedBy: 'import' };
1741 }
1742 }
1743
1744 // Python absolute `from a.b import submodule` (a FastAPI router aggregator's
1745 // `from app.api.routes import authentication`): resolveImportPath only maps
1746 // RELATIVE dotted paths to a file, so resolve the absolute dotted module
1747 // directly to its file node.
1748 if (ref.language === 'python') {
1749 const modFile = findPythonModuleFile(modulePath, context, ref.filePath);
1750 if (modFile) {
1751 return { original: ref, targetNodeId: modFile.id, confidence: 0.9, resolvedBy: 'import' };
1752 }
1753 }
1754 }
1755 return null;
1756}
1757
1758/**
1759 * Find the file node for a Python dotted module path `a.b.c` — a module file

Callers 1

resolveViaImportFunction · 0.85

Calls 3

resolveImportPathFunction · 0.85
findPythonModuleFileFunction · 0.85
getNodesInFileMethod · 0.65

Tested by

no test coverage detected