Walk ancestors from the call site to the enclosing function, collecting * conditions innermost-first (the caller reverses for display). Returns the * guard count; sets *in_loop when any ancestor is a loop. */
| 162 | * conditions innermost-first (the caller reverses for display). Returns the |
| 163 | * guard count; sets *in_loop when any ancestor is a loop. */ |
| 164 | static int why_guards_at(const char *source, int source_len, TSNode at, why_guard_t *guards, |
| 165 | bool *in_loop) { |
| 166 | int count = 0; |
| 167 | *in_loop = false; |
| 168 | TSNode prev = at; |
| 169 | for (TSNode node = ts_node_parent(at); !ts_node_is_null(node); |
| 170 | prev = node, node = ts_node_parent(node)) { |
| 171 | const char *type = ts_node_type(node); |
| 172 | if (why_type_has(type, "function") || why_type_has(type, "method") || |
| 173 | why_type_has(type, "lambda") || why_type_has(type, "closure")) |
| 174 | break; |
| 175 | if (count >= WHY_MAX_GUARDS) |
| 176 | continue; /* keep scanning for the function boundary + loops */ |
| 177 | |
| 178 | const char *kind = NULL; |
| 179 | TSNode cond = ts_node_child_by_field_name(node, "condition", 9); |
| 180 | bool negated = false; |
| 181 | |
| 182 | if (why_type_has(type, "if_") || strcmp(type, "if") == 0 || why_type_has(type, "elif")) { |
| 183 | kind = "if"; |
| 184 | TSNode alt = ts_node_child_by_field_name(node, "alternative", 11); |
| 185 | if (!ts_node_is_null(alt) && ts_node_start_byte(alt) <= ts_node_start_byte(prev) && |
| 186 | ts_node_end_byte(prev) <= ts_node_end_byte(alt)) |
| 187 | negated = true; |
| 188 | } else if (why_type_has(type, "conditional_expression") || why_type_has(type, "ternary")) { |
| 189 | kind = "ternary"; |
| 190 | TSNode alt = ts_node_child_by_field_name(node, "alternative", 11); |
| 191 | if (!ts_node_is_null(alt) && ts_node_start_byte(alt) <= ts_node_start_byte(prev) && |
| 192 | ts_node_end_byte(prev) <= ts_node_end_byte(alt)) |
| 193 | negated = true; |
| 194 | } else if (why_type_has(type, "case") || why_type_has(type, "when_")) { |
| 195 | kind = "case"; |
| 196 | if (ts_node_is_null(cond)) |
| 197 | cond = ts_node_child_by_field_name(node, "value", 5); |
| 198 | } else if (why_type_has(type, "switch") || why_type_has(type, "match")) { |
| 199 | /* The switch head itself: only worth a guard when the case level |
| 200 | * was missing (some grammars nest cases invisibly). */ |
| 201 | kind = "switch"; |
| 202 | } else if (why_type_has(type, "while") || why_type_has(type, "for") || |
| 203 | why_type_has(type, "do_statement") || why_type_has(type, "loop") || |
| 204 | why_type_has(type, "repeat")) { |
| 205 | kind = "loop"; |
| 206 | *in_loop = true; |
| 207 | } else if (why_type_has(type, "catch") || why_type_has(type, "except") || |
| 208 | why_type_has(type, "rescue")) { |
| 209 | kind = "catch"; |
| 210 | } |
| 211 | if (!kind) |
| 212 | continue; |
| 213 | /* Skip switch heads when a case already carries the discriminant. */ |
| 214 | if (strcmp(kind, "switch") == 0 && count > 0 && strcmp(guards[count - 1].kind, "case") == 0) |
| 215 | continue; |
| 216 | |
| 217 | why_guard_t *guard = &guards[count]; |
| 218 | guard->kind = kind; |
| 219 | guard->negated = negated; |
| 220 | guard->cond[0] = '\0'; |
| 221 | if (!ts_node_is_null(cond)) |
no test coverage detected