Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
feat(r): box::use/library imports + module\$fn calls (#218, #219)
Martin Vogel1a636f1962ff
diff --git a/internal/cbm/extract_calls.c b/internal/cbm/extract_calls.cindex 942e306ff..fa78f61d5 100644--- a/internal/cbm/extract_calls.c+++ b/internal/cbm/extract_calls.c@@ -163,6 +163,23 @@ static char *extract_callee_from_fields(CBMArena *a, TSNode node, const char *sostrcmp(fk, "qualified_identifier") == 0) {return cbm_node_text(a, func_node, source);}+ // R member call: module$fn() — function node is an extract_operator+ // with lhs (object) and rhs (method). Emit "module.fn" so it resolves+ // like other member calls (#219). Previously dropped → no CALLS edge.+ if (strcmp(fk, "extract_operator") == 0) {+ TSNode lhs = ts_node_child_by_field_name(func_node, TS_FIELD("lhs"));+ TSNode rhs = ts_node_child_by_field_name(func_node, TS_FIELD("rhs"));+ if (!ts_node_is_null(rhs)) {+ char *rt = cbm_node_text(a, rhs, source);+ if (!ts_node_is_null(lhs)) {+ char *lt = cbm_node_text(a, lhs, source);+ if (lt && lt[0] && rt && rt[0]) {+ return cbm_arena_sprintf(a, "%s.%s", lt, rt);+ }+ }+ return rt;+ }+ }}// Try "name" field (Java method_invocation)diff --git a/internal/cbm/extract_imports.c b/internal/cbm/extract_imports.cindex 6295fb327..2d9430fe2 100644--- a/internal/cbm/extract_imports.c+++ b/internal/cbm/extract_imports.c@@ -28,6 +28,7 @@ static void parse_rust_imports(CBMExtractCtx *ctx);static void parse_c_imports(CBMExtractCtx *ctx);static void parse_ruby_imports(CBMExtractCtx *ctx);static void parse_lua_imports(CBMExtractCtx *ctx);+static void parse_r_imports(CBMExtractCtx *ctx);static void parse_generic_imports(CBMExtractCtx *ctx, const char *node_type);static void parse_wolfram_imports(CBMExtractCtx *ctx);@@ -711,6 +712,98 @@ static void parse_lua_imports(CBMExtractCtx *ctx) {ts_tree_cursor_delete(&cursor);}+// --- R imports: library()/require()/source() + box::use() (#218) ---++// Emit an IMPORTS edge for a module path string (strips a trailing [symbols]+// list and surrounding quotes; uses the last path segment as the local name).+static void r_push_import(CBMExtractCtx *ctx, const char *raw) {+ CBMArena *a = ctx->arena;+ if (!raw || !raw[0]) {+ return;+ }+ char *mod = strip_quotes(a, raw);+ // box::use specs look like "shiny[moduleServer, NS]" or+ // "app/logic/validation[validate_input]" — the module path is the part+ // before the '[' symbol list.+ char *bracket = strchr(mod, '[');+ if (bracket) {+ *bracket = '\0';+ }+ // Trim trailing whitespace left by truncation.+ size_t len = strlen(mod);+ while (len > 0 && (mod[len - SKIP_ONE] == ' ' || mod[len - SKIP_ONE] == '\t' ||+ mod[len - SKIP_ONE] == '\n')) {+ mod[--len] = '\0';+ }+ if (mod[0] == '\0') {+ return;+ }+ CBMImport imp = {.local_name = path_last(a, mod), .module_path = mod};+ cbm_imports_push(&ctx->result->imports, a, imp);+}++// Recursively scan R for import-producing calls.+static void r_collect_imports(CBMExtractCtx *ctx, TSNode node) { // NOLINT(misc-no-recursion)+ if (strcmp(ts_node_type(node), "call") == 0) {+ TSNode fn = ts_node_child_by_field_name(node, TS_FIELD("function"));+ TSNode args = ts_node_child_by_field_name(node, TS_FIELD("arguments"));+ if (!ts_node_is_null(fn) && !ts_node_is_null(args)) {+ const char *ft = ts_node_type(fn);+ if (strcmp(ft, "namespace_operator") == 0) {+ // box::use(mod[syms], pkg/path[syms], ...) — one IMPORTS per arg.+ TSNode lhs = ts_node_child_by_field_name(fn, TS_FIELD("lhs"));+ TSNode rhs = ts_node_child_by_field_name(fn, TS_FIELD("rhs"));+ char *lt =+ ts_node_is_null(lhs) ? NULL : cbm_node_text(ctx->arena, lhs, ctx->source);+ char *rt =+ ts_node_is_null(rhs) ? NULL : cbm_node_text(ctx->arena, rhs, ctx->source);+ if (lt && rt && strcmp(lt, "box") == 0 && strcmp(rt, "use") == 0) {+ uint32_t na = ts_node_named_child_count(args);+ for (uint32_t i = 0; i < na; i++) {+ TSNode arg = ts_node_named_child(args, i);+ if (strcmp(ts_node_type(arg), "argument") != 0) {+ continue;+ }+ TSNode val = ts_node_child_by_field_name(arg, TS_FIELD("value"));+ if (!ts_node_is_null(val)) {+ r_push_import(ctx, cbm_node_text(ctx->arena, val, ctx->source));+ }+ }+ }+ } else if (strcmp(ft, "identifier") == 0) {+ // library(pkg) / require(pkg) / requireNamespace("pkg") /+ // loadNamespace("pkg") / source("file.R") — first arg is the module.+ char *fname = cbm_node_text(ctx->arena, fn, ctx->source);+ if (fname &&+ (strcmp(fname, "library") == 0 || strcmp(fname, "require") == 0 ||+ strcmp(fname, "requireNamespace") == 0 ||+ strcmp(fname, "loadNamespace") == 0 || strcmp(fname, "source") == 0)) {+ uint32_t na = ts_node_named_child_count(args);+ for (uint32_t i = 0; i < na; i++) {+ TSNode arg = ts_node_named_child(args, i);+ if (strcmp(ts_node_type(arg), "argument") != 0) {+ continue;+ }+ TSNode val = ts_node_child_by_field_name(arg, TS_FIELD("value"));+ if (!ts_node_is_null(val)) {+ r_push_import(ctx, cbm_node_text(ctx->arena, val, ctx->source));+ }+ break; // first positional argument only+ }+ }+ }+ }+ }+ uint32_t n = ts_node_named_child_count(node);+ for (uint32_t i = 0; i < n; i++) {+ r_collect_imports(ctx, ts_node_named_child(node, i));+ }+}++static void parse_r_imports(CBMExtractCtx *ctx) {+ r_collect_imports(ctx, ctx->root);+}+// --- Generic import parsing for languages with simple import_declaration ---// Try known field names (path/source/module/name) to extract import path.@@ -979,6 +1072,9 @@ void cbm_extract_imports(CBMExtractCtx *ctx) {case CBM_LANG_LUA:parse_lua_imports(ctx);break;+ case CBM_LANG_R:+ parse_r_imports(ctx);+ break;case CBM_LANG_ELIXIR:// Elixir: import/use/alias/require are call nodesparse_generic_imports(ctx, "call");diff --git a/src/foundation/recursion_whitelist.h b/src/foundation/recursion_whitelist.hindex 982be35a2..b26e015fc 100644--- a/src/foundation/recursion_whitelist.h+++ b/src/foundation/recursion_whitelist.h@@ -15,6 +15,9 @@* - glob_match, glob_match_star, glob_match_doublestar* - glob_match_doublestar_slash, glob_match_doublestar_any*+ * R import scanner (bounded by source-file AST depth):+ * - r_collect_imports+ ** To add a function: add it below AND add NOLINT(misc-no-recursion) on* the function definition line. The lint gate verifies both match.*/@@ -22,4 +25,4 @@"parse_or_expr", "parse_xor_expr", "parse_and_expr", "parse_not_expr", "parse_atom_expr", \"parse_post_where", "cbm_parse", "eval_expr", "glob_match", "glob_match_star", \"glob_match_doublestar", "glob_match_doublestar_slash", "glob_match_doublestar_any", \- "parse_bool_expr", "parse_bool_atom"+ "parse_bool_expr", "parse_bool_atom", "r_collect_imports"diff --git a/tests/test_extraction.c b/tests/test_extraction.cindex 53e4af58e..fd1107b3d 100644--- a/tests/test_extraction.c+++ b/tests/test_extraction.c@@ -68,6 +68,37 @@ static CBMFileResult *extract(const char *src, CBMLanguage lang, const char *pro* Group A: OOP Languages* ═══════════════════════════════════════════════════════════════════ */+/* --- R: box::use imports (#218) + module$fn calls (#219) --- */+TEST(extract_r_box_use_imports_issue218) {+ CBMFileResult *r = extract("box::use(\n"+ " shiny[moduleServer, NS],\n"+ " app/logic/validation[validate_input],\n"+ ")\n"+ "library(dplyr)\n"+ "source(\"helpers.R\")\n",+ CBM_LANG_R, "t", "app.R");+ ASSERT_NOT_NULL(r);+ ASSERT_FALSE(r->has_error);+ /* box::use specs → one IMPORTS edge per module (symbol list stripped). */+ ASSERT(has_import(r, "shiny"));+ ASSERT(has_import(r, "app/logic/validation"));+ /* base-R imports work too. */+ ASSERT(has_import(r, "dplyr"));+ ASSERT(has_import(r, "helpers"));+ cbm_free_result(r);+ PASS();+}++TEST(extract_r_dollar_call_issue219) {+ CBMFileResult *r = extract("validation$validate_input(x)\n", CBM_LANG_R, "t", "app.R");+ ASSERT_NOT_NULL(r);+ ASSERT_FALSE(r->has_error);+ /* module$fn() now produces a CALLS edge (was silently dropped). */+ ASSERT(has_call(r, "validation.validate_input"));+ cbm_free_result(r);+ PASS();+}+/* --- Java --- */TEST(java_class) {CBMFileResult *r = extract(@@ -111,9 +142,9 @@ TEST(java_interface) {* was emitted, the interfaces were dropped.* 2) the emitted name was the full field text including the keyword. */TEST(java_class_extends_and_implements) {- CBMFileResult *r = extract(- "public class DefaultLinkTool extends DefaultDiagramTool implements ILinkTool, Closeable { }",- CBM_LANG_JAVA, "t", "DefaultLinkTool.java");+ CBMFileResult *r = extract("public class DefaultLinkTool extends DefaultDiagramTool implements "+ "ILinkTool, Closeable { }",+ CBM_LANG_JAVA, "t", "DefaultLinkTool.java");ASSERT_NOT_NULL(r);ASSERT_FALSE(r->has_error);@@ -137,9 +168,12 @@ TEST(java_class_extends_and_implements) {* "implements ..." literally inside one of the entries. */ASSERT_NULL(strstr(*b, "extends"));ASSERT_NULL(strstr(*b, "implements"));- if (strcmp(*b, "DefaultDiagramTool") == 0) saw_super = true;- if (strcmp(*b, "ILinkTool") == 0) saw_iface_a = true;- if (strcmp(*b, "Closeable") == 0) saw_iface_b = true;+ if (strcmp(*b, "DefaultDiagramTool") == 0)+ saw_super = true;+ if (strcmp(*b, "ILinkTool") == 0)+ saw_iface_a = true;+ if (strcmp(*b, "Closeable") == 0)+ saw_iface_b = true;}ASSERT_TRUE(saw_super);ASSERT_TRUE(saw_iface_a);@@ -800,8 +834,8 @@ TEST(swift_simple_call) {}TEST(swift_method_call) {- CBMFileResult *r = extract("class Foo {\n func bar() { baz.run() }\n}\n", CBM_LANG_SWIFT,- "t", "Foo.swift");+ CBMFileResult *r =+ extract("class Foo {\n func bar() { baz.run() }\n}\n", CBM_LANG_SWIFT, "t", "Foo.swift");ASSERT_NOT_NULL(r);ASSERT_FALSE(r->has_error);ASSERT(has_call(r, "baz.run"));@@ -1730,10 +1764,10 @@ TEST(go_imports) {}TEST(java_imports) {- CBMFileResult *r =- extract("import java.util.List;\nimport java.util.ArrayList;\nimport static java.lang.Math.PI;\n"- "public class Foo {}\n",- CBM_LANG_JAVA, "t", "Foo.java");+ CBMFileResult *r = extract(+ "import java.util.List;\nimport java.util.ArrayList;\nimport static java.lang.Math.PI;\n"+ "public class Foo {}\n",+ CBM_LANG_JAVA, "t", "Foo.java");ASSERT_NOT_NULL(r);ASSERT_FALSE(r->has_error);ASSERT_GT(r->imports.count, 0);@@ -1743,10 +1777,10 @@ TEST(java_imports) {}TEST(rust_imports) {- CBMFileResult *r =- extract("use std::collections::HashMap;\nuse std::io::{self, Write};\nuse serde::Serialize;\n"- "fn main() {}\n",- CBM_LANG_RUST, "t", "main.rs");+ CBMFileResult *r = extract(+ "use std::collections::HashMap;\nuse std::io::{self, Write};\nuse serde::Serialize;\n"+ "fn main() {}\n",+ CBM_LANG_RUST, "t", "main.rs");ASSERT_NOT_NULL(r);ASSERT_FALSE(r->has_error);ASSERT_GT(r->imports.count, 0);@@ -1756,9 +1790,9 @@ TEST(rust_imports) {}TEST(c_imports) {- CBMFileResult *r =- extract("#include <stdio.h>\n#include <stdlib.h>\n#include \"mylib.h\"\n\nint main() { return 0; }\n",- CBM_LANG_C, "t", "main.c");+ CBMFileResult *r = extract("#include <stdio.h>\n#include <stdlib.h>\n#include "+ "\"mylib.h\"\n\nint main() { return 0; }\n",+ CBM_LANG_C, "t", "main.c");ASSERT_NOT_NULL(r);ASSERT_FALSE(r->has_error);ASSERT_GT(r->imports.count, 0);@@ -1768,9 +1802,9 @@ TEST(c_imports) {}TEST(ruby_imports) {- CBMFileResult *r =- extract("require 'json'\nrequire 'net/http'\nrequire_relative 'helpers'\n\nclass Foo; end\n",- CBM_LANG_RUBY, "t", "app.rb");+ CBMFileResult *r = extract(+ "require 'json'\nrequire 'net/http'\nrequire_relative 'helpers'\n\nclass Foo; end\n",+ CBM_LANG_RUBY, "t", "app.rb");ASSERT_NOT_NULL(r);ASSERT_FALSE(r->has_error);ASSERT_GT(r->imports.count, 0);@@ -1780,9 +1814,9 @@ TEST(ruby_imports) {}TEST(lua_imports) {- CBMFileResult *r =- extract("local json = require(\"dkjson\")\nlocal http = require(\"socket.http\")\n\nlocal function greet() end\n",- CBM_LANG_LUA, "t", "main.lua");+ CBMFileResult *r = extract("local json = require(\"dkjson\")\nlocal http = "+ "require(\"socket.http\")\n\nlocal function greet() end\n",+ CBM_LANG_LUA, "t", "main.lua");ASSERT_NOT_NULL(r);ASSERT_FALSE(r->has_error);ASSERT_GT(r->imports.count, 0);@@ -1825,15 +1859,14 @@ TEST(import_stress_go) {TEST(svelte_imports_basic) {/* Default import + named imports + namespace import */- CBMFileResult *r = extract(- "<script>\n"- "import Foo from './Foo.svelte';\n"- "import { bar, baz } from '../lib/utils';\n"- "import * as helpers from './helpers';\n"- "export let value = 42;\n"- "</script>\n"- "<h1>Hello {value}</h1>\n",- CBM_LANG_SVELTE, "t", "Comp.svelte");+ CBMFileResult *r = extract("<script>\n"+ "import Foo from './Foo.svelte';\n"+ "import { bar, baz } from '../lib/utils';\n"+ "import * as helpers from './helpers';\n"+ "export let value = 42;\n"+ "</script>\n"+ "<h1>Hello {value}</h1>\n",+ CBM_LANG_SVELTE, "t", "Comp.svelte");ASSERT_NOT_NULL(r);ASSERT_FALSE(r->has_error);ASSERT_GTE(r->imports.count, 3);@@ -1846,10 +1879,9 @@ TEST(svelte_imports_basic) {TEST(svelte_imports_no_script) {/* .svelte with no <script> block must not crash, 0 imports */- CBMFileResult *r = extract(- "<h1>Static page</h1>\n"- "<p>No script here.</p>\n",- CBM_LANG_SVELTE, "t", "Static.svelte");+ CBMFileResult *r = extract("<h1>Static page</h1>\n"+ "<p>No script here.</p>\n",+ CBM_LANG_SVELTE, "t", "Static.svelte");ASSERT_NOT_NULL(r);ASSERT_FALSE(r->has_error);ASSERT_EQ(r->imports.count, 0);@@ -1859,14 +1891,13 @@ TEST(svelte_imports_no_script) {TEST(vue_imports_basic) {/* Vue SFC: same document→script_element→raw_text AST structure */- CBMFileResult *r = extract(- "<template><div>{{ msg }}</div></template>\n"- "<script>\n"- "import MyComp from './MyComp.vue';\n"- "import { ref } from 'vue';\n"- "export default { name: 'App' };\n"- "</script>\n",- CBM_LANG_VUE, "t", "App.vue");+ CBMFileResult *r = extract("<template><div>{{ msg }}</div></template>\n"+ "<script>\n"+ "import MyComp from './MyComp.vue';\n"+ "import { ref } from 'vue';\n"+ "export default { name: 'App' };\n"+ "</script>\n",+ CBM_LANG_VUE, "t", "App.vue");ASSERT_NOT_NULL(r);ASSERT_FALSE(r->has_error);ASSERT_GTE(r->imports.count, 2);@@ -1878,15 +1909,14 @@ TEST(vue_imports_basic) {TEST(html_imports_basic) {/* Plain HTML with inline ES module imports — same generic walker. */- CBMFileResult *r = extract(- "<!DOCTYPE html><html><head>\n"- "<script type=\"module\">\n"- "import { renderApp } from './app.js';\n"- "import * as utils from './utils.js';\n"- "renderApp();\n"- "</script>\n"- "</head><body></body></html>\n",- CBM_LANG_HTML, "t", "index.html");+ CBMFileResult *r = extract("<!DOCTYPE html><html><head>\n"+ "<script type=\"module\">\n"+ "import { renderApp } from './app.js';\n"+ "import * as utils from './utils.js';\n"+ "renderApp();\n"+ "</script>\n"+ "</head><body></body></html>\n",+ CBM_LANG_HTML, "t", "index.html");ASSERT_NOT_NULL(r);ASSERT_FALSE(r->has_error);ASSERT_GTE(r->imports.count, 2);@@ -2300,7 +2330,6 @@ TEST(extract_java_method_annotations_issue382) {PASS();}-/* Issue #213: large TS files were indexed as a File node with zero children. */TEST(extract_large_ts_has_functions_issue213) {enum { NFUNCS = 4000 };@@ -2309,9 +2338,9 @@ TEST(extract_large_ts_has_functions_issue213) {ASSERT_NOT_NULL(src);size_t off = 0;for (int i = 0; i < NFUNCS; i++) {- off += (size_t)snprintf(src + off, cap - off,- "export function fn%d(a: number): number { return a + %d; }\n", i,- i);+ off +=+ (size_t)snprintf(src + off, cap - off,+ "export function fn%d(a: number): number { return a + %d; }\n", i, i);}CBMFileResult *r =cbm_extract_file(src, (int)off, CBM_LANG_TYPESCRIPT, "t", "big.ts", 0, NULL, NULL);@@ -2323,7 +2352,6 @@ TEST(extract_large_ts_has_functions_issue213) {PASS();}-/* ═══════════════════════════════════════════════════════════════════* Suite* ═══════════════════════════════════════════════════════════════════ */@@ -2332,6 +2360,10 @@ SUITE(extraction) {/* Initialize extraction library */cbm_init();+ /* R box-module imports + member calls */+ RUN_TEST(extract_r_box_use_imports_issue218);+ RUN_TEST(extract_r_dollar_call_issue219);+/* OOP */RUN_TEST(java_class);RUN_TEST(java_method);