Skip to content

Commit

Permalink
Fixed unbraced if with comments
Browse files Browse the repository at this point in the history
Fixes #1079
  • Loading branch information
bitwiseman committed Dec 23, 2016
1 parent 119403d commit 21fac82
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
20 changes: 17 additions & 3 deletions js/lib/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,10 @@ if (!Object.values) {
function print_newline(force_newline, preserve_statement_flags) {
if (!preserve_statement_flags) {
if (flags.last_text !== ';' && flags.last_text !== ',' && flags.last_text !== '=' && last_type !== 'TK_OPERATOR') {
while (flags.mode === MODE.Statement && !flags.if_block && !flags.do_block) {
var next_token = get_token(1);
while (flags.mode === MODE.Statement &&
!(flags.if_block && next_token && next_token.type === 'TK_RESERVED' && next_token.text === 'else') &&
!flags.do_block) {
restore_mode();
}
}
Expand Down Expand Up @@ -1190,7 +1193,10 @@ if (!Object.values) {
// Semicolon can be the start (and end) of a statement
output.space_before_token = false;
}
while (flags.mode === MODE.Statement && !flags.if_block && !flags.do_block) {
var next_token = get_token(1);
while (flags.mode === MODE.Statement &&
!(flags.if_block && next_token && next_token.type === 'TK_RESERVED' && next_token.text === 'else') &&
!flags.do_block) {
restore_mode();
}

Expand Down Expand Up @@ -1831,7 +1837,15 @@ if (!Object.values) {
var Token = function(type, text, newlines, whitespace_before, parent) {
this.type = type;
this.text = text;
this.comments_before = [];

// comments_before are
// comments that have a new line before them
// and may or may not have a newline after
// this is a set of comments before
this.comments_before = /* inline comment*/ [];


this.comments_after = []; // no new line before and newline after
this.newlines = newlines || 0;
this.wanted_newline = newlines > 0;
this.whitespace_before = whitespace_before || '';
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 @@ -2654,6 +2654,15 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
test_fragment(
'sd = 1;\n' +
'/');

// Issue #1079 - unbraced if with comments should still look right
bt(
'if (console.log)\n' +
' for (var i = 0; i < 20; ++i)\n' +
' if (i % 3)\n' +
' console.log(i);\n' +
'// all done\n' +
'console.log("done");');


//============================================================
Expand Down
10 changes: 8 additions & 2 deletions python/jsbeautifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,10 @@ def allow_wrap_or_preserved_newline(self, current_token, force_linewrap = False)
def print_newline(self, force_newline = False, preserve_statement_flags = False):
if not preserve_statement_flags:
if self.flags.last_text != ';' and self.flags.last_text != ',' and self.flags.last_text != '=' and self.last_type != 'TK_OPERATOR':
while self.flags.mode == MODE.Statement and not self.flags.if_block and not self.flags.do_block:
next_token = self.get_token(1)
while (self.flags.mode == MODE.Statement and
not (self.flags.if_block and next_token and next_token.type == 'TK_RESERVED' and next_token.text == 'else') and
not self.flags.do_block):
self.restore_mode()

if self.output.add_new_line(force_newline):
Expand Down Expand Up @@ -1122,7 +1125,10 @@ def handle_semicolon(self, current_token):
# The conditional starts the statement if appropriate.
# Semicolon can be the start (and end) of a statement
self.output.space_before_token = False
while self.flags.mode == MODE.Statement and not self.flags.if_block and not self.flags.do_block:
next_token = self.get_token(1)
while (self.flags.mode == MODE.Statement and
not (self.flags.if_block and next_token and next_token.type == 'TK_RESERVED' and next_token.text == 'else') and
not self.flags.do_block):
self.restore_mode()

if self.flags.import_block:
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 @@ -2482,6 +2482,15 @@ def unicode_char(value):
test_fragment(
'sd = 1;\n' +
'/')

# Issue #1079 - unbraced if with comments should still look right
bt(
'if (console.log)\n' +
' for (var i = 0; i < 20; ++i)\n' +
' if (i % 3)\n' +
' console.log(i);\n' +
'// all done\n' +
'console.log("done");')


#============================================================
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 @@ -2368,6 +2368,17 @@ exports.test_data = {
'sd = 1;',
'/'
]
},
{
comment: "Issue #1079 - unbraced if with comments should still look right",
unchanged: [
'if (console.log)',
' for (var i = 0; i < 20; ++i)',
' if (i % 3)',
' console.log(i);',
'// all done',
'console.log("done");'
]
}
]
}, {
Expand Down

0 comments on commit 21fac82

Please sign in to comment.