Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable merging var statements #112

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/slimit/visitors/minvisitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def __init__(self):
self.ifelse_stack = []

def visit(self, node):
self._merge_var_statements(node)
method = 'visit_%s' % node.__class__.__name__
return getattr(self, method, self.generic_visit)(node)

Expand Down Expand Up @@ -435,3 +436,24 @@ def visit_Array(self, node):
def visit_This(self, node):
return 'this'

def _merge_var_statements(self, node):
elements = None
if hasattr(node, 'elements'):
elements = node.elements
elif hasattr(node, '_children_list'):
elements = node._children_list
if elements is not None:
first_var = None
elements_to_delete = []
for element in elements:
if element.__class__.__name__ == 'VarStatement':
if first_var is None:
first_var = element
else:
first_var._children_list += element._children_list
elements_to_delete.append(element)
else:
first_var = None
for element in elements_to_delete:
elements.remove(element)