| 1178 | // --- Module-level constant collection --- |
| 1179 | |
| 1180 | static void handle_string_constants(CBMExtractCtx *ctx, TSNode node, const WalkState *state) { |
| 1181 | /* Only collect at module level (not inside functions/classes) */ |
| 1182 | if (state->enclosing_func_qn != NULL && state->enclosing_func_qn != ctx->module_qn) { |
| 1183 | return; |
| 1184 | } |
| 1185 | |
| 1186 | const char *kind = ts_node_type(node); |
| 1187 | |
| 1188 | /* Python: expression_statement → assignment → identifier = string */ |
| 1189 | /* Go: short_var_declaration, const_spec */ |
| 1190 | /* JS/TS: variable_declarator, lexical_declaration */ |
| 1191 | if (strcmp(kind, "assignment") != 0 && strcmp(kind, "expression_statement") != 0 && |
| 1192 | strcmp(kind, "short_var_declaration") != 0 && strcmp(kind, "const_spec") != 0 && |
| 1193 | strcmp(kind, "variable_declarator") != 0) { |
| 1194 | return; |
| 1195 | } |
| 1196 | |
| 1197 | /* Find name (left side) and value (right side) */ |
| 1198 | TSNode name_node = ts_node_child_by_field_name(node, TS_FIELD("left")); |
| 1199 | TSNode value_node = ts_node_child_by_field_name(node, TS_FIELD("right")); |
| 1200 | |
| 1201 | /* Some grammars use "name" + "value" fields */ |
| 1202 | if (ts_node_is_null(name_node)) { |
| 1203 | name_node = ts_node_child_by_field_name(node, TS_FIELD("name")); |
| 1204 | } |
| 1205 | if (ts_node_is_null(value_node)) { |
| 1206 | value_node = ts_node_child_by_field_name(node, TS_FIELD("value")); |
| 1207 | } |
| 1208 | |
| 1209 | if (ts_node_is_null(name_node) || ts_node_is_null(value_node)) { |
| 1210 | return; |
| 1211 | } |
| 1212 | |
| 1213 | /* Name must be an identifier */ |
| 1214 | const char *name_kind = ts_node_type(name_node); |
| 1215 | if (strcmp(name_kind, "identifier") != 0 && strcmp(name_kind, "constant") != 0) { |
| 1216 | return; |
| 1217 | } |
| 1218 | |
| 1219 | /* Python and JS/TS fold the value: `BASE + "/path"`, `${BASE}/path`, |
| 1220 | * f"{BASE}/path" and object-literal endpoint maps (#706, #1147). */ |
| 1221 | if (cbm_url_fold_lang(ctx->language)) { |
| 1222 | cbm_url_fold_record_constant(ctx, cbm_node_text(ctx->arena, name_node, ctx->source), |
| 1223 | value_node); |
| 1224 | return; |
| 1225 | } |
| 1226 | |
| 1227 | /* Value must be a string literal (template literals flatten to "{}" form) */ |
| 1228 | const char *value_kind = ts_node_type(value_node); |
| 1229 | const char *flat_value = NULL; |
| 1230 | if (strcmp(value_kind, "template_string") == 0) { |
| 1231 | flat_value = cbm_template_string_text(ctx->arena, value_node, ctx->source); |
| 1232 | if (!flat_value) { |
| 1233 | return; |
| 1234 | } |
| 1235 | } else if (!is_string_node(value_kind)) { |
| 1236 | return; |
| 1237 | } |
no test coverage detected