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(pipeline): resolve constants in template URLs

lorenzozanee21c224fba56f
diff --git a/internal/cbm/extract_calls.c b/internal/cbm/extract_calls.c
index f14efb766..1ec05de2a 100644
--- a/internal/cbm/extract_calls.c
+++ b/internal/cbm/extract_calls.c
@@ -2192,9 +2192,9 @@ static void extract_call_args(CBMExtractCtx *ctx, TSNode args, CBMCall *call) {
if (is_string_like(ak) && ca->expr) {
ca->value = strip_quotes(ctx->arena, ca->expr);
} else if (strcmp(ak, "template_string") == 0) {
- /* Flattened {} form so downstream url-arg detection joins the
- * canonical server route shape (issue #1006/#1009). */
- ca->value = cbm_template_string_text(ctx->arena, arg_node, ctx->source);
+ /* Resolve known constants; dynamic substitutions keep the route placeholder. */
+ ca->value = cbm_template_string_text(ctx->arena, arg_node, ctx->source,
+ &ctx->string_constants);
} else if (strcmp(ak, "identifier") == 0 && ca->expr) {
ca->value = lookup_string_constant(ctx, ca->expr);
} else if (strcmp(ak, "object") == 0) {
@@ -2259,7 +2259,8 @@ static bool is_queue_topic_field(const char *key) {
static const char *extract_string_value(CBMExtractCtx *ctx, TSNode val_node) {
const char *vk = ts_node_type(val_node);
if (strcmp(vk, "template_string") == 0) {
- return cbm_template_string_text(ctx->arena, val_node, ctx->source);
+ return cbm_template_string_text(ctx->arena, val_node, ctx->source,
+ &ctx->string_constants);
}
if (is_string_like(vk)) {
char *text = cbm_node_text(ctx->arena, val_node, ctx->source);
@@ -2434,10 +2435,10 @@ static TSNode swift_unwrap_url_constructor(CBMExtractCtx *ctx, TSNode arg) {
}
static const char *extract_positional_url(CBMExtractCtx *ctx, TSNode arg, const char *ak) {
- /* JS/TS template literals: `/things/${id}` normalizes to "/things/{}" so the
- * client URL joins the server route's canonical placeholder (issue #1006). */
+ /* Resolve constants while keeping dynamic route parts in canonical form. */
if (strcmp(ak, "template_string") == 0) {
- const char *flat = cbm_template_string_text(ctx->arena, arg, ctx->source);
+ const char *flat = cbm_template_string_text(ctx->arena, arg, ctx->source,
+ &ctx->string_constants);
if (flat) {
return strip_and_validate_string_arg(ctx->arena, (char *)flat);
}
diff --git a/internal/cbm/extract_unified.c b/internal/cbm/extract_unified.c
index 086f501a0..cce2100b7 100644
--- a/internal/cbm/extract_unified.c
+++ b/internal/cbm/extract_unified.c
@@ -1212,7 +1212,7 @@ static void handle_string_constants(CBMExtractCtx *ctx, TSNode node, const WalkS
const char *value_kind = ts_node_type(value_node);
const char *flat_value = NULL;
if (strcmp(value_kind, "template_string") == 0) {
- flat_value = cbm_template_string_text(ctx->arena, value_node, ctx->source);
+ flat_value = cbm_template_string_text(ctx->arena, value_node, ctx->source, NULL);
if (!flat_value) {
return;
}
@@ -1263,7 +1263,7 @@ static void handle_string_refs(CBMExtractCtx *ctx, TSNode node, const WalkState
* template strings become string_refs with the canonical placeholder shape
* shared with server route paths (issue #1006). */
if (strcmp(kind, "template_string") == 0) {
- const char *flat = cbm_template_string_text(ctx->arena, node, ctx->source);
+ const char *flat = cbm_template_string_text(ctx->arena, node, ctx->source, NULL);
if (!flat) {
return;
}
diff --git a/internal/cbm/helpers.c b/internal/cbm/helpers.c
index 4c4d340f0..86c54c4e9 100644
--- a/internal/cbm/helpers.c
+++ b/internal/cbm/helpers.c
@@ -1803,13 +1803,10 @@ int cbm_classify_string(const char *str, int len) {
return NOT_FOUND;
}
-/* Flatten a JS/TS `template_string` node into plain text (issue #1006).
- * String fragments are kept verbatim; each ${...} substitution becomes the
- * "{}" placeholder so client URLs built from template literals share the
- * canonical parameter shape of server-side route paths
- * (`/things/${id}/x` -> "/things/{}/x"). Returns NULL when the node yields
- * no text or exceeds the route-sized buffer. */
-const char *cbm_template_string_text(CBMArena *a, TSNode node, const char *source) {
+/* Flatten a JS/TS template string, resolving known string constants and
+ * replacing other substitutions with the route placeholder. */
+const char *cbm_template_string_text(CBMArena *a, TSNode node, const char *source,
+ const CBMStringConstantMap *string_constants) {
enum { TPL_BUF = 512 };
char buf[TPL_BUF];
size_t pos = 0;
@@ -1829,6 +1826,29 @@ const char *cbm_template_string_text(CBMArena *a, TSNode node, const char *sourc
memcpy(buf + pos, frag, fl);
pos += fl;
} else if (strcmp(k, "template_substitution") == 0) {
+ const char *resolved = NULL;
+ TSNode expr = ts_node_named_child(c, 0);
+ if (string_constants && !ts_node_is_null(expr) &&
+ strcmp(ts_node_type(expr), "identifier") == 0) {
+ char *name = cbm_node_text(a, expr, source);
+ for (int j = 0; name && j < string_constants->count; j++) {
+ if (!string_constants->is_url_builder[j] && string_constants->names[j] &&
+ string_constants->values[j] &&
+ strcmp(string_constants->names[j], name) == 0) {
+ resolved = string_constants->values[j];
+ break;
+ }
+ }
+ }
+ if (resolved) {
+ size_t rl = strlen(resolved);
+ if (pos + rl >= TPL_BUF) {
+ return NULL;
+ }
+ memcpy(buf + pos, resolved, rl);
+ pos += rl;
+ continue;
+ }
if (pos + PAIR_LEN >= TPL_BUF) {
return NULL;
}
diff --git a/internal/cbm/helpers.h b/internal/cbm/helpers.h
index 00e3d30dc..16c132d10 100644
--- a/internal/cbm/helpers.h
+++ b/internal/cbm/helpers.h
@@ -253,10 +253,9 @@ char *cbm_fqn_compute_source_lang(CBMArena *a, const char *project, const char *
// Folder QN: project.dir_parts
char *cbm_fqn_folder(CBMArena *a, const char *project, const char *rel_dir);
-/* Flatten a JS/TS `template_string` node into plain text: string fragments are
- * kept verbatim and each ${...} substitution becomes the "{}" placeholder, so
- * client-side URLs built from template literals share the canonical parameter
- * shape that server-side route paths already use. NULL when empty/oversized. */
-const char *cbm_template_string_text(CBMArena *a, TSNode node, const char *source);
+/* Flatten JS/TS template strings for URL extraction. Known string constants
+ * resolve; other substitutions become "{}". NULL when empty/oversized. */
+const char *cbm_template_string_text(CBMArena *a, TSNode node, const char *source,
+ const CBMStringConstantMap *string_constants);
#endif // CBM_HELPERS_H
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index ed9b0dd3c..7c8d61cbe 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -8158,6 +8158,40 @@ TEST(pipeline_native_fetch_classified_as_http_calls) {
PASS();
}
+TEST(pipeline_native_fetch_template_base_url_makes_route) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_fetch_template_base_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ FAIL("tmpdir");
+ }
+
+ write_temp_file(tmp, "src/api.ts",
+ "const BASE_URL = \"http://payments:8080\";\n"
+ "export async function createPayment(body: unknown) {\n"
+ " return fetch(`${BASE_URL}/v1/payments`, { method: 'POST', body });\n"
+ "}\n");
+
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/fetch_template_base.db", tmp);
+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p);
+ ASSERT_EQ(cbm_pipeline_run(p), 0);
+ const char *project = cbm_pipeline_project_name(p);
+
+ cbm_store_t *s = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(s);
+ int http_call_count = cbm_store_count_edges_by_type(s, project, "HTTP_CALLS");
+ int matching_route_count =
+ http_calls_to_route(s, project, "createPayment", "/v1/payments");
+
+ cbm_store_close(s);
+ cbm_pipeline_free(p);
+ th_rmtree(tmp);
+ ASSERT_EQ(http_call_count, 1);
+ ASSERT_EQ(matching_route_count, 1);
+ PASS();
+}
+
/* #1892: Swift produced no Route node and no HTTP_CALLS edge, because the
* Swift grammar has no "arguments" field and the generic lookup therefore read
* no call arguments at all. Alamofire/URLSession were already in the service
@@ -17671,6 +17705,7 @@ SUITE(pipeline) {
RUN_TEST(pipeline_ts_config_object_url_http_calls_issue2235);
RUN_TEST(pipeline_arg_url_rejects_document_file_paths);
RUN_TEST(pipeline_native_fetch_classified_as_http_calls);
+ RUN_TEST(pipeline_native_fetch_template_base_url_makes_route);
RUN_TEST(pipeline_swift_nested_url_makes_route_issue1892);
RUN_TEST(pipeline_swift_http_call_makes_route_issue1892);
RUN_TEST(pipeline_native_fetch_parallel_classified_as_http_calls);