Coverage for dead_code/parsers/base.py: 89%

1"""Base parser interface for dead code detection."""

2

3 from __future__ import annotations

4

5 from dataclasses import dataclass

6 from abc import ABC, abstractmethod

7 from typing import TYPE_CHECKING

8

9 if TYPE_CHECKING:

10 from tree_sitter import Node

11

12

13@dataclass

14 class SymbolDefinition:

15 name: str

16 line: int

17 definition_type: str

18 exported: bool = False

19

20

21@dataclass

22 class ImportedSymbol:

23 name: str

24 line: int

25 imported_from: str | None = None

26 alias: str | None = None

27

28

29@dataclass

30 class VariableDefinition:

31 name: str

32 line: int

33 scope_type: str

34 definition_type: str

35

36

37@dataclass

38 class SymbolReference:

39 name: str

40 line: int

41

42

43 class DeadCodeParser(ABC):

44 def __init__(self):

45 pass

46

47 @abstractmethod

48 def extract_imports(self, root: Node, source: str) -> list[ImportedSymbol]:

49 pass

50

51 @abstractmethod

52 def extract_definitions(self, root: Node, source: str) -> list[SymbolDefinition]:

53 pass

54

55 @abstractmethod

56 def extract_variable_definitions(

57 self, root: Node, source: str

58 ) -> dict[tuple, list[VariableDefinition]]:

59 pass

60

61 @abstractmethod

62 def extract_references(self, root: Node, source: str) -> list[SymbolReference]:

63 pass

64

65 @abstractmethod

66 def extract_scope_references(

67 self, root: Node, source: str

68 ) -> dict[tuple, list[SymbolReference]]:

69 pass