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
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())
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()))
def _test_function_expression(self): text = """ if (true) { function() { foo; location = 'http://anywhere.com'; } } """ parser = Parser() parser.parse(text)
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())
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)
def test_line_terminator_at_the_end_of_file(self): parser = Parser() parser.parse('var $_ = function(x){}(window);\n')
def test_func(self): parser = Parser() result = parser.parse(input).to_ecma() self.assertMultiLineEqual(result, expected)
def test_that_parsing_eventually_stops(self): text = """var a; , b;""" parser = Parser() self.assertRaises(SyntaxError, parser.parse, text)
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()))