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
« prev ^ index » next coverage.py v6.5.0, created at 2022-12-08 16:33 -0600
1from typing import Optional
3from .element import Element
4from .node import Node
5from .position import Position
6from .root import Root
9class Literal(Node):
10 """Literal (UnistLiteral) represents a node in hast containing a value."""
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 """
18 value: str
19 """The Literal nodes value. All literal values must be strings"""
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
31 def __eq__(self, obj) -> bool:
32 return bool(obj is not None and self.type == obj.type and self.value == obj.value)
34 def get_ancestry(self) -> list[str]:
35 """Get the ancestry of the literal node.
37 Used to validate whether there is a `pre` element in the ancestry.
38 """
40 def get_parent(parent) -> list[str]:
41 result = []
43 if parent is not None and hasattr(parent, "tag"):
44 result.append(parent.tag)
46 if parent.parent is not None:
47 result.extend(get_parent(parent.parent))
49 return result
51 return get_parent(self.parent)