Skip to content

Commit facd32b

Browse files
authored
Merge pull request DeusData#1008 from CharlesQueiroz/fix/template-string-urls
fix: flatten JS/TS template-literal URLs to {} placeholders
2 parents c6f564e + a029b3a commit facd32b

5 files changed

Lines changed: 122 additions & 5 deletions

File tree

‎internal/cbm/extract_calls.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,9 @@ static bool is_queue_topic_field(const char *key) {
16061606
// Extract string value from a node (literal or constant reference).
16071607
static const char *extract_string_value(CBMExtractCtx *ctx, TSNode val_node) {
16081608
const char *vk = ts_node_type(val_node);
1609+
if (strcmp(vk, "template_string") == 0) {
1610+
return cbm_template_string_text(ctx->arena, val_node, ctx->source);
1611+
}
16091612
if (is_string_like(vk)) {
16101613
char *text = cbm_node_text(ctx->arena, val_node, ctx->source);
16111614
if (text && text[0]) {
@@ -1706,6 +1709,14 @@ static const char *extract_keyword_url(CBMExtractCtx *ctx, TSNode arg) {
17061709

17071710
// Try to extract URL/topic from a positional argument (string or constant).
17081711
static const char *extract_positional_url(CBMExtractCtx *ctx, TSNode arg, const char *ak) {
1712+
/* JS/TS template literals: `/things/${id}` normalizes to "/things/{}" so the
1713+
* client URL joins the server route's canonical placeholder (issue #1006). */
1714+
if (strcmp(ak, "template_string") == 0) {
1715+
const char *flat = cbm_template_string_text(ctx->arena, arg, ctx->source);
1716+
if (flat) {
1717+
return strip_and_validate_string_arg(ctx->arena, (char *)flat);
1718+
}
1719+
}
17091720
if (is_string_like(ak)) {
17101721
char *text = cbm_node_text(ctx->arena, arg, ctx->source);
17111722
const char *validated = strip_and_validate_string_arg(ctx->arena, text);

‎internal/cbm/extract_unified.c‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -746,20 +746,28 @@ static void handle_string_constants(CBMExtractCtx *ctx, TSNode node, const WalkS
746746
return;
747747
}
748748

749-
/* Value must be a string literal */
750-
if (!is_string_node(ts_node_type(value_node))) {
749+
/* Value must be a string literal (template literals flatten to "{}" form) */
750+
const char *value_kind = ts_node_type(value_node);
751+
const char *flat_value = NULL;
752+
if (strcmp(value_kind, "template_string") == 0) {
753+
flat_value = cbm_template_string_text(ctx->arena, value_node, ctx->source);
754+
if (!flat_value) {
755+
return;
756+
}
757+
} else if (!is_string_node(value_kind)) {
751758
return;
752759
}
753760

754761
char *name = cbm_node_text(ctx->arena, name_node, ctx->source);
755-
char *value = cbm_node_text(ctx->arena, value_node, ctx->source);
762+
char *value =
763+
flat_value ? (char *)flat_value : cbm_node_text(ctx->arena, value_node, ctx->source);
756764
if (!name || !name[0] || !value || !value[0]) {
757765
return;
758766
}
759767

760-
/* Strip quotes from value */
768+
/* Strip quotes from value (template values are already unquoted) */
761769
int vlen = (int)strlen(value);
762-
if (vlen >= CBM_QUOTE_PAIR && (value[0] == '"' || value[0] == '\'')) {
770+
if (!flat_value && vlen >= CBM_QUOTE_PAIR && (value[0] == '"' || value[0] == '\'')) {
763771
value = cbm_arena_strndup(ctx->arena, value + SKIP_ONE, (size_t)(vlen - PAIR_LEN));
764772
if (!value) {
765773
return;
@@ -789,6 +797,27 @@ static bool is_string_node(const char *kind) {
789797

790798
static void handle_string_refs(CBMExtractCtx *ctx, TSNode node, const WalkState *state) {
791799
const char *kind = ts_node_type(node);
800+
/* JS/TS template literals: flatten ${...} substitutions to "{}" so URL-ish
801+
* template strings become string_refs with the canonical placeholder shape
802+
* shared with server route paths (issue #1006). */
803+
if (strcmp(kind, "template_string") == 0) {
804+
const char *flat = cbm_template_string_text(ctx->arena, node, ctx->source);
805+
if (!flat) {
806+
return;
807+
}
808+
int kind_val = cbm_classify_string(flat, (int)strlen(flat));
809+
if (kind_val < 0) {
810+
return;
811+
}
812+
CBMStringRef ref = {
813+
.value = flat,
814+
.enclosing_func_qn =
815+
state->enclosing_func_qn ? state->enclosing_func_qn : ctx->module_qn,
816+
.kind = (CBMStringRefKind)kind_val,
817+
};
818+
cbm_stringref_push(&ctx->result->string_refs, ctx->arena, ref);
819+
return;
820+
}
792821
if (!is_string_node(kind)) {
793822
return;
794823
}

‎internal/cbm/helpers.c‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,3 +1443,42 @@ int cbm_classify_string(const char *str, int len) {
14431443

14441444
return NOT_FOUND;
14451445
}
1446+
1447+
/* Flatten a JS/TS `template_string` node into plain text (issue #1006).
1448+
* String fragments are kept verbatim; each ${...} substitution becomes the
1449+
* "{}" placeholder so client URLs built from template literals share the
1450+
* canonical parameter shape of server-side route paths
1451+
* (`/things/${id}/x` -> "/things/{}/x"). Returns NULL when the node yields
1452+
* no text or exceeds the route-sized buffer. */
1453+
const char *cbm_template_string_text(CBMArena *a, TSNode node, const char *source) {
1454+
enum { TPL_BUF = 512 };
1455+
char buf[TPL_BUF];
1456+
size_t pos = 0;
1457+
uint32_t nc = ts_node_named_child_count(node);
1458+
for (uint32_t i = 0; i < nc; i++) {
1459+
TSNode c = ts_node_named_child(node, i);
1460+
const char *k = ts_node_type(c);
1461+
if (strcmp(k, "string_fragment") == 0) {
1462+
char *frag = cbm_node_text(a, c, source);
1463+
if (!frag) {
1464+
continue;
1465+
}
1466+
size_t fl = strlen(frag);
1467+
if (pos + fl >= TPL_BUF) {
1468+
return NULL;
1469+
}
1470+
memcpy(buf + pos, frag, fl);
1471+
pos += fl;
1472+
} else if (strcmp(k, "template_substitution") == 0) {
1473+
if (pos + PAIR_LEN >= TPL_BUF) {
1474+
return NULL;
1475+
}
1476+
buf[pos++] = '{';
1477+
buf[pos++] = '}';
1478+
}
1479+
}
1480+
if (pos == 0) {
1481+
return NULL;
1482+
}
1483+
return cbm_arena_strndup(a, buf, pos);
1484+
}

‎internal/cbm/helpers.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,10 @@ char *cbm_fqn_compute_source_lang(CBMArena *a, const char *project, const char *
149149
// Folder QN: project.dir_parts
150150
char *cbm_fqn_folder(CBMArena *a, const char *project, const char *rel_dir);
151151

152+
/* Flatten a JS/TS `template_string` node into plain text: string fragments are
153+
* kept verbatim and each ${...} substitution becomes the "{}" placeholder, so
154+
* client-side URLs built from template literals share the canonical parameter
155+
* shape that server-side route paths already use. NULL when empty/oversized. */
156+
const char *cbm_template_string_text(CBMArena *a, TSNode node, const char *source);
157+
152158
#endif // CBM_HELPERS_H

‎tests/test_extraction.c‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2999,6 +2999,37 @@ static const CBMCall *find_call_by_callee(CBMFileResult *r, const char *callee)
29992999
return NULL;
30003000
}
30013001

3002+
/* Issue #1006: JS/TS template-literal URLs must flatten ${...} substitutions
3003+
* to the canonical "{}" placeholder, both as call arguments (HTTP_CALLS) and
3004+
* as URL-shaped string_refs collected from const/return positions. */
3005+
TEST(extract_ts_template_string_url_issue1006) {
3006+
CBMFileResult *r = extract("export function detailPath(id: string): string {\n"
3007+
" return `/api/v1/things/${id}/detail`;\n"
3008+
"}\n"
3009+
"export function load(id: string) {\n"
3010+
" return fetch(`/api/v1/things/${id}`);\n"
3011+
"}\n",
3012+
CBM_LANG_TYPESCRIPT, "t", "paths.ts");
3013+
ASSERT_NOT_NULL(r);
3014+
ASSERT_FALSE(r->has_error);
3015+
const CBMCall *c = find_call_by_callee(r, "fetch");
3016+
ASSERT_NOT_NULL(c);
3017+
ASSERT_NOT_NULL(c->first_string_arg);
3018+
ASSERT_STR_EQ(c->first_string_arg, "/api/v1/things/{}");
3019+
int found = 0;
3020+
for (int i = 0; i < r->string_refs.count; i++) {
3021+
if (r->string_refs.items[i].value &&
3022+
strcmp(r->string_refs.items[i].value, "/api/v1/things/{}/detail") == 0) {
3023+
found = 1;
3024+
break;
3025+
}
3026+
}
3027+
ASSERT(found);
3028+
cbm_free_result(r);
3029+
PASS();
3030+
}
3031+
3032+
30023033
/* Reproduce-first: Java module QN must derive from the CONTAINING DIRECTORY, not
30033034
* the filename stem, so a top-level class `Outer` in `Outer.java` is `t.Outer`,
30043035
* NOT the doubled `t.Outer.Outer`. The nested method def QN must also equal the
@@ -5124,6 +5155,7 @@ SUITE(extraction) {
51245155
RUN_TEST(python_regular_module_qn_unchanged);
51255156
RUN_TEST(extract_java_method_annotations_issue382);
51265157
RUN_TEST(extract_java_jaxrs_path_composition_issue1005);
5158+
RUN_TEST(extract_ts_template_string_url_issue1006);
51275159
RUN_TEST(extract_java_no_double_class_qn);
51285160
RUN_TEST(extract_go_no_filename_in_module_qn);
51295161
RUN_TEST(extract_large_ts_has_functions_issue213);

0 commit comments

Comments
 (0)