corrade-nucleus-nucleons – Blame information for rev 2

Subversion Repositories:
Rev:
Rev Author Line No. Line
2 office 1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3  
4 '''
5 AUTO-GENERATED. DO NOT MODIFY.
6 Script: test/generate-tests.js
7 Template: test/data/javascript/python.mustache
8 Data: test/data/javascript/tests.js
9  
10 The MIT License (MIT)
11  
12 Copyright (c) 2007-2017 Einar Lielmanis, Liam Newman, and contributors.
13  
14 Permission is hereby granted, free of charge, to any person
15 obtaining a copy of this software and associated documentation files
16 (the "Software"), to deal in the Software without restriction,
17 including without limitation the rights to use, copy, modify, merge,
18 publish, distribute, sublicense, and/or sell copies of the Software,
19 and to permit persons to whom the Software is furnished to do so,
20 subject to the following conditions:
21  
22 The above copyright notice and this permission notice shall be
23 included in all copies or substantial portions of the Software.
24  
25 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 SOFTWARE.
33 '''
34  
35 import re
36 import unittest
37 import jsbeautifier
38 import six
39 import copy
40  
41 class TestJSBeautifier(unittest.TestCase):
42 options = None
43  
44 @classmethod
45 def setUpClass(cls):
46 true = True
47 false = False
48  
49 default_options = jsbeautifier.default_options()
50 default_options.indent_size = 4
51 default_options.indent_char = ' '
52 default_options.preserve_newlines = True
53 default_options.jslint_happy = False
54 default_options.keep_array_indentation = False
55 default_options.brace_style = 'collapse'
56 default_options.indent_level = 0
57 default_options.break_chained_methods = False
58 default_options.eol = '\n'
59  
60 default_options.indent_size = 4
61 default_options.indent_char = ' '
62 default_options.preserve_newlines = true
63 default_options.jslint_happy = false
64 default_options.keep_array_indentation = false
65 default_options.brace_style = 'collapse'
66 default_options.operator_position = 'before-newline'
67  
68 cls.default_options = default_options
69 cls.wrapregex = re.compile('^(.+)$', re.MULTILINE)
70  
71 def reset_options(self):
72 self.options = copy.copy(self.default_options)
73  
74 def test_unescape(self):
75 # Test cases contributed by <chrisjshull on GitHub.com>
76 test_fragment = self.decodesto
77 self.reset_options()
78 bt = self.bt
79  
80 def unicode_char(value):
81 return six.unichr(value)
82  
83 bt('"\\\\s"') # == "\\s" in the js source
84 bt("'\\\\s'") # == '\\s' in the js source
85 bt("'\\\\\\s'") # == '\\\s' in the js source
86 bt("'\\s'") # == '\s' in the js source
87 bt('"•"')
88 bt('"—"')
89 bt('"\\x41\\x42\\x43\\x01"', '"\\x41\\x42\\x43\\x01"')
90 bt('"\\u2022"', '"\\u2022"')
91 bt('a = /\s+/')
92 #bt('a = /\\x41/','a = /A/')
93 bt('"\\u2022";a = /\s+/;"\\x41\\x42\\x43\\x01".match(/\\x41/);','"\\u2022";\na = /\s+/;\n"\\x41\\x42\\x43\\x01".match(/\\x41/);')
94 test_fragment('"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff and \\xzz","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"', '"\\x22\\x27", \'\\x22\\x27\', "\\x5c", \'\\x5c\', "\\xff and \\xzz", "unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"')
95  
96 self.options.unescape_strings = True
97  
98 bt('"\\x41\\x42\\x43\\x01"', '"ABC\\x01"')
99 test_fragment('"\\x20\\x40\\x4a"', '" @J"');
100 test_fragment('"\\xff\\x40\\x4a"');
101 test_fragment('"\\u0072\\u016B\\u0137\\u012B\\u0074\\u0069\\u0073"', six.u('"\u0072\u016B\u0137\u012B\u0074\u0069\u0073"'));
102  
103 bt('a = /\s+/')
104 test_fragment('"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff"',
105 '"\\"\\\'", \'\\"\\\'\', "\\\\", \'\\\\\', "\\xff", "unicode \\u0000 \\" \\\' \\\\ ' + unicode_char(0xffff) + '"');
106  
107 # For error case, return the string unchanged
108 test_fragment('"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff and \\xzz","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"',
109 '"\\"\\\'", \'\\"\\\'\', "\\\\", \'\\\\\', "\\xff and \\xzz", "unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"');
110  
111 self.options.unescape_strings = False
112  
113 def test_beautifier(self):
114 test_fragment = self.decodesto
115 bt = self.bt
116  
117 true = True
118 false = False
119  
120 def unicode_char(value):
121 return six.unichr(value)
122  
123  
124 #============================================================
125 # Unicode Support
126 self.reset_options();
127 bt('var ' + unicode_char(3232) + '_' + unicode_char(3232) + ' = "hi";')
128 bt(
129 'var ' + unicode_char(228) + 'x = {\n' +
130 ' ' + unicode_char(228) + 'rgerlich: true\n' +
131 '};')
132  
133  
134 #============================================================
135 # Test template and continuation strings
136 self.reset_options();
137 bt('`This is a ${template} string.`')
138 bt(
139 '`This\n' +
140 ' is\n' +
141 ' a\n' +
142 ' ${template}\n' +
143 ' string.`')
144 bt(
145 'a = `This is a continuation\\\n' +
146 'string.`')
147 bt(
148 'a = "This is a continuation\\\n' +
149 'string."')
150 bt(
151 '`SELECT\n' +
152 ' nextval(\'${this.options.schema ? `${this.options.schema}.` : \'\'}"${this.tableName}_${this.autoIncrementField}_seq"\'::regclass\n' +
153 ' ) nextval;`')
154  
155 # Tests for #1030
156 bt(
157 'const composeUrl = (host) => {\n' +
158 ' return `${host `test`}`;\n' +
159 '};')
160 bt(
161 'const composeUrl = (host, api, key, data) => {\n' +
162 ' switch (api) {\n' +
163 ' case "Init":\n' +
164 ' return `${host}/vwapi/Init?VWID=${key}&DATA=${encodeURIComponent(\n' +
165 ' Object.keys(data).map((k) => `${k}=${ data[k]}` ).join(";")\n' +
166 ' )}`;\n' +
167 ' case "Pay":\n' +
168 ' return `${host}/vwapi/Pay?SessionId=${par}`;\n' +
169 ' };\n' +
170 '};')
171  
172  
173 #============================================================
174 # ES7 Decorators
175 self.reset_options();
176 bt('@foo')
177 bt('@foo(bar)')
178 bt(
179 '@foo(function(k, v) {\n' +
180 ' implementation();\n' +
181 '})')
182  
183  
184 #============================================================
185 # ES7 exponential
186 self.reset_options();
187 bt('x ** 2')
188 bt('x ** -2')
189  
190  
191 #============================================================
192 # Spread operator
193 self.reset_options();
194 self.options.brace_style = "collapse,preserve-inline"
195 bt('const m = { ...item, c: 3 };')
196 bt(
197 'const m = {\n' +
198 ' ...item,\n' +
199 ' c: 3\n' +
200 '};')
201 bt('const m = { c: 3, ...item };')
202 bt('const m = [...item, 3];')
203 bt('const m = [3, ...item];')
204  
205  
206 #============================================================
207 # Object literal shorthand functions
208 self.reset_options();
209 bt(
210 'return {\n' +
211 ' foo() {\n' +
212 ' return 42;\n' +
213 ' }\n' +
214 '}')
215 bt(
216 'var foo = {\n' +
217 ' * bar() {\n' +
218 ' yield 42;\n' +
219 ' }\n' +
220 '};')
221 bt(
222 'var foo = {bar(){return 42;},*barGen(){yield 42;}};',
223 # -- output --
224 'var foo = {\n' +
225 ' bar() {\n' +
226 ' return 42;\n' +
227 ' },\n' +
228 ' * barGen() {\n' +
229 ' yield 42;\n' +
230 ' }\n' +
231 '};')
232  
233 # also handle generator shorthand in class - #1013
234 bt(
235 'class A {\n' +
236 ' fn() {\n' +
237 ' return true;\n' +
238 ' }\n' +
239 '\n' +
240 ' * gen() {\n' +
241 ' return true;\n' +
242 ' }\n' +
243 '}')
244 bt(
245 'class A {\n' +
246 ' * gen() {\n' +
247 ' return true;\n' +
248 ' }\n' +
249 '\n' +
250 ' fn() {\n' +
251 ' return true;\n' +
252 ' }\n' +
253 '}')
254  
255  
256 #============================================================
257 # End With Newline - (eof = "\n")
258 self.reset_options();
259 self.options.end_with_newline = true
260 test_fragment('', '\n')
261 test_fragment(' return .5', ' return .5\n')
262 test_fragment(
263 ' \n' +
264 '\n' +
265 'return .5\n' +
266 '\n' +
267 '\n' +
268 '\n',
269 # -- output --
270 ' return .5\n')
271 test_fragment('\n')
272  
273 # End With Newline - (eof = "")
274 self.reset_options();
275 self.options.end_with_newline = false
276 test_fragment('')
277 test_fragment(' return .5')
278 test_fragment(
279 ' \n' +
280 '\n' +
281 'return .5\n' +
282 '\n' +
283 '\n' +
284 '\n',
285 # -- output --
286 ' return .5')
287 test_fragment('\n', '')
288  
289  
290 #============================================================
291 # Support simple language specific option inheritance/overriding - (j = " ")
292 self.reset_options();
293 self.options.js = { 'indent_size': 3 }
294 self.options.css = { 'indent_size': 5 }
295 bt(
296 'if (a == b) {\n' +
297 ' test();\n' +
298 '}')
299  
300 # Support simple language specific option inheritance/overriding - (j = " ")
301 self.reset_options();
302 self.options.html = { 'js': { 'indent_size': 3 }, 'css': { 'indent_size': 5 } }
303 bt(
304 'if (a == b) {\n' +
305 ' test();\n' +
306 '}')
307  
308 # Support simple language specific option inheritance/overriding - (j = " ")
309 self.reset_options();
310 self.options.indent_size = 9
311 self.options.html = { 'js': { 'indent_size': 3 }, 'css': { 'indent_size': 5 }, 'indent_size': 2}
312 self.options.js = { 'indent_size': 4 }
313 self.options.css = { 'indent_size': 3 }
314 bt(
315 'if (a == b) {\n' +
316 ' test();\n' +
317 '}')
318  
319  
320 #============================================================
321 # Brace style permutations - (ibo = "", iao = "", ibc = "", iac = "", obo = " ", oao = " ", obc = " ", oac = " ")
322 self.reset_options();
323 self.options.brace_style = 'collapse,preserve-inline'
324 bt(
325 'var a ={a: 2};\n' +
326 'var a ={a: 2};',
327 # -- output --
328 'var a = { a: 2 };\n' +
329 'var a = { a: 2 };')
330 bt(
331 '//case 1\n' +
332 'if (a == 1){}\n' +
333 '//case 2\n' +
334 'else if (a == 2){}',
335 # -- output --
336 '//case 1\n' +
337 'if (a == 1) {}\n' +
338 '//case 2\n' +
339 'else if (a == 2) {}')
340 bt('if(1){2}else{3}', 'if (1) { 2 } else { 3 }')
341 bt('try{a();}catch(b){c();}catch(d){}finally{e();}', 'try { a(); } catch (b) { c(); } catch (d) {} finally { e(); }')
342  
343 # Brace style permutations - (ibo = "\n", iao = "\n", ibc = "\n", iac = "\n", obo = " ", oao = "\n ", obc = "\n", oac = " ")
344 self.reset_options();
345 self.options.brace_style = 'collapse,preserve-inline'
346 bt(
347 'var a =\n' +
348 '{\n' +
349 'a: 2\n' +
350 '}\n' +
351 ';\n' +
352 'var a =\n' +
353 '{\n' +
354 'a: 2\n' +
355 '}\n' +
356 ';',
357 # -- output --
358 'var a = {\n' +
359 ' a: 2\n' +
360 '};\n' +
361 'var a = {\n' +
362 ' a: 2\n' +
363 '};')
364 bt(
365 '//case 1\n' +
366 'if (a == 1)\n' +
367 '{}\n' +
368 '//case 2\n' +
369 'else if (a == 2)\n' +
370 '{}',
371 # -- output --
372 '//case 1\n' +
373 'if (a == 1) {}\n' +
374 '//case 2\n' +
375 'else if (a == 2) {}')
376 bt(
377 'if(1)\n' +
378 '{\n' +
379 '2\n' +
380 '}\n' +
381 'else\n' +
382 '{\n' +
383 '3\n' +
384 '}',
385 # -- output --
386 'if (1) {\n' +
387 ' 2\n' +
388 '} else {\n' +
389 ' 3\n' +
390 '}')
391 bt(
392 'try\n' +
393 '{\n' +
394 'a();\n' +
395 '}\n' +
396 'catch(b)\n' +
397 '{\n' +
398 'c();\n' +
399 '}\n' +
400 'catch(d)\n' +
401 '{}\n' +
402 'finally\n' +
403 '{\n' +
404 'e();\n' +
405 '}',
406 # -- output --
407 'try {\n' +
408 ' a();\n' +
409 '} catch (b) {\n' +
410 ' c();\n' +
411 '} catch (d) {} finally {\n' +
412 ' e();\n' +
413 '}')
414  
415 # Brace style permutations - (ibo = "", iao = "", ibc = "", iac = "", obo = " ", oao = "\n ", obc = "\n", oac = " ")
416 self.reset_options();
417 self.options.brace_style = 'collapse'
418 bt(
419 'var a ={a: 2};\n' +
420 'var a ={a: 2};',
421 # -- output --
422 'var a = {\n' +
423 ' a: 2\n' +
424 '};\n' +
425 'var a = {\n' +
426 ' a: 2\n' +
427 '};')
428 bt(
429 '//case 1\n' +
430 'if (a == 1){}\n' +
431 '//case 2\n' +
432 'else if (a == 2){}',
433 # -- output --
434 '//case 1\n' +
435 'if (a == 1) {}\n' +
436 '//case 2\n' +
437 'else if (a == 2) {}')
438 bt(
439 'if(1){2}else{3}',
440 # -- output --
441 'if (1) {\n' +
442 ' 2\n' +
443 '} else {\n' +
444 ' 3\n' +
445 '}')
446 bt(
447 'try{a();}catch(b){c();}catch(d){}finally{e();}',
448 # -- output --
449 'try {\n' +
450 ' a();\n' +
451 '} catch (b) {\n' +
452 ' c();\n' +
453 '} catch (d) {} finally {\n' +
454 ' e();\n' +
455 '}')
456  
457 # Brace style permutations - (ibo = "\n", iao = "\n", ibc = "\n", iac = "\n", obo = " ", oao = "\n ", obc = "\n", oac = " ")
458 self.reset_options();
459 self.options.brace_style = 'collapse'
460 bt(
461 'var a =\n' +
462 '{\n' +
463 'a: 2\n' +
464 '}\n' +
465 ';\n' +
466 'var a =\n' +
467 '{\n' +
468 'a: 2\n' +
469 '}\n' +
470 ';',
471 # -- output --
472 'var a = {\n' +
473 ' a: 2\n' +
474 '};\n' +
475 'var a = {\n' +
476 ' a: 2\n' +
477 '};')
478 bt(
479 '//case 1\n' +
480 'if (a == 1)\n' +
481 '{}\n' +
482 '//case 2\n' +
483 'else if (a == 2)\n' +
484 '{}',
485 # -- output --
486 '//case 1\n' +
487 'if (a == 1) {}\n' +
488 '//case 2\n' +
489 'else if (a == 2) {}')
490 bt(
491 'if(1)\n' +
492 '{\n' +
493 '2\n' +
494 '}\n' +
495 'else\n' +
496 '{\n' +
497 '3\n' +
498 '}',
499 # -- output --
500 'if (1) {\n' +
501 ' 2\n' +
502 '} else {\n' +
503 ' 3\n' +
504 '}')
505 bt(
506 'try\n' +
507 '{\n' +
508 'a();\n' +
509 '}\n' +
510 'catch(b)\n' +
511 '{\n' +
512 'c();\n' +
513 '}\n' +
514 'catch(d)\n' +
515 '{}\n' +
516 'finally\n' +
517 '{\n' +
518 'e();\n' +
519 '}',
520 # -- output --
521 'try {\n' +
522 ' a();\n' +
523 '} catch (b) {\n' +
524 ' c();\n' +
525 '} catch (d) {} finally {\n' +
526 ' e();\n' +
527 '}')
528  
529  
530 #============================================================
531 # Comma-first option - (c0 = ",\n", c1 = ",\n ", c2 = ",\n ", c3 = ",\n ", f1 = " ,\n ")
532 self.reset_options();
533 self.options.comma_first = false
534 bt(
535 '{a:1, b:2}',
536 # -- output --
537 '{\n' +
538 ' a: 1,\n' +
539 ' b: 2\n' +
540 '}')
541 bt(
542 'var a=1, b=c[d], e=6;',
543 # -- output --
544 'var a = 1,\n' +
545 ' b = c[d],\n' +
546 ' e = 6;')
547 bt(
548 'for(var a=1,b=2,c=3;d<3;d++)\n' +
549 'e',
550 # -- output --
551 'for (var a = 1, b = 2, c = 3; d < 3; d++)\n' +
552 ' e')
553 bt(
554 'for(var a=1,b=2,\n' +
555 'c=3;d<3;d++)\n' +
556 'e',
557 # -- output --
558 'for (var a = 1, b = 2,\n' +
559 ' c = 3; d < 3; d++)\n' +
560 ' e')
561 bt(
562 'function foo() {\n' +
563 ' return [\n' +
564 ' "one",\n' +
565 ' "two"\n' +
566 ' ];\n' +
567 '}')
568 bt(
569 'a=[[1,2],[4,5],[7,8]]',
570 # -- output --
571 'a = [\n' +
572 ' [1, 2],\n' +
573 ' [4, 5],\n' +
574 ' [7, 8]\n' +
575 ']')
576 bt(
577 'a=[[1,2],[4,5],[7,8],]',
578 # -- output --
579 'a = [\n' +
580 ' [1, 2],\n' +
581 ' [4, 5],\n' +
582 ' [7, 8],\n' +
583 ']')
584 bt(
585 'a=[[1,2],[4,5],function(){},[7,8]]',
586 # -- output --
587 'a = [\n' +
588 ' [1, 2],\n' +
589 ' [4, 5],\n' +
590 ' function() {},\n' +
591 ' [7, 8]\n' +
592 ']')
593 bt(
594 'a=[[1,2],[4,5],function(){},function(){},[7,8]]',
595 # -- output --
596 'a = [\n' +
597 ' [1, 2],\n' +
598 ' [4, 5],\n' +
599 ' function() {},\n' +
600 ' function() {},\n' +
601 ' [7, 8]\n' +
602 ']')
603 bt(
604 'a=[[1,2],[4,5],function(){},[7,8]]',
605 # -- output --
606 'a = [\n' +
607 ' [1, 2],\n' +
608 ' [4, 5],\n' +
609 ' function() {},\n' +
610 ' [7, 8]\n' +
611 ']')
612 bt('a=[b,c,function(){},function(){},d]', 'a = [b, c, function() {}, function() {}, d]')
613 bt(
614 'a=[b,c,\n' +
615 'function(){},function(){},d]',
616 # -- output --
617 'a = [b, c,\n' +
618 ' function() {},\n' +
619 ' function() {},\n' +
620 ' d\n' +
621 ']')
622 bt('a=[a[1],b[4],c[d[7]]]', 'a = [a[1], b[4], c[d[7]]]')
623 bt('[1,2,[3,4,[5,6],7],8]', '[1, 2, [3, 4, [5, 6], 7], 8]')
624 bt(
625 '[[["1","2"],["3","4"]],[["5","6","7"],["8","9","0"]],[["1","2","3"],["4","5","6","7"],["8","9","0"]]]',
626 # -- output --
627 '[\n' +
628 ' [\n' +
629 ' ["1", "2"],\n' +
630 ' ["3", "4"]\n' +
631 ' ],\n' +
632 ' [\n' +
633 ' ["5", "6", "7"],\n' +
634 ' ["8", "9", "0"]\n' +
635 ' ],\n' +
636 ' [\n' +
637 ' ["1", "2", "3"],\n' +
638 ' ["4", "5", "6", "7"],\n' +
639 ' ["8", "9", "0"]\n' +
640 ' ]\n' +
641 ']')
642 bt(
643 'changeCollection.add({\n' +
644 ' name: "Jonathan" // New line inserted after this line on every save\n' +
645 ' , age: 25\n' +
646 '});',
647 # -- output --
648 'changeCollection.add({\n' +
649 ' name: "Jonathan" // New line inserted after this line on every save\n' +
650 ' ,\n' +
651 ' age: 25\n' +
652 '});')
653 bt(
654 'changeCollection.add(\n' +
655 ' function() {\n' +
656 ' return true;\n' +
657 ' },\n' +
658 ' function() {\n' +
659 ' return true;\n' +
660 ' }\n' +
661 ');')
662  
663 # Comma-first option - (c0 = "\n, ", c1 = "\n , ", c2 = "\n , ", c3 = "\n , ", f1 = ", ")
664 self.reset_options();
665 self.options.comma_first = true
666 bt(
667 '{a:1, b:2}',
668 # -- output --
669 '{\n' +
670 ' a: 1\n' +
671 ' , b: 2\n' +
672 '}')
673 bt(
674 'var a=1, b=c[d], e=6;',
675 # -- output --
676 'var a = 1\n' +
677 ' , b = c[d]\n' +
678 ' , e = 6;')
679 bt(
680 'for(var a=1,b=2,c=3;d<3;d++)\n' +
681 'e',
682 # -- output --
683 'for (var a = 1, b = 2, c = 3; d < 3; d++)\n' +
684 ' e')
685 bt(
686 'for(var a=1,b=2,\n' +
687 'c=3;d<3;d++)\n' +
688 'e',
689 # -- output --
690 'for (var a = 1, b = 2\n' +
691 ' , c = 3; d < 3; d++)\n' +
692 ' e')
693 bt(
694 'function foo() {\n' +
695 ' return [\n' +
696 ' "one"\n' +
697 ' , "two"\n' +
698 ' ];\n' +
699 '}')
700 bt(
701 'a=[[1,2],[4,5],[7,8]]',
702 # -- output --
703 'a = [\n' +
704 ' [1, 2]\n' +
705 ' , [4, 5]\n' +
706 ' , [7, 8]\n' +
707 ']')
708 bt(
709 'a=[[1,2],[4,5],[7,8],]',
710 # -- output --
711 'a = [\n' +
712 ' [1, 2]\n' +
713 ' , [4, 5]\n' +
714 ' , [7, 8]\n' +
715 ', ]')
716 bt(
717 'a=[[1,2],[4,5],function(){},[7,8]]',
718 # -- output --
719 'a = [\n' +
720 ' [1, 2]\n' +
721 ' , [4, 5]\n' +
722 ' , function() {}\n' +
723 ' , [7, 8]\n' +
724 ']')
725 bt(
726 'a=[[1,2],[4,5],function(){},function(){},[7,8]]',
727 # -- output --
728 'a = [\n' +
729 ' [1, 2]\n' +
730 ' , [4, 5]\n' +
731 ' , function() {}\n' +
732 ' , function() {}\n' +
733 ' , [7, 8]\n' +
734 ']')
735 bt(
736 'a=[[1,2],[4,5],function(){},[7,8]]',
737 # -- output --
738 'a = [\n' +
739 ' [1, 2]\n' +
740 ' , [4, 5]\n' +
741 ' , function() {}\n' +
742 ' , [7, 8]\n' +
743 ']')
744 bt('a=[b,c,function(){},function(){},d]', 'a = [b, c, function() {}, function() {}, d]')
745 bt(
746 'a=[b,c,\n' +
747 'function(){},function(){},d]',
748 # -- output --
749 'a = [b, c\n' +
750 ' , function() {}\n' +
751 ' , function() {}\n' +
752 ' , d\n' +
753 ']')
754 bt('a=[a[1],b[4],c[d[7]]]', 'a = [a[1], b[4], c[d[7]]]')
755 bt('[1,2,[3,4,[5,6],7],8]', '[1, 2, [3, 4, [5, 6], 7], 8]')
756 bt(
757 '[[["1","2"],["3","4"]],[["5","6","7"],["8","9","0"]],[["1","2","3"],["4","5","6","7"],["8","9","0"]]]',
758 # -- output --
759 '[\n' +
760 ' [\n' +
761 ' ["1", "2"]\n' +
762 ' , ["3", "4"]\n' +
763 ' ]\n' +
764 ' , [\n' +
765 ' ["5", "6", "7"]\n' +
766 ' , ["8", "9", "0"]\n' +
767 ' ]\n' +
768 ' , [\n' +
769 ' ["1", "2", "3"]\n' +
770 ' , ["4", "5", "6", "7"]\n' +
771 ' , ["8", "9", "0"]\n' +
772 ' ]\n' +
773 ']')
774 bt(
775 'changeCollection.add({\n' +
776 ' name: "Jonathan" // New line inserted after this line on every save\n' +
777 ' , age: 25\n' +
778 '});')
779 bt(
780 'changeCollection.add(\n' +
781 ' function() {\n' +
782 ' return true;\n' +
783 ' },\n' +
784 ' function() {\n' +
785 ' return true;\n' +
786 ' }\n' +
787 ');',
788 # -- output --
789 'changeCollection.add(\n' +
790 ' function() {\n' +
791 ' return true;\n' +
792 ' }\n' +
793 ' , function() {\n' +
794 ' return true;\n' +
795 ' }\n' +
796 ');')
797  
798  
799 #============================================================
800 # Space in parens tests - (s = "", e = "")
801 self.reset_options();
802 self.options.space_in_paren = false
803 self.options.space_in_empty_paren = false
804 bt('if(p) foo(a,b);', 'if (p) foo(a, b);')
805 bt(
806 'try{while(true){willThrow()}}catch(result)switch(result){case 1:++result }',
807 # -- output --
808 'try {\n' +
809 ' while (true) {\n' +
810 ' willThrow()\n' +
811 ' }\n' +
812 '} catch (result) switch (result) {\n' +
813 ' case 1:\n' +
814 ' ++result\n' +
815 '}')
816 bt('((e/((a+(b)*c)-d))^2)*5;', '((e / ((a + (b) * c) - d)) ^ 2) * 5;')
817 bt(
818 'function f(a,b) {if(a) b()}function g(a,b) {if(!a) b()}',
819 # -- output --
820 'function f(a, b) {\n' +
821 ' if (a) b()\n' +
822 '}\n' +
823 '\n' +
824 'function g(a, b) {\n' +
825 ' if (!a) b()\n' +
826 '}')
827 bt('a=[];', 'a = [];')
828 bt('a=[b,c,d];', 'a = [b, c, d];')
829 bt('a= f[b];', 'a = f[b];')
830 bt(
831 '{\n' +
832 ' files: [ {\n' +
833 ' expand: true,\n' +
834 ' cwd: "www/gui/",\n' +
835 ' src: [ "im/design_standards/*.*" ],\n' +
836 ' dest: "www/gui/build"\n' +
837 ' } ]\n' +
838 '}',
839 # -- output --
840 '{\n' +
841 ' files: [{\n' +
842 ' expand: true,\n' +
843 ' cwd: "www/gui/",\n' +
844 ' src: ["im/design_standards/*.*"],\n' +
845 ' dest: "www/gui/build"\n' +
846 ' }]\n' +
847 '}')
848  
849 # Space in parens tests - (s = "", e = "")
850 self.reset_options();
851 self.options.space_in_paren = false
852 self.options.space_in_empty_paren = true
853 bt('if(p) foo(a,b);', 'if (p) foo(a, b);')
854 bt(
855 'try{while(true){willThrow()}}catch(result)switch(result){case 1:++result }',
856 # -- output --
857 'try {\n' +
858 ' while (true) {\n' +
859 ' willThrow()\n' +
860 ' }\n' +
861 '} catch (result) switch (result) {\n' +
862 ' case 1:\n' +
863 ' ++result\n' +
864 '}')
865 bt('((e/((a+(b)*c)-d))^2)*5;', '((e / ((a + (b) * c) - d)) ^ 2) * 5;')
866 bt(
867 'function f(a,b) {if(a) b()}function g(a,b) {if(!a) b()}',
868 # -- output --
869 'function f(a, b) {\n' +
870 ' if (a) b()\n' +
871 '}\n' +
872 '\n' +
873 'function g(a, b) {\n' +
874 ' if (!a) b()\n' +
875 '}')
876 bt('a=[];', 'a = [];')
877 bt('a=[b,c,d];', 'a = [b, c, d];')
878 bt('a= f[b];', 'a = f[b];')
879 bt(
880 '{\n' +
881 ' files: [ {\n' +
882 ' expand: true,\n' +
883 ' cwd: "www/gui/",\n' +
884 ' src: [ "im/design_standards/*.*" ],\n' +
885 ' dest: "www/gui/build"\n' +
886 ' } ]\n' +
887 '}',
888 # -- output --
889 '{\n' +
890 ' files: [{\n' +
891 ' expand: true,\n' +
892 ' cwd: "www/gui/",\n' +
893 ' src: ["im/design_standards/*.*"],\n' +
894 ' dest: "www/gui/build"\n' +
895 ' }]\n' +
896 '}')
897  
898 # Space in parens tests - (s = " ", e = "")
899 self.reset_options();
900 self.options.space_in_paren = true
901 self.options.space_in_empty_paren = false
902 bt('if(p) foo(a,b);', 'if ( p ) foo( a, b );')
903 bt(
904 'try{while(true){willThrow()}}catch(result)switch(result){case 1:++result }',
905 # -- output --
906 'try {\n' +
907 ' while ( true ) {\n' +
908 ' willThrow()\n' +
909 ' }\n' +
910 '} catch ( result ) switch ( result ) {\n' +
911 ' case 1:\n' +
912 ' ++result\n' +
913 '}')
914 bt('((e/((a+(b)*c)-d))^2)*5;', '( ( e / ( ( a + ( b ) * c ) - d ) ) ^ 2 ) * 5;')
915 bt(
916 'function f(a,b) {if(a) b()}function g(a,b) {if(!a) b()}',
917 # -- output --
918 'function f( a, b ) {\n' +
919 ' if ( a ) b()\n' +
920 '}\n' +
921 '\n' +
922 'function g( a, b ) {\n' +
923 ' if ( !a ) b()\n' +
924 '}')
925 bt('a=[];', 'a = [];')
926 bt('a=[b,c,d];', 'a = [ b, c, d ];')
927 bt('a= f[b];', 'a = f[ b ];')
928 bt(
929 '{\n' +
930 ' files: [ {\n' +
931 ' expand: true,\n' +
932 ' cwd: "www/gui/",\n' +
933 ' src: [ "im/design_standards/*.*" ],\n' +
934 ' dest: "www/gui/build"\n' +
935 ' } ]\n' +
936 '}')
937  
938 # Space in parens tests - (s = " ", e = " ")
939 self.reset_options();
940 self.options.space_in_paren = true
941 self.options.space_in_empty_paren = true
942 bt('if(p) foo(a,b);', 'if ( p ) foo( a, b );')
943 bt(
944 'try{while(true){willThrow()}}catch(result)switch(result){case 1:++result }',
945 # -- output --
946 'try {\n' +
947 ' while ( true ) {\n' +
948 ' willThrow( )\n' +
949 ' }\n' +
950 '} catch ( result ) switch ( result ) {\n' +
951 ' case 1:\n' +
952 ' ++result\n' +
953 '}')
954 bt('((e/((a+(b)*c)-d))^2)*5;', '( ( e / ( ( a + ( b ) * c ) - d ) ) ^ 2 ) * 5;')
955 bt(
956 'function f(a,b) {if(a) b()}function g(a,b) {if(!a) b()}',
957 # -- output --
958 'function f( a, b ) {\n' +
959 ' if ( a ) b( )\n' +
960 '}\n' +
961 '\n' +
962 'function g( a, b ) {\n' +
963 ' if ( !a ) b( )\n' +
964 '}')
965 bt('a=[];', 'a = [ ];')
966 bt('a=[b,c,d];', 'a = [ b, c, d ];')
967 bt('a= f[b];', 'a = f[ b ];')
968 bt(
969 '{\n' +
970 ' files: [ {\n' +
971 ' expand: true,\n' +
972 ' cwd: "www/gui/",\n' +
973 ' src: [ "im/design_standards/*.*" ],\n' +
974 ' dest: "www/gui/build"\n' +
975 ' } ]\n' +
976 '}')
977  
978  
979 #============================================================
980 # operator_position option - ensure no neswlines if preserve_newlines is false - ()
981 self.reset_options();
982 self.options.operator_position = 'before-newline'
983 self.options.preserve_newlines = false
984 bt(
985 'var res = a + b - c / d * e % f;\n' +
986 'var res = g & h | i ^ j;\n' +
987 'var res = (k && l || m) ? n : o;\n' +
988 'var res = p >> q << r >>> s;\n' +
989 'var res = t === u !== v != w == x >= y <= z > aa < ab;\n' +
990 'ac + -ad')
991 bt(
992 'var res = a + b\n' +
993 '- c /\n' +
994 'd * e\n' +
995 '%\n' +
996 'f;\n' +
997 ' var res = g & h\n' +
998 '| i ^\n' +
999 'j;\n' +
1000 'var res = (k &&\n' +
1001 'l\n' +
1002 '|| m) ?\n' +
1003 'n\n' +
1004 ': o\n' +
1005 ';\n' +
1006 'var res = p\n' +
1007 '>> q <<\n' +
1008 'r\n' +
1009 '>>> s;\n' +
1010 'var res\n' +
1011 ' = t\n' +
1012 '\n' +
1013 ' === u !== v\n' +
1014 ' !=\n' +
1015 'w\n' +
1016 '== x >=\n' +
1017 'y <= z > aa <\n' +
1018 'ab;\n' +
1019 'ac +\n' +
1020 '-ad',
1021 # -- output --
1022 'var res = a + b - c / d * e % f;\n' +
1023 'var res = g & h | i ^ j;\n' +
1024 'var res = (k && l || m) ? n : o;\n' +
1025 'var res = p >> q << r >>> s;\n' +
1026 'var res = t === u !== v != w == x >= y <= z > aa < ab;\n' +
1027 'ac + -ad')
1028  
1029 # operator_position option - ensure no neswlines if preserve_newlines is false - ()
1030 self.reset_options();
1031 self.options.operator_position = 'after-newline'
1032 self.options.preserve_newlines = false
1033 bt(
1034 'var res = a + b - c / d * e % f;\n' +
1035 'var res = g & h | i ^ j;\n' +
1036 'var res = (k && l || m) ? n : o;\n' +
1037 'var res = p >> q << r >>> s;\n' +
1038 'var res = t === u !== v != w == x >= y <= z > aa < ab;\n' +
1039 'ac + -ad')
1040 bt(
1041 'var res = a + b\n' +
1042 '- c /\n' +
1043 'd * e\n' +
1044 '%\n' +
1045 'f;\n' +
1046 ' var res = g & h\n' +
1047 '| i ^\n' +
1048 'j;\n' +
1049 'var res = (k &&\n' +
1050 'l\n' +
1051 '|| m) ?\n' +
1052 'n\n' +
1053 ': o\n' +
1054 ';\n' +
1055 'var res = p\n' +
1056 '>> q <<\n' +
1057 'r\n' +
1058 '>>> s;\n' +
1059 'var res\n' +
1060 ' = t\n' +
1061 '\n' +
1062 ' === u !== v\n' +
1063 ' !=\n' +
1064 'w\n' +
1065 '== x >=\n' +
1066 'y <= z > aa <\n' +
1067 'ab;\n' +
1068 'ac +\n' +
1069 '-ad',
1070 # -- output --
1071 'var res = a + b - c / d * e % f;\n' +
1072 'var res = g & h | i ^ j;\n' +
1073 'var res = (k && l || m) ? n : o;\n' +
1074 'var res = p >> q << r >>> s;\n' +
1075 'var res = t === u !== v != w == x >= y <= z > aa < ab;\n' +
1076 'ac + -ad')
1077  
1078 # operator_position option - ensure no neswlines if preserve_newlines is false - ()
1079 self.reset_options();
1080 self.options.operator_position = 'preserve-newline'
1081 self.options.preserve_newlines = false
1082 bt(
1083 'var res = a + b - c / d * e % f;\n' +
1084 'var res = g & h | i ^ j;\n' +
1085 'var res = (k && l || m) ? n : o;\n' +
1086 'var res = p >> q << r >>> s;\n' +
1087 'var res = t === u !== v != w == x >= y <= z > aa < ab;\n' +
1088 'ac + -ad')
1089 bt(
1090 'var res = a + b\n' +
1091 '- c /\n' +
1092 'd * e\n' +
1093 '%\n' +
1094 'f;\n' +
1095 ' var res = g & h\n' +
1096 '| i ^\n' +
1097 'j;\n' +
1098 'var res = (k &&\n' +
1099 'l\n' +
1100 '|| m) ?\n' +
1101 'n\n' +
1102 ': o\n' +
1103 ';\n' +
1104 'var res = p\n' +
1105 '>> q <<\n' +
1106 'r\n' +
1107 '>>> s;\n' +
1108 'var res\n' +
1109 ' = t\n' +
1110 '\n' +
1111 ' === u !== v\n' +
1112 ' !=\n' +
1113 'w\n' +
1114 '== x >=\n' +
1115 'y <= z > aa <\n' +
1116 'ab;\n' +
1117 'ac +\n' +
1118 '-ad',
1119 # -- output --
1120 'var res = a + b - c / d * e % f;\n' +
1121 'var res = g & h | i ^ j;\n' +
1122 'var res = (k && l || m) ? n : o;\n' +
1123 'var res = p >> q << r >>> s;\n' +
1124 'var res = t === u !== v != w == x >= y <= z > aa < ab;\n' +
1125 'ac + -ad')
1126  
1127  
1128 #============================================================
1129 # operator_position option - set to 'before-newline' (default value)
1130 self.reset_options();
1131  
1132 # comprehensive, various newlines
1133 bt(
1134 'var res = a + b\n' +
1135 '- c /\n' +
1136 'd * e\n' +
1137 '%\n' +
1138 'f;\n' +
1139 ' var res = g & h\n' +
1140 '| i ^\n' +
1141 'j;\n' +
1142 'var res = (k &&\n' +
1143 'l\n' +
1144 '|| m) ?\n' +
1145 'n\n' +
1146 ': o\n' +
1147 ';\n' +
1148 'var res = p\n' +
1149 '>> q <<\n' +
1150 'r\n' +
1151 '>>> s;\n' +
1152 'var res\n' +
1153 ' = t\n' +
1154 '\n' +
1155 ' === u !== v\n' +
1156 ' !=\n' +
1157 'w\n' +
1158 '== x >=\n' +
1159 'y <= z > aa <\n' +
1160 'ab;\n' +
1161 'ac +\n' +
1162 '-ad',
1163 # -- output --
1164 'var res = a + b -\n' +
1165 ' c /\n' +
1166 ' d * e %\n' +
1167 ' f;\n' +
1168 'var res = g & h |\n' +
1169 ' i ^\n' +
1170 ' j;\n' +
1171 'var res = (k &&\n' +
1172 ' l ||\n' +
1173 ' m) ?\n' +
1174 ' n :\n' +
1175 ' o;\n' +
1176 'var res = p >>\n' +
1177 ' q <<\n' +
1178 ' r >>>\n' +
1179 ' s;\n' +
1180 'var res = t\n' +
1181 '\n' +
1182 ' ===\n' +
1183 ' u !== v !=\n' +
1184 ' w ==\n' +
1185 ' x >=\n' +
1186 ' y <= z > aa <\n' +
1187 ' ab;\n' +
1188 'ac +\n' +
1189 ' -ad')
1190  
1191 # colon special case
1192 bt(
1193 'var a = {\n' +
1194 ' b\n' +
1195 ': bval,\n' +
1196 ' c:\n' +
1197 'cval\n' +
1198 ' ,d: dval\n' +
1199 '};\n' +
1200 'var e = f ? g\n' +
1201 ': h;\n' +
1202 'var i = j ? k :\n' +
1203 'l;',
1204 # -- output --
1205 'var a = {\n' +
1206 ' b: bval,\n' +
1207 ' c: cval,\n' +
1208 ' d: dval\n' +
1209 '};\n' +
1210 'var e = f ? g :\n' +
1211 ' h;\n' +
1212 'var i = j ? k :\n' +
1213 ' l;')
1214  
1215 # catch-all, includes brackets and other various code
1216 bt(
1217 'var d = 1;\n' +
1218 'if (a === b\n' +
1219 ' && c) {\n' +
1220 ' d = (c * everything\n' +
1221 ' / something_else) %\n' +
1222 ' b;\n' +
1223 ' e\n' +
1224 ' += d;\n' +
1225 '\n' +
1226 '} else if (!(complex && simple) ||\n' +
1227 ' (emotion && emotion.name === "happy")) {\n' +
1228 ' cryTearsOfJoy(many ||\n' +
1229 ' anOcean\n' +
1230 ' || aRiver);\n' +
1231 '}',
1232 # -- output --
1233 'var d = 1;\n' +
1234 'if (a === b &&\n' +
1235 ' c) {\n' +
1236 ' d = (c * everything /\n' +
1237 ' something_else) %\n' +
1238 ' b;\n' +
1239 ' e\n' +
1240 ' += d;\n' +
1241 '\n' +
1242 '} else if (!(complex && simple) ||\n' +
1243 ' (emotion && emotion.name === "happy")) {\n' +
1244 ' cryTearsOfJoy(many ||\n' +
1245 ' anOcean ||\n' +
1246 ' aRiver);\n' +
1247 '}')
1248  
1249  
1250 #============================================================
1251 # operator_position option - set to 'after_newline'
1252 self.reset_options();
1253 self.options.operator_position = 'after-newline'
1254  
1255 # comprehensive, various newlines
1256 bt(
1257 'var res = a + b\n' +
1258 '- c /\n' +
1259 'd * e\n' +
1260 '%\n' +
1261 'f;\n' +
1262 ' var res = g & h\n' +
1263 '| i ^\n' +
1264 'j;\n' +
1265 'var res = (k &&\n' +
1266 'l\n' +
1267 '|| m) ?\n' +
1268 'n\n' +
1269 ': o\n' +
1270 ';\n' +
1271 'var res = p\n' +
1272 '>> q <<\n' +
1273 'r\n' +
1274 '>>> s;\n' +
1275 'var res\n' +
1276 ' = t\n' +
1277 '\n' +
1278 ' === u !== v\n' +
1279 ' !=\n' +
1280 'w\n' +
1281 '== x >=\n' +
1282 'y <= z > aa <\n' +
1283 'ab;\n' +
1284 'ac +\n' +
1285 '-ad',
1286 # -- output --
1287 'var res = a + b\n' +
1288 ' - c\n' +
1289 ' / d * e\n' +
1290 ' % f;\n' +
1291 'var res = g & h\n' +
1292 ' | i\n' +
1293 ' ^ j;\n' +
1294 'var res = (k\n' +
1295 ' && l\n' +
1296 ' || m)\n' +
1297 ' ? n\n' +
1298 ' : o;\n' +
1299 'var res = p\n' +
1300 ' >> q\n' +
1301 ' << r\n' +
1302 ' >>> s;\n' +
1303 'var res = t\n' +
1304 '\n' +
1305 ' === u !== v\n' +
1306 ' != w\n' +
1307 ' == x\n' +
1308 ' >= y <= z > aa\n' +
1309 ' < ab;\n' +
1310 'ac\n' +
1311 ' + -ad')
1312  
1313 # colon special case
1314 bt(
1315 'var a = {\n' +
1316 ' b\n' +
1317 ': bval,\n' +
1318 ' c:\n' +
1319 'cval\n' +
1320 ' ,d: dval\n' +
1321 '};\n' +
1322 'var e = f ? g\n' +
1323 ': h;\n' +
1324 'var i = j ? k :\n' +
1325 'l;',
1326 # -- output --
1327 'var a = {\n' +
1328 ' b: bval,\n' +
1329 ' c: cval,\n' +
1330 ' d: dval\n' +
1331 '};\n' +
1332 'var e = f ? g\n' +
1333 ' : h;\n' +
1334 'var i = j ? k\n' +
1335 ' : l;')
1336  
1337 # catch-all, includes brackets and other various code
1338 bt(
1339 'var d = 1;\n' +
1340 'if (a === b\n' +
1341 ' && c) {\n' +
1342 ' d = (c * everything\n' +
1343 ' / something_else) %\n' +
1344 ' b;\n' +
1345 ' e\n' +
1346 ' += d;\n' +
1347 '\n' +
1348 '} else if (!(complex && simple) ||\n' +
1349 ' (emotion && emotion.name === "happy")) {\n' +
1350 ' cryTearsOfJoy(many ||\n' +
1351 ' anOcean\n' +
1352 ' || aRiver);\n' +
1353 '}',
1354 # -- output --
1355 'var d = 1;\n' +
1356 'if (a === b\n' +
1357 ' && c) {\n' +
1358 ' d = (c * everything\n' +
1359 ' / something_else)\n' +
1360 ' % b;\n' +
1361 ' e\n' +
1362 ' += d;\n' +
1363 '\n' +
1364 '} else if (!(complex && simple)\n' +
1365 ' || (emotion && emotion.name === "happy")) {\n' +
1366 ' cryTearsOfJoy(many\n' +
1367 ' || anOcean\n' +
1368 ' || aRiver);\n' +
1369 '}')
1370  
1371  
1372 #============================================================
1373 # operator_position option - set to 'preserve-newline'
1374 self.reset_options();
1375 self.options.operator_position = 'preserve-newline'
1376  
1377 # comprehensive, various newlines
1378 bt(
1379 'var res = a + b\n' +
1380 '- c /\n' +
1381 'd * e\n' +
1382 '%\n' +
1383 'f;\n' +
1384 ' var res = g & h\n' +
1385 '| i ^\n' +
1386 'j;\n' +
1387 'var res = (k &&\n' +
1388 'l\n' +
1389 '|| m) ?\n' +
1390 'n\n' +
1391 ': o\n' +
1392 ';\n' +
1393 'var res = p\n' +
1394 '>> q <<\n' +
1395 'r\n' +
1396 '>>> s;\n' +
1397 'var res\n' +
1398 ' = t\n' +
1399 '\n' +
1400 ' === u !== v\n' +
1401 ' !=\n' +
1402 'w\n' +
1403 '== x >=\n' +
1404 'y <= z > aa <\n' +
1405 'ab;\n' +
1406 'ac +\n' +
1407 '-ad',
1408 # -- output --
1409 'var res = a + b\n' +
1410 ' - c /\n' +
1411 ' d * e\n' +
1412 ' %\n' +
1413 ' f;\n' +
1414 'var res = g & h\n' +
1415 ' | i ^\n' +
1416 ' j;\n' +
1417 'var res = (k &&\n' +
1418 ' l\n' +
1419 ' || m) ?\n' +
1420 ' n\n' +
1421 ' : o;\n' +
1422 'var res = p\n' +
1423 ' >> q <<\n' +
1424 ' r\n' +
1425 ' >>> s;\n' +
1426 'var res = t\n' +
1427 '\n' +
1428 ' === u !== v\n' +
1429 ' !=\n' +
1430 ' w\n' +
1431 ' == x >=\n' +
1432 ' y <= z > aa <\n' +
1433 ' ab;\n' +
1434 'ac +\n' +
1435 ' -ad')
1436  
1437 # colon special case
1438 bt(
1439 'var a = {\n' +
1440 ' b\n' +
1441 ': bval,\n' +
1442 ' c:\n' +
1443 'cval\n' +
1444 ' ,d: dval\n' +
1445 '};\n' +
1446 'var e = f ? g\n' +
1447 ': h;\n' +
1448 'var i = j ? k :\n' +
1449 'l;',
1450 # -- output --
1451 'var a = {\n' +
1452 ' b: bval,\n' +
1453 ' c: cval,\n' +
1454 ' d: dval\n' +
1455 '};\n' +
1456 'var e = f ? g\n' +
1457 ' : h;\n' +
1458 'var i = j ? k :\n' +
1459 ' l;')
1460  
1461 # catch-all, includes brackets and other various code
1462 bt(
1463 'var d = 1;\n' +
1464 'if (a === b\n' +
1465 ' && c) {\n' +
1466 ' d = (c * everything\n' +
1467 ' / something_else) %\n' +
1468 ' b;\n' +
1469 ' e\n' +
1470 ' += d;\n' +
1471 '\n' +
1472 '} else if (!(complex && simple) ||\n' +
1473 ' (emotion && emotion.name === "happy")) {\n' +
1474 ' cryTearsOfJoy(many ||\n' +
1475 ' anOcean\n' +
1476 ' || aRiver);\n' +
1477 '}')
1478  
1479  
1480 #============================================================
1481 # Yield tests
1482 self.reset_options();
1483 bt('yield /foo\\//;')
1484 bt('result = yield pgClient.query_(queryString);')
1485 bt('yield [1, 2]')
1486 bt('yield* bar();')
1487  
1488 # yield should have no space between yield and star
1489 bt('yield * bar();', 'yield* bar();')
1490  
1491 # yield should have space between star and generator
1492 bt('yield *bar();', 'yield* bar();')
1493  
1494  
1495 #============================================================
1496 # Async / await tests
1497 self.reset_options();
1498 bt('async function foo() {}')
1499 bt('let w = async function foo() {}')
1500 bt(
1501 'async function foo() {}\n' +
1502 'var x = await foo();')
1503  
1504 # async function as an input to another function
1505 bt('wrapper(async function foo() {})')
1506  
1507 # await on inline anonymous function. should have a space after await
1508 bt(
1509 'async function() {\n' +
1510 ' var w = await(async function() {\n' +
1511 ' return await foo();\n' +
1512 ' })();\n' +
1513 '}',
1514 # -- output --
1515 'async function() {\n' +
1516 ' var w = await (async function() {\n' +
1517 ' return await foo();\n' +
1518 ' })();\n' +
1519 '}')
1520  
1521 # ensure that this doesn't break anyone with the async library
1522 bt('async.map(function(t) {})')
1523  
1524  
1525 #============================================================
1526 # e4x - Test that e4x literals passed through when e4x-option is enabled
1527 self.reset_options();
1528 self.options.e4x = true
1529 bt(
1530 'xml=<a b="c"><d/><e>\n' +
1531 ' foo</e>x</a>;',
1532 # -- output --
1533 'xml = <a b="c"><d/><e>\n' +
1534 ' foo</e>x</a>;')
1535 bt('<a b=\'This is a quoted "c".\'/>')
1536 bt('<a b="This is a quoted \'c\'."/>')
1537 bt('<a b="A quote \' inside string."/>')
1538 bt('<a b=\'A quote " inside string.\'/>')
1539 bt('<a b=\'Some """ quotes "" inside string.\'/>')
1540  
1541 # Handles inline expressions
1542 bt(
1543 'xml=<{a} b="c"><d/><e v={z}>\n' +
1544 ' foo</e>x</{a}>;',
1545 # -- output --
1546 'xml = <{a} b="c"><d/><e v={z}>\n' +
1547 ' foo</e>x</{a}>;')
1548 bt(
1549 'xml=<{a} b="c">\n' +
1550 ' <e v={z}>\n' +
1551 ' foo</e>x</{a}>;',
1552 # -- output --
1553 'xml = <{a} b="c">\n' +
1554 ' <e v={z}>\n' +
1555 ' foo</e>x</{a}>;')
1556  
1557 # xml literals with special characters in elem names - see http://www.w3.org/TR/REC-xml/#NT-NameChar
1558 bt('xml = <_:.valid.xml- _:.valid.xml-="123"/>;')
1559  
1560 # xml literals with attributes without equal sign
1561 bt('xml = <elem someAttr/>;')
1562  
1563 # Handles CDATA
1564 bt(
1565 'xml=<![CDATA[ b="c"><d/><e v={z}>\n' +
1566 ' foo</e>x/]]>;',
1567 # -- output --
1568 'xml = <![CDATA[ b="c"><d/><e v={z}>\n' +
1569 ' foo</e>x/]]>;')
1570 bt('xml=<![CDATA[]]>;', 'xml = <![CDATA[]]>;')
1571 bt('xml=<a b="c"><![CDATA[d/></a></{}]]></a>;', 'xml = <a b="c"><![CDATA[d/></a></{}]]></a>;')
1572  
1573 # JSX - working jsx from http://prettydiff.com/unit_tests/beautification_javascript_jsx.txt
1574 bt(
1575 'var ListItem = React.createClass({\n' +
1576 ' render: function() {\n' +
1577 ' return (\n' +
1578 ' <li className="ListItem">\n' +
1579 ' <a href={ "/items/" + this.props.item.id }>\n' +
1580 ' this.props.item.name\n' +
1581 ' </a>\n' +
1582 ' </li>\n' +
1583 ' );\n' +
1584 ' }\n' +
1585 '});')
1586 bt(
1587 'var List = React.createClass({\n' +
1588 ' renderList: function() {\n' +
1589 ' return this.props.items.map(function(item) {\n' +
1590 ' return <ListItem item={item} key={item.id} />;\n' +
1591 ' });\n' +
1592 ' },\n' +
1593 '\n' +
1594 ' render: function() {\n' +
1595 ' return <ul className="List">\n' +
1596 ' this.renderList()\n' +
1597 ' </ul>\n' +
1598 ' }\n' +
1599 '});')
1600 bt(
1601 'var Mist = React.createClass({\n' +
1602 ' renderList: function() {\n' +
1603 ' return this.props.items.map(function(item) {\n' +
1604 ' return <ListItem item={return <tag>{item}</tag>} key={item.id} />;\n' +
1605 ' });\n' +
1606 ' }\n' +
1607 '});')
1608 bt(
1609 '// JSX\n' +
1610 'var box = <Box>\n' +
1611 ' {shouldShowAnswer(user) ?\n' +
1612 ' <Answer value={false}>no</Answer> : <Box.Comment>\n' +
1613 ' Text Content\n' +
1614 ' </Box.Comment>}\n' +
1615 ' </Box>;\n' +
1616 'var a = function() {\n' +
1617 ' return <tsdf>asdf</tsdf>;\n' +
1618 '};\n' +
1619 '\n' +
1620 'var HelloMessage = React.createClass({\n' +
1621 ' render: function() {\n' +
1622 ' return <div {someAttr}>Hello {this.props.name}</div>;\n' +
1623 ' }\n' +
1624 '});\n' +
1625 'React.render(<HelloMessage name="John" />, mountNode);')
1626 bt(
1627 'var Timer = React.createClass({\n' +
1628 ' getInitialState: function() {\n' +
1629 ' return {\n' +
1630 ' secondsElapsed: 0\n' +
1631 ' };\n' +
1632 ' },\n' +
1633 ' tick: function() {\n' +
1634 ' this.setState({\n' +
1635 ' secondsElapsed: this.state.secondsElapsed + 1\n' +
1636 ' });\n' +
1637 ' },\n' +
1638 ' componentDidMount: function() {\n' +
1639 ' this.interval = setInterval(this.tick, 1000);\n' +
1640 ' },\n' +
1641 ' componentWillUnmount: function() {\n' +
1642 ' clearInterval(this.interval);\n' +
1643 ' },\n' +
1644 ' render: function() {\n' +
1645 ' return (\n' +
1646 ' <div>Seconds Elapsed: {this.state.secondsElapsed}</div>\n' +
1647 ' );\n' +
1648 ' }\n' +
1649 '});\n' +
1650 'React.render(<Timer />, mountNode);')
1651 bt(
1652 'var TodoList = React.createClass({\n' +
1653 ' render: function() {\n' +
1654 ' var createItem = function(itemText) {\n' +
1655 ' return <li>{itemText}</li>;\n' +
1656 ' };\n' +
1657 ' return <ul>{this.props.items.map(createItem)}</ul>;\n' +
1658 ' }\n' +
1659 '});')
1660 bt(
1661 'var TodoApp = React.createClass({\n' +
1662 ' getInitialState: function() {\n' +
1663 ' return {\n' +
1664 ' items: [],\n' +
1665 ' text: \'\'\n' +
1666 ' };\n' +
1667 ' },\n' +
1668 ' onChange: function(e) {\n' +
1669 ' this.setState({\n' +
1670 ' text: e.target.value\n' +
1671 ' });\n' +
1672 ' },\n' +
1673 ' handleSubmit: function(e) {\n' +
1674 ' e.preventDefault();\n' +
1675 ' var nextItems = this.state.items.concat([this.state.text]);\n' +
1676 ' var nextText = \'\';\n' +
1677 ' this.setState({\n' +
1678 ' items: nextItems,\n' +
1679 ' text: nextText\n' +
1680 ' });\n' +
1681 ' },\n' +
1682 ' render: function() {\n' +
1683 ' return (\n' +
1684 ' <div>\n' +
1685 ' <h3 {someAttr}>TODO</h3>\n' +
1686 ' <TodoList items={this.state.items} />\n' +
1687 ' <form onSubmit={this.handleSubmit}>\n' +
1688 ' <input onChange={this.onChange} value={this.state.text} />\n' +
1689 ' <button>{\'Add #\' + (this.state.items.length + 1)}</button>\n' +
1690 ' </form>\n' +
1691 ' </div>\n' +
1692 ' );\n' +
1693 ' }\n' +
1694 '});\n' +
1695 'React.render(<TodoApp />, mountNode);')
1696 bt(
1697 'var converter = new Showdown.converter();\n' +
1698 'var MarkdownEditor = React.createClass({\n' +
1699 ' getInitialState: function() {\n' +
1700 ' return {value: \'Type some *markdown* here!\'};\n' +
1701 ' },\n' +
1702 ' handleChange: function() {\n' +
1703 ' this.setState({value: this.refs.textarea.getDOMNode().value});\n' +
1704 ' },\n' +
1705 ' render: function() {\n' +
1706 ' return (\n' +
1707 ' <div className="MarkdownEditor">\n' +
1708 ' <h3>Input</h3>\n' +
1709 ' <textarea\n' +
1710 ' onChange={this.handleChange}\n' +
1711 ' ref="textarea"\n' +
1712 ' defaultValue={this.state.value} />\n' +
1713 ' <h3>Output</h3>\n' +
1714 ' <div\n' +
1715 ' className="content"\n' +
1716 ' dangerouslySetInnerHTML=\n' +
1717 ' />\n' +
1718 ' </div>\n' +
1719 ' );\n' +
1720 ' }\n' +
1721 '});\n' +
1722 'React.render(<MarkdownEditor />, mountNode);',
1723 # -- output --
1724 'var converter = new Showdown.converter();\n' +
1725 'var MarkdownEditor = React.createClass({\n' +
1726 ' getInitialState: function() {\n' +
1727 ' return {\n' +
1728 ' value: \'Type some *markdown* here!\'\n' +
1729 ' };\n' +
1730 ' },\n' +
1731 ' handleChange: function() {\n' +
1732 ' this.setState({\n' +
1733 ' value: this.refs.textarea.getDOMNode().value\n' +
1734 ' });\n' +
1735 ' },\n' +
1736 ' render: function() {\n' +
1737 ' return (\n' +
1738 ' <div className="MarkdownEditor">\n' +
1739 ' <h3>Input</h3>\n' +
1740 ' <textarea\n' +
1741 ' onChange={this.handleChange}\n' +
1742 ' ref="textarea"\n' +
1743 ' defaultValue={this.state.value} />\n' +
1744 ' <h3>Output</h3>\n' +
1745 ' <div\n' +
1746 ' className="content"\n' +
1747 ' dangerouslySetInnerHTML=\n' +
1748 ' />\n' +
1749 ' </div>\n' +
1750 ' );\n' +
1751 ' }\n' +
1752 '});\n' +
1753 'React.render(<MarkdownEditor />, mountNode);')
1754  
1755 # JSX - Not quite correct jsx formatting that still works
1756 bt(
1757 'var content = (\n' +
1758 ' <Nav>\n' +
1759 ' {/* child comment, put {} around */}\n' +
1760 ' <Person\n' +
1761 ' /* multi\n' +
1762 ' line\n' +
1763 ' comment */\n' +
1764 ' //attr="test"\n' +
1765 ' name={window.isLoggedIn ? window.name : \'\'} // end of line comment\n' +
1766 ' />\n' +
1767 ' </Nav>\n' +
1768 ' );\n' +
1769 'var qwer = <DropDown> A dropdown list <Menu> <MenuItem>Do Something</MenuItem> <MenuItem>Do Something Fun!</MenuItem> <MenuItem>Do Something Else</MenuItem> </Menu> </DropDown>;\n' +
1770 'render(dropdown);',
1771 # -- output --
1772 'var content = (\n' +
1773 ' <Nav>\n' +
1774 ' {/* child comment, put {} around */}\n' +
1775 ' <Person\n' +
1776 ' /* multi\n' +
1777 ' line\n' +
1778 ' comment */\n' +
1779 ' //attr="test"\n' +
1780 ' name={window.isLoggedIn ? window.name : \'\'} // end of line comment\n' +
1781 ' />\n' +
1782 ' </Nav>\n' +
1783 ');\n' +
1784 'var qwer = <DropDown> A dropdown list <Menu> <MenuItem>Do Something</MenuItem> <MenuItem>Do Something Fun!</MenuItem> <MenuItem>Do Something Else</MenuItem> </Menu> </DropDown>;\n' +
1785 'render(dropdown);')
1786  
1787 # Handles messed up tags, as long as it isn't the same name
1788 # as the root tag. Also handles tags of same name as root tag
1789 # as long as nesting matches.
1790 bt(
1791 'xml=<a x="jn"><c></b></f><a><d jnj="jnn"><f></a ></nj></a>;',
1792 # -- output --
1793 'xml = <a x="jn"><c></b></f><a><d jnj="jnn"><f></a ></nj></a>;')
1794  
1795 # If xml is not terminated, the remainder of the file is treated
1796 # as part of the xml-literal (passed through unaltered)
1797 test_fragment(
1798 'xml=<a></b>\n' +
1799 'c<b;',
1800 # -- output --
1801 'xml = <a></b>\n' +
1802 'c<b;')
1803  
1804 # Issue #646 = whitespace is allowed in attribute declarations
1805 bt(
1806 'let a = React.createClass({\n' +
1807 ' render() {\n' +
1808 ' return (\n' +
1809 ' <p className=\'a\'>\n' +
1810 ' <span>c</span>\n' +
1811 ' </p>\n' +
1812 ' );\n' +
1813 ' }\n' +
1814 '});')
1815 bt(
1816 'let a = React.createClass({\n' +
1817 ' render() {\n' +
1818 ' return (\n' +
1819 ' <p className = \'b\'>\n' +
1820 ' <span>c</span>\n' +
1821 ' </p>\n' +
1822 ' );\n' +
1823 ' }\n' +
1824 '});')
1825 bt(
1826 'let a = React.createClass({\n' +
1827 ' render() {\n' +
1828 ' return (\n' +
1829 ' <p className = "c">\n' +
1830 ' <span>c</span>\n' +
1831 ' </p>\n' +
1832 ' );\n' +
1833 ' }\n' +
1834 '});')
1835 bt(
1836 'let a = React.createClass({\n' +
1837 ' render() {\n' +
1838 ' return (\n' +
1839 ' <{e} className = {d}>\n' +
1840 ' <span>c</span>\n' +
1841 ' </{e}>\n' +
1842 ' );\n' +
1843 ' }\n' +
1844 '});')
1845  
1846 # Issue #914 - Multiline attribute in root tag
1847 bt(
1848 'return (\n' +
1849 ' <a href="#"\n' +
1850 ' onClick={e => {\n' +
1851 ' e.preventDefault()\n' +
1852 ' onClick()\n' +
1853 ' }}>\n' +
1854 ' {children}\n' +
1855 ' </a>\n' +
1856 ');')
1857 bt(
1858 'return (\n' +
1859 ' <{\n' +
1860 ' a + b\n' +
1861 ' } href="#"\n' +
1862 ' onClick={e => {\n' +
1863 ' e.preventDefault()\n' +
1864 ' onClick()\n' +
1865 ' }}>\n' +
1866 ' {children}\n' +
1867 ' </{\n' +
1868 ' a + b\n' +
1869 ' }>\n' +
1870 ');')
1871 bt(
1872 'return (\n' +
1873 ' <{\n' +
1874 ' a + b\n' +
1875 ' } href="#"\n' +
1876 ' onClick={e => {\n' +
1877 ' e.preventDefault()\n' +
1878 ' onClick()\n' +
1879 ' }}>\n' +
1880 ' {children}\n' +
1881 ' </{a + b}>\n' +
1882 ' );',
1883 # -- output --
1884 'return (\n' +
1885 ' <{\n' +
1886 ' a + b\n' +
1887 ' } href="#"\n' +
1888 ' onClick={e => {\n' +
1889 ' e.preventDefault()\n' +
1890 ' onClick()\n' +
1891 ' }}>\n' +
1892 ' {children}\n' +
1893 ' </{a + b}>\n' +
1894 ');')
1895  
1896  
1897 #============================================================
1898 # e4x disabled
1899 self.reset_options();
1900 self.options.e4x = false
1901 bt(
1902 'xml=<a b="c"><d/><e>\n' +
1903 ' foo</e>x</a>;',
1904 # -- output --
1905 'xml = < a b = "c" > < d / > < e >\n' +
1906 ' foo < /e>x</a > ;')
1907  
1908  
1909 #============================================================
1910 # Multiple braces
1911 self.reset_options();
1912 bt(
1913 '{{}/z/}',
1914 # -- output --
1915 '{\n' +
1916 ' {}\n' +
1917 ' /z/\n' +
1918 '}')
1919  
1920  
1921 #============================================================
1922 # Beautify preserve formatting
1923 self.reset_options();
1924 bt(
1925 '/* beautify preserve:start */\n' +
1926 '/* beautify preserve:end */')
1927 bt(
1928 '/* beautify preserve:start */\n' +
1929 ' var a = 1;\n' +
1930 '/* beautify preserve:end */')
1931 bt(
1932 'var a = 1;\n' +
1933 '/* beautify preserve:start */\n' +
1934 ' var a = 1;\n' +
1935 '/* beautify preserve:end */')
1936 bt('/* beautify preserve:start */ {asdklgh;y;;{}dd2d}/* beautify preserve:end */')
1937 bt(
1938 'var a = 1;\n' +
1939 '/* beautify preserve:start */\n' +
1940 ' var a = 1;\n' +
1941 '/* beautify preserve:end */',
1942 # -- output --
1943 'var a = 1;\n' +
1944 '/* beautify preserve:start */\n' +
1945 ' var a = 1;\n' +
1946 '/* beautify preserve:end */')
1947 bt(
1948 'var a = 1;\n' +
1949 ' /* beautify preserve:start */\n' +
1950 ' var a = 1;\n' +
1951 '/* beautify preserve:end */',
1952 # -- output --
1953 'var a = 1;\n' +
1954 '/* beautify preserve:start */\n' +
1955 ' var a = 1;\n' +
1956 '/* beautify preserve:end */')
1957 bt(
1958 'var a = {\n' +
1959 ' /* beautify preserve:start */\n' +
1960 ' one : 1\n' +
1961 ' two : 2,\n' +
1962 ' three : 3,\n' +
1963 ' ten : 10\n' +
1964 ' /* beautify preserve:end */\n' +
1965 '};')
1966 bt(
1967 'var a = {\n' +
1968 '/* beautify preserve:start */\n' +
1969 ' one : 1,\n' +
1970 ' two : 2,\n' +
1971 ' three : 3,\n' +
1972 ' ten : 10\n' +
1973 '/* beautify preserve:end */\n' +
1974 '};',
1975 # -- output --
1976 'var a = {\n' +
1977 ' /* beautify preserve:start */\n' +
1978 ' one : 1,\n' +
1979 ' two : 2,\n' +
1980 ' three : 3,\n' +
1981 ' ten : 10\n' +
1982 '/* beautify preserve:end */\n' +
1983 '};')
1984  
1985 # one space before and after required, only single spaces inside.
1986 bt(
1987 'var a = {\n' +
1988 '/* beautify preserve:start */\n' +
1989 ' one : 1,\n' +
1990 ' two : 2,\n' +
1991 ' three : 3,\n' +
1992 ' ten : 10\n' +
1993 '};',
1994 # -- output --
1995 'var a = {\n' +
1996 ' /* beautify preserve:start */\n' +
1997 ' one: 1,\n' +
1998 ' two: 2,\n' +
1999 ' three: 3,\n' +
2000 ' ten: 10\n' +
2001 '};')
2002 bt(
2003 'var a = {\n' +
2004 '/*beautify preserve:start*/\n' +
2005 ' one : 1,\n' +
2006 ' two : 2,\n' +
2007 ' three : 3,\n' +
2008 ' ten : 10\n' +
2009 '};',
2010 # -- output --
2011 'var a = {\n' +
2012 ' /*beautify preserve:start*/\n' +
2013 ' one: 1,\n' +
2014 ' two: 2,\n' +
2015 ' three: 3,\n' +
2016 ' ten: 10\n' +
2017 '};')
2018 bt(
2019 'var a = {\n' +
2020 '/*beautify preserve:start*/\n' +
2021 ' one : 1,\n' +
2022 ' two : 2,\n' +
2023 ' three : 3,\n' +
2024 ' ten : 10\n' +
2025 '};',
2026 # -- output --
2027 'var a = {\n' +
2028 ' /*beautify preserve:start*/\n' +
2029 ' one: 1,\n' +
2030 ' two: 2,\n' +
2031 ' three: 3,\n' +
2032 ' ten: 10\n' +
2033 '};')
2034  
2035 # Directive: ignore
2036 bt(
2037 '/* beautify ignore:start */\n' +
2038 '/* beautify ignore:end */')
2039 bt(
2040 '/* beautify ignore:start */\n' +
2041 ' var a,,,{ 1;\n' +
2042 '/* beautify ignore:end */')
2043 bt(
2044 'var a = 1;\n' +
2045 '/* beautify ignore:start */\n' +
2046 ' var a = 1;\n' +
2047 '/* beautify ignore:end */')
2048 bt('/* beautify ignore:start */ {asdklgh;y;+++;dd2d}/* beautify ignore:end */')
2049 bt(
2050 'var a = 1;\n' +
2051 '/* beautify ignore:start */\n' +
2052 ' var a,,,{ 1;\n' +
2053 '/* beautify ignore:end */',
2054 # -- output --
2055 'var a = 1;\n' +
2056 '/* beautify ignore:start */\n' +
2057 ' var a,,,{ 1;\n' +
2058 '/* beautify ignore:end */')
2059 bt(
2060 'var a = 1;\n' +
2061 ' /* beautify ignore:start */\n' +
2062 ' var a,,,{ 1;\n' +
2063 '/* beautify ignore:end */',
2064 # -- output --
2065 'var a = 1;\n' +
2066 '/* beautify ignore:start */\n' +
2067 ' var a,,,{ 1;\n' +
2068 '/* beautify ignore:end */')
2069 bt(
2070 'var a = {\n' +
2071 ' /* beautify ignore:start */\n' +
2072 ' one : 1\n' +
2073 ' two : 2,\n' +
2074 ' three : {\n' +
2075 ' ten : 10\n' +
2076 ' /* beautify ignore:end */\n' +
2077 '};')
2078 bt(
2079 'var a = {\n' +
2080 '/* beautify ignore:start */\n' +
2081 ' one : 1\n' +
2082 ' two : 2,\n' +
2083 ' three : {\n' +
2084 ' ten : 10\n' +
2085 '/* beautify ignore:end */\n' +
2086 '};',
2087 # -- output --
2088 'var a = {\n' +
2089 ' /* beautify ignore:start */\n' +
2090 ' one : 1\n' +
2091 ' two : 2,\n' +
2092 ' three : {\n' +
2093 ' ten : 10\n' +
2094 '/* beautify ignore:end */\n' +
2095 '};')
2096  
2097 # Directives - multiple and interacting
2098 bt(
2099 'var a = {\n' +
2100 '/* beautify preserve:start */\n' +
2101 '/* beautify preserve:start */\n' +
2102 ' one : 1,\n' +
2103 ' /* beautify preserve:end */\n' +
2104 ' two : 2,\n' +
2105 ' three : 3,\n' +
2106 '/* beautify preserve:start */\n' +
2107 ' ten : 10\n' +
2108 '/* beautify preserve:end */\n' +
2109 '};',
2110 # -- output --
2111 'var a = {\n' +
2112 ' /* beautify preserve:start */\n' +
2113 '/* beautify preserve:start */\n' +
2114 ' one : 1,\n' +
2115 ' /* beautify preserve:end */\n' +
2116 ' two: 2,\n' +
2117 ' three: 3,\n' +
2118 ' /* beautify preserve:start */\n' +
2119 ' ten : 10\n' +
2120 '/* beautify preserve:end */\n' +
2121 '};')
2122 bt(
2123 'var a = {\n' +
2124 '/* beautify ignore:start */\n' +
2125 ' one : 1\n' +
2126 ' /* beautify ignore:end */\n' +
2127 ' two : 2,\n' +
2128 '/* beautify ignore:start */\n' +
2129 ' three : {\n' +
2130 ' ten : 10\n' +
2131 '/* beautify ignore:end */\n' +
2132 '};',
2133 # -- output --
2134 'var a = {\n' +
2135 ' /* beautify ignore:start */\n' +
2136 ' one : 1\n' +
2137 ' /* beautify ignore:end */\n' +
2138 ' two: 2,\n' +
2139 ' /* beautify ignore:start */\n' +
2140 ' three : {\n' +
2141 ' ten : 10\n' +
2142 '/* beautify ignore:end */\n' +
2143 '};')
2144  
2145 # Starts can occur together, ignore:end must occur alone.
2146 bt(
2147 'var a = {\n' +
2148 '/* beautify ignore:start */\n' +
2149 ' one : 1\n' +
2150 ' NOTE: ignore end block does not support starting other directives\n' +
2151 ' This does not match the ending the ignore...\n' +
2152 ' /* beautify ignore:end preserve:start */\n' +
2153 ' two : 2,\n' +
2154 '/* beautify ignore:start */\n' +
2155 ' three : {\n' +
2156 ' ten : 10\n' +
2157 ' ==The next comment ends the starting ignore==\n' +
2158 '/* beautify ignore:end */\n' +
2159 '};',
2160 # -- output --
2161 'var a = {\n' +
2162 ' /* beautify ignore:start */\n' +
2163 ' one : 1\n' +
2164 ' NOTE: ignore end block does not support starting other directives\n' +
2165 ' This does not match the ending the ignore...\n' +
2166 ' /* beautify ignore:end preserve:start */\n' +
2167 ' two : 2,\n' +
2168 '/* beautify ignore:start */\n' +
2169 ' three : {\n' +
2170 ' ten : 10\n' +
2171 ' ==The next comment ends the starting ignore==\n' +
2172 '/* beautify ignore:end */\n' +
2173 '};')
2174 bt(
2175 'var a = {\n' +
2176 '/* beautify ignore:start preserve:start */\n' +
2177 ' one : {\n' +
2178 ' /* beautify ignore:end */\n' +
2179 ' two : 2,\n' +
2180 ' /* beautify ignore:start */\n' +
2181 ' three : {\n' +
2182 '/* beautify ignore:end */\n' +
2183 ' ten : 10\n' +
2184 ' // This is all preserved\n' +
2185 '};',
2186 # -- output --
2187 'var a = {\n' +
2188 ' /* beautify ignore:start preserve:start */\n' +
2189 ' one : {\n' +
2190 ' /* beautify ignore:end */\n' +
2191 ' two : 2,\n' +
2192 ' /* beautify ignore:start */\n' +
2193 ' three : {\n' +
2194 '/* beautify ignore:end */\n' +
2195 ' ten : 10\n' +
2196 ' // This is all preserved\n' +
2197 '};')
2198 bt(
2199 'var a = {\n' +
2200 '/* beautify ignore:start preserve:start */\n' +
2201 ' one : {\n' +
2202 ' /* beautify ignore:end */\n' +
2203 ' two : 2,\n' +
2204 ' /* beautify ignore:start */\n' +
2205 ' three : {\n' +
2206 '/* beautify ignore:end */\n' +
2207 ' ten : 10,\n' +
2208 '/* beautify preserve:end */\n' +
2209 ' eleven: 11\n' +
2210 '};',
2211 # -- output --
2212 'var a = {\n' +
2213 ' /* beautify ignore:start preserve:start */\n' +
2214 ' one : {\n' +
2215 ' /* beautify ignore:end */\n' +
2216 ' two : 2,\n' +
2217 ' /* beautify ignore:start */\n' +
2218 ' three : {\n' +
2219 '/* beautify ignore:end */\n' +
2220 ' ten : 10,\n' +
2221 '/* beautify preserve:end */\n' +
2222 ' eleven: 11\n' +
2223 '};')
2224  
2225  
2226 #============================================================
2227 # Comments and tests
2228 self.reset_options();
2229  
2230 # #913
2231 bt(
2232 'class test {\n' +
2233 ' method1() {\n' +
2234 ' let resp = null;\n' +
2235 ' }\n' +
2236 ' /**\n' +
2237 ' * @param {String} id\n' +
2238 ' */\n' +
2239 ' method2(id) {\n' +
2240 ' let resp2 = null;\n' +
2241 ' }\n' +
2242 '}')
2243  
2244 # #1090
2245 bt(
2246 'for (var i = 0; i < 20; ++i) // loop\n' +
2247 ' if (i % 3) {\n' +
2248 ' console.log(i);\n' +
2249 ' }\n' +
2250 'console.log("done");')
2251  
2252 # #1043
2253 bt(
2254 'var o = {\n' +
2255 ' k: 0\n' +
2256 '}\n' +
2257 '// ...\n' +
2258 'foo(o)')
2259  
2260 # #713 and #964
2261 bt(
2262 'Meteor.call("foo", bar, function(err, result) {\n' +
2263 ' Session.set("baz", result.lorem)\n' +
2264 '})\n' +
2265 '//blah blah')
2266  
2267 # #815
2268 bt(
2269 'foo()\n' +
2270 '// this is a comment\n' +
2271 'bar()\n' +
2272 '\n' +
2273 'const foo = 5\n' +
2274 '// comment\n' +
2275 'bar()')
2276  
2277 # This shows current behavior. Note #1069 is not addressed yet.
2278 bt(
2279 'if (modulus === 2) {\n' +
2280 ' // i might be odd here\n' +
2281 ' i += (i & 1);\n' +
2282 ' // now i is guaranteed to be even\n' +
2283 ' // this block is obviously about the statement above\n' +
2284 '\n' +
2285 ' // #1069 This should attach to the block below\n' +
2286 ' // this comment is about the block after it.\n' +
2287 '} else {\n' +
2288 ' // rounding up using integer arithmetic only\n' +
2289 ' if (i % modulus)\n' +
2290 ' i += modulus - (i % modulus);\n' +
2291 ' // now i is divisible by modulus\n' +
2292 ' // behavior of comments should be different for single statements vs block statements/expressions\n' +
2293 '}\n' +
2294 '\n' +
2295 'if (modulus === 2)\n' +
2296 ' // i might be odd here\n' +
2297 ' i += (i & 1);\n' +
2298 '// now i is guaranteed to be even\n' +
2299 '// non-braced comments unindent immediately\n' +
2300 '\n' +
2301 '// this comment is about the block after it.\n' +
2302 'else\n' +
2303 ' // rounding up using integer arithmetic only\n' +
2304 ' if (i % modulus)\n' +
2305 ' i += modulus - (i % modulus);\n' +
2306 '// behavior of comments should be different for single statements vs block statements/expressions')
2307  
2308  
2309 #============================================================
2310 # Template Formatting
2311 self.reset_options();
2312 bt('<?=$view["name"]; ?>')
2313 bt('a = <?= external() ?>;')
2314 bt(
2315 '<?php\n' +
2316 'for($i = 1; $i <= 100; $i++;) {\n' +
2317 ' #count to 100!\n' +
2318 ' echo($i . "</br>");\n' +
2319 '}\n' +
2320 '?>')
2321 bt('a = <%= external() %>;')
2322  
2323  
2324 #============================================================
2325 # jslint and space after anon function - (f = " ", c = "")
2326 self.reset_options();
2327 self.options.jslint_happy = true
2328 self.options.space_after_anon_function = true
2329 bt(
2330 'a=typeof(x)',
2331 # -- output --
2332 'a = typeof (x)')
2333 bt(
2334 'x();\n' +
2335 '\n' +
2336 'function(){}',
2337 # -- output --
2338 'x();\n' +
2339 '\n' +
2340 'function () {}')
2341 bt(
2342 'x();\n' +
2343 '\n' +
2344 'var x = {\n' +
2345 'x: function(){}\n' +
2346 '}',
2347 # -- output --
2348 'x();\n' +
2349 '\n' +
2350 'var x = {\n' +
2351 ' x: function () {}\n' +
2352 '}')
2353 bt(
2354 'function () {\n' +
2355 ' var a, b, c, d, e = [],\n' +
2356 ' f;\n' +
2357 '}')
2358 bt(
2359 'switch(x) {case 0: case 1: a(); break; default: break}',
2360 # -- output --
2361 'switch (x) {\n' +
2362 'case 0:\n' +
2363 'case 1:\n' +
2364 ' a();\n' +
2365 ' break;\n' +
2366 'default:\n' +
2367 ' break\n' +
2368 '}')
2369 bt(
2370 'switch(x){case -1:break;case !y:break;}',
2371 # -- output --
2372 'switch (x) {\n' +
2373 'case -1:\n' +
2374 ' break;\n' +
2375 'case !y:\n' +
2376 ' break;\n' +
2377 '}')
2378  
2379 # typical greasemonkey start
2380 test_fragment(
2381 '// comment 2\n' +
2382 '(function ()')
2383 bt(
2384 'var a2, b2, c2, d2 = 0, c = function() {}, d = \'\';',
2385 # -- output --
2386 'var a2, b2, c2, d2 = 0,\n' +
2387 ' c = function () {},\n' +
2388 ' d = \'\';')
2389 bt(
2390 'var a2, b2, c2, d2 = 0, c = function() {},\n' +
2391 'd = \'\';',
2392 # -- output --
2393 'var a2, b2, c2, d2 = 0,\n' +
2394 ' c = function () {},\n' +
2395 ' d = \'\';')
2396 bt(
2397 'var o2=$.extend(a);function(){alert(x);}',
2398 # -- output --
2399 'var o2 = $.extend(a);\n' +
2400 '\n' +
2401 'function () {\n' +
2402 ' alert(x);\n' +
2403 '}')
2404 bt(
2405 'function*() {\n' +
2406 ' yield 1;\n' +
2407 '}',
2408 # -- output --
2409 'function* () {\n' +
2410 ' yield 1;\n' +
2411 '}')
2412 bt(
2413 'function* x() {\n' +
2414 ' yield 1;\n' +
2415 '}')
2416  
2417 # jslint and space after anon function - (f = " ", c = "")
2418 self.reset_options();
2419 self.options.jslint_happy = true
2420 self.options.space_after_anon_function = false
2421 bt(
2422 'a=typeof(x)',
2423 # -- output --
2424 'a = typeof (x)')
2425 bt(
2426 'x();\n' +
2427 '\n' +
2428 'function(){}',
2429 # -- output --
2430 'x();\n' +
2431 '\n' +
2432 'function () {}')
2433 bt(
2434 'x();\n' +
2435 '\n' +
2436 'var x = {\n' +
2437 'x: function(){}\n' +
2438 '}',
2439 # -- output --
2440 'x();\n' +
2441 '\n' +
2442 'var x = {\n' +
2443 ' x: function () {}\n' +
2444 '}')
2445 bt(
2446 'function () {\n' +
2447 ' var a, b, c, d, e = [],\n' +
2448 ' f;\n' +
2449 '}')
2450 bt(
2451 'switch(x) {case 0: case 1: a(); break; default: break}',
2452 # -- output --
2453 'switch (x) {\n' +
2454 'case 0:\n' +
2455 'case 1:\n' +
2456 ' a();\n' +
2457 ' break;\n' +
2458 'default:\n' +
2459 ' break\n' +
2460 '}')
2461 bt(
2462 'switch(x){case -1:break;case !y:break;}',
2463 # -- output --
2464 'switch (x) {\n' +
2465 'case -1:\n' +
2466 ' break;\n' +
2467 'case !y:\n' +
2468 ' break;\n' +
2469 '}')
2470  
2471 # typical greasemonkey start
2472 test_fragment(
2473 '// comment 2\n' +
2474 '(function ()')
2475 bt(
2476 'var a2, b2, c2, d2 = 0, c = function() {}, d = \'\';',
2477 # -- output --
2478 'var a2, b2, c2, d2 = 0,\n' +
2479 ' c = function () {},\n' +
2480 ' d = \'\';')
2481 bt(
2482 'var a2, b2, c2, d2 = 0, c = function() {},\n' +
2483 'd = \'\';',
2484 # -- output --
2485 'var a2, b2, c2, d2 = 0,\n' +
2486 ' c = function () {},\n' +
2487 ' d = \'\';')
2488 bt(
2489 'var o2=$.extend(a);function(){alert(x);}',
2490 # -- output --
2491 'var o2 = $.extend(a);\n' +
2492 '\n' +
2493 'function () {\n' +
2494 ' alert(x);\n' +
2495 '}')
2496 bt(
2497 'function*() {\n' +
2498 ' yield 1;\n' +
2499 '}',
2500 # -- output --
2501 'function* () {\n' +
2502 ' yield 1;\n' +
2503 '}')
2504 bt(
2505 'function* x() {\n' +
2506 ' yield 1;\n' +
2507 '}')
2508  
2509 # jslint and space after anon function - (f = " ", c = " ")
2510 self.reset_options();
2511 self.options.jslint_happy = false
2512 self.options.space_after_anon_function = true
2513 bt(
2514 'a=typeof(x)',
2515 # -- output --
2516 'a = typeof (x)')
2517 bt(
2518 'x();\n' +
2519 '\n' +
2520 'function(){}',
2521 # -- output --
2522 'x();\n' +
2523 '\n' +
2524 'function () {}')
2525 bt(
2526 'x();\n' +
2527 '\n' +
2528 'var x = {\n' +
2529 'x: function(){}\n' +
2530 '}',
2531 # -- output --
2532 'x();\n' +
2533 '\n' +
2534 'var x = {\n' +
2535 ' x: function () {}\n' +
2536 '}')
2537 bt(
2538 'function () {\n' +
2539 ' var a, b, c, d, e = [],\n' +
2540 ' f;\n' +
2541 '}')
2542 bt(
2543 'switch(x) {case 0: case 1: a(); break; default: break}',
2544 # -- output --
2545 'switch (x) {\n' +
2546 ' case 0:\n' +
2547 ' case 1:\n' +
2548 ' a();\n' +
2549 ' break;\n' +
2550 ' default:\n' +
2551 ' break\n' +
2552 '}')
2553 bt(
2554 'switch(x){case -1:break;case !y:break;}',
2555 # -- output --
2556 'switch (x) {\n' +
2557 ' case -1:\n' +
2558 ' break;\n' +
2559 ' case !y:\n' +
2560 ' break;\n' +
2561 '}')
2562  
2563 # typical greasemonkey start
2564 test_fragment(
2565 '// comment 2\n' +
2566 '(function ()')
2567 bt(
2568 'var a2, b2, c2, d2 = 0, c = function() {}, d = \'\';',
2569 # -- output --
2570 'var a2, b2, c2, d2 = 0,\n' +
2571 ' c = function () {},\n' +
2572 ' d = \'\';')
2573 bt(
2574 'var a2, b2, c2, d2 = 0, c = function() {},\n' +
2575 'd = \'\';',
2576 # -- output --
2577 'var a2, b2, c2, d2 = 0,\n' +
2578 ' c = function () {},\n' +
2579 ' d = \'\';')
2580 bt(
2581 'var o2=$.extend(a);function(){alert(x);}',
2582 # -- output --
2583 'var o2 = $.extend(a);\n' +
2584 '\n' +
2585 'function () {\n' +
2586 ' alert(x);\n' +
2587 '}')
2588 bt(
2589 'function*() {\n' +
2590 ' yield 1;\n' +
2591 '}',
2592 # -- output --
2593 'function* () {\n' +
2594 ' yield 1;\n' +
2595 '}')
2596 bt(
2597 'function* x() {\n' +
2598 ' yield 1;\n' +
2599 '}')
2600  
2601 # jslint and space after anon function - (f = "", c = " ")
2602 self.reset_options();
2603 self.options.jslint_happy = false
2604 self.options.space_after_anon_function = false
2605 bt(
2606 'a=typeof(x)',
2607 # -- output --
2608 'a = typeof(x)')
2609 bt(
2610 'x();\n' +
2611 '\n' +
2612 'function(){}',
2613 # -- output --
2614 'x();\n' +
2615 '\n' +
2616 'function() {}')
2617 bt(
2618 'x();\n' +
2619 '\n' +
2620 'var x = {\n' +
2621 'x: function(){}\n' +
2622 '}',
2623 # -- output --
2624 'x();\n' +
2625 '\n' +
2626 'var x = {\n' +
2627 ' x: function() {}\n' +
2628 '}')
2629 bt(
2630 'function () {\n' +
2631 ' var a, b, c, d, e = [],\n' +
2632 ' f;\n' +
2633 '}',
2634 # -- output --
2635 'function() {\n' +
2636 ' var a, b, c, d, e = [],\n' +
2637 ' f;\n' +
2638 '}')
2639 bt(
2640 'switch(x) {case 0: case 1: a(); break; default: break}',
2641 # -- output --
2642 'switch (x) {\n' +
2643 ' case 0:\n' +
2644 ' case 1:\n' +
2645 ' a();\n' +
2646 ' break;\n' +
2647 ' default:\n' +
2648 ' break\n' +
2649 '}')
2650 bt(
2651 'switch(x){case -1:break;case !y:break;}',
2652 # -- output --
2653 'switch (x) {\n' +
2654 ' case -1:\n' +
2655 ' break;\n' +
2656 ' case !y:\n' +
2657 ' break;\n' +
2658 '}')
2659  
2660 # typical greasemonkey start
2661 test_fragment(
2662 '// comment 2\n' +
2663 '(function()')
2664 bt(
2665 'var a2, b2, c2, d2 = 0, c = function() {}, d = \'\';',
2666 # -- output --
2667 'var a2, b2, c2, d2 = 0,\n' +
2668 ' c = function() {},\n' +
2669 ' d = \'\';')
2670 bt(
2671 'var a2, b2, c2, d2 = 0, c = function() {},\n' +
2672 'd = \'\';',
2673 # -- output --
2674 'var a2, b2, c2, d2 = 0,\n' +
2675 ' c = function() {},\n' +
2676 ' d = \'\';')
2677 bt(
2678 'var o2=$.extend(a);function(){alert(x);}',
2679 # -- output --
2680 'var o2 = $.extend(a);\n' +
2681 '\n' +
2682 'function() {\n' +
2683 ' alert(x);\n' +
2684 '}')
2685 bt(
2686 'function*() {\n' +
2687 ' yield 1;\n' +
2688 '}')
2689 bt(
2690 'function* x() {\n' +
2691 ' yield 1;\n' +
2692 '}')
2693  
2694  
2695 #============================================================
2696 # Regression tests
2697 self.reset_options();
2698  
2699 # Issue 241
2700 bt(
2701 'obj\n' +
2702 ' .last({\n' +
2703 ' foo: 1,\n' +
2704 ' bar: 2\n' +
2705 ' });\n' +
2706 'var test = 1;')
2707 bt(
2708 'obj\n' +
2709 ' .last(a, function() {\n' +
2710 ' var test;\n' +
2711 ' });\n' +
2712 'var test = 1;')
2713 bt(
2714 'obj.first()\n' +
2715 ' .second()\n' +
2716 ' .last(function(err, response) {\n' +
2717 ' console.log(err);\n' +
2718 ' });')
2719  
2720 # Issue 268 and 275
2721 bt(
2722 'obj.last(a, function() {\n' +
2723 ' var test;\n' +
2724 '});\n' +
2725 'var test = 1;')
2726 bt(
2727 'obj.last(a,\n' +
2728 ' function() {\n' +
2729 ' var test;\n' +
2730 ' });\n' +
2731 'var test = 1;')
2732 bt(
2733 '(function() {if (!window.FOO) window.FOO || (window.FOO = function() {var b = {bar: "zort"};});})();',
2734 # -- output --
2735 '(function() {\n' +
2736 ' if (!window.FOO) window.FOO || (window.FOO = function() {\n' +
2737 ' var b = {\n' +
2738 ' bar: "zort"\n' +
2739 ' };\n' +
2740 ' });\n' +
2741 '})();')
2742  
2743 # Issue 281
2744 bt(
2745 'define(["dojo/_base/declare", "my/Employee", "dijit/form/Button",\n' +
2746 ' "dojo/_base/lang", "dojo/Deferred"\n' +
2747 '], function(declare, Employee, Button, lang, Deferred) {\n' +
2748 ' return declare(Employee, {\n' +
2749 ' constructor: function() {\n' +
2750 ' new Button({\n' +
2751 ' onClick: lang.hitch(this, function() {\n' +
2752 ' new Deferred().then(lang.hitch(this, function() {\n' +
2753 ' this.salary * 0.25;\n' +
2754 ' }));\n' +
2755 ' })\n' +
2756 ' });\n' +
2757 ' }\n' +
2758 ' });\n' +
2759 '});')
2760 bt(
2761 'define(["dojo/_base/declare", "my/Employee", "dijit/form/Button",\n' +
2762 ' "dojo/_base/lang", "dojo/Deferred"\n' +
2763 ' ],\n' +
2764 ' function(declare, Employee, Button, lang, Deferred) {\n' +
2765 ' return declare(Employee, {\n' +
2766 ' constructor: function() {\n' +
2767 ' new Button({\n' +
2768 ' onClick: lang.hitch(this, function() {\n' +
2769 ' new Deferred().then(lang.hitch(this, function() {\n' +
2770 ' this.salary * 0.25;\n' +
2771 ' }));\n' +
2772 ' })\n' +
2773 ' });\n' +
2774 ' }\n' +
2775 ' });\n' +
2776 ' });')
2777  
2778 # Issue 459
2779 bt(
2780 '(function() {\n' +
2781 ' return {\n' +
2782 ' foo: function() {\n' +
2783 ' return "bar";\n' +
2784 ' },\n' +
2785 ' bar: ["bar"]\n' +
2786 ' };\n' +
2787 '}());')
2788  
2789 # Issue 505 - strings should end at newline unless continued by backslash
2790 bt(
2791 'var name = "a;\n' +
2792 'name = "b";')
2793 bt(
2794 'var name = "a;\\\n' +
2795 ' name = b";')
2796  
2797 # Issue 514 - some operators require spaces to distinguish them
2798 bt('var c = "_ACTION_TO_NATIVEAPI_" + ++g++ + +new Date;')
2799 bt('var c = "_ACTION_TO_NATIVEAPI_" - --g-- - -new Date;')
2800  
2801 # Issue 440 - reserved words can be used as object property names
2802 bt(
2803 'a = {\n' +
2804 ' function: {},\n' +
2805 ' "function": {},\n' +
2806 ' throw: {},\n' +
2807 ' "throw": {},\n' +
2808 ' var: {},\n' +
2809 ' "var": {},\n' +
2810 ' set: {},\n' +
2811 ' "set": {},\n' +
2812 ' get: {},\n' +
2813 ' "get": {},\n' +
2814 ' if: {},\n' +
2815 ' "if": {},\n' +
2816 ' then: {},\n' +
2817 ' "then": {},\n' +
2818 ' else: {},\n' +
2819 ' "else": {},\n' +
2820 ' yay: {}\n' +
2821 '};')
2822  
2823 # Issue 331 - if-else with braces edge case
2824 bt(
2825 'if(x){a();}else{b();}if(y){c();}',
2826 # -- output --
2827 'if (x) {\n' +
2828 ' a();\n' +
2829 '} else {\n' +
2830 ' b();\n' +
2831 '}\n' +
2832 'if (y) {\n' +
2833 ' c();\n' +
2834 '}')
2835  
2836 # Issue 485 - ensure function declarations behave the same in arrays as elsewhere
2837 bt(
2838 'var v = ["a",\n' +
2839 ' function() {\n' +
2840 ' return;\n' +
2841 ' }, {\n' +
2842 ' id: 1\n' +
2843 ' }\n' +
2844 '];')
2845 bt(
2846 'var v = ["a", function() {\n' +
2847 ' return;\n' +
2848 '}, {\n' +
2849 ' id: 1\n' +
2850 '}];')
2851  
2852 # Issue 382 - initial totally cursory support for es6 module export
2853 bt(
2854 'module "Even" {\n' +
2855 ' import odd from "Odd";\n' +
2856 ' export function sum(x, y) {\n' +
2857 ' return x + y;\n' +
2858 ' }\n' +
2859 ' export var pi = 3.141593;\n' +
2860 ' export default moduleName;\n' +
2861 '}')
2862 bt(
2863 'module "Even" {\n' +
2864 ' export default function div(x, y) {}\n' +
2865 '}')
2866  
2867 # Issue 889 - export default { ... }
2868 bt(
2869 'export default {\n' +
2870 ' func1() {},\n' +
2871 ' func2() {}\n' +
2872 ' func3() {}\n' +
2873 '}')
2874 bt(
2875 'export default {\n' +
2876 ' a() {\n' +
2877 ' return 1;\n' +
2878 ' },\n' +
2879 ' b() {\n' +
2880 ' return 2;\n' +
2881 ' },\n' +
2882 ' c() {\n' +
2883 ' return 3;\n' +
2884 ' }\n' +
2885 '}')
2886  
2887 # Issue 508
2888 bt('set["name"]')
2889 bt('get["name"]')
2890 bt(
2891 'a = {\n' +
2892 ' set b(x) {},\n' +
2893 ' c: 1,\n' +
2894 ' d: function() {}\n' +
2895 '};')
2896 bt(
2897 'a = {\n' +
2898 ' get b() {\n' +
2899 ' retun 0;\n' +
2900 ' },\n' +
2901 ' c: 1,\n' +
2902 ' d: function() {}\n' +
2903 '};')
2904  
2905 # Issue 298 - do not under indent if/while/for condtionals experesions
2906 bt(
2907 '\'use strict\';\n' +
2908 'if ([].some(function() {\n' +
2909 ' return false;\n' +
2910 ' })) {\n' +
2911 ' console.log("hello");\n' +
2912 '}')
2913  
2914 # Issue 298 - do not under indent if/while/for condtionals experesions
2915 bt(
2916 '\'use strict\';\n' +
2917 'if ([].some(function() {\n' +
2918 ' return false;\n' +
2919 ' })) {\n' +
2920 ' console.log("hello");\n' +
2921 '}')
2922  
2923 # Issue 552 - Typescript? Okay... we didn't break it before, so try not to break it now.
2924 bt(
2925 'class Test {\n' +
2926 ' blah: string[];\n' +
2927 ' foo(): number {\n' +
2928 ' return 0;\n' +
2929 ' }\n' +
2930 ' bar(): number {\n' +
2931 ' return 0;\n' +
2932 ' }\n' +
2933 '}')
2934 bt(
2935 'interface Test {\n' +
2936 ' blah: string[];\n' +
2937 ' foo(): number {\n' +
2938 ' return 0;\n' +
2939 ' }\n' +
2940 ' bar(): number {\n' +
2941 ' return 0;\n' +
2942 ' }\n' +
2943 '}')
2944  
2945 # Issue 583 - Functions with comments after them should still indent correctly.
2946 bt(
2947 'function exit(code) {\n' +
2948 ' setTimeout(function() {\n' +
2949 ' phantom.exit(code);\n' +
2950 ' }, 0);\n' +
2951 ' phantom.onError = function() {};\n' +
2952 '}\n' +
2953 '// Comment')
2954  
2955 # Issue 806 - newline arrow functions
2956 bt(
2957 'a.b("c",\n' +
2958 ' () => d.e\n' +
2959 ')')
2960  
2961 # Issue 810 - es6 object literal detection
2962 bt(
2963 'function badFormatting() {\n' +
2964 ' return {\n' +
2965 ' a,\n' +
2966 ' b: c,\n' +
2967 ' d: e,\n' +
2968 ' f: g,\n' +
2969 ' h,\n' +
2970 ' i,\n' +
2971 ' j: k\n' +
2972 ' }\n' +
2973 '}\n' +
2974 '\n' +
2975 'function goodFormatting() {\n' +
2976 ' return {\n' +
2977 ' a: b,\n' +
2978 ' c,\n' +
2979 ' d: e,\n' +
2980 ' f: g,\n' +
2981 ' h,\n' +
2982 ' i,\n' +
2983 ' j: k\n' +
2984 ' }\n' +
2985 '}')
2986  
2987 # Issue 602 - ES6 object literal shorthand functions
2988 bt(
2989 'return {\n' +
2990 ' fn1() {},\n' +
2991 ' fn2() {}\n' +
2992 '}')
2993 bt(
2994 'throw {\n' +
2995 ' fn1() {},\n' +
2996 ' fn2() {}\n' +
2997 '}')
2998 bt(
2999 'foo({\n' +
3000 ' fn1(a) {}\n' +
3001 ' fn2(a) {}\n' +
3002 '})')
3003 bt(
3004 'foo("text", {\n' +
3005 ' fn1(a) {}\n' +
3006 ' fn2(a) {}\n' +
3007 '})')
3008 bt(
3009 'oneArg = {\n' +
3010 ' fn1(a) {\n' +
3011 ' do();\n' +
3012 ' },\n' +
3013 ' fn2() {}\n' +
3014 '}')
3015 bt(
3016 'multiArg = {\n' +
3017 ' fn1(a, b, c) {\n' +
3018 ' do();\n' +
3019 ' },\n' +
3020 ' fn2() {}\n' +
3021 '}')
3022 bt(
3023 'noArgs = {\n' +
3024 ' fn1() {\n' +
3025 ' do();\n' +
3026 ' },\n' +
3027 ' fn2() {}\n' +
3028 '}')
3029 bt(
3030 'emptyFn = {\n' +
3031 ' fn1() {},\n' +
3032 ' fn2() {}\n' +
3033 '}')
3034 bt(
3035 'nested = {\n' +
3036 ' fns: {\n' +
3037 ' fn1() {},\n' +
3038 ' fn2() {}\n' +
3039 ' }\n' +
3040 '}')
3041 bt(
3042 'array = [{\n' +
3043 ' fn1() {},\n' +
3044 ' prop: val,\n' +
3045 ' fn2() {}\n' +
3046 '}]')
3047 bt(
3048 'expr = expr ? expr : {\n' +
3049 ' fn1() {},\n' +
3050 ' fn2() {}\n' +
3051 '}')
3052 bt(
3053 'strange = valid + {\n' +
3054 ' fn1() {},\n' +
3055 ' fn2() {\n' +
3056 ' return 1;\n' +
3057 ' }\n' +
3058 '}.fn2()')
3059  
3060 # Issue 854 - Arrow function with statement block
3061 bt(
3062 'test(() => {\n' +
3063 ' var a = {}\n' +
3064 '\n' +
3065 ' a.what = () => true ? 1 : 2\n' +
3066 '\n' +
3067 ' a.thing = () => {\n' +
3068 ' b();\n' +
3069 ' }\n' +
3070 '})')
3071  
3072 # Issue 406 - Multiline array
3073 bt(
3074 'var tempName = [\n' +
3075 ' "temp",\n' +
3076 ' process.pid,\n' +
3077 ' (Math.random() * 0x1000000000).toString(36),\n' +
3078 ' new Date().getTime()\n' +
3079 '].join("-");')
3080  
3081 # Issue #996 - Input ends with backslash throws exception
3082 test_fragment(
3083 'sd = 1;\n' +
3084 '/')
3085  
3086 # Issue #1079 - unbraced if with comments should still look right
3087 bt(
3088 'if (console.log)\n' +
3089 ' for (var i = 0; i < 20; ++i)\n' +
3090 ' if (i % 3)\n' +
3091 ' console.log(i);\n' +
3092 '// all done\n' +
3093 'console.log("done");')
3094  
3095 # Issue #1085 - function should not have blank line in a number of cases
3096 bt(
3097 'var transformer =\n' +
3098 ' options.transformer ||\n' +
3099 ' globalSettings.transformer ||\n' +
3100 ' function(x) {\n' +
3101 ' return x;\n' +
3102 ' };')
3103  
3104 # Issue #569 - function should not have blank line in a number of cases
3105 bt(
3106 '(function(global) {\n' +
3107 ' "use strict";\n' +
3108 '\n' +
3109 ' /* jshint ignore:start */\n' +
3110 ' include "somefile.js"\n' +
3111 ' /* jshint ignore:end */\n' +
3112 '}(this));')
3113 bt(
3114 'function bindAuthEvent(eventName) {\n' +
3115 ' self.auth.on(eventName, function(event, meta) {\n' +
3116 ' self.emit(eventName, event, meta);\n' +
3117 ' });\n' +
3118 '}\n' +
3119 '["logged_in", "logged_out", "signed_up", "updated_user"].forEach(bindAuthEvent);\n' +
3120 '\n' +
3121 'function bindBrowserEvent(eventName) {\n' +
3122 ' browser.on(eventName, function(event, meta) {\n' +
3123 ' self.emit(eventName, event, meta);\n' +
3124 ' });\n' +
3125 '}\n' +
3126 '["navigating"].forEach(bindBrowserEvent);')
3127  
3128 # Issue #892 - new line between chained methods
3129 bt(
3130 'foo\n' +
3131 ' .who()\n' +
3132 '\n' +
3133 ' .knows()\n' +
3134 ' // comment\n' +
3135 ' .nothing() // comment\n' +
3136 '\n' +
3137 ' .more()')
3138  
3139  
3140 #============================================================
3141 # Test non-positionable-ops
3142 self.reset_options();
3143 bt('a += 2;')
3144 bt('a -= 2;')
3145 bt('a *= 2;')
3146 bt('a /= 2;')
3147 bt('a %= 2;')
3148 bt('a &= 2;')
3149 bt('a ^= 2;')
3150 bt('a |= 2;')
3151 bt('a **= 2;')
3152 bt('a <<= 2;')
3153 bt('a >>= 2;')
3154  
3155  
3156 #============================================================
3157 # brace_style ,preserve-inline tests - (obo = " ", obot = "", oao = "\n", oaot = " ", obc = "\n", oac = " ", oact = "")
3158 self.reset_options();
3159 self.options.brace_style = 'collapse,preserve-inline'
3160 bt('import { asdf } from "asdf";')
3161 bt('import { get } from "asdf";')
3162 bt('function inLine() { console.log("oh em gee"); }')
3163 bt('if (cancer) { console.log("Im sorry but you only have so long to live..."); }')
3164 bt('if (ding) { console.log("dong"); } else { console.log("dang"); }')
3165 bt(
3166 'function kindaComplex() {\n' +
3167 ' var a = 2;\n' +
3168 ' var obj = {};\n' +
3169 ' var obj2 = { a: "a", b: "b" };\n' +
3170 ' var obj3 = {\n' +
3171 ' c: "c",\n' +
3172 ' d: "d",\n' +
3173 ' e: "e"\n' +
3174 ' };\n' +
3175 '}')
3176 bt(
3177 'function complex() {\n' +
3178 ' console.log("wowe");\n' +
3179 ' (function() { var a = 2; var b = 3; })();\n' +
3180 ' $.each(arr, function(el, idx) { return el; });\n' +
3181 ' var obj = {\n' +
3182 ' a: function() { console.log("test"); },\n' +
3183 ' b() {\n' +
3184 ' console.log("test2");\n' +
3185 ' }\n' +
3186 ' };\n' +
3187 '}',
3188 # -- output --
3189 'function complex() {\n' +
3190 ' console.log("wowe");\n' +
3191 ' (function() { var a = 2; var b = 3; })();\n' +
3192 ' $.each(arr, function(el, idx) { return el; });\n' +
3193 ' var obj = {\n' +
3194 ' a: function() { console.log("test"); },\n' +
3195 ' b() {\n' +
3196 ' console.log("test2");\n' +
3197 ' }\n' +
3198 ' };\n' +
3199 '}')
3200  
3201 # brace_style ,preserve-inline tests - (obo = "\n", obot = " ", oao = "\n", oaot = " ", obc = "\n", oac = "\n", oact = " ")
3202 self.reset_options();
3203 self.options.brace_style = 'expand,preserve-inline'
3204 bt('import { asdf } from "asdf";')
3205 bt('import { get } from "asdf";')
3206 bt('function inLine() { console.log("oh em gee"); }')
3207 bt('if (cancer) { console.log("Im sorry but you only have so long to live..."); }')
3208 bt(
3209 'if (ding) { console.log("dong"); } else { console.log("dang"); }',
3210 # -- output --
3211 'if (ding) { console.log("dong"); }\n' +
3212 'else { console.log("dang"); }')
3213 bt(
3214 'function kindaComplex() {\n' +
3215 ' var a = 2;\n' +
3216 ' var obj = {};\n' +
3217 ' var obj2 = { a: "a", b: "b" };\n' +
3218 ' var obj3 = {\n' +
3219 ' c: "c",\n' +
3220 ' d: "d",\n' +
3221 ' e: "e"\n' +
3222 ' };\n' +
3223 '}',
3224 # -- output --
3225 'function kindaComplex()\n' +
3226 '{\n' +
3227 ' var a = 2;\n' +
3228 ' var obj = {};\n' +
3229 ' var obj2 = { a: "a", b: "b" };\n' +
3230 ' var obj3 = {\n' +
3231 ' c: "c",\n' +
3232 ' d: "d",\n' +
3233 ' e: "e"\n' +
3234 ' };\n' +
3235 '}')
3236 bt(
3237 'function complex() {\n' +
3238 ' console.log("wowe");\n' +
3239 ' (function() { var a = 2; var b = 3; })();\n' +
3240 ' $.each(arr, function(el, idx) { return el; });\n' +
3241 ' var obj = {\n' +
3242 ' a: function() { console.log("test"); },\n' +
3243 ' b() {\n' +
3244 ' console.log("test2");\n' +
3245 ' }\n' +
3246 ' };\n' +
3247 '}',
3248 # -- output --
3249 'function complex()\n' +
3250 '{\n' +
3251 ' console.log("wowe");\n' +
3252 ' (function() { var a = 2; var b = 3; })();\n' +
3253 ' $.each(arr, function(el, idx) { return el; });\n' +
3254 ' var obj = {\n' +
3255 ' a: function() { console.log("test"); },\n' +
3256 ' b()\n' +
3257 ' {\n' +
3258 ' console.log("test2");\n' +
3259 ' }\n' +
3260 ' };\n' +
3261 '}')
3262  
3263 # brace_style ,preserve-inline tests - (obo = " ", obot = "", oao = "\n", oaot = " ", obc = "\n", oac = "\n", oact = " ")
3264 self.reset_options();
3265 self.options.brace_style = 'end-expand,preserve-inline'
3266 bt('import { asdf } from "asdf";')
3267 bt('import { get } from "asdf";')
3268 bt('function inLine() { console.log("oh em gee"); }')
3269 bt('if (cancer) { console.log("Im sorry but you only have so long to live..."); }')
3270 bt(
3271 'if (ding) { console.log("dong"); } else { console.log("dang"); }',
3272 # -- output --
3273 'if (ding) { console.log("dong"); }\n' +
3274 'else { console.log("dang"); }')
3275 bt(
3276 'function kindaComplex() {\n' +
3277 ' var a = 2;\n' +
3278 ' var obj = {};\n' +
3279 ' var obj2 = { a: "a", b: "b" };\n' +
3280 ' var obj3 = {\n' +
3281 ' c: "c",\n' +
3282 ' d: "d",\n' +
3283 ' e: "e"\n' +
3284 ' };\n' +
3285 '}')
3286 bt(
3287 'function complex() {\n' +
3288 ' console.log("wowe");\n' +
3289 ' (function() { var a = 2; var b = 3; })();\n' +
3290 ' $.each(arr, function(el, idx) { return el; });\n' +
3291 ' var obj = {\n' +
3292 ' a: function() { console.log("test"); },\n' +
3293 ' b() {\n' +
3294 ' console.log("test2");\n' +
3295 ' }\n' +
3296 ' };\n' +
3297 '}',
3298 # -- output --
3299 'function complex() {\n' +
3300 ' console.log("wowe");\n' +
3301 ' (function() { var a = 2; var b = 3; })();\n' +
3302 ' $.each(arr, function(el, idx) { return el; });\n' +
3303 ' var obj = {\n' +
3304 ' a: function() { console.log("test"); },\n' +
3305 ' b() {\n' +
3306 ' console.log("test2");\n' +
3307 ' }\n' +
3308 ' };\n' +
3309 '}')
3310  
3311 # brace_style ,preserve-inline tests - (obo = " ", obot = "", oao = "\n", oaot = " ", obc = "\n", oac = " ", oact = "")
3312 self.reset_options();
3313 self.options.brace_style = 'none,preserve-inline'
3314 bt('import { asdf } from "asdf";')
3315 bt('import { get } from "asdf";')
3316 bt('function inLine() { console.log("oh em gee"); }')
3317 bt('if (cancer) { console.log("Im sorry but you only have so long to live..."); }')
3318 bt('if (ding) { console.log("dong"); } else { console.log("dang"); }')
3319 bt(
3320 'function kindaComplex() {\n' +
3321 ' var a = 2;\n' +
3322 ' var obj = {};\n' +
3323 ' var obj2 = { a: "a", b: "b" };\n' +
3324 ' var obj3 = {\n' +
3325 ' c: "c",\n' +
3326 ' d: "d",\n' +
3327 ' e: "e"\n' +
3328 ' };\n' +
3329 '}')
3330 bt(
3331 'function complex() {\n' +
3332 ' console.log("wowe");\n' +
3333 ' (function() { var a = 2; var b = 3; })();\n' +
3334 ' $.each(arr, function(el, idx) { return el; });\n' +
3335 ' var obj = {\n' +
3336 ' a: function() { console.log("test"); },\n' +
3337 ' b() {\n' +
3338 ' console.log("test2");\n' +
3339 ' }\n' +
3340 ' };\n' +
3341 '}',
3342 # -- output --
3343 'function complex() {\n' +
3344 ' console.log("wowe");\n' +
3345 ' (function() { var a = 2; var b = 3; })();\n' +
3346 ' $.each(arr, function(el, idx) { return el; });\n' +
3347 ' var obj = {\n' +
3348 ' a: function() { console.log("test"); },\n' +
3349 ' b() {\n' +
3350 ' console.log("test2");\n' +
3351 ' }\n' +
3352 ' };\n' +
3353 '}')
3354  
3355 # brace_style ,preserve-inline tests - (obo = " ", obot = "", oao = "\n", oaot = " ", obc = "\n", oac = " ", oact = "")
3356 self.reset_options();
3357 self.options.brace_style = 'collapse-preserve-inline'
3358 bt('import { asdf } from "asdf";')
3359 bt('import { get } from "asdf";')
3360 bt('function inLine() { console.log("oh em gee"); }')
3361 bt('if (cancer) { console.log("Im sorry but you only have so long to live..."); }')
3362 bt('if (ding) { console.log("dong"); } else { console.log("dang"); }')
3363 bt(
3364 'function kindaComplex() {\n' +
3365 ' var a = 2;\n' +
3366 ' var obj = {};\n' +
3367 ' var obj2 = { a: "a", b: "b" };\n' +
3368 ' var obj3 = {\n' +
3369 ' c: "c",\n' +
3370 ' d: "d",\n' +
3371 ' e: "e"\n' +
3372 ' };\n' +
3373 '}')
3374 bt(
3375 'function complex() {\n' +
3376 ' console.log("wowe");\n' +
3377 ' (function() { var a = 2; var b = 3; })();\n' +
3378 ' $.each(arr, function(el, idx) { return el; });\n' +
3379 ' var obj = {\n' +
3380 ' a: function() { console.log("test"); },\n' +
3381 ' b() {\n' +
3382 ' console.log("test2");\n' +
3383 ' }\n' +
3384 ' };\n' +
3385 '}',
3386 # -- output --
3387 'function complex() {\n' +
3388 ' console.log("wowe");\n' +
3389 ' (function() { var a = 2; var b = 3; })();\n' +
3390 ' $.each(arr, function(el, idx) { return el; });\n' +
3391 ' var obj = {\n' +
3392 ' a: function() { console.log("test"); },\n' +
3393 ' b() {\n' +
3394 ' console.log("test2");\n' +
3395 ' }\n' +
3396 ' };\n' +
3397 '}')
3398  
3399  
3400 #============================================================
3401 # Destructured and related
3402 self.reset_options();
3403 self.options.brace_style = 'collapse,preserve-inline'
3404  
3405 # Issue 382 - import destructured
3406 bt(
3407 'module "Even" {\n' +
3408 ' import { odd, oddly } from "Odd";\n' +
3409 '}')
3410 bt(
3411 'import defaultMember from "module-name";\n' +
3412 'import * as name from "module-name";\n' +
3413 'import { member } from "module-name";\n' +
3414 'import { member as alias } from "module-name";\n' +
3415 'import { member1, member2 } from "module-name";\n' +
3416 'import { member1, member2 as alias2 } from "module-name";\n' +
3417 'import defaultMember, { member, member2 } from "module-name";\n' +
3418 'import defaultMember, * as name from "module-name";\n' +
3419 'import "module-name";')
3420  
3421 # Issue 858 - from is a keyword only after import
3422 bt(
3423 'if (from < to) {\n' +
3424 ' from++;\n' +
3425 '} else {\n' +
3426 ' from--;\n' +
3427 '}')
3428  
3429 # Issue 511 - destrutured
3430 bt(
3431 'var { b, c } = require("../stores");\n' +
3432 'var { ProjectStore } = require("../stores");\n' +
3433 '\n' +
3434 'function takeThing({ prop }) {\n' +
3435 ' console.log("inner prop", prop)\n' +
3436 '}')
3437  
3438 # Issue 315 - Short objects
3439 bt('var a = { b: { c: { d: e } } };')
3440 bt(
3441 'var a = {\n' +
3442 ' b: {\n' +
3443 ' c: { d: e }\n' +
3444 ' c3: { d: e }\n' +
3445 ' },\n' +
3446 ' b2: { c: { d: e } }\n' +
3447 '};')
3448  
3449 # Issue 370 - Short objects in array
3450 bt(
3451 'var methods = [\n' +
3452 ' { name: "to" },\n' +
3453 ' { name: "step" },\n' +
3454 ' { name: "move" },\n' +
3455 ' { name: "min" },\n' +
3456 ' { name: "max" }\n' +
3457 '];')
3458  
3459 # Issue 838 - Short objects in array
3460 bt(
3461 'function(url, callback) {\n' +
3462 ' var script = document.createElement("script")\n' +
3463 ' if (true) script.onreadystatechange = function() {\n' +
3464 ' foo();\n' +
3465 ' }\n' +
3466 ' else script.onload = callback;\n' +
3467 '}')
3468  
3469 # Issue 578 - Odd indenting after function
3470 bt(
3471 'function bindAuthEvent(eventName) {\n' +
3472 ' self.auth.on(eventName, function(event, meta) {\n' +
3473 ' self.emit(eventName, event, meta);\n' +
3474 ' });\n' +
3475 '}\n' +
3476 '["logged_in", "logged_out", "signed_up", "updated_user"].forEach(bindAuthEvent);')
3477  
3478 # Issue #487 - some short expressions examples
3479 bt(
3480 'if (a == 1) { a++; }\n' +
3481 'a = { a: a };\n' +
3482 'UserDB.findOne({ username: "xyz" }, function(err, user) {});\n' +
3483 'import { fs } from "fs";')
3484  
3485 # Issue #982 - Fixed return expression collapse-preserve-inline
3486 bt(
3487 'function foo(arg) {\n' +
3488 ' if (!arg) { a(); }\n' +
3489 ' if (!arg) { return false; }\n' +
3490 ' if (!arg) { throw "inline"; }\n' +
3491 ' return true;\n' +
3492 '}')
3493  
3494 # Issue #338 - Short expressions
3495 bt(
3496 'if (someCondition) { return something; }\n' +
3497 'if (someCondition) {\n' +
3498 ' return something;\n' +
3499 '}\n' +
3500 'if (someCondition) { break; }\n' +
3501 'if (someCondition) {\n' +
3502 ' return something;\n' +
3503 '}')
3504  
3505  
3506 #============================================================
3507 # Old tests
3508 self.reset_options();
3509 bt('')
3510 test_fragment(' return .5')
3511 test_fragment(
3512 ' return .5;\n' +
3513 ' a();')
3514 test_fragment(
3515 ' return .5;\n' +
3516 ' a();')
3517 test_fragment(
3518 ' return .5;\n' +
3519 ' a();')
3520 test_fragment(' < div')
3521 bt('a = 1', 'a = 1')
3522 bt('a=1', 'a = 1')
3523 bt('(3) / 2')
3524 bt('["a", "b"].join("")')
3525 bt(
3526 'a();\n' +
3527 '\n' +
3528 'b();')
3529 bt(
3530 'var a = 1 var b = 2',
3531 # -- output --
3532 'var a = 1\n' +
3533 'var b = 2')
3534 bt(
3535 'var a=1, b=c[d], e=6;',
3536 # -- output --
3537 'var a = 1,\n' +
3538 ' b = c[d],\n' +
3539 ' e = 6;')
3540 bt(
3541 'var a,\n' +
3542 ' b,\n' +
3543 ' c;')
3544 bt(
3545 'let a = 1 let b = 2',
3546 # -- output --
3547 'let a = 1\n' +
3548 'let b = 2')
3549 bt(
3550 'let a=1, b=c[d], e=6;',
3551 # -- output --
3552 'let a = 1,\n' +
3553 ' b = c[d],\n' +
3554 ' e = 6;')
3555 bt(
3556 'let a,\n' +
3557 ' b,\n' +
3558 ' c;')
3559 bt(
3560 'const a = 1 const b = 2',
3561 # -- output --
3562 'const a = 1\n' +
3563 'const b = 2')
3564 bt(
3565 'const a=1, b=c[d], e=6;',
3566 # -- output --
3567 'const a = 1,\n' +
3568 ' b = c[d],\n' +
3569 ' e = 6;')
3570 bt(
3571 'const a,\n' +
3572 ' b,\n' +
3573 ' c;')
3574 bt('a = " 12345 "')
3575 bt('a = \' 12345 \'')
3576 bt('if (a == 1) b = 2;')
3577 bt(
3578 'if(1){2}else{3}',
3579 # -- output --
3580 'if (1) {\n' +
3581 ' 2\n' +
3582 '} else {\n' +
3583 ' 3\n' +
3584 '}')
3585 bt('if(1||2);', 'if (1 || 2);')
3586 bt('(a==1)||(b==2)', '(a == 1) || (b == 2)')
3587 bt(
3588 'var a = 1 if (2) 3;',
3589 # -- output --
3590 'var a = 1\n' +
3591 'if (2) 3;')
3592 bt('a = a + 1')
3593 bt('a = a == 1')
3594 bt('/12345[^678]*9+/.match(a)')
3595 bt('a /= 5')
3596 bt('a = 0.5 * 3')
3597 bt('a *= 10.55')
3598 bt('a < .5')
3599 bt('a <= .5')
3600 bt('a<.5', 'a < .5')
3601 bt('a<=.5', 'a <= .5')
3602  
3603 # exponent literals
3604 bt('a = 1e10')
3605 bt('a = 1.3e10')
3606 bt('a = 1.3e-10')
3607 bt('a = -12345.3e-10')
3608 bt('a = .12345e-10')
3609 bt('a = 06789e-10')
3610 bt('a = e - 10')
3611 bt('a = 1.3e+10')
3612 bt('a = 1.e-7')
3613 bt('a = -12345.3e+10')
3614 bt('a = .12345e+10')
3615 bt('a = 06789e+10')
3616 bt('a = e + 10')
3617 bt('a=0e-12345.3e-10', 'a = 0e-12345 .3e-10')
3618 bt('a=0.e-12345.3e-10', 'a = 0.e-12345 .3e-10')
3619 bt('a=0x.e-12345.3e-10', 'a = 0x.e - 12345.3e-10')
3620 bt('a=0x0.e-12345.3e-10', 'a = 0x0.e - 12345.3e-10')
3621 bt('a=0x0.0e-12345.3e-10', 'a = 0x0 .0e-12345 .3e-10')
3622 bt('a=0g-12345.3e-10', 'a = 0 g - 12345.3e-10')
3623 bt('a=0.g-12345.3e-10', 'a = 0. g - 12345.3e-10')
3624 bt('a=0x.g-12345.3e-10', 'a = 0x.g - 12345.3e-10')
3625 bt('a=0x0.g-12345.3e-10', 'a = 0x0.g - 12345.3e-10')
3626 bt('a=0x0.0g-12345.3e-10', 'a = 0x0 .0 g - 12345.3e-10')
3627  
3628 # Decimal literals
3629 bt('a = 0123456789;')
3630 bt('a = 9876543210;')
3631 bt('a = 5647308291;')
3632 bt('a=030e-5', 'a = 030e-5')
3633 bt('a=00+4', 'a = 00 + 4')
3634 bt('a=32+4', 'a = 32 + 4')
3635 bt('a=0.6g+4', 'a = 0.6 g + 4')
3636 bt('a=01.10', 'a = 01.10')
3637 bt('a=a.10', 'a = a .10')
3638 bt('a=00B0x0', 'a = 00 B0x0')
3639 bt('a=00B0xb0', 'a = 00 B0xb0')
3640 bt('a=00B0x0b0', 'a = 00 B0x0b0')
3641 bt('a=0090x0', 'a = 0090 x0')
3642 bt('a=0g0b0o0', 'a = 0 g0b0o0')
3643  
3644 # Hexadecimal literals
3645 bt('a = 0x0123456789abcdef;')
3646 bt('a = 0X0123456789ABCDEF;')
3647 bt('a = 0xFeDcBa9876543210;')
3648 bt('a=0x30e-5', 'a = 0x30e - 5')
3649 bt('a=0xF0+4', 'a = 0xF0 + 4')
3650 bt('a=0Xff+4', 'a = 0Xff + 4')
3651 bt('a=0Xffg+4', 'a = 0Xff g + 4')
3652 bt('a=0x01.10', 'a = 0x01 .10')
3653 bt('a = 0xb0ce;')
3654 bt('a = 0x0b0;')
3655 bt('a=0x0B0x0', 'a = 0x0B0 x0')
3656 bt('a=0x0B0xb0', 'a = 0x0B0 xb0')
3657 bt('a=0x0B0x0b0', 'a = 0x0B0 x0b0')
3658 bt('a=0X090x0', 'a = 0X090 x0')
3659 bt('a=0Xg0b0o0', 'a = 0X g0b0o0')
3660  
3661 # Octal literals
3662 bt('a = 0o01234567;')
3663 bt('a = 0O01234567;')
3664 bt('a = 0o34120675;')
3665 bt('a=0o30e-5', 'a = 0o30 e - 5')
3666 bt('a=0o70+4', 'a = 0o70 + 4')
3667 bt('a=0O77+4', 'a = 0O77 + 4')
3668 bt('a=0O778+4', 'a = 0O77 8 + 4')
3669 bt('a=0O77a+4', 'a = 0O77 a + 4')
3670 bt('a=0o01.10', 'a = 0o01 .10')
3671 bt('a=0o0B0x0', 'a = 0o0 B0x0')
3672 bt('a=0o0B0xb0', 'a = 0o0 B0xb0')
3673 bt('a=0o0B0x0b0', 'a = 0o0 B0x0b0')
3674 bt('a=0O090x0', 'a = 0O0 90 x0')
3675 bt('a=0Og0b0o0', 'a = 0O g0b0o0')
3676  
3677 # Binary literals
3678 bt('a = 0b010011;')
3679 bt('a = 0B010011;')
3680 bt('a = 0b01001100001111;')
3681 bt('a=0b10e-5', 'a = 0b10 e - 5')
3682 bt('a=0b10+4', 'a = 0b10 + 4')
3683 bt('a=0B11+4', 'a = 0B11 + 4')
3684 bt('a=0B112+4', 'a = 0B11 2 + 4')
3685 bt('a=0B11a+4', 'a = 0B11 a + 4')
3686 bt('a=0b01.10', 'a = 0b01 .10')
3687 bt('a=0b0B0x0', 'a = 0b0 B0x0')
3688 bt('a=0b0B0xb0', 'a = 0b0 B0xb0')
3689 bt('a=0b0B0x0b0', 'a = 0b0 B0x0b0')
3690 bt('a=0B090x0', 'a = 0B0 90 x0')
3691 bt('a=0Bg0b0o0', 'a = 0B g0b0o0')
3692 bt('a = [1, 2, 3, 4]')
3693 bt('F*(g/=f)*g+b', 'F * (g /= f) * g + b')
3694 bt(
3695 'a.b({c:d})',
3696 # -- output --
3697 'a.b({\n' +
3698 ' c: d\n' +
3699 '})')
3700 bt(
3701 'a.b\n' +
3702 '(\n' +
3703 '{\n' +
3704 'c:\n' +
3705 'd\n' +
3706 '}\n' +
3707 ')',
3708 # -- output --
3709 'a.b({\n' +
3710 ' c: d\n' +
3711 '})')
3712 bt(
3713 'a.b({c:"d"})',
3714 # -- output --
3715 'a.b({\n' +
3716 ' c: "d"\n' +
3717 '})')
3718 bt(
3719 'a.b\n' +
3720 '(\n' +
3721 '{\n' +
3722 'c:\n' +
3723 '"d"\n' +
3724 '}\n' +
3725 ')',
3726 # -- output --
3727 'a.b({\n' +
3728 ' c: "d"\n' +
3729 '})')
3730 bt('a=!b', 'a = !b')
3731 bt('a=!!b', 'a = !!b')
3732 bt('a?b:c', 'a ? b : c')
3733 bt('a?1:2', 'a ? 1 : 2')
3734 bt('a?(b):c', 'a ? (b) : c')
3735 bt(
3736 'x={a:1,b:w=="foo"?x:y,c:z}',
3737 # -- output --
3738 'x = {\n' +
3739 ' a: 1,\n' +
3740 ' b: w == "foo" ? x : y,\n' +
3741 ' c: z\n' +
3742 '}')
3743 bt('x=a?b?c?d:e:f:g;', 'x = a ? b ? c ? d : e : f : g;')
3744 bt(
3745 'x=a?b?c?d:{e1:1,e2:2}:f:g;',
3746 # -- output --
3747 'x = a ? b ? c ? d : {\n' +
3748 ' e1: 1,\n' +
3749 ' e2: 2\n' +
3750 '} : f : g;')
3751 bt('function void(void) {}')
3752 bt('if(!a)foo();', 'if (!a) foo();')
3753 bt('a=~a', 'a = ~a')
3754 bt(
3755 'a;/*comment*/b;',
3756 # -- output --
3757 'a; /*comment*/\n' +
3758 'b;')
3759 bt(
3760 'a;/* comment */b;',
3761 # -- output --
3762 'a; /* comment */\n' +
3763 'b;')
3764  
3765 # simple comments don't get touched at all
3766 test_fragment(
3767 'a;/*\n' +
3768 'comment\n' +
3769 '*/b;',
3770 # -- output --
3771 'a;\n' +
3772 '/*\n' +
3773 'comment\n' +
3774 '*/\n' +
3775 'b;')
3776 bt(
3777 'a;/**\n' +
3778 '* javadoc\n' +
3779 '*/b;',
3780 # -- output --
3781 'a;\n' +
3782 '/**\n' +
3783 ' * javadoc\n' +
3784 ' */\n' +
3785 'b;')
3786 test_fragment(
3787 'a;/**\n' +
3788 '\n' +
3789 'no javadoc\n' +
3790 '*/b;',
3791 # -- output --
3792 'a;\n' +
3793 '/**\n' +
3794 '\n' +
3795 'no javadoc\n' +
3796 '*/\n' +
3797 'b;')
3798  
3799 # comment blocks detected and reindented even w/o javadoc starter
3800 bt(
3801 'a;/*\n' +
3802 '* javadoc\n' +
3803 '*/b;',
3804 # -- output --
3805 'a;\n' +
3806 '/*\n' +
3807 ' * javadoc\n' +
3808 ' */\n' +
3809 'b;')
3810 bt('if(a)break;', 'if (a) break;')
3811 bt(
3812 'if(a){break}',
3813 # -- output --
3814 'if (a) {\n' +
3815 ' break\n' +
3816 '}')
3817 bt('if((a))foo();', 'if ((a)) foo();')
3818 bt('for(var i=0;;) a', 'for (var i = 0;;) a')
3819 bt(
3820 'for(var i=0;;)\n' +
3821 'a',
3822 # -- output --
3823 'for (var i = 0;;)\n' +
3824 ' a')
3825 bt('a++;')
3826 bt('for(;;i++)a()', 'for (;; i++) a()')
3827 bt(
3828 'for(;;i++)\n' +
3829 'a()',
3830 # -- output --
3831 'for (;; i++)\n' +
3832 ' a()')
3833 bt('for(;;++i)a', 'for (;; ++i) a')
3834 bt('return(1)', 'return (1)')
3835 bt(
3836 'try{a();}catch(b){c();}finally{d();}',
3837 # -- output --
3838 'try {\n' +
3839 ' a();\n' +
3840 '} catch (b) {\n' +
3841 ' c();\n' +
3842 '} finally {\n' +
3843 ' d();\n' +
3844 '}')
3845  
3846 # magic function call
3847 bt('(xx)()')
3848  
3849 # another magic function call
3850 bt('a[1]()')
3851 bt(
3852 'if(a){b();}else if(c) foo();',
3853 # -- output --
3854 'if (a) {\n' +
3855 ' b();\n' +
3856 '} else if (c) foo();')
3857 bt(
3858 'switch(x) {case 0: case 1: a(); break; default: break}',
3859 # -- output --
3860 'switch (x) {\n' +
3861 ' case 0:\n' +
3862 ' case 1:\n' +
3863 ' a();\n' +
3864 ' break;\n' +
3865 ' default:\n' +
3866 ' break\n' +
3867 '}')
3868 bt(
3869 'switch(x){case -1:break;case !y:break;}',
3870 # -- output --
3871 'switch (x) {\n' +
3872 ' case -1:\n' +
3873 ' break;\n' +
3874 ' case !y:\n' +
3875 ' break;\n' +
3876 '}')
3877 bt('a !== b')
3878 bt(
3879 'if (a) b(); else c();',
3880 # -- output --
3881 'if (a) b();\n' +
3882 'else c();')
3883  
3884 # typical greasemonkey start
3885 bt(
3886 '// comment\n' +
3887 '(function something() {})')
3888  
3889 # duplicating newlines
3890 bt(
3891 '{\n' +
3892 '\n' +
3893 ' x();\n' +
3894 '\n' +
3895 '}')
3896 bt('if (a in b) foo();')
3897 bt('if (a of b) foo();')
3898 bt('if (a of [1, 2, 3]) foo();')
3899 bt(
3900 'if(X)if(Y)a();else b();else c();',
3901 # -- output --
3902 'if (X)\n' +
3903 ' if (Y) a();\n' +
3904 ' else b();\n' +
3905 'else c();')
3906 bt(
3907 'if (foo) bar();\n' +
3908 'else break')
3909 bt('var a, b;')
3910 bt('var a = new function();')
3911 test_fragment('new function')
3912 bt('var a, b')
3913 bt(
3914 '{a:1, b:2}',
3915 # -- output --
3916 '{\n' +
3917 ' a: 1,\n' +
3918 ' b: 2\n' +
3919 '}')
3920 bt(
3921 'a={1:[-1],2:[+1]}',
3922 # -- output --
3923 'a = {\n' +
3924 ' 1: [-1],\n' +
3925 ' 2: [+1]\n' +
3926 '}')
3927 bt(
3928 'var l = {\'a\':\'1\', \'b\':\'2\'}',
3929 # -- output --
3930 'var l = {\n' +
3931 ' \'a\': \'1\',\n' +
3932 ' \'b\': \'2\'\n' +
3933 '}')
3934 bt('if (template.user[n] in bk) foo();')
3935 bt('return 45')
3936 bt(
3937 'return this.prevObject ||\n' +
3938 '\n' +
3939 ' this.constructor(null);')
3940 bt('If[1]')
3941 bt('Then[1]')
3942 bt('a = 1;// comment', 'a = 1; // comment')
3943 bt('a = 1; // comment')
3944 bt(
3945 'a = 1;\n' +
3946 ' // comment',
3947 # -- output --
3948 'a = 1;\n' +
3949 '// comment')
3950 bt('a = [-1, -1, -1]')
3951 bt(
3952 '// a\n' +
3953 '// b\n' +
3954 '\n' +
3955 '\n' +
3956 '\n' +
3957 '// c\n' +
3958 '// d')
3959 bt(
3960 '// func-comment\n' +
3961 '\n' +
3962 'function foo() {}\n' +
3963 '\n' +
3964 '// end-func-comment')
3965  
3966 # The exact formatting these should have is open for discussion, but they are at least reasonable
3967 bt(
3968 'a = [ // comment\n' +
3969 ' -1, -1, -1\n' +
3970 ']')
3971 bt(
3972 'var a = [ // comment\n' +
3973 ' -1, -1, -1\n' +
3974 ']')
3975 bt(
3976 'a = [ // comment\n' +
3977 ' -1, // comment\n' +
3978 ' -1, -1\n' +
3979 ']')
3980 bt(
3981 'var a = [ // comment\n' +
3982 ' -1, // comment\n' +
3983 ' -1, -1\n' +
3984 ']')
3985 bt(
3986 'o = [{a:b},{c:d}]',
3987 # -- output --
3988 'o = [{\n' +
3989 ' a: b\n' +
3990 '}, {\n' +
3991 ' c: d\n' +
3992 '}]')
3993  
3994 # was: extra space appended
3995 bt(
3996 'if (a) {\n' +
3997 ' do();\n' +
3998 '}')
3999  
4000 # if/else statement with empty body
4001 bt(
4002 'if (a) {\n' +
4003 '// comment\n' +
4004 '}else{\n' +
4005 '// comment\n' +
4006 '}',
4007 # -- output --
4008 'if (a) {\n' +
4009 ' // comment\n' +
4010 '} else {\n' +
4011 ' // comment\n' +
4012 '}')
4013  
4014 # multiple comments indentation
4015 bt(
4016 'if (a) {\n' +
4017 '// comment\n' +
4018 '// comment\n' +
4019 '}',
4020 # -- output --
4021 'if (a) {\n' +
4022 ' // comment\n' +
4023 ' // comment\n' +
4024 '}')
4025 bt(
4026 'if (a) b() else c();',
4027 # -- output --
4028 'if (a) b()\n' +
4029 'else c();')
4030 bt(
4031 'if (a) b() else if c() d();',
4032 # -- output --
4033 'if (a) b()\n' +
4034 'else if c() d();')
4035 bt('{}')
4036 bt(
4037 '{\n' +
4038 '\n' +
4039 '}')
4040 bt(
4041 'do { a(); } while ( 1 );',
4042 # -- output --
4043 'do {\n' +
4044 ' a();\n' +
4045 '} while (1);')
4046 bt('do {} while (1);')
4047 bt(
4048 'do {\n' +
4049 '} while (1);',
4050 # -- output --
4051 'do {} while (1);')
4052 bt(
4053 'do {\n' +
4054 '\n' +
4055 '} while (1);')
4056 bt('var a = x(a, b, c)')
4057 bt(
4058 'delete x if (a) b();',
4059 # -- output --
4060 'delete x\n' +
4061 'if (a) b();')
4062 bt(
4063 'delete x[x] if (a) b();',
4064 # -- output --
4065 'delete x[x]\n' +
4066 'if (a) b();')
4067 bt('for(var a=1,b=2)d', 'for (var a = 1, b = 2) d')
4068 bt('for(var a=1,b=2,c=3) d', 'for (var a = 1, b = 2, c = 3) d')
4069 bt(
4070 'for(var a=1,b=2,c=3;d<3;d++)\n' +
4071 'e',
4072 # -- output --
4073 'for (var a = 1, b = 2, c = 3; d < 3; d++)\n' +
4074 ' e')
4075 bt(
4076 'function x(){(a||b).c()}',
4077 # -- output --
4078 'function x() {\n' +
4079 ' (a || b).c()\n' +
4080 '}')
4081 bt(
4082 'function x(){return - 1}',
4083 # -- output --
4084 'function x() {\n' +
4085 ' return -1\n' +
4086 '}')
4087 bt(
4088 'function x(){return ! a}',
4089 # -- output --
4090 'function x() {\n' +
4091 ' return !a\n' +
4092 '}')
4093 bt('x => x')
4094 bt('(x) => x')
4095 bt(
4096 'x => { x }',
4097 # -- output --
4098 'x => {\n' +
4099 ' x\n' +
4100 '}')
4101 bt(
4102 '(x) => { x }',
4103 # -- output --
4104 '(x) => {\n' +
4105 ' x\n' +
4106 '}')
4107  
4108 # a common snippet in jQuery plugins
4109 bt(
4110 'settings = $.extend({},defaults,settings);',
4111 # -- output --
4112 'settings = $.extend({}, defaults, settings);')
4113 bt('$http().then().finally().default()')
4114 bt(
4115 '$http()\n' +
4116 '.then()\n' +
4117 '.finally()\n' +
4118 '.default()',
4119 # -- output --
4120 '$http()\n' +
4121 ' .then()\n' +
4122 ' .finally()\n' +
4123 ' .default()')
4124 bt('$http().when.in.new.catch().throw()')
4125 bt(
4126 '$http()\n' +
4127 '.when\n' +
4128 '.in\n' +
4129 '.new\n' +
4130 '.catch()\n' +
4131 '.throw()',
4132 # -- output --
4133 '$http()\n' +
4134 ' .when\n' +
4135 ' .in\n' +
4136 ' .new\n' +
4137 ' .catch()\n' +
4138 ' .throw()')
4139 bt(
4140 '{xxx;}()',
4141 # -- output --
4142 '{\n' +
4143 ' xxx;\n' +
4144 '}()')
4145 bt(
4146 'a = \'a\'\n' +
4147 'b = \'b\'')
4148 bt('a = /reg/exp')
4149 bt('a = /reg/')
4150 bt('/abc/.test()')
4151 bt('/abc/i.test()')
4152 bt(
4153 '{/abc/i.test()}',
4154 # -- output --
4155 '{\n' +
4156 ' /abc/i.test()\n' +
4157 '}')
4158 bt('var x=(a)/a;', 'var x = (a) / a;')
4159 bt('x != -1')
4160 bt('for (; s-->0;)t', 'for (; s-- > 0;) t')
4161 bt('for (; s++>0;)u', 'for (; s++ > 0;) u')
4162 bt('a = s++>s--;', 'a = s++ > s--;')
4163 bt('a = s++>--s;', 'a = s++ > --s;')
4164 bt(
4165 '{x=#1=[]}',
4166 # -- output --
4167 '{\n' +
4168 ' x = #1=[]\n' +
4169 '}')
4170 bt(
4171 '{a:#1={}}',
4172 # -- output --
4173 '{\n' +
4174 ' a: #1={}\n' +
4175 '}')
4176 bt(
4177 '{a:#1#}',
4178 # -- output --
4179 '{\n' +
4180 ' a: #1#\n' +
4181 '}')
4182 test_fragment('"incomplete-string')
4183 test_fragment('\'incomplete-string')
4184 test_fragment('/incomplete-regex')
4185 test_fragment('`incomplete-template-string')
4186 test_fragment(
4187 '{a:1},{a:2}',
4188 # -- output --
4189 '{\n' +
4190 ' a: 1\n' +
4191 '}, {\n' +
4192 ' a: 2\n' +
4193 '}')
4194 test_fragment(
4195 'var ary=[{a:1}, {a:2}];',
4196 # -- output --
4197 'var ary = [{\n' +
4198 ' a: 1\n' +
4199 '}, {\n' +
4200 ' a: 2\n' +
4201 '}];')
4202  
4203 # incomplete
4204 test_fragment(
4205 '{a:#1',
4206 # -- output --
4207 '{\n' +
4208 ' a: #1')
4209  
4210 # incomplete
4211 test_fragment(
4212 '{a:#',
4213 # -- output --
4214 '{\n' +
4215 ' a: #')
4216  
4217 # incomplete
4218 test_fragment(
4219 '}}}',
4220 # -- output --
4221 '}\n' +
4222 '}\n' +
4223 '}')
4224 test_fragment(
4225 '<!--\n' +
4226 'void();\n' +
4227 '// -->')
4228  
4229 # incomplete regexp
4230 test_fragment('a=/regexp', 'a = /regexp')
4231 bt(
4232 '{a:#1=[],b:#1#,c:#999999#}',
4233 # -- output --
4234 '{\n' +
4235 ' a: #1=[],\n' +
4236 ' b: #1#,\n' +
4237 ' c: #999999#\n' +
4238 '}')
4239 bt(
4240 'do{x()}while(a>1)',
4241 # -- output --
4242 'do {\n' +
4243 ' x()\n' +
4244 '} while (a > 1)')
4245 bt(
4246 'x(); /reg/exp.match(something)',
4247 # -- output --
4248 'x();\n' +
4249 '/reg/exp.match(something)')
4250 test_fragment(
4251 'something();(',
4252 # -- output --
4253 'something();\n' +
4254 '(')
4255 test_fragment(
4256 '#!she/bangs, she bangs\n' +
4257 'f=1',
4258 # -- output --
4259 '#!she/bangs, she bangs\n' +
4260 '\n' +
4261 'f = 1')
4262 test_fragment(
4263 '#!she/bangs, she bangs\n' +
4264 '\n' +
4265 'f=1',
4266 # -- output --
4267 '#!she/bangs, she bangs\n' +
4268 '\n' +
4269 'f = 1')
4270 test_fragment(
4271 '#!she/bangs, she bangs\n' +
4272 '\n' +
4273 '/* comment */')
4274 test_fragment(
4275 '#!she/bangs, she bangs\n' +
4276 '\n' +
4277 '\n' +
4278 '/* comment */')
4279 test_fragment('#')
4280 test_fragment('#!')
4281 bt('function namespace::something()')
4282 test_fragment(
4283 '<!--\n' +
4284 'something();\n' +
4285 '-->')
4286 test_fragment(
4287 '<!--\n' +
4288 'if(i<0){bla();}\n' +
4289 '-->',
4290 # -- output --
4291 '<!--\n' +
4292 'if (i < 0) {\n' +
4293 ' bla();\n' +
4294 '}\n' +
4295 '-->')
4296 bt(
4297 '{foo();--bar;}',
4298 # -- output --
4299 '{\n' +
4300 ' foo();\n' +
4301 ' --bar;\n' +
4302 '}')
4303 bt(
4304 '{foo();++bar;}',
4305 # -- output --
4306 '{\n' +
4307 ' foo();\n' +
4308 ' ++bar;\n' +
4309 '}')
4310 bt(
4311 '{--bar;}',
4312 # -- output --
4313 '{\n' +
4314 ' --bar;\n' +
4315 '}')
4316 bt(
4317 '{++bar;}',
4318 # -- output --
4319 '{\n' +
4320 ' ++bar;\n' +
4321 '}')
4322 bt('if(true)++a;', 'if (true) ++a;')
4323 bt(
4324 'if(true)\n' +
4325 '++a;',
4326 # -- output --
4327 'if (true)\n' +
4328 ' ++a;')
4329 bt('if(true)--a;', 'if (true) --a;')
4330 bt(
4331 'if(true)\n' +
4332 '--a;',
4333 # -- output --
4334 'if (true)\n' +
4335 ' --a;')
4336 bt('elem[array]++;')
4337 bt('elem++ * elem[array]++;')
4338 bt('elem-- * -elem[array]++;')
4339 bt('elem-- + elem[array]++;')
4340 bt('elem-- - elem[array]++;')
4341 bt('elem-- - -elem[array]++;')
4342 bt('elem-- - +elem[array]++;')
4343  
4344 # Handling of newlines around unary ++ and -- operators
4345 bt(
4346 '{foo\n' +
4347 '++bar;}',
4348 # -- output --
4349 '{\n' +
4350 ' foo\n' +
4351 ' ++bar;\n' +
4352 '}')
4353 bt(
4354 '{foo++\n' +
4355 'bar;}',
4356 # -- output --
4357 '{\n' +
4358 ' foo++\n' +
4359 ' bar;\n' +
4360 '}')
4361  
4362 # This is invalid, but harder to guard against. Issue #203.
4363 bt(
4364 '{foo\n' +
4365 '++\n' +
4366 'bar;}',
4367 # -- output --
4368 '{\n' +
4369 ' foo\n' +
4370 ' ++\n' +
4371 ' bar;\n' +
4372 '}')
4373  
4374 # regexps
4375 bt(
4376 'a(/abc\\/\\/def/);b()',
4377 # -- output --
4378 'a(/abc\\/\\/def/);\n' +
4379 'b()')
4380 bt(
4381 'a(/a[b\\[\\]c]d/);b()',
4382 # -- output --
4383 'a(/a[b\\[\\]c]d/);\n' +
4384 'b()')
4385  
4386 # incomplete char class
4387 test_fragment('a(/a[b\\[')
4388  
4389 # allow unescaped / in char classes
4390 bt(
4391 'a(/[a/b]/);b()',
4392 # -- output --
4393 'a(/[a/b]/);\n' +
4394 'b()')
4395 bt('typeof /foo\\//;')
4396 bt('throw /foo\\//;')
4397 bt('do /foo\\//;')
4398 bt('return /foo\\//;')
4399 bt(
4400 'switch (a) {\n' +
4401 ' case /foo\\//:\n' +
4402 ' b\n' +
4403 '}')
4404 bt(
4405 'if (a) /foo\\//\n' +
4406 'else /foo\\//;')
4407 bt('if (foo) /regex/.test();')
4408 bt('for (index in [1, 2, 3]) /^test$/i.test(s)')
4409 bt(
4410 'function foo() {\n' +
4411 ' return [\n' +
4412 ' "one",\n' +
4413 ' "two"\n' +
4414 ' ];\n' +
4415 '}')
4416 bt(
4417 'a=[[1,2],[4,5],[7,8]]',
4418 # -- output --
4419 'a = [\n' +
4420 ' [1, 2],\n' +
4421 ' [4, 5],\n' +
4422 ' [7, 8]\n' +
4423 ']')
4424 bt(
4425 'a=[[1,2],[4,5],function(){},[7,8]]',
4426 # -- output --
4427 'a = [\n' +
4428 ' [1, 2],\n' +
4429 ' [4, 5],\n' +
4430 ' function() {},\n' +
4431 ' [7, 8]\n' +
4432 ']')
4433 bt(
4434 'a=[[1,2],[4,5],function(){},function(){},[7,8]]',
4435 # -- output --
4436 'a = [\n' +
4437 ' [1, 2],\n' +
4438 ' [4, 5],\n' +
4439 ' function() {},\n' +
4440 ' function() {},\n' +
4441 ' [7, 8]\n' +
4442 ']')
4443 bt(
4444 'a=[[1,2],[4,5],function(){},[7,8]]',
4445 # -- output --
4446 'a = [\n' +
4447 ' [1, 2],\n' +
4448 ' [4, 5],\n' +
4449 ' function() {},\n' +
4450 ' [7, 8]\n' +
4451 ']')
4452 bt('a=[b,c,function(){},function(){},d]', 'a = [b, c, function() {}, function() {}, d]')
4453 bt(
4454 'a=[b,c,\n' +
4455 'function(){},function(){},d]',
4456 # -- output --
4457 'a = [b, c,\n' +
4458 ' function() {},\n' +
4459 ' function() {},\n' +
4460 ' d\n' +
4461 ']')
4462 bt('a=[a[1],b[4],c[d[7]]]', 'a = [a[1], b[4], c[d[7]]]')
4463 bt('[1,2,[3,4,[5,6],7],8]', '[1, 2, [3, 4, [5, 6], 7], 8]')
4464 bt(
4465 '[[["1","2"],["3","4"]],[["5","6","7"],["8","9","0"]],[["1","2","3"],["4","5","6","7"],["8","9","0"]]]',
4466 # -- output --
4467 '[\n' +
4468 ' [\n' +
4469 ' ["1", "2"],\n' +
4470 ' ["3", "4"]\n' +
4471 ' ],\n' +
4472 ' [\n' +
4473 ' ["5", "6", "7"],\n' +
4474 ' ["8", "9", "0"]\n' +
4475 ' ],\n' +
4476 ' [\n' +
4477 ' ["1", "2", "3"],\n' +
4478 ' ["4", "5", "6", "7"],\n' +
4479 ' ["8", "9", "0"]\n' +
4480 ' ]\n' +
4481 ']')
4482 bt(
4483 '{[x()[0]];indent;}',
4484 # -- output --
4485 '{\n' +
4486 ' [x()[0]];\n' +
4487 ' indent;\n' +
4488 '}')
4489 bt(
4490 '/*\n' +
4491 ' foo trailing space \n' +
4492 ' * bar trailing space \n' +
4493 '**/')
4494 bt(
4495 '{\n' +
4496 ' /*\n' +
4497 ' foo \n' +
4498 ' * bar \n' +
4499 ' */\n' +
4500 '}')
4501 bt('return ++i')
4502 bt('return !!x')
4503 bt('return !x')
4504 bt('return [1,2]', 'return [1, 2]')
4505 bt('return;')
4506 bt(
4507 'return\n' +
4508 'func')
4509 bt('catch(e)', 'catch (e)')
4510 bt(
4511 'var a=1,b={foo:2,bar:3},{baz:4,wham:5},c=4;',
4512 # -- output --
4513 'var a = 1,\n' +
4514 ' b = {\n' +
4515 ' foo: 2,\n' +
4516 ' bar: 3\n' +
4517 ' },\n' +
4518 ' {\n' +
4519 ' baz: 4,\n' +
4520 ' wham: 5\n' +
4521 ' }, c = 4;')
4522 bt(
4523 'var a=1,b={foo:2,bar:3},{baz:4,wham:5},\n' +
4524 'c=4;',
4525 # -- output --
4526 'var a = 1,\n' +
4527 ' b = {\n' +
4528 ' foo: 2,\n' +
4529 ' bar: 3\n' +
4530 ' },\n' +
4531 ' {\n' +
4532 ' baz: 4,\n' +
4533 ' wham: 5\n' +
4534 ' },\n' +
4535 ' c = 4;')
4536  
4537 # inline comment
4538 bt(
4539 'function x(/*int*/ start, /*string*/ foo)',
4540 # -- output --
4541 'function x( /*int*/ start, /*string*/ foo)')
4542  
4543 # javadoc comment
4544 bt(
4545 '/**\n' +
4546 '* foo\n' +
4547 '*/',
4548 # -- output --
4549 '/**\n' +
4550 ' * foo\n' +
4551 ' */')
4552 bt(
4553 '{\n' +
4554 '/**\n' +
4555 '* foo\n' +
4556 '*/\n' +
4557 '}',
4558 # -- output --
4559 '{\n' +
4560 ' /**\n' +
4561 ' * foo\n' +
4562 ' */\n' +
4563 '}')
4564  
4565 # starless block comment
4566 bt(
4567 '/**\n' +
4568 'foo\n' +
4569 '*/')
4570 bt(
4571 '/**\n' +
4572 'foo\n' +
4573 '**/')
4574 bt(
4575 '/**\n' +
4576 'foo\n' +
4577 'bar\n' +
4578 '**/')
4579 bt(
4580 '/**\n' +
4581 'foo\n' +
4582 '\n' +
4583 'bar\n' +
4584 '**/')
4585 bt(
4586 '/**\n' +
4587 'foo\n' +
4588 ' bar\n' +
4589 '**/')
4590 bt(
4591 '{\n' +
4592 '/**\n' +
4593 'foo\n' +
4594 '*/\n' +
4595 '}',
4596 # -- output --
4597 '{\n' +
4598 ' /**\n' +
4599 ' foo\n' +
4600 ' */\n' +
4601 '}')
4602 bt(
4603 '{\n' +
4604 '/**\n' +
4605 'foo\n' +
4606 '**/\n' +
4607 '}',
4608 # -- output --
4609 '{\n' +
4610 ' /**\n' +
4611 ' foo\n' +
4612 ' **/\n' +
4613 '}')
4614 bt(
4615 '{\n' +
4616 '/**\n' +
4617 'foo\n' +
4618 'bar\n' +
4619 '**/\n' +
4620 '}',
4621 # -- output --
4622 '{\n' +
4623 ' /**\n' +
4624 ' foo\n' +
4625 ' bar\n' +
4626 ' **/\n' +
4627 '}')
4628 bt(
4629 '{\n' +
4630 '/**\n' +
4631 'foo\n' +
4632 '\n' +
4633 'bar\n' +
4634 '**/\n' +
4635 '}',
4636 # -- output --
4637 '{\n' +
4638 ' /**\n' +
4639 ' foo\n' +
4640 '\n' +
4641 ' bar\n' +
4642 ' **/\n' +
4643 '}')
4644 bt(
4645 '{\n' +
4646 '/**\n' +
4647 'foo\n' +
4648 ' bar\n' +
4649 '**/\n' +
4650 '}',
4651 # -- output --
4652 '{\n' +
4653 ' /**\n' +
4654 ' foo\n' +
4655 ' bar\n' +
4656 ' **/\n' +
4657 '}')
4658 bt(
4659 '{\n' +
4660 ' /**\n' +
4661 ' foo\n' +
4662 'bar\n' +
4663 ' **/\n' +
4664 '}')
4665 bt(
4666 'var a,b,c=1,d,e,f=2;',
4667 # -- output --
4668 'var a, b, c = 1,\n' +
4669 ' d, e, f = 2;')
4670 bt(
4671 'var a,b,c=[],d,e,f=2;',
4672 # -- output --
4673 'var a, b, c = [],\n' +
4674 ' d, e, f = 2;')
4675 bt(
4676 'function() {\n' +
4677 ' var a, b, c, d, e = [],\n' +
4678 ' f;\n' +
4679 '}')
4680 bt(
4681 'do/regexp/;\n' +
4682 'while(1);',
4683 # -- output --
4684 'do /regexp/;\n' +
4685 'while (1);')
4686 bt(
4687 'var a = a,\n' +
4688 'a;\n' +
4689 'b = {\n' +
4690 'b\n' +
4691 '}',
4692 # -- output --
4693 'var a = a,\n' +
4694 ' a;\n' +
4695 'b = {\n' +
4696 ' b\n' +
4697 '}')
4698 bt(
4699 'var a = a,\n' +
4700 ' /* c */\n' +
4701 ' b;')
4702 bt(
4703 'var a = a,\n' +
4704 ' // c\n' +
4705 ' b;')
4706  
4707 # weird element referencing
4708 bt('foo.("bar");')
4709 bt(
4710 'if (a) a()\n' +
4711 'else b()\n' +
4712 'newline()')
4713 bt(
4714 'if (a) a()\n' +
4715 'newline()')
4716 bt('a=typeof(x)', 'a = typeof(x)')
4717 bt(
4718 'var a = function() {\n' +
4719 ' return null;\n' +
4720 ' },\n' +
4721 ' b = false;')
4722 bt(
4723 'var a = function() {\n' +
4724 ' func1()\n' +
4725 '}')
4726 bt(
4727 'var a = function() {\n' +
4728 ' func1()\n' +
4729 '}\n' +
4730 'var b = function() {\n' +
4731 ' func2()\n' +
4732 '}')
4733  
4734 # code with and without semicolons
4735 bt(
4736 'var whatever = require("whatever");\n' +
4737 'function() {\n' +
4738 ' a = 6;\n' +
4739 '}',
4740 # -- output --
4741 'var whatever = require("whatever");\n' +
4742 '\n' +
4743 'function() {\n' +
4744 ' a = 6;\n' +
4745 '}')
4746 bt(
4747 'var whatever = require("whatever")\n' +
4748 'function() {\n' +
4749 ' a = 6\n' +
4750 '}',
4751 # -- output --
4752 'var whatever = require("whatever")\n' +
4753 '\n' +
4754 'function() {\n' +
4755 ' a = 6\n' +
4756 '}')
4757 bt(
4758 '{"x":[{"a":1,"b":3},\n' +
4759 '7,8,8,8,8,{"b":99},{"a":11}]}',
4760 # -- output --
4761 '{\n' +
4762 ' "x": [{\n' +
4763 ' "a": 1,\n' +
4764 ' "b": 3\n' +
4765 ' },\n' +
4766 ' 7, 8, 8, 8, 8, {\n' +
4767 ' "b": 99\n' +
4768 ' }, {\n' +
4769 ' "a": 11\n' +
4770 ' }\n' +
4771 ' ]\n' +
4772 '}')
4773 bt(
4774 '{"x":[{"a":1,"b":3},7,8,8,8,8,{"b":99},{"a":11}]}',
4775 # -- output --
4776 '{\n' +
4777 ' "x": [{\n' +
4778 ' "a": 1,\n' +
4779 ' "b": 3\n' +
4780 ' }, 7, 8, 8, 8, 8, {\n' +
4781 ' "b": 99\n' +
4782 ' }, {\n' +
4783 ' "a": 11\n' +
4784 ' }]\n' +
4785 '}')
4786 bt(
4787 '{"1":{"1a":"1b"},"2"}',
4788 # -- output --
4789 '{\n' +
4790 ' "1": {\n' +
4791 ' "1a": "1b"\n' +
4792 ' },\n' +
4793 ' "2"\n' +
4794 '}')
4795 bt(
4796 '{a:{a:b},c}',
4797 # -- output --
4798 '{\n' +
4799 ' a: {\n' +
4800 ' a: b\n' +
4801 ' },\n' +
4802 ' c\n' +
4803 '}')
4804 bt(
4805 '{[y[a]];keep_indent;}',
4806 # -- output --
4807 '{\n' +
4808 ' [y[a]];\n' +
4809 ' keep_indent;\n' +
4810 '}')
4811 bt(
4812 'if (x) {y} else { if (x) {y}}',
4813 # -- output --
4814 'if (x) {\n' +
4815 ' y\n' +
4816 '} else {\n' +
4817 ' if (x) {\n' +
4818 ' y\n' +
4819 ' }\n' +
4820 '}')
4821 bt(
4822 'if (foo) one()\n' +
4823 'two()\n' +
4824 'three()')
4825 bt(
4826 'if (1 + foo() && bar(baz()) / 2) one()\n' +
4827 'two()\n' +
4828 'three()')
4829 bt(
4830 'if (1 + foo() && bar(baz()) / 2) one();\n' +
4831 'two();\n' +
4832 'three();')
4833 bt(
4834 'var a=1,b={bang:2},c=3;',
4835 # -- output --
4836 'var a = 1,\n' +
4837 ' b = {\n' +
4838 ' bang: 2\n' +
4839 ' },\n' +
4840 ' c = 3;')
4841 bt(
4842 'var a={bing:1},b=2,c=3;',
4843 # -- output --
4844 'var a = {\n' +
4845 ' bing: 1\n' +
4846 ' },\n' +
4847 ' b = 2,\n' +
4848 ' c = 3;')
4849  
4850  
4851  
4852 def test_beautifier_unconverted(self):
4853 test_fragment = self.decodesto
4854 bt = self.bt
4855  
4856 self.reset_options();
4857 #============================================================
4858 self.options.indent_size = 1;
4859 self.options.indent_char = ' ';
4860 bt('{ one_char() }', "{\n one_char()\n}")
4861  
4862 bt('var a,b=1,c=2', 'var a, b = 1,\n c = 2')
4863  
4864 self.options.indent_size = 4;
4865 self.options.indent_char = ' ';
4866 bt('{ one_char() }', "{\n one_char()\n}")
4867  
4868 self.options.indent_size = 1;
4869 self.options.indent_char = "\t";
4870 bt('{ one_char() }', "{\n\tone_char()\n}")
4871 bt('x = a ? b : c; x;', 'x = a ? b : c;\nx;')
4872  
4873 #set to something else than it should change to, but with tabs on, should override
4874 self.options.indent_size = 5;
4875 self.options.indent_char = ' ';
4876 self.options.indent_with_tabs = True;
4877  
4878 bt('{ one_char() }', "{\n\tone_char()\n}")
4879 bt('x = a ? b : c; x;', 'x = a ? b : c;\nx;')
4880  
4881  
4882 self.reset_options();
4883 #============================================================
4884 self.options.preserve_newlines = False;
4885 bt('var\na=dont_preserve_newlines;', 'var a = dont_preserve_newlines;')
4886  
4887 # make sure the blank line between function definitions stays
4888 # even when preserve_newlines = False
4889 bt('function foo() {\n return 1;\n}\n\nfunction foo() {\n return 1;\n}')
4890 bt('function foo() {\n return 1;\n}\nfunction foo() {\n return 1;\n}',
4891 'function foo() {\n return 1;\n}\n\nfunction foo() {\n return 1;\n}'
4892 )
4893 bt('function foo() {\n return 1;\n}\n\n\nfunction foo() {\n return 1;\n}',
4894 'function foo() {\n return 1;\n}\n\nfunction foo() {\n return 1;\n}'
4895 )
4896  
4897  
4898 self.options.preserve_newlines = True;
4899 bt('var\na=do_preserve_newlines;', 'var\n a = do_preserve_newlines;')
4900 bt('if (foo) // comment\n{\n bar();\n}')
4901  
4902  
4903 self.reset_options();
4904 #============================================================
4905 self.options.keep_array_indentation = False;
4906 bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f']",
4907 "a = ['a', 'b', 'c',\n 'd', 'e', 'f'\n]")
4908 bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f',\n 'g', 'h', 'i']",
4909 "a = ['a', 'b', 'c',\n 'd', 'e', 'f',\n 'g', 'h', 'i'\n]")
4910 bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f',\n 'g', 'h', 'i']",
4911 "a = ['a', 'b', 'c',\n 'd', 'e', 'f',\n 'g', 'h', 'i'\n]")
4912 bt('var x = [{}\n]', 'var x = [{}]')
4913 bt('var x = [{foo:bar}\n]', 'var x = [{\n foo: bar\n}]')
4914 bt("a = ['something',\n 'completely',\n 'different'];\nif (x);",
4915 "a = ['something',\n 'completely',\n 'different'\n];\nif (x);")
4916 bt("a = ['a','b','c']", "a = ['a', 'b', 'c']")
4917 bt("a = ['a', 'b','c']", "a = ['a', 'b', 'c']")
4918 bt("x = [{'a':0}]",
4919 "x = [{\n 'a': 0\n}]")
4920 bt('{a([[a1]], {b;});}',
4921 '{\n a([\n [a1]\n ], {\n b;\n });\n}')
4922 bt("a();\n [\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ].toString();",
4923 "a();\n[\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n].toString();")
4924 bt("a();\na = [\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ].toString();",
4925 "a();\na = [\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n].toString();")
4926 bt("function() {\n Foo([\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ]);\n}",
4927 "function() {\n Foo([\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ]);\n}")
4928 bt('function foo() {\n return [\n "one",\n "two"\n ];\n}')
4929 # 4 spaces per indent input, processed with 4-spaces per indent
4930 bt( "function foo() {\n" +
4931 " return [\n" +
4932 " {\n" +
4933 " one: 'x',\n" +
4934 " two: [\n" +
4935 " {\n" +
4936 " id: 'a',\n" +
4937 " name: 'apple'\n" +
4938 " }, {\n" +
4939 " id: 'b',\n" +
4940 " name: 'banana'\n" +
4941 " }\n" +
4942 " ]\n" +
4943 " }\n" +
4944 " ];\n" +
4945 "}",
4946 "function foo() {\n" +
4947 " return [{\n" +
4948 " one: 'x',\n" +
4949 " two: [{\n" +
4950 " id: 'a',\n" +
4951 " name: 'apple'\n" +
4952 " }, {\n" +
4953 " id: 'b',\n" +
4954 " name: 'banana'\n" +
4955 " }]\n" +
4956 " }];\n" +
4957 "}")
4958 # 3 spaces per indent input, processed with 4-spaces per indent
4959 bt( "function foo() {\n" +
4960 " return [\n" +
4961 " {\n" +
4962 " one: 'x',\n" +
4963 " two: [\n" +
4964 " {\n" +
4965 " id: 'a',\n" +
4966 " name: 'apple'\n" +
4967 " }, {\n" +
4968 " id: 'b',\n" +
4969 " name: 'banana'\n" +
4970 " }\n" +
4971 " ]\n" +
4972 " }\n" +
4973 " ];\n" +
4974 "}",
4975 "function foo() {\n" +
4976 " return [{\n" +
4977 " one: 'x',\n" +
4978 " two: [{\n" +
4979 " id: 'a',\n" +
4980 " name: 'apple'\n" +
4981 " }, {\n" +
4982 " id: 'b',\n" +
4983 " name: 'banana'\n" +
4984 " }]\n" +
4985 " }];\n" +
4986 "}")
4987  
4988 self.options.keep_array_indentation = True;
4989 bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f']")
4990 bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f',\n 'g', 'h', 'i']")
4991 bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f',\n 'g', 'h', 'i']")
4992 bt('var x = [{}\n]', 'var x = [{}\n]')
4993 bt('var x = [{foo:bar}\n]', 'var x = [{\n foo: bar\n }\n]')
4994 bt("a = ['something',\n 'completely',\n 'different'];\nif (x);")
4995 bt("a = ['a','b','c']", "a = ['a', 'b', 'c']")
4996 bt("a = ['a', 'b','c']", "a = ['a', 'b', 'c']")
4997 bt("x = [{'a':0}]",
4998 "x = [{\n 'a': 0\n}]")
4999 bt('{a([[a1]], {b;});}',
5000 '{\n a([[a1]], {\n b;\n });\n}')
5001 bt("a();\n [\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ].toString();",
5002 "a();\n [\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ].toString();")
5003 bt("a();\na = [\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ].toString();",
5004 "a();\na = [\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ].toString();")
5005 bt("function() {\n Foo([\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ]);\n}",
5006 "function() {\n Foo([\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ]);\n}")
5007 bt('function foo() {\n return [\n "one",\n "two"\n ];\n}')
5008 # 4 spaces per indent input, processed with 4-spaces per indent
5009 bt( "function foo() {\n" +
5010 " return [\n" +
5011 " {\n" +
5012 " one: 'x',\n" +
5013 " two: [\n" +
5014 " {\n" +
5015 " id: 'a',\n" +
5016 " name: 'apple'\n" +
5017 " }, {\n" +
5018 " id: 'b',\n" +
5019 " name: 'banana'\n" +
5020 " }\n" +
5021 " ]\n" +
5022 " }\n" +
5023 " ];\n" +
5024 "}")
5025 # 3 spaces per indent input, processed with 4-spaces per indent
5026 # Should be unchanged, but is not - #445
5027 # bt( "function foo() {\n" +
5028 # " return [\n" +
5029 # " {\n" +
5030 # " one: 'x',\n" +
5031 # " two: [\n" +
5032 # " {\n" +
5033 # " id: 'a',\n" +
5034 # " name: 'apple'\n" +
5035 # " }, {\n" +
5036 # " id: 'b',\n" +
5037 # " name: 'banana'\n" +
5038 # " }\n" +
5039 # " ]\n" +
5040 # " }\n" +
5041 # " ];\n" +
5042 # "}")
5043  
5044 self.reset_options();
5045 #============================================================
5046 bt('a = //comment\n /regex/;')
5047  
5048 bt('if (a)\n{\nb;\n}\nelse\n{\nc;\n}', 'if (a) {\n b;\n} else {\n c;\n}')
5049  
5050 bt('var a = new function();')
5051 test_fragment('new function')
5052  
5053 self.reset_options();
5054 #============================================================
5055 # START tests for brace positioning
5056  
5057 # If this is ever supported, update tests for each brace style.
5058 # test_fragment('return\n{', 'return\n{') # can't support this?, but that's an improbable and extreme case anyway.
5059  
5060 self.options.brace_style = 'expand';
5061  
5062 bt('//case 1\nif (a == 1)\n{}\n//case 2\nelse if (a == 2)\n{}')
5063 bt('if(1){2}else{3}', "if (1)\n{\n 2\n}\nelse\n{\n 3\n}")
5064 bt('try{a();}catch(b){c();}catch(d){}finally{e();}',
5065 "try\n{\n a();\n}\ncatch (b)\n{\n c();\n}\ncatch (d)\n{}\nfinally\n{\n e();\n}")
5066 bt('if(a){b();}else if(c) foo();',
5067 "if (a)\n{\n b();\n}\nelse if (c) foo();")
5068 bt("if (a) {\n// comment\n}else{\n// comment\n}",
5069 "if (a)\n{\n // comment\n}\nelse\n{\n // comment\n}") # if/else statement with empty body
5070 bt('if (x) {y} else { if (x) {y}}',
5071 'if (x)\n{\n y\n}\nelse\n{\n if (x)\n {\n y\n }\n}')
5072 bt('if (a)\n{\nb;\n}\nelse\n{\nc;\n}',
5073 'if (a)\n{\n b;\n}\nelse\n{\n c;\n}')
5074 test_fragment(' /*\n* xx\n*/\n// xx\nif (foo) {\n bar();\n}',
5075 ' /*\n * xx\n */\n // xx\n if (foo)\n {\n bar();\n }')
5076 bt('if (foo)\n{}\nelse /regex/.test();')
5077 test_fragment('if (foo) {', 'if (foo)\n{')
5078 test_fragment('foo {', 'foo\n{')
5079 test_fragment('return {', 'return {') # return needs the brace.
5080 test_fragment('return /* inline */ {', 'return /* inline */ {')
5081 test_fragment('return;\n{', 'return;\n{')
5082 bt("throw {}")
5083 bt("throw {\n foo;\n}")
5084 bt('var foo = {}')
5085 bt('function x() {\n foo();\n}zzz', 'function x()\n{\n foo();\n}\nzzz')
5086 test_fragment('a: do {} while (); xxx', 'a: do {} while ();\nxxx')
5087 bt('{a: do {} while (); xxx}', '{\n a: do {} while ();xxx\n}')
5088 bt('var a = new function() {};')
5089 bt('var a = new function a() {};', 'var a = new function a()\n{};')
5090 bt('var a = new function()\n{};', 'var a = new function() {};')
5091 bt('var a = new function a()\n{};')
5092 bt('var a = new function a()\n {},\n b = new function b()\n {};')
5093 bt("foo({\n 'a': 1\n},\n10);",
5094 "foo(\n {\n 'a': 1\n },\n 10);")
5095 bt('(["foo","bar"]).each(function(i) {return i;});',
5096 '(["foo", "bar"]).each(function(i)\n{\n return i;\n});')
5097 bt('(function(i) {return i;})();',
5098 '(function(i)\n{\n return i;\n})();')
5099 bt( "test( /*Argument 1*/ {\n" +
5100 " 'Value1': '1'\n" +
5101 "}, /*Argument 2\n" +
5102 " */ {\n" +
5103 " 'Value2': '2'\n" +
5104 "});",
5105 # expected
5106 "test( /*Argument 1*/\n" +
5107 " {\n" +
5108 " 'Value1': '1'\n" +
5109 " },\n" +
5110 " /*Argument 2\n" +
5111 " */\n" +
5112 " {\n" +
5113 " 'Value2': '2'\n" +
5114 " });")
5115 bt( "test(\n" +
5116 "/*Argument 1*/ {\n" +
5117 " 'Value1': '1'\n" +
5118 "},\n" +
5119 "/*Argument 2\n" +
5120 " */ {\n" +
5121 " 'Value2': '2'\n" +
5122 "});",
5123 # expected
5124 "test(\n" +
5125 " /*Argument 1*/\n" +
5126 " {\n" +
5127 " 'Value1': '1'\n" +
5128 " },\n" +
5129 " /*Argument 2\n" +
5130 " */\n" +
5131 " {\n" +
5132 " 'Value2': '2'\n" +
5133 " });")
5134 bt( "test( /*Argument 1*/\n" +
5135 "{\n" +
5136 " 'Value1': '1'\n" +
5137 "}, /*Argument 2\n" +
5138 " */\n" +
5139 "{\n" +
5140 " 'Value2': '2'\n" +
5141 "});",
5142 # expected
5143 "test( /*Argument 1*/\n" +
5144 " {\n" +
5145 " 'Value1': '1'\n" +
5146 " },\n" +
5147 " /*Argument 2\n" +
5148 " */\n" +
5149 " {\n" +
5150 " 'Value2': '2'\n" +
5151 " });")
5152  
5153 self.options.brace_style = 'collapse';
5154  
5155 bt('//case 1\nif (a == 1) {}\n//case 2\nelse if (a == 2) {}')
5156 bt('if(1){2}else{3}', "if (1) {\n 2\n} else {\n 3\n}")
5157 bt('try{a();}catch(b){c();}catch(d){}finally{e();}',
5158 "try {\n a();\n} catch (b) {\n c();\n} catch (d) {} finally {\n e();\n}")
5159 bt('if(a){b();}else if(c) foo();',
5160 "if (a) {\n b();\n} else if (c) foo();")
5161 bt("if (a) {\n// comment\n}else{\n// comment\n}",
5162 "if (a) {\n // comment\n} else {\n // comment\n}") # if/else statement with empty body
5163 bt('if (x) {y} else { if (x) {y}}',
5164 'if (x) {\n y\n} else {\n if (x) {\n y\n }\n}')
5165 bt('if (a)\n{\nb;\n}\nelse\n{\nc;\n}',
5166 'if (a) {\n b;\n} else {\n c;\n}')
5167 test_fragment(' /*\n* xx\n*/\n// xx\nif (foo) {\n bar();\n}',
5168 ' /*\n * xx\n */\n // xx\n if (foo) {\n bar();\n }')
5169 bt('if (foo) {} else /regex/.test();')
5170 test_fragment('if (foo) {', 'if (foo) {')
5171 test_fragment('foo {', 'foo {')
5172 test_fragment('return {', 'return {') # return needs the brace.
5173 test_fragment('return /* inline */ {', 'return /* inline */ {')
5174 test_fragment('return;\n{', 'return; {')
5175 bt("throw {}")
5176 bt("throw {\n foo;\n}")
5177 bt('var foo = {}')
5178 bt('function x() {\n foo();\n}zzz', 'function x() {\n foo();\n}\nzzz')
5179 test_fragment('a: do {} while (); xxx', 'a: do {} while ();\nxxx')
5180 bt('{a: do {} while (); xxx}', '{\n a: do {} while ();xxx\n}')
5181 bt('var a = new function() {};')
5182 bt('var a = new function a() {};')
5183 bt('var a = new function()\n{};', 'var a = new function() {};')
5184 bt('var a = new function a()\n{};', 'var a = new function a() {};')
5185 bt('var a = new function a()\n {},\n b = new function b()\n {};', 'var a = new function a() {},\n b = new function b() {};')
5186 bt("foo({\n 'a': 1\n},\n10);",
5187 "foo({\n 'a': 1\n },\n 10);")
5188 bt('(["foo","bar"]).each(function(i) {return i;});',
5189 '(["foo", "bar"]).each(function(i) {\n return i;\n});')
5190 bt('(function(i) {return i;})();',
5191 '(function(i) {\n return i;\n})();')
5192 bt( "test( /*Argument 1*/ {\n" +
5193 " 'Value1': '1'\n" +
5194 "}, /*Argument 2\n" +
5195 " */ {\n" +
5196 " 'Value2': '2'\n" +
5197 "});",
5198 # expected
5199 "test( /*Argument 1*/ {\n" +
5200 " 'Value1': '1'\n" +
5201 " },\n" +
5202 " /*Argument 2\n" +
5203 " */\n" +
5204 " {\n" +
5205 " 'Value2': '2'\n" +
5206 " });")
5207 bt( "test(\n" +
5208 "/*Argument 1*/ {\n" +
5209 " 'Value1': '1'\n" +
5210 "},\n" +
5211 "/*Argument 2\n" +
5212 " */ {\n" +
5213 " 'Value2': '2'\n" +
5214 "});",
5215 # expected
5216 "test(\n" +
5217 " /*Argument 1*/\n" +
5218 " {\n" +
5219 " 'Value1': '1'\n" +
5220 " },\n" +
5221 " /*Argument 2\n" +
5222 " */\n" +
5223 " {\n" +
5224 " 'Value2': '2'\n" +
5225 " });")
5226 bt( "test( /*Argument 1*/\n" +
5227 "{\n" +
5228 " 'Value1': '1'\n" +
5229 "}, /*Argument 2\n" +
5230 " */\n" +
5231 "{\n" +
5232 " 'Value2': '2'\n" +
5233 "});",
5234 # expected
5235 "test( /*Argument 1*/ {\n" +
5236 " 'Value1': '1'\n" +
5237 " },\n" +
5238 " /*Argument 2\n" +
5239 " */\n" +
5240 " {\n" +
5241 " 'Value2': '2'\n" +
5242 " });")
5243  
5244 self.options.brace_style = "end-expand";
5245  
5246 bt('//case 1\nif (a == 1) {}\n//case 2\nelse if (a == 2) {}')
5247 bt('if(1){2}else{3}', "if (1) {\n 2\n}\nelse {\n 3\n}")
5248 bt('try{a();}catch(b){c();}catch(d){}finally{e();}',
5249 "try {\n a();\n}\ncatch (b) {\n c();\n}\ncatch (d) {}\nfinally {\n e();\n}")
5250 bt('if(a){b();}else if(c) foo();',
5251 "if (a) {\n b();\n}\nelse if (c) foo();")
5252 bt("if (a) {\n// comment\n}else{\n// comment\n}",
5253 "if (a) {\n // comment\n}\nelse {\n // comment\n}") # if/else statement with empty body
5254 bt('if (x) {y} else { if (x) {y}}',
5255 'if (x) {\n y\n}\nelse {\n if (x) {\n y\n }\n}')
5256 bt('if (a)\n{\nb;\n}\nelse\n{\nc;\n}',
5257 'if (a) {\n b;\n}\nelse {\n c;\n}')
5258 test_fragment(' /*\n* xx\n*/\n// xx\nif (foo) {\n bar();\n}',
5259 ' /*\n * xx\n */\n // xx\n if (foo) {\n bar();\n }')
5260 bt('if (foo) {}\nelse /regex/.test();')
5261 test_fragment('if (foo) {', 'if (foo) {')
5262 test_fragment('foo {', 'foo {')
5263 test_fragment('return {', 'return {') # return needs the brace.
5264 test_fragment('return /* inline */ {', 'return /* inline */ {')
5265 test_fragment('return;\n{', 'return; {')
5266 bt("throw {}")
5267 bt("throw {\n foo;\n}")
5268 bt('var foo = {}')
5269 bt('function x() {\n foo();\n}zzz', 'function x() {\n foo();\n}\nzzz')
5270 test_fragment('a: do {} while (); xxx', 'a: do {} while ();\nxxx')
5271 bt('{a: do {} while (); xxx}', '{\n a: do {} while ();xxx\n}')
5272 bt('var a = new function() {};')
5273 bt('var a = new function a() {};')
5274 bt('var a = new function()\n{};', 'var a = new function() {};')
5275 bt('var a = new function a()\n{};', 'var a = new function a() {};')
5276 bt('var a = new function a()\n {},\n b = new function b()\n {};', 'var a = new function a() {},\n b = new function b() {};')
5277 bt("foo({\n 'a': 1\n},\n10);",
5278 "foo({\n 'a': 1\n },\n 10);")
5279 bt('(["foo","bar"]).each(function(i) {return i;});',
5280 '(["foo", "bar"]).each(function(i) {\n return i;\n});')
5281 bt('(function(i) {return i;})();',
5282 '(function(i) {\n return i;\n})();')
5283 bt( "test( /*Argument 1*/ {\n" +
5284 " 'Value1': '1'\n" +
5285 "}, /*Argument 2\n" +
5286 " */ {\n" +
5287 " 'Value2': '2'\n" +
5288 "});",
5289 # expected
5290 "test( /*Argument 1*/ {\n" +
5291 " 'Value1': '1'\n" +
5292 " },\n" +
5293 " /*Argument 2\n" +
5294 " */\n" +
5295 " {\n" +
5296 " 'Value2': '2'\n" +
5297 " });")
5298 bt( "test(\n" +
5299 "/*Argument 1*/ {\n" +
5300 " 'Value1': '1'\n" +
5301 "},\n" +
5302 "/*Argument 2\n" +
5303 " */ {\n" +
5304 " 'Value2': '2'\n" +
5305 "});",
5306 # expected
5307 "test(\n" +
5308 " /*Argument 1*/\n" +
5309 " {\n" +
5310 " 'Value1': '1'\n" +
5311 " },\n" +
5312 " /*Argument 2\n" +
5313 " */\n" +
5314 " {\n" +
5315 " 'Value2': '2'\n" +
5316 " });")
5317 bt( "test( /*Argument 1*/\n" +
5318 "{\n" +
5319 " 'Value1': '1'\n" +
5320 "}, /*Argument 2\n" +
5321 " */\n" +
5322 "{\n" +
5323 " 'Value2': '2'\n" +
5324 "});",
5325 # expected
5326 "test( /*Argument 1*/ {\n" +
5327 " 'Value1': '1'\n" +
5328 " },\n" +
5329 " /*Argument 2\n" +
5330 " */\n" +
5331 " {\n" +
5332 " 'Value2': '2'\n" +
5333 " });")
5334  
5335 self.options.brace_style = 'none';
5336  
5337 bt('//case 1\nif (a == 1)\n{}\n//case 2\nelse if (a == 2)\n{}')
5338 bt('if(1){2}else{3}', "if (1) {\n 2\n} else {\n 3\n}")
5339 bt('try{a();}catch(b){c();}catch(d){}finally{e();}',
5340 "try {\n a();\n} catch (b) {\n c();\n} catch (d) {} finally {\n e();\n}")
5341 bt('if(a){b();}else if(c) foo();',
5342 "if (a) {\n b();\n} else if (c) foo();")
5343 bt("if (a) {\n// comment\n}else{\n// comment\n}",
5344 "if (a) {\n // comment\n} else {\n // comment\n}") # if/else statement with empty body
5345 bt('if (x) {y} else { if (x) {y}}',
5346 'if (x) {\n y\n} else {\n if (x) {\n y\n }\n}')
5347 bt('if (a)\n{\nb;\n}\nelse\n{\nc;\n}',
5348 'if (a)\n{\n b;\n}\nelse\n{\n c;\n}')
5349 test_fragment(' /*\n* xx\n*/\n// xx\nif (foo) {\n bar();\n}',
5350 ' /*\n * xx\n */\n // xx\n if (foo) {\n bar();\n }')
5351 bt('if (foo)\n{}\nelse /regex/.test();')
5352 test_fragment('if (foo) {')
5353 test_fragment('foo {')
5354 test_fragment('return {') # return needs the brace.
5355 test_fragment('return /* inline */ {')
5356 test_fragment('return;\n{')
5357 bt("throw {}")
5358 bt("throw {\n foo;\n}")
5359 bt('var foo = {}')
5360 bt('function x() {\n foo();\n}zzz', 'function x() {\n foo();\n}\nzzz')
5361 test_fragment('a: do {} while (); xxx', 'a: do {} while ();\nxxx')
5362 bt('{a: do {} while (); xxx}', '{\n a: do {} while ();xxx\n}')
5363 bt('var a = new function() {};')
5364 bt('var a = new function a() {};')
5365 bt('var a = new function()\n{};', 'var a = new function() {};')
5366 bt('var a = new function a()\n{};')
5367 bt('var a = new function a()\n {},\n b = new function b()\n {};')
5368 bt("foo({\n 'a': 1\n},\n10);",
5369 "foo({\n 'a': 1\n },\n 10);")
5370 bt('(["foo","bar"]).each(function(i) {return i;});',
5371 '(["foo", "bar"]).each(function(i) {\n return i;\n});')
5372 bt('(function(i) {return i;})();',
5373 '(function(i) {\n return i;\n})();')
5374 bt( "test( /*Argument 1*/ {\n" +
5375 " 'Value1': '1'\n" +
5376 "}, /*Argument 2\n" +
5377 " */ {\n" +
5378 " 'Value2': '2'\n" +
5379 "});",
5380 # expected
5381 "test( /*Argument 1*/ {\n" +
5382 " 'Value1': '1'\n" +
5383 " },\n" +
5384 " /*Argument 2\n" +
5385 " */\n" +
5386 " {\n" +
5387 " 'Value2': '2'\n" +
5388 " });")
5389 bt( "test(\n" +
5390 "/*Argument 1*/ {\n" +
5391 " 'Value1': '1'\n" +
5392 "},\n" +
5393 "/*Argument 2\n" +
5394 " */ {\n" +
5395 " 'Value2': '2'\n" +
5396 "});",
5397 # expected
5398 "test(\n" +
5399 " /*Argument 1*/\n" +
5400 " {\n" +
5401 " 'Value1': '1'\n" +
5402 " },\n" +
5403 " /*Argument 2\n" +
5404 " */\n" +
5405 " {\n" +
5406 " 'Value2': '2'\n" +
5407 " });")
5408 bt( "test( /*Argument 1*/\n" +
5409 "{\n" +
5410 " 'Value1': '1'\n" +
5411 "}, /*Argument 2\n" +
5412 " */\n" +
5413 "{\n" +
5414 " 'Value2': '2'\n" +
5415 "});",
5416 # expected
5417 "test( /*Argument 1*/\n" +
5418 " {\n" +
5419 " 'Value1': '1'\n" +
5420 " },\n" +
5421 " /*Argument 2\n" +
5422 " */\n" +
5423 " {\n" +
5424 " 'Value2': '2'\n" +
5425 " });")
5426 # END tests for brace position
5427  
5428 self.reset_options();
5429 #============================================================
5430 test_fragment('roo = {\n /*\n ****\n FOO\n ****\n */\n BAR: 0\n};')
5431 test_fragment("if (zz) {\n // ....\n}\n(function")
5432  
5433 self.reset_options();
5434 #============================================================
5435 self.options.preserve_newlines = True;
5436 bt('var a = 42; // foo\n\nvar b;')
5437 bt('var a = 42; // foo\n\n\nvar b;')
5438 bt("var a = 'foo' +\n 'bar';")
5439 bt("var a = \"foo\" +\n \"bar\";")
5440  
5441 bt('"foo""bar""baz"', '"foo"\n"bar"\n"baz"')
5442 bt("'foo''bar''baz'", "'foo'\n'bar'\n'baz'")
5443 bt("{\n get foo() {}\n}")
5444 bt("{\n var a = get\n foo();\n}")
5445 bt("{\n set foo() {}\n}")
5446 bt("{\n var a = set\n foo();\n}")
5447 bt("var x = {\n get function()\n}")
5448 bt("var x = {\n set function()\n}")
5449  
5450 # According to my current research get/set have no special meaning outside of an object literal
5451 bt("var x = set\n\na() {}", "var x = set\n\na() {}")
5452 bt("var x = set\n\nfunction() {}", "var x = set\n\nfunction() {}")
5453  
5454 bt('<!-- foo\nbar();\n-->')
5455 bt('<!-- dont crash') # -->
5456 bt('for () /abc/.test()')
5457 bt('if (k) /aaa/m.test(v) && l();')
5458 bt('switch (true) {\n case /swf/i.test(foo):\n bar();\n}')
5459 bt('createdAt = {\n type: Date,\n default: Date.now\n}')
5460 bt('switch (createdAt) {\n case a:\n Date,\n default:\n Date.now\n}')
5461  
5462 bt('return function();')
5463 bt('var a = function();')
5464 bt('var a = 5 + function();')
5465  
5466 bt('{\n foo // something\n ,\n bar // something\n baz\n}')
5467 bt('function a(a) {} function b(b) {} function c(c) {}', 'function a(a) {}\n\nfunction b(b) {}\n\nfunction c(c) {}')
5468  
5469  
5470 bt('import foo.*;', 'import foo.*;') # actionscript's import
5471 test_fragment('function f(a: a, b: b)') # actionscript
5472 bt('foo(a, function() {})')
5473 bt('foo(a, /regex/)')
5474  
5475 bt('/* foo */\n"x"')
5476  
5477 self.reset_options();
5478 #============================================================
5479 self.options.break_chained_methods = False
5480 self.options.preserve_newlines = False
5481 bt('foo\n.bar()\n.baz().cucumber(fat)', 'foo.bar().baz().cucumber(fat)')
5482 bt('foo\n.bar()\n.baz().cucumber(fat); foo.bar().baz().cucumber(fat)', 'foo.bar().baz().cucumber(fat);\nfoo.bar().baz().cucumber(fat)')
5483 bt('foo\n.bar()\n.baz().cucumber(fat)\n foo.bar().baz().cucumber(fat)', 'foo.bar().baz().cucumber(fat)\nfoo.bar().baz().cucumber(fat)')
5484 bt('this\n.something = foo.bar()\n.baz().cucumber(fat)', 'this.something = foo.bar().baz().cucumber(fat)')
5485 bt('this.something.xxx = foo.moo.bar()')
5486 bt('this\n.something\n.xxx = foo.moo\n.bar()', 'this.something.xxx = foo.moo.bar()')
5487  
5488 self.options.break_chained_methods = False
5489 self.options.preserve_newlines = True
5490 bt('foo\n.bar()\n.baz().cucumber(fat)', 'foo\n .bar()\n .baz().cucumber(fat)')
5491 bt('foo\n.bar()\n.baz().cucumber(fat); foo.bar().baz().cucumber(fat)', 'foo\n .bar()\n .baz().cucumber(fat);\nfoo.bar().baz().cucumber(fat)')
5492 bt('foo\n.bar()\n.baz().cucumber(fat)\n foo.bar().baz().cucumber(fat)', 'foo\n .bar()\n .baz().cucumber(fat)\nfoo.bar().baz().cucumber(fat)')
5493 bt('this\n.something = foo.bar()\n.baz().cucumber(fat)', 'this\n .something = foo.bar()\n .baz().cucumber(fat)')
5494 bt('this.something.xxx = foo.moo.bar()')
5495 bt('this\n.something\n.xxx = foo.moo\n.bar()', 'this\n .something\n .xxx = foo.moo\n .bar()')
5496  
5497 self.options.break_chained_methods = True
5498 self.options.preserve_newlines = False
5499 bt('foo\n.bar()\n.baz().cucumber(fat)', 'foo.bar()\n .baz()\n .cucumber(fat)')
5500 bt('foo\n.bar()\n.baz().cucumber(fat); foo.bar().baz().cucumber(fat)', 'foo.bar()\n .baz()\n .cucumber(fat);\nfoo.bar()\n .baz()\n .cucumber(fat)')
5501 bt('foo\n.bar()\n.baz().cucumber(fat)\n foo.bar().baz().cucumber(fat)', 'foo.bar()\n .baz()\n .cucumber(fat)\nfoo.bar()\n .baz()\n .cucumber(fat)')
5502 bt('this\n.something = foo.bar()\n.baz().cucumber(fat)', 'this.something = foo.bar()\n .baz()\n .cucumber(fat)')
5503 bt('this.something.xxx = foo.moo.bar()')
5504 bt('this\n.something\n.xxx = foo.moo\n.bar()', 'this.something.xxx = foo.moo.bar()')
5505  
5506 self.options.break_chained_methods = True
5507 self.options.preserve_newlines = True
5508 bt('foo\n.bar()\n.baz().cucumber(fat)', 'foo\n .bar()\n .baz()\n .cucumber(fat)')
5509 bt('foo\n.bar()\n.baz().cucumber(fat); foo.bar().baz().cucumber(fat)', 'foo\n .bar()\n .baz()\n .cucumber(fat);\nfoo.bar()\n .baz()\n .cucumber(fat)')
5510 bt('foo\n.bar()\n.baz().cucumber(fat)\n foo.bar().baz().cucumber(fat)', 'foo\n .bar()\n .baz()\n .cucumber(fat)\nfoo.bar()\n .baz()\n .cucumber(fat)')
5511 bt('this\n.something = foo.bar()\n.baz().cucumber(fat)', 'this\n .something = foo.bar()\n .baz()\n .cucumber(fat)')
5512 bt('this.something.xxx = foo.moo.bar()')
5513 bt('this\n.something\n.xxx = foo.moo\n.bar()', 'this\n .something\n .xxx = foo.moo\n .bar()')
5514  
5515 self.reset_options();
5516 #============================================================
5517 # Line wrap test intputs
5518 #..............---------1---------2---------3---------4---------5---------6---------7
5519 #..............1234567890123456789012345678901234567890123456789012345678901234567890
5520 wrap_input_1=('foo.bar().baz().cucumber((fat && "sassy") || (leans && mean));\n' +
5521 'Test_very_long_variable_name_this_should_never_wrap\n.but_this_can\n' +
5522 'return between_return_and_expression_should_never_wrap.but_this_can\n' +
5523 'throw between_throw_and_expression_should_never_wrap.but_this_can\n' +
5524 'if (wraps_can_occur && inside_an_if_block) that_is_\n.okay();\n' +
5525 'object_literal = {\n' +
5526 ' propertx: first_token + 12345678.99999E-6,\n' +
5527 ' property: first_token_should_never_wrap + but_this_can,\n' +
5528 ' propertz: first_token_should_never_wrap + !but_this_can,\n' +
5529 ' proper: "first_token_should_never_wrap" + "but_this_can"\n' +
5530 '}')
5531  
5532 #..............---------1---------2---------3---------4---------5---------6---------7
5533 #..............1234567890123456789012345678901234567890123456789012345678901234567890
5534 wrap_input_2=('{\n' +
5535 ' foo.bar().baz().cucumber((fat && "sassy") || (leans && mean));\n' +
5536 ' Test_very_long_variable_name_this_should_never_wrap\n.but_this_can\n' +
5537 ' return between_return_and_expression_should_never_wrap.but_this_can\n' +
5538 ' throw between_throw_and_expression_should_never_wrap.but_this_can\n' +
5539 ' if (wraps_can_occur && inside_an_if_block) that_is_\n.okay();\n' +
5540 ' object_literal = {\n' +
5541 ' propertx: first_token + 12345678.99999E-6,\n' +
5542 ' property: first_token_should_never_wrap + but_this_can,\n' +
5543 ' propertz: first_token_should_never_wrap + !but_this_can,\n' +
5544 ' proper: "first_token_should_never_wrap" + "but_this_can"\n' +
5545 ' }' +
5546 '}')
5547  
5548 self.options.preserve_newlines = False
5549 self.options.wrap_line_length = 0
5550 #..............---------1---------2---------3---------4---------5---------6---------7
5551 #..............1234567890123456789012345678901234567890123456789012345678901234567890
5552 test_fragment(wrap_input_1,
5553 # expected #
5554 'foo.bar().baz().cucumber((fat && "sassy") || (leans && mean));\n' +
5555 'Test_very_long_variable_name_this_should_never_wrap.but_this_can\n' +
5556 'return between_return_and_expression_should_never_wrap.but_this_can\n' +
5557 'throw between_throw_and_expression_should_never_wrap.but_this_can\n' +
5558 'if (wraps_can_occur && inside_an_if_block) that_is_.okay();\n' +
5559 'object_literal = {\n' +
5560 ' propertx: first_token + 12345678.99999E-6,\n' +
5561 ' property: first_token_should_never_wrap + but_this_can,\n' +
5562 ' propertz: first_token_should_never_wrap + !but_this_can,\n' +
5563 ' proper: "first_token_should_never_wrap" + "but_this_can"\n' +
5564 '}')
5565  
5566 self.options.wrap_line_length = 70
5567 #..............---------1---------2---------3---------4---------5---------6---------7
5568 #..............1234567890123456789012345678901234567890123456789012345678901234567890
5569 test_fragment(wrap_input_1,
5570 # expected #
5571 'foo.bar().baz().cucumber((fat && "sassy") || (leans && mean));\n' +
5572 'Test_very_long_variable_name_this_should_never_wrap.but_this_can\n' +
5573 'return between_return_and_expression_should_never_wrap.but_this_can\n' +
5574 'throw between_throw_and_expression_should_never_wrap.but_this_can\n' +
5575 'if (wraps_can_occur && inside_an_if_block) that_is_.okay();\n' +
5576 'object_literal = {\n' +
5577 ' propertx: first_token + 12345678.99999E-6,\n' +
5578 ' property: first_token_should_never_wrap + but_this_can,\n' +
5579 ' propertz: first_token_should_never_wrap + !but_this_can,\n' +
5580 ' proper: "first_token_should_never_wrap" + "but_this_can"\n' +
5581 '}')
5582  
5583 self.options.wrap_line_length = 40
5584 #..............---------1---------2---------3---------4---------5---------6---------7
5585 #..............1234567890123456789012345678901234567890123456789012345678901234567890
5586 test_fragment(wrap_input_1,
5587 # expected #
5588 'foo.bar().baz().cucumber((fat &&\n' +
5589 ' "sassy") || (leans && mean));\n' +
5590 'Test_very_long_variable_name_this_should_never_wrap\n' +
5591 ' .but_this_can\n' +
5592 'return between_return_and_expression_should_never_wrap\n' +
5593 ' .but_this_can\n' +
5594 'throw between_throw_and_expression_should_never_wrap\n' +
5595 ' .but_this_can\n' +
5596 'if (wraps_can_occur &&\n' +
5597 ' inside_an_if_block) that_is_.okay();\n' +
5598 'object_literal = {\n' +
5599 ' propertx: first_token +\n' +
5600 ' 12345678.99999E-6,\n' +
5601 ' property: first_token_should_never_wrap +\n' +
5602 ' but_this_can,\n' +
5603 ' propertz: first_token_should_never_wrap +\n' +
5604 ' !but_this_can,\n' +
5605 ' proper: "first_token_should_never_wrap" +\n' +
5606 ' "but_this_can"\n' +
5607 '}')
5608  
5609 self.options.wrap_line_length = 41
5610 # NOTE: wrap is only best effort - line continues until next wrap point is found.
5611 #..............---------1---------2---------3---------4---------5---------6---------7
5612 #..............1234567890123456789012345678901234567890123456789012345678901234567890
5613 test_fragment(wrap_input_1,
5614 # expected #
5615 'foo.bar().baz().cucumber((fat && "sassy") ||\n' +
5616 ' (leans && mean));\n' +
5617 'Test_very_long_variable_name_this_should_never_wrap\n' +
5618 ' .but_this_can\n' +
5619 'return between_return_and_expression_should_never_wrap\n' +
5620 ' .but_this_can\n' +
5621 'throw between_throw_and_expression_should_never_wrap\n' +
5622 ' .but_this_can\n' +
5623 'if (wraps_can_occur &&\n' +
5624 ' inside_an_if_block) that_is_.okay();\n' +
5625 'object_literal = {\n' +
5626 ' propertx: first_token +\n' +
5627 ' 12345678.99999E-6,\n' +
5628 ' property: first_token_should_never_wrap +\n' +
5629 ' but_this_can,\n' +
5630 ' propertz: first_token_should_never_wrap +\n' +
5631 ' !but_this_can,\n' +
5632 ' proper: "first_token_should_never_wrap" +\n' +
5633 ' "but_this_can"\n' +
5634 '}')
5635  
5636  
5637 self.options.wrap_line_length = 45
5638 # NOTE: wrap is only best effort - line continues until next wrap point is found.
5639 #..............---------1---------2---------3---------4---------5---------6---------7
5640 #..............1234567890123456789012345678901234567890123456789012345678901234567890
5641 test_fragment(wrap_input_2,
5642 # expected #
5643 '{\n' +
5644 ' foo.bar().baz().cucumber((fat && "sassy") ||\n' +
5645 ' (leans && mean));\n' +
5646 ' Test_very_long_variable_name_this_should_never_wrap\n' +
5647 ' .but_this_can\n' +
5648 ' return between_return_and_expression_should_never_wrap\n' +
5649 ' .but_this_can\n' +
5650 ' throw between_throw_and_expression_should_never_wrap\n' +
5651 ' .but_this_can\n' +
5652 ' if (wraps_can_occur &&\n' +
5653 ' inside_an_if_block) that_is_.okay();\n' +
5654 ' object_literal = {\n' +
5655 ' propertx: first_token +\n' +
5656 ' 12345678.99999E-6,\n' +
5657 ' property: first_token_should_never_wrap +\n' +
5658 ' but_this_can,\n' +
5659 ' propertz: first_token_should_never_wrap +\n' +
5660 ' !but_this_can,\n' +
5661 ' proper: "first_token_should_never_wrap" +\n' +
5662 ' "but_this_can"\n' +
5663 ' }\n'+
5664 '}')
5665  
5666 self.options.preserve_newlines = True
5667 self.options.wrap_line_length = 0
5668 #..............---------1---------2---------3---------4---------5---------6---------7
5669 #..............1234567890123456789012345678901234567890123456789012345678901234567890
5670 test_fragment(wrap_input_1,
5671 # expected #
5672 'foo.bar().baz().cucumber((fat && "sassy") || (leans && mean));\n' +
5673 'Test_very_long_variable_name_this_should_never_wrap\n' +
5674 ' .but_this_can\n' +
5675 'return between_return_and_expression_should_never_wrap.but_this_can\n' +
5676 'throw between_throw_and_expression_should_never_wrap.but_this_can\n' +
5677 'if (wraps_can_occur && inside_an_if_block) that_is_\n' +
5678 ' .okay();\n' +
5679 'object_literal = {\n' +
5680 ' propertx: first_token + 12345678.99999E-6,\n' +
5681 ' property: first_token_should_never_wrap + but_this_can,\n' +
5682 ' propertz: first_token_should_never_wrap + !but_this_can,\n' +
5683 ' proper: "first_token_should_never_wrap" + "but_this_can"\n' +
5684 '}')
5685  
5686  
5687 self.options.wrap_line_length = 70
5688 #..............---------1---------2---------3---------4---------5---------6---------7
5689 #..............1234567890123456789012345678901234567890123456789012345678901234567890
5690 test_fragment(wrap_input_1,
5691 # expected #
5692 'foo.bar().baz().cucumber((fat && "sassy") || (leans && mean));\n' +
5693 'Test_very_long_variable_name_this_should_never_wrap\n' +
5694 ' .but_this_can\n' +
5695 'return between_return_and_expression_should_never_wrap.but_this_can\n' +
5696 'throw between_throw_and_expression_should_never_wrap.but_this_can\n' +
5697 'if (wraps_can_occur && inside_an_if_block) that_is_\n' +
5698 ' .okay();\n' +
5699 'object_literal = {\n' +
5700 ' propertx: first_token + 12345678.99999E-6,\n' +
5701 ' property: first_token_should_never_wrap + but_this_can,\n' +
5702 ' propertz: first_token_should_never_wrap + !but_this_can,\n' +
5703 ' proper: "first_token_should_never_wrap" + "but_this_can"\n' +
5704 '}')
5705  
5706  
5707 self.options.wrap_line_length = 40
5708 #..............---------1---------2---------3---------4---------5---------6---------7
5709 #..............1234567890123456789012345678901234567890123456789012345678901234567890
5710 test_fragment(wrap_input_1,
5711 # expected #
5712 'foo.bar().baz().cucumber((fat &&\n' +
5713 ' "sassy") || (leans && mean));\n' +
5714 'Test_very_long_variable_name_this_should_never_wrap\n' +
5715 ' .but_this_can\n' +
5716 'return between_return_and_expression_should_never_wrap\n' +
5717 ' .but_this_can\n' +
5718 'throw between_throw_and_expression_should_never_wrap\n' +
5719 ' .but_this_can\n' +
5720 'if (wraps_can_occur &&\n' +
5721 ' inside_an_if_block) that_is_\n' +
5722 ' .okay();\n' +
5723 'object_literal = {\n' +
5724 ' propertx: first_token +\n' +
5725 ' 12345678.99999E-6,\n' +
5726 ' property: first_token_should_never_wrap +\n' +
5727 ' but_this_can,\n' +
5728 ' propertz: first_token_should_never_wrap +\n' +
5729 ' !but_this_can,\n' +
5730 ' proper: "first_token_should_never_wrap" +\n' +
5731 ' "but_this_can"\n' +
5732 '}')
5733  
5734 self.options.wrap_line_length = 41
5735 # NOTE: wrap is only best effort - line continues until next wrap point is found.
5736 #..............---------1---------2---------3---------4---------5---------6---------7
5737 #..............1234567890123456789012345678901234567890123456789012345678901234567890
5738 test_fragment(wrap_input_1,
5739 # expected #
5740 'foo.bar().baz().cucumber((fat && "sassy") ||\n' +
5741 ' (leans && mean));\n' +
5742 'Test_very_long_variable_name_this_should_never_wrap\n' +
5743 ' .but_this_can\n' +
5744 'return between_return_and_expression_should_never_wrap\n' +
5745 ' .but_this_can\n' +
5746 'throw between_throw_and_expression_should_never_wrap\n' +
5747 ' .but_this_can\n' +
5748 'if (wraps_can_occur &&\n' +
5749 ' inside_an_if_block) that_is_\n' +
5750 ' .okay();\n' +
5751 'object_literal = {\n' +
5752 ' propertx: first_token +\n' +
5753 ' 12345678.99999E-6,\n' +
5754 ' property: first_token_should_never_wrap +\n' +
5755 ' but_this_can,\n' +
5756 ' propertz: first_token_should_never_wrap +\n' +
5757 ' !but_this_can,\n' +
5758 ' proper: "first_token_should_never_wrap" +\n' +
5759 ' "but_this_can"\n' +
5760 '}')
5761  
5762 self.options.wrap_line_length = 45
5763 # NOTE: wrap is only best effort - line continues until next wrap point is found.
5764 #..............---------1---------2---------3---------4---------5---------6---------7
5765 #..............1234567890123456789012345678901234567890123456789012345678901234567890
5766 test_fragment(wrap_input_2,
5767 # expected #
5768 '{\n' +
5769 ' foo.bar().baz().cucumber((fat && "sassy") ||\n' +
5770 ' (leans && mean));\n' +
5771 ' Test_very_long_variable_name_this_should_never_wrap\n' +
5772 ' .but_this_can\n' +
5773 ' return between_return_and_expression_should_never_wrap\n' +
5774 ' .but_this_can\n' +
5775 ' throw between_throw_and_expression_should_never_wrap\n' +
5776 ' .but_this_can\n' +
5777 ' if (wraps_can_occur &&\n' +
5778 ' inside_an_if_block) that_is_\n' +
5779 ' .okay();\n' +
5780 ' object_literal = {\n' +
5781 ' propertx: first_token +\n' +
5782 ' 12345678.99999E-6,\n' +
5783 ' property: first_token_should_never_wrap +\n' +
5784 ' but_this_can,\n' +
5785 ' propertz: first_token_should_never_wrap +\n' +
5786 ' !but_this_can,\n' +
5787 ' proper: "first_token_should_never_wrap" +\n' +
5788 ' "but_this_can"\n' +
5789 ' }\n'+
5790 '}')
5791  
5792 self.reset_options();
5793 #============================================================
5794 self.options.preserve_newlines = False
5795 bt('if (foo) // comment\n bar();')
5796 bt('if (foo) // comment\n (bar());')
5797 bt('if (foo) // comment\n (bar());')
5798 bt('if (foo) // comment\n /asdf/;')
5799 bt('this.oa = new OAuth(\n' +
5800 ' _requestToken,\n' +
5801 ' _accessToken,\n' +
5802 ' consumer_key\n' +
5803 ');',
5804 'this.oa = new OAuth(_requestToken, _accessToken, consumer_key);')
5805 bt('foo = {\n x: y, // #44\n w: z // #44\n}')
5806 bt('switch (x) {\n case "a":\n // comment on newline\n break;\n case "b": // comment on same line\n break;\n}')
5807 bt('this.type =\n this.options =\n // comment\n this.enabled null;',
5808 'this.type = this.options =\n // comment\n this.enabled null;')
5809 bt('someObj\n .someFunc1()\n // This comment should not break the indent\n .someFunc2();',
5810 'someObj.someFunc1()\n // This comment should not break the indent\n .someFunc2();')
5811  
5812 bt('if (true ||\n!true) return;', 'if (true || !true) return;')
5813  
5814 # these aren't ready yet.
5815 #bt('if (foo) // comment\n bar() /*i*/ + baz() /*j\n*/ + asdf();')
5816 bt('if\n(foo)\nif\n(bar)\nif\n(baz)\nwhee();\na();',
5817 'if (foo)\n if (bar)\n if (baz) whee();\na();')
5818 bt('if\n(foo)\nif\n(bar)\nif\n(baz)\nwhee();\nelse\na();',
5819 'if (foo)\n if (bar)\n if (baz) whee();\n else a();')
5820 bt('if (foo)\nbar();\nelse\ncar();',
5821 'if (foo) bar();\nelse car();')
5822  
5823 bt('if (foo) if (bar) if (baz);\na();',
5824 'if (foo)\n if (bar)\n if (baz);\na();')
5825 bt('if (foo) if (bar) if (baz) whee();\na();',
5826 'if (foo)\n if (bar)\n if (baz) whee();\na();')
5827 bt('if (foo) a()\nif (bar) if (baz) whee();\na();',
5828 'if (foo) a()\nif (bar)\n if (baz) whee();\na();')
5829 bt('if (foo);\nif (bar) if (baz) whee();\na();',
5830 'if (foo);\nif (bar)\n if (baz) whee();\na();')
5831 bt('if (options)\n' +
5832 ' for (var p in options)\n' +
5833 ' this[p] = options[p];',
5834 'if (options)\n'+
5835 ' for (var p in options) this[p] = options[p];')
5836 bt('if (options) for (var p in options) this[p] = options[p];',
5837 'if (options)\n for (var p in options) this[p] = options[p];')
5838  
5839 bt('if (options) do q(); while (b());',
5840 'if (options)\n do q(); while (b());')
5841 bt('if (options) while (b()) q();',
5842 'if (options)\n while (b()) q();')
5843 bt('if (options) do while (b()) q(); while (a());',
5844 'if (options)\n do\n while (b()) q(); while (a());')
5845  
5846 bt('function f(a, b, c,\nd, e) {}',
5847 'function f(a, b, c, d, e) {}')
5848  
5849 bt('function f(a,b) {if(a) b()}function g(a,b) {if(!a) b()}',
5850 'function f(a, b) {\n if (a) b()\n}\n\nfunction g(a, b) {\n if (!a) b()\n}')
5851 bt('function f(a,b) {if(a) b()}\n\n\n\nfunction g(a,b) {if(!a) b()}',
5852 'function f(a, b) {\n if (a) b()\n}\n\nfunction g(a, b) {\n if (!a) b()\n}')
5853 # This is not valid syntax, but still want to behave reasonably and not side-effect
5854 bt('(if(a) b())(if(a) b())',
5855 '(\n if (a) b())(\n if (a) b())')
5856 bt('(if(a) b())\n\n\n(if(a) b())',
5857 '(\n if (a) b())\n(\n if (a) b())')
5858  
5859 # space between functions
5860 bt('/*\n * foo\n */\nfunction foo() {}')
5861 bt('// a nice function\nfunction foo() {}')
5862 bt('function foo() {}\nfunction foo() {}',
5863 'function foo() {}\n\nfunction foo() {}'
5864 )
5865  
5866 bt('[\n function() {}\n]')
5867  
5868  
5869 bt("if\n(a)\nb();", "if (a) b();")
5870 bt('var a =\nfoo', 'var a = foo')
5871 bt('var a = {\n"a":1,\n"b":2}', "var a = {\n \"a\": 1,\n \"b\": 2\n}")
5872 bt("var a = {\n'a':1,\n'b':2}", "var a = {\n 'a': 1,\n 'b': 2\n}")
5873 bt('var a = /*i*/ "b";')
5874 bt('var a = /*i*/\n"b";', 'var a = /*i*/ "b";')
5875 bt('var a = /*i*/\nb;', 'var a = /*i*/ b;')
5876 bt('{\n\n\n"x"\n}', '{\n "x"\n}')
5877 bt('if(a &&\nb\n||\nc\n||d\n&&\ne) e = f', 'if (a && b || c || d && e) e = f')
5878 bt('if(a &&\n(b\n||\nc\n||d)\n&&\ne) e = f', 'if (a && (b || c || d) && e) e = f')
5879 test_fragment('\n\n"x"', '"x"')
5880 bt('a = 1;\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nb = 2;',
5881 'a = 1;\nb = 2;')
5882  
5883  
5884 self.options.preserve_newlines = True
5885 bt('if (foo) // comment\n bar();')
5886 bt('if (foo) // comment\n (bar());')
5887 bt('if (foo) // comment\n (bar());')
5888 bt('if (foo) // comment\n /asdf/;')
5889 bt('this.oa = new OAuth(\n' +
5890 ' _requestToken,\n' +
5891 ' _accessToken,\n' +
5892 ' consumer_key\n' +
5893 ');')
5894 bt('foo = {\n x: y, // #44\n w: z // #44\n}')
5895 bt('switch (x) {\n case "a":\n // comment on newline\n break;\n case "b": // comment on same line\n break;\n}')
5896 bt('this.type =\n this.options =\n // comment\n this.enabled null;')
5897 bt('someObj\n .someFunc1()\n // This comment should not break the indent\n .someFunc2();')
5898  
5899 bt('if (true ||\n!true) return;', 'if (true ||\n !true) return;')
5900  
5901 # these aren't ready yet.
5902 # bt('if (foo) // comment\n bar() /*i*/ + baz() /*j\n*/ + asdf();')
5903 bt('if\n(foo)\nif\n(bar)\nif\n(baz)\nwhee();\na();',
5904 'if (foo)\n if (bar)\n if (baz)\n whee();\na();')
5905 bt('if\n(foo)\nif\n(bar)\nif\n(baz)\nwhee();\nelse\na();',
5906 'if (foo)\n if (bar)\n if (baz)\n whee();\n else\n a();')
5907 bt('if (foo)\nbar();\nelse\ncar();',
5908 'if (foo)\n bar();\nelse\n car();')
5909 bt('if (foo) bar();\nelse\ncar();',
5910 'if (foo) bar();\nelse\n car();')
5911  
5912 bt('if (foo) if (bar) if (baz);\na();',
5913 'if (foo)\n if (bar)\n if (baz);\na();')
5914 bt('if (foo) if (bar) if (baz) whee();\na();',
5915 'if (foo)\n if (bar)\n if (baz) whee();\na();')
5916 bt('if (foo) a()\nif (bar) if (baz) whee();\na();',
5917 'if (foo) a()\nif (bar)\n if (baz) whee();\na();')
5918 bt('if (foo);\nif (bar) if (baz) whee();\na();',
5919 'if (foo);\nif (bar)\n if (baz) whee();\na();')
5920 bt('if (options)\n' +
5921 ' for (var p in options)\n' +
5922 ' this[p] = options[p];')
5923 bt('if (options) for (var p in options) this[p] = options[p];',
5924 'if (options)\n for (var p in options) this[p] = options[p];')
5925  
5926 bt('if (options) do q(); while (b());',
5927 'if (options)\n do q(); while (b());')
5928 bt('if (options) do; while (b());',
5929 'if (options)\n do; while (b());')
5930 bt('if (options) while (b()) q();',
5931 'if (options)\n while (b()) q();')
5932 bt('if (options) do while (b()) q(); while (a());',
5933 'if (options)\n do\n while (b()) q(); while (a());')
5934  
5935 bt('function f(a, b, c,\nd, e) {}',
5936 'function f(a, b, c,\n d, e) {}')
5937  
5938 bt('function f(a,b) {if(a) b()}function g(a,b) {if(!a) b()}',
5939 'function f(a, b) {\n if (a) b()\n}\n\nfunction g(a, b) {\n if (!a) b()\n}')
5940 bt('function f(a,b) {if(a) b()}\n\n\n\nfunction g(a,b) {if(!a) b()}',
5941 'function f(a, b) {\n if (a) b()\n}\n\n\n\nfunction g(a, b) {\n if (!a) b()\n}')
5942 # This is not valid syntax, but still want to behave reasonably and not side-effect
5943 bt('(if(a) b())(if(a) b())',
5944 '(\n if (a) b())(\n if (a) b())')
5945 bt('(if(a) b())\n\n\n(if(a) b())',
5946 '(\n if (a) b())\n\n\n(\n if (a) b())')
5947  
5948  
5949 bt("if\n(a)\nb();", "if (a)\n b();")
5950 bt('var a =\nfoo', 'var a =\n foo')
5951 bt('var a = {\n"a":1,\n"b":2}', "var a = {\n \"a\": 1,\n \"b\": 2\n}")
5952 bt("var a = {\n'a':1,\n'b':2}", "var a = {\n 'a': 1,\n 'b': 2\n}")
5953 bt('var a = /*i*/ "b";')
5954 bt('var a = /*i*/\n"b";', 'var a = /*i*/\n "b";')
5955 bt('var a = /*i*/\nb;', 'var a = /*i*/\n b;')
5956 bt('{\n\n\n"x"\n}', '{\n\n\n "x"\n}')
5957 bt('if(a &&\nb\n||\nc\n||d\n&&\ne) e = f', 'if (a &&\n b ||\n c ||\n d &&\n e) e = f')
5958 bt('if(a &&\n(b\n||\nc\n||d)\n&&\ne) e = f', 'if (a &&\n (b ||\n c ||\n d) &&\n e) e = f')
5959 test_fragment('\n\n"x"', '"x"')
5960 # this beavior differs between js and python, defaults to unlimited in js, 10 in python
5961 bt('a = 1;\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nb = 2;',
5962 'a = 1;\n\n\n\n\n\n\n\n\n\nb = 2;')
5963 self.options.max_preserve_newlines = 8;
5964 bt('a = 1;\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nb = 2;',
5965 'a = 1;\n\n\n\n\n\n\n\nb = 2;')
5966  
5967 self.reset_options();
5968 #============================================================
5969  
5970  
5971 def decodesto(self, input, expectation=None):
5972 if expectation == None:
5973 expectation = input
5974  
5975 self.assertMultiLineEqual(
5976 jsbeautifier.beautify(input, self.options), expectation)
5977  
5978 # if the expected is different from input, run it again
5979 # expected output should be unchanged when run twice.
5980 if not expectation == None:
5981 self.assertMultiLineEqual(
5982 jsbeautifier.beautify(expectation, self.options), expectation)
5983  
5984 # Everywhere we do newlines, they should be replaced with opts.eol
5985 self.options.eol = '\r\\n';
5986 expectation = expectation.replace('\n', '\r\n')
5987 self.assertMultiLineEqual(
5988 jsbeautifier.beautify(input, self.options), expectation)
5989 if input.find('\n') != -1:
5990 input = input.replace('\n', '\r\n')
5991 self.assertMultiLineEqual(
5992 jsbeautifier.beautify(input, self.options), expectation)
5993 # Ensure support for auto eol detection
5994 self.options.eol = 'auto'
5995 self.assertMultiLineEqual(
5996 jsbeautifier.beautify(input, self.options), expectation)
5997 self.options.eol = '\n'
5998  
5999 def wrap(self, text):
6000 return self.wrapregex.sub(' \\1', text)
6001  
6002 def bt(self, input, expectation=None):
6003 if expectation == None:
6004 expectation = input
6005  
6006 self.decodesto(input, expectation)
6007 # If we set raw, input should be unchanged
6008 self.options.test_output_raw = True
6009 if self.options.end_with_newline:
6010 elf.decodesto(input, input)
6011 self.options.test_output_raw = False
6012  
6013 current_indent_size = None
6014 if self.options.js and self.options.js['indent_size']:
6015 current_indent_size = self.options.js['indent_size']
6016  
6017 if not current_indent_size:
6018 current_indent_size = self.options.indent_size
6019  
6020 if current_indent_size == 4 and input:
6021 wrapped_input = '{\n%s\n foo = bar;\n}' % self.wrap(input)
6022 wrapped_expect = '{\n%s\n foo = bar;\n}' % self.wrap(expectation)
6023 self.decodesto(wrapped_input, wrapped_expect)
6024  
6025 # If we set raw, input should be unchanged
6026 self.options.test_output_raw = True
6027 if self.options.end_with_newline:
6028 elf.decodesto(wrapped_input, wrapped_input)
6029 self.options.test_output_raw = False
6030  
6031  
6032 if __name__ == '__main__':
6033 unittest.main()