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

Function resolveRelativeImport

src/resolution/import-resolver.ts:1369–1397  ·  view source on GitHub ↗

* Resolve a relative import

(
  importPath: string,
  fromDir: string,
  language: Language,
  context: ResolutionContext
)

Source from the content-addressed store, hash-verified

1367 * Resolve a relative import
1368 */
1369function resolveRelativeImport(
1370 importPath: string,
1371 fromDir: string,
1372 language: Language,
1373 context: ResolutionContext
1374): string | null {
1375 const projectRoot = context.getProjectRoot();
1376 const extensions = EXTENSION_RESOLUTION[language] || [];
1377
1378 // Try the path as-is first
1379 const basePath = path.resolve(fromDir, importPath);
1380 const relativePath = path.relative(projectRoot, basePath).replace(/\\/g, '/');
1381
1382 // Try each extension. `require('..')` up to the project root is its `index.js`.
1383 for (const ext of extensions) {
1384 if (relativePath === '' && !ext.startsWith('/')) continue;
1385 const candidatePath = relativePath === '' ? ext.slice(1) : relativePath + ext;
1386 if (context.fileExists(candidatePath)) {
1387 return candidatePath;
1388 }
1389 }
1390
1391 // Try without extension (might already have one)
1392 if (context.fileExists(relativePath)) {
1393 return relativePath;
1394 }
1395
1396 return findSourceForEmittedSpecifier(relativePath, language, context);
1397}
1398
1399/**
1400 * TypeScript under `moduleResolution: node16 | nodenext | bundler` writes the

Callers 1

Calls 4

fileExistsMethod · 0.80
getProjectRootMethod · 0.65
resolveMethod · 0.65

Tested by

no test coverage detected