Skip to content

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.

fix: gate URL-builder resolution to JS/TS and to call arguments

Charles Queiroz606052a2fc91
diff --git a/internal/cbm/cbm.h b/internal/cbm/cbm.h
index aebd12291..946fac2e7 100644
--- a/internal/cbm/cbm.h
+++ b/internal/cbm/cbm.h
@@ -551,6 +551,7 @@ typedef struct {
typedef struct {
const char *names[CBM_MAX_STRING_CONSTANTS];
const char *values[CBM_MAX_STRING_CONSTANTS];
+ bool is_url_builder[CBM_MAX_STRING_CONSTANTS];
int count;
} CBMStringConstantMap;
diff --git a/internal/cbm/extract_calls.c b/internal/cbm/extract_calls.c
index 9d83ed0b6..49cb4209b 100644
--- a/internal/cbm/extract_calls.c
+++ b/internal/cbm/extract_calls.c
@@ -30,14 +30,29 @@ enum { MIN_PRINTABLE = 0x20 };
/* Handler arg scan start index (skip first positional). */
enum { HANDLER_START_IDX = 1 };
-/* Look up a module-level string constant by name. */
+/* Look up a module-level string constant by name. URL-builder entries share the
+ * map but are not constants: a bare `thingPath` reference is the function, not
+ * the URL it would build (issue #1009). */
static const char *lookup_string_constant(const CBMExtractCtx *ctx, const char *name) {
if (!name || !name[0]) {
return NULL;
}
const CBMStringConstantMap *map = &ctx->string_constants;
for (int i = 0; i < map->count; i++) {
- if (strcmp(map->names[i], name) == 0) {
+ if (!map->is_url_builder[i] && strcmp(map->names[i], name) == 0) {
+ return map->values[i];
+ }
+ }
+ return NULL;
+}
+
+static const char *lookup_url_builder(const CBMExtractCtx *ctx, const char *name) {
+ if (!name || !name[0]) {
+ return NULL;
+ }
+ const CBMStringConstantMap *map = &ctx->string_constants;
+ for (int i = 0; i < map->count; i++) {
+ if (map->is_url_builder[i] && strcmp(map->names[i], name) == 0) {
return map->values[i];
}
}
@@ -1868,7 +1883,7 @@ static void extract_call_args(CBMExtractCtx *ctx, TSNode args, CBMCall *call) {
if (!ts_node_is_null(fn) && strcmp(ts_node_type(fn), "identifier") == 0) {
char *fname = cbm_node_text(ctx->arena, fn, ctx->source);
if (fname) {
- ca->value = lookup_string_constant(ctx, fname);
+ ca->value = lookup_url_builder(ctx, fname);
}
}
}
@@ -2105,7 +2120,7 @@ static const char *extract_url_or_topic_arg(CBMExtractCtx *ctx, TSNode args) {
TSNode fn = ts_node_child_by_field_name(arg, TS_FIELD("function"));
if (!ts_node_is_null(fn) && strcmp(ts_node_type(fn), "identifier") == 0) {
char *fname = cbm_node_text(ctx->arena, fn, ctx->source);
- const char *val = fname ? lookup_string_constant(ctx, fname) : NULL;
+ const char *val = fname ? lookup_url_builder(ctx, fname) : NULL;
if (val) {
return val;
}
diff --git a/internal/cbm/extract_unified.c b/internal/cbm/extract_unified.c
index 50bc96b69..7a6306870 100644
--- a/internal/cbm/extract_unified.c
+++ b/internal/cbm/extract_unified.c
@@ -1122,18 +1122,19 @@ static const char *builder_template_text(CBMExtractCtx *ctx, TSNode node) {
piece = "{}";
if (ts_node_named_child_count(c) > 0) {
TSNode expr = ts_node_named_child(c, 0);
- const char *ek = ts_node_type(expr);
- TSNode name_node = expr;
- if (strcmp(ek, "call_expression") == 0) {
- name_node = ts_node_child_by_field_name(expr, TS_FIELD("function"));
- }
+ /* `${basePath(id)}` inlines a builder, `${BASE}` a const. A bare
+ * name never resolves to a builder: that reads the function. */
+ bool want_builder = strcmp(ts_node_type(expr), "call_expression") == 0;
+ TSNode name_node =
+ want_builder ? ts_node_child_by_field_name(expr, TS_FIELD("function")) : expr;
if (!ts_node_is_null(name_node) &&
strcmp(ts_node_type(name_node), "identifier") == 0) {
char *nm = cbm_node_text(ctx->arena, name_node, ctx->source);
if (nm) {
const CBMStringConstantMap *map = &ctx->string_constants;
for (int mi = 0; mi < map->count; mi++) {
- if (map->values[mi] && strcmp(map->names[mi], nm) == 0) {
+ if (map->values[mi] && map->is_url_builder[mi] == want_builder &&
+ strcmp(map->names[mi], nm) == 0) {
piece = map->values[mi];
break;
}
@@ -1187,9 +1188,15 @@ static const char *url_builder_literal_text(CBMExtractCtx *ctx, TSNode value_nod
return text;
}
-/* Record `name -> url` in the per-file constant map so call-site resolution
- * (lookup_string_constant) can see it. A builder that returns two DIFFERENT
- * urls is ambiguous: tombstone it with a NULL value so lookups miss. */
+/* A route-shaped URL literal: an absolute path that classifies as a URL. */
+static const char *builder_route_url(CBMExtractCtx *ctx, TSNode value_node) {
+ const char *url = url_builder_literal_text(ctx, value_node);
+ if (!url || url[0] != '/' || cbm_classify_string(url, (int)strlen(url)) != CBM_STRREF_URL) {
+ return NULL;
+ }
+ return url;
+}
+
static void record_url_builder(CBMExtractCtx *ctx, const char *name, const char *url) {
if (!name || !name[0] || !url) {
return;
@@ -1197,7 +1204,7 @@ static void record_url_builder(CBMExtractCtx *ctx, const char *name, const char
CBMStringConstantMap *map = &ctx->string_constants;
for (int i = 0; i < map->count; i++) {
if (strcmp(map->names[i], name) == 0) {
- if (map->values[i] && strcmp(map->values[i], url) != 0) {
+ if (map->is_url_builder[i] && map->values[i] && strcmp(map->values[i], url) != 0) {
map->values[i] = NULL; /* ambiguous builder */
}
return;
@@ -1206,17 +1213,70 @@ static void record_url_builder(CBMExtractCtx *ctx, const char *name, const char
if (map->count < CBM_MAX_STRING_CONSTANTS) {
map->names[map->count] = (char *)name;
map->values[map->count] = (char *)url;
+ map->is_url_builder[map->count] = true;
map->count++;
}
}
+/* Every `return` this function owns yields a route-shaped URL literal. A body
+ * that also returns a computed path (`return computePath(kind)`) would attribute
+ * its one literal to call sites that never produce it, so a mixed builder is
+ * declined rather than guessed. Returns inside a nested function belong to that
+ * function, not to this one. */
+static bool builder_returns_only_urls(CBMExtractCtx *ctx, TSNode func) {
+ TSNode body = ts_node_child_by_field_name(func, TS_FIELD("body"));
+ if (ts_node_is_null(body)) {
+ return false;
+ }
+ bool only_urls = true;
+ TSTreeCursor cursor = ts_tree_cursor_new(body);
+ for (;;) {
+ TSNode node = ts_tree_cursor_current_node(&cursor);
+ if (strcmp(ts_node_type(node), "return_statement") == 0 &&
+ ts_node_eq(cbm_find_enclosing_func(node, ctx->language), func)) {
+ if (ts_node_named_child_count(node) == 0 ||
+ !builder_route_url(ctx, ts_node_named_child(node, 0))) {
+ only_urls = false;
+ break;
+ }
+ }
+ if (ts_tree_cursor_goto_first_child(&cursor)) {
+ continue;
+ }
+ if (ts_tree_cursor_goto_next_sibling(&cursor)) {
+ continue;
+ }
+ bool found = false;
+ while (ts_tree_cursor_goto_parent(&cursor)) {
+ if (ts_tree_cursor_goto_next_sibling(&cursor)) {
+ found = true;
+ break;
+ }
+ }
+ if (!found) {
+ break;
+ }
+ }
+ ts_tree_cursor_delete(&cursor);
+ return only_urls;
+}
+
/* URL-builder helper pattern (issue #1009): a small function whose return value
* is a URL-shaped literal, consumed as `client(buildPath(id))`. The literal
* never appears as a call argument, so first_string_arg resolution cannot see
- * it; record `functionName -> url` in the same per-file constant map used for
- * module-level consts and let the call-site call_expression branch resolve it.
- * Covers `return`-statement bodies and arrow-function expression bodies. */
+ * it; record `functionName -> url` in the per-file constant map and let the
+ * call-site call_expression branch resolve it. Covers `return`-statement bodies
+ * and arrow-function expression bodies.
+ *
+ * JS/TS only. The predicate accepts any absolute pathname, and `return` plus a
+ * string literal is also the shape of every C or Go helper handing back a
+ * filesystem path (`/etc/...`, `/proc/self/...`); recording those would mint a
+ * Route node and an HTTP_CALLS edge in a language that speaks no HTTP. */
static void handle_url_builders(CBMExtractCtx *ctx, TSNode node, const WalkState *state) {
+ if (ctx->language != CBM_LANG_JAVASCRIPT && ctx->language != CBM_LANG_TYPESCRIPT &&
+ ctx->language != CBM_LANG_TSX) {
+ return;
+ }
const char *kind = ts_node_type(node);
if (strcmp(kind, "arrow_function") == 0) {
@@ -1224,8 +1284,8 @@ static void handle_url_builders(CBMExtractCtx *ctx, TSNode node, const WalkState
if (ts_node_is_null(body) || strcmp(ts_node_type(body), "statement_block") == 0) {
return;
}
- const char *url = url_builder_literal_text(ctx, body);
- if (!url || url[0] != '/' || cbm_classify_string(url, (int)strlen(url)) != CBM_STRREF_URL) {
+ const char *url = builder_route_url(ctx, body);
+ if (!url) {
return;
}
TSNode parent = ts_node_parent(node);
@@ -1247,8 +1307,12 @@ static void handle_url_builders(CBMExtractCtx *ctx, TSNode node, const WalkState
if (ts_node_named_child_count(node) == 0) {
return;
}
- const char *url = url_builder_literal_text(ctx, ts_node_named_child(node, 0));
- if (!url || url[0] != '/' || cbm_classify_string(url, (int)strlen(url)) != CBM_STRREF_URL) {
+ const char *url = builder_route_url(ctx, ts_node_named_child(node, 0));
+ if (!url) {
+ return;
+ }
+ TSNode func = cbm_find_enclosing_func(node, ctx->language);
+ if (ts_node_is_null(func) || !builder_returns_only_urls(ctx, func)) {
return;
}
const char *name = strrchr(state->enclosing_func_qn, '.');
diff --git a/tests/test_extraction.c b/tests/test_extraction.c
index 67c2734ca..e9f44e113 100644
--- a/tests/test_extraction.c
+++ b/tests/test_extraction.c
@@ -3427,6 +3427,112 @@ TEST(extract_ts_url_builder_composed_issue1009) {
PASS();
}
+static bool any_call_arg_resolves_to(CBMFileResult *r, const char *url) {
+ for (int i = 0; i < r->calls.count; i++) {
+ const CBMCall *c = &r->calls.items[i];
+ if (c->first_string_arg && strcmp(c->first_string_arg, url) == 0) {
+ return true;
+ }
+ for (int a = 0; a < c->arg_count; a++) {
+ if (c->args[a].value && strcmp(c->args[a].value, url) == 0) {
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+TEST(extract_c_url_builder_gated_issue1009) {
+ CBMFileResult *r = extract("static const char *cfg_path(void) {\n"
+ " return \"/etc/myapp/conf.d\";\n"
+ "}\n"
+ "void init(void) {\n"
+ " parse_config(cfg_path());\n"
+ "}\n",
+ CBM_LANG_C, "t", "conf.c");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+ ASSERT_FALSE(any_call_arg_resolves_to(r, "/etc/myapp/conf.d"));
+ cbm_free_result(r);
+ PASS();
+}
+
+/* One literal return next to a computed one would attribute the literal to every
+ * call site, including those taking the computed branch. A builder whose returns
+ * are not all URL literals is declined. */
+TEST(extract_ts_url_builder_mixed_returns_issue1009) {
+ CBMFileResult *r = extract("function pathFor(kind: string): string {\n"
+ " if (kind === 'user') return '/api/users';\n"
+ " return computePath(kind);\n"
+ "}\n"
+ "export function load(kind: string) {\n"
+ " return apiGet(pathFor(kind));\n"
+ "}\n",
+ CBM_LANG_TYPESCRIPT, "t", "mixed.ts");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+ ASSERT_FALSE(any_call_arg_resolves_to(r, "/api/users"));
+ cbm_free_result(r);
+ PASS();
+}
+
+/* Two different literal URLs make the call site's route unknowable, so the
+ * builder is tombstoned and lookups miss. */
+TEST(extract_ts_url_builder_ambiguous_issue1009) {
+ CBMFileResult *r = extract("function pathFor(kind: string): string {\n"
+ " if (kind === 'user') return '/api/users';\n"
+ " return '/api/teams';\n"
+ "}\n"
+ "export function load(kind: string) {\n"
+ " return apiGet(pathFor(kind));\n"
+ "}\n",
+ CBM_LANG_TYPESCRIPT, "t", "ambiguous.ts");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+ ASSERT_FALSE(any_call_arg_resolves_to(r, "/api/users"));
+ ASSERT_FALSE(any_call_arg_resolves_to(r, "/api/teams"));
+ cbm_free_result(r);
+ PASS();
+}
+
+/* Handing a builder to a callback position builds no URL. Only a call to it
+ * resolves, so the mapping function does not inherit an HTTP_CALLS edge to the
+ * route. */
+TEST(extract_ts_url_builder_reference_issue1009) {
+ CBMFileResult *r = extract("function thingPath(id: string): string {\n"
+ " return `/api/v1/things/${id}`;\n"
+ "}\n"
+ "export function loadAll(ids: string[]) {\n"
+ " return ids.map(thingPath);\n"
+ "}\n",
+ CBM_LANG_TYPESCRIPT, "t", "reference.ts");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+ ASSERT_FALSE(any_call_arg_resolves_to(r, "/api/v1/things/{}"));
+ cbm_free_result(r);
+ PASS();
+}
+
+/* A helper returning an ordinary string is not a URL builder, so its call site
+ * gets no resolved string argument. */
+TEST(extract_ts_url_builder_non_url_issue1009) {
+ CBMFileResult *r = extract("function label(): string {\n"
+ " return 'plain text';\n"
+ "}\n"
+ "export function render() {\n"
+ " return send(label());\n"
+ "}\n",
+ CBM_LANG_TYPESCRIPT, "t", "label.ts");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+ const CBMCall *c = find_call_by_callee(r, "send");
+ ASSERT_NOT_NULL(c);
+ ASSERT_NULL(c->first_string_arg);
+ ASSERT_FALSE(any_call_arg_resolves_to(r, "plain text"));
+ cbm_free_result(r);
+ PASS();
+}
+
/* Issue #1006: JS/TS template-literal URLs must flatten ${...} substitutions
* to the canonical "{}" placeholder, both as call arguments (HTTP_CALLS) and
* as URL-shaped string_refs collected from const/return positions. */
@@ -5845,6 +5951,11 @@ SUITE(extraction) {
RUN_TEST(extract_go_binary_concat_url_no_literal_suffix_issue1249);
RUN_TEST(extract_ts_url_builder_issue1009);
RUN_TEST(extract_ts_url_builder_composed_issue1009);
+ RUN_TEST(extract_c_url_builder_gated_issue1009);
+ RUN_TEST(extract_ts_url_builder_mixed_returns_issue1009);
+ RUN_TEST(extract_ts_url_builder_ambiguous_issue1009);
+ RUN_TEST(extract_ts_url_builder_reference_issue1009);
+ RUN_TEST(extract_ts_url_builder_non_url_issue1009);
RUN_TEST(extract_java_no_double_class_qn);
RUN_TEST(extract_go_no_filename_in_module_qn);
RUN_TEST(extract_large_ts_has_functions_issue213);