Coverage for metrics/cyclomatic/typescript.py: 100%

1"""McCabe cyclomatic complexity for TypeScript."""

2

3 from __future__ import annotations

4

5 from metrics.cyclomatic.shared import (

6 create_cyclomatic_computer,

7 create_cyclomatic_wrapper,

8)

9

10

11 _TS_SKIP_TYPES = {

12 "function_declaration",

13 "function_expression",

14 "method_definition",

15 "generator_function_declaration",

16 "class_declaration",

17 "class_expression",

18 "arrow_function",

19}

20

21 _TS_DECISION_TYPES = {

22 "if_statement",

23 "for_statement",

24 "for_in_statement",

25 "while_statement",

26 "do_statement",

27 "switch_case",

28 "catch_clause",

29 "ternary_expression",

30}

31

32 _TS_LOGICAL_OPS = {"&&", "||", "??"}

33

34

35 def _ts_skip_check(child, top_func) -> bool:

36 """Skip nested function declarations."""

37 if child.type in _TS_SKIP_TYPES and child is not top_func:

38 return True

39 return False

40

41

42 _count_decisions = create_cyclomatic_computer(

43 _ts_skip_check, _TS_DECISION_TYPES, _TS_LOGICAL_OPS

44)

45 compute_cyclomatic_ts = create_cyclomatic_wrapper(_count_decisions)