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

Function resolveViaImport

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

Source from the content-addressed store, hash-verified

3177}
3178
3179export function resolveViaImport(
3180 ref: UnresolvedRef,
3181 context: ResolutionContext
3182): ResolvedRef | null {
3183 // A JS/TS module specifier — `import './polyfills'`, the module of `import x
3184 // from '../lib/a'`, a CommonJS `require('./application')` — names a FILE,
3185 // found the way the runtime finds it: extensions, `index` files, path
3186 // aliases. Matching the basename instead missed every extensionless one
3187 // (the common spelling) and could land on a same-named file elsewhere.
3188 const moduleFile = jsModuleFile(ref, context);
3189 if (moduleFile) {
3190 const fileNode = context.getNodesInFile(moduleFile).find((n) => n.kind === 'file');
3191 if (fileNode) return { original: ref, targetNodeId: fileNode.id, confidence: 0.9, resolvedBy: 'import' };
3192 }
3193 // A Dart `import` / `export` URI names a library file, and a `part` URI one
3194 // of the library's own files: `package:app/x.dart` is app's lib/x.dart, any
3195 // other path is from the file the directive is in.
3196 if (isDartImportRef(ref)) {
3197 const file = dartDirectiveFile(ref.filePath, ref.referenceName, context);
3198 const fileNode = file && file !== ref.filePath ? context.getNodesInFile(file).find((n) => n.kind === 'file') : undefined;
3199 return fileNode ? { original: ref, targetNodeId: fileNode.id, confidence: 0.9, resolvedBy: 'import' } : null;
3200 }
3201 // C/C++ #include references — resolve directly to the included file
3202 // (file→file edge), bypassing symbol lookup. The extractor emits these
3203 // with `referenceKind: 'imports'` and `referenceName: <include path>`
3204 // (e.g. "uint256.h" or "common/args.h"). Without this branch the
3205 // include-dir scan path inside resolveImportPath never produces an
3206 // edge — resolveViaImport's symbol lookup below would search the
3207 // resolved file for a symbol named like the file extension and fail.
3208 if ((ref.language === 'c' || ref.language === 'cpp') && ref.referenceKind === 'imports') {
3209 // C/C++ quoted includes (`#include "X.h"`) resolve relative to the
3210 // INCLUDING file's own directory first (the C standard's quoted-include
3211 // search order). Prefer a same-directory header over an -I directory or a
3212 // same-named header on another platform (windows/code/RNCAsyncStorage.h vs
3213 // apple/.../RNCAsyncStorage.h) — the include-dir heuristic below would
3214 // otherwise pick an arbitrary same-named header, leaving the real local one
3215 // with no dependents.
3216 const slash = ref.filePath.lastIndexOf('/');
3217 const fromDir = slash >= 0 ? ref.filePath.slice(0, slash) : '';
3218 const siblingPath = path.posix.normalize(fromDir ? `${fromDir}/${ref.referenceName}` : ref.referenceName);
3219 const siblingBase = siblingPath.split('/').pop()!;
3220 const sibling = context
3221 .getNodesByName(siblingBase)
3222 .find((n) => n.kind === 'file' && n.filePath === siblingPath);
3223 if (sibling) {
3224 return { original: ref, targetNodeId: sibling.id, confidence: 0.92, resolvedBy: 'import' };
3225 }
3226 const resolvedPath = resolveImportPath(ref.referenceName, ref.filePath, ref.language, context);
3227 if (!resolvedPath) return null;
3228 const basename = resolvedPath.split('/').pop()!;
3229 const fileNodes = context.getNodesByName(basename).filter((n) => n.kind === 'file');
3230 const fileNode = fileNodes.find((n) => n.filePath === resolvedPath);
3231 if (fileNode) {
3232 return {
3233 original: ref,
3234 targetNodeId: fileNode.id,
3235 confidence: 0.9,
3236 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