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

extended PR #60: fixed dot and bracket accessors on immediate numbers #87

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions src/slimit/tests/test_ecmavisitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,15 @@ def setUp(self):
}
};
""",
# function call on immediate number
'(0x25).toString();',
'(1e3).toString();',
'(25).toString();',

# attribute access on immediate number
'(25).attr;',
'25["attr"];',
'0["attr"];',
]


11 changes: 11 additions & 0 deletions src/slimit/tests/test_minifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,17 @@ def assertMinified(self, source, expected):
""",
"(function($){$.hello='world';}(jQuery));"),

# function call on immediate number
('(0x25).toString()', '0x25.toString();'),
('(1e3).toString()', '1e3.toString();'),
('((25)).toString()', '(25).toString();'),
('((25))["toString"]()', '(25).toString();'),

# attribute access on immediate number
('((25)).attr', '(25).attr;'),
('((25))["attr"]', '(25).attr;'),
('((0))["attr"]', '(0).attr;'),

# function call in FOR init
('for(o(); i < 3; i++) {}', 'for(o();i<3;i++){}'),

Expand Down
2 changes: 2 additions & 0 deletions src/slimit/visitors/ecmavisitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ def visit_NewExpr(self, node):
def visit_DotAccessor(self, node):
if getattr(node, '_parens', False):
template = '(%s.%s)'
elif isinstance(node.node, ast.Number):
template = '(%s).%s'
else:
template = '%s.%s'
s = template % (self.visit(node.node), self.visit(node.identifier))
Expand Down
11 changes: 9 additions & 2 deletions src/slimit/visitors/minvisitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
from slimit.lexer import Lexer

_HAS_ID_MATCH = re.compile('^%s$' % Lexer.identifier).match
# Matches all immediate numbers, which are dot accessable (expects a number as input)
_DOT_ACCESSABLE_NUMBER = re.compile('^(0.+|.+[eE].+|.+\..+)$')


def _is_identifier(value):
return _HAS_ID_MATCH(value) and value not in Lexer.keywords_dict
Expand Down Expand Up @@ -386,6 +389,8 @@ def visit_NewExpr(self, node):
def visit_DotAccessor(self, node):
if getattr(node, '_parens', False):
template = '(%s.%s)'
elif isinstance(node.node, ast.Number) and not _DOT_ACCESSABLE_NUMBER.match(node.node.value):
template = '(%s).%s'
else:
template = '%s.%s'
s = template % (self.visit(node.node), self.visit(node.identifier))
Expand All @@ -400,7 +405,10 @@ def visit_BracketAccessor(self, node):
elif value.startswith('"'):
value = value.strip('"')
if _is_identifier(value):
s = '%s.%s' % (self.visit(node.node), value)
left = self.visit(node.node)
if isinstance(node.node, ast.Number) and not _DOT_ACCESSABLE_NUMBER.match(node.node.value):
left = '(%s)' % left
s = '%s.%s' % (left, value)
return s

s = '%s[%s]' % (self.visit(node.node), self.visit(node.expr))
Expand Down Expand Up @@ -434,4 +442,3 @@ def visit_Array(self, node):

def visit_This(self, node):
return 'this'