From e0e725bc94df1776b0e41cc78add70fb1256cf65 Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Sat, 27 Sep 2014 01:41:06 -0700 Subject: [PATCH] Function declarations inside of expressions and arrays should behave the same Fixes #485 --- js/lib/beautify.js | 4 ++-- js/test/beautify-tests.js | 21 ++++++++++++++++++- python/jsbeautifier/__init__.py | 4 ++-- python/jsbeautifier/tests/testjsbeautifier.py | 21 ++++++++++++++++++- 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/js/lib/beautify.js b/js/lib/beautify.js index f80ace430..6279799e3 100644 --- a/js/lib/beautify.js +++ b/js/lib/beautify.js @@ -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(); @@ -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; diff --git a/js/test/beautify-tests.js b/js/test/beautify-tests.js index cd34ca439..3f4003ac1 100755 --- a/js/test/beautify-tests.js +++ b/js/test/beautify-tests.js @@ -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]"); @@ -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}'); @@ -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;'); diff --git a/python/jsbeautifier/__init__.py b/python/jsbeautifier/__init__.py index 19197047a..63a221ace 100644 --- a/python/jsbeautifier/__init__.py +++ b/python/jsbeautifier/__init__.py @@ -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: @@ -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 diff --git a/python/jsbeautifier/tests/testjsbeautifier.py b/python/jsbeautifier/tests/testjsbeautifier.py index ef9d0e691..9fa1f6919 100644 --- a/python/jsbeautifier/tests/testjsbeautifier.py +++ b/python/jsbeautifier/tests/testjsbeautifier.py @@ -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]"); @@ -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}'); @@ -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;',