| 1286 | // function_call: require("X") |
| 1287 | |
| 1288 | static void parse_lua_imports(CBMExtractCtx *ctx) { |
| 1289 | CBMArena *a = ctx->arena; |
| 1290 | |
| 1291 | TSTreeCursor cursor = ts_tree_cursor_new(ctx->root); |
| 1292 | if (!ts_tree_cursor_goto_first_child(&cursor)) { |
| 1293 | ts_tree_cursor_delete(&cursor); |
| 1294 | return; |
| 1295 | } |
| 1296 | do { |
| 1297 | TSNode node = ts_tree_cursor_current_node(&cursor); |
| 1298 | // Lua: local X = require("Y") → assignment_statement or variable_declaration |
| 1299 | // containing function_call(require, "Y") |
| 1300 | char *text = cbm_node_text(a, node, ctx->source); |
| 1301 | if (!text) { |
| 1302 | continue; |
| 1303 | } |
| 1304 | if (strstr(text, "require") == NULL) { |
| 1305 | continue; |
| 1306 | } |
| 1307 | |
| 1308 | // Simple extraction: find require("...") pattern in node text |
| 1309 | const char *req = strstr(text, "require"); |
| 1310 | if (!req) { |
| 1311 | continue; |
| 1312 | } |
| 1313 | |
| 1314 | // Find the string argument |
| 1315 | const char *open = strchr(req, '('); |
| 1316 | if (!open) { |
| 1317 | open = strchr(req, '"'); |
| 1318 | } |
| 1319 | if (!open) { |
| 1320 | open = strchr(req, '\''); |
| 1321 | } |
| 1322 | if (!open) { |
| 1323 | continue; |
| 1324 | } |
| 1325 | |
| 1326 | const char *q1 = strchr(open, '"'); |
| 1327 | const char *q2 = strchr(open, '\''); |
| 1328 | if (!q1 && !q2) { |
| 1329 | continue; |
| 1330 | } |
| 1331 | const char *start = q1 && (!q2 || q1 < q2) ? q1 : q2; |
| 1332 | char delim = *start; |
| 1333 | start++; |
| 1334 | const char *end = strchr(start, delim); |
| 1335 | if (!end) { |
| 1336 | continue; |
| 1337 | } |
| 1338 | |
| 1339 | char *mod = cbm_arena_strndup(a, start, (size_t)(end - start)); |
| 1340 | CBMImport imp = {.local_name = path_last(a, mod), .module_path = mod}; |
| 1341 | cbm_imports_push(&ctx->result->imports, a, imp); |
| 1342 | } while (ts_tree_cursor_goto_next_sibling(&cursor)); |
| 1343 | ts_tree_cursor_delete(&cursor); |
| 1344 | } |
| 1345 |
no test coverage detected