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

Function resolveLuaRequire

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

* Resolve a Lua/Luau `require(...)` to its module file. The reference name is * either a dotted module path (`telescope.config` → `telescope/config.lua`) or a * Roblox instance-path leaf (`Signal` from `require(script.Parent.Signal)` → * `Signal.luau`). We try ` .lua|.luau` and ` /init.

(ref: UnresolvedRef, context: ResolutionContext)

Source from the content-addressed store, hash-verified

4304 * requiring file wins (instance-path requires resolve within the same package).
4305 */
4306function resolveLuaRequire(ref: UnresolvedRef, context: ResolutionContext): ResolvedRef | null {
4307 const name = ref.referenceName;
4308 if (!name) return null;
4309 const base = name.includes('.') ? name.replace(/\./g, '/') : name;
4310 const suffixes = [`${base}.lua`, `${base}.luau`, `${base}/init.lua`, `${base}/init.luau`];
4311 const byBasename = luaBasenameIndex(context);
4312 const shared = (a: string, b: string): number => {
4313 let i = 0;
4314 while (i < a.length && i < b.length && a[i] === b[i]) i++;
4315 return i;
4316 };
4317 for (const suffix of suffixes) {
4318 // Only files sharing the suffix's basename can match — the bucket is in
4319 // getAllFiles() order, so this filter yields exactly what the full-list
4320 // scan did.
4321 const candidates = byBasename.get(suffix.split('/').pop() ?? '') ?? [];
4322 const matches = candidates.filter((f) => f === suffix || f.endsWith('/' + suffix));
4323 if (matches.length === 0) continue;
4324 matches.sort((x, y) => shared(y, ref.filePath) - shared(x, ref.filePath));
4325 const best = matches[0]!;
4326 if (best === ref.filePath) continue;
4327 const fileNode = context.getNodesInFile(best).find((n) => n.kind === 'file');
4328 if (fileNode) {
4329 // Confidence ≥ 0.9 so this deterministic path/suffix match wins over
4330 // name-matching, which otherwise resolves the require to the import node
4331 // itself (a same-name self-match).
4332 return { original: ref, targetNodeId: fileNode.id, confidence: 0.9, resolvedBy: 'import' };
4333 }
4334 }
4335 return null;
4336}
4337
4338/**
4339 * `UploadApi.uploadARCapture()` where `UploadApi` is a NAMESPACE OBJECT — the

Callers 1

resolveViaImportFunction · 0.85

Calls 4

luaBasenameIndexFunction · 0.85
sharedFunction · 0.70
getMethod · 0.65
getNodesInFileMethod · 0.65

Tested by

no test coverage detected