From c90470061c775a42b9c7a99840d2a5d436a89407 Mon Sep 17 00:00:00 2001 From: Muhammad Hammad Date: Thu, 17 Nov 2022 18:43:39 -0500 Subject: [PATCH] fix - semicolon followed by block statement doesnt have new line This change makes sure that a new line is added everytime a semicolon is followed by a block statement fixes #1852 --- js/src/javascript/beautifier.js | 2 +- python/jsbeautifier/javascript/beautifier.py | 2 +- test/data/javascript/node.mustache | 2 +- test/data/javascript/tests.js | 27 ++++++++++++++++++++ 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/js/src/javascript/beautifier.js b/js/src/javascript/beautifier.js index 51cd10d8e..dfec38351 100644 --- a/js/src/javascript/beautifier.js +++ b/js/src/javascript/beautifier.js @@ -765,7 +765,7 @@ Beautifier.prototype.handle_start_block = function(current_token) { } } if (this._flags.last_token.type !== TOKEN.OPERATOR && this._flags.last_token.type !== TOKEN.START_EXPR) { - if (this._flags.last_token.type === TOKEN.START_BLOCK && !this._flags.inline_frame) { + if (in_array(this._flags.last_token.type, [TOKEN.START_BLOCK, TOKEN.SEMICOLON]) && !this._flags.inline_frame) { this.print_newline(); } else { this._output.space_before_token = true; diff --git a/python/jsbeautifier/javascript/beautifier.py b/python/jsbeautifier/javascript/beautifier.py index efeb584b4..69f2dbdaf 100644 --- a/python/jsbeautifier/javascript/beautifier.py +++ b/python/jsbeautifier/javascript/beautifier.py @@ -831,7 +831,7 @@ def handle_start_block(self, current_token): elif self._flags.last_token.type not in [TOKEN.OPERATOR, TOKEN.START_EXPR]: if ( - self._flags.last_token.type == TOKEN.START_BLOCK + self._flags.last_token.type in [TOKEN.START_BLOCK, TOKEN.SEMICOLON] and not self._flags.inline_frame ): self.print_newline() diff --git a/test/data/javascript/node.mustache b/test/data/javascript/node.mustache index 0681e91f2..775d849f3 100644 --- a/test/data/javascript/node.mustache +++ b/test/data/javascript/node.mustache @@ -192,7 +192,7 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify, 'if (foo)' + eo + '{}' + ec + 'else /regex/.test();'); test_fragment('if (foo)' + to + '{', 'if (foo)' + eo + '{'); test_fragment('foo' + to + '{', 'foo' + eo + '{'); - test_fragment('return;' + to + '{', 'return;' + eo + '{'); + test_fragment('return;' + to + '{', 'return;\n{'); bt( 'function x()' + to + '{\n foo();\n}zzz', 'function x()' + eo +'{\n foo();\n}\nzzz'); bt( 'var a = new function a()' + to + '{};', 'var a = new function a()' + eo + '{};'); bt( 'var a = new function a()' + to + ' {},\n b = new function b()' + to + ' {};', diff --git a/test/data/javascript/tests.js b/test/data/javascript/tests.js index 367c3e231..d9e6c41f2 100644 --- a/test/data/javascript/tests.js +++ b/test/data/javascript/tests.js @@ -3367,6 +3367,33 @@ exports.test_data = { ' });', 'var test = 1;' ] + }, { + comment: "Issue #1852 - semicolon followed by block statement", + unchanged: [ + '(function() {', + ' some_code_here();', + ' {', + ' /* IE11 let bug bypass */', + ' let index;', + ' for (index in a) {', + ' a[index];', + ' }', + ' }', + '})();' + ] + }, { + comment: "Issue #1852 - semicolon followed by block statement 2", + input: [ + 'let x = { A: 1 }; { console.log("hello"); }' + ], + output: [ + 'let x = {', + ' A: 1', + '};', + '{', + ' console.log("hello");', + '}' + ] }, { comment: "Issue #772", input: [