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

Function requireModule

src/extraction/languages/lua.ts:29–61  ·  view source on GitHub ↗

* If `callNode` is a `require(...)` call, return the module name; otherwise null. * Lua/Luau have no import statement — modules are loaded by calling the global * `require`. Handles both: * - string requires: `require("net.http")` / `require "net.http"` → "net.http" * - Roblox/Luau path re

(callNode: SyntaxNode, source: string)

Source from the content-addressed store, hash-verified

27 * rather than a string — use the trailing field as the module name).
28 */
29function requireModule(callNode: SyntaxNode, source: string): string | null {
30 // function_call > name: <callee>, arguments: arguments
31 const name = getChildByField(callNode, 'name');
32 // A dotted/colon callee (e.g. `socket.connect`) is dot/method_index_expression,
33 // never a bare `require`.
34 if (!name || name.type !== 'identifier') return null;
35 if (getNodeText(name, source) !== 'require') return null;
36
37 const args = getChildByField(callNode, 'arguments');
38 if (!args) return null;
39
40 // String require — `string > content: string_content` gives the bare name.
41 const content = findDescendant(args, 'string_content');
42 if (content) return getNodeText(content, source).trim() || null;
43 const str = findDescendant(args, 'string');
44 if (str) {
45 const mod = getNodeText(str, source)
46 .trim()
47 .replace(/^\[\[/, '')
48 .replace(/\]\]$/, '')
49 .replace(/^["']/, '')
50 .replace(/["']$/, '');
51 if (mod) return mod;
52 }
53
54 // Roblox/Luau instance-path require: `require(script.Parent.Signal)` → "Signal".
55 const idx = findDescendant(args, 'dot_index_expression') ?? findDescendant(args, 'method_index_expression');
56 if (idx) {
57 const field = getChildByField(idx, 'field') ?? getChildByField(idx, 'method');
58 if (field) return getNodeText(field, source).trim() || null;
59 }
60 return null;
61}
62
63export const luaExtractor: LanguageExtractor = {
64 // function_declaration covers global (`function f`), table (`function t.f`),

Callers 2

emitFunction · 0.85
lua.tsFile · 0.85

Calls 3

getChildByFieldFunction · 0.90
getNodeTextFunction · 0.90
findDescendantFunction · 0.85

Tested by

no test coverage detected