@@ -330,6 +330,7 @@ Beautifier.prototype.create_flags = function(flags_base, mode) {
330
330
inline_frame : false ,
331
331
if_block : false ,
332
332
else_block : false ,
333
+ class_start_block : false , // class A { INSIDE HERE } or class B extends C { INSIDE HERE }
333
334
do_block : false ,
334
335
do_while : false ,
335
336
import_block : false ,
@@ -742,6 +743,8 @@ Beautifier.prototype.handle_start_expr = function(current_token) {
742
743
( peek_back_two . text === '*' && ( peek_back_three . text === '{' || peek_back_three . text === ',' ) ) ) {
743
744
this . _output . space_before_token = true ;
744
745
}
746
+ } else if ( this . _flags . parent && this . _flags . parent . class_start_block ) {
747
+ this . _output . space_before_token = true ;
745
748
}
746
749
}
747
750
} else {
@@ -856,6 +859,12 @@ Beautifier.prototype.handle_start_block = function(current_token) {
856
859
this . set_mode ( MODE . BlockStatement ) ;
857
860
}
858
861
862
+ if ( this . _flags . last_token ) {
863
+ if ( reserved_array ( this . _flags . last_token . previous , [ 'class' , 'extends' ] ) ) {
864
+ this . _flags . class_start_block = true ;
865
+ }
866
+ }
867
+
859
868
var empty_braces = ! next_token . comments_before && next_token . text === '}' ;
860
869
var empty_anonymous_function = empty_braces && this . _flags . last_word === 'function' &&
861
870
this . _flags . last_token . type === TOKEN . END_EXPR ;
@@ -1296,13 +1305,6 @@ Beautifier.prototype.handle_operator = function(current_token) {
1296
1305
this . handle_whitespace_and_comments ( current_token , preserve_statement_flags ) ;
1297
1306
}
1298
1307
1299
- if ( reserved_array ( this . _flags . last_token , special_words ) ) {
1300
- // "return" had a special handling in TK_WORD. Now we need to return the favor
1301
- this . _output . space_before_token = true ;
1302
- this . print_token ( current_token ) ;
1303
- return ;
1304
- }
1305
-
1306
1308
// hack for actionscript's import .*;
1307
1309
if ( current_token . text === '*' && this . _flags . last_token . type === TOKEN . DOT ) {
1308
1310
this . print_token ( current_token ) ;
@@ -1430,7 +1432,11 @@ Beautifier.prototype.handle_operator = function(current_token) {
1430
1432
// http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1
1431
1433
// if there is a newline between -- or ++ and anything else we should preserve it.
1432
1434
if ( current_token . newlines && ( current_token . text === '--' || current_token . text === '++' || current_token . text === '~' ) ) {
1433
- this . print_newline ( false , true ) ;
1435
+ var new_line_needed = reserved_array ( this . _flags . last_token , special_words ) && current_token . newlines ;
1436
+ if ( new_line_needed && ( this . _previous_flags . if_block || this . _previous_flags . else_block ) ) {
1437
+ this . restore_mode ( ) ;
1438
+ }
1439
+ this . print_newline ( new_line_needed , true ) ;
1434
1440
}
1435
1441
1436
1442
if ( this . _flags . last_token . text === ';' && is_expression ( this . _flags . mode ) ) {
@@ -1570,6 +1576,10 @@ Beautifier.prototype.handle_dot = function(current_token) {
1570
1576
this . handle_whitespace_and_comments ( current_token , true ) ;
1571
1577
}
1572
1578
1579
+ if ( this . _flags . last_token . text . match ( '^[0-9]+$' ) ) {
1580
+ this . _output . space_before_token = true ;
1581
+ }
1582
+
1573
1583
if ( reserved_array ( this . _flags . last_token , special_words ) ) {
1574
1584
this . _output . space_before_token = false ;
1575
1585
} else {
@@ -2554,7 +2564,7 @@ var punct_pattern = new RegExp(punct);
2554
2564
2555
2565
// words which should always start on new line.
2556
2566
var line_starters = 'continue,try,throw,return,var,let,const,if,switch,case,default,for,while,break,function,import,export' . split ( ',' ) ;
2557
- var reserved_words = line_starters . concat ( [ 'do' , 'in' , 'of' , 'else' , 'get' , 'set' , 'new' , 'catch' , 'finally' , 'typeof' , 'yield' , 'async' , 'await' , 'from' , 'as' ] ) ;
2567
+ var reserved_words = line_starters . concat ( [ 'do' , 'in' , 'of' , 'else' , 'get' , 'set' , 'new' , 'catch' , 'finally' , 'typeof' , 'yield' , 'async' , 'await' , 'from' , 'as' , 'class' , 'extends' ] ) ;
2558
2568
var reserved_word_pattern = new RegExp ( '^(?:' + reserved_words . join ( '|' ) + ')$' ) ;
2559
2569
2560
2570
// var template_pattern = /(?:(?:<\?php|<\?=)[\s\S]*?\?>)|(?:<%[\s\S]*?%>)/g;
@@ -2645,7 +2655,8 @@ Tokenizer.prototype._read_word = function(previous_token) {
2645
2655
if ( ! ( previous_token . type === TOKEN . DOT ||
2646
2656
( previous_token . type === TOKEN . RESERVED && ( previous_token . text === 'set' || previous_token . text === 'get' ) ) ) &&
2647
2657
reserved_word_pattern . test ( resulting_string ) ) {
2648
- if ( resulting_string === 'in' || resulting_string === 'of' ) { // hack for 'in' and 'of' operators
2658
+ if ( ( resulting_string === 'in' || resulting_string === 'of' ) &&
2659
+ ( previous_token . type === TOKEN . WORD || previous_token . type === TOKEN . STRING ) ) { // hack for 'in' and 'of' operators
2649
2660
return this . _create_token ( TOKEN . OPERATOR , resulting_string ) ;
2650
2661
}
2651
2662
return this . _create_token ( TOKEN . RESERVED , resulting_string ) ;
0 commit comments