Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix space-before-conditional: false to work on switch-case statement #1881

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()' },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer this to be a valid switch/case statement, but the formatting works so... okay.

{
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