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

Function resolveModuleImportToFile

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

Source from the content-addressed store, hash-verified

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