node-javascript.mustache 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. /*global js_beautify: true */
  2. function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_beautify)
  3. {
  4. var opts = {
  5. indent_size: 4,
  6. indent_char: ' ',
  7. preserve_newlines: true,
  8. jslint_happy: false,
  9. keep_array_indentation: false,
  10. brace_style: 'collapse',
  11. space_before_conditional: true,
  12. break_chained_methods: false,
  13. selector_separator: '\n',
  14. end_with_newline: false
  15. };
  16. function test_js_beautifier(input)
  17. {
  18. return js_beautify(input, opts);
  19. }
  20. function test_html_beautifier(input)
  21. {
  22. return html_beautify(input, opts);
  23. }
  24. var sanitytest;
  25. // test the input on beautifier with the current flag settings
  26. // does not check the indentation / surroundings as bt() does
  27. function test_fragment(input, expected)
  28. {
  29. expected = expected || expected === '' ? expected : input;
  30. sanitytest.expect(input, expected);
  31. // if the expected is different from input, run it again
  32. // expected output should be unchanged when run twice.
  33. if (expected !== input) {
  34. sanitytest.expect(expected, expected);
  35. }
  36. // Everywhere we do newlines, they should be replaced with opts.eol
  37. opts.eol = '\r\\n';
  38. expected = expected.replace(/[\n]/g, '\r\n');
  39. sanitytest.expect(input, expected);
  40. input = input.replace(/[\n]/g, '\r\n');
  41. sanitytest.expect(input, expected);
  42. opts.eol = '\n';
  43. }
  44. // test the input on beautifier with the current flag settings
  45. // test both the input as well as { input } wrapping
  46. function bt(input, expectation)
  47. {
  48. var wrapped_input, wrapped_expectation;
  49. expectation = expectation || expectation === '' ? expectation : input;
  50. sanitytest.test_function(test_js_beautifier, 'js_beautify');
  51. test_fragment(input, expectation);
  52. // If we set raw, input should be unchanged
  53. opts.test_output_raw = true;
  54. if (!opts.end_with_newline) {
  55. test_fragment(input, input);
  56. }
  57. opts.test_output_raw = false;
  58. // test also the returned indentation
  59. // e.g if input = "asdf();"
  60. // then test that this remains properly formatted as well:
  61. // {
  62. // asdf();
  63. // indent;
  64. // }
  65. if (opts.indent_size === 4 && input) {
  66. wrapped_input = '{\n' + input.replace(/^(.+)$/mg, ' $1') + '\n foo = bar;\n}';
  67. wrapped_expectation = '{\n' + expectation.replace(/^(.+)$/mg, ' $1') + '\n foo = bar;\n}';
  68. test_fragment(wrapped_input, wrapped_expectation);
  69. // If we set raw, input should be unchanged
  70. opts.test_output_raw = true;
  71. if (!opts.end_with_newline) {
  72. test_fragment(wrapped_input, wrapped_input);
  73. }
  74. opts.test_output_raw = false;
  75. }
  76. }
  77. // run all tests for the given brace style ("collapse", "expand", "end-expand", or "none").
  78. // uses various whitespace combinations before and after opening and closing braces,
  79. // respectively, for most of the tests' inputs.
  80. function beautify_brace_tests(brace_style) {
  81. var ex_brace_style = opts.brace_style,
  82. 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
  83. function permute_brace_tests(expect_open_white, expect_close_white) {
  84. // run the tests that need permutation against a specific combination of
  85. // pre-opening-brace and pre-closing-brace whitespace
  86. function run_brace_permutation(test_open_white, test_close_white) {
  87. var to = test_open_white,
  88. tc = test_close_white,
  89. eo = expect_open_white ? expect_open_white : to === '' ? ' ' : to,
  90. ec = expect_close_white ? expect_close_white : tc === '' ? ' ' : tc,
  91. i = eo === '\n' ? indent_on_wrap_str: '';
  92. bt( '//case 1\nif (a == 1)' + to + '{}\n//case 2\nelse if (a == 2)' + to + '{}',
  93. '//case 1\nif (a == 1)' + eo + '{}\n//case 2\nelse if (a == 2)' + eo + '{}');
  94. bt( 'if(1)' + to + '{2}' + tc + 'else' + to + '{3}',
  95. 'if (1)' + eo + '{\n 2\n}' + ec + 'else' + eo + '{\n 3\n}');
  96. bt( 'try' + to + '{a();}' + tc +
  97. 'catch(b)' + to + '{c();}' + tc +
  98. 'catch(d)' + to + '{}' + tc +
  99. 'finally' + to + '{e();}',
  100. // expected
  101. 'try' + eo + '{\n a();\n}' + ec +
  102. 'catch (b)' + eo + '{\n c();\n}' + ec +
  103. 'catch (d)' + eo + '{}' + ec +
  104. 'finally' + eo + '{\n e();\n}');
  105. bt( 'if(a)' + to + '{b();}' + tc + 'else if(c) foo();',
  106. 'if (a)' + eo + '{\n b();\n}' + ec + 'else if (c) foo();');
  107. // if/else statement with empty body
  108. bt( 'if (a)' + to + '{\n// comment\n}' + tc + 'else' + to + '{\n// comment\n}',
  109. 'if (a)' + eo + '{\n // comment\n}' + ec + 'else' + eo + '{\n // comment\n}');
  110. bt( 'if (x)' + to + '{y}' + tc + 'else' + to + '{ if (x)' + to + '{y}}',
  111. 'if (x)' + eo + '{\n y\n}' + ec + 'else' + eo + '{\n if (x)' + eo + i + '{\n y\n }\n}');
  112. bt( 'if (a)' + to + '{\nb;\n}' + tc + 'else' + to + '{\nc;\n}',
  113. 'if (a)' + eo + '{\n b;\n}' + ec + 'else' + eo + '{\n c;\n}');
  114. test_fragment(' /*\n* xx\n*/\n// xx\nif (foo)' + to + '{\n bar();\n}',
  115. ' /*\n * xx\n */\n // xx\n if (foo)' + eo + i + '{\n bar();\n }');
  116. bt( 'if (foo)' + to + '{}' + tc + 'else /regex/.test();',
  117. 'if (foo)' + eo + '{}' + ec + 'else /regex/.test();');
  118. test_fragment('if (foo)' + to + '{', 'if (foo)' + eo + '{');
  119. test_fragment('foo' + to + '{', 'foo' + eo + '{');
  120. test_fragment('return;' + to + '{', 'return;' + eo + '{');
  121. bt( 'function x()' + to + '{\n foo();\n}zzz', 'function x()' + eo +'{\n foo();\n}\nzzz');
  122. bt( 'var a = new function a()' + to + '{};', 'var a = new function a()' + eo + '{};');
  123. bt( 'var a = new function a()' + to + ' {},\n b = new function b()' + to + ' {};',
  124. 'var a = new function a()' + eo + i + '{},\n b = new function b()' + eo + i + '{};');
  125. bt("foo(" + to + "{\n 'a': 1\n},\n10);",
  126. "foo(" + (eo === ' ' ? '' : eo) + i + "{\n 'a': 1\n },\n 10);"); // "foo( {..." is a weird case
  127. bt('(["foo","bar"]).each(function(i)' + to + '{return i;});',
  128. '(["foo", "bar"]).each(function(i)' + eo + '{\n return i;\n});');
  129. bt('(function(i)' + to + '{return i;})();', '(function(i)' + eo + '{\n return i;\n})();');
  130. bt( "test( /*Argument 1*/" + to + "{\n" +
  131. " 'Value1': '1'\n" +
  132. "}, /*Argument 2\n" +
  133. " */ {\n" +
  134. " 'Value2': '2'\n" +
  135. "});",
  136. // expected
  137. "test( /*Argument 1*/" + eo + i + "{\n" +
  138. " 'Value1': '1'\n" +
  139. " },\n" +
  140. " /*Argument 2\n" +
  141. " */\n" +
  142. " {\n" +
  143. " 'Value2': '2'\n" +
  144. " });");
  145. bt( "test( /*Argument 1*/" + to + "{\n" +
  146. " 'Value1': '1'\n" +
  147. "}, /*Argument 2\n" +
  148. " */\n" +
  149. "{\n" +
  150. " 'Value2': '2'\n" +
  151. "});",
  152. // expected
  153. "test( /*Argument 1*/" + eo + i + "{\n" +
  154. " 'Value1': '1'\n" +
  155. " },\n" +
  156. " /*Argument 2\n" +
  157. " */\n" +
  158. " {\n" +
  159. " 'Value2': '2'\n" +
  160. " });");
  161. }
  162. run_brace_permutation('\n', '\n');
  163. run_brace_permutation('\n', ' ');
  164. run_brace_permutation(' ', ' ');
  165. run_brace_permutation(' ', '\n');
  166. run_brace_permutation('','');
  167. // brace tests that don't make sense to permutate
  168. test_fragment('return {'); // return needs the brace.
  169. test_fragment('return /* inline */ {');
  170. bt('throw {}');
  171. bt('throw {\n foo;\n}');
  172. bt( 'var foo = {}');
  173. test_fragment('a: do {} while (); xxx', 'a: do {} while ();\nxxx');
  174. bt( '{a: do {} while (); xxx}', '{\n a: do {} while ();xxx\n}');
  175. bt( 'var a = new function() {};');
  176. bt( 'var a = new function()\n{};', 'var a = new function() {};');
  177. bt( "test(\n" +
  178. "/*Argument 1*/ {\n" +
  179. " 'Value1': '1'\n" +
  180. "},\n" +
  181. "/*Argument 2\n" +
  182. " */ {\n" +
  183. " 'Value2': '2'\n" +
  184. "});",
  185. // expected
  186. "test(\n" +
  187. " /*Argument 1*/\n" +
  188. " {\n" +
  189. " 'Value1': '1'\n" +
  190. " },\n" +
  191. " /*Argument 2\n" +
  192. " */\n" +
  193. " {\n" +
  194. " 'Value2': '2'\n" +
  195. " });");
  196. }
  197. opts.brace_style = brace_style;
  198. switch(opts.brace_style) {
  199. case 'collapse':
  200. permute_brace_tests(' ', ' ');
  201. break;
  202. case 'expand':
  203. permute_brace_tests('\n', '\n');
  204. break;
  205. case 'end-expand':
  206. permute_brace_tests(' ', '\n');
  207. break;
  208. case 'none':
  209. permute_brace_tests();
  210. break;
  211. }
  212. opts.brace_style = ex_brace_style;
  213. }
  214. function unicode_char(value) {
  215. return String.fromCharCode(value)
  216. }
  217. function beautifier_tests()
  218. {
  219. sanitytest = test_obj;
  220. {{#default_options}} opts.{{name}} = {{&value}};
  221. {{/default_options}}
  222. {{#groups}}{{#set_mustache_tags}}.{{/set_mustache_tags}}{{^matrix}}
  223. // {{&name}}
  224. {{#options}}
  225. opts.{{name}} = {{&value}};
  226. {{/options}}
  227. {{#tests}}
  228. {{#test_line}}.{{/test_line}};
  229. {{/tests}}
  230. {{/matrix}}{{#matrix}}
  231. // {{&name}} - ({{#matrix_context_string}}.{{/matrix_context_string}})
  232. {{#options}}
  233. opts.{{name}} = {{&value}};
  234. {{/options}}
  235. {{#tests}}
  236. {{#test_line}}.{{/test_line}};
  237. {{/tests}}
  238. {{/matrix}}{{#unset_mustache_tags}}.{{/unset_mustache_tags}}
  239. {{/groups}}
  240. opts.indent_size = 1;
  241. opts.indent_char = ' ';
  242. bt('{ one_char() }', "{\n one_char()\n}");
  243. bt('var a,b=1,c=2', 'var a, b = 1,\n c = 2');
  244. opts.indent_size = 4;
  245. opts.indent_char = ' ';
  246. bt('{ one_char() }', "{\n one_char()\n}");
  247. opts.indent_size = 1;
  248. opts.indent_char = "\t";
  249. bt('{ one_char() }', "{\n\tone_char()\n}");
  250. bt('x = a ? b : c; x;', 'x = a ? b : c;\nx;');
  251. //set to something else than it should change to, but with tabs on, should override
  252. opts.indent_size = 5;
  253. opts.indent_char = ' ';
  254. opts.indent_with_tabs = true;
  255. bt('{ one_char() }', "{\n\tone_char()\n}");
  256. bt('x = a ? b : c; x;', 'x = a ? b : c;\nx;');
  257. opts.indent_size = 4;
  258. opts.indent_char = ' ';
  259. opts.indent_with_tabs = false;
  260. opts.preserve_newlines = false;
  261. bt('var\na=dont_preserve_newlines;', 'var a = dont_preserve_newlines;');
  262. // make sure the blank line between function definitions stays
  263. // even when preserve_newlines = false
  264. bt('function foo() {\n return 1;\n}\n\nfunction foo() {\n return 1;\n}');
  265. bt('function foo() {\n return 1;\n}\nfunction foo() {\n return 1;\n}',
  266. 'function foo() {\n return 1;\n}\n\nfunction foo() {\n return 1;\n}'
  267. );
  268. bt('function foo() {\n return 1;\n}\n\n\nfunction foo() {\n return 1;\n}',
  269. 'function foo() {\n return 1;\n}\n\nfunction foo() {\n return 1;\n}'
  270. );
  271. opts.preserve_newlines = true;
  272. bt('var\na=do_preserve_newlines;', 'var\n a = do_preserve_newlines;');
  273. bt('// a\n// b\n\n// c\n// d');
  274. bt('if (foo) // comment\n{\n bar();\n}');
  275. opts.keep_array_indentation = false;
  276. bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f']",
  277. "a = ['a', 'b', 'c',\n 'd', 'e', 'f'\n]");
  278. bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f',\n 'g', 'h', 'i']",
  279. "a = ['a', 'b', 'c',\n 'd', 'e', 'f',\n 'g', 'h', 'i'\n]");
  280. bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f',\n 'g', 'h', 'i']",
  281. "a = ['a', 'b', 'c',\n 'd', 'e', 'f',\n 'g', 'h', 'i'\n]");
  282. bt('var x = [{}\n]', 'var x = [{}]');
  283. bt('var x = [{foo:bar}\n]', 'var x = [{\n foo: bar\n}]');
  284. bt("a = ['something',\n 'completely',\n 'different'];\nif (x);",
  285. "a = ['something',\n 'completely',\n 'different'\n];\nif (x);");
  286. bt("a = ['a','b','c']", "a = ['a', 'b', 'c']");
  287. bt("a = ['a', 'b','c']", "a = ['a', 'b', 'c']");
  288. bt("x = [{'a':0}]",
  289. "x = [{\n 'a': 0\n}]");
  290. bt('{a([[a1]], {b;});}',
  291. '{\n a([\n [a1]\n ], {\n b;\n });\n}');
  292. bt("a();\n [\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ].toString();",
  293. "a();\n[\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n].toString();");
  294. bt("a();\na = [\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ].toString();",
  295. "a();\na = [\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n].toString();");
  296. bt("function() {\n Foo([\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ]);\n}",
  297. "function() {\n Foo([\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ]);\n}");
  298. bt('function foo() {\n return [\n "one",\n "two"\n ];\n}');
  299. // 4 spaces per indent input, processed with 4-spaces per indent
  300. bt( "function foo() {\n" +
  301. " return [\n" +
  302. " {\n" +
  303. " one: 'x',\n" +
  304. " two: [\n" +
  305. " {\n" +
  306. " id: 'a',\n" +
  307. " name: 'apple'\n" +
  308. " }, {\n" +
  309. " id: 'b',\n" +
  310. " name: 'banana'\n" +
  311. " }\n" +
  312. " ]\n" +
  313. " }\n" +
  314. " ];\n" +
  315. "}",
  316. "function foo() {\n" +
  317. " return [{\n" +
  318. " one: 'x',\n" +
  319. " two: [{\n" +
  320. " id: 'a',\n" +
  321. " name: 'apple'\n" +
  322. " }, {\n" +
  323. " id: 'b',\n" +
  324. " name: 'banana'\n" +
  325. " }]\n" +
  326. " }];\n" +
  327. "}");
  328. // 3 spaces per indent input, processed with 4-spaces per indent
  329. bt( "function foo() {\n" +
  330. " return [\n" +
  331. " {\n" +
  332. " one: 'x',\n" +
  333. " two: [\n" +
  334. " {\n" +
  335. " id: 'a',\n" +
  336. " name: 'apple'\n" +
  337. " }, {\n" +
  338. " id: 'b',\n" +
  339. " name: 'banana'\n" +
  340. " }\n" +
  341. " ]\n" +
  342. " }\n" +
  343. " ];\n" +
  344. "}",
  345. "function foo() {\n" +
  346. " return [{\n" +
  347. " one: 'x',\n" +
  348. " two: [{\n" +
  349. " id: 'a',\n" +
  350. " name: 'apple'\n" +
  351. " }, {\n" +
  352. " id: 'b',\n" +
  353. " name: 'banana'\n" +
  354. " }]\n" +
  355. " }];\n" +
  356. "}");
  357. opts.keep_array_indentation = true;
  358. bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f']");
  359. bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f',\n 'g', 'h', 'i']");
  360. bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f',\n 'g', 'h', 'i']");
  361. bt('var x = [{}\n]', 'var x = [{}\n]');
  362. bt('var x = [{foo:bar}\n]', 'var x = [{\n foo: bar\n }\n]');
  363. bt("a = ['something',\n 'completely',\n 'different'];\nif (x);");
  364. bt("a = ['a','b','c']", "a = ['a', 'b', 'c']");
  365. bt("a = ['a', 'b','c']", "a = ['a', 'b', 'c']");
  366. bt("x = [{'a':0}]",
  367. "x = [{\n 'a': 0\n}]");
  368. bt('{a([[a1]], {b;});}',
  369. '{\n a([[a1]], {\n b;\n });\n}');
  370. bt("a();\n [\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ].toString();",
  371. "a();\n [\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ].toString();");
  372. bt("a();\na = [\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ].toString();",
  373. "a();\na = [\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ].toString();");
  374. bt("function() {\n Foo([\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ]);\n}",
  375. "function() {\n Foo([\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ]);\n}");
  376. bt('function foo() {\n return [\n "one",\n "two"\n ];\n}');
  377. // 4 spaces per indent input, processed with 4-spaces per indent
  378. bt( "function foo() {\n" +
  379. " return [\n" +
  380. " {\n" +
  381. " one: 'x',\n" +
  382. " two: [\n" +
  383. " {\n" +
  384. " id: 'a',\n" +
  385. " name: 'apple'\n" +
  386. " }, {\n" +
  387. " id: 'b',\n" +
  388. " name: 'banana'\n" +
  389. " }\n" +
  390. " ]\n" +
  391. " }\n" +
  392. " ];\n" +
  393. "}");
  394. // 3 spaces per indent input, processed with 4-spaces per indent
  395. // Should be unchanged, but is not - #445
  396. // bt( "function foo() {\n" +
  397. // " return [\n" +
  398. // " {\n" +
  399. // " one: 'x',\n" +
  400. // " two: [\n" +
  401. // " {\n" +
  402. // " id: 'a',\n" +
  403. // " name: 'apple'\n" +
  404. // " }, {\n" +
  405. // " id: 'b',\n" +
  406. // " name: 'banana'\n" +
  407. // " }\n" +
  408. // " ]\n" +
  409. // " }\n" +
  410. // " ];\n" +
  411. // "}");
  412. opts.keep_array_indentation = false;
  413. bt('a = //comment\n /regex/;');
  414. bt('if (a)\n{\nb;\n}\nelse\n{\nc;\n}', 'if (a) {\n b;\n} else {\n c;\n}');
  415. // tests for brace positioning
  416. beautify_brace_tests('expand');
  417. beautify_brace_tests('collapse');
  418. beautify_brace_tests('end-expand');
  419. beautify_brace_tests('none');
  420. bt('// func-comment\n\nfunction foo() {}\n\n// end-func-comment');
  421. test_fragment('roo = {\n /*\n ****\n FOO\n ****\n */\n BAR: 0\n};');
  422. bt('"foo""bar""baz"', '"foo"\n"bar"\n"baz"');
  423. bt("'foo''bar''baz'", "'foo'\n'bar'\n'baz'");
  424. test_fragment("if (zz) {\n // ....\n}\n(function");
  425. bt("{\n get foo() {}\n}");
  426. bt("{\n var a = get\n foo();\n}");
  427. bt("{\n set foo() {}\n}");
  428. bt("{\n var a = set\n foo();\n}");
  429. bt("var x = {\n get function()\n}");
  430. bt("var x = {\n set function()\n}");
  431. // According to my current research get/set have no special meaning outside of an object literal
  432. bt("var x = set\n\na() {}", "var x = set\n\na() {}");
  433. bt("var x = set\n\nfunction() {}", "var x = set\n\nfunction() {}");
  434. bt('<!-- foo\nbar();\n-->');
  435. bt('<!-- dont crash'); // -->
  436. bt('for () /abc/.test()');
  437. bt('if (k) /aaa/m.test(v) && l();');
  438. bt('switch (true) {\n case /swf/i.test(foo):\n bar();\n}');
  439. bt('createdAt = {\n type: Date,\n default: Date.now\n}');
  440. bt('switch (createdAt) {\n case a:\n Date,\n default:\n Date.now\n}');
  441. opts.space_before_conditional = false;
  442. bt('if(a) b()');
  443. opts.space_before_conditional = true;
  444. opts.preserve_newlines = true;
  445. bt('var a = 42; // foo\n\nvar b;');
  446. bt('var a = 42; // foo\n\n\nvar b;');
  447. bt("var a = 'foo' +\n 'bar';");
  448. bt("var a = \"foo\" +\n \"bar\";");
  449. bt('this.oa = new OAuth(\n' +
  450. ' _requestToken,\n' +
  451. ' _accessToken,\n' +
  452. ' consumer_key\n' +
  453. ');');
  454. opts.unescape_strings = false;
  455. test_fragment('"\\x22\\x27", \'\\x22\\x27\', "\\x5c", \'\\x5c\', "\\xff and \\xzz", "unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"');
  456. opts.unescape_strings = true;
  457. test_fragment('"\\x20\\x40\\x4a"', '" @J"');
  458. test_fragment('"\\xff\\x40\\x4a"');
  459. test_fragment('"\\u0072\\u016B\\u0137\\u012B\\u0074\\u0069\\u0073"', '"rūķītis"');
  460. test_fragment('"Google Chrome est\\u00E1 actualizado."', '"Google Chrome está actualizado."');
  461. /*
  462. bt('"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff and \\xzz","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"',
  463. '"\\"\'", \'"\\\'\', "\\\\", \'\\\\\', "\\xff and \\xzz", "unicode \\u0000 \\" \' \\\\ \\uffff \\uzzzz"');
  464. */
  465. opts.unescape_strings = false;
  466. bt('return function();');
  467. bt('var a = function();');
  468. bt('var a = 5 + function();');
  469. bt('3.*7;', '3. * 7;');
  470. bt('a = 1.e-64 * 0.5e+4 / 6e-23;');
  471. bt('import foo.*;', 'import foo.*;'); // actionscript's import
  472. test_fragment('function f(a: a, b: b)'); // actionscript
  473. bt('{\n foo // something\n ,\n bar // something\n baz\n}');
  474. bt('function a(a) {} function b(b) {} function c(c) {}', 'function a(a) {}\n\nfunction b(b) {}\n\nfunction c(c) {}');
  475. bt('foo(a, function() {})');
  476. bt('foo(a, /regex/)');
  477. bt('/* foo */\n"x"');
  478. opts.break_chained_methods = false;
  479. opts.preserve_newlines = false;
  480. bt('foo\n.bar()\n.baz().cucumber(fat)', 'foo.bar().baz().cucumber(fat)');
  481. bt('foo\n.bar()\n.baz().cucumber(fat); foo.bar().baz().cucumber(fat)', 'foo.bar().baz().cucumber(fat);\nfoo.bar().baz().cucumber(fat)');
  482. bt('foo\n.bar()\n.baz().cucumber(fat)\n foo.bar().baz().cucumber(fat)', 'foo.bar().baz().cucumber(fat)\nfoo.bar().baz().cucumber(fat)');
  483. bt('this\n.something = foo.bar()\n.baz().cucumber(fat)', 'this.something = foo.bar().baz().cucumber(fat)');
  484. bt('this.something.xxx = foo.moo.bar()');
  485. bt('this\n.something\n.xxx = foo.moo\n.bar()', 'this.something.xxx = foo.moo.bar()');
  486. opts.break_chained_methods = false;
  487. opts.preserve_newlines = true;
  488. bt('foo\n.bar()\n.baz().cucumber(fat)', 'foo\n .bar()\n .baz().cucumber(fat)');
  489. 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)');
  490. 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)');
  491. bt('this\n.something = foo.bar()\n.baz().cucumber(fat)', 'this\n .something = foo.bar()\n .baz().cucumber(fat)');
  492. bt('this.something.xxx = foo.moo.bar()');
  493. bt('this\n.something\n.xxx = foo.moo\n.bar()', 'this\n .something\n .xxx = foo.moo\n .bar()');
  494. opts.break_chained_methods = true;
  495. opts.preserve_newlines = false;
  496. bt('foo\n.bar()\n.baz().cucumber(fat)', 'foo.bar()\n .baz()\n .cucumber(fat)');
  497. 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)');
  498. 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)');
  499. bt('this\n.something = foo.bar()\n.baz().cucumber(fat)', 'this.something = foo.bar()\n .baz()\n .cucumber(fat)');
  500. bt('this.something.xxx = foo.moo.bar()');
  501. bt('this\n.something\n.xxx = foo.moo\n.bar()', 'this.something.xxx = foo.moo.bar()');
  502. opts.break_chained_methods = true;
  503. opts.preserve_newlines = true;
  504. bt('foo\n.bar()\n.baz().cucumber(fat)', 'foo\n .bar()\n .baz()\n .cucumber(fat)');
  505. 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)');
  506. 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)');
  507. bt('this\n.something = foo.bar()\n.baz().cucumber(fat)', 'this\n .something = foo.bar()\n .baz()\n .cucumber(fat)');
  508. bt('this.something.xxx = foo.moo.bar()');
  509. bt('this\n.something\n.xxx = foo.moo\n.bar()', 'this\n .something\n .xxx = foo.moo\n .bar()');
  510. opts.break_chained_methods = false;
  511. // Line wrap test intputs
  512. //.............---------1---------2---------3---------4---------5---------6---------7
  513. //.............1234567890123456789012345678901234567890123456789012345678901234567890
  514. wrap_input_1=('foo.bar().baz().cucumber((fat && "sassy") || (leans\n&& mean));\n' +
  515. 'Test_very_long_variable_name_this_should_never_wrap\n.but_this_can\n' +
  516. 'if (wraps_can_occur && inside_an_if_block) that_is_\n.okay();\n' +
  517. 'object_literal = {\n' +
  518. ' propertx: first_token + 12345678.99999E-6,\n' +
  519. ' property: first_token_should_never_wrap + but_this_can,\n' +
  520. ' propertz: first_token_should_never_wrap + !but_this_can,\n' +
  521. ' proper: "first_token_should_never_wrap" + "but_this_can"\n' +
  522. '}');
  523. //.............---------1---------2---------3---------4---------5---------6---------7
  524. //.............1234567890123456789012345678901234567890123456789012345678901234567890
  525. wrap_input_2=('{\n' +
  526. ' foo.bar().baz().cucumber((fat && "sassy") || (leans\n&& mean));\n' +
  527. ' Test_very_long_variable_name_this_should_never_wrap\n.but_this_can\n' +
  528. ' if (wraps_can_occur && inside_an_if_block) that_is_\n.okay();\n' +
  529. ' object_literal = {\n' +
  530. ' propertx: first_token + 12345678.99999E-6,\n' +
  531. ' property: first_token_should_never_wrap + but_this_can,\n' +
  532. ' propertz: first_token_should_never_wrap + !but_this_can,\n' +
  533. ' proper: "first_token_should_never_wrap" + "but_this_can"\n' +
  534. ' }' +
  535. '}');
  536. opts.preserve_newlines = false;
  537. opts.wrap_line_length = 0;
  538. //.............---------1---------2---------3---------4---------5---------6---------7
  539. //.............1234567890123456789012345678901234567890123456789012345678901234567890
  540. test_fragment(wrap_input_1,
  541. /* expected */
  542. 'foo.bar().baz().cucumber((fat && "sassy") || (leans && mean));\n' +
  543. 'Test_very_long_variable_name_this_should_never_wrap.but_this_can\n' +
  544. 'if (wraps_can_occur && inside_an_if_block) that_is_.okay();\n' +
  545. 'object_literal = {\n' +
  546. ' propertx: first_token + 12345678.99999E-6,\n' +
  547. ' property: first_token_should_never_wrap + but_this_can,\n' +
  548. ' propertz: first_token_should_never_wrap + !but_this_can,\n' +
  549. ' proper: "first_token_should_never_wrap" + "but_this_can"\n' +
  550. '}');
  551. opts.wrap_line_length = 70;
  552. //.............---------1---------2---------3---------4---------5---------6---------7
  553. //.............1234567890123456789012345678901234567890123456789012345678901234567890
  554. test_fragment(wrap_input_1,
  555. /* expected */
  556. 'foo.bar().baz().cucumber((fat && "sassy") || (leans && mean));\n' +
  557. 'Test_very_long_variable_name_this_should_never_wrap.but_this_can\n' +
  558. 'if (wraps_can_occur && inside_an_if_block) that_is_.okay();\n' +
  559. 'object_literal = {\n' +
  560. ' propertx: first_token + 12345678.99999E-6,\n' +
  561. ' property: first_token_should_never_wrap + but_this_can,\n' +
  562. ' propertz: first_token_should_never_wrap + !but_this_can,\n' +
  563. ' proper: "first_token_should_never_wrap" + "but_this_can"\n' +
  564. '}');
  565. opts.wrap_line_length = 40;
  566. //.............---------1---------2---------3---------4---------5---------6---------7
  567. //.............1234567890123456789012345678901234567890123456789012345678901234567890
  568. test_fragment(wrap_input_1,
  569. /* expected */
  570. 'foo.bar().baz().cucumber((fat &&\n' +
  571. ' "sassy") || (leans && mean));\n' +
  572. 'Test_very_long_variable_name_this_should_never_wrap\n' +
  573. ' .but_this_can\n' +
  574. 'if (wraps_can_occur &&\n' +
  575. ' inside_an_if_block) that_is_.okay();\n' +
  576. 'object_literal = {\n' +
  577. ' propertx: first_token +\n' +
  578. ' 12345678.99999E-6,\n' +
  579. ' property: first_token_should_never_wrap +\n' +
  580. ' but_this_can,\n' +
  581. ' propertz: first_token_should_never_wrap +\n' +
  582. ' !but_this_can,\n' +
  583. ' proper: "first_token_should_never_wrap" +\n' +
  584. ' "but_this_can"\n' +
  585. '}');
  586. opts.wrap_line_length = 41;
  587. // NOTE: wrap is only best effort - line continues until next wrap point is found.
  588. //.............---------1---------2---------3---------4---------5---------6---------7
  589. //.............1234567890123456789012345678901234567890123456789012345678901234567890
  590. test_fragment(wrap_input_1,
  591. /* expected */
  592. 'foo.bar().baz().cucumber((fat && "sassy") ||\n' +
  593. ' (leans && mean));\n' +
  594. 'Test_very_long_variable_name_this_should_never_wrap\n' +
  595. ' .but_this_can\n' +
  596. 'if (wraps_can_occur &&\n' +
  597. ' inside_an_if_block) that_is_.okay();\n' +
  598. 'object_literal = {\n' +
  599. ' propertx: first_token +\n' +
  600. ' 12345678.99999E-6,\n' +
  601. ' property: first_token_should_never_wrap +\n' +
  602. ' but_this_can,\n' +
  603. ' propertz: first_token_should_never_wrap +\n' +
  604. ' !but_this_can,\n' +
  605. ' proper: "first_token_should_never_wrap" +\n' +
  606. ' "but_this_can"\n' +
  607. '}');
  608. opts.wrap_line_length = 45;
  609. // NOTE: wrap is only best effort - line continues until next wrap point is found.
  610. //.............---------1---------2---------3---------4---------5---------6---------7
  611. //.............1234567890123456789012345678901234567890123456789012345678901234567890
  612. test_fragment(wrap_input_2,
  613. /* expected */
  614. '{\n' +
  615. ' foo.bar().baz().cucumber((fat && "sassy") ||\n' +
  616. ' (leans && mean));\n' +
  617. ' Test_very_long_variable_name_this_should_never_wrap\n' +
  618. ' .but_this_can\n' +
  619. ' if (wraps_can_occur &&\n' +
  620. ' inside_an_if_block) that_is_.okay();\n' +
  621. ' object_literal = {\n' +
  622. ' propertx: first_token +\n' +
  623. ' 12345678.99999E-6,\n' +
  624. ' property: first_token_should_never_wrap +\n' +
  625. ' but_this_can,\n' +
  626. ' propertz: first_token_should_never_wrap +\n' +
  627. ' !but_this_can,\n' +
  628. ' proper: "first_token_should_never_wrap" +\n' +
  629. ' "but_this_can"\n' +
  630. ' }\n'+
  631. '}');
  632. opts.preserve_newlines = true;
  633. opts.wrap_line_length = 0;
  634. //.............---------1---------2---------3---------4---------5---------6---------7
  635. //.............1234567890123456789012345678901234567890123456789012345678901234567890
  636. test_fragment(wrap_input_1,
  637. /* expected */
  638. 'foo.bar().baz().cucumber((fat && "sassy") || (leans && mean));\n' +
  639. 'Test_very_long_variable_name_this_should_never_wrap\n' +
  640. ' .but_this_can\n' +
  641. 'if (wraps_can_occur && inside_an_if_block) that_is_\n' +
  642. ' .okay();\n' +
  643. 'object_literal = {\n' +
  644. ' propertx: first_token + 12345678.99999E-6,\n' +
  645. ' property: first_token_should_never_wrap + but_this_can,\n' +
  646. ' propertz: first_token_should_never_wrap + !but_this_can,\n' +
  647. ' proper: "first_token_should_never_wrap" + "but_this_can"\n' +
  648. '}');
  649. opts.wrap_line_length = 70;
  650. //.............---------1---------2---------3---------4---------5---------6---------7
  651. //.............1234567890123456789012345678901234567890123456789012345678901234567890
  652. test_fragment(wrap_input_1,
  653. /* expected */
  654. 'foo.bar().baz().cucumber((fat && "sassy") || (leans && mean));\n' +
  655. 'Test_very_long_variable_name_this_should_never_wrap\n' +
  656. ' .but_this_can\n' +
  657. 'if (wraps_can_occur && inside_an_if_block) that_is_\n' +
  658. ' .okay();\n' +
  659. 'object_literal = {\n' +
  660. ' propertx: first_token + 12345678.99999E-6,\n' +
  661. ' property: first_token_should_never_wrap + but_this_can,\n' +
  662. ' propertz: first_token_should_never_wrap + !but_this_can,\n' +
  663. ' proper: "first_token_should_never_wrap" + "but_this_can"\n' +
  664. '}');
  665. opts.wrap_line_length = 40;
  666. //.............---------1---------2---------3---------4---------5---------6---------7
  667. //.............1234567890123456789012345678901234567890123456789012345678901234567890
  668. test_fragment(wrap_input_1,
  669. /* expected */
  670. 'foo.bar().baz().cucumber((fat &&\n' +
  671. ' "sassy") || (leans && mean));\n' +
  672. 'Test_very_long_variable_name_this_should_never_wrap\n' +
  673. ' .but_this_can\n' +
  674. 'if (wraps_can_occur &&\n' +
  675. ' inside_an_if_block) that_is_\n' +
  676. ' .okay();\n' +
  677. 'object_literal = {\n' +
  678. ' propertx: first_token +\n' +
  679. ' 12345678.99999E-6,\n' +
  680. ' property: first_token_should_never_wrap +\n' +
  681. ' but_this_can,\n' +
  682. ' propertz: first_token_should_never_wrap +\n' +
  683. ' !but_this_can,\n' +
  684. ' proper: "first_token_should_never_wrap" +\n' +
  685. ' "but_this_can"\n' +
  686. '}');
  687. opts.wrap_line_length = 41;
  688. // NOTE: wrap is only best effort - line continues until next wrap point is found.
  689. //.............---------1---------2---------3---------4---------5---------6---------7
  690. //.............1234567890123456789012345678901234567890123456789012345678901234567890
  691. test_fragment(wrap_input_1,
  692. /* expected */
  693. 'foo.bar().baz().cucumber((fat && "sassy") ||\n' +
  694. ' (leans && mean));\n' +
  695. 'Test_very_long_variable_name_this_should_never_wrap\n' +
  696. ' .but_this_can\n' +
  697. 'if (wraps_can_occur &&\n' +
  698. ' inside_an_if_block) that_is_\n' +
  699. ' .okay();\n' +
  700. 'object_literal = {\n' +
  701. ' propertx: first_token +\n' +
  702. ' 12345678.99999E-6,\n' +
  703. ' property: first_token_should_never_wrap +\n' +
  704. ' but_this_can,\n' +
  705. ' propertz: first_token_should_never_wrap +\n' +
  706. ' !but_this_can,\n' +
  707. ' proper: "first_token_should_never_wrap" +\n' +
  708. ' "but_this_can"\n' +
  709. '}');
  710. opts.wrap_line_length = 45;
  711. // NOTE: wrap is only best effort - line continues until next wrap point is found.
  712. //.............---------1---------2---------3---------4---------5---------6---------7
  713. //.............1234567890123456789012345678901234567890123456789012345678901234567890
  714. test_fragment(wrap_input_2,
  715. /* expected */
  716. '{\n' +
  717. ' foo.bar().baz().cucumber((fat && "sassy") ||\n' +
  718. ' (leans && mean));\n' +
  719. ' Test_very_long_variable_name_this_should_never_wrap\n' +
  720. ' .but_this_can\n' +
  721. ' if (wraps_can_occur &&\n' +
  722. ' inside_an_if_block) that_is_\n' +
  723. ' .okay();\n' +
  724. ' object_literal = {\n' +
  725. ' propertx: first_token +\n' +
  726. ' 12345678.99999E-6,\n' +
  727. ' property: first_token_should_never_wrap +\n' +
  728. ' but_this_can,\n' +
  729. ' propertz: first_token_should_never_wrap +\n' +
  730. ' !but_this_can,\n' +
  731. ' proper: "first_token_should_never_wrap" +\n' +
  732. ' "but_this_can"\n' +
  733. ' }\n'+
  734. '}');
  735. opts.wrap_line_length = 0;
  736. opts.preserve_newlines = false;
  737. bt('if (foo) // comment\n bar();');
  738. bt('if (foo) // comment\n (bar());');
  739. bt('if (foo) // comment\n (bar());');
  740. bt('if (foo) // comment\n /asdf/;');
  741. bt('this.oa = new OAuth(\n' +
  742. ' _requestToken,\n' +
  743. ' _accessToken,\n' +
  744. ' consumer_key\n' +
  745. ');',
  746. 'this.oa = new OAuth(_requestToken, _accessToken, consumer_key);');
  747. bt('foo = {\n x: y, // #44\n w: z // #44\n}');
  748. bt('switch (x) {\n case "a":\n // comment on newline\n break;\n case "b": // comment on same line\n break;\n}');
  749. bt('this.type =\n this.options =\n // comment\n this.enabled null;',
  750. 'this.type = this.options =\n // comment\n this.enabled null;');
  751. bt('someObj\n .someFunc1()\n // This comment should not break the indent\n .someFunc2();',
  752. 'someObj.someFunc1()\n // This comment should not break the indent\n .someFunc2();');
  753. bt('if (true ||\n!true) return;', 'if (true || !true) return;');
  754. // these aren't ready yet.
  755. //bt('if (foo) // comment\n bar() /*i*/ + baz() /*j\n*/ + asdf();');
  756. bt('if\n(foo)\nif\n(bar)\nif\n(baz)\nwhee();\na();',
  757. 'if (foo)\n if (bar)\n if (baz) whee();\na();');
  758. bt('if\n(foo)\nif\n(bar)\nif\n(baz)\nwhee();\nelse\na();',
  759. 'if (foo)\n if (bar)\n if (baz) whee();\n else a();');
  760. bt('if (foo)\nbar();\nelse\ncar();',
  761. 'if (foo) bar();\nelse car();');
  762. bt('if (foo) if (bar) if (baz);\na();',
  763. 'if (foo)\n if (bar)\n if (baz);\na();');
  764. bt('if (foo) if (bar) if (baz) whee();\na();',
  765. 'if (foo)\n if (bar)\n if (baz) whee();\na();');
  766. bt('if (foo) a()\nif (bar) if (baz) whee();\na();',
  767. 'if (foo) a()\nif (bar)\n if (baz) whee();\na();');
  768. bt('if (foo);\nif (bar) if (baz) whee();\na();',
  769. 'if (foo);\nif (bar)\n if (baz) whee();\na();');
  770. bt('if (options)\n' +
  771. ' for (var p in options)\n' +
  772. ' this[p] = options[p];',
  773. 'if (options)\n'+
  774. ' for (var p in options) this[p] = options[p];');
  775. bt('if (options) for (var p in options) this[p] = options[p];',
  776. 'if (options)\n for (var p in options) this[p] = options[p];');
  777. bt('if (options) do q(); while (b());',
  778. 'if (options)\n do q(); while (b());');
  779. bt('if (options) while (b()) q();',
  780. 'if (options)\n while (b()) q();');
  781. bt('if (options) do while (b()) q(); while (a());',
  782. 'if (options)\n do\n while (b()) q(); while (a());');
  783. bt('function f(a, b, c,\nd, e) {}',
  784. 'function f(a, b, c, d, e) {}');
  785. bt('function f(a,b) {if(a) b()}function g(a,b) {if(!a) b()}',
  786. 'function f(a, b) {\n if (a) b()\n}\n\nfunction g(a, b) {\n if (!a) b()\n}');
  787. bt('function f(a,b) {if(a) b()}\n\n\n\nfunction g(a,b) {if(!a) b()}',
  788. 'function f(a, b) {\n if (a) b()\n}\n\nfunction g(a, b) {\n if (!a) b()\n}');
  789. // This is not valid syntax, but still want to behave reasonably and not side-effect
  790. bt('(if(a) b())(if(a) b())',
  791. '(\n if (a) b())(\n if (a) b())');
  792. bt('(if(a) b())\n\n\n(if(a) b())',
  793. '(\n if (a) b())\n(\n if (a) b())');
  794. bt("if\n(a)\nb();", "if (a) b();");
  795. bt('var a =\nfoo', 'var a = foo');
  796. bt('var a = {\n"a":1,\n"b":2}', "var a = {\n \"a\": 1,\n \"b\": 2\n}");
  797. bt("var a = {\n'a':1,\n'b':2}", "var a = {\n 'a': 1,\n 'b': 2\n}");
  798. bt('var a = /*i*/ "b";');
  799. bt('var a = /*i*/\n"b";', 'var a = /*i*/ "b";');
  800. bt('var a = /*i*/\nb;', 'var a = /*i*/ b;');
  801. bt('{\n\n\n"x"\n}', '{\n "x"\n}');
  802. bt('if(a &&\nb\n||\nc\n||d\n&&\ne) e = f', 'if (a && b || c || d && e) e = f');
  803. bt('if(a &&\n(b\n||\nc\n||d)\n&&\ne) e = f', 'if (a && (b || c || d) && e) e = f');
  804. test_fragment('\n\n"x"', '"x"');
  805. 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;',
  806. 'a = 1;\nb = 2;');
  807. opts.preserve_newlines = true;
  808. bt('if (foo) // comment\n bar();');
  809. bt('if (foo) // comment\n (bar());');
  810. bt('if (foo) // comment\n (bar());');
  811. bt('if (foo) // comment\n /asdf/;');
  812. bt('foo = {\n x: y, // #44\n w: z // #44\n}');
  813. bt('switch (x) {\n case "a":\n // comment on newline\n break;\n case "b": // comment on same line\n break;\n}');
  814. bt('this.type =\n this.options =\n // comment\n this.enabled null;');
  815. bt('someObj\n .someFunc1()\n // This comment should not break the indent\n .someFunc2();');
  816. bt('if (true ||\n!true) return;', 'if (true ||\n !true) return;');
  817. // these aren't ready yet.
  818. // bt('if (foo) // comment\n bar() /*i*/ + baz() /*j\n*/ + asdf();');
  819. bt('if\n(foo)\nif\n(bar)\nif\n(baz)\nwhee();\na();',
  820. 'if (foo)\n if (bar)\n if (baz)\n whee();\na();');
  821. bt('if\n(foo)\nif\n(bar)\nif\n(baz)\nwhee();\nelse\na();',
  822. 'if (foo)\n if (bar)\n if (baz)\n whee();\n else\n a();');
  823. bt('if (foo) bar();\nelse\ncar();',
  824. 'if (foo) bar();\nelse\n car();');
  825. bt('if (foo) if (bar) if (baz);\na();',
  826. 'if (foo)\n if (bar)\n if (baz);\na();');
  827. bt('if (foo) if (bar) if (baz) whee();\na();',
  828. 'if (foo)\n if (bar)\n if (baz) whee();\na();');
  829. bt('if (foo) a()\nif (bar) if (baz) whee();\na();',
  830. 'if (foo) a()\nif (bar)\n if (baz) whee();\na();');
  831. bt('if (foo);\nif (bar) if (baz) whee();\na();',
  832. 'if (foo);\nif (bar)\n if (baz) whee();\na();');
  833. bt('if (options)\n' +
  834. ' for (var p in options)\n' +
  835. ' this[p] = options[p];');
  836. bt('if (options) for (var p in options) this[p] = options[p];',
  837. 'if (options)\n for (var p in options) this[p] = options[p];');
  838. bt('if (options) do q(); while (b());',
  839. 'if (options)\n do q(); while (b());');
  840. bt('if (options) do; while (b());',
  841. 'if (options)\n do; while (b());');
  842. bt('if (options) while (b()) q();',
  843. 'if (options)\n while (b()) q();');
  844. bt('if (options) do while (b()) q(); while (a());',
  845. 'if (options)\n do\n while (b()) q(); while (a());');
  846. bt('function f(a, b, c,\nd, e) {}',
  847. 'function f(a, b, c,\n d, e) {}');
  848. bt('function f(a,b) {if(a) b()}function g(a,b) {if(!a) b()}',
  849. 'function f(a, b) {\n if (a) b()\n}\n\nfunction g(a, b) {\n if (!a) b()\n}');
  850. bt('function f(a,b) {if(a) b()}\n\n\n\nfunction g(a,b) {if(!a) b()}',
  851. 'function f(a, b) {\n if (a) b()\n}\n\n\n\nfunction g(a, b) {\n if (!a) b()\n}');
  852. // This is not valid syntax, but still want to behave reasonably and not side-effect
  853. bt('(if(a) b())(if(a) b())',
  854. '(\n if (a) b())(\n if (a) b())');
  855. bt('(if(a) b())\n\n\n(if(a) b())',
  856. '(\n if (a) b())\n\n\n(\n if (a) b())');
  857. // space between functions
  858. bt('/*\n * foo\n */\nfunction foo() {}');
  859. bt('// a nice function\nfunction foo() {}');
  860. bt('function foo() {}\nfunction foo() {}',
  861. 'function foo() {}\n\nfunction foo() {}'
  862. );
  863. bt('[\n function() {}\n]');
  864. bt("if\n(a)\nb();", "if (a)\n b();");
  865. bt('var a =\nfoo', 'var a =\n foo');
  866. bt('var a = {\n"a":1,\n"b":2}', "var a = {\n \"a\": 1,\n \"b\": 2\n}");
  867. bt("var a = {\n'a':1,\n'b':2}", "var a = {\n 'a': 1,\n 'b': 2\n}");
  868. bt('var a = /*i*/ "b";');
  869. bt('var a = /*i*/\n"b";', 'var a = /*i*/\n "b";');
  870. bt('var a = /*i*/\nb;', 'var a = /*i*/\n b;');
  871. bt('{\n\n\n"x"\n}', '{\n\n\n "x"\n}');
  872. bt('if(a &&\nb\n||\nc\n||d\n&&\ne) e = f', 'if (a &&\n b ||\n c || d &&\n e) e = f');
  873. bt('if(a &&\n(b\n||\nc\n||d)\n&&\ne) e = f', 'if (a &&\n (b ||\n c || d) &&\n e) e = f');
  874. test_fragment('\n\n"x"', '"x"');
  875. // this beavior differs between js and python, defaults to unlimited in js, 10 in python
  876. 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;',
  877. '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;');
  878. opts.max_preserve_newlines = 8;
  879. 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;',
  880. 'a = 1;\n\n\n\n\n\n\n\nb = 2;');
  881. // Test the option to have spaces within parens
  882. opts.space_in_paren = false;
  883. bt('if(p) foo(a,b)', 'if (p) foo(a, b)');
  884. bt('try{while(true){willThrow()}}catch(result)switch(result){case 1:++result }',
  885. 'try {\n while (true) {\n willThrow()\n }\n} catch (result) switch (result) {\n case 1:\n ++result\n}');
  886. bt('((e/((a+(b)*c)-d))^2)*5;', '((e / ((a + (b) * c) - d)) ^ 2) * 5;');
  887. bt('function f(a,b) {if(a) b()}function g(a,b) {if(!a) b()}',
  888. 'function f(a, b) {\n if (a) b()\n}\n\nfunction g(a, b) {\n if (!a) b()\n}');
  889. bt('a=[];',
  890. 'a = [];');
  891. bt('a=[b,c,d];',
  892. 'a = [b, c, d];');
  893. bt('a= f[b];',
  894. 'a = f[b];');
  895. opts.space_in_paren = true;
  896. bt('if(p) foo(a,b)', 'if ( p ) foo( a, b )');
  897. bt('try{while(true){willThrow()}}catch(result)switch(result){case 1:++result }',
  898. 'try {\n while ( true ) {\n willThrow()\n }\n} catch ( result ) switch ( result ) {\n case 1:\n ++result\n}');
  899. bt('((e/((a+(b)*c)-d))^2)*5;', '( ( e / ( ( a + ( b ) * c ) - d ) ) ^ 2 ) * 5;');
  900. bt('function f(a,b) {if(a) b()}function g(a,b) {if(!a) b()}',
  901. 'function f( a, b ) {\n if ( a ) b()\n}\n\nfunction g( a, b ) {\n if ( !a ) b()\n}');
  902. bt('a=[];',
  903. 'a = [];');
  904. bt('a=[b,c,d];',
  905. 'a = [ b, c, d ];');
  906. bt('a= f[b];',
  907. 'a = f[ b ];');
  908. opts.space_in_empty_paren = true;
  909. bt('if(p) foo(a,b)', 'if ( p ) foo( a, b )');
  910. bt('try{while(true){willThrow()}}catch(result)switch(result){case 1:++result }',
  911. 'try {\n while ( true ) {\n willThrow( )\n }\n} catch ( result ) switch ( result ) {\n case 1:\n ++result\n}');
  912. bt('((e/((a+(b)*c)-d))^2)*5;', '( ( e / ( ( a + ( b ) * c ) - d ) ) ^ 2 ) * 5;');
  913. bt('function f(a,b) {if(a) b()}function g(a,b) {if(!a) b()}',
  914. 'function f( a, b ) {\n if ( a ) b( )\n}\n\nfunction g( a, b ) {\n if ( !a ) b( )\n}');
  915. bt('a=[];',
  916. 'a = [ ];');
  917. bt('a=[b,c,d];',
  918. 'a = [ b, c, d ];');
  919. bt('a= f[b];',
  920. 'a = f[ b ];');
  921. opts.space_in_empty_paren = false;
  922. opts.space_in_paren = false;
  923. // Test template strings
  924. bt('`This is a ${template} string.`', '`This is a ${template} string.`');
  925. bt('`This\n is\n a\n ${template}\n string.`', '`This\n is\n a\n ${template}\n string.`');
  926. bt('a = `This is a continuation\\\nstring.`', 'a = `This is a continuation\\\nstring.`');
  927. bt('a = "This is a continuation\\\nstring."', 'a = "This is a continuation\\\nstring."');
  928. Urlencoded.run_tests(sanitytest);
  929. }
  930. beautifier_tests();
  931. }
  932. if (typeof exports !== "undefined") {
  933. exports.run_javascript_tests = run_javascript_tests;
  934. }