(root: Node<'_>, source: &str, language: Language)
| 343 | } |
| 344 | |
| 345 | pub(crate) fn control_flow(root: Node<'_>, source: &str, language: Language) -> (u32, u32) { |
| 346 | let mut complexity = 1; |
| 347 | let mut maximum = 0; |
| 348 | let mut stack = vec![(root, 0)]; |
| 349 | while let Some((node, depth)) = stack.pop() { |
| 350 | if node.id() != root.id() && (callable(node, language) || type_boundary(node, language)) { |
| 351 | continue; |
| 352 | } |
| 353 | if super::tokens::comment(node) || literal(node) { |
| 354 | continue; |
| 355 | } |
| 356 | let go_switch = matches!( |
| 357 | node.kind(), |
| 358 | "expression_switch_statement" | "type_switch_statement" |
| 359 | ); |
| 360 | let control = go_switch |
| 361 | || matches!( |
| 362 | node.kind(), |
| 363 | "if_statement" |
| 364 | | "if_expression" |
| 365 | | "elif_clause" |
| 366 | | "for_statement" |
| 367 | | "for_expression" |
| 368 | | "for_in_statement" |
| 369 | | "while_statement" |
| 370 | | "while_expression" |
| 371 | | "do_statement" |
| 372 | | "do_while_statement" |
| 373 | | "loop_expression" |
| 374 | | "catch_clause" |
| 375 | | "except_clause" |
| 376 | | "guard_statement" |
| 377 | | "switch_statement" |
| 378 | | "switch_expression" |
| 379 | | "match_expression" |
| 380 | | "match_statement" |
| 381 | | "select_statement" |
| 382 | | "conditional_expression" |
| 383 | | "ternary_expression" |
| 384 | ); |
| 385 | let next_depth = depth + u32::from(control && !is_else_if(node)); |
| 386 | maximum = maximum.max(next_depth); |
| 387 | if control |
| 388 | && !go_switch |
| 389 | && !matches!( |
| 390 | node.kind(), |
| 391 | "switch_statement" |
| 392 | | "switch_expression" |
| 393 | | "match_expression" |
| 394 | | "match_statement" |
| 395 | | "select_statement" |
| 396 | ) |
| 397 | { |
| 398 | complexity += 1; |
| 399 | } |
| 400 | if matches!( |
| 401 | node.kind(), |
| 402 | "conjunction_expression" | "disjunction_expression" |
no test coverage detected