From 8c2357ddd5d4e9494edff7a14b4ad5dda7aa5d9f Mon Sep 17 00:00:00 2001 From: raobhavya92 Date: Tue, 26 Jan 2021 00:00:55 +0530 Subject: [PATCH 1/2] Issue #1858- Switch condition spacing --- js/src/javascript/beautifier.js | 2 +- js/test/generated/beautify-javascript-tests.js | 14 ++++++++++++++ python/jsbeautifier/javascript/beautifier.py | 2 +- python/jsbeautifier/tests/generated/tests.py | 14 ++++++++++++++ test/data/javascript/tests.js | 5 +++++ 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/js/src/javascript/beautifier.js b/js/src/javascript/beautifier.js index df902fc6d..2c0af1c57 100644 --- a/js/src/javascript/beautifier.js +++ b/js/src/javascript/beautifier.js @@ -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'])) { diff --git a/js/test/generated/beautify-javascript-tests.js b/js/test/generated/beautify-javascript-tests.js index 8bfb2f11b..83f360220 100644 --- a/js/test/generated/beautify-javascript-tests.js +++ b/js/test/generated/beautify-javascript-tests.js @@ -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();', @@ -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 ();'); @@ -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();', @@ -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 ();'); diff --git a/python/jsbeautifier/javascript/beautifier.py b/python/jsbeautifier/javascript/beautifier.py index 201802f27..992072645 100644 --- a/python/jsbeautifier/javascript/beautifier.py +++ b/python/jsbeautifier/javascript/beautifier.py @@ -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 ) diff --git a/python/jsbeautifier/tests/generated/tests.py b/python/jsbeautifier/tests/generated/tests.py index 3a76ea7b7..7660347d9 100644 --- a/python/jsbeautifier/tests/generated/tests.py +++ b/python/jsbeautifier/tests/generated/tests.py @@ -4227,6 +4227,7 @@ def unicode_char(value): 'do\n' + ' c();\n' + 'while(a) b()') + bt('switch(a) b()') bt( 'if(a)\n' + 'b();', @@ -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 ();') @@ -4259,6 +4266,7 @@ def unicode_char(value): 'do\n' + ' c();\n' + 'while (a) b()') + bt('switch (a) b()') bt( 'if(a)\n' + 'b();', @@ -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 ();') diff --git a/test/data/javascript/tests.js b/test/data/javascript/tests.js index 5da7ae75f..4b4bf5816 100644 --- a/test/data/javascript/tests.js +++ b/test/data/javascript/tests.js @@ -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();' @@ -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 ();' } ] From 0074cefc6a8a90f4622368bcd61bfdec4b3e9cf1 Mon Sep 17 00:00:00 2001 From: raobhavya92 Date: Tue, 26 Jan 2021 00:51:59 +0530 Subject: [PATCH 2/2] Issue#1858 - Updating styling changes --- test/data/javascript/tests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/data/javascript/tests.js b/test/data/javascript/tests.js index 4b4bf5816..8497e289c 100644 --- a/test/data/javascript/tests.js +++ b/test/data/javascript/tests.js @@ -2576,7 +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()'}, + { unchanged: 'switch{{s}}(a) b()' }, { input: 'if(a)\nb();', output: 'if{{s}}(a)\n b();'