Skip to content

Commit

Permalink
Do not repeat comments with indented empty line
Browse files Browse the repository at this point in the history
Fixes #1663
  • Loading branch information
bitwiseman committed Apr 29, 2019
1 parent 0cea554 commit caac81e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
19 changes: 13 additions & 6 deletions js/src/javascript/beautifier.js
Expand Up @@ -400,7 +400,7 @@ Beautifier.prototype.print_token_line_indentation = function(current_token) {
}
};

Beautifier.prototype.print_token = function(current_token, printable_token) {
Beautifier.prototype.print_token = function(current_token) {
if (this._output.raw) {
this._output.add_raw_token(current_token);
return;
Expand All @@ -426,10 +426,9 @@ Beautifier.prototype.print_token = function(current_token, printable_token) {
}
}

printable_token = printable_token || current_token.text;
this.print_token_line_indentation(current_token);
this._output.non_breaking_space = true;
this._output.add_token(printable_token);
this._output.add_token(current_token.text);
if (this._output.previous_token_wrapped) {
this._flags.multiline_frame = true;
}
Expand Down Expand Up @@ -1337,8 +1336,12 @@ Beautifier.prototype.handle_block_comment = function(current_token, preserve_sta
this.print_token(current_token);
this._output.space_before_token = true;
return;
} else {
this.print_block_commment(current_token, preserve_statement_flags);
}
};

Beautifier.prototype.print_block_commment = function(current_token, preserve_statement_flags) {
var lines = split_linebreaks(current_token.text);
var j; // iterator for this case
var javadoc = false;
Expand All @@ -1350,7 +1353,8 @@ Beautifier.prototype.handle_block_comment = function(current_token, preserve_sta
this.print_newline(false, preserve_statement_flags);

// first line always indented
this.print_token(current_token, lines[0]);
this.print_token_line_indentation(current_token);
this._output.add_token(lines[0]);
this.print_newline(false, preserve_statement_flags);


Expand All @@ -1366,10 +1370,12 @@ Beautifier.prototype.handle_block_comment = function(current_token, preserve_sta
for (j = 0; j < lines.length; j++) {
if (javadoc) {
// javadoc: reformat and re-indent
this.print_token(current_token, ltrim(lines[j]));
this.print_token_line_indentation(current_token);
this._output.add_token(ltrim(lines[j]));
} else if (starless && lines[j]) {
// starless: re-indent non-empty content, avoiding trim
this.print_token(current_token, lines[j].substring(lastIndentLength));
this.print_token_line_indentation(current_token);
this._output.add_token(lines[j].substring(lastIndentLength));
} else {
// normal comments output raw
this._output.current_line.set_indent(-1);
Expand All @@ -1384,6 +1390,7 @@ Beautifier.prototype.handle_block_comment = function(current_token, preserve_sta
}
};


Beautifier.prototype.handle_comment = function(current_token, preserve_statement_flags) {
if (current_token.newlines) {
this.print_newline(false, preserve_statement_flags);
Expand Down
8 changes: 8 additions & 0 deletions js/test/generated/beautify-javascript-tests.js
Expand Up @@ -5737,6 +5737,14 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
' bar: 2\n' +
' });\n' +
'var test = 1;');

// Issue #1663
bt(
'{\n' +
' /* howdy\n' +
' \n' +
' */\n' +
'}');
bt(
'obj\n' +
' .last(a, function() {\n' +
Expand Down
8 changes: 8 additions & 0 deletions python/jsbeautifier/tests/generated/tests.py
Expand Up @@ -5471,6 +5471,14 @@ def unicode_char(value):
' bar: 2\n' +
' });\n' +
'var test = 1;')

# Issue #1663
bt(
'{\n' +
' /* howdy\n' +
' \n' +
' */\n' +
'}')
bt(
'obj\n' +
' .last(a, function() {\n' +
Expand Down
9 changes: 9 additions & 0 deletions test/data/javascript/tests.js
Expand Up @@ -3136,6 +3136,15 @@ exports.test_data = {
' });',
'var test = 1;'
]
}, {
comment: "Issue #1663",
unchanged: [
'{',
' /* howdy',
' ',
' */',
'}'
]
},
{
unchanged: [
Expand Down

0 comments on commit caac81e

Please sign in to comment.