MCPcopy
hub / github.com/colbymchenry/codegraph / resolveModuleImportToFile

Function resolveModuleImportToFile

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

Source from the content-addressed store, hash-verified

1455}
1456
1457function resolveModuleImportToFile(
1458 ref: UnresolvedRef,
1459 imports: ImportMapping[],
1460 context: ResolutionContext
1461): ResolvedRef | null {
1462 if (ref.referenceKind !== 'imports') return null;
1463 if (ref.referenceName.includes('.')) return null;
1464
1465 for (const imp of imports) {
1466 if (imp.localName !== ref.referenceName) continue;
1467
1468 let modulePath: string;
1469 if (imp.isNamespace || imp.isDefault) {
1470 // `import * as ns from './x'` (namespace) or `import x from './x'`
1471 // (default) — the dependency is on the MODULE FILE. A default import binds
1472 // a (possibly renamed) local to whatever the module's default export is
1473 // (`import articlesController from './article.controller'` ← `export
1474 // default router`), so the binding name can't be found as a symbol — link
1475 // the file the import resolves to instead. External modules don't resolve
1476 // (no file), so `import React from 'react'` creates no edge.
1477 modulePath = imp.source;
1478 } else if (ref.language === 'python') {
1479 // `from . import certs` — the imported NAME is a submodule of the source.
1480 modulePath = imp.source.endsWith('.')
1481 ? imp.source + imp.localName
1482 : imp.source + '.' + imp.localName;
1483 } else {
1484 // A named TS/JS import binds a symbol, not a module — leave it alone.
1485 continue;
1486 }
1487
1488 const resolvedPath = resolveImportPath(modulePath, ref.filePath, ref.language, context);
1489 if (resolvedPath && resolvedPath !== ref.filePath) {
1490 const fileNode = context.getNodesInFile(resolvedPath).find((n) => n.kind === 'file');
1491 if (fileNode) {
1492 return { original: ref, targetNodeId: fileNode.id, confidence: 0.9, resolvedBy: 'import' };
1493 }
1494 }
1495
1496 // Python absolute `from a.b import submodule` (a FastAPI router aggregator's
1497 // `from app.api.routes import authentication`): resolveImportPath only maps
1498 // RELATIVE dotted paths to a file, so resolve the absolute dotted module
1499 // directly to its file node.
1500 if (ref.language === 'python') {
1501 const modFile = findPythonModuleFile(modulePath, context, ref.filePath);
1502 if (modFile) {
1503 return { original: ref, targetNodeId: modFile.id, confidence: 0.9, resolvedBy: 'import' };
1504 }
1505 }
1506 }
1507 return null;
1508}
1509
1510/**
1511 * 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