From 6d0ad2b0388aa2f086e74d3c7ab9a5a3b4c76c8b Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Fri, 30 Dec 2016 00:20:52 -0800 Subject: [PATCH] Fixed function declaration with wrong blank line after operator Fixes #1085 --- js/lib/beautify.js | 3 ++- js/test/generated/beautify-javascript-tests.js | 9 +++++++++ python/jsbeautifier/__init__.py | 3 ++- python/jsbeautifier/tests/generated/tests.py | 9 +++++++++ test/data/javascript/tests.js | 11 +++++++++++ 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/js/lib/beautify.js b/js/lib/beautify.js index eafe45e50..84a78cc4d 100644 --- a/js/lib/beautify.js +++ b/js/lib/beautify.js @@ -1052,7 +1052,8 @@ if (!Object.values) { } if (current_token.type === 'TK_RESERVED' && current_token.text === 'function') { - if (in_array(flags.last_text, ['}', ';']) || (output.just_added_newline() && !in_array(flags.last_text, ['(', '[', '{', ':', '=', ',']))) { + if (in_array(flags.last_text, ['}', ';']) || + (output.just_added_newline() && !(in_array(flags.last_text, ['(', '[', '{', ':', '=', ',']) || last_type === 'TK_OPERATOR'))) { // make sure there is a nice clean space of at least one blank line // before a new function definition if (!output.just_added_blankline() && !current_token.comments_before.length) { diff --git a/js/test/generated/beautify-javascript-tests.js b/js/test/generated/beautify-javascript-tests.js index 5b42f2910..3321d7817 100644 --- a/js/test/generated/beautify-javascript-tests.js +++ b/js/test/generated/beautify-javascript-tests.js @@ -2695,6 +2695,15 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify, ' console.log(i);\n' + '// all done\n' + 'console.log("done");'); + + // Issue #1085 - function should not have blank line in a number of cases + bt( + 'var transformer =\n' + + ' options.transformer ||\n' + + ' globalSettings.transformer ||\n' + + ' function(x) {\n' + + ' return x;\n' + + ' };'); //============================================================ diff --git a/python/jsbeautifier/__init__.py b/python/jsbeautifier/__init__.py index 6951ed5ad..02d617747 100644 --- a/python/jsbeautifier/__init__.py +++ b/python/jsbeautifier/__init__.py @@ -997,7 +997,8 @@ def handle_word(self, current_token): self.allow_wrap_or_preserved_newline(current_token) if current_token.type == 'TK_RESERVED' and current_token.text == 'function': - if self.flags.last_text in ['}', ';'] or (self.output.just_added_newline() and not self.flags.last_text in ['(', '[', '{', ':', '=', ',']): + if (self.flags.last_text in ['}', ';'] or + (self.output.just_added_newline() and not (self.flags.last_text in ['(', '[', '{', ':', '=', ','] or self.last_type == 'TK_OPERATOR'))): # make sure there is a nice clean space of at least one blank line # before a new function definition, except in arrays if not self.output.just_added_blankline() and len(current_token.comments_before) == 0: diff --git a/python/jsbeautifier/tests/generated/tests.py b/python/jsbeautifier/tests/generated/tests.py index 62f8252dd..ed04657de 100644 --- a/python/jsbeautifier/tests/generated/tests.py +++ b/python/jsbeautifier/tests/generated/tests.py @@ -2523,6 +2523,15 @@ def unicode_char(value): ' console.log(i);\n' + '// all done\n' + 'console.log("done");') + + # Issue #1085 - function should not have blank line in a number of cases + bt( + 'var transformer =\n' + + ' options.transformer ||\n' + + ' globalSettings.transformer ||\n' + + ' function(x) {\n' + + ' return x;\n' + + ' };') #============================================================ diff --git a/test/data/javascript/tests.js b/test/data/javascript/tests.js index 0f2ae804b..524a6bfbb 100644 --- a/test/data/javascript/tests.js +++ b/test/data/javascript/tests.js @@ -2400,6 +2400,17 @@ exports.test_data = { '// all done', 'console.log("done");' ] + }, + { + comment: "Issue #1085 - function should not have blank line in a number of cases", + unchanged: [ + 'var transformer =', + ' options.transformer ||', + ' globalSettings.transformer ||', + ' function(x) {', + ' return x;', + ' };' + ] } ] }, {