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

Fixed issue #47 where objects with special characters would cause breakage #48

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
18 changes: 10 additions & 8 deletions src/slimit/visitors/minvisitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
def _is_identifier(value):
return _HAS_ID_MATCH(value) and value not in Lexer.keywords_dict

specials = re.compile(r'[`\\~!@#%\^&*(){}\[\]\-+=/|<>,.:;?]+')

class ECMAMinifier(object):

Expand Down Expand Up @@ -394,14 +395,15 @@ def visit_DotAccessor(self, node):
def visit_BracketAccessor(self, node):
if isinstance(node.expr, ast.String):
value = node.expr.value
# remove single or double quotes around the value, but not both
if value.startswith("'"):
value = value.strip("'")
elif value.startswith('"'):
value = value.strip('"')
if _is_identifier(value):
s = '%s.%s' % (self.visit(node.node), value)
return s
if not specials.search(value):
# remove single or double quotes around the value, but not both
if value.startswith("'"):
value = value.strip("'")
elif value.startswith('"'):
value = value.strip('"')
if _is_identifier(value):
s = '%s.%s' % (self.visit(node.node), value)
return s

s = '%s[%s]' % (self.visit(node.node), self.visit(node.expr))
return s
Expand Down