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

Function resolveLuaRequire

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

1686 * requiring file wins (instance-path requires resolve within the same package).
1687 */
1688function resolveLuaRequire(ref: UnresolvedRef, context: ResolutionContext): ResolvedRef | null {
1689 const name = ref.referenceName;
1690 if (!name) return null;
1691 const base = name.includes('.') ? name.replace(/\./g, '/') : name;
1692 const suffixes = [`${base}.lua`, `${base}.luau`, `${base}/init.lua`, `${base}/init.luau`];
1693 const byBasename = luaBasenameIndex(context);
1694 const shared = (a: string, b: string): number => {
1695 let i = 0;
1696 while (i < a.length && i < b.length && a[i] === b[i]) i++;
1697 return i;
1698 };
1699 for (const suffix of suffixes) {
1700 // Only files sharing the suffix's basename can match — the bucket is in
1701 // getAllFiles() order, so this filter yields exactly what the full-list
1702 // scan did.
1703 const candidates = byBasename.get(suffix.split('/').pop() ?? '') ?? [];
1704 const matches = candidates.filter((f) => f === suffix || f.endsWith('/' + suffix));
1705 if (matches.length === 0) continue;
1706 matches.sort((x, y) => shared(y, ref.filePath) - shared(x, ref.filePath));
1707 const best = matches[0]!;
1708 if (best === ref.filePath) continue;
1709 const fileNode = context.getNodesInFile(best).find((n) => n.kind === 'file');
1710 if (fileNode) {
1711 // Confidence ≥ 0.9 so this deterministic path/suffix match wins over
1712 // name-matching, which otherwise resolves the require to the import node
1713 // itself (a same-name self-match).
1714 return { original: ref, targetNodeId: fileNode.id, confidence: 0.9, resolvedBy: 'import' };
1715 }
1716 }
1717 return null;
1718}
1719
1720function resolveModuleImportToFile(
1721 ref: UnresolvedRef,

Callers 1

resolveViaImportFunction · 0.85

Calls 4

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

Tested by

no test coverage detected