Coverage for metrics/cyclomatic/go.py: 92%

1"""McCabe cyclomatic complexity for Go."""

2

3 from __future__ import annotations

4

5 from parsers.go import is_named_func_literal

6 from metrics.cyclomatic.shared import (

7 create_cyclomatic_computer,

8 create_cyclomatic_wrapper,

9)

10

11

12 _GO_DECISION_TYPES = {

13 "if_statement",

14 "for_statement",

15 "expression_case",

16 "type_case",

17 "communication_case",

18}

19

20 _GO_LOGICAL_OPS = {"&&", "||"}

21

22

23 def _go_skip_check(child, top_func) -> bool:

24 """Skip nested function/method declarations."""

25 if (

26 child.type in ("function_declaration", "method_declaration")

27 and child is not top_func

28 ):

29 return True

30 if child.type == "func_literal" and child is not top_func:

31 return is_named_func_literal(child)

32 return False

33

34

35 _count_decisions = create_cyclomatic_computer(

36 _go_skip_check, _GO_DECISION_TYPES, _GO_LOGICAL_OPS

37)

38 compute_cyclomatic_go = create_cyclomatic_wrapper(_count_decisions)