visitor.py
This file
Path
src/jinja2/visitor.py
92 lines · 3.5 KiB · Python
Last changed by
drop end of life python versions (#2096)David Lordbefore adoption49c48a0
History of this fileTake it
1"""API for traversing the AST nodes. Implemented by the compiler and2meta introspection.3"""45import typing as t67from .nodes import Node89if t.TYPE_CHECKING:10 import typing_extensions as te1112 class VisitCallable(te.Protocol):13 def __call__(self, node: Node, *args: t.Any, **kwargs: t.Any) -> t.Any: ...141516class NodeVisitor:17 """Walks the abstract syntax tree and call visitor functions for every18 node found. The visitor functions may return values which will be19 forwarded by the `visit` method.2021 Per default the visitor functions for the nodes are ``'visit_'`` +22 class name of the node. So a `TryFinally` node visit function would23 be `visit_TryFinally`. This behavior can be changed by overriding24 the `get_visitor` function. If no visitor function exists for a node25 (return value `None`) the `generic_visit` visitor is used instead.26 """2728 def get_visitor(self, node: Node) -> "VisitCallable | None":29 """Return the visitor function for this node or `None` if no visitor30 exists for this node. In that case the generic visit function is31 used instead.32 """33 return getattr(self, f"visit_{type(node).__name__}", None)3435 def visit(self, node: Node, *args: t.Any, **kwargs: t.Any) -> t.Any:36 """Visit a node."""37 f = self.get_visitor(node)3839 if f is not None:40 return f(node, *args, **kwargs)4142 return self.generic_visit(node, *args, **kwargs)4344 def generic_visit(self, node: Node, *args: t.Any, **kwargs: t.Any) -> t.Any:45 """Called if no explicit visitor function exists for a node."""46 for child_node in node.iter_child_nodes():47 self.visit(child_node, *args, **kwargs)484950class NodeTransformer(NodeVisitor):51 """Walks the abstract syntax tree and allows modifications of nodes.5253 The `NodeTransformer` will walk the AST and use the return value of the54 visitor functions to replace or remove the old node. If the return55 value of the visitor function is `None` the node will be removed56 from the previous location otherwise it's replaced with the return57 value. The return value may be the original node in which case no58 replacement takes place.59 """6061 def generic_visit(self, node: Node, *args: t.Any, **kwargs: t.Any) -> Node:62 for field, old_value in node.iter_fields():63 if isinstance(old_value, list):64 new_values = []65 for value in old_value:66 if isinstance(value, Node):67 value = self.visit(value, *args, **kwargs)68 if value is None:69 continue70 elif not isinstance(value, Node):71 new_values.extend(value)72 continue73 new_values.append(value)74 old_value[:] = new_values75 elif isinstance(old_value, Node):76 new_node = self.visit(old_value, *args, **kwargs)77 if new_node is None:78 delattr(node, field)79 else:80 setattr(node, field, new_node)81 return node8283 def visit_list(self, node: Node, *args: t.Any, **kwargs: t.Any) -> list[Node]:84 """As transformers may return lists in some places this method85 can be used to enforce a list as return value.86 """87 rv = self.visit(node, *args, **kwargs)8889 if not isinstance(rv, list):90 return [rv]9192 return rv