More test conversion, minor generation fixes

This commit is contained in:
Liam Newman 2014-10-17 12:38:27 -07:00
parent 1c34779073
commit f980a6f5f4
6 changed files with 422 additions and 610 deletions

View File

@ -235,7 +235,10 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
// Unicode Support
bt('var ' + unicode_char(3232) + '_' + unicode_char(3232) + ' = "hi";');
bt('var ' + unicode_char(228) + 'x = {\n ' + unicode_char(228) + 'rgerlich: true\n};');
bt(
'var ' + unicode_char(228) + 'x = {\n' +
' ' + unicode_char(228) + 'rgerlich: true\n' +
'};');
// End With Newline - (eof = "\n")
opts.end_with_newline = true;
@ -359,150 +362,140 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
bt('a={1:[-1],2:[+1]}', 'a = {\n 1: [-1],\n 2: [+1]\n}');
bt('var l = {\'a\':\'1\', \'b\':\'2\'}', 'var l = {\n \'a\': \'1\',\n \'b\': \'2\'\n}');
bt('if (template.user[n] in bk) foo();');
bt('{{}/z/}', "{\n {}\n /z/\n}");
bt('return 45', "return 45");
bt('return 45');
bt('return this.prevObject ||\n\n this.constructor(null);');
bt('If[1]', "If[1]");
bt('Then[1]', "Then[1]");
bt('a = 1e10', "a = 1e10");
bt('a = 1.3e10', "a = 1.3e10");
bt('a = 1.3e-10', "a = 1.3e-10");
bt('a = -1.3e-10', "a = -1.3e-10");
bt('a = 1e-10', "a = 1e-10");
bt('a = e - 10', "a = e - 10");
bt('a = 11-10', "a = 11 - 10");
bt("a = 1;// comment", "a = 1; // comment");
bt("a = 1; // comment", "a = 1; // comment");
bt("a = 1;\n // comment", "a = 1;\n// comment");
bt('If[1]');
bt('Then[1]');
bt('a = 1e10');
bt('a = 1.3e10');
bt('a = 1.3e-10');
bt('a = -1.3e-10');
bt('a = 1e-10');
bt('a = e - 10');
bt('a = 11-10', 'a = 11 - 10');
bt('a = 1;// comment', 'a = 1; // comment');
bt('a = 1; // comment');
bt('a = 1;\n // comment', 'a = 1;\n// comment');
bt('a = [-1, -1, -1]');
// The exact formatting these should have is open for discussion, but they are at least reasonable
bt('a = [ // comment\n -1, -1, -1\n]');
bt('var a = [ // comment\n -1, -1, -1\n]');
bt('a = [ // comment\n -1, // comment\n -1, -1\n]');
bt('var a = [ // comment\n -1, // comment\n -1, -1\n]');
bt('o = [{a:b},{c:d}]', 'o = [{\n a: b\n}, {\n c: d\n}]');
bt("if (a) {\n do();\n}"); // was: extra space appended
bt("if (a) {\n// comment\n}else{\n// comment\n}", "if (a) {\n // comment\n} else {\n // comment\n}"); // if/else statement with empty body
bt("if (a) {\n// comment\n// comment\n}", "if (a) {\n // comment\n // comment\n}"); // multiple comments indentation
bt("if (a) b() else c();", "if (a) b()\nelse c();");
bt("if (a) b() else if c() d();", "if (a) b()\nelse if c() d();");
bt("{}");
bt("{\n\n}");
bt("do { a(); } while ( 1 );", "do {\n a();\n} while (1);");
bt("do {} while (1);");
bt("do {\n} while (1);", "do {} while (1);");
bt("do {\n\n} while (1);");
bt("var a = x(a, b, c)");
bt("delete x if (a) b();", "delete x\nif (a) b();");
bt("delete x[x] if (a) b();", "delete x[x]\nif (a) b();");
bt("for(var a=1,b=2)d", "for (var a = 1, b = 2) d");
bt("for(var a=1,b=2,c=3) d", "for (var a = 1, b = 2, c = 3) d");
bt("for(var a=1,b=2,c=3;d<3;d++)\ne", "for (var a = 1, b = 2, c = 3; d < 3; d++)\n e");
bt("function x(){(a||b).c()}", "function x() {\n (a || b).c()\n}");
bt("function x(){return - 1}", "function x() {\n return -1\n}");
bt("function x(){return ! a}", "function x() {\n return !a\n}");
bt("x => x", "x => x");
bt("(x) => x", "(x) => x");
bt("x => { x }", "x => {\n x\n}");
bt("(x) => { x }", "(x) => {\n x\n}");
// was: extra space appended
bt('if (a) {\n do();\n}');
// if/else statement with empty body
bt('if (a) {\n// comment\n}else{\n// comment\n}', 'if (a) {\n // comment\n} else {\n // comment\n}');
// multiple comments indentation
bt('if (a) {\n// comment\n// comment\n}', 'if (a) {\n // comment\n // comment\n}');
bt('if (a) b() else c();', 'if (a) b()\nelse c();');
bt('if (a) b() else if c() d();', 'if (a) b()\nelse if c() d();');
bt('{}');
bt('{\n\n}');
bt('do { a(); } while ( 1 );', 'do {\n a();\n} while (1);');
bt('do {} while (1);');
bt('do {\n} while (1);', 'do {} while (1);');
bt('do {\n\n} while (1);');
bt('var a = x(a, b, c)');
bt('delete x if (a) b();', 'delete x\nif (a) b();');
bt('delete x[x] if (a) b();', 'delete x[x]\nif (a) b();');
bt('for(var a=1,b=2)d', 'for (var a = 1, b = 2) d');
bt('for(var a=1,b=2,c=3) d', 'for (var a = 1, b = 2, c = 3) d');
bt('for(var a=1,b=2,c=3;d<3;d++)\ne', 'for (var a = 1, b = 2, c = 3; d < 3; d++)\n e');
bt('function x(){(a||b).c()}', 'function x() {\n (a || b).c()\n}');
bt('function x(){return - 1}', 'function x() {\n return -1\n}');
bt('function x(){return ! a}', 'function x() {\n return !a\n}');
bt('x => x');
bt('(x) => x');
bt('x => { x }', 'x => {\n x\n}');
bt('(x) => { x }', '(x) => {\n x\n}');
// a common snippet in jQuery plugins
bt("settings = $.extend({},defaults,settings);", "settings = $.extend({}, defaults, settings);");
// reserved words used as property names
bt("$http().then().finally().default()", "$http().then().finally().default()");
bt("$http()\n.then()\n.finally()\n.default()", "$http()\n .then()\n .finally()\n .default()");
bt("$http().when.in.new.catch().throw()", "$http().when.in.new.catch().throw()");
bt("$http()\n.when\n.in\n.new\n.catch()\n.throw()", "$http()\n .when\n .in\n .new\n .catch()\n .throw()");
bt('settings = $.extend({},defaults,settings);', 'settings = $.extend({}, defaults, settings);');
bt('$http().then().finally().default()');
bt('$http()\n.then()\n.finally()\n.default()', '$http()\n .then()\n .finally()\n .default()');
bt('$http().when.in.new.catch().throw()');
bt('$http()\n.when\n.in\n.new\n.catch()\n.throw()', '$http()\n .when\n .in\n .new\n .catch()\n .throw()');
bt('{xxx;}()', '{\n xxx;\n}()');
bt("a = 'a'\nb = 'b'");
bt("a = /reg/exp");
bt("a = /reg/");
bt('a = \'a\'\nb = \'b\'');
bt('a = /reg/exp');
bt('a = /reg/');
bt('/abc/.test()');
bt('/abc/i.test()');
bt("{/abc/i.test()}", "{\n /abc/i.test()\n}");
bt('{/abc/i.test()}', '{\n /abc/i.test()\n}');
bt('var x=(a)/a;', 'var x = (a) / a;');
bt('x != -1', 'x != -1');
bt('x != -1');
bt('for (; s-->0;)t', 'for (; s-- > 0;) t');
bt('for (; s++>0;)u', 'for (; s++ > 0;) u');
bt('a = s++>s--;', 'a = s++ > s--;');
bt('a = s++>--s;', 'a = s++ > --s;');
bt('{x=#1=[]}', '{\n x = #1=[]\n}');
bt('{a:#1={}}', '{\n a: #1={}\n}');
bt('{a:#1#}', '{\n a: #1#\n}');
test_fragment('"incomplete-string');
test_fragment("'incomplete-string");
test_fragment('\'incomplete-string');
test_fragment('/incomplete-regex');
test_fragment('`incomplete-template-string');
test_fragment('{a:1},{a:2}', '{\n a: 1\n}, {\n a: 2\n}');
test_fragment('var ary=[{a:1}, {a:2}];', 'var ary = [{\n a: 1\n}, {\n a: 2\n}];');
test_fragment('{a:#1', '{\n a: #1'); // incomplete
test_fragment('{a:#', '{\n a: #'); // incomplete
test_fragment('}}}', '}\n}\n}'); // incomplete
test_fragment('<!--\nvoid();\n// -->', '<!--\nvoid();\n// -->');
test_fragment('a=/regexp', 'a = /regexp'); // incomplete regexp
// incomplete
test_fragment('{a:#1', '{\n a: #1');
// incomplete
test_fragment('{a:#', '{\n a: #');
// incomplete
test_fragment('}}}', '}\n}\n}');
test_fragment('<!--\nvoid();\n// -->');
// incomplete regexp
test_fragment('a=/regexp', 'a = /regexp');
bt('{a:#1=[],b:#1#,c:#999999#}', '{\n a: #1=[],\n b: #1#,\n c: #999999#\n}');
bt("a = 1e+2");
bt("a = 1e-2");
bt("do{x()}while(a>1)", "do {\n x()\n} while (a > 1)");
bt("x(); /reg/exp.match(something)", "x();\n/reg/exp.match(something)");
test_fragment("something();(", "something();\n(");
test_fragment("#!she/bangs, she bangs\nf=1", "#!she/bangs, she bangs\n\nf = 1");
test_fragment("#!she/bangs, she bangs\n\nf=1", "#!she/bangs, she bangs\n\nf = 1");
test_fragment("#!she/bangs, she bangs\n\n/* comment */", "#!she/bangs, she bangs\n\n/* comment */");
test_fragment("#!she/bangs, she bangs\n\n\n/* comment */", "#!she/bangs, she bangs\n\n\n/* comment */");
test_fragment("#", "#");
test_fragment("#!", "#!");
bt("function namespace::something()");
test_fragment("<!--\nsomething();\n-->", "<!--\nsomething();\n-->");
test_fragment("<!--\nif(i<0){bla();}\n-->", "<!--\nif (i < 0) {\n bla();\n}\n-->");
bt('a = 1e+2');
bt('a = 1e-2');
bt('do{x()}while(a>1)', 'do {\n x()\n} while (a > 1)');
bt('x(); /reg/exp.match(something)', 'x();\n/reg/exp.match(something)');
test_fragment('something();(', 'something();\n(');
test_fragment('#!she/bangs, she bangs\nf=1', '#!she/bangs, she bangs\n\nf = 1');
test_fragment('#!she/bangs, she bangs\n\nf=1', '#!she/bangs, she bangs\n\nf = 1');
test_fragment('#!she/bangs, she bangs\n\n/* comment */');
test_fragment('#!she/bangs, she bangs\n\n\n/* comment */');
test_fragment('#');
test_fragment('#!');
bt('function namespace::something()');
test_fragment('<!--\nsomething();\n-->');
test_fragment('<!--\nif(i<0){bla();}\n-->', '<!--\nif (i < 0) {\n bla();\n}\n-->');
bt('{foo();--bar;}', '{\n foo();\n --bar;\n}');
bt('{foo();++bar;}', '{\n foo();\n ++bar;\n}');
bt('{--bar;}', '{\n --bar;\n}');
bt('{++bar;}', '{\n ++bar;\n}');
bt('if(true)++a;','if (true) ++a;');
bt('if(true)\n++a;','if (true)\n ++a;');
bt('if(true)--a;','if (true) --a;');
bt('if(true)\n--a;','if (true)\n --a;');
bt('if(true)++a;', 'if (true) ++a;');
bt('if(true)\n++a;', 'if (true)\n ++a;');
bt('if(true)--a;', 'if (true) --a;');
bt('if(true)\n--a;', 'if (true)\n --a;');
// Handling of newlines around unary ++ and -- operators
bt('{foo\n++bar;}', '{\n foo\n ++bar;\n}');
bt('{foo++\nbar;}', '{\n foo++\n bar;\n}');
// This is invalid, but harder to guard against. Issue #203.
bt('{foo\n++\nbar;}', '{\n foo\n ++\n bar;\n}');
// regexps
bt('a(/abc\\/\\/def/);b()', "a(/abc\\/\\/def/);\nb()");
bt('a(/a[b\\[\\]c]d/);b()', "a(/a[b\\[\\]c]d/);\nb()");
test_fragment('a(/a[b\\[', "a(/a[b\\["); // incomplete char class
bt('a(/abc\\/\\/def/);b()', 'a(/abc\\/\\/def/);\nb()');
bt('a(/a[b\\[\\]c]d/);b()', 'a(/a[b\\[\\]c]d/);\nb()');
// incomplete char class
test_fragment('a(/a[b\\[');
// allow unescaped / in char classes
bt('a(/[a/b]/);b()', "a(/[a/b]/);\nb()");
bt('a(/[a/b]/);b()', 'a(/[a/b]/);\nb()');
bt('typeof /foo\\//;');
bt('yield /foo\\//;');
bt('throw /foo\\//;');
@ -510,29 +503,21 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
bt('return /foo\\//;');
bt('switch (a) {\n case /foo\\//:\n b\n}');
bt('if (a) /foo\\//\nelse /foo\\//;');
bt('if (foo) /regex/.test();');
bt('function foo() {\n return [\n "one",\n "two"\n ];\n}');
bt('a=[[1,2],[4,5],[7,8]]', "a = [\n [1, 2],\n [4, 5],\n [7, 8]\n]");
bt('a=[[1,2],[4,5],function(){},[7,8]]',
"a = [\n [1, 2],\n [4, 5],\n function() {},\n [7, 8]\n]");
bt('a=[[1,2],[4,5],function(){},function(){},[7,8]]',
"a = [\n [1, 2],\n [4, 5],\n function() {},\n function() {},\n [7, 8]\n]");
bt('a=[[1,2],[4,5],function(){},[7,8]]',
"a = [\n [1, 2],\n [4, 5],\n function() {},\n [7, 8]\n]");
bt('a=[b,c,function(){},function(){},d]',
"a = [b, c, function() {}, function() {}, d]");
bt('a=[b,c,\nfunction(){},function(){},d]',
"a = [b, c,\n function() {},\n function() {},\n d\n]");
bt('a=[a[1],b[4],c[d[7]]]', "a = [a[1], b[4], c[d[7]]]");
bt('[1,2,[3,4,[5,6],7],8]', "[1, 2, [3, 4, [5, 6], 7], 8]");
bt('[[["1","2"],["3","4"]],[["5","6","7"],["8","9","0"]],[["1","2","3"],["4","5","6","7"],["8","9","0"]]]',
'[\n [\n ["1", "2"],\n ["3", "4"]\n ],\n [\n ["5", "6", "7"],\n ["8", "9", "0"]\n ],\n [\n ["1", "2", "3"],\n ["4", "5", "6", "7"],\n ["8", "9", "0"]\n ]\n]');
bt('a=[[1,2],[4,5],[7,8]]', 'a = [\n [1, 2],\n [4, 5],\n [7, 8]\n]');
bt('a=[[1,2],[4,5],function(){},[7,8]]', 'a = [\n [1, 2],\n [4, 5],\n function() {},\n [7, 8]\n]');
bt('a=[[1,2],[4,5],function(){},function(){},[7,8]]', 'a = [\n [1, 2],\n [4, 5],\n function() {},\n function() {},\n [7, 8]\n]');
bt('a=[[1,2],[4,5],function(){},[7,8]]', 'a = [\n [1, 2],\n [4, 5],\n function() {},\n [7, 8]\n]');
bt('a=[b,c,function(){},function(){},d]', 'a = [b, c, function() {}, function() {}, d]');
bt('a=[b,c,\nfunction(){},function(){},d]', 'a = [b, c,\n function() {},\n function() {},\n d\n]');
bt('a=[a[1],b[4],c[d[7]]]', 'a = [a[1], b[4], c[d[7]]]');
bt('[1,2,[3,4,[5,6],7],8]', '[1, 2, [3, 4, [5, 6], 7], 8]');
bt('[[["1","2"],["3","4"]],[["5","6","7"],["8","9","0"]],[["1","2","3"],["4","5","6","7"],["8","9","0"]]]', '[\n [\n ["1", "2"],\n ["3", "4"]\n ],\n [\n ["5", "6", "7"],\n ["8", "9", "0"]\n ],\n [\n ["1", "2", "3"],\n ["4", "5", "6", "7"],\n ["8", "9", "0"]\n ]\n]');
bt('{[x()[0]];indent;}', '{\n [x()[0]];\n indent;\n}');
bt('{{}/z/}', "{\n {}\n /z/\n}");
bt('return ++i', 'return ++i');
bt('return !!x', 'return !!x');
bt('return !x', 'return !x');

View File

@ -55,7 +55,10 @@ class TestJSBeautifier(unittest.TestCase):
# Unicode Support
bt('var ' + unicode_char(3232) + '_' + unicode_char(3232) + ' = "hi";')
bt('var ' + unicode_char(228) + 'x = {\n ' + unicode_char(228) + 'rgerlich: true\n};')
bt(
'var ' + unicode_char(228) + 'x = {\n' +
' ' + unicode_char(228) + 'rgerlich: true\n' +
'};')
# End With Newline - (eof = "\n")
self.options.end_with_newline = true
@ -179,150 +182,140 @@ class TestJSBeautifier(unittest.TestCase):
bt('a={1:[-1],2:[+1]}', 'a = {\n 1: [-1],\n 2: [+1]\n}')
bt('var l = {\'a\':\'1\', \'b\':\'2\'}', 'var l = {\n \'a\': \'1\',\n \'b\': \'2\'\n}')
bt('if (template.user[n] in bk) foo();')
bt('{{}/z/}', "{\n {}\n /z/\n}")
bt('return 45', "return 45")
bt('return 45')
bt('return this.prevObject ||\n\n this.constructor(null);')
bt('If[1]', "If[1]")
bt('Then[1]', "Then[1]")
bt('a = 1e10', "a = 1e10")
bt('a = 1.3e10', "a = 1.3e10")
bt('a = 1.3e-10', "a = 1.3e-10")
bt('a = -1.3e-10', "a = -1.3e-10")
bt('a = 1e-10', "a = 1e-10")
bt('a = e - 10', "a = e - 10")
bt('a = 11-10', "a = 11 - 10")
bt("a = 1;// comment", "a = 1; // comment")
bt("a = 1; // comment", "a = 1; // comment")
bt("a = 1;\n // comment", "a = 1;\n// comment")
bt('If[1]')
bt('Then[1]')
bt('a = 1e10')
bt('a = 1.3e10')
bt('a = 1.3e-10')
bt('a = -1.3e-10')
bt('a = 1e-10')
bt('a = e - 10')
bt('a = 11-10', 'a = 11 - 10')
bt('a = 1;// comment', 'a = 1; // comment')
bt('a = 1; // comment')
bt('a = 1;\n // comment', 'a = 1;\n// comment')
bt('a = [-1, -1, -1]')
# The exact formatting these should have is open for discussion, but they are at least reasonable
bt('a = [ // comment\n -1, -1, -1\n]')
bt('var a = [ // comment\n -1, -1, -1\n]')
bt('a = [ // comment\n -1, // comment\n -1, -1\n]')
bt('var a = [ // comment\n -1, // comment\n -1, -1\n]')
bt('o = [{a:b},{c:d}]', 'o = [{\n a: b\n}, {\n c: d\n}]')
bt("if (a) {\n do();\n}") # was: extra space appended
bt("if (a) {\n// comment\n}else{\n// comment\n}", "if (a) {\n // comment\n} else {\n // comment\n}") # if/else statement with empty body
bt("if (a) {\n// comment\n// comment\n}", "if (a) {\n // comment\n // comment\n}") # multiple comments indentation
bt("if (a) b() else c();", "if (a) b()\nelse c();")
bt("if (a) b() else if c() d();", "if (a) b()\nelse if c() d();")
bt("{}")
bt("{\n\n}")
bt("do { a(); } while ( 1 );", "do {\n a();\n} while (1);")
bt("do {} while (1);")
bt("do {\n} while (1);", "do {} while (1);")
bt("do {\n\n} while (1);")
bt("var a = x(a, b, c)")
bt("delete x if (a) b();", "delete x\nif (a) b();")
bt("delete x[x] if (a) b();", "delete x[x]\nif (a) b();")
bt("for(var a=1,b=2)d", "for (var a = 1, b = 2) d")
bt("for(var a=1,b=2,c=3) d", "for (var a = 1, b = 2, c = 3) d")
bt("for(var a=1,b=2,c=3;d<3;d++)\ne", "for (var a = 1, b = 2, c = 3; d < 3; d++)\n e")
bt("function x(){(a||b).c()}", "function x() {\n (a || b).c()\n}")
bt("function x(){return - 1}", "function x() {\n return -1\n}")
bt("function x(){return ! a}", "function x() {\n return !a\n}")
bt("x => x", "x => x")
bt("(x) => x", "(x) => x")
bt("x => { x }", "x => {\n x\n}")
bt("(x) => { x }", "(x) => {\n x\n}")
# was: extra space appended
bt('if (a) {\n do();\n}')
# if/else statement with empty body
bt('if (a) {\n// comment\n}else{\n// comment\n}', 'if (a) {\n // comment\n} else {\n // comment\n}')
# multiple comments indentation
bt('if (a) {\n// comment\n// comment\n}', 'if (a) {\n // comment\n // comment\n}')
bt('if (a) b() else c();', 'if (a) b()\nelse c();')
bt('if (a) b() else if c() d();', 'if (a) b()\nelse if c() d();')
bt('{}')
bt('{\n\n}')
bt('do { a(); } while ( 1 );', 'do {\n a();\n} while (1);')
bt('do {} while (1);')
bt('do {\n} while (1);', 'do {} while (1);')
bt('do {\n\n} while (1);')
bt('var a = x(a, b, c)')
bt('delete x if (a) b();', 'delete x\nif (a) b();')
bt('delete x[x] if (a) b();', 'delete x[x]\nif (a) b();')
bt('for(var a=1,b=2)d', 'for (var a = 1, b = 2) d')
bt('for(var a=1,b=2,c=3) d', 'for (var a = 1, b = 2, c = 3) d')
bt('for(var a=1,b=2,c=3;d<3;d++)\ne', 'for (var a = 1, b = 2, c = 3; d < 3; d++)\n e')
bt('function x(){(a||b).c()}', 'function x() {\n (a || b).c()\n}')
bt('function x(){return - 1}', 'function x() {\n return -1\n}')
bt('function x(){return ! a}', 'function x() {\n return !a\n}')
bt('x => x')
bt('(x) => x')
bt('x => { x }', 'x => {\n x\n}')
bt('(x) => { x }', '(x) => {\n x\n}')
# a common snippet in jQuery plugins
bt("settings = $.extend({},defaults,settings);", "settings = $.extend({}, defaults, settings);")
# reserved words used as property names
bt("$http().then().finally().default()", "$http().then().finally().default()")
bt("$http()\n.then()\n.finally()\n.default()", "$http()\n .then()\n .finally()\n .default()")
bt("$http().when.in.new.catch().throw()", "$http().when.in.new.catch().throw()")
bt("$http()\n.when\n.in\n.new\n.catch()\n.throw()", "$http()\n .when\n .in\n .new\n .catch()\n .throw()")
bt('settings = $.extend({},defaults,settings);', 'settings = $.extend({}, defaults, settings);')
bt('$http().then().finally().default()')
bt('$http()\n.then()\n.finally()\n.default()', '$http()\n .then()\n .finally()\n .default()')
bt('$http().when.in.new.catch().throw()')
bt('$http()\n.when\n.in\n.new\n.catch()\n.throw()', '$http()\n .when\n .in\n .new\n .catch()\n .throw()')
bt('{xxx;}()', '{\n xxx;\n}()')
bt("a = 'a'\nb = 'b'")
bt("a = /reg/exp")
bt("a = /reg/")
bt('a = \'a\'\nb = \'b\'')
bt('a = /reg/exp')
bt('a = /reg/')
bt('/abc/.test()')
bt('/abc/i.test()')
bt("{/abc/i.test()}", "{\n /abc/i.test()\n}")
bt('{/abc/i.test()}', '{\n /abc/i.test()\n}')
bt('var x=(a)/a;', 'var x = (a) / a;')
bt('x != -1', 'x != -1')
bt('x != -1')
bt('for (; s-->0;)t', 'for (; s-- > 0;) t')
bt('for (; s++>0;)u', 'for (; s++ > 0;) u')
bt('a = s++>s--;', 'a = s++ > s--;')
bt('a = s++>--s;', 'a = s++ > --s;')
bt('{x=#1=[]}', '{\n x = #1=[]\n}')
bt('{a:#1={}}', '{\n a: #1={}\n}')
bt('{a:#1#}', '{\n a: #1#\n}')
test_fragment('"incomplete-string')
test_fragment("'incomplete-string")
test_fragment('\'incomplete-string')
test_fragment('/incomplete-regex')
test_fragment('`incomplete-regex')
test_fragment('`incomplete-template-string')
test_fragment('{a:1},{a:2}', '{\n a: 1\n}, {\n a: 2\n}')
test_fragment('var ary=[{a:1}, {a:2}];', 'var ary = [{\n a: 1\n}, {\n a: 2\n}];')
test_fragment('{a:#1', '{\n a: #1') # incomplete
test_fragment('{a:#', '{\n a: #') # incomplete
test_fragment('}}}', '}\n}\n}') # incomplete
test_fragment('<!--\nvoid();\n// -->', '<!--\nvoid();\n// -->')
test_fragment('a=/regexp', 'a = /regexp') # incomplete regexp
# incomplete
test_fragment('{a:#1', '{\n a: #1')
# incomplete
test_fragment('{a:#', '{\n a: #')
# incomplete
test_fragment('}}}', '}\n}\n}')
test_fragment('<!--\nvoid();\n// -->')
# incomplete regexp
test_fragment('a=/regexp', 'a = /regexp')
bt('{a:#1=[],b:#1#,c:#999999#}', '{\n a: #1=[],\n b: #1#,\n c: #999999#\n}')
bt("a = 1e+2")
bt("a = 1e-2")
bt("do{x()}while(a>1)", "do {\n x()\n} while (a > 1)")
bt("x(); /reg/exp.match(something)", "x();\n/reg/exp.match(something)")
test_fragment("something();(", "something();\n(")
test_fragment("#!she/bangs, she bangs\nf=1", "#!she/bangs, she bangs\n\nf = 1")
test_fragment("#!she/bangs, she bangs\n\nf=1", "#!she/bangs, she bangs\n\nf = 1")
test_fragment("#!she/bangs, she bangs\n\n/* comment */", "#!she/bangs, she bangs\n\n/* comment */")
test_fragment("#!she/bangs, she bangs\n\n\n/* comment */", "#!she/bangs, she bangs\n\n\n/* comment */")
test_fragment("#", "#")
test_fragment("#!", "#!")
bt("function namespace::something()")
test_fragment("<!--\nsomething();\n-->", "<!--\nsomething();\n-->")
test_fragment("<!--\nif(i<0){bla();}\n-->", "<!--\nif (i < 0) {\n bla();\n}\n-->")
bt('a = 1e+2')
bt('a = 1e-2')
bt('do{x()}while(a>1)', 'do {\n x()\n} while (a > 1)')
bt('x(); /reg/exp.match(something)', 'x();\n/reg/exp.match(something)')
test_fragment('something();(', 'something();\n(')
test_fragment('#!she/bangs, she bangs\nf=1', '#!she/bangs, she bangs\n\nf = 1')
test_fragment('#!she/bangs, she bangs\n\nf=1', '#!she/bangs, she bangs\n\nf = 1')
test_fragment('#!she/bangs, she bangs\n\n/* comment */')
test_fragment('#!she/bangs, she bangs\n\n\n/* comment */')
test_fragment('#')
test_fragment('#!')
bt('function namespace::something()')
test_fragment('<!--\nsomething();\n-->')
test_fragment('<!--\nif(i<0){bla();}\n-->', '<!--\nif (i < 0) {\n bla();\n}\n-->')
bt('{foo();--bar;}', '{\n foo();\n --bar;\n}')
bt('{foo();++bar;}', '{\n foo();\n ++bar;\n}')
bt('{--bar;}', '{\n --bar;\n}')
bt('{++bar;}', '{\n ++bar;\n}')
bt('if(true)++a;','if (true) ++a;')
bt('if(true)\n++a;','if (true)\n ++a;')
bt('if(true)--a;','if (true) --a;')
bt('if(true)\n--a;','if (true)\n --a;')
bt('if(true)++a;', 'if (true) ++a;')
bt('if(true)\n++a;', 'if (true)\n ++a;')
bt('if(true)--a;', 'if (true) --a;')
bt('if(true)\n--a;', 'if (true)\n --a;')
# Handling of newlines around unary ++ and -- operators
bt('{foo\n++bar;}', '{\n foo\n ++bar;\n}')
bt('{foo++\nbar;}', '{\n foo++\n bar;\n}')
# This is invalid, but harder to guard against. Issue #203.
bt('{foo\n++\nbar;}', '{\n foo\n ++\n bar;\n}')
# regexps
bt('a(/abc\\/\\/def/);b()', "a(/abc\\/\\/def/);\nb()")
bt('a(/a[b\\[\\]c]d/);b()', "a(/a[b\\[\\]c]d/);\nb()")
test_fragment('a(/a[b\\[', "a(/a[b\\[") # incomplete char class
bt('a(/abc\\/\\/def/);b()', 'a(/abc\\/\\/def/);\nb()')
bt('a(/a[b\\[\\]c]d/);b()', 'a(/a[b\\[\\]c]d/);\nb()')
# incomplete char class
test_fragment('a(/a[b\\[')
# allow unescaped / in char classes
bt('a(/[a/b]/);b()', "a(/[a/b]/);\nb()")
bt('a(/[a/b]/);b()', 'a(/[a/b]/);\nb()')
bt('typeof /foo\\//;')
bt('yield /foo\\//;')
bt('throw /foo\\//;')
@ -330,29 +323,20 @@ class TestJSBeautifier(unittest.TestCase):
bt('return /foo\\//;')
bt('switch (a) {\n case /foo\\//:\n b\n}')
bt('if (a) /foo\\//\nelse /foo\\//;')
bt('if (foo) /regex/.test();')
bt('function foo() {\n return [\n "one",\n "two"\n ];\n}')
bt('a=[[1,2],[4,5],[7,8]]', "a = [\n [1, 2],\n [4, 5],\n [7, 8]\n]")
bt('a=[[1,2],[4,5],function(){},[7,8]]',
"a = [\n [1, 2],\n [4, 5],\n function() {},\n [7, 8]\n]")
bt('a=[[1,2],[4,5],function(){},function(){},[7,8]]',
"a = [\n [1, 2],\n [4, 5],\n function() {},\n function() {},\n [7, 8]\n]")
bt('a=[[1,2],[4,5],function(){},[7,8]]',
"a = [\n [1, 2],\n [4, 5],\n function() {},\n [7, 8]\n]")
bt('a=[b,c,function(){},function(){},d]',
"a = [b, c, function() {}, function() {}, d]")
bt('a=[b,c,\nfunction(){},function(){},d]',
"a = [b, c,\n function() {},\n function() {},\n d\n]")
bt('a=[a[1],b[4],c[d[7]]]', "a = [a[1], b[4], c[d[7]]]")
bt('[1,2,[3,4,[5,6],7],8]', "[1, 2, [3, 4, [5, 6], 7], 8]")
bt('[[["1","2"],["3","4"]],[["5","6","7"],["8","9","0"]],[["1","2","3"],["4","5","6","7"],["8","9","0"]]]',
'[\n [\n ["1", "2"],\n ["3", "4"]\n ],\n [\n ["5", "6", "7"],\n ["8", "9", "0"]\n ],\n [\n ["1", "2", "3"],\n ["4", "5", "6", "7"],\n ["8", "9", "0"]\n ]\n]')
bt('a=[[1,2],[4,5],[7,8]]', 'a = [\n [1, 2],\n [4, 5],\n [7, 8]\n]')
bt('a=[[1,2],[4,5],function(){},[7,8]]', 'a = [\n [1, 2],\n [4, 5],\n function() {},\n [7, 8]\n]')
bt('a=[[1,2],[4,5],function(){},function(){},[7,8]]', 'a = [\n [1, 2],\n [4, 5],\n function() {},\n function() {},\n [7, 8]\n]')
bt('a=[[1,2],[4,5],function(){},[7,8]]', 'a = [\n [1, 2],\n [4, 5],\n function() {},\n [7, 8]\n]')
bt('a=[b,c,function(){},function(){},d]', 'a = [b, c, function() {}, function() {}, d]')
bt('a=[b,c,\nfunction(){},function(){},d]', 'a = [b, c,\n function() {},\n function() {},\n d\n]')
bt('a=[a[1],b[4],c[d[7]]]', 'a = [a[1], b[4], c[d[7]]]')
bt('[1,2,[3,4,[5,6],7],8]', '[1, 2, [3, 4, [5, 6], 7], 8]')
bt('[[["1","2"],["3","4"]],[["5","6","7"],["8","9","0"]],[["1","2","3"],["4","5","6","7"],["8","9","0"]]]', '[\n [\n ["1", "2"],\n ["3", "4"]\n ],\n [\n ["5", "6", "7"],\n ["8", "9", "0"]\n ],\n [\n ["1", "2", "3"],\n ["4", "5", "6", "7"],\n ["8", "9", "0"]\n ]\n]')
bt('{[x()[0]];indent;}', '{\n [x()[0]];\n indent;\n}')
bt('{{}/z/}', "{\n {}\n /z/\n}")
bt('return ++i', 'return ++i')
bt('return !!x', 'return !!x')
bt('return !x', 'return !x')

View File

@ -11,8 +11,15 @@ exports.test_data = {
name: "Unicode Support",
description: "",
tests: [
{ input: "var ' + unicode_char(3232) + '_' + unicode_char(3232) + ' = \"hi\";" },
{ input: "var ' + unicode_char(228) + 'x = {\n ' + unicode_char(228) + 'rgerlich: true\n};" }
{
input: "var ' + unicode_char(3232) + '_' + unicode_char(3232) + ' = \"hi\";"
},
{
input: [
"var ' + unicode_char(228) + 'x = {",
" ' + unicode_char(228) + 'rgerlich: true",
"};"]
}
],
}, {
name: "End With Newline",
@ -143,7 +150,186 @@ exports.test_data = {
{ input: '{a:1, b:2}', output: "{\n a: 1,\n b: 2\n}" },
{ input: 'a={1:[-1],2:[+1]}', output: 'a = {\n 1: [-1],\n 2: [+1]\n}' },
{ input: "var l = {\\'a\\':\\'1\\', \\'b\\':\\'2\\'}", output: "var l = {\n \\'a\\': \\'1\\',\n \\'b\\': \\'2\\'\n}" },
{ input: 'if (template.user[n] in bk) foo();' }
{ input: 'if (template.user[n] in bk) foo();' },
{ input: 'return 45', output: "return 45" },
{ input: 'return this.prevObject ||\n\n this.constructor(null);' },
{ input: 'If[1]', output: "If[1]" },
{ input: 'Then[1]', output: "Then[1]" },
{ input: 'a = 1e10', output: "a = 1e10" },
{ input: 'a = 1.3e10', output: "a = 1.3e10" },
{ input: 'a = 1.3e-10', output: "a = 1.3e-10" },
{ input: 'a = -1.3e-10', output: "a = -1.3e-10" },
{ input: 'a = 1e-10', output: "a = 1e-10" },
{ input: 'a = e - 10', output: "a = e - 10" },
{ input: 'a = 11-10', output: "a = 11 - 10" },
{ input: "a = 1;// comment", output: "a = 1; // comment" },
{ input: "a = 1; // comment", output: "a = 1; // comment" },
{ input: "a = 1;\n // comment", output: "a = 1;\n// comment" },
{ input: 'a = [-1, -1, -1]' },
{ comment: 'The exact formatting these should have is open for discussion, but they are at least reasonable',
input: 'a = [ // comment\n -1, -1, -1\n]' },
{ input: 'var a = [ // comment\n -1, -1, -1\n]' },
{ input: 'a = [ // comment\n -1, // comment\n -1, -1\n]' },
{ input: 'var a = [ // comment\n -1, // comment\n -1, -1\n]' },
{ input: 'o = [{a:b},{c:d}]', output: 'o = [{\n a: b\n}, {\n c: d\n}]' },
{ comment: 'was: extra space appended',
input: "if (a) {\n do();\n}" },
{ comment: 'if/else statement with empty body',
input: "if (a) {\n// comment\n}else{\n// comment\n}", output: "if (a) {\n // comment\n} else {\n // comment\n}" },
{ comment: 'multiple comments indentation', input: "if (a) {\n// comment\n// comment\n}", output: "if (a) {\n // comment\n // comment\n}" },
{ input: "if (a) b() else c();", output: "if (a) b()\nelse c();" },
{ input: "if (a) b() else if c() d();", output: "if (a) b()\nelse if c() d();" },
{ input: "{}" },
{ input: "{\n\n}" },
{ input: "do { a(); } while ( 1 );", output: "do {\n a();\n} while (1);" },
{ input: "do {} while (1);" },
{ input: "do {\n} while (1);", output: "do {} while (1);" },
{ input: "do {\n\n} while (1);" },
{ input: "var a = x(a, b, c)" },
{ input: "delete x if (a) b();", output: "delete x\nif (a) b();" },
{ input: "delete x[x] if (a) b();", output: "delete x[x]\nif (a) b();" },
{ input: "for(var a=1,b=2)d", output: "for (var a = 1, b = 2) d" },
{ input: "for(var a=1,b=2,c=3) d", output: "for (var a = 1, b = 2, c = 3) d" },
{ input: "for(var a=1,b=2,c=3;d<3;d++)\ne", output: "for (var a = 1, b = 2, c = 3; d < 3; d++)\n e" },
{ input: "function x(){(a||b).c()}", output: "function x() {\n (a || b).c()\n}" },
{ input: "function x(){return - 1}", output: "function x() {\n return -1\n}" },
{ input: "function x(){return ! a}", output: "function x() {\n return !a\n}" },
{ input: "x => x", output: "x => x" },
{ input: "(x) => x", output: "(x) => x" },
{ input: "x => { x }", output: "x => {\n x\n}" },
{ input: "(x) => { x }", output: "(x) => {\n x\n}" },
{ comment: 'a common snippet in jQuery plugins',
input: "settings = $.extend({},defaults,settings);",
output: "settings = $.extend({}, defaults, settings);" },
// reserved words used as property names
{ input: "$http().then().finally().default()", output: "$http().then().finally().default()" },
{ input: "$http()\n.then()\n.finally()\n.default()", output: "$http()\n .then()\n .finally()\n .default()" },
{ input: "$http().when.in.new.catch().throw()", output: "$http().when.in.new.catch().throw()" },
{ input: "$http()\n.when\n.in\n.new\n.catch()\n.throw()", output: "$http()\n .when\n .in\n .new\n .catch()\n .throw()" },
{ input: '{xxx;}()', output: '{\n xxx;\n}()' },
{ input: "a = \\'a\\'\nb = \\'b\\'" },
{ input: "a = /reg/exp" },
{ input: "a = /reg/" },
{ input: '/abc/.test()' },
{ input: '/abc/i.test()' },
{ input: "{/abc/i.test()}", output: "{\n /abc/i.test()\n}" },
{ input: 'var x=(a)/a;', output: 'var x = (a) / a;' },
{ input: 'x != -1', output: 'x != -1' },
{ input: 'for (; s-->0;)t', output: 'for (; s-- > 0;) t' },
{ input: 'for (; s++>0;)u', output: 'for (; s++ > 0;) u' },
{ input: 'a = s++>s--;', output: 'a = s++ > s--;' },
{ input: 'a = s++>--s;', output: 'a = s++ > --s;' },
{ input: '{x=#1=[]}', output: '{\n x = #1=[]\n}' },
{ input: '{a:#1={}}', output: '{\n a: #1={}\n}' },
{ input: '{a:#1#}', output: '{\n a: #1#\n}' },
{ fragment: '"incomplete-string' },
{ fragment: "\\'incomplete-string" },
{ fragment: '/incomplete-regex' },
{ fragment: '`incomplete-template-string' },
{ fragment: '{a:1},{a:2}', output: '{\n a: 1\n}, {\n a: 2\n}' },
{ fragment: 'var ary=[{a:1}, {a:2}];', output: 'var ary = [{\n a: 1\n}, {\n a: 2\n}];' },
{ comment: 'incomplete', fragment: '{a:#1', output: '{\n a: #1' },
{ comment: 'incomplete', fragment: '{a:#', output: '{\n a: #' },
{ comment: 'incomplete', fragment: '}}}', output: '}\n}\n}' },
{ fragment: '<!--\nvoid();\n// -->', output: '<!--\nvoid();\n// -->' },
{ comment: 'incomplete regexp', fragment: 'a=/regexp', output: 'a = /regexp' },
{ input: '{a:#1=[],b:#1#,c:#999999#}', output: '{\n a: #1=[],\n b: #1#,\n c: #999999#\n}' },
{ input: "a = 1e+2" },
{ input: "a = 1e-2" },
{ input: "do{x()}while(a>1)", output: "do {\n x()\n} while (a > 1)" },
{ input: "x(); /reg/exp.match(something)", output: "x();\n/reg/exp.match(something)" },
{ fragment: "something();(", output: "something();\n(" },
{ fragment: "#!she/bangs, she bangs\nf=1", output: "#!she/bangs, she bangs\n\nf = 1" },
{ fragment: "#!she/bangs, she bangs\n\nf=1", output: "#!she/bangs, she bangs\n\nf = 1" },
{ fragment: "#!she/bangs, she bangs\n\n/* comment */", output: "#!she/bangs, she bangs\n\n/* comment */" },
{ fragment: "#!she/bangs, she bangs\n\n\n/* comment */", output: "#!she/bangs, she bangs\n\n\n/* comment */" },
{ fragment: "#", output: "#" },
{ fragment: "#!", output: "#!" },
{ input: "function namespace::something()" },
{ fragment: "<!--\nsomething();\n-->", output: "<!--\nsomething();\n-->" },
{ fragment: "<!--\nif(i<0){bla();}\n-->", output: "<!--\nif (i < 0) {\n bla();\n}\n-->" },
{ input: '{foo();--bar;}', output: '{\n foo();\n --bar;\n}' },
{ input: '{foo();++bar;}', output: '{\n foo();\n ++bar;\n}' },
{ input: '{--bar;}', output: '{\n --bar;\n}' },
{ input: '{++bar;}', output: '{\n ++bar;\n}' },
{ input: 'if(true)++a;', output: 'if (true) ++a;' },
{ input: 'if(true)\n++a;', output: 'if (true)\n ++a;' },
{ input: 'if(true)--a;', output: 'if (true) --a;' },
{ input: 'if(true)\n--a;', output: 'if (true)\n --a;' },
{ comment: 'Handling of newlines around unary ++ and -- operators',
input: '{foo\n++bar;}', output: '{\n foo\n ++bar;\n}' },
{ input: '{foo++\nbar;}', output: '{\n foo++\n bar;\n}' },
{ comment: 'This is invalid, but harder to guard against. Issue #203.',
input: '{foo\n++\nbar;}', output: '{\n foo\n ++\n bar;\n}' },
{ comment: 'regexps',
input: 'a(/abc\\\\/\\\\/def/);b()', output: "a(/abc\\\\/\\\\/def/);\nb()" },
{ input: 'a(/a[b\\\\[\\\\]c]d/);b()', output: "a(/a[b\\\\[\\\\]c]d/);\nb()" },
{ comment: 'incomplete char class', fragment: 'a(/a[b\\\\[', output: "a(/a[b\\\\[" },
{ comment: 'allow unescaped / in char classes',
input: 'a(/[a/b]/);b()', output: "a(/[a/b]/);\nb()" },
{ input: 'typeof /foo\\\\//;' },
{ input: 'yield /foo\\\\//;' },
{ input: 'throw /foo\\\\//;' },
{ input: 'do /foo\\\\//;' },
{ input: 'return /foo\\\\//;' },
{ input: 'switch (a) {\n case /foo\\\\//:\n b\n}' },
{ input: 'if (a) /foo\\\\//\nelse /foo\\\\//;' },
{ input: 'if (foo) /regex/.test();' },
{ input: 'function foo() {\n return [\n "one",\n "two"\n ];\n}' },
{ input: 'a=[[1,2],[4,5],[7,8]]', output: "a = [\n [1, 2],\n [4, 5],\n [7, 8]\n]" },
{ input: 'a=[[1,2],[4,5],function(){},[7,8]]',
output: "a = [\n [1, 2],\n [4, 5],\n function() {},\n [7, 8]\n]" },
{ input: 'a=[[1,2],[4,5],function(){},function(){},[7,8]]',
output: "a = [\n [1, 2],\n [4, 5],\n function() {},\n function() {},\n [7, 8]\n]" },
{ input: 'a=[[1,2],[4,5],function(){},[7,8]]',
output: "a = [\n [1, 2],\n [4, 5],\n function() {},\n [7, 8]\n]" },
{ input: 'a=[b,c,function(){},function(){},d]',
output: "a = [b, c, function() {}, function() {}, d]" },
{ input: 'a=[b,c,\nfunction(){},function(){},d]',
output: "a = [b, c,\n function() {},\n function() {},\n d\n]" },
{ input: 'a=[a[1],b[4],c[d[7]]]', output: "a = [a[1], b[4], c[d[7]]]" },
{ input: '[1,2,[3,4,[5,6],7],8]', output: "[1, 2, [3, 4, [5, 6], 7], 8]" },
{ input: '[[["1","2"],["3","4"]],[["5","6","7"],["8","9","0"]],[["1","2","3"],["4","5","6","7"],["8","9","0"]]]',
output: '[\n [\n ["1", "2"],\n ["3", "4"]\n ],\n [\n ["5", "6", "7"],\n ["8", "9", "0"]\n ],\n [\n ["1", "2", "3"],\n ["4", "5", "6", "7"],\n ["8", "9", "0"]\n ]\n]' },
{ input: '{[x()[0]];indent;}', output: '{\n [x()[0]];\n indent;\n}' }
],
}],
// Example

View File

@ -61,7 +61,7 @@ function set_formatters (data, test_method, comment_mark) {
var comment = "";
if (typeof this.comment === "string") {
comment = "\n " + comment_mark + this.comment + '\n ';
} else if (this.input instanceof Array) {
} else if (this.comment instanceof Array) {
comment = "\n " + comment_mark + this.comment.join('\n ' + comment_mark);
}
@ -69,17 +69,17 @@ function set_formatters (data, test_method, comment_mark) {
var before_input = "";
if (typeof this.input === "string") {
before_input = test_method + "(";
input = "'" + this.input.replace(/\n/g,'\\n') + "'";
input = "'" + this.input.replace(/\n/g,'\\n').replace(/\t/g,'\\t') + "'";
} else if (this.input instanceof Array) {
before_input = test_method + "(\n '";
input = "'" + this.input.join("\\n' +\n '") + "'";
before_input = test_method + "(\n ";
input = "'" + this.input.join("\\n' +\n '").replace(/\t/g,'\\t') + "'";
} else if (typeof this.fragment === "string") {
before_input = "test_fragment(";
input = "'" + this.fragment.replace(/\n/g,'\\n') + "'";
input = "'" + this.fragment.replace(/\n/g,'\\n').replace(/\t/g,'\\t') + "'";
} else if (this.fragment instanceof Array) {
before_input = "test_fragment(\n '";
input = "'" + this.fragment.join("\\n' +\n '") + "'";
before_input = "test_fragment(\n ";
input = "'" + this.fragment.join("\\n' +\n '").replace(/\t/g,'\\t') + "'";
}
input = render(input);
@ -87,10 +87,10 @@ function set_formatters (data, test_method, comment_mark) {
var before_output = "";
if (typeof this.output === "string") {
before_output = ', ';
output = "'" + this.output.replace(/\n/g,'\\n') + "'";
output = "'" + this.output.replace(/\n/g,'\\n').replace(/\t/g,'\\t') + "'";
} else if (this.output instanceof Array) {
before_output = ',\n ';
output = "'" + this.output.join("\\n' +\n '") + "'";
output = "'" + this.output.join("\\n' +\n '").replace(/\t/g,'\\t') + "'";
}
output = render(output);

View File

@ -254,177 +254,6 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
{{=<% %>=}}
bt('{{}/z/}', "{\n {}\n /z/\n}");
<%={{ }}=%>
bt('return 45', "return 45");
bt('return this.prevObject ||\n\n this.constructor(null);');
bt('If[1]', "If[1]");
bt('Then[1]', "Then[1]");
bt('a = 1e10', "a = 1e10");
bt('a = 1.3e10', "a = 1.3e10");
bt('a = 1.3e-10', "a = 1.3e-10");
bt('a = -1.3e-10', "a = -1.3e-10");
bt('a = 1e-10', "a = 1e-10");
bt('a = e - 10', "a = e - 10");
bt('a = 11-10', "a = 11 - 10");
bt("a = 1;// comment", "a = 1; // comment");
bt("a = 1; // comment", "a = 1; // comment");
bt("a = 1;\n // comment", "a = 1;\n// comment");
bt('a = [-1, -1, -1]');
// The exact formatting these should have is open for discussion, but they are at least reasonable
bt('a = [ // comment\n -1, -1, -1\n]');
bt('var a = [ // comment\n -1, -1, -1\n]');
bt('a = [ // comment\n -1, // comment\n -1, -1\n]');
bt('var a = [ // comment\n -1, // comment\n -1, -1\n]');
bt('o = [{a:b},{c:d}]', 'o = [{\n a: b\n}, {\n c: d\n}]');
bt("if (a) {\n do();\n}"); // was: extra space appended
bt("if (a) {\n// comment\n}else{\n// comment\n}", "if (a) {\n // comment\n} else {\n // comment\n}"); // if/else statement with empty body
bt("if (a) {\n// comment\n// comment\n}", "if (a) {\n // comment\n // comment\n}"); // multiple comments indentation
bt("if (a) b() else c();", "if (a) b()\nelse c();");
bt("if (a) b() else if c() d();", "if (a) b()\nelse if c() d();");
bt("{}");
bt("{\n\n}");
bt("do { a(); } while ( 1 );", "do {\n a();\n} while (1);");
bt("do {} while (1);");
bt("do {\n} while (1);", "do {} while (1);");
bt("do {\n\n} while (1);");
bt("var a = x(a, b, c)");
bt("delete x if (a) b();", "delete x\nif (a) b();");
bt("delete x[x] if (a) b();", "delete x[x]\nif (a) b();");
bt("for(var a=1,b=2)d", "for (var a = 1, b = 2) d");
bt("for(var a=1,b=2,c=3) d", "for (var a = 1, b = 2, c = 3) d");
bt("for(var a=1,b=2,c=3;d<3;d++)\ne", "for (var a = 1, b = 2, c = 3; d < 3; d++)\n e");
bt("function x(){(a||b).c()}", "function x() {\n (a || b).c()\n}");
bt("function x(){return - 1}", "function x() {\n return -1\n}");
bt("function x(){return ! a}", "function x() {\n return !a\n}");
bt("x => x", "x => x");
bt("(x) => x", "(x) => x");
bt("x => { x }", "x => {\n x\n}");
bt("(x) => { x }", "(x) => {\n x\n}");
// a common snippet in jQuery plugins
bt("settings = $.extend({},defaults,settings);", "settings = $.extend({}, defaults, settings);");
// reserved words used as property names
bt("$http().then().finally().default()", "$http().then().finally().default()");
bt("$http()\n.then()\n.finally()\n.default()", "$http()\n .then()\n .finally()\n .default()");
bt("$http().when.in.new.catch().throw()", "$http().when.in.new.catch().throw()");
bt("$http()\n.when\n.in\n.new\n.catch()\n.throw()", "$http()\n .when\n .in\n .new\n .catch()\n .throw()");
bt('{xxx;}()', '{\n xxx;\n}()');
bt("a = 'a'\nb = 'b'");
bt("a = /reg/exp");
bt("a = /reg/");
bt('/abc/.test()');
bt('/abc/i.test()');
bt("{/abc/i.test()}", "{\n /abc/i.test()\n}");
bt('var x=(a)/a;', 'var x = (a) / a;');
bt('x != -1', 'x != -1');
bt('for (; s-->0;)t', 'for (; s-- > 0;) t');
bt('for (; s++>0;)u', 'for (; s++ > 0;) u');
bt('a = s++>s--;', 'a = s++ > s--;');
bt('a = s++>--s;', 'a = s++ > --s;');
bt('{x=#1=[]}', '{\n x = #1=[]\n}');
bt('{a:#1={}}', '{\n a: #1={}\n}');
bt('{a:#1#}', '{\n a: #1#\n}');
test_fragment('"incomplete-string');
test_fragment("'incomplete-string");
test_fragment('/incomplete-regex');
test_fragment('`incomplete-template-string');
test_fragment('{a:1},{a:2}', '{\n a: 1\n}, {\n a: 2\n}');
test_fragment('var ary=[{a:1}, {a:2}];', 'var ary = [{\n a: 1\n}, {\n a: 2\n}];');
test_fragment('{a:#1', '{\n a: #1'); // incomplete
test_fragment('{a:#', '{\n a: #'); // incomplete
test_fragment('}}}', '}\n}\n}'); // incomplete
test_fragment('<!--\nvoid();\n// -->', '<!--\nvoid();\n// -->');
test_fragment('a=/regexp', 'a = /regexp'); // incomplete regexp
bt('{a:#1=[],b:#1#,c:#999999#}', '{\n a: #1=[],\n b: #1#,\n c: #999999#\n}');
bt("a = 1e+2");
bt("a = 1e-2");
bt("do{x()}while(a>1)", "do {\n x()\n} while (a > 1)");
bt("x(); /reg/exp.match(something)", "x();\n/reg/exp.match(something)");
test_fragment("something();(", "something();\n(");
test_fragment("#!she/bangs, she bangs\nf=1", "#!she/bangs, she bangs\n\nf = 1");
test_fragment("#!she/bangs, she bangs\n\nf=1", "#!she/bangs, she bangs\n\nf = 1");
test_fragment("#!she/bangs, she bangs\n\n/* comment */", "#!she/bangs, she bangs\n\n/* comment */");
test_fragment("#!she/bangs, she bangs\n\n\n/* comment */", "#!she/bangs, she bangs\n\n\n/* comment */");
test_fragment("#", "#");
test_fragment("#!", "#!");
bt("function namespace::something()");
test_fragment("<!--\nsomething();\n-->", "<!--\nsomething();\n-->");
test_fragment("<!--\nif(i<0){bla();}\n-->", "<!--\nif (i < 0) {\n bla();\n}\n-->");
bt('{foo();--bar;}', '{\n foo();\n --bar;\n}');
bt('{foo();++bar;}', '{\n foo();\n ++bar;\n}');
bt('{--bar;}', '{\n --bar;\n}');
bt('{++bar;}', '{\n ++bar;\n}');
bt('if(true)++a;','if (true) ++a;');
bt('if(true)\n++a;','if (true)\n ++a;');
bt('if(true)--a;','if (true) --a;');
bt('if(true)\n--a;','if (true)\n --a;');
// Handling of newlines around unary ++ and -- operators
bt('{foo\n++bar;}', '{\n foo\n ++bar;\n}');
bt('{foo++\nbar;}', '{\n foo++\n bar;\n}');
// This is invalid, but harder to guard against. Issue #203.
bt('{foo\n++\nbar;}', '{\n foo\n ++\n bar;\n}');
// regexps
bt('a(/abc\\/\\/def/);b()', "a(/abc\\/\\/def/);\nb()");
bt('a(/a[b\\[\\]c]d/);b()', "a(/a[b\\[\\]c]d/);\nb()");
test_fragment('a(/a[b\\[', "a(/a[b\\["); // incomplete char class
// allow unescaped / in char classes
bt('a(/[a/b]/);b()', "a(/[a/b]/);\nb()");
bt('typeof /foo\\//;');
bt('yield /foo\\//;');
bt('throw /foo\\//;');
bt('do /foo\\//;');
bt('return /foo\\//;');
bt('switch (a) {\n case /foo\\//:\n b\n}');
bt('if (a) /foo\\//\nelse /foo\\//;');
bt('if (foo) /regex/.test();');
bt('function foo() {\n return [\n "one",\n "two"\n ];\n}');
bt('a=[[1,2],[4,5],[7,8]]', "a = [\n [1, 2],\n [4, 5],\n [7, 8]\n]");
bt('a=[[1,2],[4,5],function(){},[7,8]]',
"a = [\n [1, 2],\n [4, 5],\n function() {},\n [7, 8]\n]");
bt('a=[[1,2],[4,5],function(){},function(){},[7,8]]',
"a = [\n [1, 2],\n [4, 5],\n function() {},\n function() {},\n [7, 8]\n]");
bt('a=[[1,2],[4,5],function(){},[7,8]]',
"a = [\n [1, 2],\n [4, 5],\n function() {},\n [7, 8]\n]");
bt('a=[b,c,function(){},function(){},d]',
"a = [b, c, function() {}, function() {}, d]");
bt('a=[b,c,\nfunction(){},function(){},d]',
"a = [b, c,\n function() {},\n function() {},\n d\n]");
bt('a=[a[1],b[4],c[d[7]]]', "a = [a[1], b[4], c[d[7]]]");
bt('[1,2,[3,4,[5,6],7],8]', "[1, 2, [3, 4, [5, 6], 7], 8]");
bt('[[["1","2"],["3","4"]],[["5","6","7"],["8","9","0"]],[["1","2","3"],["4","5","6","7"],["8","9","0"]]]',
'[\n [\n ["1", "2"],\n ["3", "4"]\n ],\n [\n ["5", "6", "7"],\n ["8", "9", "0"]\n ],\n [\n ["1", "2", "3"],\n ["4", "5", "6", "7"],\n ["8", "9", "0"]\n ]\n]');
bt('{[x()[0]];indent;}', '{\n [x()[0]];\n indent;\n}');
bt('return ++i', 'return ++i');
bt('return !!x', 'return !!x');

View File

@ -74,178 +74,6 @@ class TestJSBeautifier(unittest.TestCase):
{{=<% %>=}}
bt('{{}/z/}', "{\n {}\n /z/\n}")
<%={{ }}=%>
bt('return 45', "return 45")
bt('return this.prevObject ||\n\n this.constructor(null);')
bt('If[1]', "If[1]")
bt('Then[1]', "Then[1]")
bt('a = 1e10', "a = 1e10")
bt('a = 1.3e10', "a = 1.3e10")
bt('a = 1.3e-10', "a = 1.3e-10")
bt('a = -1.3e-10', "a = -1.3e-10")
bt('a = 1e-10', "a = 1e-10")
bt('a = e - 10', "a = e - 10")
bt('a = 11-10', "a = 11 - 10")
bt("a = 1;// comment", "a = 1; // comment")
bt("a = 1; // comment", "a = 1; // comment")
bt("a = 1;\n // comment", "a = 1;\n// comment")
bt('a = [-1, -1, -1]')
# The exact formatting these should have is open for discussion, but they are at least reasonable
bt('a = [ // comment\n -1, -1, -1\n]')
bt('var a = [ // comment\n -1, -1, -1\n]')
bt('a = [ // comment\n -1, // comment\n -1, -1\n]')
bt('var a = [ // comment\n -1, // comment\n -1, -1\n]')
bt('o = [{a:b},{c:d}]', 'o = [{\n a: b\n}, {\n c: d\n}]')
bt("if (a) {\n do();\n}") # was: extra space appended
bt("if (a) {\n// comment\n}else{\n// comment\n}", "if (a) {\n // comment\n} else {\n // comment\n}") # if/else statement with empty body
bt("if (a) {\n// comment\n// comment\n}", "if (a) {\n // comment\n // comment\n}") # multiple comments indentation
bt("if (a) b() else c();", "if (a) b()\nelse c();")
bt("if (a) b() else if c() d();", "if (a) b()\nelse if c() d();")
bt("{}")
bt("{\n\n}")
bt("do { a(); } while ( 1 );", "do {\n a();\n} while (1);")
bt("do {} while (1);")
bt("do {\n} while (1);", "do {} while (1);")
bt("do {\n\n} while (1);")
bt("var a = x(a, b, c)")
bt("delete x if (a) b();", "delete x\nif (a) b();")
bt("delete x[x] if (a) b();", "delete x[x]\nif (a) b();")
bt("for(var a=1,b=2)d", "for (var a = 1, b = 2) d")
bt("for(var a=1,b=2,c=3) d", "for (var a = 1, b = 2, c = 3) d")
bt("for(var a=1,b=2,c=3;d<3;d++)\ne", "for (var a = 1, b = 2, c = 3; d < 3; d++)\n e")
bt("function x(){(a||b).c()}", "function x() {\n (a || b).c()\n}")
bt("function x(){return - 1}", "function x() {\n return -1\n}")
bt("function x(){return ! a}", "function x() {\n return !a\n}")
bt("x => x", "x => x")
bt("(x) => x", "(x) => x")
bt("x => { x }", "x => {\n x\n}")
bt("(x) => { x }", "(x) => {\n x\n}")
# a common snippet in jQuery plugins
bt("settings = $.extend({},defaults,settings);", "settings = $.extend({}, defaults, settings);")
# reserved words used as property names
bt("$http().then().finally().default()", "$http().then().finally().default()")
bt("$http()\n.then()\n.finally()\n.default()", "$http()\n .then()\n .finally()\n .default()")
bt("$http().when.in.new.catch().throw()", "$http().when.in.new.catch().throw()")
bt("$http()\n.when\n.in\n.new\n.catch()\n.throw()", "$http()\n .when\n .in\n .new\n .catch()\n .throw()")
bt('{xxx;}()', '{\n xxx;\n}()')
bt("a = 'a'\nb = 'b'")
bt("a = /reg/exp")
bt("a = /reg/")
bt('/abc/.test()')
bt('/abc/i.test()')
bt("{/abc/i.test()}", "{\n /abc/i.test()\n}")
bt('var x=(a)/a;', 'var x = (a) / a;')
bt('x != -1', 'x != -1')
bt('for (; s-->0;)t', 'for (; s-- > 0;) t')
bt('for (; s++>0;)u', 'for (; s++ > 0;) u')
bt('a = s++>s--;', 'a = s++ > s--;')
bt('a = s++>--s;', 'a = s++ > --s;')
bt('{x=#1=[]}', '{\n x = #1=[]\n}')
bt('{a:#1={}}', '{\n a: #1={}\n}')
bt('{a:#1#}', '{\n a: #1#\n}')
test_fragment('"incomplete-string')
test_fragment("'incomplete-string")
test_fragment('/incomplete-regex')
test_fragment('`incomplete-regex')
test_fragment('{a:1},{a:2}', '{\n a: 1\n}, {\n a: 2\n}')
test_fragment('var ary=[{a:1}, {a:2}];', 'var ary = [{\n a: 1\n}, {\n a: 2\n}];')
test_fragment('{a:#1', '{\n a: #1') # incomplete
test_fragment('{a:#', '{\n a: #') # incomplete
test_fragment('}}}', '}\n}\n}') # incomplete
test_fragment('<!--\nvoid();\n// -->', '<!--\nvoid();\n// -->')
test_fragment('a=/regexp', 'a = /regexp') # incomplete regexp
bt('{a:#1=[],b:#1#,c:#999999#}', '{\n a: #1=[],\n b: #1#,\n c: #999999#\n}')
bt("a = 1e+2")
bt("a = 1e-2")
bt("do{x()}while(a>1)", "do {\n x()\n} while (a > 1)")
bt("x(); /reg/exp.match(something)", "x();\n/reg/exp.match(something)")
test_fragment("something();(", "something();\n(")
test_fragment("#!she/bangs, she bangs\nf=1", "#!she/bangs, she bangs\n\nf = 1")
test_fragment("#!she/bangs, she bangs\n\nf=1", "#!she/bangs, she bangs\n\nf = 1")
test_fragment("#!she/bangs, she bangs\n\n/* comment */", "#!she/bangs, she bangs\n\n/* comment */")
test_fragment("#!she/bangs, she bangs\n\n\n/* comment */", "#!she/bangs, she bangs\n\n\n/* comment */")
test_fragment("#", "#")
test_fragment("#!", "#!")
bt("function namespace::something()")
test_fragment("<!--\nsomething();\n-->", "<!--\nsomething();\n-->")
test_fragment("<!--\nif(i<0){bla();}\n-->", "<!--\nif (i < 0) {\n bla();\n}\n-->")
bt('{foo();--bar;}', '{\n foo();\n --bar;\n}')
bt('{foo();++bar;}', '{\n foo();\n ++bar;\n}')
bt('{--bar;}', '{\n --bar;\n}')
bt('{++bar;}', '{\n ++bar;\n}')
bt('if(true)++a;','if (true) ++a;')
bt('if(true)\n++a;','if (true)\n ++a;')
bt('if(true)--a;','if (true) --a;')
bt('if(true)\n--a;','if (true)\n --a;')
# Handling of newlines around unary ++ and -- operators
bt('{foo\n++bar;}', '{\n foo\n ++bar;\n}')
bt('{foo++\nbar;}', '{\n foo++\n bar;\n}')
# This is invalid, but harder to guard against. Issue #203.
bt('{foo\n++\nbar;}', '{\n foo\n ++\n bar;\n}')
# regexps
bt('a(/abc\\/\\/def/);b()', "a(/abc\\/\\/def/);\nb()")
bt('a(/a[b\\[\\]c]d/);b()', "a(/a[b\\[\\]c]d/);\nb()")
test_fragment('a(/a[b\\[', "a(/a[b\\[") # incomplete char class
# allow unescaped / in char classes
bt('a(/[a/b]/);b()', "a(/[a/b]/);\nb()")
bt('typeof /foo\\//;')
bt('yield /foo\\//;')
bt('throw /foo\\//;')
bt('do /foo\\//;')
bt('return /foo\\//;')
bt('switch (a) {\n case /foo\\//:\n b\n}')
bt('if (a) /foo\\//\nelse /foo\\//;')
bt('if (foo) /regex/.test();')
bt('function foo() {\n return [\n "one",\n "two"\n ];\n}')
bt('a=[[1,2],[4,5],[7,8]]', "a = [\n [1, 2],\n [4, 5],\n [7, 8]\n]")
bt('a=[[1,2],[4,5],function(){},[7,8]]',
"a = [\n [1, 2],\n [4, 5],\n function() {},\n [7, 8]\n]")
bt('a=[[1,2],[4,5],function(){},function(){},[7,8]]',
"a = [\n [1, 2],\n [4, 5],\n function() {},\n function() {},\n [7, 8]\n]")
bt('a=[[1,2],[4,5],function(){},[7,8]]',
"a = [\n [1, 2],\n [4, 5],\n function() {},\n [7, 8]\n]")
bt('a=[b,c,function(){},function(){},d]',
"a = [b, c, function() {}, function() {}, d]")
bt('a=[b,c,\nfunction(){},function(){},d]',
"a = [b, c,\n function() {},\n function() {},\n d\n]")
bt('a=[a[1],b[4],c[d[7]]]', "a = [a[1], b[4], c[d[7]]]")
bt('[1,2,[3,4,[5,6],7],8]', "[1, 2, [3, 4, [5, 6], 7], 8]")
bt('[[["1","2"],["3","4"]],[["5","6","7"],["8","9","0"]],[["1","2","3"],["4","5","6","7"],["8","9","0"]]]',
'[\n [\n ["1", "2"],\n ["3", "4"]\n ],\n [\n ["5", "6", "7"],\n ["8", "9", "0"]\n ],\n [\n ["1", "2", "3"],\n ["4", "5", "6", "7"],\n ["8", "9", "0"]\n ]\n]')
bt('{[x()[0]];indent;}', '{\n [x()[0]];\n indent;\n}')
bt('return ++i', 'return ++i')
bt('return !!x', 'return !!x')
bt('return !x', 'return !x')