Coverage for phml\nodes\literal.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-12-08 16:33 -0600

1from typing import Optional 

2 

3from .element import Element 

4from .node import Node 

5from .position import Position 

6from .root import Root 

7 

8 

9class Literal(Node): 

10 """Literal (UnistLiteral) represents a node in hast containing a value.""" 

11 

12 position: Position 

13 """The location of a node in a source document. 

14 The value of the position field implements the Position interface. 

15 The position field must not be present if a node is generated. 

16 """ 

17 

18 value: str 

19 """The Literal nodes value. All literal values must be strings""" 

20 

21 def __init__( 

22 self, 

23 value: str = "", 

24 parent: Optional[Element | Root] = None, 

25 position: Optional[Position] = None, 

26 ): 

27 super().__init__(position) 

28 self.value = value 

29 self.parent = parent 

30 

31 def __eq__(self, obj) -> bool: 

32 return bool(obj is not None and self.type == obj.type and self.value == obj.value) 

33 

34 def get_ancestry(self) -> list[str]: 

35 """Get the ancestry of the literal node. 

36 

37 Used to validate whether there is a `pre` element in the ancestry. 

38 """ 

39 

40 def get_parent(parent) -> list[str]: 

41 result = [] 

42 

43 if parent is not None and hasattr(parent, "tag"): 

44 result.append(parent.tag) 

45 

46 if parent.parent is not None: 

47 result.extend(get_parent(parent.parent)) 

48 

49 return result 

50 

51 return get_parent(self.parent)