fix for "} else if {" division into separate lines

This commit is contained in:
Einars Lielmanis 2007-10-17 02:09:59 +00:00
parent 1464f9a353
commit 2edc45823d
2 changed files with 10 additions and 2 deletions

View File

@ -89,6 +89,7 @@ bt('return(1)', 'return (1)');
bt('try{a();}catch(b){c();}finally{d();}', "try {\n a();\n} catch(b) {\n c();\n} finally {\n d();\n}");
bt('(xx)()'); // magic function call
bt('a[1]()'); // another magic function call
bt('if(a){b();}else if(', "if (a) {\n b();\n} else if (");
// known problems:
# bt('if(a)if(b)break', "if (a)\n if (b)\n break"); // won't fix, at least now

View File

@ -148,7 +148,9 @@ function js_beautify($js_source_text, $tab_size = 4)
} elseif ($last_type == TK_END_COMMAND && $in == IN_EXPR) {
$prefix = PRINT_SPACE;
} elseif ($last_type == TK_WORD) {
$prefix = PRINT_SPACE;
if ($last_word != 'else') { // else if
$prefix = PRINT_SPACE;
}
} elseif ($last_type == TK_START_BLOCK) {
$prefix = PRINT_NL;
} elseif ($last_type == TK_END_EXPR) {
@ -160,7 +162,12 @@ function js_beautify($js_source_text, $tab_size = 4)
if (in_array($token_text, $line_starters) or $prefix == PRINT_NL) {
if ($last_type != TK_END_EXPR) {
if ($last_type != TK_START_EXPR or $token_text != 'var') { // no need to force newline on 'var': for (var x = 0...)
nl();
if ($token_text == 'if' and $last_type == TK_WORD and $last_word == 'else') {
// no newline for } else if {
space();
} else {
nl();
}
}
}
} elseif ($prefix == PRINT_SPACE) {