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