diff --git a/js/lib/beautify.js b/js/lib/beautify.js index 22d78138..059d9eb6 100644 --- a/js/lib/beautify.js +++ b/js/lib/beautify.js @@ -1429,42 +1429,31 @@ function handle_block_comment() { var lines = split_newlines(token_text); var j; // iterator for this case + var javadoc = false; - if (all_lines_start_with(lines.slice(1), '*')) { - // javadoc: reformat and reindent - print_newline(false, true); - print_token(lines[0]); - for (j = 1; j < lines.length; j++) { - print_newline(false, true); - print_token(' ' + trim(lines[j])); + // block comment starts with a new line + print_newline(false, true); + if (lines.length > 1) { + if (all_lines_start_with(lines.slice(1), '*')) { + javadoc = true; } - - } else { - - // simple block comment: leave intact - if (lines.length > 1) { - // multiline comment block starts with a new line - print_newline(false, true); - } else { - // single-line /* comment */ stays where it is - if (last_type === 'TK_END_BLOCK') { - print_newline(false, true); - } else { - output_space_before_token = true; - } - - } - - print_token(lines[0]); - output.push("\n"); - for (j = 1; j < lines.length; j++) { - output.push(lines[j]); - output.push("\n"); - } - } - if (!is_next('\n')) { + // first line always indented + print_token(lines[0]); + for (j = 1; j < lines.length; j++) { + print_newline(false, true); + if (javadoc) { + // javadoc: reformat and re-indent + print_token(' ' + trim(lines[j])); + } else { + // normal comments output raw + output.push(lines[j]); + } + } + + // for comments of more than one line, make sure there's a new line after + if (lines.length > 1 && !is_next('\n')) { print_newline(false, true); } } diff --git a/js/test/beautify-tests.js b/js/test/beautify-tests.js index e59ede64..c5b0a2a0 100755 --- a/js/test/beautify-tests.js +++ b/js/test/beautify-tests.js @@ -511,6 +511,23 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify) test_fragment('new function'); bt("foo({\n 'a': 1\n},\n10);", "foo(\n {\n 'a': 1\n },\n 10);"); + bt( "test(\n" + + "/*Argument 1*/ {\n" + + " 'Value1': '1'\n" + + "},\n" + + "/*Argument 2*/ {\n" + + " 'Value2': '2'\n" + + "});", + // expected + "test(\n" + + " /*Argument 1*/\n" + + " {\n" + + " 'Value1': '1'\n" + + " },\n" + + " /*Argument 2*/\n" + + " {\n" + + " 'Value2': '2'\n" + + " });"); opts.brace_style = 'collapse'; @@ -549,6 +566,21 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify) test_fragment('new function'); bt("foo({\n 'a': 1\n},\n10);", "foo({\n 'a': 1\n },\n 10);"); + bt( "test(\n" + + "/*Argument 1*/ {\n" + + " 'Value1': '1'\n" + + "},\n" + + "/*Argument 2*/ {\n" + + " 'Value2': '2'\n" + + "});", + // expected + "test(\n" + + " /*Argument 1*/ {\n" + + " 'Value1': '1'\n" + + " },\n" + + " /*Argument 2*/ {\n" + + " 'Value2': '2'\n" + + " });"); opts.brace_style = "end-expand"; @@ -587,6 +619,21 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify) test_fragment('new function'); bt("foo({\n 'a': 1\n},\n10);", "foo({\n 'a': 1\n },\n 10);"); + bt( "test(\n" + + "/*Argument 1*/ {\n" + + " 'Value1': '1'\n" + + "},\n" + + "/*Argument 2*/ {\n" + + " 'Value2': '2'\n" + + "});", + // expected + "test(\n" + + " /*Argument 1*/ {\n" + + " 'Value1': '1'\n" + + " },\n" + + " /*Argument 2*/ {\n" + + " 'Value2': '2'\n" + + " });"); opts.brace_style = 'collapse'; diff --git a/python/jsbeautifier/__init__.py b/python/jsbeautifier/__init__.py index 33415df9..dc8de0c3 100644 --- a/python/jsbeautifier/__init__.py +++ b/python/jsbeautifier/__init__.py @@ -1238,34 +1238,30 @@ class Beautifier: def handle_block_comment(self, token_text): - lines = token_text.replace('\x0d', '').split('\x0a') - # all lines start with an asterisk? that's a proper box comment - if not any(l for l in lines[1:] if ( l.strip() == '' or (l.lstrip())[0] != '*')): + javadoc = False + + # block comment starts with a new line + self.append_newline(preserve_statement_flags = True) + if len(lines) > 1: + if not any(l for l in lines[1:] if ( l.strip() == '' or (l.lstrip())[0] != '*')): + javadoc = True + + # first line always indented + self.append_token(lines[0]) + for line in lines[1:]: self.append_newline(preserve_statement_flags = True) - self.append_token(lines[0]) - for line in lines[1:]: - self.append_newline(preserve_statement_flags = True) + if javadoc: + # javadoc: reformat and re-indent self.append_token(' ' + line.strip()) - else: - # simple block comment: leave intact - if len(lines) > 1: - # multiline comment starts on a new line - self.append_newline(preserve_statement_flags = True) else: - # single line /* ... */ comment stays on the same line - self.output_space_before_token = True - - self.append_token(lines[0]) - self.output.append('\n') - for line in lines[1:]: + # normal comments output raw self.output.append(line) - self.output.append('\n') - if not self.is_next('\n'): + # for comments of more than one line, make sure there's a new line after + if len(lines) > 1 and not self.is_next('\n'): self.append_newline(preserve_statement_flags = True) - def handle_inline_comment(self, token_text): self.output_space_before_token = True self.append_token(token_text) diff --git a/python/jsbeautifier/tests/testjsbeautifier.py b/python/jsbeautifier/tests/testjsbeautifier.py index 001ccfa2..6652be1e 100644 --- a/python/jsbeautifier/tests/testjsbeautifier.py +++ b/python/jsbeautifier/tests/testjsbeautifier.py @@ -465,6 +465,23 @@ class TestJSBeautifier(unittest.TestCase): test_fragment('new function'); bt("foo({\n 'a': 1\n},\n10);", "foo(\n {\n 'a': 1\n },\n 10);"); + bt( "test(\n" + + "/*Argument 1*/ {\n" + + " 'Value1': '1'\n" + + "},\n" + + "/*Argument 2*/ {\n" + + " 'Value2': '2'\n" + + "});", + # expected + "test(\n" + + " /*Argument 1*/\n" + + " {\n" + + " 'Value1': '1'\n" + + " },\n" + + " /*Argument 2*/\n" + + " {\n" + + " 'Value2': '2'\n" + + " });"); self.options.brace_style = 'collapse'; @@ -503,6 +520,21 @@ class TestJSBeautifier(unittest.TestCase): test_fragment('new function'); bt("foo({\n 'a': 1\n},\n10);", "foo({\n 'a': 1\n },\n 10);"); + bt( "test(\n" + + "/*Argument 1*/ {\n" + + " 'Value1': '1'\n" + + "},\n" + + "/*Argument 2*/ {\n" + + " 'Value2': '2'\n" + + "});", + # expected + "test(\n" + + " /*Argument 1*/ {\n" + + " 'Value1': '1'\n" + + " },\n" + + " /*Argument 2*/ {\n" + + " 'Value2': '2'\n" + + " });"); self.options.brace_style = "end-expand"; @@ -541,6 +573,21 @@ class TestJSBeautifier(unittest.TestCase): test_fragment('new function'); bt("foo({\n 'a': 1\n},\n10);", "foo({\n 'a': 1\n },\n 10);"); + bt( "test(\n" + + "/*Argument 1*/ {\n" + + " 'Value1': '1'\n" + + "},\n" + + "/*Argument 2*/ {\n" + + " 'Value2': '2'\n" + + "});", + # expected + "test(\n" + + " /*Argument 1*/ {\n" + + " 'Value1': '1'\n" + + " },\n" + + " /*Argument 2*/ {\n" + + " 'Value2': '2'\n" + + " });"); self.options.brace_style = 'collapse';