js-beautify 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,476 @@
1
+ <?php
2
+ require('jsbeautifier.php');
3
+
4
+ $opts = new BeautifierOptions();
5
+ $indent_size = 4;
6
+ $indent_char = ' ';
7
+ $preserve_newlines = true;
8
+ $jslint_happy = false;
9
+ $keep_array_indentation = false;
10
+ $brace_style = 'collapse';
11
+
12
+ /**
13
+ * Main test
14
+ *
15
+ * @param $input
16
+ * @param $expected
17
+ */
18
+ function st_test($input, $expected)
19
+ {
20
+ global $opts;
21
+
22
+ $debug = false;
23
+
24
+ if ($debug)
25
+ {
26
+ echo "=[ INPUT ]===\n" . $input . "\n";
27
+ echo "=[ EXPECTED ]===\n" . $expected . "\n";
28
+ }
29
+
30
+ $jsbeautifier = new JSBeautifier();
31
+ $result = $jsbeautifier->beautify($input, $opts);
32
+
33
+ if ($expected != $result)
34
+ {
35
+ if ( ! $debug)
36
+ {
37
+ echo "=[ INPUT ]===\n" . $input . "\n";
38
+ echo "=[ EXPECTED ]===\n" . $expected . "\n";
39
+ }
40
+ echo "=[ RESULT ]===\n" . $result . "\n=[ RESULT ]===\n";
41
+ echo("Test did not pass!\n");
42
+ for ($i = 0; $i < strlen($expected); $i++) {
43
+ if ( ! isset($result[$i]))
44
+ {
45
+ echo "Result too short!\n";
46
+ break;
47
+ }
48
+ elseif ($expected[$i] != $result[$i])
49
+ {
50
+ //echo "Diff at position #$i!\n";
51
+ }
52
+ }
53
+ die();
54
+ }
55
+ }
56
+
57
+ /**
58
+ * test the input on beautifier with the current flag settings
59
+ * does not check the indentation / surroundings as bt() does
60
+ *
61
+ * @param $input
62
+ * @param bool $expected
63
+ */
64
+ function test_fragment($input, $expected = false)
65
+ {
66
+ $expected = $expected ?: $input;
67
+ st_test($input, $expected);
68
+ }
69
+
70
+ /**
71
+ * test the input on beautifier with the current flag settings
72
+ * test both the input as well as { input } wrapping
73
+ * @param $input
74
+ * @param bool $expectation
75
+ */
76
+ function bt($input, $expectation = false)
77
+ {
78
+ global $opts;
79
+
80
+ $wrapped_input = false;
81
+ $wrapped_expectation = false;
82
+
83
+ $expectation = $expectation ?: $input;
84
+ test_fragment($input, $expectation);
85
+
86
+ // test also the returned indentation
87
+ // e.g if input = "asdf();"
88
+ // then test that this remains properly formatted as well:
89
+ // {
90
+ // asdf();
91
+ // indent;
92
+ // }
93
+
94
+ if ($opts->indent_size === 4 && $input)
95
+ {
96
+ $wrapped_input = "{\n" . $input . "\nfoo=bar;}";
97
+ $wrapped_expectation = "{\n" . preg_replace('/^(.+)$/m', ' \1', $expectation) . "\n foo = bar;\n}";
98
+ test_fragment($wrapped_input, $wrapped_expectation);
99
+ }
100
+ }
101
+
102
+ // make sure the blank line between function definitions stays
103
+ // even when preserve_newlines = false
104
+
105
+
106
+ /* Tests themselves */
107
+ bt('');
108
+ bt('return .5');
109
+ test_fragment(' return .5');
110
+ bt('a = 1', 'a = 1');
111
+ bt('a=1', 'a = 1');
112
+ bt("a();\n\nb();", "a();\n\nb();");
113
+ bt('var a = 1 var b = 2', "var a = 1\nvar b = 2");
114
+ bt('var a=1, b=c[d], e=6;', 'var a = 1,'."\n".' b = c[d],'."\n".' e = 6;');
115
+ bt('a = " 12345 "');
116
+ bt("a = ' 12345 '");
117
+ bt('if (a == 1) b = 2;', "if (a == 1) b = 2;");
118
+ bt('if(1){2}else{3}', "if (1) {\n 2\n} else {\n 3\n}");
119
+ bt('if (foo) bar();'."\n".'else'."\n".'car();', 'if (foo) bar();'."\n".'else car();');
120
+ bt('if(1||2);', 'if (1 || 2);');
121
+ bt('(a==1)||(b==2)', '(a == 1) || (b == 2)');
122
+ bt('var a = 1 if (2) 3;', "var a = 1\nif (2) 3;");
123
+ bt('a = a + 1');
124
+ bt('a = a == 1');
125
+ bt('/12345[^678]*9+/.match(a)');
126
+ bt('a /= 5');
127
+ bt('a = 0.5 * 3');
128
+ bt('a *= 10.55');
129
+ bt('a < .5');
130
+ bt('a <= .5');
131
+ bt('a<.5', 'a < .5');
132
+ bt('a<=.5', 'a <= .5');
133
+ bt('a = 0xff;');
134
+ bt('a=0xff+4', 'a = 0xff + 4');
135
+ bt('a = [1, 2, 3, 4]');
136
+ bt('F*(g/=f)*g+b', 'F * (g /= f) * g + b');
137
+ bt('a.b({c:d})', "a.b({\n c: d\n})");
138
+ bt('a.b'."\n".'('."\n".'{'."\n".'c:'."\n".'d'."\n".'}'."\n".')', "a.b({\n c: d\n})");
139
+ bt('a=!b', 'a = !b');
140
+ bt('a?b:c', 'a ? b : c');
141
+ bt('a?1:2', 'a ? 1 : 2');
142
+ bt('a?(b):c', 'a ? (b) : c');
143
+ 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".'}');
144
+ bt('x=a?b?c?d:e:f:g;', 'x = a ? b ? c ? d : e : f : g;');
145
+ 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;');
146
+ bt('function void(void) {}');
147
+ bt('if(!a)foo();', 'if (!a) foo();');
148
+ bt('a=~a', 'a = ~a');
149
+ bt('a;/*comment*/b;', "a; /*comment*/\nb;");
150
+ bt('a;/* comment */b;', "a; /* comment */\nb;");
151
+ test_fragment('a;/*'."\n".'comment'."\n".'*/b;', "a;\n/*\ncomment\n*/\nb;"); // simple comments don't get touched at all
152
+ bt('a;/**'."\n".'* javadoc'."\n".'*/b;', "a;\n/**\n * javadoc\n */\nb;");
153
+ test_fragment('a;/**'."\n".''."\n".'no javadoc'."\n".'*/b;', "a;\n/**\n\nno javadoc\n*/\nb;");
154
+ bt('a;/*'."\n".'* javadoc'."\n".'*/b;', "a;\n/*\n * javadoc\n */\nb;"); // comment blocks detected and reindented even w/o javadoc starter
155
+ bt('if(a)break;', "if (a) break;");
156
+ bt('if(a){break}', "if (a) {\n break\n}");
157
+ bt('if((a))foo();', 'if ((a)) foo();');
158
+ bt('for(var i=0;;)', 'for (var i = 0;;)');
159
+ bt('a++;', 'a++;');
160
+ bt('for(;;i++)', 'for (;; i++)');
161
+ bt('for(;;++i)', 'for (;; ++i)');
162
+ bt('return(1)', 'return (1)');
163
+ bt('try{a();}catch(b){c();}finally{d();}', "try {\n a();\n} catch (b) {\n c();\n} finally {\n d();\n}");
164
+ bt('(xx)()'); // magic function call
165
+ bt('a[1]()'); // another magic function call
166
+ bt('if(a){b();}else if(c) foo();', "if (a) {\n b();\n} else if (c) foo();");
167
+ 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}");
168
+ bt('switch(x){case -1:break;case !y:break;}', 'switch (x) {'."\n".'case -1:'."\n".' break;'."\n".'case !y:'."\n".' break;'."\n".'}');
169
+ bt('a !== b');
170
+ bt('if (a) b(); else c();', "if (a) b();\nelse c();");
171
+ bt("// comment\n(function something() {})"); // typical greasemonkey start
172
+ bt("{\n\n x();\n\n}"); // was: duplicating newlines
173
+ bt('if (a in b) foo();');
174
+ // bt('var a, b');
175
+ bt('{a:1, b:2}', "{\n a: 1,\n b: 2\n}");
176
+ bt('a={1:[-1],2:[+1]}', 'a = {'."\n".' 1: [-1],'."\n".' 2: [+1]'."\n".'}');
177
+ bt('var l = {\'a\':\'1\', \'b\':\'2\'}', "var l = {\n 'a': '1',\n 'b': '2'\n}");
178
+ bt('if (template.user[n] in bk) foo();');
179
+ bt('{{}/z/}', "{\n {}\n /z/\n}");
180
+ bt('return 45', "return 45");
181
+ bt('If[1]', "If[1]");
182
+ bt('Then[1]', "Then[1]");
183
+ bt('a = 1e10', "a = 1e10");
184
+ bt('a = 1.3e10', "a = 1.3e10");
185
+ bt('a = 1.3e-10', "a = 1.3e-10");
186
+ bt('a = -1.3e-10', "a = -1.3e-10");
187
+ bt('a = 1e-10', "a = 1e-10");
188
+ bt('a = e - 10', "a = e - 10");
189
+ bt('a = 11-10', "a = 11 - 10");
190
+ bt("a = 1;// comment\n", "a = 1; // comment");
191
+ bt("a = 1; // comment\n", "a = 1; // comment");
192
+ bt("a = 1;\n // comment\n", "a = 1;\n// comment");
193
+
194
+ bt('o = [{a:b},{c:d}]', 'o = [{'."\n".' a: b'."\n".'}, {'."\n".' c: d'."\n".'}]');
195
+
196
+ bt("if (a) {\n do();\n}"); // was: extra space appended
197
+ bt("if\n(a)\nb();", "if (a) b();"); // test for proper newline removal
198
+
199
+ 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
200
+ bt("if (a) {\n// comment\n// comment\n}", "if (a) {\n // comment\n // comment\n}"); // multiple comments indentation
201
+ bt("if (a) b() else c();", "if (a) b()\nelse c();");
202
+ bt("if (a) b() else if c() d();", "if (a) b()\nelse if c() d();");
203
+
204
+ bt("{}");
205
+ bt("{\n\n}");
206
+ bt("do { a(); } while ( 1 );", "do {\n a();\n} while (1);");
207
+ bt("do {} while (1);");
208
+ bt("do {\n} while (1);", "do {} while (1);");
209
+ bt("do {\n\n} while (1);");
210
+ bt("var a = x(a, b, c)");
211
+ bt("delete x if (a) b();", "delete x\nif (a) b();");
212
+ bt("delete x[x] if (a) b();", "delete x[x]\nif (a) b();");
213
+ bt("for(var a=1,b=2)", "for (var a = 1, b = 2)");
214
+ bt("for(var a=1,b=2,c=3)", "for (var a = 1, b = 2, c = 3)");
215
+ bt("for(var a=1,b=2,c=3;d<3;d++)", "for (var a = 1, b = 2, c = 3; d < 3; d++)");
216
+ bt("function x(){(a||b).c()}", "function x() {\n (a || b).c()\n}");
217
+ bt("function x(){return - 1}", "function x() {\n return -1\n}");
218
+ bt("function x(){return ! a}", "function x() {\n return !a\n}");
219
+
220
+ // a common snippet in jQuery plugins
221
+ bt("settings = $.extend({},defaults,settings);", "settings = $.extend({}, defaults, settings);");
222
+
223
+ bt('{xxx;}()', '{'."\n".' xxx;'."\n".'}()');
224
+
225
+ bt("a = 'a'\nb = 'b'");
226
+ bt("a = /reg/exp");
227
+ bt("a = /reg/");
228
+ bt('/abc/.test()');
229
+ bt('/abc/i.test()');
230
+ bt("{/abc/i.test()}", "{\n /abc/i.test()\n}");
231
+ bt('var x=(a)/a;', 'var x = (a) / a;');
232
+
233
+ bt('x != -1', 'x != -1');
234
+
235
+ bt('for (; s-->0;)', 'for (; s-- > 0;)');
236
+ bt('for (; s++>0;)', 'for (; s++ > 0;)');
237
+ bt('a = s++>s--;', 'a = s++ > s--;');
238
+ bt('a = s++>--s;', 'a = s++ > --s;');
239
+
240
+ bt('{x=#1=[]}', '{'."\n".' x = #1=[]'."\n".'}');
241
+ bt('{a:#1={}}', '{'."\n".' a: #1={}'."\n".'}');
242
+ bt('{a:#1#}', '{'."\n".' a: #1#'."\n".'}');
243
+
244
+ test_fragment('{a:1},{a:2}', '{'."\n".' a: 1'."\n".'}, {'."\n".' a: 2'."\n".'}');
245
+ test_fragment('var ary=[{a:1}, {a:2}];', 'var ary = [{'."\n".' a: 1'."\n".'}, {'."\n".' a: 2'."\n".'}];');
246
+
247
+ test_fragment('{a:#1', '{'."\n".' a: #1'); // incomplete
248
+ test_fragment('{a:#', '{'."\n".' a: #'); // incomplete
249
+
250
+ test_fragment('}}}', '}'."\n".'}'."\n".'}'); // incomplete
251
+
252
+ test_fragment('<!--'."\n".'void();'."\n".'// -->', '<!--'."\n".'void();'."\n".'// -->');
253
+
254
+ test_fragment('a=/regexp', 'a = /regexp'); // incomplete regexp
255
+
256
+ bt('{a:#1=[],b:#1#,c:#999999#}', '{'."\n".' a: #1=[],'."\n".' b: #1#,'."\n".' c: #999999#'."\n".'}');
257
+
258
+ bt("a = 1e+2");
259
+ bt("a = 1e-2");
260
+ bt("do{x()}while(a>1)", "do {\n x()\n} while (a > 1)");
261
+
262
+ bt("x(); /reg/exp.match(something)", "x();\n/reg/exp.match(something)");
263
+
264
+ test_fragment("something();(", "something();\n(");
265
+ test_fragment("#!she/bangs, she bangs\nf=1", "#!she/bangs, she bangs\n\nf = 1");
266
+ test_fragment("#!she/bangs, she bangs\n\nf=1", "#!she/bangs, she bangs\n\nf = 1");
267
+ test_fragment("#!she/bangs, she bangs\n\n/* comment */", "#!she/bangs, she bangs\n\n/* comment */");
268
+ test_fragment("#!she/bangs, she bangs\n\n\n/* comment */", "#!she/bangs, she bangs\n\n\n/* comment */");
269
+ test_fragment("#", "#");
270
+ test_fragment("#!", "#!");
271
+
272
+ bt("function namespace::something()");
273
+
274
+ test_fragment("<!--\nsomething();\n-->", "<!--\nsomething();\n-->");
275
+ test_fragment("<!--\nif(i<0){bla();}\n-->", "<!--\nif (i < 0) {\n bla();\n}\n-->");
276
+
277
+ test_fragment("<!--\nsomething();\n-->\n<!--\nsomething();\n-->", "<!--\nsomething();\n-->\n<!--\nsomething();\n-->");
278
+ test_fragment("<!--\nif(i<0){bla();}\n-->\n<!--\nif(i<0){bla();}\n-->", "<!--\nif (i < 0) {\n bla();\n}\n-->\n<!--\nif (i < 0) {\n bla();\n}\n-->");
279
+
280
+ bt('{foo();--bar;}', '{'."\n".' foo();'."\n".' --bar;'."\n".'}');
281
+ bt('{foo();++bar;}', '{'."\n".' foo();'."\n".' ++bar;'."\n".'}');
282
+ bt('{--bar;}', '{'."\n".' --bar;'."\n".'}');
283
+ bt('{++bar;}', '{'."\n".' ++bar;'."\n".'}');
284
+
285
+ // regexps
286
+ bt('a(/abc\\/\\/def/);b()', "a(/abc\\/\\/def/);\nb()");
287
+ bt('a(/a[b\\[\\]c]d/);b()', "a(/a[b\\[\\]c]d/);\nb()");
288
+ test_fragment('a(/a[b\\[', "a(/a[b\\["); // incomplete char class
289
+ // allow unescaped / in char classes
290
+ bt('a(/[a/b]/);b()', "a(/[a/b]/);\nb()");
291
+
292
+ bt('a=[[1,2],[4,5],[7,8]]', "a = [\n [1, 2],\n [4, 5],\n [7, 8]\n]");
293
+ bt('a=[a[1],b[4],c[d[7]]]', "a = [a[1], b[4], c[d[7]]]");
294
+ bt('[1,2,[3,4,[5,6],7],8]', "[1, 2, [3, 4, [5, 6], 7], 8]");
295
+
296
+ bt('[[["1","2"],["3","4"]],[["5","6","7"],["8","9","0"]],[["1","2","3"],["4","5","6","7"],["8","9","0"]]]',
297
+ '['."\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".']');
298
+
299
+ bt('{[x()[0]];indent;}', '{'."\n".' [x()[0]];'."\n".' indent;'."\n".'}');
300
+
301
+ bt('return ++i', 'return ++i');
302
+ bt('return !!x', 'return !!x');
303
+ bt('return !x', 'return !x');
304
+ bt('return [1,2]', 'return [1, 2]');
305
+ bt('return;', 'return;');
306
+ bt('return'."\n".'func', 'return'."\n".'func');
307
+ bt('catch(e)', 'catch (e)');
308
+
309
+ 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;');
310
+
311
+ // inline comment
312
+ bt('function x(/*int*/ start, /*string*/ foo)', 'function x( /*int*/ start, /*string*/ foo)');
313
+
314
+ // javadoc comment
315
+ bt('/**'."\n".'* foo'."\n".'*/', '/**'."\n".' * foo'."\n".' */');
316
+ bt('{'."\n".'/**'."\n".'* foo'."\n".'*/'."\n".'}', '{'."\n".' /**'."\n".' * foo'."\n".' */'."\n".'}');
317
+
318
+ bt('var a,b,c=1,d,e,f=2;', 'var a, b, c = 1,'."\n".' d, e, f = 2;');
319
+ bt('var a,b,c=[],d,e,f=2;', 'var a, b, c = [],'."\n".' d, e, f = 2;');
320
+
321
+ bt('do/regexp/;'."\n".'while(1);', 'do /regexp/;'."\n".'while (1);'); // hmmm
322
+
323
+ bt('var a = a,'."\n".'a;'."\n".'b = {'."\n".'b'."\n".'}', 'var a = a,'."\n".' a;'."\n".'b = {'."\n".' b'."\n".'}');
324
+
325
+ bt('var a = a,'."\n".' /* c */'."\n".' b;');
326
+ bt('var a = a,'."\n".' // c'."\n".' b;');
327
+
328
+ bt('foo.("bar");'); // weird element referencing
329
+
330
+
331
+ bt('if (a) a()'."\n".'else b()'."\n".'newline()');
332
+ bt('if (a) a()'."\n".'newline()');
333
+ bt('a=typeof(x)', 'a = typeof(x)');
334
+ bt('var a = function() {'."\n".' return null;'."\n".' },'."\n".' b = false;');
335
+
336
+ bt('var a = function() {'."\n".' func1()'."\n".' }');
337
+ bt('var a = function() {'."\n".' func1()'."\n".' }'."\n".'var b = function() {'."\n".' func2()'."\n".' }');
338
+
339
+
340
+
341
+ $opts->jslint_happy = true;
342
+
343
+ bt('a=typeof(x)', 'a = typeof (x)');
344
+ bt('x();'."\n".''."\n".'function(){}', 'x();'."\n".''."\n".'function () {}');
345
+ bt('function () {'."\n".' var a, b, c, d, e = [],'."\n".' f;'."\n".'}');
346
+ test_fragment("// comment 1\n(function()", "// comment 1\n(function ()"); // typical greasemonkey start
347
+ bt("var a1, b1, c1, d1 = 0, c = function() {}, d = '';", "var a1, b1, c1, d1 = 0,\n c = function () {},\n d = '';");
348
+ bt('var o1=$.extend(a);function(){alert(x);}', 'var o1 = $.extend(a);'."\n".''."\n".'function () {'."\n".' alert(x);'."\n".'}');
349
+
350
+ $opts->jslint_happy = false;
351
+
352
+ test_fragment("// comment 2\n(function()", "// comment 2\n(function()"); // typical greasemonkey start
353
+ bt("var a2, b2, c2, d2 = 0, c = function() {}, d = '';", "var a2, b2, c2, d2 = 0,\n c = function() {},\n d = '';");
354
+ bt('var o2=$.extend(a);function(){alert(x);}', 'var o2 = $.extend(a);'."\n".''."\n".'function() {'."\n".' alert(x);'."\n".'}');
355
+
356
+ 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".'}');
357
+
358
+ bt('{"1":{"1a":"1b"},"2"}', '{'."\n".' "1": {'."\n".' "1a": "1b"'."\n".' },'."\n".' "2"'."\n".'}');
359
+ bt('{a:{a:b},c}', '{'."\n".' a: {'."\n".' a: b'."\n".' },'."\n".' c'."\n".'}');
360
+
361
+ bt('{[y[a]];keep_indent;}', '{'."\n".' [y[a]];'."\n".' keep_indent;'."\n".'}');
362
+
363
+ bt('if (x) {y} else { if (x) {y}}', 'if (x) {'."\n".' y'."\n".'} else {'."\n".' if (x) {'."\n".' y'."\n".' }'."\n".'}');
364
+
365
+ bt('if (foo) one()'."\n".'two()'."\n".'three()');
366
+ bt('if (1 + foo() && bar(baz()) / 2) one()'."\n".'two()'."\n".'three()');
367
+ bt('if (1 + foo() && bar(baz()) / 2) one();'."\n".'two();'."\n".'three();');
368
+
369
+ $opts->indent_size = 1;
370
+ $opts->indent_char = ' ';
371
+ bt('{ one_char() }', "{\n one_char()\n}");
372
+
373
+ bt('var a,b=1,c=2', 'var a, b = 1,'."\n".' c = 2');
374
+
375
+ $opts->indent_size = 4;
376
+ $opts->indent_char = ' ';
377
+ bt('{ one_char() }', "{\n one_char()\n}");
378
+
379
+ $opts->indent_size = 1;
380
+ $opts->indent_char = "\t";
381
+ bt('{ one_char() }', "{\n\tone_char()\n}");
382
+ bt('x = a ? b : c; x;', 'x = a ? b : c;'."\n".'x;');
383
+
384
+ $opts->indent_size = 4;
385
+ $opts->indent_char = ' ';
386
+
387
+ $opts->preserve_newlines = false;
388
+ bt('var'."\n".'a=dont_preserve_newlines;', 'var a = dont_preserve_newlines;');
389
+
390
+ // make sure the blank line between function definitions stays
391
+ // even when preserve_newlines = false
392
+ bt('function foo() {'."\n".' return 1;'."\n".'}'."\n\n".'function foo() {'."\n".' return 1;'."\n".'}');
393
+ bt('function foo() {'."\n".' return 1;'."\n".'}'."\n".'function foo() {'."\n".' return 1;'."\n".'}',
394
+ 'function foo() {'."\n".' return 1;'."\n".'}'."\n\n".'function foo() {'."\n".' return 1;'."\n".'}'
395
+ );
396
+ bt('function foo() {'."\n".' return 1;'."\n".'}'."\n\n\n".'function foo() {'."\n".' return 1;'."\n".'}',
397
+ 'function foo() {'."\n".' return 1;'."\n".'}'."\n\n".'function foo() {'."\n".' return 1;'."\n".'}'
398
+ );
399
+
400
+
401
+ $opts->preserve_newlines = true;
402
+ bt('var'."\n".'a=do_preserve_newlines;', 'var'."\n".'a = do_preserve_newlines;');
403
+
404
+ $opts->keep_array_indentation = true;
405
+
406
+ // do not like keep_array_indentation at all
407
+ test_fragment('var a = ['."\n".'// comment:'."\n".'{'."\n".' foo:bar'."\n".'}'."\n".'];', 'var a = ['."\n".' // comment:'."\n".' {'."\n".' foo: bar'."\n".'}'."\n".'];');
408
+
409
+ bt('var x = [{}'."\n".']', 'var x = [{}'."\n".']');
410
+ bt('var x = [{foo:bar}'."\n".']', 'var x = [{'."\n".' foo: bar'."\n".'}'."\n".']');
411
+ bt("a = ['something',\n'completely',\n'different'];\nif (x);", "a = ['something',\n 'completely',\n 'different'];\nif (x);");
412
+ bt("a = ['a','b','c']", "a = ['a', 'b', 'c']");
413
+ bt("a = ['a', 'b','c']", "a = ['a', 'b', 'c']");
414
+
415
+ bt("x = [{'a':0}]", "x = [{\n 'a': 0\n}]");
416
+
417
+ bt('{a([[a1]], {b;});}', '{'."\n".' a([[a1]], {'."\n".' b;'."\n".' });'."\n".'}');
418
+
419
+ bt('a = //comment'."\n".'/regex/;');
420
+
421
+ test_fragment('/*'."\n".' * X'."\n".' */');
422
+ test_fragment('/*'."\r\n".' * X'."\r\n".' */', '/*'."\n".' * X'."\n".' */');
423
+
424
+ bt("if (a)\n{\nb;\n}\nelse\n{\nc;\n}", "if (a) {\n b;\n} else {\n c;\n}");
425
+
426
+
427
+ $opts->brace_style = 'expand';
428
+
429
+ 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;');
430
+ bt('if (a)'."\n".'{'."\n".'b;'."\n".'}'."\n".'else'."\n".'{'."\n".'c;'."\n".'}', 'if (a)'."\n".'{'."\n".' b;'."\n".'}'."\n".'else'."\n".'{'."\n".' c;'."\n".'}');
431
+ test_fragment('if (foo) {', 'if (foo)'."\n".'{');
432
+ test_fragment('foo {', 'foo'."\n".'{');
433
+ test_fragment('return {', 'return {'); // return needs the brace. maybe something else as well: feel free to report.
434
+ // test_fragment('return\n{', 'return\n{'); // can't support this?, but that's an improbable and extreme case anyway.
435
+ test_fragment('return;'."\n".'{', 'return;'."\n".'{');
436
+
437
+ bt('if (a)'."\n".'{'."\n".'b;'."\n".'}'."\n".'else'."\n".'{'."\n".'c;'."\n".'}', 'if (a)'."\n".'{'."\n".' b;'."\n".'}'."\n".'else'."\n".'{'."\n".' c;'."\n".'}');
438
+ bt('var foo = {}');
439
+ bt('if (a)'."\n".'{'."\n".'b;'."\n".'}'."\n".'else'."\n".'{'."\n".'c;'."\n".'}', 'if (a)'."\n".'{'."\n".' b;'."\n".'}'."\n".'else'."\n".'{'."\n".' c;'."\n".'}');
440
+ test_fragment('if (foo) {', 'if (foo)'."\n".'{');
441
+ test_fragment('foo {', 'foo'."\n".'{');
442
+ test_fragment('return {', 'return {'); // return needs the brace. maybe something else as well: feel free to report.
443
+ // test_fragment('return\n{', 'return\n{'); // can't support this?, but that's an improbable and extreme case anyway.
444
+ test_fragment('return;'."\n".'{', 'return;'."\n".'{');
445
+
446
+
447
+ $opts->brace_style = 'collapse';
448
+
449
+ bt('if (a)'."\n".'{'."\n".'b;'."\n".'}'."\n".'else'."\n".'{'."\n".'c;'."\n".'}', 'if (a) {'."\n".' b;'."\n".'} else {'."\n".' c;'."\n".'}');
450
+ test_fragment('if (foo) {', 'if (foo) {');
451
+ test_fragment('foo {', 'foo {');
452
+ test_fragment('return {', 'return {'); // return needs the brace. maybe something else as well: feel free to report.
453
+ // test_fragment('return\n{', 'return\n{'); // can't support this?, but that's an improbable and extreme case anyway.
454
+ test_fragment('return;'."\n".'{', 'return; {');
455
+
456
+ bt('if (foo) bar();'."\n".'else break');
457
+ bt('function x() {'."\n".' foo();'."\n".'}zzz', 'function x() {'."\n".' foo();'."\n".'}'."\n".'zzz');
458
+ bt('a: do {} while (); xxx', 'a: do {} while ();'."\n".'xxx');
459
+
460
+ bt('var a = new function();');
461
+ test_fragment('new function');
462
+ bt('var a ='."\n".'foo', 'var a = foo');
463
+
464
+ $opts->brace_style = "end-expand";
465
+
466
+ bt('if(1){2}else{3}', "if (1) {\n 2\n}\nelse {\n 3\n}");
467
+ bt('try{a();}catch(b){c();}finally{d();}', "try {\n a();\n}\ncatch (b) {\n c();\n}\nfinally {\n d();\n}");
468
+ bt('if(a){b();}else if(c) foo();', "if (a) {\n b();\n}\nelse if (c) foo();");
469
+ 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
470
+ bt('if (x) {y} else { if (x) {y}}', 'if (x) {'."\n".' y'."\n".'}'."\n".'else {'."\n".' if (x) {'."\n".' y'."\n".' }'."\n".'}');
471
+ bt('if (a)'."\n".'{'."\n".'b;'."\n".'}'."\n".'else'."\n".'{'."\n".'c;'."\n".'}', 'if (a) {'."\n".' b;'."\n".'}'."\n".'else {'."\n".' c;'."\n".'}');
472
+
473
+ test_fragment(' /*'."\n".'* xx'."\n".'*/'."\n".'// xx'."\n".'if (foo) {'."\n".' bar();'."\n".'}', ' /*'."\n".' * xx'."\n".' */'."\n".' // xx'."\n".' if (foo) {'."\n".' bar();'."\n".' }');
474
+
475
+ bt('a = <?= external() ?> ;'); // not the most perfect thing in the world, but you're the weirdo beaufifying php mix-ins with javascript beautifier
476
+ bt('a = <%= external() %> ;');