mirror of
https://github.com/beautifier/js-beautify.git
synced 2025-02-18 20:28:59 +00:00
Better format objects in arrays
Was: var o = [{ fu: 'bar' }, { bar: 'fu' }]; Is: var o = [{ fu: 'bar' }, { bar: 'fu' }];
This commit is contained in:
parent
f614cc40ad
commit
16c1f228ac
@ -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();
|
||||
|
@ -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}');
|
||||
|
Loading…
x
Reference in New Issue
Block a user