From 16c1f228ac6792581dfe5399e33c30887bc3e0a3 Mon Sep 17 00:00:00 2001 From: Einar Lielmanis Date: Mon, 31 Jan 2011 22:20:50 +0200 Subject: [PATCH] Better format objects in arrays Was: var o = [{ fu: 'bar' }, { bar: 'fu' }]; Is: var o = [{ fu: 'bar' }, { bar: 'fu' }]; --- beautify.js | 7 ++++++- tests/beautify-tests.js | 6 ++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/beautify.js b/beautify.js index 5a887cf8b..fe58d5360 100644 --- a/beautify.js +++ b/beautify.js @@ -726,7 +726,12 @@ function js_beautify(js_source_text, options) { } else { // if TK_OPERATOR or TK_START_EXPR if (is_array(flags.previous_mode) && last_text === ',') { - print_newline(); // [a, b, c, { + if (last_last_text === '}') { + // }, { in array context + print_single_space(); + } else { + print_newline(); // [a, b, c, { + } } } indent(); diff --git a/tests/beautify-tests.js b/tests/beautify-tests.js index 7ba03500c..318227d8c 100644 --- a/tests/beautify-tests.js +++ b/tests/beautify-tests.js @@ -157,6 +157,8 @@ function run_beautifier_tests(test_obj) bt("a = 1; // comment\n", "a = 1; // comment"); bt("a = 1;\n // comment\n", "a = 1;\n// comment"); + bt('o = [{a:b},{c:d}]', 'o = [{\n a: b\n}, {\n c: d\n}]'); + bt("if (a) {\n do();\n}"); // was: extra space appended bt("if\n(a)\nb();", "if (a) b();"); // test for proper newline removal @@ -206,7 +208,7 @@ function run_beautifier_tests(test_obj) bt('{a:#1#}', '{\n a: #1#\n}'); test_fragment('{a:1},{a:2}', '{\n a: 1\n}, {\n a: 2\n}'); - test_fragment('var ary=[{a:1}, {a:2}];', 'var ary = [{\n a: 1\n},\n{\n a: 2\n}];'); + test_fragment('var ary=[{a:1}, {a:2}];', 'var ary = [{\n a: 1\n}, {\n a: 2\n}];'); test_fragment('{a:#1', '{\n a: #1'); // incomplete test_fragment('{a:#', '{\n a: #'); // incomplete @@ -306,7 +308,7 @@ function run_beautifier_tests(test_obj) bt("var a2, b2, c2, d2 = 0, c = function() {}, d = '';", "var a2, b2, c2, d2 = 0,\n c = function() {},\n d = '';"); bt('var o2=$.extend(a);function(){alert(x);}', 'var o2 = $.extend(a);\n\nfunction() {\n alert(x);\n}'); - bt('{"x":[{"a":1,"b":3},7,8,8,8,8,{"b":99},{"a":11}]}', '{\n "x": [{\n "a": 1,\n "b": 3\n },\n 7, 8, 8, 8, 8,\n {\n "b": 99\n },\n {\n "a": 11\n }]\n}'); + bt('{"x":[{"a":1,"b":3},7,8,8,8,8,{"b":99},{"a":11}]}', '{\n "x": [{\n "a": 1,\n "b": 3\n },\n 7, 8, 8, 8, 8,\n {\n "b": 99\n }, {\n "a": 11\n }]\n}'); bt('{"1":{"1a":"1b"},"2"}', '{\n "1": {\n "1a": "1b"\n },\n "2"\n}'); bt('{a:{a:b},c}', '{\n a: {\n a: b\n },\n c\n}');