corrade-nucleus-nucleons – Blame information for rev 20

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