MCPcopy Index your code
hub / github.com/colbymchenry/codegraph / matchByFilePath

Function matchByFilePath

src/resolution/name-matcher.ts:41–104  ·  view source on GitHub ↗
(
  ref: UnresolvedRef,
  context: ResolutionContext
)

Source from the content-addressed store, hash-verified

39 * by matching the filename against file nodes.
40 */
41export function matchByFilePath(
42 ref: UnresolvedRef,
43 context: ResolutionContext
44): ResolvedRef | null {
45 // Path-like (`a/b.liquid`) OR a bare filename ending in a short extension
46 // (`Foo.h` — an Objective-C `#import "Foo.h"`, resolved to the header by
47 // basename). A bare ref WITHOUT an extension is a symbol name, not a file, so
48 // leave it to the symbol-matching strategies.
49 if (!ref.referenceName.includes('/') && !/\.[A-Za-z][A-Za-z0-9]{0,3}$/.test(ref.referenceName)) {
50 return null;
51 }
52
53 // Extract the filename from the path
54 const fileName = ref.referenceName.split('/').pop();
55 if (!fileName) return null;
56
57 // Search for file nodes with this name
58 const candidates = context.getNodesByName(fileName);
59 const fileNodes = candidates.filter(n => n.kind === 'file');
60
61 if (fileNodes.length === 0) return null;
62
63 // Prefer exact path match on qualified_name
64 const exactMatch = fileNodes.find(n => n.qualifiedName === ref.referenceName || n.filePath === ref.referenceName);
65 if (exactMatch) {
66 return {
67 original: ref,
68 targetNodeId: exactMatch.id,
69 confidence: 0.95,
70 resolvedBy: 'file-path',
71 };
72 }
73
74 // Fall back to suffix match (e.g., ref="snippets/foo.liquid" matches
75 // "src/snippets/foo.liquid"). When several files share the basename — a
76 // `#include "RNCAsyncStorage.h"` with a same-named header on another platform
77 // (windows/code/ vs apple/) — prefer the one in the includer's own directory,
78 // then by directory proximity / same language family. A C/C++ include (and any
79 // bare-filename import) resolves relative to the including file, not to an
80 // arbitrary same-named header elsewhere in the tree.
81 const suffixMatches = fileNodes.filter(
82 n => n.qualifiedName.endsWith(ref.referenceName) || n.filePath.endsWith(ref.referenceName)
83 );
84 if (suffixMatches.length > 0) {
85 return {
86 original: ref,
87 targetNodeId: pickClosestFileNode(suffixMatches, ref).id,
88 confidence: 0.85,
89 resolvedBy: 'file-path',
90 };
91 }
92
93 // If only one file node with this name, use it with lower confidence
94 if (fileNodes.length === 1) {
95 return {
96 original: ref,
97 targetNodeId: fileNodes[0]!.id,
98 confidence: 0.7,

Callers 1

matchReferenceFunction · 0.85

Calls 2

pickClosestFileNodeFunction · 0.85
getNodesByNameMethod · 0.65

Tested by

no test coverage detected