Skip to content

Commit 1b46b8b

Browse files
committed
Don't recurse into child namespace bodies
1 parent 174647c commit 1b46b8b

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

‎src/python_minifier/transforms/remove_dead_branches.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,18 @@ def explore_namespace(node):
130130
elif isinstance(node, (ast.Yield, ast.YieldFrom)):
131131
properties.is_generator = True
132132

133+
# Skip recursing into the body field of child namespaces, as they can only affect the child namespace
134+
# Other fields (decorators, annotations, arguments...) can affect this namespace,
135+
# so we do want to recurse into them
136+
skip_body = node is not namespace and isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.Lambda, ast.ClassDef))
137+
133138
for name, field in ast.iter_fields(node):
134139
if field in removed_suites:
135140
continue
136141

142+
if skip_body and name == 'body':
143+
continue
144+
137145
if isinstance(field, ast.AST):
138146
explore_namespace(field)
139147
elif isinstance(field, list):

0 commit comments

Comments
 (0)