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

1"""LLOC (Logical Lines of Code) for TypeScript."""

2

3 from __future__ import annotations

4

5 _TS_LLOC_TYPES = {

6 # Simple statements

7 "expression_statement", "return_statement",

8 "break_statement", "continue_statement",

9 "throw_statement", "debugger_statement", "empty_statement",

10 # Declarations

11 "variable_declaration", "lexical_declaration",

12 "function_declaration", "generator_function_declaration",

13 "class_declaration", "abstract_class_declaration",

14 "type_alias_declaration", "interface_declaration",

15 "enum_declaration",

16 # Import / export

17 "import_statement", "export_statement",

18 # Compound statement headers

19 "if_statement",

20 "for_statement", "for_in_statement",

21 "while_statement", "do_statement",

22 "switch_statement", "switch_case", "switch_default",

23 "try_statement", "catch_clause", "finally_clause",

24 "with_statement", "labeled_statement",

25}

26

27

28 def count_lloc(root) -> int:

29 """Count logical lines of code in a TypeScript AST."""

30 count = 0

31 if root.type in _TS_LLOC_TYPES:

32 count += 1

33 elif root.type == "else_clause":

34 if not any(c.type == "if_statement" for c in root.children):

35 count += 1

36 for child in root.children:

37 count += count_lloc(child)

38 return count