Skip to content

Commit

Permalink
Fixed function declaration with wrong blank line after operator
Browse files Browse the repository at this point in the history
Fixes #1085
  • Loading branch information
bitwiseman committed Dec 30, 2016
1 parent 3e52b98 commit 6d0ad2b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
3 changes: 2 additions & 1 deletion js/lib/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions js/test/generated/beautify-javascript-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' +
' };');


//============================================================
Expand Down
3 changes: 2 additions & 1 deletion python/jsbeautifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions python/jsbeautifier/tests/generated/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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' +
' };')


#============================================================
Expand Down
11 changes: 11 additions & 0 deletions test/data/javascript/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;',
' };'
]
}
]
}, {
Expand Down

0 comments on commit 6d0ad2b

Please sign in to comment.