diff --git a/beautify.js b/beautify.js index 9ff980cde..22b75a4b4 100644 --- a/beautify.js +++ b/beautify.js @@ -90,7 +90,6 @@ function js_beautify(js_source_text, options) { opt_jslint_happy = options.jslint_happy === 'undefined' ? false : options.jslint_happy, opt_keep_array_indentation = typeof options.keep_array_indentation === 'undefined' ? false : options.keep_array_indentation, opt_space_before_conditional = typeof options.space_before_conditional === 'undefined' ? true : options.space_before_conditional, - opt_indent_case = typeof options.indent_case === 'undefined' ? false : options.indent_case, opt_unescape_strings = typeof options.unescape_strings === 'undefined' ? false : options.unescape_strings; just_added_newline = false; @@ -172,9 +171,6 @@ function js_beautify(js_source_text, options) { if (flags.var_line && flags.var_line_reindented) { output.push(indent_string); // skip space-stuffing, if indenting with a tab } - if (flags.case_body) { - output.push(indent_string); - } } @@ -233,7 +229,7 @@ function js_beautify(js_source_text, options) { case_body: false, // the indented case-action block eat_next_space: false, indentation_baseline: -1, - indentation_level: (flags ? flags.indentation_level + (flags.case_body ? 1 : 0) + ((flags.var_line && flags.var_line_reindented) ? 1 : 0) : 0), + indentation_level: (flags ? flags.indentation_level + ((flags.var_line && flags.var_line_reindented) ? 1 : 0) : 0), ternary_depth: 0 }; } @@ -979,23 +975,16 @@ function js_beautify(js_source_text, options) { } if (token_text === 'case' || (token_text === 'default' && flags.in_case_statement)) { - if (last_text === ':' || flags.case_body) { + print_newline(); + if (flags.case_body) { // switch cases following one another + flags.indentation_level--; + flags.case_body = false; remove_indent(); - } else { - // case statement starts in the same line where switch - if (!opt_indent_case) { - flags.indentation_level--; - } - print_newline(); - if (!opt_indent_case) { - flags.indentation_level++; - } } print_token(); flags.in_case = true; flags.in_case_statement = true; - flags.case_body = false; break; } @@ -1190,10 +1179,9 @@ function js_beautify(js_source_text, options) { } if (token_text === ':' && flags.in_case) { - if (opt_indent_case) { - flags.case_body = true; - } - print_token(); // colon really asks for separate treatment + flags.case_body = true; + indent(); + print_token(); print_newline(); flags.in_case = false; break; diff --git a/python/jsbeautifier/__init__.py b/python/jsbeautifier/__init__.py index 145dfc2e2..8ec98ff11 100644 --- a/python/jsbeautifier/__init__.py +++ b/python/jsbeautifier/__init__.py @@ -84,6 +84,7 @@ def __init__(self, mode): self.chain_extra_indentation = 0 self.in_case = False self.in_case_statement = False + self.case_body = False self.eat_next_space = False self.indentation_baseline = -1 self.indentation_level = 0 @@ -853,12 +854,11 @@ def handle_word(self, token_text): return if token_text == 'case' or (token_text == 'default' and self.flags.in_case_statement): - if self.last_text == ':': - self.remove_indent() - else: - self.flags.indentation_level -= 1 - self.append_newline() - self.flags.indentation_level += 1 + self.append_newline() + if self.flags.case_body: + self.remove_indent(); + self.flags.case_body = False + self.flags.indentation_level -= 1; self.append(token_text) self.flags.in_case = True self.flags.in_case_statement = True @@ -1036,6 +1036,8 @@ def handle_operator(self, token_text): if token_text == ':' and self.flags.in_case: + self.flags.case_body = True + self.indent(); self.append(token_text) self.append_newline() self.flags.in_case = False diff --git a/python/jsbeautifier/tests/testjsbeautifier.py b/python/jsbeautifier/tests/testjsbeautifier.py index 3c533216a..e2f9eecbd 100644 --- a/python/jsbeautifier/tests/testjsbeautifier.py +++ b/python/jsbeautifier/tests/testjsbeautifier.py @@ -99,8 +99,8 @@ def test_beautifier(self): bt('(xx)()'); # magic function call bt('a[1]()'); # another magic function call bt('if(a){b();}else if(c) foo();', "if (a) {\n b();\n} else if (c) foo();"); - bt('switch(x) {case 0: case 1: a(); break; default: break}', "switch (x) {\ncase 0:\ncase 1:\n a();\n break;\ndefault:\n break\n}"); - bt('switch(x){case -1:break;case !y:break;}', 'switch (x) {\ncase -1:\n break;\ncase !y:\n break;\n}'); + bt('switch(x) {case 0: case 1: a(); break; default: break}', "switch (x) {\n case 0:\n case 1:\n a();\n break;\n default:\n break\n}"); + bt('switch(x){case -1:break;case !y:break;}', 'switch (x) {\n case -1:\n break;\n case !y:\n break;\n}'); bt('a !== b'); bt('if (a) b(); else c();', "if (a) b();\nelse c();"); bt("// comment\n(function something() {})"); # typical greasemonkey start @@ -436,9 +436,9 @@ def test_beautifier(self): bt('