From 21fac820c7a2605b674e69e9dca1a8500d4b63b0 Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Thu, 22 Dec 2016 22:56:50 -0800 Subject: [PATCH] Fixed unbraced if with comments Fixes #1079 --- js/lib/beautify.js | 20 ++++++++++++++++--- .../generated/beautify-javascript-tests.js | 9 +++++++++ python/jsbeautifier/__init__.py | 10 ++++++++-- python/jsbeautifier/tests/generated/tests.py | 9 +++++++++ test/data/javascript/tests.js | 11 ++++++++++ 5 files changed, 54 insertions(+), 5 deletions(-) diff --git a/js/lib/beautify.js b/js/lib/beautify.js index 56b53dcf8..bed34dfd1 100644 --- a/js/lib/beautify.js +++ b/js/lib/beautify.js @@ -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(); } } @@ -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(); } @@ -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 || ''; diff --git a/js/test/generated/beautify-javascript-tests.js b/js/test/generated/beautify-javascript-tests.js index 73794f510..b012125bc 100644 --- a/js/test/generated/beautify-javascript-tests.js +++ b/js/test/generated/beautify-javascript-tests.js @@ -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");'); //============================================================ diff --git a/python/jsbeautifier/__init__.py b/python/jsbeautifier/__init__.py index c985c2370..1144cc1b9 100644 --- a/python/jsbeautifier/__init__.py +++ b/python/jsbeautifier/__init__.py @@ -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): @@ -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: diff --git a/python/jsbeautifier/tests/generated/tests.py b/python/jsbeautifier/tests/generated/tests.py index c57a2b102..b2fed720a 100644 --- a/python/jsbeautifier/tests/generated/tests.py +++ b/python/jsbeautifier/tests/generated/tests.py @@ -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");') #============================================================ diff --git a/test/data/javascript/tests.js b/test/data/javascript/tests.js index e35635139..304dbf8c6 100644 --- a/test/data/javascript/tests.js +++ b/test/data/javascript/tests.js @@ -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");' + ] } ] }, {