Handle another if-else scenario with braces

Fixes #311
This commit is contained in:
Liam Newman 2014-09-29 12:09:54 -07:00
parent b770af0123
commit 5b2736f096
4 changed files with 28 additions and 2 deletions

View File

@ -919,7 +919,7 @@
} else if (last_type !== 'TK_END_EXPR') {
if ((last_type !== 'TK_START_EXPR' || !(current_token.type === 'TK_RESERVED' && in_array(current_token.text, ['var', 'let', 'const']))) && flags.last_text !== ':') {
// no need to force newline on 'var': for (var x = 0...)
if (current_token.type === 'TK_RESERVED' && current_token.text === 'if' && flags.last_word === 'else' && flags.last_text !== '{') {
if (current_token.type === 'TK_RESERVED' && current_token.text === 'if' && flags.last_text === 'else') {
// no newline for } else if {
output.space_before_token = true;
} else {

View File

@ -1732,6 +1732,19 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
'};');
// END tests for issue 440
// START tests for issue 311
// if-else with braces edge case
bt('if(x){a();}else{b();}if(y){c();}',
'if (x) {\n' +
' a();\n' +
'} else {\n' +
' b();\n' +
'}\n' +
'if (y) {\n' +
' c();\n' +
'}');
// END tests for issue 311
// START tests for issue 485
// ensure function declarations behave the same in arrays as elsewhere
bt( 'var v = ["a",\n' +

View File

@ -861,7 +861,7 @@ class Beautifier:
if (self.last_type != 'TK_START_EXPR' or not (current_token.type == 'TK_RESERVED' and current_token.text in ['var', 'let', 'const'])) and self.flags.last_text != ':':
# no need to force newline on VAR -
# for (var x = 0...
if current_token.type == 'TK_RESERVED' and current_token.text == 'if' and self.flags.last_word == 'else' and self.flags.last_text != '{':
if current_token.type == 'TK_RESERVED' and current_token.text == 'if' and self.flags.last_text == 'else':
self.output.space_before_token = True
else:
self.print_newline()

View File

@ -1602,6 +1602,19 @@ class TestJSBeautifier(unittest.TestCase):
'};');
# END tests for issue 440
# START tests for issue 311
# if-else with braces edge case
bt('if(x){a();}else{b();}if(y){c();}',
'if (x) {\n' +
' a();\n' +
'} else {\n' +
' b();\n' +
'}\n' +
'if (y) {\n' +
' c();\n' +
'}');
# END tests for issue 311
# START tests for issue 485
# ensure function declarations behave the same in arrays as elsewhere
bt( 'var v = ["a",\n' +