Skip to content

Commit

Permalink
New method to find the nearest ancestor of a particular kind
Browse files Browse the repository at this point in the history
  • Loading branch information
lelit committed Jun 18, 2022
1 parent e995f0d commit f6b187c
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pglast/visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,21 @@ def __getitem__(self, n):
n -= 1
return path.node

def __contains__(self, cls):
"Tell whether there is an ancestor of type `cls` in the chain."
def find_nearest(self, cls):
"Find the nearest ancestor with a node of the given `cls`."

path = self
while True:
if isinstance(path.node, cls):
return True
return path
path = path.parent
if path is None:
return False
break

def __contains__(self, cls):
"Tell whether there is a node of type `cls` in the anchestry."

return self.find_nearest(cls) is not None

def __truediv__(self, node_and_member):
"Create a new instance pointing to the given child node."
Expand Down

0 comments on commit f6b187c

Please sign in to comment.