Skip to content

Commit

Permalink
Merge pull request #1943 from pr0grammm/fix-switch-case-indent
Browse files Browse the repository at this point in the history
issue #1683: fix switch case indentation
  • Loading branch information
bitwiseman authored Jun 23, 2021
2 parents db64613 + 6ca0918 commit 3e51e63
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
5 changes: 4 additions & 1 deletion js/src/javascript/beautifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ Beautifier.prototype.create_flags = function(flags_base, mode) {
in_case_statement: false, // switch(..){ INSIDE HERE }
in_case: false, // we're on the exact line with "case 0:"
case_body: false, // the indented case-action block
case_block: false, // the indented case-action block is wrapped with {}
indentation_level: next_indent_level,
alignment: 0,
line_indent_level: flags_base ? flags_base.line_indent_level : next_indent_level,
Expand Down Expand Up @@ -869,7 +870,7 @@ Beautifier.prototype.handle_word = function(current_token) {

if (this._flags.in_case_statement && reserved_array(current_token, ['case', 'default'])) {
this.print_newline();
if (this._flags.last_token.type !== TOKEN.END_BLOCK && (this._flags.case_body || this._options.jslint_happy)) {
if (!this._flags.case_block && (this._flags.case_body || this._options.jslint_happy)) {
// switch cases following one another
this.deindent();
}
Expand Down Expand Up @@ -1183,7 +1184,9 @@ Beautifier.prototype.handle_operator = function(current_token) {
if (this._tokens.peek().type !== TOKEN.START_BLOCK) {
this.indent();
this.print_newline();
this._flags.case_block = false;
} else {
this._flags.case_block = true;
this._output.space_before_token = true;
}
return;
Expand Down
5 changes: 4 additions & 1 deletion python/jsbeautifier/javascript/beautifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def __init__(self, mode):
self.in_case = False
self.in_case_statement = False
self.case_body = False
self.case_block = False
self.indentation_level = 0
self.alignment = 0
self.line_indent_level = 0
Expand Down Expand Up @@ -945,7 +946,7 @@ def handle_word(self, current_token):
current_token, ["case", "default"]
):
self.print_newline()
if self._flags.last_token.type != TOKEN.END_BLOCK and (
if (not self._flags.case_block) and (
self._flags.case_body or self._options.jslint_happy
):
self.deindent()
Expand Down Expand Up @@ -1331,8 +1332,10 @@ def handle_operator(self, current_token):
if self._tokens.peek().type != TOKEN.START_BLOCK:
self.indent()
self.print_newline()
self._flags.case_block = False
else:
self._output.space_before_token = True
self._flags.case_block = True

return

Expand Down
16 changes: 16 additions & 0 deletions test/data/javascript/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3443,6 +3443,22 @@ exports.test_data = {
'}'
]
},
{
comment: "Issue #1683 - switch-case wrong indentation",
input: 'switch (x) { case 0: if (y == z) { a(); } else { b(); } case 1: c(); }',
output: [
'switch (x) {',
' case 0:',
' if (y == z) {',
' a();',
' } else {',
' b();',
' }',
' case 1:',
' c();',
'}'
]
},
{
comment: "Issue 485 - ensure function declarations behave the same in arrays as elsewhere",
unchanged: [
Expand Down

0 comments on commit 3e51e63

Please sign in to comment.