Skip to content

Commit e893287

Browse files
committed
visit: simplify handling of root node
Replicates graphql/graphql-js@f2ecc28
1 parent c228b01 commit e893287

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

src/graphql/language/visitor.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -200,22 +200,23 @@ def visit(
200200
keys: Tuple[Node, ...] = (root,)
201201
idx = -1
202202
edits: List[Any] = []
203+
node: Any = root
204+
key: Any = None
203205
parent: Any = None
204206
path: List[Any] = []
205207
path_append = path.append
206208
path_pop = path.pop
207209
ancestors: List[Any] = []
208210
ancestors_append = ancestors.append
209211
ancestors_pop = ancestors.pop
210-
new_root = root
211212

212213
while True:
213214
idx += 1
214215
is_leaving = idx == len(keys)
215216
is_edited = is_leaving and edits
216217
if is_leaving:
217218
key = path[-1] if ancestors else None
218-
node: Any = parent
219+
node = parent
219220
parent = ancestors_pop() if ancestors else None
220221
if is_edited:
221222
if in_array:
@@ -238,21 +239,16 @@ def visit(
238239
edits = stack.edits
239240
in_array = stack.in_array
240241
stack = stack.prev
241-
else:
242-
if parent:
243-
if in_array:
244-
key = idx
245-
node = parent[key]
246-
else:
247-
key = keys[idx]
248-
node = getattr(parent, key, None)
242+
elif parent:
243+
if in_array:
244+
key = idx
245+
node = parent[key]
249246
else:
250-
key = None
251-
node = new_root
247+
key = keys[idx]
248+
node = getattr(parent, key, None)
252249
if node is None:
253250
continue
254-
if parent:
255-
path_append(key)
251+
path_append(key)
256252

257253
if isinstance(node, tuple):
258254
result = None
@@ -303,9 +299,9 @@ def visit(
303299
break
304300

305301
if edits:
306-
new_root = edits[-1][1]
302+
return edits[-1][1]
307303

308-
return new_root
304+
return root
309305

310306

311307
class ParallelVisitor(Visitor):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy