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
This commit is contained in:
Muhammad Hammad 2022-11-17 18:43:39 -05:00
parent 49cd14de2f
commit c90470061c
4 changed files with 30 additions and 3 deletions

View File

@ -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;

View File

@ -831,7 +831,7 @@ class Beautifier:
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()

View File

@ -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 + ' {};',

View File

@ -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: [