Beispiel #1
0
def minify(text, mangle=True, mangle_toplevel=True):
    parser = Parser()
    tree = parser.parse(text)
    if mangle:
        mangler.mangle(tree, toplevel=mangle_toplevel)
    minified = ECMAMinifier().visit(tree)
    return minified
Beispiel #2
0
 def test_func(self):
     parser = Parser()
     tree = parser.parse(input)
     mangle(tree, toplevel=True)
     self.assertMultiLineEqual(
         textwrap.dedent(tree.to_ecma()).strip(),
         textwrap.dedent(expected).strip())
Beispiel #3
0
 def test_bug_no_semicolon_at_the_end_of_block_plus_newline_at_eof(self):
     # https://github.com/rspivak/nigit/issues/3
     text = textwrap.dedent("""
     function add(x, y) {
       return x + y;
     }
     """)
     parser = Parser()
     tree = parser.parse(text)
     self.assertTrue(bool(tree.children()))
Beispiel #4
0
 def _test_function_expression(self):
     text = """
     if (true) {
       function() {
         foo;
         location = 'http://anywhere.com';
       }
     }
     """
     parser = Parser()
     parser.parse(text)
Beispiel #5
0
 def test_modify_tree(self):
     text = """
     for (var i = 0; i < 10; i++) {
       var x = 5 + i;
     }
     """
     parser = Parser()
     tree = parser.parse(text)
     for node in nodevisitor.visit(tree):
         if isinstance(node, ast.Identifier) and node.value == 'i':
             node.value = 'hello'
     self.assertMultiLineEqual(
         tree.to_ecma(),
         textwrap.dedent("""
         for (var hello = 0; hello < 10; hello++) {
           var x = 5 + hello;
         }
         """).strip())
Beispiel #6
0
 def test_throw_statement(self):
     # expression is not optional in throw statement
     input = textwrap.dedent("""
     throw
       'exc';
     """)
     parser = Parser()
     # ASI at lexer level should insert ';' after throw
     self.assertRaises(SyntaxError, parser.parse, input)
Beispiel #7
0
 def test_line_terminator_at_the_end_of_file(self):
     parser = Parser()
     parser.parse('var $_ = function(x){}(window);\n')
Beispiel #8
0
 def test_func(self):
     parser = Parser()
     result = parser.parse(input).to_ecma()
     self.assertMultiLineEqual(result, expected)
Beispiel #9
0
 def test_that_parsing_eventually_stops(self):
     text = """var a;
     , b;"""
     parser = Parser()
     self.assertRaises(SyntaxError, parser.parse, text)
Beispiel #10
0
 def test_function_expression_is_part_of_member_expr_nobf(self):
     # https://github.com/rspivak/nigit/issues/22
     # The problem happened to be that function_expr was not
     # part of member_expr_nobf rule
     text = 'window.done_already || function () { return "nigit!" ; }();'
     self.assertTrue(bool(Parser().parse(text).children()))