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

Function resolveViaImport

src/resolution/import-resolver.ts:3319–3706  ·  view source on GitHub ↗
(
  ref: UnresolvedRef,
  context: ResolutionContext
)

Source from the content-addressed store, hash-verified

3317}
3318
3319export function resolveViaImport(
3320 ref: UnresolvedRef,
3321 context: ResolutionContext
3322): ResolvedRef | null {
3323 // A JS/TS module specifier — `import './polyfills'`, the module of `import x
3324 // from '../lib/a'`, a CommonJS `require('./application')` — names a FILE,
3325 // found the way the runtime finds it: extensions, `index` files, path
3326 // aliases. Matching the basename instead missed every extensionless one
3327 // (the common spelling) and could land on a same-named file elsewhere.
3328 const moduleFile = jsModuleFile(ref, context);
3329 if (moduleFile) {
3330 const fileNode = context.getNodesInFile(moduleFile).find((n) => n.kind === 'file');
3331 if (fileNode) return { original: ref, targetNodeId: fileNode.id, confidence: 0.9, resolvedBy: 'import' };
3332 }
3333 // A Dart `import` / `export` URI names a library file, and a `part` URI one
3334 // of the library's own files: `package:app/x.dart` is app's lib/x.dart, any
3335 // other path is from the file the directive is in.
3336 if (isDartImportRef(ref)) {
3337 const file = dartDirectiveFile(ref.filePath, ref.referenceName, context);
3338 const fileNode = file && file !== ref.filePath ? context.getNodesInFile(file).find((n) => n.kind === 'file') : undefined;
3339 return fileNode ? { original: ref, targetNodeId: fileNode.id, confidence: 0.9, resolvedBy: 'import' } : null;
3340 }
3341 // C/C++ #include references — resolve directly to the included file
3342 // (file→file edge), bypassing symbol lookup. The extractor emits these
3343 // with `referenceKind: 'imports'` and `referenceName: <include path>`
3344 // (e.g. "uint256.h" or "common/args.h"). Without this branch the
3345 // include-dir scan path inside resolveImportPath never produces an
3346 // edge — resolveViaImport's symbol lookup below would search the
3347 // resolved file for a symbol named like the file extension and fail.
3348 if ((ref.language === 'c' || ref.language === 'cpp') && ref.referenceKind === 'imports') {
3349 // C/C++ quoted includes (`#include "X.h"`) resolve relative to the
3350 // INCLUDING file's own directory first (the C standard's quoted-include
3351 // search order). Prefer a same-directory header over an -I directory or a
3352 // same-named header on another platform (windows/code/RNCAsyncStorage.h vs
3353 // apple/.../RNCAsyncStorage.h) — the include-dir heuristic below would
3354 // otherwise pick an arbitrary same-named header, leaving the real local one
3355 // with no dependents.
3356 const slash = ref.filePath.lastIndexOf('/');
3357 const fromDir = slash >= 0 ? ref.filePath.slice(0, slash) : '';
3358 const siblingPath = path.posix.normalize(fromDir ? `${fromDir}/${ref.referenceName}` : ref.referenceName);
3359 const siblingBase = siblingPath.split('/').pop()!;
3360 const sibling = context
3361 .getNodesByName(siblingBase)
3362 .find((n) => n.kind === 'file' && n.filePath === siblingPath);
3363 if (sibling) {
3364 return { original: ref, targetNodeId: sibling.id, confidence: 0.92, resolvedBy: 'import' };
3365 }
3366 const resolvedPath = resolveImportPath(ref.referenceName, ref.filePath, ref.language, context);
3367 if (!resolvedPath) return null;
3368 const basename = resolvedPath.split('/').pop()!;
3369 const fileNodes = context.getNodesByName(basename).filter((n) => n.kind === 'file');
3370 const fileNode = fileNodes.find((n) => n.filePath === resolvedPath);
3371 if (fileNode) {
3372 return {
3373 original: ref,
3374 targetNodeId: fileNode.id,
3375 confidence: 0.9,
3376 resolvedBy: 'import',

Callers 2

createContextMethod · 0.90
resolveOneInnerMethod · 0.90

Calls 15

dartDirectiveFileFunction · 0.90
jsModuleFileFunction · 0.85
isDartImportRefFunction · 0.85
resolveImportPathFunction · 0.85
isCobolCopybookRefFunction · 0.85
isPhpIncludePathRefFunction · 0.85
resolvePhpIncludePathFunction · 0.85
isNixPathImportRefFunction · 0.85
pythonImportsInScopeFunction · 0.85

Tested by

no test coverage detected