Skip to content

Commit

Permalink
Merge pull request #1881 from raobhavya92/Issue-1858-switch-case-spacing
Browse files Browse the repository at this point in the history
Issue #1858- Space-before-conditional: false doesn't work on switch case statement
  • Loading branch information
bitwiseman authored Jan 25, 2021
2 parents 854ce35 + 0074cef commit 4738a1d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion js/src/javascript/beautifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ Beautifier.prototype.handle_start_expr = function(current_token) {
if (this._flags.last_token.text === 'for') {
this._output.space_before_token = this._options.space_before_conditional;
next_mode = MODE.ForInitializer;
} else if (in_array(this._flags.last_token.text, ['if', 'while'])) {
} else if (in_array(this._flags.last_token.text, ['if', 'while', 'switch'])) {
this._output.space_before_token = this._options.space_before_conditional;
next_mode = MODE.Conditional;
} else if (in_array(this._flags.last_word, ['await', 'async'])) {
Expand Down
14 changes: 14 additions & 0 deletions js/test/generated/beautify-javascript-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4481,6 +4481,7 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
'do\n' +
' c();\n' +
'while(a) b()');
bt('switch(a) b()');
bt(
'if(a)\n' +
'b();',
Expand All @@ -4501,6 +4502,12 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
'do\n' +
' c();\n' +
'while(a);');
bt(
'switch(a)\n' +
'b()',
// -- output --
'switch(a)\n' +
' b()');
bt('return [];');
bt('return ();');

Expand All @@ -4514,6 +4521,7 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
'do\n' +
' c();\n' +
'while (a) b()');
bt('switch (a) b()');
bt(
'if(a)\n' +
'b();',
Expand All @@ -4534,6 +4542,12 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
'do\n' +
' c();\n' +
'while (a);');
bt(
'switch(a)\n' +
'b()',
// -- output --
'switch (a)\n' +
' b()');
bt('return [];');
bt('return ();');

Expand Down
2 changes: 1 addition & 1 deletion python/jsbeautifier/javascript/beautifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ def handle_start_expr(self, current_token):
self._options.space_before_conditional
)
next_mode = MODE.ForInitializer
elif self._flags.last_token.text in ["if", "while"]:
elif self._flags.last_token.text in ["if", "while", "switch"]:
self._output.space_before_token = (
self._options.space_before_conditional
)
Expand Down
14 changes: 14 additions & 0 deletions python/jsbeautifier/tests/generated/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4227,6 +4227,7 @@ def unicode_char(value):
'do\n' +
' c();\n' +
'while(a) b()')
bt('switch(a) b()')
bt(
'if(a)\n' +
'b();',
Expand All @@ -4247,6 +4248,12 @@ def unicode_char(value):
'do\n' +
' c();\n' +
'while(a);')
bt(
'switch(a)\n' +
'b()',
# -- output --
'switch(a)\n' +
' b()')
bt('return [];')
bt('return ();')

Expand All @@ -4259,6 +4266,7 @@ def unicode_char(value):
'do\n' +
' c();\n' +
'while (a) b()')
bt('switch (a) b()')
bt(
'if(a)\n' +
'b();',
Expand All @@ -4279,6 +4287,12 @@ def unicode_char(value):
'do\n' +
' c();\n' +
'while (a);')
bt(
'switch(a)\n' +
'b()',
# -- output --
'switch (a)\n' +
' b()')
bt('return [];')
bt('return ();')

Expand Down
5 changes: 5 additions & 0 deletions test/data/javascript/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2576,6 +2576,7 @@ exports.test_data = {
{ unchanged: 'if{{s}}(a) b()' },
{ unchanged: 'while{{s}}(a) b()' },
{ unchanged: 'do\n c();\nwhile{{s}}(a) b()' },
{ unchanged: 'switch{{s}}(a) b()' },
{
input: 'if(a)\nb();',
output: 'if{{s}}(a)\n b();'
Expand All @@ -2588,6 +2589,10 @@ exports.test_data = {
input: 'do\nc();\nwhile(a);',
output: 'do\n c();\nwhile{{s}}(a);'
},
{
input: 'switch(a)\nb()',
output: 'switch{{s}}(a)\n b()'
},
{ unchanged: 'return [];' },
{ unchanged: 'return ();' }
]
Expand Down

0 comments on commit 4738a1d

Please sign in to comment.