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. */
| 2329 | * (`/things/${id}/x` -> "/things/{}/x"). Returns NULL when the node yields |
| 2330 | * no text or exceeds the route-sized buffer. */ |
| 2331 | const char *cbm_template_string_text(CBMArena *a, TSNode node, const char *source) { |
| 2332 | enum { TPL_BUF = 512 }; |
| 2333 | char buf[TPL_BUF]; |
| 2334 | size_t pos = 0; |
| 2335 | uint32_t nc = ts_node_named_child_count(node); |
| 2336 | for (uint32_t i = 0; i < nc; i++) { |
| 2337 | TSNode c = ts_node_named_child(node, i); |
| 2338 | const char *k = ts_node_type(c); |
| 2339 | if (strcmp(k, "string_fragment") == 0) { |
| 2340 | char *frag = cbm_node_text(a, c, source); |
| 2341 | if (!frag) { |
| 2342 | continue; |
| 2343 | } |
| 2344 | size_t fl = strlen(frag); |
| 2345 | if (pos + fl >= TPL_BUF) { |
| 2346 | return NULL; |
| 2347 | } |
| 2348 | memcpy(buf + pos, frag, fl); |
| 2349 | pos += fl; |
| 2350 | } else if (strcmp(k, "template_substitution") == 0) { |
| 2351 | if (pos + PAIR_LEN >= TPL_BUF) { |
| 2352 | return NULL; |
| 2353 | } |
| 2354 | buf[pos++] = '{'; |
| 2355 | buf[pos++] = '}'; |
| 2356 | } |
| 2357 | } |
| 2358 | if (pos == 0) { |
| 2359 | return NULL; |
| 2360 | } |
| 2361 | return cbm_arena_strndup(a, buf, pos); |
| 2362 | } |
no test coverage detected