js-beautify 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. data/.document +5 -0
  2. data/.gitmodules +4 -0
  3. data/Gemfile +13 -0
  4. data/Gemfile.lock +27 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.rdoc +13 -0
  7. data/Rakefile +29 -0
  8. data/VERSION +1 -0
  9. data/bin/js-beautify +5 -0
  10. data/init.sh +3 -0
  11. data/js-beautify-copy/.gitmodules +3 -0
  12. data/js-beautify-copy/Makefile +71 -0
  13. data/js-beautify-copy/README.md +39 -0
  14. data/js-beautify-copy/attic/beautify-cl/beautify-cl.js +142 -0
  15. data/js-beautify-copy/attic/bin/beautify_js +80 -0
  16. data/js-beautify-copy/attic/opera-userscript/beautifier.js +1087 -0
  17. data/js-beautify-copy/attic/opera-userscript/make_opera_userscript.sh +42 -0
  18. data/js-beautify-copy/attic/qtscript/jsbeautify.cpp +121 -0
  19. data/js-beautify-copy/attic/qtscript/jsbeautify.pro +5 -0
  20. data/js-beautify-copy/attic/qtscript/jsbeautify.qrc +6 -0
  21. data/js-beautify-copy/attic/qtscript/readme.txt +28 -0
  22. data/js-beautify-copy/attic/readme.txt +2 -0
  23. data/js-beautify-copy/attic/unmaintained/bbedit/jsBeautify_BBED.scpt +522 -0
  24. data/js-beautify-copy/attic/unmaintained/c-sharp/JSBeautify.cs +801 -0
  25. data/js-beautify-copy/attic/v8/README.txt +40 -0
  26. data/js-beautify-copy/attic/v8/beautify.h +2390 -0
  27. data/js-beautify-copy/attic/v8/jsbeautify.cpp +215 -0
  28. data/js-beautify-copy/beautify-css.js +198 -0
  29. data/js-beautify-copy/beautify-html.js +514 -0
  30. data/js-beautify-copy/beautify.js +1293 -0
  31. data/js-beautify-copy/favicon.png +0 -0
  32. data/js-beautify-copy/index.html +401 -0
  33. data/js-beautify-copy/jquery/jquery.cookie.js +96 -0
  34. data/js-beautify-copy/jquery/jquery.js +167 -0
  35. data/js-beautify-copy/license.txt +24 -0
  36. data/js-beautify-copy/php/jsbeautifier.php +1599 -0
  37. data/js-beautify-copy/php/test.php +476 -0
  38. data/js-beautify-copy/python/MANIFEST.in +2 -0
  39. data/js-beautify-copy/python/js-beautify +7 -0
  40. data/js-beautify-copy/python/js-beautify-profile +16 -0
  41. data/js-beautify-copy/python/js-beautify-test +10 -0
  42. data/js-beautify-copy/python/jsbeautifier/__init__.py +1166 -0
  43. data/js-beautify-copy/python/jsbeautifier/tests/__init__.py +1 -0
  44. data/js-beautify-copy/python/jsbeautifier/tests/testindentation.py +43 -0
  45. data/js-beautify-copy/python/jsbeautifier/tests/testjsbeautifier.py +464 -0
  46. data/js-beautify-copy/python/jsbeautifier/unpackers/README.specs.mkd +25 -0
  47. data/js-beautify-copy/python/jsbeautifier/unpackers/__init__.py +67 -0
  48. data/js-beautify-copy/python/jsbeautifier/unpackers/evalbased.py +39 -0
  49. data/js-beautify-copy/python/jsbeautifier/unpackers/javascriptobfuscator.py +58 -0
  50. data/js-beautify-copy/python/jsbeautifier/unpackers/myobfuscate.py +86 -0
  51. data/js-beautify-copy/python/jsbeautifier/unpackers/packer.py +104 -0
  52. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/__init__.py +2 -0
  53. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/test-myobfuscate-input.js +1 -0
  54. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/test-myobfuscate-output.js +65 -0
  55. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/test-packer-62-input.js +1 -0
  56. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/test-packer-non62-input.js +1 -0
  57. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/testjavascriptobfuscator.py +46 -0
  58. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/testmyobfuscate.py +40 -0
  59. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/testpacker.py +34 -0
  60. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/testurlencode.py +36 -0
  61. data/js-beautify-copy/python/jsbeautifier/unpackers/urlencode.py +34 -0
  62. data/js-beautify-copy/python/setup.py +17 -0
  63. data/js-beautify-copy/tests/beautify-tests.js +489 -0
  64. data/js-beautify-copy/tests/run-tests +17 -0
  65. data/js-beautify-copy/tests/sanitytest.js +128 -0
  66. data/js-beautify-copy/unpackers/javascriptobfuscator_unpacker.js +103 -0
  67. data/js-beautify-copy/unpackers/myobfuscate_unpacker.js +81 -0
  68. data/js-beautify-copy/unpackers/p_a_c_k_e_r_unpacker.js +61 -0
  69. data/js-beautify-copy/unpackers/urlencode_unpacker.js +51 -0
  70. data/lib/js-beautify.rb +0 -0
  71. data/test/helper.rb +18 -0
  72. data/test/test_js-beautify.rb +7 -0
  73. data/update.sh +23 -0
  74. metadata +173 -0
@@ -0,0 +1,43 @@
1
+ import re
2
+ import unittest
3
+ import jsbeautifier
4
+
5
+ class TestJSBeautifierIndentation(unittest.TestCase):
6
+ def test_tabs(self):
7
+ test_fragment = self.decodesto
8
+
9
+ self.options.indent_with_tabs = 1;
10
+ test_fragment('{tabs()}', "{\n\ttabs()\n}");
11
+
12
+ def test_function_indent(self):
13
+ test_fragment = self.decodesto
14
+
15
+ self.options.indent_with_tabs = 1;
16
+ self.options.keep_function_indentation = 1;
17
+ test_fragment('var foo = function(){ bar() }();', "var foo = function() {\n\tbar()\n}();");
18
+
19
+ self.options.tabs = 1;
20
+ self.options.keep_function_indentation = 0;
21
+ test_fragment('var foo = function(){ baz() }();', "var foo = function() {\n\t\tbaz()\n\t}();");
22
+
23
+ def decodesto(self, input, expectation=None):
24
+ self.assertEqual(
25
+ jsbeautifier.beautify(input, self.options), expectation or input)
26
+
27
+ @classmethod
28
+ def setUpClass(cls):
29
+ options = jsbeautifier.default_options()
30
+ options.indent_size = 4
31
+ options.indent_char = ' '
32
+ options.preserve_newlines = True
33
+ options.jslint_happy = False
34
+ options.keep_array_indentation = False
35
+ options.brace_style = 'collapse'
36
+ options.indent_level = 0
37
+
38
+ cls.options = options
39
+ cls.wrapregex = re.compile('^(.+)$', re.MULTILINE)
40
+
41
+
42
+ if __name__ == '__main__':
43
+ unittest.main()
@@ -0,0 +1,464 @@
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ import re
5
+ import unittest
6
+ import jsbeautifier
7
+
8
+ class TestJSBeautifier(unittest.TestCase):
9
+ def test_unescape(self):
10
+ # Test cases contributed by <chrisjshull on GitHub.com>
11
+ test_fragment = self.decodesto
12
+ bt = self.bt
13
+
14
+ bt('"\\\\s"'); # == "\\s" in the js source
15
+ bt("'\\\\s'"); # == '\\s' in the js source
16
+ bt("'\\\\\\s'"); # == '\\\s' in the js source
17
+ bt("'\\s'"); # == '\s' in the js source
18
+ bt('"•"');
19
+ bt('"—"');
20
+ #bt('"\x41\x42\x43\x01"', '"ABC\\x01"');
21
+ bt('"\u2022"', '"\u2022"');
22
+ bt('a = /\s+/')
23
+ #bt('a = /\x41/','a = /A/')
24
+ bt('"\u2022";a = /\s+/;"\x41\x42\x43\x01".match(/\x41/);','"\u2022";\na = /\s+/;\n"\x41\x42\x43\x01".match(/\x41/);')
25
+
26
+ def test_beautifier(self):
27
+ test_fragment = self.decodesto
28
+ bt = self.bt
29
+
30
+ bt('');
31
+ bt('return .5');
32
+ test_fragment(' return .5');
33
+ bt('a = 1', 'a = 1');
34
+ bt('a=1', 'a = 1');
35
+ bt("a();\n\nb();", "a();\n\nb();");
36
+ bt('var a = 1 var b = 2', "var a = 1\nvar b = 2");
37
+ bt('var a=1, b=c[d], e=6;', 'var a = 1,\n b = c[d],\n e = 6;');
38
+ bt('a = " 12345 "');
39
+ bt("a = ' 12345 '");
40
+ bt('if (a == 1) b = 2;', "if (a == 1) b = 2;");
41
+ bt('if(1){2}else{3}', "if (1) {\n 2\n} else {\n 3\n}");
42
+ bt('if (foo) bar();\nelse\ncar();', 'if (foo) bar();\nelse car();');
43
+ bt('if(1||2);', 'if (1 || 2);');
44
+ bt('(a==1)||(b==2)', '(a == 1) || (b == 2)');
45
+ bt('var a = 1 if (2) 3;', "var a = 1\nif (2) 3;");
46
+ bt('a = a + 1');
47
+ bt('a = a == 1');
48
+ bt('/12345[^678]*9+/.match(a)');
49
+ bt('a /= 5');
50
+ bt('a = 0.5 * 3');
51
+ bt('a *= 10.55');
52
+ bt('a < .5');
53
+ bt('a <= .5');
54
+ bt('a<.5', 'a < .5');
55
+ bt('a<=.5', 'a <= .5');
56
+ bt('a = 0xff;');
57
+ bt('a=0xff+4', 'a = 0xff + 4');
58
+ bt('a = [1, 2, 3, 4]');
59
+ bt('F*(g/=f)*g+b', 'F * (g /= f) * g + b');
60
+ bt('a.b({c:d})', "a.b({\n c: d\n})");
61
+ bt('a.b\n(\n{\nc:\nd\n}\n)', "a.b({\n c: d\n})");
62
+ bt('a=!b', 'a = !b');
63
+ bt('a?b:c', 'a ? b : c');
64
+ bt('a?1:2', 'a ? 1 : 2');
65
+ bt('a?(b):c', 'a ? (b) : c');
66
+ bt('x={a:1,b:w=="foo"?x:y,c:z}', 'x = {\n a: 1,\n b: w == "foo" ? x : y,\n c: z\n}');
67
+ bt('x=a?b?c?d:e:f:g;', 'x = a ? b ? c ? d : e : f : g;');
68
+ bt('x=a?b?c?d:{e1:1,e2:2}:f:g;', 'x = a ? b ? c ? d : {\n e1: 1,\n e2: 2\n} : f : g;');
69
+ bt('function void(void) {}');
70
+ bt('if(!a)foo();', 'if (!a) foo();');
71
+ bt('a=~a', 'a = ~a');
72
+ bt('a;/*comment*/b;', "a; /*comment*/\nb;");
73
+ bt('a;/* comment */b;', "a; /* comment */\nb;");
74
+ test_fragment('a;/*\ncomment\n*/b;', "a;\n/*\ncomment\n*/\nb;"); # simple comments don't get touched at all
75
+ bt('a;/**\n* javadoc\n*/b;', "a;\n/**\n * javadoc\n */\nb;");
76
+ test_fragment('a;/**\n\nno javadoc\n*/b;', "a;\n/**\n\nno javadoc\n*/\nb;");
77
+ bt('a;/*\n* javadoc\n*/b;', "a;\n/*\n * javadoc\n */\nb;"); # comment blocks detected and reindented even w/o javadoc starter
78
+
79
+ bt('if(a)break;', "if (a) break;");
80
+ bt('if(a){break}', "if (a) {\n break\n}");
81
+ bt('if((a))foo();', 'if ((a)) foo();');
82
+ bt('for(var i=0;;)', 'for (var i = 0;;)');
83
+ bt('a++;', 'a++;');
84
+ bt('for(;;i++)', 'for (;; i++)');
85
+ bt('for(;;++i)', 'for (;; ++i)');
86
+ bt('return(1)', 'return (1)');
87
+ bt('try{a();}catch(b){c();}finally{d();}', "try {\n a();\n} catch (b) {\n c();\n} finally {\n d();\n}");
88
+ bt('(xx)()'); # magic function call
89
+ bt('a[1]()'); # another magic function call
90
+ bt('if(a){b();}else if(c) foo();', "if (a) {\n b();\n} else if (c) foo();");
91
+ bt('switch(x) {case 0: case 1: a(); break; default: break}', "switch (x) {\ncase 0:\ncase 1:\n a();\n break;\ndefault:\n break\n}");
92
+ bt('switch(x){case -1:break;case !y:break;}', 'switch (x) {\ncase -1:\n break;\ncase !y:\n break;\n}');
93
+ bt('a !== b');
94
+ bt('if (a) b(); else c();', "if (a) b();\nelse c();");
95
+ bt("// comment\n(function something() {})"); # typical greasemonkey start
96
+ bt("{\n\n x();\n\n}"); # was: duplicating newlines
97
+ bt('if (a in b) foo();');
98
+ bt('var a, b');
99
+ bt('{a:1, b:2}', "{\n a: 1,\n b: 2\n}");
100
+ bt('a={1:[-1],2:[+1]}', 'a = {\n 1: [-1],\n 2: [+1]\n}');
101
+ bt('var l = {\'a\':\'1\', \'b\':\'2\'}', "var l = {\n 'a': '1',\n 'b': '2'\n}");
102
+ bt('if (template.user[n] in bk) foo();');
103
+ bt('{{}/z/}', "{\n {}\n /z/\n}");
104
+ bt('return 45', "return 45");
105
+ bt('If[1]', "If[1]");
106
+ bt('Then[1]', "Then[1]");
107
+ bt('a = 1e10', "a = 1e10");
108
+ bt('a = 1.3e10', "a = 1.3e10");
109
+ bt('a = 1.3e-10', "a = 1.3e-10");
110
+ bt('a = -1.3e-10', "a = -1.3e-10");
111
+ bt('a = 1e-10', "a = 1e-10");
112
+ bt('a = e - 10', "a = e - 10");
113
+ bt('a = 11-10', "a = 11 - 10");
114
+ bt("a = 1;// comment\n", "a = 1; // comment");
115
+ bt("a = 1; // comment\n", "a = 1; // comment");
116
+ bt("a = 1;\n // comment\n", "a = 1;\n// comment");
117
+
118
+ bt('o = [{a:b},{c:d}]', 'o = [{\n a: b\n}, {\n c: d\n}]');
119
+
120
+ bt("if (a) {\n do();\n}"); # was: extra space appended
121
+ bt("if\n(a)\nb();", "if (a) b();"); # test for proper newline removal
122
+
123
+ 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
124
+ bt("if (a) {\n// comment\n// comment\n}", "if (a) {\n // comment\n // comment\n}"); # multiple comments indentation
125
+ bt("if (a) b() else c();", "if (a) b()\nelse c();");
126
+ bt("if (a) b() else if c() d();", "if (a) b()\nelse if c() d();");
127
+
128
+ bt("{}");
129
+ bt("{\n\n}");
130
+ bt("do { a(); } while ( 1 );", "do {\n a();\n} while (1);");
131
+ bt("do {} while (1);");
132
+ bt("do {\n} while (1);", "do {} while (1);");
133
+ bt("do {\n\n} while (1);");
134
+ bt("var a = x(a, b, c)");
135
+ bt("delete x if (a) b();", "delete x\nif (a) b();");
136
+ bt("delete x[x] if (a) b();", "delete x[x]\nif (a) b();");
137
+ bt("for(var a=1,b=2)", "for (var a = 1, b = 2)");
138
+ bt("for(var a=1,b=2,c=3)", "for (var a = 1, b = 2, c = 3)");
139
+ bt("for(var a=1,b=2,c=3;d<3;d++)", "for (var a = 1, b = 2, c = 3; d < 3; d++)");
140
+ bt("function x(){(a||b).c()}", "function x() {\n (a || b).c()\n}");
141
+ bt("function x(){return - 1}", "function x() {\n return -1\n}");
142
+ bt("function x(){return ! a}", "function x() {\n return !a\n}");
143
+
144
+ # a common snippet in jQuery plugins
145
+ bt("settings = $.extend({},defaults,settings);", "settings = $.extend({}, defaults, settings);");
146
+
147
+ bt('{xxx;}()', '{\n xxx;\n}()');
148
+
149
+ bt("a = 'a'\nb = 'b'");
150
+ bt("a = /reg/exp");
151
+ bt("a = /reg/");
152
+ bt('/abc/.test()');
153
+ bt('/abc/i.test()');
154
+ bt("{/abc/i.test()}", "{\n /abc/i.test()\n}");
155
+ bt('var x=(a)/a;', 'var x = (a) / a;');
156
+
157
+ bt('x != -1', 'x != -1');
158
+
159
+ bt('for (; s-->0;)', 'for (; s-- > 0;)');
160
+ bt('for (; s++>0;)', 'for (; s++ > 0;)');
161
+ bt('a = s++>s--;', 'a = s++ > s--;');
162
+ bt('a = s++>--s;', 'a = s++ > --s;');
163
+
164
+ bt('{x=#1=[]}', '{\n x = #1=[]\n}');
165
+ bt('{a:#1={}}', '{\n a: #1={}\n}');
166
+ bt('{a:#1#}', '{\n a: #1#\n}');
167
+
168
+ test_fragment('{a:1},{a:2}', '{\n a: 1\n}, {\n a: 2\n}');
169
+ test_fragment('var ary=[{a:1}, {a:2}];', 'var ary = [{\n a: 1\n}, {\n a: 2\n}];');
170
+
171
+ test_fragment('{a:#1', '{\n a: #1'); # incomplete
172
+ test_fragment('{a:#', '{\n a: #'); # incomplete
173
+
174
+ test_fragment('}}}', '}\n}\n}'); # incomplete
175
+
176
+ test_fragment('<!--\nvoid();\n// -->', '<!--\nvoid();\n// -->');
177
+
178
+ test_fragment('a=/regexp', 'a = /regexp'); # incomplete regexp
179
+
180
+ bt('{a:#1=[],b:#1#,c:#999999#}', '{\n a: #1=[],\n b: #1#,\n c: #999999#\n}');
181
+
182
+ bt("a = 1e+2");
183
+ bt("a = 1e-2");
184
+ bt("do{x()}while(a>1)", "do {\n x()\n} while (a > 1)");
185
+
186
+ bt("x(); /reg/exp.match(something)", "x();\n/reg/exp.match(something)");
187
+
188
+ test_fragment("something();(", "something();\n(");
189
+ test_fragment("#!she/bangs, she bangs\nf=1", "#!she/bangs, she bangs\n\nf = 1");
190
+ test_fragment("#!she/bangs, she bangs\n\nf=1", "#!she/bangs, she bangs\n\nf = 1");
191
+ test_fragment("#!she/bangs, she bangs\n\n/* comment */", "#!she/bangs, she bangs\n\n/* comment */");
192
+ test_fragment("#!she/bangs, she bangs\n\n\n/* comment */", "#!she/bangs, she bangs\n\n\n/* comment */");
193
+ test_fragment("#", "#");
194
+ test_fragment("#!", "#!");
195
+
196
+ bt("function namespace::something()");
197
+
198
+ test_fragment("<!--\nsomething();\n-->", "<!--\nsomething();\n-->");
199
+ test_fragment("<!--\nif(i<0){bla();}\n-->", "<!--\nif (i < 0) {\n bla();\n}\n-->");
200
+
201
+ bt('{foo();--bar;}', '{\n foo();\n --bar;\n}');
202
+ bt('{foo();++bar;}', '{\n foo();\n ++bar;\n}');
203
+ bt('{--bar;}', '{\n --bar;\n}');
204
+ bt('{++bar;}', '{\n ++bar;\n}');
205
+
206
+ # regexps
207
+ bt('a(/abc\\/\\/def/);b()', "a(/abc\\/\\/def/);\nb()");
208
+ bt('a(/a[b\\[\\]c]d/);b()', "a(/a[b\\[\\]c]d/);\nb()");
209
+ test_fragment('a(/a[b\\[', "a(/a[b\\["); # incomplete char class
210
+ # allow unescaped / in char classes
211
+ bt('a(/[a/b]/);b()', "a(/[a/b]/);\nb()");
212
+
213
+ bt('a=[[1,2],[4,5],[7,8]]', "a = [\n [1, 2],\n [4, 5],\n [7, 8]\n]");
214
+ bt('a=[a[1],b[4],c[d[7]]]', "a = [a[1], b[4], c[d[7]]]");
215
+ bt('[1,2,[3,4,[5,6],7],8]', "[1, 2, [3, 4, [5, 6], 7], 8]");
216
+
217
+ bt('[[["1","2"],["3","4"]],[["5","6","7"],["8","9","0"]],[["1","2","3"],["4","5","6","7"],["8","9","0"]]]',
218
+ '[\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]');
219
+
220
+ bt('{[x()[0]];indent;}', '{\n [x()[0]];\n indent;\n}');
221
+
222
+ bt('return ++i', 'return ++i');
223
+ bt('return !!x', 'return !!x');
224
+ bt('return !x', 'return !x');
225
+ bt('return [1,2]', 'return [1, 2]');
226
+ bt('return;', 'return;');
227
+ bt('return\nfunc', 'return\nfunc');
228
+ bt('catch(e)', 'catch (e)');
229
+
230
+ bt('var a=1,b={foo:2,bar:3},c=4;', 'var a = 1,\n b = {\n foo: 2,\n bar: 3\n },\n c = 4;');
231
+
232
+ #bt('x: y, // comment'); # FIXME #44
233
+
234
+ # inline comment
235
+ bt('function x(/*int*/ start, /*string*/ foo)', 'function x( /*int*/ start, /*string*/ foo)');
236
+
237
+ # javadoc comment
238
+ bt('/**\n* foo\n*/', '/**\n * foo\n */');
239
+ bt('{\n/**\n* foo\n*/\n}', '{\n /**\n * foo\n */\n}');
240
+
241
+ bt('var a,b,c=1,d,e,f=2;', 'var a, b, c = 1,\n d, e, f = 2;');
242
+ bt('var a,b,c=[],d,e,f=2;', 'var a, b, c = [],\n d, e, f = 2;');
243
+ bt('function() {\n var a, b, c, d, e = [],\n f;\n}');
244
+
245
+ bt('do/regexp/;\nwhile(1);', 'do /regexp/;\nwhile (1);'); # hmmm
246
+
247
+ bt('var a = a,\na;\nb = {\nb\n}', 'var a = a,\n a;\nb = {\n b\n}');
248
+
249
+ bt('var a = a,\n /* c */\n b;');
250
+ bt('var a = a,\n // c\n b;');
251
+
252
+ bt('foo.("bar");'); # weird element referencing
253
+
254
+
255
+ bt('if (a) a()\nelse b()\nnewline()');
256
+ bt('if (a) a()\nnewline()');
257
+ bt('a=typeof(x)', 'a = typeof(x)');
258
+
259
+ bt('var a = function() {\n return null;\n },\n b = false;');
260
+
261
+ bt('var a = function() {\n func1()\n }');
262
+ bt('var a = function() {\n func1()\n }\nvar b = function() {\n func2()\n }');
263
+
264
+
265
+
266
+
267
+ self.options.jslint_happy = True
268
+
269
+ bt('x();\n\nfunction(){}', 'x();\n\nfunction () {}');
270
+ bt('function () {\n var a, b, c, d, e = [],\n f;\n}');
271
+ test_fragment("// comment 1\n(function()", "// comment 1\n(function ()"); # typical greasemonkey start
272
+ bt("var a1, b1, c1, d1 = 0, c = function() {}, d = '';", "var a1, b1, c1, d1 = 0,\n c = function () {},\n d = '';");
273
+ bt('var o1=$.extend(a);function(){alert(x);}', 'var o1 = $.extend(a);\n\nfunction () {\n alert(x);\n}');
274
+ bt('a=typeof(x)', 'a = typeof (x)');
275
+
276
+ self.options.jslint_happy = False
277
+
278
+ test_fragment("// comment 2\n(function()", "// comment 2\n(function()"); # typical greasemonkey start
279
+ bt("var a2, b2, c2, d2 = 0, c = function() {}, d = '';", "var a2, b2, c2, d2 = 0,\n c = function() {},\n d = '';");
280
+ bt('var o2=$.extend(a);function(){alert(x);}', 'var o2 = $.extend(a);\n\nfunction() {\n alert(x);\n}');
281
+
282
+ bt('{"x":[{"a":1,"b":3},7,8,8,8,8,{"b":99},{"a":11}]}', '{\n "x": [{\n "a": 1,\n "b": 3\n },\n 7, 8, 8, 8, 8,\n {\n "b": 99\n }, {\n "a": 11\n }]\n}');
283
+
284
+ bt('{"1":{"1a":"1b"},"2"}', '{\n "1": {\n "1a": "1b"\n },\n "2"\n}');
285
+ bt('{a:{a:b},c}', '{\n a: {\n a: b\n },\n c\n}');
286
+
287
+ bt('{[y[a]];keep_indent;}', '{\n [y[a]];\n keep_indent;\n}');
288
+
289
+ bt('if (x) {y} else { if (x) {y}}', 'if (x) {\n y\n} else {\n if (x) {\n y\n }\n}');
290
+
291
+ bt('if (foo) one()\ntwo()\nthree()');
292
+ bt('if (1 + foo() && bar(baz()) / 2) one()\ntwo()\nthree()');
293
+ bt('if (1 + foo() && bar(baz()) / 2) one();\ntwo();\nthree();');
294
+
295
+ self.options.indent_size = 1;
296
+ self.options.indent_char = ' ';
297
+ bt('{ one_char() }', "{\n one_char()\n}");
298
+
299
+ bt('var a,b=1,c=2', 'var a, b = 1,\n c = 2');
300
+
301
+ self.options.indent_size = 4;
302
+ self.options.indent_char = ' ';
303
+ bt('{ one_char() }', "{\n one_char()\n}");
304
+
305
+ self.options.indent_size = 1;
306
+ self.options.indent_char = "\t";
307
+ bt('{ one_char() }', "{\n\tone_char()\n}");
308
+ bt('x = a ? b : c; x;', 'x = a ? b : c;\nx;');
309
+
310
+ self.options.indent_size = 4;
311
+ self.options.indent_char = ' ';
312
+
313
+ self.options.preserve_newlines = False;
314
+ bt('var\na=dont_preserve_newlines;', 'var a = dont_preserve_newlines;');
315
+
316
+ # make sure the blank line between function definitions stays
317
+ # even when preserve_newlines = False
318
+ bt('function foo() {\n return 1;\n}\n\nfunction foo() {\n return 1;\n}');
319
+ bt('function foo() {\n return 1;\n}\nfunction foo() {\n return 1;\n}',
320
+ 'function foo() {\n return 1;\n}\n\nfunction foo() {\n return 1;\n}'
321
+ );
322
+ bt('function foo() {\n return 1;\n}\n\n\nfunction foo() {\n return 1;\n}',
323
+ 'function foo() {\n return 1;\n}\n\nfunction foo() {\n return 1;\n}'
324
+ );
325
+
326
+
327
+ self.options.preserve_newlines = True;
328
+ bt('var\na=do_preserve_newlines;', 'var\na = do_preserve_newlines;')
329
+ bt('// a\n// b\n\n// c\n// d')
330
+ bt('if (foo) // comment\n{\n bar();\n}')
331
+
332
+ self.options.keep_array_indentation = True;
333
+
334
+ test_fragment('var a = [\n// comment:\n{\n foo:bar\n}\n];', 'var a = [\n // comment:\n{\n foo: bar\n}\n];')
335
+
336
+
337
+ bt('var x = [{}\n]', 'var x = [{}\n]');
338
+ bt('var x = [{foo:bar}\n]', 'var x = [{\n foo: bar\n}\n]');
339
+ bt("a = ['something',\n'completely',\n'different'];\nif (x);", "a = ['something',\n 'completely',\n 'different'];\nif (x);");
340
+ bt("a = ['a','b','c']", "a = ['a', 'b', 'c']");
341
+ bt("a = ['a', 'b','c']", "a = ['a', 'b', 'c']");
342
+
343
+ bt("x = [{'a':0}]", "x = [{\n 'a': 0\n}]");
344
+
345
+ bt('{a([[a1]], {b;});}', '{\n a([[a1]], {\n b;\n });\n}');
346
+
347
+ bt('a = //comment\n/regex/;');
348
+
349
+ test_fragment('/*\n * X\n */');
350
+ test_fragment('/*\r\n * X\r\n */', '/*\n * X\n */');
351
+
352
+ bt('if (a)\n{\nb;\n}\nelse\n{\nc;\n}', 'if (a) {\n b;\n} else {\n c;\n}');
353
+
354
+ bt('var a = new function();');
355
+ test_fragment('new function');
356
+ bt('var a =\nfoo', 'var a = foo');
357
+
358
+ self.options.brace_style = 'expand';
359
+
360
+ bt("throw {}")
361
+ bt("throw {\n foo;\n}")
362
+
363
+ bt('var a=1,b={foo:2,bar:3},c=4;', 'var a = 1,\n b = {\n foo: 2,\n bar: 3\n },\n c = 4;');
364
+ bt('if (a)\n{\nb;\n}\nelse\n{\nc;\n}', 'if (a)\n{\n b;\n}\nelse\n{\n c;\n}');
365
+ test_fragment('if (foo) {', 'if (foo)\n{');
366
+ test_fragment('foo {', 'foo\n{');
367
+ test_fragment('return {', 'return {'); # return needs the brace. maybe something else as well: feel free to report.
368
+ # test_fragment('return\n{', 'return\n{'); # can't support this?, but that's an improbable and extreme case anyway.
369
+ test_fragment('return;\n{', 'return;\n{');
370
+
371
+ bt('if (a)\n{\nb;\n}\nelse\n{\nc;\n}', 'if (a)\n{\n b;\n}\nelse\n{\n c;\n}');
372
+ bt('var foo = {}');
373
+ bt('if (a)\n{\nb;\n}\nelse\n{\nc;\n}', 'if (a)\n{\n b;\n}\nelse\n{\n c;\n}');
374
+ test_fragment('if (foo) {', 'if (foo)\n{');
375
+ test_fragment('foo {', 'foo\n{');
376
+ test_fragment('return {', 'return {'); # return needs the brace. maybe something else as well: feel free to report.
377
+ # test_fragment('return\n{', 'return\n{'); # can't support this?, but that's an improbable and extreme case anyway.
378
+ test_fragment('return;\n{', 'return;\n{');
379
+
380
+ self.options.brace_style = 'collapse';
381
+
382
+ bt('if (a)\n{\nb;\n}\nelse\n{\nc;\n}', 'if (a) {\n b;\n} else {\n c;\n}');
383
+ test_fragment('if (foo) {', 'if (foo) {');
384
+ test_fragment('foo {', 'foo {');
385
+ test_fragment('return {', 'return {'); # return needs the brace. maybe something else as well: feel free to report.
386
+ # test_fragment('return\n{', 'return\n{'); # can't support this?, but that's an improbable and extreme case anyway.
387
+ test_fragment('return;\n{', 'return; {');
388
+
389
+ bt('if (foo) bar();\nelse break');
390
+ bt('function x() {\n foo();\n}zzz', 'function x() {\n foo();\n}\nzzz');
391
+ bt('a: do {} while (); xxx', 'a: do {} while ();\nxxx');
392
+
393
+ self.options.brace_style = "end-expand";
394
+
395
+ bt('if(1){2}else{3}', "if (1) {\n 2\n}\nelse {\n 3\n}");
396
+ bt('try{a();}catch(b){c();}finally{d();}', "try {\n a();\n}\ncatch (b) {\n c();\n}\nfinally {\n d();\n}");
397
+ bt('if(a){b();}else if(c) foo();', "if (a) {\n b();\n}\nelse if (c) foo();");
398
+ bt("if (a) {\n// comment\n}else{\n// comment\n}", "if (a) {\n // comment\n}\nelse {\n // comment\n}"); # if/else statement with empty body
399
+ bt('if (x) {y} else { if (x) {y}}', 'if (x) {\n y\n}\nelse {\n if (x) {\n y\n }\n}');
400
+ bt('if (a)\n{\nb;\n}\nelse\n{\nc;\n}', 'if (a) {\n b;\n}\nelse {\n c;\n}');
401
+
402
+ bt('if (foo) {}\nelse /regex/.test();')
403
+ # bt('if (foo) /regex/.test();') # doesn't work, detects as a division. should it work?
404
+
405
+ bt('a = <?= external() ?> ;'); # not the most perfect thing in the world, but you're the weirdo beaufifying php mix-ins with javascript beautifier
406
+ bt('a = <%= external() %> ;');
407
+
408
+ test_fragment('roo = {\n /*\n ****\n FOO\n ****\n */\n BAR: 0\n};');
409
+ test_fragment("if (..) {\n // ....\n}\n(function");
410
+
411
+ bt('"foo""bar""baz"', '"foo"\n"bar"\n"baz"')
412
+ bt("'foo''bar''baz'", "'foo'\n'bar'\n'baz'")
413
+ bt("{\n get foo() {}\n}")
414
+ bt("{\n var a = get\n foo();\n}")
415
+ bt("{\n set foo() {}\n}")
416
+ bt("{\n var a = set\n foo();\n}")
417
+ bt("var x = {\n get function()\n}")
418
+ bt("var x = {\n set function()\n}")
419
+ bt("var x = set\n\nfunction() {}")
420
+
421
+ bt('<!-- foo\nbar();\n-->')
422
+ bt('<!-- dont crash')
423
+ bt('for () /abc/.test()')
424
+ bt('if (k) /aaa/m.test(v) && l();')
425
+ bt('switch (true) {\ncase /swf/i.test(foo):\n bar();\n}')
426
+ bt('createdAt = {\n type: Date,\n default: Date.now\n}')
427
+ bt('switch (createdAt) {\ncase a:\n Date,\ndefault:\n Date.now\n}')
428
+
429
+ bt('3.*7;', '3. * 7;')
430
+ bt('import foo.*;', 'import foo.*;') # actionscript's import
431
+ test_fragment('function f(a: a, b: b)') # actionscript
432
+
433
+ def decodesto(self, input, expectation=None):
434
+ self.assertEqual(
435
+ jsbeautifier.beautify(input, self.options), expectation or input)
436
+
437
+ def wrap(self, text):
438
+ return self.wrapregex.sub(' \\1', text)
439
+
440
+ def bt(self, input, expectation=None):
441
+ expectation = expectation or input
442
+ self.decodesto(input, expectation)
443
+ if self.options.indent_size == 4 and input:
444
+ wrapped_input = '{\n%s\nfoo=bar;}' % input
445
+ wrapped_expect = '{\n%s\n foo = bar;\n}' % self.wrap(expectation)
446
+ self.decodesto(wrapped_input, wrapped_expect)
447
+
448
+ @classmethod
449
+ def setUpClass(cls):
450
+ options = jsbeautifier.default_options()
451
+ options.indent_size = 4
452
+ options.indent_char = ' '
453
+ options.preserve_newlines = True
454
+ options.jslint_happy = False
455
+ options.keep_array_indentation = False
456
+ options.brace_style = 'collapse'
457
+ options.indent_level = 0
458
+
459
+ cls.options = options
460
+ cls.wrapregex = re.compile('^(.+)$', re.MULTILINE)
461
+
462
+
463
+ if __name__ == '__main__':
464
+ unittest.main()