summaryrefslogtreecommitdiff
path: root/deps/v8/third_party/jinja2/visitor.py
diff options
context:
space:
mode:
Diffstat (limited to 'deps/v8/third_party/jinja2/visitor.py')
-rw-r--r--deps/v8/third_party/jinja2/visitor.py35
1 files changed, 23 insertions, 12 deletions
diff --git a/deps/v8/third_party/jinja2/visitor.py b/deps/v8/third_party/jinja2/visitor.py
index d1365bf10e..17c6aaba57 100644
--- a/deps/v8/third_party/jinja2/visitor.py
+++ b/deps/v8/third_party/jinja2/visitor.py
@@ -1,11 +1,19 @@
-# -*- coding: utf-8 -*-
"""API for traversing the AST nodes. Implemented by the compiler and
meta introspection.
"""
+import typing as t
+
from .nodes import Node
+if t.TYPE_CHECKING:
+ import typing_extensions as te
+
+ class VisitCallable(te.Protocol):
+ def __call__(self, node: Node, *args: t.Any, **kwargs: t.Any) -> t.Any:
+ ...
-class NodeVisitor(object):
+
+class NodeVisitor:
"""Walks the abstract syntax tree and call visitor functions for every
node found. The visitor functions may return values which will be
forwarded by the `visit` method.
@@ -17,25 +25,26 @@ class NodeVisitor(object):
(return value `None`) the `generic_visit` visitor is used instead.
"""
- def get_visitor(self, node):
+ def get_visitor(self, node: Node) -> "t.Optional[VisitCallable]":
"""Return the visitor function for this node or `None` if no visitor
exists for this node. In that case the generic visit function is
used instead.
"""
- method = "visit_" + node.__class__.__name__
- return getattr(self, method, None)
+ return getattr(self, f"visit_{type(node).__name__}", None)
- def visit(self, node, *args, **kwargs):
+ def visit(self, node: Node, *args: t.Any, **kwargs: t.Any) -> t.Any:
"""Visit a node."""
f = self.get_visitor(node)
+
if f is not None:
return f(node, *args, **kwargs)
+
return self.generic_visit(node, *args, **kwargs)
- def generic_visit(self, node, *args, **kwargs):
+ def generic_visit(self, node: Node, *args: t.Any, **kwargs: t.Any) -> t.Any:
"""Called if no explicit visitor function exists for a node."""
- for node in node.iter_child_nodes():
- self.visit(node, *args, **kwargs)
+ for child_node in node.iter_child_nodes():
+ self.visit(child_node, *args, **kwargs)
class NodeTransformer(NodeVisitor):
@@ -49,7 +58,7 @@ class NodeTransformer(NodeVisitor):
replacement takes place.
"""
- def generic_visit(self, node, *args, **kwargs):
+ def generic_visit(self, node: Node, *args: t.Any, **kwargs: t.Any) -> Node:
for field, old_value in node.iter_fields():
if isinstance(old_value, list):
new_values = []
@@ -71,11 +80,13 @@ class NodeTransformer(NodeVisitor):
setattr(node, field, new_node)
return node
- def visit_list(self, node, *args, **kwargs):
+ def visit_list(self, node: Node, *args: t.Any, **kwargs: t.Any) -> t.List[Node]:
"""As transformers may return lists in some places this method
can be used to enforce a list as return value.
"""
rv = self.visit(node, *args, **kwargs)
+
if not isinstance(rv, list):
- rv = [rv]
+ return [rv]
+
return rv