Skip to content

Commit

Permalink
Fixed bug where class attributes weren't being transformed
Browse files Browse the repository at this point in the history
  • Loading branch information
mad-cat-lon committed Aug 7, 2024
1 parent e9d64ff commit a45d438
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions jargonaut/transformations/layout/randomize_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
QualifiedNameSource,
ScopeProvider,
ClassScope,
TypeInferenceProvider
FunctionScope,
TypeInferenceProvider,
ParentNodeProvider
)
import libcst as cst
import random
from typing import Optional
from yaspin.spinners import Spinners
from yaspin import kbi_safe_yaspin
import builtins
from collections import defaultdict


class RandomizeAttributes(cst.CSTTransformer):
Expand All @@ -23,11 +24,12 @@ class RandomizeAttributes(cst.CSTTransformer):
METADATA_DEPENDENCIES = (
QualifiedNameProvider,
ScopeProvider,
TypeInferenceProvider
TypeInferenceProvider,
ParentNodeProvider
)

def __init__(self):
self.randomize_map = defaultdict(self.rand_name)
self.randomize_map = {}
self.avoid_names = ["self", "__init__", "main", "super"]
self.avoid_names.extend(dir(builtins))
self.ignore = False
Expand Down Expand Up @@ -97,15 +99,23 @@ def leave_Attribute(
return updated_node
qualified_names = list(scope.get_qualified_names_for(original_node))
qualified_name = qualified_names[0]
# print(f"scope: {scope} qualified_name: {qualified_name}")
if isinstance(scope, ClassScope):
if isinstance(scope, FunctionScope):
if qualified_name.source == QualifiedNameSource.LOCAL:
if original_node.attr.value not in self.avoid_names:
return updated_node.with_changes(
attr=cst.Name(
value=self.randomize_map[original_node.attr.value]
if original_node.attr.value not in self.randomize_map:
new_name = self.rand_name()
self.randomize_map[original_node.attr.value] = new_name
return updated_node.with_changes(
attr=cst.Name(
value=new_name
)
)
)
else:
return updated_node.with_changes(
attr=cst.Name(
value=self.randomize_map[original_node.attr.value]
)
)
else:
return updated_node
else:
Expand Down

0 comments on commit a45d438

Please sign in to comment.