Map-aware template flatten for builder bodies: a ${...} substitution that is * a bare identifier or a call to an already-recorded name (const or an earlier * builder in the same file) inlines that value; anything else becomes "{}". * The query string is not part of a route's identity, so the result is * truncated at the first '?'. Handles the composed-builder shape * `return \`${basePath(id)}
| 1346 | * truncated at the first '?'. Handles the composed-builder shape |
| 1347 | * `return \`${basePath(id)}?${params}\``. */ |
| 1348 | static const char *builder_template_text(CBMExtractCtx *ctx, TSNode node) { |
| 1349 | enum { BLD_BUF = 512 }; |
| 1350 | char buf[BLD_BUF]; |
| 1351 | size_t pos = 0; |
| 1352 | uint32_t nc = ts_node_named_child_count(node); |
| 1353 | for (uint32_t i = 0; i < nc; i++) { |
| 1354 | TSNode c = ts_node_named_child(node, i); |
| 1355 | const char *k = ts_node_type(c); |
| 1356 | const char *piece = NULL; |
| 1357 | if (strcmp(k, "string_fragment") == 0) { |
| 1358 | piece = cbm_node_text(ctx->arena, c, ctx->source); |
| 1359 | } else if (strcmp(k, "template_substitution") == 0) { |
| 1360 | piece = "{}"; |
| 1361 | if (ts_node_named_child_count(c) > 0) { |
| 1362 | TSNode expr = ts_node_named_child(c, 0); |
| 1363 | /* `${basePath(id)}` inlines a builder, `${BASE}` a const. A bare |
| 1364 | * name never resolves to a builder: that reads the function. */ |
| 1365 | bool want_builder = strcmp(ts_node_type(expr), "call_expression") == 0; |
| 1366 | TSNode name_node = |
| 1367 | want_builder ? ts_node_child_by_field_name(expr, TS_FIELD("function")) : expr; |
| 1368 | if (!ts_node_is_null(name_node) && |
| 1369 | strcmp(ts_node_type(name_node), "identifier") == 0) { |
| 1370 | char *nm = cbm_node_text(ctx->arena, name_node, ctx->source); |
| 1371 | if (nm) { |
| 1372 | const CBMStringConstantMap *map = &ctx->string_constants; |
| 1373 | for (int mi = 0; mi < map->count; mi++) { |
| 1374 | if (map->values[mi] && map->is_url_builder[mi] == want_builder && |
| 1375 | strcmp(map->names[mi], nm) == 0) { |
| 1376 | piece = map->values[mi]; |
| 1377 | break; |
| 1378 | } |
| 1379 | } |
| 1380 | } |
| 1381 | } |
| 1382 | } |
| 1383 | } else { |
| 1384 | continue; |
| 1385 | } |
| 1386 | if (!piece) { |
| 1387 | continue; |
| 1388 | } |
| 1389 | size_t pl = strlen(piece); |
| 1390 | if (pos + pl >= BLD_BUF) { |
| 1391 | return NULL; |
| 1392 | } |
| 1393 | memcpy(buf + pos, piece, pl); |
| 1394 | pos += pl; |
| 1395 | } |
| 1396 | /* Route identity excludes the query string. */ |
| 1397 | for (size_t qi = 0; qi < pos; qi++) { |
| 1398 | if (buf[qi] == '?') { |
| 1399 | pos = qi; |
| 1400 | break; |
| 1401 | } |
| 1402 | } |
| 1403 | if (pos == 0) { |
| 1404 | return NULL; |
| 1405 | } |
no test coverage detected