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

Function resolveLuaRequire

src/resolution/import-resolver.ts:4058–4088  ·  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

4056 * requiring file wins (instance-path requires resolve within the same package).
4057 */
4058function resolveLuaRequire(ref: UnresolvedRef, context: ResolutionContext): ResolvedRef | null {
4059 const name = ref.referenceName;
4060 if (!name) return null;
4061 const base = name.includes('.') ? name.replace(/\./g, '/') : name;
4062 const suffixes = [`${base}.lua`, `${base}.luau`, `${base}/init.lua`, `${base}/init.luau`];
4063 const byBasename = luaBasenameIndex(context);
4064 const shared = (a: string, b: string): number => {
4065 let i = 0;
4066 while (i < a.length && i < b.length && a[i] === b[i]) i++;
4067 return i;
4068 };
4069 for (const suffix of suffixes) {
4070 // Only files sharing the suffix's basename can match — the bucket is in
4071 // getAllFiles() order, so this filter yields exactly what the full-list
4072 // scan did.
4073 const candidates = byBasename.get(suffix.split('/').pop() ?? '') ?? [];
4074 const matches = candidates.filter((f) => f === suffix || f.endsWith('/' + suffix));
4075 if (matches.length === 0) continue;
4076 matches.sort((x, y) => shared(y, ref.filePath) - shared(x, ref.filePath));
4077 const best = matches[0]!;
4078 if (best === ref.filePath) continue;
4079 const fileNode = context.getNodesInFile(best).find((n) => n.kind === 'file');
4080 if (fileNode) {
4081 // Confidence ≥ 0.9 so this deterministic path/suffix match wins over
4082 // name-matching, which otherwise resolves the require to the import node
4083 // itself (a same-name self-match).
4084 return { original: ref, targetNodeId: fileNode.id, confidence: 0.9, resolvedBy: 'import' };
4085 }
4086 }
4087 return null;
4088}
4089
4090/**
4091 * `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