Skip to content

Commit

Permalink
Function declarations inside of expressions and arrays should behave …
Browse files Browse the repository at this point in the history
…the same

Fixes #485
  • Loading branch information
bitwiseman committed Sep 27, 2014
1 parent 6448f89 commit e0e725b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
4 changes: 2 additions & 2 deletions js/lib/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@
} else if (last_type === 'TK_OPERATOR' || flags.last_text === '=') {
// foo = function
output.space_before_token = true;
} else if (is_expression(flags.mode)) {
} else if (!flags.multiline_frame && (is_expression(flags.mode) || is_array(flags.mode))) {
// (function
} else {
print_newline();
Expand Down Expand Up @@ -915,7 +915,7 @@
} else if (current_token.type === 'TK_RESERVED' && in_array(current_token.text, Tokenizer.line_starters) && flags.last_text !== ')') {
print_newline();
}
} else if (is_array(flags.mode) && flags.last_text === ',' && last_last_text === '}') {
} else if (flags.multiline_frame && is_array(flags.mode) && flags.last_text === ',' && last_last_text === '}') {
print_newline(); // }, in lists get a newline treatment
} else if (prefix === 'SPACE') {
output.space_before_token = true;
Expand Down
21 changes: 20 additions & 1 deletion js/test/beautify-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
bt('a=[[1,2],[4,5],function(){},[7,8]]',
"a = [\n [1, 2],\n [4, 5],\n function() {},\n [7, 8]\n]");
bt('a=[b,c,function(){},function(){},d]',
"a = [b, c, function() {}, function() {}, d]");
bt('a=[b,c,\nfunction(){},function(){},d]',
"a = [b, c,\n function() {},\n function() {},\n d\n]");
bt('a=[a[1],b[4],c[d[7]]]', "a = [a[1], b[4], c[d[7]]]");
bt('[1,2,[3,4,[5,6],7],8]', "[1, 2, [3, 4, [5, 6], 7], 8]");
Expand Down Expand Up @@ -499,7 +501,8 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,

bt('function* x() {\n yield 1;\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 "b": 99\n }, {\n "a": 11\n }\n ]\n}');
bt('{"x":[{"a":1,"b":3},\n7,8,8,8,8,{"b":99},{"a":11}]}', '{\n "x": [{\n "a": 1,\n "b": 3\n },\n 7, 8, 8, 8, 8, {\n "b": 99\n }, {\n "a": 11\n }\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 }, 7, 8, 8, 8, 8, {\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}');
Expand Down Expand Up @@ -1702,6 +1705,22 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
bt('var c = "_ACTION_TO_NATIVEAPI_" + ++g++ + +new Date;');
bt('var c = "_ACTION_TO_NATIVEAPI_" - --g-- - -new Date;');
// END tests for issue 514

// START tests for issue 485
// ensure function declarations behave the same in arrays as elsewhere
bt( 'var v = ["a",\n' +
' function() {\n' +
' return;\n' +
' }, {\n' +
' id: 1\n' +
' }\n' +
'];');
bt( 'var v = ["a", function() {\n' +
' return;\n' +
'}, {\n' +
' id: 1\n' +
'}];');
// END tests for issue 485

bt('var a=1,b={bang:2},c=3;',
'var a = 1,\n b = {\n bang: 2\n },\n c = 3;');
Expand Down
4 changes: 2 additions & 2 deletions python/jsbeautifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ def handle_word(self, current_token):
elif self.last_type == 'TK_OPERATOR' or self.flags.last_text == '=':
# foo = function
self.output.space_before_token = True
elif self.is_expression(self.flags.mode):
elif not self.flags.multiline_frame and (self.is_expression(self.flags.mode) or self.is_array(self.flags.mode)):
# (function
pass
else:
Expand Down Expand Up @@ -856,7 +856,7 @@ def handle_word(self, current_token):
self.print_newline()
elif current_token.type == 'TK_RESERVED' and current_token.text in Tokenizer.line_starters and self.flags.last_text != ')':
self.print_newline()
elif self.is_array(self.flags.mode) and self.flags.last_text == ',' and self.last_last_text == '}':
elif self.flags.multiline_frame and self.is_array(self.flags.mode) and self.flags.last_text == ',' and self.last_last_text == '}':
self.print_newline() # }, in lists get a newline
elif prefix == 'SPACE':
self.output.space_before_token = True
Expand Down
21 changes: 20 additions & 1 deletion python/jsbeautifier/tests/testjsbeautifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ def test_beautifier(self):
bt('a=[[1,2],[4,5],function(){},[7,8]]',
"a = [\n [1, 2],\n [4, 5],\n function() {},\n [7, 8]\n]");
bt('a=[b,c,function(){},function(){},d]',
"a = [b, c, function() {}, function() {}, d]");
bt('a=[b,c,\nfunction(){},function(){},d]',
"a = [b, c,\n function() {},\n function() {},\n d\n]");
bt('a=[a[1],b[4],c[d[7]]]', "a = [a[1], b[4], c[d[7]]]");
bt('[1,2,[3,4,[5,6],7],8]', "[1, 2, [3, 4, [5, 6], 7], 8]");
Expand Down Expand Up @@ -392,7 +394,8 @@ def test_beautifier(self):
bt('function*() {\n yield 1;\n}');
bt('function* x() {\n yield 1;\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 "b": 99\n }, {\n "a": 11\n }\n ]\n}');
bt('{"x":[{"a":1,"b":3},\n7,8,8,8,8,{"b":99},{"a":11}]}', '{\n "x": [{\n "a": 1,\n "b": 3\n },\n 7, 8, 8, 8, 8, {\n "b": 99\n }, {\n "a": 11\n }\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 }, 7, 8, 8, 8, 8, {\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}');
Expand Down Expand Up @@ -1573,6 +1576,22 @@ def test_beautifier(self):
bt('var c = "_ACTION_TO_NATIVEAPI_" - --g-- - -new Date;');
# END tests for issue 514

# START tests for issue 485
# ensure function declarations behave the same in arrays as elsewhere
bt( 'var v = ["a",\n' +
' function() {\n' +
' return;\n' +
' }, {\n' +
' id: 1\n' +
' }\n' +
'];');
bt( 'var v = ["a", function() {\n' +
' return;\n' +
'}, {\n' +
' id: 1\n' +
'}];');
# END tests for issue 485

bt('var a=1,b={bang:2},c=3;',
'var a = 1,\n b = {\n bang: 2\n },\n c = 3;');
bt('var a={bing:1},b=2,c=3;',
Expand Down

2 comments on commit e0e725b

@einars
Copy link
Contributor

@einars einars commented on e0e725b Sep 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(re:past commits) nifty bunch of changes you've got there! 👯
bugs: 📉

@bitwiseman
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 😄

Please sign in to comment.